[gedit/wip/loader-saver] tab: basic use of GtkSourceFileLoader



commit 85a85752adc38cce57d8c2a28bf8b80355329887
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Jun 22 19:18:00 2014 +0200

    tab: basic use of GtkSourceFileLoader
    
    There is no error handling nor progress info, but it works!

 gedit/gedit-tab.c |   73 +++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 65 insertions(+), 8 deletions(-)
---
diff --git a/gedit/gedit-tab.c b/gedit/gedit-tab.c
index 15d37e4..d4e1e57 100644
--- a/gedit/gedit-tab.c
+++ b/gedit/gedit-tab.c
@@ -57,9 +57,11 @@ struct _GeditTabPrivate
        GFile                  *tmp_save_location;
 
        /* tmp data for loading */
+       GtkSourceFileLoader    *loader;
+       GCancellable           *cancellable;
        gint                    tmp_line_pos;
        gint                    tmp_column_pos;
-       const GtkSourceEncoding *tmp_encoding;
+       const GtkSourceEncoding *tmp_encoding; /* TODO remove */
 
        GTimer                 *timer;
 
@@ -72,6 +74,9 @@ struct _GeditTabPrivate
        gint                    auto_save : 1;
 
        gint                    ask_if_externally_modified : 1;
+
+       /* tmp data for loading */
+       guint                   load_create : 1;
 };
 
 G_DEFINE_TYPE_WITH_PRIVATE (GeditTab, gedit_tab, GTK_TYPE_BOX)
@@ -1975,6 +1980,34 @@ gedit_tab_get_from_document (GeditDocument *doc)
        return g_object_get_data (G_OBJECT (doc), GEDIT_TAB_KEY);
 }
 
+static void
+progress_cb (goffset   current_num_bytes,
+            goffset   total_num_bytes,
+            GeditTab *tab)
+{
+}
+
+static void
+load_cb (GtkSourceFileLoader *loader,
+        GAsyncResult        *result,
+        GeditTab            *tab)
+{
+       GError *error = NULL;
+
+       gtk_source_file_loader_load_finish (loader, result, &error);
+
+       if (error != NULL)
+       {
+               g_warning ("File loading error: %s", error->message);
+               g_error_free (error);
+       }
+
+       g_clear_object (&tab->priv->loader);
+       g_clear_object (&tab->priv->cancellable);
+
+       gedit_tab_set_state (tab, GEDIT_TAB_STATE_NORMAL);
+}
+
 void
 _gedit_tab_load (GeditTab                *tab,
                 GFile                   *location,
@@ -1984,25 +2017,49 @@ _gedit_tab_load (GeditTab                *tab,
                 gboolean                 create)
 {
        GeditDocument *doc;
+       GtkSourceFile *file;
 
        g_return_if_fail (GEDIT_IS_TAB (tab));
        g_return_if_fail (G_IS_FILE (location));
        g_return_if_fail (tab->priv->state == GEDIT_TAB_STATE_NORMAL);
+       g_return_if_fail (tab->priv->loader == NULL);
+       g_return_if_fail (tab->priv->cancellable == NULL);
 
        doc = gedit_tab_get_document (tab);
+       file = gedit_document_get_file (doc);
 
        gedit_tab_set_state (tab, GEDIT_TAB_STATE_LOADING);
 
+       tab->priv->loader = gtk_source_file_loader_new (file, location);
+
+       if (encoding != NULL)
+       {
+               GSList *list = g_slist_append (NULL, (gpointer) encoding);
+               gtk_source_file_loader_set_candidate_encodings (tab->priv->loader, list);
+               g_slist_free (list);
+       }
+       else
+       {
+               /* TODO */
+       }
+
        tab->priv->tmp_line_pos = line_pos;
        tab->priv->tmp_column_pos = column_pos;
-       tab->priv->tmp_encoding = encoding;
+       tab->priv->load_create = create != FALSE;
 
-       _gedit_document_load (doc,
-                             location,
-                             encoding,
-                             line_pos,
-                             column_pos,
-                             create);
+       tab->priv->cancellable = g_cancellable_new ();
+
+       /* Keep the tab alive during the async operation. */
+       g_object_ref (tab);
+
+       gtk_source_file_loader_load_async (tab->priv->loader,
+                                          G_PRIORITY_DEFAULT,
+                                          tab->priv->cancellable,
+                                          (GFileProgressCallback) progress_cb,
+                                          tab,
+                                          NULL,
+                                          (GAsyncReadyCallback) load_cb,
+                                          tab);
 }
 
 void


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