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