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