[gimp] app: port gimp_tags_user_install() to GIO



commit afc469446ffc6dc47deece35df4a5de6ca14e36c
Author: Michael Natterer <mitch gimp org>
Date:   Mon Jul 28 12:12:21 2014 +0200

    app: port gimp_tags_user_install() to GIO

 app/core/gimp-tags.c |   48 +++++++++++++++++++++++++++++-------------------
 1 files changed, 29 insertions(+), 19 deletions(-)
---
diff --git a/app/core/gimp-tags.c b/app/core/gimp-tags.c
index 7b24f37..40d0cd3 100644
--- a/app/core/gimp-tags.c
+++ b/app/core/gimp-tags.c
@@ -20,9 +20,6 @@
 
 #include "config.h"
 
-#include <stdlib.h>
-#include <string.h>
-
 #include <gio/gio.h>
 #include <gegl.h>
 
@@ -73,7 +70,7 @@ gboolean
 gimp_tags_user_install (void)
 {
   GFile             *file;
-  gchar             *filename;
+  GOutputStream     *output;
   GMarkupParser      markup_parser;
   GimpXmlParser     *xml_parser;
   const char        *tags_locale;
@@ -106,8 +103,6 @@ gimp_tags_user_install (void)
   g_string_append (tags_installer.buf, "<?xml version='1.0' encoding='UTF-8'?>\n");
   g_string_append (tags_installer.buf, "<tags>\n");
 
-  file = gimp_data_directory_file ("tags", "gimp-tags-default.xml", NULL);
-
   markup_parser.start_element = gimp_tags_installer_load_start_element;
   markup_parser.end_element   = gimp_tags_installer_load_end_element;
   markup_parser.text          = gimp_tags_installer_load_text;
@@ -116,9 +111,10 @@ gimp_tags_user_install (void)
 
   xml_parser = gimp_xml_parser_new (&markup_parser, &tags_installer);
 
+  file = gimp_data_directory_file ("tags", "gimp-tags-default.xml", NULL);
   result = gimp_xml_parser_parse_gfile (xml_parser, file, &error);
-
   g_object_unref (file);
+
   gimp_xml_parser_free (xml_parser);
 
   if (! result)
@@ -129,22 +125,36 @@ gimp_tags_user_install (void)
 
   g_string_append (tags_installer.buf, "\n</tags>\n");
 
-  filename = g_build_filename (gimp_directory (), GIMP_TAGS_FILE, NULL);
-
-  result = g_file_set_contents (filename, tags_installer.buf->str,
-                                tags_installer.buf->len, &error);
-
-  g_free (filename);
-  g_string_free (tags_installer.buf, TRUE);
+  file = gimp_directory_file (GIMP_TAGS_FILE, NULL);
 
-  if (! result)
+  output = G_OUTPUT_STREAM (g_file_replace (file,
+                                            NULL, FALSE, G_FILE_CREATE_NONE,
+                                            NULL, &error));
+  if (! output)
     {
-      g_warning ("Error while creating tags.xml: %s\n", error->message);
-      g_error_free (error);
-      return FALSE;
+      g_printerr (_("Could not open '%s' for writing: %s"),
+                  gimp_file_get_utf8_name (file), error->message);
+      result = FALSE;
+    }
+  else if (! g_output_stream_write_all (output,
+                                        tags_installer.buf->str,
+                                        tags_installer.buf->len,
+                                        NULL, NULL, &error) ||
+           ! g_output_stream_close (output, NULL, &error))
+    {
+      g_printerr (_("Error writing '%s': %s"),
+                  gimp_file_get_utf8_name (file), error->message);
+      result = FALSE;
     }
 
-  return TRUE;
+  if (output)
+    g_object_unref (output);
+
+  g_clear_error (&error);
+  g_object_unref (file);
+  g_string_free (tags_installer.buf, TRUE);
+
+  return result;
 }
 
 static  void


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