]> code.citadel.org Git - citadel.git/blob - webcit/calendar_view.c
* Put in back/forward (yesterday/tomorrow) links in calendar day view
[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 #include "ical.h"
40
41 void calendar_month_view_display_events(time_t thetime) {
42         int i;
43         struct tm *tm;
44         icalproperty *p;
45         struct icaltimetype t;
46         int month, day, year;
47         int all_day_event = 0;
48
49         if (WC->num_cal == 0) {
50                 wprintf("<BR><BR><BR>\n");
51                 return;
52         }
53
54         tm = localtime(&thetime);
55         month = tm->tm_mon + 1;
56         day = tm->tm_mday;
57         year = tm->tm_year + 1900;
58
59         for (i=0; i<(WC->num_cal); ++i) {
60                 p = icalcomponent_get_first_property(WC->disp_cal[i],
61                                                 ICAL_DTSTART_PROPERTY);
62                 if (p != NULL) {
63                         t = icalproperty_get_dtstart(p);
64                         if ((t.year == year)
65                            && (t.month == month)
66                            && (t.day == day)) {
67
68                                 if (t.is_date) all_day_event = 1;
69
70                                 p = icalcomponent_get_first_property(
71                                                         WC->disp_cal[i],
72                                                         ICAL_SUMMARY_PROPERTY);
73                                 if (p != NULL) {
74
75                                         if (all_day_event) {
76                                                 wprintf("<TABLE border=1 cellpadding=2><TR>"
77                                                         "<TD BGCOLOR=#CCCCCC>"
78                                                 );
79                                         }
80
81                                         wprintf("<FONT SIZE=-1>"
82                                                 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=%s&year=%s&month=%s&day=%s\">",
83                                                 WC->cal_msgnum[i],
84                                                 bstr("calview"),
85                                                 bstr("year"),
86                                                 bstr("month"),
87                                                 bstr("day")
88                                         );
89                                         escputs((char *)
90                                                 icalproperty_get_comment(p));
91                                         wprintf("</A></FONT><BR>\n");
92
93                                         if (all_day_event) {
94                                                 wprintf("</TD></TR></TABLE>");
95                                         }
96
97                                 }
98
99                         }
100
101
102                 }
103         }
104 }
105
106
107
108 void calendar_month_view(int year, int month, int day) {
109         struct tm starting_tm;
110         struct tm *tm;
111         time_t thetime;
112         int i;
113         time_t previous_month;
114         time_t next_month;
115
116         /* Determine what day to start.
117          * First, back up to the 1st of the month...
118          */
119         memset(&starting_tm, 0, sizeof(struct tm));
120         starting_tm.tm_year = year - 1900;
121         starting_tm.tm_mon = month - 1;
122         starting_tm.tm_mday = day;
123         thetime = mktime(&starting_tm);
124
125         tm = &starting_tm;
126         while (tm->tm_mday != 1) {
127                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
128                 tm = localtime(&thetime);
129         }
130
131         /* Determine previous and next months ... for links */
132         previous_month = thetime - (time_t)864000L;     /* back 10 days */
133         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
134
135         /* Now back up until we're on a Sunday */
136         tm = localtime(&thetime);
137         while (tm->tm_wday != 0) {
138                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
139                 tm = localtime(&thetime);
140         }
141
142         /* Outer table (to get the background color) */
143         wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
144                 "bgcolor=#4444FF><TR><TD>\n");
145
146         wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0>"
147                 "<TR><TD align=left><font color=#FFFFFF>"
148                 "&nbsp;<A HREF=\"/display_edit_event?msgnum=0\">"
149                 "Add new calendar event</A>"
150                 "</font></TD>\n");
151
152         wprintf("<TD><CENTER><H3>");
153
154         tm = localtime(&previous_month);
155         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
156                 (int)(tm->tm_year)+1900, tm->tm_mon + 1);
157         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>\n");
158
159         wprintf("&nbsp;&nbsp;"
160                 "<FONT COLOR=#FFFFFF>"
161                 "%s %d"
162                 "</FONT>"
163                 "&nbsp;&nbsp;", months[month-1], year);
164
165         tm = localtime(&next_month);
166         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
167                 (int)(tm->tm_year)+1900, tm->tm_mon + 1);
168         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\" BORDER=0></A>\n");
169
170         wprintf("</H3></TD><TD align=right><font color=#FFFFFF size=-2>"
171                 "Click on any date for day view&nbsp;"
172                 "</FONT></TD></TR></TABLE>\n");
173
174         /* Inner table (the real one) */
175         wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
176                 "bgcolor=#4444FF>");
177         for (i=0; i<7; ++i) {
178                 wprintf("<TH><FONT COLOR=#FFFFFF>%s</FONT></TH>", days[i]);
179         }
180
181         /* Now do 35 days */
182         for (i = 0; i < 35; ++i) {
183                 tm = localtime(&thetime);
184                 if (tm->tm_wday == 0) {
185                         wprintf("<TR>");
186                 }
187
188                 wprintf("<TD BGCOLOR=%s WIDTH=14%% HEIGHT=60 VALIGN=TOP><B>",
189                         ((tm->tm_mon != month-1) ? "DDDDDD" :
190                         ((tm->tm_wday==0 || tm->tm_wday==6) ? "EEEECC" :
191                         "FFFFFF"))
192                 );
193                 if ((i==0) || (tm->tm_mday == 1)) {
194                         wprintf("%s ", months[tm->tm_mon]);
195                 }
196                 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
197                         "%d</A></B><BR>",
198                         tm->tm_year + 1900,
199                         tm->tm_mon + 1,
200                         tm->tm_mday,
201                         tm->tm_mday);
202
203                 /* put the data here, stupid */
204                 calendar_month_view_display_events(thetime);
205
206                 wprintf("</TD>");
207
208                 if (tm->tm_wday == 6) {
209                         wprintf("</TR>\n");
210                 }
211
212                 thetime += (time_t)86400;               /* ahead 24 hours */
213         }
214
215         wprintf("</TABLE>"                      /* end of inner table */
216                 "</TD></TR></TABLE>"            /* end of outer table */
217                 "</CENTER>\n");
218 }
219
220
221 void calendar_week_view(int year, int month, int day) {
222         wprintf("<CENTER><I>week view FIXME</I></CENTER><BR>\n");
223 }
224
225
226 /*
227  * Display events for a particular hour of a particular day.
228  * (Specify hour < 0 to show "all day" events)
229  */
230 void calendar_day_view_display_events(int year, int month,
231                                         int day, int hour) {
232         int i;
233         icalproperty *p;
234         struct icaltimetype t;
235         int all_day_event = 0;
236
237         if (WC->num_cal == 0) {
238                 wprintf("<BR><BR><BR>\n");
239                 return;
240         }
241
242         for (i=0; i<(WC->num_cal); ++i) {
243                 p = icalcomponent_get_first_property(WC->disp_cal[i],
244                                                 ICAL_DTSTART_PROPERTY);
245                 if (p != NULL) {
246                         t = icalproperty_get_dtstart(p);
247                         if ((t.year == year)
248                            && (t.month == month)
249                            && (t.day == day)
250                            && ( ((t.hour == hour)&&(!t.is_date)) || ((hour<0)&&(t.is_date)) )
251                            ) {
252
253                                 if (t.is_date) all_day_event = 1;
254
255                                 p = icalcomponent_get_first_property(
256                                                         WC->disp_cal[i],
257                                                         ICAL_SUMMARY_PROPERTY);
258                                 if (p != NULL) {
259
260                                         if (all_day_event) {
261                                                 wprintf("<TABLE border=1 cellpadding=2><TR>"
262                                                         "<TD BGCOLOR=#CCCCCC>"
263                                                 );
264                                         }
265
266                                         wprintf("<FONT SIZE=-1>"
267                                                 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
268                                                 WC->cal_msgnum[i],
269                                                 year, month, day
270                                         );
271                                         escputs((char *)
272                                                 icalproperty_get_comment(p));
273                                         wprintf("</A></FONT><BR>\n");
274
275                                         if (all_day_event) {
276                                                 wprintf("</TD></TR></TABLE>");
277                                         }
278                                 }
279
280                         }
281
282
283                 }
284         }
285 }
286
287
288
289 void calendar_day_view(int year, int month, int day) {
290         int hour;
291         struct icaltimetype today, yesterday, tomorrow;
292
293
294         /* Figure out the dates for "yesterday" and "tomorrow" links */
295
296         memset(&today, 0, sizeof(struct icaltimetype));
297         today.year = year;
298         today.month = month;
299         today.day = day;
300         today.is_date = 1;
301
302         memcpy(&yesterday, &today, sizeof(struct icaltimetype));
303         --yesterday.day;
304         yesterday = icaltime_normalize(yesterday);
305
306         memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
307         ++tomorrow.day;
308         tomorrow = icaltime_normalize(tomorrow);
309
310
311         /* Outer table (to get the background color) */
312         wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
313                 "bgcolor=#4444FF><TR><TD>\n");
314
315         /* Inner table (the real one) */
316         wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
317                 "bgcolor=#4444FF><TR>\n");
318
319         wprintf("<TD WIDTH=50%% VALIGN=top>");  /* begin stuff-on-the-left */
320
321         wprintf("<CENTER><H3>");
322         wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
323                 yesterday.year, yesterday.month, yesterday.day
324         );
325         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>\n");
326         wprintf("&nbsp;&nbsp;<FONT COLOR=#FFFFFF>"
327                 "%s %d, %d"
328                 "</FONT>&nbsp;&nbsp;",
329                 months[month-1], day, year);
330         wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
331                 tomorrow.year, tomorrow.month, tomorrow.day
332         );
333         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\" BORDER=0></A>\n");
334         wprintf("</H3></CENTER>\n");
335
336         wprintf("<CENTER><font color=#FFFFFF>"
337                 "&nbsp;<A HREF=\"/display_edit_event?msgnum=0\">"
338                 "Add new calendar event</A>"
339                 "<BR><BR>\n");
340
341         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">"
342                 "Back to month view</A>\n", year, month);
343
344         wprintf("</FONT></CENTER>\n");
345
346         wprintf("</TD>");                       /* end stuff-on-the-left */
347
348         /* Innermost table (contains hours etc.) */
349         wprintf("<TD WIDTH=50%%>"
350                 "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
351                 "bgcolor=#4444FF>\n");
352
353         /* Display events before 8:00 (hour=-1 is all-day events) */
354         wprintf("<TR><TD BGCOLOR=FFFFFF VALIGN=TOP>");
355         for (hour = (-1); hour <= 7; ++hour) {
356                 calendar_day_view_display_events(year, month, day, hour);
357         }
358         wprintf("</TD></TR>\n");
359
360         /* Now the middle of the day... */      
361         for (hour = 8; hour <= 17; ++hour) {    /* could do HEIGHT=xx */
362                 wprintf("<TR><TD BGCOLOR=FFFFFF VALIGN=TOP>");
363                 wprintf("%d:00%s ",
364                         (hour <= 12 ? hour : hour-12),
365                         (hour < 12 ? "am" : "pm")
366                 );
367
368                 /* put the data here, stupid */
369                 calendar_day_view_display_events(year, month, day, hour);
370
371                 wprintf("</TD></TR>\n");
372         }
373
374         /* Display events after 5:00... */
375         wprintf("<TR><TD BGCOLOR=FFFFFF VALIGN=TOP>");
376         for (hour = 18; hour <= 23; ++hour) {
377                 calendar_day_view_display_events(year, month, day, hour);
378         }
379         wprintf("</TD></TR>\n");
380
381
382         wprintf("</TABLE>"                      /* end of innermost table */
383                 "</TD></TR></TABLE>"            /* end of inner table */
384                 "</TD></TR></TABLE>"            /* end of outer table */
385         );
386
387
388
389 }
390
391
392
393
394 void do_calendar_view(void) {
395         int i;
396         time_t now;
397         struct tm *tm;
398         int year, month, day;
399         char calview[SIZ];
400
401         /* In case no date was specified, go with today */
402         now = time(NULL);
403         tm = localtime(&now);
404         year = tm->tm_year + 1900;
405         month = tm->tm_mon + 1;
406         day = tm->tm_mday;
407
408         /* Now see if a date was specified */
409         if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
410         if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
411         if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
412
413         /* How would you like that cooked? */
414         if (strlen(bstr("calview")) > 0) {
415                 strcpy(calview, bstr("calview"));
416         }
417         else {
418                 strcpy(calview, "month");
419         }
420
421         /* Display the selected view */
422         if (!strcasecmp(calview, "day")) {
423                 calendar_day_view(year, month, day);
424         }
425         else if (!strcasecmp(calview, "week")) {
426                 calendar_week_view(year, month, day);
427         }
428         else {
429                 calendar_month_view(year, month, day);
430         }
431
432         /* Free the calendar stuff */
433         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
434                 icalcomponent_free(WC->disp_cal[i]);
435         }
436         WC->num_cal = 0;
437         free(WC->disp_cal);
438         WC->disp_cal = NULL;
439         free(WC->cal_msgnum);
440         WC->cal_msgnum = NULL;
441 }
442
443
444 #endif  /* HAVE_ICAL_H */