[gnome-calendar/gbsneto/edit-dialog-cleanup: 16/23] event-editor-dialog: Stop using response type




commit c1bf2a0573ac4176faf3c40fc739510f31669bdd
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Sat Oct 31 14:35:04 2020 -0300

    event-editor-dialog: Stop using response type
    
    Using dialog responses to communicate intent to GcalWindow was always
    a hack, and as the code is cleaned, it's getting increasingly annoying.
    
    Replace this with a proper 'remove-event' signal ('delete-event' would
    conflict with the equally named GTK signal), remove the custom response
    types, and cleanup the code.

 src/gui/event-editor/gcal-event-editor-dialog.c  | 151 ++++++++++++++---------
 src/gui/event-editor/gcal-event-editor-dialog.h  |  10 --
 src/gui/event-editor/gcal-event-editor-dialog.ui |   6 +-
 src/gui/gcal-window.c                            |  97 ++++-----------
 src/gui/gcal-window.ui                           |   2 +-
 5 files changed, 119 insertions(+), 147 deletions(-)
---
diff --git a/src/gui/event-editor/gcal-event-editor-dialog.c b/src/gui/event-editor/gcal-event-editor-dialog.c
index 7bb82a5b..66c7de4e 100644
--- a/src/gui/event-editor/gcal-event-editor-dialog.c
+++ b/src/gui/event-editor/gcal-event-editor-dialog.c
@@ -102,6 +102,13 @@ enum
   N_PROPS
 };
 
+enum
+{
+  REMOVE_EVENT,
+  NUM_SIGNALS,
+};
+
+static guint signals[NUM_SIGNALS] = { 0, };
 static GParamSpec* properties[N_PROPS] = { NULL, };
 
 static const GActionEntry action_entries[] =
@@ -198,6 +205,13 @@ set_writable (GcalEventEditorDialog *self,
   g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_WRITABLE]);
 }
 
+static void
+clear_and_hide_dialog (GcalEventEditorDialog *self)
+{
+  gcal_event_editor_dialog_set_event (self, NULL);
+  gtk_widget_hide (GTK_WIDGET (self));
+}
+
 
 /*
  * Callbacks
@@ -264,59 +278,87 @@ transient_size_allocate_cb (GcalEventEditorDialog *self)
 }
 
 static void
-on_action_button_clicked_cb (GtkWidget *widget,
-                             gpointer   user_data)
+on_cancel_button_clicked_cb (GtkButton             *button,
+                             GcalEventEditorDialog *self)
 {
-  GcalEventEditorDialog *self;
+  GCAL_ENTRY;
+
+  clear_and_hide_dialog (self);
+
+  GCAL_EXIT;
+}
+
+static void
+on_delete_button_clicked_cb (GtkButton             *button,
+                             GcalEventEditorDialog *self)
+{
+  GcalRecurrenceModType mod = GCAL_RECURRENCE_MOD_THIS_ONLY;
+  GcalCalendar *calendar;
 
   GCAL_ENTRY;
 
-  self = GCAL_EVENT_EDITOR_DIALOG (user_data);
+  calendar = gcal_event_get_calendar (self->event);
+  if (!gcal_event_has_recurrence (self->event) ||
+      ask_recurrence_modification_type (GTK_WIDGET (self), &mod, calendar))
+    {
+      g_signal_emit (self, signals[REMOVE_EVENT], 0, self->event, mod);
+    }
+
+  clear_and_hide_dialog (self);
+
+  GCAL_EXIT;
+}
+
+static void
+on_done_button_clicked_cb (GtkButton             *button,
+                           GcalEventEditorDialog *self)
+{
+  GcalCalendar *selected_calendar;
+  GcalCalendar *calendar;
+  GcalManager *manager;
+  gint i;
+
+  for (i = 0; i < G_N_ELEMENTS (self->sections); i++)
+    gcal_event_editor_section_apply (self->sections[i]);
+
+  manager = gcal_context_get_manager (self->context);
+  calendar = gcal_event_get_calendar (self->event);
+  selected_calendar = g_steal_pointer (&self->selected_calendar);
 
-  if (widget == self->cancel_button || (widget == self->done_button && !self->writable))
+  if (selected_calendar && calendar != selected_calendar)
     {
-      gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_CANCEL);
+      if (self->event_is_new)
+        {
+          gcal_event_set_calendar (self->event, selected_calendar);
+        }
+      else
+        {
+          ESource *source = gcal_calendar_get_source (selected_calendar);
+
+          gcal_manager_move_event_to_source (manager, self->event, source);
+          goto out;
+        }
     }
-  else if (widget == self->delete_button)
+
+  if (self->event_is_new)
     {
-      gtk_dialog_response (GTK_DIALOG (self), GCAL_RESPONSE_DELETE_EVENT);
+      gcal_manager_create_event (manager, self->event);
     }
   else
     {
-      GcalCalendar *calendar;
-      gint response;
-      gint i;
+      GcalRecurrenceModType mod = GCAL_RECURRENCE_MOD_THIS_ONLY;
 
-      for (i = 0; i < G_N_ELEMENTS (self->sections); i++)
-        gcal_event_editor_section_apply (self->sections[i]);
-
-      response = self->event_is_new ? GCAL_RESPONSE_CREATE_EVENT : GCAL_RESPONSE_SAVE_EVENT;
-
-      /* Update the source if needed */
-      calendar = gcal_event_get_calendar (self->event);
-
-      if (self->selected_calendar && calendar != self->selected_calendar)
+      if (gcal_event_has_recurrence (self->event) &&
+          !ask_recurrence_modification_type (GTK_WIDGET (self), &mod, calendar))
         {
-          if (self->event_is_new)
-            {
-              gcal_event_set_calendar (self->event, self->selected_calendar);
-            }
-          else
-            {
-              gcal_manager_move_event_to_source (gcal_context_get_manager (self->context),
-                                                 self->event,
-                                                 gcal_calendar_get_source (self->selected_calendar));
-              response = GTK_RESPONSE_CANCEL;
-            }
+          goto out;
         }
 
-      self->selected_calendar = NULL;
-
-      /* Send the response */
-      gtk_dialog_response (GTK_DIALOG (self), response);
+      gcal_manager_update_event (manager, self->event, mod);
     }
 
-  GCAL_EXIT;
+out:
+  clear_and_hide_dialog (self);
 }
 
 
@@ -448,6 +490,15 @@ gcal_event_editor_dialog_class_init (GcalEventEditorDialogClass *klass)
   object_class->get_property = gcal_event_editor_dialog_get_property;
   object_class->set_property = gcal_event_editor_dialog_set_property;
 
+  signals[REMOVE_EVENT] = g_signal_new ("remove-event",
+                                        GCAL_TYPE_EVENT_EDITOR_DIALOG,
+                                        G_SIGNAL_RUN_FIRST,
+                                        0, NULL, NULL, NULL,
+                                        G_TYPE_NONE,
+                                        2,
+                                        GCAL_TYPE_EVENT,
+                                        G_TYPE_INT);
+
   /**
    * GcalEventEditorDialog::event:
    *
@@ -503,7 +554,9 @@ gcal_event_editor_dialog_class_init (GcalEventEditorDialogClass *klass)
 
 
   /* callbacks */
-  gtk_widget_class_bind_template_callback (widget_class, on_action_button_clicked_cb);
+  gtk_widget_class_bind_template_callback (widget_class, on_cancel_button_clicked_cb);
+  gtk_widget_class_bind_template_callback (widget_class, on_delete_button_clicked_cb);
+  gtk_widget_class_bind_template_callback (widget_class, on_done_button_clicked_cb);
 }
 
 static void
@@ -554,22 +607,6 @@ gcal_event_editor_dialog_set_event_is_new (GcalEventEditorDialog *self,
   gtk_widget_set_visible (self->delete_button, !event_is_new);
 }
 
-/**
- * gcal_event_editor_dialog_get_event:
- * @dialog: a #GcalDialog
- *
- * Retrieves the current event being edited by the @dialog.
- *
- * Returns: (transfer none)(nullable): a #GcalEvent
- */
-GcalEvent*
-gcal_event_editor_dialog_get_event (GcalEventEditorDialog *self)
-{
-  g_return_val_if_fail (GCAL_IS_EVENT_EDITOR_DIALOG (self), NULL);
-
-  return self->event;
-}
-
 /**
  * gcal_event_editor_dialog_set_event:
  * @dialog: a #GcalDialog
@@ -644,11 +681,3 @@ out:
 
   GCAL_EXIT;
 }
-
-gboolean
-gcal_event_editor_dialog_get_recurrence_changed (GcalEventEditorDialog *self)
-{
-  g_return_val_if_fail (GCAL_IS_EVENT_EDITOR_DIALOG (self), FALSE);
-
-  return self->recurrence_changed;
-}
diff --git a/src/gui/event-editor/gcal-event-editor-dialog.h b/src/gui/event-editor/gcal-event-editor-dialog.h
index 0de82d58..16c16d1f 100644
--- a/src/gui/event-editor/gcal-event-editor-dialog.h
+++ b/src/gui/event-editor/gcal-event-editor-dialog.h
@@ -25,10 +25,6 @@
 
 G_BEGIN_DECLS
 
-#define GCAL_RESPONSE_DELETE_EVENT 2
-#define GCAL_RESPONSE_SAVE_EVENT   4
-#define GCAL_RESPONSE_CREATE_EVENT 6
-
 #define GCAL_TYPE_EVENT_EDITOR_DIALOG (gcal_event_editor_dialog_get_type ())
 G_DECLARE_FINAL_TYPE (GcalEventEditorDialog, gcal_event_editor_dialog, GCAL, EVENT_EDITOR_DIALOG, GtkDialog);
 
@@ -37,13 +33,7 @@ GtkWidget*           gcal_event_editor_dialog_new                (void);
 void                 gcal_event_editor_dialog_set_event_is_new   (GcalEventEditorDialog *dialog,
                                                                   gboolean               event_is_new);
 
-GcalEvent*           gcal_event_editor_dialog_get_event          (GcalEventEditorDialog *self);
-
 void                 gcal_event_editor_dialog_set_event          (GcalEventEditorDialog *self,
                                                                   GcalEvent             *event);
 
-gboolean             gcal_event_editor_dialog_get_recurrence_changed   (GcalEventEditorDialog *self);
-
-GcalRecurrenceModType gcal_event_editor_dialog_get_recurrence_mod_type (GcalEventEditorDialog *self);
-
 G_END_DECLS
diff --git a/src/gui/event-editor/gcal-event-editor-dialog.ui 
b/src/gui/event-editor/gcal-event-editor-dialog.ui
index 6f849486..d7583121 100644
--- a/src/gui/event-editor/gcal-event-editor-dialog.ui
+++ b/src/gui/event-editor/gcal-event-editor-dialog.ui
@@ -17,7 +17,7 @@
             <property name="label" translatable="yes">Cancel</property>
             <property name="visible">True</property>
             <property name="can_focus">True</property>
-            <signal name="clicked" handler="on_action_button_clicked_cb" object="GcalEventEditorDialog" 
swapped="no"/>
+            <signal name="clicked" handler="on_cancel_button_clicked_cb" object="GcalEventEditorDialog" 
swapped="no"/>
           </object>
         </child>
         <child>
@@ -109,7 +109,7 @@
             <property name="visible">True</property>
             <property name="can_focus">True</property>
             <property name="receives_default">True</property>
-            <signal name="clicked" handler="on_action_button_clicked_cb" object="GcalEventEditorDialog" 
swapped="no"/>
+            <signal name="clicked" handler="on_done_button_clicked_cb" object="GcalEventEditorDialog" 
swapped="no"/>
             <style>
               <class name="suggested-action"/>
             </style>
@@ -218,7 +218,7 @@
                     <property name="visible">True</property>
                     <property name="halign">end</property>
                     <property name="can_focus">True</property>
-                    <signal name="clicked" handler="on_action_button_clicked_cb" 
object="GcalEventEditorDialog" swapped="no"/>
+                    <signal name="clicked" handler="on_delete_button_clicked_cb" 
object="GcalEventEditorDialog" swapped="no"/>
                     <property name="sensitive" bind-source="GcalEventEditorDialog" bind-property="writable" 
bind-flags="default" />
                     <child>
                       <object class="GtkLabel" id="delete_label">
diff --git a/src/gui/gcal-window.c b/src/gui/gcal-window.c
index 24338cc2..493295d6 100644
--- a/src/gui/gcal-window.c
+++ b/src/gui/gcal-window.c
@@ -657,91 +657,44 @@ event_activated (GcalView        *view,
 }
 
 static void
-edit_dialog_closed (GtkDialog *dialog,
-                    gint       response,
-                    gpointer   user_data)
+on_event_editor_dialog_remove_event_cb (GcalEventEditorDialog *edit_dialog,
+                                        GcalEvent             *event,
+                                        GcalRecurrenceModType  modifier,
+                                        GcalWindow            *self)
 {
-  GcalEventEditorDialog *edit_dialog;
-  GcalRecurrenceModType mod;
-  GcalCalendar *calendar;
+  g_autoptr (GList) widgets = NULL;
   GcalManager *manager;
-  GcalWindow *window;
-  GcalEvent *event;
   GcalView *view;
-  GList *widgets;
 
   GCAL_ENTRY;
 
-  window = GCAL_WINDOW (user_data);
-  manager = gcal_context_get_manager (window->context);
-  edit_dialog = GCAL_EVENT_EDITOR_DIALOG (dialog);
-  event = gcal_event_editor_dialog_get_event (edit_dialog);
-  view = GCAL_VIEW (window->views[window->active_view]);
-  mod = GCAL_RECURRENCE_MOD_THIS_ONLY;
-  calendar = gcal_event_get_calendar (event);
-
-  if (!gcal_event_editor_dialog_get_recurrence_changed (edit_dialog) &&
-      gcal_event_has_recurrence (event) &&
-      (response != GCAL_RESPONSE_CREATE_EVENT &&
-       response != GTK_RESPONSE_CANCEL &&
-       response != GTK_RESPONSE_DELETE_EVENT &&
-       gcal_event_has_recurrence (event) &&
-       !ask_recurrence_modification_type (GTK_WIDGET (dialog), &mod, calendar)))
-    {
-      return;
-    }
-
+  manager = gcal_context_get_manager (self->context);
+  view = GCAL_VIEW (self->views[self->active_view]);
 
-  switch (response)
+  if (self->event_to_delete)
     {
-    case GCAL_RESPONSE_CREATE_EVENT:
-      gcal_manager_create_event (manager, event);
-      break;
-
-    case GCAL_RESPONSE_SAVE_EVENT:
-      gcal_manager_update_event (manager, event, mod);
-      break;
-
-    case GCAL_RESPONSE_DELETE_EVENT:
-      if (window->event_to_delete != NULL)
-        {
-          gcal_manager_remove_event (manager, window->event_to_delete, window->event_to_delete_mod);
-          g_clear_object (&window->event_to_delete);
+      gcal_manager_remove_event (manager, self->event_to_delete, self->event_to_delete_mod);
+      g_clear_object (&self->event_to_delete);
 
-          create_notification (GCAL_WINDOW (user_data), _("Another event deleted"), _("Undo"));
-        }
-      else
-        {
-          create_notification (GCAL_WINDOW (user_data), _("Event deleted"), _("Undo"));
-        }
-
-      gtk_revealer_set_reveal_child (GTK_REVEALER (window->notification), TRUE);
-
-      if (window->notification_timeout != 0)
-        g_source_remove (window->notification_timeout);
-
-      window->notification_timeout = g_timeout_add_seconds (5, hide_notification_scheduled, user_data);
-
-      g_set_object (&window->event_to_delete, event);
-
-      window->event_to_delete_mod = mod;
-
-      /* hide widget of the event */
-      widgets = gcal_view_get_children_by_uuid (view, mod, gcal_event_get_uid (event));
+      create_notification (self, _("Another event deleted"), _("Undo"));
+    }
+  else
+    {
+      create_notification (self, _("Event deleted"), _("Undo"));
+    }
 
-      g_list_foreach (widgets, (GFunc) gtk_widget_hide, NULL);
-      g_list_free (widgets);
-      break;
+  gtk_revealer_set_reveal_child (GTK_REVEALER (self->notification), TRUE);
 
-    case GTK_RESPONSE_CANCEL:
-    default:
-      break;
+  g_clear_handle_id (&self->notification_timeout, g_source_remove);
+  self->notification_timeout = g_timeout_add_seconds (5, hide_notification_scheduled, self);
 
-    }
+  g_set_object (&self->event_to_delete, event);
+  self->event_to_delete_mod = modifier;
 
-  gtk_widget_hide (GTK_WIDGET (dialog));
+  /* hide widget of the event */
+  widgets = gcal_view_get_children_by_uuid (view, modifier, gcal_event_get_uid (event));
 
-  gcal_event_editor_dialog_set_event (edit_dialog, NULL);
+  g_list_foreach (widgets, (GFunc) gtk_widget_hide, NULL);
 
   GCAL_EXIT;
 }
@@ -1102,7 +1055,7 @@ gcal_window_class_init (GcalWindowClass *klass)
   gtk_widget_class_bind_template_callback (widget_class, window_state_changed);
 
   /* Edit dialog related */
-  gtk_widget_class_bind_template_callback (widget_class, edit_dialog_closed);
+  gtk_widget_class_bind_template_callback (widget_class, on_event_editor_dialog_remove_event_cb);
 }
 
 static void
diff --git a/src/gui/gcal-window.ui b/src/gui/gcal-window.ui
index 99aa25cc..b50cac7b 100644
--- a/src/gui/gcal-window.ui
+++ b/src/gui/gcal-window.ui
@@ -299,7 +299,7 @@
   <object class="GcalEventEditorDialog" id="event_editor">
     <property name="visible">False</property>
     <property name="transient_for">GcalWindow</property>
-    <signal name="response" handler="edit_dialog_closed" object="GcalWindow" swapped="no"/>
+    <signal name="remove-event" handler="on_event_editor_dialog_remove_event_cb" object="GcalWindow" 
swapped="no"/>
     <signal name="delete-event" handler="gtk_widget_hide_on_delete" object="GcalWindow" swapped="no"/>
   </object>
   


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