3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include "buddyicon.h"
24 #include "conversation.h"
29 /* NOTE: Instances of this struct are allocated without zeroing the memory, so
30 * NOTE: be sure to update purple_buddy_icon_new() if you add members. */
31 struct _PurpleBuddyIcon
33 PurpleAccount
*account
; /* The account the user is on. */
34 PurpleImage
*img
; /* The image containing
36 char *username
; /* The username the icon belongs to. */
37 char *checksum
; /* The protocol checksum. */
38 unsigned int ref_count
; /* The buddy icon reference count. */
42 * This is the big grand daddy hash table that contains references to
43 * everybody's buddy icons.
45 * Key is a PurpleAccount.
46 * Value is another hash table, usually referred to as "icon_cache."
47 * For this inner hash table:
48 * Key is the username of the buddy whose icon is being stored.
49 * Value is the PurpleBuddyIcon for this buddy.
51 static GHashTable
*account_cache
= NULL
;
54 * This hash table contains a bunch of PurpleImages that are
55 * shared across all accounts.
57 * Key is the filename for this image as constructed by
58 * purple_image_generate_filename(). So it is the base16 encoded
59 * sha-1 hash plus an appropriate file extension. For example:
60 * "0f4972d17d1e70e751c43c90c948e72efbff9796.gif"
62 * The value is a PurpleImage containing the icon data. These images are
63 * reference counted, and when the count reaches 0 we remove the image from
64 * the hash table (but it might still be saved on disk, if the icon is being
65 * used by offline accounts or some such).
67 static GHashTable
*icon_data_cache
= NULL
;
70 * This hash table contains reference counts for how many times each
71 * icon in the ~/.purple/icons/ directory is being used. It's pretty
72 * crazy. It maintains the reference count across sessions, too, so
73 * if you exit Pidgin then this hash table is reconstructed the next
76 * Key is the filename for this image as constructed by
77 * purple_image_generate_filename(). So it is the base16 encoded
78 * sha-1 hash plus an appropriate file extension. For example:
79 * "0f4972d17d1e70e751c43c90c948e72efbff9796.gif"
81 * The value is a GINT_TO_POINTER count of the number of times this
82 * icon is used. So if four of your buddies are using an icon, and
83 * you have the icon set for two of your accounts, then this number
84 * will be six. When this reference count reaches 0 the icon will
85 * be deleted from disk.
87 static GHashTable
*icon_file_cache
= NULL
;
90 * This hash table is used for both custom buddy icons on PurpleBlistNodes and
93 static GHashTable
*pointer_icon_cache
= NULL
;
95 static char *cache_dir
= NULL
;
97 /* "Should icons be cached to disk?" */
98 static gboolean icon_caching
= TRUE
;
100 static void delete_buddy_icon_settings(PurpleBlistNode
*node
, const char *setting_name
);
103 * Begin functions for dealing with the on-disk icon cache
107 ref_filename(const char *filename
)
111 g_return_if_fail(filename
!= NULL
);
113 refs
= GPOINTER_TO_INT(g_hash_table_lookup(icon_file_cache
, filename
));
115 g_hash_table_insert(icon_file_cache
, g_strdup(filename
),
116 GINT_TO_POINTER(refs
+ 1));
120 unref_filename(const char *filename
)
124 if (filename
== NULL
)
127 refs
= GPOINTER_TO_INT(g_hash_table_lookup(icon_file_cache
, filename
));
131 g_hash_table_remove(icon_file_cache
, filename
);
135 g_hash_table_insert(icon_file_cache
, g_strdup(filename
),
136 GINT_TO_POINTER(refs
- 1));
141 image_get_filename(PurpleImage
*img
)
143 return g_object_get_data(G_OBJECT(img
), "purple-buddyicon-filename");
147 purple_buddy_icon_data_cache(PurpleImage
*img
)
149 const gchar
*dirname
, *filename
;
152 g_return_if_fail(PURPLE_IS_IMAGE(img
));
154 if (!purple_buddy_icons_is_caching())
157 dirname
= purple_buddy_icons_get_cache_dir();
158 filename
= image_get_filename(img
);
159 g_return_if_fail(filename
!= NULL
);
161 if (!g_file_test(dirname
, G_FILE_TEST_IS_DIR
))
163 purple_debug_info("buddyicon", "creating icon cache directory");
165 if (g_mkdir(dirname
, S_IRUSR
| S_IWUSR
| S_IXUSR
) < 0)
167 purple_debug_error("buddyicon",
168 "unable to create directory %s: %s",
169 dirname
, g_strerror(errno
));
174 path
= g_build_filename(dirname
, filename
, NULL
);
175 if (!purple_image_save(img
, path
))
176 purple_debug_error("buddyicon", "failed to save icon %s", path
);
182 purple_buddy_icon_data_uncache_file(const char *filename
)
187 g_return_if_fail(filename
!= NULL
);
189 /* It's possible that there are other references to this icon
190 * cache file that are not currently loaded into memory. */
191 if (GPOINTER_TO_INT(g_hash_table_lookup(icon_file_cache
, filename
)))
194 dirname
= purple_buddy_icons_get_cache_dir();
195 path
= g_build_filename(dirname
, filename
, NULL
);
197 if (g_file_test(path
, G_FILE_TEST_EXISTS
))
201 purple_debug_error("buddyicon", "Failed to delete %s: %s\n",
202 path
, g_strerror(errno
));
206 purple_debug_info("buddyicon", "Deleted cache file: %s\n", path
);
214 * End functions for dealing with the on-disk icon cache
218 * Begin functions for dealing with the in-memory icon cache
222 value_equals(gpointer key
, gpointer value
, gpointer user_data
)
224 return (value
== user_data
);
228 image_deleting_cb(gpointer _filename
)
231 gchar
*filename
= _filename
;
233 img
= g_hash_table_lookup(icon_data_cache
, filename
);
234 purple_buddy_icon_data_uncache_file(filename
);
235 g_hash_table_remove(icon_data_cache
, filename
);
237 /* We could make this O(1) by using another hash table, but
238 * this is probably good enough. */
239 g_hash_table_foreach_remove(pointer_icon_cache
, value_equals
, (gpointer
)img
);
245 purple_buddy_icon_data_new(guchar
*icon_data
, size_t icon_len
)
247 PurpleImage
*newimg
, *oldimg
;
248 const gchar
*filename
;
250 g_return_val_if_fail(icon_data
!= NULL
, NULL
);
251 g_return_val_if_fail(icon_len
> 0, NULL
);
253 newimg
= purple_image_new_from_data(icon_data
, icon_len
);
254 filename
= purple_image_generate_filename(newimg
);
256 /* TODO: Why is this function called for buddies without icons? If this is
257 * intended, should the filename be null?
259 if (filename
!= NULL
) {
260 oldimg
= g_hash_table_lookup(icon_data_cache
, filename
);
262 g_warn_if_fail(PURPLE_IS_IMAGE(oldimg
));
263 g_object_unref(newimg
);
264 g_object_ref(oldimg
);
268 /* This will take ownership of file and free it as needed */
269 g_hash_table_insert(icon_data_cache
, g_strdup(filename
), newimg
);
272 g_object_set_data_full(G_OBJECT(newimg
), "purple-buddyicon-filename",
273 g_strdup(filename
), image_deleting_cb
);
275 purple_buddy_icon_data_cache(newimg
);
281 * End functions for dealing with the in-memory icon cache
284 static PurpleBuddyIcon
*
285 purple_buddy_icon_create(PurpleAccount
*account
, const char *username
)
287 PurpleBuddyIcon
*icon
;
288 GHashTable
*icon_cache
;
290 /* This does not zero. See purple_buddy_icon_new() for
291 * information on which function allocates which member. */
292 icon
= g_slice_new(PurpleBuddyIcon
);
294 icon
->account
= account
;
295 icon
->username
= g_strdup(username
);
296 icon
->checksum
= NULL
;
299 icon_cache
= g_hash_table_lookup(account_cache
, account
);
301 if (icon_cache
== NULL
)
303 icon_cache
= g_hash_table_new(g_str_hash
, g_str_equal
);
305 g_hash_table_insert(account_cache
, account
, icon_cache
);
308 g_hash_table_insert(icon_cache
,
309 (char *)purple_buddy_icon_get_username(icon
), icon
);
314 purple_buddy_icon_new(PurpleAccount
*account
, const char *username
,
315 void *icon_data
, size_t icon_len
,
316 const char *checksum
)
318 PurpleBuddyIcon
*icon
;
320 g_return_val_if_fail(account
!= NULL
, NULL
);
321 g_return_val_if_fail(username
!= NULL
, NULL
);
322 g_return_val_if_fail(icon_data
!= NULL
, NULL
);
323 g_return_val_if_fail(icon_len
> 0, NULL
);
325 /* purple_buddy_icons_find() does allocation, so be
326 * sure to update it as well when members are added. */
327 icon
= purple_buddy_icons_find(account
, username
);
329 /* purple_buddy_icon_create() sets account & username */
331 icon
= purple_buddy_icon_create(account
, username
);
333 /* purple_buddy_icon_set_data() sets img, but it
334 * references img first, so we need to initialize it */
336 purple_buddy_icon_set_data(icon
, icon_data
, icon_len
, checksum
);
342 purple_buddy_icon_ref(PurpleBuddyIcon
*icon
)
344 g_return_val_if_fail(icon
!= NULL
, NULL
);
352 purple_buddy_icon_unref(PurpleBuddyIcon
*icon
)
357 g_return_if_fail(icon
->ref_count
> 0);
361 if (icon
->ref_count
== 0)
363 GHashTable
*icon_cache
= g_hash_table_lookup(account_cache
, purple_buddy_icon_get_account(icon
));
365 if (icon_cache
!= NULL
)
366 g_hash_table_remove(icon_cache
, purple_buddy_icon_get_username(icon
));
368 g_free(icon
->username
);
369 g_free(icon
->checksum
);
370 g_object_unref(icon
->img
);
372 g_slice_free(PurpleBuddyIcon
, icon
);
377 purple_buddy_icon_update(PurpleBuddyIcon
*icon
)
379 PurpleIMConversation
*im
;
380 PurpleAccount
*account
;
381 const char *username
;
382 PurpleBuddyIcon
*icon_to_set
;
385 g_return_if_fail(icon
!= NULL
);
387 account
= purple_buddy_icon_get_account(icon
);
388 username
= purple_buddy_icon_get_username(icon
);
390 /* If no data exists (icon->img == NULL), then call the functions below
391 * with NULL to unset the icon. They will then unref the icon and it should
392 * be destroyed. The only way it wouldn't be destroyed is if someone
393 * else is holding a reference to it, in which case they can kill
394 * the icon when they realize it has no data. */
395 icon_to_set
= icon
->img
? icon
: NULL
;
397 /* Ensure that icon remains valid throughout */
398 purple_buddy_icon_ref(icon
);
400 buddies
= purple_blist_find_buddies(account
, username
);
401 while (buddies
!= NULL
)
403 PurpleBuddy
*buddy
= (PurpleBuddy
*)buddies
->data
;
406 purple_buddy_set_icon(buddy
, icon_to_set
);
407 old_icon
= g_strdup(purple_blist_node_get_string((PurpleBlistNode
*)buddy
,
409 if (icon
->img
&& purple_buddy_icons_is_caching())
411 const char *filename
= image_get_filename(icon
->img
);
412 g_warn_if_fail(filename
!= NULL
);
413 purple_blist_node_set_string((PurpleBlistNode
*)buddy
,
417 if (icon
->checksum
&& *icon
->checksum
)
419 purple_blist_node_set_string((PurpleBlistNode
*)buddy
,
425 purple_blist_node_remove_setting((PurpleBlistNode
*)buddy
,
428 ref_filename(filename
);
432 purple_blist_node_remove_setting((PurpleBlistNode
*)buddy
, "buddy_icon");
433 purple_blist_node_remove_setting((PurpleBlistNode
*)buddy
, "icon_checksum");
435 unref_filename(old_icon
);
438 buddies
= g_slist_delete_link(buddies
, buddies
);
441 im
= purple_conversations_find_im_with_account(username
, account
);
444 purple_im_conversation_set_icon(im
, icon_to_set
);
446 /* icon's refcount was incremented above */
447 purple_buddy_icon_unref(icon
);
451 purple_buddy_icon_set_data(PurpleBuddyIcon
*icon
, guchar
*data
,
452 size_t len
, const char *checksum
)
454 PurpleImage
*old_img
;
456 g_return_if_fail(icon
!= NULL
);
464 icon
->img
= purple_buddy_icon_data_new(data
, len
);
469 g_free(icon
->checksum
);
470 icon
->checksum
= g_strdup(checksum
);
472 purple_buddy_icon_update(icon
);
475 g_object_unref(old_img
);
479 purple_buddy_icon_get_account(const PurpleBuddyIcon
*icon
)
481 g_return_val_if_fail(icon
!= NULL
, NULL
);
483 return icon
->account
;
487 purple_buddy_icon_get_username(const PurpleBuddyIcon
*icon
)
489 g_return_val_if_fail(icon
!= NULL
, NULL
);
491 return icon
->username
;
495 purple_buddy_icon_get_checksum(const PurpleBuddyIcon
*icon
)
497 g_return_val_if_fail(icon
!= NULL
, NULL
);
499 return icon
->checksum
;
503 purple_buddy_icon_get_data(const PurpleBuddyIcon
*icon
, size_t *len
)
505 g_return_val_if_fail(icon
!= NULL
, NULL
);
510 *len
= purple_image_get_data_size(icon
->img
);
512 return purple_image_get_data(icon
->img
);
519 purple_buddy_icon_get_extension(const PurpleBuddyIcon
*icon
)
521 if (icon
->img
!= NULL
)
522 return purple_image_get_extension(icon
->img
);
528 purple_buddy_icons_set_for_user(PurpleAccount
*account
, const char *username
,
529 void *icon_data
, size_t icon_len
,
530 const char *checksum
)
532 GHashTable
*icon_cache
;
533 PurpleBuddyIcon
*icon
= NULL
;
535 g_return_if_fail(account
!= NULL
);
536 g_return_if_fail(username
!= NULL
);
538 icon_cache
= g_hash_table_lookup(account_cache
, account
);
540 if (icon_cache
!= NULL
)
541 icon
= g_hash_table_lookup(icon_cache
, username
);
544 purple_buddy_icon_set_data(icon
, icon_data
, icon_len
, checksum
);
545 else if (icon_data
&& icon_len
> 0)
547 PurpleBuddyIcon
*icon
= purple_buddy_icon_new(account
, username
, icon_data
, icon_len
, checksum
);
549 /* purple_buddy_icon_new() calls
550 * purple_buddy_icon_set_data(), which calls
551 * purple_buddy_icon_update(), which has the buddy list
552 * and conversations take references as appropriate.
553 * This function doesn't return icon, so we can't
554 * leave a reference dangling. */
555 purple_buddy_icon_unref(icon
);
559 /* If the buddy list or a conversation was holding a
560 * reference, we'd have found the icon in the cache.
561 * Since we know we're deleting the icon, we only
562 * need a subset of purple_buddy_icon_update(). */
564 GSList
*buddies
= purple_blist_find_buddies(account
, username
);
565 while (buddies
!= NULL
)
567 PurpleBuddy
*buddy
= (PurpleBuddy
*)buddies
->data
;
569 unref_filename(purple_blist_node_get_string((PurpleBlistNode
*)buddy
, "buddy_icon"));
570 purple_blist_node_remove_setting((PurpleBlistNode
*)buddy
, "buddy_icon");
571 purple_blist_node_remove_setting((PurpleBlistNode
*)buddy
, "icon_checksum");
573 buddies
= g_slist_delete_link(buddies
, buddies
);
579 purple_buddy_icon_get_full_path(PurpleBuddyIcon
*icon
)
583 g_return_val_if_fail(icon
!= NULL
, NULL
);
585 if (icon
->img
== NULL
)
588 path
= purple_image_get_path(icon
->img
);
589 if (!g_file_test(path
, G_FILE_TEST_EXISTS
))
597 purple_buddy_icons_get_checksum_for_user(PurpleBuddy
*buddy
)
599 return purple_blist_node_get_string((PurpleBlistNode
*)buddy
,
604 read_icon_file(const char *path
, guchar
**data
, size_t *len
)
608 if (!g_file_get_contents(path
, (gchar
**)data
, len
, &err
))
610 purple_debug_error("buddyicon", "Error reading %s: %s\n",
621 purple_buddy_icons_find(PurpleAccount
*account
, const char *username
)
623 GHashTable
*icon_cache
;
624 PurpleBuddyIcon
*icon
= NULL
;
626 g_return_val_if_fail(account
!= NULL
, NULL
);
627 g_return_val_if_fail(username
!= NULL
, NULL
);
629 icon_cache
= g_hash_table_lookup(account_cache
, account
);
631 if ((icon_cache
== NULL
) || ((icon
= g_hash_table_lookup(icon_cache
, username
)) == NULL
))
633 /* The icon is not currently cached in memory--try reading from disk */
634 PurpleBuddy
*b
= purple_blist_find_buddy(account
, username
);
635 const char *protocol_icon_file
;
645 protocol_icon_file
= purple_blist_node_get_string((PurpleBlistNode
*)b
, "buddy_icon");
647 if (protocol_icon_file
== NULL
)
650 dirname
= purple_buddy_icons_get_cache_dir();
652 caching
= purple_buddy_icons_is_caching();
653 /* By disabling caching temporarily, we avoid a loop
654 * and don't have to add special code through several
656 purple_buddy_icons_set_caching(FALSE
);
658 path
= g_build_filename(dirname
, protocol_icon_file
, NULL
);
659 if (read_icon_file(path
, &data
, &len
)) {
660 const char *checksum
;
662 icon
= purple_buddy_icon_create(account
, username
);
664 checksum
= purple_blist_node_get_string((PurpleBlistNode
*)b
,
666 purple_buddy_icon_set_data(icon
, data
, len
, checksum
);
668 delete_buddy_icon_settings((PurpleBlistNode
*)b
, "buddy_icon");
673 purple_buddy_icons_set_caching(caching
);
676 return (icon
? purple_buddy_icon_ref(icon
) : NULL
);
680 purple_buddy_icons_find_account_icon(PurpleAccount
*account
)
683 const char *account_icon_file
;
689 g_return_val_if_fail(account
!= NULL
, NULL
);
691 img
= g_hash_table_lookup(pointer_icon_cache
, account
);
697 account_icon_file
= purple_account_get_string(account
, "buddy_icon", NULL
);
699 if (account_icon_file
== NULL
)
702 dirname
= purple_buddy_icons_get_cache_dir();
703 path
= g_build_filename(dirname
, account_icon_file
, NULL
);
705 if (read_icon_file(path
, &data
, &len
)) {
707 img
= purple_buddy_icons_set_account_icon(account
, data
, len
);
717 purple_buddy_icons_set_account_icon(PurpleAccount
*account
,
718 guchar
*icon_data
, size_t icon_len
)
720 PurpleImage
*old_img
;
721 PurpleImage
*img
= NULL
;
724 if (icon_data
!= NULL
&& icon_len
> 0) {
725 img
= purple_buddy_icon_data_new(icon_data
, icon_len
);
728 old_icon
= g_strdup(purple_account_get_string(account
, "buddy_icon", NULL
));
729 if (img
&& purple_buddy_icons_is_caching())
731 const char *filename
= image_get_filename(img
);
732 g_warn_if_fail(filename
!= NULL
);
733 purple_account_set_string(account
, "buddy_icon", filename
);
734 purple_account_set_int(account
, "buddy_icon_timestamp", time(NULL
));
735 ref_filename(filename
);
739 purple_account_set_string(account
, "buddy_icon", NULL
);
740 purple_account_set_int(account
, "buddy_icon_timestamp", 0);
742 unref_filename(old_icon
);
744 old_img
= g_hash_table_lookup(pointer_icon_cache
, account
);
747 g_hash_table_insert(pointer_icon_cache
, account
, img
);
749 g_hash_table_remove(pointer_icon_cache
, account
);
751 if (!purple_account_is_disconnected(account
))
753 PurpleConnection
*gc
;
754 PurpleProtocol
*protocol
;
756 gc
= purple_account_get_connection(account
);
757 protocol
= purple_connection_get_protocol(gc
);
760 purple_protocol_server_iface_set_buddy_icon(protocol
, gc
, img
);
764 g_object_unref(old_img
);
767 /* The old icon may not have been loaded into memory. In that
768 * case, we'll need to uncache the filename. The filenames
769 * are ref-counted, so this is safe. */
770 purple_buddy_icon_data_uncache_file(old_icon
);
778 purple_buddy_icons_get_account_icon_timestamp(PurpleAccount
*account
)
782 g_return_val_if_fail(account
!= NULL
, 0);
784 ret
= purple_account_get_int(account
, "buddy_icon_timestamp", 0);
786 /* This deals with migration cases. */
787 if (ret
== 0 && purple_account_get_string(account
, "buddy_icon", NULL
) != NULL
)
790 purple_account_set_int(account
, "buddy_icon_timestamp", ret
);
797 purple_buddy_icons_node_has_custom_icon(PurpleBlistNode
*node
)
799 g_return_val_if_fail(node
!= NULL
, FALSE
);
801 return (purple_blist_node_get_string(node
, "custom_buddy_icon") != NULL
);
805 purple_buddy_icons_node_find_custom_icon(PurpleBlistNode
*node
)
811 const char *custom_icon_file
, *dirname
;
813 g_return_val_if_fail(node
!= NULL
, NULL
);
815 img
= g_hash_table_lookup(pointer_icon_cache
, node
);
821 custom_icon_file
= purple_blist_node_get_string(node
,
822 "custom_buddy_icon");
824 if (custom_icon_file
== NULL
)
827 dirname
= purple_buddy_icons_get_cache_dir();
828 path
= g_build_filename(dirname
, custom_icon_file
, NULL
);
830 if (read_icon_file(path
, &data
, &len
)) {
832 img
= purple_buddy_icons_node_set_custom_icon(node
, data
, len
);
842 purple_buddy_icons_node_set_custom_icon(PurpleBlistNode
*node
,
843 guchar
*icon_data
, size_t icon_len
)
846 PurpleImage
*old_img
;
847 PurpleImage
*img
= NULL
;
849 g_return_val_if_fail(node
!= NULL
, NULL
);
851 if (!PURPLE_IS_CONTACT(node
) &&
852 !PURPLE_IS_CHAT(node
) &&
853 !PURPLE_IS_GROUP(node
)) {
857 old_img
= g_hash_table_lookup(pointer_icon_cache
, node
);
859 if (icon_data
!= NULL
&& icon_len
> 0) {
860 img
= purple_buddy_icon_data_new(icon_data
, icon_len
);
863 old_icon
= g_strdup(purple_blist_node_get_string(node
,
864 "custom_buddy_icon"));
865 if (img
&& purple_buddy_icons_is_caching()) {
866 const char *filename
= image_get_filename(img
);
867 g_warn_if_fail(filename
);
868 purple_blist_node_set_string(node
, "custom_buddy_icon",
870 ref_filename(filename
);
872 purple_blist_node_remove_setting(node
, "custom_buddy_icon");
874 unref_filename(old_icon
);
877 g_hash_table_insert(pointer_icon_cache
, node
, img
);
879 g_hash_table_remove(pointer_icon_cache
, node
);
881 if (PURPLE_IS_CONTACT(node
)) {
882 PurpleBlistNode
*child
;
883 for (child
= purple_blist_node_get_first_child(node
);
885 child
= purple_blist_node_get_sibling_next(child
))
888 PurpleIMConversation
*im
;
890 if (!PURPLE_IS_BUDDY(child
))
893 buddy
= (PurpleBuddy
*)child
;
895 im
= purple_conversations_find_im_with_account(purple_buddy_get_name(buddy
), purple_buddy_get_account(buddy
));
897 purple_conversation_update(PURPLE_CONVERSATION(im
), PURPLE_CONVERSATION_UPDATE_ICON
);
899 /* Is this call necessary anymore? Can the buddies
900 * themselves need updating when the custom buddy
902 purple_blist_update_node(purple_blist_get_default(),
903 PURPLE_BLIST_NODE(buddy
));
905 } else if (PURPLE_IS_CHAT(node
)) {
906 PurpleChatConversation
*chat
= NULL
;
908 chat
= purple_conversations_find_chat_with_account(purple_chat_get_name((PurpleChat
*)node
), purple_chat_get_account((PurpleChat
*)node
));
910 purple_conversation_update(PURPLE_CONVERSATION(chat
), PURPLE_CONVERSATION_UPDATE_ICON
);
914 purple_blist_update_node(purple_blist_get_default(), node
);
917 g_object_unref(old_img
);
918 } else if (old_icon
) {
919 /* The old icon may not have been loaded into memory. In that
920 * case, we'll need to uncache the filename. The filenames
921 * are ref-counted, so this is safe. */
922 purple_buddy_icon_data_uncache_file(old_icon
);
930 purple_buddy_icons_node_set_custom_icon_from_file(PurpleBlistNode
*node
,
931 const gchar
*filename
)
936 g_return_val_if_fail(node
!= NULL
, NULL
);
938 if (!PURPLE_IS_CONTACT(node
) &&
939 !PURPLE_IS_CHAT(node
) &&
940 !PURPLE_IS_GROUP(node
)) {
944 if (filename
!= NULL
) {
945 if (!read_icon_file(filename
, &data
, &len
)) {
950 return purple_buddy_icons_node_set_custom_icon(node
, data
, len
);
954 delete_buddy_icon_settings(PurpleBlistNode
*node
, const char *setting_name
)
956 purple_blist_node_remove_setting(node
, setting_name
);
958 if (purple_strequal(setting_name
, "buddy_icon"))
960 purple_blist_node_remove_setting(node
, "avatar_hash");
961 purple_blist_node_remove_setting(node
, "icon_checksum");
966 _purple_buddy_icons_account_loaded_cb()
968 const char *dirname
= purple_buddy_icons_get_cache_dir();
971 for (cur
= purple_accounts_get_all(); cur
!= NULL
; cur
= cur
->next
)
973 PurpleAccount
*account
= cur
->data
;
974 const char *account_icon_file
= purple_account_get_string(account
, "buddy_icon", NULL
);
976 if (account_icon_file
!= NULL
)
978 char *path
= g_build_filename(dirname
, account_icon_file
, NULL
);
979 if (!g_file_test(path
, G_FILE_TEST_EXISTS
))
981 purple_account_set_string(account
, "buddy_icon", NULL
);
983 ref_filename(account_icon_file
);
991 _purple_buddy_icons_blist_loaded_cb()
993 PurpleBlistNode
*node
= purple_blist_get_default_root();
994 const char *dirname
= purple_buddy_icons_get_cache_dir();
998 if (PURPLE_IS_BUDDY(node
))
1000 const char *filename
;
1002 filename
= purple_blist_node_get_string(node
, "buddy_icon");
1003 if (filename
!= NULL
)
1005 char *path
= g_build_filename(dirname
, filename
, NULL
);
1006 if (!g_file_test(path
, G_FILE_TEST_EXISTS
))
1008 purple_blist_node_remove_setting(node
,
1010 purple_blist_node_remove_setting(node
,
1014 ref_filename(filename
);
1018 else if (PURPLE_IS_CONTACT(node
) ||
1019 PURPLE_IS_CHAT(node
) ||
1020 PURPLE_IS_GROUP(node
))
1022 const char *filename
;
1024 filename
= purple_blist_node_get_string(node
, "custom_buddy_icon");
1025 if (filename
!= NULL
)
1027 char *path
= g_build_filename(dirname
, filename
, NULL
);
1028 if (!g_file_test(path
, G_FILE_TEST_EXISTS
))
1030 purple_blist_node_remove_setting(node
,
1031 "custom_buddy_icon");
1034 ref_filename(filename
);
1038 node
= purple_blist_node_next(node
, TRUE
);
1043 purple_buddy_icons_set_caching(gboolean caching
)
1045 icon_caching
= caching
;
1049 purple_buddy_icons_is_caching(void)
1051 return icon_caching
;
1055 purple_buddy_icons_set_cache_dir(const char *dir
)
1057 g_return_if_fail(dir
!= NULL
);
1060 cache_dir
= g_strdup(dir
);
1064 purple_buddy_icons_get_cache_dir(void)
1070 purple_buddy_icons_get_handle()
1078 purple_buddy_icons_init()
1080 account_cache
= g_hash_table_new_full(
1081 g_direct_hash
, g_direct_equal
,
1082 NULL
, (GFreeFunc
)g_hash_table_destroy
);
1084 icon_data_cache
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
1086 icon_file_cache
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
1088 pointer_icon_cache
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1091 cache_dir
= g_build_filename(purple_cache_dir(), "icons", NULL
);
1095 purple_buddy_icons_uninit()
1097 purple_signals_disconnect_by_handle(purple_buddy_icons_get_handle());
1099 g_hash_table_destroy(account_cache
);
1100 g_hash_table_destroy(icon_data_cache
);
1101 g_hash_table_destroy(icon_file_cache
);
1102 g_hash_table_destroy(pointer_icon_cache
);
1109 purple_buddy_icon_get_type(void)
1111 static GType type
= 0;
1114 type
= g_boxed_type_register_static("PurpleBuddyIcon",
1115 (GBoxedCopyFunc
)purple_buddy_icon_ref
,
1116 (GBoxedFreeFunc
)purple_buddy_icon_unref
);
1122 PurpleBuddyIconSpec
*
1123 purple_buddy_icon_spec_new(char *format
, int min_width
, int min_height
,
1124 int max_width
, int max_height
, size_t max_filesize
,
1125 PurpleBuddyIconScaleFlags scale_rules
)
1127 PurpleBuddyIconSpec
*icon_spec
;
1129 icon_spec
= g_new0(PurpleBuddyIconSpec
, 1);
1131 icon_spec
->format
= format
;
1132 icon_spec
->min_width
= min_width
;
1133 icon_spec
->min_height
= min_height
;
1134 icon_spec
->max_width
= max_width
;
1135 icon_spec
->max_height
= max_height
;
1136 icon_spec
->max_filesize
= max_filesize
;
1137 icon_spec
->scale_rules
= scale_rules
;
1142 static PurpleBuddyIconSpec
*
1143 purple_buddy_icon_spec_copy(PurpleBuddyIconSpec
*icon_spec
)
1145 PurpleBuddyIconSpec
*icon_spec_copy
;
1147 g_return_val_if_fail(icon_spec
!= NULL
, NULL
);
1149 icon_spec_copy
= g_new0(PurpleBuddyIconSpec
, 1);
1150 *icon_spec_copy
= *icon_spec
;
1152 return icon_spec_copy
;
1155 void purple_buddy_icon_spec_get_scaled_size(PurpleBuddyIconSpec
*spec
,
1156 int *width
, int *height
)
1158 int new_width
, new_height
;
1161 new_height
= *height
;
1163 if (*width
< spec
->min_width
)
1164 new_width
= spec
->min_width
;
1165 else if (*width
> spec
->max_width
)
1166 new_width
= spec
->max_width
;
1168 if (*height
< spec
->min_height
)
1169 new_height
= spec
->min_height
;
1170 else if (*height
> spec
->max_height
)
1171 new_height
= spec
->max_height
;
1173 /* preserve aspect ratio */
1174 if ((double)*height
* (double)new_width
>
1175 (double)*width
* (double)new_height
) {
1176 new_width
= 0.5 + (double)*width
* (double)new_height
/ (double)*height
;
1178 new_height
= 0.5 + (double)*height
* (double)new_width
/ (double)*width
;
1182 *height
= new_height
;
1186 purple_buddy_icon_spec_get_type(void)
1188 static GType type
= 0;
1191 type
= g_boxed_type_register_static("PurpleBuddyIconSpec",
1192 (GBoxedCopyFunc
)purple_buddy_icon_spec_copy
,
1193 (GBoxedFreeFunc
)g_free
);