Mercurial > pidgin.yaz
annotate src/buddyicon.c @ 12541:28f03a3c56c2
[gaim-migrate @ 14854]
:%s/^ /\t/
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Sat, 17 Dec 2005 21:25:17 +0000 |
parents | 5e2a365af01b |
children | 595f690399e4 |
rev | line source |
---|---|
6846 | 1 /** |
2 * @file icon.c Buddy Icon API | |
3 * @ingroup core | |
4 * | |
5 * gaim | |
6 * | |
8046 | 7 * Gaim is the legal property of its developers, whose names are too numerous |
8 * to list here. Please refer to the COPYRIGHT file distributed with this | |
9 * source distribution. | |
6846 | 10 * |
11 * This program is free software; you can redistribute it and/or modify | |
12 * it under the terms of the GNU General Public License as published by | |
13 * the Free Software Foundation; either version 2 of the License, or | |
14 * (at your option) any later version. | |
15 * | |
16 * This program is distributed in the hope that it will be useful, | |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 * GNU General Public License for more details. | |
20 * | |
21 * You should have received a copy of the GNU General Public License | |
22 * along with this program; if not, write to the Free Software | |
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
24 */ | |
25 #include "internal.h" | |
26 #include "buddyicon.h" | |
27 #include "conversation.h" | |
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
28 #include "debug.h" |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
29 #include "util.h" |
6846 | 30 |
31 static GHashTable *account_cache = NULL; | |
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
32 static char *cache_dir = NULL; |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
33 static gboolean icon_caching = TRUE; |
6846 | 34 |
9396 | 35 static GaimBuddyIcon * |
36 gaim_buddy_icon_create(GaimAccount *account, const char *username) | |
37 { | |
38 GaimBuddyIcon *icon; | |
39 GHashTable *icon_cache; | |
40 | |
41 icon = g_new0(GaimBuddyIcon, 1); | |
42 | |
43 gaim_buddy_icon_set_account(icon, account); | |
44 gaim_buddy_icon_set_username(icon, username); | |
45 | |
46 icon_cache = g_hash_table_lookup(account_cache, account); | |
47 | |
48 if (icon_cache == NULL) | |
49 { | |
50 icon_cache = g_hash_table_new(g_str_hash, g_str_equal); | |
51 | |
52 g_hash_table_insert(account_cache, account, icon_cache); | |
53 } | |
54 | |
55 g_hash_table_insert(icon_cache, | |
56 (char *)gaim_buddy_icon_get_username(icon), icon); | |
57 return icon; | |
58 } | |
59 | |
6846 | 60 GaimBuddyIcon * |
61 gaim_buddy_icon_new(GaimAccount *account, const char *username, | |
62 void *icon_data, size_t icon_len) | |
63 { | |
64 GaimBuddyIcon *icon; | |
65 | |
66 g_return_val_if_fail(account != NULL, NULL); | |
67 g_return_val_if_fail(username != NULL, NULL); | |
68 g_return_val_if_fail(icon_data != NULL, NULL); | |
69 g_return_val_if_fail(icon_len > 0, NULL); | |
70 | |
71 icon = gaim_buddy_icons_find(account, username); | |
72 | |
73 if (icon == NULL) | |
9396 | 74 icon = gaim_buddy_icon_create(account, username); |
6846 | 75 |
9327 | 76 gaim_buddy_icon_ref(icon); |
6846 | 77 gaim_buddy_icon_set_data(icon, icon_data, icon_len); |
78 | |
11423
202a3b3c5a88
[gaim-migrate @ 13660]
Richard Laager <rlaager@wiktel.com>
parents:
11338
diff
changeset
|
79 /* gaim_buddy_icon_set_data() makes blist.c or |
202a3b3c5a88
[gaim-migrate @ 13660]
Richard Laager <rlaager@wiktel.com>
parents:
11338
diff
changeset
|
80 * conversation.c, or both, take a reference. |
202a3b3c5a88
[gaim-migrate @ 13660]
Richard Laager <rlaager@wiktel.com>
parents:
11338
diff
changeset
|
81 * |
202a3b3c5a88
[gaim-migrate @ 13660]
Richard Laager <rlaager@wiktel.com>
parents:
11338
diff
changeset
|
82 * Plus, we leave one for the caller of this function. |
9323 | 83 */ |
11423
202a3b3c5a88
[gaim-migrate @ 13660]
Richard Laager <rlaager@wiktel.com>
parents:
11338
diff
changeset
|
84 |
6846 | 85 return icon; |
86 } | |
87 | |
88 void | |
89 gaim_buddy_icon_destroy(GaimBuddyIcon *icon) | |
90 { | |
7311
1c8830db0189
[gaim-migrate @ 7895]
Christian Hammond <chipx86@chipx86.com>
parents:
7125
diff
changeset
|
91 GaimConversation *conv; |
1c8830db0189
[gaim-migrate @ 7895]
Christian Hammond <chipx86@chipx86.com>
parents:
7125
diff
changeset
|
92 GaimAccount *account; |
6846 | 93 GHashTable *icon_cache; |
7311
1c8830db0189
[gaim-migrate @ 7895]
Christian Hammond <chipx86@chipx86.com>
parents:
7125
diff
changeset
|
94 const char *username; |
9305
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
95 GSList *sl, *list; |
6846 | 96 |
97 g_return_if_fail(icon != NULL); | |
98 | |
99 if (icon->ref_count > 0) | |
100 { | |
9323 | 101 /* If the ref count is greater than 0, then we weren't called from |
102 * gaim_buddy_icon_unref(). So we go through and ask everyone to | |
103 * unref us. Then we return, since we know somewhere along the | |
104 * line we got called recursively by one of the unrefs, and the | |
105 * icon is already destroyed. | |
106 */ | |
107 account = gaim_buddy_icon_get_account(icon); | |
108 username = gaim_buddy_icon_get_username(icon); | |
109 | |
11338 | 110 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, username, account); |
10246 | 111 if (conv != NULL) |
9323 | 112 gaim_conv_im_set_icon(GAIM_CONV_IM(conv), NULL); |
113 | |
114 for (list = sl = gaim_find_buddies(account, username); sl != NULL; | |
115 sl = sl->next) | |
116 { | |
117 GaimBuddy *buddy = (GaimBuddy *)sl->data; | |
118 | |
119 gaim_buddy_set_icon(buddy, NULL); | |
120 } | |
121 | |
122 g_slist_free(list); | |
6846 | 123 |
124 return; | |
125 } | |
126 | |
127 icon_cache = g_hash_table_lookup(account_cache, | |
128 gaim_buddy_icon_get_account(icon)); | |
129 | |
130 if (icon_cache != NULL) | |
131 g_hash_table_remove(icon_cache, gaim_buddy_icon_get_username(icon)); | |
132 | |
133 if (icon->username != NULL) | |
134 g_free(icon->username); | |
135 | |
136 if (icon->data != NULL) | |
137 g_free(icon->data); | |
138 | |
139 g_free(icon); | |
140 } | |
141 | |
142 GaimBuddyIcon * | |
143 gaim_buddy_icon_ref(GaimBuddyIcon *icon) | |
144 { | |
145 g_return_val_if_fail(icon != NULL, NULL); | |
146 | |
147 icon->ref_count++; | |
148 | |
149 return icon; | |
150 } | |
151 | |
152 GaimBuddyIcon * | |
153 gaim_buddy_icon_unref(GaimBuddyIcon *icon) | |
154 { | |
155 g_return_val_if_fail(icon != NULL, NULL); | |
12250
5e2a365af01b
[gaim-migrate @ 14552]
Richard Laager <rlaager@wiktel.com>
parents:
12097
diff
changeset
|
156 g_return_val_if_fail(icon->ref_count > 0, NULL); |
6846 | 157 |
158 icon->ref_count--; | |
159 | |
160 if (icon->ref_count == 0) | |
161 { | |
162 gaim_buddy_icon_destroy(icon); | |
163 | |
164 return NULL; | |
165 } | |
166 | |
167 return icon; | |
168 } | |
169 | |
170 void | |
171 gaim_buddy_icon_update(GaimBuddyIcon *icon) | |
172 { | |
173 GaimConversation *conv; | |
174 GaimAccount *account; | |
175 const char *username; | |
8550 | 176 GSList *sl, *list; |
6846 | 177 |
178 g_return_if_fail(icon != NULL); | |
179 | |
180 account = gaim_buddy_icon_get_account(icon); | |
181 username = gaim_buddy_icon_get_username(icon); | |
182 | |
9305
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
183 for (list = sl = gaim_find_buddies(account, username); sl != NULL; |
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
184 sl = sl->next) |
6846 | 185 { |
186 GaimBuddy *buddy = (GaimBuddy *)sl->data; | |
187 | |
188 gaim_buddy_set_icon(buddy, icon); | |
189 } | |
190 | |
8550 | 191 g_slist_free(list); |
192 | |
11338 | 193 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, username, account); |
6846 | 194 |
10246 | 195 if (conv != NULL) |
7118
bf630f7dfdcd
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
6886
diff
changeset
|
196 gaim_conv_im_set_icon(GAIM_CONV_IM(conv), icon); |
6846 | 197 } |
198 | |
11040
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
199 static void |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
200 delete_icon_cache_file(const char *dirname, const char *old_icon) |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
201 { |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
202 struct stat st; |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
203 |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
204 g_return_if_fail(dirname != NULL); |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
205 g_return_if_fail(old_icon != NULL); |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
206 |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
207 if (g_stat(old_icon, &st) == 0) |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
208 g_unlink(old_icon); |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
209 else |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
210 { |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
211 char *filename = g_build_filename(dirname, old_icon, NULL); |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
212 if (g_stat(filename, &st) == 0) |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
213 g_unlink(filename); |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
214 g_free(filename); |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
215 } |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
216 } |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
217 |
6846 | 218 void |
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
219 gaim_buddy_icon_cache(GaimBuddyIcon *icon, GaimBuddy *buddy) |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
220 { |
11137 | 221 const guchar *data; |
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
222 const char *dirname; |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
223 char *random; |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
224 char *filename; |
7125 | 225 const char *old_icon; |
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
226 size_t len; |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
227 FILE *file = NULL; |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
228 |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
229 g_return_if_fail(icon != NULL); |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
230 g_return_if_fail(buddy != NULL); |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
231 |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
232 if (!gaim_buddy_icons_is_caching()) |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
233 return; |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
234 |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
235 data = gaim_buddy_icon_get_data(icon, &len); |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
236 |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
237 random = g_strdup_printf("%x", g_random_int()); |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
238 dirname = gaim_buddy_icons_get_cache_dir(); |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
239 filename = g_build_filename(dirname, random, NULL); |
7721 | 240 old_icon = gaim_blist_node_get_string((GaimBlistNode*)buddy, "buddy_icon"); |
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
241 |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
242 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
243 { |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
244 gaim_debug_info("buddy icons", "Creating icon cache directory.\n"); |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
245 |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10523
diff
changeset
|
246 if (g_mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR) < 0) |
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
247 { |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
248 gaim_debug_error("buddy icons", |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
249 "Unable to create directory %s: %s\n", |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
250 dirname, strerror(errno)); |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
251 } |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
252 } |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
253 |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10523
diff
changeset
|
254 if ((file = g_fopen(filename, "wb")) != NULL) |
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
255 { |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
256 fwrite(data, 1, len, file); |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
257 fclose(file); |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
258 } |
11021 | 259 else |
260 { | |
261 gaim_debug_error("buddy icons", "Unable to create file %s: %s\n", | |
262 filename, strerror(errno)); | |
263 } | |
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
264 |
9747 | 265 g_free(filename); |
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
266 |
9747 | 267 if (old_icon != NULL) |
11040
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
268 delete_icon_cache_file(dirname, old_icon); |
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
269 |
9747 | 270 gaim_blist_node_set_string((GaimBlistNode *)buddy, "buddy_icon", random); |
271 | |
272 g_free(random); | |
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
273 } |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
274 |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
275 void |
11040
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
276 gaim_buddy_icon_uncache(GaimBuddy *buddy) |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
277 { |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
278 const char *old_icon; |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
279 |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
280 g_return_if_fail(buddy != NULL); |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
281 |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
282 old_icon = gaim_blist_node_get_string((GaimBlistNode *)buddy, "buddy_icon"); |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
283 |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
284 if (old_icon != NULL) |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
285 delete_icon_cache_file(gaim_buddy_icons_get_cache_dir(), old_icon); |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
286 |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
287 gaim_blist_node_remove_setting((GaimBlistNode *)buddy, "buddy_icon"); |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
288 |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
289 /* Unset the icon in case this function is called from |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
290 * something other than gaim_buddy_set_icon(). */ |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
291 if (buddy->icon != NULL) |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
292 { |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
293 gaim_buddy_icon_unref(buddy->icon); |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
294 buddy->icon = NULL; |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
295 } |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
296 } |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
297 |
3428ad6f5049
[gaim-migrate @ 12940]
Richard Laager <rlaager@wiktel.com>
parents:
11033
diff
changeset
|
298 void |
6846 | 299 gaim_buddy_icon_set_account(GaimBuddyIcon *icon, GaimAccount *account) |
300 { | |
301 g_return_if_fail(icon != NULL); | |
302 g_return_if_fail(account != NULL); | |
303 | |
304 icon->account = account; | |
305 } | |
306 | |
307 void | |
308 gaim_buddy_icon_set_username(GaimBuddyIcon *icon, const char *username) | |
309 { | |
310 g_return_if_fail(icon != NULL); | |
311 g_return_if_fail(username != NULL); | |
312 | |
313 if (icon->username != NULL) | |
314 g_free(icon->username); | |
315 | |
316 icon->username = g_strdup(username); | |
317 } | |
318 | |
319 void | |
320 gaim_buddy_icon_set_data(GaimBuddyIcon *icon, void *data, size_t len) | |
321 { | |
322 g_return_if_fail(icon != NULL); | |
323 | |
324 if (icon->data != NULL) | |
325 g_free(icon->data); | |
326 | |
327 if (data != NULL && len > 0) | |
328 { | |
329 icon->data = g_memdup(data, len); | |
330 icon->len = len; | |
331 } | |
332 else | |
333 { | |
334 icon->data = NULL; | |
335 icon->len = 0; | |
336 } | |
337 | |
338 gaim_buddy_icon_update(icon); | |
339 } | |
340 | |
341 GaimAccount * | |
342 gaim_buddy_icon_get_account(const GaimBuddyIcon *icon) | |
343 { | |
344 g_return_val_if_fail(icon != NULL, NULL); | |
345 | |
346 return icon->account; | |
347 } | |
348 | |
349 const char * | |
350 gaim_buddy_icon_get_username(const GaimBuddyIcon *icon) | |
351 { | |
352 g_return_val_if_fail(icon != NULL, NULL); | |
353 | |
354 return icon->username; | |
355 } | |
356 | |
11137 | 357 const guchar * |
6846 | 358 gaim_buddy_icon_get_data(const GaimBuddyIcon *icon, size_t *len) |
359 { | |
360 g_return_val_if_fail(icon != NULL, NULL); | |
361 | |
362 if (len != NULL) | |
363 *len = icon->len; | |
364 | |
365 return icon->data; | |
366 } | |
367 | |
10953 | 368 const char * |
369 gaim_buddy_icon_get_type(const GaimBuddyIcon *icon) | |
370 { | |
371 const void *data; | |
372 size_t len; | |
373 | |
374 g_return_val_if_fail(icon != NULL, NULL); | |
375 | |
376 data = gaim_buddy_icon_get_data(icon, &len); | |
377 | |
378 /* TODO: Find a way to do this with GDK */ | |
379 if (len >= 4) | |
380 { | |
381 if (!strncmp(data, "BM", 2)) | |
382 return "bmp"; | |
383 else if (!strncmp(data, "GIF8", 4)) | |
384 return "gif"; | |
385 else if (!strncmp(data, "\xff\xd8\xff\xe0", 4)) | |
386 return "jpg"; | |
387 else if (!strncmp(data, "\x89PNG", 4)) | |
388 return "png"; | |
389 } | |
390 | |
391 return NULL; | |
392 } | |
393 | |
6846 | 394 void |
395 gaim_buddy_icons_set_for_user(GaimAccount *account, const char *username, | |
396 void *icon_data, size_t icon_len) | |
397 { | |
398 g_return_if_fail(account != NULL); | |
399 g_return_if_fail(username != NULL); | |
400 | |
9305
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
401 if (icon_data == NULL || icon_len == 0) |
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
402 { |
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
403 GaimBuddyIcon *buddy_icon; |
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
404 |
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
405 buddy_icon = gaim_buddy_icons_find(account, username); |
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
406 |
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
407 if (buddy_icon != NULL) |
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
408 gaim_buddy_icon_destroy(buddy_icon); |
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
409 } |
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
410 else |
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
411 { |
11423
202a3b3c5a88
[gaim-migrate @ 13660]
Richard Laager <rlaager@wiktel.com>
parents:
11338
diff
changeset
|
412 GaimBuddyIcon *icon = gaim_buddy_icon_new(account, username, icon_data, icon_len); |
202a3b3c5a88
[gaim-migrate @ 13660]
Richard Laager <rlaager@wiktel.com>
parents:
11338
diff
changeset
|
413 gaim_buddy_icon_unref(icon); |
9305
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
414 } |
6846 | 415 } |
416 | |
417 GaimBuddyIcon * | |
9396 | 418 gaim_buddy_icons_find(GaimAccount *account, const char *username) |
6846 | 419 { |
420 GHashTable *icon_cache; | |
9396 | 421 GaimBuddyIcon *ret = NULL; |
9747 | 422 char *filename = NULL; |
6846 | 423 |
424 g_return_val_if_fail(account != NULL, NULL); | |
425 g_return_val_if_fail(username != NULL, NULL); | |
426 | |
427 icon_cache = g_hash_table_lookup(account_cache, account); | |
428 | |
9396 | 429 if ((icon_cache == NULL) || ((ret = g_hash_table_lookup(icon_cache, username)) == NULL)) { |
430 const char *file; | |
431 struct stat st; | |
432 GaimBuddy *b = gaim_find_buddy(account, username); | |
433 | |
434 if (!b) | |
435 return NULL; | |
436 | |
437 if ((file = gaim_blist_node_get_string((GaimBlistNode*)b, "buddy_icon")) == NULL) | |
438 return NULL; | |
6846 | 439 |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10523
diff
changeset
|
440 if (!g_stat(file, &st)) |
9747 | 441 filename = g_strdup(file); |
442 else | |
443 filename = g_build_filename(gaim_buddy_icons_get_cache_dir(), file, NULL); | |
444 | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10523
diff
changeset
|
445 if (!g_stat(filename, &st)) { |
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10523
diff
changeset
|
446 FILE *f = g_fopen(filename, "rb"); |
9396 | 447 if (f) { |
448 char *data = g_malloc(st.st_size); | |
449 fread(data, 1, st.st_size, f); | |
450 fclose(f); | |
451 ret = gaim_buddy_icon_create(account, username); | |
452 gaim_buddy_icon_ref(ret); | |
453 gaim_buddy_icon_set_data(ret, data, st.st_size); | |
454 gaim_buddy_icon_unref(ret); | |
455 g_free(data); | |
9747 | 456 g_free(filename); |
9396 | 457 return ret; |
458 } | |
459 } | |
9747 | 460 g_free(filename); |
9396 | 461 } |
462 | |
463 return ret; | |
6846 | 464 } |
465 | |
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
466 void |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
467 gaim_buddy_icons_set_caching(gboolean caching) |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
468 { |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
469 icon_caching = caching; |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
470 } |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
471 |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
472 gboolean |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
473 gaim_buddy_icons_is_caching(void) |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
474 { |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
475 return icon_caching; |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
476 } |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
477 |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
478 void |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
479 gaim_buddy_icons_set_cache_dir(const char *dir) |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
480 { |
10811
dc59482c8d37
[gaim-migrate @ 12464]
Luke Schierer <lschiere@pidgin.im>
parents:
10589
diff
changeset
|
481 g_return_if_fail(dir != NULL); |
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
482 |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
483 if (cache_dir != NULL) |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
484 g_free(cache_dir); |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
485 |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
486 cache_dir = g_strdup(dir); |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
487 } |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
488 |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
489 const char * |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
490 gaim_buddy_icons_get_cache_dir(void) |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
491 { |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
492 return cache_dir; |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
493 } |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
494 |
11303
10066662176a
[gaim-migrate @ 13503]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
495 char *gaim_buddy_icons_get_full_path(const char *icon) { |
10066662176a
[gaim-migrate @ 13503]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
496 struct stat st; |
10066662176a
[gaim-migrate @ 13503]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
497 |
10066662176a
[gaim-migrate @ 13503]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
498 if (icon == NULL) |
10066662176a
[gaim-migrate @ 13503]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
499 return NULL; |
10066662176a
[gaim-migrate @ 13503]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
500 |
10066662176a
[gaim-migrate @ 13503]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
501 if (g_stat(icon, &st) == 0) |
10066662176a
[gaim-migrate @ 13503]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
502 return g_strdup(icon); |
10066662176a
[gaim-migrate @ 13503]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
503 else |
10066662176a
[gaim-migrate @ 13503]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
504 return g_build_filename(gaim_buddy_icons_get_cache_dir(), icon, NULL); |
10066662176a
[gaim-migrate @ 13503]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
505 } |
10066662176a
[gaim-migrate @ 13503]
Richard Laager <rlaager@wiktel.com>
parents:
11256
diff
changeset
|
506 |
6846 | 507 void * |
508 gaim_buddy_icons_get_handle() | |
509 { | |
510 static int handle; | |
511 | |
512 return &handle; | |
513 } | |
514 | |
515 void | |
516 gaim_buddy_icons_init() | |
517 { | |
518 account_cache = g_hash_table_new_full( | |
519 g_direct_hash, g_direct_equal, | |
520 NULL, (GFreeFunc)g_hash_table_destroy); | |
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
521 |
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
522 cache_dir = g_build_filename(gaim_user_dir(), "icons", NULL); |
6846 | 523 } |
524 | |
525 void | |
526 gaim_buddy_icons_uninit() | |
527 { | |
528 g_hash_table_destroy(account_cache); | |
529 } | |
10483 | 530 |
531 void gaim_buddy_icon_get_scale_size(GaimBuddyIconSpec *spec, int *width, int *height) | |
532 { | |
533 if(spec && spec->scale_rules & GAIM_ICON_SCALE_DISPLAY) { | |
10523 | 534 int new_width, new_height; |
535 | |
536 new_width = *width; | |
537 new_height = *height; | |
538 | |
10483 | 539 if(*width < spec->min_width) |
10523 | 540 new_width = spec->min_width; |
10483 | 541 else if(*width > spec->max_width) |
10523 | 542 new_width = spec->max_width; |
10483 | 543 |
544 if(*height < spec->min_height) | |
10523 | 545 new_height = spec->min_height; |
10483 | 546 else if(*height > spec->max_height) |
10523 | 547 new_height = spec->max_height; |
548 | |
549 /* preserve aspect ratio */ | |
550 if ((double)*height * (double)new_width > | |
551 (double)*width * (double)new_height) { | |
552 new_width = 0.5 + (double)*width * (double)new_height / (double)*height; | |
553 } else { | |
554 new_height = 0.5 + (double)*height * (double)new_width / (double)*width; | |
555 } | |
556 | |
557 *width = new_width; | |
558 *height = new_height; | |
10483 | 559 } |
560 } | |
561 |