Removed monthname() and replaced with proper strftime() calls.
[citadel.git] / webcit / calendar_view.c
1 /*
2  * $Id$
3  */
4 /**
5  * \defgroup CalHtmlHandles Handles the HTML display of calendar items.
6  * \ingroup Calendaring
7  */
8 /*@{*/
9 #include "webcit.h"
10 #include "webserver.h"
11
12 #ifndef WEBCIT_WITH_CALENDAR_SERVICE
13
14 /**\brief stub for non-libical builds */
15 void do_calendar_view(void) {
16         wprintf("<CENTER><I>");
17         wprintf(_("The calendar view is not available."));
18         wprintf("</I></CENTER><br />\n");
19 }
20
21 /**\brief stub for non-libical builds */
22 void do_tasks_view(void) {      
23         wprintf("<CENTER><I>");
24         wprintf(_("The tasks view is not available."));
25         wprintf("</I></CENTER><br />\n");
26 }
27
28 #else   /* WEBCIT_WITH_CALENDAR_SERVICE */
29
30 /****************************************************************************/
31
32 /**
33  * \brief Display one day of a whole month view of a calendar
34  * \param thetime the month we want to see 
35  */
36 void calendar_month_view_display_events(time_t thetime) {
37         int i;
38         time_t event_tt;
39         struct tm event_tm;
40         struct tm today_tm;
41         icalproperty *p;
42         struct icaltimetype t;
43         int month, day, year;
44         int all_day_event = 0;
45
46         if (WC->num_cal == 0) {
47                 wprintf("<br /><br /><br />\n");
48                 return;
49         }
50
51         localtime_r(&thetime, &today_tm);
52         month = today_tm.tm_mon + 1;
53         day = today_tm.tm_mday;
54         year = today_tm.tm_year + 1900;
55
56         for (i=0; i<(WC->num_cal); ++i) {
57                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
58                                                 ICAL_DTSTART_PROPERTY);
59                 if (p != NULL) {
60                         t = icalproperty_get_dtstart(p);
61                         event_tt = icaltime_as_timet(t);
62
63                         if (t.is_date) all_day_event = 1;
64                         else all_day_event = 0;
65
66                         if (all_day_event) {
67                                 gmtime_r(&event_tt, &event_tm);
68                         }
69                         else {
70                                 localtime_r(&event_tt, &event_tm);
71                         }
72
73                         if ((event_tm.tm_year == today_tm.tm_year)
74                            && (event_tm.tm_mon == today_tm.tm_mon)
75                            && (event_tm.tm_mday == today_tm.tm_mday)) {
76
77                                 p = icalcomponent_get_first_property(
78                                                         WC->disp_cal[i].cal,
79                                                         ICAL_SUMMARY_PROPERTY);
80                                 if (p != NULL) {
81
82                                         if (all_day_event) {
83                                                 wprintf("<TABLE border=0 cellpadding=2><TR>"
84                                                         "<TD BGCOLOR=\"#CCCCDD\">"
85                                                 );
86                                         }
87
88                                         wprintf("<FONT SIZE=-1>"
89                                                 "<a href=\"display_edit_event?msgnum=%ld&calview=%s&year=%s&month=%s&day=%s\">",
90                                                 WC->disp_cal[i].cal_msgnum,
91                                                 bstr("calview"),
92                                                 bstr("year"),
93                                                 bstr("month"),
94                                                 bstr("day")
95                                         );
96                                         escputs((char *)
97                                                 icalproperty_get_comment(p));
98                                         wprintf("</A></FONT><br />\n");
99
100                                         if (all_day_event) {
101                                                 wprintf("</TD></TR></TABLE>");
102                                         }
103
104                                 }
105
106                         }
107
108
109                 }
110         }
111 }
112
113
114 /**
115  * \brief Display one day of a whole month view of a calendar
116  * \param thetime the month we want to see 
117  */
118 void calendar_month_view_brief_events(time_t thetime, const char *daycolor) {
119         int i;
120         time_t event_tt;
121         time_t event_tts;
122         time_t event_tte;
123         struct tm event_tms;
124         struct tm event_tme;
125         struct tm today_tm;
126         icalproperty *p;
127         icalproperty *e;
128         struct icaltimetype t;
129         int month, day, year;
130         int all_day_event = 0;
131         char calhourformat[16];
132         char *timeformat;
133
134         get_preference("calhourformat", calhourformat, sizeof calhourformat);
135         if (!strcasecmp(calhourformat, "24"))   timeformat="%k:%M";
136         else timeformat="%I:%M %p";
137
138         localtime_r(&thetime, &today_tm);
139         month = today_tm.tm_mon + 1;
140         day = today_tm.tm_mday;
141         year = today_tm.tm_year + 1900;
142
143         for (i=0; i<(WC->num_cal); ++i) {
144                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
145                                                 ICAL_DTSTART_PROPERTY);
146                 if (p != NULL) {
147                         t = icalproperty_get_dtstart(p);
148                         event_tt = icaltime_as_timet(t);
149                         event_tts=event_tt;
150                         if (t.is_date) all_day_event = 1;
151                         else all_day_event = 0;
152
153                         if (all_day_event) {
154                                 gmtime_r(&event_tts, &event_tms);
155                         }
156                         else {
157                                 localtime_r(&event_tts, &event_tms);
158                         }
159                         /** \todo epoch &! daymask */
160                         if ((event_tms.tm_year == today_tm.tm_year)
161                            && (event_tms.tm_mon == today_tm.tm_mon)
162                            && (event_tms.tm_mday == today_tm.tm_mday)) {
163                                 
164                                 
165                                 char sbuf[255];
166                                 char ebuf[255];
167
168                                 p = icalcomponent_get_first_property(
169                                                         WC->disp_cal[i].cal,
170                                                         ICAL_SUMMARY_PROPERTY);
171                                 e = icalcomponent_get_first_property(
172                                                         WC->disp_cal[i].cal, 
173                                                         ICAL_DTEND_PROPERTY);
174                                 if ((p != NULL) && (e != NULL)) {
175                                         time_t difftime;
176                                         int hours, minutes;
177                                         t = icalproperty_get_dtend(e);
178                                         event_tte = icaltime_as_timet(t);
179                                         localtime_r(&event_tte, &event_tme);
180                                         difftime=(event_tte-event_tts)/60;
181                                         hours=(int)(difftime / 60);
182                                         minutes=difftime % 60;
183                                         wprintf("<tr><td bgcolor='%s'>%i:%2i</td><td bgcolor='%s'>"
184                                                         "<font size=-1>"
185                                                         "<a href=\"display_edit_event?msgnum=%ld&calview=%s&year=%s&month=%s&day=%s\">",
186                                                         daycolor,
187                                                         hours, minutes,
188                                                         daycolor,
189                                                         WC->disp_cal[i].cal_msgnum,
190                                                         bstr("calview"),
191                                                         bstr("year"),
192                                                         bstr("month"),
193                                                         bstr("day")
194                                                         );
195
196                                         escputs((char *)
197                                                         icalproperty_get_comment(p));
198                                         /** \todo: allso ammitime format */
199                                         wc_strftime(&sbuf[0], sizeof(sbuf), timeformat, &event_tms);
200                                         wc_strftime(&ebuf[0], sizeof(sbuf), timeformat, &event_tme);
201
202                                         wprintf("</a></font></td>"
203                                                         "<td bgcolor='%s'>%s</td><td bgcolor='%s'>%s</td></tr>",
204                                                         daycolor,
205                                                         sbuf,
206                                                         daycolor,
207                                                         ebuf);
208                                         
209                                 }
210                                 
211                         }
212                         
213                         
214                 }
215         }
216 }
217
218
219 /**
220  * \brief view one month. pretty view
221  * \param year the year
222  * \param month the month
223  * \param day the actual day we want to see
224  */
225 void calendar_month_view(int year, int month, int day) {
226         struct tm starting_tm;
227         struct tm tm;
228         time_t thetime;
229         int i;
230         time_t previous_month;
231         time_t next_month;
232         time_t colheader_time;
233         struct tm colheader_tm;
234         char colheader_label[32];
235
236         /** Determine what day to start.
237          * First, back up to the 1st of the month...
238          */
239         memset(&starting_tm, 0, sizeof(struct tm));
240         starting_tm.tm_year = year - 1900;
241         starting_tm.tm_mon = month - 1;
242         starting_tm.tm_mday = day;
243         thetime = mktime(&starting_tm);
244
245         memcpy(&tm, &starting_tm, sizeof(struct tm));
246         while (tm.tm_mday != 1) {
247                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
248                 localtime_r(&thetime, &tm);
249         }
250
251         /** Determine previous and next months ... for links */
252         previous_month = thetime - (time_t)864000L;     /* back 10 days */
253         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
254
255         /** Now back up until we're on a Sunday */
256         localtime_r(&thetime, &tm);
257         while (tm.tm_wday != 0) {
258                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
259                 localtime_r(&thetime, &tm);
260         }
261
262         /** Outer table (to get the background color) */
263         wprintf("<div class=\"fix_scrollbar_bug\">"
264                 "<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
265                 "bgcolor=#204B78><TR><TD>\n");
266
267         wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0><tr>\n");
268
269         wprintf("<TD ALIGN=CENTER>");
270
271         localtime_r(&previous_month, &tm);
272         wprintf("<a href=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
273                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
274         wprintf("<IMG ALIGN=MIDDLE src=\"static/prevdate_32x.gif\" BORDER=0></A>\n");
275
276         wc_strftime(colheader_label, sizeof colheader_label, "%B", &starting_tm);
277         wprintf("&nbsp;&nbsp;"
278                 "<FONT SIZE=+1 COLOR=\"#FFFFFF\">"
279                 "%s %d"
280                 "</FONT>"
281                 "&nbsp;&nbsp;", colheader_label, year);
282
283         localtime_r(&next_month, &tm);
284         wprintf("<a href=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
285                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
286         wprintf("<IMG ALIGN=MIDDLE src=\"static/nextdate_32x.gif\" BORDER=0></A>\n");
287
288         wprintf("</TD></TR></TABLE>\n");
289
290         /** Inner table (the real one) */
291         wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
292                 "bgcolor=#204B78><TR>");
293         colheader_time = thetime;
294         for (i=0; i<7; ++i) {
295                 colheader_time = thetime + (i * 86400) ;
296                 localtime_r(&colheader_time, &colheader_tm);
297                 wc_strftime(colheader_label, sizeof colheader_label, "%A", &colheader_tm);
298                 wprintf("<TD ALIGN=CENTER WIDTH=14%%>"
299                         "<FONT COLOR=\"#FFFFFF\">%s</FONT></TH>", colheader_label);
300         }
301         wprintf("</TR>\n");
302
303         /** Now do 35 days */
304         for (i = 0; i < 35; ++i) {
305                 localtime_r(&thetime, &tm);
306
307                 /** Before displaying Sunday, start a new row */
308                 if ((i % 7) == 0) {
309                         wprintf("<TR>");
310                 }
311
312                 wprintf("<TD BGCOLOR=\"#%s\" WIDTH=14%% HEIGHT=60 align=left VALIGN=TOP><B>",
313                         ((tm.tm_mon != month-1) ? "DDDDDD" :
314                         ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
315                         "FFFFFF"))
316                 );
317                 if ((i==0) || (tm.tm_mday == 1)) {
318                         wc_strftime(colheader_label, sizeof colheader_label, "%B", &tm);
319                         wprintf("%s ", colheader_label);
320                 }
321                 wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
322                         "%d</A></B><br />",
323                         tm.tm_year + 1900,
324                         tm.tm_mon + 1,
325                         tm.tm_mday,
326                         tm.tm_mday);
327
328                 /** put the data here, stupid */
329                 calendar_month_view_display_events(thetime);
330
331                 wprintf("</TD>");
332
333                 /** After displaying Saturday, end the row */
334                 if ((i % 7) == 6) {
335                         wprintf("</TR>\n");
336                 }
337
338                 thetime += (time_t)86400;               /** ahead 24 hours */
339         }
340
341         wprintf("</TABLE>"                      /** end of inner table */
342                 "</TD></TR></TABLE>"            /** end of outer table */
343                 "</div>\n");
344 }
345
346 /**
347  * \brief view one month. brief view
348  * \param year the year
349  * \param month the month
350  * \param day the actual day we want to see
351  */
352 void calendar_brief_month_view(int year, int month, int day) {
353         struct tm starting_tm;
354         struct tm tm;
355         time_t thetime;
356         int i;
357         time_t previous_month;
358         time_t next_month;
359         char month_label[32];
360
361         /** Determine what day to start.
362          * First, back up to the 1st of the month...
363          */
364         memset(&starting_tm, 0, sizeof(struct tm));
365         starting_tm.tm_year = year - 1900;
366         starting_tm.tm_mon = month - 1;
367         starting_tm.tm_mday = day;
368         thetime = mktime(&starting_tm);
369
370         memcpy(&tm, &starting_tm, sizeof(struct tm));
371         while (tm.tm_mday != 1) {
372                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
373                 localtime_r(&thetime, &tm);
374         }
375
376         /** Determine previous and next months ... for links */
377         previous_month = thetime - (time_t)864000L;     /* back 10 days */
378         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
379
380         /** Now back up until we're on a Sunday */
381         localtime_r(&thetime, &tm);
382         while (tm.tm_wday != 0) {
383                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
384                 localtime_r(&thetime, &tm);
385         }
386
387         /** Outer table (to get the background color) */
388         wprintf("<div class=\"fix_scrollbar_bug\">"
389                 "<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
390                 "bgcolor=#204B78><TR><TD>\n");
391
392         wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0><tr>\n");
393
394         wprintf("<TD ALIGN=CENTER>");
395
396         localtime_r(&previous_month, &tm);
397         wprintf("<a href=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
398                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
399         wprintf("<IMG ALIGN=MIDDLE src=\"static/prevdate_32x.gif\" BORDER=0></A>\n");
400
401         wc_strftime(month_label, sizeof month_label, "%B", &tm);
402         wprintf("&nbsp;&nbsp;"
403                 "<FONT SIZE=+1 COLOR=\"#FFFFFF\">"
404                 "%s %d"
405                 "</FONT>"
406                 "&nbsp;&nbsp;", month_label, year);
407
408         localtime_r(&next_month, &tm);
409         wprintf("<a href=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
410                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
411         wprintf("<IMG ALIGN=MIDDLE src=\"static/nextdate_32x.gif\" BORDER=0></A>\n");
412
413         wprintf("</TD></TR></TABLE>\n");
414
415         /** Inner table (the real one) */
416         wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
417                 "bgcolor=#EEEECC><TR>");
418         wprintf("</TR>\n");
419         wprintf("<TR><TD COLSPAN=\"100%\">\n");
420
421         /** Now do 35 days */
422         for (i = 0; i < 35; ++i) {
423                 char weeknumber[255];
424                 char weekday_name[32];
425                 char *daycolor;
426                 localtime_r(&thetime, &tm);
427
428
429                 /** Before displaying Sunday, start a new CELL */
430                 if ((i % 7) == 0) {
431                         wc_strftime(&weeknumber[0], sizeof(weeknumber), "%U", &tm);
432                         wprintf("<TABLE border='0' BGCOLOR=\"#EEEECC\" width='100%'> <tr><th colspan='4'>%s %s</th></tr>"
433                                         "   <tr><td>%s</td><td width='70%'>%s</td><td>%s</td><td>%s</td></tr>\n",
434                                         _("Week"), 
435                                         weeknumber,
436                                         _("Hours"),
437                                         _("Subject"),
438                                         _("Start"),
439                                         _("End")
440                                         );
441                 }
442                 
443                 daycolor=((tm.tm_mon != month-1) ? "DDDDDD" :
444                                   ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
445                                    "FFFFFF"));
446                 
447                 /** Day Header */
448                 wc_strftime(weekday_name, sizeof weekday_name, "%A", &tm);
449                 wprintf("<tr><td BGCOLOR='%s' colspan='1' align='left'> %s"
450                         "</td><td BGCOLOR='%s' colspan='3'><hr></td></tr>\n",
451                         daycolor,
452                         weekday_name,
453                         daycolor);
454
455                 /** put the data of one day  here, stupid */
456                 calendar_month_view_brief_events(thetime, daycolor);
457
458
459                 /** After displaying Saturday, end the row */
460                 if ((i % 7) == 6) {
461                         wprintf("</td></tr></table>\n");
462                 }
463
464                 thetime += (time_t)86400;               /** ahead 24 hours */
465         }
466
467         wprintf("</TABLE>"                      /** end of inner table */
468                 "</TD></TR></TABLE>"            /** end of outer table */
469                 "</div>\n");
470 }
471
472 /** 
473  * \brief view one week
474  * this should view just one week, but it's not here yet.
475  * \todo ny implemented
476  * \param year the year
477  * \param month the month
478  * \param day the day which we want to see the week around
479  */
480 void calendar_week_view(int year, int month, int day) {
481         wprintf("<CENTER><I>week view FIXME</I></CENTER><br />\n");
482 }
483
484
485 /**
486  * \brief display one day
487  * Display events for a particular hour of a particular day.
488  * (Specify hour < 0 to show "all day" events)
489  * \param year the year
490  * \param month the month
491  * \param day the day
492  * \param hour the hour we want to start displaying?????
493  */
494 void calendar_day_view_display_events(int year, int month,
495                                         int day, int hour) {
496         int i;
497         icalproperty *p;
498         struct icaltimetype t;
499         time_t event_tt;
500         struct tm *event_tm;
501         int all_day_event = 0;
502
503         if (WC->num_cal == 0) {
504                 // \todo FIXME wprintf("<br /><br /><br />\n");
505                 return;
506         }
507
508         for (i=0; i<(WC->num_cal); ++i) {
509                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
510                                                 ICAL_DTSTART_PROPERTY);
511                 if (p != NULL) {
512                         t = icalproperty_get_dtstart(p);
513                         event_tt = icaltime_as_timet(t);
514                         if (t.is_date) {
515                                 all_day_event = 1;
516                         }
517                         else {
518                                 all_day_event = 0;
519                         }
520
521                         if (all_day_event) {
522                                 event_tm = gmtime(&event_tt);
523                         }
524                         else {
525                                 event_tm = localtime(&event_tt);
526                         }
527
528                         if ((event_tm->tm_year == (year-1900))
529                            && (event_tm->tm_mon == (month-1))
530                            && (event_tm->tm_mday == day)
531                            && ( ((event_tm->tm_hour == hour)&&(!t.is_date)) || ((hour<0)&&(t.is_date)) )
532                            ) {
533
534
535                                 p = icalcomponent_get_first_property(
536                                                         WC->disp_cal[i].cal,
537                                                         ICAL_SUMMARY_PROPERTY);
538                                 if (p != NULL) {
539
540                                         if (all_day_event) {
541                                                 wprintf("<TABLE border=1 cellpadding=2><TR>"
542                                                         "<TD BGCOLOR=\"#CCCCCC\">"
543                                                 );
544                                         }
545
546                                         wprintf("<FONT SIZE=-1>"
547                                                 "<a href=\"display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
548                                                 WC->disp_cal[i].cal_msgnum,
549                                                 year, month, day
550                                         );
551                                         escputs((char *)
552                                                 icalproperty_get_comment(p));
553                                         wprintf("</A></FONT><br />\n");
554
555                                         if (all_day_event) {
556                                                 wprintf("</TD></TR></TABLE>");
557                                         }
558                                 }
559
560                         }
561
562
563                 }
564         }
565 }
566
567
568 /**
569  * \brief view one day
570  * \param year the year
571  * \param month the month 
572  * \param day the day we want to display
573  */
574 void calendar_day_view(int year, int month, int day) {
575         int hour;
576         struct icaltimetype today, yesterday, tomorrow;
577         char calhourformat[16];
578         int daystart = 8;
579         int dayend = 17;
580         char daystart_str[16], dayend_str[16];
581         struct tm d_tm;
582         char d_str[128];
583
584         get_preference("calhourformat", calhourformat, sizeof calhourformat);
585         get_preference("daystart", daystart_str, sizeof daystart_str);
586         if (strlen(daystart_str) > 0) daystart = atoi(daystart_str);
587         get_preference("dayend", dayend_str, sizeof dayend_str);
588         if (strlen(dayend_str) > 0) dayend = atoi(dayend_str);
589         
590
591         /** Figure out the dates for "yesterday" and "tomorrow" links */
592
593         memset(&today, 0, sizeof(struct icaltimetype));
594         today.year = year;
595         today.month = month;
596         today.day = day;
597         today.is_date = 1;
598
599         memcpy(&yesterday, &today, sizeof(struct icaltimetype));
600         --yesterday.day;
601         yesterday = icaltime_normalize(yesterday);
602
603         memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
604         ++tomorrow.day;
605         tomorrow = icaltime_normalize(tomorrow);
606
607
608         /** Outer table (to get the background color) */
609         wprintf("<div class=\"fix_scrollbar_bug\">"
610                 "<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
611                 "bgcolor=#204B78><TR><TD>\n");
612
613         /** Inner table (the real one) */
614         wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
615                 "bgcolor=#204B78><TR>\n");
616
617         /** Innermost table (contains hours etc.) */
618         wprintf("<TD WIDTH=80%%>"
619                 "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
620                 "bgcolor=#204B78>\n");
621
622         /** Display events before 8:00 (hour=-1 is all-day events) */
623         wprintf("<TR>"
624                 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
625                 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
626         for (hour = (-1); hour <= (daystart-1); ++hour) {
627                 calendar_day_view_display_events(year, month, day, hour);
628         }
629         wprintf("</TD></TR>\n");
630
631         /** Now the middle of the day... */     
632         for (hour = daystart; hour <= dayend; ++hour) { /* could do HEIGHT=xx */
633                 wprintf("<TR HEIGHT=30><TD BGCOLOR=\"#CCCCDD\" ALIGN=MIDDLE "
634                         "VALIGN=MIDDLE WIDTH=10%%>");
635                 wprintf("<a href=\"display_edit_event?msgnum=0"
636                         "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
637                         year, month, day, hour
638                 );
639
640                 if (!strcasecmp(calhourformat, "24")) {
641                         wprintf("%2d:00</A> ", hour);
642                 }
643                 else {
644                         wprintf("%d:00%s</A> ",
645                                 (hour <= 12 ? hour : hour-12),
646                                 (hour < 12 ? "am" : "pm")
647                         );
648                 }
649
650                 wprintf("</TD><TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
651
652                 /* put the data here, stupid */
653                 calendar_day_view_display_events(year, month, day, hour);
654
655                 wprintf("</TD></TR>\n");
656         }
657
658         /** Display events after 5:00... */
659         wprintf("<TR>"
660                 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
661                 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
662         for (hour = (dayend+1); hour <= 23; ++hour) {
663                 calendar_day_view_display_events(year, month, day, hour);
664         }
665         wprintf("</TD></TR>\n");
666
667
668         wprintf("</TABLE>"                      /* end of innermost table */
669                 "</TD>"
670         );
671
672         wprintf("<TD WIDTH=20%% VALIGN=top>");  /* begin stuff-on-the-right */
673
674
675         /** Begin todays-date-with-left-and-right-arrows */
676         wprintf("<TABLE BORDER=0 WIDTH=100%% "
677                 "CELLSPACING=0 CELLPADDING=0 BGCOLOR=\"#FFFFFF\">\n");
678         wprintf("<TR>");
679
680         /** Left arrow */       
681         wprintf("<TD ALIGN=CENTER>");
682         wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
683                 yesterday.year, yesterday.month, yesterday.day);
684         wprintf("<IMG ALIGN=MIDDLE src=\"static/prevdate_32x.gif\" BORDER=0></A>");
685         wprintf("</TD>");
686
687         /** Today's date */
688         memset(&d_tm, 0, sizeof d_tm);
689         d_tm.tm_year = year - 1900;
690         d_tm.tm_mon = month - 1;
691         d_tm.tm_mday = day;
692         wc_strftime(d_str, sizeof d_str,
693                 "<td align=center>"
694                 "<font size=+2>%B</font><br />"
695                 "<font size=+3>%d</font><br />"
696                 "<font size=+2>%Y</font><br />"
697                 "</td>",
698                 &d_tm
699         );
700         wprintf("%s", d_str);
701
702         /** Right arrow */
703         wprintf("<TD ALIGN=CENTER>");
704         wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
705                 tomorrow.year, tomorrow.month, tomorrow.day);
706         wprintf("<IMG ALIGN=MIDDLE src=\"static/nextdate_32x.gif\""
707                 " BORDER=0></A>\n");
708         wprintf("</TD>");
709
710         wprintf("</TR></TABLE>\n");
711         /** End todays-date-with-left-and-right-arrows */
712
713         /** \todo In the future we might want to put a month-o-matic here */
714
715         wprintf("</FONT></CENTER>\n");
716
717         wprintf("</TD>");                       /** end stuff-on-the-right */
718
719
720
721         wprintf("</TR></TABLE>"                 /** end of inner table */
722                 "</TD></TR></TABLE></div>"      /** end of outer table */
723         );
724
725
726
727 }
728
729 /**
730  * \brief Display today's events.
731  */
732 void calendar_summary_view(void) {
733         int i;
734         icalproperty *p;
735         struct icaltimetype t;
736         time_t event_tt;
737         struct tm event_tm;
738         struct tm today_tm;
739         time_t now;
740         int all_day_event = 0;
741         char timestring[SIZ];
742
743         if (WC->num_cal == 0) {
744                 return;
745         }
746
747         now = time(NULL);
748         localtime_r(&now, &today_tm);
749
750         for (i=0; i<(WC->num_cal); ++i) {
751                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
752                                                 ICAL_DTSTART_PROPERTY);
753                 if (p != NULL) {
754                         t = icalproperty_get_dtstart(p);
755                         event_tt = icaltime_as_timet(t);
756                         if (t.is_date) {
757                                 all_day_event = 1;
758                         }
759                         else {
760                                 all_day_event = 0;
761                         }
762                         fmt_time(timestring, event_tt);
763
764                         if (all_day_event) {
765                                 gmtime_r(&event_tt, &event_tm);
766                         }
767                         else {
768                                 localtime_r(&event_tt, &event_tm);
769                         }
770
771                         if ( (event_tm.tm_year == today_tm.tm_year)
772                            && (event_tm.tm_mon == today_tm.tm_mon)
773                            && (event_tm.tm_mday == today_tm.tm_mday)
774                            ) {
775
776
777                                 p = icalcomponent_get_first_property(
778                                                         WC->disp_cal[i].cal,
779                                                         ICAL_SUMMARY_PROPERTY);
780                                 if (p != NULL) {
781                                         escputs((char *)
782                                                 icalproperty_get_comment(p));
783                                         wprintf(" (%s)<br />\n", timestring);
784                                 }
785                         }
786                 }
787         }
788         free_calendar_buffer();
789 }
790
791
792 /**
793  * \brief clean up ical memory
794  * \todo this could get troubel with future ical versions
795  */
796 void free_calendar_buffer(void) {
797         int i;
798         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
799                 icalcomponent_free(WC->disp_cal[i].cal);
800         }
801         WC->num_cal = 0;
802         free(WC->disp_cal);
803         WC->disp_cal = NULL;
804 }
805
806
807
808 /**
809  * \brief do the whole calendar page
810  * view any part of the calender. decide which way, etc.
811  */
812 void do_calendar_view(void) {
813         time_t now;
814         struct tm tm;
815         int year, month, day;
816         char calview[SIZ];
817
818         /** In case no date was specified, go with today */
819         now = time(NULL);
820         localtime_r(&now, &tm);
821         year = tm.tm_year + 1900;
822         month = tm.tm_mon + 1;
823         day = tm.tm_mday;
824
825         /** Now see if a date was specified */
826         if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
827         if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
828         if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
829
830         /** How would you like that cooked? */
831         if (strlen(bstr("calview")) > 0) {
832                 strcpy(calview, bstr("calview"));
833         }
834         else {
835                 strcpy(calview, "month");
836         }
837
838         /** Display the selected view */
839         if (!strcasecmp(calview, "day")) {
840                 calendar_day_view(year, month, day);
841         }
842         else if (!strcasecmp(calview, "week")) {
843                 calendar_week_view(year, month, day);
844         }
845         else {
846                 if (WC->wc_view == VIEW_CALBRIEF) {
847                         calendar_brief_month_view(year, month, day);
848                 }
849                 else {
850                         calendar_month_view(year, month, day);
851                 }
852         }
853
854         /** Free the calendar stuff */
855         free_calendar_buffer();
856
857 }
858
859
860 /**
861  * \brief get task due date
862  * Helper function for do_tasks_view().  
863  * \param vtodo a task to get the due date
864  * \return the date/time due.
865  */
866 time_t get_task_due_date(icalcomponent *vtodo) {
867         icalproperty *p;
868
869         if (vtodo == NULL) {
870                 return(0L);
871         }
872
873         /**
874          * If we're looking at a fully encapsulated VCALENDAR
875          * rather than a VTODO component, recurse into the data
876          * structure until we get a VTODO.
877          */
878         if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
879                 return get_task_due_date(
880                         icalcomponent_get_first_component(
881                                 vtodo, ICAL_VTODO_COMPONENT
882                         )
883                 );
884         }
885
886         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
887         if (p != NULL) {
888                 return(icaltime_as_timet(icalproperty_get_due(p)));
889         }
890         else {
891                 return(0L);
892         }
893 }
894
895
896 /**
897  * \brief Compare the due dates of two tasks (this is for sorting)
898  * \param task1 first task to compare
899  * \param task2 second task to compare
900  */
901 int task_due_cmp(const void *task1, const void *task2) {
902         time_t t1;
903         time_t t2;
904
905         t1 =  get_task_due_date(((struct disp_cal *)task1)->cal);
906         t2 =  get_task_due_date(((struct disp_cal *)task2)->cal);
907
908         if (t1 < t2) return(-1);
909         if (t1 > t2) return(1);
910         return(0);
911 }
912
913
914
915
916 /**
917  * \brief do the whole task view stuff
918  */
919 void do_tasks_view(void) {
920         int i;
921         time_t due;
922         int bg = 0;
923         char buf[SIZ];
924         icalproperty *p;
925
926         wprintf("<div class=\"fix_scrollbar_bug\">"
927                 "<table border=0 cellspacing=0 width=100%% bgcolor=\"#FFFFFF\">\n<tr>\n"
928                 "<TH>");
929         wprintf(_("Name of task"));
930         wprintf("</TH><TH>");
931         wprintf(_("Date due"));
932         wprintf("</TH></TR>\n"
933         );
934
935         /** Sort them if necessary */
936         if (WC->num_cal > 1) {
937                 qsort(WC->disp_cal,
938                         WC->num_cal,
939                         sizeof(struct disp_cal),
940                         task_due_cmp
941                 );
942         }
943
944         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
945
946                 bg = 1 - bg;
947                 wprintf("<TR BGCOLOR=\"#%s\"><TD>",
948                         (bg ? "DDDDDD" : "FFFFFF")
949                 );
950
951                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
952                                                         ICAL_SUMMARY_PROPERTY);
953                 wprintf("<a href=\"display_edit_task?msgnum=%ld&taskrm=",
954                         WC->disp_cal[i].cal_msgnum );
955                 urlescputs(WC->wc_roomname);
956                 wprintf("\">");
957                 wprintf("<IMG ALIGN=MIDDLE "
958                         "src=\"static/taskmanag_16x.gif\" BORDER=0>&nbsp;");
959                 if (p != NULL) {
960                         escputs((char *)icalproperty_get_comment(p));
961                 }
962                 wprintf("</A>\n");
963                 wprintf("</TD>\n");
964
965                 due = get_task_due_date(WC->disp_cal[i].cal);
966                 fmt_date(buf, due, 0);
967                 wprintf("<TD><FONT");
968                 if (due < time(NULL)) {
969                         wprintf(" COLOR=\"#FF0000\"");
970                 }
971                 wprintf(">%s</FONT></TD></TR>\n", buf);
972         }
973
974         wprintf("</table></div>\n");
975
976         /** Free the list */
977         free_calendar_buffer();
978
979 }
980
981 #endif  /* WEBCIT_WITH_CALENDAR_SERVICE */
982
983 /** @} */