[gedit/wip/fix-external-tools: 1/2] commands-file: save next tab in async ready callback
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gedit/wip/fix-external-tools: 1/2] commands-file: save next tab in async ready callback
- Date: Wed, 19 Nov 2014 19:49:14 +0000 (UTC)
commit ebe4698c6f359ed08f53766824b79c73f8c4ac1b
Author: Sébastien Wilmet <swilmet gnome org>
Date: Wed Nov 19 19:09:53 2014 +0100
commands-file: save next tab in async ready callback
gedit/gedit-commands-file.c | 279 +++++++++++++++++++------------------------
1 files changed, 125 insertions(+), 154 deletions(-)
---
diff --git a/gedit/gedit-commands-file.c b/gedit/gedit-commands-file.c
index 9689f55..0736682 100644
--- a/gedit/gedit-commands-file.c
+++ b/gedit/gedit-commands-file.c
@@ -5,6 +5,7 @@
* Copyright (C) 1998, 1999 Alex Roberts, Evan Lawrence
* Copyright (C) 2000, 2001 Chema Celorio, Paolo Maggi
* Copyright (C) 2002-2005 Paolo Maggi
+ * Copyright (C) 2014 Sébastien Wilmet
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -41,7 +42,6 @@
#include "gedit-close-confirmation-dialog.h"
#define GEDIT_OPEN_DIALOG_KEY "gedit-open-dialog-key"
-#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"
#define GEDIT_IS_QUITTING "gedit-is-quitting"
@@ -1047,44 +1047,142 @@ _gedit_cmd_file_save_as (GSimpleAction *action,
}
}
+static void
+quit_if_needed (GeditWindow *window)
+{
+ gboolean is_quitting;
+ gboolean is_quitting_all;
+
+ is_quitting = GPOINTER_TO_BOOLEAN (g_object_get_data (G_OBJECT (window),
+ GEDIT_IS_QUITTING));
+
+ is_quitting_all = GPOINTER_TO_BOOLEAN (g_object_get_data (G_OBJECT (window),
+ GEDIT_IS_QUITTING_ALL));
+
+ if (is_quitting)
+ {
+ gtk_widget_destroy (GTK_WIDGET (window));
+ }
+
+ if (is_quitting_all)
+ {
+ GtkApplication *app;
+
+ app = GTK_APPLICATION (g_application_get_default ());
+
+ if (gtk_application_get_windows (app) == NULL)
+ {
+ g_application_quit (G_APPLICATION (app));
+ }
+ }
+}
+
+static gboolean
+really_close_tab (GeditTab *tab)
+{
+ GtkWidget *toplevel;
+ GeditWindow *window;
+
+ gedit_debug (DEBUG_COMMANDS);
+
+ g_return_val_if_fail (gedit_tab_get_state (tab) == GEDIT_TAB_STATE_CLOSING,
+ FALSE);
+
+ toplevel = gtk_widget_get_toplevel (GTK_WIDGET (tab));
+ g_return_val_if_fail (GEDIT_IS_WINDOW (toplevel), FALSE);
+
+ window = GEDIT_WINDOW (toplevel);
+
+ gedit_window_close_tab (window, tab);
+
+ if (gedit_window_get_active_tab (window) == NULL)
+ {
+ quit_if_needed (window);
+ }
+
+ return FALSE;
+}
+
+static void
+close_tab (GeditTab *tab)
+{
+ GeditDocument *doc;
+
+ doc = gedit_tab_get_document (tab);
+ g_return_if_fail (doc != NULL);
+
+ /* If the user has modified again the document, do not close the tab. */
+ if (_gedit_document_needs_saving (doc))
+ {
+ return;
+ }
+
+ /* Close the document only if it has been succesfully saved.
+ * Tab state is set to CLOSING (it is a state without exiting
+ * transitions) and the tab is closed in an idle handler.
+ */
+ _gedit_tab_mark_for_closing (tab);
+
+ g_idle_add_full (G_PRIORITY_HIGH_IDLE,
+ (GSourceFunc) really_close_tab,
+ tab,
+ NULL);
+}
+
typedef struct _SaveAsData SaveAsData;
struct _SaveAsData
{
+ /* Reffed */
GeditWindow *window;
+
+ /* List of reffed GeditTab's */
GSList *tabs_to_save_as;
+
+ guint close_tabs : 1;
};
+static void save_as_documents_list (SaveAsData *data);
+
static void
-save_documents_list_cb (GeditTab *tab,
- GAsyncResult *result,
- SaveAsData *data)
+save_as_documents_list_cb (GeditTab *tab,
+ GAsyncResult *result,
+ SaveAsData *data)
{
- GeditTab *next_tab;
+ gboolean saved = save_as_tab_finish (tab, result);
- save_as_tab_finish (tab, result);
+ if (saved && data->close_tabs)
+ {
+ close_tab (tab);
+ }
g_return_if_fail (tab == GEDIT_TAB (data->tabs_to_save_as->data));
g_object_unref (data->tabs_to_save_as->data);
data->tabs_to_save_as = g_slist_delete_link (data->tabs_to_save_as,
data->tabs_to_save_as);
- if (data->tabs_to_save_as == NULL)
+ if (data->tabs_to_save_as != NULL)
+ {
+ save_as_documents_list (data);
+ }
+ else
{
g_object_unref (data->window);
g_slice_free (SaveAsData, data);
- return;
}
+}
- /* Save As the next tab of the list. */
- next_tab = GEDIT_TAB (data->tabs_to_save_as->data);
+static void
+save_as_documents_list (SaveAsData *data)
+{
+ GeditTab *next_tab = GEDIT_TAB (data->tabs_to_save_as->data);
gedit_window_set_active_tab (data->window, next_tab);
save_as_tab_async (next_tab,
data->window,
NULL,
- (GAsyncReadyCallback) save_documents_list_cb,
+ (GAsyncReadyCallback) save_as_documents_list_cb,
data);
}
@@ -1134,6 +1232,7 @@ save_documents_list (GeditWindow *window,
data = g_slice_new (SaveAsData);
data->window = g_object_ref (window);
data->tabs_to_save_as = NULL;
+ data->close_tabs = FALSE;
}
data->tabs_to_save_as = g_slist_prepend (data->tabs_to_save_as,
@@ -1181,19 +1280,8 @@ save_documents_list (GeditWindow *window,
if (data != NULL)
{
- GeditTab *tab;
-
data->tabs_to_save_as = g_slist_reverse (data->tabs_to_save_as);
-
- tab = GEDIT_TAB (data->tabs_to_save_as->data);
-
- gedit_window_set_active_tab (window, tab);
-
- save_as_tab_async (tab,
- window,
- NULL,
- (GAsyncReadyCallback) save_documents_list_cb,
- data);
+ save_as_documents_list (data);
}
}
@@ -1448,88 +1536,6 @@ _gedit_cmd_file_revert (GSimpleAction *action,
}
static void
-quit_if_needed (GeditWindow *window)
-{
- gboolean is_quitting;
- gboolean is_quitting_all;
-
- is_quitting = GPOINTER_TO_BOOLEAN (g_object_get_data (G_OBJECT (window),
- GEDIT_IS_QUITTING));
-
- is_quitting_all = GPOINTER_TO_BOOLEAN (g_object_get_data (G_OBJECT (window),
- GEDIT_IS_QUITTING_ALL));
-
- if (is_quitting)
- {
- gtk_widget_destroy (GTK_WIDGET (window));
- }
-
- if (is_quitting_all)
- {
- GtkApplication *app;
-
- app = GTK_APPLICATION (g_application_get_default ());
-
- if (gtk_application_get_windows (app) == NULL)
- {
- g_application_quit (G_APPLICATION (app));
- }
- }
-}
-
-static gboolean
-really_close_tab (GeditTab *tab)
-{
- GtkWidget *toplevel;
- GeditWindow *window;
-
- gedit_debug (DEBUG_COMMANDS);
-
- g_return_val_if_fail (gedit_tab_get_state (tab) == GEDIT_TAB_STATE_CLOSING,
- FALSE);
-
- toplevel = gtk_widget_get_toplevel (GTK_WIDGET (tab));
- g_return_val_if_fail (GEDIT_IS_WINDOW (toplevel), FALSE);
-
- window = GEDIT_WINDOW (toplevel);
-
- gedit_window_close_tab (window, tab);
-
- if (gedit_window_get_active_tab (window) == NULL)
- {
- quit_if_needed (window);
- }
-
- return FALSE;
-}
-
-static void
-close_tab (GeditTab *tab)
-{
- GeditDocument *doc;
-
- doc = gedit_tab_get_document (tab);
- g_return_if_fail (doc != NULL);
-
- /* If the user has modified again the document, do not close the tab. */
- if (_gedit_document_needs_saving (doc))
- {
- return;
- }
-
- /* Close the document only if it has been succesfully saved.
- * Tab state is set to CLOSING (it is a state without exiting
- * transitions) and the tab is closed in an idle handler.
- */
- _gedit_tab_mark_for_closing (tab);
-
- g_idle_add_full (G_PRIORITY_HIGH_IDLE,
- (GSourceFunc) really_close_tab,
- tab,
- NULL);
-}
-
-static void
tab_state_changed_while_saving (GeditTab *tab,
GParamSpec *pspec,
GeditWindow *window)
@@ -1569,34 +1575,6 @@ save_and_close (GeditTab *tab,
}
static void
-save_as_and_close_cb (GeditTab *tab,
- GAsyncResult *result,
- gpointer user_data)
-{
- gboolean saved = save_as_tab_finish (tab, result);
-
- if (saved)
- {
- close_tab (tab);
- }
-}
-
-static void
-save_as_and_close (GeditTab *tab,
- GeditWindow *window)
-{
- gedit_debug (DEBUG_COMMANDS);
-
- gedit_window_set_active_tab (window, tab);
-
- save_as_tab_async (tab,
- window,
- NULL,
- (GAsyncReadyCallback) save_as_and_close_cb,
- NULL);
-}
-
-static void
save_and_close_documents (GList *docs,
GeditWindow *window,
GeditNotebook *notebook)
@@ -1604,7 +1582,7 @@ save_and_close_documents (GList *docs,
GList *tabs;
GList *l;
GSList *sl;
- GSList *tabs_to_save_as = NULL;
+ SaveAsData *data = NULL;
GSList *tabs_to_save_and_close = NULL;
GList *tabs_to_close = NULL;
@@ -1676,11 +1654,16 @@ save_and_close_documents (GList *docs,
if (gedit_document_is_untitled (doc) ||
gedit_document_get_readonly (doc))
{
- g_object_set_data (G_OBJECT (tab),
- GEDIT_IS_CLOSING_TAB,
- GBOOLEAN_TO_POINTER (TRUE));
+ if (data == NULL)
+ {
+ data = g_slice_new (SaveAsData);
+ data->window = g_object_ref (window);
+ data->tabs_to_save_as = NULL;
+ data->close_tabs = TRUE;
+ }
- tabs_to_save_as = g_slist_prepend (tabs_to_save_as, tab);
+ data->tabs_to_save_as = g_slist_prepend (data->tabs_to_save_as,
+ g_object_ref (tab));
}
else
{
@@ -1709,23 +1692,11 @@ save_and_close_documents (GList *docs,
g_slist_free (tabs_to_save_and_close);
- /* Save As and close all the files in tabs_to_save_as */
- if (tabs_to_save_as != NULL)
+ /* Save As and close all the files in data->tabs_to_save_as. */
+ if (data != NULL)
{
- GeditTab *tab;
-
- tabs_to_save_as = g_slist_reverse (tabs_to_save_as);
-
- g_return_if_fail (g_object_get_data (G_OBJECT (window),
- GEDIT_LIST_OF_TABS_TO_SAVE_AS) == NULL);
-
- g_object_set_data (G_OBJECT (window),
- GEDIT_LIST_OF_TABS_TO_SAVE_AS,
- tabs_to_save_as);
-
- tab = GEDIT_TAB (tabs_to_save_as->data);
-
- save_as_and_close (tab, window);
+ data->tabs_to_save_as = g_slist_reverse (data->tabs_to_save_as);
+ save_as_documents_list (data);
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]