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