[gtksourceview/wip/metadata: 3/5] FileSaver: save metadata



commit fc833ddf5c1cbbd9e57373d8fc2a1b724c2a1faa
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Apr 29 14:41:59 2016 +0200

    FileSaver: save metadata

 gtksourceview/gtksourcefile.c      |    8 ++++
 gtksourceview/gtksourcefile.h      |    3 +
 gtksourceview/gtksourcefilesaver.c |   75 ++++++++++++++++++++++++++++--------
 3 files changed, 70 insertions(+), 16 deletions(-)
---
diff --git a/gtksourceview/gtksourcefile.c b/gtksourceview/gtksourcefile.c
index d43bbf4..aaec0e6 100644
--- a/gtksourceview/gtksourcefile.c
+++ b/gtksourceview/gtksourcefile.c
@@ -747,6 +747,14 @@ gtk_source_file_set_metadata (GtkSourceFile *file,
        g_free (attribute_key);
 }
 
+GFileInfo *
+_gtk_source_file_get_metadata_info (GtkSourceFile *file)
+{
+       g_return_val_if_fail (GTK_SOURCE_IS_FILE (file), NULL);
+
+       return file->priv->metadata;
+}
+
 void
 _gtk_source_file_set_metadata_info (GtkSourceFile *file,
                                    GFileInfo     *metadata)
diff --git a/gtksourceview/gtksourcefile.h b/gtksourceview/gtksourcefile.h
index ee814e9..c5066d2 100644
--- a/gtksourceview/gtksourcefile.h
+++ b/gtksourceview/gtksourcefile.h
@@ -155,6 +155,9 @@ void                 _gtk_source_file_set_readonly                  (GtkSourceFile *file,
                                                                 gboolean       readonly);
 
 G_GNUC_INTERNAL
+GFileInfo *     _gtk_source_file_get_metadata_info             (GtkSourceFile *file);
+
+G_GNUC_INTERNAL
 void            _gtk_source_file_set_metadata_info             (GtkSourceFile *file,
                                                                 GFileInfo     *metadata);
 
diff --git a/gtksourceview/gtksourcefilesaver.c b/gtksourceview/gtksourcefilesaver.c
index 84d24c1..52a02dd 100644
--- a/gtksourceview/gtksourcefilesaver.c
+++ b/gtksourceview/gtksourcefilesaver.c
@@ -5,7 +5,7 @@
  * Copyright (C) 2005-2007 - Paolo Borelli and Paolo Maggi
  * Copyright (C) 2007 - Steve Frécinaux
  * Copyright (C) 2008 - Jesse van den Kieboom
- * Copyright (C) 2014 - Sébastien Wilmet
+ * Copyright (C) 2014, 2016 - Sébastien Wilmet
  *
  * GtkSourceView is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -129,6 +129,7 @@ struct _GtkSourceFileSaverPrivate
        GtkSourceBufferInputStream *input_stream;
 
        GFileInfo *info;
+       GFileInfo *metadata;
 
        guint tried_mount : 1;
 };
@@ -241,6 +242,7 @@ reset (GtkSourceFileSaver *saver)
        g_clear_object (&saver->priv->output_stream);
        g_clear_object (&saver->priv->input_stream);
        g_clear_object (&saver->priv->info);
+       g_clear_object (&saver->priv->metadata);
        g_clear_error (&saver->priv->error);
 
        if (saver->priv->progress_cb_notify != NULL)
@@ -555,6 +557,52 @@ query_info_cb (GFile              *file,
 }
 
 static void
+query_info (GtkSourceFileSaver *saver)
+{
+       /* 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
+set_metadata_cb (GFile              *location,
+                GAsyncResult       *result,
+                GtkSourceFileSaver *saver)
+{
+       GError *error = NULL;
+
+       DEBUG ({
+              g_print ("%s\n", G_STRFUNC);
+       });
+
+       g_file_set_attributes_finish (location, result, NULL, &error);
+
+       if (error != NULL)
+       {
+               DEBUG ({
+                      g_print ("Set metadata error: %s\n", error->message);
+               });
+
+               g_task_return_error (saver->priv->task, error);
+               return;
+       }
+
+       query_info (saver);
+}
+
+static void
 close_output_stream_cb (GOutputStream      *output_stream,
                        GAsyncResult       *result,
                        GtkSourceFileSaver *saver)
@@ -577,21 +625,13 @@ close_output_stream_cb (GOutputStream      *output_stream,
                return;
        }
 
-       /* 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);
+       g_file_set_attributes_async (saver->priv->location,
+                                    saver->priv->metadata,
+                                    G_FILE_QUERY_INFO_NONE,
+                                    g_task_get_priority (saver->priv->task),
+                                    g_task_get_cancellable (saver->priv->task),
+                                    (GAsyncReadyCallback) set_metadata_cb,
+                                    saver);
 }
 
 static void
@@ -1364,6 +1404,9 @@ gtk_source_file_saver_save_async (GtkSourceFileSaver     *saver,
                                                                         saver->priv->newline_type,
                                                                         implicit_trailing_newline);
 
+       saver->priv->metadata = _gtk_source_file_get_metadata_info (saver->priv->file);
+       g_object_ref (saver->priv->metadata);
+
        check_externally_modified (saver);
 }
 


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