5276039f12e501d1d483d1bc5a8c5a6cb340ede1
[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         int daystart = 8;
310         int dayend = 17;
311         char daystart_str[16], dayend_str[16];
312
313         get_preference("calhourformat", calhourformat, sizeof calhourformat);
314         get_preference("daystart", daystart_str, sizeof daystart_str);
315         if (strlen(daystart_str) > 0) daystart = atoi(daystart_str);
316         get_preference("dayend", dayend_str, sizeof dayend_str);
317         if (strlen(dayend_str) > 0) dayend = atoi(dayend_str);
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 <= (daystart-1); ++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 = daystart; hour <= dayend; ++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
369                 if (!strcasecmp(calhourformat, "24")) {
370                         wprintf("%2d:00</A> ", hour);
371                 }
372                 else {
373                         wprintf("%d:00%s</A> ",
374                                 (hour <= 12 ? hour : hour-12),
375                                 (hour < 12 ? "am" : "pm")
376                         );
377                 }
378
379                 wprintf("</TD><TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
380
381                 /* put the data here, stupid */
382                 calendar_day_view_display_events(year, month, day, hour);
383
384                 wprintf("</TD></TR>\n");
385         }
386
387         /* Display events after 5:00... */
388         wprintf("<TR>"
389                 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
390                 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
391         for (hour = (dayend+1); hour <= 23; ++hour) {
392                 calendar_day_view_display_events(year, month, day, hour);
393         }
394         wprintf("</TD></TR>\n");
395
396
397         wprintf("</TABLE>"                      /* end of innermost table */
398                 "</TD>"
399         );
400
401         wprintf("<TD WIDTH=20%% VALIGN=top>");  /* begin stuff-on-the-right */
402
403
404         /* Begin todays-date-with-left-and-right-arrows */
405         wprintf("<TABLE BORDER=0 WIDTH=100%% "
406                 "CELLSPACING=0 CELLPADDING=0 BGCOLOR=\"#FFFFFF\">\n");
407         wprintf("<TR>");
408
409         /* Left arrow */        
410         wprintf("<TD ALIGN=CENTER>");
411         wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
412                 yesterday.year, yesterday.month, yesterday.day);
413         wprintf("<IMG ALIGN=MIDDLE src=\"static/prevdate_32x.gif\" BORDER=0></A>");
414         wprintf("</TD>");
415
416         /* Today's date */
417         wprintf("<TD ALIGN=CENTER>");
418         wprintf("<FONT SIZE=+2>%s</FONT><br />"
419                 "<FONT SIZE=+3>%d</FONT><br />"
420                 "<FONT SIZE=+2>%d</FONT><br />",
421                 months[month-1], day, year);
422         wprintf("</TD>");
423
424         /* Right arrow */
425         wprintf("<TD ALIGN=CENTER>");
426         wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
427                 tomorrow.year, tomorrow.month, tomorrow.day);
428         wprintf("<IMG ALIGN=MIDDLE src=\"static/nextdate_32x.gif\""
429                 " BORDER=0></A>\n");
430         wprintf("</TD>");
431
432         wprintf("</TR></TABLE>\n");
433         /* End todays-date-with-left-and-right-arrows */
434
435         /* In the future we might want to put a month-o-matic here */
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></div>"      /* 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) {
479                                 all_day_event = 1;
480                         }
481                         else {
482                                 all_day_event = 0;
483                         }
484                         fmt_time(timestring, event_tt);
485
486                         if (all_day_event) {
487                                 gmtime_r(&event_tt, &event_tm);
488                         }
489                         else {
490                                 localtime_r(&event_tt, &event_tm);
491                         }
492
493                         if ( (event_tm.tm_year == today_tm.tm_year)
494                            && (event_tm.tm_mon == today_tm.tm_mon)
495                            && (event_tm.tm_mday == today_tm.tm_mday)
496                            ) {
497
498
499                                 p = icalcomponent_get_first_property(
500                                                         WC->disp_cal[i].cal,
501                                                         ICAL_SUMMARY_PROPERTY);
502                                 if (p != NULL) {
503                                         escputs((char *)
504                                                 icalproperty_get_comment(p));
505                                         wprintf(" (%s)<br />\n", timestring);
506                                 }
507                         }
508                 }
509         }
510         free_calendar_buffer();
511 }
512
513
514
515 void free_calendar_buffer(void) {
516         int i;
517         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
518                 icalcomponent_free(WC->disp_cal[i].cal);
519         }
520         WC->num_cal = 0;
521         free(WC->disp_cal);
522         WC->disp_cal = NULL;
523 }
524
525
526
527
528 void do_calendar_view(void) {
529         time_t now;
530         struct tm tm;
531         int year, month, day;
532         char calview[SIZ];
533
534         /* In case no date was specified, go with today */
535         now = time(NULL);
536         localtime_r(&now, &tm);
537         year = tm.tm_year + 1900;
538         month = tm.tm_mon + 1;
539         day = tm.tm_mday;
540
541         /* Now see if a date was specified */
542         if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
543         if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
544         if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
545
546         /* How would you like that cooked? */
547         if (strlen(bstr("calview")) > 0) {
548                 strcpy(calview, bstr("calview"));
549         }
550         else {
551                 strcpy(calview, "month");
552         }
553
554         /* Display the selected view */
555         if (!strcasecmp(calview, "day")) {
556                 calendar_day_view(year, month, day);
557         }
558         else if (!strcasecmp(calview, "week")) {
559                 calendar_week_view(year, month, day);
560         }
561         else {
562                 calendar_month_view(year, month, day);
563         }
564
565         /* Free the calendar stuff */
566         free_calendar_buffer();
567
568 }
569
570
571 /*
572  * Helper function for do_tasks_view().  Returns the date/time due.
573  */
574 time_t get_task_due_date(icalcomponent *vtodo) {
575         icalproperty *p;
576
577         if (vtodo == NULL) {
578                 return(0L);
579         }
580
581         /* If we're looking at a fully encapsulated VCALENDAR
582          * rather than a VTODO component, recurse into the data
583          * structure until we get a VTODO.
584          */
585         if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
586                 return get_task_due_date(
587                         icalcomponent_get_first_component(
588                                 vtodo, ICAL_VTODO_COMPONENT
589                         )
590                 );
591         }
592
593         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
594         if (p != NULL) {
595                 return(icaltime_as_timet(icalproperty_get_due(p)));
596         }
597         else {
598                 return(0L);
599         }
600 }
601
602
603 /*
604  * Compare the due dates of two tasks (this is for sorting)
605  */
606 int task_due_cmp(const void *task1, const void *task2) {
607         time_t t1;
608         time_t t2;
609
610         t1 =  get_task_due_date(((struct disp_cal *)task1)->cal);
611         t2 =  get_task_due_date(((struct disp_cal *)task2)->cal);
612
613         if (t1 < t2) return(-1);
614         if (t1 > t2) return(1);
615         return(0);
616 }
617
618
619
620
621
622 void do_tasks_view(void) {
623         int i;
624         time_t due;
625         int bg = 0;
626         char buf[SIZ];
627         icalproperty *p;
628
629         wprintf("<div id=\"fix_scrollbar_bug\">"
630                 "<table border=0 cellspacing=0 width=100%% bgcolor=\"#FFFFFF\">\n<tr>\n"
631                 "<TH>");
632         wprintf(_("Name of task"));
633         wprintf("</TH><TH>");
634         wprintf(_("Date due"));
635         wprintf("</TH></TR>\n"
636         );
637
638         /* Sort them if necessary */
639         if (WC->num_cal > 1) {
640                 qsort(WC->disp_cal,
641                         WC->num_cal,
642                         sizeof(struct disp_cal),
643                         task_due_cmp
644                 );
645         }
646
647         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
648
649                 bg = 1 - bg;
650                 wprintf("<TR BGCOLOR=\"#%s\"><TD>",
651                         (bg ? "DDDDDD" : "FFFFFF")
652                 );
653
654                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
655                                                         ICAL_SUMMARY_PROPERTY);
656                 wprintf("<a href=\"display_edit_task?msgnum=%ld&taskrm=",
657                         WC->disp_cal[i].cal_msgnum );
658                 urlescputs(WC->wc_roomname);
659                 wprintf("\">");
660                 wprintf("<IMG ALIGN=MIDDLE "
661                         "src=\"static/taskmanag_16x.gif\" BORDER=0>&nbsp;");
662                 if (p != NULL) {
663                         escputs((char *)icalproperty_get_comment(p));
664                 }
665                 wprintf("</A>\n");
666                 wprintf("</TD>\n");
667
668                 due = get_task_due_date(WC->disp_cal[i].cal);
669                 fmt_date(buf, due, 0);
670                 wprintf("<TD><FONT");
671                 if (due < time(NULL)) {
672                         wprintf(" COLOR=\"#FF0000\"");
673                 }
674                 wprintf(">%s</FONT></TD></TR>\n", buf);
675         }
676
677         wprintf("</table></div>\n");
678
679         /* Free the list */
680         free_calendar_buffer();
681
682 }
683
684 #endif  /* WEBCIT_WITH_CALENDAR_SERVICE */