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