]> Pileus Git - lackey/blob - cals/ical.c
b39f901512f58dee046d93ff4f1d20ef7d7ecae9
[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 #include <stdio.h>
19 #include <stdlib.h>
20 #include <libical/ical.h>
21
22 #include "util.h"
23 #include "date.h"
24 #include "cal.h"
25
26 /* Local types */
27 typedef struct {
28         icalcomponent *comp;
29         struct icaltimetype start;
30         struct icaltimetype end;
31 } ical_inst;
32
33 /* Helper functions */
34 static int ical_compare(const void *_a, const void *_b)
35 {
36         const ical_inst *a = _a;
37         const ical_inst *b = _b;
38         int scomp = icaltime_compare(a->start, b->start);
39         int ecomp = icaltime_compare(a->end,   b->end);
40         return scomp != 0 ? scomp :
41                ecomp != 0 ? ecomp : 0 ;
42 }
43
44 static date_t to_date(struct icaltimetype time)
45 {
46         return (date_t){
47                 .year  = time.year,
48                 .month = time.month-1,
49                 .day   = time.day-1,
50                 .hour  = time.hour,
51                 .min   = time.minute,
52         };
53 }
54
55 static event_t *to_event(ical_inst *inst)
56 {
57         icalproperty *prop = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
58
59         event_t *event = calloc(1, sizeof(event_t));
60         event->name  = icalcomponent_get_summary(inst->comp);
61         event->desc  = icalcomponent_get_description(inst->comp);
62         event->loc   = icalcomponent_get_location(inst->comp);
63         event->cat   = icalproperty_get_value_as_string(prop);
64         event->start = to_date(inst->start);
65         event->end   = to_date(inst->end);
66         return event;
67 }
68
69 static event_t *to_list(icalarray *array)
70 {
71         event_t  list = {};
72         event_t *tail = &list;
73         for (int i = 0; i < array->num_elements; i++) {
74                  ical_inst *inst = icalarray_element_at(array, i);
75                  tail->next = to_event(inst);
76                  tail = tail->next;
77         }
78         return list.next;
79 }
80
81 static void print_list(event_t *start)
82 {
83         for (event_t *cur = start; cur; cur = cur->next)
84                 printf("%04d-%02d-%02d %02d:%02d - %s\n",
85                         cur->start.year, cur->start.month, cur->start.day,
86                         cur->start.hour, cur->start.min,
87                         cur->name ?: cur->desc ?: "[no summary]");
88 }
89
90 static void add_events(icalarray *array, icalcomponent *comp,
91                 icaltimetype start, icaltimetype end)
92 {
93         icalcomponent_kind kind = icalcomponent_isa(comp);
94
95         if (kind == ICAL_VEVENT_COMPONENT ||
96             kind == ICAL_VTODO_COMPONENT) {
97                 /* Get recurrence data */
98                 struct icaltimetype cstart, cend; // Component times
99                 struct icaltimetype istart, iend; // Instance times
100                 struct icaldurationtype length;   // Duration
101
102                 icalproperty             *rrule;
103                 struct icalrecurrencetype recur;
104                 icalrecur_iterator       *iter;
105
106                 cstart = icalcomponent_get_dtstart(comp);
107                 cend   = icalcomponent_get_dtend(comp);
108                 length = icaltime_subtract(cend, cstart);
109
110                 rrule  = icalcomponent_get_first_property(comp, ICAL_RRULE_PROPERTY);
111                 recur  = icalproperty_get_rrule(rrule);
112                 iter   = icalrecur_iterator_new(recur, cstart);
113
114                 /* Add recurrences */
115                 while (1) {
116                         istart = icalrecur_iterator_next(iter);
117                         if (icaltime_is_null_time(istart))
118                                 break;    // no more instances
119                         iend   = icaltime_add(istart, length);
120                         if (icaltime_compare(iend, start) <= 0)
121                                 continue; // instance ends before start time
122                         if (icaltime_compare(istart, end) >= 0)
123                                 break;    // instance begins after stop time
124                         icalarray_append(array, &(ical_inst){
125                                 .comp  = comp,
126                                 .start = istart,
127                                 .end   = iend,
128                         });
129                 }
130         }
131
132         /* Add children */
133         icalcomponent_kind find = ICAL_ANY_COMPONENT;
134         icalcomponent *child = icalcomponent_get_first_component(comp, find);
135         while (child) {
136                 add_events(array, child, start, end);
137                 child = icalcomponent_get_next_component(comp, find);
138         }
139 }
140
141 /* Event functions */
142 event_t *ical_events(cal_t *cal, year_t year, month_t month, day_t day, int days)
143 {
144         /* Load ical */
145         FILE *file = fopen("data/all.ics", "r");
146         if (!file)
147                 return NULL;
148         icalparser *parser = icalparser_new();
149         icalparser_set_gen_data(parser, file);
150         icalcomponent *ical = icalparser_parse(parser, (void*)fgets);
151
152         /* Add events */
153         icalarray *array = icalarray_new(sizeof(ical_inst), 1);
154         icaltimetype start = {.year = 2000};
155         icaltimetype end   = {.year = 2020};
156         add_events(array, ical, start, end);
157         icalarray_sort(array, ical_compare);
158         return to_list(array);
159
160         /* Todo, memory management */
161 }
162
163 /* Test functions */
164 void ical_printr(icalcomponent *comp, int depth)
165 {
166         /* Print component */
167         icalcomponent_kind kind = icalcomponent_isa(comp);
168         printf("%*s", depth, "");
169         printf("%s",  icalcomponent_kind_to_string(kind));
170         if (kind == ICAL_VEVENT_COMPONENT ||
171             kind == ICAL_VTODO_COMPONENT)
172                 printf(" - %s", icalcomponent_get_summary(comp) ?: "[no summary]");
173         printf("\n");
174
175         /* Print children */
176         icalcomponent_kind find = ICAL_ANY_COMPONENT;
177         icalcomponent *child = icalcomponent_get_first_component(comp, find);
178         while (child) {
179                 ical_printr(child, depth+2);
180                 child = icalcomponent_get_next_component(comp, find);
181         }
182 }
183
184 void ical_test(void)
185 {
186         /* Load ical */
187         FILE *file = fopen("data/all.ics", "r");
188         icalparser *parser = icalparser_new();
189         icalparser_set_gen_data(parser, file);
190         icalcomponent *ical = icalparser_parse(parser, (void*)fgets);
191
192         /* Test print */
193         ical_printr(ical, 0);
194
195         /* Test Add */
196         icalarray *array = icalarray_new(sizeof(ical_inst), 1);
197         icaltimetype start = {.year = 2000};
198         icaltimetype end   = {.year = 2020};
199         add_events(array, ical, start, end);
200         icalarray_sort(array, ical_compare);
201         event_t *events = to_list(array);
202         print_list(events);
203
204         /* Cleanup */
205         icalparser_free(parser);
206 }