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>
33 struct icaltimetype start;
34 struct icaltimetype end;
37 typedef struct ical_t {
47 static ical_t *calendars;
49 /* Helper functions */
50 static int ical_compare(const void *_a, const void *_b)
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 ;
60 static date_t to_date(struct icaltimetype time)
64 .month = time.month ? time.month-1 : 0,
65 .day = time.day ? time.day -1 : 0,
71 static icaltimetype to_itime(date_t time)
73 return (struct icaltimetype){
75 .month = time.month + 1,
82 static void add_recur(icalarray *array, icalcomponent *comp,
83 icaltimetype start, icaltimetype end,
84 icalcomponent_kind which)
86 icalcomponent_kind kind = icalcomponent_isa(comp);
89 /* Get recurrence data */
90 struct icaltimetype cstart, cend; // Component times
91 struct icaltimetype istart, iend; // Instance times
92 struct icaldurationtype length; // Duration
95 struct icalrecurrencetype recur;
96 icalrecur_iterator *iter;
98 cstart = icalcomponent_get_dtstart(comp);
99 cend = icalcomponent_get_dtend(comp);
100 length = icaltime_subtract(cend, cstart);
102 rrule = icalcomponent_get_first_property(comp, ICAL_RRULE_PROPERTY);
103 recur = icalproperty_get_rrule(rrule);
104 iter = icalrecur_iterator_new(recur, cstart);
106 /* Add recurrences */
107 if (icaltime_is_null_time(cstart) ||
108 which == ICAL_VTODO_COMPONENT) {
109 icalarray_append(array, &(ical_inst){
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);
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
126 icalarray_append(array, &(ical_inst){
133 icalrecur_iterator_free(iter);
137 icalcomponent_kind find = ICAL_ANY_COMPONENT;
138 icalcomponent *child = icalcomponent_get_first_component(comp, find);
140 add_recur(array, child, start, end, which);
141 child = icalcomponent_get_next_component(comp, find);
145 static void read_icals(void)
147 for (ical_t *cal = calendars; cal; cal = cal->next) {
148 if (cal->comp == NULL && cal->location) {
150 wordexp(cal->location, &wexp, WRDE_NOCMD);
151 if (wexp.we_wordc == 0)
153 FILE *file = fopen(wexp.we_wordv[0], "r");
158 icalparser *parser = icalparser_new();
159 icalparser_set_gen_data(parser, file);
160 cal->comp = icalparser_parse(parser, (void*)fgets);
161 icalparser_free(parser);
166 /* Event functions */
167 static event_t *to_event(ical_inst *inst)
169 icalproperty *prop = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
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);
181 static event_t *to_events(icalarray *array)
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);
193 static void print_events(event_t *start)
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]");
203 static todo_t *to_todo(ical_inst *inst)
205 icalproperty *cat = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
206 icalproperty *perc = icalcomponent_get_first_property(inst->comp, ICAL_PERCENTCOMPLETE_PROPERTY);
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));
219 static todo_t *to_todos(icalarray *array)
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);
231 static void print_todos(todo_t *start)
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]");
241 void ical_config(const char *group, const char *name, const char *key, const char *value)
243 ical_t *cal = NULL, *last = NULL;
245 /* Make sure it's valid */
246 if (!match(group, "ical") || !name)
249 /* Find existing calendar */
250 for (cal = calendars; cal; last = cal, cal = cal->next)
251 if (match(cal->cal.name, name))
254 /* Create new calendar */
257 cal->cal.type = "ical";
258 cal->cal.name = get_name(name);
266 /* Set calendar values */
267 if (match(key, "location"))
268 cal->location = get_string(value);
269 else if (match(key, "username"))
270 cal->username = get_string(value);
271 else if (match(key, "password"))
272 cal->password = get_string(value);
276 cal_t *ical_cals(void)
280 for (ical_t *cal = calendars; cal; cal = cal->next)
281 cal->cal.next = &cal->next->cal;
283 return &calendars->cal;
286 /* Event functions */
287 event_t *ical_events(date_t _start, date_t _end)
291 icaltimetype start = to_itime(_start);
292 icaltimetype end = to_itime(_end);
293 icalarray *array = icalarray_new(sizeof(ical_inst), 1);
294 for (ical_t *cal = calendars; cal; cal = cal->next)
295 add_recur(array, cal->comp, start, end, ICAL_VEVENT_COMPONENT);
296 icalarray_sort(array, ical_compare);
297 event_t *events = to_events(array);
298 icalarray_free(array);
304 todo_t *ical_todos(date_t _start, date_t _end)
308 icaltimetype start = to_itime(_start);
309 icaltimetype end = to_itime(_end);
310 icalarray *array = icalarray_new(sizeof(ical_inst), 1);
311 for (ical_t *cal = calendars; cal; cal = cal->next)
312 add_recur(array, cal->comp, start, end, ICAL_VTODO_COMPONENT);
313 icalarray_sort(array, ical_compare);
314 todo_t *todos = to_todos(array);
315 icalarray_free(array);
321 void ical_printr(icalcomponent *comp, int depth)
323 /* Print component */
324 icalcomponent_kind kind = icalcomponent_isa(comp);
325 printf("%*s", depth, "");
326 printf("%s", icalcomponent_kind_to_string(kind));
327 if (kind == ICAL_VEVENT_COMPONENT ||
328 kind == ICAL_VTODO_COMPONENT)
329 printf(" - %s", icalcomponent_get_summary(comp) ?: "[no summary]");
333 icalcomponent_kind find = ICAL_ANY_COMPONENT;
334 icalcomponent *child = icalcomponent_get_first_component(comp, find);
336 ical_printr(child, depth+2);
337 child = icalcomponent_get_next_component(comp, find);
344 FILE *file = fopen("data/all.ics", "r");
345 icalparser *parser = icalparser_new();
346 icalparser_set_gen_data(parser, file);
347 icalcomponent *comp = icalparser_parse(parser, (void*)fgets);
351 icaltimetype start = {.year = 2000};
352 icaltimetype end = {.year = 2020};
355 array = icalarray_new(sizeof(ical_inst), 1);
356 add_recur(array, comp, start, end, ICAL_VEVENT_COMPONENT);
357 icalarray_sort(array, ical_compare);
358 event_t *events = to_events(array);
359 icalarray_free(array);
362 array = icalarray_new(sizeof(ical_inst), 1);
363 add_recur(array, comp, start, end, ICAL_VTODO_COMPONENT);
364 icalarray_sort(array, ical_compare);
365 todo_t *todos = to_todos(array);
366 icalarray_free(array);
369 //ical_printr(comp, 0);
370 //print_events(events);
379 icalparser_free(parser);