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