]> Pileus Git - lackey/blobdiff - src/cal.c
Output cal field for ical events and todos
[lackey] / src / cal.c
index 435bd336f81c6ecf3caa6404ff51e437b80e5b29..62b21ac8dd0d2c9426cfe9d2a4bb31c2e288ea82 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
@@ -23,6 +23,8 @@
 
 /* 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)
 
@@ -31,6 +33,7 @@ CAL(dummy);
 CAL(ical);
 
 /* Global data */
+cal_t   *CALS;
 event_t *EVENTS;
 todo_t  *TODOS;
 
@@ -59,6 +62,18 @@ static void add_todo(todo_t **first, todo_t **last, todo_t **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;
@@ -88,8 +103,13 @@ 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(YEAR, MONTH, DAY, 1);
 
        /* Debug */
        for (event_t *e = EVENTS; e; e = e->next)
@@ -111,8 +131,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 */
@@ -149,4 +169,20 @@ 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*/
+       //for (event_t *cur = EVENTS; cur; cur = cur->next)
+       //      if (!cur->cal)
+       //              error("Missing cal 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);
+}
+
+/* Config parser */
+void cal_config(const char *group, const char *name, const char *key, const char *value)
+{
+       if (match(group, "dummy"))
+               dummy_config(group, name, key, value);
+       else if (match(group, "ical"))
+               ical_config(group, name, key, value);
 }