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