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