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