[gnome-text-editor] savechanges: request confirmation when closing modified page



commit 8c7060dac7cef2c70d01609dcfd40f64118d98ce
Author: Christian Hergert <chergert redhat com>
Date:   Mon Jul 26 17:46:08 2021 -0700

    savechanges: request confirmation when closing modified page
    
    This of course makes things more traditional, which is pretty far from our
    initial experiment to make session handling transparent. Probably for the
    better for now, but long term I'd still like to experiment with it in #78.
    
    Fixes #118 and indirectly makes us more consistent regarding #78

 src/editor-application-actions.c |  5 ++++-
 src/editor-save-changes-dialog.c | 18 ++++++++++++---
 src/editor-window-actions.c      | 48 +++++++++++++++++++++++++++++++++++-----
 src/editor-window.c              |  9 +++++---
 4 files changed, 68 insertions(+), 12 deletions(-)
---
diff --git a/src/editor-application-actions.c b/src/editor-application-actions.c
index 8c41a77..efd3231 100644
--- a/src/editor-application-actions.c
+++ b/src/editor-application-actions.c
@@ -179,10 +179,13 @@ editor_application_actions_confirm_cb (GObject      *object,
                                        gpointer      user_data)
 {
   g_autoptr(EditorApplication) self = user_data;
+  g_autoptr(GError) error = NULL;
 
   g_assert (EDITOR_IS_APPLICATION (self));
 
-  _editor_save_changes_dialog_run_finish (result, NULL);
+  if (!_editor_save_changes_dialog_run_finish (result, &error))
+    return;
+
   editor_session_save_async (self->session,
                              NULL,
                              editor_application_actions_quit_cb,
diff --git a/src/editor-save-changes-dialog.c b/src/editor-save-changes-dialog.c
index 8cfeb72..191b91b 100644
--- a/src/editor-save-changes-dialog.c
+++ b/src/editor-save-changes-dialog.c
@@ -215,11 +215,23 @@ editor_save_changes_dialog_response (GtkMessageDialog *dialog,
   g_assert (GTK_IS_MESSAGE_DIALOG (dialog));
 
   if (response == GTK_RESPONSE_NO)
-    editor_save_changes_dialog_discard (dialog, requests);
+    {
+      editor_save_changes_dialog_discard (dialog, requests);
+    }
   else if (response == GTK_RESPONSE_YES)
-    editor_save_changes_dialog_save (dialog, requests);
+    {
+      editor_save_changes_dialog_save (dialog, requests);
+    }
   else
-    gtk_window_destroy (GTK_WINDOW (dialog));
+    {
+      GTask *task = g_object_get_data (G_OBJECT (dialog), "TASK");
+      g_print ("Returning new error\n");
+      g_task_return_new_error (task,
+                               G_IO_ERROR,
+                               G_IO_ERROR_CANCELLED,
+                               "The user cancelled the request");
+      gtk_window_destroy (GTK_WINDOW (dialog));
+    }
 }
 
 static GtkWidget *
diff --git a/src/editor-window-actions.c b/src/editor-window-actions.c
index 060e9fc..fcf1343 100644
--- a/src/editor-window-actions.c
+++ b/src/editor-window-actions.c
@@ -28,6 +28,7 @@
 #include "editor-document.h"
 #include "editor-language-dialog.h"
 #include "editor-page-private.h"
+#include "editor-save-changes-dialog-private.h"
 #include "editor-session-private.h"
 #include "editor-window-private.h"
 
@@ -45,6 +46,30 @@ editor_window_actions_new_draft_cb (GtkWidget  *widget,
   editor_session_add_draft (session, self);
 }
 
+static void
+editor_window_actions_close_page_confirm_cb (GObject      *object,
+                                             GAsyncResult *result,
+                                             gpointer      user_data)
+{
+  g_autoptr(GPtrArray) pages = user_data;
+  g_autoptr(GError) error = NULL;
+  EditorSession *session;
+
+  g_assert (pages != NULL);
+  g_assert (pages->len > 0);
+
+  if (!_editor_save_changes_dialog_run_finish (result, &error))
+    {
+      g_debug ("Failed to run dialog: %s", error->message);
+      return;
+    }
+
+  session = editor_application_get_session (EDITOR_APPLICATION_DEFAULT);
+
+  for (guint i = 0; i < pages->len; i++)
+    editor_session_remove_page (session, g_ptr_array_index (pages, i));
+}
+
 static void
 editor_window_actions_close_page_cb (GtkWidget  *widget,
                                      const char *action_name,
@@ -58,12 +83,25 @@ editor_window_actions_close_page_cb (GtkWidget  *widget,
 
   session = editor_application_get_session (EDITOR_APPLICATION_DEFAULT);
 
-  page = editor_window_get_visible_page (self);
-  if (page != NULL)
-    editor_session_remove_page (session, page);
+  if ((page = editor_window_get_visible_page (self)))
+    {
+      /* If this document has changes, then request the user to save them. */
+      if (editor_page_get_is_modified (page))
+        {
+          g_autoptr(GPtrArray) pages = g_ptr_array_new_with_free_func (g_object_unref);
+          g_ptr_array_add (pages, g_object_ref (page));
+          _editor_save_changes_dialog_run_async (GTK_WINDOW (self),
+                                                 pages,
+                                                 NULL,
+                                                 editor_window_actions_close_page_confirm_cb,
+                                                 g_ptr_array_ref (pages));
+          return;
+        }
 
-  page = editor_window_get_visible_page (self);
-  if (page == NULL)
+      editor_session_remove_page (session, page);
+    }
+
+  if (!(page = editor_window_get_visible_page (self)))
     gtk_window_close (GTK_WINDOW (self));
 }
 
diff --git a/src/editor-window.c b/src/editor-window.c
index 8868018..0f1455d 100644
--- a/src/editor-window.c
+++ b/src/editor-window.c
@@ -215,13 +215,16 @@ editor_window_confirm_cb (GObject      *object,
                           gpointer      user_data)
 {
   g_autoptr(EditorWindow) self = user_data;
+  g_autoptr(GError) error = NULL;
 
   g_assert (EDITOR_IS_WINDOW (self));
   g_assert (G_IS_ASYNC_RESULT (result));
 
-  _editor_save_changes_dialog_run_finish (result, NULL);
-  editor_window_do_close (self);
-  gtk_window_destroy (GTK_WINDOW (self));
+  if (_editor_save_changes_dialog_run_finish (result, &error))
+    {
+      editor_window_do_close (self);
+      gtk_window_destroy (GTK_WINDOW (self));
+    }
 }
 
 static gboolean


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