[evolution-data-server/wip/mcrha/libical-glib] Changes for e-cal-check-timezones.c/.h and e-cal-system-timezone.c



commit b8a2b77730edb52cee38da7fb4c172d9d2f32844
Author: Milan Crha <mcrha redhat com>
Date:   Wed Feb 6 22:24:35 2019 +0100

    Changes for e-cal-check-timezones.c/.h and e-cal-system-timezone.c

 src/calendar/libecal/e-cal-check-timezones.c | 266 ++++++++++++++-------------
 src/calendar/libecal/e-cal-check-timezones.h |  19 +-
 src/calendar/libecal/e-cal-system-timezone.c |  20 +-
 3 files changed, 162 insertions(+), 143 deletions(-)
---
diff --git a/src/calendar/libecal/e-cal-check-timezones.c b/src/calendar/libecal/e-cal-check-timezones.c
index 8594317e3..8c6898190 100644
--- a/src/calendar/libecal/e-cal-check-timezones.c
+++ b/src/calendar/libecal/e-cal-check-timezones.c
@@ -36,14 +36,14 @@
 static const gchar *
 e_cal_match_location (const gchar *location)
 {
-       icaltimezone *icomp;
+       icaltimezone *zone;
        const gchar *tail;
        gsize len;
        gchar *buffer;
 
-       icomp = icaltimezone_get_builtin_timezone (location);
-       if (icomp) {
-               return icaltimezone_get_tzid (icomp);
+       zone = icaltimezone_get_builtin_timezone (location);
+       if (zone) {
+               return icaltimezone_get_tzid (zone);
        }
 
        /* try a bit harder by stripping trailing suffix */
@@ -54,10 +54,10 @@ e_cal_match_location (const gchar *location)
        if (buffer) {
                memcpy (buffer, location, len);
                buffer[len] = 0;
-               icomp = icaltimezone_get_builtin_timezone (buffer);
+               zone = icaltimezone_get_builtin_timezone (buffer);
                g_free (buffer);
-               if (icomp) {
-                       return icaltimezone_get_tzid (icomp);
+               if (zone) {
+                       return icaltimezone_get_tzid (zone);
                }
        }
 
@@ -144,23 +144,27 @@ e_cal_match_tzid (const gchar *tzid)
 }
 
 static void
-patch_tzids (icalcomponent *subcomp,
+patch_tzids (ICalComponent *subcomp,
              GHashTable *mapping)
 {
        gchar *tzid = NULL;
 
-       if (icalcomponent_isa (subcomp) != ICAL_VTIMEZONE_COMPONENT) {
-               icalproperty *prop = icalcomponent_get_first_property (
-                       subcomp, ICAL_ANY_PROPERTY);
-               while (prop) {
-                       icalparameter *param = icalproperty_get_first_parameter (
-                               prop, ICAL_TZID_PARAMETER);
-                       while (param) {
+       if (i_cal_component_isa (subcomp) != I_CAL_VTIMEZONE_COMPONENT) {
+               ICalProperty *prop;
+
+               for (prop = i_cal_component_get_first_property (subcomp, I_CAL_ANY_PROPERTY);
+                    prop;
+                    g_object_unref (prop), prop = i_cal_component_get_next_property (subcomp, 
I_CAL_ANY_PROPERTY)) {
+                       ICalParameter *param;
+
+                       for (param = i_cal_property_get_first_parameter (prop, I_CAL_TZID_PARAMETER);
+                            param;
+                            g_object_unref (param), param = i_cal_property_get_next_parameter (prop, 
I_CAL_TZID_PARAMETER)) {
                                const gchar *oldtzid;
                                const gchar *newtzid;
 
                                g_free (tzid);
-                               tzid = g_strdup (icalparameter_get_tzid (param));
+                               tzid = g_strdup (i_cal_parameter_get_tzid (param));
 
                                if (!g_hash_table_lookup_extended (
                                        mapping, tzid,
@@ -170,13 +174,9 @@ patch_tzids (icalcomponent *subcomp,
                                        newtzid = e_cal_match_tzid (tzid);
                                }
                                if (newtzid) {
-                                       icalparameter_set_tzid (param, newtzid);
+                                       i_cal_parameter_set_tzid (param, newtzid);
                                }
-                               param = icalproperty_get_next_parameter (
-                                       prop, ICAL_TZID_PARAMETER);
                        }
-                       prop = icalcomponent_get_next_property (
-                               subcomp, ICAL_ANY_PROPERTY);
                }
        }
 
@@ -189,36 +189,38 @@ addsystemtz (gpointer key,
              gpointer user_data)
 {
        const gchar *tzid = key;
-       icalcomponent *comp = user_data;
-       icaltimezone *zone;
+       ICalComponent *vcalendar = user_data;
+       ICalTimezone *zone;
 
-       zone = icaltimezone_get_builtin_timezone_from_tzid (tzid);
+       zone = i_cal_timezone_get_builtin_timezone_from_tzid (tzid);
        if (zone) {
-               icalcomponent_add_component (
-                       comp,
-                       icalcomponent_new_clone (
-                       icaltimezone_get_component (zone)));
+               ICalComponent *zone_comp;
+
+               zone_comp = i_cal_timezone_get_component (zone);
+               if (zone_comp) {
+                       i_cal_component_take_component (vcalendar, i_cal_component_new_clone (zone_comp));
+                       g_object_unref (zone_comp);
+               }
        }
 }
 
 /**
- * e_cal_client_check_timezones:
- * @comp:     a VCALENDAR containing a list of
- *            VTIMEZONE and arbitrary other components, in
- *            arbitrary order: these other components are
- *            modified by this call
- * @comps: (element-type icalcomponent) (allow-none): a list of #icalcomponent
- * instances which also have to be patched; may be %NULL
- * @tzlookup: a callback function which is called to retrieve
- *            a calendar's VTIMEZONE definition; the returned
- *            definition is *not* freed by e_cal_client_check_timezones()
- *            (to be compatible with e_cal_get_timezone());
- *            NULL indicates that no such timezone exists
- *            or an error occurred
- * @ecalclient: an arbitrary pointer which is passed through to
- *            the @tzlookup function
+ * e_cal_client_check_timezones_sync:
+ * @vcalendar: a VCALENDAR containing a list of
+ *    VTIMEZONE and arbitrary other components, in
+ *    arbitrary order: these other components are
+ *    modified by this call
+ * @icalcomps: (element-type ICalComponent) (nullable): a list of #ICalComponent
+ *    instances which also have to be patched; may be %NULL
+ * @tzlookup: (scope call): a callback function which is called to retrieve
+ *    a calendar's VTIMEZONE definition; the returned
+ *    definition is *not* freed by e_cal_client_check_timezones()
+ *    NULL indicates that no such timezone exists
+ *    or an error occurred
+ * @tzlookup_data: (closure tzlookup): an arbitrary pointer which is passed
+ *    through to the @tzlookup function
  * @cancellable: a #GCancellable to use in @tzlookup function
- * @error:    an error description in case of a failure
+ * @error: an error description in case of a failure
  *
  * This function cleans up VEVENT, VJOURNAL, VTODO and VTIMEZONE
  * items which are to be imported into Evolution.
@@ -236,7 +238,7 @@ addsystemtz (gpointer key,
  * Some programs generate broken meeting invitations with TZID, but
  * without including the corresponding VTIMEZONE. Importing such
  * invitations unchanged causes problems later on (meeting displayed
- * incorrectly, e_cal_get_component_as_string() fails). The situation
+ * incorrectly, e_cal_component_get_as_string() fails). The situation
  * where this occurred in the past (found by a SyncEvolution user) is
  * now handled via the location based mapping.
  *
@@ -247,34 +249,31 @@ addsystemtz (gpointer key,
  * displaying of old events, whereas not replacing it breaks the new
  * events (the behavior in Evolution <= 2.22.1).
  *
- * The way this problem is resolved is by renaming the new VTIMEZONE
+ * The way this problem is resolved by renaming the new VTIMEZONE
  * definition until the TZID is unique. A running count is appended to
  * the TZID. All items referencing the renamed TZID are adapted
  * accordingly.
  *
  * Returns: %TRUE if successful, %FALSE otherwise.
  *
- * Since: 3.2
+ * Since: 3.36
  **/
 gboolean
-e_cal_client_check_timezones (icalcomponent *comp,
-                              GList *comps,
-                              icaltimezone *(*tzlookup) (const gchar *tzid,
-                                                         gconstpointer ecalclient,
-                                                         GCancellable *cancellable,
-                                                         GError **error),
-                              gconstpointer ecalclient,
-                              GCancellable *cancellable,
-                              GError **error)
+e_cal_client_check_timezones_sync (ICalComponent *vcalendar,
+                                  GSList *icalcomps,
+                                  ECalRecurResolveTimezoneCb tzlookup,
+                                  gpointer tzlookup_data,
+                                  GCancellable *cancellable,
+                                  GError **error)
 {
        gboolean success = TRUE;
-       icalcomponent *subcomp = NULL;
-       icaltimezone *zone = icaltimezone_new ();
+       ICalComponent *subcomp;
+       ICalTimezone *zone = i_cal_timezone_new ();
        gchar *key = NULL, *value = NULL;
        gchar *buffer = NULL;
        gchar *zonestr = NULL;
        gchar *tzid = NULL;
-       GList *l;
+       GSList *link;
 
        /* a hash from old to new tzid; strings dynamically allocated */
        GHashTable *mapping = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
@@ -282,18 +281,17 @@ e_cal_client_check_timezones (icalcomponent *comp,
        /* a hash of all system time zone IDs which have to be added; strings are shared with mapping hash */
        GHashTable *systemtzids = g_hash_table_new (g_str_hash, g_str_equal);
 
-       *error = NULL;
-
        if (!mapping || !zone) {
                goto nomem;
        }
 
        /* iterate over all VTIMEZONE definitions */
-       subcomp = icalcomponent_get_first_component (comp, ICAL_VTIMEZONE_COMPONENT);
-       while (subcomp) {
-               if (icaltimezone_set_component (zone, subcomp)) {
+       for (subcomp = i_cal_component_get_first_component (vcalendar, I_CAL_VTIMEZONE_COMPONENT);
+            subcomp;
+            g_object_unref (subcomp), subcomp = i_cal_component_get_next_component (vcalendar, 
I_CAL_VTIMEZONE_COMPONENT)) {
+               if (i_cal_timezone_set_component (zone, subcomp)) {
                        g_free (tzid);
-                       tzid = g_strdup (icaltimezone_get_tzid (zone));
+                       tzid = g_strdup (i_cal_timezone_get_tzid (zone));
                        if (tzid) {
                                const gchar *newtzid = e_cal_match_tzid (tzid);
                                if (newtzid) {
@@ -301,12 +299,14 @@ e_cal_client_check_timezones (icalcomponent *comp,
                                        g_free (key);
                                        key = g_strdup (tzid);
                                        if (!key) {
+                                               g_object_unref (subcomp);
                                                goto nomem;
                                        }
 
                                        g_free (value);
                                        value = g_strdup (newtzid);
                                        if (!value) {
+                                               g_object_unref (subcomp);
                                                goto nomem;
                                        }
 
@@ -316,28 +316,38 @@ e_cal_client_check_timezones (icalcomponent *comp,
                                } else {
                                        gint counter;
 
-                                       zonestr = icalcomponent_as_ical_string_r (subcomp);
+                                       zonestr = i_cal_component_as_ical_string_r (subcomp);
 
                                        /* check for collisions with existing timezones */
                                        for (counter = 0;
                                             counter < 100 /* sanity limit */;
                                             counter++) {
-                                               icaltimezone *existing_zone;
+                                               ICalTimezone *existing_zone;
+                                               ICalComponent *zone_comp;
+                                               GError *local_error = NULL;
 
                                                if (counter) {
                                                        g_free (value);
                                                        value = g_strdup_printf ("%s %d", tzid, counter);
                                                }
-                                               existing_zone = tzlookup (counter ? value : tzid, ecalclient, 
cancellable, error);
+                                               existing_zone = tzlookup (counter ? value : tzid, 
tzlookup_data, cancellable, &local_error);
                                                if (!existing_zone) {
-                                                       if (*error) {
+                                                       if (local_error) {
+                                                               g_propagate_error (error, local_error);
+                                                               g_object_unref (subcomp);
                                                                goto failed;
                                                        } else {
                                                                break;
                                                        }
                                                }
                                                g_free (buffer);
-                                               buffer = icalcomponent_as_ical_string_r 
(icaltimezone_get_component (existing_zone));
+                                               zone_comp = i_cal_timezone_get_component (existing_zone);
+                                               buffer = zone_comp ? i_cal_component_as_ical_string_r 
(zone_comp) : NULL;
+                                               g_clear_object (&zone_comp);
+                                               g_clear_object (&existing_zone);
+
+                                               if (!buffer)
+                                                       continue;
 
                                                if (counter) {
                                                        gchar *fulltzid = g_strdup_printf ("TZID:%s", value);
@@ -367,7 +377,7 @@ e_cal_client_check_timezones (icalcomponent *comp,
                                                 * is expected to occur rarely (if at all) in
                                                 * practice.
                                                 */
-                                               if (!strcmp (zonestr, buffer)) {
+                                               if (!g_strcmp0 (zonestr, buffer)) {
                                                        break;
                                                }
                                        }
@@ -376,10 +386,12 @@ e_cal_client_check_timezones (icalcomponent *comp,
                                                /* does not exist, nothing to do */
                                        } else {
                                                /* timezone renamed */
-                                               icalproperty *prop = icalcomponent_get_first_property 
(subcomp, ICAL_TZID_PROPERTY);
-                                               while (prop) {
-                                                       icalproperty_set_value_from_string (prop, value, 
"NO");
-                                                       prop = icalcomponent_get_next_property (subcomp, 
ICAL_ANY_PROPERTY);
+                                               ICalProperty *prop;
+
+                                               for (prop = i_cal_component_get_first_property (subcomp, 
I_CAL_TZID_PROPERTY);
+                                                    prop;
+                                                    g_object_unref (prop), prop = 
i_cal_component_get_next_property (subcomp, I_CAL_TZID_PROPERTY)) {
+                                                       i_cal_property_set_value_from_string (prop, value, 
"NO");
                                                }
                                                g_free (key);
                                                key = g_strdup (tzid);
@@ -389,15 +401,14 @@ e_cal_client_check_timezones (icalcomponent *comp,
                                }
                        }
                }
-
-               subcomp = icalcomponent_get_next_component (comp, ICAL_VTIMEZONE_COMPONENT);
        }
 
        /*
         * now replace all TZID parameters in place
         */
-       subcomp = icalcomponent_get_first_component (comp, ICAL_ANY_COMPONENT);
-       while (subcomp) {
+       for (subcomp = i_cal_component_get_first_component (vcalendar, ICAL_ANY_COMPONENT);
+            subcomp;
+            g_object_unref (subcomp), subcomp = i_cal_component_get_next_component (vcalendar, 
ICAL_ANY_COMPONENT)) {
                /*
                 * Leave VTIMEZONE unchanged, iterate over properties of
                 * everything else.
@@ -408,39 +419,37 @@ e_cal_client_check_timezones (icalcomponent *comp,
                 * added below.
                 */
                patch_tzids (subcomp, mapping);
-               subcomp = icalcomponent_get_next_component (comp, ICAL_ANY_COMPONENT);
        }
 
-       for (l = comps; l; l = l->next) {
-               patch_tzids (l->data, mapping);
+       for (link = icalcomps; link; link = g_slist_next (link)) {
+               ICalComponent *icalcomp = link->data;
+               patch_tzids (icalcomp, mapping);
        }
 
        /*
         * add system time zones that we mapped to: adding them ensures
         * that the VCALENDAR remains consistent
         */
-       g_hash_table_foreach (systemtzids, addsystemtz, comp);
+       g_hash_table_foreach (systemtzids, addsystemtz, vcalendar);
 
        goto done;
  nomem:
        /* set gerror for "out of memory" if possible, otherwise abort via g_error() */
-       *error = g_error_new (E_CLIENT_ERROR, E_CLIENT_ERROR_OTHER_ERROR, "out of memory");
-       if (!*error) {
-               g_error ("e_cal_check_timezones(): out of memory, cannot proceed - sorry!");
+       g_set_error_literal (error, E_CLIENT_ERROR, E_CLIENT_ERROR_OTHER_ERROR, "out of memory");
+       if (error && !*error) {
+               g_error ("%s: out of memory, cannot proceed - sorry!", G_STRFUNC);
        }
  failed:
        /* gerror should have been set already */
        success = FALSE;
  done:
-       if (mapping) {
+       if (mapping)
                g_hash_table_destroy (mapping);
-       }
-       if (systemtzids) {
+
+       if (systemtzids)
                g_hash_table_destroy (systemtzids);
-       }
-       if (zone) {
-               icaltimezone_free (zone, 1);
-       }
+
+       g_clear_object (&zone);
        g_free (tzid);
        g_free (zonestr);
        g_free (buffer);
@@ -451,31 +460,33 @@ e_cal_client_check_timezones (icalcomponent *comp,
 }
 
 /**
- * e_cal_client_tzlookup:
+ * e_cal_client_tzlookup_cb:
  * @tzid: ID of the timezone to lookup
- * @ecalclient: must be a valid #ECalClient pointer
+ * @ecalclient: (type ECalClient): a valid #ECalClient pointer
  * @cancellable: an optional #GCancellable to use, or %NULL
  * @error: an error description in case of a failure
  *
- * An implementation of the tzlookup callback which clients
+ * An implementation of the #ECalRecurResolveTimezoneCb callback which clients
  * can use. Calls e_cal_client_get_timezone_sync().
  *
- * Returns: A timezone object, or %NULL on failure. This object is owned
- *   by the @ecalclient, thus do not free it.
+ * Free a non-NULL returned timezone object with g_object_unref(), when
+ * no longer needed.
  *
- * Since: 3.2
- */
-icaltimezone *
-e_cal_client_tzlookup (const gchar *tzid,
-                       gconstpointer ecalclient,
-                       GCancellable *cancellable,
-                       GError **error)
+ * Returns: (transfer full) (nullable): A timezone object, or %NULL on failure
+ *    or when not found.
+ *
+ * Since: 3.36
+ **/
+ICalTimezone *
+e_cal_client_tzlookup_cb (const gchar *tzid,
+                         gpointer ecalclient,
+                         GCancellable *cancellable,
+                         GError **error)
 {
-       ECalClient *cal_client = (ECalClient *) ecalclient;
-       icaltimezone *zone = NULL;
+       ECalClient *cal_client = ecalclient;
+       ICalTimezone *zone = NULL;
        GError *local_error = NULL;
 
-       g_return_val_if_fail (cal_client != NULL, NULL);
        g_return_val_if_fail (E_IS_CAL_CLIENT (cal_client), NULL);
 
        if (e_cal_client_get_timezone_sync (cal_client, tzid, &zone, cancellable, &local_error)) {
@@ -496,30 +507,33 @@ e_cal_client_tzlookup (const gchar *tzid,
 }
 
 /**
- * e_cal_client_tzlookup_icomp:
+ * e_cal_client_tzlookup_icalcomp_cb:
  * @tzid: ID of the timezone to lookup
- * @custom: must be a icalcomponent pointer which contains
- *          either a VCALENDAR with VTIMEZONEs or VTIMEZONES
- *          directly
+ * @icalcomp: (type ICalComponent): an #ICalComponent pointer, which contains
+ *    either a VCALENDAR with VTIMEZONEs or VTIMEZONES directly
  * @cancellable: an optional #GCancellable to use, or %NULL
  * @error: an error description in case of a failure
  *
- * An implementation of the tzlookup callback which backends
- * like the file backend can use. Searches for the timezone
- * in the component list.
+ * An implementation of the #ECalRecurResolveTimezoneCb callback which
+ * backends can use. Searches for the timezone in the @icalcomp.
  *
- * Returns: A timezone object, or %NULL if not found inside @custom. This object is owned
- *   by the @custom, thus do not free it.
+ * Free a non-NULL returned timezone object with g_object_unref(), when
+ * no longer needed.
  *
- * Since: 3.2
- */
-icaltimezone *
-e_cal_client_tzlookup_icomp (const gchar *tzid,
-                             gconstpointer custom,
-                             GCancellable *cancellable,
-                             GError **error)
+ * Returns: (transfer full) (nullable): A timezone object, or %NULL, if
+ *    not found inside @icalcomp.
+ *
+ * Since: 3.36
+ **/
+ICalTimezone *
+e_cal_client_tzlookup_icalcomp_cb (const gchar *tzid,
+                                  gpointer icalcomp,
+                                  GCancellable *cancellable,
+                                  GError **error)
 {
-       icalcomponent *icomp = (icalcomponent *) custom;
+       ICalComponent *icomp = icalcomp;
+
+       g_return_val_if_fail (I_CAL_IS_COMPONENT (icalcomp), NULL);
 
-       return icalcomponent_get_timezone (icomp, (gchar *) tzid);
+       return i_cal_component_get_timezone (icomp, tzid);
 }
diff --git a/src/calendar/libecal/e-cal-check-timezones.h b/src/calendar/libecal/e-cal-check-timezones.h
index a819dbad1..ba4b3c987 100644
--- a/src/calendar/libecal/e-cal-check-timezones.h
+++ b/src/calendar/libecal/e-cal-check-timezones.h
@@ -27,23 +27,26 @@
 #include <glib.h>
 #include <gio/gio.h>
 
+#include <libecal/e-cal-recur.h>
+
 G_BEGIN_DECLS
 
-gboolean       e_cal_client_check_timezones    (icalcomponent *comp,
-                                                GList *comps,
-                                                icaltimezone *(*tzlookup) (const gchar *tzid, gconstpointer 
ecalclient, GCancellable *cancellable, GError **error),
-                                                gconstpointer ecalclient,
+gboolean       e_cal_client_check_timezones_sync
+                                               (ICalComponent *vcalendar,
+                                                GSList *comps, /* ICalComponent * */
+                                                ECalRecurResolveTimezoneCb tzlookup,
+                                                gpointer tzlookup_data,
                                                 GCancellable *cancellable,
                                                 GError **error);
 
-icaltimezone * e_cal_client_tzlookup           (const gchar *tzid,
-                                                gconstpointer ecalclient,
+ICalTimezone * e_cal_client_tzlookup_cb        (const gchar *tzid,
+                                                gpointer ecalclient, /* ECalClient * */
                                                 GCancellable *cancellable,
                                                 GError **error);
 
-icaltimezone * e_cal_client_tzlookup_icomp
+ICalTimezone * e_cal_client_tzlookup_icalcomp_cb
                                                (const gchar *tzid,
-                                                gconstpointer custom,
+                                                gpointer icalcomp, /* ICalComponent * */
                                                 GCancellable *cancellable,
                                                 GError **error);
 
diff --git a/src/calendar/libecal/e-cal-system-timezone.c b/src/calendar/libecal/e-cal-system-timezone.c
index 25bbb6713..09da13973 100644
--- a/src/calendar/libecal/e-cal-system-timezone.c
+++ b/src/calendar/libecal/e-cal-system-timezone.c
@@ -543,27 +543,29 @@ static gchar *
 system_timezone_find (void)
 {
        GHashTable *ical_zones;
-       icalarray *builtin_timezones;
-       gint ii;
+       ICalArray *builtin_timezones;
+       gint ii, nelems;
        gchar *tz, *config_tz = NULL;
 
        /* return only timezones known to libical */
        ical_zones = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
        g_hash_table_add (ical_zones, g_strdup ("UTC"));
 
-       builtin_timezones = icaltimezone_get_builtin_timezones ();
-       for (ii = 0; ii < builtin_timezones->num_elements; ii++) {
-               icaltimezone *zone;
+       builtin_timezones = i_cal_timezone_get_builtin_timezones ();
+       nelems = i_cal_array_size (builtin_timezones);
+
+       for (ii = 0; ii < nelems; ii++) {
+               ICalTimezone *zone;
                const gchar *location;
 
-               zone = icalarray_element_at (builtin_timezones, ii);
+               zone = i_cal_timezone_array_element_at (builtin_timezones, ii);
                if (!zone)
                        continue;
 
-               location = icaltimezone_get_location (zone);
+               location = i_cal_timezone_get_location (zone);
                if (location)
-                       g_hash_table_add (
-                               ical_zones, g_strdup (location));
+                       g_hash_table_add (ical_zones, g_strdup (location));
+               g_object_unref (zone);
        }
 
        /* softlink is the best option, it points to the correct file */


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