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