]> Pileus Git - lackey/blob - src/cal.c
d02e885eb034481eb40b2851912bd8fb5a827fc3
[lackey] / src / cal.c
1 /*
2  * Copyright (C) 2012-2013 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         void     name##_config(const char *group, const char *name, const char *key, const char *value); \
27         cal_t   *name##_cals(void); \
28         event_t *name##_events(date_t start, date_t end); \
29         todo_t  *name##_todos(date_t start, date_t end)
30
31 /* Prototypes */
32 CAL(dummy);
33 CAL(ical);
34
35 /* Global data */
36 cal_t   *CAL,   *CALS;
37 event_t *EVENT, *EVENTS;
38 todo_t  *TODO,  *TODOS;
39
40 /* Local data */
41 static date_t start;
42 static date_t end;
43
44 /* Merge events and todos */
45 static void add_event(event_t **first, event_t **last, event_t **next)
46 {
47         if (*last)
48                 (*last)->next = *next;
49         else
50                 (*first) = *next;
51         (*next)->prev = *last;
52         (*last) = (*next);
53         (*next) = (*next)->next;
54 }
55
56 static void add_todo(todo_t **first, todo_t **last, todo_t **next)
57 {
58         if (*last)
59                 (*last)->next = *next;
60         else
61                 (*first) = *next;
62         (*next)->prev = *last;
63         (*last) = (*next);
64         (*next) = (*next)->next;
65 }
66
67 static cal_t *merge_cals(cal_t *a, cal_t *b)
68 {
69         // TODO - we should sort these
70         if (!a) return b;
71         if (!b) return a;
72         cal_t *last = a;
73         while (last->next)
74                 last = last->next;
75         last->next = b;
76         return a;
77 }
78
79 static event_t *merge_events(event_t *a, event_t *b)
80 {
81         event_t *first = NULL, *last = NULL;
82         while (a && b)
83                 if (compare(&a->start, &b->start) <= 0)
84                         add_event(&first, &last, &a);
85                 else
86                         add_event(&first, &last, &b);
87         while (a) add_event(&first, &last, &a);
88         while (b) add_event(&first, &last, &b);
89         return first;
90 }
91
92 static todo_t *merge_todos(todo_t *a, todo_t *b)
93 {
94         todo_t *first = NULL, *last = NULL;
95         while (a && b)
96                 if (compare(&a->start, &b->start) <= 0)
97                         add_todo(&first, &last, &a);
98                 else
99                         add_todo(&first, &last, &b);
100         while (a) add_todo(&first, &last, &a);
101         while (b) add_todo(&first, &last, &b);
102         return first;
103 }
104
105 /* Initialize */
106 void cal_init(void)
107 {
108         /* Load calendars */
109         CALS = merge_cals(
110                 dummy_cals(),
111                  ical_cals());
112
113         /* Load data */
114         cal_load(SEL.year, SEL.month, SEL.day, 1);
115
116         /* Debug */
117 #ifdef DEBUG_CALS
118         for (event_t *e = EVENTS; e; e = e->next)
119                 debug("event: %04d-%02d-%02d %02d:%02d: %s - %s",
120                                 e->start.year, e->start.month, e->start.day,
121                                 e->start.hour, e->start.min, e->name, e->desc);
122         for (todo_t *e = TODOS; e; e = e->next)
123                 debug("todo: %04d-%02d-%02d %02d:%02d: %s - %s",
124                                 e->start.year, e->start.month, e->start.day,
125                                 e->start.hour, e->start.min, e->name, e->desc);
126 #endif
127 }
128
129 /* Load events and todos */
130 void cal_load(year_t year, month_t month, day_t day, int days)
131 {
132         year_t  eyear  = year;
133         month_t emonth = month;
134         day_t   eday   = day;
135         add_days(&eyear, &emonth, &eday, days);
136
137         /* Skip if we already loaded enough info */
138         if (before(&start, year,  month,  day,  0, 0) &&
139            !before(&end,  eyear, emonth, eday, 24, 0))
140                 return;
141
142         /* Free uneeded data */
143         for (event_t *next, *cur = EVENTS; cur; cur = next) {
144                 next = cur->next;
145                 if (cur->name) free(cur->name);
146                 if (cur->desc) free(cur->desc);
147                 if (cur->loc)  free(cur->loc);
148                 if (cur->cat)  free(cur->cat);
149                 free(cur);
150         }
151         for (todo_t *next, *cur = TODOS; cur; cur = next) {
152                 next = cur->next;
153                 if (cur->name) free(cur->name);
154                 if (cur->desc) free(cur->desc);
155                 if (cur->cat)  free(cur->cat);
156                 free(cur);
157         }
158
159         /* Push dates out a bit to avoid reloading,
160          * enough to at least cover the current year */
161         add_days(&year,  &month,  &day, -366);
162         add_days(&eyear, &emonth, &eday, 366);
163         start = (date_t){year,  month,  day};
164         end   = (date_t){eyear, emonth, eday};
165
166         /* Load events */
167         EVENTS = merge_events(
168                 dummy_events(start, end),
169                  ical_events(start, end));
170
171         /* Load todos */
172         TODOS  = merge_todos(
173                 dummy_todos(start, end),
174                  ical_todos(start, end));
175
176         /* Verify events and todos*/
177 #ifdef DEBUG_CALS
178         for (event_t *cur = EVENTS; cur; cur = cur->next) {
179                 if (!cur->cal)
180                         error("Missing cal in event '%s'", cur->name);
181                 if ((cur->next && cur->next->prev != cur) ||
182                     (cur->prev && cur->prev->next != cur))
183                         error("Broken link in event '%s'", cur->name);
184         }
185         for (todo_t *cur = TODOS; cur; cur = cur->next) {
186                 if (!cur->cal)
187                         error("Missing cal in todo '%s'", cur->name);
188                 if ((cur->next && cur->next->prev != cur) ||
189                     (cur->prev && cur->prev->next != cur))
190                         error("Broken link in todo '%s'", cur->name);
191         }
192 #endif
193 }
194
195 /* Config parser */
196 void cal_config(const char *group, const char *name, const char *key, const char *value)
197 {
198         if (match(group, "dummy"))
199                 dummy_config(group, name, key, value);
200         else if (match(group, "ical"))
201                 ical_config(group, name, key, value);
202 }