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