2 * Handles the HTML display of calendar items.
4 * Copyright (c) 1996-2010 by the citadel.org team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "webserver.h"
25 /* These define how high the hour rows are in the day view */
27 #define EXTRATIMELINE (TIMELINE / 2)
29 void embeddable_mini_calendar(int year, int month)
31 struct tm starting_tm;
35 time_t previous_month;
37 time_t colheader_time;
38 struct tm colheader_tm;
39 char colheader_label[32];
44 snprintf(div_id, sizeof div_id, "mini_calendar_%d", rand() );
46 /* Determine what day to start. If an impossible value is found, start on Sunday.
48 get_pref_long("weekstart", &weekstart, 17);
49 if (weekstart > 6) weekstart = 0;
52 * Now back up to the 1st of the month...
54 memset(&starting_tm, 0, sizeof(struct tm));
56 starting_tm.tm_year = year - 1900;
57 starting_tm.tm_mon = month - 1;
58 starting_tm.tm_mday = 1;
59 thetime = mktime(&starting_tm);
61 memcpy(&tm, &starting_tm, sizeof(struct tm));
62 while (tm.tm_mday != 1) {
63 thetime = thetime - (time_t)86400; /* go back 24 hours */
64 localtime_r(&thetime, &tm);
67 /* Determine previous and next months ... for links */
68 previous_month = thetime - (time_t)864000L; /* back 10 days */
69 next_month = thetime + (time_t)(31L * 86400L); /* ahead 31 days */
71 /* Now back up until we're on the user's preferred start day */
72 localtime_r(&thetime, &tm);
73 while (tm.tm_wday != weekstart) {
74 thetime = thetime - (time_t)86400; /* go back 24 hours */
75 localtime_r(&thetime, &tm);
78 wc_printf("<div class=\"mini_calendar\" id=\"%s\">\n", div_id);
80 /* Previous month link */
81 localtime_r(&previous_month, &tm);
82 wc_printf("<a href=\"javascript:minical_change_month(%d,%d);\">«</a>",
83 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
85 wc_strftime(colheader_label, sizeof colheader_label, "%B", &starting_tm);
86 wc_printf(" "
87 "<span class=\"mini_calendar_month_label\">"
90 " ", colheader_label, year);
93 localtime_r(&next_month, &tm);
94 wc_printf("<a href=\"javascript:minical_change_month(%d,%d);\">»</a>",
95 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
97 wc_printf("<table border=\"0\" cellpadding=\"1\" cellspacing=\"1\" class=\"mini_calendar_days\">"
99 colheader_time = thetime;
100 for (i=0; i<7; ++i) {
101 colheader_time = thetime + (i * 86400) ;
102 localtime_r(&colheader_time, &colheader_tm);
103 wc_strftime(colheader_label, sizeof colheader_label, "%A", &colheader_tm);
104 wc_printf("<th>%c</th>", colheader_label[0]);
107 wc_printf("</tr>\n");
110 /* Now do 35 or 42 days */
111 for (i = 0; i < 42; ++i) {
112 localtime_r(&thetime, &tm);
116 /* Before displaying Sunday, start a new row */
121 if (tm.tm_mon == month-1) {
122 snprintf(url, sizeof url, "readfwd?calview=day?year=%d?month=%d?day=%d",
123 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday);
124 wc_printf("<td><a href=\"%s\">%d</a></td>", url, tm.tm_mday);
127 wc_printf("<td> </td>");
130 /* After displaying one week, end the row */
132 wc_printf("</tr>\n");
137 thetime += (time_t)86400; /* ahead 24 hours */
140 wc_printf("</table>" /* end of inner table */
143 StrBufAppendPrintf(WC->trailing_javascript,
144 " function minical_change_month(year, month) { \n"
145 " p = 'year=' + year + '&month=' + month \n"
146 " + '&r=' + CtdlRandomString(); \n"
147 " new Ajax.Updater('%s', 'mini_calendar', \n"
148 " { method: 'get', parameters: p, evalScripts: true } ); \n"
157 * ajax embedder for the above mini calendar
159 void ajax_mini_calendar(void)
161 embeddable_mini_calendar( ibstr("year"), ibstr("month"));
166 * Display one day of a whole month view of a calendar
168 void calendar_month_view_display_events(int year, int month, int day)
175 icalproperty *p = NULL;
176 icalproperty *q = NULL;
177 struct icaltimetype t;
178 struct icaltimetype end_t;
179 struct icaltimetype today_start_t;
180 struct icaltimetype today_end_t;
181 struct icaltimetype today_t;
182 struct tm starting_tm;
184 int all_day_event = 0;
190 if (GetCount(WCC->disp_cal_items) == 0) {
191 wc_printf("<br /><br /><br />\n");
196 * Create an imaginary event which spans the 24 hours of today. Any events which
197 * overlap with this one take place at least partially in this day. We have to
198 * convert it from a struct tm in order to make it UTC.
200 memset(&starting_tm, 0, sizeof(struct tm));
201 starting_tm.tm_year = year - 1900;
202 starting_tm.tm_mon = month - 1;
203 starting_tm.tm_mday = day;
204 starting_tm.tm_hour = 0;
205 starting_tm.tm_min = 0;
206 today_start_t = icaltime_from_timet_with_zone(mktime(&starting_tm), 0, icaltimezone_get_utc_timezone());
207 today_start_t.is_utc = 1;
209 memset(&ending_tm, 0, sizeof(struct tm));
210 ending_tm.tm_year = year - 1900;
211 ending_tm.tm_mon = month - 1;
212 ending_tm.tm_mday = day;
213 ending_tm.tm_hour = 23;
214 ending_tm.tm_min = 59;
215 today_end_t = icaltime_from_timet_with_zone(mktime(&ending_tm), 0, icaltimezone_get_utc_timezone());
216 today_end_t.is_utc = 1;
219 * Create another one without caring about the timezone for all day events.
221 today_t = icaltime_null_date();
223 today_t.month = month;
227 * Now loop through our list of events to see which ones occur today.
229 Pos = GetNewHashPos(WCC->disp_cal_items, 0);
230 while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
231 Cal = (disp_cal*)vCal;
233 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
235 t = icalproperty_get_dtstart(q);
238 memset(&t, 0, sizeof t);
240 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
242 end_t = icalproperty_get_dtend(q);
245 memset(&end_t, 0, sizeof end_t);
247 if (t.is_date) all_day_event = 1;
251 show_event = ical_ctdl_is_overlap(t, end_t, today_t, icaltime_null_time());
255 show_event = ical_ctdl_is_overlap(t, end_t, today_start_t, today_end_t);
259 * If we determined that this event occurs today, then display it.
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 month, day, year;
420 int all_day_event = 0;
424 time_format = get_time_format_cached ();
426 if (time_format == WC_TIMEFORMAT_24) timeformat="%k:%M";
427 else timeformat="%I:%M %p";
429 localtime_r(&thetime, &today_tm);
430 month = today_tm.tm_mon + 1;
431 day = today_tm.tm_mday;
432 year = today_tm.tm_year + 1900;
434 Pos = GetNewHashPos(WCC->disp_cal_items, 0);
435 while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
436 Cal = (disp_cal*)vCal;
437 p = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
439 t = icalproperty_get_dtstart(p);
440 event_tt = icaltime_as_timet(t);
442 if (t.is_date) all_day_event = 1;
443 else all_day_event = 0;
446 gmtime_r(&event_tts, &event_tms);
449 localtime_r(&event_tts, &event_tms);
451 /* \todo epoch &! daymask */
452 if ((event_tms.tm_year == today_tm.tm_year)
453 && (event_tms.tm_mon == today_tm.tm_mon)
454 && (event_tms.tm_mday == today_tm.tm_mday)) {
460 p = icalcomponent_get_first_property(
462 ICAL_SUMMARY_PROPERTY);
464 p = icalproperty_new_summary(_("Untitled Event"));
465 icalcomponent_add_property(Cal->cal, p);
467 e = icalcomponent_get_first_property(
469 ICAL_DTEND_PROPERTY);
470 if ((p != NULL) && (e != NULL)) {
473 t = icalproperty_get_dtend(e);
474 event_tte = icaltime_as_timet(t);
475 localtime_r(&event_tte, &event_tme);
476 difftime=(event_tte-event_tts)/60;
477 hours=(int)(difftime / 60);
478 minutes=difftime % 60;
479 wc_printf("<tr><td bgcolor='%s'>%i:%2i</td><td bgcolor='%s'>"
481 "<a class=\"event%s\" href=\"display_edit_event?msgnum=%ld?calview=calbrief?year=%s?month=%s?day=%s\">",
484 (Cal->unread)?"_unread":"_read",
493 icalproperty_get_comment(p));
494 /* \todo: allso ammitime format */
495 wc_strftime(&sbuf[0], sizeof(sbuf), timeformat, &event_tms);
496 wc_strftime(&ebuf[0], sizeof(sbuf), timeformat, &event_tme);
498 wc_printf("</a></font></td>"
499 "<td bgcolor='%s'>%s</td><td bgcolor='%s'>%s</td></tr>",
516 * view one month. pretty view
518 void calendar_month_view(int year, int month, int day) {
519 struct tm starting_tm;
524 time_t previous_month;
526 time_t colheader_time;
528 struct tm colheader_tm;
529 char colheader_label[32];
533 * Make sure we know which day is today.
535 today_timet = time(NULL);
536 localtime_r(&today_timet, &today_tm);
539 * Determine what day to start. If an impossible value is found, start on Sunday.
541 get_pref_long("weekstart", &weekstart, 17);
542 if (weekstart > 6) weekstart = 0;
545 * Now back up to the 1st of the month...
547 memset(&starting_tm, 0, sizeof(struct tm));
549 starting_tm.tm_year = year - 1900;
550 starting_tm.tm_mon = month - 1;
551 starting_tm.tm_mday = day;
552 thetime = mktime(&starting_tm);
554 memcpy(&tm, &starting_tm, sizeof(struct tm));
555 while (tm.tm_mday != 1) {
556 thetime = thetime - (time_t)86400; /* go back 24 hours */
557 localtime_r(&thetime, &tm);
560 /* Determine previous and next months ... for links */
561 previous_month = thetime - (time_t)864000L; /* back 10 days */
562 next_month = thetime + (time_t)(31L * 86400L); /* ahead 31 days */
564 /* Now back up until we're on the user's preferred start day */
565 localtime_r(&thetime, &tm);
566 while (tm.tm_wday != weekstart) {
567 thetime = thetime - (time_t)86400; /* go back 24 hours */
568 localtime_r(&thetime, &tm);
571 /* Outer table (to get the background color) */
572 wc_printf("<div class=\"fix_scrollbar_bug\">"
573 "<table class=\"calendar\"> \n <tr><td>");
575 wc_printf("<table width=\"100%%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>\n");
577 wc_printf("<td align=\"center\">");
579 localtime_r(&previous_month, &tm);
580 wc_printf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
581 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
582 wc_printf("<img alt=\"%s\" align=\"middle\" src=\"static/prevdate_32x.gif\" border=\"0\"></a>\n", _("previous"));
584 wc_strftime(colheader_label, sizeof colheader_label, "%B", &starting_tm);
585 wc_printf(" "
586 "<font size=\"+1\" color=\"#FFFFFF\">"
589 " ", colheader_label, year);
591 localtime_r(&next_month, &tm);
592 wc_printf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
593 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
594 wc_printf("<img alt=\"%s\" align=\"middle\" src=\"static/nextdate_32x.gif\" border=\"0\"></A>\n", _("next"));
596 wc_printf("</td></tr></table>\n");
598 /* Inner table (the real one) */
599 wc_printf("<table width=\"100%%\" border=\"0\" cellpadding=\"1\" cellspacing=\"1\" "
600 "bgcolor='#204B78' id=\"inner_month\"><tr>");
601 wc_printf("<th align=\"center\" width=\"2%%\"></th>");
602 colheader_time = thetime;
603 for (i=0; i<7; ++i) {
604 colheader_time = thetime + (i * 86400) ;
605 localtime_r(&colheader_time, &colheader_tm);
606 wc_strftime(colheader_label, sizeof colheader_label, "%A", &colheader_tm);
607 wc_printf("<th align=\"center\" width=\"14%%\">"
608 "<font color=\"#FFFFFF\">%s</font></th>", colheader_label);
611 wc_printf("</tr>\n");
614 /* Now do 35 or 42 days */
615 localtime_r(&thetime, &tm);
616 for (i = 0; i<42; ++i) {
618 /* Before displaying the first day of the week, start a new row */
620 wc_printf("<tr><td class=\"week_of_year\">");
621 wc_strftime(colheader_label, sizeof colheader_label, "%V", &tm);
622 wc_printf("%s ", colheader_label);
625 wc_printf("<td class=\"cal%s\"><div class=\"day\">",
626 ((tm.tm_mon != month-1) ? "out" :
627 (((tm.tm_year == today_tm.tm_year) && (tm.tm_mon == today_tm.tm_mon) && (tm.tm_mday == today_tm.tm_mday)) ? "today" :
628 ((tm.tm_wday==0 || tm.tm_wday==6) ? "weekend" :
631 if ((i==0) || (tm.tm_mday == 1)) {
632 wc_strftime(colheader_label, sizeof colheader_label, "%B", &tm);
633 wc_printf("%s ", colheader_label);
635 wc_printf("<a href=\"readfwd?calview=day?year=%d?month=%d?day=%d\">"
642 /* put the data here, stupid */
643 calendar_month_view_display_events(
651 /* After displaying the last day of the week, end the row */
653 wc_printf("</tr>\n");
656 thetime += (time_t)86400; /* ahead 24 hours */
657 localtime_r(&thetime, &tm);
659 if ( ((i % 7) == 6) && (tm.tm_mon != month-1) && (tm.tm_mday < 15) ) {
660 i = 100; /* break out of the loop */
664 wc_printf("</table>" /* end of inner table */
665 "</td></tr></table>" /* end of outer table */
670 * view one month. brief view
672 void calendar_brief_month_view(int year, int month, int day) {
673 struct tm starting_tm;
677 time_t previous_month;
679 char month_label[32];
681 /* Determine what day to start.
682 * First, back up to the 1st of the month...
684 memset(&starting_tm, 0, sizeof(struct tm));
685 starting_tm.tm_year = year - 1900;
686 starting_tm.tm_mon = month - 1;
687 starting_tm.tm_mday = day;
688 thetime = mktime(&starting_tm);
690 memcpy(&tm, &starting_tm, sizeof(struct tm));
691 while (tm.tm_mday != 1) {
692 thetime = thetime - (time_t)86400; /* go back 24 hours */
693 localtime_r(&thetime, &tm);
696 /* Determine previous and next months ... for links */
697 previous_month = thetime - (time_t)864000L; /* back 10 days */
698 next_month = thetime + (time_t)(31L * 86400L); /* ahead 31 days */
700 /* Now back up until we're on a Sunday */
701 localtime_r(&thetime, &tm);
702 while (tm.tm_wday != 0) {
703 thetime = thetime - (time_t)86400; /* go back 24 hours */
704 localtime_r(&thetime, &tm);
707 /* Outer table (to get the background color) */
708 wc_printf("<div class=\"fix_scrollbar_bug\">"
709 "<table width=\"100%%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" "
710 "bgcolor=#204B78><tr><td>\n");
712 wc_printf("<table width=\"100%%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>\n");
714 wc_printf("<td align=\"center\">");
716 localtime_r(&previous_month, &tm);
717 wc_printf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
718 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
719 wc_printf("<img alt=\"%s\" align=\"middle\" src=\"static/prevdate_32x.gif\" border=\"0\"></a>\n", _("previous"));
721 wc_strftime(month_label, sizeof month_label, "%B", &tm);
722 wc_printf(" "
723 "<font size=\"+1\" color=\"#FFFFFF\">"
726 " ", month_label, year);
728 localtime_r(&next_month, &tm);
729 wc_printf("<a href=\"readfwd?calview=month?year=%d?month=%d?day=1\">",
730 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
731 wc_printf("<img alt=\"%s\" align=\"middle\" src=\"static/nextdate_32x.gif\" border=\"0\"></a>\n", _("next"));
733 wc_printf("</td></tr></table>\n");
735 /* Inner table (the real one) */
736 wc_printf("<table width=\"100%%\" border=\"0\" cellpadding=\"1\" cellspacing=\"1\" "
737 "bgcolor=#EEEECC><TR>");
738 wc_printf("</tr>\n");
739 wc_printf("<tr><td colspan=\"100%%\">\n");
742 for (i = 0; i < 35; ++i) {
743 char weeknumber[255];
744 char weekday_name[32];
746 localtime_r(&thetime, &tm);
749 /* Before displaying Sunday, start a new CELL */
751 wc_strftime(&weeknumber[0], sizeof(weeknumber), "%U", &tm);
752 wc_printf("<table border='0' bgcolor=\"#EEEECC\" width='100%%'> <tr><th colspan='4'>%s %s</th></tr>"
753 " <tr><td>%s</td><td width='70%%'>%s</td><td>%s</td><td>%s</td></tr>\n",
763 daycolor=((tm.tm_mon != month-1) ? "DDDDDD" :
764 ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
768 wc_strftime(weekday_name, sizeof weekday_name, "%A", &tm);
769 wc_printf("<tr><td bgcolor='%s' colspan='1' align='left'> %s,%i."
770 "</td><td bgcolor='%s' colspan='3'><hr></td></tr>\n",
772 weekday_name,tm.tm_mday,
775 /* put the data of one day here, stupid */
776 calendar_month_view_brief_events(thetime, daycolor);
779 /* After displaying Saturday, end the row */
781 wc_printf("</td></tr></table>\n");
784 thetime += (time_t)86400; /* ahead 24 hours */
787 wc_printf("</table>" /* end of inner table */
788 "</td></tr></table>" /* end of outer table */
793 * Calendar week view -- not implemented yet, this is a stub function
795 void calendar_week_view(int year, int month, int day) {
796 wc_printf("<center><i>week view FIXME</i></center><br />\n");
802 * Display events for a particular hour of a particular day.
803 * (Specify hour < 0 to show "all day" events)
805 * dstart and dend indicate which hours our "daytime" begins and end
807 void calendar_day_view_display_events(time_t thetime,
819 icalproperty *p = NULL;
820 icalproperty *q = NULL;
826 int all_day_event = 0;
827 int ongoing_event = 0;
830 struct icaltimetype t;
831 struct icaltimetype end_t;
832 struct icaltimetype today_start_t;
833 struct icaltimetype today_end_t;
834 struct icaltimetype today_t;
835 struct tm starting_tm;
846 if (GetCount(WCC->disp_cal_items) == 0) {
847 /* nothing to display */
851 /* Create an imaginary event which spans the current day. Any events which
852 * overlap with this one take place at least partially in this day.
854 memset(&starting_tm, 0, sizeof(struct tm));
855 starting_tm.tm_year = year - 1900;
856 starting_tm.tm_mon = month - 1;
857 starting_tm.tm_mday = day;
858 starting_tm.tm_hour = 0;
859 starting_tm.tm_min = 0;
860 today_start_t = icaltime_from_timet_with_zone(mktime(&starting_tm), 0, icaltimezone_get_utc_timezone());
861 today_start_t.is_utc = 1;
863 memset(&ending_tm, 0, sizeof(struct tm));
864 ending_tm.tm_year = year - 1900;
865 ending_tm.tm_mon = month - 1;
866 ending_tm.tm_mday = day;
867 ending_tm.tm_hour = 23;
868 ending_tm.tm_min = 59;
869 today_end_t = icaltime_from_timet_with_zone(mktime(&ending_tm), 0, icaltimezone_get_utc_timezone());
870 today_end_t.is_utc = 1;
873 * Create another one without caring about the timezone for all day events.
875 today_t = icaltime_null_date();
877 today_t.month = month;
880 /* Now loop through our list of events to see which ones occur today.
882 Pos = GetNewHashPos(WCC->disp_cal_items, 0);
883 while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
884 Cal = (disp_cal*)vCal;
889 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
891 t = icalproperty_get_dtstart(q);
892 event_tt = icaltime_as_timet(t);
893 localtime_r(&event_tt, &event_te);
896 memset(&t, 0, sizeof t);
899 if (t.is_date) all_day_event = 1;
901 q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
903 end_t = icalproperty_get_dtend(q);
906 /* no end given means end = start */
907 memcpy(&end_t, &t, sizeof(struct icaltimetype));
912 show_event = ical_ctdl_is_overlap(t, end_t, today_t, icaltime_null_time());
913 if (icaltime_compare(t, end_t)) {
915 * the end date is non-inclusive so adjust it by one
916 * day because our test is inclusive, note that a day is
917 * not to much because we are talking about all day
920 icaltime_adjust(&end_t, -1, 0, 0, 0);
925 show_event = ical_ctdl_is_overlap(t, end_t, today_start_t, today_end_t);
928 event_tte = icaltime_as_timet(end_t);
929 localtime_r(&event_tte, &event_tm);
931 /* If we determined that this event occurs today, then display it.
933 p = icalcomponent_get_first_property(Cal->cal,ICAL_SUMMARY_PROPERTY);
935 p = icalproperty_new_summary(_("Untitled Event"));
936 icalcomponent_add_property(Cal->cal, p);
939 if ((show_event) && (p != NULL)) {
941 if ((event_te.tm_mday != day) || (event_tm.tm_mday != day)) ongoing_event = 1;
943 if (all_day_event && notime_events)
945 wc_printf("<li class=\"event_framed%s\"> "
946 "<a href=\"display_edit_event?"
947 "msgnum=%ld?calview=day?year=%d?month=%d?day=%d\" "
948 " class=\"event_title\">"
950 (Cal->unread)?"_unread":"_read",
951 Cal->cal_msgnum, year, month, day
953 escputs((char *) icalproperty_get_comment(p));
954 wc_printf("<span class=\"tooltip\"><span class=\"btttop\"></span><span class=\"bttmiddle\">");
955 wc_printf("<i>%s</i><br />", _("All day event"));
956 wc_printf("<i>%s: %s</i><br />", _("From"), Cal->from);
957 wc_printf("<i>%s</i> ", _("Summary:"));
958 escputs((char *) icalproperty_get_comment(p));
960 q = icalcomponent_get_first_property(Cal->cal,ICAL_LOCATION_PROPERTY);
962 wc_printf("<i>%s</i> ", _("Location:"));
963 escputs((char *)icalproperty_get_comment(q));
966 if (!icaltime_compare(t, end_t)) { /* one day only */
967 webcit_fmt_date(buf, 256, event_tt, DATEFMT_LOCALEDATE);
968 wc_printf("<i>%s</i> %s<br>", _("Date:"), buf);
971 webcit_fmt_date(buf, 256, event_tt, DATEFMT_LOCALEDATE);
972 wc_printf("<i>%s</i> %s<br>", _("Starting date:"), buf);
973 webcit_fmt_date(buf, 256, event_tte, DATEFMT_LOCALEDATE);
974 wc_printf("<i>%s</i> %s<br>", _("Ending date:"), buf);
976 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
978 wc_printf("<i>%s</i> ", _("Notes:"));
979 escputs((char *)icalproperty_get_comment(q));
982 wc_printf("</span><span class=\"bttbottom\"></span></span>");
983 wc_printf("</a> <span>(");
984 wc_printf(_("All day event"));
985 wc_printf(")</span></li>\n");
987 else if (ongoing_event && notime_events)
989 wc_printf("<li class=\"event_framed%s\"> "
990 "<a href=\"display_edit_event?"
991 "msgnum=%ld&calview=day?year=%d?month=%d?day=%d\" "
992 " class=\"event_title\">"
994 (Cal->unread)?"_unread":"_read",
995 Cal->cal_msgnum, year, month, day
997 escputs((char *) icalproperty_get_comment(p));
998 wc_printf("<span class=\"tooltip\"><span class=\"btttop\"></span><span class=\"bttmiddle\">");
999 wc_printf("<i>%s</i><br />", _("Ongoing event"));
1000 wc_printf("<i>%s: %s</i><br />", _("From"), Cal->from);
1001 wc_printf("<i>%s</i> ", _("Summary:"));
1002 escputs((char *) icalproperty_get_comment(p));
1003 wc_printf("<br />");
1004 q = icalcomponent_get_first_property(Cal->cal,ICAL_LOCATION_PROPERTY);
1006 wc_printf("<i>%s</i> ", _("Location:"));
1007 escputs((char *)icalproperty_get_comment(q));
1008 wc_printf("<br />");
1010 webcit_fmt_date(buf, 256, event_tt, DATEFMT_BRIEF);
1011 wc_printf("<i>%s</i> %s<br>", _("Starting date/time:"), buf);
1012 webcit_fmt_date(buf, 256, event_tte, DATEFMT_BRIEF);
1013 wc_printf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
1014 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
1016 wc_printf("<i>%s</i> ", _("Notes:"));
1017 escputs((char *)icalproperty_get_comment(q));
1018 wc_printf("<br />");
1020 wc_printf("</span><span class=\"bttbottom\"></span></span>");
1021 wc_printf("</a> <span>(");
1022 wc_printf(_("Ongoing event"));
1023 wc_printf(")</span></li>\n");
1025 else if (!all_day_event && !notime_events)
1029 if (event_te.tm_mday != day) event_te.tm_hour = 0;
1030 if (event_tm.tm_mday != day) event_tm.tm_hour = 24;
1032 /* Calculate the location of the top of the box */
1033 if (event_te.tm_hour < dstart) {
1034 startmin = diffmin = event_te.tm_min / 6;
1035 top = (event_te.tm_hour * EXTRATIMELINE) + startmin;
1037 else if ((event_te.tm_hour >= dstart) && (event_te.tm_hour <= dend)) {
1038 startmin = diffmin = (event_te.tm_min / 2);
1039 top = (dstart * EXTRATIMELINE) + ((event_te.tm_hour - dstart) * TIMELINE) + startmin;
1041 else if (event_te.tm_hour >dend) {
1042 startmin = diffmin = event_te.tm_min / 6;
1043 top = (dstart * EXTRATIMELINE) + ((dend - dstart - 1) * TIMELINE) + ((event_tm.tm_hour - dend + 1) * EXTRATIMELINE) + startmin ;
1046 /* should never get here */
1049 /* Calculate the location of the bottom of the box */
1050 if (event_tm.tm_hour < dstart) {
1051 endmin = diffmin = event_tm.tm_min / 6;
1052 bottom = (event_tm.tm_hour * EXTRATIMELINE) + endmin;
1054 else if ((event_tm.tm_hour >= dstart) && (event_tm.tm_hour <= dend)) {
1055 endmin = diffmin = (event_tm.tm_min / 2);
1056 bottom = (dstart * EXTRATIMELINE) + ((event_tm.tm_hour - dstart) * TIMELINE) + endmin ;
1058 else if (event_tm.tm_hour >dend) {
1059 endmin = diffmin = event_tm.tm_min / 6;
1060 bottom = (dstart * EXTRATIMELINE) + ((dend - dstart + 1) * TIMELINE) + ((event_tm.tm_hour - dend - 1) * EXTRATIMELINE) + endmin;
1063 /* should never get here */
1066 wc_printf("<dd class=\"event_framed%s\" "
1067 "style=\"position: absolute; "
1068 "top:%dpx; left:%dpx; "
1069 "height:%dpx; \" >",
1070 (Cal->unread)?"_unread":"_read",
1071 top, (gap * 40), (bottom-top)
1073 wc_printf("<a href=\"display_edit_event?"
1074 "msgnum=%ld?calview=day?year=%d?month=%d?day=%d?hour=%d\" "
1075 "class=\"event_title\">"
1077 Cal->cal_msgnum, year, month, day, t.hour
1079 escputs((char *) icalproperty_get_comment(p));
1080 wc_printf("<span class=\"tooltip\"><span class=\"btttop\"></span><span class=\"bttmiddle\">");
1081 wc_printf("<i>%s: %s</i><br />", _("From"), Cal->from);
1082 wc_printf("<i>%s</i> ", _("Summary:"));
1083 escputs((char *) icalproperty_get_comment(p));
1084 wc_printf("<br />");
1085 q = icalcomponent_get_first_property(Cal->cal,ICAL_LOCATION_PROPERTY);
1087 wc_printf("<i>%s</i> ", _("Location:"));
1088 escputs((char *)icalproperty_get_comment(q));
1089 wc_printf("<br />");
1091 if (!icaltime_compare(t, end_t)) { /* one day only */
1092 webcit_fmt_date(buf, 256, event_tt, DATEFMT_BRIEF);
1093 wc_printf("<i>%s</i> %s<br>", _("Date/time:"), buf);
1096 webcit_fmt_date(buf, 256, event_tt, DATEFMT_BRIEF);
1097 wc_printf("<i>%s</i> %s<br>", _("Starting date/time:"), buf);
1098 webcit_fmt_date(buf, 256, event_tte, DATEFMT_BRIEF);
1099 wc_printf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
1101 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
1103 wc_printf("<i>%s</i> ", _("Notes:"));
1104 escputs((char *)icalproperty_get_comment(q));
1105 wc_printf("<br />");
1107 wc_printf("</span><span class=\"bttbottom\"></span></span>");
1108 wc_printf("</a></dd>\n");
1112 DeleteHashPos(&Pos);
1118 void calendar_day_view(int year, int month, int day) {
1120 struct icaltimetype today, yesterday, tomorrow;
1127 int timeline = TIMELINE;
1128 int extratimeline = EXTRATIMELINE;
1133 time_format = get_time_format_cached ();
1134 get_pref_long("daystart", &daystart, 8);
1135 get_pref_long("dayend", &dayend, 17);
1137 /* when loading daystart/dayend, replace missing, corrupt, or impossible values with defaults */
1138 if ((daystart < 0) || (dayend < 2) || (daystart >= 23) || (dayend > 23) || (dayend <= daystart)) {
1144 memset(&d_tm, 0, sizeof d_tm);
1145 d_tm.tm_year = year - 1900;
1146 d_tm.tm_mon = month - 1;
1148 today_t = mktime(&d_tm);
1150 /* Figure out the dates for "yesterday" and "tomorrow" links */
1152 memset(&today, 0, sizeof(struct icaltimetype));
1154 today.month = month;
1158 memcpy(&yesterday, &today, sizeof(struct icaltimetype));
1160 yesterday = icaltime_normalize(yesterday);
1162 memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
1164 tomorrow = icaltime_normalize(tomorrow);
1166 wc_printf("<div class=\"fix_scrollbar_bug\">");
1168 /* Inner table (the real one) */
1169 wc_printf("<table class=\"calendar\" id=\"inner_day\"><tr> \n");
1171 /* Innermost cell (contains hours etc.) */
1172 wc_printf("<td class=\"events_of_the_day\" >");
1173 wc_printf("<dl class=\"events\" >");
1175 /* Now the middle of the day... */
1177 extrahourlabel = extratimeline - 2;
1178 hourlabel = extrahourlabel * 150 / 100;
1179 if (hourlabel > (timeline - 2)) hourlabel = timeline - 2;
1181 for (hour = 0; hour < daystart; ++hour) { /* could do HEIGHT=xx */
1182 wc_printf("<dt class=\"extrahour\" "
1184 "position: absolute; "
1185 "top: %dpx; left: 0px; "
1189 "<a href=\"display_edit_event?msgnum=0"
1190 "?calview=day?year=%d?month=%d?day=%d?hour=%d?minute=0\">",
1191 (hour * extratimeline ),
1194 year, month, day, hour
1197 if (time_format == WC_TIMEFORMAT_24) {
1198 wc_printf("%2d:00</a> ", hour);
1201 wc_printf("%d:00%s</a> ",
1202 ((hour == 0) ? 12 : (hour <= 12 ? hour : hour-12)),
1203 (hour < 12 ? "am" : "pm")
1210 gap = daystart * extratimeline;
1212 for (hour = daystart; hour <= dayend; ++hour) { /* could do HEIGHT=xx */
1213 wc_printf("<dt class=\"hour\" "
1215 "position: absolute; "
1216 "top: %ldpx; left: 0px; "
1220 "<a href=\"display_edit_event?msgnum=0?calview=day"
1221 "?year=%d?month=%d?day=%d?hour=%d?minute=0\">",
1222 gap + ((hour - daystart) * timeline ),
1225 year, month, day, hour
1228 if (time_format == WC_TIMEFORMAT_24) {
1229 wc_printf("%2d:00</a> ", hour);
1232 wc_printf("%d:00%s</a> ",
1233 (hour <= 12 ? hour : hour-12),
1234 (hour < 12 ? "am" : "pm")
1241 gap = gap + ((dayend - daystart + 1) * timeline);
1243 for (hour = (dayend + 1); hour < 24; ++hour) { /* could do HEIGHT=xx */
1244 wc_printf("<dt class=\"extrahour\" "
1246 "position: absolute; "
1247 "top: %ldpx; left: 0px; "
1251 "<a href=\"display_edit_event?msgnum=0?calview=day"
1252 "?year=%d?month=%d?day=%d?hour=%d?minute=0\">",
1253 gap + ((hour - dayend - 1) * extratimeline ),
1256 year, month, day, hour
1259 if (time_format == WC_TIMEFORMAT_24) {
1260 wc_printf("%2d:00</a> ", hour);
1263 wc_printf("%d:00%s</a> ",
1264 (hour <= 12 ? hour : hour-12),
1265 (hour < 12 ? "am" : "pm")
1272 /* Display events with start and end times on this day */
1273 calendar_day_view_display_events(today_t, year, month, day, 0, daystart, dayend);
1276 wc_printf("</td>"); /* end of innermost table */
1278 /* Display extra events (start/end times not present or not today) in the middle column */
1279 wc_printf("<td class=\"extra_events\">");
1283 /* Display all-day events */
1284 calendar_day_view_display_events(today_t, year, month, day, 1, daystart, dayend);
1288 wc_printf("</td>"); /* end extra on the middle */
1290 wc_printf("<td width='20%%' align=\"center\" valign=top>"); /* begin stuff-on-the-right */
1292 /* Begin todays-date-with-left-and-right-arrows */
1293 wc_printf("<table border=\"0\" width=\"100%%\" "
1294 "cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#FFFFFF\">\n");
1298 wc_printf("<td align=\"center\">");
1299 wc_printf("<a href=\"readfwd?calview=day?year=%d?month=%d?day=%d\">",
1300 yesterday.year, yesterday.month, yesterday.day);
1301 wc_printf("<img alt=\"previous\" align=\"middle\" src=\"static/prevdate_32x.gif\" border=\"0\"></a>");
1304 wc_strftime(d_str, sizeof d_str,
1305 "<td align=\"center\">"
1306 "<font size='+2'>%A</font><br />"
1307 "<font size='+2'>%B</font><br />"
1308 "<font size='+3'>%d</font><br />"
1309 "<font size='+2'>%Y</font><br />"
1313 wc_printf("%s", d_str);
1316 wc_printf("<td align=\"center\">");
1317 wc_printf("<a href=\"readfwd?calview=day?year=%d?month=%d?day=%d\">",
1318 tomorrow.year, tomorrow.month, tomorrow.day);
1319 wc_printf("<img alt=\"%s\" align=\"middle\" src=\"static/nextdate_32x.gif\""
1320 " border=\"0\"></a>\n", _("next"));
1323 wc_printf("</tr></table>\n");
1324 /* End todays-date-with-left-and-right-arrows */
1326 /* Embed a mini month calendar in this space */
1327 wc_printf("<br />\n");
1328 embeddable_mini_calendar(year, month);
1330 wc_printf("</td></tr>"); /* end stuff-on-the-right */
1332 wc_printf("</table>" /* end of inner table */
1338 * Display today's events. Returns the number of items displayed.
1340 int calendar_summary_view(void) {
1342 const char *HashKey;
1347 struct icaltimetype t;
1352 int all_day_event = 0;
1353 char timestring[SIZ];
1354 wcsession *WCC = WC;
1355 int num_displayed = 0;
1357 if (GetCount(WC->disp_cal_items) == 0) {
1362 localtime_r(&now, &today_tm);
1364 Pos = GetNewHashPos(WCC->disp_cal_items, 0);
1365 while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
1366 Cal = (disp_cal*)vCal;
1367 p = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
1369 t = icalproperty_get_dtstart(p);
1370 event_tt = icaltime_as_timet(t);
1377 fmt_time(timestring, SIZ, event_tt);
1379 if (all_day_event) {
1380 gmtime_r(&event_tt, &event_tm);
1383 localtime_r(&event_tt, &event_tm);
1386 if ( (event_tm.tm_year == today_tm.tm_year)
1387 && (event_tm.tm_mon == today_tm.tm_mon)
1388 && (event_tm.tm_mday == today_tm.tm_mday)
1391 p = icalcomponent_get_first_property(Cal->cal, ICAL_SUMMARY_PROPERTY);
1393 p = icalproperty_new_summary(_("Untitled Task"));
1394 icalcomponent_add_property(Cal->cal, p);
1398 if (WCC->CurRoom.view == VIEW_TASKS) {
1399 wc_printf("<a href=\"display_edit_task"
1401 "?return_to_summary=1"
1405 escputs(ChrPtr(WCC->CurRoom.name));
1409 wc_printf("<a href=\"display_edit_event"
1417 today_tm.tm_year + 1900,
1418 today_tm.tm_mon + 1,
1421 escputs(ChrPtr(WCC->CurRoom.name));
1424 escputs((char *) icalproperty_get_comment(p));
1425 if (!all_day_event) {
1426 wc_printf(" (%s)", timestring);
1428 wc_printf("</a><br />\n");
1434 DeleteHashPos(&Pos);
1435 DeleteHash(&WC->disp_cal_items);
1436 return(num_displayed);
1440 * Parse the URL variables in order to determine the scope and display of a calendar view
1442 int calendar_GetParamsGetServerCall(SharedMessageStatus *Stat,
1443 void **ViewSpecific,
1448 wcsession *WCC = WC;
1456 c = (calview*) malloc(sizeof(calview));
1457 memset(c, 0, sizeof(calview));
1458 *ViewSpecific = (void*)c;
1460 Stat->load_seen = 1;
1461 strcpy(cmd, "MSGS ALL");
1462 Stat->maxmsgs = 32767;
1464 /* In case no date was specified, go with today */
1466 localtime_r(&now, &tm);
1467 c->year = tm.tm_year + 1900;
1468 c->month = tm.tm_mon + 1;
1469 c->day = tm.tm_mday;
1471 /* Now see if a date was specified */
1472 if (havebstr("year")) c->year = ibstr("year");
1473 if (havebstr("month")) c->month = ibstr("month");
1474 if (havebstr("day")) c->day = ibstr("day");
1476 /* How would you like that cooked? */
1477 if (havebstr("calview")) {
1478 strcpy(cv, bstr("calview"));
1481 strcpy(cv, "month");
1484 /* Display the selected view */
1485 if (!strcasecmp(cv, "day")) {
1486 c->view = calview_day;
1488 else if (!strcasecmp(cv, "week")) {
1489 c->view = calview_week;
1491 else if (!strcasecmp(cv, "summary")) { /* shouldn't ever happen, but just in case */
1492 c->view = calview_day;
1495 if (WCC->CurRoom.view == VIEW_CALBRIEF) {
1496 c->view = calview_brief;
1499 c->view = calview_month;
1503 /* Now try and set the lower and upper bounds so that we don't
1504 * burn too many cpu cycles parsing data way in the past or future
1507 tm.tm_year = c->year - 1900;
1508 tm.tm_mon = c->month - 1;
1509 tm.tm_mday = c->day;
1512 if (c->view == calview_month) span = 3888000;
1513 if (c->view == calview_brief) span = 3888000;
1514 if (c->view == calview_week) span = 604800;
1515 if (c->view == calview_day) span = 86400;
1516 if (c->view == calview_summary) span = 86400;
1518 c->lower_bound = now - span;
1519 c->upper_bound = now + span;
1526 * Render a calendar view from data previously loaded into memory
1528 int calendar_RenderView_or_Tail(SharedMessageStatus *Stat,
1529 void **ViewSpecific,
1532 wcsession *WCC = WC;
1533 calview *c = (calview*) *ViewSpecific;
1535 if (c->view == calview_day) {
1536 calendar_day_view(c->year, c->month, c->day);
1538 else if (c->view == calview_week) {
1539 calendar_week_view(c->year, c->month, c->day);
1542 if (WCC->CurRoom.view == VIEW_CALBRIEF) {
1543 calendar_brief_month_view(c->year, c->month, c->day);
1546 calendar_month_view(c->year, c->month, c->day);
1550 /* Free the in-memory list of calendar items */
1551 DeleteHash(&WC->disp_cal_items);