]> code.citadel.org Git - citadel.git/blob - webcit/calendar_view.c
The calendar showed only five lines, therefore certain days being
[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 width=100%% border=0 cellpadding=0 cellspacing=0 "
359                 "bgcolor=#204B78><TR><TD>\n");
360
361         wprintf("<table width=100%% border=0 cellpadding=0 cellspacing=0><tr>\n");
362
363         wprintf("<td align=center>");
364
365         localtime_r(&previous_month, &tm);
366         wprintf("<a href=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
367                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
368         wprintf("<img align=middle src=\"static/prevdate_32x.gif\" border=0></A>\n");
369
370         wc_strftime(colheader_label, sizeof colheader_label, "%B", &starting_tm);
371         wprintf("&nbsp;&nbsp;"
372                 "<font size=+1 color=\"#FFFFFF\">"
373                 "%s %d"
374                 "</font>"
375                 "&nbsp;&nbsp;", colheader_label, year);
376
377         localtime_r(&next_month, &tm);
378         wprintf("<a href=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
379                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
380         wprintf("<img align=middle src=\"static/nextdate_32x.gif\" border=0></A>\n");
381
382         wprintf("</td></tr></table>\n");
383
384         /** Inner table (the real one) */
385         wprintf("<table width=100%% border=0 cellpadding=1 cellspacing=1 "
386                 "bgcolor=#204B78 id=\"inner_month\"><tr>");
387         colheader_time = thetime;
388         for (i=0; i<7; ++i) {
389                 colheader_time = thetime + (i * 86400) ;
390                 localtime_r(&colheader_time, &colheader_tm);
391                 wc_strftime(colheader_label, sizeof colheader_label, "%A", &colheader_tm);
392                 wprintf("<td align=center width=14%%>"
393                         "<font color=\"#FFFFFF\">%s</font></th>", colheader_label);
394
395         }
396         wprintf("</tr>\n");
397
398
399         /** Now do 35 or 42 days */
400         for (i = 0; i < 42; ++i) {
401                 localtime_r(&thetime, &tm);
402
403                 if ((i < 35) || (chg_month == 0)) {
404
405                         if ((i > 27) && ((tm.tm_mday == 1) || (tm.tm_mday == 31))) {
406                                 chg_month = 1;
407                         }
408                         if (i > 35) {
409                                 chg_month = 0;
410                         }
411
412                         /** Before displaying Sunday, start a new row */
413                         if ((i % 7) == 0) {
414                                 wprintf("<tr>");
415                         }
416
417                         wprintf("<td bgcolor=\"#%s\" width=14%% height=60 align=left valign=top><b>",
418                                 ((tm.tm_mon != month-1) ? "DDDDDD" :
419                                 ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
420                                 "FFFFFF"))
421                         );
422                         if ((i==0) || (tm.tm_mday == 1)) {
423                                 wc_strftime(colheader_label, sizeof colheader_label, "%B", &tm);
424                                 wprintf("%s ", colheader_label);
425                         }
426                         wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
427                                 "%d</a></b><br />",
428                                 tm.tm_year + 1900,
429                                 tm.tm_mon + 1,
430                                 tm.tm_mday,
431                                 tm.tm_mday);
432
433                         /** put the data here, stupid */
434                         calendar_month_view_display_events(thetime);
435
436                         wprintf("</td>");
437
438                         /** After displaying Saturday, end the row */
439                         if ((i % 7) == 6) {
440                                 wprintf("</tr>\n");
441                         }
442
443                 }
444
445                 thetime += (time_t)86400;               /** ahead 24 hours */
446         }
447
448         wprintf("</table>"                      /** end of inner table */
449                 "</td></tr></table>"            /** end of outer table */
450                 "</div>\n");
451
452         /**
453          * Initialize the bubble tooltips.
454          *
455          * Yes, this is as stupid as it looks.  Instead of just making the call
456          * to btt_enableTooltips() straight away, we have to create a timer event
457          * and let it initialize as an event after 1 millisecond.  This is to
458          * work around a bug in Internet Explorer that causes it to crash if we
459          * manipulate the innerHTML of various DOM nodes while the page is still
460          * being rendered.  See http://www.shaftek.org/blog/archives/000212.html
461          * for more information.
462          */ 
463         wprintf("<script type=\"text/javascript\">"
464                 " setTimeout(\"btt_enableTooltips('inner_month')\", 1); "
465                 "</script>\n"
466         );
467 }
468
469 /**
470  * \brief view one month. brief view
471  * \param year the year
472  * \param month the month
473  * \param day the actual day we want to see
474  */
475 void calendar_brief_month_view(int year, int month, int day) {
476         struct tm starting_tm;
477         struct tm tm;
478         time_t thetime;
479         int i;
480         time_t previous_month;
481         time_t next_month;
482         char month_label[32];
483
484         /** Determine what day to start.
485          * First, back up to the 1st of the month...
486          */
487         memset(&starting_tm, 0, sizeof(struct tm));
488         starting_tm.tm_year = year - 1900;
489         starting_tm.tm_mon = month - 1;
490         starting_tm.tm_mday = day;
491         thetime = mktime(&starting_tm);
492
493         memcpy(&tm, &starting_tm, sizeof(struct tm));
494         while (tm.tm_mday != 1) {
495                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
496                 localtime_r(&thetime, &tm);
497         }
498
499         /** Determine previous and next months ... for links */
500         previous_month = thetime - (time_t)864000L;     /* back 10 days */
501         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
502
503         /** Now back up until we're on a Sunday */
504         localtime_r(&thetime, &tm);
505         while (tm.tm_wday != 0) {
506                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
507                 localtime_r(&thetime, &tm);
508         }
509
510         /** Outer table (to get the background color) */
511         wprintf("<div class=\"fix_scrollbar_bug\">"
512                 "<table width=100%% border=0 cellpadding=0 cellspacing=0 "
513                 "bgcolor=#204B78><TR><TD>\n");
514
515         wprintf("<table width=100%% border=0 cellpadding=0 cellspacing=0><tr>\n");
516
517         wprintf("<td align=center>");
518
519         localtime_r(&previous_month, &tm);
520         wprintf("<a href=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
521                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
522         wprintf("<img align=middle src=\"static/prevdate_32x.gif\" border=0></A>\n");
523
524         wc_strftime(month_label, sizeof month_label, "%B", &tm);
525         wprintf("&nbsp;&nbsp;"
526                 "<font size=+1 color=\"#FFFFFF\">"
527                 "%s %d"
528                 "</font>"
529                 "&nbsp;&nbsp;", month_label, year);
530
531         localtime_r(&next_month, &tm);
532         wprintf("<a href=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
533                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
534         wprintf("<img align=middle src=\"static/nextdate_32x.gif\" border=0></A>\n");
535
536         wprintf("</td></tr></table>\n");
537
538         /** Inner table (the real one) */
539         wprintf("<table width=100%% border=0 cellpadding=1 cellspacing=1 "
540                 "bgcolor=#EEEECC><TR>");
541         wprintf("</tr>\n");
542         wprintf("<tr><td colspan=\"100%\">\n");
543
544         /** Now do 35 days */
545         for (i = 0; i < 35; ++i) {
546                 char weeknumber[255];
547                 char weekday_name[32];
548                 char *daycolor;
549                 localtime_r(&thetime, &tm);
550
551
552                 /** Before displaying Sunday, start a new CELL */
553                 if ((i % 7) == 0) {
554                         wc_strftime(&weeknumber[0], sizeof(weeknumber), "%U", &tm);
555                         wprintf("<table border='0' bgcolor=\"#EEEECC\" width='100%'> <tr><th colspan='4'>%s %s</th></tr>"
556                                         "   <tr><td>%s</td><td width=70%%>%s</td><td>%s</td><td>%s</td></tr>\n",
557                                         _("Week"), 
558                                         weeknumber,
559                                         _("Hours"),
560                                         _("Subject"),
561                                         _("Start"),
562                                         _("End")
563                                         );
564                 }
565                 
566                 daycolor=((tm.tm_mon != month-1) ? "DDDDDD" :
567                                   ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
568                                    "FFFFFF"));
569                 
570                 /** Day Header */
571                 wc_strftime(weekday_name, sizeof weekday_name, "%A", &tm);
572                 wprintf("<tr><td bgcolor='%s' colspan='1' align='left'> %s,%i."
573                                 "</td><td bgcolor='%s' colspan='3'><hr></td></tr>\n",
574                                 daycolor,
575                                 weekday_name,tm.tm_mday,
576                                 daycolor);
577
578                 /** put the data of one day  here, stupid */
579                 calendar_month_view_brief_events(thetime, daycolor);
580
581
582                 /** After displaying Saturday, end the row */
583                 if ((i % 7) == 6) {
584                         wprintf("</td></tr></table>\n");
585                 }
586
587                 thetime += (time_t)86400;               /** ahead 24 hours */
588         }
589
590         wprintf("</table>"                      /** end of inner table */
591                 "</td></tr></table>"            /** end of outer table */
592                 "</div>\n");
593 }
594
595 /** 
596  * \brief view one week
597  * this should view just one week, but it's not here yet.
598  * \todo ny implemented
599  * \param year the year
600  * \param month the month
601  * \param day the day which we want to see the week around
602  */
603 void calendar_week_view(int year, int month, int day) {
604         wprintf("<center><i>week view FIXME</i></center><br />\n");
605 }
606
607
608 /**
609  * \brief display one day
610  * Display events for a particular hour of a particular day.
611  * (Specify hour < 0 to show "all day" events)
612  * \param year the year
613  * \param month the month
614  * \param day the day
615  * \param hour the hour we want to start displaying?????
616  */
617 void calendar_day_view_display_events(int year, int month,
618                                         int day, int hour) {
619         int i;
620         icalproperty *p;
621         icalproperty *pe = NULL;
622         struct icaltimetype t;
623         struct icaltimetype te;
624         time_t event_tt;
625         time_t event_tte;
626         struct tm event_te;
627         struct tm event_tm;
628         int all_day_event = 0;
629
630         if (WC->num_cal == 0) {
631                 // \todo FIXME wprintf("<br /><br /><br />\n");
632                 return;
633         }
634
635         for (i=0; i<(WC->num_cal); ++i) {
636                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
637                                                 ICAL_DTSTART_PROPERTY);
638                 pe = icalcomponent_get_first_property(WC->disp_cal[i].cal,
639                                                       ICAL_DTEND_PROPERTY);
640                 if (p != NULL) {
641                         t = icalproperty_get_dtstart(p);
642                         event_tt = icaltime_as_timet(t);
643                         if (t.is_date) {
644                                 all_day_event = 1;
645                         }
646                         else {
647                                 all_day_event = 0;
648                         }
649
650                         if (all_day_event) {
651                                 gmtime_r(&event_tt, &event_tm);
652                                 gmtime_r(&event_tt, &event_te);
653                         }
654                         else {
655                                 localtime_r(&event_tt, &event_tm);
656                                 if (pe != NULL)
657                                 {
658                                         te = icalproperty_get_dtend(pe);
659                                         event_tte = icaltime_as_timet(te);
660                                         localtime_r(&event_tte, &event_te);
661                                 }
662                                 else 
663                                         localtime_r(&event_tt, &event_te);
664                         }
665
666                         if (((event_tm.tm_year <= (year-1900))
667                              && (event_tm.tm_mon <= (month-1))
668                              && (event_tm.tm_mday <= day)
669                              && (event_te.tm_year >= (year-1900))
670                              && (event_te.tm_mon >= (month-1))
671                              && (event_te.tm_mday >= day))
672                             && (
673                                  // are we in the start hour?
674                                     ((event_tm.tm_mday == day)
675                                      && (event_tm.tm_hour == hour) 
676                                      && (!t.is_date)) 
677                                  // are we an all day event?
678                                     || ((hour<0)&&(t.is_date))
679                                  // does it span multible days and we're not at the start day?
680                                     || ((hour<0)
681                                         && (event_tm.tm_mday < day) 
682                                         && (event_te.tm_mday >= day))
683                                     ))
684                         {
685                                 p = icalcomponent_get_first_property(
686                                                         WC->disp_cal[i].cal,
687                                                         ICAL_SUMMARY_PROPERTY);
688                                 if (p != NULL) {
689
690                                         if (all_day_event) {
691                                                 wprintf("<table border=1 cellpadding=2><TR>"
692                                                         "<td bgcolor=\"#CCCCCC\">"
693                                                 );
694                                         }
695
696                                         wprintf("<font size=-1>"
697                                                 "<a href=\"display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
698                                                 WC->disp_cal[i].cal_msgnum,
699                                                 year, month, day
700                                         );
701                                         escputs((char *)
702                                                 icalproperty_get_comment(p));
703                                         wprintf("</a></font><br />\n");
704
705                                         if (all_day_event) {
706                                                 wprintf("</td></tr></table>");
707                                         }
708                                 }
709
710                         }
711
712
713                 }
714         }
715 }
716
717
718 /**
719  * \brief view one day
720  * \param year the year
721  * \param month the month 
722  * \param day the day we want to display
723  */
724 void calendar_day_view(int year, int month, int day) {
725         int hour;
726         struct icaltimetype today, yesterday, tomorrow;
727         int daystart = 8;
728         int dayend = 17;
729         char daystart_str[16], dayend_str[16];
730         struct tm d_tm;
731         char d_str[128];
732         int time_format;
733         
734         time_format = get_time_format_cached ();
735         get_preference("daystart", daystart_str, sizeof daystart_str);
736         if (!IsEmptyStr(daystart_str)) daystart = atoi(daystart_str);
737         get_preference("dayend", dayend_str, sizeof dayend_str);
738         if (!IsEmptyStr(dayend_str)) dayend = atoi(dayend_str);
739         
740
741         /** Figure out the dates for "yesterday" and "tomorrow" links */
742
743         memset(&today, 0, sizeof(struct icaltimetype));
744         today.year = year;
745         today.month = month;
746         today.day = day;
747         today.is_date = 1;
748
749         memcpy(&yesterday, &today, sizeof(struct icaltimetype));
750         --yesterday.day;
751         yesterday = icaltime_normalize(yesterday);
752
753         memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
754         ++tomorrow.day;
755         tomorrow = icaltime_normalize(tomorrow);
756
757
758         /** Outer table (to get the background color) */
759         wprintf("<div class=\"fix_scrollbar_bug\">"
760                 "<table width=100%% border=0 cellpadding=0 cellspacing=0 "
761                 "bgcolor=#204B78><tr><td>\n");
762
763         /** Inner table (the real one) */
764         wprintf("<table width=100%% border=0 cellpadding=1 cellspacing=1 "
765                 "bgcolor=#204B78><tr>\n");
766
767         /** Innermost table (contains hours etc.) */
768         wprintf("<td width=80%%>"
769                 "<table width=100%% border=0 cellpadding=1 cellspacing=1 "
770                 "bgcolor=#204B78>\n");
771
772         /** Display events before 8:00 (hour=-1 is all-day events) */
773         wprintf("<tr>"
774                 "<td bgcolor=\"#CCCCDD\" valign=middle width=10%%></td>"
775                 "<td bgcolor=\"#FFFFFF\" valign=top>");
776         for (hour = (-1); hour <= (daystart-1); ++hour) {
777                 calendar_day_view_display_events(year, month, day, hour);
778         }
779         wprintf("</td></tr>\n");
780
781         /** Now the middle of the day... */     
782         for (hour = daystart; hour <= dayend; ++hour) { /* could do HEIGHT=xx */
783                 wprintf("<tr height=30><td bgcolor=\"#CCCCDD\" align=middle "
784                         "valign=middle width=10%%>");
785                 wprintf("<a href=\"display_edit_event?msgnum=0"
786                         "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
787                         year, month, day, hour
788                 );
789
790                 if (time_format == WC_TIMEFORMAT_24) {
791                         wprintf("%2d:00</a> ", hour);
792                 }
793                 else {
794                         wprintf("%d:00%s</a> ",
795                                 (hour <= 12 ? hour : hour-12),
796                                 (hour < 12 ? "am" : "pm")
797                         );
798                 }
799
800                 wprintf("</td><td bgcolor=\"#FFFFFF\" valign=top>");
801
802                 /* put the data here, stupid */
803                 calendar_day_view_display_events(year, month, day, hour);
804
805                 wprintf("</td></tr>\n");
806         }
807
808         /** Display events after 5:00... */
809         wprintf("<tr>"
810                 "<td bgcolor=\"#CCCCDD\" valign=middle width=10%%></td>"
811                 "<td bgcolor=\"#FFFFFF\" valign=top>");
812         for (hour = (dayend+1); hour <= 23; ++hour) {
813                 calendar_day_view_display_events(year, month, day, hour);
814         }
815         wprintf("</td></tr>\n");
816
817
818         wprintf("</table>"                      /* end of innermost table */
819                 "</td>"
820         );
821
822         wprintf("<td width=20%% valign=top>");  /* begin stuff-on-the-right */
823
824
825         /** Begin todays-date-with-left-and-right-arrows */
826         wprintf("<table border=0 width=100%% "
827                 "cellspacing=0 cellpadding=0 bgcolor=\"#FFFFFF\">\n");
828         wprintf("<tr>");
829
830         /** Left arrow */       
831         wprintf("<td align=center>");
832         wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
833                 yesterday.year, yesterday.month, yesterday.day);
834         wprintf("<img align=middle src=\"static/prevdate_32x.gif\" border=0></A>");
835         wprintf("</td>");
836
837         /** Today's date */
838         memset(&d_tm, 0, sizeof d_tm);
839         d_tm.tm_year = year - 1900;
840         d_tm.tm_mon = month - 1;
841         d_tm.tm_mday = day;
842         wc_strftime(d_str, sizeof d_str,
843                 "<td align=center>"
844                 "<font size=+2>%B</font><br />"
845                 "<font size=+3>%d</font><br />"
846                 "<font size=+2>%Y</font><br />"
847                 "</td>",
848                 &d_tm
849         );
850         wprintf("%s", d_str);
851
852         /** Right arrow */
853         wprintf("<td align=center>");
854         wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
855                 tomorrow.year, tomorrow.month, tomorrow.day);
856         wprintf("<img align=middle src=\"static/nextdate_32x.gif\""
857                 " border=0></A>\n");
858         wprintf("</td>");
859
860         wprintf("</tr></table>\n");
861         /** End todays-date-with-left-and-right-arrows */
862
863         /** \todo In the future we might want to put a month-o-matic here */
864
865         wprintf("</font></center>\n");
866
867         wprintf("</td>");                       /** end stuff-on-the-right */
868
869
870
871         wprintf("</tr></table>"                 /** end of inner table */
872                 "</td></tr></table></div>"      /** end of outer table */
873         );
874
875
876
877 }
878
879 /**
880  * \brief Display today's events.
881  */
882 void calendar_summary_view(void) {
883         int i;
884         icalproperty *p;
885         struct icaltimetype t;
886         time_t event_tt;
887         struct tm event_tm;
888         struct tm today_tm;
889         time_t now;
890         int all_day_event = 0;
891         char timestring[SIZ];
892
893         if (WC->num_cal == 0) {
894                 return;
895         }
896
897         now = time(NULL);
898         localtime_r(&now, &today_tm);
899
900         for (i=0; i<(WC->num_cal); ++i) {
901                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
902                                                 ICAL_DTSTART_PROPERTY);
903                 if (p != NULL) {
904                         t = icalproperty_get_dtstart(p);
905                         event_tt = icaltime_as_timet(t);
906                         if (t.is_date) {
907                                 all_day_event = 1;
908                         }
909                         else {
910                                 all_day_event = 0;
911                         }
912                         fmt_time(timestring, event_tt);
913
914                         if (all_day_event) {
915                                 gmtime_r(&event_tt, &event_tm);
916                         }
917                         else {
918                                 localtime_r(&event_tt, &event_tm);
919                         }
920
921                         if ( (event_tm.tm_year == today_tm.tm_year)
922                            && (event_tm.tm_mon == today_tm.tm_mon)
923                            && (event_tm.tm_mday == today_tm.tm_mday)
924                            ) {
925
926
927                                 p = icalcomponent_get_first_property(
928                                                         WC->disp_cal[i].cal,
929                                                         ICAL_SUMMARY_PROPERTY);
930                                 if (p != NULL) {
931                                         escputs((char *)
932                                                 icalproperty_get_comment(p));
933                                         wprintf(" (%s)<br />\n", timestring);
934                                 }
935                         }
936                 }
937         }
938         free_calendar_buffer();
939 }
940
941
942 /**
943  * \brief clean up ical memory
944  * \todo this could get troubel with future ical versions
945  */
946 void free_calendar_buffer(void) {
947         int i;
948         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
949                 icalcomponent_free(WC->disp_cal[i].cal);
950         }
951         WC->num_cal = 0;
952         free(WC->disp_cal);
953         WC->disp_cal = NULL;
954 }
955
956
957
958 /**
959  * \brief do the whole calendar page
960  * view any part of the calender. decide which way, etc.
961  */
962 void do_calendar_view(void) {
963         time_t now;
964         struct tm tm;
965         int year, month, day;
966         char calview[SIZ];
967
968         /** In case no date was specified, go with today */
969         now = time(NULL);
970         localtime_r(&now, &tm);
971         year = tm.tm_year + 1900;
972         month = tm.tm_mon + 1;
973         day = tm.tm_mday;
974
975         /** Now see if a date was specified */
976         if (!IsEmptyStr(bstr("year"))) year = atoi(bstr("year"));
977         if (!IsEmptyStr(bstr("month"))) month = atoi(bstr("month"));
978         if (!IsEmptyStr(bstr("day"))) day = atoi(bstr("day"));
979
980         /** How would you like that cooked? */
981         if (!IsEmptyStr(bstr("calview"))) {
982                 strcpy(calview, bstr("calview"));
983         }
984         else {
985                 strcpy(calview, "month");
986         }
987
988         /** Display the selected view */
989         if (!strcasecmp(calview, "day")) {
990                 calendar_day_view(year, month, day);
991         }
992         else if (!strcasecmp(calview, "week")) {
993                 calendar_week_view(year, month, day);
994         }
995         else {
996                 if (WC->wc_view == VIEW_CALBRIEF) {
997                         calendar_brief_month_view(year, month, day);
998                 }
999                 else {
1000                         calendar_month_view(year, month, day);
1001                 }
1002         }
1003
1004         /** Free the calendar stuff */
1005         free_calendar_buffer();
1006
1007 }
1008
1009
1010 /**
1011  * \brief get task due date
1012  * Helper function for do_tasks_view().  
1013  * \param vtodo a task to get the due date
1014  * \return the date/time due.
1015  */
1016 time_t get_task_due_date(icalcomponent *vtodo) {
1017         icalproperty *p;
1018
1019         if (vtodo == NULL) {
1020                 return(0L);
1021         }
1022
1023         /**
1024          * If we're looking at a fully encapsulated VCALENDAR
1025          * rather than a VTODO component, recurse into the data
1026          * structure until we get a VTODO.
1027          */
1028         if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
1029                 return get_task_due_date(
1030                         icalcomponent_get_first_component(
1031                                 vtodo, ICAL_VTODO_COMPONENT
1032                         )
1033                 );
1034         }
1035
1036         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
1037         if (p != NULL) {
1038                 return(icaltime_as_timet(icalproperty_get_due(p)));
1039         }
1040         else {
1041                 return(0L);
1042         }
1043 }
1044
1045
1046 /**
1047  * \brief Compare the due dates of two tasks (this is for sorting)
1048  * \param task1 first task to compare
1049  * \param task2 second task to compare
1050  */
1051 int task_due_cmp(const void *task1, const void *task2) {
1052         time_t t1;
1053         time_t t2;
1054
1055         t1 =  get_task_due_date(((struct disp_cal *)task1)->cal);
1056         t2 =  get_task_due_date(((struct disp_cal *)task2)->cal);
1057
1058         if (t1 < t2) return(-1);
1059         if (t1 > t2) return(1);
1060         return(0);
1061 }
1062
1063
1064
1065
1066 /**
1067  * \brief do the whole task view stuff
1068  */
1069 void do_tasks_view(void) {
1070         int i;
1071         time_t due;
1072         int bg = 0;
1073         char buf[SIZ];
1074         icalproperty *p;
1075
1076         wprintf("<div class=\"fix_scrollbar_bug\">"
1077                 "<table class=\"calendar_view_background\">\n<tr>\n"
1078                 "<th>");
1079         wprintf(_("Name of task"));
1080         wprintf("</th><th>");
1081         wprintf(_("Date due"));
1082         wprintf("</th></tr>\n"
1083         );
1084
1085         /** Sort them if necessary */
1086         if (WC->num_cal > 1) {
1087                 qsort(WC->disp_cal,
1088                         WC->num_cal,
1089                         sizeof(struct disp_cal),
1090                         task_due_cmp
1091                 );
1092         }
1093
1094         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
1095
1096                 bg = 1 - bg;
1097                 wprintf("<tr bgcolor=\"#%s\"><td>",
1098                         (bg ? "DDDDDD" : "FFFFFF")
1099                 );
1100
1101                 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
1102                                                         ICAL_SUMMARY_PROPERTY);
1103                 wprintf("<a href=\"display_edit_task?msgnum=%ld&taskrm=",
1104                         WC->disp_cal[i].cal_msgnum );
1105                 urlescputs(WC->wc_roomname);
1106                 wprintf("\">");
1107                 wprintf("<img align=middle "
1108                         "src=\"static/taskmanag_16x.gif\" border=0>&nbsp;");
1109                 if (p != NULL) {
1110                         escputs((char *)icalproperty_get_comment(p));
1111                 }
1112                 wprintf("</a>\n");
1113                 wprintf("</td>\n");
1114
1115                 due = get_task_due_date(WC->disp_cal[i].cal);
1116                 fmt_date(buf, due, 0);
1117                 wprintf("<td><font");
1118                 if (due < time(NULL)) {
1119                         wprintf(" color=\"#FF0000\"");
1120                 }
1121                 wprintf(">%s</font></td></tr>\n", buf);
1122         }
1123
1124         wprintf("</table></div>\n");
1125
1126         /** Free the list */
1127         free_calendar_buffer();
1128
1129 }
1130
1131 #endif  /* WEBCIT_WITH_CALENDAR_SERVICE */
1132
1133 /** @} */