[gnome-calendar] event: Add API to clone events



commit dbd81f401c9f1ffa49f30c752ed283e3de0c581b
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Mon Jun 15 13:07:48 2020 -0300

    event: Add API to clone events
    
    This will be useful for updating events without having
    to keep track of the event states.
    
    Add the new API, and also a unit test.

 src/core/gcal-event.c | 21 +++++++++++++++++++++
 src/core/gcal-event.h |  2 ++
 tests/test-event.c    | 26 ++++++++++++++++++++++++++
 3 files changed, 49 insertions(+)
---
diff --git a/src/core/gcal-event.c b/src/core/gcal-event.c
index ba81d75b..74fd616a 100644
--- a/src/core/gcal-event.c
+++ b/src/core/gcal-event.c
@@ -799,6 +799,27 @@ gcal_event_new (GcalCalendar   *calendar,
                          NULL);
 }
 
+/**
+ * gcal_event_new:
+ * @self: a #GcalEvent
+ *
+ * Clones @event into a new #GcalEvent instance. This is useful
+ * for updating events.
+ *
+ * Returns: (transfer full)(nullable): a #GcalEvent
+ */
+GcalEvent*
+gcal_event_new_from_event (GcalEvent *self)
+{
+  g_autoptr (ECalComponent) component = NULL;
+
+  g_return_val_if_fail (GCAL_IS_EVENT (self), NULL);
+
+  component = e_cal_component_clone (self->component);
+
+  return gcal_event_new (self->calendar, component, NULL);
+}
+
 /**
  * gcal_event_get_all_day:
  * @self: a #GcalEvent
diff --git a/src/core/gcal-event.h b/src/core/gcal-event.h
index 55c8c5fa..a5859db9 100644
--- a/src/core/gcal-event.h
+++ b/src/core/gcal-event.h
@@ -53,6 +53,8 @@ GcalEvent*           gcal_event_new                              (GcalCalendar
                                                                   ECalComponent      *component,
                                                                   GError            **error);
 
+GcalEvent*           gcal_event_new_from_event                   (GcalEvent          *self);
+
 gboolean             gcal_event_get_all_day                      (GcalEvent          *self);
 
 void                 gcal_event_set_all_day                      (GcalEvent          *self,
diff --git a/tests/test-event.c b/tests/test-event.c
index 3f8147b7..a9cd282b 100644
--- a/tests/test-event.c
+++ b/tests/test-event.c
@@ -79,6 +79,31 @@ event_new (void)
   g_assert_nonnull (event);
 }
 
+/*********************************************************************************************************************/
+
+static void
+event_clone (void)
+{
+  g_autoptr (GcalEvent) clone1 = NULL;
+  g_autoptr (GcalEvent) clone2 = NULL;
+  g_autoptr (GcalEvent) event = NULL;
+  g_autoptr (GError) error = NULL;
+
+  event = create_event_for_string (STUB_EVENT, &error);
+
+  g_assert_no_error (error);
+  g_assert_nonnull (event);
+
+  clone1 = gcal_event_new_from_event (event);
+  g_assert_nonnull (clone1);
+
+  gcal_event_set_summary (event, "Another summary");
+
+  clone2 = gcal_event_new_from_event (event);
+  g_assert_nonnull (clone2);
+}
+
+
 
/*********************************************************************************************************************/
 
 static void
@@ -227,6 +252,7 @@ main (gint   argc,
   g_test_init (&argc, &argv, NULL);
 
   g_test_add_func ("/event/new", event_new);
+  g_test_add_func ("/event/clone", event_clone);
   g_test_add_func ("/event/uid", event_uid);
   g_test_add_func ("/event/summary", event_summary);
   g_test_add_func ("/event/date/start", event_date_start);


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