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