]> Pileus Git - lackey/blob - src/cal.c
Add cal_load function and update cal APIs
[lackey] / src / cal.c
1 /*
2  * Copyright (C) 2012 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <stdlib.h>
19
20 #include "util.h"
21 #include "date.h"
22 #include "cal.h"
23
24 /* Macros */
25 #define CAL(name) \
26         event_t *name##_events(date_t start, date_t end); \
27         todo_t  *name##_todos(date_t start, date_t end)
28
29 /* Prototypes */
30 CAL(dummy);
31 CAL(ical);
32
33 /* Global data */
34 event_t *EVENTS;
35 todo_t  *TODOS;
36
37 /* Local data */
38 static date_t start;
39 static date_t end;
40
41 /* Merge events and todos */
42 static void add_event(event_t **first, event_t **last, event_t **next)
43 {
44         if (*last)
45                 (*last)->next = *next;
46         else
47                 (*first) = *next;
48         (*last) = (*next);
49         (*next) = (*next)->next;
50 }
51
52 static void add_todo(todo_t **first, todo_t **last, todo_t **next)
53 {
54         if (*last)
55                 (*last)->next = *next;
56         else
57                 (*first) = *next;
58         (*last) = (*next);
59         (*next) = (*next)->next;
60 }
61
62 static event_t *merge_events(event_t *a, event_t *b)
63 {
64         event_t *first = NULL, *last = NULL;
65         while (a && b)
66                 if (compare(&a->start, &b->start) <= 0)
67                         add_event(&first, &last, &a);
68                 else
69                         add_event(&first, &last, &b);
70         while (a) add_event(&first, &last, &a);
71         while (b) add_event(&first, &last, &b);
72         return first;
73 }
74
75 static todo_t *merge_todos(todo_t *a, todo_t *b)
76 {
77         todo_t *first = NULL, *last = NULL;
78         while (a && b)
79                 if (compare(&a->start, &b->start) <= 0)
80                         add_todo(&first, &last, &a);
81                 else
82                         add_todo(&first, &last, &b);
83         while (a) add_todo(&first, &last, &a);
84         while (b) add_todo(&first, &last, &b);
85         return first;
86 }
87
88 /* Initialize */
89 void cal_init(void)
90 {
91         /* Load a year's worth of data */
92         cal_load(YEAR-1, DEC, 31-7, 366+7+7);
93
94         /* Debug */
95         for (event_t *e = EVENTS; e; e = e->next)
96                 debug("event: %04d-%02d-%02d %02d:%02d: %s - %s",
97                                 e->start.year, e->start.month, e->start.day,
98                                 e->start.hour, e->start.min, e->name, e->desc);
99         for (todo_t *e = TODOS; e; e = e->next)
100                 debug("todo: %04d-%02d-%02d %02d:%02d: %s - %s",
101                                 e->start.year, e->start.month, e->start.day,
102                                 e->start.hour, e->start.min, e->name, e->desc);
103 }
104
105 /* Load events and todos */
106 void cal_load(year_t year, month_t month, day_t day, int days)
107 {
108         year_t  eyear  = year;
109         month_t emonth = month;
110         day_t   eday   = day;
111         add_days(&eyear, &emonth, &eday, days);
112
113         /* Skip if we already loaded enough info */
114         if (!before(&start, year,  month,  day,  0, 0) &&
115              before(&end,  eyear, emonth, eday, 24, 0))
116                 return;
117
118         /* Free uneeded data */
119         // TODO
120
121         /* Push dates out a bit to avoid reloading,
122          * enough to at least cover the current year */
123         add_days(&year,  &month,  &day, -366);
124         add_days(&eyear, &emonth, &eday, 366);
125         start = (date_t){year,  month,  day};
126         end   = (date_t){eyear, emonth, eday};
127
128         /* Load events */
129         EVENTS = merge_events(
130                 dummy_events(start, end),
131                  ical_events(start, end));
132
133         /* Load todos */
134         TODOS  = merge_todos(
135                 dummy_todos(start, end),
136                  ical_todos(start, end));
137
138 }