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