* by feivel: make the current day be highlighted in monthly calendar view. Fixes...
[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         wprintf("<div class=\"mini_calendar\" id=\"%s\">\n", div_id);
65
66         /* Previous month link */
67         localtime_r(&previous_month, &tm);
68         wprintf("<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         wprintf("&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         wprintf("<a href=\"javascript:minical_change_month(%d,%d);\">&raquo;</a>",
81                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
82
83         wprintf("<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                 wprintf("<th>%c</th>", colheader_label[0]);
91
92         }
93         wprintf("</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                                 wprintf("<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                                 wprintf("<td><a href=\"%s\">%d</a></td>", url, tm.tm_mday);
111                         }
112                         else {
113                                 wprintf("<td> </td>");
114                         }
115
116                         /* After displaying one week, end the row */
117                         if ((i % 7) == 6) {
118                                 wprintf("</tr>\n");
119                         }
120
121                 }
122
123                 thetime += (time_t)86400;               /* ahead 24 hours */
124         }
125
126         wprintf("</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                 wprintf("<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                                         wprintf("<table border=0 cellpadding=2><TR>"
257                                                 "<td bgcolor=\"#CCCCDD\">"
258                                                 );
259                                 }
260
261                                 wprintf("<font size=\"-1\">"
262                                         "<a class=\"event%s\" href=\"display_edit_event?"
263                                         "msgnum=%ld?calview=month?year=%d?month=%d?day=%d\""
264                                         " btt_tooltext=\"",
265                                         (Cal->unread)?"_unread":"_read",
266                                         Cal->cal_msgnum,
267                                         year, month, day
268                                         );
269
270                                 wprintf("<i>%s: %s</i><br />", _("From"), Cal->from);
271                                 wprintf("<i>%s</i> ",          _("Summary:"));
272                                 escputs((char *)icalproperty_get_comment(p));
273                                 wprintf("<br />");
274                                 
275                                 q = icalcomponent_get_first_property(
276                                         Cal->cal,
277                                         ICAL_LOCATION_PROPERTY);
278                                 if (q) {
279                                         wprintf("<i>%s</i> ", _("Location:"));
280                                         escputs((char *)icalproperty_get_comment(q));
281                                         wprintf("<br />");
282                                 }
283                                 
284                                 /*
285                                  * Only show start/end times if we're actually looking at the VEVENT
286                                  * component.  Otherwise it shows bogus dates for e.g. timezones
287                                  */
288                                 if (icalcomponent_isa(Cal->cal) == ICAL_VEVENT_COMPONENT) {
289                                         
290                                         q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
291                                         if (q != NULL) {
292                                                 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                                                                 wprintf("<i>%s</i> %s<br>",
324                                                                         _("Date:"), buf);
325                                                         }
326                                                         else {
327                                                                 wprintf("<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                                                                 wprintf("<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                                                                 wprintf("<i>%s</i> %s<br>",
342                                                                         _("Date/time:"), buf);
343                                                         }
344                                                         else {
345                                                                 wprintf("<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                                                                 wprintf("<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                                         wprintf("<i>%s</i> ", _("Notes:"));
360                                         escputs((char *)icalproperty_get_comment(q));
361                                         wprintf("<br />");
362                                 }
363                                 
364                                 wprintf("\">");
365                                 escputs((char *)
366                                         icalproperty_get_comment(p));
367                                 wprintf("</a></font><br />\n");
368                                 
369                                 if (all_day_event) {
370                                         wprintf("</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                                 wprintf("<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                                 wprintf("</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         wprintf("<div class=\"fix_scrollbar_bug\">"
548                 "<table class=\"calendar\"> \n <tr><td>"); 
549
550         wprintf("<table width=100%% border=0 cellpadding=0 cellspacing=0><tr>\n");
551
552         wprintf("<td align=center>");
553
554         localtime_r(&previous_month, &tm);
555         wprintf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
556                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
557         wprintf("<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         wprintf("&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         wprintf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
568                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
569         wprintf("<img align=middle src=\"static/nextdate_32x.gif\" border=0></A>\n");
570
571         wprintf("</td></tr></table>\n");
572
573         /* Inner table (the real one) */
574         wprintf("<table width=100%% border=0 cellpadding=1 cellspacing=1 "
575                 "bgcolor=#204B78 id=\"inner_month\"><tr>");
576         colheader_time = thetime;
577         for (i=0; i<7; ++i) {
578                 colheader_time = thetime + (i * 86400) ;
579                 localtime_r(&colheader_time, &colheader_tm);
580                 wc_strftime(colheader_label, sizeof colheader_label, "%A", &colheader_tm);
581                 wprintf("<th align=center width=14%%>"
582                         "<font color=\"#FFFFFF\">%s</font></th>", colheader_label);
583
584         }
585         wprintf("</tr>\n");
586
587
588         /* Now do 35 or 42 days */
589         localtime_r(&thetime, &tm);
590         for (i = 0; i<42; ++i) {
591
592                 /* Before displaying the first day of the week, start a new row */
593                 if ((i % 7) == 0) {
594                         wprintf("<tr>");
595                 }
596
597                 wprintf("<td class=\"cal%s\"><div class=\"day\">",
598                         ((tm.tm_mon != month-1) ? "out" :
599                                 ((tm.tm_mday == day) ? "today" :
600                                 ((tm.tm_wday==0 || tm.tm_wday==6) ? "weekend" :
601                                         "day")))
602                         );
603                 if ((i==0) || (tm.tm_mday == 1)) {
604                         wc_strftime(colheader_label, sizeof colheader_label, "%B", &tm);
605                         wprintf("%s ", colheader_label);
606                 }
607                 wprintf("<a href=\"readfwd?calview=day?year=%d?month=%d?day=%d\">"
608                         "%d</a></div>",
609                         tm.tm_year + 1900,
610                         tm.tm_mon + 1,
611                         tm.tm_mday,
612                         tm.tm_mday);
613
614                 /* put the data here, stupid */
615                 calendar_month_view_display_events(
616                         tm.tm_year + 1900,
617                         tm.tm_mon + 1,
618                         tm.tm_mday
619                         );
620
621                 wprintf("</td>");
622
623                 /* After displaying the last day of the week, end the row */
624                 if ((i % 7) == 6) {
625                         wprintf("</tr>\n");
626                 }
627
628                 thetime += (time_t)86400;               /* ahead 24 hours */
629                 localtime_r(&thetime, &tm);
630
631                 if ( ((i % 7) == 6) && (tm.tm_mon != month-1) && (tm.tm_mday < 15) ) {
632                         i = 100;        /* break out of the loop */
633                 }
634         }
635
636         wprintf("</table>"                      /* end of inner table */
637                 "</td></tr></table>"            /* end of outer table */
638                 "</div>\n");
639
640         /*
641          * Initialize the bubble tooltips.
642          *
643          * Yes, this is as stupid as it looks.  Instead of just making the call
644          * to btt_enableTooltips() straight away, we have to create a timer event
645          * and let it initialize as an event after 1 millisecond.  This is to
646          * work around a bug in Internet Explorer that causes it to crash if we
647          * manipulate the innerHTML of various DOM nodes while the page is still
648          * being rendered.  See http://www.shaftek.org/blog/archives/000212.html
649          * for more information.
650          */ 
651         StrBufAppendPrintf(WC->trailing_javascript,
652                 " setTimeout(\"btt_enableTooltips('inner_month')\", 1); \n"
653         );
654 }
655
656 /*
657  * view one month. brief view
658  */
659 void calendar_brief_month_view(int year, int month, int day) {
660         struct tm starting_tm;
661         struct tm tm;
662         time_t thetime;
663         int i;
664         time_t previous_month;
665         time_t next_month;
666         char month_label[32];
667
668         /* Determine what day to start.
669          * First, back up to the 1st of the month...
670          */
671         memset(&starting_tm, 0, sizeof(struct tm));
672         starting_tm.tm_year = year - 1900;
673         starting_tm.tm_mon = month - 1;
674         starting_tm.tm_mday = day;
675         thetime = mktime(&starting_tm);
676
677         memcpy(&tm, &starting_tm, sizeof(struct tm));
678         while (tm.tm_mday != 1) {
679                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
680                 localtime_r(&thetime, &tm);
681         }
682
683         /* Determine previous and next months ... for links */
684         previous_month = thetime - (time_t)864000L;     /* back 10 days */
685         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
686
687         /* Now back up until we're on a Sunday */
688         localtime_r(&thetime, &tm);
689         while (tm.tm_wday != 0) {
690                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
691                 localtime_r(&thetime, &tm);
692         }
693
694         /* Outer table (to get the background color) */
695         wprintf("<div class=\"fix_scrollbar_bug\">"
696                 "<table width=100%% border=0 cellpadding=0 cellspacing=0 "
697                 "bgcolor=#204B78><TR><TD>\n");
698
699         wprintf("<table width=100%% border=0 cellpadding=0 cellspacing=0><tr>\n");
700
701         wprintf("<td align=center>");
702
703         localtime_r(&previous_month, &tm);
704         wprintf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
705                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
706         wprintf("<img align=middle src=\"static/prevdate_32x.gif\" border=0></A>\n");
707
708         wc_strftime(month_label, sizeof month_label, "%B", &tm);
709         wprintf("&nbsp;&nbsp;"
710                 "<font size=+1 color=\"#FFFFFF\">"
711                 "%s %d"
712                 "</font>"
713                 "&nbsp;&nbsp;", month_label, year);
714
715         localtime_r(&next_month, &tm);
716         wprintf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
717                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
718         wprintf("<img align=middle src=\"static/nextdate_32x.gif\" border=0></A>\n");
719
720         wprintf("</td></tr></table>\n");
721
722         /* Inner table (the real one) */
723         wprintf("<table width=100%% border=0 cellpadding=1 cellspacing=1 "
724                 "bgcolor=#EEEECC><TR>");
725         wprintf("</tr>\n");
726         wprintf("<tr><td colspan=\"100%%\">\n");
727
728         /* Now do 35 days */
729         for (i = 0; i < 35; ++i) {
730                 char weeknumber[255];
731                 char weekday_name[32];
732                 char *daycolor;
733                 localtime_r(&thetime, &tm);
734
735
736                 /* Before displaying Sunday, start a new CELL */
737                 if ((i % 7) == 0) {
738                         wc_strftime(&weeknumber[0], sizeof(weeknumber), "%U", &tm);
739                         wprintf("<table border='0' bgcolor=\"#EEEECC\" width='100%%'> <tr><th colspan='4'>%s %s</th></tr>"
740                                 "   <tr><td>%s</td><td width=70%%>%s</td><td>%s</td><td>%s</td></tr>\n",
741                                 _("Week"), 
742                                 weeknumber,
743                                 _("Hours"),
744                                 _("Subject"),
745                                 _("Start"),
746                                 _("End")
747                                 );
748                 }
749                 
750                 daycolor=((tm.tm_mon != month-1) ? "DDDDDD" :
751                         ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
752                                 "FFFFFF"));
753                 
754                 /* Day Header */
755                 wc_strftime(weekday_name, sizeof weekday_name, "%A", &tm);
756                 wprintf("<tr><td bgcolor='%s' colspan='1' align='left'> %s,%i."
757                         "</td><td bgcolor='%s' colspan='3'><hr></td></tr>\n",
758                         daycolor,
759                         weekday_name,tm.tm_mday,
760                         daycolor);
761
762                 /* put the data of one day  here, stupid */
763                 calendar_month_view_brief_events(thetime, daycolor);
764
765
766                 /* After displaying Saturday, end the row */
767                 if ((i % 7) == 6) {
768                         wprintf("</td></tr></table>\n");
769                 }
770
771                 thetime += (time_t)86400;               /* ahead 24 hours */
772         }
773
774         wprintf("</table>"                      /* end of inner table */
775                 "</td></tr></table>"            /* end of outer table */
776                 "</div>\n");
777 }
778
779 /*
780  * Calendar week view -- not implemented yet, this is a stub function
781  */
782 void calendar_week_view(int year, int month, int day) {
783         wprintf("<center><i>week view FIXME</i></center><br />\n");
784 }
785
786
787 /*
788  * display one day
789  * Display events for a particular hour of a particular day.
790  * (Specify hour < 0 to show "all day" events)
791  *
792  * dstart and dend indicate which hours our "daytime" begins and end
793  */
794 void calendar_day_view_display_events(time_t thetime,
795         int year,
796         int month,
797         int day,
798         int notime_events,
799         int dstart,
800         int dend)
801 {
802         long hklen;
803         const char *HashKey;
804         void *vCal;
805         HashPos *Pos;
806         icalproperty *p = NULL;
807         icalproperty *q = NULL;
808         time_t event_tt;
809         time_t event_tte;
810         struct tm event_te;
811         struct tm event_tm;
812         int show_event = 0;
813         int all_day_event = 0;
814         int ongoing_event = 0;
815         wcsession *WCC = WC;
816         disp_cal *Cal;
817         struct icaltimetype t;
818         struct icaltimetype end_t;
819         struct icaltimetype today_start_t;
820         struct icaltimetype today_end_t;
821         struct icaltimetype today_t;
822         struct tm starting_tm;
823         struct tm ending_tm;
824         int top = 0;
825         int bottom = 0;
826         int gap = 1;
827         int startmin = 0;
828         int diffmin = 0;
829         int endmin = 0;
830
831         char buf[256];
832
833         if (GetCount(WCC->disp_cal_items) == 0) {
834                 /* nothing to display */
835                 return;
836         }
837
838         /* Create an imaginary event which spans the current day.  Any events which
839          * overlap with this one take place at least partially in this day.
840          */
841         memset(&starting_tm, 0, sizeof(struct tm));
842         starting_tm.tm_year = year - 1900;
843         starting_tm.tm_mon = month - 1;
844         starting_tm.tm_mday = day;
845         starting_tm.tm_hour = 0;
846         starting_tm.tm_min = 0;
847         today_start_t = icaltime_from_timet_with_zone(mktime(&starting_tm), 0, icaltimezone_get_utc_timezone());
848         today_start_t.is_utc = 1;
849
850         memset(&ending_tm, 0, sizeof(struct tm));
851         ending_tm.tm_year = year - 1900;
852         ending_tm.tm_mon = month - 1;
853         ending_tm.tm_mday = day;
854         ending_tm.tm_hour = 23;
855         ending_tm.tm_min = 59;
856         today_end_t = icaltime_from_timet_with_zone(mktime(&ending_tm), 0, icaltimezone_get_utc_timezone());
857         today_end_t.is_utc = 1;
858
859         /*
860          * Create another one without caring about the timezone for all day events.
861          */
862         today_t = icaltime_null_date();
863         today_t.year = year;
864         today_t.month = month;
865         today_t.day = day;
866
867         /* Now loop through our list of events to see which ones occur today.
868          */
869         Pos = GetNewHashPos(WCC->disp_cal_items, 0);
870         while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
871                 Cal = (disp_cal*)vCal;
872
873                 all_day_event = 0;
874                 ongoing_event=0;
875
876                 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
877                 if (q != NULL) {
878                         t = icalproperty_get_dtstart(q);
879                         event_tt = icaltime_as_timet(t);
880                         localtime_r(&event_tt, &event_te);
881                 }
882                 else {
883                         memset(&t, 0, sizeof t);
884                 }
885
886                 if (t.is_date) all_day_event = 1;
887
888                 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
889                 if (q != NULL) {
890                         end_t = icalproperty_get_dtend(q);
891                 }
892                 else {
893                         /* no end given means end = start */
894                         memcpy(&end_t, &t, sizeof(struct icaltimetype));
895                 }
896
897                 if (all_day_event)
898                 {
899                         show_event = ical_ctdl_is_overlap(t, end_t, today_t, icaltime_null_time());
900                         if (icaltime_compare(t, end_t)) {
901                                 /*
902                                  * the end date is non-inclusive so adjust it by one
903                                  * day because our test is inclusive, note that a day is
904                                  * not to much because we are talking about all day
905                                  * events
906                                  */
907                                 icaltime_adjust(&end_t, -1, 0, 0, 0);
908                         }
909                 }
910                 else
911                 {
912                         show_event = ical_ctdl_is_overlap(t, end_t, today_start_t, today_end_t);
913                 }
914
915                 event_tte = icaltime_as_timet(end_t);
916                 localtime_r(&event_tte, &event_tm);
917
918                 /* If we determined that this event occurs today, then display it.
919                  */
920                 p = icalcomponent_get_first_property(Cal->cal,ICAL_SUMMARY_PROPERTY);
921                 if (p == NULL) {
922                         p = icalproperty_new_summary(_("Untitled Event"));
923                         icalcomponent_add_property(Cal->cal, p);
924                 }
925
926                 if ((show_event) && (p != NULL)) {
927
928                         if ((event_te.tm_mday != day) || (event_tm.tm_mday != day)) ongoing_event = 1; 
929
930                         if (all_day_event && notime_events)
931                         {
932                                 wprintf("<li class=\"event_framed%s\"> "
933                                         "<a href=\"display_edit_event?"
934                                         "msgnum=%ld?calview=day?year=%d?month=%d?day=%d\" "
935                                         " class=\"event_title\" "
936                                         " btt_tooltext=\"",
937                                         (Cal->unread)?"_unread":"_read",
938                                         Cal->cal_msgnum, year, month, day);
939                                 wprintf("<i>%s</i><br />",      _("All day event"));
940                                 wprintf("<i>%s: %s</i><br />",  _("From"), Cal->from);
941                                 wprintf("<i>%s</i> ",           _("Summary:"));
942                                 escputs((char *) icalproperty_get_comment(p));
943                                 wprintf("<br />");
944                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_LOCATION_PROPERTY);
945                                 if (q) {
946                                         wprintf("<i>%s</i> ", _("Location:"));
947                                         escputs((char *)icalproperty_get_comment(q));
948                                         wprintf("<br />");
949                                 }
950                                 if (!icaltime_compare(t, end_t)) { /* one day only */
951                                         webcit_fmt_date(buf, 256, event_tt, DATEFMT_LOCALEDATE);
952                                         wprintf("<i>%s</i> %s<br>", _("Date:"), buf);
953                                 }
954                                 else {
955                                         webcit_fmt_date(buf, 256, event_tt, DATEFMT_LOCALEDATE);
956                                         wprintf("<i>%s</i> %s<br>", _("Starting date:"), buf);
957                                         webcit_fmt_date(buf, 256, event_tte, DATEFMT_LOCALEDATE);
958                                         wprintf("<i>%s</i> %s<br>", _("Ending date:"), buf);
959                                 }
960                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
961                                 if (q) {
962                                         wprintf("<i>%s</i> ", _("Notes:"));
963                                         escputs((char *)icalproperty_get_comment(q));
964                                         wprintf("<br />");
965                                 }
966                                 wprintf("\">");
967                                 escputs((char *) icalproperty_get_comment(p));
968                                 wprintf("</a> <span>(");
969                                 wprintf(_("All day event"));
970                                 wprintf(")</span></li>\n");
971                         }
972                         else if (ongoing_event && notime_events) 
973                         {
974                                 wprintf("<li class=\"event_framed%s\"> "
975                                         "<a href=\"display_edit_event?"
976                                         "msgnum=%ld&calview=day?year=%d?month=%d?day=%d\" "
977                                         " class=\"event_title\" " 
978                                         "btt_tooltext=\"",
979                                         (Cal->unread)?"_unread":"_read",
980                                         Cal->cal_msgnum, year, month, day);
981                                 wprintf("<i>%s</i><br />",     _("Ongoing event"));
982                                 wprintf("<i>%s: %s</i><br />", _("From"), Cal->from);
983                                 wprintf("<i>%s</i> ",          _("Summary:"));
984                                 escputs((char *) icalproperty_get_comment(p));
985                                 wprintf("<br />");
986                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_LOCATION_PROPERTY);
987                                 if (q) {
988                                         wprintf("<i>%s</i> ", _("Location:"));
989                                         escputs((char *)icalproperty_get_comment(q));
990                                         wprintf("<br />");
991                                                                 }
992                                 webcit_fmt_date(buf, 256, event_tt, DATEFMT_BRIEF);
993                                 wprintf("<i>%s</i> %s<br>", _("Starting date/time:"), buf);
994                                 webcit_fmt_date(buf, 256, event_tte, DATEFMT_BRIEF);
995                                 wprintf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
996                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
997                                 if (q) {
998                                         wprintf("<i>%s</i> ", _("Notes:"));
999                                         escputs((char *)icalproperty_get_comment(q));
1000                                         wprintf("<br />");
1001                                 }
1002                                 wprintf("\">");
1003                                 escputs((char *) icalproperty_get_comment(p));
1004                                 wprintf("</a> <span>(");
1005                                 wprintf(_("Ongoing event"));
1006                                 wprintf(")</span></li>\n");
1007                         }
1008                         else if (!all_day_event && !notime_events)
1009                         {
1010                                 gap++;
1011
1012                                 if (event_te.tm_mday != day) event_te.tm_hour = 0;
1013                                 if (event_tm.tm_mday != day) event_tm.tm_hour = 24;
1014
1015                                 /* Calculate the location of the top of the box */
1016                                 if (event_te.tm_hour < dstart) {
1017                                         startmin = diffmin = event_te.tm_min / 6;
1018                                         top = (event_te.tm_hour * EXTRATIMELINE) + startmin;
1019                                 }
1020                                 else if ((event_te.tm_hour >= dstart) && (event_te.tm_hour <= dend)) {
1021                                         startmin = diffmin = (event_te.tm_min / 2);
1022                                         top = (dstart * EXTRATIMELINE) + ((event_te.tm_hour - dstart) * TIMELINE) + startmin;
1023                                 }
1024                                 else if (event_te.tm_hour >dend) {
1025                                         startmin = diffmin = event_te.tm_min / 6;
1026                                         top = (dstart * EXTRATIMELINE) + ((dend - dstart - 1) * TIMELINE) + ((event_tm.tm_hour - dend + 1) * EXTRATIMELINE) + startmin ;
1027                                 }
1028                                 else {
1029                                         /* should never get here */
1030                                 }
1031
1032                                 /* Calculate the location of the bottom of the box */
1033                                 if (event_tm.tm_hour < dstart) {
1034                                         endmin = diffmin = event_tm.tm_min / 6;
1035                                         bottom = (event_tm.tm_hour * EXTRATIMELINE) + endmin;
1036                                 }
1037                                 else if ((event_tm.tm_hour >= dstart) && (event_tm.tm_hour <= dend)) {
1038                                         endmin = diffmin = (event_tm.tm_min / 2);
1039                                         bottom = (dstart * EXTRATIMELINE) + ((event_tm.tm_hour - dstart) * TIMELINE) + endmin ;
1040                                 }
1041                                 else if (event_tm.tm_hour >dend) {
1042                                         endmin = diffmin = event_tm.tm_min / 6;
1043                                         bottom = (dstart * EXTRATIMELINE) + ((dend - dstart + 1) * TIMELINE) + ((event_tm.tm_hour - dend - 1) * EXTRATIMELINE) + endmin;
1044                                 }
1045                                 else {
1046                                         /* should never get here */
1047                                 }
1048
1049                                 wprintf("<dd  class=\"event_framed%s\" "
1050                                         "style=\"position: absolute; "
1051                                         "top:%dpx; left:%dpx; "
1052                                         "height:%dpx; \" >",
1053                                         (Cal->unread)?"_unread":"_read",
1054                                         top, (gap * 40), (bottom-top)
1055                                         );
1056                                 wprintf("<a href=\"display_edit_event?"
1057                                         "msgnum=%ld?calview=day?year=%d?month=%d?day=%d?hour=%d\" "
1058                                         "class=\"event_title\" "
1059                                         "btt_tooltext=\"",
1060                                         Cal->cal_msgnum, year, month, day, t.hour);
1061                                 wprintf("<i>%s: %s</i><br />", _("From"), Cal->from);
1062                                 wprintf("<i>%s</i> ",          _("Summary:"));
1063                                 escputs((char *) icalproperty_get_comment(p));
1064                                 wprintf("<br />");
1065                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_LOCATION_PROPERTY);
1066                                 if (q) {
1067                                         wprintf("<i>%s</i> ", _("Location:"));
1068                                         escputs((char *)icalproperty_get_comment(q));
1069                                         wprintf("<br />");
1070                                                                 }
1071                                 if (!icaltime_compare(t, end_t)) { /* one day only */
1072                                         webcit_fmt_date(buf, 256, event_tt, DATEFMT_BRIEF);
1073                                         wprintf("<i>%s</i> %s<br>", _("Date/time:"), buf);
1074                                 }
1075                                 else {
1076                                         webcit_fmt_date(buf, 256, event_tt, DATEFMT_BRIEF);
1077                                         wprintf("<i>%s</i> %s<br>", _("Starting date/time:"), buf);
1078                                         webcit_fmt_date(buf, 256, event_tte, DATEFMT_BRIEF);
1079                                         wprintf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
1080                                 }
1081                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
1082                                 if (q) {
1083                                         wprintf("<i>%s</i> ", _("Notes:"));
1084                                         escputs((char *)icalproperty_get_comment(q));
1085                                         wprintf("<br />");
1086                                 }
1087                                 wprintf("\">");
1088
1089                                 escputs((char *) icalproperty_get_comment(p));
1090                                 wprintf("</a></dd>\n");
1091                         }
1092                 }
1093         }
1094         DeleteHashPos(&Pos);
1095 }
1096
1097 /*
1098  * view one day
1099  */
1100 void calendar_day_view(int year, int month, int day) {
1101         int hour;
1102         struct icaltimetype today, yesterday, tomorrow;
1103         long daystart;
1104         long dayend;
1105         struct tm d_tm;
1106         char d_str[128];
1107         int time_format;
1108         time_t today_t;
1109         int timeline = TIMELINE;
1110         int extratimeline = EXTRATIMELINE;
1111         int gap = 0;
1112         int hourlabel;
1113         int extrahourlabel;
1114
1115         time_format = get_time_format_cached ();
1116         get_pref_long("daystart", &daystart, 8);
1117         get_pref_long("dayend", &dayend, 17);
1118
1119         /* when loading daystart/dayend, replace missing, corrupt, or impossible values with defaults */
1120         if ((daystart < 0) || (dayend < 2) || (daystart >= 23) || (dayend > 23) || (dayend <= daystart)) {
1121                 daystart = 9;
1122                 dayend = 17;
1123         }
1124         
1125         /* Today's date */
1126         memset(&d_tm, 0, sizeof d_tm);
1127         d_tm.tm_year = year - 1900;
1128         d_tm.tm_mon = month - 1;
1129         d_tm.tm_mday = day;
1130         today_t = mktime(&d_tm); 
1131
1132         /* Figure out the dates for "yesterday" and "tomorrow" links */
1133
1134         memset(&today, 0, sizeof(struct icaltimetype));
1135         today.year = year;
1136         today.month = month;
1137         today.day = day;
1138         today.is_date = 1;
1139
1140         memcpy(&yesterday, &today, sizeof(struct icaltimetype));
1141         --yesterday.day;
1142         yesterday = icaltime_normalize(yesterday);
1143
1144         memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
1145         ++tomorrow.day;
1146         tomorrow = icaltime_normalize(tomorrow);
1147
1148         wprintf("<div class=\"fix_scrollbar_bug\">");
1149
1150         /* Inner table (the real one) */
1151         wprintf("<table class=\"calendar\" id=\"inner_day\"><tr> \n");
1152
1153         /* Innermost cell (contains hours etc.) */
1154         wprintf("<td class=\"events_of_the_day\" >");
1155         wprintf("<dl class=\"events\" >");
1156
1157         /* Now the middle of the day... */
1158
1159         extrahourlabel = extratimeline - 2;
1160         hourlabel = extrahourlabel * 150 / 100;
1161         if (hourlabel > (timeline - 2)) hourlabel = timeline - 2;
1162
1163         for (hour = 0; hour < daystart; ++hour) {       /* could do HEIGHT=xx */
1164                 wprintf("<dt class=\"extrahour\"        "
1165                         "style=\"               "
1166                         "position: absolute;    "
1167                         "top: %dpx; left: 0px;  "
1168                         "height: %dpx;          "
1169                         "font-size: %dpx;       "
1170                         "\" >                   "
1171                         "<a href=\"display_edit_event?msgnum=0"
1172                         "?calview=day?year=%d?month=%d?day=%d?hour=%d?minute=0\">",
1173                         (hour * extratimeline ),
1174                         extratimeline,
1175                         extrahourlabel,
1176                         year, month, day, hour
1177                         );
1178
1179                 if (time_format == WC_TIMEFORMAT_24) {
1180                         wprintf("%2d:00</a> ", hour);
1181                 }
1182                 else {
1183                         wprintf("%d:00%s</a> ",
1184                                 ((hour == 0) ? 12 : (hour <= 12 ? hour : hour-12)),
1185                                 (hour < 12 ? "am" : "pm")
1186                                 );
1187                 }
1188
1189                 wprintf("</dt>");
1190         }
1191
1192         gap = daystart * extratimeline;
1193
1194         for (hour = daystart; hour <= dayend; ++hour) {       /* could do HEIGHT=xx */
1195                 wprintf("<dt class=\"hour\"     "
1196                         "style=\"               "
1197                         "position: absolute;    "
1198                         "top: %ldpx; left: 0px; "
1199                         "height: %dpx;          "
1200                         "font-size: %dpx;       "
1201                         "\" >                   "
1202                         "<a href=\"display_edit_event?msgnum=0?calview=day"
1203                         "?year=%d?month=%d?day=%d?hour=%d?minute=0\">",
1204                         gap + ((hour - daystart) * timeline ),
1205                         timeline,
1206                         hourlabel,
1207                         year, month, day, hour
1208                         );
1209
1210                 if (time_format == WC_TIMEFORMAT_24) {
1211                         wprintf("%2d:00</a> ", hour);
1212                 }
1213                 else {
1214                         wprintf("%d:00%s</a> ",
1215                                 (hour <= 12 ? hour : hour-12),
1216                                 (hour < 12 ? "am" : "pm")
1217                                                 );
1218                 }
1219
1220                 wprintf("</dt>");
1221         }
1222
1223         gap = gap + ((dayend - daystart + 1) * timeline);
1224
1225         for (hour = (dayend + 1); hour < 24; ++hour) {       /* could do HEIGHT=xx */
1226                 wprintf("<dt class=\"extrahour\"     "
1227                         "style=\"               "
1228                         "position: absolute;    "
1229                         "top: %ldpx; left: 0px; "
1230                         "height: %dpx;          "
1231                         "font-size: %dpx;       "
1232                         "\" >                   "
1233                         "<a href=\"display_edit_event?msgnum=0?calview=day"
1234                         "?year=%d?month=%d?day=%d?hour=%d?minute=0\">",
1235                         gap + ((hour - dayend - 1) * extratimeline ),
1236                         extratimeline,
1237                         extrahourlabel,
1238                         year, month, day, hour
1239                 );
1240
1241                 if (time_format == WC_TIMEFORMAT_24) {
1242                         wprintf("%2d:00</a> ", hour);
1243                 }
1244                 else {
1245                         wprintf("%d:00%s</a> ",
1246                                 (hour <= 12 ? hour : hour-12),
1247                                 (hour < 12 ? "am" : "pm")
1248                         );
1249                 }
1250
1251                 wprintf("</dt>");
1252         }
1253
1254         /* Display events with start and end times on this day */
1255         calendar_day_view_display_events(today_t, year, month, day, 0, daystart, dayend);
1256
1257         wprintf("</dl>");
1258         wprintf("</td>");                       /* end of innermost table */
1259
1260         /* Display extra events (start/end times not present or not today) in the middle column */
1261         wprintf("<td class=\"extra_events\">");
1262
1263         wprintf("<ul>");
1264
1265         /* Display all-day events */
1266         calendar_day_view_display_events(today_t, year, month, day, 1, daystart, dayend);
1267
1268         wprintf("</ul>");
1269
1270         wprintf("</td>");       /* end extra on the middle */
1271
1272         wprintf("<td width=20%% align=center valign=top>");     /* begin stuff-on-the-right */
1273
1274         /* Begin todays-date-with-left-and-right-arrows */
1275         wprintf("<table border=0 width=100%% "
1276                 "cellspacing=0 cellpadding=0 bgcolor=\"#FFFFFF\">\n");
1277         wprintf("<tr>");
1278
1279         /* Left arrow */        
1280         wprintf("<td align=center>");
1281         wprintf("<a href=\"readfwd?calview=day?year=%d?month=%d?day=%d\">",
1282                 yesterday.year, yesterday.month, yesterday.day);
1283         wprintf("<img align=middle src=\"static/prevdate_32x.gif\" border=0></A>");
1284         wprintf("</td>");
1285
1286         wc_strftime(d_str, sizeof d_str,
1287                 "<td align=center>"
1288                 "<font size=+2>%B</font><br />"
1289                 "<font size=+3>%d</font><br />"
1290                 "<font size=+2>%Y</font><br />"
1291                 "</td>",
1292                 &d_tm
1293                 );
1294         wprintf("%s", d_str);
1295
1296         /* Right arrow */
1297         wprintf("<td align=center>");
1298         wprintf("<a href=\"readfwd?calview=day?year=%d?month=%d?day=%d\">",
1299                 tomorrow.year, tomorrow.month, tomorrow.day);
1300         wprintf("<img align=middle src=\"static/nextdate_32x.gif\""
1301                 " border=0></a>\n");
1302         wprintf("</td>");
1303
1304         wprintf("</tr></table>\n");
1305         /* End todays-date-with-left-and-right-arrows */
1306
1307         /* Embed a mini month calendar in this space */
1308         wprintf("<br />\n");
1309         embeddable_mini_calendar(year, month);
1310
1311         wprintf("</font></center>\n");
1312
1313         wprintf("</td></tr>");                  /* end stuff-on-the-right */
1314
1315         wprintf("</table>"                      /* end of inner table */
1316                 "</div>");
1317
1318         StrBufAppendPrintf(WC->trailing_javascript,
1319                 " setTimeout(\"btt_enableTooltips('inner_day')\", 1);   \n"
1320         );
1321 }
1322
1323
1324 /*
1325  * Display today's events.  Returns the number of items displayed.
1326  */
1327 int calendar_summary_view(void) {
1328         long hklen;
1329         const char *HashKey;
1330         void *vCal;
1331         HashPos *Pos;
1332         disp_cal *Cal;
1333         icalproperty *p;
1334         struct icaltimetype t;
1335         time_t event_tt;
1336         struct tm event_tm;
1337         struct tm today_tm;
1338         time_t now;
1339         int all_day_event = 0;
1340         char timestring[SIZ];
1341         wcsession *WCC = WC;
1342         int num_displayed = 0;
1343
1344         if (GetCount(WC->disp_cal_items) == 0) {
1345                 return(0);
1346         }
1347
1348         now = time(NULL);
1349         localtime_r(&now, &today_tm);
1350
1351         Pos = GetNewHashPos(WCC->disp_cal_items, 0);
1352         while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
1353                 Cal = (disp_cal*)vCal;
1354                 p = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
1355                 if (p != NULL) {
1356                         t = icalproperty_get_dtstart(p);
1357                         event_tt = icaltime_as_timet(t);
1358                         if (t.is_date) {
1359                                 all_day_event = 1;
1360                         }
1361                         else {
1362                                 all_day_event = 0;
1363                         }
1364                         fmt_time(timestring, SIZ, event_tt);
1365
1366                         if (all_day_event) {
1367                                 gmtime_r(&event_tt, &event_tm);
1368                         }
1369                         else {
1370                                 localtime_r(&event_tt, &event_tm);
1371                         }
1372
1373                         if ( (event_tm.tm_year == today_tm.tm_year)
1374                                 && (event_tm.tm_mon == today_tm.tm_mon)
1375                                 && (event_tm.tm_mday == today_tm.tm_mday)
1376                         ) {
1377
1378                                 p = icalcomponent_get_first_property(Cal->cal, ICAL_SUMMARY_PROPERTY);
1379                                 if (p == NULL) {
1380                                         p = icalproperty_new_summary(_("Untitled Task"));
1381                                         icalcomponent_add_property(Cal->cal, p);
1382                                 }
1383                                 if (p != NULL) {
1384
1385
1386                                         if (WCC->wc_view == VIEW_TASKS) {
1387                                                 wprintf("<a href=\"display_edit_task"
1388                                                         "?msgnum=%ld"
1389                                                         "?return_to_summary=1"
1390                                                         "?gotofirst=",
1391                                                         Cal->cal_msgnum
1392                                                 );
1393                                                 escputs(ChrPtr(WCC->wc_roomname));
1394                                                 wprintf("\">");
1395                                         }
1396                                         else {
1397                                                 wprintf("<a href=\"display_edit_event"
1398                                                         "?msgnum=%ld"
1399                                                         "?calview=summary"
1400                                                         "?year=%d"
1401                                                         "?month=%d"
1402                                                         "?day=%d"
1403                                                         "?gotofirst=",
1404                                                         Cal->cal_msgnum,
1405                                                         today_tm.tm_year + 1900,
1406                                                         today_tm.tm_mon + 1,
1407                                                         today_tm.tm_mday
1408                                                 );
1409                                                 escputs(ChrPtr(WCC->wc_roomname));
1410                                                 wprintf("\">");
1411                                         }
1412                                         escputs((char *) icalproperty_get_comment(p));
1413                                         if (!all_day_event) {
1414                                                 wprintf(" (%s)", timestring);
1415                                         }
1416                                         wprintf("</a><br />\n");
1417                                         ++num_displayed;
1418                                 }
1419                         }
1420                 }
1421         }
1422         DeleteHashPos(&Pos);
1423         DeleteHash(&WC->disp_cal_items);
1424         return(num_displayed);
1425 }
1426
1427 /*
1428  * Parse the URL variables in order to determine the scope and display of a calendar view
1429  */
1430 int calendar_GetParamsGetServerCall(SharedMessageStatus *Stat, 
1431                                     void **ViewSpecific, 
1432                                     long oper, 
1433                                     char *cmd, 
1434                                     long len)
1435 {
1436         calview *c;
1437         time_t now;
1438         struct tm tm;
1439         char cv[32];
1440
1441         int span = 3888000;
1442
1443         c = (calview*) malloc(sizeof(calview));
1444         memset(c, 0, sizeof(calview));
1445         *ViewSpecific = (void*)c;
1446
1447         Stat->load_seen = 1;
1448         strcpy(cmd, "MSGS ALL");
1449         Stat->maxmsgs = 32767;
1450         
1451         /* In case no date was specified, go with today */
1452         now = time(NULL);
1453         localtime_r(&now, &tm);
1454         c->year = tm.tm_year + 1900;
1455         c->month = tm.tm_mon + 1;
1456         c->day = tm.tm_mday;
1457
1458         /* Now see if a date was specified */
1459         if (havebstr("year")) c->year = ibstr("year");
1460         if (havebstr("month")) c->month = ibstr("month");
1461         if (havebstr("day")) c->day = ibstr("day");
1462
1463         /* How would you like that cooked? */
1464         if (havebstr("calview")) {
1465                 strcpy(cv, bstr("calview"));
1466         }
1467         else {
1468                 strcpy(cv, "month");
1469         }
1470
1471         /* Display the selected view */
1472         if (!strcasecmp(cv, "day")) {
1473                 c->view = calview_day;
1474         }
1475         else if (!strcasecmp(cv, "week")) {
1476                 c->view = calview_week;
1477         }
1478         else if (!strcasecmp(cv, "summary")) {  /* shouldn't ever happen, but just in case */
1479                 c->view = calview_day;
1480         }
1481         else {
1482                 if (WC->wc_view == VIEW_CALBRIEF) {
1483                         c->view = calview_brief;
1484                 }
1485                 else {
1486                         c->view = calview_month;
1487                 }
1488         }
1489
1490         /* Now try and set the lower and upper bounds so that we don't
1491          * burn too many cpu cycles parsing data way in the past or future
1492          */
1493
1494         tm.tm_year = c->year - 1900;
1495         tm.tm_mon = c->month - 1;
1496         tm.tm_mday = c->day;
1497         now = mktime(&tm);
1498
1499         if (c->view == calview_month)   span = 3888000;
1500         if (c->view == calview_brief)   span = 3888000;
1501         if (c->view == calview_week)    span = 604800;
1502         if (c->view == calview_day)     span = 86400;
1503         if (c->view == calview_summary) span = 86400;
1504
1505         c->lower_bound = now - span;
1506         c->upper_bound = now + span;
1507         return 200;
1508 }
1509
1510
1511
1512 /*
1513  * Render a calendar view from data previously loaded into memory
1514  */
1515 int calendar_RenderView_or_Tail(SharedMessageStatus *Stat, 
1516                                 void **ViewSpecific, 
1517                                 long oper)
1518 {
1519         calview *c = (calview*) *ViewSpecific;
1520
1521         if (c->view == calview_day) {
1522                 calendar_day_view(c->year, c->month, c->day);
1523         }
1524         else if (c->view == calview_week) {
1525                 calendar_week_view(c->year, c->month, c->day);
1526         }
1527         else {
1528                 if (WC->wc_view == VIEW_CALBRIEF) {
1529                         calendar_brief_month_view(c->year, c->month, c->day);
1530                 }
1531                 else {
1532                         calendar_month_view(c->year, c->month, c->day);
1533                 }
1534         }
1535
1536         /* Free the in-memory list of calendar items */
1537         DeleteHash(&WC->disp_cal_items);
1538         return 0;
1539 }
1540
1541
1542
1543
1544