]> Pileus Git - lackey/blob - src/cal.c
Merge calendar items
[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 /* Global data */
25 event_t *EVENTS;
26 todo_t  *TODOS;
27
28 /* Merge events and todos */
29 static void add_event(event_t **first, event_t **last, event_t **next)
30 {
31         if (*last)
32                 (*last)->next = *next;
33         else
34                 (*first) = *next;
35         (*last) = (*next);
36         (*next) = (*next)->next;
37 }
38
39 static void add_todo(todo_t **first, todo_t **last, todo_t **next)
40 {
41         if (*last)
42                 (*last)->next = *next;
43         else
44                 (*first) = *next;
45         (*last) = (*next);
46         (*next) = (*next)->next;
47 }
48
49 static event_t *merge_events(event_t *a, event_t *b)
50 {
51         event_t *first = NULL, *last = NULL;
52         while (a && b)
53                 if (compare(&a->start, &b->start) <= 0)
54                         add_event(&first, &last, &a);
55                 else
56                         add_event(&first, &last, &b);
57         while (a) add_event(&first, &last, &a);
58         while (b) add_event(&first, &last, &b);
59         return first;
60 }
61
62 static todo_t *merge_todos(todo_t *a, todo_t *b)
63 {
64         todo_t *first = NULL, *last = NULL;
65         while (a && b)
66                 if (compare(&a->start, &b->start) <= 0)
67                         add_todo(&first, &last, &a);
68                 else
69                         add_todo(&first, &last, &b);
70         while (a) add_todo(&first, &last, &a);
71         while (b) add_todo(&first, &last, &b);
72         return first;
73 }
74
75 /* Initialize */
76 void cal_init(void)
77 {
78         EVENTS = cal_events(2012, JAN, 0, 366);
79         TODOS  = cal_todos(2012, JAN, 0, 366);
80
81         /* Debug */
82         for (event_t *e = EVENTS; e; e = e->next)
83                 debug("event: %04d-%02d-%02d %02d:%02d: %s - %s",
84                                 e->start.year, e->start.month, e->start.day,
85                                 e->start.hour, e->start.min, e->name, e->desc);
86         for (todo_t *e = TODOS; e; e = e->next)
87                 debug("todo: %04d-%02d-%02d %02d:%02d: %s - %s",
88                                 e->start.year, e->start.month, e->start.day,
89                                 e->start.hour, e->start.min, e->name, e->desc);
90 }
91
92 /* Get events */
93 event_t *cal_events(year_t year, month_t month, day_t day, int days)
94 {
95         event_t *dummy = dummy_events(0, year, month, day, days);
96         event_t *ical  =  ical_events(0, year, month, day, days);
97         return merge_events(dummy, ical);
98 }
99
100 /* Get todos */
101 todo_t *cal_todos(year_t year, month_t month, day_t day, int days)
102 {
103         todo_t *dummy = dummy_todos(0, year, month, day, days);
104         todo_t *ical  =  ical_todos(0, year, month, day, days);
105         return merge_todos(dummy, ical);
106 }