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 #else /* WEBCIT_WITH_CALENDAR_SERVICE */
37 /****************************************************************************/
40 void calendar_month_view_display_events(time_t thetime) {
46 struct icaltimetype t;
48 int all_day_event = 0;
50 if (WC->num_cal == 0) {
51 wprintf("<BR><BR><BR>\n");
55 memcpy(&today_tm, localtime(&thetime), sizeof(struct tm));
56 month = today_tm.tm_mon + 1;
57 day = today_tm.tm_mday;
58 year = today_tm.tm_year + 1900;
60 for (i=0; i<(WC->num_cal); ++i) {
61 p = icalcomponent_get_first_property(WC->disp_cal[i],
62 ICAL_DTSTART_PROPERTY);
64 t = icalproperty_get_dtstart(p);
65 event_tt = icaltime_as_timet(t);
66 memcpy(&event_tm, localtime(&event_tt), sizeof(struct tm));
67 if ((event_tm.tm_year == today_tm.tm_year)
68 && (event_tm.tm_mon == today_tm.tm_mon)
69 && (event_tm.tm_mday == today_tm.tm_mday)) {
71 if (t.is_date) all_day_event = 1;
72 else all_day_event = 0;
74 p = icalcomponent_get_first_property(
76 ICAL_SUMMARY_PROPERTY);
80 wprintf("<TABLE border=0 cellpadding=2><TR>"
81 "<TD BGCOLOR=\"#CCCCDD\">"
85 wprintf("<FONT SIZE=-1>"
86 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=%s&year=%s&month=%s&day=%s\">",
94 icalproperty_get_comment(p));
95 wprintf("</A></FONT><BR>\n");
98 wprintf("</TD></TR></TABLE>");
112 void calendar_month_view(int year, int month, int day) {
113 struct tm starting_tm;
117 time_t previous_month;
120 /* Determine what day to start.
121 * First, back up to the 1st of the month...
123 memset(&starting_tm, 0, sizeof(struct tm));
124 starting_tm.tm_year = year - 1900;
125 starting_tm.tm_mon = month - 1;
126 starting_tm.tm_mday = day;
127 thetime = mktime(&starting_tm);
129 memcpy(&tm, &starting_tm, sizeof(struct tm));
130 while (tm.tm_mday != 1) {
131 thetime = thetime - (time_t)86400; /* go back 24 hours */
132 memcpy(&tm, localtime(&thetime), sizeof(struct tm));
135 /* Determine previous and next months ... for links */
136 previous_month = thetime - (time_t)864000L; /* back 10 days */
137 next_month = thetime + (time_t)(31L * 86400L); /* ahead 31 days */
139 /* Now back up until we're on a Sunday */
140 memcpy(&tm, localtime(&thetime), sizeof(struct tm));
141 while (tm.tm_wday != 0) {
142 thetime = thetime - (time_t)86400; /* go back 24 hours */
143 memcpy(&tm, localtime(&thetime), sizeof(struct tm));
146 /* Outer table (to get the background color) */
147 wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
148 "bgcolor=#204B78><TR><TD>\n");
150 wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0>"
151 "<TR><TD align=left><font color=#FFFFFF>"
152 " <A HREF=\"/display_edit_event?msgnum=0"
153 "&year=%d&month=%d&day=%d\">"
154 "Add new calendar event</A>"
159 wprintf("<TD ALIGN=CENTER><FONT SIZE=+1>");
161 memcpy(&tm, localtime(&previous_month), sizeof(struct tm));
162 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
163 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
164 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>\n");
166 wprintf(" "
167 "<FONT COLOR=\"#FFFFFF\">"
170 " ", months[month-1], year);
172 memcpy(&tm, localtime(&next_month), sizeof(struct tm));
173 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
174 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
175 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\" BORDER=0></A>\n");
177 wprintf("</FONT></TD><TD align=right><font color=#FFFFFF size=-2>"
178 "Click on any date for day view "
179 "</FONT></TD></TR></TABLE>\n");
181 /* Inner table (the real one) */
182 wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
183 "bgcolor=#204B78><TR>");
184 for (i=0; i<7; ++i) {
185 wprintf("<TD ALIGN=CENTER WIDTH=14%%>"
186 "<FONT COLOR=\"#FFFFFF\">%s</FONT></TH>", days[i]);
191 for (i = 0; i < 35; ++i) {
192 memcpy(&tm, localtime(&thetime), sizeof(struct tm));
194 /* Before displaying Sunday, start a new row */
199 wprintf("<TD BGCOLOR=\"#%s\" WIDTH=14%% HEIGHT=60 VALIGN=TOP><B>",
200 ((tm.tm_mon != month-1) ? "DDDDDD" :
201 ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
204 if ((i==0) || (tm.tm_mday == 1)) {
205 wprintf("%s ", months[tm.tm_mon]);
207 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
214 /* put the data here, stupid */
215 calendar_month_view_display_events(thetime);
219 /* After displaying Saturday, end the row */
224 thetime += (time_t)86400; /* ahead 24 hours */
227 wprintf("</TABLE>" /* end of inner table */
228 "</TD></TR></TABLE>" /* end of outer table */
233 void calendar_week_view(int year, int month, int day) {
234 wprintf("<CENTER><I>week view FIXME</I></CENTER><BR>\n");
239 * Display events for a particular hour of a particular day.
240 * (Specify hour < 0 to show "all day" events)
242 void calendar_day_view_display_events(int year, int month,
246 struct icaltimetype t;
249 int all_day_event = 0;
251 if (WC->num_cal == 0) {
252 wprintf("<BR><BR><BR>\n");
256 for (i=0; i<(WC->num_cal); ++i) {
257 p = icalcomponent_get_first_property(WC->disp_cal[i],
258 ICAL_DTSTART_PROPERTY);
260 t = icalproperty_get_dtstart(p);
261 event_tt = icaltime_as_timet(t);
262 event_tm = localtime(&event_tt);
263 if ((event_tm->tm_year == (year-1900))
264 && (event_tm->tm_mon == (month-1))
265 && (event_tm->tm_mday == day)
266 && ( ((event_tm->tm_hour == hour)&&(!t.is_date)) || ((hour<0)&&(t.is_date)) )
269 if (t.is_date) all_day_event = 1;
271 p = icalcomponent_get_first_property(
273 ICAL_SUMMARY_PROPERTY);
277 wprintf("<TABLE border=1 cellpadding=2><TR>"
278 "<TD BGCOLOR=\"#CCCCCC\">"
282 wprintf("<FONT SIZE=-1>"
283 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
288 icalproperty_get_comment(p));
289 wprintf("</A></FONT><BR>\n");
292 wprintf("</TD></TR></TABLE>");
305 void calendar_day_view(int year, int month, int day) {
307 struct icaltimetype today, yesterday, tomorrow;
310 /* Figure out the dates for "yesterday" and "tomorrow" links */
312 memset(&today, 0, sizeof(struct icaltimetype));
318 memcpy(&yesterday, &today, sizeof(struct icaltimetype));
320 yesterday = icaltime_normalize(yesterday);
322 memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
324 tomorrow = icaltime_normalize(tomorrow);
327 /* Outer table (to get the background color) */
328 wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
329 "bgcolor=#204B78><TR><TD>\n");
331 /* Inner table (the real one) */
332 wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
333 "bgcolor=#204B78><TR>\n");
335 /* Innermost table (contains hours etc.) */
336 wprintf("<TD WIDTH=80%%>"
337 "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
338 "bgcolor=#204B78>\n");
340 /* Display events before 8:00 (hour=-1 is all-day events) */
342 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
343 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
344 for (hour = (-1); hour <= 7; ++hour) {
345 calendar_day_view_display_events(year, month, day, hour);
347 wprintf("</TD></TR>\n");
349 /* Now the middle of the day... */
350 for (hour = 8; hour <= 17; ++hour) { /* could do HEIGHT=xx */
351 wprintf("<TR HEIGHT=30><TD BGCOLOR=\"#CCCCDD\" ALIGN=MIDDLE "
352 "VALIGN=MIDDLE WIDTH=10%%>");
353 wprintf("<A HREF=\"/display_edit_event?msgnum=0"
354 "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
355 year, month, day, hour
357 wprintf("%d:00%s</A> ",
358 (hour <= 12 ? hour : hour-12),
359 (hour < 12 ? "am" : "pm")
361 wprintf("</TD><TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
363 /* put the data here, stupid */
364 calendar_day_view_display_events(year, month, day, hour);
366 wprintf("</TD></TR>\n");
369 /* Display events after 5:00... */
371 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
372 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
373 for (hour = 18; hour <= 23; ++hour) {
374 calendar_day_view_display_events(year, month, day, hour);
376 wprintf("</TD></TR>\n");
379 wprintf("</TABLE>" /* end of innermost table */
383 wprintf("<TD WIDTH=20%% VALIGN=top>"); /* begin stuff-on-the-right */
386 /* Begin todays-date-with-left-and-right-arrows */
387 wprintf("<TABLE BORDER=0 WIDTH=100%% "
388 "CELLSPACING=0 CELLPADDING=0 BGCOLOR=\"#FFFFFF\">\n");
392 wprintf("<TD ALIGN=CENTER>");
393 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
394 yesterday.year, yesterday.month, yesterday.day);
395 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>");
399 wprintf("<TD ALIGN=CENTER>");
400 wprintf("<FONT SIZE=+2>%s</FONT><BR>"
401 "<FONT SIZE=+3>%d</FONT><BR>"
402 "<FONT SIZE=+2>%d</FONT><BR>",
403 months[month-1], day, year);
407 wprintf("<TD ALIGN=CENTER>");
408 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
409 tomorrow.year, tomorrow.month, tomorrow.day);
410 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\""
414 wprintf("</TR></TABLE>\n");
415 /* End todays-date-with-left-and-right-arrows */
417 wprintf("<BR><BR><CENTER><font color=#FFFFFF>"
418 " <A HREF=\"/display_edit_event?msgnum=0"
419 "&year=%d&month=%d&day=%d\">"
420 "Add new calendar event</A>"
425 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">"
426 "Back to month view</A>\n", year, month);
428 wprintf("</FONT></CENTER>\n");
430 wprintf("</TD>"); /* end stuff-on-the-right */
434 wprintf("</TR></TABLE>" /* end of inner table */
435 "</TD></TR></TABLE>" /* end of outer table */
443 * Display today's events.
445 void calendar_summary_view(void) {
448 struct icaltimetype t;
453 int all_day_event = 0;
454 char timestring[SIZ];
456 if (WC->num_cal == 0) {
461 memcpy(&today_tm, localtime(&now), sizeof(struct tm));
463 for (i=0; i<(WC->num_cal); ++i) {
464 p = icalcomponent_get_first_property(WC->disp_cal[i],
465 ICAL_DTSTART_PROPERTY);
467 t = icalproperty_get_dtstart(p);
468 event_tt = icaltime_as_timet(t);
469 fmt_time(timestring, event_tt);
470 memcpy(&event_tm, localtime(&event_tt), sizeof(struct tm));
471 if ( (event_tm.tm_year == today_tm.tm_year)
472 && (event_tm.tm_mon == today_tm.tm_mon)
473 && (event_tm.tm_mday == today_tm.tm_mday)
476 if (t.is_date) all_day_event = 1;
478 p = icalcomponent_get_first_property(
480 ICAL_SUMMARY_PROPERTY);
483 icalproperty_get_comment(p));
484 wprintf(" (%s)<BR>\n", timestring);
489 free_calendar_buffer();
494 void free_calendar_buffer(void) {
496 if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
497 icalcomponent_free(WC->disp_cal[i]);
502 free(WC->cal_msgnum);
503 WC->cal_msgnum = NULL;
509 void do_calendar_view(void) {
512 int year, month, day;
515 /* In case no date was specified, go with today */
517 memcpy(&tm, localtime(&now), sizeof(struct tm));
518 year = tm.tm_year + 1900;
519 month = tm.tm_mon + 1;
522 /* Now see if a date was specified */
523 if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
524 if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
525 if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
527 /* How would you like that cooked? */
528 if (strlen(bstr("calview")) > 0) {
529 strcpy(calview, bstr("calview"));
532 strcpy(calview, "month");
535 /* Display the selected view */
536 if (!strcasecmp(calview, "day")) {
537 calendar_day_view(year, month, day);
539 else if (!strcasecmp(calview, "week")) {
540 calendar_week_view(year, month, day);
543 calendar_month_view(year, month, day);
546 /* Free the calendar stuff */
547 free_calendar_buffer();
551 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */