]> Pileus Git - lackey/blobdiff - src/cal.c
Add cal_load function and update cal APIs
[lackey] / src / cal.c
index da835da7225f9280e9463031be8fb6b76983fc08..2fda7a1fb895603cb04790dbf14d7e45cfb11702 100644 (file)
--- a/src/cal.c
+++ b/src/cal.c
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <stdlib.h>
+
 #include "util.h"
 #include "date.h"
 #include "cal.h"
 
+/* Macros */
+#define CAL(name) \
+       event_t *name##_events(date_t start, date_t end); \
+       todo_t  *name##_todos(date_t start, date_t end)
+
+/* Prototypes */
+CAL(dummy);
+CAL(ical);
+
 /* Global data */
 event_t *EVENTS;
 todo_t  *TODOS;
 
+/* Local data */
+static date_t start;
+static date_t end;
+
+/* Merge events and todos */
+static void add_event(event_t **first, event_t **last, event_t **next)
+{
+       if (*last)
+               (*last)->next = *next;
+       else
+               (*first) = *next;
+       (*last) = (*next);
+       (*next) = (*next)->next;
+}
+
+static void add_todo(todo_t **first, todo_t **last, todo_t **next)
+{
+       if (*last)
+               (*last)->next = *next;
+       else
+               (*first) = *next;
+       (*last) = (*next);
+       (*next) = (*next)->next;
+}
+
+static event_t *merge_events(event_t *a, event_t *b)
+{
+       event_t *first = NULL, *last = NULL;
+       while (a && b)
+               if (compare(&a->start, &b->start) <= 0)
+                       add_event(&first, &last, &a);
+               else
+                       add_event(&first, &last, &b);
+       while (a) add_event(&first, &last, &a);
+       while (b) add_event(&first, &last, &b);
+       return first;
+}
+
+static todo_t *merge_todos(todo_t *a, todo_t *b)
+{
+       todo_t *first = NULL, *last = NULL;
+       while (a && b)
+               if (compare(&a->start, &b->start) <= 0)
+                       add_todo(&first, &last, &a);
+               else
+                       add_todo(&first, &last, &b);
+       while (a) add_todo(&first, &last, &a);
+       while (b) add_todo(&first, &last, &b);
+       return first;
+}
+
 /* Initialize */
 void cal_init(void)
 {
-       EVENTS = cal_events(2012, JAN, 0, 366);
-       TODOS  = cal_todos(2012, JAN, 0, 366);
+       /* Load a year's worth of data */
+       cal_load(YEAR-1, DEC, 31-7, 366+7+7);
 
        /* Debug */
        for (event_t *e = EVENTS; e; e = e->next)
@@ -40,16 +102,37 @@ void cal_init(void)
                                e->start.hour, e->start.min, e->name, e->desc);
 }
 
-/* Get events */
-event_t *cal_events(year_t year, month_t month, day_t day, int days)
+/* Load events and todos */
+void cal_load(year_t year, month_t month, day_t day, int days)
 {
-       return ical_events(0, year, month, day, days)
-          ?: dummy_events(0, year, month, day, days);
-}
+       year_t  eyear  = year;
+       month_t emonth = month;
+       day_t   eday   = day;
+       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))
+               return;
+
+       /* Free uneeded data */
+       // TODO
+
+       /* Push dates out a bit to avoid reloading,
+        * enough to at least cover the current year */
+       add_days(&year,  &month,  &day, -366);
+       add_days(&eyear, &emonth, &eday, 366);
+       start = (date_t){year,  month,  day};
+       end   = (date_t){eyear, emonth, eday};
+
+       /* Load events */
+       EVENTS = merge_events(
+               dummy_events(start, end),
+                ical_events(start, end));
+
+       /* Load todos */
+       TODOS  = merge_todos(
+               dummy_todos(start, end),
+                ical_todos(start, end));
 
-/* Get todos */
-todo_t *cal_todos(year_t year, month_t month, day_t day, int days)
-{
-       return ical_todos(0, year, month, day, days)
-          ?: dummy_todos(0, year, month, day, days);
 }