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