[gnome-photos/wip/rishi/download-gfile] Make photos_base_item_download return a GFile instead of a path



commit e651e8d7cf39cf12eb30f1215486d0075cfe884e
Author: Debarshi Ray <debarshir gnome org>
Date:   Thu Sep 6 09:58:37 2018 +0200

    Make photos_base_item_download return a GFile instead of a path
    
    It's prudent to standardize around GFile across the code because:
      * Being a reference counted object, it's cheaper to pass around. No
        need to allocate little memory buffers to copy strings around.
      * Supports both paths and URIs. Even though, in this particular case,
        it's always going to be a local path, that's not true for other
        areas of the code.
      * Encapsulates all GIO operations that can be performed on a file.
        Rarely, if ever, does the code directly use UNIX system calls or
        the C standard library to operate on files because of the need for
        asynchronous operations, support for cancellation, and nicer error
        handling.
    
    For all these reasons, the new codec API for GeglBuffer is also based
    around GFile.
    
    Exceptions when raw paths or URIs are useful:
      * Serializing and deserializing across D-Bus, databases, etc..
      * Logging error or debug messages.
      * Interacting with GIO-unfriendly APIs like Exiv2.
    
    https://gitlab.gnome.org/GNOME/gnome-photos/issues/63

 src/photos-base-item.c         | 32 ++++++++++++++++++++++----------
 src/photos-base-item.h         |  8 ++++----
 src/photos-device-item.c       |  2 +-
 src/photos-dlna-renderer.c     | 12 +++++++++---
 src/photos-facebook-item.c     | 10 +++++-----
 src/photos-flickr-item.c       |  6 +++---
 src/photos-google-item.c       |  9 ++++-----
 src/photos-local-item.c        | 14 ++++++++------
 src/photos-media-server-item.c |  6 +++---
 9 files changed, 59 insertions(+), 40 deletions(-)
---
diff --git a/src/photos-base-item.c b/src/photos-base-item.c
index 453e2eee..b88c43ba 100644
--- a/src/photos-base-item.c
+++ b/src/photos-base-item.c
@@ -1049,12 +1049,12 @@ photos_base_item_download_in_thread_func (GTask *task,
                                           GCancellable *cancellable)
 {
   PhotosBaseItem *self = PHOTOS_BASE_ITEM (source_object);
-  g_autofree gchar *path = NULL;
+  g_autoptr (GFile) file = NULL;
 
   {
     g_autoptr (GError) error = NULL;
 
-    path = photos_base_item_download (self, cancellable, &error);
+    file = photos_base_item_download (self, cancellable, &error);
     if (error != NULL)
       {
         g_task_return_error (task, g_steal_pointer (&error));
@@ -1062,7 +1062,7 @@ photos_base_item_download_in_thread_func (GTask *task,
       }
   }
 
-  g_task_return_pointer (task, g_strdup (path), g_free);
+  g_task_return_pointer (task, g_object_ref (file), g_object_unref);
 
  out:
   return;
@@ -1461,6 +1461,7 @@ static GeglBuffer *
 photos_base_item_load_buffer (PhotosBaseItem *self, GCancellable *cancellable, GError **error)
 {
   PhotosBaseItemPrivate *priv;
+  g_autoptr (GFile) file = NULL;
   g_autoptr (GeglBuffer) buffer = NULL;
   GeglBuffer *ret_val = NULL;
   GeglNode *buffer_sink;
@@ -1472,10 +1473,11 @@ photos_base_item_load_buffer (PhotosBaseItem *self, GCancellable *cancellable, G
 
   priv = photos_base_item_get_instance_private (self);
 
-  path = photos_base_item_download (self, cancellable, error);
-  if (path == NULL)
+  file = photos_base_item_download (self, cancellable, error);
+  if (file == NULL)
     goto out;
 
+  path = g_file_get_path (file);
   if (!g_utf8_validate (path, -1, NULL))
     {
       g_set_error (error, PHOTOS_ERROR, 0, "Path is not UTF-8 encoded");
@@ -1960,6 +1962,7 @@ photos_base_item_save_metadata_in_thread_func (GTask *task,
   PhotosBaseItem *self = PHOTOS_BASE_ITEM (source_object);
   PhotosBaseItemPrivate *priv;
   GFile *export_file = G_FILE (task_data);
+  g_autoptr (GFile) source_file = NULL;
   g_autoptr (GExiv2Metadata) metadata = NULL;
   g_autofree gchar *export_path = NULL;
   g_autofree gchar *source_path = NULL;
@@ -1971,7 +1974,7 @@ photos_base_item_save_metadata_in_thread_func (GTask *task,
   {
     g_autoptr (GError) error = NULL;
 
-    source_path = photos_base_item_download (self, cancellable, &error);
+    source_file = photos_base_item_download (self, cancellable, &error);
     if (error != NULL)
       {
         g_task_return_error (task, g_steal_pointer (&error));
@@ -1980,6 +1983,7 @@ photos_base_item_save_metadata_in_thread_func (GTask *task,
   }
 
   metadata = gexiv2_metadata_new ();
+  source_path = g_file_get_path (source_file);
 
   {
     g_autoptr (GError) error = NULL;
@@ -3349,19 +3353,27 @@ photos_base_item_destroy (PhotosBaseItem *self)
 }
 
 
-gchar *
+GFile *
 photos_base_item_download (PhotosBaseItem *self, GCancellable *cancellable, GError **error)
 {
   PhotosBaseItemPrivate *priv;
-  gchar *ret_val;
+  g_autoptr (GFile) file = NULL;
+  GFile *ret_val = NULL;
 
   g_return_val_if_fail (PHOTOS_IS_BASE_ITEM (self), NULL);
   priv = photos_base_item_get_instance_private (self);
 
   g_mutex_lock (&priv->mutex_download);
-  ret_val = PHOTOS_BASE_ITEM_GET_CLASS (self)->download (self, cancellable, error);
+  file = PHOTOS_BASE_ITEM_GET_CLASS (self)->download (self, cancellable, error);
   g_mutex_unlock (&priv->mutex_download);
 
+  if (file == NULL)
+    goto out;
+
+  g_return_val_if_fail (g_file_is_native (file), NULL);
+  ret_val = g_object_ref (file);
+
+ out:
   return ret_val;
 }
 
@@ -3383,7 +3395,7 @@ photos_base_item_download_async (PhotosBaseItem *self,
 }
 
 
-gchar *
+GFile *
 photos_base_item_download_finish (PhotosBaseItem *self, GAsyncResult *res, GError **error)
 {
   GTask *task;
diff --git a/src/photos-base-item.h b/src/photos-base-item.h
index c889ee16..864cdddc 100644
--- a/src/photos-base-item.h
+++ b/src/photos-base-item.h
@@ -1,7 +1,7 @@
 /*
  * Photos - access, organize and share your photos on GNOME
  * Copyright © 2014 Pranav Kant
- * Copyright © 2012 – 2017 Red Hat, Inc.
+ * Copyright © 2012 – 2018 Red Hat, Inc.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -60,7 +60,7 @@ struct _PhotosBaseItemClass
   GStrv       (*create_pipeline_paths)      (PhotosBaseItem *self);
   gboolean    (*create_thumbnail)           (PhotosBaseItem *self, GCancellable *cancellable, GError 
**error);
   gchar      *(*create_thumbnail_path)      (PhotosBaseItem *self);
-  gchar      *(*download)                   (PhotosBaseItem *self, GCancellable *cancellable, GError 
**error);
+  GFile      *(*download)                   (PhotosBaseItem *self, GCancellable *cancellable, GError 
**error);
   GtkWidget  *(*get_source_widget)          (PhotosBaseItem *self);
   gboolean    (*metadata_add_shared)        (PhotosBaseItem  *self,
                                              const gchar     *provider_type,
@@ -95,7 +95,7 @@ gchar              *photos_base_item_create_thumbnail_path   (PhotosBaseItem *se
 
 void                photos_base_item_destroy                 (PhotosBaseItem *self);
 
-gchar              *photos_base_item_download                (PhotosBaseItem *self,
+GFile              *photos_base_item_download                (PhotosBaseItem *self,
                                                               GCancellable *cancellable,
                                                               GError **error);
 
@@ -104,7 +104,7 @@ void                photos_base_item_download_async          (PhotosBaseItem *se
                                                               GAsyncReadyCallback callback,
                                                               gpointer user_data);
 
-gchar              *photos_base_item_download_finish         (PhotosBaseItem *self,
+GFile              *photos_base_item_download_finish         (PhotosBaseItem *self,
                                                               GAsyncResult *res,
                                                               GError **error);
 
diff --git a/src/photos-device-item.c b/src/photos-device-item.c
index 7d775617..cf067edd 100644
--- a/src/photos-device-item.c
+++ b/src/photos-device-item.c
@@ -302,7 +302,7 @@ photos_device_item_create_thumbnail_path (PhotosBaseItem *item)
 }
 
 
-static gchar *
+static GFile *
 photos_device_item_download (PhotosBaseItem *item, GCancellable *cancellable, GError **error)
 {
   g_assert_not_reached ();
diff --git a/src/photos-dlna-renderer.c b/src/photos-dlna-renderer.c
index 2fbae0bf..3beea43a 100644
--- a/src/photos-dlna-renderer.c
+++ b/src/photos-dlna-renderer.c
@@ -1,7 +1,7 @@
 /*
  * Photos - access, organize and share your photos on GNOME
  * Copyright © 2013 Intel Corporation. All rights reserved.
- * Copyright © 2013 – 2017 Red Hat, Inc.
+ * Copyright © 2013 – 2018 Red Hat, Inc.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -431,13 +431,14 @@ photos_dlna_renderer_share_download_cb (GObject *source_object,
 {
   PhotosDlnaRenderer *self;
   GError *error;
+  GFile *file = NULL;
   GTask *task = G_TASK (user_data);
   gchar *filename;
 
   self = PHOTOS_DLNA_RENDERER (g_task_get_source_object (task));
 
   error = NULL;
-  filename = photos_base_item_download_finish (PHOTOS_BASE_ITEM (source_object), res, &error);
+  file = photos_base_item_download_finish (PHOTOS_BASE_ITEM (source_object), res, &error);
   RETURN_ON_ERROR (task, error, "Unable to extract the local filename for the shared item");
 
   /* This will call a sequence of DBus methods to send the item to the DMR:
@@ -451,12 +452,14 @@ photos_dlna_renderer_share_download_cb (GObject *source_object,
    */
 
   /* 1) DleynaRenderer.PushHost.HostFile() */
+  filename = g_file_get_path (file);
   dleyna_renderer_push_host_call_host_file (self->push_host,
                                             filename,
                                             g_task_get_cancellable (task),
                                             photos_dlna_renderer_share_host_file_cb,
                                             task);
   g_free (filename);
+  g_object_unref (file);
 }
 
 
@@ -539,21 +542,24 @@ photos_dlna_renderer_unshare_download_cb (GObject *source_object,
 {
   PhotosDlnaRenderer *self;
   GError *error;
+  GFile *file = NULL;
   GTask *task = G_TASK (user_data);
   gchar *filename;
 
   self = PHOTOS_DLNA_RENDERER (g_task_get_source_object (task));
 
   error = NULL;
-  filename = photos_base_item_download_finish (PHOTOS_BASE_ITEM (source_object), res, &error);
+  file = photos_base_item_download_finish (PHOTOS_BASE_ITEM (source_object), res, &error);
   RETURN_ON_ERROR (task, error, "Unable to extract the local filename for the shared item");
 
+  filename = g_file_get_path (file);
   dleyna_renderer_push_host_call_remove_file (self->push_host,
                                               filename,
                                               g_task_get_cancellable (task),
                                               photos_dlna_renderer_unshare_remove_file_cb,
                                               task);
   g_free (filename);
+  g_object_unref (file);
 }
 
 
diff --git a/src/photos-facebook-item.c b/src/photos-facebook-item.c
index 33c9efc0..980775a8 100644
--- a/src/photos-facebook-item.c
+++ b/src/photos-facebook-item.c
@@ -1,7 +1,7 @@
 /*
  * Photos - access, organize and share your photos on GNOME
  * Copyright © 2013 Álvaro Peña
- * Copyright © 2014 – 2017 Red Hat, Inc.
+ * Copyright © 2014 – 2018 Red Hat, Inc.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -190,9 +190,10 @@ photos_facebook_item_create_thumbnail (PhotosBaseItem *item, GCancellable *cance
 }
 
 
-static gchar *
+static GFile *
 photos_facebook_item_download (PhotosBaseItem *item, GCancellable *cancellable, GError **error)
 {
+  GFile *ret_val = NULL;
   g_autoptr (GFile) local_file = NULL;
   g_autoptr (GFile) remote_file = NULL;
   GFBGraphPhoto *photo = NULL; /* TODO: use g_autoptr */
@@ -201,7 +202,6 @@ photos_facebook_item_download (PhotosBaseItem *item, GCancellable *cancellable,
   const gchar *local_filename;
   g_autofree gchar *local_dir = NULL;
   g_autofree gchar *local_path = NULL;
-  gchar *ret_val = NULL;
 
   cache_dir = g_get_user_cache_dir ();
   local_dir = g_build_filename (cache_dir, PACKAGE_TARNAME, "facebook", NULL);
@@ -209,6 +209,7 @@ photos_facebook_item_download (PhotosBaseItem *item, GCancellable *cancellable,
 
   local_filename = photos_base_item_get_filename (item);
   local_path = g_build_filename (local_dir, local_filename, NULL);
+  local_file = g_file_new_for_path (local_path);
   if (g_file_test (local_path, G_FILE_TEST_EXISTS))
     goto end;
 
@@ -224,7 +225,6 @@ photos_facebook_item_download (PhotosBaseItem *item, GCancellable *cancellable,
     }
 
   remote_file = g_file_new_for_uri (higher_image->source);
-  local_file = g_file_new_for_path (local_path);
 
   photos_debug (PHOTOS_DEBUG_NETWORK, "Downloading %s from Facebook to %s", higher_image->source, 
local_path);
   if (!g_file_copy (remote_file,
@@ -240,7 +240,7 @@ photos_facebook_item_download (PhotosBaseItem *item, GCancellable *cancellable,
     }
 
  end:
-  ret_val = g_steal_pointer (&local_path);
+  ret_val = g_object_ref (local_file);
 
  out:
   g_clear_object (&photo);
diff --git a/src/photos-flickr-item.c b/src/photos-flickr-item.c
index 3f5d478c..7b344a7c 100644
--- a/src/photos-flickr-item.c
+++ b/src/photos-flickr-item.c
@@ -258,9 +258,10 @@ photos_flickr_item_create_thumbnail (PhotosBaseItem *item, GCancellable *cancell
 }
 
 
-static gchar *
+static GFile *
 photos_flickr_item_download (PhotosBaseItem *item, GCancellable *cancellable, GError **error)
 {
+  GFile *ret_val = NULL;
   g_autoptr (GFile) local_file = NULL;
   g_autoptr (GFile) remote_file = NULL;
   const gchar *cache_dir;
@@ -268,7 +269,6 @@ photos_flickr_item_download (PhotosBaseItem *item, GCancellable *cancellable, GE
   const gchar *uri;
   g_autofree gchar *local_dir = NULL;
   g_autofree gchar *local_path = NULL;
-  gchar *ret_val = NULL;
 
   uri = photos_base_item_get_uri (item);
   remote_file = g_file_new_for_uri (uri);
@@ -297,7 +297,7 @@ photos_flickr_item_download (PhotosBaseItem *item, GCancellable *cancellable, GE
         }
     }
 
-  ret_val = g_steal_pointer (&local_path);
+  ret_val = g_object_ref (local_file);
 
  out:
   return ret_val;
diff --git a/src/photos-google-item.c b/src/photos-google-item.c
index faf7104a..718ee346 100644
--- a/src/photos-google-item.c
+++ b/src/photos-google-item.c
@@ -219,18 +219,18 @@ photos_google_item_create_thumbnail (PhotosBaseItem *item, GCancellable *cancell
 }
 
 
-static gchar *
+static GFile *
 photos_google_item_download (PhotosBaseItem *item, GCancellable *cancellable, GError **error)
 {
   GDataEntry *entry = NULL;
   GFile *local_file = NULL;
   GFile *remote_file = NULL;
+  GFile *ret_val = NULL;
   const gchar *cache_dir;
   const gchar *local_filename;
   const gchar *uri;
   gchar *local_dir = NULL;
   gchar *local_path = NULL;
-  gchar *ret_val = NULL;
 
   cache_dir = g_get_user_cache_dir ();
   local_dir = g_build_filename (cache_dir, PACKAGE_TARNAME, "google", NULL);
@@ -238,6 +238,7 @@ photos_google_item_download (PhotosBaseItem *item, GCancellable *cancellable, GE
 
   local_filename = photos_base_item_get_filename (item);
   local_path = g_build_filename (local_dir, local_filename, NULL);
+  local_file = g_file_new_for_path (local_path);
   if (g_file_test (local_path, G_FILE_TEST_EXISTS))
     goto end;
 
@@ -247,7 +248,6 @@ photos_google_item_download (PhotosBaseItem *item, GCancellable *cancellable, GE
 
   uri = gdata_entry_get_content_uri (entry);
   remote_file = g_file_new_for_uri (uri);
-  local_file = g_file_new_for_path (local_path);
 
   photos_debug (PHOTOS_DEBUG_NETWORK, "Downloading %s from Google to %s", uri, local_path);
   if (!g_file_copy (remote_file,
@@ -263,8 +263,7 @@ photos_google_item_download (PhotosBaseItem *item, GCancellable *cancellable, GE
     }
 
  end:
-  ret_val = local_path;
-  local_path = NULL;
+  ret_val = g_object_ref (local_file);
 
  out:
   g_free (local_path);
diff --git a/src/photos-local-item.c b/src/photos-local-item.c
index a337bad1..49a40947 100644
--- a/src/photos-local-item.c
+++ b/src/photos-local-item.c
@@ -217,15 +217,15 @@ photos_local_item_create_thumbnail (PhotosBaseItem *item, GCancellable *cancella
 }
 
 
-static gchar *
+static GFile *
 photos_local_item_download (PhotosBaseItem *item, GCancellable *cancellable, GError **error)
 {
+  GFile *file;
   const gchar *uri;
-  gchar *path;
 
   uri = photos_base_item_get_uri (item);
-  path = g_filename_from_uri (uri, NULL, error);
-  return path;
+  file = g_file_new_for_uri (uri);
+  return file;
 }
 
 
@@ -281,6 +281,7 @@ photos_local_item_metadata_add_shared (PhotosBaseItem  *item,
                                        GCancellable    *cancellable,
                                        GError         **error)
 {
+  g_autoptr (GFile) file = NULL;
   g_autoptr (GVariant) shared_variant = NULL;
   const GVariantType *tuple_items[] =
     {
@@ -307,11 +308,12 @@ photos_local_item_metadata_add_shared (PhotosBaseItem  *item,
       goto out;
     }
 
-  path = photos_base_item_download (item, cancellable, error);
-  if (path == NULL)
+  file = photos_base_item_download (item, cancellable, error);
+  if (file == NULL)
     goto out;
 
   metadata = gexiv2_metadata_new ();
+  path = g_file_get_path (file);
 
   if (!gexiv2_metadata_open_path (metadata, path, error))
     goto out;
diff --git a/src/photos-media-server-item.c b/src/photos-media-server-item.c
index 029a2b51..1668d4dc 100644
--- a/src/photos-media-server-item.c
+++ b/src/photos-media-server-item.c
@@ -112,9 +112,10 @@ photos_media_server_item_create_thumbnail (PhotosBaseItem *item, GCancellable *c
 }
 
 
-static gchar *
+static GFile *
 photos_media_server_item_download (PhotosBaseItem *item, GCancellable *cancellable, GError **error)
 {
+  GFile *ret_val = NULL;
   g_autoptr (GFile) local_file = NULL;
   g_autoptr (GFile) remote_file = NULL;
   const gchar *cache_dir;
@@ -122,7 +123,6 @@ photos_media_server_item_download (PhotosBaseItem *item, GCancellable *cancellab
   g_autofree gchar *local_dir = NULL;
   g_autofree gchar *local_filename = NULL;
   g_autofree gchar *local_path = NULL;
-  gchar *ret_val = NULL;
 
   uri = photos_base_item_get_uri (item);
   remote_file = g_file_new_for_uri (uri);
@@ -151,7 +151,7 @@ photos_media_server_item_download (PhotosBaseItem *item, GCancellable *cancellab
         }
     }
 
-  ret_val = g_steal_pointer (&local_path);
+  ret_val = g_object_ref (local_file);
 
  out:
   return ret_val;


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