/*
- * Copyright (C) 2012 Andy Spencer <andy753421@gmail.com>
+ * Copyright (C) 2012-2013 Andy Spencer <andy753421@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
CAL(ical);
/* Global data */
-cal_t *CALS;
-event_t *EVENTS;
-todo_t *TODOS;
+cal_t *CAL, *CALS;
+event_t *EVENT, *EVENTS;
+todo_t *TODO, *TODOS;
/* Local data */
static date_t start;
(*last)->next = *next;
else
(*first) = *next;
+ (*next)->prev = *last;
(*last) = (*next);
(*next) = (*next)->next;
}
(*last)->next = *next;
else
(*first) = *next;
+ (*next)->prev = *last;
(*last) = (*next);
(*next) = (*next)->next;
}
ical_cals());
/* Load data */
- cal_load(YEAR, MONTH, DAY, 1);
+ cal_load(SEL.year, SEL.month, SEL.day, 1);
/* Debug */
+#ifdef CAL_DEBUG
for (event_t *e = EVENTS; e; e = e->next)
debug("event: %04d-%02d-%02d %02d:%02d: %s - %s",
e->start.year, e->start.month, e->start.day,
debug("todo: %04d-%02d-%02d %02d:%02d: %s - %s",
e->start.year, e->start.month, e->start.day,
e->start.hour, e->start.min, e->name, e->desc);
+#endif
}
/* Load events and todos */
add_days(&eyear, &emonth, &eday, days);
/* Skip if we already loaded enough info */
- if (!before(&start, year, month, day, 0, 0) &&
- before(&end, eyear, emonth, eday, 24, 0))
+ if (before(&start, year, month, day, 0, 0) &&
+ !before(&end, eyear, emonth, eday, 24, 0))
return;
/* Free uneeded data */
dummy_todos(start, end),
ical_todos(start, end));
+ /* Verify events and todos*/
+#ifdef CAL_ERROR
+ for (event_t *cur = EVENTS; cur; cur = cur->next) {
+ if (!cur->cal)
+ error("Missing cal in event '%s'", cur->name);
+ if ((cur->next && cur->next->prev != cur) ||
+ (cur->prev && cur->prev->next != cur))
+ error("Broken link in event '%s'", cur->name);
+ }
+ for (todo_t *cur = TODOS; cur; cur = cur->next) {
+ if (!cur->cal)
+ error("Missing cal in todo '%s'", cur->name);
+ if ((cur->next && cur->next->prev != cur) ||
+ (cur->prev && cur->prev->next != cur))
+ error("Broken link in todo '%s'", cur->name);
+ }
+#endif
}
/* Config parser */
else if (match(group, "ical"))
ical_config(group, name, key, value);
}
+
+/* Find event for matching target date */
+event_t *find_event(date_t *target)
+{
+ int min = 0;
+ event_t *event = NULL;
+
+ if (EVENT && compare(&EVENT->start, target) == 0)
+ return EVENT;
+
+ for (event_t *cur = EVENTS; cur; cur = cur->next) {
+ // Skip events that are on the wrong day
+ if ((target->year != cur->start.year) ||
+ (target->month != cur->start.month) ||
+ (target->day != cur->start.day))
+ continue;
+
+ // We don't want time change or leap seconds here
+ int diff = (target->hour - cur->start.hour) * 60 * 24 +
+ (target->min - cur->start.min) * 60 +
+ (target->sec - cur->start.sec);
+
+ if (event == NULL || ABS(diff) < min) {
+ min = ABS(diff);
+ event = cur;
+ }
+ }
+
+ return event;
+}