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