[gnome-photos] base-item: Let the download vfunc be publicly available



commit ade6c96c6ef0ce0ec943d97356865b9bef8ded26
Author: Debarshi Ray <debarshir gnome org>
Date:   Mon Jul 29 16:21:27 2013 +0200

    base-item: Let the download vfunc be publicly available
    
    This will be needed when pushing remote items to a DMR.

 src/photos-base-item.c |  108 +++++++++++++++++++++++++++++++++++++++++++++++-
 src/photos-base-item.h |   11 +++++
 2 files changed, 118 insertions(+), 1 deletions(-)
---
diff --git a/src/photos-base-item.c b/src/photos-base-item.c
index a232d07..48a1f7b 100644
--- a/src/photos-base-item.c
+++ b/src/photos-base-item.c
@@ -48,6 +48,7 @@ struct _PhotosBaseItemPrivate
   GeglNode *node;
   GeglRectangle bbox;
   GMutex mutex_create_thumbnail;
+  GMutex mutex_download;
   GMutex mutex;
   PhotosCollectionIconWatcher *watcher;
   TrackerSparqlCursor *cursor;
@@ -90,9 +91,35 @@ static guint signals[LAST_SIGNAL] = { 0 };
 G_DEFINE_TYPE (PhotosBaseItem, photos_base_item, G_TYPE_OBJECT);
 
 
+typedef struct _PhotosBaseItemAsyncStrData PhotosBaseItemAsyncStrData;
+
+struct _PhotosBaseItemAsyncStrData
+{
+  gchar *str;
+};
+
 static void photos_base_item_populate_from_cursor (PhotosBaseItem *self, TrackerSparqlCursor *cursor);
 
 
+static PhotosBaseItemAsyncStrData *
+photos_base_item_async_str_data_new (gchar *str)
+{
+  PhotosBaseItemAsyncStrData *data;
+
+  data = g_slice_new0 (PhotosBaseItemAsyncStrData);
+  data->str = str;
+  return data;
+}
+
+
+static void
+photos_base_item_async_str_data_free (PhotosBaseItemAsyncStrData *data)
+{
+  g_free (data->str);
+  g_slice_free (PhotosBaseItemAsyncStrData, data);
+}
+
+
 static GIcon *
 photos_base_item_create_symbolic_emblem (const gchar *name)
 {
@@ -290,6 +317,29 @@ photos_base_item_default_open (PhotosBaseItem *self, GdkScreen *screen, guint32
 
 
 static void
+photos_base_item_download_in_thread_func (GSimpleAsyncResult *simple, GObject *object, GCancellable 
*cancellable)
+{
+  PhotosBaseItem *self = PHOTOS_BASE_ITEM (object);
+  PhotosBaseItemPrivate *priv = self->priv;
+  GError *error;
+  PhotosBaseItemAsyncStrData *op_res;
+  gchar *path;
+
+  g_mutex_lock (&priv->mutex_download);
+
+  error = NULL;
+  path = photos_base_item_download (self, cancellable, &error);
+  if (error != NULL)
+    g_simple_async_result_take_error (simple, error);
+
+  op_res = photos_base_item_async_str_data_new (path);
+  g_simple_async_result_set_op_res_gpointer (simple, op_res, (GDestroyNotify) 
photos_base_item_async_str_data_free);
+
+  g_mutex_unlock (&priv->mutex_download);
+}
+
+
+static void
 photos_base_item_icon_updated (PhotosBaseItem *self, GIcon *icon)
 {
   PhotosBaseItemPrivate *priv = self->priv;
@@ -511,7 +561,7 @@ photos_base_item_load (PhotosBaseItem *self, GCancellable *cancellable, GError *
 
   if (priv->graph == NULL)
     {
-      path = PHOTOS_BASE_ITEM_GET_CLASS (self)->download(self, cancellable, error);
+      path = photos_base_item_download (self, cancellable, error);
       if (path == NULL)
         goto out;
 
@@ -760,6 +810,7 @@ photos_base_item_finalize (GObject *object)
   g_free (priv->uri);
 
   g_mutex_clear (&priv->mutex_create_thumbnail);
+  g_mutex_clear (&priv->mutex_download);
   g_mutex_clear (&priv->mutex);
 
   G_OBJECT_CLASS (photos_base_item_parent_class)->finalize (object);
@@ -816,6 +867,7 @@ photos_base_item_init (PhotosBaseItem *self)
   priv = self->priv;
 
   g_mutex_init (&priv->mutex_create_thumbnail);
+  g_mutex_init (&priv->mutex_download);
   g_mutex_init (&priv->mutex);
 }
 
@@ -888,6 +940,60 @@ photos_base_item_destroy (PhotosBaseItem *self)
 }
 
 
+gchar *
+photos_base_item_download (PhotosBaseItem *self, GCancellable *cancellable, GError **error)
+{
+  return PHOTOS_BASE_ITEM_GET_CLASS (self)->download(self, cancellable, error);
+}
+
+
+void
+photos_base_item_download_async (PhotosBaseItem *self,
+                                 GCancellable *cancellable,
+                                 GAsyncReadyCallback callback,
+                                 gpointer user_data)
+{
+  GSimpleAsyncResult *simple;
+
+  simple = g_simple_async_result_new (G_OBJECT (self),
+                                      callback,
+                                      user_data,
+                                      photos_base_item_download_async);
+  g_simple_async_result_set_check_cancellable (simple, cancellable);
+
+  g_simple_async_result_run_in_thread (simple,
+                                       photos_base_item_download_in_thread_func,
+                                       G_PRIORITY_DEFAULT,
+                                       cancellable);
+  g_object_unref (simple);
+}
+
+
+gchar *
+photos_base_item_download_finish (PhotosBaseItem *self, GAsyncResult *res, GError **error)
+{
+  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
+  PhotosBaseItemAsyncStrData *op_res;
+  gchar *ret_val = NULL;
+
+  g_return_val_if_fail (g_simple_async_result_is_valid (res,
+                                                        G_OBJECT (self),
+                                                        photos_base_item_download_async),
+                        NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+  if (g_simple_async_result_propagate_error (simple, error))
+    goto out;
+
+  op_res = g_simple_async_result_get_op_res_gpointer (simple);
+  ret_val = op_res->str;
+  op_res->str = NULL;
+
+ out:
+  return ret_val;
+}
+
+
 const gchar *
 photos_base_item_get_author (PhotosBaseItem *self)
 {
diff --git a/src/photos-base-item.h b/src/photos-base-item.h
index d44c6eb..652a339 100644
--- a/src/photos-base-item.h
+++ b/src/photos-base-item.h
@@ -85,6 +85,17 @@ gboolean            photos_base_item_can_trash          (PhotosBaseItem *self);
 
 void                photos_base_item_destroy            (PhotosBaseItem *self);
 
+gchar              *photos_base_item_download           (PhotosBaseItem *self,
+                                                         GCancellable *cancellable,
+                                                         GError **error);
+
+void                photos_base_item_download_async     (PhotosBaseItem *self,
+                                                         GCancellable *cancellable,
+                                                         GAsyncReadyCallback callback,
+                                                         gpointer user_data);
+
+gchar              *photos_base_item_download_finish    (PhotosBaseItem *self, GAsyncResult *res, GError 
**error);
+
 const gchar        *photos_base_item_get_author         (PhotosBaseItem *self);
 
 GeglRectangle       photos_base_item_get_bbox           (PhotosBaseItem *self);


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