[gthumb: 7/14] use the GAsyncResult interface to implement _g_query_metadata_async



commit 6d577d1a29664dc1e0b0724863347e09e6480b8d
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Thu Jan 27 23:53:26 2011 +0100

    use the GAsyncResult interface to implement _g_query_metadata_async

 extensions/image_print/gth-image-print-job.c      |    8 +-
 extensions/image_print/gth-load-image-info-task.c |    8 +-
 extensions/list_tools/gth-script-task.c           |    8 +-
 gthumb/gio-utils.c                                |   11 +-
 gthumb/gth-browser.c                              |    9 +-
 gthumb/gth-metadata-provider.c                    |  195 ++++++++-------------
 gthumb/gth-metadata-provider.h                    |    6 +-
 7 files changed, 108 insertions(+), 137 deletions(-)
---
diff --git a/extensions/image_print/gth-image-print-job.c b/extensions/image_print/gth-image-print-job.c
index 0fc0ad2..2f74c32 100644
--- a/extensions/image_print/gth-image-print-job.c
+++ b/extensions/image_print/gth-image-print-job.c
@@ -1008,12 +1008,14 @@ prev_page_button_clicked_cb (GtkWidget *widget,
 
 
 static void
-metadata_ready_cb (GList    *files,
-		   GError   *error,
-		   gpointer  user_data)
+metadata_ready_cb (GObject      *source_object,
+                   GAsyncResult *result,
+                   gpointer      user_data)
 {
 	GthImagePrintJob *self = user_data;
+	GError           *error;
 
+	_g_query_metadata_finish (result, &error);
 	gth_image_print_job_update_preview (self);
 }
 
diff --git a/extensions/image_print/gth-load-image-info-task.c b/extensions/image_print/gth-load-image-info-task.c
index 80518d3..c9e3dbf 100644
--- a/extensions/image_print/gth-load-image-info-task.c
+++ b/extensions/image_print/gth-load-image-info-task.c
@@ -65,12 +65,14 @@ load_next_image (GthLoadImageInfoTask *self)
 
 
 static void
-metadata_ready_cb (GList    *files,
-		   GError   *error,
-		   gpointer  user_data)
+metadata_ready_cb (GObject      *source_object,
+                   GAsyncResult *result,
+                   gpointer      user_data)
 {
 	GthLoadImageInfoTask *self = user_data;
+	GError               *error = NULL;
 
+	_g_query_metadata_finish (result, &error);
 	if (error != NULL) {
 		gth_task_completed (GTH_TASK (self), error);
 		return;
diff --git a/extensions/list_tools/gth-script-task.c b/extensions/list_tools/gth-script-task.c
index ee1bae6..4175323 100644
--- a/extensions/list_tools/gth-script-task.c
+++ b/extensions/list_tools/gth-script-task.c
@@ -196,12 +196,14 @@ _gth_script_task_exec (GthScriptTask *self)
 
 
 static void
-file_info_ready_cb (GList    *files,
-		    GError   *error,
-		    gpointer  user_data)
+file_info_ready_cb (GObject      *source_object,
+                    GAsyncResult *result,
+                    gpointer      user_data)
 {
 	GthScriptTask *self = user_data;
+	GError        *error = NULL;
 
+	_g_query_metadata_finish (result, &error);
 	if (error != NULL) {
 		gth_task_completed (GTH_TASK (self), error);
 		return;
diff --git a/gthumb/gio-utils.c b/gthumb/gio-utils.c
index 2862695..667a92c 100644
--- a/gthumb/gio-utils.c
+++ b/gthumb/gio-utils.c
@@ -374,13 +374,16 @@ for_each_child_compute_child (ForEachChildData *fec,
 
 
 static void
-for_each_child_metadata_ready_func (GList    *files,
-				    GError   *error,
-				    gpointer  user_data)
+for_each_child_metadata_ready_func (GObject      *source_object,
+                		    GAsyncResult *result,
+                		    gpointer      user_data)
 {
 	ForEachChildData *fec = user_data;
+	GList            *files;
+	GError           *error = NULL;
 
-	if (error == NULL) {
+	files = _g_query_metadata_finish (result, &error);
+	if (files != NULL) {
 		GthFileData *child_data = files->data;
 		for_each_child_compute_child (fec, child_data->file, child_data->info);
 	}
diff --git a/gthumb/gth-browser.c b/gthumb/gth-browser.c
index e5d345b..892793a 100644
--- a/gthumb/gth-browser.c
+++ b/gthumb/gth-browser.c
@@ -1566,12 +1566,15 @@ load_data_continue (LoadData *load_data,
 
 
 static void
-metadata_ready_cb (GList    *files,
-		   GError   *error,
-		   gpointer  user_data)
+metadata_ready_cb (GObject      *source_object,
+                   GAsyncResult *result,
+                   gpointer      user_data)
 {
 	LoadData *load_data = user_data;
+	GList    *files;
+	GError   *error = NULL;
 
+	files = _g_query_metadata_finish (result, &error);
 	if (error != NULL) {
 		load_data_error (load_data, error);
 		load_data_free (load_data);
diff --git a/gthumb/gth-metadata-provider.c b/gthumb/gth-metadata-provider.c
index c4083c9..c431e67 100644
--- a/gthumb/gth-metadata-provider.c
+++ b/gthumb/gth-metadata-provider.c
@@ -145,160 +145,110 @@ gth_metadata_provider_write (GthMetadataProvider   *self,
 
 
 typedef struct {
-	GList        *files;
-	char         *attributes;
-	char        **attributes_v;
-	GMutex       *mutex;
-	gboolean      thread_done;
-	GError       *error;
-} QueryMetadataThreadData;
-
-
-typedef struct {
-	GCancellable            *cancellable;
-	gulong                   cancel_signal;
-	InfoReadyCallback        ready_func;
-	gpointer                 user_data;
-	guint                    check_id;
-	GThread                 *thread;
-	QueryMetadataThreadData *rmtd;
+	GList   *files;
+	char   *attributes;
+	char  **attributes_v;
 } QueryMetadataData;
 
 
 static void
-query_metadata_done (QueryMetadataData *rmd)
+query_metadata_data_free (gpointer user_data)
 {
-	QueryMetadataThreadData *rmtd = rmd->rmtd;
-
-	if (rmd->ready_func != NULL)
-		(*rmd->ready_func) (rmtd->files, rmtd->error, rmd->user_data);
-
-	g_mutex_free (rmtd->mutex);
-	g_strfreev (rmtd->attributes_v);
-	g_free (rmtd->attributes);
-	_g_object_list_unref (rmtd->files);
-	g_free (rmtd);
-	if (rmd->cancel_signal != 0)
-		g_cancellable_disconnect (rmd->cancellable, rmd->cancel_signal);
-	_g_object_unref (rmd->cancellable);
-	g_free (rmd);
+	QueryMetadataData *qmd = user_data;
+
+	g_strfreev (qmd->attributes_v);
+	g_free (qmd->attributes);
+	_g_object_list_unref (qmd->files);
+	g_free (qmd);
 }
 
 
-static gpointer
-read_metadata_thread (gpointer data)
+static void
+_g_query_metadata_async_thread (GSimpleAsyncResult *result,
+				GObject            *object,
+				GCancellable       *cancellable)
 {
-	QueryMetadataThreadData *rmtd = data;
-	GList                   *providers;
-	GList                   *scan;
-	gboolean                 cancelled = FALSE;
+	QueryMetadataData  *qmd;
+	GList              *providers;
+	GList              *scan;
+	GError             *error = NULL;
+
+	qmd = g_simple_async_result_get_op_res_gpointer (result);
 
 	providers = NULL;
 	for (scan = gth_main_get_all_metadata_providers (); scan; scan = scan->next)
 		providers = g_list_prepend (providers, g_object_new (G_OBJECT_TYPE (scan->data), NULL));
 	providers = g_list_reverse (providers);
 
-	for (scan = rmtd->files; scan; scan = scan->next) {
+	for (scan = qmd->files; scan; scan = scan->next) {
 		GthFileData *file_data = scan->data;
 		GList       *scan_providers;
 
-		g_mutex_lock (rmtd->mutex);
-		cancelled = rmtd->thread_done;
-		g_mutex_unlock (rmtd->mutex);
-
-		if (cancelled)
+		if (g_cancellable_is_cancelled (cancellable)) {
+			error = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_CANCELLED, "");
 			break;
+		}
 
 		for (scan_providers = providers; scan_providers; scan_providers = scan_providers->next) {
 			GthMetadataProvider *metadata_provider = scan_providers->data;
 
-			if (gth_metadata_provider_can_read (metadata_provider, gth_file_data_get_mime_type (file_data), rmtd->attributes_v))
-				gth_metadata_provider_read (metadata_provider, file_data, rmtd->attributes);
+			if (gth_metadata_provider_can_read (metadata_provider, gth_file_data_get_mime_type (file_data), qmd->attributes_v))
+				gth_metadata_provider_read (metadata_provider, file_data, qmd->attributes);
 		}
 	}
 
 	_g_object_list_unref (providers);
 
-	if (! cancelled)
-		for (scan = rmtd->files; scan; scan = scan->next) {
-			GthFileData *file_data = scan->data;
-			gth_hook_invoke ("read-metadata-ready", file_data, rmtd->attributes);
-		}
-
-	g_mutex_lock (rmtd->mutex);
-	rmtd->thread_done = TRUE;
-	g_mutex_unlock (rmtd->mutex);
-
-	return rmtd;
+	if (error != NULL) {
+		g_simple_async_result_set_from_error (result, error);
+		g_error_free (error);
+	}
 }
 
 
-static gboolean
-check_read_metadata_thread (gpointer data)
+void
+_g_query_metadata_async (GList               *files,       /* GthFileData * list */
+			 const char          *attributes,
+			 GCancellable        *cancellable,
+			 GAsyncReadyCallback  callback,
+			 gpointer             user_data)
 {
-	QueryMetadataData       *rmd = data;
-	QueryMetadataThreadData *rmtd = rmd->rmtd;
-	gboolean                 thread_done;
-
-	g_source_remove (rmd->check_id);
-	rmd->check_id = 0;
-
-	g_mutex_lock (rmtd->mutex);
-	thread_done = rmtd->thread_done;
-	g_mutex_unlock (rmtd->mutex);
-
-	if (thread_done)
-		query_metadata_done (rmd);
-	else
-		rmd->check_id = g_timeout_add (CHECK_THREAD_RATE, check_read_metadata_thread, rmd);
-
-	return FALSE;
+	GSimpleAsyncResult *result;
+	QueryMetadataData  *qmd;
+
+	result = g_simple_async_result_new (NULL, callback, user_data, _g_query_metadata_async);
+
+	qmd = g_new0 (QueryMetadataData, 1);
+	qmd->files = _g_object_list_ref (files);
+	qmd->attributes = g_strdup (attributes);
+	qmd->attributes_v = gth_main_get_metadata_attributes (attributes);
+	g_simple_async_result_set_op_res_gpointer (result, qmd, query_metadata_data_free);
+	g_simple_async_result_run_in_thread (result,
+					     _g_query_metadata_async_thread,
+					     G_PRIORITY_DEFAULT,
+					     cancellable);
+
+	g_object_unref (result);
 }
 
 
-static void
-query_metadata_cancelled_cb (GCancellable *cancellable,
-                             gpointer      user_data)
+GList *
+_g_query_metadata_finish (GAsyncResult  *result,
+			  GError       **error)
 {
-	QueryMetadataData       *rmd = user_data;
-	QueryMetadataThreadData *rmtd = rmd->rmtd;
+	  GSimpleAsyncResult *simple;
+	  QueryMetadataData  *qmd;
 
-	g_mutex_lock (rmtd->mutex);
-	rmtd->thread_done = TRUE;
-	rmtd->error = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_CANCELLED, "");
-	g_mutex_unlock (rmtd->mutex);
-}
+	  g_return_val_if_fail (g_simple_async_result_is_valid (result, NULL, _g_query_metadata_async), NULL);
 
+	  simple = G_SIMPLE_ASYNC_RESULT (result);
 
-void
-_g_query_metadata_async (GList             *files,       /* GthFileData * list */
-			 const char        *attributes,
-			 GCancellable      *cancellable,
-			 InfoReadyCallback  ready_func,
-			 gpointer           user_data)
-{
-	QueryMetadataData       *rmd;
-	QueryMetadataThreadData *rmtd;
-
-	rmtd = g_new0 (QueryMetadataThreadData, 1);
-	rmtd->files = _g_object_list_ref (files);
-	rmtd->attributes = g_strdup (attributes);
-	rmtd->attributes_v = gth_main_get_metadata_attributes (attributes);
-	rmtd->mutex = g_mutex_new ();
-
-	rmd = g_new0 (QueryMetadataData, 1);
-	rmd->cancellable = _g_object_ref (cancellable);
-	if (rmd->cancellable != NULL)
-		rmd->cancel_signal = g_cancellable_connect (rmd->cancellable,
-							    G_CALLBACK (query_metadata_cancelled_cb),
-							    rmd,
-							    NULL);
-
-	rmd->ready_func = ready_func;
-	rmd->user_data = user_data;
-	rmd->rmtd = rmtd;
-	rmd->thread = g_thread_create (read_metadata_thread, rmtd, FALSE, NULL);
-	rmd->check_id = g_timeout_add (CHECK_THREAD_RATE, check_read_metadata_thread, rmd);
+	  if (g_simple_async_result_propagate_error (simple, error))
+		  return NULL;
+
+	  qmd = g_simple_async_result_get_op_res_gpointer (simple);
+
+	  return qmd->files;
 }
 
 
@@ -461,13 +411,20 @@ query_all_metadata_free (QueryAllMetadata *qam)
 
 
 static void
-qam_metadata_ready_cb (GList    *files,
-		       GError   *error,
-		       gpointer  user_data)
+qam_metadata_ready_cb (GObject      *source_object,
+                       GAsyncResult *result,
+                       gpointer      user_data)
 {
 	QueryAllMetadata *qam = user_data;
+	GList            *files;
+	GError           *error = NULL;
+
+	files = _g_query_metadata_finish (result, &error);
+	if (files != NULL)
+		qam->ready_func (files, NULL, qam->user_data);
+	else
+		qam->ready_func (NULL, error, qam->user_data);
 
-	qam->ready_func (qam->files, error, qam->user_data);
 	query_all_metadata_free (qam);
 }
 
diff --git a/gthumb/gth-metadata-provider.h b/gthumb/gth-metadata-provider.h
index 64715cf..51ef646 100644
--- a/gthumb/gth-metadata-provider.h
+++ b/gthumb/gth-metadata-provider.h
@@ -79,11 +79,13 @@ void       gth_metadata_provider_write      (GthMetadataProvider    *self,
 					     GthMetadataWriteFlags   flags,
 					     GthFileData            *file_data,
 					     const char             *attributes);
-void       _g_query_metadata_async          (GList                  *files, /* GthFileData * list */
+void       _g_query_metadata_async          (GList                  *files,       /* GthFileData * list */
 					     const char             *attributes,
 					     GCancellable           *cancellable,
-					     InfoReadyCallback       ready_func,
+					     GAsyncReadyCallback     callback,
 					     gpointer                user_data);
+GList *    _g_query_metadata_finish         (GAsyncResult           *result,
+		  	  	  	     GError                **error);
 void       _g_write_metadata_async          (GList                  *files, /* GthFileData * list */
 					     GthMetadataWriteFlags   flags,
 					     const char             *attributes,



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