2 * Handles the HTML display of calendar items.
4 * Copyright (c) 1996-2012 by the citadel.org team
6 * This program is open source software. You can redistribute it and/or
7 * modify it under the terms of the GNU General Public License, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include "webserver.h"
19 /* These define how high the hour rows are in the day view */
21 #define EXTRATIMELINE 22
23 void embeddable_mini_calendar(int year, int month)
25 struct tm starting_tm;
29 time_t previous_month;
31 time_t colheader_time;
32 struct tm colheader_tm;
33 char colheader_label[32];
38 snprintf(div_id, sizeof div_id, "mini_calendar_%d", rand() );
40 /* Determine what day to start. If an impossible value is found, start on Sunday.
42 get_pref_long("weekstart", &weekstart, 17);
43 if (weekstart > 6) weekstart = 0;
46 * Now back up to the 1st of the month...
48 memset(&starting_tm, 0, sizeof(struct tm));
50 starting_tm.tm_year = year - 1900;
51 starting_tm.tm_mon = month - 1;
52 starting_tm.tm_mday = 1;
53 thetime = mktime(&starting_tm);
55 memcpy(&tm, &starting_tm, sizeof(struct tm));
56 while (tm.tm_mday != 1) {
57 thetime = thetime - (time_t)86400; /* go back 24 hours */
58 localtime_r(&thetime, &tm);
61 /* Determine previous and next months ... for links */
62 previous_month = thetime - (time_t)864000L; /* back 10 days */
63 next_month = thetime + (time_t)(31L * 86400L); /* ahead 31 days */
65 /* Now back up until we're on the user's preferred start day */
66 localtime_r(&thetime, &tm);
67 while (tm.tm_wday != weekstart) {
68 thetime = thetime - (time_t)86400; /* go back 24 hours */
69 localtime_r(&thetime, &tm);
72 wc_printf("<div class=\"mini_calendar\" id=\"%s\">\n", div_id);
74 /* Previous month link */
75 localtime_r(&previous_month, &tm);
76 wc_printf("<a href=\"javascript:minical_change_month(%d,%d);\">«</a>",
77 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
79 wc_strftime(colheader_label, sizeof colheader_label, "%B", &starting_tm);
80 wc_printf(" "
81 "<span class=\"mini_calendar_month_label\">"
84 " ", colheader_label, year);
87 localtime_r(&next_month, &tm);
88 wc_printf("<a href=\"javascript:minical_change_month(%d,%d);\">»</a>",
89 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
91 wc_printf("<table border=\"0\" cellpadding=\"1\" cellspacing=\"1\" class=\"mini_calendar_days\">"
93 colheader_time = thetime;
95 colheader_time = thetime + (i * 86400) ;
96 localtime_r(&colheader_time, &colheader_tm);
97 wc_strftime(colheader_label, sizeof colheader_label, "%A", &colheader_tm);
98 wc_printf("<th>%c</th>", colheader_label[0]);
101 wc_printf("</tr>\n");
104 /* Now do 35 or 42 days */
105 for (i = 0; i < 42; ++i) {
106 localtime_r(&thetime, &tm);
110 /* Before displaying Sunday, start a new row */
115 if (tm.tm_mon == month-1) {
116 snprintf(url, sizeof url, "readfwd?calview=day?year=%d?month=%d?day=%d",
117 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday);
118 wc_printf("<td><a href=\"%s\">%d</a></td>", url, tm.tm_mday);
121 wc_printf("<td> </td>");
124 /* After displaying one week, end the row */
126 wc_printf("</tr>\n");
131 thetime += (time_t)86400; /* ahead 24 hours */
134 wc_printf("</table>" /* end of inner table */
137 StrBufAppendPrintf(WC->trailing_javascript,
138 " function minical_change_month(year, month) { \n"
139 " p = 'year=' + year + '&month=' + month \n"
140 " + '&r=' + ctdlRandomString(); \n"
141 " new Ajax.Updater('%s', 'mini_calendar', \n"
142 " { method: 'get', parameters: p, evalScripts: true } ); \n"
151 * ajax embedder for the above mini calendar
153 void ajax_mini_calendar(void)
155 embeddable_mini_calendar( ibstr("year"), ibstr("month"));
160 * Display one day of a whole month view of a calendar
162 void calendar_month_view_display_events(int year, int month, int day)
169 icalproperty *p = NULL;
170 icalproperty *q = NULL;
171 struct icaltimetype t;
172 struct icaltimetype end_t;
173 struct icaltimetype today_start_t;
174 struct icaltimetype today_end_t;
175 struct icaltimetype today_t;
176 struct tm starting_tm;
178 int all_day_event = 0;
184 if (GetCount(WCC->disp_cal_items) == 0) {
190 * Create an imaginary event which spans the 24 hours of today. Any events which
191 * overlap with this one take place at least partially in this day. We have to
192 * convert it from a struct tm in order to make it UTC.
194 memset(&starting_tm, 0, sizeof(struct tm));
195 starting_tm.tm_year = year - 1900;
196 starting_tm.tm_mon = month - 1;
197 starting_tm.tm_mday = day;
198 starting_tm.tm_hour = 0;
199 starting_tm.tm_min = 0;
200 today_start_t = icaltime_from_timet_with_zone(mktime(&starting_tm), 0, icaltimezone_get_utc_timezone());
201 today_start_t.is_utc = 1;
203 memset(&ending_tm, 0, sizeof(struct tm));
204 ending_tm.tm_year = year - 1900;
205 ending_tm.tm_mon = month - 1;
206 ending_tm.tm_mday = day;
207 ending_tm.tm_hour = 23;
208 ending_tm.tm_min = 59;
209 today_end_t = icaltime_from_timet_with_zone(mktime(&ending_tm), 0, icaltimezone_get_utc_timezone());
210 today_end_t.is_utc = 1;
213 * Create another one without caring about the timezone for all day events.
215 today_t = icaltime_null_date();
217 today_t.month = month;
221 * Now loop through our list of events to see which ones occur today.
223 Pos = GetNewHashPos(WCC->disp_cal_items, 0);
224 while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
225 Cal = (disp_cal*)vCal;
227 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
229 t = icalproperty_get_dtstart(q);
232 memset(&t, 0, sizeof t);
234 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
236 end_t = icalproperty_get_dtend(q);
239 memset(&end_t, 0, sizeof end_t);
241 if (t.is_date) all_day_event = 1;
245 show_event = ical_ctdl_is_overlap(t, end_t, today_t, icaltime_null_time());
249 show_event = ical_ctdl_is_overlap(t, end_t, today_start_t, today_end_t);
253 * If we determined that this event occurs today, then display it.
257 /* time_t logtt = icaltime_as_timet(t);
258 syslog(LOG_DEBUG, "Match on %04d-%02d-%02d for event %x%s on %s",
260 (int)Cal, ((all_day_event) ? " (all day)" : ""),
264 p = icalcomponent_get_first_property(Cal->cal, ICAL_SUMMARY_PROPERTY);
266 p = icalproperty_new_summary(_("Untitled Event"));
267 icalcomponent_add_property(Cal->cal, p);
272 wc_printf("<table border=\"0\" cellpadding=\"2\"><TR>"
273 "<td bgcolor=\"#CCCCDD\">"
278 wc_printf("<font size=\"-1\">"
279 "<a class=\"event%s\" href=\"display_edit_event?"
280 "msgnum=%ld?calview=month?year=%d?month=%d?day=%d\">"
282 (Cal->unread)?"_unread":"_read",
287 escputs((char *) icalproperty_get_comment(p));
289 wc_printf("<span class=\"tooltip\"><span class=\"btttop\"></span><span class=\"bttmiddle\">");
291 wc_printf("<i>%s: %s</i><br>", _("From"), Cal->from);
292 wc_printf("<i>%s</i> ", _("Summary:"));
293 escputs((char *)icalproperty_get_comment(p));
296 q = icalcomponent_get_first_property(
298 ICAL_LOCATION_PROPERTY);
300 wc_printf("<i>%s</i> ", _("Location:"));
301 escputs((char *)icalproperty_get_comment(q));
306 * Only show start/end times if we're actually looking at the VEVENT
307 * component. Otherwise it shows bogus dates for e.g. timezones
309 if (icalcomponent_isa(Cal->cal) == ICAL_VEVENT_COMPONENT) {
311 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
315 t = icalproperty_get_dtstart(q);
316 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
318 end_t = icalproperty_get_dtend(q);
322 * events with starting date/time equal
323 * ending date/time might get only
324 * DTSTART but no DTEND
334 /* end given, have to adjust it */
335 icaltime_adjust(&end_t, -1, 0, 0, 0);
337 memset(&d_tm, 0, sizeof d_tm);
338 d_tm.tm_year = t.year - 1900;
339 d_tm.tm_mon = t.month - 1;
340 d_tm.tm_mday = t.day;
341 wc_strftime(buf, sizeof buf, "%x", &d_tm);
343 if (no_end || !icaltime_compare(t, end_t)) {
344 wc_printf("<i>%s</i> %s<br>",
348 wc_printf("<i>%s</i> %s<br>",
349 _("Starting date:"), buf);
350 d_tm.tm_year = end_t.year - 1900;
351 d_tm.tm_mon = end_t.month - 1;
352 d_tm.tm_mday = end_t.day;
353 wc_strftime(buf, sizeof buf, "%x", &d_tm);
354 wc_printf("<i>%s</i> %s<br>",
355 _("Ending date:"), buf);
359 tt = icaltime_as_timet(t);
360 webcit_fmt_date(buf, 256, tt, DATEFMT_BRIEF);
361 if (no_end || !icaltime_compare(t, end_t)) {
362 wc_printf("<i>%s</i> %s<br>",
363 _("Date/time:"), buf);
366 wc_printf("<i>%s</i> %s<br>",
367 _("Starting date/time:"), buf);
368 tt = icaltime_as_timet(end_t);
369 webcit_fmt_date(buf, 256, tt, DATEFMT_BRIEF);
370 wc_printf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
378 q = icalcomponent_get_first_property(Cal->cal, ICAL_DESCRIPTION_PROPERTY);
380 wc_printf("<i>%s</i> ", _("Notes:"));
381 escputs((char *)icalproperty_get_comment(q));
385 wc_printf("</span><span class=\"bttbottom\"></span></span>");
386 wc_printf("</a></font><br>\n");
389 wc_printf("</td></tr></table>");
403 * Display one day of a whole month view of a calendar
405 void calendar_month_view_brief_events(time_t thetime, const char *daycolor) {
419 struct icaltimetype t;
421 int all_day_event = 0;
425 time_format = get_time_format_cached ();
427 if (time_format == WC_TIMEFORMAT_24) timeformat="%k:%M";
428 else timeformat="%I:%M %p";
430 localtime_r(&thetime, &today_tm);
432 Pos = GetNewHashPos(WCC->disp_cal_items, 0);
433 while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
434 Cal = (disp_cal*)vCal;
435 p = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
437 t = icalproperty_get_dtstart(p);
438 event_tt = icaltime_as_timet(t);
440 if (t.is_date) all_day_event = 1;
441 else all_day_event = 0;
444 gmtime_r(&event_tts, &event_tms);
447 localtime_r(&event_tts, &event_tms);
449 /* \todo epoch &! daymask */
450 if ((event_tms.tm_year == today_tm.tm_year)
451 && (event_tms.tm_mon == today_tm.tm_mon)
452 && (event_tms.tm_mday == today_tm.tm_mday)) {
458 p = icalcomponent_get_first_property(
460 ICAL_SUMMARY_PROPERTY);
462 p = icalproperty_new_summary(_("Untitled Event"));
463 icalcomponent_add_property(Cal->cal, p);
465 e = icalcomponent_get_first_property(
467 ICAL_DTEND_PROPERTY);
468 if ((p != NULL) && (e != NULL)) {
471 t = icalproperty_get_dtend(e);
472 event_tte = icaltime_as_timet(t);
473 localtime_r(&event_tte, &event_tme);
474 difftime=(event_tte-event_tts)/60;
475 hours=(int)(difftime / 60);
476 minutes=difftime % 60;
477 wc_printf("<tr><td bgcolor='%s'>%i:%2i</td><td bgcolor='%s'>"
479 "<a class=\"event%s\" href=\"display_edit_event?msgnum=%ld?calview=calbrief?year=%s?month=%s?day=%s\">",
482 (Cal->unread)?"_unread":"_read",
491 icalproperty_get_comment(p));
492 /* \todo: allso ammitime format */
493 wc_strftime(&sbuf[0], sizeof(sbuf), timeformat, &event_tms);
494 wc_strftime(&ebuf[0], sizeof(sbuf), timeformat, &event_tme);
496 wc_printf("</a></font></td>"
497 "<td bgcolor='%s'>%s</td><td bgcolor='%s'>%s</td></tr>",
514 * view one month. pretty view
516 void calendar_month_view(int year, int month, int day) {
517 struct tm starting_tm;
522 time_t previous_month;
524 time_t colheader_time;
526 struct tm colheader_tm;
527 char colheader_label[32];
531 * Make sure we know which day is today.
533 today_timet = time(NULL);
534 localtime_r(&today_timet, &today_tm);
537 * Determine what day to start. If an impossible value is found, start on Sunday.
539 get_pref_long("weekstart", &weekstart, 17);
540 if (weekstart > 6) weekstart = 0;
543 * Now back up to the 1st of the month...
545 memset(&starting_tm, 0, sizeof(struct tm));
547 starting_tm.tm_year = year - 1900;
548 starting_tm.tm_mon = month - 1;
549 starting_tm.tm_mday = day;
550 thetime = mktime(&starting_tm);
552 memcpy(&tm, &starting_tm, sizeof(struct tm));
553 while (tm.tm_mday != 1) {
554 thetime = thetime - (time_t)86400; /* go back 24 hours */
555 localtime_r(&thetime, &tm);
558 /* Determine previous and next months ... for links */
559 previous_month = thetime - (time_t)864000L; /* back 10 days */
560 next_month = thetime + (time_t)(31L * 86400L); /* ahead 31 days */
562 /* Now back up until we're on the user's preferred start day */
563 localtime_r(&thetime, &tm);
564 while (tm.tm_wday != weekstart) {
565 thetime = thetime - (time_t)86400; /* go back 24 hours */
566 localtime_r(&thetime, &tm);
569 /* Outer table (to get the background color) */
570 wc_printf("<table class=\"calendar\"> \n <tr><td>");
572 wc_printf("<table width=\"100%%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>\n");
574 wc_printf("<td align=\"center\">");
576 localtime_r(&previous_month, &tm);
577 wc_printf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
578 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
579 wc_printf("<img alt=\"%s\" align=\"middle\" src=\"static/webcit_icons/essen/32x32/back.png\" border=\"0\"></a>\n", _("previous"));
581 wc_strftime(colheader_label, sizeof colheader_label, "%B", &starting_tm);
582 wc_printf(" "
583 "<font size=\"+1\" color=\"#FFFFFF\">"
586 " ", colheader_label, year);
588 localtime_r(&next_month, &tm);
589 wc_printf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
590 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
591 wc_printf("<img alt=\"%s\" align=\"middle\" src=\"static/webcit_icons/essen/32x32/forward.png\" border=\"0\"></A>\n", _("next"));
593 wc_printf("</td></tr></table>\n");
595 /* Inner table (the real one) */
596 wc_printf("<table width=\"100%%\" border=\"0\" cellpadding=\"1\" cellspacing=\"1\" "
597 "bgcolor='#204B78' id=\"inner_month\"><tr>");
598 wc_printf("<th align=\"center\" width=\"2%%\"></th>");
599 colheader_time = thetime;
600 for (i=0; i<7; ++i) {
601 colheader_time = thetime + (i * 86400) ;
602 localtime_r(&colheader_time, &colheader_tm);
603 wc_strftime(colheader_label, sizeof colheader_label, "%A", &colheader_tm);
604 wc_printf("<th align=\"center\" width=\"14%%\">"
605 "<font color=\"#FFFFFF\">%s</font></th>", colheader_label);
608 wc_printf("</tr>\n");
611 /* Now do 35 or 42 days */
612 localtime_r(&thetime, &tm);
613 for (i = 0; i<42; ++i) {
615 /* Before displaying the first day of the week, start a new row */
617 wc_printf("<tr><td class=\"week_of_year\">");
618 wc_strftime(colheader_label, sizeof colheader_label, "%V", &tm);
619 wc_printf("%s ", colheader_label);
622 wc_printf("<td class=\"cal%s\"><div class=\"day\">",
623 ((tm.tm_mon != month-1) ? "out" :
624 (((tm.tm_year == today_tm.tm_year) && (tm.tm_mon == today_tm.tm_mon) && (tm.tm_mday == today_tm.tm_mday)) ? "today" :
625 ((tm.tm_wday==0 || tm.tm_wday==6) ? "weekend" :
628 if ((i==0) || (tm.tm_mday == 1)) {
629 wc_strftime(colheader_label, sizeof colheader_label, "%B", &tm);
630 wc_printf("%s ", colheader_label);
632 wc_printf("<a href=\"readfwd?calview=day?year=%d?month=%d?day=%d\">"
639 /* put the data here, stupid */
640 calendar_month_view_display_events(
648 /* After displaying the last day of the week, end the row */
650 wc_printf("</tr>\n");
653 thetime += (time_t)86400; /* ahead 24 hours */
654 localtime_r(&thetime, &tm);
656 if ( ((i % 7) == 6) && (tm.tm_mon != month-1) && (tm.tm_mday < 15) ) {
657 i = 100; /* break out of the loop */
661 wc_printf("</table>" /* end of inner table */
662 "</td></tr></table>\n" /* end of outer table */
667 * view one month. brief view
669 void calendar_brief_month_view(int year, int month, int day) {
670 struct tm starting_tm;
674 time_t previous_month;
676 char month_label[32];
678 /* Determine what day to start.
679 * First, back up to the 1st of the month...
681 memset(&starting_tm, 0, sizeof(struct tm));
682 starting_tm.tm_year = year - 1900;
683 starting_tm.tm_mon = month - 1;
684 starting_tm.tm_mday = day;
685 thetime = mktime(&starting_tm);
687 memcpy(&tm, &starting_tm, sizeof(struct tm));
688 while (tm.tm_mday != 1) {
689 thetime = thetime - (time_t)86400; /* go back 24 hours */
690 localtime_r(&thetime, &tm);
693 /* Determine previous and next months ... for links */
694 previous_month = thetime - (time_t)864000L; /* back 10 days */
695 next_month = thetime + (time_t)(31L * 86400L); /* ahead 31 days */
697 /* Now back up until we're on a Sunday */
698 localtime_r(&thetime, &tm);
699 while (tm.tm_wday != 0) {
700 thetime = thetime - (time_t)86400; /* go back 24 hours */
701 localtime_r(&thetime, &tm);
704 /* Outer table (to get the background color) */
705 wc_printf("<table width=\"100%%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" "
706 "bgcolor=#204B78><tr><td>\n");
708 wc_printf("<table width=\"100%%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>\n");
710 wc_printf("<td align=\"center\">");
712 localtime_r(&previous_month, &tm);
713 wc_printf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
714 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
715 wc_printf("<img alt=\"%s\" align=\"middle\" src=\"static/webcit_icons/essen/32x32/back.png\" border=\"0\"></a>\n", _("previous"));
717 wc_strftime(month_label, sizeof month_label, "%B", &tm);
718 wc_printf(" "
719 "<font size=\"+1\" color=\"#FFFFFF\">"
722 " ", month_label, year);
724 localtime_r(&next_month, &tm);
725 wc_printf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
726 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
727 wc_printf("<img alt=\"%s\" align=\"middle\" src=\"static/webcit_icons/essen/32x32/forward.png\" border=\"0\"></a>\n", _("next"));
729 wc_printf("</td></tr></table>\n");
731 /* Inner table (the real one) */
732 wc_printf("<table width=\"100%%\" border=\"0\" cellpadding=\"1\" cellspacing=\"1\" "
733 "bgcolor=#EEEECC><TR>");
734 wc_printf("</tr>\n");
735 wc_printf("<tr><td colspan=\"100%%\">\n");
738 for (i = 0; i < 35; ++i) {
739 char weeknumber[255];
740 char weekday_name[32];
742 localtime_r(&thetime, &tm);
745 /* Before displaying Sunday, start a new CELL */
747 wc_strftime(&weeknumber[0], sizeof(weeknumber), "%U", &tm);
748 wc_printf("<table border='0' bgcolor=\"#EEEECC\" width='100%%'> <tr><th colspan='4'>%s %s</th></tr>"
749 " <tr><td>%s</td><td width='70%%'>%s</td><td>%s</td><td>%s</td></tr>\n",
759 daycolor=((tm.tm_mon != month-1) ? "DDDDDD" :
760 ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
764 wc_strftime(weekday_name, sizeof weekday_name, "%A", &tm);
765 wc_printf("<tr><td bgcolor='%s' colspan='1' align='left'> %s,%i."
766 "</td><td bgcolor='%s' colspan='3'><hr></td></tr>\n",
768 weekday_name,tm.tm_mday,
771 /* put the data of one day here, stupid */
772 calendar_month_view_brief_events(thetime, daycolor);
775 /* After displaying Saturday, end the row */
777 wc_printf("</td></tr></table>\n");
780 thetime += (time_t)86400; /* ahead 24 hours */
783 wc_printf("</table>" /* end of inner table */
784 "</td></tr></table>\n" /* end of outer table */
789 * Calendar week view -- not implemented yet, this is a stub function
791 void calendar_week_view(int year, int month, int day) {
792 wc_printf("<center><i>week view FIXME</i></center><br>\n");
798 * Display events for a particular hour of a particular day.
799 * (Specify hour < 0 to show "all day" events)
801 * dstart and dend indicate which hours our "daytime" begins and end
803 void calendar_day_view_display_events(time_t thetime,
815 icalproperty *p = NULL;
816 icalproperty *q = NULL;
822 int all_day_event = 0;
823 int ongoing_event = 0;
826 struct icaltimetype t;
827 struct icaltimetype end_t;
828 struct icaltimetype today_start_t;
829 struct icaltimetype today_end_t;
830 struct icaltimetype today_t;
831 struct tm starting_tm;
842 if (GetCount(WCC->disp_cal_items) == 0) {
843 /* nothing to display */
847 /* Create an imaginary event which spans the current day. Any events which
848 * overlap with this one take place at least partially in this day.
850 memset(&starting_tm, 0, sizeof(struct tm));
851 starting_tm.tm_year = year - 1900;
852 starting_tm.tm_mon = month - 1;
853 starting_tm.tm_mday = day;
854 starting_tm.tm_hour = 0;
855 starting_tm.tm_min = 0;
856 today_start_t = icaltime_from_timet_with_zone(mktime(&starting_tm), 0, icaltimezone_get_utc_timezone());
857 today_start_t.is_utc = 1;
859 memset(&ending_tm, 0, sizeof(struct tm));
860 ending_tm.tm_year = year - 1900;
861 ending_tm.tm_mon = month - 1;
862 ending_tm.tm_mday = day;
863 ending_tm.tm_hour = 23;
864 ending_tm.tm_min = 59;
865 today_end_t = icaltime_from_timet_with_zone(mktime(&ending_tm), 0, icaltimezone_get_utc_timezone());
866 today_end_t.is_utc = 1;
869 * Create another one without caring about the timezone for all day events.
871 today_t = icaltime_null_date();
873 today_t.month = month;
876 /* Now loop through our list of events to see which ones occur today.
878 Pos = GetNewHashPos(WCC->disp_cal_items, 0);
879 while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
880 Cal = (disp_cal*)vCal;
885 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
887 t = icalproperty_get_dtstart(q);
888 event_tt = icaltime_as_timet(t);
889 localtime_r(&event_tt, &event_te);
892 memset(&t, 0, sizeof t);
895 if (t.is_date) all_day_event = 1;
897 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
899 end_t = icalproperty_get_dtend(q);
902 /* no end given means end = start */
903 memcpy(&end_t, &t, sizeof(struct icaltimetype));
908 show_event = ical_ctdl_is_overlap(t, end_t, today_t, icaltime_null_time());
909 if (icaltime_compare(t, end_t)) {
911 * the end date is non-inclusive so adjust it by one
912 * day because our test is inclusive, note that a day is
913 * not to much because we are talking about all day
916 icaltime_adjust(&end_t, -1, 0, 0, 0);
921 show_event = ical_ctdl_is_overlap(t, end_t, today_start_t, today_end_t);
924 event_tte = icaltime_as_timet(end_t);
925 localtime_r(&event_tte, &event_tm);
927 /* If we determined that this event occurs today, then display it.
929 p = icalcomponent_get_first_property(Cal->cal,ICAL_SUMMARY_PROPERTY);
931 p = icalproperty_new_summary(_("Untitled Event"));
932 icalcomponent_add_property(Cal->cal, p);
935 if ((show_event) && (p != NULL)) {
937 if ((event_te.tm_mday != day) || (event_tm.tm_mday != day)) ongoing_event = 1;
939 if (all_day_event && notime_events)
941 wc_printf("<li class=\"event_framed%s\"> "
942 "<a href=\"display_edit_event?"
943 "msgnum=%ld?calview=day?year=%d?month=%d?day=%d\" "
944 " class=\"event_title\">"
946 (Cal->unread)?"_unread":"_read",
947 Cal->cal_msgnum, year, month, day
949 escputs((char *) icalproperty_get_comment(p));
950 wc_printf("<span class=\"tooltip\"><span class=\"btttop\"></span><span class=\"bttmiddle\">");
951 wc_printf("<i>%s</i><br>", _("All day event"));
952 wc_printf("<i>%s: %s</i><br>", _("From"), Cal->from);
953 wc_printf("<i>%s</i> ", _("Summary:"));
954 escputs((char *) icalproperty_get_comment(p));
956 q = icalcomponent_get_first_property(Cal->cal,ICAL_LOCATION_PROPERTY);
958 wc_printf("<i>%s</i> ", _("Location:"));
959 escputs((char *)icalproperty_get_comment(q));
962 if (!icaltime_compare(t, end_t)) { /* one day only */
963 webcit_fmt_date(buf, 256, event_tt, DATEFMT_LOCALEDATE);
964 wc_printf("<i>%s</i> %s<br>", _("Date:"), buf);
967 webcit_fmt_date(buf, 256, event_tt, DATEFMT_LOCALEDATE);
968 wc_printf("<i>%s</i> %s<br>", _("Starting date:"), buf);
969 webcit_fmt_date(buf, 256, event_tte, DATEFMT_LOCALEDATE);
970 wc_printf("<i>%s</i> %s<br>", _("Ending date:"), buf);
972 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
974 wc_printf("<i>%s</i> ", _("Notes:"));
975 escputs((char *)icalproperty_get_comment(q));
978 wc_printf("</span><span class=\"bttbottom\"></span></span>");
979 wc_printf("</a> <span>(");
980 wc_printf(_("All day event"));
981 wc_printf(")</span></li>\n");
983 else if (ongoing_event && notime_events)
985 wc_printf("<li class=\"event_framed%s\"> "
986 "<a href=\"display_edit_event?"
987 "msgnum=%ld&calview=day?year=%d?month=%d?day=%d\" "
988 " class=\"event_title\">"
990 (Cal->unread)?"_unread":"_read",
991 Cal->cal_msgnum, year, month, day
993 escputs((char *) icalproperty_get_comment(p));
994 wc_printf("<span class=\"tooltip\"><span class=\"btttop\"></span><span class=\"bttmiddle\">");
995 wc_printf("<i>%s</i><br>", _("Ongoing event"));
996 wc_printf("<i>%s: %s</i><br>", _("From"), Cal->from);
997 wc_printf("<i>%s</i> ", _("Summary:"));
998 escputs((char *) icalproperty_get_comment(p));
1000 q = icalcomponent_get_first_property(Cal->cal,ICAL_LOCATION_PROPERTY);
1002 wc_printf("<i>%s</i> ", _("Location:"));
1003 escputs((char *)icalproperty_get_comment(q));
1006 webcit_fmt_date(buf, 256, event_tt, DATEFMT_BRIEF);
1007 wc_printf("<i>%s</i> %s<br>", _("Starting date/time:"), buf);
1008 webcit_fmt_date(buf, 256, event_tte, DATEFMT_BRIEF);
1009 wc_printf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
1010 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
1012 wc_printf("<i>%s</i> ", _("Notes:"));
1013 escputs((char *)icalproperty_get_comment(q));
1016 wc_printf("</span><span class=\"bttbottom\"></span></span>");
1017 wc_printf("</a> <span>(");
1018 wc_printf(_("Ongoing event"));
1019 wc_printf(")</span></li>\n");
1021 else if (!all_day_event && !notime_events)
1025 if (event_te.tm_mday != day) event_te.tm_hour = 0;
1026 if (event_tm.tm_mday != day) event_tm.tm_hour = 24;
1028 /* Calculate the location of the top of the box */
1029 if (event_te.tm_hour < dstart) {
1030 startmin = diffmin = event_te.tm_min / 6;
1031 top = (event_te.tm_hour * EXTRATIMELINE) + startmin;
1033 else if ((event_te.tm_hour >= dstart) && (event_te.tm_hour <= dend)) {
1034 startmin = diffmin = (event_te.tm_min / 2);
1035 top = (dstart * EXTRATIMELINE) + ((event_te.tm_hour - dstart) * TIMELINE) + startmin;
1037 else if (event_te.tm_hour >dend) {
1038 startmin = diffmin = event_te.tm_min / 6;
1039 top = (dstart * EXTRATIMELINE) + ((dend - dstart - 1) * TIMELINE) + ((event_tm.tm_hour - dend + 1) * EXTRATIMELINE) + startmin ;
1042 /* should never get here */
1045 /* Calculate the location of the bottom of the box */
1046 if (event_tm.tm_hour < dstart) {
1047 endmin = diffmin = event_tm.tm_min / 6;
1048 bottom = (event_tm.tm_hour * EXTRATIMELINE) + endmin;
1050 else if ((event_tm.tm_hour >= dstart) && (event_tm.tm_hour <= dend)) {
1051 endmin = diffmin = (event_tm.tm_min / 2);
1052 bottom = (dstart * EXTRATIMELINE) + ((event_tm.tm_hour - dstart) * TIMELINE) + endmin ;
1054 else if (event_tm.tm_hour >dend) {
1055 endmin = diffmin = event_tm.tm_min / 6;
1056 bottom = (dstart * EXTRATIMELINE) + ((dend - dstart + 1) * TIMELINE) + ((event_tm.tm_hour - dend - 1) * EXTRATIMELINE) + endmin;
1059 /* should never get here */
1062 wc_printf("<dd class=\"event_framed%s\" "
1063 "style=\"position: absolute; "
1064 "top:%dpx; left:%dpx; "
1065 "height:%dpx; \" >",
1066 (Cal->unread)?"_unread":"_read",
1067 top, (gap * 40), (bottom-top)
1069 wc_printf("<a href=\"display_edit_event?"
1070 "msgnum=%ld?calview=day?year=%d?month=%d?day=%d?hour=%d\" "
1071 "class=\"event_title\">"
1073 Cal->cal_msgnum, year, month, day, t.hour
1075 escputs((char *) icalproperty_get_comment(p));
1076 wc_printf("<span class=\"tooltip\"><span class=\"btttop\"></span><span class=\"bttmiddle\">");
1077 wc_printf("<i>%s: %s</i><br>", _("From"), Cal->from);
1078 wc_printf("<i>%s</i> ", _("Summary:"));
1079 escputs((char *) icalproperty_get_comment(p));
1081 q = icalcomponent_get_first_property(Cal->cal,ICAL_LOCATION_PROPERTY);
1083 wc_printf("<i>%s</i> ", _("Location:"));
1084 escputs((char *)icalproperty_get_comment(q));
1087 if (!icaltime_compare(t, end_t)) { /* one day only */
1088 webcit_fmt_date(buf, 256, event_tt, DATEFMT_BRIEF);
1089 wc_printf("<i>%s</i> %s<br>", _("Date/time:"), buf);
1092 webcit_fmt_date(buf, 256, event_tt, DATEFMT_BRIEF);
1093 wc_printf("<i>%s</i> %s<br>", _("Starting date/time:"), buf);
1094 webcit_fmt_date(buf, 256, event_tte, DATEFMT_BRIEF);
1095 wc_printf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
1097 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
1099 wc_printf("<i>%s</i> ", _("Notes:"));
1100 escputs((char *)icalproperty_get_comment(q));
1103 wc_printf("</span><span class=\"bttbottom\"></span></span>");
1104 wc_printf("</a></dd>\n");
1108 DeleteHashPos(&Pos);
1114 void calendar_day_view(int year, int month, int day) {
1116 struct icaltimetype today, yesterday, tomorrow;
1123 int timeline = TIMELINE;
1124 int extratimeline = EXTRATIMELINE;
1129 time_format = get_time_format_cached ();
1130 get_pref_long("daystart", &daystart, 8);
1131 get_pref_long("dayend", &dayend, 17);
1133 /* when loading daystart/dayend, replace missing, corrupt, or impossible values with defaults */
1134 if ((daystart < 0) || (dayend < 2) || (daystart >= 23) || (dayend > 23) || (dayend <= daystart)) {
1140 memset(&d_tm, 0, sizeof d_tm);
1141 d_tm.tm_year = year - 1900;
1142 d_tm.tm_mon = month - 1;
1144 today_t = mktime(&d_tm);
1146 /* Figure out the dates for "yesterday" and "tomorrow" links */
1148 memset(&today, 0, sizeof(struct icaltimetype));
1150 today.month = month;
1154 memcpy(&yesterday, &today, sizeof(struct icaltimetype));
1156 yesterday = icaltime_normalize(yesterday);
1158 memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
1160 tomorrow = icaltime_normalize(tomorrow);
1162 /* Inner table (the real one) */
1163 wc_printf("<table class=\"calendar\" id=\"inner_day\"><tr> \n");
1165 /* Innermost cell (contains hours etc.) */
1166 wc_printf("<td class=\"events_of_the_day\" >");
1167 wc_printf("<dl class=\"events\" >");
1169 /* Now the middle of the day... */
1171 extrahourlabel = extratimeline - 2;
1172 hourlabel = extrahourlabel * 150 / 100;
1173 if (hourlabel > (timeline - 2)) hourlabel = timeline - 2;
1175 for (hour = 0; hour < daystart; ++hour) { /* could do HEIGHT=xx */
1176 wc_printf("<dt class=\"extrahour\">"
1177 "<a href=\"display_edit_event?msgnum=0"
1178 "?calview=day?year=%d?month=%d?day=%d?hour=%d?minute=0\">",
1179 /* TODO: what have these been used for?
1180 (hour * extratimeline ),
1184 year, month, day, hour
1187 if (time_format == WC_TIMEFORMAT_24) {
1188 wc_printf("%2d:00</a> ", hour);
1191 wc_printf("%d:00%s</a> ",
1192 ((hour == 0) ? 12 : (hour <= 12 ? hour : hour-12)),
1193 (hour < 12 ? "am" : "pm")
1200 gap = daystart * extratimeline;
1202 for (hour = daystart; hour <= dayend; ++hour) { /* could do HEIGHT=xx */
1203 wc_printf("<dt class=\"hour\">"
1204 "<a href=\"display_edit_event?msgnum=0?calview=day"
1205 "?year=%d?month=%d?day=%d?hour=%d?minute=0\">",
1206 /*TODO: what have these been used for?
1207 gap + ((hour - daystart) * timeline ),
1211 year, month, day, hour
1214 if (time_format == WC_TIMEFORMAT_24) {
1215 wc_printf("%2d:00</a> ", hour);
1218 wc_printf("%d:00%s</a> ",
1219 (hour <= 12 ? hour : hour-12),
1220 (hour < 12 ? "am" : "pm")
1227 gap = gap + ((dayend - daystart + 1) * timeline);
1229 for (hour = (dayend + 1); hour < 24; ++hour) { /* could do HEIGHT=xx */
1230 wc_printf("<dt class=\"extrahour\">"
1231 "<a href=\"display_edit_event?msgnum=0?calview=day"
1232 "?year=%d?month=%d?day=%d?hour=%d?minute=0\">",
1233 /*TODO: what have these been used for?
1234 gap + ((hour - dayend - 1) * extratimeline ),
1238 year, month, day, hour
1241 if (time_format == WC_TIMEFORMAT_24) {
1242 wc_printf("%2d:00</a> ", hour);
1245 wc_printf("%d:00%s</a> ",
1246 (hour <= 12 ? hour : hour-12),
1247 (hour < 12 ? "am" : "pm")
1254 /* Display events with start and end times on this day */
1255 calendar_day_view_display_events(today_t, year, month, day, 0, daystart, dayend);
1258 wc_printf("</td>"); /* end of innermost table */
1260 /* Display extra events (start/end times not present or not today) in the middle column */
1261 wc_printf("<td class=\"extra_events\">");
1265 /* Display all-day events */
1266 calendar_day_view_display_events(today_t, year, month, day, 1, daystart, dayend);
1270 wc_printf("</td>"); /* end extra on the middle */
1272 wc_printf("<td width='20%%' align=\"center\" valign=top>"); /* begin stuff-on-the-right */
1274 /* Begin todays-date-with-left-and-right-arrows */
1275 wc_printf("<table border=\"0\" width=\"100%%\" "
1276 "cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#FFFFFF\">\n");
1280 wc_printf("<td align=\"center\">");
1281 wc_printf("<a href=\"readfwd?calview=day?year=%d?month=%d?day=%d\">",
1282 yesterday.year, yesterday.month, yesterday.day);
1283 wc_printf("<img alt=\"previous\" align=\"middle\" src=\"static/webcit_icons/essen/32x32/back.png\" border=\"0\"></a>");
1286 wc_strftime(d_str, sizeof d_str,
1287 "<td align=\"center\">"
1288 "<font size='+2'>%A</font><br>"
1289 "<font size='+2'>%B</font><br>"
1290 "<font size='+3'>%d</font><br>"
1291 "<font size='+2'>%Y</font><br>"
1295 wc_printf("%s", d_str);
1298 wc_printf("<td align=\"center\">");
1299 wc_printf("<a href=\"readfwd?calview=day?year=%d?month=%d?day=%d\">",
1300 tomorrow.year, tomorrow.month, tomorrow.day);
1301 wc_printf("<img alt=\"%s\" align=\"middle\" src=\"static/webcit_icons/essen/32x32/forward.png\""
1302 " border=\"0\"></a>\n", _("next"));
1305 wc_printf("</tr></table>\n");
1306 /* End todays-date-with-left-and-right-arrows */
1308 /* Embed a mini month calendar in this space */
1309 wc_printf("<br>\n");
1310 embeddable_mini_calendar(year, month);
1312 wc_printf("</td></tr>"); /* end stuff-on-the-right */
1313 wc_printf("</table>\n"); /* end of inner table */
1318 * Display today's events. Returns the number of items displayed.
1320 int calendar_summary_view(void) {
1322 const char *HashKey;
1327 struct icaltimetype t;
1332 int all_day_event = 0;
1333 char timestring[SIZ];
1334 wcsession *WCC = WC;
1335 int num_displayed = 0;
1337 if (GetCount(WC->disp_cal_items) == 0) {
1342 localtime_r(&now, &today_tm);
1344 Pos = GetNewHashPos(WCC->disp_cal_items, 0);
1345 while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
1346 Cal = (disp_cal*)vCal;
1347 p = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
1349 t = icalproperty_get_dtstart(p);
1350 event_tt = icaltime_as_timet(t);
1357 fmt_time(timestring, SIZ, event_tt);
1359 if (all_day_event) {
1360 gmtime_r(&event_tt, &event_tm);
1363 localtime_r(&event_tt, &event_tm);
1366 if ( (event_tm.tm_year == today_tm.tm_year)
1367 && (event_tm.tm_mon == today_tm.tm_mon)
1368 && (event_tm.tm_mday == today_tm.tm_mday)
1371 p = icalcomponent_get_first_property(Cal->cal, ICAL_SUMMARY_PROPERTY);
1373 p = icalproperty_new_summary(_("Untitled Task"));
1374 icalcomponent_add_property(Cal->cal, p);
1378 if (WCC->CurRoom.view == VIEW_TASKS) {
1379 wc_printf("<a href=\"display_edit_task"
1381 "?return_to_summary=1"
1385 escputs(ChrPtr(WCC->CurRoom.name));
1389 wc_printf("<a href=\"display_edit_event"
1397 today_tm.tm_year + 1900,
1398 today_tm.tm_mon + 1,
1401 escputs(ChrPtr(WCC->CurRoom.name));
1404 escputs((char *) icalproperty_get_comment(p));
1405 if (!all_day_event) {
1406 wc_printf(" (%s)", timestring);
1408 wc_printf("</a><br>\n");
1414 DeleteHashPos(&Pos);
1415 DeleteHash(&WC->disp_cal_items);
1416 return(num_displayed);
1420 * Parse the URL variables in order to determine the scope and display of a calendar view
1422 int calendar_GetParamsGetServerCall(SharedMessageStatus *Stat,
1423 void **ViewSpecific,
1430 wcsession *WCC = WC;
1438 c = (calview*) malloc(sizeof(calview));
1439 memset(c, 0, sizeof(calview));
1440 *ViewSpecific = (void*)c;
1442 Stat->load_seen = 1;
1443 strcpy(cmd, "MSGS ALL");
1444 Stat->maxmsgs = 32767;
1446 /* In case no date was specified, go with today */
1448 localtime_r(&now, &tm);
1449 c->year = tm.tm_year + 1900;
1450 c->month = tm.tm_mon + 1;
1451 c->day = tm.tm_mday;
1453 /* Now see if a date was specified */
1454 if (havebstr("year")) c->year = ibstr("year");
1455 if (havebstr("month")) c->month = ibstr("month");
1456 if (havebstr("day")) c->day = ibstr("day");
1458 /* How would you like that cooked? */
1459 if (havebstr("calview")) {
1460 strcpy(cv, bstr("calview"));
1463 strcpy(cv, "month");
1466 /* Display the selected view */
1467 if (!strcasecmp(cv, "day")) {
1468 c->view = calview_day;
1470 else if (!strcasecmp(cv, "week")) {
1471 c->view = calview_week;
1473 else if (!strcasecmp(cv, "summary")) { /* shouldn't ever happen, but just in case */
1474 c->view = calview_day;
1477 if (WCC->CurRoom.view == VIEW_CALBRIEF) {
1478 c->view = calview_brief;
1481 c->view = calview_month;
1485 /* Now try and set the lower and upper bounds so that we don't
1486 * burn too many cpu cycles parsing data way in the past or future
1489 tm.tm_year = c->year - 1900;
1490 tm.tm_mon = c->month - 1;
1491 tm.tm_mday = c->day;
1494 if (c->view == calview_month) span = 3888000;
1495 if (c->view == calview_brief) span = 3888000;
1496 if (c->view == calview_week) span = 604800;
1497 if (c->view == calview_day) span = 86400;
1498 if (c->view == calview_summary) span = 86400;
1500 c->lower_bound = now - span;
1501 c->upper_bound = now + span;
1508 * Render a calendar view from data previously loaded into memory
1510 int calendar_RenderView_or_Tail(SharedMessageStatus *Stat,
1511 void **ViewSpecific,
1514 wcsession *WCC = WC;
1515 calview *c = (calview*) *ViewSpecific;
1517 if (c->view == calview_day) {
1518 calendar_day_view(c->year, c->month, c->day);
1520 else if (c->view == calview_week) {
1521 calendar_week_view(c->year, c->month, c->day);
1524 if (WCC->CurRoom.view == VIEW_CALBRIEF) {
1525 calendar_brief_month_view(c->year, c->month, c->day);
1528 calendar_month_view(c->year, c->month, c->day);
1532 /* Free the in-memory list of calendar items */
1533 DeleteHash(&WC->disp_cal_items);
1538 InitModule_CALENDAR_VIEW
1541 WebcitAddUrlHandler(HKEY("mini_calendar"), "", 0, ajax_mini_calendar, AJAX);