]> Pileus Git - lackey/blob - cals/ical.c
4336e8157b305539cf50bd6762e5ebcd8942bb5c
[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 time)
62 {
63         return (date_t){
64                 .year  = time.year,
65                 .month = time.month ? time.month-1 : 0,
66                 .day   = time.day   ? time.day  -1 : 0,
67                 .hour  = time.hour,
68                 .min   = time.minute,
69         };
70 }
71
72 static icaltimetype to_itime(date_t time)
73 {
74         return (struct icaltimetype){
75                 .year   = time.year,
76                 .month  = time.month + 1,
77                 .day    = time.day   + 1,
78                 .hour   = time.hour,
79                 .minute = time.min
80         };
81 }
82
83 static void add_recur(cal_t *cal,
84                 icalarray *array, icalcomponent *comp,
85                 icaltimetype start, icaltimetype end,
86                 icalcomponent_kind which)
87 {
88         icalcomponent_kind kind = icalcomponent_isa(comp);
89
90         if (kind == which) {
91                 /* Get recurrence data */
92                 struct icaltimetype cstart, cend; // Component times
93                 struct icaltimetype istart, iend; // Instance times
94                 struct icaldurationtype length;   // Duration
95
96                 icalproperty             *rrule;
97                 struct icalrecurrencetype recur;
98                 icalrecur_iterator       *iter;
99
100                 cstart = icalcomponent_get_dtstart(comp);
101                 cend   = icalcomponent_get_dtend(comp);
102                 length = icaltime_subtract(cend, cstart);
103
104                 /* Full day event */
105                 if (icaltime_is_null_time(cstart) ||
106                     which == ICAL_VTODO_COMPONENT) {
107                         icalarray_append(array, &(ical_inst){
108                                 .cal   = cal,
109                                 .comp  = comp,
110                                 .start = cstart,
111                                 .end   = cend,
112                         });
113                 }
114
115                 /* Add all recurrences */
116                 rrule = icalcomponent_get_first_property(comp, ICAL_RRULE_PROPERTY);
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->comp == NULL && cal->location) {
160                         wordexp_t wexp;
161                         wordexp(cal->location, &wexp, WRDE_NOCMD);
162                         icalparser *parser = icalparser_new();
163                         for (int i = 0; i < wexp.we_wordc; i++) {
164                                 FILE *file = fopen(wexp.we_wordv[i], "r");
165                                 if (!file)
166                                         continue;
167                                 icalparser_set_gen_data(parser, file);
168                         }
169                         cal->comp = icalparser_parse(parser, (void*)fgets);
170                         icalparser_free(parser);
171                         wordfree(&wexp);
172                 }
173         }
174 }
175
176 /* Event functions */
177 static event_t *to_event(ical_inst *inst)
178 {
179         icalproperty *prop = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
180
181         event_t *event = new0(event_t);
182         event->name  = strcopy(icalcomponent_get_summary(inst->comp));
183         event->desc  = strcopy(icalcomponent_get_description(inst->comp));
184         event->loc   = strcopy(icalcomponent_get_location(inst->comp));
185         event->cat   = icalproperty_get_value_as_string_r(prop);
186         event->start = to_date(inst->start);
187         event->end   = to_date(inst->end);
188         event->cal   = inst->cal;
189         return event;
190 }
191
192 static event_t *to_events(icalarray *array)
193 {
194         event_t  list = {};
195         event_t *tail = &list;
196         for (int i = 0; i < array->num_elements; i++) {
197                  ical_inst *inst = icalarray_element_at(array, i);
198                  tail->next = to_event(inst);
199                  tail = tail->next;
200         }
201         return list.next;
202 }
203
204 static void print_events(event_t *start)
205 {
206         for (event_t *cur = start; cur; cur = cur->next)
207                 printf("%04d-%02d-%02d %02d:%02d - %s\n",
208                         cur->start.year, cur->start.month, cur->start.day,
209                         cur->start.hour, cur->start.min,
210                         cur->name ?: cur->desc ?: "[no summary]");
211 }
212
213 /* Todo functions */
214 static todo_t *to_todo(ical_inst *inst)
215 {
216         icalproperty *cat  = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
217         icalproperty *perc = icalcomponent_get_first_property(inst->comp, ICAL_PERCENTCOMPLETE_PROPERTY);
218
219         todo_t *todo = new0(todo_t);
220         todo->name   = strcopy(icalcomponent_get_summary(inst->comp));
221         todo->desc   = strcopy(icalcomponent_get_description(inst->comp));
222         todo->cat    = strcopy(icalproperty_get_value_as_string(cat));
223         todo->status = icalcomponent_get_status(inst->comp) == ICAL_STATUS_COMPLETED ? 100 :
224                        perc ? icalproperty_get_percentcomplete(perc) : 0;
225         todo->start  = to_date(inst->start);
226         todo->due    = to_date(icalcomponent_get_due(inst->comp));
227         todo->cal    = inst->cal;
228         return todo;
229 }
230
231 static todo_t *to_todos(icalarray *array)
232 {
233         todo_t  list = {};
234         todo_t *tail = &list;
235         for (int i = 0; i < array->num_elements; i++) {
236                  ical_inst *inst = icalarray_element_at(array, i);
237                  tail->next = to_todo(inst);
238                  tail = tail->next;
239         }
240         return list.next;
241 }
242
243 static void print_todos(todo_t *start)
244 {
245         for (todo_t *cur = start; cur; cur = cur->next)
246                 printf("%04d-%02d-%02d %02d:%02d - %d%% - %s\n",
247                         cur->due.year, cur->due.month, cur->due.day,
248                         cur->due.hour, cur->due.min,   cur->status,
249                         cur->name ?: cur->desc ?: "[no summary]");
250 }
251
252 /* Config parser */
253 void ical_config(const char *group, const char *name, const char *key, const char *value)
254 {
255         ical_t *cal = NULL, *last = NULL;
256
257         /* Make sure it's valid */
258         if (!match(group, "ical") || !name)
259                 return;
260
261         /* Find existing calendar */
262         for (cal = calendars; cal; last = cal, cal = cal->next)
263                 if (match(cal->cal.name, name))
264                         break;
265
266         /* Create new calendar */
267         if (!cal) {
268                 cal = new0(ical_t);
269                 cal->cal.type = "ical";
270                 cal->cal.name = get_name(name);
271                 if (last)
272                         last->next = cal;
273                 else
274                         calendars = cal;
275                 return;
276         }
277
278         /* Set calendar values */
279         if (match(key, "location"))
280                 cal->location = get_string(value);
281         else if (match(key, "username"))
282                 cal->username = get_string(value);
283         else if (match(key, "password"))
284                 cal->password = get_string(value);
285 }
286
287 /* Cal functions */
288 cal_t *ical_cals(void)
289 {
290         read_icals();
291
292         for (ical_t *cal = calendars; cal; cal = cal->next)
293                 cal->cal.next = &cal->next->cal;
294
295         return &calendars->cal;
296 }
297
298 /* Event functions */
299 event_t *ical_events(date_t _start, date_t _end)
300 {
301         read_icals();
302
303         icaltimetype start = to_itime(_start);
304         icaltimetype end   = to_itime(_end);
305         icalarray *array = icalarray_new(sizeof(ical_inst), 1);
306         for (ical_t *cal = calendars; cal; cal = cal->next)
307                 add_recur(&cal->cal, array, cal->comp, start, end, ICAL_VEVENT_COMPONENT);
308         icalarray_sort(array, ical_compare);
309         event_t *events = to_events(array);
310         icalarray_free(array);
311
312         return events;
313 }
314
315 /* Todo functions */
316 todo_t *ical_todos(date_t _start, date_t _end)
317 {
318         read_icals();
319
320         icaltimetype start = to_itime(_start);
321         icaltimetype end   = to_itime(_end);
322         icalarray *array = icalarray_new(sizeof(ical_inst), 1);
323         for (ical_t *cal = calendars; cal; cal = cal->next)
324                 add_recur(&cal->cal, array, cal->comp, start, end, ICAL_VTODO_COMPONENT);
325         icalarray_sort(array, ical_compare);
326         todo_t *todos = to_todos(array);
327         icalarray_free(array);
328
329         return todos;
330 }
331
332 /* Test functions */
333 void ical_printr(icalcomponent *comp, int depth)
334 {
335         /* Print component */
336         icalcomponent_kind kind = icalcomponent_isa(comp);
337         printf("%*s", depth, "");
338         printf("%s",  icalcomponent_kind_to_string(kind));
339         if (kind == ICAL_VEVENT_COMPONENT ||
340             kind == ICAL_VTODO_COMPONENT)
341                 printf(" - %s", icalcomponent_get_summary(comp) ?: "[no summary]");
342         printf("\n");
343
344         /* Print children */
345         icalcomponent_kind find = ICAL_ANY_COMPONENT;
346         icalcomponent *child = icalcomponent_get_first_component(comp, find);
347         while (child) {
348                 ical_printr(child, depth+2);
349                 child = icalcomponent_get_next_component(comp, find);
350         }
351 }
352
353 void ical_test(void)
354 {
355         /* Load ical */
356         FILE *file = fopen("data/all.ics", "r");
357         icalparser *parser = icalparser_new();
358         icalparser_set_gen_data(parser, file);
359         icalcomponent *comp = icalparser_parse(parser, (void*)fgets);
360
361         /* Misc */
362         icalarray *array;
363         icaltimetype start = {.year = 2000};
364         icaltimetype end   = {.year = 2020};
365
366         /* Find events */
367         array = icalarray_new(sizeof(ical_inst), 1);
368         add_recur(NULL, array, comp, start, end, ICAL_VEVENT_COMPONENT);
369         icalarray_sort(array, ical_compare);
370         event_t *events = to_events(array);
371         icalarray_free(array);
372
373         /* Find Todos */
374         array = icalarray_new(sizeof(ical_inst), 1);
375         add_recur(NULL, array, comp, start, end, ICAL_VTODO_COMPONENT);
376         icalarray_sort(array, ical_compare);
377         todo_t *todos = to_todos(array);
378         icalarray_free(array);
379
380         /* Print */
381         ical_printr(comp, 0);
382         print_events(events);
383         print_todos(todos);
384
385         (void)print_events;
386         (void)print_todos;
387         (void)events;
388         (void)todos;
389
390         /* Cleanup */
391         icalparser_free(parser);
392 }