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());
202 memset(&ending_tm, 0, sizeof(struct tm));
203 ending_tm.tm_year = year - 1900;
204 ending_tm.tm_mon = month - 1;
205 ending_tm.tm_mday = day;
206 ending_tm.tm_hour = 23;
207 ending_tm.tm_min = 59;
208 today_end_t = icaltime_from_timet_with_zone(mktime(&ending_tm), 0, icaltimezone_get_utc_timezone());
211 * Create another one without caring about the timezone for all day events.
213 today_t = icaltime_null_date();
215 today_t.month = month;
219 * Now loop through our list of events to see which ones occur today.
221 Pos = GetNewHashPos(WCC->disp_cal_items, 0);
222 while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
223 Cal = (disp_cal*)vCal;
225 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
227 t = icalproperty_get_dtstart(q);
230 memset(&t, 0, sizeof t);
232 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
234 end_t = icalproperty_get_dtend(q);
237 memset(&end_t, 0, sizeof end_t);
239 if (t.is_date) all_day_event = 1;
243 show_event = ical_ctdl_is_overlap(t, end_t, today_t, icaltime_null_time());
247 show_event = ical_ctdl_is_overlap(t, end_t, today_start_t, today_end_t);
251 * If we determined that this event occurs today, then display it.
255 /* time_t logtt = icaltime_as_timet(t);
256 syslog(LOG_DEBUG, "Match on %04d-%02d-%02d for event %x%s on %s",
258 (int)Cal, ((all_day_event) ? " (all day)" : ""),
262 p = icalcomponent_get_first_property(Cal->cal, ICAL_SUMMARY_PROPERTY);
264 p = icalproperty_new_summary(_("Untitled Event"));
265 icalcomponent_add_property(Cal->cal, p);
270 wc_printf("<table border=\"0\" cellpadding=\"2\"><TR>"
271 "<td bgcolor=\"#CCCCDD\">"
276 wc_printf("<font size=\"-1\">"
277 "<a class=\"event%s\" href=\"display_edit_event?"
278 "msgnum=%ld?calview=month?year=%d?month=%d?day=%d\">"
280 (Cal->unread)?"_unread":"_read",
285 escputs((char *) icalproperty_get_comment(p));
287 wc_printf("<span class=\"tooltip\"><span class=\"btttop\"></span><span class=\"bttmiddle\">");
289 wc_printf("<i>%s: %s</i><br>", _("From"), Cal->from);
290 wc_printf("<i>%s</i> ", _("Summary:"));
291 escputs((char *)icalproperty_get_comment(p));
294 q = icalcomponent_get_first_property(
296 ICAL_LOCATION_PROPERTY);
298 wc_printf("<i>%s</i> ", _("Location:"));
299 escputs((char *)icalproperty_get_comment(q));
304 * Only show start/end times if we're actually looking at the VEVENT
305 * component. Otherwise it shows bogus dates for e.g. timezones
307 if (icalcomponent_isa(Cal->cal) == ICAL_VEVENT_COMPONENT) {
309 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
313 t = icalproperty_get_dtstart(q);
314 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
316 end_t = icalproperty_get_dtend(q);
320 * events with starting date/time equal
321 * ending date/time might get only
322 * DTSTART but no DTEND
332 /* end given, have to adjust it */
333 icaltime_adjust(&end_t, -1, 0, 0, 0);
335 memset(&d_tm, 0, sizeof d_tm);
336 d_tm.tm_year = t.year - 1900;
337 d_tm.tm_mon = t.month - 1;
338 d_tm.tm_mday = t.day;
339 wc_strftime(buf, sizeof buf, "%x", &d_tm);
341 if (no_end || !icaltime_compare(t, end_t)) {
342 wc_printf("<i>%s</i> %s<br>",
346 wc_printf("<i>%s</i> %s<br>",
347 _("Starting date:"), buf);
348 d_tm.tm_year = end_t.year - 1900;
349 d_tm.tm_mon = end_t.month - 1;
350 d_tm.tm_mday = end_t.day;
351 wc_strftime(buf, sizeof buf, "%x", &d_tm);
352 wc_printf("<i>%s</i> %s<br>",
353 _("Ending date:"), buf);
357 tt = icaltime_as_timet(t);
358 webcit_fmt_date(buf, 256, tt, DATEFMT_BRIEF);
359 if (no_end || !icaltime_compare(t, end_t)) {
360 wc_printf("<i>%s</i> %s<br>",
361 _("Date/time:"), buf);
364 wc_printf("<i>%s</i> %s<br>",
365 _("Starting date/time:"), buf);
366 tt = icaltime_as_timet(end_t);
367 webcit_fmt_date(buf, 256, tt, DATEFMT_BRIEF);
368 wc_printf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
376 q = icalcomponent_get_first_property(Cal->cal, ICAL_DESCRIPTION_PROPERTY);
378 wc_printf("<i>%s</i> ", _("Notes:"));
379 escputs((char *)icalproperty_get_comment(q));
383 wc_printf("</span><span class=\"bttbottom\"></span></span>");
384 wc_printf("</a></font><br>\n");
387 wc_printf("</td></tr></table>");
401 * Display one day of a whole month view of a calendar
403 void calendar_month_view_brief_events(time_t thetime, const char *daycolor) {
417 struct icaltimetype t;
419 int all_day_event = 0;
423 time_format = get_time_format_cached ();
425 if (time_format == WC_TIMEFORMAT_24) timeformat="%k:%M";
426 else timeformat="%I:%M %p";
428 localtime_r(&thetime, &today_tm);
430 Pos = GetNewHashPos(WCC->disp_cal_items, 0);
431 while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
432 Cal = (disp_cal*)vCal;
433 p = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
435 t = icalproperty_get_dtstart(p);
436 event_tt = icaltime_as_timet(t);
438 if (t.is_date) all_day_event = 1;
439 else all_day_event = 0;
442 gmtime_r(&event_tts, &event_tms);
445 localtime_r(&event_tts, &event_tms);
447 /* \todo epoch &! daymask */
448 if ((event_tms.tm_year == today_tm.tm_year)
449 && (event_tms.tm_mon == today_tm.tm_mon)
450 && (event_tms.tm_mday == today_tm.tm_mday)) {
456 p = icalcomponent_get_first_property(
458 ICAL_SUMMARY_PROPERTY);
460 p = icalproperty_new_summary(_("Untitled Event"));
461 icalcomponent_add_property(Cal->cal, p);
463 e = icalcomponent_get_first_property(
465 ICAL_DTEND_PROPERTY);
466 if ((p != NULL) && (e != NULL)) {
469 t = icalproperty_get_dtend(e);
470 event_tte = icaltime_as_timet(t);
471 localtime_r(&event_tte, &event_tme);
472 difftime=(event_tte-event_tts)/60;
473 hours=(int)(difftime / 60);
474 minutes=difftime % 60;
475 wc_printf("<tr><td bgcolor='%s'>%i:%2i</td><td bgcolor='%s'>"
477 "<a class=\"event%s\" href=\"display_edit_event?msgnum=%ld?calview=calbrief?year=%s?month=%s?day=%s\">",
480 (Cal->unread)?"_unread":"_read",
489 icalproperty_get_comment(p));
490 /* \todo: allso ammitime format */
491 wc_strftime(&sbuf[0], sizeof(sbuf), timeformat, &event_tms);
492 wc_strftime(&ebuf[0], sizeof(sbuf), timeformat, &event_tme);
494 wc_printf("</a></font></td>"
495 "<td bgcolor='%s'>%s</td><td bgcolor='%s'>%s</td></tr>",
512 * view one month. pretty view
514 void calendar_month_view(int year, int month, int day) {
515 struct tm starting_tm;
520 time_t previous_month;
522 time_t colheader_time;
524 struct tm colheader_tm;
525 char colheader_label[32];
529 * Make sure we know which day is today.
531 today_timet = time(NULL);
532 localtime_r(&today_timet, &today_tm);
535 * Determine what day to start. If an impossible value is found, start on Sunday.
537 get_pref_long("weekstart", &weekstart, 17);
538 if (weekstart > 6) weekstart = 0;
541 * Now back up to the 1st of the month...
543 memset(&starting_tm, 0, sizeof(struct tm));
545 starting_tm.tm_year = year - 1900;
546 starting_tm.tm_mon = month - 1;
547 starting_tm.tm_mday = day;
548 thetime = mktime(&starting_tm);
550 memcpy(&tm, &starting_tm, sizeof(struct tm));
551 while (tm.tm_mday != 1) {
552 thetime = thetime - (time_t)86400; /* go back 24 hours */
553 localtime_r(&thetime, &tm);
556 /* Determine previous and next months ... for links */
557 previous_month = thetime - (time_t)864000L; /* back 10 days */
558 next_month = thetime + (time_t)(31L * 86400L); /* ahead 31 days */
560 /* Now back up until we're on the user's preferred start day */
561 localtime_r(&thetime, &tm);
562 while (tm.tm_wday != weekstart) {
563 thetime = thetime - (time_t)86400; /* go back 24 hours */
564 localtime_r(&thetime, &tm);
567 /* Outer table (to get the background color) */
568 wc_printf("<table class=\"calendar\"> \n <tr><td>");
570 wc_printf("<table width=\"100%%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>\n");
572 wc_printf("<td align=\"center\">");
574 localtime_r(&previous_month, &tm);
575 wc_printf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
576 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
577 wc_printf("<img alt=\"%s\" align=\"middle\" src=\"static/webcit_icons/essen/32x32/back.png\" border=\"0\"></a>\n", _("previous"));
579 wc_strftime(colheader_label, sizeof colheader_label, "%B", &starting_tm);
580 wc_printf(" "
581 "<font size=\"+1\" color=\"#FFFFFF\">"
584 " ", colheader_label, year);
586 localtime_r(&next_month, &tm);
587 wc_printf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
588 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
589 wc_printf("<img alt=\"%s\" align=\"middle\" src=\"static/webcit_icons/essen/32x32/forward.png\" border=\"0\"></A>\n", _("next"));
591 wc_printf("</td></tr></table>\n");
593 /* Inner table (the real one) */
594 wc_printf("<table width=\"100%%\" border=\"0\" cellpadding=\"1\" cellspacing=\"1\" "
595 "bgcolor='#204B78' id=\"inner_month\"><tr>");
596 wc_printf("<th align=\"center\" width=\"2%%\"></th>");
597 colheader_time = thetime;
598 for (i=0; i<7; ++i) {
599 colheader_time = thetime + (i * 86400) ;
600 localtime_r(&colheader_time, &colheader_tm);
601 wc_strftime(colheader_label, sizeof colheader_label, "%A", &colheader_tm);
602 wc_printf("<th align=\"center\" width=\"14%%\">"
603 "<font color=\"#FFFFFF\">%s</font></th>", colheader_label);
606 wc_printf("</tr>\n");
609 /* Now do 35 or 42 days */
610 localtime_r(&thetime, &tm);
611 for (i = 0; i<42; ++i) {
613 /* Before displaying the first day of the week, start a new row */
615 wc_printf("<tr><td class=\"week_of_year\">");
616 wc_strftime(colheader_label, sizeof colheader_label, "%V", &tm);
617 wc_printf("%s ", colheader_label);
620 wc_printf("<td class=\"cal%s\"><div class=\"day\">",
621 ((tm.tm_mon != month-1) ? "out" :
622 (((tm.tm_year == today_tm.tm_year) && (tm.tm_mon == today_tm.tm_mon) && (tm.tm_mday == today_tm.tm_mday)) ? "today" :
623 ((tm.tm_wday==0 || tm.tm_wday==6) ? "weekend" :
626 if ((i==0) || (tm.tm_mday == 1)) {
627 wc_strftime(colheader_label, sizeof colheader_label, "%B", &tm);
628 wc_printf("%s ", colheader_label);
630 wc_printf("<a href=\"readfwd?calview=day?year=%d?month=%d?day=%d\">"
637 /* put the data here, stupid */
638 calendar_month_view_display_events(
646 /* After displaying the last day of the week, end the row */
648 wc_printf("</tr>\n");
651 thetime += (time_t)86400; /* ahead 24 hours */
652 localtime_r(&thetime, &tm);
654 if ( ((i % 7) == 6) && (tm.tm_mon != month-1) && (tm.tm_mday < 15) ) {
655 i = 100; /* break out of the loop */
659 wc_printf("</table>" /* end of inner table */
660 "</td></tr></table>\n" /* end of outer table */
665 * view one month. brief view
667 void calendar_brief_month_view(int year, int month, int day) {
668 struct tm starting_tm;
672 time_t previous_month;
674 char month_label[32];
676 /* Determine what day to start.
677 * First, back up to the 1st of the month...
679 memset(&starting_tm, 0, sizeof(struct tm));
680 starting_tm.tm_year = year - 1900;
681 starting_tm.tm_mon = month - 1;
682 starting_tm.tm_mday = day;
683 thetime = mktime(&starting_tm);
685 memcpy(&tm, &starting_tm, sizeof(struct tm));
686 while (tm.tm_mday != 1) {
687 thetime = thetime - (time_t)86400; /* go back 24 hours */
688 localtime_r(&thetime, &tm);
691 /* Determine previous and next months ... for links */
692 previous_month = thetime - (time_t)864000L; /* back 10 days */
693 next_month = thetime + (time_t)(31L * 86400L); /* ahead 31 days */
695 /* Now back up until we're on a Sunday */
696 localtime_r(&thetime, &tm);
697 while (tm.tm_wday != 0) {
698 thetime = thetime - (time_t)86400; /* go back 24 hours */
699 localtime_r(&thetime, &tm);
702 /* Outer table (to get the background color) */
703 wc_printf("<table width=\"100%%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" "
704 "bgcolor=#204B78><tr><td>\n");
706 wc_printf("<table width=\"100%%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>\n");
708 wc_printf("<td align=\"center\">");
710 localtime_r(&previous_month, &tm);
711 wc_printf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
712 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
713 wc_printf("<img alt=\"%s\" align=\"middle\" src=\"static/webcit_icons/essen/32x32/back.png\" border=\"0\"></a>\n", _("previous"));
715 wc_strftime(month_label, sizeof month_label, "%B", &tm);
716 wc_printf(" "
717 "<font size=\"+1\" color=\"#FFFFFF\">"
720 " ", month_label, year);
722 localtime_r(&next_month, &tm);
723 wc_printf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
724 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
725 wc_printf("<img alt=\"%s\" align=\"middle\" src=\"static/webcit_icons/essen/32x32/forward.png\" border=\"0\"></a>\n", _("next"));
727 wc_printf("</td></tr></table>\n");
729 /* Inner table (the real one) */
730 wc_printf("<table width=\"100%%\" border=\"0\" cellpadding=\"1\" cellspacing=\"1\" "
731 "bgcolor=#EEEECC><TR>");
732 wc_printf("</tr>\n");
733 wc_printf("<tr><td colspan=\"100%%\">\n");
736 for (i = 0; i < 35; ++i) {
737 char weeknumber[255];
738 char weekday_name[32];
740 localtime_r(&thetime, &tm);
743 /* Before displaying Sunday, start a new CELL */
745 wc_strftime(&weeknumber[0], sizeof(weeknumber), "%U", &tm);
746 wc_printf("<table border='0' bgcolor=\"#EEEECC\" width='100%%'> <tr><th colspan='4'>%s %s</th></tr>"
747 " <tr><td>%s</td><td width='70%%'>%s</td><td>%s</td><td>%s</td></tr>\n",
757 daycolor=((tm.tm_mon != month-1) ? "DDDDDD" :
758 ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
762 wc_strftime(weekday_name, sizeof weekday_name, "%A", &tm);
763 wc_printf("<tr><td bgcolor='%s' colspan='1' align='left'> %s,%i."
764 "</td><td bgcolor='%s' colspan='3'><hr></td></tr>\n",
766 weekday_name,tm.tm_mday,
769 /* put the data of one day here, stupid */
770 calendar_month_view_brief_events(thetime, daycolor);
773 /* After displaying Saturday, end the row */
775 wc_printf("</td></tr></table>\n");
778 thetime += (time_t)86400; /* ahead 24 hours */
781 wc_printf("</table>" /* end of inner table */
782 "</td></tr></table>\n" /* end of outer table */
787 * Calendar week view -- not implemented yet, this is a stub function
789 void calendar_week_view(int year, int month, int day) {
790 wc_printf("<center><i>week view FIXME</i></center><br>\n");
796 * Display events for a particular hour of a particular day.
797 * (Specify hour < 0 to show "all day" events)
799 * dstart and dend indicate which hours our "daytime" begins and end
801 void calendar_day_view_display_events(time_t thetime,
813 icalproperty *p = NULL;
814 icalproperty *q = NULL;
820 int all_day_event = 0;
821 int ongoing_event = 0;
824 struct icaltimetype t;
825 struct icaltimetype end_t;
826 struct icaltimetype today_start_t;
827 struct icaltimetype today_end_t;
828 struct icaltimetype today_t;
829 struct tm starting_tm;
840 if (GetCount(WCC->disp_cal_items) == 0) {
841 /* nothing to display */
845 /* Create an imaginary event which spans the current day. Any events which
846 * overlap with this one take place at least partially in this day.
848 memset(&starting_tm, 0, sizeof(struct tm));
849 starting_tm.tm_year = year - 1900;
850 starting_tm.tm_mon = month - 1;
851 starting_tm.tm_mday = day;
852 starting_tm.tm_hour = 0;
853 starting_tm.tm_min = 0;
854 today_start_t = icaltime_from_timet_with_zone(mktime(&starting_tm), 0, icaltimezone_get_utc_timezone());
856 memset(&ending_tm, 0, sizeof(struct tm));
857 ending_tm.tm_year = year - 1900;
858 ending_tm.tm_mon = month - 1;
859 ending_tm.tm_mday = day;
860 ending_tm.tm_hour = 23;
861 ending_tm.tm_min = 59;
862 today_end_t = icaltime_from_timet_with_zone(mktime(&ending_tm), 0, icaltimezone_get_utc_timezone());
865 * Create another one without caring about the timezone for all day events.
867 today_t = icaltime_null_date();
869 today_t.month = month;
872 /* Now loop through our list of events to see which ones occur today.
874 Pos = GetNewHashPos(WCC->disp_cal_items, 0);
875 while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
876 Cal = (disp_cal*)vCal;
881 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
883 t = icalproperty_get_dtstart(q);
884 event_tt = icaltime_as_timet(t);
885 localtime_r(&event_tt, &event_te);
888 memset(&t, 0, sizeof t);
891 if (t.is_date) all_day_event = 1;
893 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
895 end_t = icalproperty_get_dtend(q);
898 /* no end given means end = start */
899 memcpy(&end_t, &t, sizeof(struct icaltimetype));
904 show_event = ical_ctdl_is_overlap(t, end_t, today_t, icaltime_null_time());
905 if (icaltime_compare(t, end_t)) {
907 * the end date is non-inclusive so adjust it by one
908 * day because our test is inclusive, note that a day is
909 * not to much because we are talking about all day
912 icaltime_adjust(&end_t, -1, 0, 0, 0);
917 show_event = ical_ctdl_is_overlap(t, end_t, today_start_t, today_end_t);
920 event_tte = icaltime_as_timet(end_t);
921 localtime_r(&event_tte, &event_tm);
923 /* If we determined that this event occurs today, then display it.
925 p = icalcomponent_get_first_property(Cal->cal,ICAL_SUMMARY_PROPERTY);
927 p = icalproperty_new_summary(_("Untitled Event"));
928 icalcomponent_add_property(Cal->cal, p);
931 if ((show_event) && (p != NULL)) {
933 if ((event_te.tm_mday != day) || (event_tm.tm_mday != day)) ongoing_event = 1;
935 if (all_day_event && notime_events)
937 wc_printf("<li class=\"event_framed%s\"> "
938 "<a href=\"display_edit_event?"
939 "msgnum=%ld?calview=day?year=%d?month=%d?day=%d\" "
940 " class=\"event_title\">"
942 (Cal->unread)?"_unread":"_read",
943 Cal->cal_msgnum, year, month, day
945 escputs((char *) icalproperty_get_comment(p));
946 wc_printf("<span class=\"tooltip\"><span class=\"btttop\"></span><span class=\"bttmiddle\">");
947 wc_printf("<i>%s</i><br>", _("All day event"));
948 wc_printf("<i>%s: %s</i><br>", _("From"), Cal->from);
949 wc_printf("<i>%s</i> ", _("Summary:"));
950 escputs((char *) icalproperty_get_comment(p));
952 q = icalcomponent_get_first_property(Cal->cal,ICAL_LOCATION_PROPERTY);
954 wc_printf("<i>%s</i> ", _("Location:"));
955 escputs((char *)icalproperty_get_comment(q));
958 if (!icaltime_compare(t, end_t)) { /* one day only */
959 webcit_fmt_date(buf, 256, event_tt, DATEFMT_LOCALEDATE);
960 wc_printf("<i>%s</i> %s<br>", _("Date:"), buf);
963 webcit_fmt_date(buf, 256, event_tt, DATEFMT_LOCALEDATE);
964 wc_printf("<i>%s</i> %s<br>", _("Starting date:"), buf);
965 webcit_fmt_date(buf, 256, event_tte, DATEFMT_LOCALEDATE);
966 wc_printf("<i>%s</i> %s<br>", _("Ending date:"), buf);
968 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
970 wc_printf("<i>%s</i> ", _("Notes:"));
971 escputs((char *)icalproperty_get_comment(q));
974 wc_printf("</span><span class=\"bttbottom\"></span></span>");
975 wc_printf("</a> <span>(");
976 wc_printf(_("All day event"));
977 wc_printf(")</span></li>\n");
979 else if (ongoing_event && notime_events)
981 wc_printf("<li class=\"event_framed%s\"> "
982 "<a href=\"display_edit_event?"
983 "msgnum=%ld&calview=day?year=%d?month=%d?day=%d\" "
984 " class=\"event_title\">"
986 (Cal->unread)?"_unread":"_read",
987 Cal->cal_msgnum, year, month, day
989 escputs((char *) icalproperty_get_comment(p));
990 wc_printf("<span class=\"tooltip\"><span class=\"btttop\"></span><span class=\"bttmiddle\">");
991 wc_printf("<i>%s</i><br>", _("Ongoing event"));
992 wc_printf("<i>%s: %s</i><br>", _("From"), Cal->from);
993 wc_printf("<i>%s</i> ", _("Summary:"));
994 escputs((char *) icalproperty_get_comment(p));
996 q = icalcomponent_get_first_property(Cal->cal,ICAL_LOCATION_PROPERTY);
998 wc_printf("<i>%s</i> ", _("Location:"));
999 escputs((char *)icalproperty_get_comment(q));
1002 webcit_fmt_date(buf, 256, event_tt, DATEFMT_BRIEF);
1003 wc_printf("<i>%s</i> %s<br>", _("Starting date/time:"), buf);
1004 webcit_fmt_date(buf, 256, event_tte, DATEFMT_BRIEF);
1005 wc_printf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
1006 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
1008 wc_printf("<i>%s</i> ", _("Notes:"));
1009 escputs((char *)icalproperty_get_comment(q));
1012 wc_printf("</span><span class=\"bttbottom\"></span></span>");
1013 wc_printf("</a> <span>(");
1014 wc_printf(_("Ongoing event"));
1015 wc_printf(")</span></li>\n");
1017 else if (!all_day_event && !notime_events)
1021 if (event_te.tm_mday != day) event_te.tm_hour = 0;
1022 if (event_tm.tm_mday != day) event_tm.tm_hour = 24;
1024 /* Calculate the location of the top of the box */
1025 if (event_te.tm_hour < dstart) {
1026 startmin = diffmin = event_te.tm_min / 6;
1027 top = (event_te.tm_hour * EXTRATIMELINE) + startmin;
1029 else if ((event_te.tm_hour >= dstart) && (event_te.tm_hour <= dend)) {
1030 startmin = diffmin = (event_te.tm_min / 2);
1031 top = (dstart * EXTRATIMELINE) + ((event_te.tm_hour - dstart) * TIMELINE) + startmin;
1033 else if (event_te.tm_hour >dend) {
1034 startmin = diffmin = event_te.tm_min / 6;
1035 top = (dstart * EXTRATIMELINE) + ((dend - dstart - 1) * TIMELINE) + ((event_tm.tm_hour - dend + 1) * EXTRATIMELINE) + startmin ;
1038 /* should never get here */
1041 /* Calculate the location of the bottom of the box */
1042 if (event_tm.tm_hour < dstart) {
1043 endmin = diffmin = event_tm.tm_min / 6;
1044 bottom = (event_tm.tm_hour * EXTRATIMELINE) + endmin;
1046 else if ((event_tm.tm_hour >= dstart) && (event_tm.tm_hour <= dend)) {
1047 endmin = diffmin = (event_tm.tm_min / 2);
1048 bottom = (dstart * EXTRATIMELINE) + ((event_tm.tm_hour - dstart) * TIMELINE) + endmin ;
1050 else if (event_tm.tm_hour >dend) {
1051 endmin = diffmin = event_tm.tm_min / 6;
1052 bottom = (dstart * EXTRATIMELINE) + ((dend - dstart + 1) * TIMELINE) + ((event_tm.tm_hour - dend - 1) * EXTRATIMELINE) + endmin;
1055 /* should never get here */
1058 wc_printf("<dd class=\"event_framed%s\" "
1059 "style=\"position: absolute; "
1060 "top:%dpx; left:%dpx; "
1061 "height:%dpx; \" >",
1062 (Cal->unread)?"_unread":"_read",
1063 top, (gap * 40), (bottom-top)
1065 wc_printf("<a href=\"display_edit_event?"
1066 "msgnum=%ld?calview=day?year=%d?month=%d?day=%d?hour=%d\" "
1067 "class=\"event_title\">"
1069 Cal->cal_msgnum, year, month, day, t.hour
1071 escputs((char *) icalproperty_get_comment(p));
1072 wc_printf("<span class=\"tooltip\"><span class=\"btttop\"></span><span class=\"bttmiddle\">");
1073 wc_printf("<i>%s: %s</i><br>", _("From"), Cal->from);
1074 wc_printf("<i>%s</i> ", _("Summary:"));
1075 escputs((char *) icalproperty_get_comment(p));
1077 q = icalcomponent_get_first_property(Cal->cal,ICAL_LOCATION_PROPERTY);
1079 wc_printf("<i>%s</i> ", _("Location:"));
1080 escputs((char *)icalproperty_get_comment(q));
1083 if (!icaltime_compare(t, end_t)) { /* one day only */
1084 webcit_fmt_date(buf, 256, event_tt, DATEFMT_BRIEF);
1085 wc_printf("<i>%s</i> %s<br>", _("Date/time:"), buf);
1088 webcit_fmt_date(buf, 256, event_tt, DATEFMT_BRIEF);
1089 wc_printf("<i>%s</i> %s<br>", _("Starting date/time:"), buf);
1090 webcit_fmt_date(buf, 256, event_tte, DATEFMT_BRIEF);
1091 wc_printf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
1093 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
1095 wc_printf("<i>%s</i> ", _("Notes:"));
1096 escputs((char *)icalproperty_get_comment(q));
1099 wc_printf("</span><span class=\"bttbottom\"></span></span>");
1100 wc_printf("</a></dd>\n");
1104 DeleteHashPos(&Pos);
1110 void calendar_day_view(int year, int month, int day) {
1112 struct icaltimetype today, yesterday, tomorrow;
1119 int timeline = TIMELINE;
1120 int extratimeline = EXTRATIMELINE;
1125 time_format = get_time_format_cached ();
1126 get_pref_long("daystart", &daystart, 8);
1127 get_pref_long("dayend", &dayend, 17);
1129 /* when loading daystart/dayend, replace missing, corrupt, or impossible values with defaults */
1130 if ((daystart < 0) || (dayend < 2) || (daystart >= 23) || (dayend > 23) || (dayend <= daystart)) {
1136 memset(&d_tm, 0, sizeof d_tm);
1137 d_tm.tm_year = year - 1900;
1138 d_tm.tm_mon = month - 1;
1140 today_t = mktime(&d_tm);
1142 /* Figure out the dates for "yesterday" and "tomorrow" links */
1144 memset(&today, 0, sizeof(struct icaltimetype));
1146 today.month = month;
1150 memcpy(&yesterday, &today, sizeof(struct icaltimetype));
1152 yesterday = icaltime_normalize(yesterday);
1154 memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
1156 tomorrow = icaltime_normalize(tomorrow);
1158 /* Inner table (the real one) */
1159 wc_printf("<table class=\"calendar\" id=\"inner_day\"><tr> \n");
1161 /* Innermost cell (contains hours etc.) */
1162 wc_printf("<td class=\"events_of_the_day\" >");
1163 wc_printf("<dl class=\"events\" >");
1165 /* Now the middle of the day... */
1167 extrahourlabel = extratimeline - 2;
1168 hourlabel = extrahourlabel * 150 / 100;
1169 if (hourlabel > (timeline - 2)) hourlabel = timeline - 2;
1171 for (hour = 0; hour < daystart; ++hour) { /* could do HEIGHT=xx */
1172 wc_printf("<dt class=\"extrahour\">"
1173 "<a href=\"display_edit_event?msgnum=0"
1174 "?calview=day?year=%d?month=%d?day=%d?hour=%d?minute=0\">",
1175 /* TODO: what have these been used for?
1176 (hour * extratimeline ),
1180 year, month, day, hour
1183 if (time_format == WC_TIMEFORMAT_24) {
1184 wc_printf("%2d:00</a> ", hour);
1187 wc_printf("%d:00%s</a> ",
1188 ((hour == 0) ? 12 : (hour <= 12 ? hour : hour-12)),
1189 (hour < 12 ? "am" : "pm")
1196 gap = daystart * extratimeline;
1198 for (hour = daystart; hour <= dayend; ++hour) { /* could do HEIGHT=xx */
1199 wc_printf("<dt class=\"hour\">"
1200 "<a href=\"display_edit_event?msgnum=0?calview=day"
1201 "?year=%d?month=%d?day=%d?hour=%d?minute=0\">",
1202 /*TODO: what have these been used for?
1203 gap + ((hour - daystart) * timeline ),
1207 year, month, day, hour
1210 if (time_format == WC_TIMEFORMAT_24) {
1211 wc_printf("%2d:00</a> ", hour);
1214 wc_printf("%d:00%s</a> ",
1215 (hour <= 12 ? hour : hour-12),
1216 (hour < 12 ? "am" : "pm")
1223 gap = gap + ((dayend - daystart + 1) * timeline);
1225 for (hour = (dayend + 1); hour < 24; ++hour) { /* could do HEIGHT=xx */
1226 wc_printf("<dt class=\"extrahour\">"
1227 "<a href=\"display_edit_event?msgnum=0?calview=day"
1228 "?year=%d?month=%d?day=%d?hour=%d?minute=0\">",
1229 /*TODO: what have these been used for?
1230 gap + ((hour - dayend - 1) * extratimeline ),
1234 year, month, day, hour
1237 if (time_format == WC_TIMEFORMAT_24) {
1238 wc_printf("%2d:00</a> ", hour);
1241 wc_printf("%d:00%s</a> ",
1242 (hour <= 12 ? hour : hour-12),
1243 (hour < 12 ? "am" : "pm")
1250 /* Display events with start and end times on this day */
1251 calendar_day_view_display_events(today_t, year, month, day, 0, daystart, dayend);
1254 wc_printf("</td>"); /* end of innermost table */
1256 /* Display extra events (start/end times not present or not today) in the middle column */
1257 wc_printf("<td class=\"extra_events\">");
1261 /* Display all-day events */
1262 calendar_day_view_display_events(today_t, year, month, day, 1, daystart, dayend);
1266 wc_printf("</td>"); /* end extra on the middle */
1268 wc_printf("<td width='20%%' align=\"center\" valign=top>"); /* begin stuff-on-the-right */
1270 /* Begin todays-date-with-left-and-right-arrows */
1271 wc_printf("<table border=\"0\" width=\"100%%\" "
1272 "cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#FFFFFF\">\n");
1276 wc_printf("<td align=\"center\">");
1277 wc_printf("<a href=\"readfwd?calview=day?year=%d?month=%d?day=%d\">",
1278 yesterday.year, yesterday.month, yesterday.day);
1279 wc_printf("<img alt=\"previous\" align=\"middle\" src=\"static/webcit_icons/essen/32x32/back.png\" border=\"0\"></a>");
1282 wc_strftime(d_str, sizeof d_str,
1283 "<td align=\"center\">"
1284 "<font size='+2'>%A</font><br>"
1285 "<font size='+2'>%B</font><br>"
1286 "<font size='+3'>%d</font><br>"
1287 "<font size='+2'>%Y</font><br>"
1291 wc_printf("%s", d_str);
1294 wc_printf("<td align=\"center\">");
1295 wc_printf("<a href=\"readfwd?calview=day?year=%d?month=%d?day=%d\">",
1296 tomorrow.year, tomorrow.month, tomorrow.day);
1297 wc_printf("<img alt=\"%s\" align=\"middle\" src=\"static/webcit_icons/essen/32x32/forward.png\""
1298 " border=\"0\"></a>\n", _("next"));
1301 wc_printf("</tr></table>\n");
1302 /* End todays-date-with-left-and-right-arrows */
1304 /* Embed a mini month calendar in this space */
1305 wc_printf("<br>\n");
1306 embeddable_mini_calendar(year, month);
1308 wc_printf("</td></tr>"); /* end stuff-on-the-right */
1309 wc_printf("</table>\n"); /* end of inner table */
1314 * Display today's events. Returns the number of items displayed.
1316 int calendar_summary_view(void) {
1318 const char *HashKey;
1323 struct icaltimetype t;
1328 int all_day_event = 0;
1329 char timestring[SIZ];
1330 wcsession *WCC = WC;
1331 int num_displayed = 0;
1333 if (GetCount(WC->disp_cal_items) == 0) {
1338 localtime_r(&now, &today_tm);
1340 Pos = GetNewHashPos(WCC->disp_cal_items, 0);
1341 while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
1342 Cal = (disp_cal*)vCal;
1343 p = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
1345 t = icalproperty_get_dtstart(p);
1346 event_tt = icaltime_as_timet(t);
1353 fmt_time(timestring, SIZ, event_tt);
1355 if (all_day_event) {
1356 gmtime_r(&event_tt, &event_tm);
1359 localtime_r(&event_tt, &event_tm);
1362 if ( (event_tm.tm_year == today_tm.tm_year)
1363 && (event_tm.tm_mon == today_tm.tm_mon)
1364 && (event_tm.tm_mday == today_tm.tm_mday)
1367 p = icalcomponent_get_first_property(Cal->cal, ICAL_SUMMARY_PROPERTY);
1369 p = icalproperty_new_summary(_("Untitled Task"));
1370 icalcomponent_add_property(Cal->cal, p);
1374 if (WCC->CurRoom.view == VIEW_TASKS) {
1375 wc_printf("<a href=\"display_edit_task"
1377 "?return_to_summary=1"
1381 escputs(ChrPtr(WCC->CurRoom.name));
1385 wc_printf("<a href=\"display_edit_event"
1393 today_tm.tm_year + 1900,
1394 today_tm.tm_mon + 1,
1397 escputs(ChrPtr(WCC->CurRoom.name));
1400 escputs((char *) icalproperty_get_comment(p));
1401 if (!all_day_event) {
1402 wc_printf(" (%s)", timestring);
1404 wc_printf("</a><br>\n");
1410 DeleteHashPos(&Pos);
1411 DeleteHash(&WC->disp_cal_items);
1412 return(num_displayed);
1416 * Parse the URL variables in order to determine the scope and display of a calendar view
1418 int calendar_GetParamsGetServerCall(SharedMessageStatus *Stat,
1419 void **ViewSpecific,
1426 wcsession *WCC = WC;
1434 c = (calview*) malloc(sizeof(calview));
1435 memset(c, 0, sizeof(calview));
1436 *ViewSpecific = (void*)c;
1438 Stat->load_seen = 1;
1439 strcpy(cmd, "MSGS ALL");
1440 Stat->maxmsgs = 32767;
1442 /* In case no date was specified, go with today */
1444 localtime_r(&now, &tm);
1445 c->year = tm.tm_year + 1900;
1446 c->month = tm.tm_mon + 1;
1447 c->day = tm.tm_mday;
1449 /* Now see if a date was specified */
1450 if (havebstr("year")) c->year = ibstr("year");
1451 if (havebstr("month")) c->month = ibstr("month");
1452 if (havebstr("day")) c->day = ibstr("day");
1454 /* How would you like that cooked? */
1455 if (havebstr("calview")) {
1456 strcpy(cv, bstr("calview"));
1459 strcpy(cv, "month");
1462 /* Display the selected view */
1463 if (!strcasecmp(cv, "day")) {
1464 c->view = calview_day;
1466 else if (!strcasecmp(cv, "week")) {
1467 c->view = calview_week;
1469 else if (!strcasecmp(cv, "summary")) { /* shouldn't ever happen, but just in case */
1470 c->view = calview_day;
1473 if (WCC->CurRoom.view == VIEW_CALBRIEF) {
1474 c->view = calview_brief;
1477 c->view = calview_month;
1481 /* Now try and set the lower and upper bounds so that we don't
1482 * burn too many cpu cycles parsing data way in the past or future
1485 tm.tm_year = c->year - 1900;
1486 tm.tm_mon = c->month - 1;
1487 tm.tm_mday = c->day;
1490 if (c->view == calview_month) span = 3888000;
1491 if (c->view == calview_brief) span = 3888000;
1492 if (c->view == calview_week) span = 604800;
1493 if (c->view == calview_day) span = 86400;
1494 if (c->view == calview_summary) span = 86400;
1496 c->lower_bound = now - span;
1497 c->upper_bound = now + span;
1504 * Render a calendar view from data previously loaded into memory
1506 int calendar_RenderView_or_Tail(SharedMessageStatus *Stat,
1507 void **ViewSpecific,
1510 wcsession *WCC = WC;
1511 calview *c = (calview*) *ViewSpecific;
1513 if (c->view == calview_day) {
1514 calendar_day_view(c->year, c->month, c->day);
1516 else if (c->view == calview_week) {
1517 calendar_week_view(c->year, c->month, c->day);
1520 if (WCC->CurRoom.view == VIEW_CALBRIEF) {
1521 calendar_brief_month_view(c->year, c->month, c->day);
1524 calendar_month_view(c->year, c->month, c->day);
1528 /* Free the in-memory list of calendar items */
1529 DeleteHash(&WC->disp_cal_items);
1534 InitModule_CALENDAR_VIEW
1537 WebcitAddUrlHandler(HKEY("mini_calendar"), "", 0, ajax_mini_calendar, AJAX);