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