Added a preference setting to allow each user to choose
[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  * \brief Display one day of a whole month view of a calendar
18  * \param thetime the month we want to see 
19  */
20 void calendar_month_view_display_events(time_t thetime) {
21         int i;
22         struct tm today_tm;
23         icalproperty *p = NULL;
24         icalproperty *q = NULL;
25         struct icaltimetype t;
26         int month, day, year;
27         int all_day_event = 0;
28         int multi_day_event = 0;
29         int fill_day_event = 0;
30         int show_event = 0;
31         time_t tt;
32         time_t dst_day;
33         char buf[256];
34         struct wcsession *WCC;
35         struct disp_cal *Cal;
36
37         WCC = WC;
38
39         if (WCC->num_cal == 0) {
40                 wprintf("<br /><br /><br />\n");
41                 return;
42         }
43
44         localtime_r(&thetime, &today_tm);
45         month = today_tm.tm_mon + 1;
46         day = today_tm.tm_mday;
47         year = today_tm.tm_year + 1900;
48         dst_day = thetime - 3600;
49         for (i=0; i<(WCC->num_cal); ++i) {
50                 fill_day_event = 0;
51                 multi_day_event = 0;
52
53                 Cal = &WCC->disp_cal[i];
54                 all_day_event =  Cal->start_hour == -1;
55                 show_event = thetime == Cal->start_day;
56 //              lprintf(1,"Date: %d %d %d %d\n", thetime, Cal->start_day, day, thetime - Cal->start_day);
57                 if (!all_day_event) {
58
59                         // are we in the range of the event?
60                         show_event = (Cal->start_day <= thetime) && 
61                                 (Cal->end_day >= thetime);
62                         if (!show_event)
63                                 // are we in the range of the event?
64                                 show_event = (Cal->start_day <= dst_day) && 
65                                         (Cal->end_day >= dst_day);
66
67                         // are we not start or end day?
68                         fill_day_event = (Cal->start_day < thetime) && 
69                                 (Cal->end_day > thetime);
70                 }
71
72                 if (show_event) {
73                         p = icalcomponent_get_first_property(
74                                 Cal->cal,
75                                 ICAL_SUMMARY_PROPERTY);
76                         if (p != NULL) {
77
78                                 if (all_day_event) {
79                                         wprintf("<table border=0 cellpadding=2><TR>"
80                                                 "<td bgcolor=\"#CCCCDD\">"
81                                                 );
82                                 }
83
84                                 wprintf("<font size=-1>"
85                                         "<a href=\"display_edit_event?"
86                                         "msgnum=%ld&calview=%s&year=%s&month=%s&day=%s\""
87                                         " btt_tooltext=\"",
88                                         WC->disp_cal[i].cal_msgnum,
89                                         bstr("calview"),
90                                         bstr("year"),
91                                         bstr("month"),
92                                         bstr("day")
93                                         );
94
95                                 wprintf("<i>%s</i> ", _("Summary:"));
96                                 escputs((char *)icalproperty_get_comment(p));
97                                 wprintf("<br />");
98                                 
99                                 q = icalcomponent_get_first_property(
100                                         WC->disp_cal[i].cal,
101                                         ICAL_LOCATION_PROPERTY);
102                                 if (q) {
103                                         wprintf("<i>%s</i> ", _("Location:"));
104                                         escputs((char *)icalproperty_get_comment(q));
105                                         wprintf("<br />");
106                                         }
107                                 
108                                 /**
109                                  * Only show start/end times if we're actually looking at the VEVENT
110                                  * component.  Otherwise it shows bogus dates for e.g. timezones
111                                  */
112                                 if (icalcomponent_isa(Cal->cal) == ICAL_VEVENT_COMPONENT) {
113                                         
114                                         q = icalcomponent_get_first_property(Cal->cal,
115                                                                              ICAL_DTSTART_PROPERTY);
116                                         if (q != NULL) {
117                                                 t = icalproperty_get_dtstart(q);
118                                                 
119                                                 if (t.is_date) {
120                                                         struct tm d_tm;
121                                                         char d_str[32];
122                                                         memset(&d_tm, 0, sizeof d_tm);
123                                                         d_tm.tm_year = t.year - 1900;
124                                                         d_tm.tm_mon = t.month - 1;
125                                                         d_tm.tm_mday = t.day;
126                                                         wc_strftime(d_str, sizeof d_str, "%x", &d_tm);
127                                                         wprintf("<i>%s</i> %s<br>",
128                                                                 _("Date:"), d_str);
129                                                 }
130                                                 else {
131                                                         tt = icaltime_as_timet(t);
132                                                         fmt_date(buf, tt, 1);
133                                                         wprintf("<i>%s</i> %s<br>",
134                                                                 _("Starting date/time:"), buf);
135                                                         
136                                                         /* Embed the 'show end date/time' loop inside here so it
137                                                          * only executes if this is NOT an all day event.
138                                                          */
139                                                         q = icalcomponent_get_first_property(Cal->cal,
140                                                                                              ICAL_DTEND_PROPERTY);
141                                                         if (q != NULL) {
142                                                                 t = icalproperty_get_dtend(q);
143                                                                 tt = icaltime_as_timet(t);
144                                                                 fmt_date(buf, tt, 1);
145                                                                 wprintf("<i>%s</i> %s<br>",
146                                                                         _("Ending date/time:"), buf);
147                                                         }
148                                                         
149                                                 }
150                                         }
151                                         
152                                 }
153                                 
154                                 q = icalcomponent_get_first_property(
155                                         Cal->cal,
156                                         ICAL_DESCRIPTION_PROPERTY);
157                                 if (q) {
158                                         wprintf("<i>%s</i> ", _("Notes:"));
159                                         escputs((char *)icalproperty_get_comment(q));
160                                         wprintf("<br />");
161                                 }
162                                 
163                                 wprintf("\">");
164                                 escputs((char *)
165                                         icalproperty_get_comment(p));
166                                 wprintf("</a></font><br />\n");
167                                 
168                                 if (all_day_event) {
169                                         wprintf("</td></tr></table>");
170                                 }
171                                 
172                         }
173                         
174                 }
175                 
176                 
177         }
178 }
179
180
181 /**
182  * \brief Display one day of a whole month view of a calendar
183  * \param thetime the month we want to see 
184  */
185 void calendar_month_view_brief_events(time_t thetime, const char *daycolor) {
186         int i;
187         time_t event_tt;
188         time_t event_tts;
189         time_t event_tte;
190         struct tm event_tms;
191         struct tm event_tme;
192         struct tm today_tm;
193         icalproperty *p;
194         icalproperty *e;
195         struct icaltimetype t;
196         int month, day, year;
197         int all_day_event = 0;
198         char *timeformat;
199         int time_format;
200         
201         time_format = get_time_format_cached ();
202
203         if (time_format == WC_TIMEFORMAT_24) timeformat="%k:%M";
204         else timeformat="%I:%M %p";
205
206         localtime_r(&thetime, &today_tm);
207         month = today_tm.tm_mon + 1;
208         day = today_tm.tm_mday;
209         year = today_tm.tm_year + 1900;
210
211         for (i=0; i<(WC->num_cal); ++i) {
212                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
213                                                 ICAL_DTSTART_PROPERTY);
214                 if (p != NULL) {
215                         t = icalproperty_get_dtstart(p);
216                         event_tt = icaltime_as_timet(t);
217                         event_tts=event_tt;
218                         if (t.is_date) all_day_event = 1;
219                         else all_day_event = 0;
220
221                         if (all_day_event) {
222                                 gmtime_r(&event_tts, &event_tms);
223                         }
224                         else {
225                                 localtime_r(&event_tts, &event_tms);
226                         }
227                         /** \todo epoch &! daymask */
228                         if ((event_tms.tm_year == today_tm.tm_year)
229                            && (event_tms.tm_mon == today_tm.tm_mon)
230                            && (event_tms.tm_mday == today_tm.tm_mday)) {
231                                 
232                                 
233                                 char sbuf[255];
234                                 char ebuf[255];
235
236                                 p = icalcomponent_get_first_property(
237                                                         WC->disp_cal[i].cal,
238                                                         ICAL_SUMMARY_PROPERTY);
239                                 e = icalcomponent_get_first_property(
240                                                         WC->disp_cal[i].cal, 
241                                                         ICAL_DTEND_PROPERTY);
242                                 if ((p != NULL) && (e != NULL)) {
243                                         time_t difftime;
244                                         int hours, minutes;
245                                         t = icalproperty_get_dtend(e);
246                                         event_tte = icaltime_as_timet(t);
247                                         localtime_r(&event_tte, &event_tme);
248                                         difftime=(event_tte-event_tts)/60;
249                                         hours=(int)(difftime / 60);
250                                         minutes=difftime % 60;
251                                         wprintf("<tr><td bgcolor='%s'>%i:%2i</td><td bgcolor='%s'>"
252                                                         "<font size=-1>"
253                                                         "<a href=\"display_edit_event?msgnum=%ld&calview=%s&year=%s&month=%s&day=%s\">",
254                                                         daycolor,
255                                                         hours, minutes,
256                                                         daycolor,
257                                                         WC->disp_cal[i].cal_msgnum,
258                                                         bstr("calview"),
259                                                         bstr("year"),
260                                                         bstr("month"),
261                                                         bstr("day")
262                                                         );
263
264                                         escputs((char *)
265                                                         icalproperty_get_comment(p));
266                                         /** \todo: allso ammitime format */
267                                         wc_strftime(&sbuf[0], sizeof(sbuf), timeformat, &event_tms);
268                                         wc_strftime(&ebuf[0], sizeof(sbuf), timeformat, &event_tme);
269
270                                         wprintf("</a></font></td>"
271                                                         "<td bgcolor='%s'>%s</td><td bgcolor='%s'>%s</td></tr>",
272                                                         daycolor,
273                                                         sbuf,
274                                                         daycolor,
275                                                         ebuf);
276                                         
277                                 }
278                                 
279                         }
280                         
281                         
282                 }
283         }
284 }
285
286
287 /**
288  * \brief view one month. pretty view
289  * \param year the year
290  * \param month the month
291  * \param day the actual day we want to see
292  */
293 void calendar_month_view(int year, int month, int day) {
294         struct tm starting_tm;
295         struct tm tm;
296         time_t thetime;
297         int i;
298         time_t previous_month;
299         time_t next_month;
300         time_t colheader_time;
301         struct tm colheader_tm;
302         char colheader_label[32];
303         int chg_month = 0;
304         int weekstart = 0;
305         char weekstart_buf[16];
306
307         /* Determine what day to start.
308          */
309         get_preference("weekstart", weekstart_buf, sizeof weekstart_buf);
310         weekstart = atoi(weekstart_buf);
311
312         /*
313          * Now back up to the 1st of the month...
314          */
315         memset(&starting_tm, 0, sizeof(struct tm));
316
317         starting_tm.tm_year = year - 1900;
318         starting_tm.tm_mon = month - 1;
319         starting_tm.tm_mday = day;
320         thetime = mktime(&starting_tm);
321
322         memcpy(&tm, &starting_tm, sizeof(struct tm));
323         while (tm.tm_mday != 1) {
324                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
325                 localtime_r(&thetime, &tm);
326         }
327
328         /** Determine previous and next months ... for links */
329         previous_month = thetime - (time_t)864000L;     /* back 10 days */
330         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
331
332         /** Now back up until we're on the user's preferred start day */
333         localtime_r(&thetime, &tm);
334         while (tm.tm_wday != weekstart) {
335                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
336                 localtime_r(&thetime, &tm);
337         }
338
339         /** Outer table (to get the background color) */
340         wprintf("<div class=\"fix_scrollbar_bug\">"
341                 "<table class=\"calendar\"> \n <tr><td>"); 
342
343         wprintf("<table width=100%% border=0 cellpadding=0 cellspacing=0><tr>\n");
344
345         wprintf("<td align=center>");
346
347         localtime_r(&previous_month, &tm);
348         wprintf("<a href=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
349                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
350         wprintf("<img align=middle src=\"static/prevdate_32x.gif\" border=0></A>\n");
351
352         wc_strftime(colheader_label, sizeof colheader_label, "%B", &starting_tm);
353         wprintf("&nbsp;&nbsp;"
354                 "<font size=+1 color=\"#FFFFFF\">"
355                 "%s %d"
356                 "</font>"
357                 "&nbsp;&nbsp;", colheader_label, year);
358
359         localtime_r(&next_month, &tm);
360         wprintf("<a href=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
361                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
362         wprintf("<img align=middle src=\"static/nextdate_32x.gif\" border=0></A>\n");
363
364         wprintf("</td></tr></table>\n");
365
366         /** Inner table (the real one) */
367         wprintf("<table width=100%% border=0 cellpadding=1 cellspacing=1 "
368                 "bgcolor=#204B78 id=\"inner_month\"><tr>");
369         colheader_time = thetime;
370         for (i=0; i<7; ++i) {
371                 colheader_time = thetime + (i * 86400) ;
372                 localtime_r(&colheader_time, &colheader_tm);
373                 wc_strftime(colheader_label, sizeof colheader_label, "%A", &colheader_tm);
374                 wprintf("<td align=center width=14%%>"
375                         "<font color=\"#FFFFFF\">%s</font></th>", colheader_label);
376
377         }
378         wprintf("</tr>\n");
379
380
381         /** Now do 35 or 42 days */
382         for (i = 0; i < 42; ++i) {
383                 localtime_r(&thetime, &tm);
384
385                 if ((i < 35) || (chg_month == 0)) {
386
387                         if ((i > 27) && ((tm.tm_mday == 1) || (tm.tm_mday == 31))) {
388                                 chg_month = 1;
389                         }
390                         if (i > 35) {
391                                 chg_month = 0;
392                         }
393
394                         /** Before displaying Sunday, start a new row */
395                         if ((i % 7) == 0) {
396                                 wprintf("<tr>");
397                         }
398
399                         wprintf("<td class=\"cal%s\"><div class=\"day\">",
400                                 ((tm.tm_mon != month-1) ? "out" :
401                                 ((tm.tm_wday==0 || tm.tm_wday==6) ? "weekend" :
402                                 "day"))
403                         );
404                         if ((i==0) || (tm.tm_mday == 1)) {
405                                 wc_strftime(colheader_label, sizeof colheader_label, "%B", &tm);
406                                 wprintf("%s ", colheader_label);
407                         }
408                         wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
409                                 "%d</a></div>",
410                                 tm.tm_year + 1900,
411                                 tm.tm_mon + 1,
412                                 tm.tm_mday,
413                                 tm.tm_mday);
414
415                         /** put the data here, stupid */
416                         calendar_month_view_display_events(thetime);
417
418                         wprintf("</td>");
419
420                         /** After displaying Saturday, end the row */
421                         if ((i % 7) == 6) {
422                                 wprintf("</tr>\n");
423                         }
424
425                 }
426
427                 thetime += (time_t)86400;               /** ahead 24 hours */
428         }
429
430         wprintf("</table>"                      /** end of inner table */
431                 "</td></tr></table>"            /** end of outer table */
432                 "</div>\n");
433
434         /**
435          * Initialize the bubble tooltips.
436          *
437          * Yes, this is as stupid as it looks.  Instead of just making the call
438          * to btt_enableTooltips() straight away, we have to create a timer event
439          * and let it initialize as an event after 1 millisecond.  This is to
440          * work around a bug in Internet Explorer that causes it to crash if we
441          * manipulate the innerHTML of various DOM nodes while the page is still
442          * being rendered.  See http://www.shaftek.org/blog/archives/000212.html
443          * for more information.
444          */ 
445         wprintf("<script type=\"text/javascript\">"
446                 " setTimeout(\"btt_enableTooltips('inner_month')\", 1); "
447                 "</script>\n"
448         );
449 }
450
451 /**
452  * \brief view one month. brief view
453  * \param year the year
454  * \param month the month
455  * \param day the actual day we want to see
456  */
457 void calendar_brief_month_view(int year, int month, int day) {
458         struct tm starting_tm;
459         struct tm tm;
460         time_t thetime;
461         int i;
462         time_t previous_month;
463         time_t next_month;
464         char month_label[32];
465
466         /** Determine what day to start.
467          * First, back up to the 1st of the month...
468          */
469         memset(&starting_tm, 0, sizeof(struct tm));
470         starting_tm.tm_year = year - 1900;
471         starting_tm.tm_mon = month - 1;
472         starting_tm.tm_mday = day;
473         thetime = mktime(&starting_tm);
474
475         memcpy(&tm, &starting_tm, sizeof(struct tm));
476         while (tm.tm_mday != 1) {
477                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
478                 localtime_r(&thetime, &tm);
479         }
480
481         /** Determine previous and next months ... for links */
482         previous_month = thetime - (time_t)864000L;     /* back 10 days */
483         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
484
485         /** Now back up until we're on a Sunday */
486         localtime_r(&thetime, &tm);
487         while (tm.tm_wday != 0) {
488                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
489                 localtime_r(&thetime, &tm);
490         }
491
492         /** Outer table (to get the background color) */
493         wprintf("<div class=\"fix_scrollbar_bug\">"
494                 "<table width=100%% border=0 cellpadding=0 cellspacing=0 "
495                 "bgcolor=#204B78><TR><TD>\n");
496
497         wprintf("<table width=100%% border=0 cellpadding=0 cellspacing=0><tr>\n");
498
499         wprintf("<td align=center>");
500
501         localtime_r(&previous_month, &tm);
502         wprintf("<a href=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
503                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
504         wprintf("<img align=middle src=\"static/prevdate_32x.gif\" border=0></A>\n");
505
506         wc_strftime(month_label, sizeof month_label, "%B", &tm);
507         wprintf("&nbsp;&nbsp;"
508                 "<font size=+1 color=\"#FFFFFF\">"
509                 "%s %d"
510                 "</font>"
511                 "&nbsp;&nbsp;", month_label, year);
512
513         localtime_r(&next_month, &tm);
514         wprintf("<a href=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
515                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
516         wprintf("<img align=middle src=\"static/nextdate_32x.gif\" border=0></A>\n");
517
518         wprintf("</td></tr></table>\n");
519
520         /** Inner table (the real one) */
521         wprintf("<table width=100%% border=0 cellpadding=1 cellspacing=1 "
522                 "bgcolor=#EEEECC><TR>");
523         wprintf("</tr>\n");
524         wprintf("<tr><td colspan=\"100%\">\n");
525
526         /** Now do 35 days */
527         for (i = 0; i < 35; ++i) {
528                 char weeknumber[255];
529                 char weekday_name[32];
530                 char *daycolor;
531                 localtime_r(&thetime, &tm);
532
533
534                 /** Before displaying Sunday, start a new CELL */
535                 if ((i % 7) == 0) {
536                         wc_strftime(&weeknumber[0], sizeof(weeknumber), "%U", &tm);
537                         wprintf("<table border='0' bgcolor=\"#EEEECC\" width='100%'> <tr><th colspan='4'>%s %s</th></tr>"
538                                         "   <tr><td>%s</td><td width=70%%>%s</td><td>%s</td><td>%s</td></tr>\n",
539                                         _("Week"), 
540                                         weeknumber,
541                                         _("Hours"),
542                                         _("Subject"),
543                                         _("Start"),
544                                         _("End")
545                                         );
546                 }
547                 
548                 daycolor=((tm.tm_mon != month-1) ? "DDDDDD" :
549                                   ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
550                                    "FFFFFF"));
551                 
552                 /** Day Header */
553                 wc_strftime(weekday_name, sizeof weekday_name, "%A", &tm);
554                 wprintf("<tr><td bgcolor='%s' colspan='1' align='left'> %s,%i."
555                                 "</td><td bgcolor='%s' colspan='3'><hr></td></tr>\n",
556                                 daycolor,
557                                 weekday_name,tm.tm_mday,
558                                 daycolor);
559
560                 /** put the data of one day  here, stupid */
561                 calendar_month_view_brief_events(thetime, daycolor);
562
563
564                 /** After displaying Saturday, end the row */
565                 if ((i % 7) == 6) {
566                         wprintf("</td></tr></table>\n");
567                 }
568
569                 thetime += (time_t)86400;               /** ahead 24 hours */
570         }
571
572         wprintf("</table>"                      /** end of inner table */
573                 "</td></tr></table>"            /** end of outer table */
574                 "</div>\n");
575 }
576
577 /** 
578  * \brief view one week
579  * this should view just one week, but it's not here yet.
580  * \todo ny implemented
581  * \param year the year
582  * \param month the month
583  * \param day the day which we want to see the week around
584  */
585 void calendar_week_view(int year, int month, int day) {
586         wprintf("<center><i>week view FIXME</i></center><br />\n");
587 }
588
589
590 /**
591  * \brief display one day
592  * Display events for a particular hour of a particular day.
593  * (Specify hour < 0 to show "all day" events)
594  * \param year the year
595  * \param month the month
596  * \param day the day
597  * \param hour the hour we want to start displaying
598  * \param inner a flag to display between daystart and dayend
599  * (Specify inner to 1 to show inner events)
600  * (Specify inner to 0 to show "all day events and events after dayend)
601  * \param dstart daystart 
602  */
603 void calendar_day_view_display_events(time_t thetime, int year, int month,
604                                         int day, int hour,
605                                         int inner, int dstart) {
606         int i;
607         icalproperty *p;
608         time_t event_start;
609         time_t event_end;
610         icalproperty *l;
611         icalproperty *n;
612         struct tm event_te;
613         struct tm event_tm;
614         int show_event = 0;
615         int all_day_event = 0;
616         struct wcsession *WCC;
617         struct disp_cal *Cal;
618
619         WCC = WC;
620
621         if (WCC->num_cal == 0) {
622                 // \todo FIXME wprintf("<br /><br /><br />\n");
623                 return;
624         }
625
626         event_start = thetime + 60 * 60 * hour;
627         event_end = thetime + 60 * 60 * (hour + 1);
628
629         for (i=0; i<(WCC->num_cal); ++i) {
630                 Cal = &WCC->disp_cal[i];
631                 all_day_event =  Cal->start_hour == -1;
632
633                 show_event = 0;
634                 if (! all_day_event && inner)
635                 {
636                         show_event = (thetime == Cal->start_day) && 
637                                 (event_start <= Cal->start_hour) &&
638                                 ((Cal->end_hour != -1)? 
639                                    (Cal->end_hour < event_end) : 1);
640
641                 }
642                 else
643                 {
644                         show_event = !inner && (Cal->start_day == thetime);
645                 }
646
647                 p = icalcomponent_get_first_property(Cal->cal,ICAL_SUMMARY_PROPERTY);
648                 if ((show_event) && (p != NULL)) {
649                         if (all_day_event) {
650                                 wprintf("<dd><a href=\"display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d&hour=%d\">",
651                                         Cal->cal_msgnum,year, month, day, hour);
652                                 escputs((char *) icalproperty_get_comment(p));
653                                 wprintf("</a></dd>\n");
654                         }
655                         else {
656                                 wprintf("<dd  class=\"event\" "
657                                         "style=\"position: absolute; "
658                                         "top:%dpx; left:100px; "
659                                         "height:%dpx; \" >",
660                                         (1 + (event_tm.tm_min / 2) + (event_te.tm_hour - hour) + (hour * 30) - (dstart * 30)),
661                                                                 (((event_te.tm_min - event_tm.tm_min) / 2) +(event_te.tm_hour - hour) * 30)
662                                         );
663                         }/*
664                         else {
665                                 wprintf("<dd>");
666                                 }* /
667                                                         );
668                                                 }
669                                                 else {
670                                                         wprintf("<dd>");
671                                                 }
672                                                 wprintf("<a href=\"display_edit_event?"
673                                                         "msgnum=%ld&calview=day&year=%d"
674                                                         "&month=%d&day=%d&hour=%d\""
675                                                         " btt_tooltext=\"",
676                                                         WC->disp_cal[i].cal_msgnum,
677                                                         year, month, day, hour
678                                                 );
679                                                 wprintf("<i>%s</i> ", _("Summary:"));
680                                                 escputs((char *)icalproperty_get_comment(p));
681                                                 wprintf("<br />");
682         
683                                                 l = icalcomponent_get_first_property(
684                                                                 WC->disp_cal[i].cal,
685                                                                 ICAL_LOCATION_PROPERTY);
686                                                 if (l) {
687                                                         wprintf("<i>%s</i> ", _("Location:"));
688                                                         escputs((char *)icalproperty_get_comment(l));
689                                                         wprintf("<br />");
690                                                 }       
691                                                 n = icalcomponent_get_first_property(
692                                                                 WC->disp_cal[i].cal,
693                                                                 ICAL_DESCRIPTION_PROPERTY);
694                                                 if (n) {
695                                                         wprintf("<i>%s</i> ", _("Notes:"));
696                                                         escputs((char *)icalproperty_get_comment(n));
697                                                         wprintf("<br />");
698                                                 }
699                                                 wprintf("\">");
700                                                 escputs((char *)
701                                                         icalproperty_get_comment(p));
702                                                 wprintf("</a></dd>\n");
703                                           
704                                         }
705                                 }
706                         }
707
708                 }
709
710                 /* TODO: whats that? thierry, when do we need to show this?
711                 wprintf("<a href=\"display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d&hour=%d\">",
712                         WC->disp_cal[i].cal_msgnum,
713                         year, month, day, hour
714                         );
715                 escputs((char *)
716                         icalproperty_get_comment(p));
717                 wprintf("</a></dd>\n");
718                 */
719                 }
720         }
721 }
722
723
724
725 /**
726  * \brief view one day
727  * \param year the year
728  * \param month the month 
729  * \param day the day we want to display
730  */
731 void calendar_day_view(int year, int month, int day) {
732         int hour;
733         struct icaltimetype today, yesterday, tomorrow;
734         int daystart = 8;
735         int dayend = 17;
736         char daystart_str[16], dayend_str[16];
737         struct tm d_tm;
738         char d_str[128];
739         int time_format;
740         time_t today_t;
741         
742         time_format = get_time_format_cached ();
743         get_preference("daystart", daystart_str, sizeof daystart_str);
744         if (!IsEmptyStr(daystart_str)) daystart = atoi(daystart_str);
745         get_preference("dayend", dayend_str, sizeof dayend_str);
746         if (!IsEmptyStr(dayend_str)) dayend = atoi(dayend_str);
747         
748         /** Today's date */
749         memset(&d_tm, 0, sizeof d_tm);
750         if (WC->num_cal > 0) 
751                 localtime_r(&WC->disp_cal[0].start_day, &d_tm);
752
753         d_tm.tm_year = year - 1900;
754         d_tm.tm_mon = month - 1;
755         d_tm.tm_mday = day;
756         today_t = mktime(&d_tm); 
757
758         /** Figure out the dates for "yesterday" and "tomorrow" links */
759
760         memset(&today, 0, sizeof(struct icaltimetype));
761         today.year = year;
762         today.month = month;
763         today.day = day;
764         today.is_date = 1;
765
766         memcpy(&yesterday, &today, sizeof(struct icaltimetype));
767         --yesterday.day;
768         yesterday = icaltime_normalize(yesterday);
769
770         memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
771         ++tomorrow.day;
772         tomorrow = icaltime_normalize(tomorrow);
773
774         wprintf("<div class=\"fix_scrollbar_bug\">");
775
776         /** Inner table (the real one) */
777         wprintf("<table class=\"calendar\" id=\"inner_day\"><tr> \n");
778
779         /** Innermost cell (contains hours etc.) */
780         wprintf("<td class=\"middle_of_the_day\" >");
781         wprintf("<dl    "
782                 "style=\"                       "
783                 "       padding:0 ;             "
784                 "       margin: 0;              "
785                 "       position: absolute ;    "
786                 "       top: 0;                 "
787                 "       left: 0;                "
788                 "       width: 500px;           "
789                 "       clip: rect(0px 500px %dpx 0px);"
790                 "       clip: rect(0px, 500px, %dpx, 0px);"
791                 "\">",
792         (dayend - daystart) * 30,
793         (dayend - daystart) * 30
794         ); 
795         /** Now the middle of the day... */     
796         for (hour = 0; hour <= dayend; ++hour) {        /* could do HEIGHT=xx */
797                 if (hour >= daystart) {
798                         wprintf("<dt><a href=\"display_edit_event?msgnum=0"
799                                 "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
800                                 year, month, day, hour
801                         );
802
803                         if (time_format == WC_TIMEFORMAT_24) {
804                                 wprintf("%2d:00</a> ", hour);
805                         }
806                         else {
807                                 wprintf("%d:00%s</a> ",
808                                         (hour <= 12 ? hour : hour-12),
809                                         (hour < 12 ? "am" : "pm")
810                                 );
811                         }
812
813                 wprintf("</dt>");
814                 }
815
816                 /* put the data here, stupid */
817                 calendar_day_view_display_events(today_t, year, month, day, hour, 1 , daystart);
818
819         }
820
821         wprintf("</dl>");
822         wprintf("</td>");                       /* end of innermost table */
823
824         /** Extra events on the middle */
825         wprintf("<td class=\"extra_events\">");
826
827         wprintf("<dl>");
828
829         /** Display all-day events) */
830         wprintf("<dt>All day events</dt>");
831                 calendar_day_view_display_events(today_t, year, month, day, -1, 0 , daystart);
832
833         /** Display events before daystart */
834         wprintf("<dt>Before day start</dt>");
835         for (hour = 0; hour <= (daystart-1); ++hour) {
836                 calendar_day_view_display_events(today_t, year, month, day, hour, 0, daystart );
837         }
838
839         /** Display events after dayend... */
840         wprintf("<dt>After</dt>");
841         for (hour = (dayend+1); hour <= 23; ++hour) {
842                 calendar_day_view_display_events(today_t, year, month, day, hour, 0, daystart );
843         }
844
845         wprintf("</dl>");
846
847         wprintf("</td>");       /** end extra on the middle */
848
849         wprintf("<td width=20%% valign=top>");  /** begin stuff-on-the-right */
850
851         /** Begin todays-date-with-left-and-right-arrows */
852         wprintf("<table border=0 width=100%% "
853                 "cellspacing=0 cellpadding=0 bgcolor=\"#FFFFFF\">\n");
854         wprintf("<tr>");
855
856         /** Left arrow */       
857         wprintf("<td align=center>");
858         wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
859                 yesterday.year, yesterday.month, yesterday.day);
860         wprintf("<img align=middle src=\"static/prevdate_32x.gif\" border=0></A>");
861         wprintf("</td>");
862
863         wc_strftime(d_str, sizeof d_str,
864                 "<td align=center>"
865                 "<font size=+2>%B</font><br />"
866                 "<font size=+3>%d</font><br />"
867                 "<font size=+2>%Y</font><br />"
868                 "</td>",
869                 &d_tm
870         );
871         wprintf("%s", d_str);
872
873         /** Right arrow */
874         wprintf("<td align=center>");
875         wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
876                 tomorrow.year, tomorrow.month, tomorrow.day);
877         wprintf("<img align=middle src=\"static/nextdate_32x.gif\""
878                 " border=0></a>\n");
879         wprintf("</td>");
880
881         wprintf("</tr></table>\n");
882         /** End todays-date-with-left-and-right-arrows */
883
884         /** \todo In the future we might want to put a month-o-matic here */
885
886         wprintf("</font></center>\n");
887
888         wprintf("</td></tr>");                  /** end stuff-on-the-right */
889
890         wprintf("</table>"                      /** end of inner table */
891                 "</div>");
892
893         wprintf("<script type=\"text/javascript\">"
894                 " setTimeout(\"btt_enableTooltips('inner_day')\", 1); "
895                 "</script>\n"
896         );
897
898
899 }
900 /**
901  * \brief Display today's events.
902  */
903 void calendar_summary_view(void) {
904         int i;
905         icalproperty *p;
906         struct icaltimetype t;
907         time_t event_tt;
908         struct tm event_tm;
909         struct tm today_tm;
910         time_t now;
911         int all_day_event = 0;
912         char timestring[SIZ];
913
914         if (WC->num_cal == 0) {
915                 return;
916         }
917
918         now = time(NULL);
919         localtime_r(&now, &today_tm);
920
921         for (i=0; i<(WC->num_cal); ++i) {
922                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
923                                                 ICAL_DTSTART_PROPERTY);
924                 if (p != NULL) {
925                         t = icalproperty_get_dtstart(p);
926                         event_tt = icaltime_as_timet(t);
927                         if (t.is_date) {
928                                 all_day_event = 1;
929                         }
930                         else {
931                                 all_day_event = 0;
932                         }
933                         fmt_time(timestring, event_tt);
934
935                         if (all_day_event) {
936                                 gmtime_r(&event_tt, &event_tm);
937                         }
938                         else {
939                                 localtime_r(&event_tt, &event_tm);
940                         }
941
942                         if ( (event_tm.tm_year == today_tm.tm_year)
943                            && (event_tm.tm_mon == today_tm.tm_mon)
944                            && (event_tm.tm_mday == today_tm.tm_mday)
945                            ) {
946
947
948                                 p = icalcomponent_get_first_property(
949                                                         WC->disp_cal[i].cal,
950                                                         ICAL_SUMMARY_PROPERTY);
951                                 if (p != NULL) {
952                                         escputs((char *)
953                                                 icalproperty_get_comment(p));
954                                         wprintf(" (%s)<br />\n", timestring);
955                                 }
956                         }
957                 }
958         }
959         free_calendar_buffer();
960 }
961
962
963 /**
964  * \brief clean up ical memory
965  * \todo this could get troubel with future ical versions
966  */
967 void free_calendar_buffer(void) {
968         int i;
969         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
970                 icalcomponent_free(WC->disp_cal[i].cal);
971         }
972         WC->num_cal = 0;
973         free(WC->disp_cal);
974         WC->disp_cal = NULL;
975 }
976
977
978
979 /**
980  * \brief do the whole calendar page
981  * view any part of the calender. decide which way, etc.
982  */
983 void do_calendar_view(void) {
984         time_t now;
985         struct tm tm;
986         int year, month, day;
987         char calview[SIZ];
988
989         /** In case no date was specified, go with today */
990         now = time(NULL);
991         localtime_r(&now, &tm);
992         year = tm.tm_year + 1900;
993         month = tm.tm_mon + 1;
994         day = tm.tm_mday;
995
996         /** Now see if a date was specified */
997         if (!IsEmptyStr(bstr("year"))) year = atoi(bstr("year"));
998         if (!IsEmptyStr(bstr("month"))) month = atoi(bstr("month"));
999         if (!IsEmptyStr(bstr("day"))) day = atoi(bstr("day"));
1000
1001         /** How would you like that cooked? */
1002         if (!IsEmptyStr(bstr("calview"))) {
1003                 strcpy(calview, bstr("calview"));
1004         }
1005         else {
1006                 strcpy(calview, "month");
1007         }
1008
1009         /** Display the selected view */
1010         if (!strcasecmp(calview, "day")) {
1011                 calendar_day_view(year, month, day);
1012         }
1013         else if (!strcasecmp(calview, "week")) {
1014                 calendar_week_view(year, month, day);
1015         }
1016         else {
1017                 if (WC->wc_view == VIEW_CALBRIEF) {
1018                         calendar_brief_month_view(year, month, day);
1019                 }
1020                 else {
1021                         calendar_month_view(year, month, day);
1022                 }
1023         }
1024
1025         /** Free the calendar stuff */
1026         free_calendar_buffer();
1027
1028 }
1029
1030
1031 /**
1032  * \brief get task due date
1033  * Helper function for do_tasks_view().  
1034  * \param vtodo a task to get the due date
1035  * \return the date/time due.
1036  */
1037 time_t get_task_due_date(icalcomponent *vtodo) {
1038         icalproperty *p;
1039
1040         if (vtodo == NULL) {
1041                 return(0L);
1042         }
1043
1044         /**
1045          * If we're looking at a fully encapsulated VCALENDAR
1046          * rather than a VTODO component, recurse into the data
1047          * structure until we get a VTODO.
1048          */
1049         if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
1050                 return get_task_due_date(
1051                         icalcomponent_get_first_component(
1052                                 vtodo, ICAL_VTODO_COMPONENT
1053                         )
1054                 );
1055         }
1056
1057         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
1058         if (p != NULL) {
1059                 return(icaltime_as_timet(icalproperty_get_due(p)));
1060         }
1061         else {
1062                 return(0L);
1063         }
1064 }
1065
1066
1067 /**
1068  * \brief Compare the due dates of two tasks (this is for sorting)
1069  * \param task1 first task to compare
1070  * \param task2 second task to compare
1071  */
1072 int task_due_cmp(const void *task1, const void *task2) {
1073         time_t t1;
1074         time_t t2;
1075
1076         t1 =  get_task_due_date(((struct disp_cal *)task1)->cal);
1077         t2 =  get_task_due_date(((struct disp_cal *)task2)->cal);
1078
1079         if (t1 < t2) return(-1);
1080         if (t1 > t2) return(1);
1081         return(0);
1082 }
1083
1084
1085
1086
1087 /**
1088  * \brief do the whole task view stuff
1089  */
1090 void do_tasks_view(void) {
1091         int i;
1092         time_t due;
1093         int bg = 0;
1094         char buf[SIZ];
1095         icalproperty *p;
1096
1097         wprintf("<div class=\"fix_scrollbar_bug\">"
1098                 "<table class=\"calendar_view_background\">\n<tr>\n"
1099                 "<th>");
1100         wprintf(_("Name of task"));
1101         wprintf("</th><th>");
1102         wprintf(_("Date due"));
1103         wprintf("</th></tr>\n"
1104         );
1105
1106         /** Sort them if necessary */
1107         if (WC->num_cal > 1) {
1108                 qsort(WC->disp_cal,
1109                         WC->num_cal,
1110                         sizeof(struct disp_cal),
1111                         task_due_cmp
1112                 );
1113         }
1114
1115         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
1116
1117                 bg = 1 - bg;
1118                 wprintf("<tr bgcolor=\"#%s\"><td>",
1119                         (bg ? "DDDDDD" : "FFFFFF")
1120                 );
1121
1122                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
1123                                                         ICAL_SUMMARY_PROPERTY);
1124                 wprintf("<a href=\"display_edit_task?msgnum=%ld&taskrm=",
1125                         WC->disp_cal[i].cal_msgnum );
1126                 urlescputs(WC->wc_roomname);
1127                 wprintf("\">");
1128                 wprintf("<img align=middle "
1129                         "src=\"static/taskmanag_16x.gif\" border=0>&nbsp;");
1130                 if (p != NULL) {
1131                         escputs((char *)icalproperty_get_comment(p));
1132                 }
1133                 wprintf("</a>\n");
1134                 wprintf("</td>\n");
1135
1136                 due = get_task_due_date(WC->disp_cal[i].cal);
1137                 fmt_date(buf, due, 0);
1138                 wprintf("<td><font");
1139                 if (due < time(NULL)) {
1140                         wprintf(" color=\"#FF0000\"");
1141                 }
1142                 wprintf(">%s</font></td></tr>\n", buf);
1143         }
1144
1145         wprintf("</table></div>\n");
1146
1147         /** Free the list */
1148         free_calendar_buffer();
1149
1150 }
1151
1152 #else   /* WEBCIT_WITH_CALENDAR_SERVICE */
1153
1154 /**\brief stub for non-libical builds */
1155 void do_calendar_view(void) {
1156         wprintf("<center><i>");
1157         wprintf(_("The calendar view is not available."));
1158         wprintf("</i></center><br />\n");
1159 }
1160
1161 /**\brief stub for non-libical builds */
1162 void do_tasks_view(void) {      
1163         wprintf("<center><I>");
1164         wprintf(_("The tasks view is not available."));
1165         wprintf("</i></center><br />\n");
1166 }
1167
1168
1169 #endif  /* WEBCIT_WITH_CALENDAR_SERVICE */
1170
1171 /** @} */