Mercurial > pidgin
annotate src/buddyicon.c @ 10976:be22eb8fa671
[gaim-migrate @ 12802]
Fix encoding of filename
committer: Tailor Script <tailor@pidgin.im>
| author | Daniel Atallah <daniel.atallah@gmail.com> |
|---|---|
| date | Mon, 06 Jun 2005 22:38:11 +0000 |
| parents | e8e535ad720b |
| children | dd5c802baf7b |
| 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); |
| 9327 | 78 gaim_buddy_icon_unref(icon); |
| 6846 | 79 |
| 9327 | 80 /* We don't take a reference here. gaim_buddy_icon_set_data() makes blist.c or |
| 81 conversation.c, or both, do that for us. | |
| 9323 | 82 */ |
| 6846 | 83 return icon; |
| 84 } | |
| 85 | |
| 86 void | |
| 87 gaim_buddy_icon_destroy(GaimBuddyIcon *icon) | |
| 88 { | |
|
7311
1c8830db0189
[gaim-migrate @ 7895]
Christian Hammond <chipx86@chipx86.com>
parents:
7125
diff
changeset
|
89 GaimConversation *conv; |
|
1c8830db0189
[gaim-migrate @ 7895]
Christian Hammond <chipx86@chipx86.com>
parents:
7125
diff
changeset
|
90 GaimAccount *account; |
| 6846 | 91 GHashTable *icon_cache; |
|
7311
1c8830db0189
[gaim-migrate @ 7895]
Christian Hammond <chipx86@chipx86.com>
parents:
7125
diff
changeset
|
92 const char *username; |
|
9305
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
93 GSList *sl, *list; |
| 6846 | 94 |
| 95 g_return_if_fail(icon != NULL); | |
| 96 | |
| 97 if (icon->ref_count > 0) | |
| 98 { | |
| 9323 | 99 /* If the ref count is greater than 0, then we weren't called from |
| 100 * gaim_buddy_icon_unref(). So we go through and ask everyone to | |
| 101 * unref us. Then we return, since we know somewhere along the | |
| 102 * line we got called recursively by one of the unrefs, and the | |
| 103 * icon is already destroyed. | |
| 104 */ | |
| 105 account = gaim_buddy_icon_get_account(icon); | |
| 106 username = gaim_buddy_icon_get_username(icon); | |
| 107 | |
| 10246 | 108 conv = gaim_find_conversation_with_account(GAIM_CONV_IM, username, account); |
| 109 if (conv != NULL) | |
| 9323 | 110 gaim_conv_im_set_icon(GAIM_CONV_IM(conv), NULL); |
| 111 | |
| 112 for (list = sl = gaim_find_buddies(account, username); sl != NULL; | |
| 113 sl = sl->next) | |
| 114 { | |
| 115 GaimBuddy *buddy = (GaimBuddy *)sl->data; | |
| 116 | |
| 117 gaim_buddy_set_icon(buddy, NULL); | |
| 118 } | |
| 119 | |
| 120 g_slist_free(list); | |
| 6846 | 121 |
| 122 return; | |
| 123 } | |
| 124 | |
| 125 icon_cache = g_hash_table_lookup(account_cache, | |
| 126 gaim_buddy_icon_get_account(icon)); | |
| 127 | |
| 128 if (icon_cache != NULL) | |
| 129 g_hash_table_remove(icon_cache, gaim_buddy_icon_get_username(icon)); | |
| 130 | |
| 131 if (icon->username != NULL) | |
| 132 g_free(icon->username); | |
| 133 | |
| 134 if (icon->data != NULL) | |
| 135 g_free(icon->data); | |
| 136 | |
| 137 g_free(icon); | |
| 138 } | |
| 139 | |
| 140 GaimBuddyIcon * | |
| 141 gaim_buddy_icon_ref(GaimBuddyIcon *icon) | |
| 142 { | |
| 143 g_return_val_if_fail(icon != NULL, NULL); | |
| 144 | |
| 145 icon->ref_count++; | |
| 146 | |
| 147 return icon; | |
| 148 } | |
| 149 | |
| 150 GaimBuddyIcon * | |
| 151 gaim_buddy_icon_unref(GaimBuddyIcon *icon) | |
| 152 { | |
| 153 g_return_val_if_fail(icon != NULL, NULL); | |
| 154 | |
| 155 if (icon->ref_count <= 0) | |
| 156 return NULL; | |
| 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 | |
| 10246 | 193 conv = gaim_find_conversation_with_account(GAIM_CONV_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 | |
| 199 void | |
|
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
200 gaim_buddy_icon_cache(GaimBuddyIcon *icon, GaimBuddy *buddy) |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
201 { |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
202 const void *data; |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
203 const char *dirname; |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
204 char *random; |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
205 char *filename; |
| 7125 | 206 const char *old_icon; |
|
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
207 size_t len; |
| 9747 | 208 struct stat st; |
|
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
209 FILE *file = NULL; |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
210 |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
211 g_return_if_fail(icon != NULL); |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
212 g_return_if_fail(buddy != NULL); |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
213 |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
214 if (!gaim_buddy_icons_is_caching()) |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
215 return; |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
216 |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
217 data = gaim_buddy_icon_get_data(icon, &len); |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
218 |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
219 random = g_strdup_printf("%x", g_random_int()); |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
220 dirname = gaim_buddy_icons_get_cache_dir(); |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
221 filename = g_build_filename(dirname, random, NULL); |
| 7721 | 222 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
|
223 |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
224 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
225 { |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
226 gaim_debug_info("buddy icons", "Creating icon cache directory.\n"); |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
227 |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10523
diff
changeset
|
228 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
|
229 { |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
230 gaim_debug_error("buddy icons", |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
231 "Unable to create directory %s: %s\n", |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
232 dirname, strerror(errno)); |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
233 } |
|
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 |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10523
diff
changeset
|
236 if ((file = g_fopen(filename, "wb")) != NULL) |
|
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
237 { |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
238 fwrite(data, 1, len, file); |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
239 fclose(file); |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
240 } |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
241 |
| 10934 | 242 gaim_signal_emit(gaim_buddy_icons_get_handle(), "buddy-icon-cached", |
| 243 icon, buddy, filename, old_icon); | |
| 244 | |
| 9747 | 245 g_free(filename); |
|
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
246 |
| 9747 | 247 if (old_icon != NULL) |
| 248 { | |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10523
diff
changeset
|
249 if(!g_stat(old_icon, &st)) |
|
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10523
diff
changeset
|
250 g_unlink(old_icon); |
| 9747 | 251 else { |
| 9801 | 252 filename = g_build_filename(dirname, old_icon, NULL); |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10523
diff
changeset
|
253 if(!g_stat(filename, &st)) |
|
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10523
diff
changeset
|
254 g_unlink(filename); |
| 9747 | 255 g_free(filename); |
| 256 } | |
| 257 } | |
|
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
258 |
| 9747 | 259 gaim_blist_node_set_string((GaimBlistNode *)buddy, "buddy_icon", random); |
| 260 | |
| 261 g_free(random); | |
|
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
262 } |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
263 |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
264 void |
| 6846 | 265 gaim_buddy_icon_set_account(GaimBuddyIcon *icon, GaimAccount *account) |
| 266 { | |
| 267 g_return_if_fail(icon != NULL); | |
| 268 g_return_if_fail(account != NULL); | |
| 269 | |
| 270 icon->account = account; | |
| 271 } | |
| 272 | |
| 273 void | |
| 274 gaim_buddy_icon_set_username(GaimBuddyIcon *icon, const char *username) | |
| 275 { | |
| 276 g_return_if_fail(icon != NULL); | |
| 277 g_return_if_fail(username != NULL); | |
| 278 | |
| 279 if (icon->username != NULL) | |
| 280 g_free(icon->username); | |
| 281 | |
| 282 icon->username = g_strdup(username); | |
| 283 } | |
| 284 | |
| 285 void | |
| 286 gaim_buddy_icon_set_data(GaimBuddyIcon *icon, void *data, size_t len) | |
| 287 { | |
| 288 g_return_if_fail(icon != NULL); | |
| 289 | |
| 290 if (icon->data != NULL) | |
| 291 g_free(icon->data); | |
| 292 | |
| 293 if (data != NULL && len > 0) | |
| 294 { | |
| 295 icon->data = g_memdup(data, len); | |
| 296 icon->len = len; | |
| 297 } | |
| 298 else | |
| 299 { | |
| 300 icon->data = NULL; | |
| 301 icon->len = 0; | |
| 302 } | |
| 303 | |
| 304 gaim_buddy_icon_update(icon); | |
| 305 } | |
| 306 | |
| 307 GaimAccount * | |
| 308 gaim_buddy_icon_get_account(const GaimBuddyIcon *icon) | |
| 309 { | |
| 310 g_return_val_if_fail(icon != NULL, NULL); | |
| 311 | |
| 312 return icon->account; | |
| 313 } | |
| 314 | |
| 315 const char * | |
| 316 gaim_buddy_icon_get_username(const GaimBuddyIcon *icon) | |
| 317 { | |
| 318 g_return_val_if_fail(icon != NULL, NULL); | |
| 319 | |
| 320 return icon->username; | |
| 321 } | |
| 322 | |
| 323 const void * | |
| 324 gaim_buddy_icon_get_data(const GaimBuddyIcon *icon, size_t *len) | |
| 325 { | |
| 326 g_return_val_if_fail(icon != NULL, NULL); | |
| 327 | |
| 328 if (len != NULL) | |
| 329 *len = icon->len; | |
| 330 | |
| 331 return icon->data; | |
| 332 } | |
| 333 | |
| 10953 | 334 const char * |
| 335 gaim_buddy_icon_get_type(const GaimBuddyIcon *icon) | |
| 336 { | |
| 337 const void *data; | |
| 338 size_t len; | |
| 339 | |
| 340 g_return_val_if_fail(icon != NULL, NULL); | |
| 341 | |
| 342 data = gaim_buddy_icon_get_data(icon, &len); | |
| 343 | |
| 344 /* TODO: Find a way to do this with GDK */ | |
| 345 if (len >= 4) | |
| 346 { | |
| 347 if (!strncmp(data, "BM", 2)) | |
| 348 return "bmp"; | |
| 349 else if (!strncmp(data, "GIF8", 4)) | |
| 350 return "gif"; | |
| 351 else if (!strncmp(data, "\xff\xd8\xff\xe0", 4)) | |
| 352 return "jpg"; | |
| 353 else if (!strncmp(data, "\x89PNG", 4)) | |
| 354 return "png"; | |
| 355 } | |
| 356 | |
| 357 return NULL; | |
| 358 } | |
| 359 | |
| 6846 | 360 void |
| 361 gaim_buddy_icons_set_for_user(GaimAccount *account, const char *username, | |
| 362 void *icon_data, size_t icon_len) | |
| 363 { | |
| 364 g_return_if_fail(account != NULL); | |
| 365 g_return_if_fail(username != NULL); | |
| 366 | |
|
9305
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
367 if (icon_data == NULL || icon_len == 0) |
|
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
368 { |
|
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
369 GaimBuddyIcon *buddy_icon; |
|
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
370 |
|
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
371 buddy_icon = gaim_buddy_icons_find(account, username); |
|
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
372 |
|
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
373 if (buddy_icon != NULL) |
|
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
374 gaim_buddy_icon_destroy(buddy_icon); |
|
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
375 } |
|
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
376 else |
|
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
377 { |
|
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
378 gaim_buddy_icon_new(account, username, icon_data, icon_len); |
|
0c201a2386c7
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
379 } |
| 6846 | 380 } |
| 381 | |
| 382 GaimBuddyIcon * | |
| 9396 | 383 gaim_buddy_icons_find(GaimAccount *account, const char *username) |
| 6846 | 384 { |
| 385 GHashTable *icon_cache; | |
| 9396 | 386 GaimBuddyIcon *ret = NULL; |
| 9747 | 387 char *filename = NULL; |
| 6846 | 388 |
| 389 g_return_val_if_fail(account != NULL, NULL); | |
| 390 g_return_val_if_fail(username != NULL, NULL); | |
| 391 | |
| 392 icon_cache = g_hash_table_lookup(account_cache, account); | |
| 393 | |
| 9396 | 394 if ((icon_cache == NULL) || ((ret = g_hash_table_lookup(icon_cache, username)) == NULL)) { |
| 395 const char *file; | |
| 396 struct stat st; | |
| 397 GaimBuddy *b = gaim_find_buddy(account, username); | |
| 398 | |
| 399 if (!b) | |
| 400 return NULL; | |
| 401 | |
| 402 if ((file = gaim_blist_node_get_string((GaimBlistNode*)b, "buddy_icon")) == NULL) | |
| 403 return NULL; | |
| 6846 | 404 |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10523
diff
changeset
|
405 if (!g_stat(file, &st)) |
| 9747 | 406 filename = g_strdup(file); |
| 407 else | |
| 408 filename = g_build_filename(gaim_buddy_icons_get_cache_dir(), file, NULL); | |
| 409 | |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10523
diff
changeset
|
410 if (!g_stat(filename, &st)) { |
|
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10523
diff
changeset
|
411 FILE *f = g_fopen(filename, "rb"); |
| 9396 | 412 if (f) { |
| 413 char *data = g_malloc(st.st_size); | |
| 414 fread(data, 1, st.st_size, f); | |
| 415 fclose(f); | |
| 416 ret = gaim_buddy_icon_create(account, username); | |
| 417 gaim_buddy_icon_ref(ret); | |
| 418 gaim_buddy_icon_set_data(ret, data, st.st_size); | |
| 419 gaim_buddy_icon_unref(ret); | |
| 420 g_free(data); | |
| 9747 | 421 g_free(filename); |
| 9396 | 422 return ret; |
| 423 } | |
| 424 } | |
| 9747 | 425 g_free(filename); |
| 9396 | 426 } |
| 427 | |
| 428 return ret; | |
| 6846 | 429 } |
| 430 | |
|
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
431 void |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
432 gaim_buddy_icons_set_caching(gboolean caching) |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
433 { |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
434 icon_caching = caching; |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
435 } |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
436 |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
437 gboolean |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
438 gaim_buddy_icons_is_caching(void) |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
439 { |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
440 return icon_caching; |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
441 } |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
442 |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
443 void |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
444 gaim_buddy_icons_set_cache_dir(const char *dir) |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
445 { |
|
10811
dc59482c8d37
[gaim-migrate @ 12464]
Luke Schierer <lschiere@pidgin.im>
parents:
10589
diff
changeset
|
446 g_return_if_fail(dir != NULL); |
|
6886
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
447 |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
448 if (cache_dir != NULL) |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
449 g_free(cache_dir); |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
450 |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
451 cache_dir = g_strdup(dir); |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
452 } |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
453 |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
454 const char * |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
455 gaim_buddy_icons_get_cache_dir(void) |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
456 { |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
457 return cache_dir; |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
458 } |
|
b5fb1d5282e5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
459 |
| 6846 | 460 void * |
| 461 gaim_buddy_icons_get_handle() | |
| 462 { | |
| 463 static int handle; | |
| 464 | |
| 465 return &handle; | |
| 466 } | |
| 467 | |
| 468 void | |
| 469 gaim_buddy_icons_init() | |
| 470 { | |
| 471 account_cache = g_hash_table_new_full( | |
| 472 g_direct_hash, g_direct_equal, | |
| 473 NULL, (GFreeFunc)g_hash_table_destroy); | |
|
6886
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 cache_dir = g_build_filename(gaim_user_dir(), "icons", NULL); |
| 10934 | 476 |
| 477 gaim_signal_register(gaim_buddy_icons_get_handle(), "buddy-icon-cached", | |
| 478 gaim_marshal_VOID__POINTER_POINTER_POINTER_POINTER, NULL, 4, | |
| 479 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
| 480 GAIM_SUBTYPE_BUDDY_ICON), | |
| 481 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
| 482 GAIM_SUBTYPE_BLIST_BUDDY), | |
| 483 gaim_value_new(GAIM_TYPE_STRING), | |
| 484 gaim_value_new(GAIM_TYPE_STRING)); | |
| 6846 | 485 } |
| 486 | |
| 487 void | |
| 488 gaim_buddy_icons_uninit() | |
| 489 { | |
| 490 g_hash_table_destroy(account_cache); | |
| 491 } | |
| 10483 | 492 |
| 493 void gaim_buddy_icon_get_scale_size(GaimBuddyIconSpec *spec, int *width, int *height) | |
| 494 { | |
| 495 if(spec && spec->scale_rules & GAIM_ICON_SCALE_DISPLAY) { | |
| 10523 | 496 int new_width, new_height; |
| 497 | |
| 498 new_width = *width; | |
| 499 new_height = *height; | |
| 500 | |
| 10483 | 501 if(*width < spec->min_width) |
| 10523 | 502 new_width = spec->min_width; |
| 10483 | 503 else if(*width > spec->max_width) |
| 10523 | 504 new_width = spec->max_width; |
| 10483 | 505 |
| 506 if(*height < spec->min_height) | |
| 10523 | 507 new_height = spec->min_height; |
| 10483 | 508 else if(*height > spec->max_height) |
| 10523 | 509 new_height = spec->max_height; |
| 510 | |
| 511 /* preserve aspect ratio */ | |
| 512 if ((double)*height * (double)new_width > | |
| 513 (double)*width * (double)new_height) { | |
| 514 new_width = 0.5 + (double)*width * (double)new_height / (double)*height; | |
| 515 } else { | |
| 516 new_height = 0.5 + (double)*height * (double)new_width / (double)*width; | |
| 517 } | |
| 518 | |
| 519 *width = new_width; | |
| 520 *height = new_height; | |
| 10483 | 521 } |
| 522 } | |
| 523 |
