1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2006 Xavier Claessens <xclaesse@gmail.com>
4 * Copyright (C) 2007 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include "empathy-avatar.h"
27 #include "empathy-utils.h"
28 #include "empathy-debug.h"
30 #define DEBUG_DOMAIN "Avatar"
33 empathy_avatar_get_type (void)
35 static GType type_id = 0;
38 type_id = g_boxed_type_register_static ("EmpathyAvatar",
39 (GBoxedCopyFunc) empathy_avatar_ref,
40 (GBoxedFreeFunc) empathy_avatar_unref);
47 avatar_get_filename (const gchar *token)
53 avatar_path = g_build_filename (g_get_home_dir (),
58 g_mkdir_with_parents (avatar_path, 0700);
60 token_escaped = empathy_escape_as_identifier (token);
61 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
63 g_free (token_escaped);
69 static EmpathyAvatar *
70 avatar_new (guchar *data,
75 EmpathyAvatar *avatar;
77 avatar = g_slice_new0 (EmpathyAvatar);
80 avatar->format = format;
81 avatar->token = token;
88 empathy_avatar_new (const guchar *data,
93 EmpathyAvatar *avatar;
97 g_return_val_if_fail (data != NULL, NULL);
98 g_return_val_if_fail (len > 0, NULL);
99 g_return_val_if_fail (format != NULL, NULL);
100 g_return_val_if_fail (!G_STR_EMPTY (token), NULL);
102 avatar = avatar_new (g_memdup (data, len),
107 /* Save to cache if not yet in it */
108 filename = avatar_get_filename (token);
109 if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
110 if (!g_file_set_contents (filename, data, len, &error)) {
111 empathy_debug (DEBUG_DOMAIN,
112 "Failed to save avatar in cache: %s",
113 error ? error->message : "No error given");
114 g_clear_error (&error);
116 empathy_debug (DEBUG_DOMAIN, "Avatar saved to %s", filename);
125 empathy_avatar_new_from_cache (const gchar *token)
127 EmpathyAvatar *avatar = NULL;
131 GError *error = NULL;
133 g_return_val_if_fail (!G_STR_EMPTY (token), NULL);
135 /* Load the avatar from file if it exists */
136 filename = avatar_get_filename (token);
137 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
138 if (!g_file_get_contents (filename, &data, &len, &error)) {
139 empathy_debug (DEBUG_DOMAIN,
140 "Failed to load avatar from cache: %s",
141 error ? error->message : "No error given");
142 g_clear_error (&error);
147 empathy_debug (DEBUG_DOMAIN, "Avatar loaded from %s", filename);
148 avatar = avatar_new (data, len, NULL, g_strdup (token));
157 empathy_avatar_unref (EmpathyAvatar *avatar)
159 g_return_if_fail (avatar != NULL);
162 if (avatar->refcount == 0) {
163 g_free (avatar->data);
164 g_free (avatar->format);
165 g_free (avatar->token);
166 g_slice_free (EmpathyAvatar, avatar);
171 empathy_avatar_ref (EmpathyAvatar *avatar)
173 g_return_val_if_fail (avatar != NULL, NULL);