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