]> Pileus Git - lackey/blobdiff - cals/ical.c
Add support for timezones
[lackey] / cals / ical.c
index 2862e725b0328f1fb16dd2e6b785c7597762f7a1..57fff1eb1e9a779dbbd02f182e1b6f649bc98511 100644 (file)
@@ -58,26 +58,14 @@ static int ical_compare(const void *_a, const void *_b)
               ecomp != 0 ? ecomp : 0 ;
 }
 
-static date_t to_date(struct icaltimetype time)
+static date_t to_date(struct icaltimetype itime)
 {
-       return (date_t){
-               .year  = time.year,
-               .month = time.month ? time.month-1 : 0,
-               .day   = time.day   ? time.day  -1 : 0,
-               .hour  = time.hour,
-               .min   = time.minute,
-       };
+       return get_date(icaltime_as_timet_with_zone(itime, itime.zone));
 }
 
-static icaltimetype to_itime(date_t time)
+static icaltimetype to_itime(date_t date)
 {
-       return (struct icaltimetype){
-               .year   = time.year,
-               .month  = time.month + 1,
-               .day    = time.day   + 1,
-               .hour   = time.hour,
-               .minute = time.min
-       };
+       return icaltime_from_timet_with_zone(get_stamp(date), 0, NULL);
 }
 
 static void add_recur(cal_t *cal,
@@ -101,11 +89,7 @@ static void add_recur(cal_t *cal,
                cend   = icalcomponent_get_dtend(comp);
                length = icaltime_subtract(cend, cstart);
 
-               rrule  = icalcomponent_get_first_property(comp, ICAL_RRULE_PROPERTY);
-               recur  = icalproperty_get_rrule(rrule);
-               iter   = icalrecur_iterator_new(recur, cstart);
-
-               /* Add recurrences */
+               /* Full day event */
                if (icaltime_is_null_time(cstart) ||
                    which == ICAL_VTODO_COMPONENT) {
                        icalarray_append(array, &(ical_inst){
@@ -114,27 +98,50 @@ static void add_recur(cal_t *cal,
                                .start = cstart,
                                .end   = cend,
                        });
-               } else while (1) {
-                       istart = iend = icalrecur_iterator_next(iter);
-                       if (icaltime_is_null_time(istart))
-                               break;    // no more instances
-                       if (!icaltime_is_null_time(cend))
-                               iend = icaltime_add(iend, length);
-
-                       if (icaltime_compare(iend, start) <= 0)
-                               continue; // instance ends before start time
-                       if (icaltime_compare(istart, end) >= 0)
-                               break;    // instance begins after stop time
+               }
 
+               /* Add all recurrences */
+               rrule = icalcomponent_get_first_property(comp, ICAL_RRULE_PROPERTY);
+
+               /* One-time event */
+               if (!rrule) {
                        icalarray_append(array, &(ical_inst){
                                .cal   = cal,
                                .comp  = comp,
-                               .start = istart,
-                               .end   = iend,
+                               .start = cstart,
+                               .end   = cend,
                        });
                }
 
-               icalrecur_iterator_free(iter);
+               /* Recurring events */
+               while (rrule) {
+                       recur = icalproperty_get_rrule(rrule);
+                       iter  = icalrecur_iterator_new(recur, cstart);
+
+                       /* Add recurrence for this rrule */
+                       while (1) {
+                               istart = iend = icalrecur_iterator_next(iter);
+                               if (icaltime_is_null_time(istart))
+                                       break;    // no more instances
+                               if (!icaltime_is_null_time(cend))
+                                       iend = icaltime_add(iend, length);
+
+                               if (icaltime_compare(iend, start) <= 0)
+                                       continue; // instance ends before start time
+                               if (icaltime_compare(istart, end) >= 0)
+                                       break;    // instance begins after stop time
+
+                               icalarray_append(array, &(ical_inst){
+                                       .cal   = cal,
+                                       .comp  = comp,
+                                       .start = istart,
+                                       .end   = iend,
+                               });
+                       }
+
+                       icalrecur_iterator_free(iter);
+                       rrule = icalcomponent_get_next_property(comp, ICAL_RRULE_PROPERTY);
+               }
        }
 
        /* Add children */
@@ -149,21 +156,24 @@ static void add_recur(cal_t *cal,
 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();
+               if (!cal->location)
+                       debug("Missing location for ical '%s'", cal->cal.name);
+               if (cal->comp)
+                       continue;
+               wordexp_t wexp;
+               wordexp(cal->location, &wexp, WRDE_NOCMD);
+               icalparser *parser = icalparser_new();
+               if (wexp.we_wordc > 1)
+                       debug("Multiple calendards are not supported '%s'", cal->location);
+               FILE *file = fopen(wexp.we_wordv[0], "r");
+               if (!file) {
+                       debug("Cannot open ical file '%s'", wexp.we_wordv[0]);
+               } else {
                        icalparser_set_gen_data(parser, file);
                        cal->comp = icalparser_parse(parser, (void*)fgets);
                        icalparser_free(parser);
                }
+               wordfree(&wexp);
        }
 }
 
@@ -266,7 +276,6 @@ void ical_config(const char *group, const char *name, const char *key, const cha
                        last->next = cal;
                else
                        calendars = cal;
-               return;
        }
 
        /* Set calendar values */
@@ -344,10 +353,10 @@ void ical_printr(icalcomponent *comp, int depth)
        }
 }
 
-void ical_test(void)
+void ical_test(char *path)
 {
        /* Load ical */
-       FILE *file = fopen("data/all.ics", "r");
+       FILE *file = fopen(path, "r");
        icalparser *parser = icalparser_new();
        icalparser_set_gen_data(parser, file);
        icalcomponent *comp = icalparser_parse(parser, (void*)fgets);
@@ -372,8 +381,8 @@ void ical_test(void)
        icalarray_free(array);
 
        /* Print */
-       //ical_printr(comp, 0);
-       //print_events(events);
+       ical_printr(comp, 0);
+       print_events(events);
        print_todos(todos);
 
        (void)print_events;