Moved some calendar view javascript out to the trailing block.
[citadel.git] / webcit / calendar_view.c
1 /*
2  * $Id$
3  *
4  * Handles the HTML display of calendar items.
5  */
6
7 #include "webcit.h"
8 #include "webserver.h"
9
10
11 void embeddable_mini_calendar(int year, int month, char *urlformat)
12 {
13         struct tm starting_tm;
14         struct tm tm;
15         time_t thetime;
16         int i, len;
17         time_t previous_month;
18         time_t next_month;
19         time_t colheader_time;
20         struct tm colheader_tm;
21         char colheader_label[32];
22         long weekstart = 0;
23         char url[256];
24         char div_id[256];
25         char escaped_urlformat[256];
26
27         snprintf(div_id, sizeof div_id, "mini_calendar_%d", rand() );
28
29         /* Determine what day to start.  If an impossible value is found, start on Sunday.
30         */
31         get_pref_long("weekstart", &weekstart, 17);
32         if (weekstart > 6) weekstart = 0;
33
34         /*
35         * Now back up to the 1st of the month...
36         */
37         memset(&starting_tm, 0, sizeof(struct tm));
38
39         starting_tm.tm_year = year - 1900;
40         starting_tm.tm_mon = month - 1;
41         starting_tm.tm_mday = 1;
42         thetime = mktime(&starting_tm);
43
44         memcpy(&tm, &starting_tm, sizeof(struct tm));
45         while (tm.tm_mday != 1) {
46                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
47                 localtime_r(&thetime, &tm);
48         }
49
50         /* Determine previous and next months ... for links */
51         previous_month = thetime - (time_t)864000L;     /* back 10 days */
52         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
53
54         /* Now back up until we're on the user's preferred start day */
55         localtime_r(&thetime, &tm);
56         while (tm.tm_wday != weekstart) {
57                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
58                 localtime_r(&thetime, &tm);
59         }
60
61         wprintf("<div class=\"mini_calendar\" id=\"%s\">\n", div_id);
62
63         /* Previous month link */
64         localtime_r(&previous_month, &tm);
65         wprintf("<a href=\"javascript:minical_change_month(%d,%d);\">&laquo;</a>", 
66                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
67
68         wc_strftime(colheader_label, sizeof colheader_label, "%B", &starting_tm);
69         wprintf("&nbsp;&nbsp;"
70                 "<span class=\"mini_calendar_month_label\">"
71                 "%s %d"
72                 "</span>"
73                 "&nbsp;&nbsp;", colheader_label, year);
74
75         /* Next month link */
76         localtime_r(&next_month, &tm);
77         wprintf("<a href=\"javascript:minical_change_month(%d,%d);\">&raquo;</a>",
78                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
79
80         wprintf("<table border=0 cellpadding=1 cellspacing=1 class=\"mini_calendar_days\">"
81                 "<tr>");
82         colheader_time = thetime;
83         for (i=0; i<7; ++i) {
84                 colheader_time = thetime + (i * 86400) ;
85                 localtime_r(&colheader_time, &colheader_tm);
86                 wc_strftime(colheader_label, sizeof colheader_label, "%A", &colheader_tm);
87                 wprintf("<th>%c</th>", colheader_label[0]);
88
89         }
90         wprintf("</tr>\n");
91
92
93         /* Now do 35 or 42 days */
94         for (i = 0; i < 42; ++i) {
95                 localtime_r(&thetime, &tm);
96
97                 if (i < 35) {
98
99                         /* Before displaying Sunday, start a new row */
100                         if ((i % 7) == 0) {
101                                 wprintf("<tr>");
102                         }
103
104                         if (tm.tm_mon == month-1) {
105                                 snprintf(url, sizeof url, urlformat,
106                                         tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday);
107                                 wprintf("<td><a href=\"%s\">%d</a></td>", url, tm.tm_mday);
108                         }
109                         else {
110                                 wprintf("<td> </td>");
111                         }
112
113                         /* After displaying one week, end the row */
114                         if ((i % 7) == 6) {
115                                 wprintf("</tr>\n");
116                         }
117
118                 }
119
120                 thetime += (time_t)86400;               /* ahead 24 hours */
121         }
122
123         wprintf("</table>"                      /* end of inner table */
124                 "</div>\n");
125
126         /* javascript for previous and next month */
127         len = strlen(urlformat);
128         for (i=0; i<len; ++i) {
129                 sprintf(&escaped_urlformat[i*2], "%02X", urlformat[i]);
130         }
131
132         wprintf("<script type=\"text/javascript\">                                                      "
133                 "       function minical_change_month(year, month) {                                    "
134                 "               p = 'year=' + year + '?month=' + month                                  "
135                 "                       + '&urlformat=%s&r=' + CtdlRandomString();                      "
136                 "               new Ajax.Updater('%s', 'mini_calendar',                                 "
137                 "                       { method: 'get', parameters: p, evalScripts: true } );          "
138                 "       }                                                                               "
139                 "</script>\n"
140                 ,
141                 escaped_urlformat, div_id
142                 );
143
144 }
145
146 /*
147  * ajax embedder for the above mini calendar 
148  */
149 void ajax_mini_calendar(void) {
150         char urlformat[256];
151         int i, len;
152         char *escaped_urlformat;
153
154         escaped_urlformat = bstr("urlformat");
155         len = strlen(escaped_urlformat) * 2 ;
156         for (i=0; i<len; ++i) {
157                 urlformat[i] = xtoi(&escaped_urlformat[i*2], 2);
158                 urlformat[i+1] = 0;
159         }
160
161         embeddable_mini_calendar( ibstr("year"), ibstr("month"), urlformat );
162 }
163
164
165 /*
166  * Display one day of a whole month view of a calendar
167  */
168 void calendar_month_view_display_events(int year, int month, int day)
169 {
170         long hklen;
171         const char *HashKey;
172         void *vCal;
173         HashPos *Pos;
174         disp_cal *Cal;
175         icalproperty *p = NULL;
176         icalproperty *q = NULL;
177         struct icaltimetype t;
178         struct icaltimetype end_t;
179         struct icaltimetype today_start_t;
180         struct icaltimetype today_end_t;
181         struct tm starting_tm;
182         struct tm ending_tm;
183         int all_day_event = 0;
184         int show_event = 0;
185         char buf[256];
186         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
187         time_t tt;
188
189         if (GetCount(WCC->disp_cal_items) == 0) {
190                 wprintf("<br /><br /><br />\n");
191                 return;
192         }
193
194         /*
195          * Create an imaginary event which spans the 24 hours of today.  Any events which
196          * overlap with this one take place at least partially in this day.  We have to
197          * convert it from a struct tm in order to make it UTC.
198          */
199         memset(&starting_tm, 0, sizeof(struct tm));
200         starting_tm.tm_year = year - 1900;
201         starting_tm.tm_mon = month - 1;
202         starting_tm.tm_mday = day;
203         starting_tm.tm_hour = 0;
204         starting_tm.tm_min = 0;
205         today_start_t = icaltime_from_timet_with_zone(mktime(&starting_tm), 0, icaltimezone_get_utc_timezone());
206         today_start_t.is_utc = 1;
207
208         memset(&ending_tm, 0, sizeof(struct tm));
209         ending_tm.tm_year = year - 1900;
210         ending_tm.tm_mon = month - 1;
211         ending_tm.tm_mday = day;
212         ending_tm.tm_hour = 23;
213         ending_tm.tm_min = 59;
214         today_end_t = icaltime_from_timet_with_zone(mktime(&ending_tm), 0, icaltimezone_get_utc_timezone());
215         today_end_t.is_utc = 1;
216
217         /*
218          * Now loop through our list of events to see which ones occur today.
219          */
220         Pos = GetNewHashPos();
221         while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
222                 Cal = (disp_cal*)vCal;
223                 all_day_event = 0;
224                 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
225                 if (q != NULL) {
226                         t = icalproperty_get_dtstart(q);
227                 }
228                 else {
229                         memset(&t, 0, sizeof t);
230                 }
231                 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
232                 if (q != NULL) {
233                         end_t = icalproperty_get_dtend(q);
234                 }
235                 else {
236                         memset(&end_t, 0, sizeof end_t);
237                 }
238                 if (t.is_date) all_day_event = 1;
239
240                 if (all_day_event)
241                 {
242                         show_event = ((t.year == year) && (t.month == month) && (t.day == day));
243                 }
244                 else
245                 {
246                         show_event = ical_ctdl_is_overlap(t, end_t, today_start_t, today_end_t);
247                 }
248
249                 /*
250                  * If we determined that this event occurs today, then display it.
251                  */
252                 if (show_event) {
253                         p = icalcomponent_get_first_property(Cal->cal, ICAL_SUMMARY_PROPERTY);
254                         if (p != NULL) {
255
256                                 if (all_day_event) {
257                                         wprintf("<table border=0 cellpadding=2><TR>"
258                                                 "<td bgcolor=\"#CCCCDD\">"
259                                                 );
260                                 }
261
262                                 wprintf("<font size=\"-1\">"
263                                         "<a class=\"event%s\" href=\"display_edit_event?"
264                                         "msgnum=%ld?calview=month?year=%d?month=%d?day=%d\""
265                                         " btt_tooltext=\"",
266                                         (Cal->unread)?"_unread":"_read",
267                                         Cal->cal_msgnum,
268                                         year, month, day
269                                         );
270
271                                 wprintf("<i>%s: %s</i><br />", _("From"), Cal->from);
272                                 wprintf("<i>%s</i> ",          _("Summary:"));
273                                 escputs((char *)icalproperty_get_comment(p));
274                                 wprintf("<br />");
275                                 
276                                 q = icalcomponent_get_first_property(
277                                         Cal->cal,
278                                         ICAL_LOCATION_PROPERTY);
279                                 if (q) {
280                                         wprintf("<i>%s</i> ", _("Location:"));
281                                         escputs((char *)icalproperty_get_comment(q));
282                                         wprintf("<br />");
283                                 }
284                                 
285                                 /*
286                                  * Only show start/end times if we're actually looking at the VEVENT
287                                  * component.  Otherwise it shows bogus dates for e.g. timezones
288                                  */
289                                 if (icalcomponent_isa(Cal->cal) == ICAL_VEVENT_COMPONENT) {
290                                         
291                                         q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
292                                         if (q != NULL) {
293                                                 t = icalproperty_get_dtstart(q);
294                                                 
295                                                 if (t.is_date) {
296                                                         struct tm d_tm;
297                                                         char d_str[32];
298                                                         memset(&d_tm, 0, sizeof d_tm);
299                                                         d_tm.tm_year = t.year - 1900;
300                                                         d_tm.tm_mon = t.month - 1;
301                                                         d_tm.tm_mday = t.day;
302                                                         wc_strftime(d_str, sizeof d_str, "%x", &d_tm);
303                                                         wprintf("<i>%s</i> %s<br>",
304                                                                 _("Date:"), d_str);
305                                                 }
306                                                 else {
307                                                         tt = icaltime_as_timet(t);
308                                                         webcit_fmt_date(buf, tt, 1);
309                                                         wprintf("<i>%s</i> %s<br>",
310                                                                 _("Starting date/time:"), buf);
311                                                         
312                                                         /*
313                                                          * Embed the 'show end date/time' loop inside here so it
314                                                          * only executes if this is NOT an all day event.
315                                                          */
316                                                         q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
317                                                         if (q != NULL) {
318                                                                 t = icalproperty_get_dtend(q);
319                                                                 tt = icaltime_as_timet(t);
320                                                                 webcit_fmt_date(buf, tt, 1);
321                                                                 wprintf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
322                                                         }
323                                                         
324                                                 }
325                                         }
326                                         
327                                 }
328                                 
329                                 q = icalcomponent_get_first_property(Cal->cal, ICAL_DESCRIPTION_PROPERTY);
330                                 if (q) {
331                                         wprintf("<i>%s</i> ", _("Notes:"));
332                                         escputs((char *)icalproperty_get_comment(q));
333                                         wprintf("<br />");
334                                 }
335                                 
336                                 wprintf("\">");
337                                 escputs((char *)
338                                         icalproperty_get_comment(p));
339                                 wprintf("</a></font><br />\n");
340                                 
341                                 if (all_day_event) {
342                                         wprintf("</td></tr></table>");
343                                 }
344                                 
345                         }
346                         
347                 }
348                 
349                 
350         }
351         DeleteHashPos(&Pos);
352 }
353
354
355 /*
356  * Display one day of a whole month view of a calendar
357  */
358 void calendar_month_view_brief_events(time_t thetime, const char *daycolor) {
359         long hklen;
360         const char *HashKey;
361         void *vCal;
362         HashPos *Pos;
363         time_t event_tt;
364         time_t event_tts;
365         time_t event_tte;
366         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
367         struct tm event_tms;
368         struct tm event_tme;
369         struct tm today_tm;
370         icalproperty *p;
371         icalproperty *e;
372         struct icaltimetype t;
373         disp_cal *Cal;
374         int month, day, year;
375         int all_day_event = 0;
376         char *timeformat;
377         int time_format;
378         
379         time_format = get_time_format_cached ();
380
381         if (time_format == WC_TIMEFORMAT_24) timeformat="%k:%M";
382         else timeformat="%I:%M %p";
383
384         localtime_r(&thetime, &today_tm);
385         month = today_tm.tm_mon + 1;
386         day = today_tm.tm_mday;
387         year = today_tm.tm_year + 1900;
388
389         Pos = GetNewHashPos();
390         while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
391                 Cal = (disp_cal*)vCal;
392                 p = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
393                 if (p != NULL) {
394                         t = icalproperty_get_dtstart(p);
395                         event_tt = icaltime_as_timet(t);
396                         event_tts=event_tt;
397                         if (t.is_date) all_day_event = 1;
398                         else all_day_event = 0;
399
400                         if (all_day_event) {
401                                 gmtime_r(&event_tts, &event_tms);
402                         }
403                         else {
404                                 localtime_r(&event_tts, &event_tms);
405                         }
406                         /* \todo epoch &! daymask */
407                         if ((event_tms.tm_year == today_tm.tm_year)
408                                 && (event_tms.tm_mon == today_tm.tm_mon)
409                         && (event_tms.tm_mday == today_tm.tm_mday)) {
410                         
411                         
412                         char sbuf[255];
413                         char ebuf[255];
414                         
415                         p = icalcomponent_get_first_property(
416                                 Cal->cal,
417                                 ICAL_SUMMARY_PROPERTY);
418                         e = icalcomponent_get_first_property(
419                                 Cal->cal, 
420                                 ICAL_DTEND_PROPERTY);
421                         if ((p != NULL) && (e != NULL)) {
422                                 time_t difftime;
423                                 int hours, minutes;
424                                 t = icalproperty_get_dtend(e);
425                                 event_tte = icaltime_as_timet(t);
426                                 localtime_r(&event_tte, &event_tme);
427                                 difftime=(event_tte-event_tts)/60;
428                                 hours=(int)(difftime / 60);
429                                 minutes=difftime % 60;
430                                 wprintf("<tr><td bgcolor='%s'>%i:%2i</td><td bgcolor='%s'>"
431                                         "<font size=\"-1\">"
432                                         "<a class=\"event%s\" href=\"display_edit_event?msgnum=%ld?calview=calbrief?year=%s?month=%s?day=%s\">",
433                                         daycolor,
434                                         hours, minutes,
435                                         (Cal->unread)?"_unread":"_read",                                                
436                                         daycolor,
437                                         Cal->cal_msgnum,
438                                         bstr("year"),
439                                         bstr("month"),
440                                         bstr("day")
441                                         );
442                                 
443                                 escputs((char *)
444                                         icalproperty_get_comment(p));
445                                 /* \todo: allso ammitime format */
446                                 wc_strftime(&sbuf[0], sizeof(sbuf), timeformat, &event_tms);
447                                 wc_strftime(&ebuf[0], sizeof(sbuf), timeformat, &event_tme);
448                                 
449                                 wprintf("</a></font></td>"
450                                         "<td bgcolor='%s'>%s</td><td bgcolor='%s'>%s</td></tr>",
451                                         daycolor,
452                                         sbuf,
453                                         daycolor,
454                                         ebuf);
455                                 }
456                         
457                         }
458                         
459                         
460                 }
461         }
462         DeleteHashPos(&Pos);
463 }
464
465
466 /*
467  * view one month. pretty view
468  */
469 void calendar_month_view(int year, int month, int day) {
470         struct tm starting_tm;
471         struct tm tm;
472         time_t thetime;
473         int i;
474         time_t previous_month;
475         time_t next_month;
476         time_t colheader_time;
477         struct tm colheader_tm;
478         char colheader_label[32];
479         int chg_month = 0;
480         long weekstart = 0;
481
482         /*
483          * Determine what day to start.  If an impossible value is found, start on Sunday.
484          */
485         get_pref_long("weekstart", &weekstart, 17);
486         if (weekstart > 6) weekstart = 0;
487
488         /*
489          * Now back up to the 1st of the month...
490          */
491         memset(&starting_tm, 0, sizeof(struct tm));
492
493         starting_tm.tm_year = year - 1900;
494         starting_tm.tm_mon = month - 1;
495         starting_tm.tm_mday = day;
496         thetime = mktime(&starting_tm);
497
498         memcpy(&tm, &starting_tm, sizeof(struct tm));
499         while (tm.tm_mday != 1) {
500                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
501                 localtime_r(&thetime, &tm);
502         }
503
504         /* Determine previous and next months ... for links */
505         previous_month = thetime - (time_t)864000L;     /* back 10 days */
506         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
507
508         /* Now back up until we're on the user's preferred start day */
509         localtime_r(&thetime, &tm);
510         while (tm.tm_wday != weekstart) {
511                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
512                 localtime_r(&thetime, &tm);
513         }
514
515         /* Outer table (to get the background color) */
516         wprintf("<div class=\"fix_scrollbar_bug\">"
517                 "<table class=\"calendar\"> \n <tr><td>"); 
518
519         wprintf("<table width=100%% border=0 cellpadding=0 cellspacing=0><tr>\n");
520
521         wprintf("<td align=center>");
522
523         localtime_r(&previous_month, &tm);
524         wprintf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
525                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
526         wprintf("<img align=middle src=\"static/prevdate_32x.gif\" border=0></A>\n");
527
528         wc_strftime(colheader_label, sizeof colheader_label, "%B", &starting_tm);
529         wprintf("&nbsp;&nbsp;"
530                 "<font size=+1 color=\"#FFFFFF\">"
531                 "%s %d"
532                 "</font>"
533                 "&nbsp;&nbsp;", colheader_label, year);
534
535         localtime_r(&next_month, &tm);
536         wprintf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
537                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
538         wprintf("<img align=middle src=\"static/nextdate_32x.gif\" border=0></A>\n");
539
540         wprintf("</td></tr></table>\n");
541
542         /* Inner table (the real one) */
543         wprintf("<table width=100%% border=0 cellpadding=1 cellspacing=1 "
544                 "bgcolor=#204B78 id=\"inner_month\"><tr>");
545         colheader_time = thetime;
546         for (i=0; i<7; ++i) {
547                 colheader_time = thetime + (i * 86400) ;
548                 localtime_r(&colheader_time, &colheader_tm);
549                 wc_strftime(colheader_label, sizeof colheader_label, "%A", &colheader_tm);
550                 wprintf("<th align=center width=14%%>"
551                         "<font color=\"#FFFFFF\">%s</font></th>", colheader_label);
552
553         }
554         wprintf("</tr>\n");
555
556
557         /* Now do 35 or 42 days */
558         for (i = 0; i < 42; ++i) {
559                 localtime_r(&thetime, &tm);
560
561                 if ((i < 35) || (chg_month == 0)) {
562
563                         if ((i > 27) && ((tm.tm_mday == 1) || (tm.tm_mday == 31))) {
564                                 chg_month = 1;
565                         }
566                         if (i > 35) {
567                                 chg_month = 0;
568                         }
569
570                         /* Before displaying the first day of the week, start a new row */
571                         if ((i % 7) == 0) {
572                                 wprintf("<tr>");
573                         }
574
575                         wprintf("<td class=\"cal%s\"><div class=\"day\">",
576                                 ((tm.tm_mon != month-1) ? "out" :
577                                         ((tm.tm_wday==0 || tm.tm_wday==6) ? "weekend" :
578                                                 "day"))
579                                 );
580                         if ((i==0) || (tm.tm_mday == 1)) {
581                                 wc_strftime(colheader_label, sizeof colheader_label, "%B", &tm);
582                                 wprintf("%s ", colheader_label);
583                         }
584                         wprintf("<a href=\"readfwd?calview=day?year=%d?month=%d?day=%d\">"
585                                 "%d</a></div>",
586                                 tm.tm_year + 1900,
587                                 tm.tm_mon + 1,
588                                 tm.tm_mday,
589                                 tm.tm_mday);
590
591                         /* put the data here, stupid */
592                         calendar_month_view_display_events(
593                                 tm.tm_year + 1900,
594                                 tm.tm_mon + 1,
595                                 tm.tm_mday
596                                 );
597
598                         wprintf("</td>");
599
600                         /* After displaying the last day of the week, end the row */
601                         if ((i % 7) == 6) {
602                                 wprintf("</tr>\n");
603                         }
604
605                 }
606
607                 thetime += (time_t)86400;               /* ahead 24 hours */
608         }
609
610         wprintf("</table>"                      /* end of inner table */
611                 "</td></tr></table>"            /* end of outer table */
612                 "</div>\n");
613
614         /*
615          * Initialize the bubble tooltips.
616          *
617          * Yes, this is as stupid as it looks.  Instead of just making the call
618          * to btt_enableTooltips() straight away, we have to create a timer event
619          * and let it initialize as an event after 1 millisecond.  This is to
620          * work around a bug in Internet Explorer that causes it to crash if we
621          * manipulate the innerHTML of various DOM nodes while the page is still
622          * being rendered.  See http://www.shaftek.org/blog/archives/000212.html
623          * for more information.
624          */ 
625         StrBufAppendPrintf(WC->trailing_javascript,
626                 " setTimeout(\"btt_enableTooltips('inner_month')\", 1); \n"
627         );
628 }
629
630 /*
631  * view one month. brief view
632  */
633 void calendar_brief_month_view(int year, int month, int day) {
634         struct tm starting_tm;
635         struct tm tm;
636         time_t thetime;
637         int i;
638         time_t previous_month;
639         time_t next_month;
640         char month_label[32];
641
642         /* Determine what day to start.
643          * First, back up to the 1st of the month...
644          */
645         memset(&starting_tm, 0, sizeof(struct tm));
646         starting_tm.tm_year = year - 1900;
647         starting_tm.tm_mon = month - 1;
648         starting_tm.tm_mday = day;
649         thetime = mktime(&starting_tm);
650
651         memcpy(&tm, &starting_tm, sizeof(struct tm));
652         while (tm.tm_mday != 1) {
653                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
654                 localtime_r(&thetime, &tm);
655         }
656
657         /* Determine previous and next months ... for links */
658         previous_month = thetime - (time_t)864000L;     /* back 10 days */
659         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
660
661         /* Now back up until we're on a Sunday */
662         localtime_r(&thetime, &tm);
663         while (tm.tm_wday != 0) {
664                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
665                 localtime_r(&thetime, &tm);
666         }
667
668         /* Outer table (to get the background color) */
669         wprintf("<div class=\"fix_scrollbar_bug\">"
670                 "<table width=100%% border=0 cellpadding=0 cellspacing=0 "
671                 "bgcolor=#204B78><TR><TD>\n");
672
673         wprintf("<table width=100%% border=0 cellpadding=0 cellspacing=0><tr>\n");
674
675         wprintf("<td align=center>");
676
677         localtime_r(&previous_month, &tm);
678         wprintf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
679                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
680         wprintf("<img align=middle src=\"static/prevdate_32x.gif\" border=0></A>\n");
681
682         wc_strftime(month_label, sizeof month_label, "%B", &tm);
683         wprintf("&nbsp;&nbsp;"
684                 "<font size=+1 color=\"#FFFFFF\">"
685                 "%s %d"
686                 "</font>"
687                 "&nbsp;&nbsp;", month_label, year);
688
689         localtime_r(&next_month, &tm);
690         wprintf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
691                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
692         wprintf("<img align=middle src=\"static/nextdate_32x.gif\" border=0></A>\n");
693
694         wprintf("</td></tr></table>\n");
695
696         /* Inner table (the real one) */
697         wprintf("<table width=100%% border=0 cellpadding=1 cellspacing=1 "
698                 "bgcolor=#EEEECC><TR>");
699         wprintf("</tr>\n");
700         wprintf("<tr><td colspan=\"100%%\">\n");
701
702         /* Now do 35 days */
703         for (i = 0; i < 35; ++i) {
704                 char weeknumber[255];
705                 char weekday_name[32];
706                 char *daycolor;
707                 localtime_r(&thetime, &tm);
708
709
710                 /* Before displaying Sunday, start a new CELL */
711                 if ((i % 7) == 0) {
712                         wc_strftime(&weeknumber[0], sizeof(weeknumber), "%U", &tm);
713                         wprintf("<table border='0' bgcolor=\"#EEEECC\" width='100%%'> <tr><th colspan='4'>%s %s</th></tr>"
714                                 "   <tr><td>%s</td><td width=70%%>%s</td><td>%s</td><td>%s</td></tr>\n",
715                                 _("Week"), 
716                                 weeknumber,
717                                 _("Hours"),
718                                 _("Subject"),
719                                 _("Start"),
720                                 _("End")
721                                 );
722                 }
723                 
724                 daycolor=((tm.tm_mon != month-1) ? "DDDDDD" :
725                         ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
726                                 "FFFFFF"));
727                 
728                 /* Day Header */
729                 wc_strftime(weekday_name, sizeof weekday_name, "%A", &tm);
730                 wprintf("<tr><td bgcolor='%s' colspan='1' align='left'> %s,%i."
731                         "</td><td bgcolor='%s' colspan='3'><hr></td></tr>\n",
732                         daycolor,
733                         weekday_name,tm.tm_mday,
734                         daycolor);
735
736                 /* put the data of one day  here, stupid */
737                 calendar_month_view_brief_events(thetime, daycolor);
738
739
740                 /* After displaying Saturday, end the row */
741                 if ((i % 7) == 6) {
742                         wprintf("</td></tr></table>\n");
743                 }
744
745                 thetime += (time_t)86400;               /* ahead 24 hours */
746         }
747
748         wprintf("</table>"                      /* end of inner table */
749                 "</td></tr></table>"            /* end of outer table */
750                 "</div>\n");
751 }
752
753 /*
754  * Calendar week view -- not implemented yet, this is a stub function
755  */
756 void calendar_week_view(int year, int month, int day) {
757         wprintf("<center><i>week view FIXME</i></center><br />\n");
758 }
759
760
761 /*
762  * display one day
763  * Display events for a particular hour of a particular day.
764  * (Specify hour < 0 to show "all day" events)
765  *
766  * dstart and dend indicate which hours our "daytime" begins and end
767  */
768 void calendar_day_view_display_events(time_t thetime,
769         int year,
770         int month,
771         int day,
772         int notime_events,
773         int dstart,
774         int dend)
775 {
776         long hklen;
777         const char *HashKey;
778         void *vCal;
779         HashPos *Pos;
780         icalproperty *p = NULL;
781         icalproperty *q = NULL;
782         time_t event_tt;
783         time_t event_tte;
784         struct tm event_te;
785         struct tm event_tm;
786         int show_event = 0;
787         int all_day_event = 0;
788         int ongoing_event = 0;
789         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
790         disp_cal *Cal;
791         struct icaltimetype t;
792         struct icaltimetype end_t;
793         struct icaltimetype today_start_t;
794         struct icaltimetype today_end_t;
795         struct tm starting_tm;
796         struct tm ending_tm;
797         int top = 0;
798         int bottom = 0;
799         int gap = 1;
800         int startmin = 0;
801         int diffmin = 0;
802         int endmin = 0;
803
804         char buf[256];
805         struct tm d_tm;
806         char d_str[32];
807
808         if (GetCount(WCC->disp_cal_items) == 0) {
809                 /* nothing to display */
810                 return;
811         }
812
813         /* Create an imaginary event which spans the current day.  Any events which
814          * overlap with this one take place at least partially in this day.
815          */
816         memset(&starting_tm, 0, sizeof(struct tm));
817         starting_tm.tm_year = year - 1900;
818         starting_tm.tm_mon = month - 1;
819         starting_tm.tm_mday = day;
820         starting_tm.tm_hour = 0;
821         starting_tm.tm_min = 0;
822         today_start_t = icaltime_from_timet_with_zone(mktime(&starting_tm), 0, icaltimezone_get_utc_timezone());
823         today_start_t.is_utc = 1;
824
825         memset(&ending_tm, 0, sizeof(struct tm));
826         ending_tm.tm_year = year - 1900;
827         ending_tm.tm_mon = month - 1;
828         ending_tm.tm_mday = day;
829         ending_tm.tm_hour = 23;
830         ending_tm.tm_min = 59;
831         today_end_t = icaltime_from_timet_with_zone(mktime(&ending_tm), 0, icaltimezone_get_utc_timezone());
832         today_end_t.is_utc = 1;
833
834         /* Now loop through our list of events to see which ones occur today.
835          */
836         Pos = GetNewHashPos();
837         while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
838                 Cal = (disp_cal*)vCal;
839
840                 all_day_event = 0;
841                 ongoing_event=0;
842
843                 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
844                 if (q != NULL) {
845                         t = icalproperty_get_dtstart(q);
846                         event_tt = icaltime_as_timet(t);
847                         localtime_r(&event_tt, &event_te);
848                 }
849                 else {
850                         memset(&t, 0, sizeof t);
851                 }
852                 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
853                 if (q != NULL) {
854                         end_t = icalproperty_get_dtend(q);
855                         event_tte = icaltime_as_timet(end_t);
856                         localtime_r(&event_tte, &event_tm);
857                 }
858                 else {
859                         memset(&end_t, 0, sizeof end_t);
860                 }
861                 if (t.is_date) all_day_event = 1;
862
863                 if (all_day_event)
864                 {
865                         show_event = ((t.year == year) && (t.month == month) && (t.day == day) && (notime_events));
866                 }
867                 else
868                 {
869                         show_event = ical_ctdl_is_overlap(t, end_t, today_start_t, today_end_t);
870                 }
871
872                 /* If we determined that this event occurs today, then display it.
873                  */
874                 p = icalcomponent_get_first_property(Cal->cal,ICAL_SUMMARY_PROPERTY);
875
876                 if ((show_event) && (p != NULL)) {
877
878                         if ((event_te.tm_mday != day) || (event_tm.tm_mday != day)) ongoing_event = 1; 
879
880                         if (all_day_event && notime_events)
881                         {
882                                 wprintf("<li class=\"event_framed%s\"> "
883                                         "<a href=\"display_edit_event?"
884                                         "msgnum=%ld?calview=day?year=%d?month=%d?day=%d\" "
885                                         " class=\"event_title\" "
886                                         " btt_tooltext=\"",
887                                         (Cal->unread)?"_unread":"_read",
888                                         Cal->cal_msgnum, year, month, day);
889                                 wprintf("<i>%s</i><br />",      _("All day event"));
890                                 wprintf("<i>%s: %s</i><br />",  _("From"), Cal->from);
891                                 wprintf("<i>%s</i> ",           _("Summary:"));
892                                 escputs((char *) icalproperty_get_comment(p));
893                                 wprintf("<br />");
894                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_LOCATION_PROPERTY);
895                                 if (q) {
896                                         wprintf("<i>%s</i> ", _("Location:"));
897                                         escputs((char *)icalproperty_get_comment(q));
898                                         wprintf("<br />");
899                                                                 }
900                                 memset(&d_tm, 0, sizeof d_tm);
901                                 d_tm.tm_year = t.year - 1900;
902                                 d_tm.tm_mon = t.month - 1;
903                                 d_tm.tm_mday = t.day;
904                                 wc_strftime(d_str, sizeof d_str, "%x", &d_tm);
905                                 wprintf("<i>%s</i> %s<br>",_("Date:"), d_str);
906                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
907                                 if (q) {
908                                         wprintf("<i>%s</i> ", _("Notes:"));
909                                         escputs((char *)icalproperty_get_comment(q));
910                                         wprintf("<br />");
911                                 }
912                                 wprintf("\">");
913                                 escputs((char *) icalproperty_get_comment(p));
914                                 wprintf("</a> <span>(");
915                                 wprintf(_("All day event"));
916                                 wprintf(")</span></li>\n");
917                         }
918                         else if (ongoing_event && notime_events) 
919                         {
920                                 wprintf("<li class=\"event_framed%s\"> "
921                                         "<a href=\"display_edit_event?"
922                                         "msgnum=%ld&calview=day?year=%d?month=%d?day=%d\" "
923                                         " class=\"event_title\" " 
924                                         "btt_tooltext=\"",
925                                         (Cal->unread)?"_unread":"_read",
926                                         Cal->cal_msgnum, year, month, day);
927                                 wprintf("<i>%s</i><br />",     _("Ongoing event"));
928                                 wprintf("<i>%s: %s</i><br />", _("From"), Cal->from);
929                                 wprintf("<i>%s</i> ",          _("Summary:"));
930                                 escputs((char *) icalproperty_get_comment(p));
931                                 wprintf("<br />");
932                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_LOCATION_PROPERTY);
933                                 if (q) {
934                                         wprintf("<i>%s</i> ", _("Location:"));
935                                         escputs((char *)icalproperty_get_comment(q));
936                                         wprintf("<br />");
937                                                                 }
938                                 webcit_fmt_date(buf, event_tt, 1);
939                                 wprintf("<i>%s</i> %s<br>", _("Starting date/time:"), buf);
940                                 webcit_fmt_date(buf, event_tte, 1);
941                                 wprintf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
942                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
943                                 if (q) {
944                                         wprintf("<i>%s</i> ", _("Notes:"));
945                                         escputs((char *)icalproperty_get_comment(q));
946                                         wprintf("<br />");
947                                 }
948                                 wprintf("\">");
949                                 escputs((char *) icalproperty_get_comment(p));
950                                 wprintf("</a> <span>(");
951                                 wprintf(_("Ongoing event"));
952                                 wprintf(")</span></li>\n");
953                         }
954                         else if (!all_day_event && !notime_events)
955                         {
956                                 gap++;
957
958                                 if (event_te.tm_mday != day) event_te.tm_hour = 0;
959                                 if (event_tm.tm_mday != day) event_tm.tm_hour = 24;
960
961                                 /* Calculate the location of the top of the box */
962                                 if (event_te.tm_hour < dstart) {
963                                         startmin = diffmin = event_te.tm_min / 6;
964                                         top = (event_te.tm_hour * 10) + startmin;
965                                 }
966                                 else if ((event_te.tm_hour >= dstart) && (event_te.tm_hour <= dend)) {
967                                         startmin = diffmin = (event_te.tm_min / 2);
968                                         top = (dstart * 10) + ((event_te.tm_hour - dstart) * 30) + startmin;
969                                 }
970                                 else if (event_te.tm_hour >dend) {
971                                         startmin = diffmin = event_te.tm_min / 6;
972                                         top = (dstart * 10) + ((dend - dstart - 1) * 30) + ((event_tm.tm_hour - dend + 1) * 10) + startmin ;
973                                 }
974                                 else {
975                                         /* should never get here */
976                                 }
977
978                                 /* Calculate the location of the bottom of the box */
979                                 if (event_tm.tm_hour < dstart) {
980                                         endmin = diffmin = event_tm.tm_min / 6;
981                                         bottom = (event_tm.tm_hour * 10) + endmin;
982                                 }
983                                 else if ((event_tm.tm_hour >= dstart) && (event_tm.tm_hour <= dend)) {
984                                         endmin = diffmin = (event_tm.tm_min / 2);
985                                         bottom = (dstart * 10) + ((event_tm.tm_hour - dstart) * 30) + endmin ;
986                                 }
987                                 else if (event_tm.tm_hour >dend) {
988                                         endmin = diffmin = event_tm.tm_min / 6;
989                                         bottom = (dstart * 10) + ((dend - dstart + 1) * 30) + ((event_tm.tm_hour - dend - 1) * 10) + endmin;
990                                 }
991                                 else {
992                                         /* should never get here */
993                                 }
994
995                                 wprintf("<dd  class=\"event_framed%s\" "
996                                         "style=\"position: absolute; "
997                                         "top:%dpx; left:%dpx; "
998                                         "height:%dpx; \" >",
999                                         (Cal->unread)?"_unread":"_read",
1000                                         top, (gap * 40), (bottom-top)
1001                                         );
1002                                 wprintf("<a href=\"display_edit_event?"
1003                                         "msgnum=%ld?calview=day?year=%d?month=%d?day=%d?hour=%d\" "
1004                                         "class=\"event_title\" "
1005                                         "btt_tooltext=\"",
1006                                         Cal->cal_msgnum, year, month, day, t.hour);
1007                                 wprintf("<i>%s: %s</i><br />", _("From"), Cal->from);
1008                                 wprintf("<i>%s</i> ",          _("Summary:"));
1009                                 escputs((char *) icalproperty_get_comment(p));
1010                                 wprintf("<br />");
1011                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_LOCATION_PROPERTY);
1012                                 if (q) {
1013                                         wprintf("<i>%s</i> ", _("Location:"));
1014                                         escputs((char *)icalproperty_get_comment(q));
1015                                         wprintf("<br />");
1016                                                                 }
1017                                 webcit_fmt_date(buf, event_tt, 1);
1018                                 wprintf("<i>%s</i> %s<br>", _("Starting date/time:"), buf);
1019                                 webcit_fmt_date(buf, event_tte, 1);
1020                                 wprintf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
1021                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
1022                                 if (q) {
1023                                         wprintf("<i>%s</i> ", _("Notes:"));
1024                                         escputs((char *)icalproperty_get_comment(q));
1025                                         wprintf("<br />");
1026                                 }
1027                                 wprintf("\">");
1028
1029                                 escputs((char *) icalproperty_get_comment(p));
1030                                 wprintf("</a></dd>\n");
1031                         }
1032                 }
1033         }
1034         DeleteHashPos(&Pos);
1035 }
1036
1037 /*
1038  * view one day
1039  */
1040 void calendar_day_view(int year, int month, int day) {
1041         int hour;
1042         struct icaltimetype today, yesterday, tomorrow;
1043         long daystart;
1044         long dayend;
1045         struct tm d_tm;
1046         char d_str[128];
1047         int time_format;
1048         time_t today_t;
1049         int timeline = 30;
1050         int extratimeline = 0;
1051         int gap = 0;
1052
1053         time_format = get_time_format_cached ();
1054         get_pref_long("daystart", &daystart, 8);
1055         get_pref_long("dayend", &dayend, 17);
1056
1057         /* when loading daystart/dayend, replace missing, corrupt, or impossible values with defaults */
1058         if ((daystart < 0) || (dayend < 2) || (daystart >= 23) || (dayend > 23) || (dayend <= daystart)) {
1059                 daystart = 9;
1060                 dayend = 17;
1061         }
1062         
1063         /* Today's date */
1064         memset(&d_tm, 0, sizeof d_tm);
1065         d_tm.tm_year = year - 1900;
1066         d_tm.tm_mon = month - 1;
1067         d_tm.tm_mday = day;
1068         today_t = mktime(&d_tm); 
1069
1070         /* Figure out the dates for "yesterday" and "tomorrow" links */
1071
1072         memset(&today, 0, sizeof(struct icaltimetype));
1073         today.year = year;
1074         today.month = month;
1075         today.day = day;
1076         today.is_date = 1;
1077
1078         memcpy(&yesterday, &today, sizeof(struct icaltimetype));
1079         --yesterday.day;
1080         yesterday = icaltime_normalize(yesterday);
1081
1082         memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
1083         ++tomorrow.day;
1084         tomorrow = icaltime_normalize(tomorrow);
1085
1086         wprintf("<div class=\"fix_scrollbar_bug\">");
1087
1088         /* Inner table (the real one) */
1089         wprintf("<table class=\"calendar\" id=\"inner_day\"><tr> \n");
1090
1091         /* Innermost cell (contains hours etc.) */
1092         wprintf("<td class=\"events_of_the_day\" >");
1093         wprintf("<dl class=\"events\" >");
1094
1095         /* Now the middle of the day... */
1096
1097         extratimeline = timeline / 3;   
1098
1099         for (hour = 0; hour < daystart; ++hour) {       /* could do HEIGHT=xx */
1100                 wprintf("<dt class=\"extrahour\"        "
1101                         "style=\"               "
1102                         "position: absolute;    "
1103                         "top: %dpx; left: 0px;  "
1104                         "height: %dpx;          "       
1105                         "\" >                   "
1106                         "<a href=\"display_edit_event?msgnum=0"
1107                         "?calview=day?year=%d?month=%d?day=%d?hour=%d?minute=0\">",
1108                         (hour * extratimeline ), extratimeline, 
1109                         year, month, day, hour
1110                         );
1111
1112                 if (time_format == WC_TIMEFORMAT_24) {
1113                         wprintf("%2d:00</a> ", hour);
1114                 }
1115                 else {
1116                         wprintf("%d:00%s</a> ",
1117                                 (hour <= 12 ? hour : hour-12),
1118                                 (hour < 12 ? "am" : "pm")
1119                                 );
1120                 }
1121
1122                 wprintf("</dt>");
1123         }
1124
1125         gap = daystart * extratimeline;
1126
1127         for (hour = daystart; hour <= dayend; ++hour) {       /* could do HEIGHT=xx */
1128                 wprintf("<dt class=\"hour\"     "
1129                         "style=\"               "
1130                         "position: absolute;    "
1131                         "top: %ldpx; left: 0px;  "
1132                         "height: %dpx;          "
1133                         "\" >                   "
1134                         "<a href=\"display_edit_event?msgnum=0?calview=day"
1135                         "?year=%d?month=%d?day=%d?hour=%d?minute=0\">",
1136                         gap + ((hour - daystart) * timeline ), timeline,
1137                         year, month, day, hour
1138                         );
1139
1140                 if (time_format == WC_TIMEFORMAT_24) {
1141                         wprintf("%2d:00</a> ", hour);
1142                 }
1143                 else {
1144                         wprintf("%d:00%s</a> ",
1145                                 (hour <= 12 ? hour : hour-12),
1146                                 (hour < 12 ? "am" : "pm")
1147                                                 );
1148                 }
1149
1150                 wprintf("</dt>");
1151         }
1152
1153         gap = gap + ((dayend - daystart + 1) * timeline);
1154
1155         for (hour = (dayend + 1); hour < 24; ++hour) {       /* could do HEIGHT=xx */
1156                 wprintf("<dt class=\"extrahour\"     "
1157                         "style=\"               "
1158                         "position: absolute;    "
1159                         "top: %ldpx; left: 0px; "
1160                         "height: %dpx;          "
1161                         "\" >                   "
1162                         "<a href=\"display_edit_event?msgnum=0?calview=day"
1163                         "?year=%d?month=%d?day=%d?hour=%d?minute=0\">",
1164                         gap + ((hour - dayend - 1) * extratimeline ), extratimeline,
1165                         year, month, day, hour
1166                 );
1167
1168                 if (time_format == WC_TIMEFORMAT_24) {
1169                         wprintf("%2d:00</a> ", hour);
1170                 }
1171                 else {
1172                         wprintf("%d:00%s</a> ",
1173                                 (hour <= 12 ? hour : hour-12),
1174                                 (hour < 12 ? "am" : "pm")
1175                         );
1176                 }
1177
1178                 wprintf("</dt>");
1179         }
1180
1181         /* Display events with start and end times on this day */
1182         calendar_day_view_display_events(today_t, year, month, day, 0, daystart, dayend);
1183
1184         wprintf("</dl>");
1185         wprintf("</td>");                       /* end of innermost table */
1186
1187         /* Display extra events (start/end times not present or not today) in the middle column */
1188         wprintf("<td class=\"extra_events\">");
1189
1190         wprintf("<ul>");
1191
1192         /* Display all-day events */
1193         calendar_day_view_display_events(today_t, year, month, day, 1, daystart, dayend);
1194
1195         wprintf("</ul>");
1196
1197         wprintf("</td>");       /* end extra on the middle */
1198
1199         wprintf("<td width=20%% align=center valign=top>");     /* begin stuff-on-the-right */
1200
1201         /* Begin todays-date-with-left-and-right-arrows */
1202         wprintf("<table border=0 width=100%% "
1203                 "cellspacing=0 cellpadding=0 bgcolor=\"#FFFFFF\">\n");
1204         wprintf("<tr>");
1205
1206         /* Left arrow */        
1207         wprintf("<td align=center>");
1208         wprintf("<a href=\"readfwd?calview=day?year=%d?month=%d?day=%d\">",
1209                 yesterday.year, yesterday.month, yesterday.day);
1210         wprintf("<img align=middle src=\"static/prevdate_32x.gif\" border=0></A>");
1211         wprintf("</td>");
1212
1213         wc_strftime(d_str, sizeof d_str,
1214                 "<td align=center>"
1215                 "<font size=+2>%B</font><br />"
1216                 "<font size=+3>%d</font><br />"
1217                 "<font size=+2>%Y</font><br />"
1218                 "</td>",
1219                 &d_tm
1220                 );
1221         wprintf("%s", d_str);
1222
1223         /* Right arrow */
1224         wprintf("<td align=center>");
1225         wprintf("<a href=\"readfwd?calview=day?year=%d?month=%d?day=%d\">",
1226                 tomorrow.year, tomorrow.month, tomorrow.day);
1227         wprintf("<img align=middle src=\"static/nextdate_32x.gif\""
1228                 " border=0></a>\n");
1229         wprintf("</td>");
1230
1231         wprintf("</tr></table>\n");
1232         /* End todays-date-with-left-and-right-arrows */
1233
1234         /* Embed a mini month calendar in this space */
1235         wprintf("<br />\n");
1236         embeddable_mini_calendar(year, month, "readfwd?calview=day?year=%d?month=%d?day=%d");
1237
1238         wprintf("</font></center>\n");
1239
1240         wprintf("</td></tr>");                  /* end stuff-on-the-right */
1241
1242         wprintf("</table>"                      /* end of inner table */
1243                 "</div>");
1244
1245         StrBufAppendPrintf(WC->trailing_javascript,
1246                 " setTimeout(\"btt_enableTooltips('inner_day')\", 1);   \n"
1247         );
1248 }
1249
1250
1251 /*
1252  * Display today's events.
1253  */
1254 void calendar_summary_view(void) {
1255         long hklen;
1256         const char *HashKey;
1257         void *vCal;
1258         HashPos *Pos;
1259         disp_cal *Cal;
1260         icalproperty *p;
1261         struct icaltimetype t;
1262         time_t event_tt;
1263         struct tm event_tm;
1264         struct tm today_tm;
1265         time_t now;
1266         int all_day_event = 0;
1267         char timestring[SIZ];
1268         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
1269
1270         if (GetCount(WC->disp_cal_items) == 0) {
1271                 return;
1272         }
1273
1274         now = time(NULL);
1275         localtime_r(&now, &today_tm);
1276
1277         Pos = GetNewHashPos();
1278         while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
1279                 Cal = (disp_cal*)vCal;
1280                 p = icalcomponent_get_first_property(Cal->cal,
1281                                                 ICAL_DTSTART_PROPERTY);
1282                 if (p != NULL) {
1283                         t = icalproperty_get_dtstart(p);
1284                         event_tt = icaltime_as_timet(t);
1285                         if (t.is_date) {
1286                                 all_day_event = 1;
1287                         }
1288                         else {
1289                                 all_day_event = 0;
1290                         }
1291                         fmt_time(timestring, event_tt);
1292
1293                         if (all_day_event) {
1294                                 gmtime_r(&event_tt, &event_tm);
1295                         }
1296                         else {
1297                                 localtime_r(&event_tt, &event_tm);
1298                         }
1299
1300                         if ( (event_tm.tm_year == today_tm.tm_year)
1301                                 && (event_tm.tm_mon == today_tm.tm_mon)
1302                         && (event_tm.tm_mday == today_tm.tm_mday)
1303                         ) {
1304
1305
1306                         p = icalcomponent_get_first_property(
1307                                 Cal->cal,
1308                                 ICAL_SUMMARY_PROPERTY);
1309                         if (p != NULL) {
1310                                 escputs((char *)
1311                                         icalproperty_get_comment(p));
1312                                 wprintf(" (%s)<br />\n", timestring);
1313                         }
1314                         }
1315                 }
1316         }
1317         DeleteHashPos(&Pos);
1318         DeleteHash(&WC->disp_cal_items);
1319 }
1320
1321
1322 /*
1323  * do the whole calendar page
1324  * view any part of the calender. decide which way, etc.
1325  */
1326 void do_calendar_view(void) {
1327         time_t now;
1328         struct tm tm;
1329         int year, month, day;
1330         char calview[SIZ];
1331
1332         /* In case no date was specified, go with today */
1333         now = time(NULL);
1334         localtime_r(&now, &tm);
1335         year = tm.tm_year + 1900;
1336         month = tm.tm_mon + 1;
1337         day = tm.tm_mday;
1338
1339         /* Now see if a date was specified */
1340         if (havebstr("year")) year = ibstr("year");
1341         if (havebstr("month")) month = ibstr("month");
1342         if (havebstr("day")) day = ibstr("day");
1343
1344         /* How would you like that cooked? */
1345         if (havebstr("calview")) {
1346                 strcpy(calview, bstr("calview"));
1347         }
1348         else {
1349                 strcpy(calview, "month");
1350         }
1351
1352         /* Display the selected view */
1353         if (!strcasecmp(calview, "day")) {
1354                 calendar_day_view(year, month, day);
1355         }
1356         else if (!strcasecmp(calview, "week")) {
1357                 calendar_week_view(year, month, day);
1358         }
1359         else {
1360                 if (WC->wc_view == VIEW_CALBRIEF) {
1361                         calendar_brief_month_view(year, month, day);
1362                 }
1363                 else {
1364                         calendar_month_view(year, month, day);
1365                 }
1366         }
1367
1368         /* Free the in-memory list of calendar items */
1369         DeleteHash(&WC->disp_cal_items);
1370 }
1371
1372
1373 /*
1374  * Helper function for do_tasks_view().  Returns the due date/time of a vtodo.
1375  */
1376 time_t get_task_due_date(icalcomponent *vtodo) {
1377         icalproperty *p;
1378
1379         if (vtodo == NULL) {
1380                 return(0L);
1381         }
1382
1383         /*
1384          * If we're looking at a fully encapsulated VCALENDAR
1385          * rather than a VTODO component, recurse into the data
1386          * structure until we get a VTODO.
1387          */
1388         if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
1389                 return get_task_due_date(
1390                         icalcomponent_get_first_component(
1391                                 vtodo, ICAL_VTODO_COMPONENT
1392                                 )
1393                         );
1394         }
1395
1396         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
1397         if (p != NULL) {
1398                 return(icaltime_as_timet(icalproperty_get_due(p)));
1399         }
1400         else {
1401                 return(0L);
1402         }
1403 }
1404
1405
1406 /*
1407  * Compare the due dates of two tasks (this is for sorting)
1408  */
1409 int task_due_cmp(const void *vtask1, const void *vtask2) {
1410         disp_cal * Task1 = (disp_cal *)GetSearchPayload(vtask1);
1411         disp_cal * Task2 = (disp_cal *)GetSearchPayload(vtask2);
1412
1413         time_t t1;
1414         time_t t2;
1415
1416         t1 =  get_task_due_date(Task1->cal);
1417         t2 =  get_task_due_date(Task2->cal);
1418         if (t1 < t2) return(-1);
1419         if (t1 > t2) return(1);
1420         return(0);
1421 }
1422
1423 /*
1424  * qsort filter to move completed tasks to bottom of task list
1425  */
1426 int task_completed_cmp(const void *vtask1, const void *vtask2) {
1427         disp_cal * Task1 = (disp_cal *)GetSearchPayload(vtask1);
1428 //      disp_cal * Task2 = (disp_cal *)GetSearchPayload(vtask2);
1429
1430         icalproperty_status t1 = icalcomponent_get_status((Task1)->cal);
1431         // icalproperty_status t2 = icalcomponent_get_status(((struct disp_cal *)task2)->cal);
1432         
1433         if (t1 == ICAL_STATUS_COMPLETED) 
1434                 return 1;
1435         return 0;
1436 }
1437
1438
1439
1440 /*
1441  * do the whole task view stuff
1442  */
1443 void do_tasks_view(void) {
1444         long hklen;
1445         const char *HashKey;
1446         void *vCal;
1447         disp_cal *Cal;
1448         HashPos *Pos;
1449         int nItems;
1450         time_t due;
1451         char buf[SIZ];
1452         icalproperty *p;
1453         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
1454
1455         wprintf("<div class=\"fix_scrollbar_bug\">"
1456                 "<table class=\"calendar_view_background\"><tbody id=\"taskview\">\n<tr>\n"
1457                 "<th>");
1458         wprintf(_("Completed?"));
1459         wprintf("</th><th>");
1460         wprintf(_("Name of task"));
1461         wprintf("</th><th>");
1462         wprintf(_("Date due"));
1463         wprintf("</th><th>");
1464         wprintf(_("Category"));
1465         wprintf(" (<select id=\"selectcategory\"><option value=\"showall\">%s</option></select>)</th></tr>\n",
1466                 _("Show All"));
1467
1468         nItems = GetCount(WC->disp_cal_items);
1469
1470         /* Sort them if necessary
1471         if (nItems > 1) {
1472                 SortByPayload(WC->disp_cal_items, task_due_cmp);
1473         }
1474         * this shouldn't be neccessary, since we sort by the start time.
1475         */
1476
1477         /* And then again, by completed */
1478         if (nItems > 1) {
1479                 SortByPayload(WC->disp_cal_items, 
1480                               task_completed_cmp);
1481         }
1482
1483         Pos = GetNewHashPos();
1484         while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
1485                 Cal = (disp_cal*)vCal;
1486                 wprintf("<tr><td>");
1487                 icalproperty_status todoStatus = icalcomponent_get_status(Cal->cal);
1488                 wprintf("<input type=\"checkbox\" name=\"completed\" value=\"completed\" ");
1489                 if (todoStatus == ICAL_STATUS_COMPLETED) {
1490                         wprintf("checked=\"checked\" ");
1491                 }
1492                 wprintf("disabled=\"disabled\">\n</td><td>");
1493                 p = icalcomponent_get_first_property(Cal->cal,
1494                         ICAL_SUMMARY_PROPERTY);
1495                 wprintf("<a href=\"display_edit_task?msgnum=%ld&amp;taskrm=",
1496                         Cal->cal_msgnum );
1497                 urlescputs(WC->wc_roomname);
1498                 wprintf("\">");
1499                 /* wprintf("<img align=middle "
1500                 "src=\"static/taskmanag_16x.gif\" border=0>&nbsp;"); */
1501                 if (p != NULL) {
1502                         escputs((char *)icalproperty_get_comment(p));
1503                 }
1504                 wprintf("</a>\n");
1505                 wprintf("</td>\n");
1506
1507                 due = get_task_due_date(Cal->cal);
1508                 wprintf("<td><span");
1509                 if (due > 0) {
1510                         webcit_fmt_date(buf, due, 0);
1511                         wprintf(">%s",buf);
1512                 }
1513                 else {
1514                         wprintf(">");
1515                 }
1516                 wprintf("</span></td>");
1517                 wprintf("<td>");
1518                 p = icalcomponent_get_first_property(Cal->cal,
1519                         ICAL_CATEGORIES_PROPERTY);
1520                 if (p != NULL) {
1521                         escputs((char *)icalproperty_get_categories(p));
1522                 }
1523                 wprintf("</td>");
1524                 wprintf("</tr>");
1525         }
1526
1527         wprintf("</tbody></table></div>\n");
1528
1529         /* Free the list */
1530         DeleteHash(&WC->disp_cal_items);
1531         DeleteHashPos(&Pos);
1532 }
1533