[gtksourceview/wip/loader-saver] Move MountOperationFactory in GtkSourceFile



commit 62c0289e5b1f478e50eccc9afbb5dca6063d5fe3
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Dec 28 19:00:33 2013 +0100

    Move MountOperationFactory in GtkSourceFile

 gtksourceview/gtksourcefile.c      |   32 ++++++++--
 gtksourceview/gtksourcefile.h      |   14 ++++
 gtksourceview/gtksourcefilesaver.c |  119 +++++++++++++-----------------------
 gtksourceview/gtksourcefilesaver.h |   52 ++++++----------
 4 files changed, 103 insertions(+), 114 deletions(-)
---
diff --git a/gtksourceview/gtksourcefile.c b/gtksourceview/gtksourcefile.c
index bcc83a1..0e01bd2 100644
--- a/gtksourceview/gtksourcefile.c
+++ b/gtksourceview/gtksourcefile.c
@@ -44,6 +44,9 @@ struct _GtkSourceFilePrivate
        GtkSourceNewlineType newline_type;
        GtkSourceCompressionType compression_type;
 
+       GtkSourceMountOperationFactory mount_operation_factory;
+       gpointer mount_operation_userdata;
+
        GtkSourceFileSaver *saver;
 
        /* The time when the file was last modified, from our point of view. The
@@ -365,6 +368,27 @@ gtk_source_file_set_ensure_trailing_newline (GtkSourceFile *file,
        }
 }
 
+void
+gtk_source_file_set_mount_operation_factory (GtkSourceFile                  *file,
+                                            GtkSourceMountOperationFactory  callback,
+                                            gpointer                        user_data)
+{
+       g_return_if_fail (GTK_SOURCE_IS_FILE (file));
+
+       file->priv->mount_operation_factory = callback;
+       file->priv->mount_operation_userdata = user_data;
+}
+
+GMountOperation *
+_gtk_source_file_create_mount_operation (GtkSourceFile *file)
+{
+       g_return_val_if_fail (GTK_SOURCE_IS_FILE (file), NULL);
+
+       return file->priv->mount_operation_factory != NULL ?
+               file->priv->mount_operation_factory (file->priv->mount_operation_userdata) :
+               g_mount_operation_new ();
+}
+
 /* FIXME add load flags parameter for future expension? */
 gboolean
 gtk_source_file_load (GtkSourceFile          *file,
@@ -454,8 +478,7 @@ gtk_source_file_save_async (GtkSourceFile          *file,
                flags |= GTK_SOURCE_FILE_SAVE_IGNORE_MTIME;
        }
 
-       file->priv->saver = gtk_source_file_saver_new (GTK_TEXT_BUFFER (file->priv->buffer),
-                                                      file->priv->location,
+       file->priv->saver = gtk_source_file_saver_new (file,
                                                       file->priv->encoding,
                                                       file->priv->newline_type,
                                                       file->priv->compression_type,
@@ -463,9 +486,8 @@ gtk_source_file_save_async (GtkSourceFile          *file,
                                                       flags);
 
        gtk_source_file_saver_save_async (file->priv->saver,
-                                         file,
-                                         io_priority,
                                          mtime,
+                                         io_priority,
                                          cancellable,
                                          progress_callback,
                                          progress_callback_data,
@@ -490,7 +512,7 @@ gtk_source_file_save_finish (GtkSourceFile  *file,
 
        g_return_val_if_fail (file->priv->saver != NULL, FALSE);
 
-       ok = gtk_source_file_saver_save_finish (file->priv->saver, file, result, error);
+       ok = gtk_source_file_saver_save_finish (file->priv->saver, result, error);
 
        if (ok)
        {
diff --git a/gtksourceview/gtksourcefile.h b/gtksourceview/gtksourcefile.h
index 352a139..2545bcb 100644
--- a/gtksourceview/gtksourcefile.h
+++ b/gtksourceview/gtksourcefile.h
@@ -92,6 +92,12 @@ enum
        GTK_SOURCE_FILE_ERROR_CONVERSION_FALLBACK
 };
 
+/**
+ * GtkSourceMountOperationFactory: (skip)
+ * @userdata:
+ */
+typedef GMountOperation *(*GtkSourceMountOperationFactory)(gpointer userdata);
+
 struct _GtkSourceFile
 {
        GObject parent;
@@ -137,6 +143,11 @@ void                        gtk_source_file_set_ensure_trailing_newline
                                                                (GtkSourceFile            *file,
                                                                 gboolean                  
ensure_trailing_newline);
 
+void                    gtk_source_file_set_mount_operation_factory
+                                                               (GtkSourceFile                  *file,
+                                                                GtkSourceMountOperationFactory  callback,
+                                                                gpointer                        user_data);
+
 gboolean                gtk_source_file_load                   (GtkSourceFile            *file,
                                                                 GCancellable             *cancellable,
                                                                 GFileProgressCallback     progress_callback,
@@ -175,6 +186,9 @@ gboolean             gtk_source_file_save_finish            (GtkSourceFile            
*file,
                                                                 GAsyncResult             *result,
                                                                 GError                  **error);
 
+G_GNUC_INTERNAL
+GMountOperation                *_gtk_source_file_create_mount_operation (GtkSourceFile           *file);
+
 G_END_DECLS
 
 #endif /* __GTK_SOURCE_FILE_H__ */
diff --git a/gtksourceview/gtksourcefilesaver.c b/gtksourceview/gtksourcefilesaver.c
index a3d8362..4d72260 100644
--- a/gtksourceview/gtksourcefilesaver.c
+++ b/gtksourceview/gtksourcefilesaver.c
@@ -49,8 +49,7 @@
 enum
 {
        PROP_0,
-       PROP_BUFFER,
-       PROP_LOCATION,
+       PROP_FILE,
        PROP_ENCODING,
        PROP_NEWLINE_TYPE,
        PROP_COMPRESSION_TYPE,
@@ -60,8 +59,7 @@ enum
 
 struct _GtkSourceFileSaverPrivate
 {
-       GtkTextBuffer *buffer;
-       GFile *location;
+       GtkSourceFile *file;
        const GtkSourceEncoding *encoding;
        GtkSourceNewlineType newline_type;
        GtkSourceCompressionType compression_type;
@@ -92,10 +90,6 @@ struct _GtkSourceFileSaverPrivate
        GOutputStream *output_stream;
        GInputStream *input_stream;
 
-       /* TODO use this in GtkSourceFile */
-       GtkSourceMountOperationFactory mount_operation_factory;
-       gpointer mount_operation_userdata;
-
        GFileInfo *info;
 
        guint ensure_trailing_newline : 1;
@@ -119,14 +113,9 @@ gtk_source_file_saver_set_property (GObject      *object,
 
        switch (prop_id)
        {
-               case PROP_BUFFER:
-                       g_assert (saver->priv->buffer == NULL);
-                       saver->priv->buffer = g_value_get_object (value);
-                       break;
-
-               case PROP_LOCATION:
-                       g_assert (saver->priv->location == NULL);
-                       saver->priv->location = g_value_dup_object (value);
+               case PROP_FILE:
+                       g_assert (saver->priv->file == NULL);
+                       saver->priv->file = g_value_get_object (value);
                        break;
 
                case PROP_ENCODING:
@@ -170,12 +159,8 @@ gtk_source_file_saver_get_property (GObject    *object,
 
        switch (prop_id)
        {
-               case PROP_BUFFER:
-                       g_value_set_object (value, saver->priv->buffer);
-                       break;
-
-               case PROP_LOCATION:
-                       g_value_set_object (value, saver->priv->location);
+               case PROP_FILE:
+                       g_value_set_object (value, saver->priv->file);
                        break;
 
                case PROP_ENCODING:
@@ -211,11 +196,12 @@ gtk_source_file_saver_dispose (GObject *object)
 
        g_clear_object (&priv->output_stream);
        g_clear_object (&priv->input_stream);
-       g_clear_object (&priv->location);
        g_clear_object (&priv->task);
        g_clear_object (&priv->info);
        g_clear_error (&priv->error);
 
+       priv->file = NULL;
+
        G_OBJECT_CLASS (gtk_source_file_saver_parent_class)->dispose (object);
 }
 
@@ -229,21 +215,11 @@ gtk_source_file_saver_class_init (GtkSourceFileSaverClass *klass)
        object_class->get_property = gtk_source_file_saver_get_property;
 
        g_object_class_install_property (object_class,
-                                        PROP_BUFFER,
-                                        g_param_spec_object ("buffer",
-                                                             "Buffer",
-                                                             "The associated GtkTextBuffer",
-                                                             GTK_TYPE_TEXT_BUFFER,
-                                                             G_PARAM_READWRITE |
-                                                             G_PARAM_CONSTRUCT_ONLY |
-                                                             G_PARAM_STATIC_STRINGS));
-
-       g_object_class_install_property (object_class,
-                                        PROP_LOCATION,
-                                        g_param_spec_object ("location",
-                                                             "Location",
-                                                             "The output location",
-                                                             G_TYPE_FILE,
+                                        PROP_FILE,
+                                        g_param_spec_object ("file",
+                                                             "File",
+                                                             "The associated GtkSourceFile",
+                                                             GTK_SOURCE_TYPE_FILE,
                                                              G_PARAM_READWRITE |
                                                              G_PARAM_CONSTRUCT_ONLY |
                                                              G_PARAM_STATIC_STRINGS));
@@ -406,6 +382,7 @@ close_output_stream_cb (GOutputStream      *output_stream,
                        GtkSourceFileSaver *saver)
 {
        GError *error = NULL;
+       GFile *location;
 
        DEBUG ({
               g_print ("%s\n", G_STRFUNC);
@@ -431,7 +408,9 @@ close_output_stream_cb (GOutputStream      *output_stream,
               g_print ("Query info on file\n");
        });
 
-       g_file_query_info_async (saver->priv->location,
+       location = gtk_source_file_get_location (saver->priv->file);
+
+       g_file_query_info_async (location,
                                 QUERY_ATTRIBUTES,
                                 G_FILE_QUERY_INFO_NONE,
                                 g_task_get_priority (saver->priv->task),
@@ -593,6 +572,7 @@ replace_file_cb (GFile              *location,
        GFileOutputStream *file_output_stream;
        GOutputStream *output_stream;
        GtkSourceBufferInputStream *buffer_stream;
+       GtkTextBuffer *buffer;
        GError *error = NULL;
 
        DEBUG ({
@@ -667,7 +647,9 @@ replace_file_cb (GFile              *location,
                saver->priv->output_stream = G_OUTPUT_STREAM (output_stream);
        }
 
-       saver->priv->input_stream = _gtk_source_buffer_input_stream_new (saver->priv->buffer,
+       buffer = GTK_TEXT_BUFFER (gtk_source_file_get_buffer (saver->priv->file));
+
+       saver->priv->input_stream = _gtk_source_buffer_input_stream_new (buffer,
                                                                         saver->priv->newline_type,
                                                                         
saver->priv->ensure_trailing_newline);
 
@@ -684,6 +666,7 @@ replace_file_cb (GFile              *location,
 static void
 begin_write (GtkSourceFileSaver *saver)
 {
+       GFile *location;
        gboolean create_backup = (saver->priv->flags & GTK_SOURCE_FILE_SAVE_CREATE_BACKUP) != 0;
 
        DEBUG ({
@@ -691,7 +674,9 @@ begin_write (GtkSourceFileSaver *saver)
               g_print ("Make backup: %s\n", make_backup ? "yes" : "no");
        });
 
-       g_file_replace_async (saver->priv->location,
+       location = gtk_source_file_get_location (saver->priv->file);
+
+       g_file_replace_async (location,
                              NULL,
                              create_backup,
                              G_FILE_CREATE_NONE,
@@ -728,25 +713,21 @@ mount_cb (GFile              *location,
        }
 }
 
-static GMountOperation *
-create_mount_operation (GtkSourceFileSaver *saver)
-{
-       return saver->priv->mount_operation_factory != NULL ?
-               saver->priv->mount_operation_factory (saver->priv->mount_operation_userdata) :
-               g_mount_operation_new ();
-}
-
 static void
 recover_not_mounted (GtkSourceFileSaver *saver)
 {
-       GMountOperation *mount_operation = create_mount_operation (saver);
+       GFile *location;
+       GMountOperation *mount_operation = _gtk_source_file_create_mount_operation (saver->priv->file);
 
        DEBUG ({
               g_print ("%s\n", G_STRFUNC);
        });
 
        saver->priv->tried_mount = TRUE;
-       g_file_mount_enclosing_volume (saver->priv->location,
+
+       location = gtk_source_file_get_location (saver->priv->file);
+
+       g_file_mount_enclosing_volume (location,
                                       G_MOUNT_MOUNT_NONE,
                                       mount_operation,
                                       g_task_get_cancellable (saver->priv->task),
@@ -837,11 +818,15 @@ check_externally_modified_cb (GFile              *location,
 static void
 check_externally_modified (GtkSourceFileSaver *saver)
 {
+       GFile *location;
+
        DEBUG ({
               g_print ("Check externally modified\n");
        });
 
-       g_file_query_info_async (saver->priv->location,
+       location = gtk_source_file_get_location (saver->priv->file);
+
+       g_file_query_info_async (location,
                                 G_FILE_ATTRIBUTE_TIME_MODIFIED,
                                 G_FILE_QUERY_INFO_NONE,
                                 g_task_get_priority (saver->priv->task),
@@ -851,20 +836,17 @@ check_externally_modified (GtkSourceFileSaver *saver)
 }
 
 GtkSourceFileSaver *
-gtk_source_file_saver_new (GtkTextBuffer            *buffer,
-                          GFile                    *location,
+gtk_source_file_saver_new (GtkSourceFile            *file,
                           const GtkSourceEncoding  *encoding,
                           GtkSourceNewlineType      newline_type,
                           GtkSourceCompressionType  compression_type,
                           gboolean                  ensure_trailing_newline,
                           GtkSourceFileSaveFlags    flags)
 {
-       g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
-       g_return_val_if_fail (G_IS_FILE (location), NULL);
+       g_return_val_if_fail (GTK_SOURCE_IS_FILE (file), NULL);
 
        return g_object_new (GTK_SOURCE_TYPE_FILE_SAVER,
-                            "buffer", buffer,
-                            "location", location,
+                            "file", file,
                             "encoding", encoding,
                             "newline_type", newline_type,
                             "compression_type", compression_type,
@@ -874,21 +856,9 @@ gtk_source_file_saver_new (GtkTextBuffer            *buffer,
 }
 
 void
-gtk_source_file_saver_set_mount_operation_factory (GtkSourceFileSaver             *saver,
-                                                  GtkSourceMountOperationFactory  callback,
-                                                  gpointer                        user_data)
-{
-       g_return_if_fail (GTK_SOURCE_IS_FILE_SAVER (saver));
-
-       saver->priv->mount_operation_factory = callback;
-       saver->priv->mount_operation_userdata = user_data;
-}
-
-void
 gtk_source_file_saver_save_async (GtkSourceFileSaver     *saver,
-                                 GtkSourceFile          *file,
-                                 gint                    io_priority,
                                  GTimeVal               *old_mtime,
+                                 gint                    io_priority,
                                  GCancellable           *cancellable,
                                  GFileProgressCallback   progress_callback,
                                  gpointer                progress_callback_data,
@@ -896,10 +866,9 @@ gtk_source_file_saver_save_async (GtkSourceFileSaver     *saver,
                                  gpointer                user_data)
 {
        g_return_if_fail (GTK_SOURCE_IS_FILE_SAVER (saver));
-       g_return_if_fail (GTK_SOURCE_IS_FILE (file));
        g_return_if_fail (saver->priv->task == NULL);
 
-       saver->priv->task = g_task_new (file, cancellable, callback, user_data);
+       saver->priv->task = g_task_new (saver->priv->file, cancellable, callback, user_data);
        g_task_set_priority (saver->priv->task, io_priority);
 
        saver->priv->progress_cb = progress_callback;
@@ -923,14 +892,12 @@ gtk_source_file_saver_save_async (GtkSourceFileSaver     *saver,
 
 gboolean
 gtk_source_file_saver_save_finish (GtkSourceFileSaver  *saver,
-                                  GtkSourceFile       *file,
                                   GAsyncResult        *result,
                                   GError             **error)
 {
        g_return_val_if_fail (GTK_SOURCE_IS_FILE_SAVER (saver), FALSE);
-       g_return_val_if_fail (GTK_SOURCE_IS_FILE (file), FALSE);
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
-       g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
+       g_return_val_if_fail (g_task_is_valid (result, saver->priv->file), FALSE);
 
        return g_task_propagate_boolean (G_TASK (result), error);
 }
diff --git a/gtksourceview/gtksourcefilesaver.h b/gtksourceview/gtksourcefilesaver.h
index 7978832..c50ee73 100644
--- a/gtksourceview/gtksourcefilesaver.h
+++ b/gtksourceview/gtksourcefilesaver.h
@@ -42,12 +42,6 @@ G_BEGIN_DECLS
 typedef struct _GtkSourceFileSaverClass   GtkSourceFileSaverClass;
 typedef struct _GtkSourceFileSaverPrivate GtkSourceFileSaverPrivate;
 
-/**
- * GtkSourceMountOperationFactory: (skip)
- * @userdata:
- */
-typedef GMountOperation *(*GtkSourceMountOperationFactory)(gpointer userdata);
-
 struct _GtkSourceFileSaver
 {
        GObject object;
@@ -61,41 +55,33 @@ struct _GtkSourceFileSaverClass
 };
 
 G_GNUC_INTERNAL
-GType                   gtk_source_file_saver_get_type                         (void) G_GNUC_CONST;
-
-G_GNUC_INTERNAL
-GtkSourceFileSaver     *gtk_source_file_saver_new                              (GtkTextBuffer                
  *buffer,
-                                                                                GFile                        
  *location,
-                                                                                const GtkSourceEncoding      
  *encoding,
-                                                                                GtkSourceNewlineType         
   newline_type,
-                                                                                GtkSourceCompressionType     
   compression_type,
-                                                                                gboolean                     
   ensure_trailing_newline,
-                                                                                GtkSourceFileSaveFlags       
   flags);
+GType                   gtk_source_file_saver_get_type         (void) G_GNUC_CONST;
 
 G_GNUC_INTERNAL
-void                    gtk_source_file_saver_set_mount_operation_factory      (GtkSourceFileSaver           
  *saver,
-                                                                                
GtkSourceMountOperationFactory  callback,
-                                                                                gpointer                     
   user_data);
+GtkSourceFileSaver     *gtk_source_file_saver_new              (GtkSourceFile            *file,
+                                                                const GtkSourceEncoding  *encoding,
+                                                                GtkSourceNewlineType      newline_type,
+                                                                GtkSourceCompressionType  compression_type,
+                                                                gboolean                  
ensure_trailing_newline,
+                                                                GtkSourceFileSaveFlags    flags);
 
 G_GNUC_INTERNAL
-void                    gtk_source_file_saver_save_async                       (GtkSourceFileSaver           
  *saver,
-                                                                                GtkSourceFile                
  *file,
-                                                                                gint                         
   io_priority,
-                                                                                GTimeVal                     
  *old_mtime,
-                                                                                GCancellable                 
  *cancellable,
-                                                                                GFileProgressCallback        
   progress_callback,
-                                                                                gpointer                     
   progress_callback_data,
-                                                                                GAsyncReadyCallback          
   callback,
-                                                                                gpointer                     
   user_data);
+void                    gtk_source_file_saver_save_async       (GtkSourceFileSaver       *saver,
+                                                                GTimeVal                 *old_mtime,
+                                                                gint                      io_priority,
+                                                                GCancellable             *cancellable,
+                                                                GFileProgressCallback     progress_callback,
+                                                                gpointer                  
progress_callback_data,
+                                                                GAsyncReadyCallback       callback,
+                                                                gpointer                  user_data);
 
 G_GNUC_INTERNAL
-gboolean                gtk_source_file_saver_save_finish                      (GtkSourceFileSaver           
  *saver,
-                                                                                GtkSourceFile                
  *file,
-                                                                                GAsyncResult                 
  *result,
-                                                                                GError                       
 **error);
+gboolean                gtk_source_file_saver_save_finish      (GtkSourceFileSaver       *saver,
+                                                                GAsyncResult             *result,
+                                                                GError                  **error);
 
 G_GNUC_INTERNAL
-GFileInfo              *gtk_source_file_saver_get_info                         (GtkSourceFileSaver           
  *saver);
+GFileInfo              *gtk_source_file_saver_get_info         (GtkSourceFileSaver       *saver);
 
 G_END_DECLS
 


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