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>"
170 "<TR><TD align=left><font color=#FFFFFF>"
171 " <A HREF=\"/display_edit_event?msgnum=0"
172 "&year=%d&month=%d&day=%d\">"
173 "Add new calendar event</A>"
178 wprintf("<TD ALIGN=CENTER>");
180 localtime_r(&previous_month, &tm);
181 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
182 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
183 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>\n");
185 wprintf(" "
186 "<FONT SIZE=+1 COLOR=\"#FFFFFF\">"
189 " ", months[month-1], year);
191 localtime_r(&next_month, &tm);
192 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
193 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
194 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\" BORDER=0></A>\n");
196 wprintf("</TD><TD align=right><font color=#FFFFFF size=-2>"
197 "Click on any date for day view "
198 "</FONT></TD></TR></TABLE>\n");
200 /* Inner table (the real one) */
201 wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
202 "bgcolor=#204B78><TR>");
203 for (i=0; i<7; ++i) {
204 wprintf("<TD ALIGN=CENTER WIDTH=14%%>"
205 "<FONT COLOR=\"#FFFFFF\">%s</FONT></TH>", days[i]);
210 for (i = 0; i < 35; ++i) {
211 localtime_r(&thetime, &tm);
213 /* Before displaying Sunday, start a new row */
218 wprintf("<TD BGCOLOR=\"#%s\" WIDTH=14%% HEIGHT=60 VALIGN=TOP><B>",
219 ((tm.tm_mon != month-1) ? "DDDDDD" :
220 ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
223 if ((i==0) || (tm.tm_mday == 1)) {
224 wprintf("%s ", months[tm.tm_mon]);
226 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
233 /* put the data here, stupid */
234 calendar_month_view_display_events(thetime);
238 /* After displaying Saturday, end the row */
243 thetime += (time_t)86400; /* ahead 24 hours */
246 wprintf("</TABLE>" /* end of inner table */
247 "</TD></TR></TABLE>" /* end of outer table */
252 void calendar_week_view(int year, int month, int day) {
253 wprintf("<CENTER><I>week view FIXME</I></CENTER><br />\n");
258 * Display events for a particular hour of a particular day.
259 * (Specify hour < 0 to show "all day" events)
261 void calendar_day_view_display_events(int year, int month,
265 struct icaltimetype t;
268 int all_day_event = 0;
270 if (WC->num_cal == 0) {
271 wprintf("<br /><br /><br />\n");
275 for (i=0; i<(WC->num_cal); ++i) {
276 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
277 ICAL_DTSTART_PROPERTY);
279 t = icalproperty_get_dtstart(p);
280 event_tt = icaltime_as_timet(t);
281 if (t.is_date) all_day_event = 1;
284 event_tm = gmtime(&event_tt);
287 event_tm = localtime(&event_tt);
290 if ((event_tm->tm_year == (year-1900))
291 && (event_tm->tm_mon == (month-1))
292 && (event_tm->tm_mday == day)
293 && ( ((event_tm->tm_hour == hour)&&(!t.is_date)) || ((hour<0)&&(t.is_date)) )
297 p = icalcomponent_get_first_property(
299 ICAL_SUMMARY_PROPERTY);
303 wprintf("<TABLE border=1 cellpadding=2><TR>"
304 "<TD BGCOLOR=\"#CCCCCC\">"
308 wprintf("<FONT SIZE=-1>"
309 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
310 WC->disp_cal[i].cal_msgnum,
314 icalproperty_get_comment(p));
315 wprintf("</A></FONT><br />\n");
318 wprintf("</TD></TR></TABLE>");
331 void calendar_day_view(int year, int month, int day) {
333 struct icaltimetype today, yesterday, tomorrow;
336 /* Figure out the dates for "yesterday" and "tomorrow" links */
338 memset(&today, 0, sizeof(struct icaltimetype));
344 memcpy(&yesterday, &today, sizeof(struct icaltimetype));
346 yesterday = icaltime_normalize(yesterday);
348 memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
350 tomorrow = icaltime_normalize(tomorrow);
353 /* Outer table (to get the background color) */
354 wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
355 "bgcolor=#204B78><TR><TD>\n");
357 /* Inner table (the real one) */
358 wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
359 "bgcolor=#204B78><TR>\n");
361 /* Innermost table (contains hours etc.) */
362 wprintf("<TD WIDTH=80%%>"
363 "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
364 "bgcolor=#204B78>\n");
366 /* Display events before 8:00 (hour=-1 is all-day events) */
368 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
369 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
370 for (hour = (-1); hour <= 7; ++hour) {
371 calendar_day_view_display_events(year, month, day, hour);
373 wprintf("</TD></TR>\n");
375 /* Now the middle of the day... */
376 for (hour = 8; hour <= 17; ++hour) { /* could do HEIGHT=xx */
377 wprintf("<TR HEIGHT=30><TD BGCOLOR=\"#CCCCDD\" ALIGN=MIDDLE "
378 "VALIGN=MIDDLE WIDTH=10%%>");
379 wprintf("<A HREF=\"/display_edit_event?msgnum=0"
380 "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
381 year, month, day, hour
383 wprintf("%d:00%s</A> ",
384 (hour <= 12 ? hour : hour-12),
385 (hour < 12 ? "am" : "pm")
387 wprintf("</TD><TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
389 /* put the data here, stupid */
390 calendar_day_view_display_events(year, month, day, hour);
392 wprintf("</TD></TR>\n");
395 /* Display events after 5:00... */
397 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
398 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
399 for (hour = 18; hour <= 23; ++hour) {
400 calendar_day_view_display_events(year, month, day, hour);
402 wprintf("</TD></TR>\n");
405 wprintf("</TABLE>" /* end of innermost table */
409 wprintf("<TD WIDTH=20%% VALIGN=top>"); /* begin stuff-on-the-right */
412 /* Begin todays-date-with-left-and-right-arrows */
413 wprintf("<TABLE BORDER=0 WIDTH=100%% "
414 "CELLSPACING=0 CELLPADDING=0 BGCOLOR=\"#FFFFFF\">\n");
418 wprintf("<TD ALIGN=CENTER>");
419 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
420 yesterday.year, yesterday.month, yesterday.day);
421 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>");
425 wprintf("<TD ALIGN=CENTER>");
426 wprintf("<FONT SIZE=+2>%s</FONT><br />"
427 "<FONT SIZE=+3>%d</FONT><br />"
428 "<FONT SIZE=+2>%d</FONT><br />",
429 months[month-1], day, year);
433 wprintf("<TD ALIGN=CENTER>");
434 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
435 tomorrow.year, tomorrow.month, tomorrow.day);
436 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\""
440 wprintf("</TR></TABLE>\n");
441 /* End todays-date-with-left-and-right-arrows */
443 wprintf("<br /><br /><CENTER><font color=#FFFFFF>"
444 " <A HREF=\"/display_edit_event?msgnum=0"
445 "&year=%d&month=%d&day=%d\">"
446 "Add new calendar event</A>"
451 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">"
452 "Back to month view</A>\n", year, month);
454 wprintf("</FONT></CENTER>\n");
456 wprintf("</TD>"); /* end stuff-on-the-right */
460 wprintf("</TR></TABLE>" /* end of inner table */
461 "</TD></TR></TABLE>" /* end of outer table */
469 * Display today's events.
471 void calendar_summary_view(void) {
474 struct icaltimetype t;
479 int all_day_event = 0;
480 char timestring[SIZ];
482 if (WC->num_cal == 0) {
487 localtime_r(&now, &today_tm);
489 for (i=0; i<(WC->num_cal); ++i) {
490 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
491 ICAL_DTSTART_PROPERTY);
493 t = icalproperty_get_dtstart(p);
494 event_tt = icaltime_as_timet(t);
495 if (t.is_date) all_day_event = 1;
496 fmt_time(timestring, event_tt);
499 gmtime_r(&event_tt, &event_tm);
502 localtime_r(&event_tt, &event_tm);
505 if ( (event_tm.tm_year == today_tm.tm_year)
506 && (event_tm.tm_mon == today_tm.tm_mon)
507 && (event_tm.tm_mday == today_tm.tm_mday)
511 p = icalcomponent_get_first_property(
513 ICAL_SUMMARY_PROPERTY);
516 icalproperty_get_comment(p));
517 wprintf(" (%s)<br />\n", timestring);
522 free_calendar_buffer();
527 void free_calendar_buffer(void) {
529 if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
530 icalcomponent_free(WC->disp_cal[i].cal);
540 void do_calendar_view(void) {
543 int year, month, day;
546 /* In case no date was specified, go with today */
548 localtime_r(&now, &tm);
549 year = tm.tm_year + 1900;
550 month = tm.tm_mon + 1;
553 /* Now see if a date was specified */
554 if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
555 if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
556 if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
558 /* How would you like that cooked? */
559 if (strlen(bstr("calview")) > 0) {
560 strcpy(calview, bstr("calview"));
563 strcpy(calview, "month");
566 /* Display the selected view */
567 if (!strcasecmp(calview, "day")) {
568 calendar_day_view(year, month, day);
570 else if (!strcasecmp(calview, "week")) {
571 calendar_week_view(year, month, day);
574 calendar_month_view(year, month, day);
577 /* Free the calendar stuff */
578 free_calendar_buffer();
584 * Helper function for do_tasks_view(). Returns the date/time due.
586 time_t get_task_due_date(icalcomponent *vtodo) {
593 /* If we're looking at a fully encapsulated VCALENDAR
594 * rather than a VTODO component, recurse into the data
595 * structure until we get a VTODO.
597 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
598 return get_task_due_date(
599 icalcomponent_get_first_component(
600 vtodo, ICAL_VTODO_COMPONENT
605 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
607 return(icaltime_as_timet(icalproperty_get_due(p)));
616 * Compare the due dates of two tasks (this is for sorting)
618 int task_due_cmp(const void *task1, const void *task2) {
622 t1 = get_task_due_date(((struct disp_cal *)task1)->cal);
623 t2 = get_task_due_date(((struct disp_cal *)task2)->cal);
625 if (t1 < t2) return(-1);
626 if (t1 > t2) return(1);
634 void do_tasks_view(void) {
641 do_template("beginbox_nt");
643 wprintf("<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%%>\n<TR>\n"
644 "<TH>Name of task</TH>\n"
645 "<TH>Date due</TH></TR>\n"
648 /* Sort them if necessary */
649 if (WC->num_cal > 1) {
652 sizeof(struct disp_cal),
657 if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
660 wprintf("<TR BGCOLOR=\"#%s\"><TD>",
661 (bg ? "DDDDDD" : "FFFFFF")
664 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
665 ICAL_SUMMARY_PROPERTY);
666 wprintf("<A HREF=\"/display_edit_task?msgnum=%ld&taskrm=",
667 WC->disp_cal[i].cal_msgnum );
668 urlescputs(WC->wc_roomname);
671 escputs((char *)icalproperty_get_comment(p));
676 due = get_task_due_date(WC->disp_cal[i].cal);
678 wprintf("<TD><FONT");
679 if (due < time(NULL)) {
680 wprintf(" COLOR=\"#FF0000\"");
682 wprintf(">%s</FONT></TD></TR>\n", buf);
685 wprintf("</TABLE>\n");
687 wprintf("<hr /><A HREF=\"/display_edit_task?msgnum=0\">"
691 do_template("endbox");
695 free_calendar_buffer();
699 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */