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