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