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 | |
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')
-rw-r--r-- | libempathy/Makefile.am | 2 | ||||
-rw-r--r-- | libempathy/empathy-avatar.c | 179 | ||||
-rw-r--r-- | libempathy/empathy-avatar.h | 53 | ||||
-rw-r--r-- | libempathy/empathy-contact.c | 163 | ||||
-rw-r--r-- | libempathy/empathy-contact.h | 26 | ||||
-rw-r--r-- | libempathy/empathy-log-manager.c | 10 | ||||
-rw-r--r-- | libempathy/empathy-tp-contact-factory.c | 23 |
7 files changed, 199 insertions, 257 deletions
diff --git a/libempathy/Makefile.am b/libempathy/Makefile.am index 0db883d3d..3fb491855 100644 --- a/libempathy/Makefile.am +++ b/libempathy/Makefile.am @@ -16,7 +16,6 @@ BUILT_SOURCES = \ lib_LTLIBRARIES = libempathy.la libempathy_la_SOURCES = \ - empathy-avatar.c \ empathy-time.c \ empathy-status-presets.c \ empathy-debug.c \ @@ -57,7 +56,6 @@ libempathy_la_LDFLAGS = \ -export-symbols-regex ^empathy_ libempathy_headers = \ - empathy-avatar.h \ empathy-time.h \ empathy-status-presets.h \ empathy-debug.h \ diff --git a/libempathy/empathy-avatar.c b/libempathy/empathy-avatar.c deleted file mode 100644 index 41395d6e9..000000000 --- a/libempathy/empathy-avatar.c +++ /dev/null @@ -1,179 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ -/* - * Copyright (C) 2006 Xavier Claessens <xclaesse@gmail.com> - * Copyright (C) 2007-2008 Collabora Ltd. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * Authors: Xavier Claessens <xclaesse@gmail.com> - */ - - -#include "config.h" - -#include <telepathy-glib/util.h> - -#include "empathy-avatar.h" -#include "empathy-utils.h" - -#define DEBUG_FLAG EMPATHY_DEBUG_OTHER -#include "empathy-debug.h" - -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; -} - -static gchar * -avatar_get_filename (const gchar *token) -{ - gchar *avatar_path; - gchar *avatar_file; - gchar *token_escaped; - - avatar_path = g_build_filename (g_get_home_dir (), - ".gnome2", - PACKAGE_NAME, - "avatars", - NULL); - g_mkdir_with_parents (avatar_path, 0700); - - token_escaped = tp_escape_as_identifier (token); - avatar_file = g_build_filename (avatar_path, token_escaped, NULL); - - g_free (token_escaped); - g_free (avatar_path); - - return avatar_file; -} - -static EmpathyAvatar * -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; -} - -EmpathyAvatar * -empathy_avatar_new (const guchar *data, - const gsize len, - const gchar *format, - const gchar *token) -{ - EmpathyAvatar *avatar; - gchar *filename; - GError *error = NULL; - - g_return_val_if_fail (data != NULL, NULL); - g_return_val_if_fail (len > 0, NULL); - g_return_val_if_fail (format != NULL, NULL); - g_return_val_if_fail (!G_STR_EMPTY (token), NULL); - - avatar = avatar_new (g_memdup (data, len), - len, - g_strdup (format), - g_strdup (token)); - - /* Save to cache if not yet in it */ - filename = avatar_get_filename (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); - - return avatar; -} - -EmpathyAvatar * -empathy_avatar_new_from_cache (const gchar *token) -{ - EmpathyAvatar *avatar = NULL; - gchar *filename; - gchar *data = NULL; - gsize len; - GError *error = NULL; - - g_return_val_if_fail (!G_STR_EMPTY (token), NULL); - - /* Load the avatar from file if it exists */ - filename = avatar_get_filename (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 = avatar_new (data, len, NULL, g_strdup (token)); - } - - g_free (filename); - - 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; -} - diff --git a/libempathy/empathy-avatar.h b/libempathy/empathy-avatar.h deleted file mode 100644 index 63be370a2..000000000 --- a/libempathy/empathy-avatar.h +++ /dev/null @@ -1,53 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ -/* - * Copyright (C) 2006 Xavier Claessens <xclaesse@gmail.com> - * Copyright (C) 2007-2008 Collabora Ltd. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * Authors: Xavier Claessens <xclaesse@gmail.com> - */ - -#ifndef __EMPATHY_AVATAR_H__ -#define __EMPATHY_AVATAR_H__ - -#include <glib-object.h> - -G_BEGIN_DECLS - -#define EMPATHY_TYPE_AVATAR (empathy_avatar_get_type ()) - -typedef struct _EmpathyAvatar EmpathyAvatar; - -struct _EmpathyAvatar { - guchar *data; - gsize len; - gchar *format; - gchar *token; - guint refcount; -}; - -GType empathy_avatar_get_type (void) G_GNUC_CONST; -EmpathyAvatar * empathy_avatar_new (const guchar *avatar, - const gsize len, - const gchar *format, - const gchar *token); -EmpathyAvatar * empathy_avatar_new_from_cache (const gchar *token); -EmpathyAvatar * empathy_avatar_ref (EmpathyAvatar *avatar); -void empathy_avatar_unref (EmpathyAvatar *avatar); - -G_END_DECLS - -#endif /* __EMPATHY_AVATAR_H__ */ 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; +} + diff --git a/libempathy/empathy-contact.h b/libempathy/empathy-contact.h index 4abca99bd..fe27a04c8 100644 --- a/libempathy/empathy-contact.h +++ b/libempathy/empathy-contact.h @@ -31,8 +31,6 @@ #include <libmissioncontrol/mc-account.h> #include <libmissioncontrol/mission-control.h> -#include "empathy-avatar.h" - G_BEGIN_DECLS #define EMPATHY_TYPE_CONTACT (empathy_contact_get_type ()) @@ -54,6 +52,14 @@ struct _EmpathyContactClass { GObjectClass parent_class; }; +typedef struct { + guchar *data; + gsize len; + gchar *format; + gchar *token; + guint refcount; +} EmpathyAvatar; + typedef enum { EMPATHY_CAPABILITIES_NONE = 0, EMPATHY_CAPABILITIES_AUDIO = 1 << 0, @@ -111,6 +117,22 @@ guint empathy_contact_hash (gconstpointer key void empathy_contact_run_until_ready (EmpathyContact *contact, EmpathyContactReady ready, GMainLoop **loop); +void empathy_contact_load_avatar_data (EmpathyContact *contact, + const guchar *data, + const gsize len, + const gchar *format, + const gchar *token); +gboolean empathy_contact_load_avatar_cache (EmpathyContact *contact, + const gchar *token); + +#define EMPATHY_TYPE_AVATAR (empathy_avatar_get_type ()) +GType empathy_avatar_get_type (void) G_GNUC_CONST; +EmpathyAvatar * empathy_avatar_new (guchar *data, + gsize len, + gchar *format, + gchar *token); +EmpathyAvatar * empathy_avatar_ref (EmpathyAvatar *avatar); +void empathy_avatar_unref (EmpathyAvatar *avatar); G_END_DECLS diff --git a/libempathy/empathy-log-manager.c b/libempathy/empathy-log-manager.c index 1115e43c3..aa70b49da 100644 --- a/libempathy/empathy-log-manager.c +++ b/libempathy/empathy-log-manager.c @@ -337,7 +337,6 @@ empathy_log_manager_get_messages_for_file (EmpathyLogManager *manager, for (node = log_node->children; node; node = node->next) { EmpathyMessage *message; EmpathyContact *sender; - EmpathyAvatar *avatar = NULL; gchar *time; time_t t; gchar *sender_id; @@ -361,9 +360,6 @@ empathy_log_manager_get_messages_for_file (EmpathyLogManager *manager, is_user_str = xmlGetProp (node, "isuser"); msg_type_str = xmlGetProp (node, "type"); - if (!G_STR_EMPTY (sender_avatar_token)) { - avatar = empathy_avatar_new_from_cache (sender_avatar_token); - } if (is_user_str) { is_user = strcmp (is_user_str, "true") == 0; } @@ -375,9 +371,9 @@ empathy_log_manager_get_messages_for_file (EmpathyLogManager *manager, sender = empathy_contact_new_full (account, sender_id, sender_name); empathy_contact_set_is_user (sender, is_user); - if (avatar) { - empathy_contact_set_avatar (sender, avatar); - empathy_avatar_unref (avatar); + if (!G_STR_EMPTY (sender_avatar_token)) { + empathy_contact_load_avatar_cache (sender, + sender_avatar_token); } message = empathy_message_new (body); diff --git a/libempathy/empathy-tp-contact-factory.c b/libempathy/empathy-tp-contact-factory.c index 7cf0b53cc..fe68ed0a7 100644 --- a/libempathy/empathy-tp-contact-factory.c +++ b/libempathy/empathy-tp-contact-factory.c @@ -323,7 +323,6 @@ tp_contact_factory_avatar_retrieved_cb (TpConnection *connection, GObject *tp_factory) { EmpathyContact *contact; - EmpathyAvatar *avatar; contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory), handle); @@ -335,13 +334,11 @@ tp_contact_factory_avatar_retrieved_cb (TpConnection *connection, empathy_contact_get_id (contact), handle); - avatar = empathy_avatar_new (avatar_data->data, - avatar_data->len, - mime_type, - token); - - empathy_contact_set_avatar (contact, avatar); - empathy_avatar_unref (avatar); + empathy_contact_load_avatar_data (contact, + avatar_data->data, + avatar_data->len, + mime_type, + token); } static void @@ -381,11 +378,8 @@ tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory, } /* The avatar changed, search the new one in the cache */ - avatar = empathy_avatar_new_from_cache (token); - if (avatar) { + if (empathy_contact_load_avatar_cache (contact, token)) { /* Got from cache, use it */ - empathy_contact_set_avatar (contact, avatar); - empathy_avatar_unref (avatar); return TRUE; } @@ -804,8 +798,6 @@ tp_contact_factory_got_self_handle_cb (TpConnection *proxy, handle_needed_contacts, tp_contact_factory_list_free, G_OBJECT (tp_factory)); - tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory, - id_needed); tp_cli_connection_call_inspect_handles (priv->connection, -1, TP_HANDLE_TYPE_CONTACT, @@ -814,6 +806,9 @@ tp_contact_factory_got_self_handle_cb (TpConnection *proxy, id_needed_contacts, tp_contact_factory_list_free, G_OBJECT (tp_factory)); + tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory, + id_needed); + g_array_free (handle_needed, TRUE); g_array_free (id_needed, TRUE); } |