Browse Source

reworked Alarm and Event

fixed wrong offset in datetime conversion
main
Stefan Pranger 3 years ago
parent
commit
889a37abd6
  1. 3
      .gitmodules
  2. 1
      db/db.h
  3. 44
      ical/Alarm.cpp
  4. 15
      ical/Alarm.h
  5. 26
      ical/Event.cpp
  6. 17
      util/calendar_parsing.cpp

3
.gitmodules

@ -1,3 +1,6 @@
[submodule "thirdparty/SQLiteCpp"]
path = thirdparty/SQLiteCpp
url = https://github.com/SRombauts/SQLiteCpp
[submodule "thirdparty/inotify-cpp"]
path = thirdparty/inotify-cpp
url = https://github.com/erikzenker/inotify-cpp.git

1
db/db.h

@ -1,6 +1,7 @@
#pragma once
#include <string>
#include <vector>
#include <SQLiteCpp.h>
#include <io/debug.h>

44
ical/Alarm.cpp

@ -1,8 +1,11 @@
#include <ical/Alarm.h>
#include <libical/icalproperty_cxx.h>
#include <db/db.h>
namespace ical {
Alarm::Alarm(icalcomponent* alarm_component) {
Alarm::Alarm(icalcomponent* alarm_component, const uint32_t& event_dtstart) : m_event_dtstart(event_dtstart) {
DEBUG << "Parsing ALARM";
parse(alarm_component);
}
@ -20,7 +23,7 @@ namespace ical {
void Alarm::parse_description(icalcomponent* alarm_component) {
icalproperty* description_property = icalcomponent_get_first_property(alarm_component, ICAL_DESCRIPTION_PROPERTY);
if(description_property != 0) {
description = icalproperty_get_value_as_string(description_property);
m_description = icalproperty_get_value_as_string(description_property);
}
}
@ -30,17 +33,46 @@ namespace ical {
if(trigger_property != 0) {
tr = icalproperty_get_trigger(trigger_property);
if (!icaltime_is_null_time(tr.time)) {
trigger = ical::trigger_type::ABSOLUTE;
m_trigger = ical::trigger_type::ABSOLUTE;
DEBUG << icaltime_as_ical_string(tr.time);
} else {
trigger = ical::trigger_type::RELATIVE;
m_trigger = ical::trigger_type::RELATIVE;
DEBUG << icaldurationtype_as_int(tr.duration);
m_absolute_trigger = m_event_dtstart + icaldurationtype_as_int(tr.duration);
}
}
}
std::string Alarm::get_action() const {
//switch(m_action) {
// case ical::action::DISPLAY: return "display";
// case ical::action::AUDIO: return "audio";
// case ical::action::EMAIL: return "email";
// case ical::action::PROC: return "proc";
// default: return "";
//}
return "";
}
std::string Alarm::get_trigger_type() const {
switch(m_trigger) {
case ical::trigger_type::ABSOLUTE: return "absolute";
case ical::trigger_type::RELATIVE: return "relative";
default: return "";
}
}
std::vector<std::string> Alarm::get_db_row() const {
return {db_safe(get_action()),
db_safe(m_description),
db_safe(std::to_string(m_absolute_trigger))};
}
std::string Alarm::print() const {
std::string s = "";
s += "DESCRIPTION: " + description + "\n";
s += trigger == ical::trigger_type::ABSOLUTE ? "ABSOLUTE: " : "RELATIVE: ";
//s += "DESCRIPTION: " + m_description + "\n";
////s += m_trigger == ical::trigger_type::ABSOLUTE ? "ABSOLUTE: " : "RELATIVE: ";
//s += "ABSOLUTE_TRIGGER: " + std::to_string(m_absolute_trigger) + "\n";
return s;
}
}

15
ical/Alarm.h

@ -2,6 +2,7 @@
#include <string>
#include <memory>
#include <vector>
#include <libical/ical.h>
#include <io/debug.h>
@ -17,18 +18,24 @@ namespace ical {
class Alarm {
public:
Alarm(icalcomponent* alarm_component);
Alarm(icalcomponent* alarm_component, const uint32_t &event_dtstart);
void parse(icalcomponent* alarm_component);
void parse_action(icalcomponent* alarm_component);
void parse_description(icalcomponent* alarm_component);
void parse_trigger(icalcomponent* alarm_component);
std::string get_action() const;
std::string get_trigger_type() const;
std::vector<std::string> get_db_row() const;
std::string print() const;
private:
action a;
trigger_type trigger;
const uint32_t m_event_dtstart;
std::string description;
action m_action;
trigger_type m_trigger;
uint32_t m_absolute_trigger;
std::string m_description;
};
}

26
ical/Event.cpp

@ -1,3 +1,5 @@
#include <iomanip>
#include <ical/Event.h>
#include <ical/Alarm.h>
@ -5,14 +7,30 @@
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 = gmtime(&temp1);
return mktime(temp2);
}
else
{
std::cout << "Parse failed\n";
return -1;
}
}
Event::Event(icalcomponent* event_component) {
parse(event_component);
}
void Event::parse(icalcomponent* event_component) {
uid = icalcomponent_get_uid(event_component);
dtstart = icaltime_as_timet(icalcomponent_get_dtstart(event_component));
dtend = icaltime_as_timet(icalcomponent_get_dtend(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));
parse_alarms(event_component);
}
@ -23,7 +41,7 @@ namespace ical {
c != 0;
c = icalcomponent_get_next_component(event_component, ICAL_VALARM_COMPONENT))
{
alarms.push_back(Alarm(c));
alarms.push_back(Alarm(c, dtstart));
}
}
@ -60,7 +78,7 @@ namespace ical {
s += "DTSTART: " + std::to_string(dtstart) + "\n";
s += "DTEND: " + std::to_string(dtend) + "\n";
s += "SUMMARY: " + summary + "\n";
s += "ALARMS:\n";
s += std::to_string(alarms.size()) + " ALARMS:\n";
for(auto const& a : alarms) {
s += a.print();
}

17
util/calendar_parsing.cpp

@ -3,19 +3,18 @@
#include <memory>
namespace util {
std::vector<ical::IcalObject*> parse_cal_dir(std::string directory) {
std::vector<ical::IcalObject*> objects;
std::vector<icalcomponent*> components;
uint counter = 0;
for (boost::filesystem::directory_entry& entry : boost::filesystem::directory_iterator(directory)) {
DEBUG << "Parsing: " + entry.path().generic_string();
ical::IcalObject* object = new ical::IcalObject();
parse_main_component(object, parse_ics_file(entry.path().generic_string()));
objects.push_back(object);
counter++;
}
DEBUG << "files parsed: " << counter;
DEBUG << "objects: "<< objects.size();
return objects;
}
@ -34,6 +33,7 @@ namespace util {
}
void parse_component(ical::IcalObject* object, icalcomponent* component) {
DEBUG<< icaltime_as_ical_string(icalcomponent_get_dtstart(component));
switch(icalcomponent_isa(component)) {
case ICAL_VALARM_COMPONENT: break;
case ICAL_NO_COMPONENT:
@ -124,6 +124,19 @@ namespace util {
case ICAL_VTIMEZONE_COMPONENT:
{
//DEBUG << "parsed component: ICAL_VTIMEZONE_COMPONENT";
DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
//icaltimezone* zone;
//icalproperty *prop;
//const char *tzid;
//prop = icalcomponent_get_first_property(component, ICAL_TZID_PROPERTY);
//tzid = icalproperty_get_tzid(prop);
//zone->tzid = strdup(tzid);
//zone->component = component;
//zone->location = icaltimezone_get_location_from_vtimezone(component);
//zone->tznames = icaltimezone_get_tznames_from_vtimezone(component);
//DEBUG << icaltimezone_get_tzid(zone);
//int offset = icaltimezone_get_utc_offset(zone, struct icaltimetype *tt, int *is_daylight)
break;
}
case ICAL_XSTANDARD_COMPONENT:

Loading…
Cancel
Save