2 * Copyright (C) 2012-2013 Andy Spencer <andy753421@gmail.com>
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.
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.
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/>.
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)
37 event_t *EVENT, *EVENTS;
44 /* Merge events and todos */
45 static void add_event(event_t **first, event_t **last, event_t **next)
48 (*last)->next = *next;
51 (*next)->prev = *last;
53 (*next) = (*next)->next;
56 static void add_todo(todo_t **first, todo_t **last, todo_t **next)
59 (*last)->next = *next;
62 (*next)->prev = *last;
64 (*next) = (*next)->next;
67 static cal_t *merge_cals(cal_t *a, cal_t *b)
69 // TODO - we should sort these
79 static event_t *merge_events(event_t *a, event_t *b)
81 event_t *first = NULL, *last = NULL;
83 if (compare(&a->start, &b->start) <= 0)
84 add_event(&first, &last, &a);
86 add_event(&first, &last, &b);
87 while (a) add_event(&first, &last, &a);
88 while (b) add_event(&first, &last, &b);
92 static todo_t *merge_todos(todo_t *a, todo_t *b)
94 todo_t *first = NULL, *last = NULL;
96 if (compare(&a->start, &b->start) <= 0)
97 add_todo(&first, &last, &a);
99 add_todo(&first, &last, &b);
100 while (a) add_todo(&first, &last, &a);
101 while (b) add_todo(&first, &last, &b);
114 cal_load(SEL.year, SEL.month, SEL.day, 1);
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);
129 /* Load events and todos */
130 void cal_load(year_t year, month_t month, day_t day, int days)
133 month_t emonth = month;
135 add_days(&eyear, &emonth, &eday, days);
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))
142 /* Free uneeded data */
143 for (event_t *next, *cur = EVENTS; cur; 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);
151 for (todo_t *next, *cur = TODOS; cur; cur = next) {
153 if (cur->name) free(cur->name);
154 if (cur->desc) free(cur->desc);
155 if (cur->cat) free(cur->cat);
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};
167 EVENTS = merge_events(
168 dummy_events(start, end),
169 ical_events(start, end));
173 dummy_todos(start, end),
174 ical_todos(start, end));
176 /* Verify events and todos*/
178 for (event_t *cur = EVENTS; cur; cur = cur->next) {
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);
185 for (todo_t *cur = TODOS; cur; cur = cur->next) {
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);
196 void cal_config(const char *group, const char *name, const char *key, const char *value)
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);
204 /* Find event for matching target date */
205 event_t *find_event(date_t *target)
208 event_t *event = NULL;
210 if (EVENT && compare(&EVENT->start, target) == 0)
213 for (event_t *cur = EVENTS; cur; cur = cur->next) {
214 // Skip events that are on the wrong day
215 if ((target->year != cur->start.year) ||
216 (target->month != cur->start.month) ||
217 (target->day != cur->start.day))
220 // We don't want time change or leap seconds here
221 int diff = (target->hour - cur->start.hour) * 60 * 24 +
222 (target->min - cur->start.min) * 60 +
223 (target->sec - cur->start.sec);
225 if (event == NULL || ABS(diff) < min) {