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 lprintf(9, "Event: %04d-%s-%02d, Now: %04d-%s-%02d\n",
82 event_tm.tm_year + 1900,
83 ascmonths[event_tm.tm_mon],
85 today_tm.tm_year + 1900,
86 ascmonths[today_tm.tm_mon],
89 if ((event_tm.tm_year == today_tm.tm_year)
90 && (event_tm.tm_mon == today_tm.tm_mon)
91 && (event_tm.tm_mday == today_tm.tm_mday)) {
93 p = icalcomponent_get_first_property(
95 ICAL_SUMMARY_PROPERTY);
99 wprintf("<TABLE border=0 cellpadding=2><TR>"
100 "<TD BGCOLOR=\"#CCCCDD\">"
104 wprintf("<FONT SIZE=-1>"
105 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=%s&year=%s&month=%s&day=%s\">",
106 WC->disp_cal[i].cal_msgnum,
113 icalproperty_get_comment(p));
114 wprintf("</A></FONT><br />\n");
117 wprintf("</TD></TR></TABLE>");
131 void calendar_month_view(int year, int month, int day) {
132 struct tm starting_tm;
136 time_t previous_month;
139 /* Determine what day to start.
140 * First, back up to the 1st of the month...
142 memset(&starting_tm, 0, sizeof(struct tm));
143 starting_tm.tm_year = year - 1900;
144 starting_tm.tm_mon = month - 1;
145 starting_tm.tm_mday = day;
146 thetime = mktime(&starting_tm);
148 memcpy(&tm, &starting_tm, sizeof(struct tm));
149 while (tm.tm_mday != 1) {
150 thetime = thetime - (time_t)86400; /* go back 24 hours */
151 localtime_r(&thetime, &tm);
154 /* Determine previous and next months ... for links */
155 previous_month = thetime - (time_t)864000L; /* back 10 days */
156 next_month = thetime + (time_t)(31L * 86400L); /* ahead 31 days */
158 /* Now back up until we're on a Sunday */
159 localtime_r(&thetime, &tm);
160 while (tm.tm_wday != 0) {
161 thetime = thetime - (time_t)86400; /* go back 24 hours */
162 localtime_r(&thetime, &tm);
165 /* Outer table (to get the background color) */
166 wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
167 "bgcolor=#204B78><TR><TD>\n");
169 wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0><tr>\n");
171 wprintf("<TD ALIGN=CENTER>");
173 localtime_r(&previous_month, &tm);
174 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
175 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
176 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>\n");
178 wprintf(" "
179 "<FONT SIZE=+1 COLOR=\"#FFFFFF\">"
182 " ", months[month-1], year);
184 localtime_r(&next_month, &tm);
185 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
186 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
187 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\" BORDER=0></A>\n");
189 wprintf("</TD></TR></TABLE>\n");
191 /* Inner table (the real one) */
192 wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
193 "bgcolor=#204B78><TR>");
194 for (i=0; i<7; ++i) {
195 wprintf("<TD ALIGN=CENTER WIDTH=14%%>"
196 "<FONT COLOR=\"#FFFFFF\">%s</FONT></TH>", days[i]);
201 for (i = 0; i < 35; ++i) {
202 localtime_r(&thetime, &tm);
204 /* Before displaying Sunday, start a new row */
209 wprintf("<TD BGCOLOR=\"#%s\" WIDTH=14%% HEIGHT=60 VALIGN=TOP><B>",
210 ((tm.tm_mon != month-1) ? "DDDDDD" :
211 ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
214 if ((i==0) || (tm.tm_mday == 1)) {
215 wprintf("%s ", months[tm.tm_mon]);
217 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
224 /* put the data here, stupid */
225 calendar_month_view_display_events(thetime);
229 /* After displaying Saturday, end the row */
234 thetime += (time_t)86400; /* ahead 24 hours */
237 wprintf("</TABLE>" /* end of inner table */
238 "</TD></TR></TABLE>" /* end of outer table */
243 void calendar_week_view(int year, int month, int day) {
244 wprintf("<CENTER><I>week view FIXME</I></CENTER><br />\n");
249 * Display events for a particular hour of a particular day.
250 * (Specify hour < 0 to show "all day" events)
252 void calendar_day_view_display_events(int year, int month,
256 struct icaltimetype t;
259 int all_day_event = 0;
261 if (WC->num_cal == 0) {
262 wprintf("<br /><br /><br />\n");
266 for (i=0; i<(WC->num_cal); ++i) {
267 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
268 ICAL_DTSTART_PROPERTY);
270 t = icalproperty_get_dtstart(p);
271 event_tt = icaltime_as_timet(t);
272 if (t.is_date) all_day_event = 1;
275 event_tm = gmtime(&event_tt);
278 event_tm = localtime(&event_tt);
281 if ((event_tm->tm_year == (year-1900))
282 && (event_tm->tm_mon == (month-1))
283 && (event_tm->tm_mday == day)
284 && ( ((event_tm->tm_hour == hour)&&(!t.is_date)) || ((hour<0)&&(t.is_date)) )
288 p = icalcomponent_get_first_property(
290 ICAL_SUMMARY_PROPERTY);
294 wprintf("<TABLE border=1 cellpadding=2><TR>"
295 "<TD BGCOLOR=\"#CCCCCC\">"
299 wprintf("<FONT SIZE=-1>"
300 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
301 WC->disp_cal[i].cal_msgnum,
305 icalproperty_get_comment(p));
306 wprintf("</A></FONT><br />\n");
309 wprintf("</TD></TR></TABLE>");
322 void calendar_day_view(int year, int month, int day) {
324 struct icaltimetype today, yesterday, tomorrow;
327 /* Figure out the dates for "yesterday" and "tomorrow" links */
329 memset(&today, 0, sizeof(struct icaltimetype));
335 memcpy(&yesterday, &today, sizeof(struct icaltimetype));
337 yesterday = icaltime_normalize(yesterday);
339 memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
341 tomorrow = icaltime_normalize(tomorrow);
344 /* Outer table (to get the background color) */
345 wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
346 "bgcolor=#204B78><TR><TD>\n");
348 /* Inner table (the real one) */
349 wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
350 "bgcolor=#204B78><TR>\n");
352 /* Innermost table (contains hours etc.) */
353 wprintf("<TD WIDTH=80%%>"
354 "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
355 "bgcolor=#204B78>\n");
357 /* Display events before 8:00 (hour=-1 is all-day events) */
359 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
360 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
361 for (hour = (-1); hour <= 7; ++hour) {
362 calendar_day_view_display_events(year, month, day, hour);
364 wprintf("</TD></TR>\n");
366 /* Now the middle of the day... */
367 for (hour = 8; hour <= 17; ++hour) { /* could do HEIGHT=xx */
368 wprintf("<TR HEIGHT=30><TD BGCOLOR=\"#CCCCDD\" ALIGN=MIDDLE "
369 "VALIGN=MIDDLE WIDTH=10%%>");
370 wprintf("<A HREF=\"/display_edit_event?msgnum=0"
371 "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
372 year, month, day, hour
374 wprintf("%d:00%s</A> ",
375 (hour <= 12 ? hour : hour-12),
376 (hour < 12 ? "am" : "pm")
378 wprintf("</TD><TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
380 /* put the data here, stupid */
381 calendar_day_view_display_events(year, month, day, hour);
383 wprintf("</TD></TR>\n");
386 /* Display events after 5:00... */
388 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
389 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
390 for (hour = 18; hour <= 23; ++hour) {
391 calendar_day_view_display_events(year, month, day, hour);
393 wprintf("</TD></TR>\n");
396 wprintf("</TABLE>" /* end of innermost table */
400 wprintf("<TD WIDTH=20%% VALIGN=top>"); /* begin stuff-on-the-right */
403 /* Begin todays-date-with-left-and-right-arrows */
404 wprintf("<TABLE BORDER=0 WIDTH=100%% "
405 "CELLSPACING=0 CELLPADDING=0 BGCOLOR=\"#FFFFFF\">\n");
409 wprintf("<TD ALIGN=CENTER>");
410 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
411 yesterday.year, yesterday.month, yesterday.day);
412 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>");
416 wprintf("<TD ALIGN=CENTER>");
417 wprintf("<FONT SIZE=+2>%s</FONT><br />"
418 "<FONT SIZE=+3>%d</FONT><br />"
419 "<FONT SIZE=+2>%d</FONT><br />",
420 months[month-1], day, year);
424 wprintf("<TD ALIGN=CENTER>");
425 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
426 tomorrow.year, tomorrow.month, tomorrow.day);
427 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\""
431 wprintf("</TR></TABLE>\n");
432 /* End todays-date-with-left-and-right-arrows */
434 wprintf("<br /><br /><CENTER><font color=#FFFFFF>"
435 " <A HREF=\"/display_edit_event?msgnum=0"
436 "&year=%d&month=%d&day=%d\">"
437 "Add new calendar event</A>"
442 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">"
443 "Back to month view</A>\n", year, month);
445 wprintf("</FONT></CENTER>\n");
447 wprintf("</TD>"); /* end stuff-on-the-right */
451 wprintf("</TR></TABLE>" /* end of inner table */
452 "</TD></TR></TABLE>" /* end of outer table */
460 * Display today's events.
462 void calendar_summary_view(void) {
465 struct icaltimetype t;
470 int all_day_event = 0;
471 char timestring[SIZ];
473 if (WC->num_cal == 0) {
478 localtime_r(&now, &today_tm);
480 for (i=0; i<(WC->num_cal); ++i) {
481 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
482 ICAL_DTSTART_PROPERTY);
484 t = icalproperty_get_dtstart(p);
485 event_tt = icaltime_as_timet(t);
486 if (t.is_date) all_day_event = 1;
487 fmt_time(timestring, event_tt);
490 gmtime_r(&event_tt, &event_tm);
493 localtime_r(&event_tt, &event_tm);
496 if ( (event_tm.tm_year == today_tm.tm_year)
497 && (event_tm.tm_mon == today_tm.tm_mon)
498 && (event_tm.tm_mday == today_tm.tm_mday)
502 p = icalcomponent_get_first_property(
504 ICAL_SUMMARY_PROPERTY);
507 icalproperty_get_comment(p));
508 wprintf(" (%s)<br />\n", timestring);
513 free_calendar_buffer();
518 void free_calendar_buffer(void) {
520 if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
521 icalcomponent_free(WC->disp_cal[i].cal);
531 void do_calendar_view(void) {
534 int year, month, day;
537 /* In case no date was specified, go with today */
539 localtime_r(&now, &tm);
540 year = tm.tm_year + 1900;
541 month = tm.tm_mon + 1;
544 /* Now see if a date was specified */
545 if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
546 if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
547 if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
549 /* How would you like that cooked? */
550 if (strlen(bstr("calview")) > 0) {
551 strcpy(calview, bstr("calview"));
554 strcpy(calview, "month");
557 /* Display the selected view */
558 if (!strcasecmp(calview, "day")) {
559 calendar_day_view(year, month, day);
561 else if (!strcasecmp(calview, "week")) {
562 calendar_week_view(year, month, day);
565 calendar_month_view(year, month, day);
568 /* Free the calendar stuff */
569 free_calendar_buffer();
575 * Helper function for do_tasks_view(). Returns the date/time due.
577 time_t get_task_due_date(icalcomponent *vtodo) {
584 /* If we're looking at a fully encapsulated VCALENDAR
585 * rather than a VTODO component, recurse into the data
586 * structure until we get a VTODO.
588 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
589 return get_task_due_date(
590 icalcomponent_get_first_component(
591 vtodo, ICAL_VTODO_COMPONENT
596 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
598 return(icaltime_as_timet(icalproperty_get_due(p)));
607 * Compare the due dates of two tasks (this is for sorting)
609 int task_due_cmp(const void *task1, const void *task2) {
613 t1 = get_task_due_date(((struct disp_cal *)task1)->cal);
614 t2 = get_task_due_date(((struct disp_cal *)task2)->cal);
616 if (t1 < t2) return(-1);
617 if (t1 > t2) return(1);
625 void do_tasks_view(void) {
632 do_template("beginbox_nt");
634 wprintf("<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%%>\n<TR>\n"
635 "<TH>Name of task</TH>\n"
636 "<TH>Date due</TH></TR>\n"
639 /* Sort them if necessary */
640 if (WC->num_cal > 1) {
643 sizeof(struct disp_cal),
648 if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
651 wprintf("<TR BGCOLOR=\"#%s\"><TD>",
652 (bg ? "DDDDDD" : "FFFFFF")
655 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
656 ICAL_SUMMARY_PROPERTY);
657 wprintf("<A HREF=\"/display_edit_task?msgnum=%ld&taskrm=",
658 WC->disp_cal[i].cal_msgnum );
659 urlescputs(WC->wc_roomname);
662 escputs((char *)icalproperty_get_comment(p));
667 due = get_task_due_date(WC->disp_cal[i].cal);
669 wprintf("<TD><FONT");
670 if (due < time(NULL)) {
671 wprintf(" COLOR=\"#FF0000\"");
673 wprintf(">%s</FONT></TD></TR>\n", buf);
676 wprintf("</TABLE>\n");
678 do_template("endbox");
682 free_calendar_buffer();
686 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */