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 void add_recur(icalarray *array, icalcomponent *comp,
73 icaltimetype start, icaltimetype end,
74 icalcomponent_kind which)
76 icalcomponent_kind kind = icalcomponent_isa(comp);
79 /* Get recurrence data */
80 struct icaltimetype cstart, cend; // Component times
81 struct icaltimetype istart, iend; // Instance times
82 struct icaldurationtype length; // Duration
85 struct icalrecurrencetype recur;
86 icalrecur_iterator *iter;
88 cstart = icalcomponent_get_dtstart(comp);
89 cend = icalcomponent_get_dtend(comp);
90 length = icaltime_subtract(cend, cstart);
92 rrule = icalcomponent_get_first_property(comp, ICAL_RRULE_PROPERTY);
93 recur = icalproperty_get_rrule(rrule);
94 iter = icalrecur_iterator_new(recur, cstart);
97 if (icaltime_is_null_time(cstart) ||
98 which == ICAL_VTODO_COMPONENT) {
99 icalarray_append(array, &(ical_inst){
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);
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
116 icalarray_append(array, &(ical_inst){
123 icalrecur_iterator_free(iter);
127 icalcomponent_kind find = ICAL_ANY_COMPONENT;
128 icalcomponent *child = icalcomponent_get_first_component(comp, find);
130 add_recur(array, child, start, end, which);
131 child = icalcomponent_get_next_component(comp, find);
135 static void read_icals(void)
137 for (ical_t *cal = calendars; cal; cal = cal->next) {
138 if (cal->ical == NULL && cal->location) {
140 wordexp(cal->location, &wexp, WRDE_NOCMD);
141 if (wexp.we_wordc == 0)
143 FILE *file = fopen(wexp.we_wordv[0], "r");
148 icalparser *parser = icalparser_new();
149 icalparser_set_gen_data(parser, file);
150 cal->ical = icalparser_parse(parser, (void*)fgets);
151 icalparser_free(parser);
156 /* Event functions */
157 static event_t *to_event(ical_inst *inst)
159 icalproperty *prop = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
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);
171 static event_t *to_events(icalarray *array)
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);
183 static void print_events(event_t *start)
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]");
193 static todo_t *to_todo(ical_inst *inst)
195 icalproperty *cat = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
196 icalproperty *perc = icalcomponent_get_first_property(inst->comp, ICAL_PERCENTCOMPLETE_PROPERTY);
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));
209 static todo_t *to_todos(icalarray *array)
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);
221 static void print_todos(todo_t *start)
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]");
230 /* Event functions */
231 event_t *ical_events(cal_t *cal, year_t year, month_t month, day_t day, int days)
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);
248 todo_t *ical_todos(cal_t *cal, year_t year, month_t month, day_t day, int days)
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);
265 void ical_printr(icalcomponent *comp, int depth)
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]");
277 icalcomponent_kind find = ICAL_ANY_COMPONENT;
278 icalcomponent *child = icalcomponent_get_first_component(comp, find);
280 ical_printr(child, depth+2);
281 child = icalcomponent_get_next_component(comp, find);
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);
295 icaltimetype start = {.year = 2000};
296 icaltimetype end = {.year = 2020};
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);
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);
313 //ical_printr(ical, 0);
314 //print_events(events);
323 icalparser_free(parser);