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");
170 wprintf("<TD align=left><font color=#FFFFFF size=-2>"
171 "Click on any date for day view "
174 wprintf("<TD ALIGN=CENTER>");
176 localtime_r(&previous_month, &tm);
177 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
178 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
179 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>\n");
181 wprintf(" "
182 "<FONT SIZE=+1 COLOR=\"#FFFFFF\">"
185 " ", months[month-1], year);
187 localtime_r(&next_month, &tm);
188 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
189 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
190 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\" BORDER=0></A>\n");
192 wprintf("</TD><TD align=right><font color=#FFFFFF size=-2>"
193 "Click on any date for day view "
194 "</FONT></TD></TR></TABLE>\n");
196 /* Inner table (the real one) */
197 wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
198 "bgcolor=#204B78><TR>");
199 for (i=0; i<7; ++i) {
200 wprintf("<TD ALIGN=CENTER WIDTH=14%%>"
201 "<FONT COLOR=\"#FFFFFF\">%s</FONT></TH>", days[i]);
206 for (i = 0; i < 35; ++i) {
207 localtime_r(&thetime, &tm);
209 /* Before displaying Sunday, start a new row */
214 wprintf("<TD BGCOLOR=\"#%s\" WIDTH=14%% HEIGHT=60 VALIGN=TOP><B>",
215 ((tm.tm_mon != month-1) ? "DDDDDD" :
216 ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
219 if ((i==0) || (tm.tm_mday == 1)) {
220 wprintf("%s ", months[tm.tm_mon]);
222 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
229 /* put the data here, stupid */
230 calendar_month_view_display_events(thetime);
234 /* After displaying Saturday, end the row */
239 thetime += (time_t)86400; /* ahead 24 hours */
242 wprintf("</TABLE>" /* end of inner table */
243 "</TD></TR></TABLE>" /* end of outer table */
248 void calendar_week_view(int year, int month, int day) {
249 wprintf("<CENTER><I>week view FIXME</I></CENTER><br />\n");
254 * Display events for a particular hour of a particular day.
255 * (Specify hour < 0 to show "all day" events)
257 void calendar_day_view_display_events(int year, int month,
261 struct icaltimetype t;
264 int all_day_event = 0;
266 if (WC->num_cal == 0) {
267 wprintf("<br /><br /><br />\n");
271 for (i=0; i<(WC->num_cal); ++i) {
272 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
273 ICAL_DTSTART_PROPERTY);
275 t = icalproperty_get_dtstart(p);
276 event_tt = icaltime_as_timet(t);
277 if (t.is_date) all_day_event = 1;
280 event_tm = gmtime(&event_tt);
283 event_tm = localtime(&event_tt);
286 if ((event_tm->tm_year == (year-1900))
287 && (event_tm->tm_mon == (month-1))
288 && (event_tm->tm_mday == day)
289 && ( ((event_tm->tm_hour == hour)&&(!t.is_date)) || ((hour<0)&&(t.is_date)) )
293 p = icalcomponent_get_first_property(
295 ICAL_SUMMARY_PROPERTY);
299 wprintf("<TABLE border=1 cellpadding=2><TR>"
300 "<TD BGCOLOR=\"#CCCCCC\">"
304 wprintf("<FONT SIZE=-1>"
305 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
306 WC->disp_cal[i].cal_msgnum,
310 icalproperty_get_comment(p));
311 wprintf("</A></FONT><br />\n");
314 wprintf("</TD></TR></TABLE>");
327 void calendar_day_view(int year, int month, int day) {
329 struct icaltimetype today, yesterday, tomorrow;
332 /* Figure out the dates for "yesterday" and "tomorrow" links */
334 memset(&today, 0, sizeof(struct icaltimetype));
340 memcpy(&yesterday, &today, sizeof(struct icaltimetype));
342 yesterday = icaltime_normalize(yesterday);
344 memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
346 tomorrow = icaltime_normalize(tomorrow);
349 /* Outer table (to get the background color) */
350 wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
351 "bgcolor=#204B78><TR><TD>\n");
353 /* Inner table (the real one) */
354 wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
355 "bgcolor=#204B78><TR>\n");
357 /* Innermost table (contains hours etc.) */
358 wprintf("<TD WIDTH=80%%>"
359 "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
360 "bgcolor=#204B78>\n");
362 /* Display events before 8:00 (hour=-1 is all-day events) */
364 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
365 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
366 for (hour = (-1); hour <= 7; ++hour) {
367 calendar_day_view_display_events(year, month, day, hour);
369 wprintf("</TD></TR>\n");
371 /* Now the middle of the day... */
372 for (hour = 8; hour <= 17; ++hour) { /* could do HEIGHT=xx */
373 wprintf("<TR HEIGHT=30><TD BGCOLOR=\"#CCCCDD\" ALIGN=MIDDLE "
374 "VALIGN=MIDDLE WIDTH=10%%>");
375 wprintf("<A HREF=\"/display_edit_event?msgnum=0"
376 "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
377 year, month, day, hour
379 wprintf("%d:00%s</A> ",
380 (hour <= 12 ? hour : hour-12),
381 (hour < 12 ? "am" : "pm")
383 wprintf("</TD><TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
385 /* put the data here, stupid */
386 calendar_day_view_display_events(year, month, day, hour);
388 wprintf("</TD></TR>\n");
391 /* Display events after 5:00... */
393 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
394 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
395 for (hour = 18; hour <= 23; ++hour) {
396 calendar_day_view_display_events(year, month, day, hour);
398 wprintf("</TD></TR>\n");
401 wprintf("</TABLE>" /* end of innermost table */
405 wprintf("<TD WIDTH=20%% VALIGN=top>"); /* begin stuff-on-the-right */
408 /* Begin todays-date-with-left-and-right-arrows */
409 wprintf("<TABLE BORDER=0 WIDTH=100%% "
410 "CELLSPACING=0 CELLPADDING=0 BGCOLOR=\"#FFFFFF\">\n");
414 wprintf("<TD ALIGN=CENTER>");
415 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
416 yesterday.year, yesterday.month, yesterday.day);
417 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>");
421 wprintf("<TD ALIGN=CENTER>");
422 wprintf("<FONT SIZE=+2>%s</FONT><br />"
423 "<FONT SIZE=+3>%d</FONT><br />"
424 "<FONT SIZE=+2>%d</FONT><br />",
425 months[month-1], day, year);
429 wprintf("<TD ALIGN=CENTER>");
430 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
431 tomorrow.year, tomorrow.month, tomorrow.day);
432 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\""
436 wprintf("</TR></TABLE>\n");
437 /* End todays-date-with-left-and-right-arrows */
439 wprintf("<br /><br /><CENTER><font color=#FFFFFF>"
440 " <A HREF=\"/display_edit_event?msgnum=0"
441 "&year=%d&month=%d&day=%d\">"
442 "Add new calendar event</A>"
447 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">"
448 "Back to month view</A>\n", year, month);
450 wprintf("</FONT></CENTER>\n");
452 wprintf("</TD>"); /* end stuff-on-the-right */
456 wprintf("</TR></TABLE>" /* end of inner table */
457 "</TD></TR></TABLE>" /* end of outer table */
465 * Display today's events.
467 void calendar_summary_view(void) {
470 struct icaltimetype t;
475 int all_day_event = 0;
476 char timestring[SIZ];
478 if (WC->num_cal == 0) {
483 localtime_r(&now, &today_tm);
485 for (i=0; i<(WC->num_cal); ++i) {
486 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
487 ICAL_DTSTART_PROPERTY);
489 t = icalproperty_get_dtstart(p);
490 event_tt = icaltime_as_timet(t);
491 if (t.is_date) all_day_event = 1;
492 fmt_time(timestring, event_tt);
495 gmtime_r(&event_tt, &event_tm);
498 localtime_r(&event_tt, &event_tm);
501 if ( (event_tm.tm_year == today_tm.tm_year)
502 && (event_tm.tm_mon == today_tm.tm_mon)
503 && (event_tm.tm_mday == today_tm.tm_mday)
507 p = icalcomponent_get_first_property(
509 ICAL_SUMMARY_PROPERTY);
512 icalproperty_get_comment(p));
513 wprintf(" (%s)<br />\n", timestring);
518 free_calendar_buffer();
523 void free_calendar_buffer(void) {
525 if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
526 icalcomponent_free(WC->disp_cal[i].cal);
536 void do_calendar_view(void) {
539 int year, month, day;
542 /* In case no date was specified, go with today */
544 localtime_r(&now, &tm);
545 year = tm.tm_year + 1900;
546 month = tm.tm_mon + 1;
549 /* Now see if a date was specified */
550 if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
551 if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
552 if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
554 /* How would you like that cooked? */
555 if (strlen(bstr("calview")) > 0) {
556 strcpy(calview, bstr("calview"));
559 strcpy(calview, "month");
562 /* Display the selected view */
563 if (!strcasecmp(calview, "day")) {
564 calendar_day_view(year, month, day);
566 else if (!strcasecmp(calview, "week")) {
567 calendar_week_view(year, month, day);
570 calendar_month_view(year, month, day);
573 /* Free the calendar stuff */
574 free_calendar_buffer();
580 * Helper function for do_tasks_view(). Returns the date/time due.
582 time_t get_task_due_date(icalcomponent *vtodo) {
589 /* If we're looking at a fully encapsulated VCALENDAR
590 * rather than a VTODO component, recurse into the data
591 * structure until we get a VTODO.
593 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
594 return get_task_due_date(
595 icalcomponent_get_first_component(
596 vtodo, ICAL_VTODO_COMPONENT
601 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
603 return(icaltime_as_timet(icalproperty_get_due(p)));
612 * Compare the due dates of two tasks (this is for sorting)
614 int task_due_cmp(const void *task1, const void *task2) {
618 t1 = get_task_due_date(((struct disp_cal *)task1)->cal);
619 t2 = get_task_due_date(((struct disp_cal *)task2)->cal);
621 if (t1 < t2) return(-1);
622 if (t1 > t2) return(1);
630 void do_tasks_view(void) {
637 do_template("beginbox_nt");
639 wprintf("<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%%>\n<TR>\n"
640 "<TH>Name of task</TH>\n"
641 "<TH>Date due</TH></TR>\n"
644 /* Sort them if necessary */
645 if (WC->num_cal > 1) {
648 sizeof(struct disp_cal),
653 if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
656 wprintf("<TR BGCOLOR=\"#%s\"><TD>",
657 (bg ? "DDDDDD" : "FFFFFF")
660 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
661 ICAL_SUMMARY_PROPERTY);
662 wprintf("<A HREF=\"/display_edit_task?msgnum=%ld&taskrm=",
663 WC->disp_cal[i].cal_msgnum );
664 urlescputs(WC->wc_roomname);
667 escputs((char *)icalproperty_get_comment(p));
672 due = get_task_due_date(WC->disp_cal[i].cal);
674 wprintf("<TD><FONT");
675 if (due < time(NULL)) {
676 wprintf(" COLOR=\"#FF0000\"");
678 wprintf(">%s</FONT></TD></TR>\n", buf);
681 wprintf("</TABLE>\n");
683 wprintf("<hr /><A HREF=\"/display_edit_task?msgnum=0\">"
687 do_template("endbox");
691 free_calendar_buffer();
695 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */