Daily vue of Calendar.
[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         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
638         struct disp_cal *Cal;
639         struct icaltimetype t;
640         struct icaltimetype end_t;
641         struct icaltimetype today_start_t;
642         struct icaltimetype today_end_t;
643         struct tm starting_tm;
644         struct tm ending_tm;
645
646
647         if (WCC->num_cal == 0) {
648                 // \todo FIXME wprintf("<br /><br /><br />\n");
649                 return;
650         }
651
652         event_start = thetime + 60 * 60 * hour;
653         event_end = thetime + 60 * 60 * (hour + 1);
654
655
656         /* Create an imaginary event which spans the 24 hours of today.  Any events which
657          * overlap with this one take place at least partially in this day.
658          */
659         memset(&starting_tm, 0, sizeof(struct tm));
660         starting_tm.tm_year = year - 1900;
661         starting_tm.tm_mon = month - 1;
662         starting_tm.tm_mday = day;
663         starting_tm.tm_hour = hour;
664         starting_tm.tm_min = 0;
665         today_start_t = icaltime_from_timet_with_zone(mktime(&starting_tm), 0, icaltimezone_get_utc_timezone());
666         today_start_t.is_utc = 1;
667
668         memset(&ending_tm, 0, sizeof(struct tm));
669         ending_tm.tm_year = year - 1900;
670         ending_tm.tm_mon = month - 1;
671         ending_tm.tm_mday = day;
672         ending_tm.tm_hour = hour;
673         ending_tm.tm_min = 59;
674         today_end_t = icaltime_from_timet_with_zone(mktime(&ending_tm), 0, icaltimezone_get_utc_timezone());
675 /*      today_end_t.is_utc = 1;*/
676
677         /* Now loop through our list of events to see which ones occur today.
678          */
679         for (i=0; i<(WCC->num_cal); ++i) {
680                 Cal = &WCC->disp_cal[i];
681
682                 all_day_event = 0;
683                 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
684                 if (q != NULL) {
685                         t = icalproperty_get_dtstart(q);
686                         event_tt = icaltime_as_timet(t);
687                 }
688                 else {
689                         memset(&t, 0, sizeof t);
690                 }
691                 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
692                 if (q != NULL) {
693                         end_t = icalproperty_get_dtend(q);
694                 }
695                 else {
696                         memset(&end_t, 0, sizeof end_t);
697                 }
698                 if (t.is_date) all_day_event = 1;
699
700                 if (all_day_event)
701                 {
702                         show_event = ((t.year == year) && (t.month == month) && (t.day == day) && (hour == -1));
703                 }
704                 else
705                 {
706                         show_event = ical_ctdl_is_overlap(t, end_t, today_start_t, today_end_t);
707                         localtime_r(&event_tt, &event_te);
708                 }
709
710                 /* If we determined that this event occurs today, then display it.
711                  */
712                 p = icalcomponent_get_first_property(Cal->cal,ICAL_SUMMARY_PROPERTY);
713                 if ((show_event) && (p != NULL)) {
714                         if (all_day_event)
715                         {
716                                 wprintf("<dd><a href=\"display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d&hour=%d\">",
717                                         Cal->cal_msgnum, year, month, day, hour);
718                                 escputs((char *) icalproperty_get_comment(p));
719                                 wprintf("</a></dd>\n");
720                         }
721                         else 
722                         {
723                                 event_tte = icaltime_as_timet(end_t);
724                                 localtime_r(&event_tte, &event_tm);
725                                 if (hour == event_te.tm_hour) {
726                                 wprintf("<dd  class=\"event\" "
727                                         "style=\"position: absolute; "
728                                         "top:%dpx; left:100px; "
729                                         "height:%dpx; \" >",
730                                         ((dstart * 11) + (event_te.tm_hour - dstart) * (event_te.tm_hour < dstart? 11 : 31)),
731                                         ((event_tm.tm_hour - event_te.tm_hour) * 30)
732                                         );
733                                 wprintf("<a href=\"display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d&hour=%d&case=%d\">",
734                                         Cal->cal_msgnum, year, month, day, t.hour, hour);
735                                 escputs((char *) icalproperty_get_comment(p));
736                                 wprintf("</a></dd>\n");
737                                 }
738                                 
739                         }
740                 }
741         }
742 }
743
744 /**
745  * \brief view one day
746  * \param year the year
747  * \param month the month 
748  * \param day the day we want to display
749  */
750 void calendar_day_view(int year, int month, int day) {
751         int hour;
752         struct icaltimetype today, yesterday, tomorrow;
753         int daystart = 8;
754         int dayend = 17;
755         char daystart_str[16], dayend_str[16];
756         struct tm d_tm;
757         char d_str[128];
758         int time_format;
759         time_t today_t;
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         /** Today's date */
768         memset(&d_tm, 0, sizeof d_tm);
769         d_tm.tm_year = year - 1900;
770         d_tm.tm_mon = month - 1;
771         d_tm.tm_mday = day;
772         today_t = mktime(&d_tm); 
773
774         /** Figure out the dates for "yesterday" and "tomorrow" links */
775
776         memset(&today, 0, sizeof(struct icaltimetype));
777         today.year = year;
778         today.month = month;
779         today.day = day;
780         today.is_date = 1;
781
782         memcpy(&yesterday, &today, sizeof(struct icaltimetype));
783         --yesterday.day;
784         yesterday = icaltime_normalize(yesterday);
785
786         memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
787         ++tomorrow.day;
788         tomorrow = icaltime_normalize(tomorrow);
789
790         wprintf("<div class=\"fix_scrollbar_bug\">");
791
792         /** Inner table (the real one) */
793         wprintf("<table class=\"calendar\" id=\"inner_day\"><tr> \n");
794
795         /** Innermost cell (contains hours etc.) */
796         wprintf("<td class=\"events_of_the_day\" >");
797         wprintf("<dl class=\"events\" >");
798         /** Now the middle of the day... */     
799         for (hour = 0; hour < 24; ++hour) {     /* could do HEIGHT=xx */
800                 wprintf("<dt class=\"hour%s\"><a href=\"display_edit_event?msgnum=0"
801                         "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
802                         (hour < daystart ? "before" : (hour > dayend ? "after" : "")),
803                         year, month, day, hour
804                 );
805
806                 if (time_format == WC_TIMEFORMAT_24) {
807                         wprintf("%2d:00</a> ", hour);
808                 }
809                 else {
810                         wprintf("%d:00%s</a> ",
811                                 (hour <= 12 ? hour : hour-12),
812                                 (hour < 12 ? "am" : "pm")
813                         );
814                 }
815
816                 wprintf("</dt>");
817         
818
819                 /* put the data here, stupid */
820                 calendar_day_view_display_events(today_t, year, month, day, hour, daystart, dayend);
821
822         }
823
824         wprintf("</dl>");
825         wprintf("</td>");                       /* end of innermost table */
826
827         /** Extra events on the middle */
828         wprintf("<td class=\"extra_events\">");
829
830         wprintf("<dl>");
831
832         /** Display all-day events) */
833         wprintf("<dt>All day events</dt>");
834                 calendar_day_view_display_events(today_t, year, month, day, -1, daystart, dayend);
835
836         wprintf("</dl>");
837
838         wprintf("</td>");       /** end extra on the middle */
839
840         wprintf("<td width=20%% valign=top>");  /** begin stuff-on-the-right */
841
842         /** Begin todays-date-with-left-and-right-arrows */
843         wprintf("<table border=0 width=100%% "
844                 "cellspacing=0 cellpadding=0 bgcolor=\"#FFFFFF\">\n");
845         wprintf("<tr>");
846
847         /** Left arrow */       
848         wprintf("<td align=center>");
849         wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
850                 yesterday.year, yesterday.month, yesterday.day);
851         wprintf("<img align=middle src=\"static/prevdate_32x.gif\" border=0></A>");
852         wprintf("</td>");
853
854         wc_strftime(d_str, sizeof d_str,
855                 "<td align=center>"
856                 "<font size=+2>%B</font><br />"
857                 "<font size=+3>%d</font><br />"
858                 "<font size=+2>%Y</font><br />"
859                 "</td>",
860                 &d_tm
861         );
862         wprintf("%s", d_str);
863
864         /** Right arrow */
865         wprintf("<td align=center>");
866         wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
867                 tomorrow.year, tomorrow.month, tomorrow.day);
868         wprintf("<img align=middle src=\"static/nextdate_32x.gif\""
869                 " border=0></a>\n");
870         wprintf("</td>");
871
872         wprintf("</tr></table>\n");
873         /** End todays-date-with-left-and-right-arrows */
874
875         /** \todo In the future we might want to put a month-o-matic here */
876
877         wprintf("</font></center>\n");
878
879         wprintf("</td></tr>");                  /** end stuff-on-the-right */
880
881         wprintf("</table>"                      /** end of inner table */
882                 "</div>");
883
884         wprintf("<script type=\"text/javascript\">"
885                 " setTimeout(\"btt_enableTooltips('inner_day')\", 1); "
886                 "</script>\n"
887         );
888 }
889
890
891 /**
892  * \brief Display today's events.
893  */
894 void calendar_summary_view(void) {
895         int i;
896         icalproperty *p;
897         struct icaltimetype t;
898         time_t event_tt;
899         struct tm event_tm;
900         struct tm today_tm;
901         time_t now;
902         int all_day_event = 0;
903         char timestring[SIZ];
904
905         if (WC->num_cal == 0) {
906                 return;
907         }
908
909         now = time(NULL);
910         localtime_r(&now, &today_tm);
911
912         for (i=0; i<(WC->num_cal); ++i) {
913                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
914                                                 ICAL_DTSTART_PROPERTY);
915                 if (p != NULL) {
916                         t = icalproperty_get_dtstart(p);
917                         event_tt = icaltime_as_timet(t);
918                         if (t.is_date) {
919                                 all_day_event = 1;
920                         }
921                         else {
922                                 all_day_event = 0;
923                         }
924                         fmt_time(timestring, event_tt);
925
926                         if (all_day_event) {
927                                 gmtime_r(&event_tt, &event_tm);
928                         }
929                         else {
930                                 localtime_r(&event_tt, &event_tm);
931                         }
932
933                         if ( (event_tm.tm_year == today_tm.tm_year)
934                            && (event_tm.tm_mon == today_tm.tm_mon)
935                            && (event_tm.tm_mday == today_tm.tm_mday)
936                            ) {
937
938
939                                 p = icalcomponent_get_first_property(
940                                                         WC->disp_cal[i].cal,
941                                                         ICAL_SUMMARY_PROPERTY);
942                                 if (p != NULL) {
943                                         escputs((char *)
944                                                 icalproperty_get_comment(p));
945                                         wprintf(" (%s)<br />\n", timestring);
946                                 }
947                         }
948                 }
949         }
950         free_calendar_buffer();
951 }
952
953
954 /**
955  * \brief clean up ical memory
956  * \todo this could get troubel with future ical versions
957  */
958 void free_calendar_buffer(void) {
959         int i;
960         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
961                 icalcomponent_free(WC->disp_cal[i].cal);
962         }
963         WC->num_cal = 0;
964         free(WC->disp_cal);
965         WC->disp_cal = NULL;
966 }
967
968
969
970 /**
971  * \brief do the whole calendar page
972  * view any part of the calender. decide which way, etc.
973  */
974 void do_calendar_view(void) {
975         time_t now;
976         struct tm tm;
977         int year, month, day;
978         char calview[SIZ];
979
980         /** In case no date was specified, go with today */
981         now = time(NULL);
982         localtime_r(&now, &tm);
983         year = tm.tm_year + 1900;
984         month = tm.tm_mon + 1;
985         day = tm.tm_mday;
986
987         /** Now see if a date was specified */
988         if (!IsEmptyStr(bstr("year"))) year = atoi(bstr("year"));
989         if (!IsEmptyStr(bstr("month"))) month = atoi(bstr("month"));
990         if (!IsEmptyStr(bstr("day"))) day = atoi(bstr("day"));
991
992         /** How would you like that cooked? */
993         if (!IsEmptyStr(bstr("calview"))) {
994                 strcpy(calview, bstr("calview"));
995         }
996         else {
997                 strcpy(calview, "month");
998         }
999
1000         /** Display the selected view */
1001         if (!strcasecmp(calview, "day")) {
1002                 calendar_day_view(year, month, day);
1003         }
1004         else if (!strcasecmp(calview, "week")) {
1005                 calendar_week_view(year, month, day);
1006         }
1007         else {
1008                 if (WC->wc_view == VIEW_CALBRIEF) {
1009                         calendar_brief_month_view(year, month, day);
1010                 }
1011                 else {
1012                         calendar_month_view(year, month, day);
1013                 }
1014         }
1015
1016         /** Free the calendar stuff */
1017         free_calendar_buffer();
1018
1019 }
1020
1021
1022 /**
1023  * \brief get task due date
1024  * Helper function for do_tasks_view().  
1025  * \param vtodo a task to get the due date
1026  * \return the date/time due.
1027  */
1028 time_t get_task_due_date(icalcomponent *vtodo) {
1029         icalproperty *p;
1030
1031         if (vtodo == NULL) {
1032                 return(0L);
1033         }
1034
1035         /**
1036          * If we're looking at a fully encapsulated VCALENDAR
1037          * rather than a VTODO component, recurse into the data
1038          * structure until we get a VTODO.
1039          */
1040         if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
1041                 return get_task_due_date(
1042                         icalcomponent_get_first_component(
1043                                 vtodo, ICAL_VTODO_COMPONENT
1044                         )
1045                 );
1046         }
1047
1048         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
1049         if (p != NULL) {
1050                 return(icaltime_as_timet(icalproperty_get_due(p)));
1051         }
1052         else {
1053                 return(0L);
1054         }
1055 }
1056
1057
1058 /**
1059  * \brief Compare the due dates of two tasks (this is for sorting)
1060  * \param task1 first task to compare
1061  * \param task2 second task to compare
1062  */
1063 int task_due_cmp(const void *task1, const void *task2) {
1064         time_t t1;
1065         time_t t2;
1066
1067         t1 =  get_task_due_date(((struct disp_cal *)task1)->cal);
1068         t2 =  get_task_due_date(((struct disp_cal *)task2)->cal);
1069
1070         if (t1 < t2) return(-1);
1071         if (t1 > t2) return(1);
1072         return(0);
1073 }
1074
1075
1076
1077
1078 /**
1079  * \brief do the whole task view stuff
1080  */
1081 void do_tasks_view(void) {
1082         int i;
1083         time_t due;
1084         int bg = 0;
1085         char buf[SIZ];
1086         icalproperty *p;
1087
1088         wprintf("<div class=\"fix_scrollbar_bug\">"
1089                 "<table class=\"calendar_view_background\">\n<tr>\n"
1090                 "<th>");
1091         wprintf(_("Name of task"));
1092         wprintf("</th><th>");
1093         wprintf(_("Date due"));
1094         wprintf("</th></tr>\n"
1095         );
1096
1097         /** Sort them if necessary */
1098         if (WC->num_cal > 1) {
1099                 qsort(WC->disp_cal,
1100                         WC->num_cal,
1101                         sizeof(struct disp_cal),
1102                         task_due_cmp
1103                 );
1104         }
1105
1106         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
1107
1108                 bg = 1 - bg;
1109                 wprintf("<tr bgcolor=\"#%s\"><td>",
1110                         (bg ? "DDDDDD" : "FFFFFF")
1111                 );
1112
1113                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
1114                                                         ICAL_SUMMARY_PROPERTY);
1115                 wprintf("<a href=\"display_edit_task?msgnum=%ld&taskrm=",
1116                         WC->disp_cal[i].cal_msgnum );
1117                 urlescputs(WC->wc_roomname);
1118                 wprintf("\">");
1119                 wprintf("<img align=middle "
1120                         "src=\"static/taskmanag_16x.gif\" border=0>&nbsp;");
1121                 if (p != NULL) {
1122                         escputs((char *)icalproperty_get_comment(p));
1123                 }
1124                 wprintf("</a>\n");
1125                 wprintf("</td>\n");
1126
1127                 due = get_task_due_date(WC->disp_cal[i].cal);
1128                 fmt_date(buf, due, 0);
1129                 wprintf("<td><font");
1130                 if (due < time(NULL)) {
1131                         wprintf(" color=\"#FF0000\"");
1132                 }
1133                 wprintf(">%s</font></td></tr>\n", buf);
1134         }
1135
1136         wprintf("</table></div>\n");
1137
1138         /** Free the list */
1139         free_calendar_buffer();
1140
1141 }
1142
1143 #else   /* WEBCIT_WITH_CALENDAR_SERVICE */
1144
1145 /**\brief stub for non-libical builds */
1146 void do_calendar_view(void) {
1147         wprintf("<center><i>");
1148         wprintf(_("The calendar view is not available."));
1149         wprintf("</i></center><br />\n");
1150 }
1151
1152 /**\brief stub for non-libical builds */
1153 void do_tasks_view(void) {      
1154         wprintf("<center><I>");
1155         wprintf(_("The tasks view is not available."));
1156         wprintf("</i></center><br />\n");
1157 }
1158
1159
1160 #endif  /* WEBCIT_WITH_CALENDAR_SERVICE */
1161
1162 /** @} */