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);
67 if (t.is_date) all_day_event = 1;
68 else all_day_event = 0;
71 memcpy(&event_tm, gmtime(&event_tt), sizeof(struct tm));
74 memcpy(&event_tm, localtime(&event_tt), sizeof(struct tm));
77 lprintf(9, "Event: %04d/%02d/%02d, Now: %04d/%02d/%02d\n",
86 if ((event_tm.tm_year == today_tm.tm_year)
87 && (event_tm.tm_mon == today_tm.tm_mon)
88 && (event_tm.tm_mday == today_tm.tm_mday)) {
90 p = icalcomponent_get_first_property(
92 ICAL_SUMMARY_PROPERTY);
96 wprintf("<TABLE border=0 cellpadding=2><TR>"
97 "<TD BGCOLOR=\"#CCCCDD\">"
101 wprintf("<FONT SIZE=-1>"
102 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=%s&year=%s&month=%s&day=%s\">",
110 icalproperty_get_comment(p));
111 wprintf("</A></FONT><BR>\n");
114 wprintf("</TD></TR></TABLE>");
128 void calendar_month_view(int year, int month, int day) {
129 struct tm starting_tm;
133 time_t previous_month;
136 /* Determine what day to start.
137 * First, back up to the 1st of the month...
139 memset(&starting_tm, 0, sizeof(struct tm));
140 starting_tm.tm_year = year - 1900;
141 starting_tm.tm_mon = month - 1;
142 starting_tm.tm_mday = day;
143 thetime = mktime(&starting_tm);
145 memcpy(&tm, &starting_tm, sizeof(struct tm));
146 while (tm.tm_mday != 1) {
147 thetime = thetime - (time_t)86400; /* go back 24 hours */
148 memcpy(&tm, localtime(&thetime), sizeof(struct tm));
151 /* Determine previous and next months ... for links */
152 previous_month = thetime - (time_t)864000L; /* back 10 days */
153 next_month = thetime + (time_t)(31L * 86400L); /* ahead 31 days */
155 /* Now back up until we're on a Sunday */
156 memcpy(&tm, localtime(&thetime), sizeof(struct tm));
157 while (tm.tm_wday != 0) {
158 thetime = thetime - (time_t)86400; /* go back 24 hours */
159 memcpy(&tm, localtime(&thetime), sizeof(struct tm));
162 /* Outer table (to get the background color) */
163 wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
164 "bgcolor=#204B78><TR><TD>\n");
166 wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0>"
167 "<TR><TD align=left><font color=#FFFFFF>"
168 " <A HREF=\"/display_edit_event?msgnum=0"
169 "&year=%d&month=%d&day=%d\">"
170 "Add new calendar event</A>"
175 wprintf("<TD ALIGN=CENTER><FONT SIZE=+1>");
177 memcpy(&tm, localtime(&previous_month), sizeof(struct tm));
178 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
179 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
180 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>\n");
182 wprintf(" "
183 "<FONT COLOR=\"#FFFFFF\">"
186 " ", months[month-1], year);
188 memcpy(&tm, localtime(&next_month), sizeof(struct tm));
189 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
190 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
191 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\" BORDER=0></A>\n");
193 wprintf("</FONT></TD><TD align=right><font color=#FFFFFF size=-2>"
194 "Click on any date for day view "
195 "</FONT></TD></TR></TABLE>\n");
197 /* Inner table (the real one) */
198 wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
199 "bgcolor=#204B78><TR>");
200 for (i=0; i<7; ++i) {
201 wprintf("<TD ALIGN=CENTER WIDTH=14%%>"
202 "<FONT COLOR=\"#FFFFFF\">%s</FONT></TH>", days[i]);
207 for (i = 0; i < 35; ++i) {
208 memcpy(&tm, localtime(&thetime), sizeof(struct tm));
210 /* Before displaying Sunday, start a new row */
215 wprintf("<TD BGCOLOR=\"#%s\" WIDTH=14%% HEIGHT=60 VALIGN=TOP><B>",
216 ((tm.tm_mon != month-1) ? "DDDDDD" :
217 ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
220 if ((i==0) || (tm.tm_mday == 1)) {
221 wprintf("%s ", months[tm.tm_mon]);
223 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
230 /* put the data here, stupid */
231 calendar_month_view_display_events(thetime);
235 /* After displaying Saturday, end the row */
240 thetime += (time_t)86400; /* ahead 24 hours */
243 wprintf("</TABLE>" /* end of inner table */
244 "</TD></TR></TABLE>" /* end of outer table */
249 void calendar_week_view(int year, int month, int day) {
250 wprintf("<CENTER><I>week view FIXME</I></CENTER><BR>\n");
255 * Display events for a particular hour of a particular day.
256 * (Specify hour < 0 to show "all day" events)
258 void calendar_day_view_display_events(int year, int month,
262 struct icaltimetype t;
265 int all_day_event = 0;
267 if (WC->num_cal == 0) {
268 wprintf("<BR><BR><BR>\n");
272 for (i=0; i<(WC->num_cal); ++i) {
273 p = icalcomponent_get_first_property(WC->disp_cal[i],
274 ICAL_DTSTART_PROPERTY);
276 t = icalproperty_get_dtstart(p);
277 event_tt = icaltime_as_timet(t);
278 if (t.is_date) all_day_event = 1;
281 event_tm = gmtime(&event_tt);
284 event_tm = localtime(&event_tt);
287 if ((event_tm->tm_year == (year-1900))
288 && (event_tm->tm_mon == (month-1))
289 && (event_tm->tm_mday == day)
290 && ( ((event_tm->tm_hour == hour)&&(!t.is_date)) || ((hour<0)&&(t.is_date)) )
294 p = icalcomponent_get_first_property(
296 ICAL_SUMMARY_PROPERTY);
300 wprintf("<TABLE border=1 cellpadding=2><TR>"
301 "<TD BGCOLOR=\"#CCCCCC\">"
305 wprintf("<FONT SIZE=-1>"
306 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
311 icalproperty_get_comment(p));
312 wprintf("</A></FONT><BR>\n");
315 wprintf("</TD></TR></TABLE>");
328 void calendar_day_view(int year, int month, int day) {
330 struct icaltimetype today, yesterday, tomorrow;
333 /* Figure out the dates for "yesterday" and "tomorrow" links */
335 memset(&today, 0, sizeof(struct icaltimetype));
341 memcpy(&yesterday, &today, sizeof(struct icaltimetype));
343 yesterday = icaltime_normalize(yesterday);
345 memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
347 tomorrow = icaltime_normalize(tomorrow);
350 /* Outer table (to get the background color) */
351 wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
352 "bgcolor=#204B78><TR><TD>\n");
354 /* Inner table (the real one) */
355 wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
356 "bgcolor=#204B78><TR>\n");
358 /* Innermost table (contains hours etc.) */
359 wprintf("<TD WIDTH=80%%>"
360 "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
361 "bgcolor=#204B78>\n");
363 /* Display events before 8:00 (hour=-1 is all-day events) */
365 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
366 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
367 for (hour = (-1); hour <= 7; ++hour) {
368 calendar_day_view_display_events(year, month, day, hour);
370 wprintf("</TD></TR>\n");
372 /* Now the middle of the day... */
373 for (hour = 8; hour <= 17; ++hour) { /* could do HEIGHT=xx */
374 wprintf("<TR HEIGHT=30><TD BGCOLOR=\"#CCCCDD\" ALIGN=MIDDLE "
375 "VALIGN=MIDDLE WIDTH=10%%>");
376 wprintf("<A HREF=\"/display_edit_event?msgnum=0"
377 "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
378 year, month, day, hour
380 wprintf("%d:00%s</A> ",
381 (hour <= 12 ? hour : hour-12),
382 (hour < 12 ? "am" : "pm")
384 wprintf("</TD><TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
386 /* put the data here, stupid */
387 calendar_day_view_display_events(year, month, day, hour);
389 wprintf("</TD></TR>\n");
392 /* Display events after 5:00... */
394 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
395 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
396 for (hour = 18; hour <= 23; ++hour) {
397 calendar_day_view_display_events(year, month, day, hour);
399 wprintf("</TD></TR>\n");
402 wprintf("</TABLE>" /* end of innermost table */
406 wprintf("<TD WIDTH=20%% VALIGN=top>"); /* begin stuff-on-the-right */
409 /* Begin todays-date-with-left-and-right-arrows */
410 wprintf("<TABLE BORDER=0 WIDTH=100%% "
411 "CELLSPACING=0 CELLPADDING=0 BGCOLOR=\"#FFFFFF\">\n");
415 wprintf("<TD ALIGN=CENTER>");
416 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
417 yesterday.year, yesterday.month, yesterday.day);
418 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>");
422 wprintf("<TD ALIGN=CENTER>");
423 wprintf("<FONT SIZE=+2>%s</FONT><BR>"
424 "<FONT SIZE=+3>%d</FONT><BR>"
425 "<FONT SIZE=+2>%d</FONT><BR>",
426 months[month-1], day, year);
430 wprintf("<TD ALIGN=CENTER>");
431 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
432 tomorrow.year, tomorrow.month, tomorrow.day);
433 wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\""
437 wprintf("</TR></TABLE>\n");
438 /* End todays-date-with-left-and-right-arrows */
440 wprintf("<BR><BR><CENTER><font color=#FFFFFF>"
441 " <A HREF=\"/display_edit_event?msgnum=0"
442 "&year=%d&month=%d&day=%d\">"
443 "Add new calendar event</A>"
448 wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">"
449 "Back to month view</A>\n", year, month);
451 wprintf("</FONT></CENTER>\n");
453 wprintf("</TD>"); /* end stuff-on-the-right */
457 wprintf("</TR></TABLE>" /* end of inner table */
458 "</TD></TR></TABLE>" /* end of outer table */
466 * Display today's events.
468 void calendar_summary_view(void) {
471 struct icaltimetype t;
476 int all_day_event = 0;
477 char timestring[SIZ];
479 if (WC->num_cal == 0) {
484 memcpy(&today_tm, localtime(&now), sizeof(struct tm));
486 for (i=0; i<(WC->num_cal); ++i) {
487 p = icalcomponent_get_first_property(WC->disp_cal[i],
488 ICAL_DTSTART_PROPERTY);
490 t = icalproperty_get_dtstart(p);
491 event_tt = icaltime_as_timet(t);
492 if (t.is_date) all_day_event = 1;
493 fmt_time(timestring, event_tt);
496 memcpy(&event_tm, gmtime(&event_tt), sizeof(struct tm));
499 memcpy(&event_tm, localtime(&event_tt), sizeof(struct tm));
502 if ( (event_tm.tm_year == today_tm.tm_year)
503 && (event_tm.tm_mon == today_tm.tm_mon)
504 && (event_tm.tm_mday == today_tm.tm_mday)
508 p = icalcomponent_get_first_property(
510 ICAL_SUMMARY_PROPERTY);
513 icalproperty_get_comment(p));
514 wprintf(" (%s)<BR>\n", timestring);
519 free_calendar_buffer();
524 void free_calendar_buffer(void) {
526 if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
527 icalcomponent_free(WC->disp_cal[i]);
532 free(WC->cal_msgnum);
533 WC->cal_msgnum = NULL;
539 void do_calendar_view(void) {
542 int year, month, day;
545 /* In case no date was specified, go with today */
547 memcpy(&tm, localtime(&now), sizeof(struct tm));
548 year = tm.tm_year + 1900;
549 month = tm.tm_mon + 1;
552 /* Now see if a date was specified */
553 if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
554 if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
555 if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
557 /* How would you like that cooked? */
558 if (strlen(bstr("calview")) > 0) {
559 strcpy(calview, bstr("calview"));
562 strcpy(calview, "month");
565 /* Display the selected view */
566 if (!strcasecmp(calview, "day")) {
567 calendar_day_view(year, month, day);
569 else if (!strcasecmp(calview, "week")) {
570 calendar_week_view(year, month, day);
573 calendar_month_view(year, month, day);
576 /* Free the calendar stuff */
577 free_calendar_buffer();
581 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */