From 8dead037e402d76807c8aa577b661bea827c2aff Mon Sep 17 00:00:00 2001 From: Stefan Pranger Date: Tue, 29 Jun 2021 14:11:29 +0200 Subject: [PATCH] init db update and refactored ical classes - IcalObject is now a proper class - fixed Alarm/Event/IcalObject includes - objects now get instantiated in parse_cal_dir and get filled up along the way --- calendar-daemon.cpp | 23 ++++--- db/update.h | 10 +++ ical/Alarm.cpp | 4 +- ical/Alarm.h | 4 +- ical/Event.cpp | 7 +- ical/Event.h | 9 ++- ical/IcalObject.cpp | 24 +++++++ ical/IcalObject.h | 15 +++-- util/calendar_parsing.cpp | 132 +++++++++++++++++++------------------- util/calendar_parsing.h | 6 +- 10 files changed, 143 insertions(+), 91 deletions(-) create mode 100644 db/update.h create mode 100644 ical/IcalObject.cpp diff --git a/calendar-daemon.cpp b/calendar-daemon.cpp index c7465d3..8f874a3 100644 --- a/calendar-daemon.cpp +++ b/calendar-daemon.cpp @@ -14,24 +14,29 @@ #include #include +#include #include #include +#include #include const int MINUTE = 1000000; -void init_database(std::string calendar) { - db::init(calendar + ".db"); +sql::Database* init_database(std::string calendar) { + return db::init(calendar + ".db"); } -void update_database(std::string directory) { - std::vector objects = util::parse_cal_dir(directory); +void update_database(std::string directory, const sql::Database *db) { + std::vector objects = util::parse_cal_dir(directory); + for(auto const& obj : objects) { + if(obj->empty()) continue; + db::insert_object(db, obj); + } } -void do_heartbeat(std::string directory) -{ +void do_heartbeat(std::string directory, const sql::Database *db) { util::notify("Updating files from " + directory, ""); - update_database(directory); + update_database(directory, db); } // For security purposes, we don't allow any arguments to be passed into the daemon @@ -97,14 +102,14 @@ int main(void) std::string calendar = "dummy"; std::string directory = "/home/stefan/.local/share/khal/calendars/" + calendar + "/"; - init_database(calendar); + sql::Database* db = init_database(calendar); // Enter daemon loop while(1) { // Execute daemon heartbeat, where your recurring activity occurs - do_heartbeat(directory); + do_heartbeat(directory, db); exit(0); // Sleep for a period of time diff --git a/db/update.h b/db/update.h new file mode 100644 index 0000000..fce44b4 --- /dev/null +++ b/db/update.h @@ -0,0 +1,10 @@ +#pragma once + +#include +#include + +namespace db { + uint32_t insert_object(const sql::Database *db, const ical::IcalObject *object) { + DEBUG << *object; + } +} diff --git a/ical/Alarm.cpp b/ical/Alarm.cpp index f464c24..ab8b0f0 100644 --- a/ical/Alarm.cpp +++ b/ical/Alarm.cpp @@ -10,11 +10,11 @@ namespace ical { parse_action(alarm_component); parse_description(alarm_component); parse_trigger(alarm_component); - DEBUG << "\n" << print(); } void Alarm::parse_action(icalcomponent* alarm_component) { - DEBUG << icalproperty_get_value_as_string(icalcomponent_get_first_property(alarm_component, ICAL_ACTION_PROPERTY)); + DEBUG << "TODO alarm action parsing" << std::endl; + //DEBUG << icalproperty_get_value_as_string(icalcomponent_get_first_property(alarm_component, ICAL_ACTION_PROPERTY)) << std::endl; } void Alarm::parse_description(icalcomponent* alarm_component) { diff --git a/ical/Alarm.h b/ical/Alarm.h index 23309c8..2b62ba9 100644 --- a/ical/Alarm.h +++ b/ical/Alarm.h @@ -2,7 +2,9 @@ #include #include -#include + +#include +#include namespace ical { enum class action { diff --git a/ical/Event.cpp b/ical/Event.cpp index caa0a58..308bf83 100644 --- a/ical/Event.cpp +++ b/ical/Event.cpp @@ -2,16 +2,15 @@ #include namespace ical { - Event::Event(ical::ical_object* object, icalcomponent* event_component) { - parse(object, event_component); + Event::Event(icalcomponent* event_component) { + parse(event_component); } - void Event::parse(ical::ical_object* object, icalcomponent* event_component) { + void Event::parse(icalcomponent* event_component) { dtstart = icalcomponent_get_dtstart(event_component); dtend = icalcomponent_get_dtstart(event_component); summary = std::string(icalcomponent_get_summary(event_component)); parse_alarms(event_component); - object->events.push_back(this); } void Event::parse_alarms(icalcomponent* event_component) { diff --git a/ical/Event.h b/ical/Event.h index 9c5274f..99f3349 100644 --- a/ical/Event.h +++ b/ical/Event.h @@ -3,14 +3,17 @@ #include #include #include -#include + +#include +#include +#include namespace ical { class Event { public: - Event(ical::ical_object* object, icalcomponent* event_component); + Event(icalcomponent* event_component); - void parse(ical::ical_object* object, icalcomponent* event); + void parse(icalcomponent* event); void parse_alarms(icalcomponent* event_component); std::string print() const; diff --git a/ical/IcalObject.cpp b/ical/IcalObject.cpp new file mode 100644 index 0000000..7167086 --- /dev/null +++ b/ical/IcalObject.cpp @@ -0,0 +1,24 @@ +#include + +namespace ical { + void IcalObject::add_event(ical::Event* event) { + events.push_back(event); + } + + std::vector IcalObject::get_events() { + return events; + } + + bool IcalObject::empty() { + return events.size() == 0; + } + + std::ostream& operator<<(std::ostream &os, ical::IcalObject const &obj) { + os << "Object:" << std::endl; + for(auto const& e : obj.events) { + os << e->print(); + } + os << std::endl; + return os; + } +} diff --git a/ical/IcalObject.h b/ical/IcalObject.h index 87e705e..4280cba 100644 --- a/ical/IcalObject.h +++ b/ical/IcalObject.h @@ -4,12 +4,19 @@ #include #include +#include +#include namespace ical { - class Alarm; - class Event; - struct ical_object { - std::vector events; + class IcalObject { + public: + void add_event(ical::Event* event); + std::vector get_events(); + bool empty(); + + friend std::ostream& operator<<(std::ostream &os, ical::IcalObject const &obj); + private: + std::vector events; }; } diff --git a/util/calendar_parsing.cpp b/util/calendar_parsing.cpp index 062ab05..70e73dc 100644 --- a/util/calendar_parsing.cpp +++ b/util/calendar_parsing.cpp @@ -3,21 +3,23 @@ #include namespace util { - std::vector parse_cal_dir(std::string directory) { - std::vector objects; + 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::ical_object* object = new ical::ical_object(); + 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; } - icalcomponent* parse_main_component(ical::ical_object* object, icalcomponent* component) { + icalcomponent* parse_main_component(ical::IcalObject* object, icalcomponent* component) { icalcomponent* c = icalcomponent_get_first_component(component, ICAL_ANY_COMPONENT); while(c != 0) { if(c != 0) { @@ -31,197 +33,197 @@ namespace util { return c; } - void parse_component(ical::ical_object* object, icalcomponent* component) { + void parse_component(ical::IcalObject* object, icalcomponent* component) { switch(icalcomponent_isa(component)) { case ICAL_VALARM_COMPONENT: break; case ICAL_NO_COMPONENT: { - DEBUG << "parsed component: ICAL_NO_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_NO_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_ANY_COMPONENT: { - DEBUG << "parsed component: ICAL_ANY_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_ANY_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_XROOT_COMPONENT: { - DEBUG << "parsed component: ICAL_XROOT_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_XROOT_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_XATTACH_COMPONENT: { - DEBUG << "parsed component: ICAL_XATTACH_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_XATTACH_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_VEVENT_COMPONENT: { //DEBUG << "parsed component: ICAL_VEVENT_COMPONENT"; //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); - ical::Event(object, component); + object->add_event(new ical::Event(component)); break; } case ICAL_VTODO_COMPONENT: { - DEBUG << "parsed component: ICAL_VTODO_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_VTODO_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_VJOURNAL_COMPONENT: { - DEBUG << "parsed component: ICAL_VJOURNAL_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_VJOURNAL_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_VCALENDAR_COMPONENT: { - DEBUG << "parsed component: ICAL_VCALENDAR_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_VCALENDAR_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_VAGENDA_COMPONENT: { - DEBUG << "parsed component: ICAL_VAGENDA_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_VAGENDA_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_VFREEBUSY_COMPONENT: { - DEBUG << "parsed component: ICAL_VFREEBUSY_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_VFREEBUSY_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_XAUDIOALARM_COMPONENT: { - DEBUG << "parsed component: ICAL_XAUDIOALARM_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_XAUDIOALARM_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_XDISPLAYALARM_COMPONENT: { - DEBUG << "parsed component: ICAL_XDISPLAYALARM_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_XDISPLAYALARM_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_XEMAILALARM_COMPONENT: { - DEBUG << "parsed component: ICAL_XEMAILALARM_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_XEMAILALARM_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_XPROCEDUREALARM_COMPONENT: { - DEBUG << "parsed component: ICAL_XPROCEDUREALARM_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_XPROCEDUREALARM_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_VTIMEZONE_COMPONENT: { - DEBUG << "parsed component: ICAL_VTIMEZONE_COMPONENT"; + //DEBUG << "parsed component: ICAL_VTIMEZONE_COMPONENT"; break; } case ICAL_XSTANDARD_COMPONENT: { - DEBUG << "parsed component: ICAL_XSTANDARD_COMPONENT"; + //DEBUG << "parsed component: ICAL_XSTANDARD_COMPONENT"; break; } case ICAL_XDAYLIGHT_COMPONENT: { - DEBUG << "parsed component: ICAL_XDAYLIGHT_COMPONENT"; + //DEBUG << "parsed component: ICAL_XDAYLIGHT_COMPONENT"; break; } case ICAL_X_COMPONENT: { - DEBUG << "parsed component: ICAL_X_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_X_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_VSCHEDULE_COMPONENT: { - DEBUG << "parsed component: ICAL_VSCHEDULE_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_VSCHEDULE_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_VQUERY_COMPONENT: { - DEBUG << "parsed component: ICAL_VQUERY_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_VQUERY_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_VREPLY_COMPONENT: { - DEBUG << "parsed component: ICAL_VREPLY_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_VREPLY_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_VCAR_COMPONENT: { - DEBUG << "parsed component: ICAL_VCAR_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_VCAR_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_VCOMMAND_COMPONENT: { - DEBUG << "parsed component: ICAL_VCOMMAND_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_VCOMMAND_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_XLICINVALID_COMPONENT: { - DEBUG << "parsed component: ICAL_XLICINVALID_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_XLICINVALID_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_XLICMIMEPART_COMPONENT: { - DEBUG << "parsed component: ICAL_XLICMIMEPART_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_XLICMIMEPART_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_VAVAILABILITY_COMPONENT: { - DEBUG << "parsed component: ICAL_VAVAILABILITY_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_VAVAILABILITY_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_XAVAILABLE_COMPONENT: { - DEBUG << "parsed component: ICAL_XAVAILABLE_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_XAVAILABLE_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_VPOLL_COMPONENT: { - DEBUG << "parsed component: ICAL_VPOLL_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_VPOLL_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_VVOTER_COMPONENT: { - DEBUG << "parsed component: ICAL_VVOTER_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_VVOTER_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_XVOTE_COMPONENT: { - DEBUG << "parsed component: ICAL_XVOTE_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_XVOTE_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_VPATCH_COMPONENT: { - DEBUG << "parsed component: ICAL_VPATCH_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_VPATCH_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } case ICAL_XPATCH_COMPONENT: { - DEBUG << "parsed component: ICAL_XPATCH_COMPONENT"; - DEBUG << "\n" << icalcomponent_as_ical_string_r(component); + //DEBUG << "parsed component: ICAL_XPATCH_COMPONENT"; + //DEBUG << "\n" << icalcomponent_as_ical_string_r(component); break; } default: diff --git a/util/calendar_parsing.h b/util/calendar_parsing.h index c699100..a0b0401 100644 --- a/util/calendar_parsing.h +++ b/util/calendar_parsing.h @@ -14,9 +14,9 @@ #include namespace util { - std::vector parse_cal_dir(std::string directory); - icalcomponent* parse_main_component(ical::ical_object* object, icalcomponent* component); - void parse_component(ical::ical_object* object, icalcomponent* component); + std::vector parse_cal_dir(std::string directory); + icalcomponent* parse_main_component(ical::IcalObject* object, icalcomponent* component); + void parse_component(ical::IcalObject* object, icalcomponent* component); std::string slurp(std::ifstream& in); icalcomponent* parse_ics_file(std::string filename);