From 8fe756a9455a49a03967f516b3b974a23c5c4099 Mon Sep 17 00:00:00 2001 From: Chandni Verma Date: Sun, 15 Sep 2013 00:48:07 +0530 Subject: [PATCH] Make avatar image caching asynchronous Fixes https://bugs.freedesktop.org/show_bug.cgi?id=63402 Based on patch by Luca Versari --- telepathy-glib/contact.c | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/telepathy-glib/contact.c b/telepathy-glib/contact.c index 66b4c45..5058faa 100644 --- a/telepathy-glib/contact.c +++ b/telepathy-glib/contact.c @@ -2761,6 +2761,11 @@ typedef struct{ GArray *data; } write_data; +typedef struct{ + write_data *avatar_data; + write_data *mime_data; + GFile *mime_file; +} AvatarFileWrittenData; static void mime_file_written (GObject *file, GAsyncResult *res, @@ -2789,7 +2794,11 @@ static void mime_file_written (GObject *file, } g_free (mime_data->self->priv->avatar_mime_type); mime_data->self->priv->avatar_mime_type = g_strdup (mime_data->str); + + /* Notify both property changes together once both files have been written */ g_object_notify ((GObject *) mime_data->self, "avatar-mime-type"); + g_object_notify ((GObject *) mime_data->self, "avatar-file"); + g_free (mime_data->str); g_object_unref ((GFile *) file); g_slice_free (write_data, mime_data); @@ -2799,7 +2808,10 @@ static void avatar_file_written (GObject *file, GAsyncResult *res, gpointer user_data){ GError *error = NULL; - write_data *avatar_data = (write_data *) user_data; + AvatarFileWrittenData *avatar_file_written_data = + (AvatarFileWrittenData *) user_data; + write_data *avatar_data = avatar_file_written_data->avatar_data; + write_data *mime_data = avatar_file_written_data->mime_data; g_array_unref (avatar_data->data); g_file_replace_contents_finish ((GFile *) file, res, NULL, &error); if (error != NULL) @@ -2809,6 +2821,10 @@ static void avatar_file_written (GObject *file, error ? error->message : "No error message"); g_free (avatar_data->str); g_slice_free (write_data, avatar_data); + g_free (mime_data->str); + g_slice_free (write_data, mime_data); + g_object_unref (avatar_file_written_data->mime_file); + g_slice_free (AvatarFileWrittenData, avatar_file_written_data); g_clear_error (&error); return; } @@ -2819,6 +2835,10 @@ static void avatar_file_written (GObject *file, { g_free (avatar_data->str); g_slice_free (write_data, avatar_data); + g_free (mime_data->str); + g_slice_free (write_data, mime_data); + g_object_unref (avatar_file_written_data->mime_file); + g_slice_free (AvatarFileWrittenData, avatar_file_written_data); return; } /* Update the avatar token if a newer one is given */ @@ -2826,9 +2846,15 @@ static void avatar_file_written (GObject *file, tp_clear_object (&avatar_data->self->priv->avatar_file); avatar_data->self->priv->avatar_file = (GFile *) file; - g_object_notify ((GObject *) avatar_data->self, "avatar-file"); g_free (avatar_data->str); g_slice_free (write_data, avatar_data); + + /* Now that the avatar-file has been written, write the mime-file. */ + g_file_replace_contents_async (avatar_file_written_data->mime_file, + mime_data->str, strlen (mime_data->str), NULL, FALSE, G_FILE_CREATE_NONE, + NULL, &mime_file_written, mime_data); + + g_slice_free (AvatarFileWrittenData, avatar_file_written_data); } static void @@ -2847,6 +2873,8 @@ contact_avatar_retrieved (TpConnection *connection, GFile *mime_file; write_data *avatar_data = g_slice_new (write_data); write_data *mime_data = g_slice_new (write_data); + AvatarFileWrittenData *avatar_file_written_data = g_slice_new + (AvatarFileWrittenData); if (!build_avatar_filename (connection, token, TRUE, &filename, &mime_filename)) @@ -2856,21 +2884,20 @@ contact_avatar_retrieved (TpConnection *connection, * possible future avatar requests */ avatar_file = g_file_new_for_path (filename); mime_file = g_file_new_for_path (mime_filename); - + avatar_file_written_data->mime_file = mime_file; avatar_data->data = g_array_new (FALSE, FALSE, sizeof (gchar)); g_array_append_vals (avatar_data->data, avatar->data, avatar->len); avatar_data->self = self; avatar_data->str = g_strdup (token); + avatar_file_written_data->avatar_data = avatar_data; mime_data->self = self; mime_data->str = g_strdup (mime_type); + avatar_file_written_data->mime_data = mime_data; g_file_replace_contents_async (avatar_file, avatar_data->data->data, avatar_data->data->len, - NULL, FALSE, G_FILE_CREATE_NONE, NULL, &avatar_file_written, avatar_data); - - g_file_replace_contents_async (mime_file, mime_data->str, strlen (mime_data->str), - NULL, FALSE, G_FILE_CREATE_NONE, NULL, &mime_file_written, mime_data); + NULL, FALSE, G_FILE_CREATE_NONE, NULL, &avatar_file_written, avatar_file_written_data); g_free (filename); g_free (mime_filename); -- 1.8.1.2