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