]> Pileus Git - lackey/blob - cals/ical.c
Add support for timezones
[lackey] / cals / ical.c
1 /*
2  * Copyright (C) 2012-2013 Andy Spencer <andy753421@gmail.com>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #define _XOPEN_SOURCE
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <wordexp.h>
23 #include <libical/ical.h>
24
25 #include "util.h"
26 #include "conf.h"
27 #include "date.h"
28 #include "cal.h"
29
30 /* Local types */
31 typedef struct {
32         cal_t *cal;
33         icalcomponent *comp;
34         struct icaltimetype start;
35         struct icaltimetype end;
36 } ical_inst;
37
38 typedef struct ical_t {
39         cal_t          cal;
40         char          *location;
41         char          *username;
42         char          *password;
43         icalcomponent *comp;
44         struct ical_t *next;
45 } ical_t;
46
47 /* Static data */
48 static ical_t *calendars;
49
50 /* Helper functions */
51 static int ical_compare(const void *_a, const void *_b)
52 {
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 ;
59 }
60
61 static date_t to_date(struct icaltimetype itime)
62 {
63         return get_date(icaltime_as_timet_with_zone(itime, itime.zone));
64 }
65
66 static icaltimetype to_itime(date_t date)
67 {
68         return icaltime_from_timet_with_zone(get_stamp(date), 0, NULL);
69 }
70
71 static void add_recur(cal_t *cal,
72                 icalarray *array, icalcomponent *comp,
73                 icaltimetype start, icaltimetype end,
74                 icalcomponent_kind which)
75 {
76         icalcomponent_kind kind = icalcomponent_isa(comp);
77
78         if (kind == which) {
79                 /* Get recurrence data */
80                 struct icaltimetype cstart, cend; // Component times
81                 struct icaltimetype istart, iend; // Instance times
82                 struct icaldurationtype length;   // Duration
83
84                 icalproperty             *rrule;
85                 struct icalrecurrencetype recur;
86                 icalrecur_iterator       *iter;
87
88                 cstart = icalcomponent_get_dtstart(comp);
89                 cend   = icalcomponent_get_dtend(comp);
90                 length = icaltime_subtract(cend, cstart);
91
92                 /* Full day event */
93                 if (icaltime_is_null_time(cstart) ||
94                     which == ICAL_VTODO_COMPONENT) {
95                         icalarray_append(array, &(ical_inst){
96                                 .cal   = cal,
97                                 .comp  = comp,
98                                 .start = cstart,
99                                 .end   = cend,
100                         });
101                 }
102
103                 /* Add all recurrences */
104                 rrule = icalcomponent_get_first_property(comp, ICAL_RRULE_PROPERTY);
105
106                 /* One-time event */
107                 if (!rrule) {
108                         icalarray_append(array, &(ical_inst){
109                                 .cal   = cal,
110                                 .comp  = comp,
111                                 .start = cstart,
112                                 .end   = cend,
113                         });
114                 }
115
116                 /* Recurring events */
117                 while (rrule) {
118                         recur = icalproperty_get_rrule(rrule);
119                         iter  = icalrecur_iterator_new(recur, cstart);
120
121                         /* Add recurrence for this rrule */
122                         while (1) {
123                                 istart = iend = icalrecur_iterator_next(iter);
124                                 if (icaltime_is_null_time(istart))
125                                         break;    // no more instances
126                                 if (!icaltime_is_null_time(cend))
127                                         iend = icaltime_add(iend, length);
128
129                                 if (icaltime_compare(iend, start) <= 0)
130                                         continue; // instance ends before start time
131                                 if (icaltime_compare(istart, end) >= 0)
132                                         break;    // instance begins after stop time
133
134                                 icalarray_append(array, &(ical_inst){
135                                         .cal   = cal,
136                                         .comp  = comp,
137                                         .start = istart,
138                                         .end   = iend,
139                                 });
140                         }
141
142                         icalrecur_iterator_free(iter);
143                         rrule = icalcomponent_get_next_property(comp, ICAL_RRULE_PROPERTY);
144                 }
145         }
146
147         /* Add children */
148         icalcomponent_kind find = ICAL_ANY_COMPONENT;
149         icalcomponent *child = icalcomponent_get_first_component(comp, find);
150         while (child) {
151                 add_recur(cal, array, child, start, end, which);
152                 child = icalcomponent_get_next_component(comp, find);
153         }
154 }
155
156 static void read_icals(void)
157 {
158         for (ical_t *cal = calendars; cal; cal = cal->next) {
159                 if (!cal->location)
160                         debug("Missing location for ical '%s'", cal->cal.name);
161                 if (cal->comp)
162                         continue;
163                 wordexp_t wexp;
164                 wordexp(cal->location, &wexp, WRDE_NOCMD);
165                 icalparser *parser = icalparser_new();
166                 if (wexp.we_wordc > 1)
167                         debug("Multiple calendards are not supported '%s'", cal->location);
168                 FILE *file = fopen(wexp.we_wordv[0], "r");
169                 if (!file) {
170                         debug("Cannot open ical file '%s'", wexp.we_wordv[0]);
171                 } else {
172                         icalparser_set_gen_data(parser, file);
173                         cal->comp = icalparser_parse(parser, (void*)fgets);
174                         icalparser_free(parser);
175                 }
176                 wordfree(&wexp);
177         }
178 }
179
180 /* Event functions */
181 static event_t *to_event(ical_inst *inst)
182 {
183         icalproperty *prop = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
184
185         event_t *event = new0(event_t);
186         event->name  = strcopy(icalcomponent_get_summary(inst->comp));
187         event->desc  = strcopy(icalcomponent_get_description(inst->comp));
188         event->loc   = strcopy(icalcomponent_get_location(inst->comp));
189         event->cat   = icalproperty_get_value_as_string_r(prop);
190         event->start = to_date(inst->start);
191         event->end   = to_date(inst->end);
192         event->cal   = inst->cal;
193         return event;
194 }
195
196 static event_t *to_events(icalarray *array)
197 {
198         event_t  list = {};
199         event_t *tail = &list;
200         for (int i = 0; i < array->num_elements; i++) {
201                  ical_inst *inst = icalarray_element_at(array, i);
202                  tail->next = to_event(inst);
203                  tail = tail->next;
204         }
205         return list.next;
206 }
207
208 static void print_events(event_t *start)
209 {
210         for (event_t *cur = start; cur; cur = cur->next)
211                 printf("%04d-%02d-%02d %02d:%02d - %s\n",
212                         cur->start.year, cur->start.month, cur->start.day,
213                         cur->start.hour, cur->start.min,
214                         cur->name ?: cur->desc ?: "[no summary]");
215 }
216
217 /* Todo functions */
218 static todo_t *to_todo(ical_inst *inst)
219 {
220         icalproperty *cat  = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
221         icalproperty *perc = icalcomponent_get_first_property(inst->comp, ICAL_PERCENTCOMPLETE_PROPERTY);
222
223         todo_t *todo = new0(todo_t);
224         todo->name   = strcopy(icalcomponent_get_summary(inst->comp));
225         todo->desc   = strcopy(icalcomponent_get_description(inst->comp));
226         todo->cat    = strcopy(icalproperty_get_value_as_string(cat));
227         todo->status = icalcomponent_get_status(inst->comp) == ICAL_STATUS_COMPLETED ? 100 :
228                        perc ? icalproperty_get_percentcomplete(perc) : 0;
229         todo->start  = to_date(inst->start);
230         todo->due    = to_date(icalcomponent_get_due(inst->comp));
231         todo->cal    = inst->cal;
232         return todo;
233 }
234
235 static todo_t *to_todos(icalarray *array)
236 {
237         todo_t  list = {};
238         todo_t *tail = &list;
239         for (int i = 0; i < array->num_elements; i++) {
240                  ical_inst *inst = icalarray_element_at(array, i);
241                  tail->next = to_todo(inst);
242                  tail = tail->next;
243         }
244         return list.next;
245 }
246
247 static void print_todos(todo_t *start)
248 {
249         for (todo_t *cur = start; cur; cur = cur->next)
250                 printf("%04d-%02d-%02d %02d:%02d - %d%% - %s\n",
251                         cur->due.year, cur->due.month, cur->due.day,
252                         cur->due.hour, cur->due.min,   cur->status,
253                         cur->name ?: cur->desc ?: "[no summary]");
254 }
255
256 /* Config parser */
257 void ical_config(const char *group, const char *name, const char *key, const char *value)
258 {
259         ical_t *cal = NULL, *last = NULL;
260
261         /* Make sure it's valid */
262         if (!match(group, "ical") || !name)
263                 return;
264
265         /* Find existing calendar */
266         for (cal = calendars; cal; last = cal, cal = cal->next)
267                 if (match(cal->cal.name, name))
268                         break;
269
270         /* Create new calendar */
271         if (!cal) {
272                 cal = new0(ical_t);
273                 cal->cal.type = "ical";
274                 cal->cal.name = get_name(name);
275                 if (last)
276                         last->next = cal;
277                 else
278                         calendars = cal;
279         }
280
281         /* Set calendar values */
282         if (match(key, "location"))
283                 cal->location = get_string(value);
284         else if (match(key, "username"))
285                 cal->username = get_string(value);
286         else if (match(key, "password"))
287                 cal->password = get_string(value);
288 }
289
290 /* Cal functions */
291 cal_t *ical_cals(void)
292 {
293         read_icals();
294
295         for (ical_t *cal = calendars; cal; cal = cal->next)
296                 cal->cal.next = &cal->next->cal;
297
298         return &calendars->cal;
299 }
300
301 /* Event functions */
302 event_t *ical_events(date_t _start, date_t _end)
303 {
304         read_icals();
305
306         icaltimetype start = to_itime(_start);
307         icaltimetype end   = to_itime(_end);
308         icalarray *array = icalarray_new(sizeof(ical_inst), 1);
309         for (ical_t *cal = calendars; cal; cal = cal->next)
310                 add_recur(&cal->cal, array, cal->comp, start, end, ICAL_VEVENT_COMPONENT);
311         icalarray_sort(array, ical_compare);
312         event_t *events = to_events(array);
313         icalarray_free(array);
314
315         return events;
316 }
317
318 /* Todo functions */
319 todo_t *ical_todos(date_t _start, date_t _end)
320 {
321         read_icals();
322
323         icaltimetype start = to_itime(_start);
324         icaltimetype end   = to_itime(_end);
325         icalarray *array = icalarray_new(sizeof(ical_inst), 1);
326         for (ical_t *cal = calendars; cal; cal = cal->next)
327                 add_recur(&cal->cal, array, cal->comp, start, end, ICAL_VTODO_COMPONENT);
328         icalarray_sort(array, ical_compare);
329         todo_t *todos = to_todos(array);
330         icalarray_free(array);
331
332         return todos;
333 }
334
335 /* Test functions */
336 void ical_printr(icalcomponent *comp, int depth)
337 {
338         /* Print component */
339         icalcomponent_kind kind = icalcomponent_isa(comp);
340         printf("%*s", depth, "");
341         printf("%s",  icalcomponent_kind_to_string(kind));
342         if (kind == ICAL_VEVENT_COMPONENT ||
343             kind == ICAL_VTODO_COMPONENT)
344                 printf(" - %s", icalcomponent_get_summary(comp) ?: "[no summary]");
345         printf("\n");
346
347         /* Print children */
348         icalcomponent_kind find = ICAL_ANY_COMPONENT;
349         icalcomponent *child = icalcomponent_get_first_component(comp, find);
350         while (child) {
351                 ical_printr(child, depth+2);
352                 child = icalcomponent_get_next_component(comp, find);
353         }
354 }
355
356 void ical_test(char *path)
357 {
358         /* Load ical */
359         FILE *file = fopen(path, "r");
360         icalparser *parser = icalparser_new();
361         icalparser_set_gen_data(parser, file);
362         icalcomponent *comp = icalparser_parse(parser, (void*)fgets);
363
364         /* Misc */
365         icalarray *array;
366         icaltimetype start = {.year = 2000};
367         icaltimetype end   = {.year = 2020};
368
369         /* Find events */
370         array = icalarray_new(sizeof(ical_inst), 1);
371         add_recur(NULL, array, comp, start, end, ICAL_VEVENT_COMPONENT);
372         icalarray_sort(array, ical_compare);
373         event_t *events = to_events(array);
374         icalarray_free(array);
375
376         /* Find Todos */
377         array = icalarray_new(sizeof(ical_inst), 1);
378         add_recur(NULL, array, comp, start, end, ICAL_VTODO_COMPONENT);
379         icalarray_sort(array, ical_compare);
380         todo_t *todos = to_todos(array);
381         icalarray_free(array);
382
383         /* Print */
384         ical_printr(comp, 0);
385         print_events(events);
386         print_todos(todos);
387
388         (void)print_events;
389         (void)print_todos;
390         (void)events;
391         (void)todos;
392
393         /* Cleanup */
394         icalparser_free(parser);
395 }