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