[gnome-photos] single-item-job: Convert to async API



commit 0a9e97b1a38f71addbf4c40d730c8beb616bf1cb
Author: Rafael Fonseca <r4f4rfs gmail com>
Date:   Wed Apr 27 14:01:23 2016 +0200

    single-item-job: Convert to async API
    
    https://bugzilla.gnome.org/show_bug.cgi?id=764086

 src/photos-application.c             |   15 ++++++-
 src/photos-base-item.c               |   15 ++++++-
 src/photos-collection-icon-watcher.c |   14 +++++-
 src/photos-fetch-metas-job.c         |   16 ++++++-
 src/photos-item-manager.c            |   15 ++++++-
 src/photos-single-item-job.c         |   83 ++++++++++++++++++---------------
 src/photos-single-item-job.h         |    7 +++-
 7 files changed, 121 insertions(+), 44 deletions(-)
---
diff --git a/src/photos-application.c b/src/photos-application.c
index 4b1368c..de42521 100644
--- a/src/photos-application.c
+++ b/src/photos-application.c
@@ -668,12 +668,23 @@ photos_application_activate_item (PhotosApplication *self, GObject *item)
 
 
 static void
-photos_application_activate_query_executed (TrackerSparqlCursor *cursor, gpointer user_data)
+photos_application_activate_query_executed (GObject *source_object, GAsyncResult *res, gpointer user_data)
 {
   PhotosApplication *self = PHOTOS_APPLICATION (user_data);
+  GError *error = NULL;
   GObject *item;
+  PhotosSingleItemJob *job = PHOTOS_SINGLE_ITEM_JOB (source_object);
+  TrackerSparqlCursor *cursor = NULL;
   const gchar *identifier;
 
+  cursor = photos_single_item_job_finish (job, res, &error);
+  if (error != NULL)
+    {
+      g_warning ("Unable to query single item: %s", error->message);
+      g_error_free (error);
+      goto out;
+    }
+
   if (cursor == NULL)
     goto out;
 
@@ -685,6 +696,7 @@ photos_application_activate_query_executed (TrackerSparqlCursor *cursor, gpointe
   photos_application_activate_item (self, item);
 
  out:
+  g_clear_object (&cursor);
   g_application_release (G_APPLICATION (self));
 }
 
@@ -711,6 +723,7 @@ photos_application_activate_result (PhotosApplication *self,
       photos_single_item_job_run (job,
                                   self->state,
                                   PHOTOS_QUERY_FLAGS_UNFILTERED,
+                                  NULL,
                                   photos_application_activate_query_executed,
                                   self);
       g_object_unref (job);
diff --git a/src/photos-base-item.c b/src/photos-base-item.c
index 7648bf3..30daf7f 100644
--- a/src/photos-base-item.c
+++ b/src/photos-base-item.c
@@ -545,9 +545,20 @@ photos_base_item_refresh_collection_icon (PhotosBaseItem *self)
 
 
 static void
-photos_base_item_refresh_executed (TrackerSparqlCursor *cursor, gpointer user_data)
+photos_base_item_refresh_executed (GObject *source_object, GAsyncResult *res, gpointer user_data)
 {
   PhotosBaseItem *self = PHOTOS_BASE_ITEM (user_data);
+  GError *error = NULL;
+  PhotosSingleItemJob *job = PHOTOS_SINGLE_ITEM_JOB (source_object);
+  TrackerSparqlCursor *cursor = NULL;
+
+  cursor = photos_single_item_job_finish (job, res, &error);
+  if (error != NULL)
+    {
+      g_warning ("Unable to query single item: %s", error->message);
+      g_error_free (error);
+      goto out;
+    }
 
   if (cursor == NULL)
     goto out;
@@ -555,6 +566,7 @@ photos_base_item_refresh_executed (TrackerSparqlCursor *cursor, gpointer user_da
   photos_base_item_populate_from_cursor (self, cursor);
 
  out:
+  g_clear_object (&cursor);
   g_object_unref (self);
 }
 
@@ -2476,6 +2488,7 @@ photos_base_item_refresh (PhotosBaseItem *self)
   photos_single_item_job_run (job,
                               state,
                               PHOTOS_QUERY_FLAGS_NONE,
+                              NULL,
                               photos_base_item_refresh_executed,
                               g_object_ref (self));
   g_object_unref (job);
diff --git a/src/photos-collection-icon-watcher.c b/src/photos-collection-icon-watcher.c
index fd56cd3..a12d43c 100644
--- a/src/photos-collection-icon-watcher.c
+++ b/src/photos-collection-icon-watcher.c
@@ -169,9 +169,19 @@ photos_collection_icon_watcher_to_query_collector (PhotosCollectionIconWatcher *
 
 
 static void
-photos_collection_icon_watcher_to_query_executed (TrackerSparqlCursor *cursor, gpointer user_data)
+photos_collection_icon_watcher_to_query_executed (GObject *source_object, GAsyncResult *res, gpointer 
user_data)
 {
   PhotosCollectionIconWatcher *self = PHOTOS_COLLECTION_ICON_WATCHER (user_data);
+  GError *error = NULL;
+  PhotosSingleItemJob *job = PHOTOS_SINGLE_ITEM_JOB (source_object);
+  TrackerSparqlCursor *cursor = NULL;
+
+  cursor = photos_single_item_job_finish (job, res, &error);
+  if (error != NULL)
+    {
+      g_warning ("Unable to query single item: %s", error->message);
+      g_error_free (error);
+    }
 
   if (cursor != NULL && self->item_mngr != NULL)
     {
@@ -182,6 +192,7 @@ photos_collection_icon_watcher_to_query_executed (TrackerSparqlCursor *cursor, g
     }
 
   photos_collection_icon_watcher_to_query_collector (self);
+  g_clear_object (&cursor);
   g_object_unref (self);
 }
 
@@ -234,6 +245,7 @@ photos_collection_icon_watcher_finished (PhotosCollectionIconWatcher *self)
       photos_single_item_job_run (job,
                                   state,
                                   PHOTOS_QUERY_FLAGS_UNFILTERED,
+                                  NULL,
                                   photos_collection_icon_watcher_to_query_executed,
                                   g_object_ref (self));
       g_object_unref (job);
diff --git a/src/photos-fetch-metas-job.c b/src/photos-fetch-metas-job.c
index 51f1458..6ce8872 100644
--- a/src/photos-fetch-metas-job.c
+++ b/src/photos-fetch-metas-job.c
@@ -151,16 +151,27 @@ photos_fetch_metas_job_create_collection_pixbuf (PhotosFetchMetasJob *self, Phot
 
 
 static void
-photos_fetch_metas_job_executed (TrackerSparqlCursor *cursor, gpointer user_data)
+photos_fetch_metas_job_executed (GObject *source_object, GAsyncResult *res, gpointer user_data)
 {
   PhotosFetchMetasJob *self = PHOTOS_FETCH_METAS_JOB (user_data);
+  GError *error = NULL;
   GIcon *icon = NULL;
   PhotosFetchMeta *meta;
+  PhotosSingleItemJob *job = PHOTOS_SINGLE_ITEM_JOB (source_object);
+  TrackerSparqlCursor *cursor = NULL;
   gboolean is_collection;
   const gchar *id;
   const gchar *rdf_type;
   const gchar *title;
 
+  cursor = photos_single_item_job_finish (job, res, &error);
+  if (error != NULL)
+    {
+      g_warning ("Unable to query single item: %s", error->message);
+      g_error_free (error);
+      goto out;
+    }
+
   id = tracker_sparql_cursor_get_string (cursor, PHOTOS_QUERY_COLUMNS_URN, NULL);
   title = tracker_sparql_cursor_get_string (cursor, PHOTOS_QUERY_COLUMNS_TITLE, NULL);
   rdf_type = tracker_sparql_cursor_get_string (cursor, PHOTOS_QUERY_COLUMNS_RDF_TYPE, NULL);
@@ -183,6 +194,8 @@ photos_fetch_metas_job_executed (TrackerSparqlCursor *cursor, gpointer user_data
       photos_fetch_metas_job_collector (self);
     }
 
+ out:
+  g_clear_object (&cursor);
   g_clear_object (&icon);
   g_object_unref (self);
 }
@@ -296,6 +309,7 @@ photos_fetch_metas_job_run (PhotosFetchMetasJob *self,
       photos_single_item_job_run (job,
                                   state,
                                   PHOTOS_QUERY_FLAGS_UNFILTERED,
+                                  NULL,
                                   photos_fetch_metas_job_executed,
                                   g_object_ref (self));
       g_object_unref (job);
diff --git a/src/photos-item-manager.c b/src/photos-item-manager.c
index 18abc84..c1b60ad 100644
--- a/src/photos-item-manager.c
+++ b/src/photos-item-manager.c
@@ -118,9 +118,20 @@ photos_item_manager_add_object (PhotosBaseManager *mngr, GObject *object)
 
 
 static void
-photos_item_manager_item_created_executed (TrackerSparqlCursor *cursor, gpointer user_data)
+photos_item_manager_item_created_executed (GObject *source_object, GAsyncResult *res, gpointer user_data)
 {
   PhotosItemManager *self = PHOTOS_ITEM_MANAGER (user_data);
+  GError *error = NULL;
+  PhotosSingleItemJob *job = PHOTOS_SINGLE_ITEM_JOB (source_object);
+  TrackerSparqlCursor *cursor = NULL;
+
+  cursor = photos_single_item_job_finish (job, res, &error);
+  if (error != NULL)
+    {
+      g_warning ("Unable to query single item: %s", error->message);
+      g_error_free (error);
+      goto out;
+    }
 
   if (cursor == NULL)
     goto out;
@@ -128,6 +139,7 @@ photos_item_manager_item_created_executed (TrackerSparqlCursor *cursor, gpointer
   photos_item_manager_add_item (self, cursor);
 
  out:
+  g_clear_object (&cursor);
   g_object_unref (self);
 }
 
@@ -146,6 +158,7 @@ photos_item_manager_item_created (PhotosItemManager *self, const gchar *urn)
   photos_single_item_job_run (job,
                               state,
                               PHOTOS_QUERY_FLAGS_NONE,
+                              NULL,
                               photos_item_manager_item_created_executed,
                               g_object_ref (self));
   g_object_unref (job);
diff --git a/src/photos-single-item-job.c b/src/photos-single-item-job.c
index ab0fc95..cc6f618 100644
--- a/src/photos-single-item-job.c
+++ b/src/photos-single-item-job.c
@@ -36,11 +36,9 @@
 struct _PhotosSingleItemJob
 {
   GObject parent_instance;
-  PhotosSingleItemJobCallback callback;
+  GError *queue_error;
   PhotosTrackerQueue *queue;
-  TrackerSparqlCursor *cursor;
   gchar *urn;
-  gpointer user_data;
 };
 
 struct _PhotosSingleItemJobClass
@@ -59,46 +57,40 @@ G_DEFINE_TYPE (PhotosSingleItemJob, photos_single_item_job, G_TYPE_OBJECT);
 
 
 static void
-photos_single_item_job_emit_callback (PhotosSingleItemJob *self)
-{
-  if (self->callback == NULL)
-    return;
-
-  (*self->callback) (self->cursor, self->user_data);
-}
-
-
-static void
 photos_single_item_job_cursor_next (GObject *source_object, GAsyncResult *res, gpointer user_data)
 {
-  PhotosSingleItemJob *self = PHOTOS_SINGLE_ITEM_JOB (user_data);
+  GTask *task = G_TASK (user_data);
   TrackerSparqlCursor *cursor = TRACKER_SPARQL_CURSOR (source_object);
+  GDestroyNotify result_destroy = NULL;
   GError *error;
   gboolean valid;
+  gpointer result = NULL;
 
   error = NULL;
   valid = tracker_sparql_cursor_next_finish (cursor, res, &error);
   if (error != NULL)
     {
-      g_warning ("Unable to query single item: %s", error->message);
-      g_error_free (error);
+      g_task_return_error (task, error);
       goto out;
     }
-  else if (!valid)
-    goto out;
 
-  self->cursor = g_object_ref (cursor);
+  if (valid)
+    {
+      result = g_object_ref (cursor);
+      result_destroy = g_object_unref;
+    }
+
+  g_task_return_pointer (task, result, result_destroy);
 
  out:
-  photos_single_item_job_emit_callback (self);
-  g_object_unref (self);
+  g_object_unref (task);
 }
 
 
 static void
 photos_single_item_job_query_executed (GObject *source_object, GAsyncResult *res, gpointer user_data)
 {
-  PhotosSingleItemJob *self = PHOTOS_SINGLE_ITEM_JOB (user_data);
+  GTask *task = G_TASK (user_data);
   TrackerSparqlConnection *connection = TRACKER_SPARQL_CONNECTION (source_object);
   TrackerSparqlCursor *cursor;
   GError *error;
@@ -107,16 +99,14 @@ photos_single_item_job_query_executed (GObject *source_object, GAsyncResult *res
   cursor = tracker_sparql_connection_query_finish (connection, res, &error);
   if (error != NULL)
     {
-      g_warning ("Unable to query single item: %s", error->message);
-      g_error_free (error);
-      photos_single_item_job_emit_callback (self);
+      g_task_return_error (task, error);
       return;
     }
 
   tracker_sparql_cursor_next_async (cursor,
-                                    NULL,
+                                    g_task_get_cancellable (task),
                                     photos_single_item_job_cursor_next,
-                                    g_object_ref (self));
+                                    g_object_ref (task));
   g_object_unref (cursor);
 }
 
@@ -126,7 +116,7 @@ photos_single_item_job_dispose (GObject *object)
 {
   PhotosSingleItemJob *self = PHOTOS_SINGLE_ITEM_JOB (object);
 
-  g_clear_object (&self->cursor);
+  g_clear_error (&self->queue_error);
   g_clear_object (&self->queue);
 
   G_OBJECT_CLASS (photos_single_item_job_parent_class)->dispose (object);
@@ -165,7 +155,7 @@ photos_single_item_job_set_property (GObject *object, guint prop_id, const GValu
 static void
 photos_single_item_job_init (PhotosSingleItemJob *self)
 {
-  self->queue = photos_tracker_queue_dup_singleton (NULL, NULL);
+  self->queue = photos_tracker_queue_dup_singleton (NULL, &self->queue_error);
 }
 
 
@@ -195,31 +185,48 @@ photos_single_item_job_new (const gchar *urn)
 }
 
 
+TrackerSparqlCursor *
+photos_single_item_job_finish (PhotosSingleItemJob *self, GAsyncResult *res, GError **error)
+{
+  GTask *task = G_TASK (res);
+
+  g_return_val_if_fail (g_task_is_valid (res, self), NULL);
+  g_return_val_if_fail (g_task_get_source_tag (task) == photos_single_item_job_run, NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+  return g_task_propagate_pointer (task, error);
+}
+
+
 void
 photos_single_item_job_run (PhotosSingleItemJob *self,
                             PhotosSearchContextState *state,
                             gint flags,
-                            PhotosSingleItemJobCallback callback,
+                            GCancellable *cancellable,
+                            GAsyncReadyCallback callback,
                             gpointer user_data)
 {
+  GTask *task;
   PhotosQuery *query;
 
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_source_tag (task, photos_single_item_job_run);
+
   if (G_UNLIKELY (self->queue == NULL))
     {
-      if (callback != NULL)
-        (*callback) (NULL, user_data);
-      return;
+      g_task_return_error (task, g_error_copy (self->queue_error));
+      goto out;
     }
 
-  self->callback = callback;
-  self->user_data = user_data;
-
   query = photos_query_builder_single_query (state, flags, self->urn);
   photos_tracker_queue_select (self->queue,
                                query->sparql,
-                               NULL,
+                               cancellable,
                                photos_single_item_job_query_executed,
-                               g_object_ref (self),
+                               g_object_ref (task),
                                g_object_unref);
   photos_query_free (query);
+
+ out:
+  g_object_unref (task);
 }
diff --git a/src/photos-single-item-job.h b/src/photos-single-item-job.h
index 3c3e6a8..b31c1a2 100644
--- a/src/photos-single-item-job.h
+++ b/src/photos-single-item-job.h
@@ -51,10 +51,15 @@ GType                  photos_single_item_job_get_type             (void) G_GNUC
 
 PhotosSingleItemJob   *photos_single_item_job_new                  (const gchar *urn);
 
+TrackerSparqlCursor   *photos_single_item_job_finish               (PhotosSingleItemJob *self,
+                                                                    GAsyncResult *res,
+                                                                    GError **error);
+
 void                   photos_single_item_job_run                  (PhotosSingleItemJob *self,
                                                                     PhotosSearchContextState *state,
                                                                     gint flags,
-                                                                    PhotosSingleItemJobCallback callback,
+                                                                    GCancellable *cancellable,
+                                                                    GAsyncReadyCallback callback,
                                                                     gpointer user_data);
 
 G_END_DECLS


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