Browse Source

updated event creation

- added filename to events
 - added util for epoch to dtime with format string
 - fixed bug in which epoch times would be parsed with gmtime instead of
   localtime
main
Stefan Pranger 3 years ago
parent
commit
495f323e07
  1. 33
      ical/Event.cpp
  2. 7
      ical/Event.h
  3. 24
      util/calendar_parsing.cpp
  4. 14
      util/calendar_parsing.h

33
ical/Event.cpp

@ -11,27 +11,27 @@ namespace ical {
//TODO somewhat hacky as libical should be able to provide us with correct timezone/daylight offsets....
std::tm t = {};
std::istringstream ss(dt);
if (ss >> std::get_time(&t, "%Y%m%dT%H%M%S"))
{
if (ss >> std::get_time(&t, "%Y%m%dT%H%M%S")) {
time_t temp1 = mktime(&t);
struct tm* temp2 = gmtime(&temp1);
struct tm* temp2 = localtime(&temp1);
return mktime(temp2);
}
else
{
} else {
std::cout << "Parse failed\n";
return -1;
}
}
Event::Event(icalcomponent* event_component) {
parse(event_component);
Event::Event(icalcomponent* event_component, const std::string &file) {
parse(event_component, file);
}
void Event::parse(icalcomponent* event_component) {
void Event::parse(icalcomponent* event_component, const std::string &file) {
// TODO parse recurring events
uid = icalcomponent_get_uid(event_component);
dtstart = date_to_epoch(icaltime_as_ical_string(icalcomponent_get_dtstart(event_component)));
dtend = date_to_epoch(icaltime_as_ical_string(icalcomponent_get_dtend(event_component)));
summary = std::string(icalcomponent_get_summary(event_component));
filename = file;
parse_alarms(event_component);
}
@ -61,6 +61,10 @@ namespace ical {
return dtend;
}
//uint32_t Event::get_location() const {
// return location;
//}
std::vector<Alarm> Event::get_alarms() const {
return alarms;
}
@ -69,7 +73,16 @@ namespace ical {
return {db_safe(uid),
db_safe(summary),
db_safe(std::to_string(dtstart)),
db_safe(std::to_string(dtend))};
db_safe(std::to_string(dtend)),
db_safe(filename)};
}
std::string Event::update_row_string() const {
return "uid = '" + uid + "', " +
"summary = '" + summary + "', " +
"dtstart = '" + std::to_string(dtstart) + "', " +
"dtend = '" + std::to_string(dtend) + "', " +
"filename = '" + filename + "' ";
}
std::string Event::print() const {

7
ical/Event.h

@ -11,9 +11,9 @@
namespace ical {
class Event {
public:
Event(icalcomponent* event_component);
Event(icalcomponent* event_component, const std::string &file);
void parse(icalcomponent* event);
void parse(icalcomponent* event, const std::string &file);
void parse_alarms(icalcomponent* event_component);
std::string get_uid() const;
@ -23,6 +23,7 @@ namespace ical {
std::vector<Alarm> get_alarms() const;
std::vector<std::string> get_db_row() const;
std::string update_row_string() const;
std::string print() const;
private:
@ -31,5 +32,7 @@ namespace ical {
uint32_t dtstart;
uint32_t dtend;
std::vector<Alarm> alarms;
std::string filename;
};
}

24
util/calendar_parsing.cpp

@ -1,16 +1,18 @@
#include <util/calendar_parsing.h>
#include <memory>
#include <iomanip>
#include <string>
namespace util {
std::vector<ical::IcalObject*> parse_cal_dir(std::string directory) {
std::vector<ical::IcalObject*> objects;
std::vector<icalcomponent*> components;
uint counter = 0;
DEBUG << "Parsing: " << directory << " for .ics files";
for (boost::filesystem::directory_entry& entry : boost::filesystem::directory_iterator(directory)) {
ical::IcalObject* object = new ical::IcalObject();
parse_main_component(object, parse_ics_file(entry.path().generic_string()));
parse_main_component(object, parse_ics_file(entry.path().generic_string()), entry.path().generic_string());
objects.push_back(object);
counter++;
}
@ -18,13 +20,13 @@ namespace util {
return objects;
}
icalcomponent* parse_main_component(ical::IcalObject* object, icalcomponent* component) {
icalcomponent* parse_main_component(ical::IcalObject* object, icalcomponent* component, const std::string &file) {
icalcomponent* c = icalcomponent_get_first_component(component, ICAL_ANY_COMPONENT);
while(c != 0) {
if(c != 0) {
parse_component(object, c);
parse_component(object, c, file);
}
c = parse_main_component(object, c);
c = parse_main_component(object, c, file);
if(c == 0) {
c = icalcomponent_get_next_component(component, ICAL_ANY_COMPONENT);
}
@ -32,8 +34,7 @@ namespace util {
return c;
}
void parse_component(ical::IcalObject* object, icalcomponent* component) {
DEBUG<< icaltime_as_ical_string(icalcomponent_get_dtstart(component));
void parse_component(ical::IcalObject* object, icalcomponent* component, const std::string &file) {
switch(icalcomponent_isa(component)) {
case ICAL_VALARM_COMPONENT: break;
case ICAL_NO_COMPONENT:
@ -64,7 +65,7 @@ namespace util {
{
//DEBUG << "parsed component: ICAL_VEVENT_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
object->add_event(new ical::Event(component));
object->add_event(new ical::Event(component, file));
break;
}
case ICAL_VTODO_COMPONENT:
@ -259,4 +260,11 @@ namespace util {
void debug(icalcomponent* component) {
DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
}
std::string date_string(const uint32_t &epoch, const std::string &format) {
std::time_t time = static_cast<time_t>(epoch);
char buf[80];
strftime(buf, sizeof(buf), format.c_str(), std::localtime(&time));
return buf;
}
}

14
util/calendar_parsing.h

@ -1,26 +1,30 @@
#pragma once
#include <fstream>
#include <sstream>
#include <iostream>
#include <sstream>
#include <string>
#include <libical/ical.h>
#include <ical/IcalObject.h>
#include <ical/Alarm.h>
#include <ical/Event.h>
#include <ical/IcalObject.h>
#include <libical/ical.h>
#include <boost/filesystem.hpp>
#include <io/debug.h>
const std::string dtime_fmt = "%d.%m.%Y_%H:%M:%S";
namespace util {
std::vector<ical::IcalObject*> parse_cal_dir(std::string directory);
icalcomponent* parse_main_component(ical::IcalObject* object, icalcomponent* component);
void parse_component(ical::IcalObject* object, icalcomponent* component);
icalcomponent* parse_main_component(ical::IcalObject* object, icalcomponent* component, const std::string &file);
void parse_component(ical::IcalObject* object, icalcomponent* component, const std::string &file);
std::string slurp(std::ifstream& in);
icalcomponent* parse_ics_file(std::string filename);
void debug(icalcomponent* component);
std::string date_string(const uint32_t &epoch, const std::string &format = "%c");
}
Loading…
Cancel
Save