[tepl] File saving: add high-level API for location != NULL case



commit 84f0baba7606444fe9df0844395d09afef4d56af
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Oct 29 14:08:24 2017 +0100

    File saving: add high-level API for location != NULL case

 docs/reference/tepl-4.0-sections.txt |    3 +
 tepl/tepl-tab.c                      |   91 +++++++++++++++++++++++++++++++++-
 tepl/tepl-tab.h                      |    9 +++
 3 files changed, 102 insertions(+), 1 deletions(-)
---
diff --git a/docs/reference/tepl-4.0-sections.txt b/docs/reference/tepl-4.0-sections.txt
index f51104b..cad100b 100644
--- a/docs/reference/tepl-4.0-sections.txt
+++ b/docs/reference/tepl-4.0-sections.txt
@@ -459,6 +459,9 @@ tepl_tab_get_view
 tepl_tab_get_buffer
 tepl_tab_add_info_bar
 tepl_tab_load_file
+tepl_tab_save_async
+tepl_tab_save_finish
+tepl_tab_save_async_simple
 <SUBSECTION Standard>
 TEPL_IS_TAB
 TEPL_IS_TAB_CLASS
diff --git a/tepl/tepl-tab.c b/tepl/tepl-tab.c
index a5b692b..4607840 100644
--- a/tepl/tepl-tab.c
+++ b/tepl/tepl-tab.c
@@ -20,12 +20,14 @@
 #include "config.h"
 #include "tepl-tab.h"
 #include <glib/gi18n-lib.h>
-#include "tepl-view.h"
 #include "tepl-buffer.h"
 #include "tepl-file-loader.h"
 #include "tepl-file-metadata.h"
+#include "tepl-file-saver.h"
 #include "tepl-info-bar.h"
 #include "tepl-tab-group.h"
+#include "tepl-tab-saving.h"
+#include "tepl-view.h"
 
 /**
  * SECTION:tab
@@ -547,3 +549,90 @@ tepl_tab_load_file (TeplTab *tab,
                                     load_file_content_cb,
                                     g_object_ref (tab));
 }
+
+/**
+ * tepl_tab_save_async:
+ * @tab: a #TeplTab.
+ * @callback: (scope async): a #GAsyncReadyCallback to call when the request is
+ *   satisfied.
+ * @user_data: user data to pass to @callback.
+ *
+ * Saves asynchronously the content of the @tab. The #TeplFile:location must not
+ * be %NULL.
+ *
+ * See the #GAsyncResult documentation to know how to use this function.
+ *
+ * Since: 4.0
+ */
+void
+tepl_tab_save_async (TeplTab             *tab,
+                    GAsyncReadyCallback  callback,
+                    gpointer             user_data)
+{
+       TeplBuffer *buffer;
+       TeplFile *file;
+       GFile *location;
+       TeplFileSaver *saver;
+
+       g_return_if_fail (TEPL_IS_TAB (tab));
+
+       buffer = tepl_tab_get_buffer (tab);
+       file = tepl_buffer_get_file (buffer);
+       location = tepl_file_get_location (file);
+       g_return_if_fail (location != NULL);
+
+       saver = tepl_file_saver_new (buffer, file);
+       _tepl_tab_saving_save_async (tab, saver, callback, user_data);
+       g_object_unref (saver);
+}
+
+/**
+ * tepl_tab_save_finish:
+ * @tab: a #TeplTab.
+ * @result: a #GAsyncResult.
+ *
+ * Finishes a tab saving started with tepl_tab_save_async().
+ *
+ * Returns: whether the tab was saved successfully.
+ * Since: 4.0
+ */
+gboolean
+tepl_tab_save_finish (TeplTab      *tab,
+                     GAsyncResult *result)
+{
+       return _tepl_tab_saving_save_finish (tab, result);
+}
+
+static void
+save_async_simple_cb (GObject      *source_object,
+                     GAsyncResult *result,
+                     gpointer      user_data)
+{
+       TeplTab *tab = TEPL_TAB (source_object);
+
+       tepl_tab_save_finish (tab, result);
+       g_object_unref (tab);
+}
+
+/**
+ * tepl_tab_save_async_simple:
+ * @tab: a #TeplTab.
+ *
+ * The same as tepl_tab_save_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_async_simple (TeplTab *tab)
+{
+       g_return_if_fail (TEPL_IS_TAB (tab));
+
+       g_object_ref (tab);
+       tepl_tab_save_async (tab,
+                            save_async_simple_cb,
+                            NULL);
+}
diff --git a/tepl/tepl-tab.h b/tepl/tepl-tab.h
index f663fda..a1a5580 100644
--- a/tepl/tepl-tab.h
+++ b/tepl/tepl-tab.h
@@ -97,6 +97,15 @@ void         tepl_tab_add_info_bar           (TeplTab    *tab,
 void           tepl_tab_load_file              (TeplTab *tab,
                                                 GFile   *location);
 
+void           tepl_tab_save_async             (TeplTab             *tab,
+                                                GAsyncReadyCallback  callback,
+                                                gpointer             user_data);
+
+gboolean       tepl_tab_save_finish            (TeplTab      *tab,
+                                                GAsyncResult *result);
+
+void           tepl_tab_save_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]