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