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) */
159 "<TABLE width=99%% 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/back.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/forward.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 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;
320 /* Figure out the dates for "yesterday" and "tomorrow" links */
322 memset(&today, 0, sizeof(struct icaltimetype));
328 memcpy(&yesterday, &today, sizeof(struct icaltimetype));
330 yesterday = icaltime_normalize(yesterday);
332 memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
334 tomorrow = icaltime_normalize(tomorrow);
337 /* Outer table (to get the background color) */
339 "<TABLE width=99%% border=0 cellpadding=0 cellspacing=0 "
340 "bgcolor=#204B78><TR><TD>\n");
342 /* Inner table (the real one) */
343 wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
344 "bgcolor=#204B78><TR>\n");
346 /* Innermost table (contains hours etc.) */
347 wprintf("<TD WIDTH=80%%>"
348 "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
349 "bgcolor=#204B78>\n");
351 /* Display events before 8:00 (hour=-1 is all-day events) */
353 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
354 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
355 for (hour = (-1); hour <= 7; ++hour) {
356 calendar_day_view_display_events(year, month, day, hour);
358 wprintf("</TD></TR>\n");
360 /* Now the middle of the day... */
361 for (hour = 8; hour <= 17; ++hour) { /* could do HEIGHT=xx */
362 wprintf("<TR HEIGHT=30><TD BGCOLOR=\"#CCCCDD\" ALIGN=MIDDLE "
363 "VALIGN=MIDDLE WIDTH=10%%>");
364 wprintf("<A HREF=\"/display_edit_event?msgnum=0"
365 "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
366 year, month, day, hour
368 wprintf("%d:00%s</A> ",
369 (hour <= 12 ? hour : hour-12),
370 (hour < 12 ? "am" : "pm")
372 wprintf("</TD><TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
374 /* put the data here, stupid */
375 calendar_day_view_display_events(year, month, day, hour);
377 wprintf("</TD></TR>\n");
380 /* Display events after 5:00... */
382 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
383 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
384 for (hour = 18; hour <= 23; ++hour) {
385 calendar_day_view_display_events(year, month, day, hour);
387 wprintf("</TD></TR>\n");
390 wprintf("</TABLE>" /* end of innermost table */
394 wprintf("<TD WIDTH=20%% VALIGN=top>"); /* begin stuff-on-the-right */
397 /* Begin todays-date-with-left-and-right-arrows */
398 wprintf("<TABLE BORDER=0 WIDTH=100%% "
399 "CELLSPACING=0 CELLPADDING=0 BGCOLOR=\"#FFFFFF\">\n");
403 wprintf("<TD ALIGN=CENTER>");
404 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
405 yesterday.year, yesterday.month, yesterday.day);
406 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>");
410 wprintf("<TD ALIGN=CENTER>");
411 wprintf("<FONT SIZE=+2>%s</FONT><br />"
412 "<FONT SIZE=+3>%d</FONT><br />"
413 "<FONT SIZE=+2>%d</FONT><br />",
414 months[month-1], day, year);
418 wprintf("<TD ALIGN=CENTER>");
419 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
420 tomorrow.year, tomorrow.month, tomorrow.day);
421 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\""
425 wprintf("</TR></TABLE>\n");
426 /* End todays-date-with-left-and-right-arrows */
428 /* In the future we might want to put a month-o-matic here */
430 wprintf("</FONT></CENTER>\n");
432 wprintf("</TD>"); /* end stuff-on-the-right */
436 wprintf("</TR></TABLE>" /* end of inner table */
437 "</TD></TR></TABLE></center>" /* end of outer table */
445 * Display today's events.
447 void calendar_summary_view(void) {
450 struct icaltimetype t;
455 int all_day_event = 0;
456 char timestring[SIZ];
458 if (WC->num_cal == 0) {
463 localtime_r(&now, &today_tm);
465 for (i=0; i<(WC->num_cal); ++i) {
466 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
467 ICAL_DTSTART_PROPERTY);
469 t = icalproperty_get_dtstart(p);
470 event_tt = icaltime_as_timet(t);
471 if (t.is_date) all_day_event = 1;
472 fmt_time(timestring, event_tt);
475 gmtime_r(&event_tt, &event_tm);
478 localtime_r(&event_tt, &event_tm);
481 if ( (event_tm.tm_year == today_tm.tm_year)
482 && (event_tm.tm_mon == today_tm.tm_mon)
483 && (event_tm.tm_mday == today_tm.tm_mday)
487 p = icalcomponent_get_first_property(
489 ICAL_SUMMARY_PROPERTY);
492 icalproperty_get_comment(p));
493 wprintf(" (%s)<br />\n", timestring);
498 free_calendar_buffer();
503 void free_calendar_buffer(void) {
505 if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
506 icalcomponent_free(WC->disp_cal[i].cal);
516 void do_calendar_view(void) {
519 int year, month, day;
522 /* In case no date was specified, go with today */
524 localtime_r(&now, &tm);
525 year = tm.tm_year + 1900;
526 month = tm.tm_mon + 1;
529 /* Now see if a date was specified */
530 if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
531 if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
532 if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
534 /* How would you like that cooked? */
535 if (strlen(bstr("calview")) > 0) {
536 strcpy(calview, bstr("calview"));
539 strcpy(calview, "month");
542 /* Display the selected view */
543 if (!strcasecmp(calview, "day")) {
544 calendar_day_view(year, month, day);
546 else if (!strcasecmp(calview, "week")) {
547 calendar_week_view(year, month, day);
550 calendar_month_view(year, month, day);
553 /* Free the calendar stuff */
554 free_calendar_buffer();
560 * Helper function for do_tasks_view(). Returns the date/time due.
562 time_t get_task_due_date(icalcomponent *vtodo) {
569 /* If we're looking at a fully encapsulated VCALENDAR
570 * rather than a VTODO component, recurse into the data
571 * structure until we get a VTODO.
573 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
574 return get_task_due_date(
575 icalcomponent_get_first_component(
576 vtodo, ICAL_VTODO_COMPONENT
581 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
583 return(icaltime_as_timet(icalproperty_get_due(p)));
592 * Compare the due dates of two tasks (this is for sorting)
594 int task_due_cmp(const void *task1, const void *task2) {
598 t1 = get_task_due_date(((struct disp_cal *)task1)->cal);
599 t2 = get_task_due_date(((struct disp_cal *)task2)->cal);
601 if (t1 < t2) return(-1);
602 if (t1 > t2) return(1);
610 void do_tasks_view(void) {
617 wprintf("<center><table border=0 cellspacing=0 width=99%% bgcolor=\"#FFFFFF\">\n<tr>\n"
618 "<TH>Name of task</TH>\n"
619 "<TH>Date due</TH></TR>\n"
622 /* Sort them if necessary */
623 if (WC->num_cal > 1) {
626 sizeof(struct disp_cal),
631 if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
634 wprintf("<TR BGCOLOR=\"#%s\"><TD>",
635 (bg ? "DDDDDD" : "FFFFFF")
638 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
639 ICAL_SUMMARY_PROPERTY);
640 wprintf("<A HREF=\"/display_edit_task?msgnum=%ld&taskrm=",
641 WC->disp_cal[i].cal_msgnum );
642 urlescputs(WC->wc_roomname);
645 escputs((char *)icalproperty_get_comment(p));
650 due = get_task_due_date(WC->disp_cal[i].cal);
652 wprintf("<TD><FONT");
653 if (due < time(NULL)) {
654 wprintf(" COLOR=\"#FF0000\"");
656 wprintf(">%s</FONT></TD></TR>\n", buf);
659 wprintf("</table></center>\n");
662 free_calendar_buffer();
666 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */