]> Pileus Git - lackey/blobdiff - cals/ical.c
Bump copyright on files modified in 2013
[lackey] / cals / ical.c
index a05128d7a1f04a8d9f360cef6dbf4ed1219f8558..29abe10d48622a65ee68af75976daf4e026661a3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012 Andy Spencer <andy753421@gmail.com>
+ * Copyright (C) 2012-2013 Andy Spencer <andy753421@gmail.com>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * 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"
+#include "conf.h"
 #include "date.h"
 #include "cal.h"
 
@@ -30,6 +34,18 @@ typedef struct {
        struct icaltimetype end;
 } ical_inst;
 
+typedef struct ical_t {
+       cal_t          cal;
+       char          *location;
+       char          *username;
+       char          *password;
+       icalcomponent *comp;
+       struct ical_t *next;
+} ical_t;
+
+/* Static data */
+static ical_t *calendars;
+
 /* Helper functions */
 static int ical_compare(const void *_a, const void *_b)
 {
@@ -52,6 +68,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)
@@ -115,6 +142,27 @@ static void add_recur(icalarray *array, icalcomponent *comp,
        }
 }
 
+static void read_icals(void)
+{
+       for (ical_t *cal = calendars; cal; cal = cal->next) {
+               if (cal->comp == 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->comp = icalparser_parse(parser, (void*)fgets);
+                       icalparser_free(parser);
+               }
+       }
+}
+
 /* Event functions */
 static event_t *to_event(ical_inst *inst)
 {
@@ -189,52 +237,82 @@ static void print_todos(todo_t *start)
                        cur->name ?: cur->desc ?: "[no summary]");
 }
 
+/* Config parser */
+void ical_config(const char *group, const char *name, const char *key, const char *value)
+{
+       ical_t *cal = NULL, *last = NULL;
+
+       /* Make sure it's valid */
+       if (!match(group, "ical") || !name)
+               return;
+
+       /* Find existing calendar */
+       for (cal = calendars; cal; last = cal, cal = cal->next)
+               if (match(cal->cal.name, name))
+                       break;
+
+       /* Create new calendar */
+       if (!cal) {
+               cal = new0(ical_t);
+               cal->cal.type = "ical";
+               cal->cal.name = get_name(name);
+               if (last)
+                       last->next = cal;
+               else
+                       calendars = cal;
+               return;
+       }
+
+       /* Set calendar values */
+       if (match(key, "location"))
+               cal->location = get_string(value);
+       else if (match(key, "username"))
+               cal->username = get_string(value);
+       else if (match(key, "password"))
+               cal->password = get_string(value);
+}
+
+/* Cal functions */
+cal_t *ical_cals(void)
+{
+       read_icals();
+
+       for (ical_t *cal = calendars; cal; cal = cal->next)
+               cal->cal.next = &cal->next->cal;
+
+       return &calendars->cal;
+}
+
 /* 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);
-       icalparser_free(parser);
+       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->comp, start, end, ICAL_VEVENT_COMPONENT);
        icalarray_sort(array, ical_compare);
        event_t *events = to_events(array);
        icalarray_free(array);
-       icalcomponent_free(ical);
 
        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);
-       icalparser_free(parser);
+       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->comp, start, end, ICAL_VTODO_COMPONENT);
        icalarray_sort(array, ical_compare);
        todo_t *todos = to_todos(array);
        icalarray_free(array);
-       icalcomponent_free(ical);
 
        return todos;
 }
@@ -266,7 +344,7 @@ void ical_test(void)
        FILE *file = fopen("data/all.ics", "r");
        icalparser *parser = icalparser_new();
        icalparser_set_gen_data(parser, file);
-       icalcomponent *ical = icalparser_parse(parser, (void*)fgets);
+       icalcomponent *comp = icalparser_parse(parser, (void*)fgets);
 
        /* Misc */
        icalarray *array;
@@ -275,20 +353,20 @@ void ical_test(void)
 
        /* Find events */
        array = icalarray_new(sizeof(ical_inst), 1);
-       add_recur(array, ical, start, end, ICAL_VEVENT_COMPONENT);
+       add_recur(array, comp, 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);
+       add_recur(array, comp, start, end, ICAL_VTODO_COMPONENT);
        icalarray_sort(array, ical_compare);
        todo_t *todos = to_todos(array);
        icalarray_free(array);
 
        /* Print */
-       //ical_printr(ical, 0);
+       //ical_printr(comp, 0);
        //print_events(events);
        print_todos(todos);