aboutsummaryrefslogtreecommitdiffstats
path: root/libempathy/empathy-avatar.c
diff options
context:
space:
mode:
authorXavier Claessens <xclaesse@gmail.com>2007-09-27 04:16:00 +0800
committerXavier Claessens <xclaesse@src.gnome.org>2007-09-27 04:16:00 +0800
commit8c22592a9a13ac09f8e7c0357fafcfd163910a4c (patch)
tree59b0436d072c06b8c423038064dc79728f619614 /libempathy/empathy-avatar.c
parentbf1079017d1d5d78147619a6377ef00e62010bf4 (diff)
downloadgsoc2013-empathy-8c22592a9a13ac09f8e7c0357fafcfd163910a4c.tar
gsoc2013-empathy-8c22592a9a13ac09f8e7c0357fafcfd163910a4c.tar.gz
gsoc2013-empathy-8c22592a9a13ac09f8e7c0357fafcfd163910a4c.tar.bz2
gsoc2013-empathy-8c22592a9a13ac09f8e7c0357fafcfd163910a4c.tar.lz
gsoc2013-empathy-8c22592a9a13ac09f8e7c0357fafcfd163910a4c.tar.xz
gsoc2013-empathy-8c22592a9a13ac09f8e7c0357fafcfd163910a4c.tar.zst
gsoc2013-empathy-8c22592a9a13ac09f8e7c0357fafcfd163910a4c.zip
Cache avatars and RequestAvatars only when needed.
2007-09-26 Xavier Claessens <xclaesse@gmail.com> * libempathy/empathy-utils.c: * libempathy/empathy-utils.h: * libempathy/empathy-avatar.c: * libempathy/empathy-avatar.h: * libempathy/empathy-contact-factory.c: Cache avatars and RequestAvatars only when needed. svn path=/trunk/; revision=320
Diffstat (limited to 'libempathy/empathy-avatar.c')
-rw-r--r--libempathy/empathy-avatar.c106
1 files changed, 98 insertions, 8 deletions
diff --git a/libempathy/empathy-avatar.c b/libempathy/empathy-avatar.c
index 425f23f55..e08dd28c4 100644
--- a/libempathy/empathy-avatar.c
+++ b/libempathy/empathy-avatar.c
@@ -24,6 +24,8 @@
#include "config.h"
#include "empathy-avatar.h"
+#include "empathy-utils.h"
+#include "empathy-debug.h"
#define DEBUG_DOMAIN "Avatar"
@@ -41,26 +43,113 @@ empathy_avatar_get_type (void)
return type_id;
}
-EmpathyAvatar *
-empathy_avatar_new (guchar *data,
- gsize len,
- gchar *format)
+static gchar *
+avatar_get_filename (const gchar *token)
{
- EmpathyAvatar *avatar;
+ gchar *avatar_path;
+ gchar *avatar_file;
+ gchar *token_escaped;
- g_return_val_if_fail (data != NULL, NULL);
- g_return_val_if_fail (len > 0, NULL);
- g_return_val_if_fail (format != NULL, NULL);
+ avatar_path = g_build_filename (g_get_home_dir (),
+ ".gnome2",
+ PACKAGE_NAME,
+ "avatars",
+ NULL);
+ g_mkdir_with_parents (avatar_path, 0700);
+
+ token_escaped = empathy_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 (const guchar *data,
+ const gsize len,
+ const gchar *format,
+ const gchar *token)
+{
+ EmpathyAvatar *avatar;
avatar = g_slice_new0 (EmpathyAvatar);
avatar->data = g_memdup (data, len);
avatar->len = len;
avatar->format = g_strdup (format);
+ avatar->token = g_strdup (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 (data, len, format, 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)) {
+ empathy_debug (DEBUG_DOMAIN,
+ "Failed to save avatar in cache: %s",
+ error ? error->message : "No error given");
+ g_clear_error (&error);
+ } else {
+ empathy_debug (DEBUG_DOMAIN, "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)) {
+ empathy_debug (DEBUG_DOMAIN,
+ "Failed to load avatar from cache: %s",
+ error ? error->message : "No error given");
+ g_clear_error (&error);
+ }
+ }
+
+ if (data) {
+ empathy_debug (DEBUG_DOMAIN, "Avatar loaded from %s", filename);
+ avatar = avatar_new (data, len, NULL, token);
+ }
+
+ g_free (filename);
+
+ return avatar;
+}
+
void
empathy_avatar_unref (EmpathyAvatar *avatar)
{
@@ -70,6 +159,7 @@ empathy_avatar_unref (EmpathyAvatar *avatar)
if (avatar->refcount == 0) {
g_free (avatar->data);
g_free (avatar->format);
+ g_free (avatar->token);
g_slice_free (EmpathyAvatar, avatar);
}
}