[gtksourceview/wip/loader-saver] File and FileSaver: update mtime



commit 2f12fdd96e3db248bfa2e9d6a0b02c27d949a2d5
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Dec 28 17:23:35 2013 +0100

    File and FileSaver: update mtime

 gtksourceview/gtksourcefile.c      |    8 +++
 gtksourceview/gtksourcefilesaver.c |  103 +++++++++++++++++++++++++++--------
 gtksourceview/gtksourcefilesaver.h |   52 +++++++++---------
 3 files changed, 114 insertions(+), 49 deletions(-)
---
diff --git a/gtksourceview/gtksourcefile.c b/gtksourceview/gtksourcefile.c
index 0e3268d..bcc83a1 100644
--- a/gtksourceview/gtksourcefile.c
+++ b/gtksourceview/gtksourcefile.c
@@ -492,6 +492,14 @@ gtk_source_file_save_finish (GtkSourceFile  *file,
 
        ok = gtk_source_file_saver_save_finish (file->priv->saver, file, result, error);
 
+       if (ok)
+       {
+               GFileInfo *info = gtk_source_file_saver_get_info (file->priv->saver);
+
+               g_file_info_get_modification_time (info, &file->priv->mtime);
+               file->priv->mtime_set = TRUE;
+       }
+
        g_clear_object (&file->priv->saver);
 
        return ok;
diff --git a/gtksourceview/gtksourcefilesaver.c b/gtksourceview/gtksourcefilesaver.c
index 2896f5c..5f616cd 100644
--- a/gtksourceview/gtksourcefilesaver.c
+++ b/gtksourceview/gtksourcefilesaver.c
@@ -44,6 +44,8 @@
 
 #define WRITE_CHUNK_SIZE 8192
 
+#define QUERY_ATTRIBUTES G_FILE_ATTRIBUTE_TIME_MODIFIED
+
 enum
 {
        PROP_0,
@@ -94,6 +96,8 @@ struct _GtkSourceFileSaverPrivate
        GtkSourceMountOperationFactory mount_operation_factory;
        gpointer mount_operation_userdata;
 
+       GFileInfo *info;
+
        guint ensure_trailing_newline : 1;
        guint tried_mount : 1;
 };
@@ -209,6 +213,8 @@ gtk_source_file_saver_dispose (GObject *object)
        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);
 
        G_OBJECT_CLASS (gtk_source_file_saver_parent_class)->dispose (object);
 }
@@ -302,29 +308,6 @@ gtk_source_file_saver_init (GtkSourceFileSaver *saver)
        saver->priv = gtk_source_file_saver_get_instance_private (saver);
 }
 
-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)
-{
-       g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
-       g_return_val_if_fail (G_IS_FILE (location), NULL);
-
-       return g_object_new (GTK_SOURCE_TYPE_FILE_SAVER,
-                            "buffer", buffer,
-                            "location", location,
-                            "encoding", encoding,
-                            "newline_type", newline_type,
-                            "compression_type", compression_type,
-                            "ensure-trailing-newline", ensure_trailing_newline,
-                            "flags", flags,
-                            NULL);
-}
-
 /* BEGIN NOTE:
  *
  * This fixes an issue in GOutputStream that applies the atomic replace save
@@ -391,6 +374,33 @@ cancel_output_stream (GtkSourceFileSaver *saver)
  */
 
 static void
+query_info_cb (GFile              *location,
+              GAsyncResult       *result,
+              GtkSourceFileSaver *saver)
+{
+       GError *error = NULL;
+
+       DEBUG ({
+              g_print ("Finished query info on file\n");
+       });
+
+       g_clear_object (&saver->priv->info);
+       saver->priv->info = g_file_query_info_finish (location, result, &error);
+
+       if (error != NULL)
+       {
+               DEBUG ({
+                      g_print ("Query info failed: %s\n", error->message);
+               });
+
+               g_task_return_error (saver->priv->task, error);
+               return;
+       }
+
+       g_task_return_boolean (saver->priv->task, TRUE);
+}
+
+static void
 close_output_stream_cb (GOutputStream      *output_stream,
                        GAsyncResult       *result,
                        GtkSourceFileSaver *saver)
@@ -413,7 +423,21 @@ close_output_stream_cb (GOutputStream      *output_stream,
                return;
        }
 
-       g_task_return_boolean (saver->priv->task, TRUE);
+       /* Get the file info: note we cannot use
+        * g_file_output_stream_query_info_async() since it is not able to get
+        * the modification time.
+        */
+       DEBUG ({
+              g_print ("Query info on file\n");
+       });
+
+       g_file_query_info_async (saver->priv->location,
+                                QUERY_ATTRIBUTES,
+                                G_FILE_QUERY_INFO_NONE,
+                                g_task_get_priority (saver->priv->task),
+                                g_task_get_cancellable (saver->priv->task),
+                                (GAsyncReadyCallback) query_info_cb,
+                                saver);
 }
 
 static void
@@ -829,6 +853,29 @@ check_externally_modified (GtkSourceFileSaver *saver)
                                 saver);
 }
 
+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)
+{
+       g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
+       g_return_val_if_fail (G_IS_FILE (location), NULL);
+
+       return g_object_new (GTK_SOURCE_TYPE_FILE_SAVER,
+                            "buffer", buffer,
+                            "location", location,
+                            "encoding", encoding,
+                            "newline_type", newline_type,
+                            "compression_type", compression_type,
+                            "ensure-trailing-newline", ensure_trailing_newline,
+                            "flags", flags,
+                            NULL);
+}
+
 void
 gtk_source_file_saver_set_mount_operation_factory (GtkSourceFileSaver             *saver,
                                                   GtkSourceMountOperationFactory  callback,
@@ -890,3 +937,11 @@ gtk_source_file_saver_save_finish (GtkSourceFileSaver  *saver,
 
        return g_task_propagate_boolean (G_TASK (result), error);
 }
+
+GFileInfo *
+gtk_source_file_saver_get_info (GtkSourceFileSaver *saver)
+{
+       g_return_val_if_fail (GTK_SOURCE_IS_FILE_SAVER (saver), NULL);
+
+       return saver->priv->info;
+}
diff --git a/gtksourceview/gtksourcefilesaver.h b/gtksourceview/gtksourcefilesaver.h
index 0bead52..7978832 100644
--- a/gtksourceview/gtksourcefilesaver.h
+++ b/gtksourceview/gtksourcefilesaver.h
@@ -61,39 +61,41 @@ struct _GtkSourceFileSaverClass
 };
 
 G_GNUC_INTERNAL
-GType                   gtk_source_file_saver_get_type         (void) G_GNUC_CONST;
+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);
+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);
 
 G_GNUC_INTERNAL
-void                    gtk_source_file_saver_set_mount_operation_factory
-                                                               (GtkSourceFileSaver             *saver,
-                                                                GtkSourceMountOperationFactory  callback,
-                                                                gpointer                        user_data);
+void                    gtk_source_file_saver_set_mount_operation_factory      (GtkSourceFileSaver           
  *saver,
+                                                                                
GtkSourceMountOperationFactory  callback,
+                                                                                gpointer                     
   user_data);
 
 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,
+                                                                                GtkSourceFile                
  *file,
+                                                                                gint                         
   io_priority,
+                                                                                GTimeVal                     
  *old_mtime,
+                                                                                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,
+                                                                                GtkSourceFile                
  *file,
+                                                                                GAsyncResult                 
  *result,
+                                                                                GError                       
 **error);
+
+G_GNUC_INTERNAL
+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]