From 889a37abd68a80f08af072189f841e6dccb8ca68 Mon Sep 17 00:00:00 2001 From: Stefan Pranger Date: Fri, 2 Jul 2021 21:30:22 +0200 Subject: [PATCH] reworked Alarm and Event fixed wrong offset in datetime conversion --- .gitmodules | 3 +++ db/db.h | 1 + ical/Alarm.cpp | 44 +++++++++++++++++++++++++++++++++------ ical/Alarm.h | 15 +++++++++---- ical/Event.cpp | 26 +++++++++++++++++++---- util/calendar_parsing.cpp | 17 +++++++++++++-- 6 files changed, 90 insertions(+), 16 deletions(-) diff --git a/.gitmodules b/.gitmodules index 70996b3..a5328c5 100644 --- a/.gitmodules +++ b/.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 diff --git a/db/db.h b/db/db.h index 8e8346d..4e1b1af 100644 --- a/db/db.h +++ b/db/db.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include diff --git a/ical/Alarm.cpp b/ical/Alarm.cpp index ab8b0f0..63fc384 100644 --- a/ical/Alarm.cpp +++ b/ical/Alarm.cpp @@ -1,8 +1,11 @@ #include #include +#include + 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 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; } } diff --git a/ical/Alarm.h b/ical/Alarm.h index 2b62ba9..d5d05c8 100644 --- a/ical/Alarm.h +++ b/ical/Alarm.h @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -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 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; }; } diff --git a/ical/Event.cpp b/ical/Event.cpp index 75d067d..d23ea94 100644 --- a/ical/Event.cpp +++ b/ical/Event.cpp @@ -1,3 +1,5 @@ +#include + #include #include @@ -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(); } diff --git a/util/calendar_parsing.cpp b/util/calendar_parsing.cpp index 70e73dc..2d54545 100644 --- a/util/calendar_parsing.cpp +++ b/util/calendar_parsing.cpp @@ -3,19 +3,18 @@ #include namespace util { + std::vector parse_cal_dir(std::string directory) { std::vector objects; std::vector 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: