* The previous <div style=\"margin-right:1px\"> has been replaced by a
[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/back.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/forward.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
319
320         /* Figure out the dates for "yesterday" and "tomorrow" links */
321
322         memset(&today, 0, sizeof(struct icaltimetype));
323         today.year = year;
324         today.month = month;
325         today.day = day;
326         today.is_date = 1;
327
328         memcpy(&yesterday, &today, sizeof(struct icaltimetype));
329         --yesterday.day;
330         yesterday = icaltime_normalize(yesterday);
331
332         memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
333         ++tomorrow.day;
334         tomorrow = icaltime_normalize(tomorrow);
335
336
337         /* Outer table (to get the background color) */
338         wprintf("<div id=\"fix_scrollbar_bug\">"
339                 "<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
340                 "bgcolor=#204B78><TR><TD>\n");
341
342         /* Inner table (the real one) */
343         wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
344                 "bgcolor=#204B78><TR>\n");
345
346         /* Innermost table (contains hours etc.) */
347         wprintf("<TD WIDTH=80%%>"
348                 "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
349                 "bgcolor=#204B78>\n");
350
351         /* Display events before 8:00 (hour=-1 is all-day events) */
352         wprintf("<TR>"
353                 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
354                 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
355         for (hour = (-1); hour <= 7; ++hour) {
356                 calendar_day_view_display_events(year, month, day, hour);
357         }
358         wprintf("</TD></TR>\n");
359
360         /* Now the middle of the day... */      
361         for (hour = 8; hour <= 17; ++hour) {    /* could do HEIGHT=xx */
362                 wprintf("<TR HEIGHT=30><TD BGCOLOR=\"#CCCCDD\" ALIGN=MIDDLE "
363                         "VALIGN=MIDDLE WIDTH=10%%>");
364                 wprintf("<A HREF=\"/display_edit_event?msgnum=0"
365                         "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
366                         year, month, day, hour
367                 );
368                 wprintf("%d:00%s</A> ",
369                         (hour <= 12 ? hour : hour-12),
370                         (hour < 12 ? "am" : "pm")
371                 );
372                 wprintf("</TD><TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
373
374                 /* put the data here, stupid */
375                 calendar_day_view_display_events(year, month, day, hour);
376
377                 wprintf("</TD></TR>\n");
378         }
379
380         /* Display events after 5:00... */
381         wprintf("<TR>"
382                 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
383                 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
384         for (hour = 18; hour <= 23; ++hour) {
385                 calendar_day_view_display_events(year, month, day, hour);
386         }
387         wprintf("</TD></TR>\n");
388
389
390         wprintf("</TABLE>"                      /* end of innermost table */
391                 "</TD>"
392         );
393
394         wprintf("<TD WIDTH=20%% VALIGN=top>");  /* begin stuff-on-the-right */
395
396
397         /* Begin todays-date-with-left-and-right-arrows */
398         wprintf("<TABLE BORDER=0 WIDTH=100%% "
399                 "CELLSPACING=0 CELLPADDING=0 BGCOLOR=\"#FFFFFF\">\n");
400         wprintf("<TR>");
401
402         /* Left arrow */        
403         wprintf("<TD ALIGN=CENTER>");
404         wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
405                 yesterday.year, yesterday.month, yesterday.day);
406         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>");
407         wprintf("</TD>");
408
409         /* Today's date */
410         wprintf("<TD ALIGN=CENTER>");
411         wprintf("<FONT SIZE=+2>%s</FONT><br />"
412                 "<FONT SIZE=+3>%d</FONT><br />"
413                 "<FONT SIZE=+2>%d</FONT><br />",
414                 months[month-1], day, year);
415         wprintf("</TD>");
416
417         /* Right arrow */
418         wprintf("<TD ALIGN=CENTER>");
419         wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
420                 tomorrow.year, tomorrow.month, tomorrow.day);
421         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\""
422                 " BORDER=0></A>\n");
423         wprintf("</TD>");
424
425         wprintf("</TR></TABLE>\n");
426         /* End todays-date-with-left-and-right-arrows */
427
428         /* In the future we might want to put a month-o-matic here */
429
430         wprintf("</FONT></CENTER>\n");
431
432         wprintf("</TD>");                       /* end stuff-on-the-right */
433
434
435
436         wprintf("</TR></TABLE>"                 /* end of inner table */
437                 "</TD></TR></TABLE></div>"      /* end of outer table */
438         );
439
440
441
442 }
443
444 /*
445  * Display today's events.
446  */
447 void calendar_summary_view(void) {
448         int i;
449         icalproperty *p;
450         struct icaltimetype t;
451         time_t event_tt;
452         struct tm event_tm;
453         struct tm today_tm;
454         time_t now;
455         int all_day_event = 0;
456         char timestring[SIZ];
457
458         if (WC->num_cal == 0) {
459                 return;
460         }
461
462         now = time(NULL);
463         localtime_r(&now, &today_tm);
464
465         for (i=0; i<(WC->num_cal); ++i) {
466                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
467                                                 ICAL_DTSTART_PROPERTY);
468                 if (p != NULL) {
469                         t = icalproperty_get_dtstart(p);
470                         event_tt = icaltime_as_timet(t);
471                         if (t.is_date) all_day_event = 1;
472                         fmt_time(timestring, event_tt);
473
474                         if (all_day_event) {
475                                 gmtime_r(&event_tt, &event_tm);
476                         }
477                         else {
478                                 localtime_r(&event_tt, &event_tm);
479                         }
480
481                         if ( (event_tm.tm_year == today_tm.tm_year)
482                            && (event_tm.tm_mon == today_tm.tm_mon)
483                            && (event_tm.tm_mday == today_tm.tm_mday)
484                            ) {
485
486
487                                 p = icalcomponent_get_first_property(
488                                                         WC->disp_cal[i].cal,
489                                                         ICAL_SUMMARY_PROPERTY);
490                                 if (p != NULL) {
491                                         escputs((char *)
492                                                 icalproperty_get_comment(p));
493                                         wprintf(" (%s)<br />\n", timestring);
494                                 }
495                         }
496                 }
497         }
498         free_calendar_buffer();
499 }
500
501
502
503 void free_calendar_buffer(void) {
504         int i;
505         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
506                 icalcomponent_free(WC->disp_cal[i].cal);
507         }
508         WC->num_cal = 0;
509         free(WC->disp_cal);
510         WC->disp_cal = NULL;
511 }
512
513
514
515
516 void do_calendar_view(void) {
517         time_t now;
518         struct tm tm;
519         int year, month, day;
520         char calview[SIZ];
521
522         /* In case no date was specified, go with today */
523         now = time(NULL);
524         localtime_r(&now, &tm);
525         year = tm.tm_year + 1900;
526         month = tm.tm_mon + 1;
527         day = tm.tm_mday;
528
529         /* Now see if a date was specified */
530         if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
531         if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
532         if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
533
534         /* How would you like that cooked? */
535         if (strlen(bstr("calview")) > 0) {
536                 strcpy(calview, bstr("calview"));
537         }
538         else {
539                 strcpy(calview, "month");
540         }
541
542         /* Display the selected view */
543         if (!strcasecmp(calview, "day")) {
544                 calendar_day_view(year, month, day);
545         }
546         else if (!strcasecmp(calview, "week")) {
547                 calendar_week_view(year, month, day);
548         }
549         else {
550                 calendar_month_view(year, month, day);
551         }
552
553         /* Free the calendar stuff */
554         free_calendar_buffer();
555
556 }
557
558
559 /*
560  * Helper function for do_tasks_view().  Returns the date/time due.
561  */
562 time_t get_task_due_date(icalcomponent *vtodo) {
563         icalproperty *p;
564
565         if (vtodo == NULL) {
566                 return(0L);
567         }
568
569         /* If we're looking at a fully encapsulated VCALENDAR
570          * rather than a VTODO component, recurse into the data
571          * structure until we get a VTODO.
572          */
573         if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
574                 return get_task_due_date(
575                         icalcomponent_get_first_component(
576                                 vtodo, ICAL_VTODO_COMPONENT
577                         )
578                 );
579         }
580
581         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
582         if (p != NULL) {
583                 return(icaltime_as_timet(icalproperty_get_due(p)));
584         }
585         else {
586                 return(0L);
587         }
588 }
589
590
591 /*
592  * Compare the due dates of two tasks (this is for sorting)
593  */
594 int task_due_cmp(const void *task1, const void *task2) {
595         time_t t1;
596         time_t t2;
597
598         t1 =  get_task_due_date(((struct disp_cal *)task1)->cal);
599         t2 =  get_task_due_date(((struct disp_cal *)task2)->cal);
600
601         if (t1 < t2) return(-1);
602         if (t1 > t2) return(1);
603         return(0);
604 }
605
606
607
608
609
610 void do_tasks_view(void) {
611         int i;
612         time_t due;
613         int bg = 0;
614         char buf[SIZ];
615         icalproperty *p;
616
617         wprintf("<div id=\"fix_scrollbar_bug\">"
618                 "<table border=0 cellspacing=0 width=100%% bgcolor=\"#FFFFFF\">\n<tr>\n"
619                 "<TH>Name of task</TH>\n"
620                 "<TH>Date due</TH></TR>\n"
621         );
622
623         /* Sort them if necessary */
624         if (WC->num_cal > 1) {
625                 qsort(WC->disp_cal,
626                         WC->num_cal,
627                         sizeof(struct disp_cal),
628                         task_due_cmp
629                 );
630         }
631
632         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
633
634                 bg = 1 - bg;
635                 wprintf("<TR BGCOLOR=\"#%s\"><TD>",
636                         (bg ? "DDDDDD" : "FFFFFF")
637                 );
638
639                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
640                                                         ICAL_SUMMARY_PROPERTY);
641                 wprintf("<A HREF=\"/display_edit_task?msgnum=%ld&taskrm=",
642                         WC->disp_cal[i].cal_msgnum );
643                 urlescputs(WC->wc_roomname);
644                 wprintf("\">");
645                 if (p != NULL) {
646                         escputs((char *)icalproperty_get_comment(p));
647                 }
648                 wprintf("</A>\n");
649                 wprintf("</TD>\n");
650
651                 due = get_task_due_date(WC->disp_cal[i].cal);
652                 fmt_date(buf, due);
653                 wprintf("<TD><FONT");
654                 if (due < time(NULL)) {
655                         wprintf(" COLOR=\"#FF0000\"");
656                 }
657                 wprintf(">%s</FONT></TD></TR>\n", buf);
658         }
659
660         wprintf("</table></div>\n");
661
662         /* Free the list */
663         free_calendar_buffer();
664
665 }
666
667 #endif  /* WEBCIT_WITH_CALENDAR_SERVICE */