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