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