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