]> Pileus Git - lackey/blob - view/week.c
Working iCalendar parsing
[lackey] / view / week.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 #define _XOPEN_SOURCE_EXTENDED
19
20 #include <ncurses.h>
21
22 #include "util.h"
23 #include "date.h"
24 #include "event.h"
25
26 /* Static data */
27 static WINDOW *win;
28
29 /* Local functions */
30 static void print_event(event_t *event, wday_t day, hour_t hour, min_t min, float hstep)
31 {
32         int x = 6+ROUND(day*hstep);
33         int y = 3+hour*4;
34         int l = (event->end.min - event->start.min)/15;
35         mvwprintw(win, y, x, "%s", event->name);
36         debug("week: event = %s\n", event->name);
37         (void)l;
38 }
39
40 static int before(date_t *start, int year, int month, int day, int hour, int min)
41 {
42         int rval = start->year  < year  ? 1 : start->year  > year ? 0 :
43                    start->month < month ? 1 : start->month > month? 0 :
44                    start->day   < day   ? 1 : start->day   > day  ? 0 :
45                    start->hour  < hour  ? 1 : start->hour  > hour ? 0 :
46                    start->min   < min   ? 1 : start->min   > min  ? 0 : 0;
47         debug("week: %04d-%02d-%02d %02d:%02d < %04d-%02d-%02d %02d:%02d == %d\n",
48                         start->year, start->month, start->day, start->hour, start->min,
49                         year, month, day, hour, min, rval);
50         return rval;
51 }
52
53 /* Week init */
54 void week_init(WINDOW *_win)
55 {
56         win = _win;
57 }
58
59 /* Week draw */
60 void week_draw(void)
61 {
62         int x = 6;
63         int y = 3;
64         const float hstep = (float)(COLS-x)/7.0;
65
66         /* Clear */
67         werase(win);
68
69         /* Get start of week */
70         year_t  year  = YEAR;
71         month_t month = MONTH;
72         day_t   day   = DAY;
73         int shift = day_of_week(year, month, day);
74         add_days(&year, &month, &day, -shift);
75
76         /* Print Header */
77         mvwprintw(win, 1, 0, "%s", month_to_str(MONTH));
78         for (int d = 0; d < 7; d++) {
79                 const char *str = hstep >= 10 ? day_to_string(d) : day_to_str(d);
80                 if (d == shift) wattron(win, A_BOLD);
81                 mvwprintw(win, 0, x+ROUND(d*hstep), "%02d/%02d", month+1, day+1);
82                 mvwprintw(win, 1, x+ROUND(d*hstep), "%s", str);
83                 if (d == shift) wattroff(win, A_BOLD);
84                 add_days(&year, &month, &day, 1);
85         }
86
87         /* Print times */
88         hour_t start = 8;
89         for (int h = 0; h < (LINES-6)/4+1; h++)
90                 mvwprintw(win, 3+h*4, 0,"%02d:%02d", (start+h)%12, 0);
91
92         /* Print lines */
93         mvwhline(win, y-1, 0, ACS_HLINE, COLS);
94         for (int d = 0; d < 7; d++)
95                 mvwvline(win, y, x+ROUND(d*hstep)-1, ACS_VLINE, LINES-y-2);
96
97         /* Print events */
98         event_t *event = EVENTS;
99         add_days(&year, &month, &day, -7);
100         for (int d = 0; d <  7; d++) {
101                 for (int h = 0; h < (LINES-6)/4+1; h++) {
102                         for (int m = 0; m < 60; m+=15) {
103                                 while (event && before(&event->start, year, month, day, start+h+(m+15)/60, (m+15)%60)) {
104                                         if (!before(&event->start, year, month, day, start+h, m))
105                                                 print_event(event, d, h, m, hstep);
106                                         event = event->next;
107                                 }
108                         }
109                 }
110                 add_days(&year, &month, &day, 1);
111         }
112
113         /* Draw today */
114         int l = x+ROUND((shift+0)*hstep)-1;
115         int r = x+ROUND((shift+1)*hstep)-1;
116         mvwhline    (win, y-1, l, ACS_BLOCK, r-l+1);
117         mvwvline_set(win, y,   l, WACS_T_VLINE, LINES-y-2);
118         mvwvline_set(win, y,   r, WACS_T_VLINE, LINES-y-2);
119 }
120
121 /* Week run */
122 int week_run(int key, mmask_t btn, int row, int col)
123 {
124         int days = 0;
125         switch (key)
126         {
127                 case 'h': days = -1; break;
128                 case 'l': days =  1; break;
129                 case 'i': days = -7; break;
130                 case 'o': days =  7; break;
131         }
132         if (days) {
133                 add_days(&YEAR, &MONTH, &DAY, days);
134                 week_draw();
135                 wrefresh(win);
136         }
137         return 0;
138 }