[gnome-photos] base-item: Port the download operation to GTask



commit 63a95d0917ea5cef8c2aefe2209a3a0a5f6840a0
Author: Debarshi Ray <debarshir gnome org>
Date:   Mon Aug 12 12:48:51 2013 +0200

    base-item: Port the download operation to GTask

 src/photos-base-item.c |   80 ++++++++++++-----------------------------------
 1 files changed, 21 insertions(+), 59 deletions(-)
---
diff --git a/src/photos-base-item.c b/src/photos-base-item.c
index c5179e3..84c0507 100644
--- a/src/photos-base-item.c
+++ b/src/photos-base-item.c
@@ -95,35 +95,9 @@ static guint signals[LAST_SIGNAL] = { 0 };
 G_DEFINE_TYPE_WITH_PRIVATE (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)
 {
@@ -321,12 +295,14 @@ 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)
+photos_base_item_download_in_thread_func (GTask *task,
+                                          gpointer source_object,
+                                          gpointer task_data,
+                                          GCancellable *cancellable)
 {
-  PhotosBaseItem *self = PHOTOS_BASE_ITEM (object);
+  PhotosBaseItem *self = PHOTOS_BASE_ITEM (source_object);
   PhotosBaseItemPrivate *priv = self->priv;
   GError *error;
-  PhotosBaseItemAsyncStrData *op_res;
   gchar *path;
 
   g_mutex_lock (&priv->mutex_download);
@@ -334,11 +310,14 @@ photos_base_item_download_in_thread_func (GSimpleAsyncResult *simple, GObject *o
   error = NULL;
   path = photos_base_item_download (self, cancellable, &error);
   if (error != NULL)
-    g_simple_async_result_take_error (simple, error);
+    {
+      g_task_return_error (task, error);
+      goto out;
+    }
 
-  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_task_return_pointer (task, path, g_free);
 
+ out:
   g_mutex_unlock (&priv->mutex_download);
 }
 
@@ -982,44 +961,27 @@ photos_base_item_download_async (PhotosBaseItem *self,
                                  GAsyncReadyCallback callback,
                                  gpointer user_data)
 {
-  GSimpleAsyncResult *simple;
+  GTask *task;
 
-  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);
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_check_cancellable (task, TRUE);
+  g_task_set_source_tag (task, photos_base_item_download_async);
 
-  g_simple_async_result_run_in_thread (simple,
-                                       photos_base_item_download_in_thread_func,
-                                       G_PRIORITY_DEFAULT,
-                                       cancellable);
-  g_object_unref (simple);
+  g_task_run_in_thread (task, photos_base_item_download_in_thread_func);
+  g_object_unref (task);
 }
 
 
 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;
+  GTask *task = G_TASK (res);
 
-  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 (g_task_is_valid (res, self), NULL);
+  g_return_val_if_fail (g_task_get_source_tag (task) == 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;
+  return g_task_propagate_pointer (task, error);
 }
 
 


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