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-2008 Collabora Ltd.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <telepathy-glib/util.h>
28 #include "empathy-avatar.h"
29 #include "empathy-utils.h"
31 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
32 #include "empathy-debug.h"
35 empathy_avatar_get_type (void)
37 static GType type_id = 0;
40 type_id = g_boxed_type_register_static ("EmpathyAvatar",
41 (GBoxedCopyFunc) empathy_avatar_ref,
42 (GBoxedFreeFunc) empathy_avatar_unref);
49 avatar_get_filename (const gchar *token)
55 avatar_path = g_build_filename (g_get_home_dir (),
60 g_mkdir_with_parents (avatar_path, 0700);
62 token_escaped = tp_escape_as_identifier (token);
63 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
65 g_free (token_escaped);
71 static EmpathyAvatar *
72 avatar_new (guchar *data,
77 EmpathyAvatar *avatar;
79 avatar = g_slice_new0 (EmpathyAvatar);
82 avatar->format = format;
83 avatar->token = token;
90 empathy_avatar_new (const guchar *data,
95 EmpathyAvatar *avatar;
99 g_return_val_if_fail (data != NULL, NULL);
100 g_return_val_if_fail (len > 0, NULL);
101 g_return_val_if_fail (format != NULL, NULL);
102 g_return_val_if_fail (!G_STR_EMPTY (token), NULL);
104 avatar = avatar_new (g_memdup (data, len),
109 /* Save to cache if not yet in it */
110 filename = avatar_get_filename (token);
111 if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
112 if (!g_file_set_contents (filename, data, len, &error)) {
113 DEBUG ("Failed to save avatar in cache: %s",
114 error ? error->message : "No error given");
115 g_clear_error (&error);
117 DEBUG ("Avatar saved to %s", filename);
126 empathy_avatar_new_from_cache (const gchar *token)
128 EmpathyAvatar *avatar = NULL;
132 GError *error = NULL;
134 g_return_val_if_fail (!G_STR_EMPTY (token), NULL);
136 /* Load the avatar from file if it exists */
137 filename = avatar_get_filename (token);
138 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
139 if (!g_file_get_contents (filename, &data, &len, &error)) {
140 DEBUG ("Failed to load avatar from cache: %s",
141 error ? error->message : "No error given");
142 g_clear_error (&error);
147 DEBUG ("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);