[gedit/wip/fix-external-tools: 2/2] commands-file: async/finish version of gedit_commands_save_document()
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gedit/wip/fix-external-tools: 2/2] commands-file: async/finish version of gedit_commands_save_document()
- Date: Wed, 19 Nov 2014 19:49:19 +0000 (UTC)
commit ff6dcba5cd6dc03ebd2fdf27e47d9195c4316ba0
Author: Sébastien Wilmet <swilmet gnome org>
Date: Wed Nov 19 20:33:50 2014 +0100
commands-file: async/finish version of gedit_commands_save_document()
The important part is the finish(), to get the result. Because
gedit_commands_save_document() was already async, but it was difficult
to know whether the document was correctly saved (by listening to the
GeditDocument signals it _was_ possible, but it's no longer possible
because the error parameter has been removed from the "saved" signal).
https://bugzilla.gnome.org/show_bug.cgi?id=739686
docs/reference/gedit-sections.txt | 2 +
gedit/gedit-commands-file.c | 135 ++++++++++++++++++++++++++++++++++---
gedit/gedit-commands.h | 9 +++
3 files changed, 135 insertions(+), 11 deletions(-)
---
diff --git a/docs/reference/gedit-sections.txt b/docs/reference/gedit-sections.txt
index ac30577..50e7817 100644
--- a/docs/reference/gedit-sections.txt
+++ b/docs/reference/gedit-sections.txt
@@ -44,6 +44,8 @@ gedit_app_activatable_get_type
gedit_commands_load_location
gedit_commands_load_locations
gedit_commands_save_document
+gedit_commands_save_document_async
+gedit_commands_save_document_finish
gedit_commands_save_all_documents
</SECTION>
diff --git a/gedit/gedit-commands-file.c b/gedit/gedit-commands-file.c
index 0736682..a7c1da2 100644
--- a/gedit/gedit-commands-file.c
+++ b/gedit/gedit-commands-file.c
@@ -954,23 +954,34 @@ save_as_tab_finish (GeditTab *tab,
static void
save_as_tab_ready_cb (GeditTab *tab,
GAsyncResult *result,
- gpointer user_data)
+ GTask *task)
{
- save_as_tab_finish (tab, result);
+ gboolean success = save_as_tab_finish (tab, result);
+
+ g_task_return_boolean (task, success);
+ g_object_unref (task);
}
static void
tab_save_ready_cb (GeditTab *tab,
GAsyncResult *result,
- gpointer user_data)
+ 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);
}
+/* Call save_tab_finish() in @callback. */
static void
-save_tab (GeditTab *tab,
- GeditWindow *window)
+save_tab_async (GeditTab *tab,
+ GeditWindow *window,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
{
+ GTask *task;
GeditDocument *doc;
gchar *uri_for_display;
@@ -982,6 +993,8 @@ save_tab (GeditTab *tab,
doc = gedit_tab_get_document (tab);
g_return_if_fail (GEDIT_IS_DOCUMENT (doc));
+ task = g_task_new (tab, cancellable, callback, user_data);
+
if (gedit_document_is_untitled (doc) ||
gedit_document_get_readonly (doc))
{
@@ -989,9 +1002,9 @@ save_tab (GeditTab *tab,
save_as_tab_async (tab,
window,
- NULL,
+ cancellable,
(GAsyncReadyCallback) save_as_tab_ready_cb,
- NULL);
+ task);
return;
}
@@ -1004,9 +1017,38 @@ save_tab (GeditTab *tab,
g_free (uri_for_display);
_gedit_tab_save_async (tab,
- NULL,
+ cancellable,
(GAsyncReadyCallback) tab_save_ready_cb,
- NULL);
+ task);
+}
+
+static gboolean
+save_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_tab_ready_cb (GeditTab *tab,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ save_tab_finish (tab, result);
+}
+
+/* Save tab asynchronously, but without results. */
+static void
+save_tab (GeditTab *tab,
+ GeditWindow *window)
+{
+ save_tab_async (tab,
+ window,
+ NULL,
+ (GAsyncReadyCallback) save_tab_ready_cb,
+ NULL);
}
void
@@ -1026,6 +1068,14 @@ _gedit_cmd_file_save (GSimpleAction *action,
}
}
+static void
+_gedit_cmd_file_save_as_cb (GeditTab *tab,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ save_as_tab_finish (tab, result);
+}
+
void
_gedit_cmd_file_save_as (GSimpleAction *action,
GVariant *parameter,
@@ -1042,7 +1092,7 @@ _gedit_cmd_file_save_as (GSimpleAction *action,
save_as_tab_async (tab,
window,
NULL,
- (GAsyncReadyCallback) save_as_tab_ready_cb,
+ (GAsyncReadyCallback) _gedit_cmd_file_save_as_cb,
NULL);
}
}
@@ -1324,6 +1374,69 @@ gedit_commands_save_document (GeditWindow *window,
save_tab (tab, window);
}
+/**
+ * gedit_commands_save_document_async:
+ * @window: a #GeditWindow.
+ * @document: the #GeditDocument to save.
+ * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
+ * @callback: (scope async): a #GAsyncReadyCallback to call when the operation
+ * is finished.
+ * @user_data: (closure): the data to pass to the @callback function.
+ *
+ * Asynchronously save the @document. @document must belong to @window.
+ *
+ * When the operation is finished, @callback will be called. You can then call
+ * gedit_commands_save_document_finish() to get the result of the operation.
+ *
+ * Since: 3.14
+ */
+void
+gedit_commands_save_document_async (GeditWindow *window,
+ GeditDocument *document,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GeditTab *tab;
+
+ g_return_if_fail (GEDIT_IS_WINDOW (window));
+ g_return_if_fail (GEDIT_IS_DOCUMENT (document));
+
+ gedit_debug (DEBUG_COMMANDS);
+
+ tab = gedit_tab_get_from_document (document);
+
+ save_tab_async (tab,
+ window,
+ cancellable,
+ callback,
+ user_data);
+}
+
+/**
+ * gedit_commands_save_document_finish:
+ * @document: a #GeditDocument.
+ * @result: a #GAsyncResult.
+ *
+ * Finishes an asynchronous document saving operation started with
+ * gedit_commands_save_document_async().
+ *
+ * Returns: %TRUE if the document has been correctly saved, %FALSE otherwise.
+ * Since: 3.14
+ */
+gboolean
+gedit_commands_save_document_finish (GeditDocument *document,
+ GAsyncResult *result)
+{
+ GeditTab *tab;
+
+ g_return_val_if_fail (GEDIT_IS_DOCUMENT (document), FALSE);
+
+ tab = gedit_tab_get_from_document (document);
+
+ return save_tab_finish (tab, result);
+}
+
/* File revert */
static void
do_revert (GeditWindow *window,
diff --git a/gedit/gedit-commands.h b/gedit/gedit-commands.h
index 2e433aa..59b7790 100644
--- a/gedit/gedit-commands.h
+++ b/gedit/gedit-commands.h
@@ -47,6 +47,15 @@ GSList *gedit_commands_load_locations (GeditWindow
*window,
void gedit_commands_save_document (GeditWindow *window,
GeditDocument *document);
+void gedit_commands_save_document_async (GeditWindow *window,
+ GeditDocument *document,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+
+gboolean gedit_commands_save_document_finish (GeditDocument *document,
+ GAsyncResult *result);
+
void gedit_commands_save_all_documents (GeditWindow *window);
/*
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]