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