[evolution-data-server/wip/mcrha/libical-glib] Add functions to search and remove by kind in property and parameter bags



commit c74c8f8afc9b3c2cf3c411e98993d0c2cc3819f9
Author: Milan Crha <mcrha redhat com>
Date:   Wed Mar 27 18:53:57 2019 +0100

    Add functions to search and remove by kind in property and parameter bags

 .../libecal/e-cal-component-parameter-bag.c        | 71 ++++++++++++++++++++++
 .../libecal/e-cal-component-parameter-bag.h        |  7 +++
 .../libecal/e-cal-component-property-bag.c         | 71 ++++++++++++++++++++++
 .../libecal/e-cal-component-property-bag.h         |  7 +++
 tests/libecal/test-cal-component.c                 | 46 ++++++++++++++
 5 files changed, 202 insertions(+)
---
diff --git a/src/calendar/libecal/e-cal-component-parameter-bag.c 
b/src/calendar/libecal/e-cal-component-parameter-bag.c
index 234338385..8afeb5f16 100644
--- a/src/calendar/libecal/e-cal-component-parameter-bag.c
+++ b/src/calendar/libecal/e-cal-component-parameter-bag.c
@@ -313,6 +313,36 @@ e_cal_component_parameter_bag_get (const ECalComponentParameterBag *bag,
        return g_ptr_array_index (bag->parameters, index);
 }
 
+/**
+ * e_cal_component_parameter_bag_get_first_by_kind:
+ * @bag: an #ECalComponentParameterBag
+ * @kind: an #ICalParameterKind to search for
+ *
+ * Returns: the index of the first parameter of the given @kind, or value
+ *    out of bounds, if such parameter cannot be found
+ *
+ * Since: 3.36
+ **/
+guint
+e_cal_component_parameter_bag_get_first_by_kind (const ECalComponentParameterBag *bag,
+                                                ICalParameterKind kind)
+{
+       guint index;
+
+       g_return_val_if_fail (bag != NULL, ~0);
+       g_return_val_if_fail (bag->parameters != NULL, ~0);
+
+       for (index = 0; index < bag->parameters->len; index++) {
+               ICalParameter *param;
+
+               param = g_ptr_array_index (bag->parameters, index);
+               if (param && i_cal_parameter_isa (param) == kind)
+                       return index;
+       }
+
+       return ~0;
+}
+
 /**
  * e_cal_component_parameter_bag_remove:
  * @bag: an #ECalComponentParameterBag
@@ -335,6 +365,47 @@ e_cal_component_parameter_bag_remove (ECalComponentParameterBag *bag,
                g_ptr_array_remove_index (bag->parameters, index);
 }
 
+/**
+ * e_cal_component_parameter_bag_remove_by_kind:
+ * @bag: an #ECalComponentParameterBag
+ * @kind: an #ICalParameterKind to remove
+ * @all: %TRUE to remove all parameters of the @kind, or %FALSE to only the first
+ *
+ * Removes the first or all (depending on the @all) parameters of the given @kind.
+ *
+ * Returns: how many parameters had been removed
+ *
+ * Since: 3.36
+ **/
+guint
+e_cal_component_parameter_bag_remove_by_kind (ECalComponentParameterBag *bag,
+                                             ICalParameterKind kind,
+                                             gboolean all)
+{
+       guint index, count = 0;
+
+       g_return_val_if_fail (bag != NULL, 0);
+       g_return_val_if_fail (bag->parameters != NULL, 0);
+
+       index = 0;
+       while (index < bag->parameters->len) {
+               ICalParameter *param;
+
+               param = g_ptr_array_index (bag->parameters, index);
+               if (param && i_cal_parameter_isa (param) == kind) {
+                       g_ptr_array_remove_index (bag->parameters, index);
+                       count++;
+
+                       if (!all)
+                               break;
+               } else {
+                       index++;
+               }
+       }
+
+       return count;
+}
+
 /**
  * e_cal_component_parameter_bag_clear:
  * @bag: an #ECalComponentParameterBag
diff --git a/src/calendar/libecal/e-cal-component-parameter-bag.h 
b/src/calendar/libecal/e-cal-component-parameter-bag.h
index 3f4a998c4..ca2b77fd7 100644
--- a/src/calendar/libecal/e-cal-component-parameter-bag.h
+++ b/src/calendar/libecal/e-cal-component-parameter-bag.h
@@ -87,9 +87,16 @@ guint                e_cal_component_parameter_bag_get_count
                                                (const ECalComponentParameterBag *bag);
 ICalParameter *        e_cal_component_parameter_bag_get(const ECalComponentParameterBag *bag,
                                                 guint index);
+guint          e_cal_component_parameter_bag_get_first_by_kind
+                                               (const ECalComponentParameterBag *bag,
+                                                ICalParameterKind kind);
 void           e_cal_component_parameter_bag_remove
                                                (ECalComponentParameterBag *bag,
                                                 guint index);
+guint          e_cal_component_parameter_bag_remove_by_kind
+                                               (ECalComponentParameterBag *bag,
+                                                ICalParameterKind kind,
+                                                gboolean all);
 void           e_cal_component_parameter_bag_clear
                                                (ECalComponentParameterBag *bag);
 
diff --git a/src/calendar/libecal/e-cal-component-property-bag.c 
b/src/calendar/libecal/e-cal-component-property-bag.c
index 5a38e2d9f..7ff36af5d 100644
--- a/src/calendar/libecal/e-cal-component-property-bag.c
+++ b/src/calendar/libecal/e-cal-component-property-bag.c
@@ -313,6 +313,36 @@ e_cal_component_property_bag_get (const ECalComponentPropertyBag *bag,
        return g_ptr_array_index (bag->properties, index);
 }
 
+/**
+ * e_cal_component_property_bag_get_first_by_kind:
+ * @bag: an #ECalComponentPropertyBag
+ * @kind: an #ICalPropertyKind to search for
+ *
+ * Returns: the index of the first property of the given @kind, or value
+ *    out of bounds, if such property cannot be found
+ *
+ * Since: 3.36
+ **/
+guint
+e_cal_component_property_bag_get_first_by_kind (const ECalComponentPropertyBag *bag,
+                                               ICalPropertyKind kind)
+{
+       guint index;
+
+       g_return_val_if_fail (bag != NULL, ~0);
+       g_return_val_if_fail (bag->properties != NULL, ~0);
+
+       for (index = 0; index < bag->properties->len; index++) {
+               ICalProperty *prop;
+
+               prop = g_ptr_array_index (bag->properties, index);
+               if (prop && i_cal_property_isa (prop) == kind)
+                       return index;
+       }
+
+       return ~0;
+}
+
 /**
  * e_cal_component_property_bag_remove:
  * @bag: an #ECalComponentPropertyBag
@@ -335,6 +365,47 @@ e_cal_component_property_bag_remove (ECalComponentPropertyBag *bag,
                g_ptr_array_remove_index (bag->properties, index);
 }
 
+/**
+ * e_cal_component_property_bag_remove_by_kind:
+ * @bag: an #ECalComponentPropertyBag
+ * @kind: an #ICalPropertyKind to remove
+ * @all: %TRUE to remove all properties of the @kind, or %FALSE to only the first
+ *
+ * Removes the first or all (depending on the @all) properties of the given @kind.
+ *
+ * Returns: how many properties had been removed
+ *
+ * Since: 3.36
+ **/
+guint
+e_cal_component_property_bag_remove_by_kind (ECalComponentPropertyBag *bag,
+                                            ICalPropertyKind kind,
+                                            gboolean all)
+{
+       guint index, count = 0;
+
+       g_return_val_if_fail (bag != NULL, 0);
+       g_return_val_if_fail (bag->properties != NULL, 0);
+
+       index = 0;
+       while (index < bag->properties->len) {
+               ICalProperty *prop;
+
+               prop = g_ptr_array_index (bag->properties, index);
+               if (prop && i_cal_property_isa (prop) == kind) {
+                       g_ptr_array_remove_index (bag->properties, index);
+                       count++;
+
+                       if (!all)
+                               break;
+               } else {
+                       index++;
+               }
+       }
+
+       return count;
+}
+
 /**
  * e_cal_component_property_bag_clear:
  * @bag: an #ECalComponentPropertyBag
diff --git a/src/calendar/libecal/e-cal-component-property-bag.h 
b/src/calendar/libecal/e-cal-component-property-bag.h
index c029e2dcd..537c2c6ae 100644
--- a/src/calendar/libecal/e-cal-component-property-bag.h
+++ b/src/calendar/libecal/e-cal-component-property-bag.h
@@ -86,9 +86,16 @@ guint                e_cal_component_property_bag_get_count
                                                (const ECalComponentPropertyBag *bag);
 ICalProperty * e_cal_component_property_bag_get(const ECalComponentPropertyBag *bag,
                                                 guint index);
+guint          e_cal_component_property_bag_get_first_by_kind
+                                               (const ECalComponentPropertyBag *bag,
+                                                ICalPropertyKind kind);
 void           e_cal_component_property_bag_remove
                                                (ECalComponentPropertyBag *bag,
                                                 guint index);
+guint          e_cal_component_property_bag_remove_by_kind
+                                               (ECalComponentPropertyBag *bag,
+                                                ICalPropertyKind kind,
+                                                gboolean all);
 void           e_cal_component_property_bag_clear
                                                (ECalComponentPropertyBag *bag);
 
diff --git a/tests/libecal/test-cal-component.c b/tests/libecal/test-cal-component.c
index 509ccac31..ab2b4ab80 100644
--- a/tests/libecal/test-cal-component.c
+++ b/tests/libecal/test-cal-component.c
@@ -1612,6 +1612,29 @@ test_component_struct_parameter_bag (void)
        g_assert_cmpint (i_cal_property_count_parameters (prop), ==, e_cal_component_parameter_bag_get_count 
(bag));
        g_object_unref (prop);
 
+       e_cal_component_parameter_bag_clear (bag);
+
+       e_cal_component_parameter_bag_take (bag, i_cal_parameter_new_cutype (I_CAL_CUTYPE_ROOM));
+       e_cal_component_parameter_bag_take (bag, i_cal_parameter_new_cn ("111"));
+       e_cal_component_parameter_bag_take (bag, i_cal_parameter_new_cutype (I_CAL_CUTYPE_ROOM));
+       e_cal_component_parameter_bag_take (bag, i_cal_parameter_new_cutype (I_CAL_CUTYPE_RESOURCE));
+
+       g_assert_cmpint (e_cal_component_parameter_bag_get_count (bag), ==, 4);
+       g_assert_cmpint (e_cal_component_parameter_bag_get_first_by_kind (bag, I_CAL_CUTYPE_PARAMETER), ==, 
0);
+       g_assert_cmpint (e_cal_component_parameter_bag_get_first_by_kind (bag, I_CAL_CN_PARAMETER), ==, 1);
+       g_assert_cmpint (e_cal_component_parameter_bag_remove_by_kind (bag, I_CAL_TZID_PARAMETER, FALSE), ==, 
0);
+       g_assert_cmpint (e_cal_component_parameter_bag_get_count (bag), ==, 4);
+       g_assert_cmpint (e_cal_component_parameter_bag_remove_by_kind (bag, I_CAL_TZID_PARAMETER, TRUE), ==, 
0);
+       g_assert_cmpint (e_cal_component_parameter_bag_get_count (bag), ==, 4);
+       g_assert_cmpint (e_cal_component_parameter_bag_remove_by_kind (bag, I_CAL_CUTYPE_PARAMETER, FALSE), 
==, 1);
+       g_assert_cmpint (e_cal_component_parameter_bag_get_count (bag), ==, 3);
+       g_assert_cmpint (e_cal_component_parameter_bag_get_first_by_kind (bag, I_CAL_CN_PARAMETER), ==, 0);
+       g_assert_cmpint (e_cal_component_parameter_bag_get_first_by_kind (bag, I_CAL_CUTYPE_PARAMETER), ==, 
1);
+       g_assert_cmpint (e_cal_component_parameter_bag_remove_by_kind (bag, I_CAL_CUTYPE_PARAMETER, TRUE), 
==, 2);
+       g_assert_cmpint (e_cal_component_parameter_bag_get_count (bag), ==, 1);
+       g_assert_cmpint (e_cal_component_parameter_bag_get_first_by_kind (bag, I_CAL_CN_PARAMETER), ==, 0);
+       g_assert_cmpint (e_cal_component_parameter_bag_get_first_by_kind (bag, I_CAL_CUTYPE_PARAMETER), >=, 
e_cal_component_parameter_bag_get_count (bag));
+
        e_cal_component_parameter_bag_free (bag);
 }
 
@@ -1909,6 +1932,29 @@ test_component_struct_property_bag (void)
        g_assert_cmpint (i_cal_component_count_properties (icomp, I_CAL_ANY_PROPERTY), ==, 
e_cal_component_property_bag_get_count (bag));
        g_object_unref (icomp);
 
+       e_cal_component_property_bag_clear (bag);
+
+       e_cal_component_property_bag_take (bag, i_cal_property_new_status (I_CAL_STATUS_CANCELLED));
+       e_cal_component_property_bag_take (bag, i_cal_property_new_uid ("111"));
+       e_cal_component_property_bag_take (bag, i_cal_property_new_status (I_CAL_STATUS_COMPLETED));
+       e_cal_component_property_bag_take (bag, i_cal_property_new_status (I_CAL_STATUS_INPROCESS));
+
+       g_assert_cmpint (e_cal_component_property_bag_get_count (bag), ==, 4);
+       g_assert_cmpint (e_cal_component_property_bag_get_first_by_kind (bag, I_CAL_STATUS_PROPERTY), ==, 0);
+       g_assert_cmpint (e_cal_component_property_bag_get_first_by_kind (bag, I_CAL_UID_PROPERTY), ==, 1);
+       g_assert_cmpint (e_cal_component_property_bag_remove_by_kind (bag, I_CAL_TZID_PROPERTY, FALSE), ==, 
0);
+       g_assert_cmpint (e_cal_component_property_bag_get_count (bag), ==, 4);
+       g_assert_cmpint (e_cal_component_property_bag_remove_by_kind (bag, I_CAL_TZID_PROPERTY, TRUE), ==, 0);
+       g_assert_cmpint (e_cal_component_property_bag_get_count (bag), ==, 4);
+       g_assert_cmpint (e_cal_component_property_bag_remove_by_kind (bag, I_CAL_STATUS_PROPERTY, FALSE), ==, 
1);
+       g_assert_cmpint (e_cal_component_property_bag_get_count (bag), ==, 3);
+       g_assert_cmpint (e_cal_component_property_bag_get_first_by_kind (bag, I_CAL_UID_PROPERTY), ==, 0);
+       g_assert_cmpint (e_cal_component_property_bag_get_first_by_kind (bag, I_CAL_STATUS_PROPERTY), ==, 1);
+       g_assert_cmpint (e_cal_component_property_bag_remove_by_kind (bag, I_CAL_STATUS_PROPERTY, TRUE), ==, 
2);
+       g_assert_cmpint (e_cal_component_property_bag_get_count (bag), ==, 1);
+       g_assert_cmpint (e_cal_component_property_bag_get_first_by_kind (bag, I_CAL_UID_PROPERTY), ==, 0);
+       g_assert_cmpint (e_cal_component_property_bag_get_first_by_kind (bag, I_CAL_STATUS_PROPERTY), >=, 
e_cal_component_property_bag_get_count (bag));
+
        e_cal_component_property_bag_free (bag);
 }
 


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