2 * Copyright (C) 2012 Andy Spencer <andy753421@gmail.com>
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.
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.
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/>.
23 #include <libical/ical.h>
32 struct icaltimetype start;
33 struct icaltimetype end;
36 typedef struct ical_t {
46 static ical_t calendars[] = {
47 { .location = "data/all.ics" },
50 /* Helper functions */
51 static int ical_compare(const void *_a, const void *_b)
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 ;
61 static date_t to_date(struct icaltimetype time)
65 .month = time.month ? time.month-1 : 0,
66 .day = time.day ? time.day -1 : 0,
72 static icaltimetype to_itime(date_t time)
74 return (struct icaltimetype){
76 .month = time.month + 1,
83 static void add_recur(icalarray *array, icalcomponent *comp,
84 icaltimetype start, icaltimetype end,
85 icalcomponent_kind which)
87 icalcomponent_kind kind = icalcomponent_isa(comp);
90 /* Get recurrence data */
91 struct icaltimetype cstart, cend; // Component times
92 struct icaltimetype istart, iend; // Instance times
93 struct icaldurationtype length; // Duration
96 struct icalrecurrencetype recur;
97 icalrecur_iterator *iter;
99 cstart = icalcomponent_get_dtstart(comp);
100 cend = icalcomponent_get_dtend(comp);
101 length = icaltime_subtract(cend, cstart);
103 rrule = icalcomponent_get_first_property(comp, ICAL_RRULE_PROPERTY);
104 recur = icalproperty_get_rrule(rrule);
105 iter = icalrecur_iterator_new(recur, cstart);
107 /* Add recurrences */
108 if (icaltime_is_null_time(cstart) ||
109 which == ICAL_VTODO_COMPONENT) {
110 icalarray_append(array, &(ical_inst){
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);
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
127 icalarray_append(array, &(ical_inst){
134 icalrecur_iterator_free(iter);
138 icalcomponent_kind find = ICAL_ANY_COMPONENT;
139 icalcomponent *child = icalcomponent_get_first_component(comp, find);
141 add_recur(array, child, start, end, which);
142 child = icalcomponent_get_next_component(comp, find);
146 static void read_icals(void)
148 for (ical_t *cal = calendars; cal; cal = cal->next) {
149 if (cal->ical == NULL && cal->location) {
151 wordexp(cal->location, &wexp, WRDE_NOCMD);
152 if (wexp.we_wordc == 0)
154 FILE *file = fopen(wexp.we_wordv[0], "r");
159 icalparser *parser = icalparser_new();
160 icalparser_set_gen_data(parser, file);
161 cal->ical = icalparser_parse(parser, (void*)fgets);
162 icalparser_free(parser);
167 /* Event functions */
168 static event_t *to_event(ical_inst *inst)
170 icalproperty *prop = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
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);
182 static event_t *to_events(icalarray *array)
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);
194 static void print_events(event_t *start)
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]");
204 static todo_t *to_todo(ical_inst *inst)
206 icalproperty *cat = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
207 icalproperty *perc = icalcomponent_get_first_property(inst->comp, ICAL_PERCENTCOMPLETE_PROPERTY);
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));
220 static todo_t *to_todos(icalarray *array)
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);
232 static void print_todos(todo_t *start)
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]");
241 /* Event functions */
242 event_t *ical_events(date_t _start, date_t _end)
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);
259 todo_t *ical_todos(date_t _start, date_t _end)
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);
276 void ical_printr(icalcomponent *comp, int depth)
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]");
288 icalcomponent_kind find = ICAL_ANY_COMPONENT;
289 icalcomponent *child = icalcomponent_get_first_component(comp, find);
291 ical_printr(child, depth+2);
292 child = icalcomponent_get_next_component(comp, find);
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);
306 icaltimetype start = {.year = 2000};
307 icaltimetype end = {.year = 2020};
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);
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);
324 //ical_printr(ical, 0);
325 //print_events(events);
334 icalparser_free(parser);