[tepl] File loading: add basic API to open a GFile



commit bb0195c5696107c04e987014c489f4e09f54ccd4
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Oct 7 09:59:34 2017 +0200

    File loading: add basic API to open a GFile
    
    Start with a simple implementation.

 docs/reference/tepl-3.0-sections.txt |    2 +
 tepl/tepl-application-window.c       |   44 ++++++++++++++
 tepl/tepl-application-window.h       |    3 +
 tepl/tepl-tab.c                      |  110 ++++++++++++++++++++++++++++++++++
 tepl/tepl-tab.h                      |    3 +
 5 files changed, 162 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/tepl-3.0-sections.txt b/docs/reference/tepl-3.0-sections.txt
index cc98aa1..bfa5322 100644
--- a/docs/reference/tepl-3.0-sections.txt
+++ b/docs/reference/tepl-3.0-sections.txt
@@ -216,6 +216,7 @@ tepl_application_window_get_application_window
 tepl_application_window_set_tab_group
 tepl_application_window_get_handle_title
 tepl_application_window_set_handle_title
+tepl_application_window_open_file
 <SUBSECTION Standard>
 TEPL_APPLICATION_WINDOW
 TEPL_APPLICATION_WINDOW_CLASS
@@ -450,6 +451,7 @@ tepl_tab_new_with_view
 tepl_tab_get_view
 tepl_tab_get_buffer
 tepl_tab_add_info_bar
+tepl_tab_load_file
 <SUBSECTION Standard>
 TEPL_IS_TAB
 TEPL_IS_TAB_CLASS
diff --git a/tepl/tepl-application-window.c b/tepl/tepl-application-window.c
index c6c2deb..c096b8f 100644
--- a/tepl/tepl-application-window.c
+++ b/tepl/tepl-application-window.c
@@ -24,6 +24,7 @@
 #include "tepl-abstract-factory.h"
 #include "tepl-application.h"
 #include "tepl-buffer.h"
+#include "tepl-tab.h"
 #include "tepl-tab-group.h"
 #include "tepl-view.h"
 #include "tepl-signal-group.h"
@@ -1183,4 +1184,47 @@ tepl_application_window_set_handle_title (TeplApplicationWindow *tepl_window,
        }
 }
 
+/**
+ * tepl_application_window_open_file:
+ * @tepl_window: a #TeplApplicationWindow.
+ * @location: a #GFile.
+ *
+ * Opens a file in @tepl_window. If the active tab is untouched (see
+ * tepl_buffer_is_untouched()), then the file is loaded in that tab. Otherwise a
+ * new tab is created and becomes the new active tab.
+ *
+ * This function is asynchronous, the file loading is done with the
+ * tepl_tab_load_file() function. There is no way to know when the file loading
+ * is finished.
+ *
+ * Since: 3.2
+ */
+void
+tepl_application_window_open_file (TeplApplicationWindow *tepl_window,
+                                  GFile                 *location)
+{
+       TeplTab *tab;
+       TeplBuffer *buffer;
+
+       g_return_if_fail (TEPL_IS_APPLICATION_WINDOW (tepl_window));
+       g_return_if_fail (G_IS_FILE (location));
+
+       tab = tepl_tab_group_get_active_tab (TEPL_TAB_GROUP (tepl_window));
+       buffer = tepl_tab_group_get_active_buffer (TEPL_TAB_GROUP (tepl_window));
+
+       if (buffer == NULL ||
+           !tepl_buffer_is_untouched (buffer))
+       {
+               TeplAbstractFactory *factory;
+
+               factory = tepl_abstract_factory_get_singleton ();
+               tab = tepl_abstract_factory_create_tab (factory);
+               gtk_widget_show (GTK_WIDGET (tab));
+
+               tepl_tab_group_append_tab (TEPL_TAB_GROUP (tepl_window), tab, TRUE);
+       }
+
+       tepl_tab_load_file (tab, location);
+}
+
 /* ex:set ts=8 noet: */
diff --git a/tepl/tepl-application-window.h b/tepl/tepl-application-window.h
index 61db092..49e70a9 100644
--- a/tepl/tepl-application-window.h
+++ b/tepl/tepl-application-window.h
@@ -67,6 +67,9 @@ gboolean              tepl_application_window_get_handle_title                
(TeplApplicationWindow *tepl
 void                   tepl_application_window_set_handle_title                (TeplApplicationWindow 
*tepl_window,
                                                                                 gboolean               
handle_title);
 
+void                   tepl_application_window_open_file                       (TeplApplicationWindow 
*tepl_window,
+                                                                                GFile                 
*location);
+
 G_END_DECLS
 
 #endif /* TEPL_APPLICATION_WINDOW_H */
diff --git a/tepl/tepl-tab.c b/tepl/tepl-tab.c
index 6c443e4..86326a7 100644
--- a/tepl/tepl-tab.c
+++ b/tepl/tepl-tab.c
@@ -17,9 +17,13 @@
  * along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
+#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-info-bar.h"
 #include "tepl-tab-group.h"
 
@@ -438,3 +442,109 @@ tepl_tab_add_info_bar (TeplTab    *tab,
 
        TEPL_TAB_GET_CLASS (tab)->pack_info_bar (tab, info_bar);
 }
+
+static void
+load_metadata_cb (GObject      *source_object,
+                 GAsyncResult *result,
+                 gpointer      user_data)
+{
+       TeplFileMetadata *metadata = TEPL_FILE_METADATA (source_object);
+       TeplTab *tab = TEPL_TAB (user_data);
+       GError *error = NULL;
+
+       tepl_file_metadata_load_finish (metadata, result, &error);
+
+       if (error != NULL)
+       {
+               g_warning ("Error when loading metadata: %s", error->message);
+               g_clear_error (&error);
+       }
+
+       g_object_unref (tab);
+}
+
+static void
+load_file_content_cb (GObject      *source_object,
+                     GAsyncResult *result,
+                     gpointer      user_data)
+{
+       TeplFileLoader *loader = TEPL_FILE_LOADER (source_object);
+       TeplTab *tab = TEPL_TAB (user_data);
+       TeplBuffer *buffer;
+       GError *error = NULL;
+
+       buffer = tepl_tab_get_buffer (tab);
+
+       if (tepl_file_loader_load_finish (loader, result, &error))
+       {
+               TeplFile *file;
+               TeplFileMetadata *metadata;
+
+               // TODO
+               //tepl_file_add_uri_to_recent_manager (buffer);
+
+               file = tepl_buffer_get_file (buffer);
+               metadata = tepl_file_get_file_metadata (file);
+
+               tepl_file_metadata_load_async (metadata,
+                                              G_PRIORITY_DEFAULT,
+                                              NULL,
+                                              load_metadata_cb,
+                                              g_object_ref (tab));
+       }
+
+       if (error != NULL)
+       {
+               TeplInfoBar *info_bar;
+
+               info_bar = tepl_info_bar_new_simple (GTK_MESSAGE_ERROR,
+                                                    _("Error when loading file."),
+                                                    error->message);
+
+               tepl_tab_add_info_bar (tab, GTK_INFO_BAR (info_bar));
+               gtk_widget_show (GTK_WIDGET (info_bar));
+
+               g_clear_error (&error);
+       }
+
+       g_object_unref (loader);
+       g_object_unref (tab);
+}
+
+/**
+ * tepl_tab_load_file:
+ * @tab: a #TeplTab.
+ * @location: a #GFile.
+ *
+ * Unconditionally loads a file in @tab, regardless if there are unsaved changes
+ * in the #GtkTextBuffer. The previous buffer content is lost.
+ *
+ * This function is asynchronous, there is no way to know when the file loading
+ * is finished.
+ *
+ * Since: 3.2
+ */
+void
+tepl_tab_load_file (TeplTab *tab,
+                   GFile   *location)
+{
+       TeplBuffer *buffer;
+       TeplFile *file;
+       TeplFileLoader *loader;
+
+       g_return_if_fail (TEPL_IS_TAB (tab));
+       g_return_if_fail (G_IS_FILE (location));
+
+       buffer = tepl_tab_get_buffer (tab);
+       file = tepl_buffer_get_file (buffer);
+
+       tepl_file_set_location (file, location);
+       loader = tepl_file_loader_new (buffer, file);
+
+       tepl_file_loader_load_async (loader,
+                                    G_PRIORITY_DEFAULT,
+                                    NULL, /* cancellable */
+                                    NULL, NULL, NULL, /* progress */
+                                    load_file_content_cb,
+                                    g_object_ref (tab));
+}
diff --git a/tepl/tepl-tab.h b/tepl/tepl-tab.h
index 762338e..f663fda 100644
--- a/tepl/tepl-tab.h
+++ b/tepl/tepl-tab.h
@@ -94,6 +94,9 @@ TeplBuffer *  tepl_tab_get_buffer             (TeplTab *tab);
 void           tepl_tab_add_info_bar           (TeplTab    *tab,
                                                 GtkInfoBar *info_bar);
 
+void           tepl_tab_load_file              (TeplTab *tab,
+                                                GFile   *location);
+
 G_END_DECLS
 
 #endif /* TEPL_TAB_H */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]