[gimp] plug-ins: more GFile porting in file-uri



commit 1348cd06a6dacf0d008548ae07846f9cd396a8ba
Author: Michael Natterer <mitch gimp org>
Date:   Thu Jul 10 21:38:23 2014 +0200

    plug-ins: more GFile porting in file-uri

 plug-ins/file-uri/uri-backend-gvfs.c |   49 +++++++----------------
 plug-ins/file-uri/uri-backend.h      |    6 +-
 plug-ins/file-uri/uri.c              |   73 +++++++++++++++++++++------------
 3 files changed, 64 insertions(+), 64 deletions(-)
---
diff --git a/plug-ins/file-uri/uri-backend-gvfs.c b/plug-ins/file-uri/uri-backend-gvfs.c
index 52eddef..9eff3b6 100644
--- a/plug-ins/file-uri/uri-backend-gvfs.c
+++ b/plug-ins/file-uri/uri-backend-gvfs.c
@@ -40,7 +40,7 @@ typedef enum
 
 
 static gchar    * get_protocols          (void);
-static gboolean   copy_uri               (GFile        *src_file,
+static gboolean   copy_file              (GFile        *src_file,
                                           GFile        *dest_file,
                                           Mode          mode,
                                           GimpRunMode   run_mode,
@@ -102,46 +102,27 @@ uri_backend_get_save_protocols (void)
 
 gboolean
 uri_backend_load_image (GFile        *file,
-                        const gchar  *tmpname,
+                        GFile        *local_file,
                         GimpRunMode   run_mode,
                         GError      **error)
 {
-  GFile    *dest_file;
-  gboolean  success;
-
-  dest_file = g_file_new_for_path (tmpname);
-
-  success = copy_uri (file, dest_file, DOWNLOAD, run_mode, error);
-
-  g_object_unref (dest_file);
-
-  return success;
+  return copy_file (file, local_file, DOWNLOAD, run_mode, error);
 }
 
 gboolean
 uri_backend_save_image (GFile        *file,
-                        const gchar  *tmpname,
+                        GFile        *local_file,
                         GimpRunMode   run_mode,
                         GError      **error)
 {
-  GFile    *src_file;
-  gboolean  success;
-
-  src_file = g_file_new_for_path (tmpname);
-
-  success = copy_uri (src_file, file, UPLOAD, run_mode, error);
-
-  g_object_unref (src_file);
-
-  return success;
+  return copy_file (local_file, file, UPLOAD, run_mode, error);
 }
 
-gchar *
+GFile *
 uri_backend_map_image (GFile       *file,
                        GimpRunMode  run_mode)
 {
-  gchar    *path    = NULL;
-  gboolean  success = TRUE;
+  gboolean success = TRUE;
 
   if (run_mode == GIMP_RUN_INTERACTIVE)
     {
@@ -157,10 +138,10 @@ uri_backend_map_image (GFile       *file,
         }
     }
 
-  if (success)
-    path = g_file_get_path (file);
+  if (success && g_file_is_native (file))
+    return g_object_ref (file);
 
-  return path;
+  return NULL;
 }
 
 
@@ -293,11 +274,11 @@ mount_enclosing_volume (GFile   *file,
 }
 
 static gboolean
-copy_uri (GFile        *src_file,
-          GFile        *dest_file,
-          Mode          mode,
-          GimpRunMode   run_mode,
-          GError      **error)
+copy_file (GFile        *src_file,
+           GFile        *dest_file,
+           Mode          mode,
+           GimpRunMode   run_mode,
+           GError      **error)
 {
   UriProgress progress = { 0, };
   gboolean    success;
diff --git a/plug-ins/file-uri/uri-backend.h b/plug-ins/file-uri/uri-backend.h
index 6c8168a..1ece627 100644
--- a/plug-ins/file-uri/uri-backend.h
+++ b/plug-ins/file-uri/uri-backend.h
@@ -32,14 +32,14 @@ const gchar * uri_backend_get_load_protocols (void);
 const gchar * uri_backend_get_save_protocols (void);
 
 gboolean      uri_backend_load_image         (GFile        *file,
-                                              const gchar  *tmpname,
+                                              GFile        *local_file,
                                               GimpRunMode   run_mode,
                                               GError      **error);
 gboolean      uri_backend_save_image         (GFile        *file,
-                                              const gchar  *tmpname,
+                                              GFile        *local_file,
                                               GimpRunMode   run_mode,
                                               GError      **error);
-gchar       * uri_backend_map_image          (GFile        *file,
+GFile       * uri_backend_map_image          (GFile        *file,
                                               GimpRunMode   run_mode);
 
 
diff --git a/plug-ins/file-uri/uri.c b/plug-ins/file-uri/uri.c
index ffed739..640dcc0 100644
--- a/plug-ins/file-uri/uri.c
+++ b/plug-ins/file-uri/uri.c
@@ -59,9 +59,9 @@ static GimpPDBStatusType   save_image    (GFile            *file,
                                           gint32            run_mode,
                                           GError          **error);
 
-static gchar             * get_temp_name (GFile            *file,
+static GFile             * get_temp_file (GFile            *file,
                                           gboolean         *name_image);
-static gboolean            valid_file    (const gchar      *filename);
+static gboolean            valid_file    (GFile            *file);
 
 
 const GimpPlugInInfo PLUG_IN_INFO =
@@ -240,24 +240,26 @@ load_image (GFile        *file,
 {
   gint32    image_ID   = -1;
   gboolean  name_image = FALSE;
-  gchar    *tmpname;
+  GFile    *local_file;
   gboolean  mapped     = FALSE;
 
-  tmpname = uri_backend_map_image (file, run_mode);
+  local_file = uri_backend_map_image (file, run_mode);
 
-  if (tmpname)
+  if (local_file)
     {
       mapped = TRUE;
     }
   else
     {
-      tmpname = get_temp_name (file, &name_image);
+      local_file = get_temp_file (file, &name_image);
 
-      if (! uri_backend_load_image (file, tmpname, run_mode, error))
+      if (! uri_backend_load_image (file, local_file, run_mode, error))
         return -1;
     }
 
-  image_ID = gimp_file_load (run_mode, tmpname, tmpname);
+  image_ID = gimp_file_load (run_mode,
+                             g_file_get_path (local_file),
+                             g_file_get_path (local_file));
 
   if (image_ID != -1)
     {
@@ -273,9 +275,9 @@ load_image (GFile        *file,
     }
 
   if (! mapped)
-    g_unlink (tmpname);
+    g_file_delete (local_file, NULL, NULL);
 
-  g_free (tmpname);
+  g_object_unref (local_file);
 
   return image_ID;
 }
@@ -288,29 +290,29 @@ save_image (GFile   *file,
             GError **error)
 {
   GimpPDBStatusType  status = GIMP_PDB_EXECUTION_ERROR;
-  gchar             *tmpname;
+  GFile             *local_file;
   gboolean           mapped = FALSE;
 
-  tmpname = uri_backend_map_image (file, run_mode);
+  local_file = uri_backend_map_image (file, run_mode);
 
-  if (tmpname)
+  if (local_file)
     mapped = TRUE;
   else
-    tmpname = get_temp_name (file, NULL);
+    local_file = get_temp_file (file, NULL);
 
   if (gimp_file_save (run_mode,
                       image_ID,
                       drawable_ID,
-                      tmpname,
-                      tmpname))
+                      g_file_get_path (local_file),
+                      g_file_get_path (local_file)))
     {
       if (mapped)
         {
           status = GIMP_PDB_SUCCESS;
         }
-      else if (valid_file (tmpname))
+      else if (valid_file (local_file))
         {
-          if (uri_backend_save_image (file, tmpname, run_mode, error))
+          if (uri_backend_save_image (file, local_file, run_mode, error))
             {
               status = GIMP_PDB_SUCCESS;
             }
@@ -319,7 +321,7 @@ save_image (GFile   *file,
         {
           g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                        _("Failed to save to temporary file '%s'"),
-                       gimp_filename_to_utf8 (tmpname));
+                       gimp_file_get_utf8_name (local_file));
         }
     }
   else
@@ -329,19 +331,20 @@ save_image (GFile   *file,
     }
 
   if (! mapped)
-    g_unlink (tmpname);
+    g_file_delete (local_file, NULL, NULL);
 
-  g_free (tmpname);
+  g_object_unref (local_file);
 
   return status;
 }
 
-static gchar *
-get_temp_name (GFile    *file,
+static GFile *
+get_temp_file (GFile    *file,
                gboolean *name_image)
 {
   gchar *basename;
   gchar *tmpname = NULL;
+  GFile *tmpfile;
 
   if (name_image)
     *name_image = FALSE;
@@ -366,13 +369,29 @@ get_temp_name (GFile    *file,
   if (! tmpname)
     tmpname = gimp_temp_name ("xxx");
 
-  return tmpname;
+  tmpfile = g_file_new_for_path (tmpname);
+
+  g_free (tmpname);
+
+  return tmpfile;
 }
 
 static gboolean
-valid_file (const gchar *filename)
+valid_file (GFile *file)
 {
-  struct stat buf;
+  GFileInfo *info = g_file_query_info (file,
+                                       G_FILE_ATTRIBUTE_STANDARD_SIZE,
+                                       G_FILE_QUERY_INFO_NONE,
+                                       NULL, NULL);
+
+  if (info)
+    {
+      goffset size = g_file_info_get_size (info);
+
+      g_object_unref (info);
+
+      return size > 0;
+    }
 
-  return g_stat (filename, &buf) == 0 && buf.st_size > 0;
+  return FALSE;
 }


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