[gnome-calendar/fix-shifting-events] manager: Fix bug where recurring events shift after being edited




commit 48184e35f5cb0d7f0ba65bd2568cd4ce6739e9ac
Author: Ray Strode <rstrode redhat com>
Date:   Tue Oct 11 15:33:36 2022 -0400

    manager: Fix bug where recurring events shift after being edited
    
    Right now if a user edits say event 3 in a set of 5 events, the
    whole event set gets shifted over by 3. It's as if the middle
    instance event is treated as the new first event.
    
    This is because of a bit of the code that clears the recurrence
    id of the event, to get at the "main event", and edit the whole
    set. The code retains the start time from the instance, though,
    rather than use the start time of the main event it's trying to
    update.
    
    This commit queries for the main event from the instance event,
    and sets the new main event start time to match the old main
    event start time, before doing an update.

 src/core/gcal-event.c   | 80 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/core/gcal-event.h   |  2 ++
 src/core/gcal-manager.c | 19 ++++++------
 3 files changed, 92 insertions(+), 9 deletions(-)
---
diff --git a/src/core/gcal-event.c b/src/core/gcal-event.c
index 49833a3a..6afc044c 100644
--- a/src/core/gcal-event.c
+++ b/src/core/gcal-event.c
@@ -832,6 +832,86 @@ gcal_event_new_from_event (GcalEvent *self)
   return gcal_event_new (self->calendar, component, NULL);
 }
 
+/**
+ * gcal_event_new_main_event_from_instance_event:
+ * @self: a #GcalEvent
+ *
+ * Creates a main @event from an instance event. This is useful
+ * for updating all recurring events at the same time to match
+ * the contence of one instance in the set.
+ *
+ * Returns: (transfer full)(nullable): a #GcalEvent
+ */
+GcalEvent*
+gcal_event_new_main_event_from_instance_event (GcalEvent *self)
+{
+  GcalEvent *main_event;
+
+  g_autoptr (ECalComponent) new_main_ecal_component = NULL;
+  g_autoptr (ECalComponent) old_main_ecal_component = NULL;
+  ECalComponentDateTime *start_date = NULL;
+  ECalComponentDateTime *end_date = NULL;
+  g_autoptr (GError) error = NULL;
+  ECalClient *client;
+  const char *uid;
+
+  ICalComponent *old_main_ical_component;
+
+  g_return_val_if_fail (GCAL_IS_EVENT (self), NULL);
+
+  if (!e_cal_component_is_instance (self->component))
+    return NULL;
+
+  new_main_ecal_component = e_cal_component_clone (self->component);
+
+  /* The main event shares a uid with the instance event, but has
+   * a null recurrence id
+   */
+  uid = e_cal_component_get_uid (new_main_ecal_component);
+
+  client = gcal_calendar_get_client (self->calendar);
+
+  if (!e_cal_client_get_object_sync (client,
+                                     uid,
+                                     NULL,
+                                     &old_main_ical_component,
+                                     NULL,
+                                     &error))
+    {
+      g_warning ("Error finding main event from instance event: %s", error->message);
+      return NULL;
+    }
+
+  old_main_ecal_component = e_cal_component_new_from_icalcomponent (g_steal_pointer 
(&old_main_ical_component));
+
+  if (!old_main_ecal_component)
+    return NULL;
+
+  /* Copy the start time and end time from the old main event to the new one
+   * and clear the recurrence id.
+   */
+  start_date = e_cal_component_get_dtstart (old_main_ecal_component);
+  e_cal_component_set_dtstart (new_main_ecal_component, start_date);
+  g_clear_pointer (&start_date, e_cal_component_datetime_free);
+
+  end_date = e_cal_component_get_dtend (old_main_ecal_component);
+  e_cal_component_set_dtend (new_main_ecal_component, end_date);
+  g_clear_pointer (&end_date, e_cal_component_datetime_free);
+
+  e_cal_component_set_recurid (new_main_ecal_component, NULL);
+  e_cal_component_commit_sequence (new_main_ecal_component);
+
+  main_event = gcal_event_new (self->calendar, new_main_ecal_component, &error);
+
+  if (!main_event)
+    {
+      g_warning ("Error creating main event from modified instance event: %s", error->message);
+      return NULL;
+    }
+
+  return main_event;
+}
+
 /**
  * gcal_event_get_all_day:
  * @self: a #GcalEvent
diff --git a/src/core/gcal-event.h b/src/core/gcal-event.h
index a5859db9..09beb5f4 100644
--- a/src/core/gcal-event.h
+++ b/src/core/gcal-event.h
@@ -55,6 +55,8 @@ GcalEvent*           gcal_event_new                              (GcalCalendar
 
 GcalEvent*           gcal_event_new_from_event                   (GcalEvent          *self);
 
+GcalEvent*           gcal_event_new_main_event_from_instance_event (GcalEvent        *self);
+
 gboolean             gcal_event_get_all_day                      (GcalEvent          *self);
 
 void                 gcal_event_set_all_day                      (GcalEvent          *self,
diff --git a/src/core/gcal-manager.c b/src/core/gcal-manager.c
index c5c54d36..9c85d9b1 100644
--- a/src/core/gcal-manager.c
+++ b/src/core/gcal-manager.c
@@ -1049,18 +1049,19 @@ gcal_manager_update_event (GcalManager           *self,
   g_return_if_fail (GCAL_IS_MANAGER (self));
   g_return_if_fail (GCAL_IS_EVENT (event));
 
+  if (mod == GCAL_RECURRENCE_MOD_ALL)
+    {
+      GcalEvent *main_event;
+
+      main_event = gcal_event_new_main_event_from_instance_event (event);
+
+      if (main_event != NULL)
+        event = main_event;
+    }
+
   calendar = gcal_event_get_calendar (event);
   component = gcal_event_get_component (event);
 
-  /*
-   * HACK: In Evolution Calendar, a NULL 'rid' is usually associated
-   * with an E_CAL_OBJ_MOD_ALL modtype. Here, we are manually setting
-   * the rid to NULL when modifying a recurrent event with MOD_ALL
-   * modtype.
-   */
-  if (mod == GCAL_RECURRENCE_MOD_ALL)
-    e_cal_component_set_recurid (component, NULL);
-
   /*
    * While we're updating the event, we don't want the component
    * to be destroyed, so take a reference of the component while


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