]> git.0d.be Git - empathy.git/blob - libempathy/empathy-avatar.c
Do not load avatar from cache if token is empty. Fixes bug #517098.
[empathy.git] / libempathy / empathy-avatar.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2006 Xavier Claessens <xclaesse@gmail.com>
4  * Copyright (C) 2007 Collabora Ltd.
5  *
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.
10  *
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.
15  *
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
19  *
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include "config.h"
24
25 #include <telepathy-glib/util.h>
26
27 #include "empathy-avatar.h"
28 #include "empathy-utils.h"
29 #include "empathy-debug.h"
30
31 #define DEBUG_DOMAIN "Avatar"
32
33 GType
34 empathy_avatar_get_type (void)
35 {
36         static GType type_id = 0;
37
38         if (!type_id) {
39                 type_id = g_boxed_type_register_static ("EmpathyAvatar",
40                                                         (GBoxedCopyFunc) empathy_avatar_ref,
41                                                         (GBoxedFreeFunc) empathy_avatar_unref);
42         }
43
44         return type_id;
45 }
46
47 static gchar *
48 avatar_get_filename (const gchar *token)
49 {
50         gchar *avatar_path;
51         gchar *avatar_file;
52         gchar *token_escaped;
53
54         avatar_path = g_build_filename (g_get_home_dir (),
55                                         ".gnome2",
56                                         PACKAGE_NAME,
57                                         "avatars",
58                                         NULL);
59         g_mkdir_with_parents (avatar_path, 0700);
60
61         token_escaped = tp_escape_as_identifier (token);
62         avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
63
64         g_free (token_escaped);
65         g_free (avatar_path);
66
67         return avatar_file;
68 }
69
70 static EmpathyAvatar *
71 avatar_new (guchar *data,
72             gsize   len,
73             gchar  *format,
74             gchar  *token)
75 {
76         EmpathyAvatar *avatar;
77
78         avatar = g_slice_new0 (EmpathyAvatar);
79         avatar->data = data;
80         avatar->len = len;
81         avatar->format = format;
82         avatar->token = token;
83         avatar->refcount = 1;
84
85         return avatar;
86 }
87
88 EmpathyAvatar *
89 empathy_avatar_new (const guchar *data,
90                     const gsize   len,
91                     const gchar  *format,
92                     const gchar  *token)
93 {
94         EmpathyAvatar *avatar;
95         gchar         *filename;
96         GError        *error = NULL;
97
98         g_return_val_if_fail (data != NULL, NULL);
99         g_return_val_if_fail (len > 0, NULL);
100         g_return_val_if_fail (format != NULL, NULL);
101         g_return_val_if_fail (!G_STR_EMPTY (token), NULL);
102
103         avatar = avatar_new (g_memdup (data, len),
104                              len,
105                              g_strdup (format),
106                              g_strdup (token));
107
108         /* Save to cache if not yet in it */
109         filename = avatar_get_filename (token);
110         if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
111                 if (!g_file_set_contents (filename, data, len, &error)) {
112                         empathy_debug (DEBUG_DOMAIN,
113                                        "Failed to save avatar in cache: %s",
114                                        error ? error->message : "No error given");
115                         g_clear_error (&error);
116                 } else {
117                         empathy_debug (DEBUG_DOMAIN, "Avatar saved to %s", filename);
118                 }
119         }
120         g_free (filename);
121
122         return avatar;
123 }
124
125 EmpathyAvatar *
126 empathy_avatar_new_from_cache (const gchar *token)
127 {
128         EmpathyAvatar *avatar = NULL;
129         gchar         *filename;
130         gchar         *data = NULL;
131         gsize          len;
132         GError        *error = NULL;
133
134         g_return_val_if_fail (!G_STR_EMPTY (token), NULL);
135
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                         empathy_debug (DEBUG_DOMAIN,
141                                        "Failed to load avatar from cache: %s",
142                                        error ? error->message : "No error given");
143                         g_clear_error (&error);
144                 }
145         }
146
147         if (data) {
148                 empathy_debug (DEBUG_DOMAIN, "Avatar loaded from %s", filename);
149                 avatar = avatar_new (data, len, NULL, g_strdup (token));
150         }
151
152         g_free (filename);
153
154         return avatar;
155 }
156
157 void
158 empathy_avatar_unref (EmpathyAvatar *avatar)
159 {
160         g_return_if_fail (avatar != NULL);
161
162         avatar->refcount--;
163         if (avatar->refcount == 0) {
164                 g_free (avatar->data);
165                 g_free (avatar->format);
166                 g_free (avatar->token);
167                 g_slice_free (EmpathyAvatar, avatar);
168         }
169 }
170
171 EmpathyAvatar *
172 empathy_avatar_ref (EmpathyAvatar *avatar)
173 {
174         g_return_val_if_fail (avatar != NULL, NULL);
175
176         avatar->refcount++;
177
178         return avatar;
179 }
180