]> Pileus Git - lackey/blob - cals/ical.c
Add simple config file parser
[lackey] / cals / ical.c
1 /*
2  * Copyright (C) 2012 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         char          *name;
39         char          *location;
40         char          *username;
41         char          *password;
42         icalcomponent *ical;
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->ical == 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->ical = 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->name, name))
252                         break;
253
254         /* Create new calendar */
255         if (!cal) {
256                 cal = new0(ical_t);
257                 cal->name = get_name(name);
258                 if (last)
259                         last->next = cal;
260                 else
261                         calendars = cal;
262                 return;
263         }
264
265         /* Set calendar values */
266         if (match(key, "location"))
267                 cal->location = get_string(value);
268         else if (match(key, "username"))
269                 cal->username = get_string(value);
270         else if (match(key, "password"))
271                 cal->password = get_string(value);
272 }
273
274 /* Event functions */
275 event_t *ical_events(date_t _start, date_t _end)
276 {
277         read_icals();
278
279         icaltimetype start = to_itime(_start);
280         icaltimetype end   = to_itime(_end);
281         icalarray *array = icalarray_new(sizeof(ical_inst), 1);
282         for (ical_t *cal = calendars; cal; cal = cal->next)
283                 add_recur(array, cal->ical, start, end, ICAL_VEVENT_COMPONENT);
284         icalarray_sort(array, ical_compare);
285         event_t *events = to_events(array);
286         icalarray_free(array);
287
288         return events;
289 }
290
291 /* Todo functions */
292 todo_t *ical_todos(date_t _start, date_t _end)
293 {
294         read_icals();
295
296         icaltimetype start = to_itime(_start);
297         icaltimetype end   = to_itime(_end);
298         icalarray *array = icalarray_new(sizeof(ical_inst), 1);
299         for (ical_t *cal = calendars; cal; cal = cal->next)
300                 add_recur(array, cal->ical, start, end, ICAL_VTODO_COMPONENT);
301         icalarray_sort(array, ical_compare);
302         todo_t *todos = to_todos(array);
303         icalarray_free(array);
304
305         return todos;
306 }
307
308 /* Test functions */
309 void ical_printr(icalcomponent *comp, int depth)
310 {
311         /* Print component */
312         icalcomponent_kind kind = icalcomponent_isa(comp);
313         printf("%*s", depth, "");
314         printf("%s",  icalcomponent_kind_to_string(kind));
315         if (kind == ICAL_VEVENT_COMPONENT ||
316             kind == ICAL_VTODO_COMPONENT)
317                 printf(" - %s", icalcomponent_get_summary(comp) ?: "[no summary]");
318         printf("\n");
319
320         /* Print children */
321         icalcomponent_kind find = ICAL_ANY_COMPONENT;
322         icalcomponent *child = icalcomponent_get_first_component(comp, find);
323         while (child) {
324                 ical_printr(child, depth+2);
325                 child = icalcomponent_get_next_component(comp, find);
326         }
327 }
328
329 void ical_test(void)
330 {
331         /* Load ical */
332         FILE *file = fopen("data/all.ics", "r");
333         icalparser *parser = icalparser_new();
334         icalparser_set_gen_data(parser, file);
335         icalcomponent *ical = icalparser_parse(parser, (void*)fgets);
336
337         /* Misc */
338         icalarray *array;
339         icaltimetype start = {.year = 2000};
340         icaltimetype end   = {.year = 2020};
341
342         /* Find events */
343         array = icalarray_new(sizeof(ical_inst), 1);
344         add_recur(array, ical, start, end, ICAL_VEVENT_COMPONENT);
345         icalarray_sort(array, ical_compare);
346         event_t *events = to_events(array);
347         icalarray_free(array);
348
349         /* Find Todos */
350         array = icalarray_new(sizeof(ical_inst), 1);
351         add_recur(array, ical, start, end, ICAL_VTODO_COMPONENT);
352         icalarray_sort(array, ical_compare);
353         todo_t *todos = to_todos(array);
354         icalarray_free(array);
355
356         /* Print */
357         //ical_printr(ical, 0);
358         //print_events(events);
359         print_todos(todos);
360
361         (void)print_events;
362         (void)print_todos;
363         (void)events;
364         (void)todos;
365
366         /* Cleanup */
367         icalparser_free(parser);
368 }