4 * Handles the HTML display of calendar items.
13 #include <sys/types.h>
15 #include <sys/socket.h>
17 #include <netinet/in.h>
27 #include "webserver.h"
29 #ifndef WEBCIT_WITH_CALENDAR_SERVICE
31 void do_calendar_view(void) { /* stub for non-libical builds */
32 wprintf("<CENTER><I>Calendar view not available</I></CENTER><br />\n");
35 void do_tasks_view(void) { /* stub for non-libical builds */
36 wprintf("<CENTER><I>Tasks view not available</I></CENTER><br />\n");
39 #else /* WEBCIT_WITH_CALENDAR_SERVICE */
41 /****************************************************************************/
44 void calendar_month_view_display_events(time_t thetime) {
50 struct icaltimetype t;
52 int all_day_event = 0;
54 if (WC->num_cal == 0) {
55 wprintf("<br /><br /><br />\n");
59 localtime_r(&thetime, &today_tm);
60 month = today_tm.tm_mon + 1;
61 day = today_tm.tm_mday;
62 year = today_tm.tm_year + 1900;
64 for (i=0; i<(WC->num_cal); ++i) {
65 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
66 ICAL_DTSTART_PROPERTY);
68 t = icalproperty_get_dtstart(p);
69 event_tt = icaltime_as_timet(t);
71 if (t.is_date) all_day_event = 1;
72 else all_day_event = 0;
75 gmtime_r(&event_tt, &event_tm);
78 localtime_r(&event_tt, &event_tm);
81 if ((event_tm.tm_year == today_tm.tm_year)
82 && (event_tm.tm_mon == today_tm.tm_mon)
83 && (event_tm.tm_mday == today_tm.tm_mday)) {
85 p = icalcomponent_get_first_property(
87 ICAL_SUMMARY_PROPERTY);
91 wprintf("<TABLE border=0 cellpadding=2><TR>"
92 "<TD BGCOLOR=\"#CCCCDD\">"
96 wprintf("<FONT SIZE=-1>"
97 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=%s&year=%s&month=%s&day=%s\">",
98 WC->disp_cal[i].cal_msgnum,
105 icalproperty_get_comment(p));
106 wprintf("</A></FONT><br />\n");
109 wprintf("</TD></TR></TABLE>");
123 void calendar_month_view(int year, int month, int day) {
124 struct tm starting_tm;
128 time_t previous_month;
131 /* Determine what day to start.
132 * First, back up to the 1st of the month...
134 memset(&starting_tm, 0, sizeof(struct tm));
135 starting_tm.tm_year = year - 1900;
136 starting_tm.tm_mon = month - 1;
137 starting_tm.tm_mday = day;
138 thetime = mktime(&starting_tm);
140 memcpy(&tm, &starting_tm, sizeof(struct tm));
141 while (tm.tm_mday != 1) {
142 thetime = thetime - (time_t)86400; /* go back 24 hours */
143 localtime_r(&thetime, &tm);
146 /* Determine previous and next months ... for links */
147 previous_month = thetime - (time_t)864000L; /* back 10 days */
148 next_month = thetime + (time_t)(31L * 86400L); /* ahead 31 days */
150 /* Now back up until we're on a Sunday */
151 localtime_r(&thetime, &tm);
152 while (tm.tm_wday != 0) {
153 thetime = thetime - (time_t)86400; /* go back 24 hours */
154 localtime_r(&thetime, &tm);
157 /* Outer table (to get the background color) */
158 wprintf("<div id=\"fix_scrollbar_bug\">"
159 "<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
160 "bgcolor=#204B78><TR><TD>\n");
162 wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0><tr>\n");
164 wprintf("<TD ALIGN=CENTER>");
166 localtime_r(&previous_month, &tm);
167 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
168 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
169 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/prevdate_32x.gif\" BORDER=0></A>\n");
171 wprintf(" "
172 "<FONT SIZE=+1 COLOR=\"#FFFFFF\">"
175 " ", months[month-1], year);
177 localtime_r(&next_month, &tm);
178 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
179 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
180 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/nextdate_32x.gif\" BORDER=0></A>\n");
182 wprintf("</TD></TR></TABLE>\n");
184 /* Inner table (the real one) */
185 wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
186 "bgcolor=#204B78><TR>");
187 for (i=0; i<7; ++i) {
188 wprintf("<TD ALIGN=CENTER WIDTH=14%%>"
189 "<FONT COLOR=\"#FFFFFF\">%s</FONT></TH>", days[i]);
194 for (i = 0; i < 35; ++i) {
195 localtime_r(&thetime, &tm);
197 /* Before displaying Sunday, start a new row */
202 wprintf("<TD BGCOLOR=\"#%s\" WIDTH=14%% HEIGHT=60 align=left VALIGN=TOP><B>",
203 ((tm.tm_mon != month-1) ? "DDDDDD" :
204 ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
207 if ((i==0) || (tm.tm_mday == 1)) {
208 wprintf("%s ", months[tm.tm_mon]);
210 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
217 /* put the data here, stupid */
218 calendar_month_view_display_events(thetime);
222 /* After displaying Saturday, end the row */
227 thetime += (time_t)86400; /* ahead 24 hours */
230 wprintf("</TABLE>" /* end of inner table */
231 "</TD></TR></TABLE>" /* end of outer table */
236 void calendar_week_view(int year, int month, int day) {
237 wprintf("<CENTER><I>week view FIXME</I></CENTER><br />\n");
242 * Display events for a particular hour of a particular day.
243 * (Specify hour < 0 to show "all day" events)
245 void calendar_day_view_display_events(int year, int month,
249 struct icaltimetype t;
252 int all_day_event = 0;
254 if (WC->num_cal == 0) {
255 // FIXME wprintf("<br /><br /><br />\n");
259 for (i=0; i<(WC->num_cal); ++i) {
260 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
261 ICAL_DTSTART_PROPERTY);
263 t = icalproperty_get_dtstart(p);
264 event_tt = icaltime_as_timet(t);
265 if (t.is_date) all_day_event = 1;
268 event_tm = gmtime(&event_tt);
271 event_tm = localtime(&event_tt);
274 if ((event_tm->tm_year == (year-1900))
275 && (event_tm->tm_mon == (month-1))
276 && (event_tm->tm_mday == day)
277 && ( ((event_tm->tm_hour == hour)&&(!t.is_date)) || ((hour<0)&&(t.is_date)) )
281 p = icalcomponent_get_first_property(
283 ICAL_SUMMARY_PROPERTY);
287 wprintf("<TABLE border=1 cellpadding=2><TR>"
288 "<TD BGCOLOR=\"#CCCCCC\">"
292 wprintf("<FONT SIZE=-1>"
293 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
294 WC->disp_cal[i].cal_msgnum,
298 icalproperty_get_comment(p));
299 wprintf("</A></FONT><br />\n");
302 wprintf("</TD></TR></TABLE>");
315 void calendar_day_view(int year, int month, int day) {
317 struct icaltimetype today, yesterday, tomorrow;
318 char calhourformat[16];
320 get_preference("calhourformat", calhourformat, sizeof calhourformat);
322 /* Figure out the dates for "yesterday" and "tomorrow" links */
324 memset(&today, 0, sizeof(struct icaltimetype));
330 memcpy(&yesterday, &today, sizeof(struct icaltimetype));
332 yesterday = icaltime_normalize(yesterday);
334 memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
336 tomorrow = icaltime_normalize(tomorrow);
339 /* Outer table (to get the background color) */
340 wprintf("<div id=\"fix_scrollbar_bug\">"
341 "<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
342 "bgcolor=#204B78><TR><TD>\n");
344 /* Inner table (the real one) */
345 wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
346 "bgcolor=#204B78><TR>\n");
348 /* Innermost table (contains hours etc.) */
349 wprintf("<TD WIDTH=80%%>"
350 "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
351 "bgcolor=#204B78>\n");
353 /* Display events before 8:00 (hour=-1 is all-day events) */
355 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
356 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
357 for (hour = (-1); hour <= 7; ++hour) {
358 calendar_day_view_display_events(year, month, day, hour);
360 wprintf("</TD></TR>\n");
362 /* Now the middle of the day... */
363 for (hour = 8; hour <= 17; ++hour) { /* could do HEIGHT=xx */
364 wprintf("<TR HEIGHT=30><TD BGCOLOR=\"#CCCCDD\" ALIGN=MIDDLE "
365 "VALIGN=MIDDLE WIDTH=10%%>");
366 wprintf("<A HREF=\"/display_edit_event?msgnum=0"
367 "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
368 year, month, day, hour
371 if (!strcasecmp(calhourformat, "24")) {
372 wprintf("%2d:00</A> ", hour);
375 wprintf("%d:00%s</A> ",
376 (hour <= 12 ? hour : hour-12),
377 (hour < 12 ? "am" : "pm")
381 wprintf("</TD><TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
383 /* put the data here, stupid */
384 calendar_day_view_display_events(year, month, day, hour);
386 wprintf("</TD></TR>\n");
389 /* Display events after 5:00... */
391 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
392 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
393 for (hour = 18; hour <= 23; ++hour) {
394 calendar_day_view_display_events(year, month, day, hour);
396 wprintf("</TD></TR>\n");
399 wprintf("</TABLE>" /* end of innermost table */
403 wprintf("<TD WIDTH=20%% VALIGN=top>"); /* begin stuff-on-the-right */
406 /* Begin todays-date-with-left-and-right-arrows */
407 wprintf("<TABLE BORDER=0 WIDTH=100%% "
408 "CELLSPACING=0 CELLPADDING=0 BGCOLOR=\"#FFFFFF\">\n");
412 wprintf("<TD ALIGN=CENTER>");
413 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
414 yesterday.year, yesterday.month, yesterday.day);
415 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/prevdate_32x.gif\" BORDER=0></A>");
419 wprintf("<TD ALIGN=CENTER>");
420 wprintf("<FONT SIZE=+2>%s</FONT><br />"
421 "<FONT SIZE=+3>%d</FONT><br />"
422 "<FONT SIZE=+2>%d</FONT><br />",
423 months[month-1], day, year);
427 wprintf("<TD ALIGN=CENTER>");
428 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
429 tomorrow.year, tomorrow.month, tomorrow.day);
430 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/nextdate_32x.gif\""
434 wprintf("</TR></TABLE>\n");
435 /* End todays-date-with-left-and-right-arrows */
437 /* In the future we might want to put a month-o-matic here */
439 wprintf("</FONT></CENTER>\n");
441 wprintf("</TD>"); /* end stuff-on-the-right */
445 wprintf("</TR></TABLE>" /* end of inner table */
446 "</TD></TR></TABLE></div>" /* end of outer table */
454 * Display today's events.
456 void calendar_summary_view(void) {
459 struct icaltimetype t;
464 int all_day_event = 0;
465 char timestring[SIZ];
467 if (WC->num_cal == 0) {
472 localtime_r(&now, &today_tm);
474 for (i=0; i<(WC->num_cal); ++i) {
475 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
476 ICAL_DTSTART_PROPERTY);
478 t = icalproperty_get_dtstart(p);
479 event_tt = icaltime_as_timet(t);
480 if (t.is_date) all_day_event = 1;
481 fmt_time(timestring, event_tt);
484 gmtime_r(&event_tt, &event_tm);
487 localtime_r(&event_tt, &event_tm);
490 if ( (event_tm.tm_year == today_tm.tm_year)
491 && (event_tm.tm_mon == today_tm.tm_mon)
492 && (event_tm.tm_mday == today_tm.tm_mday)
496 p = icalcomponent_get_first_property(
498 ICAL_SUMMARY_PROPERTY);
501 icalproperty_get_comment(p));
502 wprintf(" (%s)<br />\n", timestring);
507 free_calendar_buffer();
512 void free_calendar_buffer(void) {
514 if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
515 icalcomponent_free(WC->disp_cal[i].cal);
525 void do_calendar_view(void) {
528 int year, month, day;
531 /* In case no date was specified, go with today */
533 localtime_r(&now, &tm);
534 year = tm.tm_year + 1900;
535 month = tm.tm_mon + 1;
538 /* Now see if a date was specified */
539 if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
540 if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
541 if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
543 /* How would you like that cooked? */
544 if (strlen(bstr("calview")) > 0) {
545 strcpy(calview, bstr("calview"));
548 strcpy(calview, "month");
551 /* Display the selected view */
552 if (!strcasecmp(calview, "day")) {
553 calendar_day_view(year, month, day);
555 else if (!strcasecmp(calview, "week")) {
556 calendar_week_view(year, month, day);
559 calendar_month_view(year, month, day);
562 /* Free the calendar stuff */
563 free_calendar_buffer();
569 * Helper function for do_tasks_view(). Returns the date/time due.
571 time_t get_task_due_date(icalcomponent *vtodo) {
578 /* If we're looking at a fully encapsulated VCALENDAR
579 * rather than a VTODO component, recurse into the data
580 * structure until we get a VTODO.
582 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
583 return get_task_due_date(
584 icalcomponent_get_first_component(
585 vtodo, ICAL_VTODO_COMPONENT
590 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
592 return(icaltime_as_timet(icalproperty_get_due(p)));
601 * Compare the due dates of two tasks (this is for sorting)
603 int task_due_cmp(const void *task1, const void *task2) {
607 t1 = get_task_due_date(((struct disp_cal *)task1)->cal);
608 t2 = get_task_due_date(((struct disp_cal *)task2)->cal);
610 if (t1 < t2) return(-1);
611 if (t1 > t2) return(1);
619 void do_tasks_view(void) {
626 wprintf("<div id=\"fix_scrollbar_bug\">"
627 "<table border=0 cellspacing=0 width=100%% bgcolor=\"#FFFFFF\">\n<tr>\n"
628 "<TH>Name of task</TH>\n"
629 "<TH>Date due</TH></TR>\n"
632 /* Sort them if necessary */
633 if (WC->num_cal > 1) {
636 sizeof(struct disp_cal),
641 if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
644 wprintf("<TR BGCOLOR=\"#%s\"><TD>",
645 (bg ? "DDDDDD" : "FFFFFF")
648 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
649 ICAL_SUMMARY_PROPERTY);
650 wprintf("<A HREF=\"/display_edit_task?msgnum=%ld&taskrm=",
651 WC->disp_cal[i].cal_msgnum );
652 urlescputs(WC->wc_roomname);
654 wprintf("<IMG ALIGN=MIDDLE "
655 "SRC=\"/static/taskmanag_16x.gif\" BORDER=0> ");
657 escputs((char *)icalproperty_get_comment(p));
662 due = get_task_due_date(WC->disp_cal[i].cal);
663 fmt_date(buf, due, 0);
664 wprintf("<TD><FONT");
665 if (due < time(NULL)) {
666 wprintf(" COLOR=\"#FF0000\"");
668 wprintf(">%s</FONT></TD></TR>\n", buf);
671 wprintf("</table></div>\n");
674 free_calendar_buffer();
678 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */