2 * Copyright (C) 2012-2013 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/>.
24 #include <libical/ical.h>
36 struct icaltimetype start;
37 struct icaltimetype end;
40 typedef struct ical_t {
50 static ical_t *calendars;
53 static form_text_t ff_name = TEXT('t');
54 static form_text_t ff_desc = TEXT('d');
55 static form_text_t ff_loc = TEXT('o');
56 static form_text_t ff_cat = TEXT('g');
57 static form_date_t ff_start = DATE('s');
58 static form_date_t ff_end = DATE('e');
59 static form_number_t ff_status = NUMBER('p', .f.after="%");
60 static form_date_t ff_due = DATE('u');
61 static form_list_t ff_cal = LIST('c');
62 static form_list_t ff_recur = LIST('r');
63 static form_number_t ff_freq = NUMBER('v');
64 static form_button_t ff_wdays = BUTTONS("Su Mo Tu We Th Fr Sa");
67 static form_t form_cal = { 1, 2, {
69 { TAB, LABEL("_Title"), &ff_name.f },
73 static form_t form_event = { 12, 6, {
75 { TAB, LABEL("_Title"), &ff_name.f },
76 { TAB, LABEL("L_ocation"), &ff_loc.f },
77 { TAB, LABEL("_Start"), &ff_start.f,
78 TAB, LABEL("_End"), &ff_end.f },
79 { TAB, LABEL("_Calendar"), &ff_cal.f,
80 TAB, LABEL("Cate_gory"), &ff_cat.f },
82 { HEAD("Recurrence") },
83 { TAB, LABEL("_Repeat"), &ff_recur.f,
84 TAB, LABEL("E_very"), &ff_freq.f },
85 { TAB, TAB, &ff_wdays.f },
92 static form_t form_todo = { 12, 6, {
94 { TAB, LABEL("_Title"), &ff_name.f },
95 { TAB, LABEL("Com_pleted"), &ff_status.f },
96 { TAB, LABEL("_Start"), &ff_start.f,
97 TAB, LABEL("D_ue Date"), &ff_due.f },
98 { TAB, LABEL("_Calendar"), &ff_cal.f,
99 TAB, LABEL("Cate_gory"), &ff_cat.f },
101 { HEAD("Recurrence") },
102 { TAB, LABEL("_Repeat"), &ff_recur.f,
103 TAB, LABEL("E_very"), &ff_freq.f },
104 { TAB, TAB, &ff_wdays.f },
106 { HEAD("_Details") },
110 /* Helper functions */
111 static int ical_compare(const void *_a, const void *_b)
113 const ical_inst *a = _a;
114 const ical_inst *b = _b;
115 int scomp = icaltime_compare(a->start, b->start);
116 int ecomp = icaltime_compare(a->end, b->end);
117 return scomp != 0 ? scomp :
118 ecomp != 0 ? ecomp : 0 ;
121 static date_t to_date(struct icaltimetype itime)
123 return get_date(icaltime_as_timet_with_zone(itime, itime.zone));
126 static icaltimetype to_itime(date_t date)
128 return icaltime_from_timet_with_zone(get_stamp(date), 0, NULL);
131 static void add_recur(cal_t *cal,
132 icalarray *array, icalcomponent *comp,
133 icaltimetype start, icaltimetype end,
134 icalcomponent_kind which)
136 icalcomponent_kind kind = icalcomponent_isa(comp);
139 /* Get recurrence data */
140 struct icaltimetype cstart, cend; // Component times
141 struct icaltimetype istart, iend; // Instance times
142 struct icaldurationtype length; // Duration
145 struct icalrecurrencetype recur;
146 icalrecur_iterator *iter;
148 cstart = icalcomponent_get_dtstart(comp);
149 cend = icalcomponent_get_dtend(comp);
150 length = icaltime_subtract(cend, cstart);
153 if (icaltime_is_null_time(cstart) ||
154 which == ICAL_VTODO_COMPONENT) {
155 icalarray_append(array, &(ical_inst){
163 /* Add all recurrences */
164 rrule = icalcomponent_get_first_property(comp, ICAL_RRULE_PROPERTY);
168 icalarray_append(array, &(ical_inst){
176 /* Recurring events */
178 recur = icalproperty_get_rrule(rrule);
179 iter = icalrecur_iterator_new(recur, cstart);
181 /* Add recurrence for this rrule */
183 istart = iend = icalrecur_iterator_next(iter);
184 if (icaltime_is_null_time(istart))
185 break; // no more instances
186 if (!icaltime_is_null_time(cend))
187 iend = icaltime_add(iend, length);
189 if (icaltime_compare(iend, start) <= 0)
190 continue; // instance ends before start time
191 if (icaltime_compare(istart, end) >= 0)
192 break; // instance begins after stop time
194 icalarray_append(array, &(ical_inst){
202 icalrecur_iterator_free(iter);
203 rrule = icalcomponent_get_next_property(comp, ICAL_RRULE_PROPERTY);
208 icalcomponent_kind find = ICAL_ANY_COMPONENT;
209 icalcomponent *child = icalcomponent_get_first_component(comp, find);
211 add_recur(cal, array, child, start, end, which);
212 child = icalcomponent_get_next_component(comp, find);
216 static void read_icals(void)
218 for (ical_t *cal = calendars; cal; cal = cal->next) {
220 debug("Missing location for ical '%s'", cal->cal.name);
224 wordexp(cal->location, &wexp, WRDE_NOCMD);
225 icalparser *parser = icalparser_new();
226 if (wexp.we_wordc > 1)
227 debug("Multiple calendards are not supported '%s'", cal->location);
228 FILE *file = fopen(wexp.we_wordv[0], "r");
230 debug("Cannot open ical file '%s'", wexp.we_wordv[0]);
232 icalparser_set_gen_data(parser, file);
233 cal->comp = icalparser_parse(parser, (void*)fgets);
234 icalparser_free(parser);
240 /* Event functions */
241 static event_t *to_event(ical_inst *inst)
243 icalproperty *prop = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
245 event_t *event = new0(event_t);
246 event->name = strcopy(icalcomponent_get_summary(inst->comp));
247 event->desc = strcopy(icalcomponent_get_description(inst->comp));
248 event->loc = strcopy(icalcomponent_get_location(inst->comp));
249 event->cat = icalproperty_get_value_as_string_r(prop);
250 event->start = to_date(inst->start);
251 event->end = to_date(inst->end);
252 event->cal = inst->cal;
256 static event_t *to_events(icalarray *array)
259 event_t *tail = &list;
260 for (int i = 0; i < array->num_elements; i++) {
261 ical_inst *inst = icalarray_element_at(array, i);
262 tail->next = to_event(inst);
268 static void print_events(event_t *start)
270 for (event_t *cur = start; cur; cur = cur->next)
271 printf("%04d-%02d-%02d %02d:%02d - %s\n",
272 cur->start.year, cur->start.month, cur->start.day,
273 cur->start.hour, cur->start.min,
274 cur->name ?: cur->desc ?: "[no summary]");
278 static todo_t *to_todo(ical_inst *inst)
280 icalproperty *cat = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
281 icalproperty *perc = icalcomponent_get_first_property(inst->comp, ICAL_PERCENTCOMPLETE_PROPERTY);
283 todo_t *todo = new0(todo_t);
284 todo->name = strcopy(icalcomponent_get_summary(inst->comp));
285 todo->desc = strcopy(icalcomponent_get_description(inst->comp));
286 todo->cat = strcopy(icalproperty_get_value_as_string(cat));
287 todo->status = icalcomponent_get_status(inst->comp) == ICAL_STATUS_COMPLETED ? 100 :
288 perc ? icalproperty_get_percentcomplete(perc) : 0;
289 todo->start = to_date(inst->start);
290 todo->due = to_date(icalcomponent_get_due(inst->comp));
291 todo->cal = inst->cal;
295 static todo_t *to_todos(icalarray *array)
298 todo_t *tail = &list;
299 for (int i = 0; i < array->num_elements; i++) {
300 ical_inst *inst = icalarray_element_at(array, i);
301 tail->next = to_todo(inst);
307 static void print_todos(todo_t *start)
309 for (todo_t *cur = start; cur; cur = cur->next)
310 printf("%04d-%02d-%02d %02d:%02d - %d%% - %s\n",
311 cur->due.year, cur->due.month, cur->due.day,
312 cur->due.hour, cur->due.min, cur->status,
313 cur->name ?: cur->desc ?: "[no summary]");
317 void ical_config(const char *group, const char *name, const char *key, const char *value)
319 ical_t *cal = NULL, *last = NULL;
321 /* Make sure it's valid */
322 if (!match(group, "ical") || !name)
325 /* Find existing calendar */
326 for (cal = calendars; cal; last = cal, cal = cal->next)
327 if (match(cal->cal.name, name))
330 /* Create new calendar */
333 cal->cal.type = "ical";
334 cal->cal.name = get_name(name);
341 /* Set calendar values */
342 if (match(key, "location"))
343 cal->location = get_string(value);
344 else if (match(key, "username"))
345 cal->username = get_string(value);
346 else if (match(key, "password"))
347 cal->password = get_string(value);
351 cal_t *ical_cals(void)
355 for (ical_t *cal = calendars; cal; cal = cal->next)
356 cal->cal.next = &cal->next->cal;
358 return &calendars->cal;
361 /* Event functions */
362 event_t *ical_events(date_t _start, date_t _end)
366 icaltimetype start = to_itime(_start);
367 icaltimetype end = to_itime(_end);
368 icalarray *array = icalarray_new(sizeof(ical_inst), 1);
369 for (ical_t *cal = calendars; cal; cal = cal->next)
370 add_recur(&cal->cal, array, cal->comp, start, end, ICAL_VEVENT_COMPONENT);
371 icalarray_sort(array, ical_compare);
372 event_t *events = to_events(array);
373 icalarray_free(array);
379 todo_t *ical_todos(date_t _start, date_t _end)
383 icaltimetype start = to_itime(_start);
384 icaltimetype end = to_itime(_end);
385 icalarray *array = icalarray_new(sizeof(ical_inst), 1);
386 for (ical_t *cal = calendars; cal; cal = cal->next)
387 add_recur(&cal->cal, array, cal->comp, start, end, ICAL_VTODO_COMPONENT);
388 icalarray_sort(array, ical_compare);
389 todo_t *todos = to_todos(array);
390 icalarray_free(array);
396 void ical_edit(edit_t mode)
404 form_show(&form_cal);
407 ff_name.text = strcopy(EVENT->name);
408 ff_desc.text = strcopy(EVENT->desc);
409 ff_loc.text = strcopy(EVENT->loc);
410 ff_cat.text = strcopy(EVENT->cat);
411 ff_start.date = EVENT->start;
412 ff_end.date = EVENT->end;
413 //ff_recur.text = strcopy(EVENT->recur);
414 form_show(&form_event);
417 ff_name.text = strcopy(TODO->name);
418 ff_desc.text = strcopy(TODO->desc);
419 ff_status.number = TODO->status;
420 ff_cat.text = strcopy(TODO->cat);
421 ff_start.date = TODO->start;
422 ff_due.date = TODO->due;
423 //ff_recur.text = strcopy(TODO->recur);
424 form_show(&form_todo);
429 void ical_save(edit_t mode)
431 //if (isevent || istodo) {
433 // //save_location();
443 // //save_completed();
446 // //save_username();
447 // //save_password();
452 void ical_printr(icalcomponent *comp, int depth)
454 /* Print component */
455 icalcomponent_kind kind = icalcomponent_isa(comp);
456 printf("%*s", depth, "");
457 printf("%s", icalcomponent_kind_to_string(kind));
458 if (kind == ICAL_VEVENT_COMPONENT ||
459 kind == ICAL_VTODO_COMPONENT)
460 printf(" - %s", icalcomponent_get_summary(comp) ?: "[no summary]");
464 icalcomponent_kind find = ICAL_ANY_COMPONENT;
465 icalcomponent *child = icalcomponent_get_first_component(comp, find);
467 ical_printr(child, depth+2);
468 child = icalcomponent_get_next_component(comp, find);
472 void ical_test(char *path)
475 FILE *file = fopen(path, "r");
476 icalparser *parser = icalparser_new();
477 icalparser_set_gen_data(parser, file);
478 icalcomponent *comp = icalparser_parse(parser, (void*)fgets);
482 icaltimetype start = {.year = 2000};
483 icaltimetype end = {.year = 2020};
486 array = icalarray_new(sizeof(ical_inst), 1);
487 add_recur(NULL, array, comp, start, end, ICAL_VEVENT_COMPONENT);
488 icalarray_sort(array, ical_compare);
489 event_t *events = to_events(array);
490 icalarray_free(array);
493 array = icalarray_new(sizeof(ical_inst), 1);
494 add_recur(NULL, array, comp, start, end, ICAL_VTODO_COMPONENT);
495 icalarray_sort(array, ical_compare);
496 todo_t *todos = to_todos(array);
497 icalarray_free(array);
500 ical_printr(comp, 0);
501 print_events(events);
510 icalparser_free(parser);