From 385e86f4a8694462b1f723de6aca93c7dbd9e623 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Sat, 13 Oct 2007 05:00:06 +0000 Subject: [PATCH] Rewrote the multi day event code using only the libical API and no time_t/localtime stuff. --- webcit/calendar.c | 72 ++------------ webcit/calendar_view.c | 213 +++++++++++++++++++++++++---------------- webcit/fmt_date.c | 13 +-- webcit/ical_dezonify.c | 1 - webcit/webcit.h | 12 +-- 5 files changed, 145 insertions(+), 166 deletions(-) diff --git a/webcit/calendar.c b/webcit/calendar.c index f6f1251c3..92a90cd5b 100644 --- a/webcit/calendar.c +++ b/webcit/calendar.c @@ -444,75 +444,15 @@ void handle_rsvp(void) { * \param cal Our calendar to process * \param msgnum number of the mesage in our db */ -void display_individual_cal(icalcomponent *cal, long msgnum) +void display_individual_cal(icalcomponent *cal, long msgnum) { - icalproperty *ps = NULL; - struct icaltimetype t; - struct wcsession *WCC; - struct disp_cal *Cal; - struct tm event; - struct tm event_hr; - time_t event_ts; - - WCC = WC; + struct wcsession *WCC = WC; /* stack this for faster access (WC is a function) */ WCC->num_cal += 1; - - WCC->disp_cal = realloc(WCC->disp_cal, - (sizeof(struct disp_cal) * WCC->num_cal) ); - - Cal = &WCC->disp_cal[WCC->num_cal - 1]; - Cal->cal = icalcomponent_new_clone(cal); - - Cal->cal_msgnum = msgnum; - - //! Precalculate some Values we can use for easy comparison later. - ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY); - if (ps != NULL) { - t = icalproperty_get_dtstart(ps); - event_ts = icaltime_as_timet(t); - - if (t.is_date) { //! calculate whether we are a day event. - Cal->start_hour = -1; - Cal->end_hour = -1; - Cal->end_day = -1; - localtime_r(&event_ts, &event); - event.tm_sec = 0; - event.tm_min = 0; - event.tm_hour = 0; - Cal->start_day = mktime (&event); - } - else { //! Precalc start day and start day + hour - localtime_r(&event_ts, &event); - event.tm_sec = 0; - event.tm_min = 0; - memcpy (&event_hr, &event, sizeof(struct tm)); - Cal->start_hour = mktime (&event_hr); - event.tm_hour = 0; - Cal->start_day = mktime (&event); - - ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY); - if (ps != NULL) { //! Precalc the end day and end day + hour - t = icalproperty_get_dtstart(ps); - event_ts = icaltime_as_timet(t); - localtime_r(&event_ts, &event); - event.tm_sec = 0; - event.tm_min = 0; - memcpy (&event_hr, &event, sizeof(struct tm)); - Cal->end_hour = mktime (&event); - event.tm_hour = 0; - Cal->end_day = mktime (&event); - } - else - { - Cal->end_hour = -1; - Cal->end_day = -1; - } - } - - - } - Cal->multi_day_event = Cal->start_day != Cal->end_day; + WCC->disp_cal = realloc(WC->disp_cal, (sizeof(struct disp_cal) * WCC->num_cal) ); + WCC->disp_cal[WCC->num_cal - 1].cal = icalcomponent_new_clone(cal); + ical_dezonify(WCC->disp_cal[WCC->num_cal - 1].cal); + WCC->disp_cal[WCC->num_cal - 1].cal_msgnum = msgnum; } diff --git a/webcit/calendar_view.c b/webcit/calendar_view.c index caf0e3ca7..6393aedaf 100644 --- a/webcit/calendar_view.c +++ b/webcit/calendar_view.c @@ -17,64 +17,77 @@ * \brief Display one day of a whole month view of a calendar * \param thetime the month we want to see */ -void calendar_month_view_display_events(time_t thetime) { +void calendar_month_view_display_events(int year, int month, int day) +{ int i; - struct tm today_tm; icalproperty *p = NULL; icalproperty *q = NULL; struct icaltimetype t; - int month, day, year; + struct icaltimetype end_t; + struct icaltimetype today_start_t; + struct icaltimetype today_end_t; int all_day_event = 0; - int multi_day_event = 0; - int fill_day_event = 0; int show_event = 0; - time_t tt; - time_t dst_day; char buf[256]; - struct wcsession *WCC; + struct wcsession *WCC = WC; /* This is done to make it run faster; WC is a function */ struct disp_cal *Cal; - - WCC = WC; + time_t tt; if (WCC->num_cal == 0) { wprintf("


\n"); return; } - localtime_r(&thetime, &today_tm); - month = today_tm.tm_mon + 1; - day = today_tm.tm_mday; - year = today_tm.tm_year + 1900; - dst_day = thetime - 3600; + /* Create an imaginary event which spans the 24 hours of today. Any events which + * overlap with this one take place at least partially in this day. + */ + memset(&today_start_t, 0, sizeof today_start_t); + today_start_t.year = year; + today_start_t.month = month; + today_start_t.day = day; + today_start_t.hour = 0; + today_start_t.minute = 0; + memset(&today_end_t, 0, sizeof today_end_t); + today_end_t.year = year; + today_end_t.month = month; + today_end_t.day = day; + today_end_t.hour = 23; + today_end_t.minute = 59; + + /* Now loop through our list of events to see which ones occur today. + */ for (i=0; i<(WCC->num_cal); ++i) { - fill_day_event = 0; - multi_day_event = 0; - Cal = &WCC->disp_cal[i]; - all_day_event = Cal->start_hour == -1; -// lprintf(1,"Date: %d %d %d %d\n", thetime, Cal->start_day, day, thetime - Cal->start_day); - if (!all_day_event) { - - // are we in the range of the event? - show_event = (Cal->start_day <= thetime) && - (Cal->end_day >= thetime); - if (!show_event) - // are we in the range of the event? - show_event = (Cal->start_day <= dst_day) && - (Cal->end_day >= dst_day); - - // are we not start or end day? - fill_day_event = (Cal->start_day < thetime) && - (Cal->end_day > thetime); + all_day_event = 0; + q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY); + if (q != NULL) { + t = icalproperty_get_dtstart(q); } - else + else { + memset(&t, 0, sizeof t); + } + q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY); + if (q != NULL) { + end_t = icalproperty_get_dtend(q); + } + else { + memset(&end_t, 0, sizeof end_t); + } + if (t.is_date) all_day_event = 1; + + if (all_day_event) { - show_event = (thetime == Cal->start_day) || (dst_day == Cal->start_day); + show_event = ((t.year == year) && (t.month == month) && (t.day == day)); } + else + { + show_event = ical_ctdl_is_overlap(t, end_t, today_start_t, today_end_t); + } + + /* If we determined that this event occurs today, then display it. + */ if (show_event) { - p = icalcomponent_get_first_property( - Cal->cal, - ICAL_SUMMARY_PROPERTY); + p = icalcomponent_get_first_property(Cal->cal, ICAL_SUMMARY_PROPERTY); if (p != NULL) { if (all_day_event) { @@ -113,8 +126,7 @@ void calendar_month_view_display_events(time_t thetime) { */ if (icalcomponent_isa(Cal->cal) == ICAL_VEVENT_COMPONENT) { - q = icalcomponent_get_first_property(Cal->cal, - ICAL_DTSTART_PROPERTY); + q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY); if (q != NULL) { t = icalproperty_get_dtstart(q); @@ -138,14 +150,12 @@ void calendar_month_view_display_events(time_t thetime) { /* Embed the 'show end date/time' loop inside here so it * only executes if this is NOT an all day event. */ - q = icalcomponent_get_first_property(Cal->cal, - ICAL_DTEND_PROPERTY); + q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY); if (q != NULL) { t = icalproperty_get_dtend(q); tt = icaltime_as_timet(t); fmt_date(buf, tt, 1); - wprintf("%s %s
", - _("Ending date/time:"), buf); + wprintf("%s %s
", _("Ending date/time:"), buf); } } @@ -153,9 +163,7 @@ void calendar_month_view_display_events(time_t thetime) { } - q = icalcomponent_get_first_property( - Cal->cal, - ICAL_DESCRIPTION_PROPERTY); + q = icalcomponent_get_first_property(Cal->cal, ICAL_DESCRIPTION_PROPERTY); if (q) { wprintf("%s ", _("Notes:")); escputs((char *)icalproperty_get_comment(q)); @@ -415,7 +423,11 @@ void calendar_month_view(int year, int month, int day) { tm.tm_mday); /** put the data here, stupid */ - calendar_month_view_display_events(thetime); + calendar_month_view_display_events( + tm.tm_year + 1900, + tm.tm_mon + 1, + tm.tm_mday + ); wprintf(""); @@ -604,17 +616,21 @@ void calendar_day_view_display_events(time_t thetime, int year, int month, int day, int hour, int dstart, int dend) { int i; - icalproperty *p; + icalproperty *p = NULL; + icalproperty *q = NULL; time_t event_start; time_t event_end; struct tm event_te; struct tm event_tm; int show_event = 0; int all_day_event = 0; - struct wcsession *WCC; + struct wcsession *WCC = WC; /* This is done to make it run faster; WC is a function */ struct disp_cal *Cal; + struct icaltimetype t; + struct icaltimetype end_t; + struct icaltimetype today_start_t; + struct icaltimetype today_end_t; - WCC = WC; if (WCC->num_cal == 0) { // \todo FIXME wprintf("


\n"); @@ -624,43 +640,79 @@ void calendar_day_view_display_events(time_t thetime, int year, int month, event_start = thetime + 60 * 60 * hour; event_end = thetime + 60 * 60 * (hour + 1); + + /* Create an imaginary event which spans the 24 hours of today. Any events which + * overlap with this one take place at least partially in this day. + */ + memset(&today_start_t, 0, sizeof today_start_t); + today_start_t.year = year; + today_start_t.month = month; + today_start_t.day = day; + today_start_t.hour = 0; + today_start_t.minute = 0; + memset(&today_end_t, 0, sizeof today_end_t); + today_end_t.year = year; + today_end_t.month = month; + today_end_t.day = day; + today_end_t.hour = 23; + today_end_t.minute = 59; + + + /* Now loop through our list of events to see which ones occur today. + */ for (i=0; i<(WCC->num_cal); ++i) { Cal = &WCC->disp_cal[i]; - all_day_event = Cal->start_hour == -1; - show_event = 0; - if (! all_day_event ) - { - show_event = (Cal->start_day == thetime) && - (Cal->start_hour >= event_start) && - (Cal->start_hour < (event_start + 1)); + all_day_event = 0; + q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY); + if (q != NULL) { + t = icalproperty_get_dtstart(q); + } + else { + memset(&t, 0, sizeof t); + } + q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY); + if (q != NULL) { + end_t = icalproperty_get_dtend(q); + } + else { + memset(&end_t, 0, sizeof end_t); + } + if (t.is_date) all_day_event = 1; + if (all_day_event) + { + show_event = ((t.year == year) && (t.month == month) && (t.day == day)); } else { - show_event = (Cal->start_day == thetime); + show_event = ical_ctdl_is_overlap(t, end_t, today_start_t, today_end_t); } + /* If we determined that this event occurs today, then display it. + */ p = icalcomponent_get_first_property(Cal->cal,ICAL_SUMMARY_PROPERTY); if ((show_event) && (p != NULL)) { - if (all_day_event && (hour == -1)) { - wprintf("
", - Cal->cal_msgnum, year, month, day, hour); - escputs((char *) icalproperty_get_comment(p)); - wprintf("
\n"); - } - if (!all_day_event) { - wprintf("
", - (100 + (event_tm.tm_min / 2) + (event_te.tm_hour - hour) + (hour * 30) - (dstart * 30)), - (((event_te.tm_min - event_tm.tm_min) / 2) +(event_te.tm_hour - hour) * 30) - ); - wprintf("", - Cal->cal_msgnum, year, month, day, hour); - escputs((char *) icalproperty_get_comment(p)); - wprintf("
\n"); + if (all_day_event) + { + wprintf("
", + Cal->cal_msgnum, year, month, day, hour); + escputs((char *) icalproperty_get_comment(p)); + wprintf("
\n"); + } + else + { + wprintf("
", + (100 + (event_tm.tm_min / 2) + (event_te.tm_hour - hour) + (hour * 30) - (dstart * 30)), + (((event_te.tm_min - event_tm.tm_min) / 2) +(event_te.tm_hour - hour) * 30) + ); + wprintf("", + Cal->cal_msgnum, year, month, day, hour); + escputs((char *) icalproperty_get_comment(p)); + wprintf("
\n"); } } } @@ -682,7 +734,7 @@ void calendar_day_view(int year, int month, int day) { char d_str[128]; int time_format; time_t today_t; - + time_format = get_time_format_cached (); get_preference("daystart", daystart_str, sizeof daystart_str); if (!IsEmptyStr(daystart_str)) daystart = atoi(daystart_str); @@ -691,9 +743,6 @@ void calendar_day_view(int year, int month, int day) { /** Today's date */ memset(&d_tm, 0, sizeof d_tm); - if (WC->num_cal > 0) - localtime_r(&WC->disp_cal[0].start_day, &d_tm); - d_tm.tm_year = year - 1900; d_tm.tm_mon = month - 1; d_tm.tm_mday = day; @@ -813,9 +862,9 @@ void calendar_day_view(int year, int month, int day) { " setTimeout(\"btt_enableTooltips('inner_day')\", 1); " "\n" ); +} -} /** * \brief Display today's events. */ diff --git a/webcit/fmt_date.c b/webcit/fmt_date.c index 2b66b0e3b..198e8ff60 100644 --- a/webcit/fmt_date.c +++ b/webcit/fmt_date.c @@ -49,21 +49,13 @@ void fmt_date(char *buf, time_t thetime, int brief) struct tm tm; struct tm today_tm; time_t today_timet; - int hour; int time_format; time_format = get_time_format_cached (); today_timet = time(NULL); localtime_r(&today_timet, &today_tm); - localtime_r(&thetime, &tm);/* - hour = tm.tm_hour; - if (hour == 0) - hour = 12; - else if (hour > 12) - hour = hour - 12; - */ - buf[0] = 0; + localtime_r(&thetime, &tm); if (brief) { @@ -76,8 +68,7 @@ void fmt_date(char *buf, time_t thetime, int brief) else wc_strftime(buf, 32, "%l:%M%p", &tm); } - /** Otherwise, for messages up to 6 months old, show the - * month and day, and the time */ + /** Otherwise, for messages up to 6 months old, show the month and day, and the time */ else if (today_timet - thetime < 15552000) { if (time_format == WC_TIMEFORMAT_24) wc_strftime(buf, 32, "%b %d %k:%M", &tm); diff --git a/webcit/ical_dezonify.c b/webcit/ical_dezonify.c index aa21dfe95..f48051eb5 100644 --- a/webcit/ical_dezonify.c +++ b/webcit/ical_dezonify.c @@ -25,7 +25,6 @@ * Figure out which time zone needs to be used for timestamps that are * not UTC and do not have a time zone specified. * - * FIXME - most sites are not in New York :) */ icaltimezone *get_default_icaltimezone(void) { diff --git a/webcit/webcit.h b/webcit/webcit.h index 73bd8d184..75376fef9 100644 --- a/webcit/webcit.h +++ b/webcit/webcit.h @@ -373,12 +373,6 @@ struct wcsession { struct disp_cal { icalcomponent *cal; /**< cal items for display */ long cal_msgnum; /**< cal msgids for display */ - /* to speed up processing we pre-calc some values here. */ - time_t start_day; /**< which day do we start */ - time_t start_hour; /**< which day + hour do we start */ - time_t end_day; /**< which day do we end */ - time_t end_hour; /**< which day + hour do we end? */ - int multi_day_event; /**< do we span several days? */ } *disp_cal; int num_cal; /**< number of calendar items for display */ #endif @@ -687,6 +681,12 @@ void partstat_as_string(char *buf, icalproperty *attendee); icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp); void check_attendee_availability(icalcomponent *supplied_vevent); void do_freebusy(char *req); +int ical_ctdl_is_overlap( + struct icaltimetype t1start, + struct icaltimetype t1end, + struct icaltimetype t2start, + struct icaltimetype t2end +); #endif #ifdef ENABLE_NLS -- 2.30.2