[evolution-data-server/wip/offline-cache] Correct ECalCache time-related issues



commit c00861413563799cf2119f5c2a26efcc44e8ac9d
Author: Milan Crha <mcrha redhat com>
Date:   Wed May 17 15:23:56 2017 +0200

    Correct ECalCache time-related issues
    
    Different libical behaves differently, and the previous code had been
    tested on a "broken" libical (their issue #287 fixes it).

 src/calendar/libecal/e-cal-util.c          |   18 +++++--
 src/calendar/libedata-cal/e-cal-cache.c    |   69 ++++++++++++++++-----------
 tests/libedata-cal/components/event-9.ics  |    4 +-
 tests/libedata-cal/test-cal-cache-search.c |   14 +++---
 4 files changed, 63 insertions(+), 42 deletions(-)
---
diff --git a/src/calendar/libecal/e-cal-util.c b/src/calendar/libecal/e-cal-util.c
index 653f03b..a9426f8 100644
--- a/src/calendar/libecal/e-cal-util.c
+++ b/src/calendar/libecal/e-cal-util.c
@@ -1496,7 +1496,6 @@ componenttime_to_utc_timet (const ECalComponentDateTime *dt_time,
                if (dt_time->tzid)
                        zone = tz_cb (dt_time->tzid, tz_cb_data);
 
-               // zone = icaltimezone_get_utc_timezone ();
                timet = icaltime_as_timet_with_zone (
                        *dt_time->value, zone ? zone : default_zone);
        }
@@ -1530,6 +1529,7 @@ e_cal_util_get_component_occur_times (ECalComponent *comp,
 {
        struct icalrecurrencetype ir;
        ECalComponentDateTime dt_start, dt_end;
+       time_t duration;
 
        g_return_if_fail (comp != NULL);
        g_return_if_fail (start != NULL);
@@ -1545,6 +1545,14 @@ e_cal_util_get_component_occur_times (ECalComponent *comp,
 
        e_cal_component_free_datetime (&dt_start);
 
+       e_cal_component_get_dtend (comp, &dt_end);
+       duration = componenttime_to_utc_timet (&dt_end, tz_cb, tz_cb_data, default_timezone);
+       if (duration <= 0 || *start == _TIME_MIN || *start > duration)
+               duration = 0;
+       else
+               duration = duration - *start;
+       e_cal_component_free_datetime (&dt_end);
+
        /* find out end date of component */
        *end = _TIME_MAX;
 
@@ -1603,8 +1611,8 @@ e_cal_util_get_component_occur_times (ECalComponent *comp,
 
                                if (rule_end == -1) /* repeats forever */
                                        may_end = _TIME_MAX;
-                               else if (rule_end > may_end) /* new maximum */
-                                       may_end = rule_end;
+                               else if (rule_end + duration > may_end) /* new maximum */
+                                       may_end = rule_end + duration;
                        }
 
                        /* Do the EXRULEs. */
@@ -1620,8 +1628,8 @@ e_cal_util_get_component_occur_times (ECalComponent *comp,
 
                                if (rule_end == -1) /* repeats forever */
                                        may_end = _TIME_MAX;
-                               else if (rule_end > may_end)
-                                       may_end = rule_end;
+                               else if (rule_end + duration > may_end)
+                                       may_end = rule_end + duration;
                        }
 
                        /* Do the RDATEs */
diff --git a/src/calendar/libedata-cal/e-cal-cache.c b/src/calendar/libedata-cal/e-cal-cache.c
index 24783bf..fd97743 100644
--- a/src/calendar/libedata-cal/e-cal-cache.c
+++ b/src/calendar/libedata-cal/e-cal-cache.c
@@ -476,6 +476,32 @@ ecc_decode_id_sql (const gchar *id,
        return TRUE;
 }
 
+static icaltimezone *
+ecc_resolve_tzid_cb (const gchar *tzid,
+                    gpointer user_data)
+{
+       ECalCache *cal_cache = user_data;
+       icaltimezone *zone = NULL;
+
+       g_return_val_if_fail (E_IS_CAL_CACHE (cal_cache), NULL);
+
+       if (e_cal_cache_get_timezone (cal_cache, tzid, &zone, NULL, NULL) && zone)
+               return zone;
+
+       zone = icaltimezone_get_builtin_timezone (tzid);
+       if (!zone)
+               zone = icaltimezone_get_builtin_timezone_from_tzid (tzid);
+       if (!zone) {
+               tzid = e_cal_match_tzid (tzid);
+               zone = icaltimezone_get_builtin_timezone (tzid);
+       }
+
+       if (!zone)
+               zone = icaltimezone_get_builtin_timezone_from_tzid (tzid);
+
+       return zone;
+}
+
 static gchar *
 ecc_encode_itt_to_sql (struct icaltimetype itt)
 {
@@ -491,13 +517,11 @@ ecc_encode_time_to_sql (ECalCache *cal_cache,
        struct icaltimetype itt;
        icaltimezone *zone = NULL;
 
-       if (!dt || !dt->value)
+       if (!dt || !dt->value || (!dt->value->is_utc && (!dt->tzid || !*dt->tzid)))
                return NULL;
 
        itt = *dt->value;
-
-       if (!e_cal_cache_get_timezone (cal_cache, dt->tzid, &zone, NULL, NULL))
-               zone = NULL;
+       zone = ecc_resolve_tzid_cb (dt->tzid, cal_cache);
 
        icaltimezone_convert_time (&itt, zone, icaltimezone_get_utc_timezone ());
 
@@ -513,26 +537,11 @@ ecc_encode_timet_to_sql (ECalCache *cal_cache,
        if (tt <= 0)
                return NULL;
 
-       itt = icaltime_from_timet_with_zone (tt, FALSE, NULL);
+       itt = icaltime_from_timet_with_zone (tt, FALSE, icaltimezone_get_utc_timezone ());
 
        return ecc_encode_itt_to_sql (itt);
 }
 
-static icaltimezone *
-ecc_resolve_tzid_cb (const gchar *tzid,
-                    gpointer user_data)
-{
-       ECalCache *cal_cache = user_data;
-       icaltimezone *zone = NULL;
-
-       g_return_val_if_fail (E_IS_CAL_CACHE (cal_cache), NULL);
-
-       if (!e_cal_cache_get_timezone (cal_cache, tzid, &zone, NULL, NULL))
-               return NULL;
-
-       return zone;
-}
-
 static gchar *
 ecc_extract_text_list (const GSList *list)
 {
@@ -810,8 +819,16 @@ ecc_fill_other_columns (ECalCache *cal_cache,
                ecc_resolve_tzid_cb, cal_cache, icaltimezone_get_utc_timezone (),
                icalcomponent_isa (icalcomp));
 
-       add_value (ECC_COLUMN_OCCUR_START, ecc_encode_timet_to_sql (cal_cache, occur_start));
-       add_value (ECC_COLUMN_OCCUR_END, ecc_encode_timet_to_sql (cal_cache, occur_end));
+       e_cal_component_get_dtstart (comp, &dt);
+       add_value (ECC_COLUMN_OCCUR_START, dt.value && ((dt.tzid && *dt.tzid) || dt.value->is_utc) ? 
ecc_encode_timet_to_sql (cal_cache, occur_start) : NULL);
+
+       has = dt.value != NULL;
+       add_value (ECC_COLUMN_HAS_START, g_strdup (has ? "1" : "0"));
+       e_cal_component_free_datetime (&dt);
+
+       e_cal_component_get_dtend (comp, &dt);
+       add_value (ECC_COLUMN_OCCUR_END, dt.value && ((dt.tzid && *dt.tzid) || dt.value->is_utc) ? 
ecc_encode_timet_to_sql (cal_cache, occur_end) : NULL);
+       e_cal_component_free_datetime (&dt);
 
        e_cal_component_get_due (comp, &dt);
        add_value (ECC_COLUMN_DUE, ecc_encode_time_to_sql (cal_cache, &dt));
@@ -852,11 +869,6 @@ ecc_fill_other_columns (ECalCache *cal_cache,
        has = e_cal_component_has_attachments (comp);
        add_value (ECC_COLUMN_HAS_ATTACHMENT, g_strdup (has ? "1" : "0"));
 
-       e_cal_component_get_dtstart (comp, &dt);
-       has = dt.value != NULL;
-       add_value (ECC_COLUMN_HAS_START, g_strdup (has ? "1" : "0"));
-       e_cal_component_free_datetime (&dt);
-
        has = e_cal_component_has_recurrences (comp) ||
              e_cal_component_is_instance (comp);
        add_value (ECC_COLUMN_HAS_RECURRENCES, g_strdup (has ? "1" : "0"));
@@ -3067,7 +3079,8 @@ e_cal_cache_dup_timezone_as_string (ECalCache *cal_cache,
                "SELECT zone FROM " ECC_TABLE_TIMEZONES " WHERE tzid=%Q",
                tzid);
 
-       success = e_cache_sqlite_select (E_CACHE (cal_cache), stmt, e_cal_cache_get_string, out_zone_string, 
cancellable, error);
+       success = e_cache_sqlite_select (E_CACHE (cal_cache), stmt, e_cal_cache_get_string, out_zone_string, 
cancellable, error) &&
+               *out_zone_string != NULL;
 
        e_cache_sqlite_stmt_free (stmt);
 
diff --git a/tests/libedata-cal/components/event-9.ics b/tests/libedata-cal/components/event-9.ics
index 70b96f7..f0edf3c 100644
--- a/tests/libedata-cal/components/event-9.ics
+++ b/tests/libedata-cal/components/event-9.ics
@@ -1,8 +1,8 @@
 BEGIN:VEVENT
 UID:event-9
 DTSTAMP:20170221T121736Z
-DTSTART;TZID=America/new_York:20170225T160000
-DTEND;TZID=America/new_York:20170225T170000
+DTSTART;TZID=America/New_York:20170225T160000
+DTEND;TZID=America/New_York:20170225T170000
 SEQUENCE:2
 ORGANIZER;CN=Alice:MAILTO:alice@no.where
 ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;
diff --git a/tests/libedata-cal/test-cal-cache-search.c b/tests/libedata-cal/test-cal-cache-search.c
index 7373fa4..ac662b7 100644
--- a/tests/libedata-cal/test-cal-cache-search.c
+++ b/tests/libedata-cal/test-cal-cache-search.c
@@ -260,15 +260,15 @@ test_search_occur_in_time_range (TCUFixture *fixture,
        test_search (fixture, "(occur-in-time-range? (make-time \"20170209T000000Z\") (make-time 
\"20170210T000000Z\"))", "event-1");
        test_search (fixture, "(occur-in-time-range? (make-time \"20170209T020000Z\") (make-time 
\"20170209T023000Z\"))", "event-1");
        test_search (fixture, "(occur-in-time-range? (make-time \"20111231T000000Z\") (make-time 
\"20111231T595959Z\"))", "event-5");
-       test_search (fixture, "(occur-in-time-range? (make-time \"20170225T160100Z\") (make-time 
\"20170225T160200Z\") \"America/New_York\")", "event-8");
-       test_search (fixture, "(occur-in-time-range? (make-time \"20170225T160100Z\") (make-time 
\"20170225T160200Z\") \"Europe/Berlin\")", "event-8");
+       test_search (fixture, "(occur-in-time-range? (make-time \"20170225T210100Z\") (make-time 
\"20170225T210200Z\") \"America/New_York\")", "event-8");
+       test_search (fixture, "(occur-in-time-range? (make-time \"20170225T150100Z\") (make-time 
\"20170225T150200Z\") \"Europe/Berlin\")", "event-8");
        test_search (fixture, "(occur-in-time-range? (make-time \"20170225T160100Z\") (make-time 
\"20170225T160200Z\") \"UTC\")", "event-8");
 
-       /* the second instance of event-6 */
-       test_search (fixture, "(occur-in-time-range? (make-time \"20170221T130000Z\") (make-time 
\"20170221T140000Z\"))", "event-6");
-       test_search (fixture, "(occur-in-time-range? (make-time \"20170221T130000Z\") (make-time 
\"20170221T140000Z\") \"America/New_York\")", "event-6");
-       test_search (fixture, "(occur-in-time-range? (make-time \"20170221T190000Z\") (make-time 
\"20170221T200000Z\") \"Europe/Berlin\")", "!event-6");
-       test_search (fixture, "(occur-in-time-range? (make-time \"20170221T130000Z\") (make-time 
\"20170221T140000Z\") \"Europe/Berlin\")", "event-6");
+       /* event-6 */
+       test_search (fixture, "(occur-in-time-range? (make-time \"20170221T180000Z\") (make-time 
\"20170221T190000Z\"))", "event-6");
+       test_search (fixture, "(occur-in-time-range? (make-time \"20170221T180000Z\") (make-time 
\"20170221T190000Z\") \"America/New_York\")", "event-6");
+       test_search (fixture, "(occur-in-time-range? (make-time \"20170221T200000Z\") (make-time 
\"20170221T210000Z\") \"Europe/Berlin\")", "!event-6");
+       test_search (fixture, "(occur-in-time-range? (make-time \"20170221T180000Z\") (make-time 
\"20170221T190000Z\") \"Europe/Berlin\")", "event-6");
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]