]> Pileus Git - lackey/blobdiff - src/cal.c
Add event selection to day and week views
[lackey] / src / cal.c
index 627feaee9ff18799607fbec24ad5ff8e05e2be49..a05dd1feefe0d69868a934a51ab30538868f09ab 100644 (file)
--- a/src/cal.c
+++ b/src/cal.c
@@ -1,5 +1,5 @@
 /*
- * 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
@@ -24,6 +24,7 @@
 /* Macros */
 #define CAL(name) \
        void     name##_config(const char *group, const char *name, const char *key, const char *value); \
+       cal_t   *name##_cals(void); \
        event_t *name##_events(date_t start, date_t end); \
        todo_t  *name##_todos(date_t start, date_t end)
 
@@ -32,8 +33,9 @@ CAL(dummy);
 CAL(ical);
 
 /* Global data */
-event_t *EVENTS;
-todo_t  *TODOS;
+cal_t   *CAL,   *CALS;
+event_t *EVENT, *EVENTS;
+todo_t  *TODO,  *TODOS;
 
 /* Local data */
 static date_t start;
@@ -46,6 +48,7 @@ static void add_event(event_t **first, event_t **last, event_t **next)
                (*last)->next = *next;
        else
                (*first) = *next;
+       (*next)->prev = *last;
        (*last) = (*next);
        (*next) = (*next)->next;
 }
@@ -56,10 +59,23 @@ static void add_todo(todo_t **first, todo_t **last, todo_t **next)
                (*last)->next = *next;
        else
                (*first) = *next;
+       (*next)->prev = *last;
        (*last) = (*next);
        (*next) = (*next)->next;
 }
 
+static cal_t *merge_cals(cal_t *a, cal_t *b)
+{
+       // TODO - we should sort these
+       if (!a) return b;
+       if (!b) return a;
+       cal_t *last = a;
+       while (last->next)
+               last = last->next;
+       last->next = b;
+       return a;
+}
+
 static event_t *merge_events(event_t *a, event_t *b)
 {
        event_t *first = NULL, *last = NULL;
@@ -89,10 +105,16 @@ static todo_t *merge_todos(todo_t *a, todo_t *b)
 /* Initialize */
 void cal_init(void)
 {
-       /* Load a year's worth of data */
-       cal_load(YEAR-1, DEC, 31-7, 366+7+7);
+       /* Load calendars */
+       CALS = merge_cals(
+               dummy_cals(),
+                ical_cals());
+
+       /* Load data */
+       cal_load(SEL.year, SEL.month, SEL.day, 1);
 
        /* Debug */
+#ifdef DEBUG_CALS
        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,
@@ -101,6 +123,7 @@ void cal_init(void)
                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 */
@@ -112,8 +135,8 @@ void cal_load(year_t year, month_t month, day_t day, int days)
        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 */
@@ -150,6 +173,23 @@ void cal_load(year_t year, month_t month, day_t day, int days)
                dummy_todos(start, end),
                 ical_todos(start, end));
 
+       /* Verify events and todos*/
+#ifdef DEBUG_CALS
+       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 */
@@ -160,3 +200,33 @@ void cal_config(const char *group, const char *name, const char *key, const char
        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;
+}