[gedit/wip/fix-external-tools] commands-file: save_as_tab_async()/finish() (wip)



commit 143de069f9b800af7a9f6220b0ab87ff9ce56318
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Nov 19 15:38:56 2014 +0100

    commands-file: save_as_tab_async()/finish() (wip)

 gedit/gedit-commands-file.c |  164 ++++++++++++++++++++++++-------------------
 1 files changed, 91 insertions(+), 73 deletions(-)
---
diff --git a/gedit/gedit-commands-file.c b/gedit/gedit-commands-file.c
index 8dde228..069b64a 100644
--- a/gedit/gedit-commands-file.c
+++ b/gedit/gedit-commands-file.c
@@ -41,7 +41,6 @@
 #include "gedit-close-confirmation-dialog.h"
 
 #define GEDIT_OPEN_DIALOG_KEY "gedit-open-dialog-key"
-#define GEDIT_TAB_TO_SAVE_AS "gedit-tab-to-save-as"
 #define GEDIT_LIST_OF_TABS_TO_SAVE_AS "gedit-list-of-tabs-to-save-as"
 #define GEDIT_IS_CLOSING_ALL "gedit-is-closing-all"
 #define GEDIT_NOTEBOOK_TO_CLOSE "gedit-notebook-to-close"
@@ -705,102 +704,100 @@ get_compression_type_from_file (GFile *file)
 }
 
 static void
-save_finish_cb (GeditTab     *tab,
-               GAsyncResult *result,
-               gpointer      user_data)
+save_as_finish_cb (GeditTab     *tab,
+                  GAsyncResult *result,
+                  GTask        *task)
 {
-       _gedit_tab_save_finish (tab, result);
+       gboolean success = _gedit_tab_save_finish (tab, result);
+       g_task_return_boolean (task, success);
+       g_object_unref (task);
 }
 
 static void
 save_dialog_response_cb (GeditFileChooserDialog *dialog,
-                         gint                    response_id,
-                         GeditWindow            *window)
+                        gint                    response_id,
+                        GTask                  *task)
 {
        GeditTab *tab;
-       gpointer data;
-       GSList *tabs_to_save_as;
+       GeditWindow *window;
+       GeditDocument *doc;
+       GtkSourceFile *file;
+       GFile *location;
+       gchar *parse_name;
+       GtkSourceNewlineType newline_type;
+       GtkSourceCompressionType compression_type;
+       GtkSourceCompressionType current_compression_type;
+       const GtkSourceEncoding *encoding;
 
        gedit_debug (DEBUG_COMMANDS);
 
-       tab = GEDIT_TAB (g_object_get_data (G_OBJECT (dialog),
-                                           GEDIT_TAB_TO_SAVE_AS));
+       tab = g_task_get_source_object (task);
+       window = g_task_get_task_data (task);
 
        if (response_id != GTK_RESPONSE_OK)
        {
                gedit_file_chooser_dialog_destroy (dialog);
-
-               goto save_next_tab;
+               g_task_return_boolean (task, FALSE);
+               g_object_unref (task);
+               return;
        }
 
-       if (tab != NULL)
-       {
-               GFile *location;
-               GeditDocument *doc;
-               GtkSourceFile *file;
-               gchar *parse_name;
-               GtkSourceNewlineType newline_type;
-               GtkSourceCompressionType compression_type;
-               GtkSourceCompressionType current_compression_type;
-               const GtkSourceEncoding *encoding;
+       doc = gedit_tab_get_document (tab);
+       file = gedit_document_get_file (doc);
 
-               doc = gedit_tab_get_document (tab);
-               file = gedit_document_get_file (doc);
+       location = gedit_file_chooser_dialog_get_file (dialog);
+       g_return_if_fail (location != NULL);
 
-               location = gedit_file_chooser_dialog_get_file (dialog);
-               g_return_if_fail (location != NULL);
+       compression_type = get_compression_type_from_file (location);
+       current_compression_type = gtk_source_file_get_compression_type (file);
 
-               compression_type = get_compression_type_from_file (location);
-               current_compression_type = gtk_source_file_get_compression_type (file);
+       if ((compression_type == GTK_SOURCE_COMPRESSION_TYPE_NONE) !=
+           (current_compression_type == GTK_SOURCE_COMPRESSION_TYPE_NONE))
+       {
+               GtkWindow *dialog_window = gedit_file_chooser_dialog_get_window (dialog);
 
-               if ((compression_type == GTK_SOURCE_COMPRESSION_TYPE_NONE) !=
-                   (current_compression_type == GTK_SOURCE_COMPRESSION_TYPE_NONE))
+               if (!change_compression (dialog_window,
+                                        location,
+                                        compression_type != GTK_SOURCE_COMPRESSION_TYPE_NONE))
                {
-                       GtkWindow *dialog_window = gedit_file_chooser_dialog_get_window (dialog);
-
-                       if (!change_compression (dialog_window,
-                                                location,
-                                                compression_type != GTK_SOURCE_COMPRESSION_TYPE_NONE))
-                       {
-                               gedit_file_chooser_dialog_destroy (dialog);
-                               g_object_unref (location);
+                       gedit_file_chooser_dialog_destroy (dialog);
+                       g_object_unref (location);
 
-                               goto save_next_tab;
-                       }
+                       g_task_return_boolean (task, FALSE);
+                       g_object_unref (task);
+                       return;
                }
+       }
 
-               encoding = gedit_file_chooser_dialog_get_encoding (dialog);
-               newline_type = gedit_file_chooser_dialog_get_newline_type (dialog);
-
-               gedit_file_chooser_dialog_destroy (dialog);
+       encoding = gedit_file_chooser_dialog_get_encoding (dialog);
+       newline_type = gedit_file_chooser_dialog_get_newline_type (dialog);
 
-               doc = gedit_tab_get_document (tab);
-               g_return_if_fail (GEDIT_IS_DOCUMENT (doc));
+       gedit_file_chooser_dialog_destroy (dialog);
 
-               parse_name = g_file_get_parse_name (location);
+       parse_name = g_file_get_parse_name (location);
 
-               gedit_statusbar_flash_message (GEDIT_STATUSBAR (window->priv->statusbar),
-                                               window->priv->generic_message_cid,
-                                              _("Saving file '%s'\342\200\246"),
-                                              parse_name);
+       gedit_statusbar_flash_message (GEDIT_STATUSBAR (window->priv->statusbar),
+                                      window->priv->generic_message_cid,
+                                      _("Saving file '%s'\342\200\246"),
+                                      parse_name);
 
-               g_free (parse_name);
+       g_free (parse_name);
 
-               /* Let's remember the dir we navigated to, even if the saving fails... */
-               _gedit_window_set_default_location (window, location);
+       /* Let's remember the dir we navigated to, even if the saving fails... */
+       _gedit_window_set_default_location (window, location);
 
-               _gedit_tab_save_as_async (tab,
-                                         location,
-                                         encoding,
-                                         newline_type,
-                                         compression_type,
-                                         NULL,
-                                         (GAsyncReadyCallback) save_finish_cb,
-                                         NULL);
+       _gedit_tab_save_as_async (tab,
+                                 location,
+                                 encoding,
+                                 newline_type,
+                                 compression_type,
+                                 g_task_get_cancellable (task),
+                                 (GAsyncReadyCallback) save_as_finish_cb,
+                                 task);
 
-               g_object_unref (location);
-       }
+       g_object_unref (location);
 
+#if 0
 save_next_tab:
 
        data = g_object_get_data (G_OBJECT (window),
@@ -843,6 +840,7 @@ save_next_tab:
                gedit_window_set_active_tab (window, tab);
                save_as_tab (tab, window);
        }
+#endif
 }
 
 static GtkFileChooserConfirmation
@@ -883,9 +881,13 @@ confirm_overwrite_callback (GeditFileChooserDialog *dialog,
 }
 
 static void
-save_as_tab (GeditTab    *tab,
-            GeditWindow *window)
+save_as_tab_async (GeditTab            *tab,
+                  GeditWindow         *window,
+                  GCancellable        *cancellable,
+                  GAsyncReadyCallback  callback,
+                  gpointer             user_data)
 {
+       GTask *task;
        GeditFileChooserDialog *save_dialog;
        GtkWindowGroup *window_group;
        GtkWindow *dialog_window;
@@ -900,6 +902,9 @@ save_as_tab (GeditTab    *tab,
 
        gedit_debug (DEBUG_COMMANDS);
 
+       task = g_task_new (tab, cancellable, callback, user_data);
+       g_task_set_task_data (task, g_object_ref (window), g_object_unref);
+
        save_dialog = gedit_file_chooser_dialog_create (_("Save As"),
                                                        GTK_WINDOW (window),
                                                        GEDIT_FILE_CHOOSER_SAVE |
@@ -960,7 +965,7 @@ save_as_tab (GeditTab    *tab,
                g_free (docname);
        }
 
-       /* Set suggested encoding */
+       /* Set suggested encoding and newline type. */
        encoding = gtk_source_file_get_encoding (file);
 
        if (encoding == NULL)
@@ -976,18 +981,31 @@ save_as_tab (GeditTab    *tab,
        gedit_file_chooser_dialog_set_newline_type (GEDIT_FILE_CHOOSER_DIALOG (save_dialog),
                                                    newline_type);
 
-       g_object_set_data (G_OBJECT (save_dialog),
-                          GEDIT_TAB_TO_SAVE_AS,
-                          tab);
-
        g_signal_connect (save_dialog,
                          "response",
                          G_CALLBACK (save_dialog_response_cb),
-                         window);
+                         task);
 
        gedit_file_chooser_dialog_show (save_dialog);
 }
 
+static gboolean
+save_as_tab_finish (GeditTab     *tab,
+                   GAsyncResult *result)
+{
+       g_return_val_if_fail (g_task_is_valid (result, tab), FALSE);
+
+       return g_task_propagate_boolean (G_TASK (result), NULL);
+}
+
+static void
+save_finish_cb (GeditTab     *tab,
+               GAsyncResult *result,
+               gpointer      user_data)
+{
+       _gedit_tab_save_finish (tab, result);
+}
+
 static void
 save_tab (GeditTab    *tab,
          GeditWindow *window)


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