[evolution] Fix some details in our asynchronous functions.



commit 0b98cf60cbe1350479e189ebcf54aeae775e6264
Author: Matthew Barnes <mbarnes redhat com>
Date:   Fri Nov 20 14:12:51 2009 -0500

    Fix some details in our asynchronous functions.
    
    Don't unref the GAsyncResult in finish functions.  Do it after calling
    g_simple_async_result_complete().  This allows the GAsyncReadyCallback
    to be optional, and we'll still clean up resources properly.
    
    Also, don't call g_simple_async_result_complete_in_idle() unless we're
    completing an operation from a separate thread, which we're not in any
    of the current cases.

 composer/e-composer-autosave.c    |   13 +-----
 widgets/misc/e-attachment-store.c |   82 ++++++++++++-------------------------
 widgets/misc/e-attachment.c       |   38 +++--------------
 3 files changed, 35 insertions(+), 98 deletions(-)
---
diff --git a/composer/e-composer-autosave.c b/composer/e-composer-autosave.c
index a4d5343..b0bc659 100644
--- a/composer/e-composer-autosave.c
+++ b/composer/e-composer-autosave.c
@@ -278,6 +278,7 @@ static void
 autosave_data_free (AutosaveData *data)
 {
 	g_object_unref (data->composer);
+	g_object_unref (data->simple);
 
 	if (data->input_stream != NULL)
 		g_object_unref (data->input_stream);
@@ -294,10 +295,7 @@ autosave_snapshot_check_for_error (AutosaveData *data,
 	if (error == NULL)
 		return FALSE;
 
-	/* Steal the result. */
 	simple = data->simple;
-	data->simple = NULL;
-
 	g_simple_async_result_set_from_error (simple, error);
 	g_simple_async_result_set_op_res_gboolean (simple, FALSE);
 	g_simple_async_result_complete (simple);
@@ -321,10 +319,7 @@ autosave_snapshot_splice_cb (GOutputStream *output_stream,
 	if (autosave_snapshot_check_for_error (data, error))
 		return;
 
-	/* Steal the result. */
 	simple = data->simple;
-	data->simple = NULL;
-
 	g_simple_async_result_set_op_res_gboolean (simple, TRUE);
 	g_simple_async_result_complete (simple);
 
@@ -353,11 +348,8 @@ autosave_snapshot_cb (GFile *file,
 	if (message == NULL) {
 		GSimpleAsyncResult *simple;
 
-		/* Steal the result. */
-		simple = data->simple;
-		data->simple = NULL;
-
 		/* FIXME Need to set a GError here. */
+		simple = data->simple;
 		g_simple_async_result_set_op_res_gboolean (simple, FALSE);
 		g_simple_async_result_complete (simple);
 		g_object_unref (output_stream);
@@ -463,7 +455,6 @@ e_composer_autosave_snapshot_finish (EMsgComposer *composer,
 	simple = G_SIMPLE_ASYNC_RESULT (result);
 	success = g_simple_async_result_get_op_res_gboolean (simple);
 	g_simple_async_result_propagate_error (simple, error);
-	g_object_unref (simple);
 
 	return success;
 }
diff --git a/widgets/misc/e-attachment-store.c b/widgets/misc/e-attachment-store.c
index d3db89d..6fd71f0 100644
--- a/widgets/misc/e-attachment-store.c
+++ b/widgets/misc/e-attachment-store.c
@@ -695,7 +695,7 @@ attachment_store_uri_context_new (EAttachmentStore *store,
 static void
 attachment_store_uri_context_free (UriContext *uri_context)
 {
-	/* Do not free the GSimpleAsyncResult. */
+	g_object_unref (uri_context->simple);
 
 	/* The attachment list should be empty now. */
 	g_warn_if_fail (uri_context->attachment_list == NULL);
@@ -755,11 +755,7 @@ attachment_store_get_uris_save_cb (EAttachment *attachment,
 	if (uri_context->attachment_list != NULL)
 		return;
 
-	/* Steal the result. */
-	simple = uri_context->simple;
-	uri_context->simple = NULL;
-
-	/* And the URI list. */
+	/* Steal the URI list. */
 	uris = uri_context->uris;
 	uri_context->uris = NULL;
 
@@ -767,6 +763,8 @@ attachment_store_get_uris_save_cb (EAttachment *attachment,
 	error = uri_context->error;
 	uri_context->error = NULL;
 
+	simple = uri_context->simple;
+
 	if (error == NULL)
 		g_simple_async_result_set_op_res_gpointer (simple, uris, NULL);
 	else {
@@ -792,7 +790,6 @@ e_attachment_store_get_uris_async (EAttachmentStore *store,
 	gchar *path;
 
 	g_return_if_fail (E_IS_ATTACHMENT_STORE (store));
-	g_return_if_fail (callback != NULL);
 
 	uri_context = attachment_store_uri_context_new (
 		store, attachment_list, callback, user_data);
@@ -832,16 +829,14 @@ e_attachment_store_get_uris_async (EAttachmentStore *store,
 		GSimpleAsyncResult *simple;
 		gchar **uris;
 
-		/* Steal the result. */
-		simple = uri_context->simple;
-		uri_context->simple = NULL;
-
-		/* And the URI list. */
+		/* Steal the URI list. */
 		uris = uri_context->uris;
 		uri_context->uris = NULL;
 
+		simple = uri_context->simple;
 		g_simple_async_result_set_op_res_gpointer (simple, uris, NULL);
-		g_simple_async_result_complete_in_idle (simple);
+		g_simple_async_result_complete (simple);
+
 		attachment_store_uri_context_free (uri_context);
 		return;
 	}
@@ -858,16 +853,13 @@ e_attachment_store_get_uris_async (EAttachmentStore *store,
 	if (path == NULL) {
 		GSimpleAsyncResult *simple;
 
-		/* Steal the result. */
 		simple = uri_context->simple;
-		uri_context->simple = NULL;
-
 		g_simple_async_result_set_error (
 			simple, G_FILE_ERROR,
 			g_file_error_from_errno (errno),
 			"%s", g_strerror (errno));
+		g_simple_async_result_complete (simple);
 
-		g_simple_async_result_complete_in_idle (simple);
 		attachment_store_uri_context_free (uri_context);
 		return;
 	}
@@ -899,7 +891,6 @@ e_attachment_store_get_uris_finish (EAttachmentStore *store,
 	simple = G_SIMPLE_ASYNC_RESULT (result);
 	uris = g_simple_async_result_get_op_res_gpointer (simple);
 	g_simple_async_result_propagate_error (simple, error);
-	g_object_unref (simple);
 
 	return uris;
 }
@@ -941,7 +932,7 @@ attachment_store_load_context_new (EAttachmentStore *store,
 static void
 attachment_store_load_context_free (LoadContext *load_context)
 {
-	/* Do not free the GSimpleAsyncResult. */
+	g_object_unref (load_context->simple);
 
 	/* The attachment list should be empty now. */
 	g_warn_if_fail (load_context->attachment_list == NULL);
@@ -991,14 +982,12 @@ attachment_store_load_ready_cb (EAttachment *attachment,
 	if (load_context->attachment_list != NULL)
 		return;
 
-	/* Steal the result. */
-	simple = load_context->simple;
-	load_context->simple = NULL;
-
-	/* And the error. */
+	/* Steal the error. */
 	error = load_context->error;
 	load_context->error = NULL;
 
+	simple = load_context->simple;
+
 	if (error == NULL)
 		g_simple_async_result_set_op_res_gboolean (simple, TRUE);
 	else {
@@ -1021,7 +1010,6 @@ e_attachment_store_load_async (EAttachmentStore *store,
 	GList *iter;
 
 	g_return_if_fail (E_IS_ATTACHMENT_STORE (store));
-	g_return_if_fail (callback != NULL);
 
 	load_context = attachment_store_load_context_new (
 		store, attachment_list, callback, user_data);
@@ -1029,12 +1017,10 @@ e_attachment_store_load_async (EAttachmentStore *store,
 	if (attachment_list == NULL) {
 		GSimpleAsyncResult *simple;
 
-		/* Steal the result. */
 		simple = load_context->simple;
-		load_context->simple = NULL;
-
 		g_simple_async_result_set_op_res_gboolean (simple, TRUE);
 		g_simple_async_result_complete (simple);
+
 		attachment_store_load_context_free (load_context);
 		return;
 	}
@@ -1065,7 +1051,6 @@ e_attachment_store_load_finish (EAttachmentStore *store,
 	simple = G_SIMPLE_ASYNC_RESULT (result);
 	success = g_simple_async_result_get_op_res_gboolean (simple);
 	g_simple_async_result_propagate_error (simple, error);
-	g_object_unref (simple);
 
 	return success;
 }
@@ -1119,7 +1104,7 @@ attachment_store_save_context_new (EAttachmentStore *store,
 static void
 attachment_store_save_context_free (SaveContext *save_context)
 {
-	/* Do not free the GSimpleAsyncResult. */
+	g_object_unref (save_context->simple);
 
 	/* The attachment list should be empty now. */
 	g_warn_if_fail (save_context->attachment_list == NULL);
@@ -1207,16 +1192,15 @@ attachment_store_save_cb (EAttachment *attachment,
 
 	/* If an error occurred while saving, we're done. */
 	if (save_context->error != NULL) {
-		/* Steal the result. */
-		simple = save_context->simple;
-		save_context->simple = NULL;
 
-		/* And the error. */
+		/* Steal the error. */
 		error = save_context->error;
 		save_context->error = NULL;
 
+		simple = save_context->simple;
 		g_simple_async_result_set_from_error (simple, error);
 		g_simple_async_result_complete (simple);
+
 		attachment_store_save_context_free (save_context);
 		g_error_free (error);
 		return;
@@ -1251,12 +1235,10 @@ attachment_store_save_cb (EAttachment *attachment,
 	if (error != NULL && !g_error_matches (
 		error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) {
 
-		/* Steal the result. */
 		simple = save_context->simple;
-		save_context->simple = NULL;
-
 		g_simple_async_result_set_from_error (simple, error);
 		g_simple_async_result_complete (simple);
+
 		attachment_store_save_context_free (save_context);
 		g_error_free (error);
 		return;
@@ -1272,25 +1254,20 @@ attachment_store_save_cb (EAttachment *attachment,
 		G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
 
 	if (error != NULL) {
-		/* Steal the result. */
 		simple = save_context->simple;
-		save_context->simple = NULL;
-
 		g_simple_async_result_set_from_error (simple, error);
 		g_simple_async_result_complete (simple);
+
 		attachment_store_save_context_free (save_context);
 		g_error_free (error);
 		return;
 	}
 
-	/* Steal the result. */
-	simple = save_context->simple;
-	save_context->simple = NULL;
-
 	/* And the URI list. */
 	uris = save_context->uris;
 	save_context->uris = NULL;
 
+	simple = save_context->simple;
 	g_simple_async_result_set_op_res_gpointer (simple, uris, NULL);
 	g_simple_async_result_complete (simple);
 
@@ -1311,7 +1288,6 @@ e_attachment_store_save_async (EAttachmentStore *store,
 
 	g_return_if_fail (E_IS_ATTACHMENT_STORE (store));
 	g_return_if_fail (G_IS_FILE (destination));
-	g_return_if_fail (callback != NULL);
 
 	save_context = attachment_store_save_context_new (
 		store, destination, callback, user_data);
@@ -1325,16 +1301,14 @@ e_attachment_store_save_async (EAttachmentStore *store,
 		GSimpleAsyncResult *simple;
 		gchar **uris;
 
-		/* Steal the result. */
-		simple = save_context->simple;
-		save_context->simple = NULL;
-
-		/* And the URI list. */
+		/* Steal the URI list. */
 		uris = save_context->uris;
 		save_context->uris = NULL;
 
+		simple = save_context->simple;
 		g_simple_async_result_set_op_res_gpointer (simple, uris, NULL);
-		g_simple_async_result_complete_in_idle (simple);
+		g_simple_async_result_complete (simple);
+
 		attachment_store_save_context_free (save_context);
 		return;
 	}
@@ -1351,16 +1325,13 @@ e_attachment_store_save_async (EAttachmentStore *store,
 	if (path == NULL) {
 		GSimpleAsyncResult *simple;
 
-		/* Steal the result. */
 		simple = save_context->simple;
-		save_context->simple = NULL;
-
 		g_simple_async_result_set_error (
 			simple, G_FILE_ERROR,
 			g_file_error_from_errno (errno),
 			"%s", g_strerror (errno));
+		g_simple_async_result_complete (simple);
 
-		g_simple_async_result_complete_in_idle (simple);
 		attachment_store_save_context_free (save_context);
 		return;
 	}
@@ -1390,7 +1361,6 @@ e_attachment_store_save_finish (EAttachmentStore *store,
 	simple = G_SIMPLE_ASYNC_RESULT (result);
 	uris = g_simple_async_result_get_op_res_gpointer (simple);
 	g_simple_async_result_propagate_error (simple, error);
-	g_object_unref (simple);
 
 	return uris;
 }
diff --git a/widgets/misc/e-attachment.c b/widgets/misc/e-attachment.c
index 1fec792..68e8bd5 100644
--- a/widgets/misc/e-attachment.c
+++ b/widgets/misc/e-attachment.c
@@ -1451,8 +1451,8 @@ attachment_load_context_new (EAttachment *attachment,
 static void
 attachment_load_context_free (LoadContext *load_context)
 {
-	/* Do not free the GSimpleAsyncResult. */
 	g_object_unref (load_context->attachment);
+	g_object_unref (load_context->simple);
 
 	if (load_context->input_stream != NULL)
 		g_object_unref (load_context->input_stream);
@@ -1475,10 +1475,7 @@ attachment_load_check_for_error (LoadContext *load_context,
 	if (error == NULL)
 		return FALSE;
 
-	/* Steal the result. */
 	simple = load_context->simple;
-	load_context->simple = NULL;
-
 	g_simple_async_result_set_from_error (simple, error);
 	g_simple_async_result_complete (simple);
 	g_error_free (error);
@@ -1507,9 +1504,7 @@ attachment_load_finish (LoadContext *load_context)
 	gpointer data;
 	gsize size;
 
-	/* Steal the result. */
 	simple = load_context->simple;
-	load_context->simple = NULL;
 
 	file_info = load_context->file_info;
 	attachment = load_context->attachment;
@@ -1789,15 +1784,13 @@ attachment_load_from_mime_part (LoadContext *load_context)
 
 	attachment_set_file_info (attachment, file_info);
 
-	/* Steal the result. */
-	simple = load_context->simple;
-	load_context->simple = NULL;
-
 	camel_object_ref (mime_part);
+
+	simple = load_context->simple;
 	g_simple_async_result_set_op_res_gpointer (
 		simple, mime_part,
 		(GDestroyNotify) camel_object_unref);
-	g_simple_async_result_complete_in_idle (simple);
+	g_simple_async_result_complete (simple);
 
 	attachment_load_context_free (load_context);
 }
@@ -1813,7 +1806,6 @@ e_attachment_load_async (EAttachment *attachment,
 	GFile *file;
 
 	g_return_if_fail (E_IS_ATTACHMENT (attachment));
-	g_return_if_fail (callback != NULL);
 
 	if (e_attachment_get_loading (attachment)) {
 		g_simple_async_report_error_in_idle (
@@ -1869,7 +1861,6 @@ e_attachment_load_finish (EAttachment *attachment,
 	if (mime_part != NULL)
 		e_attachment_set_mime_part (attachment, mime_part);
 	g_simple_async_result_propagate_error (simple, error);
-	g_object_unref (simple);
 
 	attachment_set_loading (attachment, FALSE);
 
@@ -1972,8 +1963,8 @@ attachment_open_context_new (EAttachment *attachment,
 static void
 attachment_open_context_free (OpenContext *open_context)
 {
-	/* Do not free the GSimpleAsyncResult. */
 	g_object_unref (open_context->attachment);
+	g_object_unref (open_context->simple);
 
 	if (open_context->app_info != NULL)
 		g_object_unref (open_context->app_info);
@@ -1990,10 +1981,7 @@ attachment_open_check_for_error (OpenContext *open_context,
 	if (error == NULL)
 		return FALSE;
 
-	/* Steal the result. */
 	simple = open_context->simple;
-	open_context->simple = NULL;
-
 	g_simple_async_result_set_from_error (simple, error);
 	g_simple_async_result_complete (simple);
 	g_error_free (error);
@@ -2012,9 +2000,7 @@ attachment_open_file (GFile *file,
 	gboolean success;
 	GError *error = NULL;
 
-	/* Steal the result. */
 	simple = open_context->simple;
-	open_context->simple = NULL;
 
 	context = gdk_app_launch_context_new ();
 
@@ -2115,7 +2101,6 @@ e_attachment_open_async (EAttachment *attachment,
 	GFile *file;
 
 	g_return_if_fail (E_IS_ATTACHMENT (attachment));
-	g_return_if_fail (callback != NULL);
 
 	file = e_attachment_get_file (attachment);
 	mime_part = e_attachment_get_mime_part (attachment);
@@ -2151,7 +2136,6 @@ e_attachment_open_finish (EAttachment *attachment,
 	simple = G_SIMPLE_ASYNC_RESULT (result);
 	success = g_simple_async_result_get_op_res_gboolean (simple);
 	g_simple_async_result_propagate_error (simple, error);
-	g_object_unref (simple);
 
 	return success;
 }
@@ -2254,8 +2238,8 @@ attachment_save_context_new (EAttachment *attachment,
 static void
 attachment_save_context_free (SaveContext *save_context)
 {
-	/* Do not free the GSimpleAsyncResult. */
 	g_object_unref (save_context->attachment);
+	g_object_unref (save_context->simple);
 
 	if (save_context->directory != NULL)
 		g_object_unref (save_context->directory);
@@ -2281,10 +2265,7 @@ attachment_save_check_for_error (SaveContext *save_context,
 	if (error == NULL)
 		return FALSE;
 
-	/* Steal the result. */
 	simple = save_context->simple;
-	save_context->simple = NULL;
-
 	g_simple_async_result_set_from_error (simple, error);
 	g_simple_async_result_complete (simple);
 	g_error_free (error);
@@ -2409,14 +2390,11 @@ attachment_save_read_cb (GInputStream *input_stream,
 		GSimpleAsyncResult *simple;
 		GFile *destination;
 
-		/* Steal the result. */
-		simple = save_context->simple;
-		save_context->simple = NULL;
-
 		/* Steal the destination. */
 		destination = save_context->destination;
 		save_context->destination = NULL;
 
+		simple = save_context->simple;
 		g_simple_async_result_set_op_res_gpointer (
 			simple, destination, (GDestroyNotify) g_object_unref);
 		g_simple_async_result_complete (simple);
@@ -2610,7 +2588,6 @@ e_attachment_save_async (EAttachment *attachment,
 
 	g_return_if_fail (E_IS_ATTACHMENT (attachment));
 	g_return_if_fail (G_IS_FILE (destination));
-	g_return_if_fail (callback != NULL);
 
 	if (e_attachment_get_loading (attachment)) {
 		g_simple_async_report_error_in_idle (
@@ -2666,7 +2643,6 @@ e_attachment_save_finish (EAttachment *attachment,
 	if (destination != NULL)
 		g_object_ref (destination);
 	g_simple_async_result_propagate_error (simple, error);
-	g_object_unref (simple);
 
 	attachment_set_saving (attachment, FALSE);
 



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