[gtksourceview/wip/loader-saver] FileLoader: remove dependency to GtkSourceFile



commit 694682413c687f69d046deae4b02dd916b375195
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Mar 26 17:53:36 2014 +0100

    FileLoader: remove dependency to GtkSourceFile

 gtksourceview/gtksourcefileloader.c |  223 +++++++++++++++++++++++++----------
 gtksourceview/gtksourcefileloader.h |   16 ++-
 2 files changed, 168 insertions(+), 71 deletions(-)
---
diff --git a/gtksourceview/gtksourcefileloader.c b/gtksourceview/gtksourcefileloader.c
index c8c8360..5a7f33e 100644
--- a/gtksourceview/gtksourcefileloader.c
+++ b/gtksourceview/gtksourcefileloader.c
@@ -22,12 +22,12 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-/*#include <glib/gstdio.h>*/
 #include <gio/gio.h>
 #include "gtksourcefileloader.h"
 #include "gtksourcebufferoutputstream.h"
 #include "gtksourceencoding.h"
 #include "gtksourceview-typebuiltins.h"
+#include "gtksourceview-i18n.h"
 
 #if 0
 #define DEBUG(x) (x)
@@ -38,6 +38,7 @@
 enum
 {
        PROP_0,
+       PROP_BUFFER,
        PROP_FILE,
        PROP_INPUT_STREAM,
        PROP_NEWLINE_TYPE, /* FIXME needed? */
@@ -54,7 +55,8 @@ enum
 
 struct _GtkSourceFileLoaderPrivate
 {
-       GtkSourceFile *file;
+       GtkSourceBuffer *source_buffer;
+       GFile *file;
 
        /* The value of the "input-stream" property. */
        GInputStream *input_stream_property;
@@ -67,6 +69,9 @@ struct _GtkSourceFileLoaderPrivate
        GtkSourceNewlineType auto_detected_newline_type;
        GtkSourceCompressionType auto_detected_compression_type;
 
+       GtkSourceMountOperationFactory mount_operation_factory;
+       gpointer mount_operation_userdata;
+
        GTask *task;
 
        goffset total_bytes_read;
@@ -90,7 +95,7 @@ struct _GtkSourceFileLoaderPrivate
 
 G_DEFINE_TYPE_WITH_PRIVATE (GtkSourceFileLoader, gtk_source_file_loader, G_TYPE_OBJECT)
 
-static void open_location (GtkSourceFileLoader *loader);
+static void open_file (GtkSourceFileLoader *loader);
 static void read_file_chunk (GtkSourceFileLoader *loader);
 
 static GtkSourceCompressionType
@@ -119,9 +124,16 @@ gtk_source_file_loader_set_property (GObject      *object,
 
        switch (prop_id)
        {
+               case PROP_BUFFER:
+                       g_assert (loader->priv->source_buffer == NULL);
+                       loader->priv->source_buffer = g_value_get_object (value);
+                       g_object_add_weak_pointer (G_OBJECT (loader->priv->source_buffer),
+                                                  (gpointer *)&loader->priv->source_buffer);
+                       break;
+
                case PROP_FILE:
                        g_assert (loader->priv->file == NULL);
-                       loader->priv->file = g_value_get_object (value);
+                       loader->priv->file = g_value_dup_object (value);
                        break;
 
                case PROP_INPUT_STREAM:
@@ -138,6 +150,7 @@ gtk_source_file_loader_set_property (GObject      *object,
                        break;
 
                case PROP_REMOVE_TRAILING_NEWLINE:
+                       g_return_if_fail (loader->priv->task == NULL);
                        loader->priv->remove_trailing_newline = g_value_get_boolean (value);
                        break;
 
@@ -157,6 +170,10 @@ gtk_source_file_loader_get_property (GObject    *object,
 
        switch (prop_id)
        {
+               case PROP_BUFFER:
+                       g_value_set_object (value, loader->priv->source_buffer);
+                       break;
+
                case PROP_FILE:
                        g_value_set_object (value, loader->priv->file);
                        break;
@@ -184,20 +201,34 @@ gtk_source_file_loader_get_property (GObject    *object,
 }
 
 static void
+reset (GtkSourceFileLoader *loader)
+{
+       g_clear_object (&loader->priv->task);
+       g_clear_object (&loader->priv->input_stream);
+       g_clear_object (&loader->priv->output_stream);
+       g_clear_object (&loader->priv->info);
+}
+
+static void
 gtk_source_file_loader_dispose (GObject *object)
 {
-       GtkSourceFileLoaderPrivate *priv;
+       GtkSourceFileLoader *loader = GTK_SOURCE_FILE_LOADER (object);
 
-       priv = GTK_SOURCE_FILE_LOADER (object)->priv;
+       reset (loader);
 
-       g_clear_object (&priv->task);
-       g_clear_object (&priv->input_stream_property);
-       g_clear_object (&priv->input_stream);
-       g_clear_object (&priv->output_stream);
-       g_clear_object (&priv->info);
+       if (loader->priv->source_buffer != NULL)
+       {
+               g_object_remove_weak_pointer (G_OBJECT (loader->priv->source_buffer),
+                                             (gpointer *)&loader->priv->source_buffer);
 
-       g_slist_free (priv->candidate_encodings);
-       priv->candidate_encodings = NULL;
+               loader->priv->source_buffer = NULL;
+       }
+
+       g_clear_object (&loader->priv->file);
+       g_clear_object (&loader->priv->input_stream_property);
+
+       g_slist_free (loader->priv->candidate_encodings);
+       loader->priv->candidate_encodings = NULL;
 
        G_OBJECT_CLASS (gtk_source_file_loader_parent_class)->dispose (object);
 }
@@ -211,19 +242,51 @@ gtk_source_file_loader_class_init (GtkSourceFileLoaderClass *klass)
        object_class->get_property = gtk_source_file_loader_get_property;
        object_class->set_property = gtk_source_file_loader_set_property;
 
+       /**
+        * GtkSourceFileLoader:buffer:
+        *
+        * The output #GtkSourceBuffer.
+        *
+        * Since: 3.14
+        */
+       g_object_class_install_property (object_class, PROP_BUFFER,
+                                        g_param_spec_object ("buffer",
+                                                             "GtkSourceBuffer",
+                                                             "The associated GtkSourceBuffer",
+                                                             GTK_SOURCE_TYPE_BUFFER,
+                                                             G_PARAM_READWRITE |
+                                                             G_PARAM_CONSTRUCT_ONLY |
+                                                             G_PARAM_STATIC_STRINGS));
+
+       /**
+        * GtkSourceFileLoader:file:
+        *
+        * The #GFile to load.
+        *
+        * Since: 3.14
+        */
        g_object_class_install_property (object_class, PROP_FILE,
                                         g_param_spec_object ("file",
-                                                             "File",
-                                                             "The associated GtkSourceFile",
-                                                             GTK_SOURCE_TYPE_FILE,
+                                                             "GFile",
+                                                             "The GFile to load",
+                                                             G_TYPE_FILE,
                                                              G_PARAM_READWRITE |
                                                              G_PARAM_CONSTRUCT_ONLY |
                                                              G_PARAM_STATIC_STRINGS));
 
+       /**
+        * GtkSourceFileLoader:input-stream:
+        *
+        * The #GInputStream to load. Useful for reading e.g. stdin. If this
+        * property is set, it has a higher priority than
+        * the #GtkSourceFileLoader:file property.
+        *
+        * Since: 3.14
+        */
        g_object_class_install_property (object_class, PROP_INPUT_STREAM,
                                         g_param_spec_object ("input-stream",
-                                                             "Input stream",
-                                                             "The input stream to load",
+                                                             "GInputStream",
+                                                             "The GInputStream to load",
                                                              G_TYPE_INPUT_STREAM,
                                                              G_PARAM_READWRITE |
                                                              G_PARAM_CONSTRUCT_ONLY |
@@ -231,7 +294,7 @@ gtk_source_file_loader_class_init (GtkSourceFileLoaderClass *klass)
 
        g_object_class_install_property (object_class, PROP_NEWLINE_TYPE,
                                         g_param_spec_enum ("newline-type",
-                                                           "Newline type",
+                                                           _("Newline type"),
                                                            "The type of line ending",
                                                            GTK_SOURCE_TYPE_NEWLINE_TYPE,
                                                            GTK_SOURCE_NEWLINE_TYPE_LF,
@@ -241,7 +304,7 @@ gtk_source_file_loader_class_init (GtkSourceFileLoaderClass *klass)
 
        g_object_class_install_property (object_class, PROP_COMPRESSION_TYPE,
                                         g_param_spec_enum ("compression-type",
-                                                           "Compression type",
+                                                           _("Compression type"),
                                                            "The compression type",
                                                            GTK_SOURCE_TYPE_COMPRESSION_TYPE,
                                                            GTK_SOURCE_COMPRESSION_TYPE_NONE,
@@ -249,13 +312,20 @@ gtk_source_file_loader_class_init (GtkSourceFileLoaderClass *klass)
                                                            G_PARAM_CONSTRUCT |
                                                            G_PARAM_STATIC_STRINGS));
 
+       /**
+        * GtkSourceFileLoader:remove-trailing-newline:
+        *
+        * When loading the content, remove the trailing newline if present.
+        *
+        * Since: 3.14
+        */
        g_object_class_install_property (object_class, PROP_REMOVE_TRAILING_NEWLINE,
                                         g_param_spec_boolean ("remove-trailing-newline",
-                                                              "Remove Trailing Newline",
+                                                              _("Remove Trailing Newline"),
                                                               "Remove the trailing newline if present",
                                                               TRUE,
                                                               G_PARAM_READWRITE |
-                                                              G_PARAM_CONSTRUCT_ONLY |
+                                                              G_PARAM_CONSTRUCT |
                                                               G_PARAM_STATIC_STRINGS));
 }
 
@@ -478,8 +548,6 @@ add_gzip_decompressor_stream (GtkSourceFileLoader *loader)
 static void
 create_streams (GtkSourceFileLoader *loader)
 {
-       GtkSourceBuffer *source_buffer;
-
        loader->priv->auto_detected_compression_type = GTK_SOURCE_COMPRESSION_TYPE_NONE;
 
        if (loader->priv->input_stream_property != NULL)
@@ -505,9 +573,7 @@ create_streams (GtkSourceFileLoader *loader)
 
        g_return_if_fail (loader->priv->input_stream != NULL);
 
-       source_buffer = gtk_source_file_get_buffer (loader->priv->file);
-
-       loader->priv->output_stream = gtk_source_buffer_output_stream_new (source_buffer,
+       loader->priv->output_stream = gtk_source_buffer_output_stream_new (loader->priv->source_buffer,
                                                                           loader->priv->candidate_encodings,
                                                                           
loader->priv->remove_trailing_newline);
 
@@ -516,7 +582,7 @@ create_streams (GtkSourceFileLoader *loader)
 }
 
 static void
-query_info_cb (GFile               *location,
+query_info_cb (GFile               *file,
               GAsyncResult        *result,
               GtkSourceFileLoader *loader)
 {
@@ -526,7 +592,7 @@ query_info_cb (GFile               *location,
               g_print ("%s\n", G_STRFUNC);
        });
 
-       loader->priv->info = g_file_query_info_finish (location, result, &error);
+       loader->priv->info = g_file_query_info_finish (file, result, &error);
 
        if (error != NULL)
        {
@@ -567,27 +633,30 @@ mount_cb (GFile               *file,
        else
        {
                /* Try again to open the file for reading. */
-               open_location (loader);
+               open_file (loader);
        }
 }
 
+static GMountOperation *
+create_mount_operation (GtkSourceFileLoader *loader)
+{
+       return loader->priv->mount_operation_factory != NULL ?
+               loader->priv->mount_operation_factory (loader->priv->mount_operation_userdata) :
+               g_mount_operation_new ();
+}
+
 static void
 recover_not_mounted (GtkSourceFileLoader *loader)
 {
-       GMountOperation *mount_operation;
-       GFile *location;
+       GMountOperation *mount_operation = create_mount_operation (loader);
 
        DEBUG ({
               g_print ("%s\n", G_STRFUNC);
        });
 
-       mount_operation = _gtk_source_file_create_mount_operation (loader->priv->file);
-
        loader->priv->tried_mount = TRUE;
 
-       location = gtk_source_file_get_location (loader->priv->file);
-
-       g_file_mount_enclosing_volume (location,
+       g_file_mount_enclosing_volume (loader->priv->file,
                                       G_MOUNT_MOUNT_NONE,
                                       mount_operation,
                                       g_task_get_cancellable (loader->priv->task),
@@ -595,13 +664,12 @@ recover_not_mounted (GtkSourceFileLoader *loader)
                                       loader);
 
        g_object_unref (mount_operation);
-       g_object_unref (location);
 }
 
 static void
-open_location_cb (GFile               *location,
-                 GAsyncResult        *result,
-                 GtkSourceFileLoader *loader)
+open_file_cb (GFile               *file,
+             GAsyncResult        *result,
+             GtkSourceFileLoader *loader)
 {
        GError *error = NULL;
 
@@ -609,7 +677,7 @@ open_location_cb (GFile               *location,
               g_print ("%s\n", G_STRFUNC);
        });
 
-       loader->priv->input_stream = G_INPUT_STREAM (g_file_read_finish (location, result, &error));
+       loader->priv->input_stream = G_INPUT_STREAM (g_file_read_finish (file, result, &error));
 
        if (error != NULL)
        {
@@ -630,7 +698,7 @@ open_location_cb (GFile               *location,
         * Using the file instead of the stream is slightly racy, but for
         * loading this is not too bad...
         */
-       g_file_query_info_async (location,
+       g_file_query_info_async (file,
                                 LOADER_QUERY_ATTRIBUTES,
                                  G_FILE_QUERY_INFO_NONE,
                                 g_task_get_priority (loader->priv->task),
@@ -640,43 +708,38 @@ open_location_cb (GFile               *location,
 }
 
 static void
-open_location (GtkSourceFileLoader *loader)
+open_file (GtkSourceFileLoader *loader)
 {
-       GFile *location = gtk_source_file_get_location (loader->priv->file);
-
-       g_file_read_async (location,
+       g_file_read_async (loader->priv->file,
                           g_task_get_priority (loader->priv->task),
                           g_task_get_cancellable (loader->priv->task),
-                          (GAsyncReadyCallback) open_location_cb,
+                          (GAsyncReadyCallback) open_file_cb,
                           loader);
-
-       g_object_unref (location);
 }
 
 GtkSourceFileLoader *
-gtk_source_file_loader_new (GtkSourceFile *file,
-                           gboolean       remove_trailing_newline)
+gtk_source_file_loader_new (GtkSourceBuffer *buffer,
+                           GFile           *file)
 {
-       g_return_val_if_fail (GTK_SOURCE_IS_FILE (file), NULL);
+       g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), NULL);
+       g_return_val_if_fail (G_IS_FILE (file), NULL);
 
        return g_object_new (GTK_SOURCE_TYPE_FILE_LOADER,
+                            "buffer", buffer,
                             "file", file,
-                            "remove-trailing-newline", remove_trailing_newline,
                             NULL);
 }
 
 GtkSourceFileLoader *
-gtk_source_file_loader_new_from_stream (GtkSourceFile *file,
-                                       GInputStream  *stream,
-                                       gboolean       remove_trailing_newline)
+gtk_source_file_loader_new_from_stream (GtkSourceBuffer *buffer,
+                                       GInputStream    *stream)
 {
-       g_return_val_if_fail (GTK_SOURCE_IS_FILE (file), NULL);
+       g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), NULL);
        g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL);
 
        return g_object_new (GTK_SOURCE_TYPE_FILE_LOADER,
-                            "file", file,
-                            "stream-to-read", stream,
-                            "remove-trailing-newline", remove_trailing_newline,
+                            "buffer", buffer,
+                            "input-stream", stream,
                             NULL);
 }
 
@@ -701,7 +764,7 @@ gtk_source_file_loader_load_async (GtkSourceFileLoader   *loader,
        g_return_if_fail (GTK_SOURCE_IS_FILE_LOADER (loader));
        g_return_if_fail (loader->priv->task == NULL);
 
-       loader->priv->task = g_task_new (loader->priv->file, cancellable, callback, user_data);
+       loader->priv->task = g_task_new (loader, cancellable, callback, user_data);
        g_task_set_priority (loader->priv->task, io_priority);
 
        loader->priv->progress_cb = progress_callback;
@@ -720,7 +783,7 @@ gtk_source_file_loader_load_async (GtkSourceFileLoader   *loader,
        }
        else
        {
-               open_location (loader);
+               open_file (loader);
        }
 }
 
@@ -729,11 +792,17 @@ gtk_source_file_loader_load_finish (GtkSourceFileLoader  *loader,
                                    GAsyncResult         *result,
                                    GError              **error)
 {
+       gboolean ok;
+
        g_return_val_if_fail (GTK_SOURCE_IS_FILE_LOADER (loader), FALSE);
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
-       g_return_val_if_fail (g_task_is_valid (result, loader->priv->file), FALSE);
+       g_return_val_if_fail (g_task_is_valid (result, loader), FALSE);
 
-       return g_task_propagate_boolean (G_TASK (result), error);
+       ok = g_task_propagate_boolean (G_TASK (result), error);
+
+       reset (loader);
+
+       return ok;
 }
 
 const GtkSourceEncoding *
@@ -777,3 +846,27 @@ gtk_source_file_loader_get_info (GtkSourceFileLoader *loader)
 
        return loader->priv->info;
 }
+
+/**
+ * gtk_source_file_loader_set_mount_operation_factory:
+ * @loader: a #GtkSourceFileLoader.
+ * @callback: a #GtkSourceMountOperationFactory to call when a #GMountOperation
+ * is needed.
+ * @user_data: the data to pass to the @callback function.
+ *
+ * Sets a #GtkSourceMountOperationFactory function that will be called when a
+ * #GMountOperation must be created. This is useful for creating a
+ * #GtkMountOperation with the parent #GtkWindow.
+ *
+ * Since: 3.14
+ */
+void
+gtk_source_file_loader_set_mount_operation_factory (GtkSourceFileLoader            *loader,
+                                                   GtkSourceMountOperationFactory  callback,
+                                                   gpointer                        user_data)
+{
+       g_return_if_fail (GTK_SOURCE_IS_FILE_LOADER (loader));
+
+       loader->priv->mount_operation_factory = callback;
+       loader->priv->mount_operation_userdata = user_data;
+}
diff --git a/gtksourceview/gtksourcefileloader.h b/gtksourceview/gtksourcefileloader.h
index edd9b52..6a5efd5 100644
--- a/gtksourceview/gtksourcefileloader.h
+++ b/gtksourceview/gtksourcefileloader.h
@@ -27,7 +27,7 @@
 
 #include <gtk/gtk.h>
 #include <gtksourceview/gtksourcetypes.h>
-#include <gtksourceview/gtksourcefile.h>
+#include <gtksourceview/gtksourcebuffer.h>
 
 G_BEGIN_DECLS
 
@@ -55,12 +55,11 @@ struct _GtkSourceFileLoaderClass
 
 GType                   gtk_source_file_loader_get_type        (void) G_GNUC_CONST;
 
-GtkSourceFileLoader    *gtk_source_file_loader_new             (GtkSourceFile           *file,
-                                                                gboolean                 
remove_trailing_newline);
+GtkSourceFileLoader    *gtk_source_file_loader_new             (GtkSourceBuffer         *buffer,
+                                                                GFile                   *file);
 
-GtkSourceFileLoader    *gtk_source_file_loader_new_from_stream (GtkSourceFile           *file,
-                                                                GInputStream            *stream,
-                                                                gboolean                 
remove_trailing_newline);
+GtkSourceFileLoader    *gtk_source_file_loader_new_from_stream (GtkSourceBuffer         *buffer,
+                                                                GInputStream            *stream);
 
 void                    gtk_source_file_loader_set_candidate_encodings
                                                                (GtkSourceFileLoader     *loader,
@@ -90,6 +89,11 @@ GtkSourceCompressionType gtk_source_file_loader_get_compression_type
  */
 GFileInfo              *gtk_source_file_loader_get_info        (GtkSourceFileLoader     *loader);
 
+void                    gtk_source_file_loader_set_mount_operation_factory
+                                                               (GtkSourceFileLoader            *loader,
+                                                                GtkSourceMountOperationFactory  callback,
+                                                                gpointer                        user_data);
+
 G_END_DECLS
 
 #endif  /* __GTK_SOURCE_FILE_LOADER_H__  */


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