+static event_t *to_events(icalarray *array)
+{
+ event_t list = {};
+ event_t *tail = &list;
+ for (int i = 0; i < array->num_elements; i++) {
+ ical_inst *inst = icalarray_element_at(array, i);
+ tail->next = to_event(inst);
+ tail = tail->next;
+ }
+ return list.next;
+}
+
+static void print_events(event_t *start)
+{
+ for (event_t *cur = start; cur; cur = cur->next)
+ printf("%04d-%02d-%02d %02d:%02d - %s\n",
+ cur->start.year, cur->start.month, cur->start.day,
+ cur->start.hour, cur->start.min,
+ cur->name ?: cur->desc ?: "[no summary]");
+}
+
+/* Todo functions */
+static todo_t *to_todo(ical_inst *inst)
+{
+ icalproperty *cat = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
+ icalproperty *perc = icalcomponent_get_first_property(inst->comp, ICAL_PERCENTCOMPLETE_PROPERTY);
+
+ todo_t *todo = new0(todo_t);
+ todo->name = strcopy(icalcomponent_get_summary(inst->comp));
+ todo->desc = strcopy(icalcomponent_get_description(inst->comp));
+ todo->cat = strcopy(icalproperty_get_value_as_string(cat));
+ todo->status = icalcomponent_get_status(inst->comp) == ICAL_STATUS_COMPLETED ? 100 :
+ perc ? icalproperty_get_percentcomplete(perc) : 0;
+ todo->start = to_date(inst->start);
+ todo->due = to_date(icalcomponent_get_due(inst->comp));
+ return todo;
+}
+
+static todo_t *to_todos(icalarray *array)
+{
+ todo_t list = {};
+ todo_t *tail = &list;
+ for (int i = 0; i < array->num_elements; i++) {
+ ical_inst *inst = icalarray_element_at(array, i);
+ tail->next = to_todo(inst);
+ tail = tail->next;
+ }
+ return list.next;
+}
+
+static void print_todos(todo_t *start)
+{
+ for (todo_t *cur = start; cur; cur = cur->next)
+ printf("%04d-%02d-%02d %02d:%02d - %d%% - %s\n",
+ cur->due.year, cur->due.month, cur->due.day,
+ cur->due.hour, cur->due.min, cur->status,
+ cur->name ?: cur->desc ?: "[no summary]");
+}
+
+/* Config parser */
+void ical_config(const char *group, const char *name, const char *key, const char *value)
+{
+ ical_t *cal = NULL, *last = NULL;
+
+ /* Make sure it's valid */
+ if (!match(group, "ical") || !name)
+ return;
+
+ /* Find existing calendar */
+ for (cal = calendars; cal; last = cal, cal = cal->next)
+ if (match(cal->cal.name, name))
+ break;
+
+ /* Create new calendar */
+ if (!cal) {
+ cal = new0(ical_t);
+ cal->cal.type = "ical";
+ cal->cal.name = get_name(name);
+ if (last)
+ last->next = cal;
+ else
+ calendars = cal;
+ return;
+ }
+
+ /* Set calendar values */
+ if (match(key, "location"))
+ cal->location = get_string(value);
+ else if (match(key, "username"))
+ cal->username = get_string(value);
+ else if (match(key, "password"))
+ cal->password = get_string(value);
+}
+
+/* Cal functions */
+cal_t *ical_cals(void)
+{
+ read_icals();
+
+ for (ical_t *cal = calendars; cal; cal = cal->next)
+ cal->cal.next = &cal->next->cal;
+
+ return &calendars->cal;
+}
+
+/* Event functions */
+event_t *ical_events(date_t _start, date_t _end)
+{
+ read_icals();
+
+ icaltimetype start = to_itime(_start);
+ icaltimetype end = to_itime(_end);