Browse Source

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
main
Stefan Pranger 3 years ago
parent
commit
8dead037e4
  1. 23
      calendar-daemon.cpp
  2. 10
      db/update.h
  3. 4
      ical/Alarm.cpp
  4. 4
      ical/Alarm.h
  5. 7
      ical/Event.cpp
  6. 9
      ical/Event.h
  7. 24
      ical/IcalObject.cpp
  8. 15
      ical/IcalObject.h
  9. 132
      util/calendar_parsing.cpp
  10. 6
      util/calendar_parsing.h

23
calendar-daemon.cpp

@ -14,24 +14,29 @@
#include <db/db.h>
#include <db/create.h>
#include <db/update.h>
#include <util/notifications.h>
#include <util/calendar_parsing.h>
#include <ical/IcalObject.h>
#include <io/debug.h>
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<ical::ical_object*> objects = util::parse_cal_dir(directory);
void update_database(std::string directory, const sql::Database *db) {
std::vector<ical::IcalObject*> 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

10
db/update.h

@ -0,0 +1,10 @@
#pragma once
#include <db/db.h>
#include <ical/IcalObject.h>
namespace db {
uint32_t insert_object(const sql::Database *db, const ical::IcalObject *object) {
DEBUG << *object;
}
}

4
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) {

4
ical/Alarm.h

@ -2,7 +2,9 @@
#include <string>
#include <memory>
#include <ical/IcalObject.h>
#include <libical/ical.h>
#include <io/debug.h>
namespace ical {
enum class action {

7
ical/Event.cpp

@ -2,16 +2,15 @@
#include <ical/Alarm.h>
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) {

9
ical/Event.h

@ -3,14 +3,17 @@
#include <string>
#include <vector>
#include <memory>
#include <ical/IcalObject.h>
#include <libical/ical.h>
#include <io/debug.h>
#include <ical/Alarm.h>
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;

24
ical/IcalObject.cpp

@ -0,0 +1,24 @@
#include <ical/IcalObject.h>
namespace ical {
void IcalObject::add_event(ical::Event* event) {
events.push_back(event);
}
std::vector<ical::Event*> 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;
}
}

15
ical/IcalObject.h

@ -4,12 +4,19 @@
#include <vector>
#include <io/debug.h>
#include <ical/Alarm.h>
#include <ical/Event.h>
namespace ical {
class Alarm;
class Event;
struct ical_object {
std::vector<ical::Event*> events;
class IcalObject {
public:
void add_event(ical::Event* event);
std::vector<ical::Event*> get_events();
bool empty();
friend std::ostream& operator<<(std::ostream &os, ical::IcalObject const &obj);
private:
std::vector<ical::Event*> events;
};
}

132
util/calendar_parsing.cpp

@ -3,21 +3,23 @@
#include <memory>
namespace util {
std::vector<ical::ical_object*> parse_cal_dir(std::string directory) {
std::vector<ical::ical_object*> objects;
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::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:

6
util/calendar_parsing.h

@ -14,9 +14,9 @@
#include <io/debug.h>
namespace util {
std::vector<ical::ical_object*> 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<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);
std::string slurp(std::ifstream& in);
icalcomponent* parse_ics_file(std::string filename);

Loading…
Cancel
Save