6681389d9ee2d45fbaea5641aca3cf5a68388b1a
[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         int weekstart = 0;
23         char weekstart_buf[16];
24         char url[256];
25         char div_id[256];
26         char escaped_urlformat[256];
27
28         snprintf(div_id, sizeof div_id, "mini_calendar_%d", rand() );
29
30         /* Determine what day to start.
31         */
32         get_preference("weekstart", weekstart_buf, sizeof weekstart_buf);
33         weekstart = atoi(weekstart_buf);
34
35         /*
36         * Now back up to the 1st of the month...
37         */
38         memset(&starting_tm, 0, sizeof(struct tm));
39
40         starting_tm.tm_year = year - 1900;
41         starting_tm.tm_mon = month - 1;
42         starting_tm.tm_mday = 1;
43         thetime = mktime(&starting_tm);
44
45         memcpy(&tm, &starting_tm, sizeof(struct tm));
46         while (tm.tm_mday != 1) {
47                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
48                 localtime_r(&thetime, &tm);
49         }
50
51         /** Determine previous and next months ... for links */
52         previous_month = thetime - (time_t)864000L;     /* back 10 days */
53         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
54
55         /** Now back up until we're on the user's preferred start day */
56         localtime_r(&thetime, &tm);
57         while (tm.tm_wday != weekstart) {
58                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
59                 localtime_r(&thetime, &tm);
60         }
61
62         wprintf("<div class=\"mini_calendar\" id=\"%s\">\n", div_id);
63
64         /* Previous month link */
65         localtime_r(&previous_month, &tm);
66         wprintf("<a href=\"javascript:minical_change_month(%d,%d);\">&laquo;</a>", 
67                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
68
69         wc_strftime(colheader_label, sizeof colheader_label, "%B", &starting_tm);
70         wprintf("&nbsp;&nbsp;"
71                 "<span class=\"mini_calendar_month_label\">"
72                 "%s %d"
73                 "</span>"
74                 "&nbsp;&nbsp;", colheader_label, year);
75
76         /* Next month link */
77         localtime_r(&next_month, &tm);
78         wprintf("<a href=\"javascript:minical_change_month(%d,%d);\">&raquo;</a>",
79                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
80
81         wprintf("<table border=0 cellpadding=1 cellspacing=1 class=\"mini_calendar_days\">"
82                 "<tr>");
83         colheader_time = thetime;
84         for (i=0; i<7; ++i) {
85                 colheader_time = thetime + (i * 86400) ;
86                 localtime_r(&colheader_time, &colheader_tm);
87                 wc_strftime(colheader_label, sizeof colheader_label, "%A", &colheader_tm);
88                 wprintf("<th>%c</th>", colheader_label[0]);
89
90         }
91         wprintf("</tr>\n");
92
93
94         /** Now do 35 or 42 days */
95         for (i = 0; i < 42; ++i) {
96                 localtime_r(&thetime, &tm);
97
98                 if (i < 35) {
99
100                         /** Before displaying Sunday, start a new row */
101                         if ((i % 7) == 0) {
102                                 wprintf("<tr>");
103                         }
104
105                         if (tm.tm_mon == month-1) {
106                                 snprintf(url, sizeof url, urlformat,
107                                         tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday);
108                                 wprintf("<td><a href=\"%s\">%d</a></td>", url, tm.tm_mday);
109                         }
110                         else {
111                                 wprintf("<td> </td>");
112                         }
113
114                         /** After displaying one week, end the row */
115                         if ((i % 7) == 6) {
116                                 wprintf("</tr>\n");
117                         }
118
119                 }
120
121                 thetime += (time_t)86400;               /** ahead 24 hours */
122         }
123
124         wprintf("</table>"                      /** end of inner table */
125                 "</div>\n");
126
127         /* javascript for previous and next month */
128         len = strlen(urlformat);
129         for (i=0; i<len; ++i) {
130                 sprintf(&escaped_urlformat[i*2], "%02X", urlformat[i]);
131         }
132
133         wprintf("<script type=\"text/javascript\">                                                      "
134                 "       function minical_change_month(year, month) {                                    "
135                 "               p = 'year=' + year + '&month=' + month                                  "
136                 "                       + '&urlformat=%s&r=' + CtdlRandomString();                      "
137                 "               new Ajax.Updater('%s', 'mini_calendar',                                 "
138                 "                       { method: 'get', parameters: p, evalScripts: true } );          "
139                 "       }                                                                               "
140                 "</script>\n"
141                 ,
142                 escaped_urlformat, div_id
143                 );
144
145 }
146
147 /**
148  * \brief  ajax embedder for the above mini calendar 
149  */
150 void ajax_mini_calendar(void) {
151         char urlformat[256];
152         int i, len;
153         char *escaped_urlformat;
154
155         escaped_urlformat = bstr("urlformat");
156         len = strlen(escaped_urlformat) * 2 ;
157         for (i=0; i<len; ++i) {
158                 urlformat[i] = xtoi(&escaped_urlformat[i*2], 2);
159                 urlformat[i+1] = 0;
160         }
161
162         embeddable_mini_calendar( ibstr("year"), ibstr("month"), urlformat );
163 }
164
165
166 /**
167  * \brief Display one day of a whole month view of a calendar
168  * \param thetime the month we want to see 
169  */
170 void calendar_month_view_display_events(int year, int month, int day)
171 {
172         long hklen;
173         char *HashKey;
174         void *vCal;
175         HashPos *Pos;
176         disp_cal *Cal;
177         icalproperty *p = NULL;
178         icalproperty *q = NULL;
179         struct icaltimetype t;
180         struct icaltimetype end_t;
181         struct icaltimetype today_start_t;
182         struct icaltimetype today_end_t;
183         struct tm starting_tm;
184         struct tm ending_tm;
185         int all_day_event = 0;
186         int show_event = 0;
187         char buf[256];
188         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
189         time_t tt;
190
191         if (GetCount(WCC->disp_cal_items) == 0) {
192                 wprintf("<br /><br /><br />\n");
193                 return;
194         }
195
196         /**
197          * Create an imaginary event which spans the 24 hours of today.  Any events which
198          * overlap with this one take place at least partially in this day.  We have to
199          * convert it from a struct tm in order to make it UTC.
200          */
201         memset(&starting_tm, 0, sizeof(struct tm));
202         starting_tm.tm_year = year - 1900;
203         starting_tm.tm_mon = month - 1;
204         starting_tm.tm_mday = day;
205         starting_tm.tm_hour = 0;
206         starting_tm.tm_min = 0;
207         today_start_t = icaltime_from_timet_with_zone(mktime(&starting_tm), 0, icaltimezone_get_utc_timezone());
208         today_start_t.is_utc = 1;
209
210         memset(&ending_tm, 0, sizeof(struct tm));
211         ending_tm.tm_year = year - 1900;
212         ending_tm.tm_mon = month - 1;
213         ending_tm.tm_mday = day;
214         ending_tm.tm_hour = 23;
215         ending_tm.tm_min = 59;
216         today_end_t = icaltime_from_timet_with_zone(mktime(&ending_tm), 0, icaltimezone_get_utc_timezone());
217         today_end_t.is_utc = 1;
218
219         /**
220          * Now loop through our list of events to see which ones occur today.
221          */
222         Pos = GetNewHashPos();
223         while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
224                 Cal = (disp_cal*)vCal;
225                 all_day_event = 0;
226                 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
227                 if (q != NULL) {
228                         t = icalproperty_get_dtstart(q);
229                 }
230                 else {
231                         memset(&t, 0, sizeof t);
232                 }
233                 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
234                 if (q != NULL) {
235                         end_t = icalproperty_get_dtend(q);
236                 }
237                 else {
238                         memset(&end_t, 0, sizeof end_t);
239                 }
240                 if (t.is_date) all_day_event = 1;
241
242                 if (all_day_event)
243                 {
244                         show_event = ((t.year == year) && (t.month == month) && (t.day == day));
245                 }
246                 else
247                 {
248                         show_event = ical_ctdl_is_overlap(t, end_t, today_start_t, today_end_t);
249                 }
250
251                 /**
252                  * If we determined that this event occurs today, then display it.
253                  */
254                 if (show_event) {
255                         p = icalcomponent_get_first_property(Cal->cal, ICAL_SUMMARY_PROPERTY);
256                         if (p != NULL) {
257
258                                 if (all_day_event) {
259                                         wprintf("<table border=0 cellpadding=2><TR>"
260                                                 "<td bgcolor=\"#CCCCDD\">"
261                                                 );
262                                 }
263
264                                 wprintf("<font size=-1>"
265                                         "<a class=\"event%s\" href=\"display_edit_event?"
266                                         "msgnum=%ld&calview=month&year=%d&month=%d&day=%d\""
267                                         " btt_tooltext=\"",
268                                         (Cal->unread)?"_unread":"_read",
269                                         Cal->cal_msgnum,
270                                         year, month, day
271                                         );
272
273                                 wprintf("<i>%s: %s</i><br />", _("From"), Cal->from);
274                                 wprintf("<i>%s</i> ",          _("Summary:"));
275                                 escputs((char *)icalproperty_get_comment(p));
276                                 wprintf("<br />");
277                                 
278                                 q = icalcomponent_get_first_property(
279                                         Cal->cal,
280                                         ICAL_LOCATION_PROPERTY);
281                                 if (q) {
282                                         wprintf("<i>%s</i> ", _("Location:"));
283                                         escputs((char *)icalproperty_get_comment(q));
284                                         wprintf("<br />");
285                                 }
286                                 
287                                 /**
288                                  * Only show start/end times if we're actually looking at the VEVENT
289                                  * component.  Otherwise it shows bogus dates for e.g. timezones
290                                  */
291                                 if (icalcomponent_isa(Cal->cal) == ICAL_VEVENT_COMPONENT) {
292                                         
293                                         q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
294                                         if (q != NULL) {
295                                                 t = icalproperty_get_dtstart(q);
296                                                 
297                                                 if (t.is_date) {
298                                                         struct tm d_tm;
299                                                         char d_str[32];
300                                                         memset(&d_tm, 0, sizeof d_tm);
301                                                         d_tm.tm_year = t.year - 1900;
302                                                         d_tm.tm_mon = t.month - 1;
303                                                         d_tm.tm_mday = t.day;
304                                                         wc_strftime(d_str, sizeof d_str, "%x", &d_tm);
305                                                         wprintf("<i>%s</i> %s<br>",
306                                                                 _("Date:"), d_str);
307                                                 }
308                                                 else {
309                                                         tt = icaltime_as_timet(t);
310                                                         webcit_fmt_date(buf, tt, 1);
311                                                         wprintf("<i>%s</i> %s<br>",
312                                                                 _("Starting date/time:"), buf);
313                                                         
314                                                         /**
315                                                          * Embed the 'show end date/time' loop inside here so it
316                                                          * only executes if this is NOT an all day event.
317                                                          */
318                                                         q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
319                                                         if (q != NULL) {
320                                                                 t = icalproperty_get_dtend(q);
321                                                                 tt = icaltime_as_timet(t);
322                                                                 webcit_fmt_date(buf, tt, 1);
323                                                                 wprintf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
324                                                         }
325                                                         
326                                                 }
327                                         }
328                                         
329                                 }
330                                 
331                                 q = icalcomponent_get_first_property(Cal->cal, ICAL_DESCRIPTION_PROPERTY);
332                                 if (q) {
333                                         wprintf("<i>%s</i> ", _("Notes:"));
334                                         escputs((char *)icalproperty_get_comment(q));
335                                         wprintf("<br />");
336                                 }
337                                 
338                                 wprintf("\">");
339                                 escputs((char *)
340                                         icalproperty_get_comment(p));
341                                 wprintf("</a></font><br />\n");
342                                 
343                                 if (all_day_event) {
344                                         wprintf("</td></tr></table>");
345                                 }
346                                 
347                         }
348                         
349                 }
350                 
351                 
352         }
353         DeleteHashPos(&Pos);
354 }
355
356
357 /**
358  * \brief Display one day of a whole month view of a calendar
359  * \param thetime the month we want to see 
360  */
361 void calendar_month_view_brief_events(time_t thetime, const char *daycolor) {
362         long hklen;
363         char *HashKey;
364         void *vCal;
365         HashPos *Pos;
366         time_t event_tt;
367         time_t event_tts;
368         time_t event_tte;
369         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
370         struct tm event_tms;
371         struct tm event_tme;
372         struct tm today_tm;
373         icalproperty *p;
374         icalproperty *e;
375         struct icaltimetype t;
376         disp_cal *Cal;
377         int month, day, year;
378         int all_day_event = 0;
379         char *timeformat;
380         int time_format;
381         
382         time_format = get_time_format_cached ();
383
384         if (time_format == WC_TIMEFORMAT_24) timeformat="%k:%M";
385         else timeformat="%I:%M %p";
386
387         localtime_r(&thetime, &today_tm);
388         month = today_tm.tm_mon + 1;
389         day = today_tm.tm_mday;
390         year = today_tm.tm_year + 1900;
391
392         Pos = GetNewHashPos();
393         while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
394                 Cal = (disp_cal*)vCal;
395                 p = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
396                 if (p != NULL) {
397                         t = icalproperty_get_dtstart(p);
398                         event_tt = icaltime_as_timet(t);
399                         event_tts=event_tt;
400                         if (t.is_date) all_day_event = 1;
401                         else all_day_event = 0;
402
403                         if (all_day_event) {
404                                 gmtime_r(&event_tts, &event_tms);
405                         }
406                         else {
407                                 localtime_r(&event_tts, &event_tms);
408                         }
409                         /* \todo epoch &! daymask */
410                         if ((event_tms.tm_year == today_tm.tm_year)
411                                 && (event_tms.tm_mon == today_tm.tm_mon)
412                         && (event_tms.tm_mday == today_tm.tm_mday)) {
413                         
414                         
415                         char sbuf[255];
416                         char ebuf[255];
417                         
418                         p = icalcomponent_get_first_property(
419                                 Cal->cal,
420                                 ICAL_SUMMARY_PROPERTY);
421                         e = icalcomponent_get_first_property(
422                                 Cal->cal, 
423                                 ICAL_DTEND_PROPERTY);
424                         if ((p != NULL) && (e != NULL)) {
425                                 time_t difftime;
426                                 int hours, minutes;
427                                 t = icalproperty_get_dtend(e);
428                                 event_tte = icaltime_as_timet(t);
429                                 localtime_r(&event_tte, &event_tme);
430                                 difftime=(event_tte-event_tts)/60;
431                                 hours=(int)(difftime / 60);
432                                 minutes=difftime % 60;
433                                 wprintf("<tr><td bgcolor='%s'>%i:%2i</td><td bgcolor='%s'>"
434                                         "<font size=-1>"
435                                         "<a class=\"event%s\" href=\"display_edit_event?msgnum=%ld&calview=calbrief&year=%s&month=%s&day=%s\">",
436                                         daycolor,
437                                         hours, minutes,
438                                         (Cal->unread)?"_unread":"_read",                                                
439                                         daycolor,
440                                         Cal->cal_msgnum,
441                                         bstr("year"),
442                                         bstr("month"),
443                                         bstr("day")
444                                         );
445                                 
446                                 escputs((char *)
447                                         icalproperty_get_comment(p));
448                                 /* \todo: allso ammitime format */
449                                 wc_strftime(&sbuf[0], sizeof(sbuf), timeformat, &event_tms);
450                                 wc_strftime(&ebuf[0], sizeof(sbuf), timeformat, &event_tme);
451                                 
452                                 wprintf("</a></font></td>"
453                                         "<td bgcolor='%s'>%s</td><td bgcolor='%s'>%s</td></tr>",
454                                         daycolor,
455                                         sbuf,
456                                         daycolor,
457                                         ebuf);
458                                 }
459                         
460                         }
461                         
462                         
463                 }
464         }
465         DeleteHashPos(&Pos);
466 }
467
468
469 /*
470  * view one month. pretty view
471  */
472 void calendar_month_view(int year, int month, int day) {
473         struct tm starting_tm;
474         struct tm tm;
475         time_t thetime;
476         int i;
477         time_t previous_month;
478         time_t next_month;
479         time_t colheader_time;
480         struct tm colheader_tm;
481         char colheader_label[32];
482         int chg_month = 0;
483         int weekstart = 0;
484         char weekstart_buf[16];
485
486         /*
487          * Determine what day to start
488          */
489         get_preference("weekstart", weekstart_buf, sizeof weekstart_buf);
490         weekstart = atoi(weekstart_buf);
491
492         /*
493          * Now back up to the 1st of the month...
494          */
495         memset(&starting_tm, 0, sizeof(struct tm));
496
497         starting_tm.tm_year = year - 1900;
498         starting_tm.tm_mon = month - 1;
499         starting_tm.tm_mday = day;
500         thetime = mktime(&starting_tm);
501
502         memcpy(&tm, &starting_tm, sizeof(struct tm));
503         while (tm.tm_mday != 1) {
504                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
505                 localtime_r(&thetime, &tm);
506         }
507
508         /* Determine previous and next months ... for links */
509         previous_month = thetime - (time_t)864000L;     /* back 10 days */
510         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
511
512         /* Now back up until we're on the user's preferred start day */
513         localtime_r(&thetime, &tm);
514         while (tm.tm_wday != weekstart) {
515                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
516                 localtime_r(&thetime, &tm);
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         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         int daystart = 8;
1049         int dayend = 17;
1050         char daystart_str[16], dayend_str[16];
1051         struct tm d_tm;
1052         char d_str[128];
1053         int time_format;
1054         time_t today_t;
1055         int timeline = 30;
1056         int extratimeline = 0;
1057         int gap = 0;
1058
1059         time_format = get_time_format_cached ();
1060         get_preference("daystart", daystart_str, sizeof daystart_str);
1061         if (!IsEmptyStr(daystart_str)) daystart = atoi(daystart_str);
1062         get_preference("dayend", dayend_str, sizeof dayend_str);
1063         if (!IsEmptyStr(dayend_str)) dayend = atoi(dayend_str);
1064         
1065         /* Today's date */
1066         memset(&d_tm, 0, sizeof d_tm);
1067         d_tm.tm_year = year - 1900;
1068         d_tm.tm_mon = month - 1;
1069         d_tm.tm_mday = day;
1070         today_t = mktime(&d_tm); 
1071
1072         /* Figure out the dates for "yesterday" and "tomorrow" links */
1073
1074         memset(&today, 0, sizeof(struct icaltimetype));
1075         today.year = year;
1076         today.month = month;
1077         today.day = day;
1078         today.is_date = 1;
1079
1080         memcpy(&yesterday, &today, sizeof(struct icaltimetype));
1081         --yesterday.day;
1082         yesterday = icaltime_normalize(yesterday);
1083
1084         memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
1085         ++tomorrow.day;
1086         tomorrow = icaltime_normalize(tomorrow);
1087
1088         wprintf("<div class=\"fix_scrollbar_bug\">");
1089
1090         /* Inner table (the real one) */
1091         wprintf("<table class=\"calendar\" id=\"inner_day\"><tr> \n");
1092
1093         /* Innermost cell (contains hours etc.) */
1094         wprintf("<td class=\"events_of_the_day\" >");
1095         wprintf("<dl class=\"events\" >");
1096
1097         /* Now the middle of the day... */
1098
1099         extratimeline = timeline / 3;   
1100
1101         for (hour = 0; hour < daystart; ++hour) {       /* could do HEIGHT=xx */
1102                 wprintf("<dt class=\"extrahour\"        "
1103                         "style=\"               "
1104                         "position: absolute;    "
1105                         "top: %dpx; left: 0px;  "
1106                         "height: %dpx;          "       
1107                         "\" >                   "
1108                         "<a href=\"display_edit_event?msgnum=0"
1109                         "&calview=day&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
1110                         (hour * extratimeline ), extratimeline, 
1111                         year, month, day, hour
1112                         );
1113
1114                 if (time_format == WC_TIMEFORMAT_24) {
1115                         wprintf("%2d:00</a> ", hour);
1116                 }
1117                 else {
1118                         wprintf("%d:00%s</a> ",
1119                                 (hour <= 12 ? hour : hour-12),
1120                                 (hour < 12 ? "am" : "pm")
1121                                 );
1122                 }
1123
1124                 wprintf("</dt>");
1125         }
1126
1127         gap = daystart * extratimeline;
1128
1129         for (hour = daystart; hour <= dayend; ++hour) {       /* could do HEIGHT=xx */
1130                 wprintf("<dt class=\"hour\"     "
1131                         "style=\"               "
1132                         "position: absolute;    "
1133                         "top: %dpx; left: 0px;  "
1134                         "height: %dpx;          "
1135                         "\" >                   "
1136                         "<a href=\"display_edit_event?msgnum=0&calview=day"
1137                         "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
1138                         gap + ((hour - daystart) * timeline ), timeline,
1139                         year, month, day, hour
1140                         );
1141
1142                 if (time_format == WC_TIMEFORMAT_24) {
1143                         wprintf("%2d:00</a> ", hour);
1144                 }
1145                 else {
1146                         wprintf("%d:00%s</a> ",
1147                                 (hour <= 12 ? hour : hour-12),
1148                                 (hour < 12 ? "am" : "pm")
1149                                                 );
1150                 }
1151
1152                 wprintf("</dt>");
1153         }
1154
1155         gap = gap + ((dayend - daystart + 1) * timeline);
1156
1157         for (hour = (dayend + 1); hour < 24; ++hour) {       /* could do HEIGHT=xx */
1158                 wprintf("<dt class=\"extrahour\"     "
1159                         "style=\"               "
1160                         "position: absolute;    "
1161                         "top: %dpx; left: 0px;  "
1162                         "height: %dpx;          "
1163                         "\" >                   "
1164                         "<a href=\"display_edit_event?msgnum=0&calview=day"
1165                         "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
1166                         gap + ((hour - dayend - 1) * extratimeline ), extratimeline,
1167                         year, month, day, hour
1168                 );
1169
1170                 if (time_format == WC_TIMEFORMAT_24) {
1171                         wprintf("%2d:00</a> ", hour);
1172                 }
1173                 else {
1174                         wprintf("%d:00%s</a> ",
1175                                 (hour <= 12 ? hour : hour-12),
1176                                 (hour < 12 ? "am" : "pm")
1177                         );
1178                 }
1179
1180                 wprintf("</dt>");
1181         }
1182
1183         /* Display events with start and end times on this day */
1184         calendar_day_view_display_events(today_t, year, month, day, 0, daystart, dayend);
1185
1186         wprintf("</dl>");
1187         wprintf("</td>");                       /* end of innermost table */
1188
1189         /* Display extra events (start/end times not present or not today) in the middle column */
1190         wprintf("<td class=\"extra_events\">");
1191
1192         wprintf("<ul>");
1193
1194         /* Display all-day events */
1195         calendar_day_view_display_events(today_t, year, month, day, 1, daystart, dayend);
1196
1197         wprintf("</ul>");
1198
1199         wprintf("</td>");       /** end extra on the middle */
1200
1201         wprintf("<td width=20%% align=center valign=top>");     /** begin stuff-on-the-right */
1202
1203         /* Begin todays-date-with-left-and-right-arrows */
1204         wprintf("<table border=0 width=100%% "
1205                 "cellspacing=0 cellpadding=0 bgcolor=\"#FFFFFF\">\n");
1206         wprintf("<tr>");
1207
1208         /* Left arrow */        
1209         wprintf("<td align=center>");
1210         wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
1211                 yesterday.year, yesterday.month, yesterday.day);
1212         wprintf("<img align=middle src=\"static/prevdate_32x.gif\" border=0></A>");
1213         wprintf("</td>");
1214
1215         wc_strftime(d_str, sizeof d_str,
1216                 "<td align=center>"
1217                 "<font size=+2>%B</font><br />"
1218                 "<font size=+3>%d</font><br />"
1219                 "<font size=+2>%Y</font><br />"
1220                 "</td>",
1221                 &d_tm
1222                 );
1223         wprintf("%s", d_str);
1224
1225         /* Right arrow */
1226         wprintf("<td align=center>");
1227         wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
1228                 tomorrow.year, tomorrow.month, tomorrow.day);
1229         wprintf("<img align=middle src=\"static/nextdate_32x.gif\""
1230                 " border=0></a>\n");
1231         wprintf("</td>");
1232
1233         wprintf("</tr></table>\n");
1234         /* End todays-date-with-left-and-right-arrows */
1235
1236         /* Embed a mini month calendar in this space */
1237         wprintf("<br />\n");
1238         embeddable_mini_calendar(year, month, "readfwd?calview=day&year=%d&month=%d&day=%d");
1239
1240         wprintf("</font></center>\n");
1241
1242         wprintf("</td></tr>");                  /** end stuff-on-the-right */
1243
1244         wprintf("</table>"                      /** end of inner table */
1245                 "</div>");
1246
1247         wprintf("<script type=\"text/javascript\">"
1248                 " setTimeout(\"btt_enableTooltips('inner_day')\", 1); "
1249                 "</script>\n"
1250         );
1251 }
1252
1253
1254 /*
1255  * Display today's events.
1256  */
1257 void calendar_summary_view(void) {
1258         long hklen;
1259         char *HashKey;
1260         void *vCal;
1261         HashPos *Pos;
1262         disp_cal *Cal;
1263         icalproperty *p;
1264         struct icaltimetype t;
1265         time_t event_tt;
1266         struct tm event_tm;
1267         struct tm today_tm;
1268         time_t now;
1269         int all_day_event = 0;
1270         char timestring[SIZ];
1271         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
1272
1273         if (GetCount(WC->disp_cal_items) == 0) {
1274                 return;
1275         }
1276
1277         now = time(NULL);
1278         localtime_r(&now, &today_tm);
1279
1280         Pos = GetNewHashPos();
1281         while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
1282                 Cal = (disp_cal*)vCal;
1283                 p = icalcomponent_get_first_property(Cal->cal,
1284                                                 ICAL_DTSTART_PROPERTY);
1285                 if (p != NULL) {
1286                         t = icalproperty_get_dtstart(p);
1287                         event_tt = icaltime_as_timet(t);
1288                         if (t.is_date) {
1289                                 all_day_event = 1;
1290                         }
1291                         else {
1292                                 all_day_event = 0;
1293                         }
1294                         fmt_time(timestring, event_tt);
1295
1296                         if (all_day_event) {
1297                                 gmtime_r(&event_tt, &event_tm);
1298                         }
1299                         else {
1300                                 localtime_r(&event_tt, &event_tm);
1301                         }
1302
1303                         if ( (event_tm.tm_year == today_tm.tm_year)
1304                                 && (event_tm.tm_mon == today_tm.tm_mon)
1305                         && (event_tm.tm_mday == today_tm.tm_mday)
1306                         ) {
1307
1308
1309                         p = icalcomponent_get_first_property(
1310                                 Cal->cal,
1311                                 ICAL_SUMMARY_PROPERTY);
1312                         if (p != NULL) {
1313                                 escputs((char *)
1314                                         icalproperty_get_comment(p));
1315                                 wprintf(" (%s)<br />\n", timestring);
1316                         }
1317                         }
1318                 }
1319         }
1320         DeleteHashPos(&Pos);
1321         DeleteHash(&WC->disp_cal_items);
1322 }
1323
1324
1325 /*
1326  * do the whole calendar page
1327  * view any part of the calender. decide which way, etc.
1328  */
1329 void do_calendar_view(void) {
1330         time_t now;
1331         struct tm tm;
1332         int year, month, day;
1333         char calview[SIZ];
1334
1335         /* In case no date was specified, go with today */
1336         now = time(NULL);
1337         localtime_r(&now, &tm);
1338         year = tm.tm_year + 1900;
1339         month = tm.tm_mon + 1;
1340         day = tm.tm_mday;
1341
1342         /* Now see if a date was specified */
1343         if (havebstr("year")) year = ibstr("year");
1344         if (havebstr("month")) month = ibstr("month");
1345         if (havebstr("day")) day = ibstr("day");
1346
1347         /* How would you like that cooked? */
1348         if (havebstr("calview")) {
1349                 strcpy(calview, bstr("calview"));
1350         }
1351         else {
1352                 strcpy(calview, "month");
1353         }
1354
1355         /* Display the selected view */
1356         if (!strcasecmp(calview, "day")) {
1357                 calendar_day_view(year, month, day);
1358         }
1359         else if (!strcasecmp(calview, "week")) {
1360                 calendar_week_view(year, month, day);
1361         }
1362         else {
1363                 if (WC->wc_view == VIEW_CALBRIEF) {
1364                         calendar_brief_month_view(year, month, day);
1365                 }
1366                 else {
1367                         calendar_month_view(year, month, day);
1368                 }
1369         }
1370
1371         /* Free the in-memory list of calendar items */
1372         DeleteHash(&WC->disp_cal_items);
1373 }
1374
1375
1376 /*
1377  * Helper function for do_tasks_view().  Returns the due date/time of a vtodo.
1378  */
1379 time_t get_task_due_date(icalcomponent *vtodo) {
1380         icalproperty *p;
1381
1382         if (vtodo == NULL) {
1383                 return(0L);
1384         }
1385
1386         /*
1387          * If we're looking at a fully encapsulated VCALENDAR
1388          * rather than a VTODO component, recurse into the data
1389          * structure until we get a VTODO.
1390          */
1391         if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
1392                 return get_task_due_date(
1393                         icalcomponent_get_first_component(
1394                                 vtodo, ICAL_VTODO_COMPONENT
1395                                 )
1396                         );
1397         }
1398
1399         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
1400         if (p != NULL) {
1401                 return(icaltime_as_timet(icalproperty_get_due(p)));
1402         }
1403         else {
1404                 return(0L);
1405         }
1406 }
1407
1408
1409 /*
1410  * Compare the due dates of two tasks (this is for sorting)
1411  */
1412 int task_due_cmp(const void *vtask1, const void *vtask2) {
1413         disp_cal * Task1 = (disp_cal *)GetSearchPayload(vtask1);
1414         disp_cal * Task2 = (disp_cal *)GetSearchPayload(vtask2);
1415
1416         time_t t1;
1417         time_t t2;
1418
1419         t1 =  get_task_due_date(Task1->cal);
1420         t2 =  get_task_due_date(Task2->cal);
1421         if (t1 < t2) return(-1);
1422         if (t1 > t2) return(1);
1423         return(0);
1424 }
1425
1426 /*
1427  * qsort filter to move completed tasks to bottom of task list
1428  */
1429 int task_completed_cmp(const void *vtask1, const void *vtask2) {
1430         disp_cal * Task1 = (disp_cal *)GetSearchPayload(vtask1);
1431 //      disp_cal * Task2 = (disp_cal *)GetSearchPayload(vtask2);
1432
1433         icalproperty_status t1 = icalcomponent_get_status((Task1)->cal);
1434         // icalproperty_status t2 = icalcomponent_get_status(((struct disp_cal *)task2)->cal);
1435         
1436         if (t1 == ICAL_STATUS_COMPLETED) 
1437                 return 1;
1438         return 0;
1439 }
1440
1441
1442
1443 /*
1444  * do the whole task view stuff
1445  */
1446 void do_tasks_view(void) {
1447         long hklen;
1448         char *HashKey;
1449         void *vCal;
1450         disp_cal *Cal;
1451         HashPos *Pos;
1452         int nItems;
1453         time_t due;
1454         char buf[SIZ];
1455         icalproperty *p;
1456         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
1457
1458         wprintf("<div class=\"fix_scrollbar_bug\">"
1459                 "<table class=\"calendar_view_background\"><tbody id=\"taskview\">\n<tr>\n"
1460                 "<th>");
1461         wprintf(_("Completed?"));
1462         wprintf("</th><th>");
1463         wprintf(_("Name of task"));
1464         wprintf("</th><th>");
1465         wprintf(_("Date due"));
1466         wprintf("</th><th>");
1467         wprintf(_("Category"));
1468         wprintf(" (<select id=\"selectcategory\"><option value=\"showall\">%s</option></select>)</th></tr>\n",
1469                 _("Show All"));
1470
1471         nItems = GetCount(WC->disp_cal_items);
1472
1473         /* Sort them if necessary
1474         if (nItems > 1) {
1475                 SortByPayload(WC->disp_cal_items, task_due_cmp);
1476         }
1477         * this shouldn't be neccessary, since we sort by the start time.
1478         */
1479
1480         /* And then again, by completed */
1481         if (nItems > 1) {
1482                 SortByPayload(WC->disp_cal_items, 
1483                               task_completed_cmp);
1484         }
1485
1486         Pos = GetNewHashPos();
1487         while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
1488                 Cal = (disp_cal*)vCal;
1489                 wprintf("<tr><td>");
1490                 icalproperty_status todoStatus = icalcomponent_get_status(Cal->cal);
1491                 wprintf("<input type=\"checkbox\" name=\"completed\" value=\"completed\" ");
1492                 if (todoStatus == ICAL_STATUS_COMPLETED) {
1493                         wprintf("checked=\"checked\" ");
1494                 }
1495                 wprintf("disabled=\"disabled\">\n</td><td>");
1496                 p = icalcomponent_get_first_property(Cal->cal,
1497                         ICAL_SUMMARY_PROPERTY);
1498                 wprintf("<a href=\"display_edit_task?msgnum=%ld&amp;taskrm=",
1499                         Cal->cal_msgnum );
1500                 urlescputs(WC->wc_roomname);
1501                 wprintf("\">");
1502                 /* wprintf("<img align=middle "
1503                 "src=\"static/taskmanag_16x.gif\" border=0>&nbsp;"); */
1504                 if (p != NULL) {
1505                         escputs((char *)icalproperty_get_comment(p));
1506                 }
1507                 wprintf("</a>\n");
1508                 wprintf("</td>\n");
1509
1510                 due = get_task_due_date(Cal->cal);
1511                 wprintf("<td><span");
1512                 if (due > 0) {
1513                         webcit_fmt_date(buf, due, 0);
1514                         wprintf(">%s",buf);
1515                 }
1516                 else {
1517                         wprintf(">");
1518                 }
1519                 wprintf("</span></td>");
1520                 wprintf("<td>");
1521                 p = icalcomponent_get_first_property(Cal->cal,
1522                         ICAL_CATEGORIES_PROPERTY);
1523                 if (p != NULL) {
1524                         escputs((char *)icalproperty_get_categories(p));
1525                 }
1526                 wprintf("</td>");
1527                 wprintf("</tr>");
1528         }
1529
1530         wprintf("</tbody></table></div>\n");
1531
1532         /* Free the list */
1533         DeleteHash(&WC->disp_cal_items);
1534         DeleteHashPos(&Pos);
1535 }
1536