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