[gnome-photos] update-mtime-job: Convert to Async API



commit b99234eb8aecc3ec3b3ab01defaed925ee1e5efe
Author: Rafael Fonseca <r4f4rfs gmail com>
Date:   Thu Mar 24 19:05:16 2016 +0100

    update-mtime-job: Convert to Async API
    
    https://bugzilla.gnome.org/show_bug.cgi?id=764086

 src/photos-set-collection-job.c |   13 ++++++++-
 src/photos-update-mtime-job.c   |   52 ++++++++++++++++++++++++--------------
 src/photos-update-mtime-job.h   |   10 +++++--
 3 files changed, 51 insertions(+), 24 deletions(-)
---
diff --git a/src/photos-set-collection-job.c b/src/photos-set-collection-job.c
index 46efbcf..48da860 100644
--- a/src/photos-set-collection-job.c
+++ b/src/photos-set-collection-job.c
@@ -67,9 +67,18 @@ G_DEFINE_TYPE (PhotosSetCollectionJob, photos_set_collection_job, G_TYPE_OBJECT)
 
 
 static void
-photos_set_collection_job_update_mtime (gpointer user_data)
+photos_set_collection_job_update_mtime (GObject *source_object, GAsyncResult *res, gpointer user_data)
 {
+  PhotosUpdateMtimeJob *job = PHOTOS_UPDATE_MTIME_JOB (source_object);
   PhotosSetCollectionJob *self = PHOTOS_SET_COLLECTION_JOB (user_data);
+  GError *error = NULL;
+
+  photos_update_mtime_job_finish (job, res, &error);
+  if (error != NULL)
+    {
+      g_warning ("Unable to update mtime: %s", error->message);
+      g_error_free (error);
+    }
 
   if (self->callback != NULL)
     (*self->callback) (self->user_data);
@@ -87,7 +96,7 @@ photos_set_collection_job_job_collector (PhotosSetCollectionJob *self)
       PhotosUpdateMtimeJob *job;
 
       job = photos_update_mtime_job_new (self->collection_urn);
-      photos_update_mtime_job_run (job, photos_set_collection_job_update_mtime, g_object_ref (self));
+      photos_update_mtime_job_run (job, NULL, photos_set_collection_job_update_mtime, g_object_ref (self));
       g_object_unref (job);
     }
 }
diff --git a/src/photos-update-mtime-job.c b/src/photos-update-mtime-job.c
index 83d3b04..228050a 100644
--- a/src/photos-update-mtime-job.c
+++ b/src/photos-update-mtime-job.c
@@ -39,10 +39,9 @@
 struct _PhotosUpdateMtimeJob
 {
   GObject parent_instance;
-  PhotosUpdateMtimeJobCallback callback;
+  GError *queue_error;
   PhotosTrackerQueue *queue;
   gchar *urn;
-  gpointer user_data;
 };
 
 struct _PhotosUpdateMtimeJobClass
@@ -63,7 +62,7 @@ G_DEFINE_TYPE (PhotosUpdateMtimeJob, photos_update_mtime_job, G_TYPE_OBJECT);
 static void
 photos_update_mtime_job_query_executed (GObject *source_object, GAsyncResult *res, gpointer user_data)
 {
-  PhotosUpdateMtimeJob *self = PHOTOS_UPDATE_MTIME_JOB (user_data);
+  GTask *task = G_TASK (user_data);
   TrackerSparqlConnection *connection = TRACKER_SPARQL_CONNECTION (source_object);
   GError *error;
 
@@ -71,14 +70,11 @@ photos_update_mtime_job_query_executed (GObject *source_object, GAsyncResult *re
   tracker_sparql_connection_update_finish (connection, res, &error);
   if (error != NULL)
     {
-      g_warning ("Unable to update mtime: %s", error->message);
-      g_error_free (error);
-      goto out;
+      g_task_return_error (task, error);
+      return;
     }
 
- out:
-  if (self->callback != NULL)
-    (*self->callback) (self->user_data);
+  g_task_return_boolean (task, TRUE);
 }
 
 
@@ -98,6 +94,7 @@ photos_update_mtime_job_finalize (GObject *object)
 {
   PhotosUpdateMtimeJob *self = PHOTOS_UPDATE_MTIME_JOB (object);
 
+  g_clear_error (&self->queue_error);
   g_free (self->urn);
 
   G_OBJECT_CLASS (photos_update_mtime_job_parent_class)->finalize (object);
@@ -125,7 +122,7 @@ photos_update_mtime_job_set_property (GObject *object, guint prop_id, const GVal
 static void
 photos_update_mtime_job_init (PhotosUpdateMtimeJob *self)
 {
-  self->queue = photos_tracker_queue_dup_singleton (NULL, NULL);
+  self->queue = photos_tracker_queue_dup_singleton (NULL, &self->queue_error);
 }
 
 
@@ -155,34 +152,51 @@ photos_update_mtime_job_new (const gchar *urn)
 }
 
 
+gboolean
+photos_update_mtime_job_finish (PhotosUpdateMtimeJob *self, GAsyncResult *res, GError **error)
+{
+  GTask *task = G_TASK (res);
+
+  g_return_val_if_fail (g_task_is_valid (res, self), FALSE);
+  g_return_val_if_fail (g_task_get_source_tag (task) == photos_update_mtime_job_run, FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  return g_task_propagate_boolean (task, error);
+}
+
+
 void
 photos_update_mtime_job_run (PhotosUpdateMtimeJob *self,
-                             PhotosUpdateMtimeJobCallback callback,
+                             GCancellable *cancellable,
+                             GAsyncReadyCallback callback,
                              gpointer user_data)
 {
   GApplication *app;
+  GTask *task;
   PhotosQuery *query;
   PhotosSearchContextState *state;
 
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_source_tag (task, photos_update_mtime_job_run);
+
   if (G_UNLIKELY (self->queue == NULL))
     {
-      if (callback != NULL)
-        (*callback) (user_data);
-      return;
+      g_task_return_error (task, g_error_copy (self->queue_error));
+      goto out;
     }
 
-  self->callback = callback;
-  self->user_data = user_data;
-
   app = g_application_get_default ();
   state = photos_search_context_get_state (PHOTOS_SEARCH_CONTEXT (app));
 
   query = photos_query_builder_update_mtime_query (state, self->urn);
   photos_tracker_queue_update (self->queue,
                                query->sparql,
-                               NULL,
+                               cancellable,
                                photos_update_mtime_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-update-mtime-job.h b/src/photos-update-mtime-job.h
index 049ca0e..718f826 100644
--- a/src/photos-update-mtime-job.h
+++ b/src/photos-update-mtime-job.h
@@ -39,8 +39,6 @@ G_BEGIN_DECLS
   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
    PHOTOS_TYPE_UPDATE_MTIME_JOB))
 
-typedef void (*PhotosUpdateMtimeJobCallback) (gpointer);
-
 typedef struct _PhotosUpdateMtimeJob      PhotosUpdateMtimeJob;
 typedef struct _PhotosUpdateMtimeJobClass PhotosUpdateMtimeJobClass;
 
@@ -49,9 +47,15 @@ GType                   photos_update_mtime_job_get_type             (void) G_GN
 PhotosUpdateMtimeJob   *photos_update_mtime_job_new                  (const gchar *urn);
 
 void                    photos_update_mtime_job_run                  (PhotosUpdateMtimeJob *self,
-                                                                      PhotosUpdateMtimeJobCallback callback,
+                                                                      GCancellable *cancellable,
+                                                                      GAsyncReadyCallback callback,
                                                                       gpointer user_data);
 
+gboolean                photos_update_mtime_job_finish               (PhotosUpdateMtimeJob *self,
+                                                                      GAsyncResult *res,
+                                                                      GError **error);
+
+
 G_END_DECLS
 
 #endif /* PHOTOS_UPDATE_MTIME_JOB_H */


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