Thu, 01 Jul 2004 15:57:38 +0000
[gaim-migrate @ 10256]
this patch had some little discussion, so the original comments about it
don't make too much sense now.
it makes the log viewwer further collapse things into months for things
older than the current month. for relatively short logs this might not be
wonderful, but it should help with very long logs. see patch #963827
oh and thanks to Cole Kowalski for this
committer: Luke Schierer <lschiere@pidgin.im>
| 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
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
28 | #include "debug.h" |
|
97734a57c0f5
[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
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
32 | static char *cache_dir = NULL; |
|
97734a57c0f5
[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
cca08cb2fa3e
[gaim-migrate @ 7895]
Christian Hammond <chipx86@chipx86.com>
parents:
7125
diff
changeset
|
89 | GaimConversation *conv; |
|
cca08cb2fa3e
[gaim-migrate @ 7895]
Christian Hammond <chipx86@chipx86.com>
parents:
7125
diff
changeset
|
90 | GaimAccount *account; |
| 6846 | 91 | GHashTable *icon_cache; |
|
7311
cca08cb2fa3e
[gaim-migrate @ 7895]
Christian Hammond <chipx86@chipx86.com>
parents:
7125
diff
changeset
|
92 | const char *username; |
|
9305
63a1b462a208
[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 | ||
| 108 | conv = gaim_find_conversation_with_account(username, account); | |
| 109 | if (conv != NULL && gaim_conversation_get_type(conv) == GAIM_CONV_IM) | |
| 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
63a1b462a208
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
183 | for (list = sl = gaim_find_buddies(account, username); sl != NULL; |
|
63a1b462a208
[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 | ||
| 6846 | 193 | conv = gaim_find_conversation_with_account(username, account); |
| 194 | ||
| 195 | if (conv != NULL && gaim_conversation_get_type(conv) == GAIM_CONV_IM) | |
|
7118
280b3b85a28a
[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
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
200 | gaim_buddy_icon_cache(GaimBuddyIcon *icon, GaimBuddy *buddy) |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
201 | { |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
202 | const void *data; |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
203 | const char *dirname; |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
204 | char *random; |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
205 | char *filename; |
| 7125 | 206 | const char *old_icon; |
|
6886
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
207 | size_t len; |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
208 | FILE *file = NULL; |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
209 | |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
210 | g_return_if_fail(icon != NULL); |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
211 | g_return_if_fail(buddy != NULL); |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
212 | |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
213 | if (!gaim_buddy_icons_is_caching()) |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
214 | return; |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
215 | |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
216 | data = gaim_buddy_icon_get_data(icon, &len); |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
217 | |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
218 | random = g_strdup_printf("%x", g_random_int()); |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
219 | dirname = gaim_buddy_icons_get_cache_dir(); |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
220 | filename = g_build_filename(dirname, random, NULL); |
| 7721 | 221 | old_icon = gaim_blist_node_get_string((GaimBlistNode*)buddy, "buddy_icon"); |
|
6886
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
222 | |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
223 | g_free(random); |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
224 | |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
225 | if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
226 | { |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
227 | gaim_debug_info("buddy icons", "Creating icon cache directory.\n"); |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
228 | |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
229 | if (mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR) < 0) |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
230 | { |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
231 | gaim_debug_error("buddy icons", |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
232 | "Unable to create directory %s: %s\n", |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
233 | dirname, strerror(errno)); |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
234 | } |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
235 | } |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
236 | |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
237 | if ((file = fopen(filename, "wb")) != NULL) |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
238 | { |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
239 | fwrite(data, 1, len, file); |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
240 | fclose(file); |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
241 | } |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
242 | |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
243 | if (old_icon != NULL) |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
244 | unlink(old_icon); |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
245 | |
|
9285
9cedf5d26577
[gaim-migrate @ 10088]
Mark Doliner <markdoliner@pidgin.im>
parents:
8550
diff
changeset
|
246 | gaim_blist_node_set_string((GaimBlistNode *)buddy, "buddy_icon", filename); |
|
6886
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
247 | |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
248 | g_free(filename); |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
249 | } |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
250 | |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
251 | void |
| 6846 | 252 | gaim_buddy_icon_set_account(GaimBuddyIcon *icon, GaimAccount *account) |
| 253 | { | |
| 254 | g_return_if_fail(icon != NULL); | |
| 255 | g_return_if_fail(account != NULL); | |
| 256 | ||
| 257 | icon->account = account; | |
| 258 | } | |
| 259 | ||
| 260 | void | |
| 261 | gaim_buddy_icon_set_username(GaimBuddyIcon *icon, const char *username) | |
| 262 | { | |
| 263 | g_return_if_fail(icon != NULL); | |
| 264 | g_return_if_fail(username != NULL); | |
| 265 | ||
| 266 | if (icon->username != NULL) | |
| 267 | g_free(icon->username); | |
| 268 | ||
| 269 | icon->username = g_strdup(username); | |
| 270 | } | |
| 271 | ||
| 272 | void | |
| 273 | gaim_buddy_icon_set_data(GaimBuddyIcon *icon, void *data, size_t len) | |
| 274 | { | |
| 275 | g_return_if_fail(icon != NULL); | |
| 276 | ||
| 277 | if (icon->data != NULL) | |
| 278 | g_free(icon->data); | |
| 279 | ||
| 280 | if (data != NULL && len > 0) | |
| 281 | { | |
| 282 | icon->data = g_memdup(data, len); | |
| 283 | icon->len = len; | |
| 284 | } | |
| 285 | else | |
| 286 | { | |
| 287 | icon->data = NULL; | |
| 288 | icon->len = 0; | |
| 289 | } | |
| 290 | ||
| 291 | gaim_buddy_icon_update(icon); | |
| 292 | } | |
| 293 | ||
| 294 | GaimAccount * | |
| 295 | gaim_buddy_icon_get_account(const GaimBuddyIcon *icon) | |
| 296 | { | |
| 297 | g_return_val_if_fail(icon != NULL, NULL); | |
| 298 | ||
| 299 | return icon->account; | |
| 300 | } | |
| 301 | ||
| 302 | const char * | |
| 303 | gaim_buddy_icon_get_username(const GaimBuddyIcon *icon) | |
| 304 | { | |
| 305 | g_return_val_if_fail(icon != NULL, NULL); | |
| 306 | ||
| 307 | return icon->username; | |
| 308 | } | |
| 309 | ||
| 310 | const void * | |
| 311 | gaim_buddy_icon_get_data(const GaimBuddyIcon *icon, size_t *len) | |
| 312 | { | |
| 313 | g_return_val_if_fail(icon != NULL, NULL); | |
| 314 | ||
| 315 | if (len != NULL) | |
| 316 | *len = icon->len; | |
| 317 | ||
| 318 | return icon->data; | |
| 319 | } | |
| 320 | ||
| 321 | void | |
| 322 | gaim_buddy_icons_set_for_user(GaimAccount *account, const char *username, | |
| 323 | void *icon_data, size_t icon_len) | |
| 324 | { | |
| 325 | g_return_if_fail(account != NULL); | |
| 326 | g_return_if_fail(username != NULL); | |
| 327 | ||
|
9305
63a1b462a208
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
328 | if (icon_data == NULL || icon_len == 0) |
|
63a1b462a208
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
329 | { |
|
63a1b462a208
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
330 | GaimBuddyIcon *buddy_icon; |
|
63a1b462a208
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
331 | |
|
63a1b462a208
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
332 | buddy_icon = gaim_buddy_icons_find(account, username); |
|
63a1b462a208
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
333 | |
|
63a1b462a208
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
334 | if (buddy_icon != NULL) |
|
63a1b462a208
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
335 | gaim_buddy_icon_destroy(buddy_icon); |
|
63a1b462a208
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
336 | } |
|
63a1b462a208
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
337 | else |
|
63a1b462a208
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
338 | { |
|
63a1b462a208
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
339 | gaim_buddy_icon_new(account, username, icon_data, icon_len); |
|
63a1b462a208
[gaim-migrate @ 10113]
Christian Hammond <chipx86@chipx86.com>
parents:
9285
diff
changeset
|
340 | } |
| 6846 | 341 | } |
| 342 | ||
| 343 | GaimBuddyIcon * | |
| 9396 | 344 | gaim_buddy_icons_find(GaimAccount *account, const char *username) |
| 6846 | 345 | { |
| 346 | GHashTable *icon_cache; | |
| 9396 | 347 | GaimBuddyIcon *ret = NULL; |
| 6846 | 348 | |
| 349 | g_return_val_if_fail(account != NULL, NULL); | |
| 350 | g_return_val_if_fail(username != NULL, NULL); | |
| 351 | ||
| 352 | icon_cache = g_hash_table_lookup(account_cache, account); | |
| 353 | ||
| 9396 | 354 | if ((icon_cache == NULL) || ((ret = g_hash_table_lookup(icon_cache, username)) == NULL)) { |
| 355 | const char *file; | |
| 356 | struct stat st; | |
| 357 | GaimBuddy *b = gaim_find_buddy(account, username); | |
| 358 | ||
| 359 | if (!b) | |
| 360 | return NULL; | |
| 361 | ||
| 362 | if ((file = gaim_blist_node_get_string((GaimBlistNode*)b, "buddy_icon")) == NULL) | |
| 363 | return NULL; | |
| 6846 | 364 | |
| 9396 | 365 | if (!stat(file, &st)) { |
| 366 | FILE *f = fopen(file, "rb"); | |
| 367 | if (f) { | |
| 368 | char *data = g_malloc(st.st_size); | |
| 369 | fread(data, 1, st.st_size, f); | |
| 370 | fclose(f); | |
| 371 | ret = gaim_buddy_icon_create(account, username); | |
| 372 | gaim_buddy_icon_ref(ret); | |
| 373 | gaim_buddy_icon_set_data(ret, data, st.st_size); | |
| 374 | gaim_buddy_icon_unref(ret); | |
| 375 | g_free(data); | |
| 376 | return ret; | |
| 377 | } | |
| 378 | } | |
| 379 | } | |
| 380 | ||
| 381 | return ret; | |
| 6846 | 382 | } |
| 383 | ||
|
6886
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
384 | void |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
385 | gaim_buddy_icons_set_caching(gboolean caching) |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
386 | { |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
387 | icon_caching = caching; |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
388 | } |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
389 | |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
390 | gboolean |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
391 | gaim_buddy_icons_is_caching(void) |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
392 | { |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
393 | return icon_caching; |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
394 | } |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
395 | |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
396 | void |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
397 | gaim_buddy_icons_set_cache_dir(const char *dir) |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
398 | { |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
399 | g_return_if_fail(cache_dir != NULL); |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
400 | |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
401 | if (cache_dir != NULL) |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
402 | g_free(cache_dir); |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
403 | |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
404 | cache_dir = g_strdup(dir); |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
405 | } |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
406 | |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
407 | const char * |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
408 | gaim_buddy_icons_get_cache_dir(void) |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
409 | { |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
410 | return cache_dir; |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
411 | } |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
412 | |
| 6846 | 413 | void * |
| 414 | gaim_buddy_icons_get_handle() | |
| 415 | { | |
| 416 | static int handle; | |
| 417 | ||
| 418 | return &handle; | |
| 419 | } | |
| 420 | ||
| 421 | void | |
| 422 | gaim_buddy_icons_init() | |
| 423 | { | |
| 424 | account_cache = g_hash_table_new_full( | |
| 425 | g_direct_hash, g_direct_equal, | |
| 426 | NULL, (GFreeFunc)g_hash_table_destroy); | |
|
6886
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
427 | |
|
97734a57c0f5
[gaim-migrate @ 7432]
Christian Hammond <chipx86@chipx86.com>
parents:
6846
diff
changeset
|
428 | cache_dir = g_build_filename(gaim_user_dir(), "icons", NULL); |
| 6846 | 429 | } |
| 430 | ||
| 431 | void | |
| 432 | gaim_buddy_icons_uninit() | |
| 433 | { | |
| 434 | g_hash_table_destroy(account_cache); | |
| 435 | } |