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