]> Pileus Git - lackey/blob - cals/ical.c
Support multiple ical files
[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 "date.h"
27 #include "cal.h"
28
29 /* Local types */
30 typedef struct {
31         icalcomponent *comp;
32         struct icaltimetype start;
33         struct icaltimetype end;
34 } ical_inst;
35
36 typedef struct ical_t {
37         char          *name;
38         char          *location;
39         char          *username;
40         char          *password;
41         icalcomponent *ical;
42         struct ical_t *next;
43 } ical_t;
44
45 /* Static data */
46 static ical_t  calendars[] = {
47         { .location = "data/all.ics" },
48 };
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 void add_recur(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                 rrule  = icalcomponent_get_first_property(comp, ICAL_RRULE_PROPERTY);
93                 recur  = icalproperty_get_rrule(rrule);
94                 iter   = icalrecur_iterator_new(recur, cstart);
95
96                 /* Add recurrences */
97                 if (icaltime_is_null_time(cstart) ||
98                     which == ICAL_VTODO_COMPONENT) {
99                         icalarray_append(array, &(ical_inst){
100                                 .comp  = comp,
101                                 .start = cstart,
102                                 .end   = cend,
103                         });
104                 } else while (1) {
105                         istart = iend = icalrecur_iterator_next(iter);
106                         if (icaltime_is_null_time(istart))
107                                 break;    // no more instances
108                         if (!icaltime_is_null_time(cend))
109                                 iend = icaltime_add(iend, length);
110
111                         if (icaltime_compare(iend, start) <= 0)
112                                 continue; // instance ends before start time
113                         if (icaltime_compare(istart, end) >= 0)
114                                 break;    // instance begins after stop time
115
116                         icalarray_append(array, &(ical_inst){
117                                 .comp  = comp,
118                                 .start = istart,
119                                 .end   = iend,
120                         });
121                 }
122
123                 icalrecur_iterator_free(iter);
124         }
125
126         /* Add children */
127         icalcomponent_kind find = ICAL_ANY_COMPONENT;
128         icalcomponent *child = icalcomponent_get_first_component(comp, find);
129         while (child) {
130                 add_recur(array, child, start, end, which);
131                 child = icalcomponent_get_next_component(comp, find);
132         }
133 }
134
135 static void read_icals(void)
136 {
137         for (ical_t *cal = calendars; cal; cal = cal->next) {
138                 if (cal->ical == NULL && cal->location) {
139                         wordexp_t wexp;
140                         wordexp(cal->location, &wexp, WRDE_NOCMD);
141                         if (wexp.we_wordc == 0)
142                                 continue;
143                         FILE *file = fopen(wexp.we_wordv[0], "r");
144                         wordfree(&wexp);
145                         if (!file)
146                                 continue;
147
148                         icalparser *parser = icalparser_new();
149                         icalparser_set_gen_data(parser, file);
150                         cal->ical = icalparser_parse(parser, (void*)fgets);
151                         icalparser_free(parser);
152                 }
153         }
154 }
155
156 /* Event functions */
157 static event_t *to_event(ical_inst *inst)
158 {
159         icalproperty *prop = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
160
161         event_t *event = new0(event_t);
162         event->name  = strcopy(icalcomponent_get_summary(inst->comp));
163         event->desc  = strcopy(icalcomponent_get_description(inst->comp));
164         event->loc   = strcopy(icalcomponent_get_location(inst->comp));
165         event->cat   = icalproperty_get_value_as_string_r(prop);
166         event->start = to_date(inst->start);
167         event->end   = to_date(inst->end);
168         return event;
169 }
170
171 static event_t *to_events(icalarray *array)
172 {
173         event_t  list = {};
174         event_t *tail = &list;
175         for (int i = 0; i < array->num_elements; i++) {
176                  ical_inst *inst = icalarray_element_at(array, i);
177                  tail->next = to_event(inst);
178                  tail = tail->next;
179         }
180         return list.next;
181 }
182
183 static void print_events(event_t *start)
184 {
185         for (event_t *cur = start; cur; cur = cur->next)
186                 printf("%04d-%02d-%02d %02d:%02d - %s\n",
187                         cur->start.year, cur->start.month, cur->start.day,
188                         cur->start.hour, cur->start.min,
189                         cur->name ?: cur->desc ?: "[no summary]");
190 }
191
192 /* Todo functions */
193 static todo_t *to_todo(ical_inst *inst)
194 {
195         icalproperty *cat  = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
196         icalproperty *perc = icalcomponent_get_first_property(inst->comp, ICAL_PERCENTCOMPLETE_PROPERTY);
197
198         todo_t *todo = new0(todo_t);
199         todo->name   = strcopy(icalcomponent_get_summary(inst->comp));
200         todo->desc   = strcopy(icalcomponent_get_description(inst->comp));
201         todo->cat    = strcopy(icalproperty_get_value_as_string(cat));
202         todo->status = icalcomponent_get_status(inst->comp) == ICAL_STATUS_COMPLETED ? 100 :
203                        perc ? icalproperty_get_percentcomplete(perc) : 0;
204         todo->start  = to_date(inst->start);
205         todo->due    = to_date(icalcomponent_get_due(inst->comp));
206         return todo;
207 }
208
209 static todo_t *to_todos(icalarray *array)
210 {
211         todo_t  list = {};
212         todo_t *tail = &list;
213         for (int i = 0; i < array->num_elements; i++) {
214                  ical_inst *inst = icalarray_element_at(array, i);
215                  tail->next = to_todo(inst);
216                  tail = tail->next;
217         }
218         return list.next;
219 }
220
221 static void print_todos(todo_t *start)
222 {
223         for (todo_t *cur = start; cur; cur = cur->next)
224                 printf("%04d-%02d-%02d %02d:%02d - %d%% - %s\n",
225                         cur->due.year, cur->due.month, cur->due.day,
226                         cur->due.hour, cur->due.min,   cur->status,
227                         cur->name ?: cur->desc ?: "[no summary]");
228 }
229
230 /* Event functions */
231 event_t *ical_events(cal_t *cal, year_t year, month_t month, day_t day, int days)
232 {
233         read_icals();
234
235         icalarray *array = icalarray_new(sizeof(ical_inst), 1);
236         icaltimetype start = {.year = year-10};
237         icaltimetype end   = {.year = year+10};
238         for (ical_t *cal = calendars; cal; cal = cal->next)
239                 add_recur(array, cal->ical, start, end, ICAL_VEVENT_COMPONENT);
240         icalarray_sort(array, ical_compare);
241         event_t *events = to_events(array);
242         icalarray_free(array);
243
244         return events;
245 }
246
247 /* Todo functions */
248 todo_t *ical_todos(cal_t *cal, year_t year, month_t month, day_t day, int days)
249 {
250         read_icals();
251
252         icalarray *array = icalarray_new(sizeof(ical_inst), 1);
253         icaltimetype start = {.year = year-10};
254         icaltimetype end   = {.year = year+10};
255         for (ical_t *cal = calendars; cal; cal = cal->next)
256                 add_recur(array, cal->ical, start, end, ICAL_VTODO_COMPONENT);
257         icalarray_sort(array, ical_compare);
258         todo_t *todos = to_todos(array);
259         icalarray_free(array);
260
261         return todos;
262 }
263
264 /* Test functions */
265 void ical_printr(icalcomponent *comp, int depth)
266 {
267         /* Print component */
268         icalcomponent_kind kind = icalcomponent_isa(comp);
269         printf("%*s", depth, "");
270         printf("%s",  icalcomponent_kind_to_string(kind));
271         if (kind == ICAL_VEVENT_COMPONENT ||
272             kind == ICAL_VTODO_COMPONENT)
273                 printf(" - %s", icalcomponent_get_summary(comp) ?: "[no summary]");
274         printf("\n");
275
276         /* Print children */
277         icalcomponent_kind find = ICAL_ANY_COMPONENT;
278         icalcomponent *child = icalcomponent_get_first_component(comp, find);
279         while (child) {
280                 ical_printr(child, depth+2);
281                 child = icalcomponent_get_next_component(comp, find);
282         }
283 }
284
285 void ical_test(void)
286 {
287         /* Load ical */
288         FILE *file = fopen("data/all.ics", "r");
289         icalparser *parser = icalparser_new();
290         icalparser_set_gen_data(parser, file);
291         icalcomponent *ical = icalparser_parse(parser, (void*)fgets);
292
293         /* Misc */
294         icalarray *array;
295         icaltimetype start = {.year = 2000};
296         icaltimetype end   = {.year = 2020};
297
298         /* Find events */
299         array = icalarray_new(sizeof(ical_inst), 1);
300         add_recur(array, ical, start, end, ICAL_VEVENT_COMPONENT);
301         icalarray_sort(array, ical_compare);
302         event_t *events = to_events(array);
303         icalarray_free(array);
304
305         /* Find Todos */
306         array = icalarray_new(sizeof(ical_inst), 1);
307         add_recur(array, ical, start, end, ICAL_VTODO_COMPONENT);
308         icalarray_sort(array, ical_compare);
309         todo_t *todos = to_todos(array);
310         icalarray_free(array);
311
312         /* Print */
313         //ical_printr(ical, 0);
314         //print_events(events);
315         print_todos(todos);
316
317         (void)print_events;
318         (void)print_todos;
319         (void)events;
320         (void)todos;
321
322         /* Cleanup */
323         icalparser_free(parser);
324 }