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