[tepl] close-confirm-dialog-single: handle location == NULL case



commit d538a95f103903fc10dd60c463af96382f13817c
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Dec 1 10:17:16 2017 +0100

    close-confirm-dialog-single: handle location == NULL case

 tepl/tepl-close-confirm-dialog-single.c |   61 ++++++++++++++++++++++++-------
 1 files changed, 47 insertions(+), 14 deletions(-)
---
diff --git a/tepl/tepl-close-confirm-dialog-single.c b/tepl/tepl-close-confirm-dialog-single.c
index 6f5a532..d62ce7a 100644
--- a/tepl/tepl-close-confirm-dialog-single.c
+++ b/tepl/tepl-close-confirm-dialog-single.c
@@ -28,6 +28,9 @@
 #define CAN_CLOSE (TRUE)
 #define CANNOT_CLOSE (FALSE)
 
+#define DIALOG_RESPONSE_SAVE   (1)
+#define DIALOG_RESPONSE_SAVE_AS        (2)
+
 /* When closing a TeplTab, show a message dialog if the buffer is modified. */
 
 static void
@@ -55,14 +58,42 @@ save_tab (GTask *task)
 }
 
 static void
+save_as_tab_cb (GObject      *source_object,
+               GAsyncResult *result,
+               gpointer      user_data)
+{
+       TeplTab *tab = TEPL_TAB (source_object);
+       GTask *task = G_TASK (user_data);
+       gboolean can_close;
+
+       can_close = tepl_tab_save_as_finish (tab, result);
+
+       g_task_return_boolean (task, can_close);
+       g_object_unref (task);
+}
+
+static void
+save_as_tab (GTask *task)
+{
+       TeplTab *tab;
+
+       tab = g_task_get_source_object (task);
+       tepl_tab_save_as_async (tab, save_as_tab_cb, task);
+}
+
+static void
 dialog_response_cb (GtkDialog *dialog,
                    gint       response_id,
                    GTask     *task)
 {
-       if (response_id == GTK_RESPONSE_ACCEPT)
+       if (response_id == DIALOG_RESPONSE_SAVE)
        {
                save_tab (task);
        }
+       else if (response_id == DIALOG_RESPONSE_SAVE_AS)
+       {
+               save_as_tab (task);
+       }
        else if (response_id == GTK_RESPONSE_CLOSE)
        {
                g_task_return_boolean (task, CAN_CLOSE);
@@ -83,12 +114,14 @@ create_dialog (GTask *task)
        TeplTab *tab;
        TeplBuffer *buffer;
        TeplFile *file;
+       GFile *location;
        const gchar *file_short_name;
        GtkWidget *dialog;
 
        tab = g_task_get_source_object (task);
        buffer = tepl_tab_get_buffer (tab);
        file = tepl_buffer_get_file (buffer);
+       location = tepl_file_get_location (file);
 
        file_short_name = tepl_file_get_short_name (file);
 
@@ -103,9 +136,21 @@ create_dialog (GTask *task)
        gtk_dialog_add_buttons (GTK_DIALOG (dialog),
                                _("Close _without Saving"), GTK_RESPONSE_CLOSE,
                                _("_Cancel"), GTK_RESPONSE_CANCEL,
-                               _("_Save"), GTK_RESPONSE_ACCEPT,
                                NULL);
 
+       if (location != NULL)
+       {
+               gtk_dialog_add_button (GTK_DIALOG (dialog),
+                                      _("_Save"),
+                                      DIALOG_RESPONSE_SAVE);
+       }
+       else
+       {
+               gtk_dialog_add_button (GTK_DIALOG (dialog),
+                                      _("_Save As…"),
+                                      DIALOG_RESPONSE_SAVE_AS);
+       }
+
        _tepl_utils_associate_secondary_window (GTK_WINDOW (dialog),
                                                GTK_WIDGET (tab));
 
@@ -124,8 +169,6 @@ _tepl_close_confirm_dialog_single_async (TeplTab             *tab,
 {
        GTask *task;
        TeplBuffer *buffer;
-       TeplFile *file;
-       GFile *location;
 
        g_return_if_fail (TEPL_IS_TAB (tab));
 
@@ -139,16 +182,6 @@ _tepl_close_confirm_dialog_single_async (TeplTab             *tab,
                return;
        }
 
-       file = tepl_buffer_get_file (buffer);
-       location = tepl_file_get_location (file);
-       if (location == NULL)
-       {
-               /* TODO propose to save as. */
-               g_task_return_boolean (task, CANNOT_CLOSE);
-               g_object_unref (task);
-               return;
-       }
-
        create_dialog (task);
 }
 


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