[tepl] File saving: add save as API to TeplTab
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tepl] File saving: add save as API to TeplTab
- Date: Fri, 1 Dec 2017 08:13:52 +0000 (UTC)
commit a4010b098d8600c122257323843b10a23797a796
Author: Sébastien Wilmet <swilmet gnome org>
Date: Fri Dec 1 07:22:09 2017 +0100
File saving: add save as API to TeplTab
docs/reference/tepl-4.0-sections.txt | 3 +
tepl/tepl-application-window.c | 61 +-------------
tepl/tepl-tab.c | 159 ++++++++++++++++++++++++++++++++++
tepl/tepl-tab.h | 9 ++
4 files changed, 173 insertions(+), 59 deletions(-)
---
diff --git a/docs/reference/tepl-4.0-sections.txt b/docs/reference/tepl-4.0-sections.txt
index 3a08c65..52ac42f 100644
--- a/docs/reference/tepl-4.0-sections.txt
+++ b/docs/reference/tepl-4.0-sections.txt
@@ -479,6 +479,9 @@ tepl_tab_load_file
tepl_tab_save_async
tepl_tab_save_finish
tepl_tab_save_async_simple
+tepl_tab_save_as_async
+tepl_tab_save_as_finish
+tepl_tab_save_as_async_simple
<SUBSECTION Standard>
TEPL_IS_TAB
TEPL_IS_TAB_CLASS
diff --git a/tepl/tepl-application-window.c b/tepl/tepl-application-window.c
index 4774483..d7b465e 100644
--- a/tepl/tepl-application-window.c
+++ b/tepl/tepl-application-window.c
@@ -22,13 +22,11 @@
#include <amtk/amtk.h>
#include <glib/gi18n-lib.h>
#include "tepl-abstract-factory.h"
-#include "tepl-application.h"
#include "tepl-buffer.h"
-#include "tepl-file-saver.h"
+#include "tepl-file.h"
#include "tepl-signal-group.h"
#include "tepl-tab.h"
#include "tepl-tab-group.h"
-#include "tepl-tab-saving.h"
#include "tepl-view.h"
/**
@@ -231,72 +229,17 @@ save_cb (GSimpleAction *save_action,
}
static void
-save_file_chooser_response_cb (GtkFileChooserDialog *file_chooser_dialog,
- gint response_id,
- TeplTab *tab)
-{
- if (response_id == GTK_RESPONSE_ACCEPT)
- {
- TeplBuffer *buffer;
- TeplFile *file;
- GFile *location;
- TeplFileSaver *saver;
-
- buffer = tepl_tab_get_buffer (tab);
- file = tepl_buffer_get_file (buffer);
- location = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (file_chooser_dialog));
-
- saver = tepl_file_saver_new_with_target (buffer, file, location);
- _tepl_tab_saving_save_async_simple (tab, saver);
- g_object_unref (saver);
-
- g_object_unref (location);
- }
-
- gtk_widget_destroy (GTK_WIDGET (file_chooser_dialog));
-}
-
-static void
save_as_cb (GSimpleAction *save_as_action,
GVariant *parameter,
gpointer user_data)
{
TeplApplicationWindow *tepl_window = TEPL_APPLICATION_WINDOW (user_data);
TeplTab *tab;
- GtkWidget *file_chooser_dialog;
- GtkFileChooser *file_chooser;
- GtkWindowGroup *window_group;
tab = tepl_tab_group_get_active_tab (TEPL_TAB_GROUP (tepl_window));
g_return_if_fail (tab != NULL);
- file_chooser_dialog = gtk_file_chooser_dialog_new (_("Save File"),
- GTK_WINDOW (tepl_window->priv->gtk_window),
- GTK_FILE_CHOOSER_ACTION_SAVE,
- _("_Cancel"), GTK_RESPONSE_CANCEL,
- _("_Save"), GTK_RESPONSE_ACCEPT,
- NULL);
-
- gtk_dialog_set_default_response (GTK_DIALOG (file_chooser_dialog), GTK_RESPONSE_ACCEPT);
-
- /* Prevent tab from being destroyed. */
- gtk_window_set_modal (GTK_WINDOW (file_chooser_dialog), TRUE);
-
- window_group = tepl_application_window_get_window_group (tepl_window);
- gtk_window_group_add_window (window_group, GTK_WINDOW (file_chooser_dialog));
-
- file_chooser = GTK_FILE_CHOOSER (file_chooser_dialog);
-
- gtk_file_chooser_set_do_overwrite_confirmation (file_chooser, TRUE);
- gtk_file_chooser_set_local_only (file_chooser, FALSE);
-
- g_signal_connect_object (file_chooser_dialog,
- "response",
- G_CALLBACK (save_file_chooser_response_cb),
- tab,
- 0);
-
- gtk_widget_show (file_chooser_dialog);
+ tepl_tab_save_as_async_simple (tab);
}
static void
diff --git a/tepl/tepl-tab.c b/tepl/tepl-tab.c
index c4b667e..85e9cbc 100644
--- a/tepl/tepl-tab.c
+++ b/tepl/tepl-tab.c
@@ -28,6 +28,7 @@
#include "tepl-info-bar.h"
#include "tepl-tab-group.h"
#include "tepl-tab-saving.h"
+#include "tepl-utils.h"
#include "tepl-view.h"
/**
@@ -667,3 +668,161 @@ tepl_tab_save_async_simple (TeplTab *tab)
save_async_simple_cb,
NULL);
}
+
+static void
+save_as_cb (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ TeplTab *tab = TEPL_TAB (source_object);
+ GTask *task = G_TASK (user_data);
+ gboolean ok;
+
+ ok = _tepl_tab_saving_save_finish (tab, result);
+
+ g_task_return_boolean (task, ok);
+ g_object_unref (task);
+}
+
+static void
+save_file_chooser_response_cb (GtkFileChooserDialog *file_chooser_dialog,
+ gint response_id,
+ GTask *task)
+{
+ if (response_id == GTK_RESPONSE_ACCEPT)
+ {
+ TeplTab *tab;
+ TeplBuffer *buffer;
+ TeplFile *file;
+ GFile *location;
+ TeplFileSaver *saver;
+
+ tab = g_task_get_source_object (task);
+ buffer = tepl_tab_get_buffer (tab);
+ file = tepl_buffer_get_file (buffer);
+
+ location = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (file_chooser_dialog));
+
+ saver = tepl_file_saver_new_with_target (buffer, file, location);
+ g_object_unref (location);
+
+ _tepl_tab_saving_save_async (tab, saver, save_as_cb, task);
+ g_object_unref (saver);
+ }
+ else
+ {
+ g_task_return_boolean (task, FALSE);
+ g_object_unref (task);
+ }
+
+ gtk_widget_destroy (GTK_WIDGET (file_chooser_dialog));
+}
+
+/**
+ * tepl_tab_save_as_async:
+ * @tab: a #TeplTab.
+ * @callback: (scope async): a #GAsyncReadyCallback to call when the request is
+ * satisfied.
+ * @user_data: user data to pass to @callback.
+ *
+ * Shows a #GtkFileChooser to save the @tab to a different location, creates an
+ * appropriate #TeplFileSaver and asynchronously runs it.
+ *
+ * See the #GAsyncResult documentation to know how to use this function.
+ *
+ * Since: 4.0
+ */
+void
+tepl_tab_save_as_async (TeplTab *tab,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GTask *task;
+ GtkWidget *file_chooser_dialog;
+ GtkFileChooser *file_chooser;
+
+ g_return_if_fail (TEPL_IS_TAB (tab));
+
+ task = g_task_new (tab, NULL, callback, user_data);
+
+ file_chooser_dialog = gtk_file_chooser_dialog_new (_("Save File"),
+ NULL,
+ GTK_FILE_CHOOSER_ACTION_SAVE,
+ _("_Cancel"), GTK_RESPONSE_CANCEL,
+ _("_Save"), GTK_RESPONSE_ACCEPT,
+ NULL);
+
+ gtk_dialog_set_default_response (GTK_DIALOG (file_chooser_dialog), GTK_RESPONSE_ACCEPT);
+
+ /* Prevent tab from being destroyed. */
+ gtk_window_set_modal (GTK_WINDOW (file_chooser_dialog), TRUE);
+
+ _tepl_utils_associate_secondary_window (GTK_WINDOW (file_chooser_dialog),
+ GTK_WIDGET (tab));
+
+ file_chooser = GTK_FILE_CHOOSER (file_chooser_dialog);
+
+ gtk_file_chooser_set_do_overwrite_confirmation (file_chooser, TRUE);
+ gtk_file_chooser_set_local_only (file_chooser, FALSE);
+
+ g_signal_connect (file_chooser_dialog,
+ "response",
+ G_CALLBACK (save_file_chooser_response_cb),
+ task);
+
+ gtk_widget_show (file_chooser_dialog);
+}
+
+/**
+ * tepl_tab_save_as_finish:
+ * @tab: a #TeplTab.
+ * @result: a #GAsyncResult.
+ *
+ * Finishes a tab saving started with tepl_tab_save_as_async().
+ *
+ * Returns: whether the tab was saved successfully.
+ * Since: 4.0
+ */
+gboolean
+tepl_tab_save_as_finish (TeplTab *tab,
+ GAsyncResult *result)
+{
+ g_return_val_if_fail (TEPL_IS_TAB (tab), FALSE);
+ g_return_val_if_fail (g_task_is_valid (result, tab), FALSE);
+
+ return g_task_propagate_boolean (G_TASK (result), NULL);
+}
+
+static void
+save_as_async_simple_cb (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ TeplTab *tab = TEPL_TAB (source_object);
+
+ tepl_tab_save_as_finish (tab, result);
+ g_object_unref (tab);
+}
+
+/**
+ * tepl_tab_save_as_async_simple:
+ * @tab: a #TeplTab.
+ *
+ * The same as tepl_tab_save_as_async(), but without callback.
+ *
+ * This function is useful when you don't need to know:
+ * - when the operation is finished;
+ * - and whether the operation ran successfully.
+ *
+ * Since: 4.0
+ */
+void
+tepl_tab_save_as_async_simple (TeplTab *tab)
+{
+ g_return_if_fail (TEPL_IS_TAB (tab));
+
+ g_object_ref (tab);
+ tepl_tab_save_as_async (tab,
+ save_as_async_simple_cb,
+ NULL);
+}
diff --git a/tepl/tepl-tab.h b/tepl/tepl-tab.h
index a1a5580..25b9328 100644
--- a/tepl/tepl-tab.h
+++ b/tepl/tepl-tab.h
@@ -106,6 +106,15 @@ gboolean tepl_tab_save_finish (TeplTab *tab,
void tepl_tab_save_async_simple (TeplTab *tab);
+void tepl_tab_save_as_async (TeplTab *tab,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+
+gboolean tepl_tab_save_as_finish (TeplTab *tab,
+ GAsyncResult *result);
+
+void tepl_tab_save_as_async_simple (TeplTab *tab);
+
G_END_DECLS
#endif /* TEPL_TAB_H */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]