]> code.citadel.org Git - citadel.git/blob - webcit/calendar_view.c
* Fixed buggy month-selection algorithm (January is month 0 in 'struct tm' but
[citadel.git] / webcit / calendar_view.c
1 /*
2  * $Id$
3  *
4  *
5  */
6
7 #include <ctype.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <sys/socket.h>
16 #include <limits.h>
17 #include <netinet/in.h>
18 #include <netdb.h>
19 #include <string.h>
20 #include <pwd.h>
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <pthread.h>
24 #include <signal.h>
25 #include <time.h>
26 #include "webcit.h"
27 #include "webserver.h"
28
29 #ifndef HAVE_ICAL_H
30
31 void do_calendar_view(void) {   /* stub for non-libical builds */
32         wprintf("<CENTER><I>Calendar view not available</I></CENTER><BR>\n");
33 }
34
35 #else   /* HAVE_ICAL_H */
36
37 /****************************************************************************/
38
39 void calendar_month_view_display_events(time_t thetime) {
40         int i;
41         struct tm *tm;
42         icalproperty *p;
43         struct icaltimetype t;
44         int month, day, year;
45
46         if (WC->num_cal == 0) {
47                 wprintf("<BR><BR><BR>\n");
48                 return;
49         }
50
51         tm = localtime(&thetime);
52         month = tm->tm_mon + 1;
53         day = tm->tm_mday;
54         year = tm->tm_year + 1900;
55
56         for (i=0; i<(WC->num_cal); ++i) {
57                 p = icalcomponent_get_first_property(WC->disp_cal[i],
58                                                 ICAL_DTSTART_PROPERTY);
59                 if (p != NULL) {
60                         t = icalproperty_get_dtstart(p);
61                         if ((t.year == year)
62                            && (t.month == month)
63                            && (t.day == day)) {
64
65                                 p = icalcomponent_get_first_property(
66                                                         WC->disp_cal[i],
67                                                         ICAL_SUMMARY_PROPERTY);
68                                 if (p != NULL) {
69                                         escputs((char *)
70                                                 icalproperty_get_comment(p));
71                                 }
72
73                         }
74
75
76                 }
77         }
78 }
79
80
81
82 void calendar_month_view(int year, int month, int day) {
83         struct tm starting_tm;
84         struct tm *tm;
85         time_t thetime;
86         int i;
87         time_t previous_month;
88         time_t next_month;
89
90         /* Determine what day to start.
91          * First, back up to the 1st of the month...
92          */
93         memset(&starting_tm, 0, sizeof(struct tm));
94         starting_tm.tm_year = year - 1900;
95         starting_tm.tm_mon = month - 1;
96         starting_tm.tm_mday = day;
97         thetime = mktime(&starting_tm);
98         lprintf(9, "Starting at %s", asctime(localtime(&thetime)));
99
100         tm = &starting_tm;
101         while (tm->tm_mday != 1) {
102                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
103                 lprintf(9, "Backing off to %s", asctime(localtime(&thetime)));
104                 tm = localtime(&thetime);
105         }
106
107         /* Determine previous and next months ... for links */
108         previous_month = thetime - (time_t)864000L;     /* back 10 days */
109         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
110         lprintf(9, "previous month is %s", asctime(localtime(&previous_month)));
111         lprintf(9, "next month is %s", asctime(localtime(&next_month)));
112
113         /* Now back up until we're on a Sunday */
114         tm = localtime(&thetime);
115         while (tm->tm_wday != 0) {
116                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
117                 lprintf(9, "Backing off to %s", asctime(localtime(&thetime)));
118                 tm = localtime(&thetime);
119         }
120
121         /* Outer table (to get the background color) */
122         wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
123                 "bgcolor=#4444FF><TR><TD>\n");
124
125         wprintf("<CENTER><H3>");
126
127         tm = localtime(&previous_month);
128         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
129                 (int)(tm->tm_year)+1900, tm->tm_mon + 1);
130         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>\n");
131
132         wprintf("&nbsp;&nbsp;"
133                 "<FONT COLOR=#FFFFFF>"
134                 "%s %d"
135                 "</FONT>"
136                 "&nbsp;&nbsp;", months[month-1], year);
137
138         tm = localtime(&next_month);
139         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
140                 (int)(tm->tm_year)+1900, tm->tm_mon + 1);
141         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\" BORDER=0></A>\n");
142
143         wprintf("</H3>");
144
145         /* Inner table (the real one) */
146         wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
147                 "bgcolor=#4444FF>");
148         for (i=0; i<7; ++i) {
149                 wprintf("<TH><FONT COLOR=#FFFFFF>%s</FONT></TH>", days[i]);
150         }
151
152         /* Now do 35 days */
153         for (i = 0; i < 35; ++i) {
154                 tm = localtime(&thetime);
155                 if (tm->tm_wday == 0) {
156                         wprintf("<TR>");
157                 }
158
159                 wprintf("<TD BGCOLOR=FFFFFF WIDTH=14%% HEIGHT=60 VALIGN=TOP>"
160                         "<B>");
161                 if ((i==0) || (tm->tm_mday == 1)) {
162                         wprintf("%s ", months[tm->tm_mon]);
163                 }
164                 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
165                         "%d</A></B><BR>",
166                         tm->tm_year + 1900,
167                         tm->tm_mon + 1,
168                         tm->tm_mday,
169                         tm->tm_mday);
170
171                 /* put the data here, stupid */
172                 calendar_month_view_display_events(thetime);
173
174                 wprintf("</TD>");
175
176                 if (tm->tm_wday == 6) {
177                         wprintf("</TR>\n");
178                 }
179
180                 thetime += (time_t)86400;               /* ahead 24 hours */
181         }
182
183         wprintf("</TABLE>"                      /* end of inner table */
184                 "</TD></TR></TABLE>"            /* end of outer table */
185                 "</CENTER>\n");
186 }
187
188
189 void calendar_week_view(int year, int month, int day) {
190         wprintf("<CENTER><I>week view FIXME</I></CENTER><BR>\n");
191 }
192
193
194 void calendar_day_view(int year, int month, int day) {
195         struct tm starting_tm;
196         time_t thetime;
197
198         /* Determine what day we're viewing.
199          */
200         memset(&starting_tm, 0, sizeof(struct tm));
201         starting_tm.tm_year = year - 1900;
202         starting_tm.tm_mon = month - 1;
203         starting_tm.tm_mday = day;
204         thetime = mktime(&starting_tm);
205         lprintf(9, "Starting at %s", asctime(localtime(&thetime)));
206
207         /* put the data here, stupid */
208         calendar_month_view_display_events(thetime);
209
210         wprintf("<CENTER><I>FIXME day view for %02d/%02d/%04d</I>"
211                 "</CENTER><BR>\n", month, day, year);
212         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">"
213                 "Back to month view</A><BR>\n", year, month);
214
215
216 }
217
218
219
220
221
222 void do_calendar_view(void) {
223         int i;
224         time_t now;
225         struct tm *tm;
226         int year, month, day;
227         char calview[SIZ];
228
229         /* In case no date was specified, go with today */
230         now = time(NULL);
231         tm = localtime(&now);
232         year = tm->tm_year + 1900;
233         month = tm->tm_mon + 1;
234         day = tm->tm_mday;
235
236         /* Now see if a date was specified */
237         if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
238         if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
239         if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
240
241         /* How would you like that cooked? */
242         if (strlen(bstr("calview")) > 0) {
243                 strcpy(calview, bstr("calview"));
244         }
245         else {
246                 strcpy(calview, "month");
247         }
248
249         /* Display the selected view */
250         if (!strcasecmp(calview, "day")) {
251                 calendar_day_view(year, month, day);
252         }
253         else if (!strcasecmp(calview, "week")) {
254                 calendar_week_view(year, month, day);
255         }
256         else {
257                 calendar_month_view(year, month, day);
258         }
259
260         /* Free the calendar stuff */
261         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
262                 icalcomponent_free(WC->disp_cal[i]);
263         }
264         WC->num_cal = 0;
265         free(WC->disp_cal);
266         WC->disp_cal = NULL;
267         free(WC->cal_msgnum);
268         WC->cal_msgnum = NULL;
269 }
270
271
272 #endif  /* HAVE_ICAL_H */