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