X-Git-Url: http://pileus.org/git/?p=lackey;a=blobdiff_plain;f=cals%2Fical.c;h=b21cad0efb4f78852d41ccc9466810797be7d104;hp=29abe10d48622a65ee68af75976daf4e026661a3;hb=11458d86386ce37543f7fa26512de15db703df75;hpb=e1c3ab2bd61a81054b713f735fa31f827d258d76 diff --git a/cals/ical.c b/cals/ical.c index 29abe10..b21cad0 100644 --- a/cals/ical.c +++ b/cals/ical.c @@ -29,6 +29,7 @@ /* Local types */ typedef struct { + cal_t *cal; icalcomponent *comp; struct icaltimetype start; struct icaltimetype end; @@ -79,7 +80,8 @@ static icaltimetype to_itime(date_t time) }; } -static void add_recur(icalarray *array, icalcomponent *comp, +static void add_recur(cal_t *cal, + icalarray *array, icalcomponent *comp, icaltimetype start, icaltimetype end, icalcomponent_kind which) { @@ -99,45 +101,66 @@ static void add_recur(icalarray *array, icalcomponent *comp, 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){ + .cal = cal, .comp = comp, .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 */ icalcomponent_kind find = ICAL_ANY_COMPONENT; icalcomponent *child = icalcomponent_get_first_component(comp, find); while (child) { - add_recur(array, child, start, end, which); + add_recur(cal, array, child, start, end, which); child = icalcomponent_get_next_component(comp, find); } } @@ -145,21 +168,24 @@ 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(); + 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); } } @@ -175,6 +201,7 @@ static event_t *to_event(ical_inst *inst) event->cat = icalproperty_get_value_as_string_r(prop); event->start = to_date(inst->start); event->end = to_date(inst->end); + event->cal = inst->cal; return event; } @@ -213,6 +240,7 @@ static todo_t *to_todo(ical_inst *inst) perc ? icalproperty_get_percentcomplete(perc) : 0; todo->start = to_date(inst->start); todo->due = to_date(icalcomponent_get_due(inst->comp)); + todo->cal = inst->cal; return todo; } @@ -260,7 +288,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 */ @@ -292,7 +319,7 @@ event_t *ical_events(date_t _start, date_t _end) icaltimetype end = to_itime(_end); icalarray *array = icalarray_new(sizeof(ical_inst), 1); for (ical_t *cal = calendars; cal; cal = cal->next) - add_recur(array, cal->comp, start, end, ICAL_VEVENT_COMPONENT); + add_recur(&cal->cal, array, cal->comp, start, end, ICAL_VEVENT_COMPONENT); icalarray_sort(array, ical_compare); event_t *events = to_events(array); icalarray_free(array); @@ -309,7 +336,7 @@ todo_t *ical_todos(date_t _start, date_t _end) icaltimetype end = to_itime(_end); icalarray *array = icalarray_new(sizeof(ical_inst), 1); for (ical_t *cal = calendars; cal; cal = cal->next) - add_recur(array, cal->comp, start, end, ICAL_VTODO_COMPONENT); + add_recur(&cal->cal, array, cal->comp, start, end, ICAL_VTODO_COMPONENT); icalarray_sort(array, ical_compare); todo_t *todos = to_todos(array); icalarray_free(array); @@ -338,10 +365,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); @@ -353,21 +380,21 @@ void ical_test(void) /* Find events */ array = icalarray_new(sizeof(ical_inst), 1); - add_recur(array, comp, start, end, ICAL_VEVENT_COMPONENT); + add_recur(NULL, 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, comp, start, end, ICAL_VTODO_COMPONENT); + add_recur(NULL, array, comp, start, end, ICAL_VTODO_COMPONENT); icalarray_sort(array, ical_compare); todo_t *todos = to_todos(array); icalarray_free(array); /* Print */ - //ical_printr(comp, 0); - //print_events(events); + ical_printr(comp, 0); + print_events(events); print_todos(todos); (void)print_events;