[tepl] File saving: write internal _tepl_tab_saving functions
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tepl] File saving: write internal _tepl_tab_saving functions
- Date: Fri, 27 Oct 2017 15:49:00 +0000 (UTC)
commit f8ad8da322949d9780f35fcd72cf48f25834849c
Author: Sébastien Wilmet <swilmet gnome org>
Date: Fri Oct 27 16:37:41 2017 +0200
File saving: write internal _tepl_tab_saving functions
In gedit that code is implemented in GeditTab, but GeditTab is a quite
big class. It's better to delegate the work to other files.
At first I thought about creating a TeplTabSaver class, but actually all
of what is needed is just the async/finish functions, since it's
possible to create a struct for the GTask data.
Let's see how it goes, maybe I'll change my mind later and do
refactorings if needed.
The implementation is currently very simplistic. It'll be improved
later, to handle certain errors by showing different infobars.
docs/reference/Makefile.am | 3 +-
po/POTFILES.in | 1 +
tepl/Makefile.am | 6 +-
tepl/tepl-tab-saving.c | 149 ++++++++++++++++++++++++++++++++++++++++++++
tepl/tepl-tab-saving.h | 44 +++++++++++++
5 files changed, 200 insertions(+), 3 deletions(-)
---
diff --git a/docs/reference/Makefile.am b/docs/reference/Makefile.am
index dc75a26..4787497 100644
--- a/docs/reference/Makefile.am
+++ b/docs/reference/Makefile.am
@@ -33,7 +33,8 @@ IGNORE_HFILES = \
tepl-file-content-loader.h \
tepl-io-error-info-bar.h \
tepl-progress-info-bar.h \
- tepl-signal-group.h
+ tepl-signal-group.h \
+ tepl-tab-saving.h
# Extra options to supply to gtkdoc-mkdb
MKDB_OPTIONS = --xml-mode --output-format=xml
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 02a9bde..3be9975 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -32,5 +32,6 @@ tepl/tepl-signal-group.c
tepl/tepl-tab.c
tepl/tepl-tab-group.c
tepl/tepl-tab-label.c
+tepl/tepl-tab-saving.c
tepl/tepl-utils.c
tepl/tepl-view.c
diff --git a/tepl/Makefile.am b/tepl/Makefile.am
index 27e0440..aa7f03d 100644
--- a/tepl/Makefile.am
+++ b/tepl/Makefile.am
@@ -68,7 +68,8 @@ tepl_private_headers = \
tepl-file-content-loader.h \
tepl-io-error-info-bar.h \
tepl-progress-info-bar.h \
- tepl-signal-group.h
+ tepl-signal-group.h \
+ tepl-tab-saving.h
tepl_private_c_files = \
tepl-buffer-input-stream.c \
@@ -77,7 +78,8 @@ tepl_private_c_files = \
tepl-file-content-loader.c \
tepl-io-error-info-bar.c \
tepl-progress-info-bar.c \
- tepl-signal-group.c
+ tepl-signal-group.c \
+ tepl-tab-saving.c
tepl_built_public_headers = \
tepl-enum-types.h
diff --git a/tepl/tepl-tab-saving.c b/tepl/tepl-tab-saving.c
new file mode 100644
index 0000000..0478af1
--- /dev/null
+++ b/tepl/tepl-tab-saving.c
@@ -0,0 +1,149 @@
+/*
+ * This file is part of Tepl, a text editor library.
+ *
+ * Copyright 2017 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * Tepl is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the
+ * Free Software Foundation; either version 2.1 of the License, or (at your
+ * option) any later version.
+ *
+ * Tepl is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+#include "tepl-tab-saving.h"
+#include <glib/gi18n-lib.h>
+#include "tepl-file.h"
+#include "tepl-file-saver.h"
+#include "tepl-info-bar.h"
+#include "tepl-tab.h"
+
+static void
+launch_saver_cb (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ TeplFileSaver *saver = TEPL_FILE_SAVER (source_object);
+ GTask *task = G_TASK (user_data);
+ TeplTab *tab;
+ GApplication *app;
+ GError *error = NULL;
+ gboolean success;
+
+ tab = g_task_get_source_object (task);
+
+ success = tepl_file_saver_save_finish (saver, result, &error);
+
+ if (success)
+ {
+ TeplFile *file;
+
+ file = tepl_file_saver_get_file (saver);
+ tepl_file_add_uri_to_recent_manager (file);
+ }
+
+ if (error != NULL)
+ {
+ TeplInfoBar *info_bar;
+
+ info_bar = tepl_info_bar_new_simple (GTK_MESSAGE_ERROR,
+ _("Error when saving the file."),
+ error->message);
+ tepl_info_bar_add_close_button (info_bar);
+ tepl_tab_add_info_bar (tab, GTK_INFO_BAR (info_bar));
+ gtk_widget_show (GTK_WIDGET (info_bar));
+
+ g_clear_error (&error);
+ }
+
+ app = g_application_get_default ();
+ g_application_unmark_busy (app);
+ g_application_release (app);
+
+ g_task_return_boolean (task, success);
+ g_object_unref (task);
+}
+
+static void
+launch_saver (GTask *task)
+{
+ TeplFileSaver *saver;
+ GApplication *app;
+
+ saver = g_task_get_task_data (task);
+
+ app = g_application_get_default ();
+ g_application_hold (app);
+ g_application_mark_busy (app);
+
+ tepl_file_saver_save_async (saver,
+ G_PRIORITY_DEFAULT,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ launch_saver_cb,
+ task);
+}
+
+void
+_tepl_tab_saving_save_async (TeplTab *tab,
+ TeplFileSaver *saver,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GTask *task;
+
+ g_return_if_fail (TEPL_IS_TAB (tab));
+ g_return_if_fail (TEPL_IS_FILE_SAVER (saver));
+
+ task = g_task_new (tab, NULL, callback, user_data);
+ g_task_set_task_data (task,
+ g_object_ref (saver),
+ g_object_unref);
+
+ launch_saver (task);
+}
+
+gboolean
+_tepl_tab_saving_save_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_cb (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ TeplTab *tab = TEPL_TAB (source_object);
+
+ _tepl_tab_saving_save_finish (tab, result);
+ g_object_unref (tab);
+}
+
+/* It's also an async function. Useful when we don't care about:
+ * - when the operation is finished.
+ * - the result.
+ */
+void
+_tepl_tab_saving_save (TeplTab *tab,
+ TeplFileSaver *saver)
+{
+ g_return_if_fail (TEPL_IS_TAB (tab));
+ g_return_if_fail (TEPL_IS_FILE_SAVER (saver));
+
+ g_object_ref (tab);
+ _tepl_tab_saving_save_async (tab, saver, save_cb, NULL);
+}
diff --git a/tepl/tepl-tab-saving.h b/tepl/tepl-tab-saving.h
new file mode 100644
index 0000000..61e2ae5
--- /dev/null
+++ b/tepl/tepl-tab-saving.h
@@ -0,0 +1,44 @@
+/*
+ * This file is part of Tepl, a text editor library.
+ *
+ * Copyright 2017 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * Tepl is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the
+ * Free Software Foundation; either version 2.1 of the License, or (at your
+ * option) any later version.
+ *
+ * Tepl is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TEPL_TAB_SAVING_H
+#define TEPL_TAB_SAVING_H
+
+#include <gio/gio.h>
+#include "tepl-types.h"
+
+G_BEGIN_DECLS
+
+G_GNUC_INTERNAL
+void _tepl_tab_saving_save_async (TeplTab *tab,
+ TeplFileSaver *saver,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+
+G_GNUC_INTERNAL
+gboolean _tepl_tab_saving_save_finish (TeplTab *tab,
+ GAsyncResult *result);
+
+G_GNUC_INTERNAL
+void _tepl_tab_saving_save (TeplTab *tab,
+ TeplFileSaver *saver);
+
+G_END_DECLS
+
+#endif /* TEPL_TAB_SAVING_H */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]