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