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/%02d/%02d, Now: %04d/%02d/%02d\n",
90 if ((event_tm.tm_year == today_tm.tm_year)
91 && (event_tm.tm_mon == today_tm.tm_mon)
92 && (event_tm.tm_mday == today_tm.tm_mday)) {
94 p = icalcomponent_get_first_property(
96 ICAL_SUMMARY_PROPERTY);
100 wprintf("<TABLE border=0 cellpadding=2><TR>"
101 "<TD BGCOLOR=\"#CCCCDD\">"
105 wprintf("<FONT SIZE=-1>"
106 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=%s&year=%s&month=%s&day=%s\">",
107 WC->disp_cal[i].cal_msgnum,
114 icalproperty_get_comment(p));
115 wprintf("</A></FONT><br />\n");
118 wprintf("</TD></TR></TABLE>");
132 void calendar_month_view(int year, int month, int day) {
133 struct tm starting_tm;
137 time_t previous_month;
140 /* Determine what day to start.
141 * First, back up to the 1st of the month...
143 memset(&starting_tm, 0, sizeof(struct tm));
144 starting_tm.tm_year = year - 1900;
145 starting_tm.tm_mon = month - 1;
146 starting_tm.tm_mday = day;
147 thetime = mktime(&starting_tm);
149 memcpy(&tm, &starting_tm, sizeof(struct tm));
150 while (tm.tm_mday != 1) {
151 thetime = thetime - (time_t)86400; /* go back 24 hours */
152 localtime_r(&thetime, &tm);
155 /* Determine previous and next months ... for links */
156 previous_month = thetime - (time_t)864000L; /* back 10 days */
157 next_month = thetime + (time_t)(31L * 86400L); /* ahead 31 days */
159 /* Now back up until we're on a Sunday */
160 localtime_r(&thetime, &tm);
161 while (tm.tm_wday != 0) {
162 thetime = thetime - (time_t)86400; /* go back 24 hours */
163 localtime_r(&thetime, &tm);
166 /* Outer table (to get the background color) */
167 wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
168 "bgcolor=#204B78><TR><TD>\n");
170 wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0>"
171 "<TR><TD align=left><font color=#FFFFFF>"
172 " <A HREF=\"/display_edit_event?msgnum=0"
173 "&year=%d&month=%d&day=%d\">"
174 "Add new calendar event</A>"
179 wprintf("<TD ALIGN=CENTER>");
181 localtime_r(&previous_month, &tm);
182 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
183 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
184 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>\n");
186 wprintf(" "
187 "<FONT SIZE=+1 COLOR=\"#FFFFFF\">"
190 " ", months[month-1], year);
192 localtime_r(&next_month, &tm);
193 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
194 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
195 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\" BORDER=0></A>\n");
197 wprintf("</TD><TD align=right><font color=#FFFFFF size=-2>"
198 "Click on any date for day view "
199 "</FONT></TD></TR></TABLE>\n");
201 /* Inner table (the real one) */
202 wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
203 "bgcolor=#204B78><TR>");
204 for (i=0; i<7; ++i) {
205 wprintf("<TD ALIGN=CENTER WIDTH=14%%>"
206 "<FONT COLOR=\"#FFFFFF\">%s</FONT></TH>", days[i]);
211 for (i = 0; i < 35; ++i) {
212 localtime_r(&thetime, &tm);
214 /* Before displaying Sunday, start a new row */
219 wprintf("<TD BGCOLOR=\"#%s\" WIDTH=14%% HEIGHT=60 VALIGN=TOP><B>",
220 ((tm.tm_mon != month-1) ? "DDDDDD" :
221 ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
224 if ((i==0) || (tm.tm_mday == 1)) {
225 wprintf("%s ", months[tm.tm_mon]);
227 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
234 /* put the data here, stupid */
235 calendar_month_view_display_events(thetime);
239 /* After displaying Saturday, end the row */
244 thetime += (time_t)86400; /* ahead 24 hours */
247 wprintf("</TABLE>" /* end of inner table */
248 "</TD></TR></TABLE>" /* end of outer table */
253 void calendar_week_view(int year, int month, int day) {
254 wprintf("<CENTER><I>week view FIXME</I></CENTER><br />\n");
259 * Display events for a particular hour of a particular day.
260 * (Specify hour < 0 to show "all day" events)
262 void calendar_day_view_display_events(int year, int month,
266 struct icaltimetype t;
269 int all_day_event = 0;
271 if (WC->num_cal == 0) {
272 wprintf("<br /><br /><br />\n");
276 for (i=0; i<(WC->num_cal); ++i) {
277 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
278 ICAL_DTSTART_PROPERTY);
280 t = icalproperty_get_dtstart(p);
281 event_tt = icaltime_as_timet(t);
282 if (t.is_date) all_day_event = 1;
285 event_tm = gmtime(&event_tt);
288 event_tm = localtime(&event_tt);
291 if ((event_tm->tm_year == (year-1900))
292 && (event_tm->tm_mon == (month-1))
293 && (event_tm->tm_mday == day)
294 && ( ((event_tm->tm_hour == hour)&&(!t.is_date)) || ((hour<0)&&(t.is_date)) )
298 p = icalcomponent_get_first_property(
300 ICAL_SUMMARY_PROPERTY);
304 wprintf("<TABLE border=1 cellpadding=2><TR>"
305 "<TD BGCOLOR=\"#CCCCCC\">"
309 wprintf("<FONT SIZE=-1>"
310 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
311 WC->disp_cal[i].cal_msgnum,
315 icalproperty_get_comment(p));
316 wprintf("</A></FONT><br />\n");
319 wprintf("</TD></TR></TABLE>");
332 void calendar_day_view(int year, int month, int day) {
334 struct icaltimetype today, yesterday, tomorrow;
337 /* Figure out the dates for "yesterday" and "tomorrow" links */
339 memset(&today, 0, sizeof(struct icaltimetype));
345 memcpy(&yesterday, &today, sizeof(struct icaltimetype));
347 yesterday = icaltime_normalize(yesterday);
349 memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
351 tomorrow = icaltime_normalize(tomorrow);
354 /* Outer table (to get the background color) */
355 wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
356 "bgcolor=#204B78><TR><TD>\n");
358 /* Inner table (the real one) */
359 wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
360 "bgcolor=#204B78><TR>\n");
362 /* Innermost table (contains hours etc.) */
363 wprintf("<TD WIDTH=80%%>"
364 "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
365 "bgcolor=#204B78>\n");
367 /* Display events before 8:00 (hour=-1 is all-day events) */
369 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
370 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
371 for (hour = (-1); hour <= 7; ++hour) {
372 calendar_day_view_display_events(year, month, day, hour);
374 wprintf("</TD></TR>\n");
376 /* Now the middle of the day... */
377 for (hour = 8; hour <= 17; ++hour) { /* could do HEIGHT=xx */
378 wprintf("<TR HEIGHT=30><TD BGCOLOR=\"#CCCCDD\" ALIGN=MIDDLE "
379 "VALIGN=MIDDLE WIDTH=10%%>");
380 wprintf("<A HREF=\"/display_edit_event?msgnum=0"
381 "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
382 year, month, day, hour
384 wprintf("%d:00%s</A> ",
385 (hour <= 12 ? hour : hour-12),
386 (hour < 12 ? "am" : "pm")
388 wprintf("</TD><TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
390 /* put the data here, stupid */
391 calendar_day_view_display_events(year, month, day, hour);
393 wprintf("</TD></TR>\n");
396 /* Display events after 5:00... */
398 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
399 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
400 for (hour = 18; hour <= 23; ++hour) {
401 calendar_day_view_display_events(year, month, day, hour);
403 wprintf("</TD></TR>\n");
406 wprintf("</TABLE>" /* end of innermost table */
410 wprintf("<TD WIDTH=20%% VALIGN=top>"); /* begin stuff-on-the-right */
413 /* Begin todays-date-with-left-and-right-arrows */
414 wprintf("<TABLE BORDER=0 WIDTH=100%% "
415 "CELLSPACING=0 CELLPADDING=0 BGCOLOR=\"#FFFFFF\">\n");
419 wprintf("<TD ALIGN=CENTER>");
420 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
421 yesterday.year, yesterday.month, yesterday.day);
422 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>");
426 wprintf("<TD ALIGN=CENTER>");
427 wprintf("<FONT SIZE=+2>%s</FONT><br />"
428 "<FONT SIZE=+3>%d</FONT><br />"
429 "<FONT SIZE=+2>%d</FONT><br />",
430 months[month-1], day, year);
434 wprintf("<TD ALIGN=CENTER>");
435 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
436 tomorrow.year, tomorrow.month, tomorrow.day);
437 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\""
441 wprintf("</TR></TABLE>\n");
442 /* End todays-date-with-left-and-right-arrows */
444 wprintf("<br /><br /><CENTER><font color=#FFFFFF>"
445 " <A HREF=\"/display_edit_event?msgnum=0"
446 "&year=%d&month=%d&day=%d\">"
447 "Add new calendar event</A>"
452 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">"
453 "Back to month view</A>\n", year, month);
455 wprintf("</FONT></CENTER>\n");
457 wprintf("</TD>"); /* end stuff-on-the-right */
461 wprintf("</TR></TABLE>" /* end of inner table */
462 "</TD></TR></TABLE>" /* end of outer table */
470 * Display today's events.
472 void calendar_summary_view(void) {
475 struct icaltimetype t;
480 int all_day_event = 0;
481 char timestring[SIZ];
483 if (WC->num_cal == 0) {
488 localtime_r(&now, &today_tm);
490 for (i=0; i<(WC->num_cal); ++i) {
491 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
492 ICAL_DTSTART_PROPERTY);
494 t = icalproperty_get_dtstart(p);
495 event_tt = icaltime_as_timet(t);
496 if (t.is_date) all_day_event = 1;
497 fmt_time(timestring, event_tt);
500 gmtime_r(&event_tt, &event_tm);
503 localtime_r(&event_tt, &event_tm);
506 if ( (event_tm.tm_year == today_tm.tm_year)
507 && (event_tm.tm_mon == today_tm.tm_mon)
508 && (event_tm.tm_mday == today_tm.tm_mday)
512 p = icalcomponent_get_first_property(
514 ICAL_SUMMARY_PROPERTY);
517 icalproperty_get_comment(p));
518 wprintf(" (%s)<br />\n", timestring);
523 free_calendar_buffer();
528 void free_calendar_buffer(void) {
530 if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
531 icalcomponent_free(WC->disp_cal[i].cal);
541 void do_calendar_view(void) {
544 int year, month, day;
547 /* In case no date was specified, go with today */
549 localtime_r(&now, &tm);
550 year = tm.tm_year + 1900;
551 month = tm.tm_mon + 1;
554 /* Now see if a date was specified */
555 if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
556 if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
557 if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
559 /* How would you like that cooked? */
560 if (strlen(bstr("calview")) > 0) {
561 strcpy(calview, bstr("calview"));
564 strcpy(calview, "month");
567 /* Display the selected view */
568 if (!strcasecmp(calview, "day")) {
569 calendar_day_view(year, month, day);
571 else if (!strcasecmp(calview, "week")) {
572 calendar_week_view(year, month, day);
575 calendar_month_view(year, month, day);
578 /* Free the calendar stuff */
579 free_calendar_buffer();
585 * Helper function for do_tasks_view(). Returns the date/time due.
587 time_t get_task_due_date(icalcomponent *vtodo) {
594 /* If we're looking at a fully encapsulated VCALENDAR
595 * rather than a VTODO component, recurse into the data
596 * structure until we get a VTODO.
598 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
599 return get_task_due_date(
600 icalcomponent_get_first_component(
601 vtodo, ICAL_VTODO_COMPONENT
606 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
608 return(icaltime_as_timet(icalproperty_get_due(p)));
617 * Compare the due dates of two tasks (this is for sorting)
619 int task_due_cmp(const void *task1, const void *task2) {
623 t1 = get_task_due_date(((struct disp_cal *)task1)->cal);
624 t2 = get_task_due_date(((struct disp_cal *)task2)->cal);
626 if (t1 < t2) return(-1);
627 if (t1 > t2) return(1);
635 void do_tasks_view(void) {
642 do_template("beginbox_nt");
644 wprintf("<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%%>\n<TR>\n"
645 "<TH>Name of task</TH>\n"
646 "<TH>Date due</TH></TR>\n"
649 /* Sort them if necessary */
650 if (WC->num_cal > 1) {
653 sizeof(struct disp_cal),
658 if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
661 wprintf("<TR BGCOLOR=\"#%s\"><TD>",
662 (bg ? "DDDDDD" : "FFFFFF")
665 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
666 ICAL_SUMMARY_PROPERTY);
667 wprintf("<A HREF=\"/display_edit_task?msgnum=%ld&taskrm=",
668 WC->disp_cal[i].cal_msgnum );
669 urlescputs(WC->wc_roomname);
672 escputs((char *)icalproperty_get_comment(p));
677 due = get_task_due_date(WC->disp_cal[i].cal);
679 wprintf("<TD><FONT");
680 if (due < time(NULL)) {
681 wprintf(" COLOR=\"#FF0000\"");
683 wprintf(">%s</FONT></TD></TR>\n", buf);
686 wprintf("</TABLE>\n");
688 wprintf("<hr /><A HREF=\"/display_edit_task?msgnum=0\">"
692 do_template("endbox");
696 free_calendar_buffer();
700 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */