* correct all GetNextHashPos() calls to have const chars
[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  * \brief  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  * \brief Display one day of a whole month view of a calendar
167  * \param thetime the month we want to see 
168  */
169 void calendar_month_view_display_events(int year, int month, int day)
170 {
171         long hklen;
172         const char *HashKey;
173         void *vCal;
174         HashPos *Pos;
175         disp_cal *Cal;
176         icalproperty *p = NULL;
177         icalproperty *q = NULL;
178         struct icaltimetype t;
179         struct icaltimetype end_t;
180         struct icaltimetype today_start_t;
181         struct icaltimetype today_end_t;
182         struct tm starting_tm;
183         struct tm ending_tm;
184         int all_day_event = 0;
185         int show_event = 0;
186         char buf[256];
187         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
188         time_t tt;
189
190         if (GetCount(WCC->disp_cal_items) == 0) {
191                 wprintf("<br /><br /><br />\n");
192                 return;
193         }
194
195         /**
196          * Create an imaginary event which spans the 24 hours of today.  Any events which
197          * overlap with this one take place at least partially in this day.  We have to
198          * convert it from a struct tm in order to make it UTC.
199          */
200         memset(&starting_tm, 0, sizeof(struct tm));
201         starting_tm.tm_year = year - 1900;
202         starting_tm.tm_mon = month - 1;
203         starting_tm.tm_mday = day;
204         starting_tm.tm_hour = 0;
205         starting_tm.tm_min = 0;
206         today_start_t = icaltime_from_timet_with_zone(mktime(&starting_tm), 0, icaltimezone_get_utc_timezone());
207         today_start_t.is_utc = 1;
208
209         memset(&ending_tm, 0, sizeof(struct tm));
210         ending_tm.tm_year = year - 1900;
211         ending_tm.tm_mon = month - 1;
212         ending_tm.tm_mday = day;
213         ending_tm.tm_hour = 23;
214         ending_tm.tm_min = 59;
215         today_end_t = icaltime_from_timet_with_zone(mktime(&ending_tm), 0, icaltimezone_get_utc_timezone());
216         today_end_t.is_utc = 1;
217
218         /**
219          * Now loop through our list of events to see which ones occur today.
220          */
221         Pos = GetNewHashPos();
222         while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
223                 Cal = (disp_cal*)vCal;
224                 all_day_event = 0;
225                 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
226                 if (q != NULL) {
227                         t = icalproperty_get_dtstart(q);
228                 }
229                 else {
230                         memset(&t, 0, sizeof t);
231                 }
232                 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
233                 if (q != NULL) {
234                         end_t = icalproperty_get_dtend(q);
235                 }
236                 else {
237                         memset(&end_t, 0, sizeof end_t);
238                 }
239                 if (t.is_date) all_day_event = 1;
240
241                 if (all_day_event)
242                 {
243                         show_event = ((t.year == year) && (t.month == month) && (t.day == day));
244                 }
245                 else
246                 {
247                         show_event = ical_ctdl_is_overlap(t, end_t, today_start_t, today_end_t);
248                 }
249
250                 /**
251                  * If we determined that this event occurs today, then display it.
252                  */
253                 if (show_event) {
254                         p = icalcomponent_get_first_property(Cal->cal, ICAL_SUMMARY_PROPERTY);
255                         if (p != NULL) {
256
257                                 if (all_day_event) {
258                                         wprintf("<table border=0 cellpadding=2><TR>"
259                                                 "<td bgcolor=\"#CCCCDD\">"
260                                                 );
261                                 }
262
263                                 wprintf("<font size=-1>"
264                                         "<a class=\"event%s\" href=\"display_edit_event?"
265                                         "msgnum=%ld&calview=month&year=%d&month=%d&day=%d\""
266                                         " btt_tooltext=\"",
267                                         (Cal->unread)?"_unread":"_read",
268                                         Cal->cal_msgnum,
269                                         year, month, day
270                                         );
271
272                                 wprintf("<i>%s: %s</i><br />", _("From"), Cal->from);
273                                 wprintf("<i>%s</i> ",          _("Summary:"));
274                                 escputs((char *)icalproperty_get_comment(p));
275                                 wprintf("<br />");
276                                 
277                                 q = icalcomponent_get_first_property(
278                                         Cal->cal,
279                                         ICAL_LOCATION_PROPERTY);
280                                 if (q) {
281                                         wprintf("<i>%s</i> ", _("Location:"));
282                                         escputs((char *)icalproperty_get_comment(q));
283                                         wprintf("<br />");
284                                 }
285                                 
286                                 /**
287                                  * Only show start/end times if we're actually looking at the VEVENT
288                                  * component.  Otherwise it shows bogus dates for e.g. timezones
289                                  */
290                                 if (icalcomponent_isa(Cal->cal) == ICAL_VEVENT_COMPONENT) {
291                                         
292                                         q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
293                                         if (q != NULL) {
294                                                 t = icalproperty_get_dtstart(q);
295                                                 
296                                                 if (t.is_date) {
297                                                         struct tm d_tm;
298                                                         char d_str[32];
299                                                         memset(&d_tm, 0, sizeof d_tm);
300                                                         d_tm.tm_year = t.year - 1900;
301                                                         d_tm.tm_mon = t.month - 1;
302                                                         d_tm.tm_mday = t.day;
303                                                         wc_strftime(d_str, sizeof d_str, "%x", &d_tm);
304                                                         wprintf("<i>%s</i> %s<br>",
305                                                                 _("Date:"), d_str);
306                                                 }
307                                                 else {
308                                                         tt = icaltime_as_timet(t);
309                                                         webcit_fmt_date(buf, tt, 1);
310                                                         wprintf("<i>%s</i> %s<br>",
311                                                                 _("Starting date/time:"), buf);
312                                                         
313                                                         /**
314                                                          * Embed the 'show end date/time' loop inside here so it
315                                                          * only executes if this is NOT an all day event.
316                                                          */
317                                                         q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
318                                                         if (q != NULL) {
319                                                                 t = icalproperty_get_dtend(q);
320                                                                 tt = icaltime_as_timet(t);
321                                                                 webcit_fmt_date(buf, tt, 1);
322                                                                 wprintf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
323                                                         }
324                                                         
325                                                 }
326                                         }
327                                         
328                                 }
329                                 
330                                 q = icalcomponent_get_first_property(Cal->cal, ICAL_DESCRIPTION_PROPERTY);
331                                 if (q) {
332                                         wprintf("<i>%s</i> ", _("Notes:"));
333                                         escputs((char *)icalproperty_get_comment(q));
334                                         wprintf("<br />");
335                                 }
336                                 
337                                 wprintf("\">");
338                                 escputs((char *)
339                                         icalproperty_get_comment(p));
340                                 wprintf("</a></font><br />\n");
341                                 
342                                 if (all_day_event) {
343                                         wprintf("</td></tr></table>");
344                                 }
345                                 
346                         }
347                         
348                 }
349                 
350                 
351         }
352         DeleteHashPos(&Pos);
353 }
354
355
356 /**
357  * \brief Display one day of a whole month view of a calendar
358  * \param thetime the month we want to see 
359  */
360 void calendar_month_view_brief_events(time_t thetime, const char *daycolor) {
361         long hklen;
362         const char *HashKey;
363         void *vCal;
364         HashPos *Pos;
365         time_t event_tt;
366         time_t event_tts;
367         time_t event_tte;
368         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
369         struct tm event_tms;
370         struct tm event_tme;
371         struct tm today_tm;
372         icalproperty *p;
373         icalproperty *e;
374         struct icaltimetype t;
375         disp_cal *Cal;
376         int month, day, year;
377         int all_day_event = 0;
378         char *timeformat;
379         int time_format;
380         
381         time_format = get_time_format_cached ();
382
383         if (time_format == WC_TIMEFORMAT_24) timeformat="%k:%M";
384         else timeformat="%I:%M %p";
385
386         localtime_r(&thetime, &today_tm);
387         month = today_tm.tm_mon + 1;
388         day = today_tm.tm_mday;
389         year = today_tm.tm_year + 1900;
390
391         Pos = GetNewHashPos();
392         while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
393                 Cal = (disp_cal*)vCal;
394                 p = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
395                 if (p != NULL) {
396                         t = icalproperty_get_dtstart(p);
397                         event_tt = icaltime_as_timet(t);
398                         event_tts=event_tt;
399                         if (t.is_date) all_day_event = 1;
400                         else all_day_event = 0;
401
402                         if (all_day_event) {
403                                 gmtime_r(&event_tts, &event_tms);
404                         }
405                         else {
406                                 localtime_r(&event_tts, &event_tms);
407                         }
408                         /* \todo epoch &! daymask */
409                         if ((event_tms.tm_year == today_tm.tm_year)
410                                 && (event_tms.tm_mon == today_tm.tm_mon)
411                         && (event_tms.tm_mday == today_tm.tm_mday)) {
412                         
413                         
414                         char sbuf[255];
415                         char ebuf[255];
416                         
417                         p = icalcomponent_get_first_property(
418                                 Cal->cal,
419                                 ICAL_SUMMARY_PROPERTY);
420                         e = icalcomponent_get_first_property(
421                                 Cal->cal, 
422                                 ICAL_DTEND_PROPERTY);
423                         if ((p != NULL) && (e != NULL)) {
424                                 time_t difftime;
425                                 int hours, minutes;
426                                 t = icalproperty_get_dtend(e);
427                                 event_tte = icaltime_as_timet(t);
428                                 localtime_r(&event_tte, &event_tme);
429                                 difftime=(event_tte-event_tts)/60;
430                                 hours=(int)(difftime / 60);
431                                 minutes=difftime % 60;
432                                 wprintf("<tr><td bgcolor='%s'>%i:%2i</td><td bgcolor='%s'>"
433                                         "<font size=-1>"
434                                         "<a class=\"event%s\" href=\"display_edit_event?msgnum=%ld&calview=calbrief&year=%s&month=%s&day=%s\">",
435                                         daycolor,
436                                         hours, minutes,
437                                         (Cal->unread)?"_unread":"_read",                                                
438                                         daycolor,
439                                         Cal->cal_msgnum,
440                                         bstr("year"),
441                                         bstr("month"),
442                                         bstr("day")
443                                         );
444                                 
445                                 escputs((char *)
446                                         icalproperty_get_comment(p));
447                                 /* \todo: allso ammitime format */
448                                 wc_strftime(&sbuf[0], sizeof(sbuf), timeformat, &event_tms);
449                                 wc_strftime(&ebuf[0], sizeof(sbuf), timeformat, &event_tme);
450                                 
451                                 wprintf("</a></font></td>"
452                                         "<td bgcolor='%s'>%s</td><td bgcolor='%s'>%s</td></tr>",
453                                         daycolor,
454                                         sbuf,
455                                         daycolor,
456                                         ebuf);
457                                 }
458                         
459                         }
460                         
461                         
462                 }
463         }
464         DeleteHashPos(&Pos);
465 }
466
467
468 /*
469  * view one month. pretty view
470  */
471 void calendar_month_view(int year, int month, int day) {
472         struct tm starting_tm;
473         struct tm tm;
474         time_t thetime;
475         int i;
476         time_t previous_month;
477         time_t next_month;
478         time_t colheader_time;
479         struct tm colheader_tm;
480         char colheader_label[32];
481         int chg_month = 0;
482         long weekstart = 0;
483
484         /*
485          * Determine what day to start.  If an impossible value is found, start on Sunday.
486          */
487         get_pref_long("weekstart", &weekstart, 17);
488         if (weekstart > 6) weekstart = 0;
489
490         /*
491          * Now back up to the 1st of the month...
492          */
493         memset(&starting_tm, 0, sizeof(struct tm));
494
495         starting_tm.tm_year = year - 1900;
496         starting_tm.tm_mon = month - 1;
497         starting_tm.tm_mday = day;
498         thetime = mktime(&starting_tm);
499
500         memcpy(&tm, &starting_tm, sizeof(struct tm));
501         while (tm.tm_mday != 1) {
502                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
503                 localtime_r(&thetime, &tm);
504         }
505
506         /* Determine previous and next months ... for links */
507         previous_month = thetime - (time_t)864000L;     /* back 10 days */
508         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
509
510         /* Now back up until we're on the user's preferred start day */
511         localtime_r(&thetime, &tm);
512         while (tm.tm_wday != weekstart) {
513                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
514                 lprintf(9, "Subtracting a day, thetime is now %d - %s", thetime, ctime(&thetime));
515                 localtime_r(&thetime, &tm);
516                 lprintf(9, "tm.tm_wday is %d, weekstart is %d\n", tm.tm_wday, weekstart);
517         }
518
519         /* Outer table (to get the background color) */
520         wprintf("<div class=\"fix_scrollbar_bug\">"
521                 "<table class=\"calendar\"> \n <tr><td>"); 
522
523         wprintf("<table width=100%% border=0 cellpadding=0 cellspacing=0><tr>\n");
524
525         wprintf("<td align=center>");
526
527         localtime_r(&previous_month, &tm);
528         wprintf("<a href=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
529                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
530         wprintf("<img align=middle src=\"static/prevdate_32x.gif\" border=0></A>\n");
531
532         wc_strftime(colheader_label, sizeof colheader_label, "%B", &starting_tm);
533         wprintf("&nbsp;&nbsp;"
534                 "<font size=+1 color=\"#FFFFFF\">"
535                 "%s %d"
536                 "</font>"
537                 "&nbsp;&nbsp;", colheader_label, year);
538
539         localtime_r(&next_month, &tm);
540         wprintf("<a href=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
541                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
542         wprintf("<img align=middle src=\"static/nextdate_32x.gif\" border=0></A>\n");
543
544         wprintf("</td></tr></table>\n");
545
546         /* Inner table (the real one) */
547         wprintf("<table width=100%% border=0 cellpadding=1 cellspacing=1 "
548                 "bgcolor=#204B78 id=\"inner_month\"><tr>");
549         colheader_time = thetime;
550         for (i=0; i<7; ++i) {
551                 colheader_time = thetime + (i * 86400) ;
552                 localtime_r(&colheader_time, &colheader_tm);
553                 wc_strftime(colheader_label, sizeof colheader_label, "%A", &colheader_tm);
554                 wprintf("<th align=center width=14%%>"
555                         "<font color=\"#FFFFFF\">%s</font></th>", colheader_label);
556
557         }
558         wprintf("</tr>\n");
559
560
561         /* Now do 35 or 42 days */
562         for (i = 0; i < 42; ++i) {
563                 localtime_r(&thetime, &tm);
564
565                 if ((i < 35) || (chg_month == 0)) {
566
567                         if ((i > 27) && ((tm.tm_mday == 1) || (tm.tm_mday == 31))) {
568                                 chg_month = 1;
569                         }
570                         if (i > 35) {
571                                 chg_month = 0;
572                         }
573
574                         /* Before displaying the first day of the week, start a new row */
575                         if ((i % 7) == 0) {
576                                 wprintf("<tr>");
577                         }
578
579                         wprintf("<td class=\"cal%s\"><div class=\"day\">",
580                                 ((tm.tm_mon != month-1) ? "out" :
581                                         ((tm.tm_wday==0 || tm.tm_wday==6) ? "weekend" :
582                                                 "day"))
583                                 );
584                         if ((i==0) || (tm.tm_mday == 1)) {
585                                 wc_strftime(colheader_label, sizeof colheader_label, "%B", &tm);
586                                 wprintf("%s ", colheader_label);
587                         }
588                         wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
589                                 "%d</a></div>",
590                                 tm.tm_year + 1900,
591                                 tm.tm_mon + 1,
592                                 tm.tm_mday,
593                                 tm.tm_mday);
594
595                         /** put the data here, stupid */
596                         calendar_month_view_display_events(
597                                 tm.tm_year + 1900,
598                                 tm.tm_mon + 1,
599                                 tm.tm_mday
600                                 );
601
602                         wprintf("</td>");
603
604                         /* After displaying the last day of the week, end the row */
605                         if ((i % 7) == 6) {
606                                 wprintf("</tr>\n");
607                         }
608
609                 }
610
611                 thetime += (time_t)86400;               /* ahead 24 hours */
612         }
613
614         wprintf("</table>"                      /* end of inner table */
615                 "</td></tr></table>"            /* end of outer table */
616                 "</div>\n");
617
618         /*
619          * Initialize the bubble tooltips.
620          *
621          * Yes, this is as stupid as it looks.  Instead of just making the call
622          * to btt_enableTooltips() straight away, we have to create a timer event
623          * and let it initialize as an event after 1 millisecond.  This is to
624          * work around a bug in Internet Explorer that causes it to crash if we
625          * manipulate the innerHTML of various DOM nodes while the page is still
626          * being rendered.  See http://www.shaftek.org/blog/archives/000212.html
627          * for more information.
628          */ 
629         wprintf("<script type=\"text/javascript\">"
630                 " setTimeout(\"btt_enableTooltips('inner_month')\", 1); "
631                 "</script>\n"
632         );
633 }
634
635 /*
636  * view one month. brief view
637  */
638 void calendar_brief_month_view(int year, int month, int day) {
639         struct tm starting_tm;
640         struct tm tm;
641         time_t thetime;
642         int i;
643         time_t previous_month;
644         time_t next_month;
645         char month_label[32];
646
647         /* Determine what day to start.
648          * First, back up to the 1st of the month...
649          */
650         memset(&starting_tm, 0, sizeof(struct tm));
651         starting_tm.tm_year = year - 1900;
652         starting_tm.tm_mon = month - 1;
653         starting_tm.tm_mday = day;
654         thetime = mktime(&starting_tm);
655
656         memcpy(&tm, &starting_tm, sizeof(struct tm));
657         while (tm.tm_mday != 1) {
658                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
659                 localtime_r(&thetime, &tm);
660         }
661
662         /* Determine previous and next months ... for links */
663         previous_month = thetime - (time_t)864000L;     /* back 10 days */
664         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
665
666         /* Now back up until we're on a Sunday */
667         localtime_r(&thetime, &tm);
668         while (tm.tm_wday != 0) {
669                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
670                 localtime_r(&thetime, &tm);
671         }
672
673         /* Outer table (to get the background color) */
674         wprintf("<div class=\"fix_scrollbar_bug\">"
675                 "<table width=100%% border=0 cellpadding=0 cellspacing=0 "
676                 "bgcolor=#204B78><TR><TD>\n");
677
678         wprintf("<table width=100%% border=0 cellpadding=0 cellspacing=0><tr>\n");
679
680         wprintf("<td align=center>");
681
682         localtime_r(&previous_month, &tm);
683         wprintf("<a href=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
684                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
685         wprintf("<img align=middle src=\"static/prevdate_32x.gif\" border=0></A>\n");
686
687         wc_strftime(month_label, sizeof month_label, "%B", &tm);
688         wprintf("&nbsp;&nbsp;"
689                 "<font size=+1 color=\"#FFFFFF\">"
690                 "%s %d"
691                 "</font>"
692                 "&nbsp;&nbsp;", month_label, year);
693
694         localtime_r(&next_month, &tm);
695         wprintf("<a href=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
696                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
697         wprintf("<img align=middle src=\"static/nextdate_32x.gif\" border=0></A>\n");
698
699         wprintf("</td></tr></table>\n");
700
701         /* Inner table (the real one) */
702         wprintf("<table width=100%% border=0 cellpadding=1 cellspacing=1 "
703                 "bgcolor=#EEEECC><TR>");
704         wprintf("</tr>\n");
705         wprintf("<tr><td colspan=\"100%%\">\n");
706
707         /* Now do 35 days */
708         for (i = 0; i < 35; ++i) {
709                 char weeknumber[255];
710                 char weekday_name[32];
711                 char *daycolor;
712                 localtime_r(&thetime, &tm);
713
714
715                 /* Before displaying Sunday, start a new CELL */
716                 if ((i % 7) == 0) {
717                         wc_strftime(&weeknumber[0], sizeof(weeknumber), "%U", &tm);
718                         wprintf("<table border='0' bgcolor=\"#EEEECC\" width='100%%'> <tr><th colspan='4'>%s %s</th></tr>"
719                                 "   <tr><td>%s</td><td width=70%%>%s</td><td>%s</td><td>%s</td></tr>\n",
720                                 _("Week"), 
721                                 weeknumber,
722                                 _("Hours"),
723                                 _("Subject"),
724                                 _("Start"),
725                                 _("End")
726                                 );
727                 }
728                 
729                 daycolor=((tm.tm_mon != month-1) ? "DDDDDD" :
730                         ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
731                                 "FFFFFF"));
732                 
733                 /* Day Header */
734                 wc_strftime(weekday_name, sizeof weekday_name, "%A", &tm);
735                 wprintf("<tr><td bgcolor='%s' colspan='1' align='left'> %s,%i."
736                         "</td><td bgcolor='%s' colspan='3'><hr></td></tr>\n",
737                         daycolor,
738                         weekday_name,tm.tm_mday,
739                         daycolor);
740
741                 /* put the data of one day  here, stupid */
742                 calendar_month_view_brief_events(thetime, daycolor);
743
744
745                 /* After displaying Saturday, end the row */
746                 if ((i % 7) == 6) {
747                         wprintf("</td></tr></table>\n");
748                 }
749
750                 thetime += (time_t)86400;               /** ahead 24 hours */
751         }
752
753         wprintf("</table>"                      /** end of inner table */
754                 "</td></tr></table>"            /** end of outer table */
755                 "</div>\n");
756 }
757
758 /*
759  * Calendar week view -- not implemented yet, this is a stub function
760  */
761 void calendar_week_view(int year, int month, int day) {
762         wprintf("<center><i>week view FIXME</i></center><br />\n");
763 }
764
765
766 /*
767  * display one day
768  * Display events for a particular hour of a particular day.
769  * (Specify hour < 0 to show "all day" events)
770  *
771  * dstart and dend indicate which hours our "daytime" begins and end
772  */
773 void calendar_day_view_display_events(time_t thetime,
774         int year,
775         int month,
776         int day,
777         int notime_events,
778         int dstart,
779         int dend)
780 {
781         long hklen;
782         const char *HashKey;
783         void *vCal;
784         HashPos *Pos;
785         icalproperty *p = NULL;
786         icalproperty *q = NULL;
787         time_t event_tt;
788         time_t event_tte;
789         struct tm event_te;
790         struct tm event_tm;
791         int show_event = 0;
792         int all_day_event = 0;
793         int ongoing_event = 0;
794         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
795         disp_cal *Cal;
796         struct icaltimetype t;
797         struct icaltimetype end_t;
798         struct icaltimetype today_start_t;
799         struct icaltimetype today_end_t;
800         struct tm starting_tm;
801         struct tm ending_tm;
802         int top = 0;
803         int bottom = 0;
804         int gap = 1;
805         int startmin = 0;
806         int diffmin = 0;
807         int endmin = 0;
808
809         char buf[256];
810         struct tm d_tm;
811         char d_str[32];
812
813         if (GetCount(WCC->disp_cal_items) == 0) {
814                 /* nothing to display */
815                 return;
816         }
817
818         /* Create an imaginary event which spans the current day.  Any events which
819          * overlap with this one take place at least partially in this day.
820          */
821         memset(&starting_tm, 0, sizeof(struct tm));
822         starting_tm.tm_year = year - 1900;
823         starting_tm.tm_mon = month - 1;
824         starting_tm.tm_mday = day;
825         starting_tm.tm_hour = 0;
826         starting_tm.tm_min = 0;
827         today_start_t = icaltime_from_timet_with_zone(mktime(&starting_tm), 0, icaltimezone_get_utc_timezone());
828         today_start_t.is_utc = 1;
829
830         memset(&ending_tm, 0, sizeof(struct tm));
831         ending_tm.tm_year = year - 1900;
832         ending_tm.tm_mon = month - 1;
833         ending_tm.tm_mday = day;
834         ending_tm.tm_hour = 23;
835         ending_tm.tm_min = 59;
836         today_end_t = icaltime_from_timet_with_zone(mktime(&ending_tm), 0, icaltimezone_get_utc_timezone());
837         today_end_t.is_utc = 1;
838
839         /* Now loop through our list of events to see which ones occur today.
840          */
841         Pos = GetNewHashPos();
842         while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
843                 Cal = (disp_cal*)vCal;
844
845                 all_day_event = 0;
846                 ongoing_event=0;
847
848                 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
849                 if (q != NULL) {
850                         t = icalproperty_get_dtstart(q);
851                         event_tt = icaltime_as_timet(t);
852                         localtime_r(&event_tt, &event_te);
853                 }
854                 else {
855                         memset(&t, 0, sizeof t);
856                 }
857                 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
858                 if (q != NULL) {
859                         end_t = icalproperty_get_dtend(q);
860                         event_tte = icaltime_as_timet(end_t);
861                         localtime_r(&event_tte, &event_tm);
862                 }
863                 else {
864                         memset(&end_t, 0, sizeof end_t);
865                 }
866                 if (t.is_date) all_day_event = 1;
867
868                 if (all_day_event)
869                 {
870                         show_event = ((t.year == year) && (t.month == month) && (t.day == day) && (notime_events));
871                 }
872                 else
873                 {
874                         show_event = ical_ctdl_is_overlap(t, end_t, today_start_t, today_end_t);
875                 }
876
877                 /* If we determined that this event occurs today, then display it.
878                  */
879                 p = icalcomponent_get_first_property(Cal->cal,ICAL_SUMMARY_PROPERTY);
880
881                 if ((show_event) && (p != NULL)) {
882
883                         if ((event_te.tm_mday != day) || (event_tm.tm_mday != day)) ongoing_event = 1; 
884
885                         if (all_day_event && notime_events)
886                         {
887                                 wprintf("<li class=\"event_framed%s\"> "
888                                         "<a href=\"display_edit_event?"
889                                         "msgnum=%ld&calview=day&year=%d&month=%d&day=%d\" "
890                                         " class=\"event_title\" "
891                                         " btt_tooltext=\"",
892                                         (Cal->unread)?"_unread":"_read",
893                                         Cal->cal_msgnum, year, month, day);
894                                 wprintf("<i>%s</i><br />",      _("All day event"));
895                                 wprintf("<i>%s: %s</i><br />",  _("From"), Cal->from);
896                                 wprintf("<i>%s</i> ",           _("Summary:"));
897                                 escputs((char *) icalproperty_get_comment(p));
898                                 wprintf("<br />");
899                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_LOCATION_PROPERTY);
900                                 if (q) {
901                                         wprintf("<i>%s</i> ", _("Location:"));
902                                         escputs((char *)icalproperty_get_comment(q));
903                                         wprintf("<br />");
904                                                                 }
905                                 memset(&d_tm, 0, sizeof d_tm);
906                                 d_tm.tm_year = t.year - 1900;
907                                 d_tm.tm_mon = t.month - 1;
908                                 d_tm.tm_mday = t.day;
909                                 wc_strftime(d_str, sizeof d_str, "%x", &d_tm);
910                                 wprintf("<i>%s</i> %s<br>",_("Date:"), d_str);
911                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
912                                 if (q) {
913                                         wprintf("<i>%s</i> ", _("Notes:"));
914                                         escputs((char *)icalproperty_get_comment(q));
915                                         wprintf("<br />");
916                                 }
917                                 wprintf("\">");
918                                 escputs((char *) icalproperty_get_comment(p));
919                                 wprintf("</a> <span>(");
920                                 wprintf(_("All day event"));
921                                 wprintf(")</span></li>\n");
922                         }
923                         else if (ongoing_event && notime_events) 
924                         {
925                                 wprintf("<li class=\"event_framed%s\"> "
926                                         "<a href=\"display_edit_event?"
927                                         "msgnum=%ld&calview=day&year=%d&month=%d&day=%d\" "
928                                         " class=\"event_title\" " 
929                                         "btt_tooltext=\"",
930                                         (Cal->unread)?"_unread":"_read",
931                                         Cal->cal_msgnum, year, month, day);
932                                 wprintf("<i>%s</i><br />",     _("Ongoing event"));
933                                 wprintf("<i>%s: %s</i><br />", _("From"), Cal->from);
934                                 wprintf("<i>%s</i> ",          _("Summary:"));
935                                 escputs((char *) icalproperty_get_comment(p));
936                                 wprintf("<br />");
937                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_LOCATION_PROPERTY);
938                                 if (q) {
939                                         wprintf("<i>%s</i> ", _("Location:"));
940                                         escputs((char *)icalproperty_get_comment(q));
941                                         wprintf("<br />");
942                                                                 }
943                                 webcit_fmt_date(buf, event_tt, 1);
944                                 wprintf("<i>%s</i> %s<br>", _("Starting date/time:"), buf);
945                                 webcit_fmt_date(buf, event_tte, 1);
946                                 wprintf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
947                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
948                                 if (q) {
949                                         wprintf("<i>%s</i> ", _("Notes:"));
950                                         escputs((char *)icalproperty_get_comment(q));
951                                         wprintf("<br />");
952                                 }
953                                 wprintf("\">");
954                                 escputs((char *) icalproperty_get_comment(p));
955                                 wprintf("</a> <span>(");
956                                 wprintf(_("Ongoing event"));
957                                 wprintf(")</span></li>\n");
958                         }
959                         else if (!all_day_event && !notime_events)
960                         {
961                                 gap++;
962
963                                 if (event_te.tm_mday != day) event_te.tm_hour = 0;
964                                 if (event_tm.tm_mday != day) event_tm.tm_hour = 24;
965
966                                 /* Calculate the location of the top of the box */
967                                 if (event_te.tm_hour < dstart) {
968                                         startmin = diffmin = event_te.tm_min / 6;
969                                         top = (event_te.tm_hour * 10) + startmin;
970                                 }
971                                 else if ((event_te.tm_hour >= dstart) && (event_te.tm_hour <= dend)) {
972                                         startmin = diffmin = (event_te.tm_min / 2);
973                                         top = (dstart * 10) + ((event_te.tm_hour - dstart) * 30) + startmin;
974                                 }
975                                 else if (event_te.tm_hour >dend) {
976                                         startmin = diffmin = event_te.tm_min / 6;
977                                         top = (dstart * 10) + ((dend - dstart - 1) * 30) + ((event_tm.tm_hour - dend + 1) * 10) + startmin ;
978                                 }
979                                 else {
980                                         /* should never get here */
981                                 }
982
983                                 /* Calculate the location of the bottom of the box */
984                                 if (event_tm.tm_hour < dstart) {
985                                         endmin = diffmin = event_tm.tm_min / 6;
986                                         bottom = (event_tm.tm_hour * 10) + endmin;
987                                 }
988                                 else if ((event_tm.tm_hour >= dstart) && (event_tm.tm_hour <= dend)) {
989                                         endmin = diffmin = (event_tm.tm_min / 2);
990                                         bottom = (dstart * 10) + ((event_tm.tm_hour - dstart) * 30) + endmin ;
991                                 }
992                                 else if (event_tm.tm_hour >dend) {
993                                         endmin = diffmin = event_tm.tm_min / 6;
994                                         bottom = (dstart * 10) + ((dend - dstart + 1) * 30) + ((event_tm.tm_hour - dend - 1) * 10) + endmin;
995                                 }
996                                 else {
997                                         /* should never get here */
998                                 }
999
1000                                 wprintf("<dd  class=\"event_framed%s\" "
1001                                         "style=\"position: absolute; "
1002                                         "top:%dpx; left:%dpx; "
1003                                         "height:%dpx; \" >",
1004                                         (Cal->unread)?"_unread":"_read",
1005                                         top, (gap * 40), (bottom-top)
1006                                         );
1007                                 wprintf("<a href=\"display_edit_event?"
1008                                         "msgnum=%ld&calview=day&year=%d&month=%d&day=%d&hour=%d\" "
1009                                         "class=\"event_title\" "
1010                                         "btt_tooltext=\"",
1011                                         Cal->cal_msgnum, year, month, day, t.hour);
1012                                 wprintf("<i>%s: %s</i><br />", _("From"), Cal->from);
1013                                 wprintf("<i>%s</i> ",          _("Summary:"));
1014                                 escputs((char *) icalproperty_get_comment(p));
1015                                 wprintf("<br />");
1016                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_LOCATION_PROPERTY);
1017                                 if (q) {
1018                                         wprintf("<i>%s</i> ", _("Location:"));
1019                                         escputs((char *)icalproperty_get_comment(q));
1020                                         wprintf("<br />");
1021                                                                 }
1022                                 webcit_fmt_date(buf, event_tt, 1);
1023                                 wprintf("<i>%s</i> %s<br>", _("Starting date/time:"), buf);
1024                                 webcit_fmt_date(buf, event_tte, 1);
1025                                 wprintf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
1026                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
1027                                 if (q) {
1028                                         wprintf("<i>%s</i> ", _("Notes:"));
1029                                         escputs((char *)icalproperty_get_comment(q));
1030                                         wprintf("<br />");
1031                                 }
1032                                 wprintf("\">");
1033
1034                                 escputs((char *) icalproperty_get_comment(p));
1035                                 wprintf("</a></dd>\n");
1036                         }
1037                 }
1038         }
1039         DeleteHashPos(&Pos);
1040 }
1041
1042 /*
1043  * view one day
1044  */
1045 void calendar_day_view(int year, int month, int day) {
1046         int hour;
1047         struct icaltimetype today, yesterday, tomorrow;
1048         long daystart;
1049         long dayend;
1050         struct tm d_tm;
1051         char d_str[128];
1052         int time_format;
1053         time_t today_t;
1054         int timeline = 30;
1055         int extratimeline = 0;
1056         int gap = 0;
1057
1058         time_format = get_time_format_cached ();
1059         get_pref_long("daystart", &daystart, 8);
1060         get_pref_long("dayend", &dayend, 17);
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         wprintf("<script type=\"text/javascript\">"
1245                 " setTimeout(\"btt_enableTooltips('inner_day')\", 1); "
1246                 "</script>\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