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><CENTER><H3>");
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("</H3></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 "
184 for (i=0; i<7; ++i) {
185 wprintf("<TH><FONT COLOR=\"#FFFFFF\">%s</FONT></TH>", days[i]);
189 for (i = 0; i < 35; ++i) {
190 memcpy(&tm, localtime(&thetime), sizeof(struct tm));
192 /* Before displaying Sunday, start a new row */
197 wprintf("<TD BGCOLOR=\"#%s\" WIDTH=14%% HEIGHT=60 VALIGN=TOP><B>",
198 ((tm.tm_mon != month-1) ? "DDDDDD" :
199 ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
202 if ((i==0) || (tm.tm_mday == 1)) {
203 wprintf("%s ", months[tm.tm_mon]);
205 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
212 /* put the data here, stupid */
213 calendar_month_view_display_events(thetime);
217 /* After displaying Saturday, end the row */
222 thetime += (time_t)86400; /* ahead 24 hours */
225 wprintf("</TABLE>" /* end of inner table */
226 "</TD></TR></TABLE>" /* end of outer table */
231 void calendar_week_view(int year, int month, int day) {
232 wprintf("<CENTER><I>week view FIXME</I></CENTER><BR>\n");
237 * Display events for a particular hour of a particular day.
238 * (Specify hour < 0 to show "all day" events)
240 void calendar_day_view_display_events(int year, int month,
244 struct icaltimetype t;
247 int all_day_event = 0;
249 if (WC->num_cal == 0) {
250 wprintf("<BR><BR><BR>\n");
254 for (i=0; i<(WC->num_cal); ++i) {
255 p = icalcomponent_get_first_property(WC->disp_cal[i],
256 ICAL_DTSTART_PROPERTY);
258 t = icalproperty_get_dtstart(p);
259 event_tt = icaltime_as_timet(t);
260 event_tm = localtime(&event_tt);
261 if ((event_tm->tm_year == (year-1900))
262 && (event_tm->tm_mon == (month-1))
263 && (event_tm->tm_mday == day)
264 && ( ((event_tm->tm_hour == hour)&&(!t.is_date)) || ((hour<0)&&(t.is_date)) )
267 if (t.is_date) all_day_event = 1;
269 p = icalcomponent_get_first_property(
271 ICAL_SUMMARY_PROPERTY);
275 wprintf("<TABLE border=1 cellpadding=2><TR>"
276 "<TD BGCOLOR=\"#CCCCCC\">"
280 wprintf("<FONT SIZE=-1>"
281 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
286 icalproperty_get_comment(p));
287 wprintf("</A></FONT><BR>\n");
290 wprintf("</TD></TR></TABLE>");
303 void calendar_day_view(int year, int month, int day) {
305 struct icaltimetype today, yesterday, tomorrow;
308 /* Figure out the dates for "yesterday" and "tomorrow" links */
310 memset(&today, 0, sizeof(struct icaltimetype));
316 memcpy(&yesterday, &today, sizeof(struct icaltimetype));
318 yesterday = icaltime_normalize(yesterday);
320 memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
322 tomorrow = icaltime_normalize(tomorrow);
325 /* Outer table (to get the background color) */
326 wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
327 "bgcolor=#204B78><TR><TD>\n");
329 /* Inner table (the real one) */
330 wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
331 "bgcolor=#204B78><TR>\n");
333 /* Innermost table (contains hours etc.) */
334 wprintf("<TD WIDTH=80%%>"
335 "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
336 "bgcolor=#204B78>\n");
338 /* Display events before 8:00 (hour=-1 is all-day events) */
340 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
341 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
342 for (hour = (-1); hour <= 7; ++hour) {
343 calendar_day_view_display_events(year, month, day, hour);
345 wprintf("</TD></TR>\n");
347 /* Now the middle of the day... */
348 for (hour = 8; hour <= 17; ++hour) { /* could do HEIGHT=xx */
349 wprintf("<TR HEIGHT=30><TD BGCOLOR=\"#CCCCDD\" ALIGN=MIDDLE "
350 "VALIGN=MIDDLE WIDTH=10%%>");
351 wprintf("<A HREF=\"/display_edit_event?msgnum=0"
352 "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
353 year, month, day, hour
355 wprintf("%d:00%s</A> ",
356 (hour <= 12 ? hour : hour-12),
357 (hour < 12 ? "am" : "pm")
359 wprintf("</TD><TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
361 /* put the data here, stupid */
362 calendar_day_view_display_events(year, month, day, hour);
364 wprintf("</TD></TR>\n");
367 /* Display events after 5:00... */
369 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
370 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
371 for (hour = 18; hour <= 23; ++hour) {
372 calendar_day_view_display_events(year, month, day, hour);
374 wprintf("</TD></TR>\n");
377 wprintf("</TABLE>" /* end of innermost table */
381 wprintf("<TD WIDTH=20%% VALIGN=top>"); /* begin stuff-on-the-right */
384 /* Begin todays-date-with-left-and-right-arrows */
385 wprintf("<TABLE BORDER=0 WIDTH=100%% "
386 "CELLSPACING=0 CELLPADDING=0 BGCOLOR=\"#FFFFFF\">\n");
390 wprintf("<TD ALIGN=CENTER>");
391 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
392 yesterday.year, yesterday.month, yesterday.day);
393 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>");
397 wprintf("<TD ALIGN=CENTER>");
398 wprintf("<FONT SIZE=+2>%s</FONT><BR>"
399 "<FONT SIZE=+3>%d</FONT><BR>"
400 "<FONT SIZE=+2>%d</FONT><BR>",
401 months[month-1], day, year);
405 wprintf("<TD ALIGN=CENTER>");
406 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
407 tomorrow.year, tomorrow.month, tomorrow.day);
408 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\""
412 wprintf("</TR></TABLE>\n");
413 /* End todays-date-with-left-and-right-arrows */
415 wprintf("<BR><BR><CENTER><font color=#FFFFFF>"
416 " <A HREF=\"/display_edit_event?msgnum=0"
417 "&year=%d&month=%d&day=%d\">"
418 "Add new calendar event</A>"
423 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">"
424 "Back to month view</A>\n", year, month);
426 wprintf("</FONT></CENTER>\n");
428 wprintf("</TD>"); /* end stuff-on-the-right */
432 wprintf("</TR></TABLE>" /* end of inner table */
433 "</TD></TR></TABLE>" /* end of outer table */
441 * Display today's events.
443 void calendar_summary_view(void) {
446 struct icaltimetype t;
451 int all_day_event = 0;
452 char timestring[SIZ];
454 if (WC->num_cal == 0) {
459 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);
484 icalproperty_get_comment(p));
485 wprintf(" (%s)\n", timestring);
491 free_calendar_buffer();
496 void free_calendar_buffer(void) {
498 if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
499 icalcomponent_free(WC->disp_cal[i]);
504 free(WC->cal_msgnum);
505 WC->cal_msgnum = NULL;
511 void do_calendar_view(void) {
514 int year, month, day;
517 /* In case no date was specified, go with today */
519 memcpy(&tm, localtime(&now), sizeof(struct tm));
520 year = tm.tm_year + 1900;
521 month = tm.tm_mon + 1;
524 /* Now see if a date was specified */
525 if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
526 if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
527 if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
529 /* How would you like that cooked? */
530 if (strlen(bstr("calview")) > 0) {
531 strcpy(calview, bstr("calview"));
534 strcpy(calview, "month");
537 /* Display the selected view */
538 if (!strcasecmp(calview, "day")) {
539 calendar_day_view(year, month, day);
541 else if (!strcasecmp(calview, "week")) {
542 calendar_week_view(year, month, day);
545 calendar_month_view(year, month, day);
548 /* Free the calendar stuff */
549 free_calendar_buffer();
553 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */