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