[gnome-notes] detached-window: Fix crash when deleting a note item



commit c1c7c356d37ffa91b4e83497fc1c83c6ff5539ff
Author: Jonathan Kang <jonathankang gnome org>
Date:   Sun Apr 25 11:22:27 2021 +0800

    detached-window: Fix crash when deleting a note item

 src/bjb-detached-window.c |  2 ++
 src/bjb-note-view.c       | 28 ----------------------------
 src/bjb-window-base.c     | 39 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 41 insertions(+), 28 deletions(-)
---
diff --git a/src/bjb-detached-window.c b/src/bjb-detached-window.c
index 11227e7..a1837ae 100644
--- a/src/bjb-detached-window.c
+++ b/src/bjb-detached-window.c
@@ -192,6 +192,8 @@ on_trash_cb (GSimpleAction *action,
   /* Delete the note from notebook
    * The deleted note will emit a signal. */
   biji_item_trash (BIJI_ITEM (note));
+
+  gtk_widget_destroy (GTK_WIDGET (self));
 }
 
 static void
diff --git a/src/bjb-note-view.c b/src/bjb-note-view.c
index f1d3328..07c1ef8 100644
--- a/src/bjb-note-view.c
+++ b/src/bjb-note-view.c
@@ -55,7 +55,6 @@ struct _BjbNoteView
 G_DEFINE_TYPE (BjbNoteView, bjb_note_view, GTK_TYPE_OVERLAY)
 
 static void on_window_closed(GtkWidget *window,gpointer note);
-static gboolean on_note_trashed (BijiNoteObj *note, BjbNoteView *view);
 static void on_note_color_changed_cb (BijiNoteObj *note, BjbNoteView *self);
 
 void
@@ -76,7 +75,6 @@ static void
 bjb_note_view_disconnect (BjbNoteView *self)
 {
   g_signal_handlers_disconnect_by_func (self->window, on_window_closed, self->note);
-  g_signal_handlers_disconnect_by_func (self->note, on_note_trashed, self);
   g_signal_handlers_disconnect_by_func (self->note, on_note_color_changed_cb, self);
 }
 
@@ -153,27 +151,6 @@ on_window_closed(GtkWidget *window,gpointer note)
 
 /* Callbacks */
 
-static void
-just_switch_to_main_view(BjbNoteView *self)
-{
-  GtkWindow     *window;
-
-  /* Avoid stupid crash */
-  bjb_note_view_disconnect (self);
-
-  window = GTK_WINDOW(self->window);
-  bjb_window_base_switch_to (BJB_WINDOW_BASE (window),
-                             BJB_WINDOW_BASE_MAIN_VIEW);
-}
-
-static gboolean
-on_note_trashed (BijiNoteObj *note, BjbNoteView *view)
-{
-  just_switch_to_main_view (view);
-  return TRUE;
-}
-
-
 static void
 on_note_color_changed_cb (BijiNoteObj *note, BjbNoteView *self)
 {
@@ -201,11 +178,6 @@ bjb_note_view_constructed (GObject *obj)
   /* view new from note deserializes the note-content. */
   self->view = biji_note_obj_open (self->note);
 
-  g_signal_connect(self->note,"deleted",
-                   G_CALLBACK(on_note_trashed),self);
-  g_signal_connect(self->note,"trashed",
-                   G_CALLBACK(on_note_trashed),self);
-
   g_signal_connect(self->window,"destroy",
                    G_CALLBACK(on_window_closed), self->note);
 
diff --git a/src/bjb-window-base.c b/src/bjb-window-base.c
index 34ac54c..524a1c4 100644
--- a/src/bjb-window-base.c
+++ b/src/bjb-window-base.c
@@ -46,6 +46,8 @@ struct _BjbWindowBase
   gchar                *entry; // FIXME, remove this
 
   gulong                display_notebooks_changed;
+  gulong                note_deleted;
+  gulong                note_trashed;
 
   GtkStack             *stack;
   BjbWindowViewType     current_view;
@@ -202,8 +204,15 @@ bjb_window_base_finalize (GObject *object)
   GSettings *settings = G_SETTINGS (self->settings);
 
   if (self->note != NULL)
+  {
     g_settings_set_string (settings, "last-opened-item", biji_note_obj_get_path (self->note));
 
+    if (self->note_deleted != 0)
+      g_signal_handler_disconnect (self->note, self->note_deleted);
+    if (self->note_trashed != 0)
+      g_signal_handler_disconnect (self->note, self->note_trashed);
+  }
+
   if (self->display_notebooks_changed != 0)
     g_signal_handler_disconnect (self->controller, self->display_notebooks_changed);
 
@@ -500,6 +509,7 @@ on_trash_cb (GSimpleAction *action,
   biji_item_trash (BIJI_ITEM (note));
 
   destroy_note_if_needed (self);
+  bjb_window_base_switch_to (self, BJB_WINDOW_BASE_MAIN_VIEW);
 }
 
 static void
@@ -921,11 +931,35 @@ populate_headerbar_for_note_view (BjbWindowBase *self)
   g_signal_connect (self->note, "changed", G_CALLBACK (on_last_updated_cb), self);
 }
 
+static gboolean
+on_note_trashed (BijiNoteObj *note,
+                 gpointer     user_data)
+{
+  BjbWindowBase *self = BJB_WINDOW_BASE (user_data);
+
+  destroy_note_if_needed (self);
+  bjb_window_base_switch_to (self, BJB_WINDOW_BASE_MAIN_VIEW);
+
+  return TRUE;
+}
+
 void
 bjb_window_base_load_note_item (BjbWindowBase *self, BijiItem *item)
 {
   GtkWidget *w = GTK_WIDGET (self);
 
+  /* Disconnect these two callbacks for previously opened note item. */
+  if (self->note_deleted != 0)
+  {
+    g_signal_handler_disconnect (self->note, self->note_deleted);
+    self->note_deleted = 0;
+  }
+  if (self->note_trashed != 0)
+  {
+    g_signal_handler_disconnect (self->note, self->note_trashed);
+    self->note_trashed = 0;
+  }
+
   destroy_note_if_needed (self);
 
   if (BIJI_IS_NOTE_OBJ (item))
@@ -938,6 +972,11 @@ bjb_window_base_load_note_item (BjbWindowBase *self, BijiItem *item)
     gtk_widget_show (GTK_WIDGET (self->note_view));
     bjb_note_view_grab_focus (self->note_view);
 
+    self->note_deleted = g_signal_connect (self->note, "deleted",
+                                           G_CALLBACK (on_note_trashed), self);
+    self->note_trashed = g_signal_connect (self->note, "trashed",
+                                           G_CALLBACK (on_note_trashed), self);
+
     populate_headerbar_for_note_view (self);
   }
 }


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