You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

100 lines
2.7 KiB

#include <iomanip>
#include <ical/Event.h>
#include <ical/Alarm.h>
#include <db/db.h>
namespace ical {
uint32_t date_to_epoch(const std::string &dt) {
//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")) {
time_t temp1 = mktime(&t);
struct tm* temp2 = localtime(&temp1);
return mktime(temp2);
} else {
std::cout << "Parse failed\n";
return -1;
}
}
Event::Event(icalcomponent* event_component, const std::string &file) {
parse(event_component, file);
}
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);
}
void Event::parse_alarms(icalcomponent* event_component) {
icalcomponent* c;
for(c = icalcomponent_get_first_component(event_component, ICAL_VALARM_COMPONENT);
c != 0;
c = icalcomponent_get_next_component(event_component, ICAL_VALARM_COMPONENT))
{
alarms.push_back(Alarm(c, dtstart));
}
}
std::string Event::get_uid() const {
return uid;
}
std::string Event::get_summary() const {
return summary;
}
uint32_t Event::get_dtstart() const {
return dtstart;
}
uint32_t Event::get_dtend() const {
return dtend;
}
//uint32_t Event::get_location() const {
// return location;
//}
std::vector<Alarm> Event::get_alarms() const {
return alarms;
}
std::vector<std::string> Event::get_db_row() const {
return {db_safe(uid),
db_safe(summary),
db_safe(std::to_string(dtstart)),
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 {
std::string s = "";
s += "UID: " + uid + "\n";
s += "DTSTART: " + std::to_string(dtstart) + "\n";
s += "DTEND: " + std::to_string(dtend) + "\n";
s += "SUMMARY: " + summary + "\n";
s += std::to_string(alarms.size()) + " ALARMS:\n";
for(auto const& a : alarms) {
s += a.print();
}
return s;
}
}