summaryrefslogtreecommitdiff
path: root/libpurple/smiley.c
blob: 1384a73369b06bbb500888fa33b889d43236b5d0 (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
/**
 * @file smiley.c Simley API
 * @ingroup core
 */

/* 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 "internal.h"
#include "dbus-maybe.h"
#include "debug.h"
#include "imgstore.h"
#include "smiley.h"
#include "util.h"
#include "xmlnode.h"

#define PURPLE_SMILEY_GET_PRIVATE(obj) \
	(G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_SMILEY, PurpleSmileyPrivate))

/**************************************************************************/
/* Structs                                                                */
/**************************************************************************/

typedef struct {
	PurpleStoredImage *img;        /**< The id of the stored image with the
	                                    the smiley data.        */
	char *shortcut;                /**< Shortcut associated with the custom
	                                    smiley. This field will work as a
	                                    unique key by this API. */
	char *checksum;                /**< The smiley checksum.        */
} PurpleSmileyPrivate;

static GHashTable *smiley_shortcut_index = NULL; /* shortcut (char *) => smiley (PurpleSmiley*) */
static GHashTable *smiley_checksum_index = NULL; /* checksum (char *) => smiley (PurpleSmiley*) */

static guint save_timer = 0;
static gboolean smileys_loaded = FALSE;
static char *smileys_dir = NULL;

#define SMILEYS_DEFAULT_FOLDER			"custom_smiley"
#define SMILEYS_LOG_ID				"smileys"

#define XML_FILE_NAME				"smileys.xml"

#define XML_ROOT_TAG				"smileys"
#define XML_PROFILE_TAG			"profile"
#define XML_PROFILE_NAME_ATTRIB_TAG		"name"
#define XML_ACCOUNT_TAG			"account"
#define XML_ACCOUNT_USERID_ATTRIB_TAG		"userid"
#define XML_SMILEY_SET_TAG			"smiley_set"
#define XML_SMILEY_TAG				"smiley"
#define XML_SHORTCUT_ATTRIB_TAG		"shortcut"
#define XML_CHECKSUM_ATRIB_TAG			"checksum"
#define XML_FILENAME_ATRIB_TAG			"filename"


/******************************************************************************
 * XML descriptor file layout                                                 *
 ******************************************************************************
 *
 * Although we are creating the profile XML structure here, now we
 * won't handle it.
 * So, we just add one profile named "default" that has no associated
 * account elements, and have only the smiley_set that will contain
 * all existent custom smiley.
 *
 * It's our "Highlander Profile" :-)
 *
 ******************************************************************************
 *
 * <smileys>
 *   <profile name="john.doe">
 *     <account userid="john.doe@jabber.org">
 *     <account userid="john.doe@gmail.com">
 *     <smiley_set>
 *       <smiley shortcut="aaa" checksum="xxxxxxxx" filename="file_name1.gif"/>
 *       <smiley shortcut="bbb" checksum="yyyyyyy" filename="file_name2.gif"/>
 *     </smiley_set>
 *   </profile>
 * </smiley>
 *
 *****************************************************************************/


/*********************************************************************
 * Forward declarations                                              *
 *********************************************************************/

static gboolean read_smiley_file(const char *path, guchar **data, size_t *len);

static char *get_file_full_path(const char *filename);

static PurpleSmiley *purple_smiley_create(const char *shortcut);

static void purple_smiley_load_file(const char *shortcut, const char *checksum,
		const char *filename);

static void
purple_smiley_set_data_impl(PurpleSmiley *smiley, guchar *smiley_data,
		size_t smiley_data_len);

static void
purple_smiley_data_store(PurpleStoredImage *stored_img);

static void
purple_smiley_data_unstore(const char *filename);

/*********************************************************************
 * Writing to disk                                                   *
 *********************************************************************/

static PurpleXmlNode *
smiley_to_xmlnode(PurpleSmiley *smiley)
{
	PurpleSmileyPrivate *priv = NULL;
	PurpleXmlNode *smiley_node = NULL;

	smiley_node = purple_xmlnode_new(XML_SMILEY_TAG);

	if (!smiley_node)
		return NULL;

	priv = PURPLE_SMILEY_GET_PRIVATE(smiley);

	purple_xmlnode_set_attrib(smiley_node, XML_SHORTCUT_ATTRIB_TAG,
			priv->shortcut);

	purple_xmlnode_set_attrib(smiley_node, XML_CHECKSUM_ATRIB_TAG,
			priv->checksum);

	purple_xmlnode_set_attrib(smiley_node, XML_FILENAME_ATRIB_TAG,
			purple_imgstore_get_filename(priv->img));

	return smiley_node;
}

static void
add_smiley_to_main_node(gpointer key, gpointer value, gpointer user_data)
{
	PurpleXmlNode *child_node;

	child_node = smiley_to_xmlnode(value);
	purple_xmlnode_insert_child((PurpleXmlNode*)user_data, child_node);
}

static PurpleXmlNode *
smileys_to_xmlnode(void)
{
	PurpleXmlNode *root_node, *profile_node, *smileyset_node;

	root_node = purple_xmlnode_new(XML_ROOT_TAG);
	purple_xmlnode_set_attrib(root_node, "version", "1.0");

	/* See the top comments above to understand why initial tag elements
	 * are not being considered by now. */
	profile_node = purple_xmlnode_new(XML_PROFILE_TAG);
	if (profile_node) {
		purple_xmlnode_set_attrib(profile_node, XML_PROFILE_NAME_ATTRIB_TAG, "Default");
		purple_xmlnode_insert_child(root_node, profile_node);

		smileyset_node = purple_xmlnode_new(XML_SMILEY_SET_TAG);
		if (smileyset_node) {
			purple_xmlnode_insert_child(profile_node, smileyset_node);
			g_hash_table_foreach(smiley_shortcut_index, add_smiley_to_main_node, smileyset_node);
		}
	}

	return root_node;
}

static void
sync_smileys(void)
{
	PurpleXmlNode *root_node;
	char *data;

	if (!smileys_loaded) {
		purple_debug_error(SMILEYS_LOG_ID, "Attempted to save smileys before it "
						 "was read!\n");
		return;
	}

	root_node = smileys_to_xmlnode();
	data = purple_xmlnode_to_formatted_str(root_node, NULL);
	purple_util_write_data_to_file(XML_FILE_NAME, data, -1);

	g_free(data);
	purple_xmlnode_free(root_node);
}

static gboolean
save_smileys_cb(gpointer data)
{
	sync_smileys();
	save_timer = 0;
	return FALSE;
}

static void
purple_smileys_save(void)
{
	if (save_timer == 0)
		save_timer = purple_timeout_add_seconds(5, save_smileys_cb, NULL);
}


/*********************************************************************
 * Reading from disk                                                 *
 *********************************************************************/

static void
parse_smiley(PurpleXmlNode *smiley_node)
{
	const char *shortcut = NULL;
	const char *checksum = NULL;
	const char *filename = NULL;

	shortcut = purple_xmlnode_get_attrib(smiley_node, XML_SHORTCUT_ATTRIB_TAG);
	checksum = purple_xmlnode_get_attrib(smiley_node, XML_CHECKSUM_ATRIB_TAG);
	filename = purple_xmlnode_get_attrib(smiley_node, XML_FILENAME_ATRIB_TAG);

	if ((shortcut == NULL) || (checksum == NULL) || (filename == NULL))
		return;

	purple_smiley_load_file(shortcut, checksum, filename);
}

static void
purple_smileys_load(void)
{
	PurpleXmlNode *root_node, *profile_node;
	PurpleXmlNode *smileyset_node = NULL;
	PurpleXmlNode *smiley_node;

	smileys_loaded = TRUE;

	root_node = purple_util_read_xml_from_file(XML_FILE_NAME,
			_(SMILEYS_LOG_ID));

	if (root_node == NULL)
		return;

	/* See the top comments above to understand why initial tag elements
	 * are not being considered by now. */
	profile_node = purple_xmlnode_get_child(root_node, XML_PROFILE_TAG);
	if (profile_node)
		smileyset_node = purple_xmlnode_get_child(profile_node, XML_SMILEY_SET_TAG);

	if (smileyset_node) {
		smiley_node = purple_xmlnode_get_child(smileyset_node, XML_SMILEY_TAG);
		for (; smiley_node != NULL;
				smiley_node = purple_xmlnode_get_next_twin(smiley_node)) {
			parse_smiley(smiley_node);
		}
	}

	purple_xmlnode_free(root_node);
}

/*********************************************************************
 * GObject Stuff                                                     *
 *********************************************************************/
enum
{
	PROP_0,
	PROP_SHORTCUT,
	PROP_IMGSTORE
};

#define PROP_SHORTCUT_S "shortcut"
#define PROP_IMGSTORE_S "image"

enum
{
	SIG_DESTROY,
	SIG_LAST
};

static guint signals[SIG_LAST];
static GObjectClass *parent_class;

static void
purple_smiley_init(GTypeInstance *instance, gpointer klass)
{
	PurpleSmiley *smiley = PURPLE_SMILEY(instance);
	PURPLE_DBUS_REGISTER_POINTER(smiley, PurpleSmiley);
}

static void
purple_smiley_get_property(GObject *object, guint param_id, GValue *value,
		GParamSpec *spec)
{
	PurpleSmiley *smiley = PURPLE_SMILEY(object);
	PurpleSmileyPrivate *priv = PURPLE_SMILEY_GET_PRIVATE(smiley);

	switch (param_id) {
		case PROP_SHORTCUT:
			g_value_set_string(value, priv->shortcut);
			break;
		case PROP_IMGSTORE:
			g_value_set_pointer(value, priv->img);
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, spec);
			break;
	}
}

static void
purple_smiley_set_property(GObject *object, guint param_id, const GValue *value,
		GParamSpec *spec)
{
	PurpleSmiley *smiley = PURPLE_SMILEY(object);
	PurpleSmileyPrivate *priv = PURPLE_SMILEY_GET_PRIVATE(smiley);

	switch (param_id) {
		case PROP_SHORTCUT:
			{
				const char *shortcut = g_value_get_string(value);
				purple_smiley_set_shortcut(smiley, shortcut);
			}
			break;
		case PROP_IMGSTORE:
			{
				PurpleStoredImage *img = g_value_get_pointer(value);

				purple_imgstore_unref(priv->img);
				g_free(priv->checksum);

				priv->img = img;
				if (img) {
					priv->checksum = g_compute_checksum_for_data(
							G_CHECKSUM_SHA1,
							purple_imgstore_get_data(img),
							purple_imgstore_get_size(img));
					purple_smiley_data_store(img);
				} else {
					priv->checksum = NULL;
				}

				g_object_notify(object, PROP_IMGSTORE_S);
			}
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, spec);
			break;
	}
}

static void
purple_smiley_finalize(GObject *obj)
{
	PurpleSmiley *smiley = PURPLE_SMILEY(obj);
	PurpleSmileyPrivate *priv = PURPLE_SMILEY_GET_PRIVATE(smiley);

	if (g_hash_table_lookup(smiley_shortcut_index, priv->shortcut)) {
		g_hash_table_remove(smiley_shortcut_index, priv->shortcut);
		g_hash_table_remove(smiley_checksum_index, priv->checksum);
	}

	g_free(priv->shortcut);
	g_free(priv->checksum);
	if (priv->img)
		purple_smiley_data_unstore(purple_imgstore_get_filename(priv->img));
	purple_imgstore_unref(priv->img);

	PURPLE_DBUS_UNREGISTER_POINTER(smiley);

	purple_smileys_save();
}

static void
purple_smiley_dispose(GObject *gobj)
{
	g_signal_emit(gobj, signals[SIG_DESTROY], 0);
	parent_class->dispose(gobj);
}

static void
purple_smiley_class_init(PurpleSmileyClass *klass)
{
	GObjectClass *gobj_class = G_OBJECT_CLASS(klass);
	GParamSpec *pspec;

	parent_class = g_type_class_peek_parent(klass);

	g_type_class_add_private(klass, sizeof(PurpleSmileyPrivate));

	gobj_class->get_property = purple_smiley_get_property;
	gobj_class->set_property = purple_smiley_set_property;
	gobj_class->finalize = purple_smiley_finalize;
	gobj_class->dispose = purple_smiley_dispose;

	/* Shortcut */
	pspec = g_param_spec_string(PROP_SHORTCUT_S, _("Shortcut"),
			_("The text-shortcut for the smiley"),
			NULL,
			G_PARAM_READWRITE);
	g_object_class_install_property(gobj_class, PROP_SHORTCUT, pspec);

	/* Stored Image */
	pspec = g_param_spec_pointer(PROP_IMGSTORE_S, _("Stored Image"),
			_("Stored Image. (that'll have to do for now)"),
			G_PARAM_READWRITE);
	g_object_class_install_property(gobj_class, PROP_IMGSTORE, pspec);

	signals[SIG_DESTROY] = g_signal_new("destroy",
			G_OBJECT_CLASS_TYPE(klass),
			G_SIGNAL_RUN_LAST,
			0, NULL, NULL,
			g_cclosure_marshal_VOID__VOID,
			G_TYPE_NONE, 0);
}

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

	if(type == 0) {
		static const GTypeInfo info = {
			sizeof(PurpleSmileyClass),
			NULL,
			NULL,
			(GClassInitFunc)purple_smiley_class_init,
			NULL,
			NULL,
			sizeof(PurpleSmiley),
			0,
			purple_smiley_init,
			NULL,
		};

		type = g_type_register_static(G_TYPE_OBJECT,
				"PurpleSmiley",
				&info, 0);
	}

	return type;
}

/*********************************************************************
 * Other Stuff                                                       *
 *********************************************************************/

static char *get_file_full_path(const char *filename)
{
	char *path;

	path = g_build_filename(purple_smileys_get_storing_dir(), filename, NULL);

	if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
		g_free(path);
		return NULL;
	}

	return path;
}

static void
purple_smiley_load_file(const char *shortcut, const char *checksum, const char *filename)
{
	PurpleSmiley *smiley = NULL;
	PurpleSmileyPrivate *priv = NULL;
	guchar *smiley_data;
	size_t smiley_data_len;
	char *fullpath = NULL;

	g_return_if_fail(shortcut  != NULL);
	g_return_if_fail(checksum  != NULL);
	g_return_if_fail(filename != NULL);

	fullpath = get_file_full_path(filename);
	if (!fullpath) {
		purple_debug_error(SMILEYS_LOG_ID, "Path for filename %s doesn't exist\n", filename);
		return;
	}

	smiley = purple_smiley_create(shortcut);
	if (!smiley) {
		g_free(fullpath);
		return;
	}

	priv = PURPLE_SMILEY_GET_PRIVATE(smiley);

	priv->checksum = g_strdup(checksum);

	if (read_smiley_file(fullpath, &smiley_data, &smiley_data_len))
		purple_smiley_set_data_impl(smiley, smiley_data,
				smiley_data_len);
	else
		purple_smiley_delete(smiley);

	g_free(fullpath);

}

static void
purple_smiley_data_store(PurpleStoredImage *stored_img)
{
	const char *dirname;
	char *path;
	FILE *file = NULL;

	g_return_if_fail(stored_img != NULL);

	if (!smileys_loaded)
		return;

	dirname  = purple_smileys_get_storing_dir();
	path = g_build_filename(dirname, purple_imgstore_get_filename(stored_img), NULL);

	if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
		purple_debug_info(SMILEYS_LOG_ID, "Creating smileys directory.\n");

		if (g_mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR) < 0) {
			purple_debug_error(SMILEYS_LOG_ID,
			                   "Unable to create directory %s: %s\n",
			                   dirname, g_strerror(errno));
		}
	}

	if ((file = g_fopen(path, "wb")) != NULL) {
		if (!fwrite(purple_imgstore_get_data(stored_img),
				purple_imgstore_get_size(stored_img), 1, file)) {
			purple_debug_error(SMILEYS_LOG_ID, "Error writing %s: %s\n",
			                   path, g_strerror(errno));
		} else {
			purple_debug_info(SMILEYS_LOG_ID, "Wrote cache file: %s\n", path);
		}

		fclose(file);
	} else {
		purple_debug_error(SMILEYS_LOG_ID, "Unable to create file %s: %s\n",
		                   path, g_strerror(errno));
		g_free(path);

		return;
	}

	g_free(path);
}

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

	g_return_if_fail(filename != NULL);

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

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

	g_free(path);
}

static gboolean
read_smiley_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(SMILEYS_LOG_ID, "Error reading %s: %s\n",
				path, err->message);
		g_error_free(err);

		return FALSE;
	}

	return TRUE;
}

static PurpleStoredImage *
purple_smiley_data_new(guchar *smiley_data, size_t smiley_data_len)
{
	char *filename;
	PurpleStoredImage *stored_img;

	g_return_val_if_fail(smiley_data != NULL,   NULL);
	g_return_val_if_fail(smiley_data_len  > 0,  NULL);

	filename = purple_util_get_image_filename(smiley_data, smiley_data_len);

	if (filename == NULL) {
		g_free(smiley_data);
		return NULL;
	}

	stored_img = purple_imgstore_new(smiley_data, smiley_data_len, filename);

	g_free(filename);

	return stored_img;
}

static void
purple_smiley_set_data_impl(PurpleSmiley *smiley, guchar *smiley_data,
				size_t smiley_data_len)
{
	PurpleSmileyPrivate *priv = NULL;
	PurpleStoredImage *old_img, *new_img;
	const char *old_filename = NULL;
	const char *new_filename = NULL;

	g_return_if_fail(smiley     != NULL);
	g_return_if_fail(smiley_data != NULL);
	g_return_if_fail(smiley_data_len > 0);

	priv = PURPLE_SMILEY_GET_PRIVATE(smiley);

	old_img = priv->img;

	new_img = purple_smiley_data_new(smiley_data, smiley_data_len);

	g_object_set(G_OBJECT(smiley), PROP_IMGSTORE_S, new_img, NULL);

	/* If the old and new image files have different names we need
	 * to unstore old image file. */
	if (!old_img)
		return;

	old_filename = purple_imgstore_get_filename(old_img);
	new_filename = purple_imgstore_get_filename(priv->img);

	if (g_ascii_strcasecmp(old_filename, new_filename))
		purple_smiley_data_unstore(old_filename);
	purple_imgstore_unref(old_img);
}


/*****************************************************************************
 * Public API functions                                                      *
 *****************************************************************************/

static PurpleSmiley *
purple_smiley_create(const char *shortcut)
{
	PurpleSmiley *smiley;

	smiley = PURPLE_SMILEY(g_object_new(PURPLE_TYPE_SMILEY, PROP_SHORTCUT_S, shortcut, NULL));

	return smiley;
}

PurpleSmiley *
purple_smiley_new(PurpleStoredImage *img, const char *shortcut)
{
	PurpleSmiley *smiley = NULL;

	g_return_val_if_fail(shortcut  != NULL, NULL);
	g_return_val_if_fail(img       != NULL, NULL);

	smiley = purple_smileys_find_by_shortcut(shortcut);
	if (smiley)
		return smiley;

	smiley = purple_smiley_create(shortcut);
	if (!smiley)
		return NULL;

	g_object_set(G_OBJECT(smiley), PROP_IMGSTORE_S, img, NULL);

	return smiley;
}

static PurpleSmiley *
purple_smiley_new_from_stream(const char *shortcut, guchar *smiley_data,
			size_t smiley_data_len)
{
	PurpleSmiley *smiley = NULL;
	PurpleSmileyPrivate *priv = NULL;

	g_return_val_if_fail(shortcut  != NULL,    NULL);
	g_return_val_if_fail(smiley_data != NULL,  NULL);
	g_return_val_if_fail(smiley_data_len  > 0, NULL);

	smiley = purple_smileys_find_by_shortcut(shortcut);
	if (smiley)
		return smiley;

	/* purple_smiley_create() sets shortcut */
	smiley = purple_smiley_create(shortcut);
	if (!smiley)
		return NULL;

	priv = PURPLE_SMILEY_GET_PRIVATE(smiley);

	purple_smiley_set_data_impl(smiley, smiley_data, smiley_data_len);
	purple_smiley_data_store(priv->img);

	return smiley;
}

PurpleSmiley *
purple_smiley_new_from_file(const char *shortcut, const char *filepath)
{
	PurpleSmiley *smiley = NULL;
	guchar *smiley_data;
	size_t smiley_data_len;

	g_return_val_if_fail(shortcut  != NULL,  NULL);
	g_return_val_if_fail(filepath != NULL,  NULL);

	if (read_smiley_file(filepath, &smiley_data, &smiley_data_len)) {
		smiley = purple_smiley_new_from_stream(shortcut, smiley_data,
				smiley_data_len);
	}

	return smiley;
}

void
purple_smiley_delete(PurpleSmiley *smiley)
{
	g_return_if_fail(smiley != NULL);

	g_object_unref(smiley);
}

gboolean
purple_smiley_set_shortcut(PurpleSmiley *smiley, const char *shortcut)
{
	PurpleSmileyPrivate *priv = NULL;

	g_return_val_if_fail(smiley  != NULL, FALSE);
	g_return_val_if_fail(shortcut != NULL, FALSE);

	/* Check out whether the new shortcut is already being used. */
	if (g_hash_table_lookup(smiley_shortcut_index, shortcut))
		return FALSE;

	priv = PURPLE_SMILEY_GET_PRIVATE(smiley);

	/* Remove the old shortcut. */
	if (priv->shortcut)
		g_hash_table_remove(smiley_shortcut_index, priv->shortcut);

	/* Insert the new shortcut. */
	g_hash_table_insert(smiley_shortcut_index, g_strdup(shortcut), smiley);

	g_free(priv->shortcut);
	priv->shortcut = g_strdup(shortcut);

	g_object_notify(G_OBJECT(smiley), PROP_SHORTCUT_S);

	purple_smileys_save();

	return TRUE;
}

void
purple_smiley_set_data(PurpleSmiley *smiley, guchar *smiley_data,
			   size_t smiley_data_len)
{
	PurpleSmileyPrivate *priv = NULL;

	g_return_if_fail(smiley     != NULL);
	g_return_if_fail(smiley_data != NULL);
	g_return_if_fail(smiley_data_len > 0);

	priv = PURPLE_SMILEY_GET_PRIVATE(smiley);

	/* Remove the previous entry */
	g_hash_table_remove(smiley_checksum_index, priv->checksum);

	/* Update the file data. This also updates the checksum. */
	purple_smiley_set_data_impl(smiley, smiley_data, smiley_data_len);

	/* Reinsert the index item. */
	g_hash_table_insert(smiley_checksum_index, g_strdup(priv->checksum), smiley);

	purple_smileys_save();
}

PurpleStoredImage *
purple_smiley_get_stored_image(const PurpleSmiley *smiley)
{
	PurpleSmileyPrivate *priv = PURPLE_SMILEY_GET_PRIVATE(smiley);
	return purple_imgstore_ref(priv->img);
}

const char *purple_smiley_get_shortcut(const PurpleSmiley *smiley)
{
	PurpleSmileyPrivate *priv = NULL;

	g_return_val_if_fail(smiley != NULL, NULL);

	priv = PURPLE_SMILEY_GET_PRIVATE(smiley);
	return priv->shortcut;
}

const char *
purple_smiley_get_checksum(const PurpleSmiley *smiley)
{
	PurpleSmileyPrivate *priv = NULL;

	g_return_val_if_fail(smiley != NULL, NULL);

	priv = PURPLE_SMILEY_GET_PRIVATE(smiley);
	return priv->checksum;
}

gconstpointer
purple_smiley_get_data(const PurpleSmiley *smiley, size_t *len)
{
	PurpleSmileyPrivate *priv = NULL;

	g_return_val_if_fail(smiley != NULL, NULL);

	priv = PURPLE_SMILEY_GET_PRIVATE(smiley);

	if (priv->img) {
		if (len != NULL)
			*len = purple_imgstore_get_size(priv->img);

		return purple_imgstore_get_data(priv->img);
	}

	return NULL;
}

const char *
purple_smiley_get_extension(const PurpleSmiley *smiley)
{
	PurpleSmileyPrivate *priv = PURPLE_SMILEY_GET_PRIVATE(smiley);

	if (priv->img != NULL)
		return purple_imgstore_get_extension(priv->img);

	return NULL;
}

char *purple_smiley_get_full_path(PurpleSmiley *smiley)
{
	PurpleSmileyPrivate *priv = NULL;

	g_return_val_if_fail(smiley != NULL, NULL);

	priv = PURPLE_SMILEY_GET_PRIVATE(smiley);

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

	return get_file_full_path(purple_imgstore_get_filename(priv->img));
}

static void add_smiley_to_list(gpointer key, gpointer value, gpointer user_data)
{
	GList** returninglist = (GList**)user_data;

	*returninglist = g_list_append(*returninglist, value);
}

GList *
purple_smileys_get_all(void)
{
	GList *returninglist = NULL;

	g_hash_table_foreach(smiley_shortcut_index, add_smiley_to_list, &returninglist);

	return returninglist;
}

PurpleSmiley *
purple_smileys_find_by_shortcut(const char *shortcut)
{
	g_return_val_if_fail(shortcut != NULL, NULL);

	return g_hash_table_lookup(smiley_shortcut_index, shortcut);
}

PurpleSmiley *
purple_smileys_find_by_checksum(const char *checksum)
{
	g_return_val_if_fail(checksum != NULL, NULL);

	return g_hash_table_lookup(smiley_checksum_index, checksum);
}

const char *
purple_smileys_get_storing_dir(void)
{
	return smileys_dir;
}

void
purple_smileys_init(void)
{
	smiley_shortcut_index = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
	smiley_checksum_index = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);

	smileys_dir = g_build_filename(purple_user_dir(), SMILEYS_DEFAULT_FOLDER, NULL);

	purple_smileys_load();
}

void
purple_smileys_uninit(void)
{
	if (save_timer != 0) {
		purple_timeout_remove(save_timer);
		save_timer = 0;
		sync_smileys();
	}

	g_hash_table_destroy(smiley_shortcut_index);
	g_hash_table_destroy(smiley_checksum_index);
	g_free(smileys_dir);
}