]> Pileus Git - lackey/blobdiff - cals/ical.c
Add cal_load function and update cal APIs
[lackey] / cals / ical.c
index 2a48bada91117467bf7f260d6697853f23451eb3..30653edf279cca356650da044d1a183e14e0bb60 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#define _XOPEN_SOURCE
+
 #include <stdio.h>
 #include <stdlib.h>
+#include <wordexp.h>
 #include <libical/ical.h>
 
 #include "util.h"
@@ -30,6 +33,20 @@ typedef struct {
        struct icaltimetype end;
 } ical_inst;
 
+typedef struct ical_t {
+       char          *name;
+       char          *location;
+       char          *username;
+       char          *password;
+       icalcomponent *ical;
+       struct ical_t *next;
+} ical_t;
+
+/* Static data */
+static ical_t  calendars[] = {
+       { .location = "data/all.ics" },
+};
+
 /* Helper functions */
 static int ical_compare(const void *_a, const void *_b)
 {
@@ -52,6 +69,17 @@ static date_t to_date(struct icaltimetype time)
        };
 }
 
+static icaltimetype to_itime(date_t time)
+{
+       return (struct icaltimetype){
+               .year   = time.year,
+               .month  = time.month + 1,
+               .day    = time.day   + 1,
+               .hour   = time.hour,
+               .minute = time.min
+       };
+}
+
 static void add_recur(icalarray *array, icalcomponent *comp,
                icaltimetype start, icaltimetype end,
                icalcomponent_kind which)
@@ -102,6 +130,8 @@ static void add_recur(icalarray *array, icalcomponent *comp,
                                .end   = iend,
                        });
                }
+
+               icalrecur_iterator_free(iter);
        }
 
        /* Add children */
@@ -113,15 +143,36 @@ static void add_recur(icalarray *array, icalcomponent *comp,
        }
 }
 
+static void read_icals(void)
+{
+       for (ical_t *cal = calendars; cal; cal = cal->next) {
+               if (cal->ical == NULL && cal->location) {
+                       wordexp_t wexp;
+                       wordexp(cal->location, &wexp, WRDE_NOCMD);
+                       if (wexp.we_wordc == 0)
+                               continue;
+                       FILE *file = fopen(wexp.we_wordv[0], "r");
+                       wordfree(&wexp);
+                       if (!file)
+                               continue;
+
+                       icalparser *parser = icalparser_new();
+                       icalparser_set_gen_data(parser, file);
+                       cal->ical = icalparser_parse(parser, (void*)fgets);
+                       icalparser_free(parser);
+               }
+       }
+}
+
 /* Event functions */
 static event_t *to_event(ical_inst *inst)
 {
        icalproperty *prop = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
 
-       event_t *event = calloc(1, sizeof(event_t));
-       event->name  = icalcomponent_get_summary(inst->comp);
-       event->desc  = icalcomponent_get_description(inst->comp);
-       event->loc   = icalcomponent_get_location(inst->comp);
+       event_t *event = new0(event_t);
+       event->name  = strcopy(icalcomponent_get_summary(inst->comp));
+       event->desc  = strcopy(icalcomponent_get_description(inst->comp));
+       event->loc   = strcopy(icalcomponent_get_location(inst->comp));
        event->cat   = icalproperty_get_value_as_string_r(prop);
        event->start = to_date(inst->start);
        event->end   = to_date(inst->end);
@@ -155,10 +206,10 @@ static todo_t *to_todo(ical_inst *inst)
        icalproperty *cat  = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
        icalproperty *perc = icalcomponent_get_first_property(inst->comp, ICAL_PERCENTCOMPLETE_PROPERTY);
 
-       todo_t *todo = calloc(1, sizeof(todo_t));
-       todo->name   = icalcomponent_get_summary(inst->comp);
-       todo->desc   = icalcomponent_get_description(inst->comp);
-       todo->cat    = icalproperty_get_value_as_string(cat);
+       todo_t *todo = new0(todo_t);
+       todo->name   = strcopy(icalcomponent_get_summary(inst->comp));
+       todo->desc   = strcopy(icalcomponent_get_description(inst->comp));
+       todo->cat    = strcopy(icalproperty_get_value_as_string(cat));
        todo->status = icalcomponent_get_status(inst->comp) == ICAL_STATUS_COMPLETED ? 100 :
                       perc ? icalproperty_get_percentcomplete(perc) : 0;
        todo->start  = to_date(inst->start);
@@ -188,47 +239,37 @@ static void print_todos(todo_t *start)
 }
 
 /* Event functions */
-event_t *ical_events(cal_t *cal, year_t year, month_t month, day_t day, int days)
+event_t *ical_events(date_t _start, date_t _end)
 {
-       /* Load ical */
-       FILE *file = fopen("data/all.ics", "r");
-       if (!file)
-               return NULL;
-       icalparser *parser = icalparser_new();
-       icalparser_set_gen_data(parser, file);
-       icalcomponent *ical = icalparser_parse(parser, (void*)fgets);
+       read_icals();
 
-       /* Add events */
+       icaltimetype start = to_itime(_start);
+       icaltimetype end   = to_itime(_end);
        icalarray *array = icalarray_new(sizeof(ical_inst), 1);
-       icaltimetype start = {.year = 2000};
-       icaltimetype end   = {.year = 2020};
-       add_recur(array, ical, start, end, ICAL_VEVENT_COMPONENT);
+       for (ical_t *cal = calendars; cal; cal = cal->next)
+               add_recur(array, cal->ical, start, end, ICAL_VEVENT_COMPONENT);
        icalarray_sort(array, ical_compare);
-       return to_events(array);
+       event_t *events = to_events(array);
+       icalarray_free(array);
 
-       /* Todo, memory management */
+       return events;
 }
 
 /* Todo functions */
-todo_t *ical_todos(cal_t *cal, year_t year, month_t month, day_t day, int days)
+todo_t *ical_todos(date_t _start, date_t _end)
 {
-       /* Load ical */
-       FILE *file = fopen("data/all.ics", "r");
-       if (!file)
-               return NULL;
-       icalparser *parser = icalparser_new();
-       icalparser_set_gen_data(parser, file);
-       icalcomponent *ical = icalparser_parse(parser, (void*)fgets);
+       read_icals();
 
-       /* Add todos */
+       icaltimetype start = to_itime(_start);
+       icaltimetype end   = to_itime(_end);
        icalarray *array = icalarray_new(sizeof(ical_inst), 1);
-       icaltimetype start = {.year = 2000};
-       icaltimetype end   = {.year = 2020};
-       add_recur(array, ical, start, end, ICAL_VTODO_COMPONENT);
+       for (ical_t *cal = calendars; cal; cal = cal->next)
+               add_recur(array, cal->ical, start, end, ICAL_VTODO_COMPONENT);
        icalarray_sort(array, ical_compare);
-       return to_todos(array);
+       todo_t *todos = to_todos(array);
+       icalarray_free(array);
 
-       /* Todo, memory management */
+       return todos;
 }
 
 /* Test functions */
@@ -270,12 +311,14 @@ void ical_test(void)
        add_recur(array, ical, start, end, ICAL_VEVENT_COMPONENT);
        icalarray_sort(array, ical_compare);
        event_t *events = to_events(array);
+       icalarray_free(array);
 
        /* Find Todos */
        array = icalarray_new(sizeof(ical_inst), 1);
        add_recur(array, ical, start, end, ICAL_VTODO_COMPONENT);
        icalarray_sort(array, ical_compare);
        todo_t *todos = to_todos(array);
+       icalarray_free(array);
 
        /* Print */
        //ical_printr(ical, 0);