[gnome-photos] fetch-ids-job: Convert to async API



commit 3ab8a737358b66d3e29afaf40b4f70a4eebe3d82
Author: Rafael Fonseca <r4f4rfs gmail com>
Date:   Wed Apr 20 15:41:37 2016 +0200

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

 src/photos-fetch-ids-job.c   |   90 ++++++++++++++++++++++++------------------
 src/photos-fetch-ids-job.h   |    6 ++-
 src/photos-search-provider.c |   14 ++++++-
 3 files changed, 69 insertions(+), 41 deletions(-)
---
diff --git a/src/photos-fetch-ids-job.c b/src/photos-fetch-ids-job.c
index 0bfbbf8..0e75584 100644
--- a/src/photos-fetch-ids-job.c
+++ b/src/photos-fetch-ids-job.c
@@ -38,12 +38,10 @@
 struct _PhotosFetchIdsJob
 {
   GObject parent_instance;
-  GCancellable *cancellable;
+  GError *queue_error;
   GPtrArray *ids;
-  PhotosFetchIdsJobCallback callback;
   PhotosTrackerQueue *queue;
   gchar **terms;
-  gpointer user_data;
 };
 
 struct _PhotosFetchIdsJobClass
@@ -62,30 +60,23 @@ G_DEFINE_TYPE (PhotosFetchIdsJob, photos_fetch_ids_job, G_TYPE_OBJECT);
 
 
 static void
-photos_fetch_ids_job_emit_callback (PhotosFetchIdsJob *self)
-{
-  if (self->callback == NULL)
-    return;
-
-  g_ptr_array_add (self->ids, NULL);
-  (*self->callback) ((const gchar *const *) self->ids->pdata, self->user_data);
-}
-
-
-static void
 photos_fetch_ids_job_cursor_next (GObject *source_object, GAsyncResult *res, gpointer user_data)
 {
-  PhotosFetchIdsJob *self = PHOTOS_FETCH_IDS_JOB (user_data);
+  PhotosFetchIdsJob *self;
+  GCancellable *cancellable;
+  GTask *task = G_TASK (user_data);
   TrackerSparqlCursor *cursor = TRACKER_SPARQL_CURSOR (source_object);
   GError *error;
   gboolean success;
 
+  self = PHOTOS_FETCH_IDS_JOB (g_task_get_source_object (task));
+  cancellable = g_task_get_cancellable (task);
+
   error = NULL;
   success = tracker_sparql_cursor_next_finish (cursor, res, &error);
   if (error != NULL)
     {
-      g_warning ("Unable to read results of FetchIdsJob: %s", error->message);
-      g_error_free (error);
+      g_task_return_error (task, error);
       goto end;
     }
 
@@ -96,38 +87,47 @@ photos_fetch_ids_job_cursor_next (GObject *source_object, GAsyncResult *res, gpo
       id = tracker_sparql_cursor_get_string (cursor, PHOTOS_QUERY_COLUMNS_URN, NULL);
       g_ptr_array_add (self->ids, g_strdup (id));
 
-      tracker_sparql_cursor_next_async (cursor, self->cancellable, photos_fetch_ids_job_cursor_next, self);
+      tracker_sparql_cursor_next_async (cursor,
+                                        cancellable,
+                                        photos_fetch_ids_job_cursor_next,
+                                        g_object_ref (task));
+      g_object_unref (task);
       return;
     }
 
+  g_ptr_array_add (self->ids, NULL);
+  g_task_return_pointer (task, self->ids->pdata, NULL);
+
  end:
-  photos_fetch_ids_job_emit_callback (self);
-  g_object_unref (self);
+  g_object_unref (task);
 }
 
 
 static void
 photos_fetch_ids_job_query_executed (GObject *source_object, GAsyncResult *res, gpointer user_data)
 {
-  PhotosFetchIdsJob *self = PHOTOS_FETCH_IDS_JOB (user_data);
+  PhotosFetchIdsJob *self;
+  GCancellable *cancellable;
+  GTask *task = G_TASK (user_data);
   TrackerSparqlConnection *connection = TRACKER_SPARQL_CONNECTION (source_object);
   TrackerSparqlCursor *cursor;
   GError *error;
 
+  self = PHOTOS_FETCH_IDS_JOB (g_task_get_source_object (task));
+  cancellable = g_task_get_cancellable (task);
+
   error = NULL;
   cursor = tracker_sparql_connection_query_finish (connection, res, &error);
   if (error != NULL)
     {
-      g_warning ("Unable to run FetchIdsJob: %s", error->message);
-      g_error_free (error);
-      photos_fetch_ids_job_emit_callback (self);
+      g_task_return_error (task, error);
       return;
     }
 
   tracker_sparql_cursor_next_async (cursor,
-                                    self->cancellable,
+                                    cancellable,
                                     photos_fetch_ids_job_cursor_next,
-                                    g_object_ref (self));
+                                    g_object_ref (task));
   g_object_unref (cursor);
 }
 
@@ -138,7 +138,6 @@ photos_fetch_ids_job_dispose (GObject *object)
   PhotosFetchIdsJob *self = PHOTOS_FETCH_IDS_JOB (object);
 
   g_clear_pointer (&self->ids, (GDestroyNotify) g_ptr_array_unref);
-  g_clear_object (&self->cancellable);
   g_clear_object (&self->queue);
 
   G_OBJECT_CLASS (photos_fetch_ids_job_parent_class)->dispose (object);
@@ -178,7 +177,7 @@ static void
 photos_fetch_ids_job_init (PhotosFetchIdsJob *self)
 {
   self->ids = g_ptr_array_new_with_free_func (g_free);
-  self->queue = photos_tracker_queue_dup_singleton (NULL, NULL);
+  self->queue = photos_tracker_queue_dup_singleton (NULL, &self->queue_error);
 }
 
 
@@ -208,29 +207,39 @@ photos_fetch_ids_job_new (const gchar *const *terms)
 }
 
 
+const gchar *const *
+photos_fetch_ids_job_finish (PhotosFetchIdsJob *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_fetch_ids_job_run, NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+  return g_task_propagate_pointer (task, error);
+}
+
+
 void
 photos_fetch_ids_job_run (PhotosFetchIdsJob *self,
                           PhotosSearchContextState *state,
                           GCancellable *cancellable,
-                          PhotosFetchIdsJobCallback callback,
+                          GAsyncReadyCallback callback,
                           gpointer user_data)
 {
   PhotosQuery *query;
   gchar *str;
+  GTask *task;
 
-  self->callback = callback;
-  self->user_data = user_data;
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_source_tag (task, photos_fetch_ids_job_run);
 
   if (G_UNLIKELY (self->queue == NULL))
     {
-      photos_fetch_ids_job_emit_callback (self);
-      return;
+      g_task_return_error (task, g_error_copy (self->queue_error));
+      goto out;
     }
 
-  g_clear_object (&self->cancellable);
-  if (cancellable != NULL)
-    self->cancellable = g_object_ref (cancellable);
-
   str = g_strjoinv (" ", (gchar **) self->terms);
   photos_search_controller_set_string (state->srch_cntrlr, str);
   g_free (str);
@@ -238,9 +247,12 @@ photos_fetch_ids_job_run (PhotosFetchIdsJob *self,
   query = photos_query_builder_global_query (state, PHOTOS_QUERY_FLAGS_SEARCH, NULL);
   photos_tracker_queue_select (self->queue,
                                query->sparql,
-                               self->cancellable,
+                               cancellable,
                                photos_fetch_ids_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-fetch-ids-job.h b/src/photos-fetch-ids-job.h
index 249063f..f905098 100644
--- a/src/photos-fetch-ids-job.h
+++ b/src/photos-fetch-ids-job.h
@@ -50,10 +50,14 @@ GType                photos_fetch_ids_job_get_type          (void) G_GNUC_CONST;
 
 PhotosFetchIdsJob   *photos_fetch_ids_job_new               (const gchar *const *terms);
 
+const gchar *const  *photos_fetch_ids_job_finish            (PhotosFetchIdsJob *self,
+                                                             GAsyncResult *res,
+                                                             GError **error);
+
 void                 photos_fetch_ids_job_run               (PhotosFetchIdsJob *self,
                                                              PhotosSearchContextState *state,
                                                              GCancellable *cancellable,
-                                                             PhotosFetchIdsJobCallback callback,
+                                                             GAsyncReadyCallback callback,
                                                              gpointer user_data);
 
 G_END_DECLS
diff --git a/src/photos-search-provider.c b/src/photos-search-provider.c
index b260e34..ef9e367 100644
--- a/src/photos-search-provider.c
+++ b/src/photos-search-provider.c
@@ -79,17 +79,29 @@ photos_search_provider_activate_result (PhotosSearchProvider *self,
 
 
 static void
-photos_search_provider_fetch_ids_executed (const gchar *const *ids, gpointer user_data)
+photos_search_provider_fetch_ids_executed (GObject *source_object, GAsyncResult *res, gpointer user_data)
 {
   GApplication *app;
   GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (user_data);
+  PhotosFetchIdsJob *job = PHOTOS_FETCH_IDS_JOB (source_object);
+  GError *error = NULL;
   GVariant *parameters;
+  const gchar *const *ids;
 
   app = g_application_get_default ();
   g_application_release (app);
 
+  ids = photos_fetch_ids_job_finish (job, res, &error);
+  if (error != NULL)
+    {
+      g_dbus_method_invocation_take_error (invocation, error);
+      goto out;
+    }
+
   parameters = g_variant_new ("(^as)", ids);
   g_dbus_method_invocation_return_value (invocation, parameters);
+
+ out:
   g_object_unref (invocation);
 }
 


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