]> git.0d.be Git - empathy.git/blob - libempathy/empathy-avatar.c
Set supress_handler to TRUE when creating new group channel.
[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 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.
10  *
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.
15  *
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.
20  *
21  * Authors: Xavier Claessens <xclaesse@gmail.com>
22  */
23
24 #include "config.h"
25
26 #include "empathy-avatar.h"
27 #include "empathy-utils.h"
28 #include "empathy-debug.h"
29
30 #define DEBUG_DOMAIN "Avatar"
31
32 GType
33 empathy_avatar_get_type (void)
34 {
35         static GType type_id = 0;
36
37         if (!type_id) {
38                 type_id = g_boxed_type_register_static ("EmpathyAvatar",
39                                                         (GBoxedCopyFunc) empathy_avatar_ref,
40                                                         (GBoxedFreeFunc) empathy_avatar_unref);
41         }
42
43         return type_id;
44 }
45
46 static gchar *
47 avatar_get_filename (const gchar *token)
48 {
49         gchar *avatar_path;
50         gchar *avatar_file;
51         gchar *token_escaped;
52
53         avatar_path = g_build_filename (g_get_home_dir (),
54                                         ".gnome2",
55                                         PACKAGE_NAME,
56                                         "avatars",
57                                         NULL);
58         g_mkdir_with_parents (avatar_path, 0700);
59
60         token_escaped = empathy_escape_as_identifier (token);
61         avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
62
63         g_free (token_escaped);
64         g_free (avatar_path);
65
66         return avatar_file;
67 }
68
69 static EmpathyAvatar *
70 avatar_new (guchar *data,
71             gsize   len,
72             gchar  *format,
73             gchar  *token)
74 {
75         EmpathyAvatar *avatar;
76
77         avatar = g_slice_new0 (EmpathyAvatar);
78         avatar->data = data;
79         avatar->len = len;
80         avatar->format = format;
81         avatar->token = token;
82         avatar->refcount = 1;
83
84         return avatar;
85 }
86
87 EmpathyAvatar *
88 empathy_avatar_new (const guchar *data,
89                     const gsize   len,
90                     const gchar  *format,
91                     const gchar  *token)
92 {
93         EmpathyAvatar *avatar;
94         gchar         *filename;
95         GError        *error = NULL;
96
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);
101
102         avatar = avatar_new (g_memdup (data, len),
103                              len,
104                              g_strdup (format),
105                              g_strdup (token));
106
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);
115                 } else {
116                         empathy_debug (DEBUG_DOMAIN, "Avatar saved to %s", filename);
117                 }
118         }
119         g_free (filename);
120
121         return avatar;
122 }
123
124 EmpathyAvatar *
125 empathy_avatar_new_from_cache (const gchar *token)
126 {
127         EmpathyAvatar *avatar = NULL;
128         gchar         *filename;
129         gchar         *data = NULL;
130         gsize          len;
131         GError        *error = NULL;
132
133         g_return_val_if_fail (!G_STR_EMPTY (token), NULL);
134
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);
143                 }
144         }
145
146         if (data) {
147                 empathy_debug (DEBUG_DOMAIN, "Avatar loaded from %s", filename);
148                 avatar = avatar_new (data, len, NULL, g_strdup (token));
149         }
150
151         g_free (filename);
152
153         return avatar;
154 }
155
156 void
157 empathy_avatar_unref (EmpathyAvatar *avatar)
158 {
159         g_return_if_fail (avatar != NULL);
160
161         avatar->refcount--;
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);
167         }
168 }
169
170 EmpathyAvatar *
171 empathy_avatar_ref (EmpathyAvatar *avatar)
172 {
173         g_return_val_if_fail (avatar != NULL, NULL);
174
175         avatar->refcount++;
176
177         return avatar;
178 }
179