diff options
author | Xavier Claessens <xclaesse@src.gnome.org> | 2008-05-28 01:34:44 +0800 |
---|---|---|
committer | Xavier Claessens <xclaesse@src.gnome.org> | 2008-05-28 01:34:44 +0800 |
commit | 891dd64e17fe816e6bab0b1e1d3ea35eb61a2e35 (patch) | |
tree | ff5389343502b4121a6eebb5496edb6d83d52245 /libempathy/empathy-contact.c | |
parent | 1403c791daa33f39c3dc86917c2f133ced2ae8f4 (diff) | |
download | gsoc2013-empathy-891dd64e17fe816e6bab0b1e1d3ea35eb61a2e35.tar gsoc2013-empathy-891dd64e17fe816e6bab0b1e1d3ea35eb61a2e35.tar.gz gsoc2013-empathy-891dd64e17fe816e6bab0b1e1d3ea35eb61a2e35.tar.bz2 gsoc2013-empathy-891dd64e17fe816e6bab0b1e1d3ea35eb61a2e35.tar.lz gsoc2013-empathy-891dd64e17fe816e6bab0b1e1d3ea35eb61a2e35.tar.xz gsoc2013-empathy-891dd64e17fe816e6bab0b1e1d3ea35eb61a2e35.tar.zst gsoc2013-empathy-891dd64e17fe816e6bab0b1e1d3ea35eb61a2e35.zip |
Move avatar cache to ~/.cache and don't mix avatar from different contacts, tokens are not garanteed to be different.
svn path=/trunk/; revision=1128
Diffstat (limited to 'libempathy/empathy-contact.c')
-rw-r--r-- | libempathy/empathy-contact.c | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/libempathy/empathy-contact.c b/libempathy/empathy-contact.c index 19283cc72..39d6148a2 100644 --- a/libempathy/empathy-contact.c +++ b/libempathy/empathy-contact.c @@ -759,3 +759,166 @@ empathy_contact_run_until_ready (EmpathyContact *contact, loop); } +static gchar * +contact_get_avatar_filename (EmpathyContact *contact, + const gchar *token) +{ + EmpathyContactPriv *priv = GET_PRIV (contact); + gchar *avatar_path; + gchar *avatar_file; + gchar *token_escaped; + gchar *contact_escaped; + + contact_escaped = tp_escape_as_identifier (priv->id); + token_escaped = tp_escape_as_identifier (token); + + avatar_path = g_build_filename (g_get_user_cache_dir (), + PACKAGE_NAME, + "avatars", + mc_account_get_unique_name (priv->account), + contact_escaped, + NULL); + g_mkdir_with_parents (avatar_path, 0700); + + avatar_file = g_build_filename (avatar_path, token_escaped, NULL); + + g_free (contact_escaped); + g_free (token_escaped); + g_free (avatar_path); + + return avatar_file; +} + +void +empathy_contact_load_avatar_data (EmpathyContact *contact, + const guchar *data, + const gsize len, + const gchar *format, + const gchar *token) +{ + EmpathyContactPriv *priv = GET_PRIV (contact); + EmpathyAvatar *avatar; + gchar *filename; + GError *error = NULL; + + g_return_if_fail (EMPATHY_IS_CONTACT (contact)); + g_return_if_fail (!G_STR_EMPTY (priv->id)); + g_return_if_fail (data != NULL); + g_return_if_fail (len > 0); + g_return_if_fail (format != NULL); + g_return_if_fail (!G_STR_EMPTY (token)); + + /* Load and set the avatar */ + avatar = empathy_avatar_new (g_memdup (data, len), len, + g_strdup (format), + g_strdup (token)); + empathy_contact_set_avatar (contact, avatar); + empathy_avatar_unref (avatar); + + /* Save to cache if not yet in it */ + filename = contact_get_avatar_filename (contact, token); + if (!g_file_test (filename, G_FILE_TEST_EXISTS)) { + if (!g_file_set_contents (filename, data, len, &error)) { + DEBUG ("Failed to save avatar in cache: %s", + error ? error->message : "No error given"); + g_clear_error (&error); + } else { + DEBUG ("Avatar saved to %s", filename); + } + } + g_free (filename); +} + +gboolean +empathy_contact_load_avatar_cache (EmpathyContact *contact, + const gchar *token) +{ + EmpathyContactPriv *priv = GET_PRIV (contact); + EmpathyAvatar *avatar = NULL; + gchar *filename; + gchar *data = NULL; + gsize len; + GError *error = NULL; + + g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE); + g_return_val_if_fail (!G_STR_EMPTY (priv->id), FALSE); + g_return_val_if_fail (!G_STR_EMPTY (token), FALSE); + + /* Load the avatar from file if it exists */ + filename = contact_get_avatar_filename (contact, token); + if (g_file_test (filename, G_FILE_TEST_EXISTS)) { + if (!g_file_get_contents (filename, &data, &len, &error)) { + DEBUG ("Failed to load avatar from cache: %s", + error ? error->message : "No error given"); + g_clear_error (&error); + } + } + + if (data) { + DEBUG ("Avatar loaded from %s", filename); + avatar = empathy_avatar_new (data, len, NULL, g_strdup (token)); + empathy_contact_set_avatar (contact, avatar); + empathy_avatar_unref (avatar); + } + + g_free (filename); + + return data != NULL; +} + +GType +empathy_avatar_get_type (void) +{ + static GType type_id = 0; + + if (!type_id) { + type_id = g_boxed_type_register_static ("EmpathyAvatar", + (GBoxedCopyFunc) empathy_avatar_ref, + (GBoxedFreeFunc) empathy_avatar_unref); + } + + return type_id; +} + +EmpathyAvatar * +empathy_avatar_new (guchar *data, + gsize len, + gchar *format, + gchar *token) +{ + EmpathyAvatar *avatar; + + avatar = g_slice_new0 (EmpathyAvatar); + avatar->data = data; + avatar->len = len; + avatar->format = format; + avatar->token = token; + avatar->refcount = 1; + + return avatar; +} + +void +empathy_avatar_unref (EmpathyAvatar *avatar) +{ + g_return_if_fail (avatar != NULL); + + avatar->refcount--; + if (avatar->refcount == 0) { + g_free (avatar->data); + g_free (avatar->format); + g_free (avatar->token); + g_slice_free (EmpathyAvatar, avatar); + } +} + +EmpathyAvatar * +empathy_avatar_ref (EmpathyAvatar *avatar) +{ + g_return_val_if_fail (avatar != NULL, NULL); + + avatar->refcount++; + + return avatar; +} + |