5bfe3497d45faefa9ae8bdae9c0acd17f3065939
[citadel.git] / webcit / calendar_view.c
1 /*
2  * $Id$
3  *
4  * Handles the HTML display of calendar items.
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 void do_tasks_view(void) {      /* stub for non-libical builds */
36         wprintf("<CENTER><I>Tasks view not available</I></CENTER><br />\n");
37 }
38
39 #else   /* WEBCIT_WITH_CALENDAR_SERVICE */
40
41 /****************************************************************************/
42
43
44 void calendar_month_view_display_events(time_t thetime) {
45         int i;
46         time_t event_tt;
47         struct tm event_tm;
48         struct tm today_tm;
49         icalproperty *p;
50         struct icaltimetype t;
51         int month, day, year;
52         int all_day_event = 0;
53
54         if (WC->num_cal == 0) {
55                 wprintf("<br /><br /><br />\n");
56                 return;
57         }
58
59         localtime_r(&thetime, &today_tm);
60         month = today_tm.tm_mon + 1;
61         day = today_tm.tm_mday;
62         year = today_tm.tm_year + 1900;
63
64         for (i=0; i<(WC->num_cal); ++i) {
65                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
66                                                 ICAL_DTSTART_PROPERTY);
67                 if (p != NULL) {
68                         t = icalproperty_get_dtstart(p);
69                         event_tt = icaltime_as_timet(t);
70
71                         if (t.is_date) all_day_event = 1;
72                         else all_day_event = 0;
73
74                         if (all_day_event) {
75                                 gmtime_r(&event_tt, &event_tm);
76                         }
77                         else {
78                                 localtime_r(&event_tt, &event_tm);
79                         }
80
81                         if ((event_tm.tm_year == today_tm.tm_year)
82                            && (event_tm.tm_mon == today_tm.tm_mon)
83                            && (event_tm.tm_mday == today_tm.tm_mday)) {
84
85                                 p = icalcomponent_get_first_property(
86                                                         WC->disp_cal[i].cal,
87                                                         ICAL_SUMMARY_PROPERTY);
88                                 if (p != NULL) {
89
90                                         if (all_day_event) {
91                                                 wprintf("<TABLE border=0 cellpadding=2><TR>"
92                                                         "<TD BGCOLOR=\"#CCCCDD\">"
93                                                 );
94                                         }
95
96                                         wprintf("<FONT SIZE=-1>"
97                                                 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=%s&year=%s&month=%s&day=%s\">",
98                                                 WC->disp_cal[i].cal_msgnum,
99                                                 bstr("calview"),
100                                                 bstr("year"),
101                                                 bstr("month"),
102                                                 bstr("day")
103                                         );
104                                         escputs((char *)
105                                                 icalproperty_get_comment(p));
106                                         wprintf("</A></FONT><br />\n");
107
108                                         if (all_day_event) {
109                                                 wprintf("</TD></TR></TABLE>");
110                                         }
111
112                                 }
113
114                         }
115
116
117                 }
118         }
119 }
120
121
122
123 void calendar_month_view(int year, int month, int day) {
124         struct tm starting_tm;
125         struct tm tm;
126         time_t thetime;
127         int i;
128         time_t previous_month;
129         time_t next_month;
130
131         /* Determine what day to start.
132          * First, back up to the 1st of the month...
133          */
134         memset(&starting_tm, 0, sizeof(struct tm));
135         starting_tm.tm_year = year - 1900;
136         starting_tm.tm_mon = month - 1;
137         starting_tm.tm_mday = day;
138         thetime = mktime(&starting_tm);
139
140         memcpy(&tm, &starting_tm, sizeof(struct tm));
141         while (tm.tm_mday != 1) {
142                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
143                 localtime_r(&thetime, &tm);
144         }
145
146         /* Determine previous and next months ... for links */
147         previous_month = thetime - (time_t)864000L;     /* back 10 days */
148         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
149
150         /* Now back up until we're on a Sunday */
151         localtime_r(&thetime, &tm);
152         while (tm.tm_wday != 0) {
153                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
154                 localtime_r(&thetime, &tm);
155         }
156
157         /* Outer table (to get the background color) */
158         wprintf("<div id=\"fix_scrollbar_bug\">"
159                 "<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
160                 "bgcolor=#204B78><TR><TD>\n");
161
162         wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0><tr>\n");
163
164         wprintf("<TD ALIGN=CENTER>");
165
166         localtime_r(&previous_month, &tm);
167         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
168                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
169         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/prevdate_32x.gif\" BORDER=0></A>\n");
170
171         wprintf("&nbsp;&nbsp;"
172                 "<FONT SIZE=+1 COLOR=\"#FFFFFF\">"
173                 "%s %d"
174                 "</FONT>"
175                 "&nbsp;&nbsp;", months[month-1], year);
176
177         localtime_r(&next_month, &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/nextdate_32x.gif\" BORDER=0></A>\n");
181
182         wprintf("</TD></TR></TABLE>\n");
183
184         /* Inner table (the real one) */
185         wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
186                 "bgcolor=#204B78><TR>");
187         for (i=0; i<7; ++i) {
188                 wprintf("<TD ALIGN=CENTER WIDTH=14%%>"
189                         "<FONT COLOR=\"#FFFFFF\">%s</FONT></TH>", days[i]);
190         }
191         wprintf("</TR>\n");
192
193         /* Now do 35 days */
194         for (i = 0; i < 35; ++i) {
195                 localtime_r(&thetime, &tm);
196
197                 /* Before displaying Sunday, start a new row */
198                 if ((i % 7) == 0) {
199                         wprintf("<TR>");
200                 }
201
202                 wprintf("<TD BGCOLOR=\"#%s\" WIDTH=14%% HEIGHT=60 align=left VALIGN=TOP><B>",
203                         ((tm.tm_mon != month-1) ? "DDDDDD" :
204                         ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
205                         "FFFFFF"))
206                 );
207                 if ((i==0) || (tm.tm_mday == 1)) {
208                         wprintf("%s ", months[tm.tm_mon]);
209                 }
210                 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
211                         "%d</A></B><br />",
212                         tm.tm_year + 1900,
213                         tm.tm_mon + 1,
214                         tm.tm_mday,
215                         tm.tm_mday);
216
217                 /* put the data here, stupid */
218                 calendar_month_view_display_events(thetime);
219
220                 wprintf("</TD>");
221
222                 /* After displaying Saturday, end the row */
223                 if ((i % 7) == 6) {
224                         wprintf("</TR>\n");
225                 }
226
227                 thetime += (time_t)86400;               /* ahead 24 hours */
228         }
229
230         wprintf("</TABLE>"                      /* end of inner table */
231                 "</TD></TR></TABLE>"            /* end of outer table */
232                 "</div>\n");
233 }
234
235
236 void calendar_week_view(int year, int month, int day) {
237         wprintf("<CENTER><I>week view FIXME</I></CENTER><br />\n");
238 }
239
240
241 /*
242  * Display events for a particular hour of a particular day.
243  * (Specify hour < 0 to show "all day" events)
244  */
245 void calendar_day_view_display_events(int year, int month,
246                                         int day, int hour) {
247         int i;
248         icalproperty *p;
249         struct icaltimetype t;
250         time_t event_tt;
251         struct tm *event_tm;
252         int all_day_event = 0;
253
254         if (WC->num_cal == 0) {
255                 // FIXME wprintf("<br /><br /><br />\n");
256                 return;
257         }
258
259         for (i=0; i<(WC->num_cal); ++i) {
260                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
261                                                 ICAL_DTSTART_PROPERTY);
262                 if (p != NULL) {
263                         t = icalproperty_get_dtstart(p);
264                         event_tt = icaltime_as_timet(t);
265                         if (t.is_date) all_day_event = 1;
266
267                         if (all_day_event) {
268                                 event_tm = gmtime(&event_tt);
269                         }
270                         else {
271                                 event_tm = localtime(&event_tt);
272                         }
273
274                         if ((event_tm->tm_year == (year-1900))
275                            && (event_tm->tm_mon == (month-1))
276                            && (event_tm->tm_mday == day)
277                            && ( ((event_tm->tm_hour == hour)&&(!t.is_date)) || ((hour<0)&&(t.is_date)) )
278                            ) {
279
280
281                                 p = icalcomponent_get_first_property(
282                                                         WC->disp_cal[i].cal,
283                                                         ICAL_SUMMARY_PROPERTY);
284                                 if (p != NULL) {
285
286                                         if (all_day_event) {
287                                                 wprintf("<TABLE border=1 cellpadding=2><TR>"
288                                                         "<TD BGCOLOR=\"#CCCCCC\">"
289                                                 );
290                                         }
291
292                                         wprintf("<FONT SIZE=-1>"
293                                                 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
294                                                 WC->disp_cal[i].cal_msgnum,
295                                                 year, month, day
296                                         );
297                                         escputs((char *)
298                                                 icalproperty_get_comment(p));
299                                         wprintf("</A></FONT><br />\n");
300
301                                         if (all_day_event) {
302                                                 wprintf("</TD></TR></TABLE>");
303                                         }
304                                 }
305
306                         }
307
308
309                 }
310         }
311 }
312
313
314
315 void calendar_day_view(int year, int month, int day) {
316         int hour;
317         struct icaltimetype today, yesterday, tomorrow;
318         char calhourformat[16];
319
320         get_preference("calhourformat", calhourformat, sizeof calhourformat);
321
322         /* Figure out the dates for "yesterday" and "tomorrow" links */
323
324         memset(&today, 0, sizeof(struct icaltimetype));
325         today.year = year;
326         today.month = month;
327         today.day = day;
328         today.is_date = 1;
329
330         memcpy(&yesterday, &today, sizeof(struct icaltimetype));
331         --yesterday.day;
332         yesterday = icaltime_normalize(yesterday);
333
334         memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
335         ++tomorrow.day;
336         tomorrow = icaltime_normalize(tomorrow);
337
338
339         /* Outer table (to get the background color) */
340         wprintf("<div id=\"fix_scrollbar_bug\">"
341                 "<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
342                 "bgcolor=#204B78><TR><TD>\n");
343
344         /* Inner table (the real one) */
345         wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
346                 "bgcolor=#204B78><TR>\n");
347
348         /* Innermost table (contains hours etc.) */
349         wprintf("<TD WIDTH=80%%>"
350                 "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
351                 "bgcolor=#204B78>\n");
352
353         /* Display events before 8:00 (hour=-1 is all-day events) */
354         wprintf("<TR>"
355                 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
356                 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
357         for (hour = (-1); hour <= 7; ++hour) {
358                 calendar_day_view_display_events(year, month, day, hour);
359         }
360         wprintf("</TD></TR>\n");
361
362         /* Now the middle of the day... */      
363         for (hour = 8; hour <= 17; ++hour) {    /* could do HEIGHT=xx */
364                 wprintf("<TR HEIGHT=30><TD BGCOLOR=\"#CCCCDD\" ALIGN=MIDDLE "
365                         "VALIGN=MIDDLE WIDTH=10%%>");
366                 wprintf("<A HREF=\"/display_edit_event?msgnum=0"
367                         "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
368                         year, month, day, hour
369                 );
370
371                 if (!strcasecmp(calhourformat, "24")) {
372                         wprintf("%2d:00</A> ", hour);
373                 }
374                 else {
375                         wprintf("%d:00%s</A> ",
376                                 (hour <= 12 ? hour : hour-12),
377                                 (hour < 12 ? "am" : "pm")
378                         );
379                 }
380
381                 wprintf("</TD><TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
382
383                 /* put the data here, stupid */
384                 calendar_day_view_display_events(year, month, day, hour);
385
386                 wprintf("</TD></TR>\n");
387         }
388
389         /* Display events after 5:00... */
390         wprintf("<TR>"
391                 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
392                 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
393         for (hour = 18; hour <= 23; ++hour) {
394                 calendar_day_view_display_events(year, month, day, hour);
395         }
396         wprintf("</TD></TR>\n");
397
398
399         wprintf("</TABLE>"                      /* end of innermost table */
400                 "</TD>"
401         );
402
403         wprintf("<TD WIDTH=20%% VALIGN=top>");  /* begin stuff-on-the-right */
404
405
406         /* Begin todays-date-with-left-and-right-arrows */
407         wprintf("<TABLE BORDER=0 WIDTH=100%% "
408                 "CELLSPACING=0 CELLPADDING=0 BGCOLOR=\"#FFFFFF\">\n");
409         wprintf("<TR>");
410
411         /* Left arrow */        
412         wprintf("<TD ALIGN=CENTER>");
413         wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
414                 yesterday.year, yesterday.month, yesterday.day);
415         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/prevdate_32x.gif\" BORDER=0></A>");
416         wprintf("</TD>");
417
418         /* Today's date */
419         wprintf("<TD ALIGN=CENTER>");
420         wprintf("<FONT SIZE=+2>%s</FONT><br />"
421                 "<FONT SIZE=+3>%d</FONT><br />"
422                 "<FONT SIZE=+2>%d</FONT><br />",
423                 months[month-1], day, year);
424         wprintf("</TD>");
425
426         /* Right arrow */
427         wprintf("<TD ALIGN=CENTER>");
428         wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
429                 tomorrow.year, tomorrow.month, tomorrow.day);
430         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/nextdate_32x.gif\""
431                 " BORDER=0></A>\n");
432         wprintf("</TD>");
433
434         wprintf("</TR></TABLE>\n");
435         /* End todays-date-with-left-and-right-arrows */
436
437         /* In the future we might want to put a month-o-matic here */
438
439         wprintf("</FONT></CENTER>\n");
440
441         wprintf("</TD>");                       /* end stuff-on-the-right */
442
443
444
445         wprintf("</TR></TABLE>"                 /* end of inner table */
446                 "</TD></TR></TABLE></div>"      /* end of outer table */
447         );
448
449
450
451 }
452
453 /*
454  * Display today's events.
455  */
456 void calendar_summary_view(void) {
457         int i;
458         icalproperty *p;
459         struct icaltimetype t;
460         time_t event_tt;
461         struct tm event_tm;
462         struct tm today_tm;
463         time_t now;
464         int all_day_event = 0;
465         char timestring[SIZ];
466
467         if (WC->num_cal == 0) {
468                 return;
469         }
470
471         now = time(NULL);
472         localtime_r(&now, &today_tm);
473
474         for (i=0; i<(WC->num_cal); ++i) {
475                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
476                                                 ICAL_DTSTART_PROPERTY);
477                 if (p != NULL) {
478                         t = icalproperty_get_dtstart(p);
479                         event_tt = icaltime_as_timet(t);
480                         if (t.is_date) all_day_event = 1;
481                         fmt_time(timestring, event_tt);
482
483                         if (all_day_event) {
484                                 gmtime_r(&event_tt, &event_tm);
485                         }
486                         else {
487                                 localtime_r(&event_tt, &event_tm);
488                         }
489
490                         if ( (event_tm.tm_year == today_tm.tm_year)
491                            && (event_tm.tm_mon == today_tm.tm_mon)
492                            && (event_tm.tm_mday == today_tm.tm_mday)
493                            ) {
494
495
496                                 p = icalcomponent_get_first_property(
497                                                         WC->disp_cal[i].cal,
498                                                         ICAL_SUMMARY_PROPERTY);
499                                 if (p != NULL) {
500                                         escputs((char *)
501                                                 icalproperty_get_comment(p));
502                                         wprintf(" (%s)<br />\n", timestring);
503                                 }
504                         }
505                 }
506         }
507         free_calendar_buffer();
508 }
509
510
511
512 void free_calendar_buffer(void) {
513         int i;
514         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
515                 icalcomponent_free(WC->disp_cal[i].cal);
516         }
517         WC->num_cal = 0;
518         free(WC->disp_cal);
519         WC->disp_cal = NULL;
520 }
521
522
523
524
525 void do_calendar_view(void) {
526         time_t now;
527         struct tm tm;
528         int year, month, day;
529         char calview[SIZ];
530
531         /* In case no date was specified, go with today */
532         now = time(NULL);
533         localtime_r(&now, &tm);
534         year = tm.tm_year + 1900;
535         month = tm.tm_mon + 1;
536         day = tm.tm_mday;
537
538         /* Now see if a date was specified */
539         if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
540         if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
541         if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
542
543         /* How would you like that cooked? */
544         if (strlen(bstr("calview")) > 0) {
545                 strcpy(calview, bstr("calview"));
546         }
547         else {
548                 strcpy(calview, "month");
549         }
550
551         /* Display the selected view */
552         if (!strcasecmp(calview, "day")) {
553                 calendar_day_view(year, month, day);
554         }
555         else if (!strcasecmp(calview, "week")) {
556                 calendar_week_view(year, month, day);
557         }
558         else {
559                 calendar_month_view(year, month, day);
560         }
561
562         /* Free the calendar stuff */
563         free_calendar_buffer();
564
565 }
566
567
568 /*
569  * Helper function for do_tasks_view().  Returns the date/time due.
570  */
571 time_t get_task_due_date(icalcomponent *vtodo) {
572         icalproperty *p;
573
574         if (vtodo == NULL) {
575                 return(0L);
576         }
577
578         /* If we're looking at a fully encapsulated VCALENDAR
579          * rather than a VTODO component, recurse into the data
580          * structure until we get a VTODO.
581          */
582         if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
583                 return get_task_due_date(
584                         icalcomponent_get_first_component(
585                                 vtodo, ICAL_VTODO_COMPONENT
586                         )
587                 );
588         }
589
590         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
591         if (p != NULL) {
592                 return(icaltime_as_timet(icalproperty_get_due(p)));
593         }
594         else {
595                 return(0L);
596         }
597 }
598
599
600 /*
601  * Compare the due dates of two tasks (this is for sorting)
602  */
603 int task_due_cmp(const void *task1, const void *task2) {
604         time_t t1;
605         time_t t2;
606
607         t1 =  get_task_due_date(((struct disp_cal *)task1)->cal);
608         t2 =  get_task_due_date(((struct disp_cal *)task2)->cal);
609
610         if (t1 < t2) return(-1);
611         if (t1 > t2) return(1);
612         return(0);
613 }
614
615
616
617
618
619 void do_tasks_view(void) {
620         int i;
621         time_t due;
622         int bg = 0;
623         char buf[SIZ];
624         icalproperty *p;
625
626         wprintf("<div id=\"fix_scrollbar_bug\">"
627                 "<table border=0 cellspacing=0 width=100%% bgcolor=\"#FFFFFF\">\n<tr>\n"
628                 "<TH>Name of task</TH>\n"
629                 "<TH>Date due</TH></TR>\n"
630         );
631
632         /* Sort them if necessary */
633         if (WC->num_cal > 1) {
634                 qsort(WC->disp_cal,
635                         WC->num_cal,
636                         sizeof(struct disp_cal),
637                         task_due_cmp
638                 );
639         }
640
641         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
642
643                 bg = 1 - bg;
644                 wprintf("<TR BGCOLOR=\"#%s\"><TD>",
645                         (bg ? "DDDDDD" : "FFFFFF")
646                 );
647
648                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
649                                                         ICAL_SUMMARY_PROPERTY);
650                 wprintf("<A HREF=\"/display_edit_task?msgnum=%ld&taskrm=",
651                         WC->disp_cal[i].cal_msgnum );
652                 urlescputs(WC->wc_roomname);
653                 wprintf("\">");
654                 wprintf("<IMG ALIGN=MIDDLE "
655                         "SRC=\"/static/taskmanag_16x.gif\" BORDER=0>&nbsp;");
656                 if (p != NULL) {
657                         escputs((char *)icalproperty_get_comment(p));
658                 }
659                 wprintf("</A>\n");
660                 wprintf("</TD>\n");
661
662                 due = get_task_due_date(WC->disp_cal[i].cal);
663                 fmt_date(buf, due, 0);
664                 wprintf("<TD><FONT");
665                 if (due < time(NULL)) {
666                         wprintf(" COLOR=\"#FF0000\"");
667                 }
668                 wprintf(">%s</FONT></TD></TR>\n", buf);
669         }
670
671         wprintf("</table></div>\n");
672
673         /* Free the list */
674         free_calendar_buffer();
675
676 }
677
678 #endif  /* WEBCIT_WITH_CALENDAR_SERVICE */