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