summaryrefslogtreecommitdiff
path: root/libpurple/buddyicon.c
blob: 707035719955f66bde0234190410e66b6b8455f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
/* purple
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 */

#include "buddyicon.h"
#include "debug.h"
#include "image.h"
#include "purpleaccountmanager.h"
#include "purpleconversation.h"
#include "purpleconversationmanager.h"
#include "purplepath.h"
#include "purpleprivate.h"
#include "purpleprotocolserver.h"

/* NOTE: Instances of this struct are allocated without zeroing the memory, so
 * NOTE: be sure to update purple_buddy_icon_new() if you add members. */
struct _PurpleBuddyIcon
{
	PurpleAccount *account;    /* The account the user is on.          */
	PurpleImage *img;          /* The image containing
	                              the icon data.                       */
	char *username;            /* The username the icon belongs to.    */
	char *checksum;            /* The protocol checksum.               */
	unsigned int ref_count;    /* The buddy icon reference count.      */
};

/*
 * This is the big grand daddy hash table that contains references to
 * everybody's buddy icons.
 *
 * Key is a PurpleAccount.
 * Value is another hash table, usually referred to as "icon_cache."
 * For this inner hash table:
 *    Key is the username of the buddy whose icon is being stored.
 *    Value is the PurpleBuddyIcon for this buddy.
 */
static GHashTable *account_cache = NULL;

/*
 * This hash table contains a bunch of PurpleImages that are
 * shared across all accounts.
 *
 * Key is the filename for this image as constructed by
 * purple_image_generate_filename().  So it is the base16 encoded
 * sha-1 hash plus an appropriate file extension.  For example:
 *   "0f4972d17d1e70e751c43c90c948e72efbff9796.gif"
 *
 * The value is a PurpleImage containing the icon data.  These images are
 * reference counted, and when the count reaches 0 we remove the image from
 * the hash table (but it might still be saved on disk, if the icon is being
 * used by offline accounts or some such).
 */
static GHashTable *icon_data_cache = NULL;

/*
 * This hash table contains reference counts for how many times each
 * icon in the ~/.purple/icons/ directory is being used.  It's pretty
 * crazy.  It maintains the reference count across sessions, too, so
 * if you exit Pidgin then this hash table is reconstructed the next
 * time Pidgin starts.
 *
 * Key is the filename for this image as constructed by
 * purple_image_generate_filename().  So it is the base16 encoded
 * sha-1 hash plus an appropriate file extension.  For example:
 *   "0f4972d17d1e70e751c43c90c948e72efbff9796.gif"
 *
 * The value is a GINT_TO_POINTER count of the number of times this
 * icon is used.  So if four of your buddies are using an icon, and
 * you have the icon set for two of your accounts, then this number
 * will be six.  When this reference count reaches 0 the icon will
 * be deleted from disk.
 */
static GHashTable *icon_file_cache = NULL;

/*
 * This hash table is used for both custom buddy icons on PurpleBlistNodes and
 * account icons.
 */
static GHashTable *pointer_icon_cache = NULL;

static char       *cache_dir     = NULL;

/* "Should icons be cached to disk?" */
static gboolean    icon_caching  = TRUE;

static void delete_buddy_icon_settings(PurpleBlistNode *node, const char *setting_name);

/*
 * Begin functions for dealing with the on-disk icon cache
 */

static void
ref_filename(const char *filename)
{
	int refs;

	g_return_if_fail(filename != NULL);

	refs = GPOINTER_TO_INT(g_hash_table_lookup(icon_file_cache, filename));

	g_hash_table_insert(icon_file_cache, g_strdup(filename),
	                    GINT_TO_POINTER(refs + 1));
}

static void
unref_filename(const char *filename)
{
	int refs;

	if (filename == NULL)
		return;

	refs = GPOINTER_TO_INT(g_hash_table_lookup(icon_file_cache, filename));

	if (refs == 1)
	{
		g_hash_table_remove(icon_file_cache, filename);
	}
	else
	{
		g_hash_table_insert(icon_file_cache, g_strdup(filename),
		                    GINT_TO_POINTER(refs - 1));
	}
}

static const gchar *
image_get_filename(PurpleImage *img)
{
	return g_object_get_data(G_OBJECT(img), "purple-buddyicon-filename");
}

static void
purple_buddy_icon_data_cache(PurpleImage *img)
{
	const gchar *dirname, *filename;
	gchar *path;

	g_return_if_fail(PURPLE_IS_IMAGE(img));

	if (!purple_buddy_icons_is_caching())
		return;

	dirname = purple_buddy_icons_get_cache_dir();
	filename = image_get_filename(img);
	g_return_if_fail(filename != NULL);

	if (!g_file_test(dirname, G_FILE_TEST_IS_DIR))
	{
		purple_debug_info("buddyicon", "creating icon cache directory");

		if (g_mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR) < 0)
		{
			purple_debug_error("buddyicon",
				"unable to create directory %s: %s",
				dirname, g_strerror(errno));
			return;
		}
	}

	path = g_build_filename(dirname, filename, NULL);
	if (!purple_image_save(img, path))
		purple_debug_error("buddyicon", "failed to save icon %s", path);

	g_free(path);
}

static void
purple_buddy_icon_data_uncache_file(const char *filename)
{
	const char *dirname;
	char *path;

	g_return_if_fail(filename != NULL);

	/* It's possible that there are other references to this icon
	 * cache file that are not currently loaded into memory. */
	if (GPOINTER_TO_INT(g_hash_table_lookup(icon_file_cache, filename)))
		return;

	dirname = purple_buddy_icons_get_cache_dir();
	path = g_build_filename(dirname, filename, NULL);

	if (g_file_test(path, G_FILE_TEST_EXISTS))
	{
		if (g_unlink(path))
		{
			purple_debug_error("buddyicon", "Failed to delete %s: %s\n",
			                   path, g_strerror(errno));
		}
		else
		{
			purple_debug_info("buddyicon", "Deleted cache file: %s\n", path);
		}
	}

	g_free(path);
}

/*
 * End functions for dealing with the on-disk icon cache
 */

/*
 * Begin functions for dealing with the in-memory icon cache
 */

static gboolean
value_equals(G_GNUC_UNUSED gpointer key, gpointer value, gpointer user_data)
{
	return (value == user_data);
}

static void
image_deleting_cb(gpointer _filename)
{
	PurpleImage *img;
	gchar *filename = _filename;

	img = g_hash_table_lookup(icon_data_cache, filename);
	purple_buddy_icon_data_uncache_file(filename);
	g_hash_table_remove(icon_data_cache, filename);

	/* We could make this O(1) by using another hash table, but
	 * this is probably good enough. */
	g_hash_table_foreach_remove(pointer_icon_cache, value_equals, (gpointer)img);

	g_free(filename);
}

static PurpleImage *
purple_buddy_icon_data_new(guchar *icon_data, size_t icon_len)
{
	PurpleImage *newimg, *oldimg;
	const gchar *filename;

	g_return_val_if_fail(icon_data != NULL, NULL);
	g_return_val_if_fail(icon_len > 0, NULL);

	newimg = purple_image_new_take_data(icon_data, icon_len);
	filename = purple_image_generate_filename(newimg);

	/* TODO: Why is this function called for buddies without icons? If this is
	 * intended, should the filename be null?
	 */
	if (filename != NULL) {
		oldimg = g_hash_table_lookup(icon_data_cache, filename);
		if (oldimg) {
			g_warn_if_fail(PURPLE_IS_IMAGE(oldimg));
			g_object_unref(newimg);
			g_object_ref(oldimg);
			return oldimg;
		}

		/* This will take ownership of file and free it as needed */
		g_hash_table_insert(icon_data_cache, g_strdup(filename), newimg);
	}

	g_object_set_data_full(G_OBJECT(newimg), "purple-buddyicon-filename",
		g_strdup(filename), image_deleting_cb);

	purple_buddy_icon_data_cache(newimg);

	return newimg;
}

/*
 * End functions for dealing with the in-memory icon cache
 */

static PurpleBuddyIcon *
purple_buddy_icon_create(PurpleAccount *account, const char *username)
{
	PurpleBuddyIcon *icon;
	GHashTable *icon_cache;

	/* This does not zero.  See purple_buddy_icon_new() for
	 * information on which function allocates which member. */
	icon = g_slice_new(PurpleBuddyIcon);

	icon->account = account;
	icon->username = g_strdup(username);
	icon->checksum = NULL;
	icon->ref_count = 1;

	icon_cache = g_hash_table_lookup(account_cache, account);

	if (icon_cache == NULL)
	{
		icon_cache = g_hash_table_new(g_str_hash, g_str_equal);

		g_hash_table_insert(account_cache, account, icon_cache);
	}

	g_hash_table_insert(icon_cache,
	                    (char *)purple_buddy_icon_get_username(icon), icon);
	return icon;
}

PurpleBuddyIcon *
purple_buddy_icon_new(PurpleAccount *account, const char *username,
                      void *icon_data, size_t icon_len,
                      const char *checksum)
{
	PurpleBuddyIcon *icon;

	g_return_val_if_fail(account   != NULL, NULL);
	g_return_val_if_fail(username  != NULL, NULL);
	g_return_val_if_fail(icon_data != NULL, NULL);
	g_return_val_if_fail(icon_len  > 0,    NULL);

	/* purple_buddy_icons_find() does allocation, so be
	 * sure to update it as well when members are added. */
	icon = purple_buddy_icons_find(account, username);

	/* purple_buddy_icon_create() sets account & username */
	if (icon == NULL)
		icon = purple_buddy_icon_create(account, username);

	/* purple_buddy_icon_set_data() sets img, but it
	 * references img first, so we need to initialize it */
	icon->img = NULL;
	purple_buddy_icon_set_data(icon, icon_data, icon_len, checksum);

	return icon;
}

PurpleBuddyIcon *
purple_buddy_icon_ref(PurpleBuddyIcon *icon)
{
	g_return_val_if_fail(icon != NULL, NULL);

	icon->ref_count++;

	return icon;
}

void
purple_buddy_icon_unref(PurpleBuddyIcon *icon)
{
	if (icon == NULL)
		return;

	g_return_if_fail(icon->ref_count > 0);

	icon->ref_count--;

	if (icon->ref_count == 0)
	{
		GHashTable *icon_cache = g_hash_table_lookup(account_cache, purple_buddy_icon_get_account(icon));

		if (icon_cache != NULL)
			g_hash_table_remove(icon_cache, purple_buddy_icon_get_username(icon));

		g_free(icon->username);
		g_free(icon->checksum);
		g_object_unref(icon->img);

		g_slice_free(PurpleBuddyIcon, icon);
	}
}

void
purple_buddy_icon_update(PurpleBuddyIcon *icon)
{
	PurpleAccount *account;
	const char *username;
	PurpleBuddyIcon *icon_to_set;
	GSList *buddies;

	g_return_if_fail(icon != NULL);

	account  = purple_buddy_icon_get_account(icon);
	username = purple_buddy_icon_get_username(icon);

	/* If no data exists (icon->img == NULL), then call the functions below
	 * with NULL to unset the icon.  They will then unref the icon and it should
	 * be destroyed.  The only way it wouldn't be destroyed is if someone
	 * else is holding a reference to it, in which case they can kill
	 * the icon when they realize it has no data. */
	icon_to_set = icon->img ? icon : NULL;

	/* Ensure that icon remains valid throughout */
	purple_buddy_icon_ref(icon);

	buddies = purple_blist_find_buddies(account, username);
	while (buddies != NULL)
	{
		PurpleBuddy *buddy = (PurpleBuddy *)buddies->data;
		char *old_icon;

		purple_buddy_set_icon(buddy, icon_to_set);
		old_icon = g_strdup(purple_blist_node_get_string((PurpleBlistNode *)buddy,
		                                                 "buddy_icon"));
		if (icon->img && purple_buddy_icons_is_caching())
		{
			const char *filename = image_get_filename(icon->img);
			g_warn_if_fail(filename != NULL);
			purple_blist_node_set_string((PurpleBlistNode *)buddy,
			                             "buddy_icon",
			                             filename);

			if (icon->checksum && *icon->checksum)
			{
				purple_blist_node_set_string((PurpleBlistNode *)buddy,
				                             "icon_checksum",
				                             icon->checksum);
			}
			else
			{
				purple_blist_node_remove_setting((PurpleBlistNode *)buddy,
				                                 "icon_checksum");
			}
			ref_filename(filename);
		}
		else if (!icon->img)
		{
			purple_blist_node_remove_setting((PurpleBlistNode *)buddy, "buddy_icon");
			purple_blist_node_remove_setting((PurpleBlistNode *)buddy, "icon_checksum");
		}
		unref_filename(old_icon);
		g_free(old_icon);

		buddies = g_slist_delete_link(buddies, buddies);
	}

	/* icon's refcount was incremented above */
	purple_buddy_icon_unref(icon);
}

void
purple_buddy_icon_set_data(PurpleBuddyIcon *icon, guchar *data,
                           size_t len, const char *checksum)
{
	PurpleImage *old_img;

	g_return_if_fail(icon != NULL);

	old_img = icon->img;
	icon->img = NULL;

	if (data != NULL)
	{
		if (len > 0)
			icon->img = purple_buddy_icon_data_new(data, len);
		else
			g_free(data);
	}

	g_free(icon->checksum);
	icon->checksum = g_strdup(checksum);

	purple_buddy_icon_update(icon);

	if (old_img)
		g_object_unref(old_img);
}

gboolean
purple_buddy_icon_save_to_filename(PurpleBuddyIcon *icon,
                                   const gchar *filename, GError **error)
{
	gconstpointer data;
	size_t len = 0;

	data = purple_buddy_icon_get_data(icon, &len);

	return g_file_set_contents(filename, data, len, error);
}


PurpleAccount *
purple_buddy_icon_get_account(const PurpleBuddyIcon *icon)
{
	g_return_val_if_fail(icon != NULL, NULL);

	return icon->account;
}

const char *
purple_buddy_icon_get_username(const PurpleBuddyIcon *icon)
{
	g_return_val_if_fail(icon != NULL, NULL);

	return icon->username;
}

const char *
purple_buddy_icon_get_checksum(const PurpleBuddyIcon *icon)
{
	g_return_val_if_fail(icon != NULL, NULL);

	return icon->checksum;
}

gconstpointer
purple_buddy_icon_get_data(const PurpleBuddyIcon *icon, size_t *len)
{
	g_return_val_if_fail(icon != NULL, NULL);

	if (icon->img)
	{
		if (len != NULL)
			*len = purple_image_get_data_size(icon->img);

		return purple_image_get_data(icon->img);
	}

	return NULL;
}

GInputStream *
purple_buddy_icon_get_stream(PurpleBuddyIcon *icon) {
	gconstpointer data = NULL;
	size_t len = 0;

	g_return_val_if_fail(icon != NULL, NULL);

	data = purple_buddy_icon_get_data(icon, &len);

	return g_memory_input_stream_new_from_data(data, (gssize)len, NULL);
}

const char *
purple_buddy_icon_get_extension(const PurpleBuddyIcon *icon)
{
	if (icon->img != NULL)
		return purple_image_get_extension(icon->img);

	return NULL;
}

void
purple_buddy_icons_set_for_user(PurpleAccount *account, const char *username,
                                void *icon_data, size_t icon_len,
                                const char *checksum)
{
	GHashTable *icon_cache;
	PurpleBuddyIcon *icon = NULL;

	g_return_if_fail(account  != NULL);
	g_return_if_fail(username != NULL);

	icon_cache = g_hash_table_lookup(account_cache, account);

	if (icon_cache != NULL)
		icon = g_hash_table_lookup(icon_cache, username);

	if (icon != NULL)
		purple_buddy_icon_set_data(icon, icon_data, icon_len, checksum);
	else if (icon_data && icon_len > 0)
	{
		PurpleBuddyIcon *icon = purple_buddy_icon_new(account, username, icon_data, icon_len, checksum);

		/* purple_buddy_icon_new() calls
		 * purple_buddy_icon_set_data(), which calls
		 * purple_buddy_icon_update(), which has the buddy list
		 * and conversations take references as appropriate.
		 * This function doesn't return icon, so we can't
		 * leave a reference dangling. */
		purple_buddy_icon_unref(icon);
	}
	else
	{
		/* If the buddy list or a conversation was holding a
		 * reference, we'd have found the icon in the cache.
		 * Since we know we're deleting the icon, we only
		 * need a subset of purple_buddy_icon_update(). */

		GSList *buddies = purple_blist_find_buddies(account, username);
		while (buddies != NULL)
		{
			PurpleBuddy *buddy = (PurpleBuddy *)buddies->data;

			unref_filename(purple_blist_node_get_string((PurpleBlistNode *)buddy, "buddy_icon"));
			purple_blist_node_remove_setting((PurpleBlistNode *)buddy, "buddy_icon");
			purple_blist_node_remove_setting((PurpleBlistNode *)buddy, "icon_checksum");

			buddies = g_slist_delete_link(buddies, buddies);
		}
	}
}

const gchar *
purple_buddy_icon_get_full_path(PurpleBuddyIcon *icon)
{
	const gchar *path;

	g_return_val_if_fail(icon != NULL, NULL);

	if (icon->img == NULL)
		return NULL;

	path = purple_image_get_path(icon->img);
	if (!g_file_test(path, G_FILE_TEST_EXISTS))
	{
		return NULL;
	}
	return path;
}

const char *
purple_buddy_icons_get_checksum_for_user(PurpleBuddy *buddy)
{
	return purple_blist_node_get_string((PurpleBlistNode*)buddy,
	                                    "icon_checksum");
}

static gboolean
read_icon_file(const char *path, guchar **data, size_t *len)
{
	GError *err = NULL;

	if (!g_file_get_contents(path, (gchar **)data, len, &err))
	{
		purple_debug_error("buddyicon", "Error reading %s: %s\n",
		                   path, err->message);
		g_error_free(err);

		return FALSE;
	}

	return TRUE;
}

PurpleBuddyIcon *
purple_buddy_icons_find(PurpleAccount *account, const char *username)
{
	GHashTable *icon_cache;
	PurpleBuddyIcon *icon = NULL;

	g_return_val_if_fail(account  != NULL, NULL);
	g_return_val_if_fail(username != NULL, NULL);

	icon_cache = g_hash_table_lookup(account_cache, account);

	if ((icon_cache == NULL) || ((icon = g_hash_table_lookup(icon_cache, username)) == NULL))
	{
		/* The icon is not currently cached in memory--try reading from disk */
		PurpleBuddy *b = purple_blist_find_buddy(account, username);
		const char *protocol_icon_file;
		const char *dirname;
		gboolean caching;
		gchar *path;
		guchar *data;
		size_t len;

		if (!b)
			return NULL;

		protocol_icon_file = purple_blist_node_get_string((PurpleBlistNode*)b, "buddy_icon");

		if (protocol_icon_file == NULL)
			return NULL;

		dirname = purple_buddy_icons_get_cache_dir();

		caching = purple_buddy_icons_is_caching();
		/* By disabling caching temporarily, we avoid a loop
		 * and don't have to add special code through several
		 * functions. */
		purple_buddy_icons_set_caching(FALSE);

		path = g_build_filename(dirname, protocol_icon_file, NULL);
		if (read_icon_file(path, &data, &len)) {
			const char *checksum;

			icon = purple_buddy_icon_create(account, username);
			icon->img = NULL;
			checksum = purple_blist_node_get_string((PurpleBlistNode *)b,
			                                        "icon_checksum");
			purple_buddy_icon_set_data(icon, data, len, checksum);
		} else {
			delete_buddy_icon_settings((PurpleBlistNode *)b, "buddy_icon");
		}

		g_free(path);

		purple_buddy_icons_set_caching(caching);
	}

	return (icon ? purple_buddy_icon_ref(icon) : NULL);
}

PurpleImage *
purple_buddy_icons_find_account_icon(PurpleAccount *account)
{
	PurpleImage *img;
	const char *account_icon_file;
	const char *dirname;
	char *path;
	guchar *data;
	size_t len;

	g_return_val_if_fail(account != NULL, NULL);

	img = g_hash_table_lookup(pointer_icon_cache, account);
	if (img) {
		g_object_ref(img);
		return img;
	}

	account_icon_file = purple_account_get_string(account, "buddy_icon", NULL);

	if (account_icon_file == NULL)
		return NULL;

	dirname = purple_buddy_icons_get_cache_dir();
	path = g_build_filename(dirname, account_icon_file, NULL);

	if (read_icon_file(path, &data, &len)) {
		g_free(path);
		img = purple_buddy_icons_set_account_icon(account, data, len);
		g_object_ref(img);
		return img;
	}
	g_free(path);

	return NULL;
}

PurpleImage *
purple_buddy_icons_set_account_icon(PurpleAccount *account,
                                    guchar *icon_data, size_t icon_len)
{
	PurpleImage *old_img;
	PurpleImage *img = NULL;
	char *old_icon;

	if (icon_data != NULL && icon_len > 0) {
		img = purple_buddy_icon_data_new(icon_data, icon_len);
	}

	old_icon = g_strdup(purple_account_get_string(account, "buddy_icon", NULL));
	if (img && purple_buddy_icons_is_caching())
	{
		const char *filename = image_get_filename(img);
		g_warn_if_fail(filename != NULL);
		purple_account_set_string(account, "buddy_icon", filename);
		purple_account_set_int(account, "buddy_icon_timestamp", time(NULL));
		ref_filename(filename);
	}
	else
	{
		purple_account_set_string(account, "buddy_icon", NULL);
		purple_account_set_int(account, "buddy_icon_timestamp", 0);
	}
	unref_filename(old_icon);

	old_img = g_hash_table_lookup(pointer_icon_cache, account);

	if (img)
		g_hash_table_insert(pointer_icon_cache, account, img);
	else
		g_hash_table_remove(pointer_icon_cache, account);

	if (!purple_account_is_disconnected(account))
	{
		PurpleConnection *gc;
		PurpleProtocol *protocol;

		gc = purple_account_get_connection(account);
		protocol = purple_connection_get_protocol(gc);

		if(PURPLE_IS_PROTOCOL_SERVER(protocol)) {
			purple_protocol_server_set_buddy_icon(PURPLE_PROTOCOL_SERVER(protocol),
			                                      gc, img);
		}
	}

	if (old_img)
		g_object_unref(old_img);
	else if (old_icon)
	{
		/* The old icon may not have been loaded into memory.  In that
		 * case, we'll need to uncache the filename.  The filenames
		 * are ref-counted, so this is safe. */
		purple_buddy_icon_data_uncache_file(old_icon);
	}
	g_free(old_icon);

	return img;
}

time_t
purple_buddy_icons_get_account_icon_timestamp(PurpleAccount *account)
{
	time_t ret;

	g_return_val_if_fail(account != NULL, 0);

	ret = purple_account_get_int(account, "buddy_icon_timestamp", 0);

	/* This deals with migration cases. */
	if (ret == 0 && purple_account_get_string(account, "buddy_icon", NULL) != NULL)
	{
		ret = time(NULL);
		purple_account_set_int(account, "buddy_icon_timestamp", ret);
	}

	return ret;
}

gboolean
purple_buddy_icons_node_has_custom_icon(PurpleBlistNode *node)
{
	g_return_val_if_fail(node != NULL, FALSE);

	return (purple_blist_node_get_string(node, "custom_buddy_icon") != NULL);
}

PurpleImage *
purple_buddy_icons_node_find_custom_icon(PurpleBlistNode *node)
{
	char *path;
	size_t len;
	guchar *data;
	PurpleImage *img;
	const char *custom_icon_file, *dirname;

	g_return_val_if_fail(node != NULL, NULL);

	img = g_hash_table_lookup(pointer_icon_cache, node);
	if (img) {
		g_object_ref(img);
		return img;
	}

	custom_icon_file = purple_blist_node_get_string(node,
	                                                "custom_buddy_icon");

	if (custom_icon_file == NULL)
		return NULL;

	dirname = purple_buddy_icons_get_cache_dir();
	path = g_build_filename(dirname, custom_icon_file, NULL);

	if (read_icon_file(path, &data, &len)) {
		g_free(path);
		img = purple_buddy_icons_node_set_custom_icon(node, data, len);
		g_object_ref(img);
		return img;
	}
	g_free(path);

	return NULL;
}

PurpleImage *
purple_buddy_icons_node_set_custom_icon(PurpleBlistNode *node,
                                        guchar *icon_data, size_t icon_len)
{
	char *old_icon;
	PurpleConversationManager *manager = NULL;
	PurpleImage *old_img;
	PurpleImage *img = NULL;

	g_return_val_if_fail(node != NULL, NULL);

	if (!PURPLE_IS_META_CONTACT(node) &&
	    !PURPLE_IS_CHAT(node) &&
	    !PURPLE_IS_GROUP(node)) {
		return NULL;
	}

	old_img = g_hash_table_lookup(pointer_icon_cache, node);

	if (icon_data != NULL && icon_len > 0) {
		img = purple_buddy_icon_data_new(icon_data, icon_len);
	}

	old_icon = g_strdup(purple_blist_node_get_string(node,
	                                                 "custom_buddy_icon"));
	if (img && purple_buddy_icons_is_caching()) {
		const char *filename = image_get_filename(img);
		g_warn_if_fail(filename);
		purple_blist_node_set_string(node, "custom_buddy_icon",
		                             filename);
		ref_filename(filename);
	} else {
		purple_blist_node_remove_setting(node, "custom_buddy_icon");
	}
	unref_filename(old_icon);

	if (img)
		g_hash_table_insert(pointer_icon_cache, node, img);
	else
		g_hash_table_remove(pointer_icon_cache, node);

	manager = purple_conversation_manager_get_default();

	if (PURPLE_IS_META_CONTACT(node)) {
		PurpleBlistNode *child;
		for (child = purple_blist_node_get_first_child(node);
		     child;
			 child = purple_blist_node_get_sibling_next(child))
		{
			PurpleBuddy *buddy;
			PurpleConversation *im;

			if(!PURPLE_IS_BUDDY(child)) {
				continue;
			}

			buddy = PURPLE_BUDDY(child);

			im = purple_conversation_manager_find_im(manager,
			                                         purple_buddy_get_account(buddy),
			                                         purple_buddy_get_name(buddy));
			if(PURPLE_IS_IM_CONVERSATION(im)) {
				purple_conversation_update(im, PURPLE_CONVERSATION_UPDATE_ICON);
			}

			/* Is this call necessary anymore? Can the buddies
			 * themselves need updating when the custom buddy
			 * icon changes? */
			purple_blist_update_node(purple_blist_get_default(), child);
		}
	} else if (PURPLE_IS_CHAT(node)) {
		PurpleAccount *account = purple_chat_get_account(PURPLE_CHAT(node));
		PurpleConversation *chat = NULL;
		const gchar *name = purple_chat_get_name(PURPLE_CHAT(node));

		chat = purple_conversation_manager_find_chat(manager, account, name);
		if(PURPLE_IS_CHAT_CONVERSATION(chat)) {
			purple_conversation_update(chat, PURPLE_CONVERSATION_UPDATE_ICON);
		}
	}

	purple_blist_update_node(purple_blist_get_default(), node);

	if (old_img) {
		g_object_unref(old_img);
	} else if (old_icon) {
		/* The old icon may not have been loaded into memory.  In that
		 * case, we'll need to uncache the filename.  The filenames
		 * are ref-counted, so this is safe. */
		purple_buddy_icon_data_uncache_file(old_icon);
	}
	g_free(old_icon);

	return img;
}

PurpleImage *
purple_buddy_icons_node_set_custom_icon_from_file(PurpleBlistNode *node,
                                                  const gchar *filename)
{
	size_t len = 0;
	guchar *data = NULL;

	g_return_val_if_fail(node != NULL, NULL);

	if (!PURPLE_IS_META_CONTACT(node) &&
	    !PURPLE_IS_CHAT(node) &&
	    !PURPLE_IS_GROUP(node)) {
		return NULL;
	}

	if (filename != NULL) {
		if (!read_icon_file(filename, &data, &len)) {
			return NULL;
		}
	}

	return purple_buddy_icons_node_set_custom_icon(node, data, len);
}

static void
delete_buddy_icon_settings(PurpleBlistNode *node, const char *setting_name)
{
	purple_blist_node_remove_setting(node, setting_name);

	if (purple_strequal(setting_name, "buddy_icon"))
	{
		purple_blist_node_remove_setting(node, "avatar_hash");
		purple_blist_node_remove_setting(node, "icon_checksum");
	}
}

static void
_purple_buddy_icons_account_loaded_cb_helper(PurpleAccount *account,
                                             gpointer data)
{
	const gchar *dirname = (const gchar *)data;
	const gchar *filename = NULL;

	filename = purple_account_get_string(account, "buddy_icon", NULL);
	if(filename != NULL) {
		gchar *path = g_build_filename(dirname, filename, NULL);

		if(!g_file_test(path, G_FILE_TEST_EXISTS)) {
			purple_account_set_string(account, "buddy_icon", NULL);
		} else {
			ref_filename(filename);
		}
		g_free(path);
	}
}

void
_purple_buddy_icons_account_loaded_cb(void)
{
	PurpleAccountManager *manager = purple_account_manager_get_default();
	const char *dirname = purple_buddy_icons_get_cache_dir();

	purple_account_manager_foreach(manager,
	                               _purple_buddy_icons_account_loaded_cb_helper,
	                               (gpointer)dirname);
}

void
_purple_buddy_icons_blist_loaded_cb(void)
{
	PurpleBlistNode *node = purple_blist_get_default_root();
	const char *dirname = purple_buddy_icons_get_cache_dir();

	while (node != NULL)
	{
		if (PURPLE_IS_BUDDY(node))
		{
			const char *filename;

			filename = purple_blist_node_get_string(node, "buddy_icon");
			if (filename != NULL)
			{
				char *path = g_build_filename(dirname, filename, NULL);
				if (!g_file_test(path, G_FILE_TEST_EXISTS))
				{
					purple_blist_node_remove_setting(node,
					                                 "buddy_icon");
					purple_blist_node_remove_setting(node,
					                                 "icon_checksum");
				}
				else
					ref_filename(filename);
				g_free(path);
			}
		}
		else if (PURPLE_IS_META_CONTACT(node) ||
		         PURPLE_IS_CHAT(node) ||
		         PURPLE_IS_GROUP(node))
		{
			const char *filename;

			filename = purple_blist_node_get_string(node, "custom_buddy_icon");
			if (filename != NULL)
			{
				char *path = g_build_filename(dirname, filename, NULL);
				if (!g_file_test(path, G_FILE_TEST_EXISTS))
				{
					purple_blist_node_remove_setting(node,
					                                 "custom_buddy_icon");
				}
				else
					ref_filename(filename);
				g_free(path);
			}
		}
		node = purple_blist_node_next(node, TRUE);
	}
}

void
purple_buddy_icons_set_caching(gboolean caching)
{
	icon_caching = caching;
}

gboolean
purple_buddy_icons_is_caching(void)
{
	return icon_caching;
}

void
purple_buddy_icons_set_cache_dir(const char *dir)
{
	g_return_if_fail(dir != NULL);

	g_free(cache_dir);
	cache_dir = g_strdup(dir);
}

const char *
purple_buddy_icons_get_cache_dir(void)
{
	return cache_dir;
}

void *
purple_buddy_icons_get_handle(void)
{
	static int handle;

	return &handle;
}

void
purple_buddy_icons_init(void)
{
	account_cache = g_hash_table_new_full(
		g_direct_hash, g_direct_equal,
		NULL, (GFreeFunc)g_hash_table_destroy);

	icon_data_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
	                                        g_free, NULL);
	icon_file_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
	                                        g_free, NULL);
	pointer_icon_cache = g_hash_table_new(g_direct_hash, g_direct_equal);

	if (!cache_dir)
		cache_dir = g_build_filename(purple_cache_dir(), "icons", NULL);
}

void
purple_buddy_icons_uninit(void)
{
	purple_signals_disconnect_by_handle(purple_buddy_icons_get_handle());

	g_hash_table_destroy(account_cache);
	g_hash_table_destroy(icon_data_cache);
	g_hash_table_destroy(icon_file_cache);
	g_hash_table_destroy(pointer_icon_cache);
	g_free(cache_dir);

	cache_dir = NULL;
}

GType
purple_buddy_icon_get_type(void)
{
	static GType type = 0;

	if (type == 0) {
		type = g_boxed_type_register_static("PurpleBuddyIcon",
				(GBoxedCopyFunc)purple_buddy_icon_ref,
				(GBoxedFreeFunc)purple_buddy_icon_unref);
	}

	return type;
}

PurpleBuddyIconSpec *
purple_buddy_icon_spec_new(char *format, int min_width, int min_height,
		int max_width, int max_height, size_t max_filesize,
		PurpleBuddyIconScaleFlags scale_rules)
{
	PurpleBuddyIconSpec *icon_spec;

	icon_spec = g_new0(PurpleBuddyIconSpec, 1);

	icon_spec->format       = format;
	icon_spec->min_width    = min_width;
	icon_spec->min_height   = min_height;
	icon_spec->max_width    = max_width;
	icon_spec->max_height   = max_height;
	icon_spec->max_filesize = max_filesize;
	icon_spec->scale_rules  = scale_rules;

	return icon_spec;
}

void
purple_buddy_icon_spec_free(PurpleBuddyIconSpec *spec) {
	if(spec == NULL) {
		return;
	}

	g_free(spec);
}

static PurpleBuddyIconSpec *
purple_buddy_icon_spec_copy(PurpleBuddyIconSpec *icon_spec)
{
	PurpleBuddyIconSpec *icon_spec_copy;

	g_return_val_if_fail(icon_spec != NULL, NULL);

	icon_spec_copy  = g_new0(PurpleBuddyIconSpec, 1);
	*icon_spec_copy = *icon_spec;

	return icon_spec_copy;
}

void purple_buddy_icon_spec_get_scaled_size(PurpleBuddyIconSpec *spec,
		int *width, int *height)
{
	int new_width, new_height;

	new_width = *width;
	new_height = *height;

	if (*width < spec->min_width)
		new_width = spec->min_width;
	else if (*width > spec->max_width)
		new_width = spec->max_width;

	if (*height < spec->min_height)
		new_height = spec->min_height;
	else if (*height > spec->max_height)
		new_height = spec->max_height;

	/* preserve aspect ratio */
	if ((double)*height * (double)new_width >
		(double)*width * (double)new_height) {
			new_width = 0.5 + (double)*width * (double)new_height / (double)*height;
	} else {
			new_height = 0.5 + (double)*height * (double)new_width / (double)*width;
	}

	*width = new_width;
	*height = new_height;
}

GType
purple_buddy_icon_spec_get_type(void)
{
	static GType type = 0;

	if (type == 0) {
		type = g_boxed_type_register_static("PurpleBuddyIconSpec",
				(GBoxedCopyFunc)purple_buddy_icon_spec_copy,
				(GBoxedFreeFunc)purple_buddy_icon_spec_free);
	}

	return type;
}