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