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