2 * Copyright (C) 2012-2013 Andy Spencer <andy753421@gmail.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <libical/ical.h>
34 struct icaltimetype start;
35 struct icaltimetype end;
38 typedef struct ical_t {
48 static ical_t *calendars;
50 /* Helper functions */
51 static int ical_compare(const void *_a, const void *_b)
53 const ical_inst *a = _a;
54 const ical_inst *b = _b;
55 int scomp = icaltime_compare(a->start, b->start);
56 int ecomp = icaltime_compare(a->end, b->end);
57 return scomp != 0 ? scomp :
58 ecomp != 0 ? ecomp : 0 ;
61 static date_t to_date(struct icaltimetype time)
65 .month = time.month ? time.month-1 : 0,
66 .day = time.day ? time.day -1 : 0,
72 static icaltimetype to_itime(date_t time)
74 return (struct icaltimetype){
76 .month = time.month + 1,
83 static void add_recur(cal_t *cal,
84 icalarray *array, icalcomponent *comp,
85 icaltimetype start, icaltimetype end,
86 icalcomponent_kind which)
88 icalcomponent_kind kind = icalcomponent_isa(comp);
91 /* Get recurrence data */
92 struct icaltimetype cstart, cend; // Component times
93 struct icaltimetype istart, iend; // Instance times
94 struct icaldurationtype length; // Duration
97 struct icalrecurrencetype recur;
98 icalrecur_iterator *iter;
100 cstart = icalcomponent_get_dtstart(comp);
101 cend = icalcomponent_get_dtend(comp);
102 length = icaltime_subtract(cend, cstart);
105 if (icaltime_is_null_time(cstart) ||
106 which == ICAL_VTODO_COMPONENT) {
107 icalarray_append(array, &(ical_inst){
115 /* Add all recurrences */
116 rrule = icalcomponent_get_first_property(comp, ICAL_RRULE_PROPERTY);
120 icalarray_append(array, &(ical_inst){
128 /* Recurring events */
130 recur = icalproperty_get_rrule(rrule);
131 iter = icalrecur_iterator_new(recur, cstart);
133 /* Add recurrence for this rrule */
135 istart = iend = icalrecur_iterator_next(iter);
136 if (icaltime_is_null_time(istart))
137 break; // no more instances
138 if (!icaltime_is_null_time(cend))
139 iend = icaltime_add(iend, length);
141 if (icaltime_compare(iend, start) <= 0)
142 continue; // instance ends before start time
143 if (icaltime_compare(istart, end) >= 0)
144 break; // instance begins after stop time
146 icalarray_append(array, &(ical_inst){
154 icalrecur_iterator_free(iter);
155 rrule = icalcomponent_get_next_property(comp, ICAL_RRULE_PROPERTY);
160 icalcomponent_kind find = ICAL_ANY_COMPONENT;
161 icalcomponent *child = icalcomponent_get_first_component(comp, find);
163 add_recur(cal, array, child, start, end, which);
164 child = icalcomponent_get_next_component(comp, find);
168 static void read_icals(void)
170 for (ical_t *cal = calendars; cal; cal = cal->next) {
172 debug("Missing location for ical '%s'", cal->cal.name);
176 wordexp(cal->location, &wexp, WRDE_NOCMD);
177 icalparser *parser = icalparser_new();
178 if (wexp.we_wordc > 1)
179 debug("Multiple calendards are not supported '%s'", cal->location);
180 FILE *file = fopen(wexp.we_wordv[0], "r");
182 debug("Cannot open ical file '%s'", wexp.we_wordv[0]);
184 icalparser_set_gen_data(parser, file);
185 cal->comp = icalparser_parse(parser, (void*)fgets);
186 icalparser_free(parser);
192 /* Event functions */
193 static event_t *to_event(ical_inst *inst)
195 icalproperty *prop = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
197 event_t *event = new0(event_t);
198 event->name = strcopy(icalcomponent_get_summary(inst->comp));
199 event->desc = strcopy(icalcomponent_get_description(inst->comp));
200 event->loc = strcopy(icalcomponent_get_location(inst->comp));
201 event->cat = icalproperty_get_value_as_string_r(prop);
202 event->start = to_date(inst->start);
203 event->end = to_date(inst->end);
204 event->cal = inst->cal;
208 static event_t *to_events(icalarray *array)
211 event_t *tail = &list;
212 for (int i = 0; i < array->num_elements; i++) {
213 ical_inst *inst = icalarray_element_at(array, i);
214 tail->next = to_event(inst);
220 static void print_events(event_t *start)
222 for (event_t *cur = start; cur; cur = cur->next)
223 printf("%04d-%02d-%02d %02d:%02d - %s\n",
224 cur->start.year, cur->start.month, cur->start.day,
225 cur->start.hour, cur->start.min,
226 cur->name ?: cur->desc ?: "[no summary]");
230 static todo_t *to_todo(ical_inst *inst)
232 icalproperty *cat = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
233 icalproperty *perc = icalcomponent_get_first_property(inst->comp, ICAL_PERCENTCOMPLETE_PROPERTY);
235 todo_t *todo = new0(todo_t);
236 todo->name = strcopy(icalcomponent_get_summary(inst->comp));
237 todo->desc = strcopy(icalcomponent_get_description(inst->comp));
238 todo->cat = strcopy(icalproperty_get_value_as_string(cat));
239 todo->status = icalcomponent_get_status(inst->comp) == ICAL_STATUS_COMPLETED ? 100 :
240 perc ? icalproperty_get_percentcomplete(perc) : 0;
241 todo->start = to_date(inst->start);
242 todo->due = to_date(icalcomponent_get_due(inst->comp));
243 todo->cal = inst->cal;
247 static todo_t *to_todos(icalarray *array)
250 todo_t *tail = &list;
251 for (int i = 0; i < array->num_elements; i++) {
252 ical_inst *inst = icalarray_element_at(array, i);
253 tail->next = to_todo(inst);
259 static void print_todos(todo_t *start)
261 for (todo_t *cur = start; cur; cur = cur->next)
262 printf("%04d-%02d-%02d %02d:%02d - %d%% - %s\n",
263 cur->due.year, cur->due.month, cur->due.day,
264 cur->due.hour, cur->due.min, cur->status,
265 cur->name ?: cur->desc ?: "[no summary]");
269 void ical_config(const char *group, const char *name, const char *key, const char *value)
271 ical_t *cal = NULL, *last = NULL;
273 /* Make sure it's valid */
274 if (!match(group, "ical") || !name)
277 /* Find existing calendar */
278 for (cal = calendars; cal; last = cal, cal = cal->next)
279 if (match(cal->cal.name, name))
282 /* Create new calendar */
285 cal->cal.type = "ical";
286 cal->cal.name = get_name(name);
293 /* Set calendar values */
294 if (match(key, "location"))
295 cal->location = get_string(value);
296 else if (match(key, "username"))
297 cal->username = get_string(value);
298 else if (match(key, "password"))
299 cal->password = get_string(value);
303 cal_t *ical_cals(void)
307 for (ical_t *cal = calendars; cal; cal = cal->next)
308 cal->cal.next = &cal->next->cal;
310 return &calendars->cal;
313 /* Event functions */
314 event_t *ical_events(date_t _start, date_t _end)
318 icaltimetype start = to_itime(_start);
319 icaltimetype end = to_itime(_end);
320 icalarray *array = icalarray_new(sizeof(ical_inst), 1);
321 for (ical_t *cal = calendars; cal; cal = cal->next)
322 add_recur(&cal->cal, array, cal->comp, start, end, ICAL_VEVENT_COMPONENT);
323 icalarray_sort(array, ical_compare);
324 event_t *events = to_events(array);
325 icalarray_free(array);
331 todo_t *ical_todos(date_t _start, date_t _end)
335 icaltimetype start = to_itime(_start);
336 icaltimetype end = to_itime(_end);
337 icalarray *array = icalarray_new(sizeof(ical_inst), 1);
338 for (ical_t *cal = calendars; cal; cal = cal->next)
339 add_recur(&cal->cal, array, cal->comp, start, end, ICAL_VTODO_COMPONENT);
340 icalarray_sort(array, ical_compare);
341 todo_t *todos = to_todos(array);
342 icalarray_free(array);
348 void ical_printr(icalcomponent *comp, int depth)
350 /* Print component */
351 icalcomponent_kind kind = icalcomponent_isa(comp);
352 printf("%*s", depth, "");
353 printf("%s", icalcomponent_kind_to_string(kind));
354 if (kind == ICAL_VEVENT_COMPONENT ||
355 kind == ICAL_VTODO_COMPONENT)
356 printf(" - %s", icalcomponent_get_summary(comp) ?: "[no summary]");
360 icalcomponent_kind find = ICAL_ANY_COMPONENT;
361 icalcomponent *child = icalcomponent_get_first_component(comp, find);
363 ical_printr(child, depth+2);
364 child = icalcomponent_get_next_component(comp, find);
368 void ical_test(char *path)
371 FILE *file = fopen(path, "r");
372 icalparser *parser = icalparser_new();
373 icalparser_set_gen_data(parser, file);
374 icalcomponent *comp = icalparser_parse(parser, (void*)fgets);
378 icaltimetype start = {.year = 2000};
379 icaltimetype end = {.year = 2020};
382 array = icalarray_new(sizeof(ical_inst), 1);
383 add_recur(NULL, array, comp, start, end, ICAL_VEVENT_COMPONENT);
384 icalarray_sort(array, ical_compare);
385 event_t *events = to_events(array);
386 icalarray_free(array);
389 array = icalarray_new(sizeof(ical_inst), 1);
390 add_recur(NULL, array, comp, start, end, ICAL_VTODO_COMPONENT);
391 icalarray_sort(array, ical_compare);
392 todo_t *todos = to_todos(array);
393 icalarray_free(array);
396 ical_printr(comp, 0);
397 print_events(events);
406 icalparser_free(parser);