5 * \defgroup CalHtmlHandles Handles the HTML display of calendar items.
10 #include "webserver.h"
12 #ifndef WEBCIT_WITH_CALENDAR_SERVICE
14 /**\brief stub for non-libical builds */
15 void do_calendar_view(void) {
16 wprintf("<CENTER><I>");
17 wprintf(_("The calendar view is not available."));
18 wprintf("</I></CENTER><br />\n");
21 /**\brief stub for non-libical builds */
22 void do_tasks_view(void) {
23 wprintf("<CENTER><I>");
24 wprintf(_("The tasks view is not available."));
25 wprintf("</I></CENTER><br />\n");
28 #else /* WEBCIT_WITH_CALENDAR_SERVICE */
30 /****************************************************************************/
33 * \brief Display a whole month view of a calendar
34 * \param thetime the month we want to see
36 void calendar_month_view_display_events(time_t thetime) {
42 struct icaltimetype t;
44 int all_day_event = 0;
46 if (WC->num_cal == 0) {
47 wprintf("<br /><br /><br />\n");
51 localtime_r(&thetime, &today_tm);
52 month = today_tm.tm_mon + 1;
53 day = today_tm.tm_mday;
54 year = today_tm.tm_year + 1900;
56 for (i=0; i<(WC->num_cal); ++i) {
57 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
58 ICAL_DTSTART_PROPERTY);
60 t = icalproperty_get_dtstart(p);
61 event_tt = icaltime_as_timet(t);
63 if (t.is_date) all_day_event = 1;
64 else all_day_event = 0;
67 gmtime_r(&event_tt, &event_tm);
70 localtime_r(&event_tt, &event_tm);
73 if ((event_tm.tm_year == today_tm.tm_year)
74 && (event_tm.tm_mon == today_tm.tm_mon)
75 && (event_tm.tm_mday == today_tm.tm_mday)) {
77 p = icalcomponent_get_first_property(
79 ICAL_SUMMARY_PROPERTY);
83 wprintf("<TABLE border=0 cellpadding=2><TR>"
84 "<TD BGCOLOR=\"#CCCCDD\">"
88 wprintf("<FONT SIZE=-1>"
89 "<a href=\"display_edit_event?msgnum=%ld&calview=%s&year=%s&month=%s&day=%s\">",
90 WC->disp_cal[i].cal_msgnum,
97 icalproperty_get_comment(p));
98 wprintf("</A></FONT><br />\n");
101 wprintf("</TD></TR></TABLE>");
115 * \brief view one day
116 * \param year the year
117 * \param month the month
118 * \param day the actual day we want to see
120 void calendar_month_view(int year, int month, int day) {
121 struct tm starting_tm;
125 time_t previous_month;
128 /** Determine what day to start.
129 * First, back up to the 1st of the month...
131 memset(&starting_tm, 0, sizeof(struct tm));
132 starting_tm.tm_year = year - 1900;
133 starting_tm.tm_mon = month - 1;
134 starting_tm.tm_mday = day;
135 thetime = mktime(&starting_tm);
137 memcpy(&tm, &starting_tm, sizeof(struct tm));
138 while (tm.tm_mday != 1) {
139 thetime = thetime - (time_t)86400; /* go back 24 hours */
140 localtime_r(&thetime, &tm);
143 /** Determine previous and next months ... for links */
144 previous_month = thetime - (time_t)864000L; /* back 10 days */
145 next_month = thetime + (time_t)(31L * 86400L); /* ahead 31 days */
147 /** Now back up until we're on a Sunday */
148 localtime_r(&thetime, &tm);
149 while (tm.tm_wday != 0) {
150 thetime = thetime - (time_t)86400; /* go back 24 hours */
151 localtime_r(&thetime, &tm);
154 /** Outer table (to get the background color) */
155 wprintf("<div class=\"fix_scrollbar_bug\">"
156 "<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
157 "bgcolor=#204B78><TR><TD>\n");
159 wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0><tr>\n");
161 wprintf("<TD ALIGN=CENTER>");
163 localtime_r(&previous_month, &tm);
164 wprintf("<a href=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
165 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
166 wprintf("<IMG ALIGN=MIDDLE src=\"static/prevdate_32x.gif\" BORDER=0></A>\n");
168 wprintf(" "
169 "<FONT SIZE=+1 COLOR=\"#FFFFFF\">"
172 " ", months[month-1], year);
174 localtime_r(&next_month, &tm);
175 wprintf("<a href=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
176 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
177 wprintf("<IMG ALIGN=MIDDLE src=\"static/nextdate_32x.gif\" BORDER=0></A>\n");
179 wprintf("</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>", wdays[i]);
190 /** Now do 35 days */
191 for (i = 0; i < 35; ++i) {
192 localtime_r(&thetime, &tm);
194 /* Before displaying Sunday, start a new row */
199 wprintf("<TD BGCOLOR=\"#%s\" WIDTH=14%% HEIGHT=60 align=left 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 * \brief view one week
234 * this should view just one week, but it's not here yet.
235 * \todo ny implemented
236 * \param year the year
237 * \param month the month
238 * \param day the day which we want to see the week around
240 void calendar_week_view(int year, int month, int day) {
241 wprintf("<CENTER><I>week view FIXME</I></CENTER><br />\n");
246 * \brief display one day
247 * Display events for a particular hour of a particular day.
248 * (Specify hour < 0 to show "all day" events)
249 * \param year the year
250 * \param month the month
252 * \param hour the hour we want to start displaying?????
254 void calendar_day_view_display_events(int year, int month,
258 struct icaltimetype t;
261 int all_day_event = 0;
263 if (WC->num_cal == 0) {
264 // \todo FIXME wprintf("<br /><br /><br />\n");
268 for (i=0; i<(WC->num_cal); ++i) {
269 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
270 ICAL_DTSTART_PROPERTY);
272 t = icalproperty_get_dtstart(p);
273 event_tt = icaltime_as_timet(t);
282 event_tm = gmtime(&event_tt);
285 event_tm = localtime(&event_tt);
288 if ((event_tm->tm_year == (year-1900))
289 && (event_tm->tm_mon == (month-1))
290 && (event_tm->tm_mday == day)
291 && ( ((event_tm->tm_hour == hour)&&(!t.is_date)) || ((hour<0)&&(t.is_date)) )
295 p = icalcomponent_get_first_property(
297 ICAL_SUMMARY_PROPERTY);
301 wprintf("<TABLE border=1 cellpadding=2><TR>"
302 "<TD BGCOLOR=\"#CCCCCC\">"
306 wprintf("<FONT SIZE=-1>"
307 "<a href=\"display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
308 WC->disp_cal[i].cal_msgnum,
312 icalproperty_get_comment(p));
313 wprintf("</A></FONT><br />\n");
316 wprintf("</TD></TR></TABLE>");
329 * \brief view one day
330 * \param year the year
331 * \param month the month
332 * \param day the day we want to display
334 void calendar_day_view(int year, int month, int day) {
336 struct icaltimetype today, yesterday, tomorrow;
337 char calhourformat[16];
340 char daystart_str[16], dayend_str[16];
342 get_preference("calhourformat", calhourformat, sizeof calhourformat);
343 get_preference("daystart", daystart_str, sizeof daystart_str);
344 if (strlen(daystart_str) > 0) daystart = atoi(daystart_str);
345 get_preference("dayend", dayend_str, sizeof dayend_str);
346 if (strlen(dayend_str) > 0) dayend = atoi(dayend_str);
349 /** Figure out the dates for "yesterday" and "tomorrow" links */
351 memset(&today, 0, sizeof(struct icaltimetype));
357 memcpy(&yesterday, &today, sizeof(struct icaltimetype));
359 yesterday = icaltime_normalize(yesterday);
361 memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
363 tomorrow = icaltime_normalize(tomorrow);
366 /** Outer table (to get the background color) */
367 wprintf("<div class=\"fix_scrollbar_bug\">"
368 "<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
369 "bgcolor=#204B78><TR><TD>\n");
371 /** Inner table (the real one) */
372 wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
373 "bgcolor=#204B78><TR>\n");
375 /** Innermost table (contains hours etc.) */
376 wprintf("<TD WIDTH=80%%>"
377 "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
378 "bgcolor=#204B78>\n");
380 /** Display events before 8:00 (hour=-1 is all-day events) */
382 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
383 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
384 for (hour = (-1); hour <= (daystart-1); ++hour) {
385 calendar_day_view_display_events(year, month, day, hour);
387 wprintf("</TD></TR>\n");
389 /** Now the middle of the day... */
390 for (hour = daystart; hour <= dayend; ++hour) { /* could do HEIGHT=xx */
391 wprintf("<TR HEIGHT=30><TD BGCOLOR=\"#CCCCDD\" ALIGN=MIDDLE "
392 "VALIGN=MIDDLE WIDTH=10%%>");
393 wprintf("<a href=\"display_edit_event?msgnum=0"
394 "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
395 year, month, day, hour
398 if (!strcasecmp(calhourformat, "24")) {
399 wprintf("%2d:00</A> ", hour);
402 wprintf("%d:00%s</A> ",
403 (hour <= 12 ? hour : hour-12),
404 (hour < 12 ? "am" : "pm")
408 wprintf("</TD><TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
410 /* put the data here, stupid */
411 calendar_day_view_display_events(year, month, day, hour);
413 wprintf("</TD></TR>\n");
416 /** Display events after 5:00... */
418 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
419 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
420 for (hour = (dayend+1); hour <= 23; ++hour) {
421 calendar_day_view_display_events(year, month, day, hour);
423 wprintf("</TD></TR>\n");
426 wprintf("</TABLE>" /* end of innermost table */
430 wprintf("<TD WIDTH=20%% VALIGN=top>"); /* begin stuff-on-the-right */
433 /** Begin todays-date-with-left-and-right-arrows */
434 wprintf("<TABLE BORDER=0 WIDTH=100%% "
435 "CELLSPACING=0 CELLPADDING=0 BGCOLOR=\"#FFFFFF\">\n");
439 wprintf("<TD ALIGN=CENTER>");
440 wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
441 yesterday.year, yesterday.month, yesterday.day);
442 wprintf("<IMG ALIGN=MIDDLE src=\"static/prevdate_32x.gif\" BORDER=0></A>");
446 wprintf("<TD ALIGN=CENTER>");
447 wprintf("<FONT SIZE=+2>%s</FONT><br />"
448 "<FONT SIZE=+3>%d</FONT><br />"
449 "<FONT SIZE=+2>%d</FONT><br />",
450 months[month-1], day, year);
454 wprintf("<TD ALIGN=CENTER>");
455 wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
456 tomorrow.year, tomorrow.month, tomorrow.day);
457 wprintf("<IMG ALIGN=MIDDLE src=\"static/nextdate_32x.gif\""
461 wprintf("</TR></TABLE>\n");
462 /** End todays-date-with-left-and-right-arrows */
464 /** \todo In the future we might want to put a month-o-matic here */
466 wprintf("</FONT></CENTER>\n");
468 wprintf("</TD>"); /** end stuff-on-the-right */
472 wprintf("</TR></TABLE>" /** end of inner table */
473 "</TD></TR></TABLE></div>" /** end of outer table */
481 * \brief Display today's events.
483 void calendar_summary_view(void) {
486 struct icaltimetype t;
491 int all_day_event = 0;
492 char timestring[SIZ];
494 if (WC->num_cal == 0) {
499 localtime_r(&now, &today_tm);
501 for (i=0; i<(WC->num_cal); ++i) {
502 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
503 ICAL_DTSTART_PROPERTY);
505 t = icalproperty_get_dtstart(p);
506 event_tt = icaltime_as_timet(t);
513 fmt_time(timestring, event_tt);
516 gmtime_r(&event_tt, &event_tm);
519 localtime_r(&event_tt, &event_tm);
522 if ( (event_tm.tm_year == today_tm.tm_year)
523 && (event_tm.tm_mon == today_tm.tm_mon)
524 && (event_tm.tm_mday == today_tm.tm_mday)
528 p = icalcomponent_get_first_property(
530 ICAL_SUMMARY_PROPERTY);
533 icalproperty_get_comment(p));
534 wprintf(" (%s)<br />\n", timestring);
539 free_calendar_buffer();
544 * \brief clean up ical memory
545 * \todo this could get troubel with future ical versions
547 void free_calendar_buffer(void) {
549 if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
550 icalcomponent_free(WC->disp_cal[i].cal);
560 * \brief do the whole calendar page
561 * view any part of the calender. decide which way, etc.
563 void do_calendar_view(void) {
566 int year, month, day;
569 /** In case no date was specified, go with today */
571 localtime_r(&now, &tm);
572 year = tm.tm_year + 1900;
573 month = tm.tm_mon + 1;
576 /** Now see if a date was specified */
577 if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
578 if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
579 if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
581 /** How would you like that cooked? */
582 if (strlen(bstr("calview")) > 0) {
583 strcpy(calview, bstr("calview"));
586 strcpy(calview, "month");
589 /** Display the selected view */
590 if (!strcasecmp(calview, "day")) {
591 calendar_day_view(year, month, day);
593 else if (!strcasecmp(calview, "week")) {
594 calendar_week_view(year, month, day);
597 calendar_month_view(year, month, day);
600 /** Free the calendar stuff */
601 free_calendar_buffer();
607 * \brief get task due date
608 * Helper function for do_tasks_view().
609 * \param vtodo a task to get the due date
610 * \return the date/time due.
612 time_t get_task_due_date(icalcomponent *vtodo) {
620 * If we're looking at a fully encapsulated VCALENDAR
621 * rather than a VTODO component, recurse into the data
622 * structure until we get a VTODO.
624 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
625 return get_task_due_date(
626 icalcomponent_get_first_component(
627 vtodo, ICAL_VTODO_COMPONENT
632 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
634 return(icaltime_as_timet(icalproperty_get_due(p)));
643 * \brief Compare the due dates of two tasks (this is for sorting)
644 * \param task1 first task to compare
645 * \param task2 second task to compare
647 int task_due_cmp(const void *task1, const void *task2) {
651 t1 = get_task_due_date(((struct disp_cal *)task1)->cal);
652 t2 = get_task_due_date(((struct disp_cal *)task2)->cal);
654 if (t1 < t2) return(-1);
655 if (t1 > t2) return(1);
663 * \brief do the whole task view stuff
665 void do_tasks_view(void) {
672 wprintf("<div class=\"fix_scrollbar_bug\">"
673 "<table border=0 cellspacing=0 width=100%% bgcolor=\"#FFFFFF\">\n<tr>\n"
675 wprintf(_("Name of task"));
676 wprintf("</TH><TH>");
677 wprintf(_("Date due"));
678 wprintf("</TH></TR>\n"
681 /** Sort them if necessary */
682 if (WC->num_cal > 1) {
685 sizeof(struct disp_cal),
690 if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
693 wprintf("<TR BGCOLOR=\"#%s\"><TD>",
694 (bg ? "DDDDDD" : "FFFFFF")
697 p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
698 ICAL_SUMMARY_PROPERTY);
699 wprintf("<a href=\"display_edit_task?msgnum=%ld&taskrm=",
700 WC->disp_cal[i].cal_msgnum );
701 urlescputs(WC->wc_roomname);
703 wprintf("<IMG ALIGN=MIDDLE "
704 "src=\"static/taskmanag_16x.gif\" BORDER=0> ");
706 escputs((char *)icalproperty_get_comment(p));
711 due = get_task_due_date(WC->disp_cal[i].cal);
712 fmt_date(buf, due, 0);
713 wprintf("<TD><FONT");
714 if (due < time(NULL)) {
715 wprintf(" COLOR=\"#FF0000\"");
717 wprintf(">%s</FONT></TD></TR>\n", buf);
720 wprintf("</table></div>\n");
723 free_calendar_buffer();
727 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */