]> Pileus Git - lackey/blob - cals/ical.c
Make testing code build time configurable
[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                 rrule  = icalcomponent_get_first_property(comp, ICAL_RRULE_PROPERTY);
105                 recur  = icalproperty_get_rrule(rrule);
106                 iter   = icalrecur_iterator_new(recur, cstart);
107
108                 /* Add recurrences */
109                 if (icaltime_is_null_time(cstart) ||
110                     which == ICAL_VTODO_COMPONENT) {
111                         icalarray_append(array, &(ical_inst){
112                                 .cal   = cal,
113                                 .comp  = comp,
114                                 .start = cstart,
115                                 .end   = cend,
116                         });
117                 } else while (1) {
118                         istart = iend = icalrecur_iterator_next(iter);
119                         if (icaltime_is_null_time(istart))
120                                 break;    // no more instances
121                         if (!icaltime_is_null_time(cend))
122                                 iend = icaltime_add(iend, length);
123
124                         if (icaltime_compare(iend, start) <= 0)
125                                 continue; // instance ends before start time
126                         if (icaltime_compare(istart, end) >= 0)
127                                 break;    // instance begins after stop time
128
129                         icalarray_append(array, &(ical_inst){
130                                 .cal   = cal,
131                                 .comp  = comp,
132                                 .start = istart,
133                                 .end   = iend,
134                         });
135                 }
136
137                 icalrecur_iterator_free(iter);
138         }
139
140         /* Add children */
141         icalcomponent_kind find = ICAL_ANY_COMPONENT;
142         icalcomponent *child = icalcomponent_get_first_component(comp, find);
143         while (child) {
144                 add_recur(cal, array, child, start, end, which);
145                 child = icalcomponent_get_next_component(comp, find);
146         }
147 }
148
149 static void read_icals(void)
150 {
151         for (ical_t *cal = calendars; cal; cal = cal->next) {
152                 if (cal->comp == NULL && cal->location) {
153                         wordexp_t wexp;
154                         wordexp(cal->location, &wexp, WRDE_NOCMD);
155                         if (wexp.we_wordc == 0)
156                                 continue;
157                         FILE *file = fopen(wexp.we_wordv[0], "r");
158                         wordfree(&wexp);
159                         if (!file)
160                                 continue;
161
162                         icalparser *parser = icalparser_new();
163                         icalparser_set_gen_data(parser, file);
164                         cal->comp = icalparser_parse(parser, (void*)fgets);
165                         icalparser_free(parser);
166                 }
167         }
168 }
169
170 /* Event functions */
171 static event_t *to_event(ical_inst *inst)
172 {
173         icalproperty *prop = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
174
175         event_t *event = new0(event_t);
176         event->name  = strcopy(icalcomponent_get_summary(inst->comp));
177         event->desc  = strcopy(icalcomponent_get_description(inst->comp));
178         event->loc   = strcopy(icalcomponent_get_location(inst->comp));
179         event->cat   = icalproperty_get_value_as_string_r(prop);
180         event->start = to_date(inst->start);
181         event->end   = to_date(inst->end);
182         event->cal   = inst->cal;
183         return event;
184 }
185
186 static event_t *to_events(icalarray *array)
187 {
188         event_t  list = {};
189         event_t *tail = &list;
190         for (int i = 0; i < array->num_elements; i++) {
191                  ical_inst *inst = icalarray_element_at(array, i);
192                  tail->next = to_event(inst);
193                  tail = tail->next;
194         }
195         return list.next;
196 }
197
198 static void print_events(event_t *start)
199 {
200         for (event_t *cur = start; cur; cur = cur->next)
201                 printf("%04d-%02d-%02d %02d:%02d - %s\n",
202                         cur->start.year, cur->start.month, cur->start.day,
203                         cur->start.hour, cur->start.min,
204                         cur->name ?: cur->desc ?: "[no summary]");
205 }
206
207 /* Todo functions */
208 static todo_t *to_todo(ical_inst *inst)
209 {
210         icalproperty *cat  = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
211         icalproperty *perc = icalcomponent_get_first_property(inst->comp, ICAL_PERCENTCOMPLETE_PROPERTY);
212
213         todo_t *todo = new0(todo_t);
214         todo->name   = strcopy(icalcomponent_get_summary(inst->comp));
215         todo->desc   = strcopy(icalcomponent_get_description(inst->comp));
216         todo->cat    = strcopy(icalproperty_get_value_as_string(cat));
217         todo->status = icalcomponent_get_status(inst->comp) == ICAL_STATUS_COMPLETED ? 100 :
218                        perc ? icalproperty_get_percentcomplete(perc) : 0;
219         todo->start  = to_date(inst->start);
220         todo->due    = to_date(icalcomponent_get_due(inst->comp));
221         todo->cal    = inst->cal;
222         return todo;
223 }
224
225 static todo_t *to_todos(icalarray *array)
226 {
227         todo_t  list = {};
228         todo_t *tail = &list;
229         for (int i = 0; i < array->num_elements; i++) {
230                  ical_inst *inst = icalarray_element_at(array, i);
231                  tail->next = to_todo(inst);
232                  tail = tail->next;
233         }
234         return list.next;
235 }
236
237 static void print_todos(todo_t *start)
238 {
239         for (todo_t *cur = start; cur; cur = cur->next)
240                 printf("%04d-%02d-%02d %02d:%02d - %d%% - %s\n",
241                         cur->due.year, cur->due.month, cur->due.day,
242                         cur->due.hour, cur->due.min,   cur->status,
243                         cur->name ?: cur->desc ?: "[no summary]");
244 }
245
246 /* Config parser */
247 void ical_config(const char *group, const char *name, const char *key, const char *value)
248 {
249         ical_t *cal = NULL, *last = NULL;
250
251         /* Make sure it's valid */
252         if (!match(group, "ical") || !name)
253                 return;
254
255         /* Find existing calendar */
256         for (cal = calendars; cal; last = cal, cal = cal->next)
257                 if (match(cal->cal.name, name))
258                         break;
259
260         /* Create new calendar */
261         if (!cal) {
262                 cal = new0(ical_t);
263                 cal->cal.type = "ical";
264                 cal->cal.name = get_name(name);
265                 if (last)
266                         last->next = cal;
267                 else
268                         calendars = cal;
269                 return;
270         }
271
272         /* Set calendar values */
273         if (match(key, "location"))
274                 cal->location = get_string(value);
275         else if (match(key, "username"))
276                 cal->username = get_string(value);
277         else if (match(key, "password"))
278                 cal->password = get_string(value);
279 }
280
281 /* Cal functions */
282 cal_t *ical_cals(void)
283 {
284         read_icals();
285
286         for (ical_t *cal = calendars; cal; cal = cal->next)
287                 cal->cal.next = &cal->next->cal;
288
289         return &calendars->cal;
290 }
291
292 /* Event functions */
293 event_t *ical_events(date_t _start, date_t _end)
294 {
295         read_icals();
296
297         icaltimetype start = to_itime(_start);
298         icaltimetype end   = to_itime(_end);
299         icalarray *array = icalarray_new(sizeof(ical_inst), 1);
300         for (ical_t *cal = calendars; cal; cal = cal->next)
301                 add_recur(&cal->cal, array, cal->comp, start, end, ICAL_VEVENT_COMPONENT);
302         icalarray_sort(array, ical_compare);
303         event_t *events = to_events(array);
304         icalarray_free(array);
305
306         return events;
307 }
308
309 /* Todo functions */
310 todo_t *ical_todos(date_t _start, date_t _end)
311 {
312         read_icals();
313
314         icaltimetype start = to_itime(_start);
315         icaltimetype end   = to_itime(_end);
316         icalarray *array = icalarray_new(sizeof(ical_inst), 1);
317         for (ical_t *cal = calendars; cal; cal = cal->next)
318                 add_recur(&cal->cal, array, cal->comp, start, end, ICAL_VTODO_COMPONENT);
319         icalarray_sort(array, ical_compare);
320         todo_t *todos = to_todos(array);
321         icalarray_free(array);
322
323         return todos;
324 }
325
326 /* Test functions */
327 void ical_printr(icalcomponent *comp, int depth)
328 {
329         /* Print component */
330         icalcomponent_kind kind = icalcomponent_isa(comp);
331         printf("%*s", depth, "");
332         printf("%s",  icalcomponent_kind_to_string(kind));
333         if (kind == ICAL_VEVENT_COMPONENT ||
334             kind == ICAL_VTODO_COMPONENT)
335                 printf(" - %s", icalcomponent_get_summary(comp) ?: "[no summary]");
336         printf("\n");
337
338         /* Print children */
339         icalcomponent_kind find = ICAL_ANY_COMPONENT;
340         icalcomponent *child = icalcomponent_get_first_component(comp, find);
341         while (child) {
342                 ical_printr(child, depth+2);
343                 child = icalcomponent_get_next_component(comp, find);
344         }
345 }
346
347 void ical_test(void)
348 {
349         /* Load ical */
350         FILE *file = fopen("data/all.ics", "r");
351         icalparser *parser = icalparser_new();
352         icalparser_set_gen_data(parser, file);
353         icalcomponent *comp = icalparser_parse(parser, (void*)fgets);
354
355         /* Misc */
356         icalarray *array;
357         icaltimetype start = {.year = 2000};
358         icaltimetype end   = {.year = 2020};
359
360         /* Find events */
361         array = icalarray_new(sizeof(ical_inst), 1);
362         add_recur(NULL, array, comp, start, end, ICAL_VEVENT_COMPONENT);
363         icalarray_sort(array, ical_compare);
364         event_t *events = to_events(array);
365         icalarray_free(array);
366
367         /* Find Todos */
368         array = icalarray_new(sizeof(ical_inst), 1);
369         add_recur(NULL, array, comp, start, end, ICAL_VTODO_COMPONENT);
370         icalarray_sort(array, ical_compare);
371         todo_t *todos = to_todos(array);
372         icalarray_free(array);
373
374         /* Print */
375         ical_printr(comp, 0);
376         print_events(events);
377         print_todos(todos);
378
379         (void)print_events;
380         (void)print_todos;
381         (void)events;
382         (void)todos;
383
384         /* Cleanup */
385         icalparser_free(parser);
386 }