summaryrefslogtreecommitdiff
path: root/libpurple/purplecontactinfo.c
blob: a5690823ef7da45a766de046c2dddb2952e07954 (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
/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * 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, see <https://www.gnu.org/licenses/>.
 */

#include "purplecontactinfo.h"

#include "purpleenums.h"
#include "util.h"

typedef struct  {
	char *id;

	char *username;
	char *display_name;
	char *alias;
	char *color;

	char *name_for_display;

	GdkPixbuf *avatar;

	PurplePresence *presence;

	PurpleTags *tags;

	PurplePerson *person;

	PurpleContactInfoPermission permission;
} PurpleContactInfoPrivate;

enum {
	PROP_0,
	PROP_ID,
	PROP_USERNAME,
	PROP_DISPLAY_NAME,
	PROP_ALIAS,
	PROP_COLOR,
	PROP_AVATAR,
	PROP_PRESENCE,
	PROP_TAGS,
	PROP_PERSON,
	PROP_PERMISSION,
	PROP_NAME_FOR_DISPLAY,
	N_PROPERTIES
};
static GParamSpec *properties[N_PROPERTIES] = {NULL, };

enum {
	SIG_PRESENCE_CHANGED,
	N_SIGNALS,
};
static guint signals[N_SIGNALS] = {0, };

G_DEFINE_TYPE_WITH_PRIVATE(PurpleContactInfo, purple_contact_info,
                           G_TYPE_OBJECT)

/******************************************************************************
 * Helpers
 *****************************************************************************/
static void
purple_contact_info_update_name_for_display(PurpleContactInfo *info) {
	PurpleContactInfoPrivate *priv = NULL;
	const char *name_for_display = NULL;

	priv = purple_contact_info_get_instance_private(info);

	/* If the info has an alias set, use it. */
	if(name_for_display == NULL && !purple_strempty(priv->alias)) {
		name_for_display = priv->alias;
	}

	/* If info is associated with a PurplePerson that has an alias set, use the
	 * alias of that PurplePerson.
	 */
	if(name_for_display == NULL && priv->person != NULL) {
		const char *alias = purple_person_get_alias(priv->person);

		if(!purple_strempty(alias)) {
			name_for_display = alias;
		}
	}

	/* If the info has a display name set, use it. */
	if(name_for_display == NULL && !purple_strempty(priv->display_name)) {
		name_for_display = priv->display_name;
	}

	/* Fallback to the username if that is set. */
	if(name_for_display == NULL && !purple_strempty(priv->username)) {
		name_for_display = priv->username;
	}

	/* Finally, in a last ditch effort, use the id of the info. */
	if(name_for_display == NULL) {
		name_for_display = priv->id;
	}

	if(!purple_strequal(name_for_display, priv->name_for_display)) {
		/* If we have a new name for display, free the old one, dup the new one
		 * into the struct, and then emit the notify signal.
		 */
		g_free(priv->name_for_display);
		priv->name_for_display = g_strdup(name_for_display);

		g_object_notify_by_pspec(G_OBJECT(info),
		                         properties[PROP_NAME_FOR_DISPLAY]);
	}
}

/******************************************************************************
 * Callbacks
 *****************************************************************************/
static void
purple_contact_info_person_alias_changed_cb(G_GNUC_UNUSED GObject *obj,
                                            G_GNUC_UNUSED GParamSpec *pspec,
                                            gpointer data)
{
	purple_contact_info_update_name_for_display(data);
}

/*
 * This is a notify callback on the presence for a contact info, it is used
 * to emit the presence-changed signal.
 */
static void
purple_contact_info_presence_notify_cb(GObject *source, GParamSpec *pspec,
                                       gpointer data)
{
	g_signal_emit(data, signals[SIG_PRESENCE_CHANGED],
	              g_param_spec_get_name_quark(pspec),
	              source, pspec);
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
static void
purple_contact_info_get_property(GObject *obj, guint param_id, GValue *value,
                                 GParamSpec *pspec)
{
	PurpleContactInfo *info = PURPLE_CONTACT_INFO(obj);

	switch(param_id) {
		case PROP_ID:
			g_value_set_string(value, purple_contact_info_get_id(info));
			break;
		case PROP_USERNAME:
			g_value_set_string(value, purple_contact_info_get_username(info));
			break;
		case PROP_DISPLAY_NAME:
			g_value_set_string(value,
			                   purple_contact_info_get_display_name(info));
			break;
		case PROP_ALIAS:
			g_value_set_string(value, purple_contact_info_get_alias(info));
			break;
		case PROP_COLOR:
			g_value_set_string(value, purple_contact_info_get_color(info));
			break;
		case PROP_AVATAR:
			g_value_set_object(value, purple_contact_info_get_avatar(info));
			break;
		case PROP_PRESENCE:
			g_value_set_object(value, purple_contact_info_get_presence(info));
			break;
		case PROP_TAGS:
			g_value_set_object(value, purple_contact_info_get_tags(info));
			break;
		case PROP_PERSON:
			g_value_set_object(value, purple_contact_info_get_person(info));
			break;
		case PROP_PERMISSION:
			g_value_set_enum(value, purple_contact_info_get_permission(info));
			break;
		case PROP_NAME_FOR_DISPLAY:
			g_value_set_string(value,
			                   purple_contact_info_get_name_for_display(info));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
			break;
	}
}

static void
purple_contact_info_set_property(GObject *obj, guint param_id,
                                 const GValue *value, GParamSpec *pspec)
{
	PurpleContactInfo *info = PURPLE_CONTACT_INFO(obj);

	switch(param_id) {
		case PROP_ID:
			purple_contact_info_set_id(info, g_value_get_string(value));
			break;
		case PROP_USERNAME:
			purple_contact_info_set_username(info, g_value_get_string(value));
			break;
		case PROP_DISPLAY_NAME:
			purple_contact_info_set_display_name(info,
			                                     g_value_get_string(value));
			break;
		case PROP_ALIAS:
			purple_contact_info_set_alias(info, g_value_get_string(value));
			break;
		case PROP_COLOR:
			purple_contact_info_set_color(info, g_value_get_string(value));
			break;
		case PROP_AVATAR:
			purple_contact_info_set_avatar(info, g_value_get_object(value));
			break;
		case PROP_PERSON:
			purple_contact_info_set_person(info, g_value_get_object(value));
			break;
		case PROP_PERMISSION:
			purple_contact_info_set_permission(info, g_value_get_enum(value));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
			break;
	}
}

static void
purple_contact_info_dispose(GObject *obj) {
	PurpleContactInfo *info = PURPLE_CONTACT_INFO(obj);
	PurpleContactInfoPrivate *priv = NULL;

	priv = purple_contact_info_get_instance_private(info);

	g_clear_object(&priv->avatar);
	g_clear_object(&priv->presence);
	g_clear_object(&priv->tags);
	g_clear_object(&priv->person);

	G_OBJECT_CLASS(purple_contact_info_parent_class)->dispose(obj);
}

static void
purple_contact_info_finalize(GObject *obj) {
	PurpleContactInfo *info = PURPLE_CONTACT_INFO(obj);
	PurpleContactInfoPrivate *priv = NULL;

	priv = purple_contact_info_get_instance_private(info);

	g_clear_pointer(&priv->id, g_free);
	g_clear_pointer(&priv->username, g_free);
	g_clear_pointer(&priv->display_name, g_free);
	g_clear_pointer(&priv->alias, g_free);
	g_clear_pointer(&priv->color, g_free);
	g_clear_pointer(&priv->name_for_display, g_free);

	G_OBJECT_CLASS(purple_contact_info_parent_class)->finalize(obj);
}

static void
purple_contact_info_constructed(GObject *obj) {
	PurpleContactInfo *info = NULL;
	PurpleContactInfoPrivate *priv = NULL;

	G_OBJECT_CLASS(purple_contact_info_parent_class)->constructed(obj);

	info = PURPLE_CONTACT_INFO(obj);
	priv = purple_contact_info_get_instance_private(info);

	if(priv->id == NULL) {
		purple_contact_info_set_id(info, NULL);
	}

	purple_contact_info_update_name_for_display(info);
}

static void
purple_contact_info_init(PurpleContactInfo *info) {
	PurpleContactInfoPrivate *priv = NULL;

	priv = purple_contact_info_get_instance_private(info);

	priv->tags = purple_tags_new();

	priv->presence = g_object_new(PURPLE_TYPE_PRESENCE, NULL);
	g_signal_connect_object(priv->presence, "notify",
	                        G_CALLBACK(purple_contact_info_presence_notify_cb),
	                        info, 0);
}

static void
purple_contact_info_class_init(PurpleContactInfoClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->constructed = purple_contact_info_constructed;
	obj_class->dispose = purple_contact_info_dispose;
	obj_class->finalize = purple_contact_info_finalize;
	obj_class->get_property = purple_contact_info_get_property;
	obj_class->set_property = purple_contact_info_set_property;

	/**
	 * PurpleContactInfo:id:
	 *
	 * The protocol specific id for the contact.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_ID] = g_param_spec_string(
		"id", "id",
		"The id of the contact",
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleContactInfo:username:
	 *
	 * The username for this contact. In rare cases this can change, like when
	 * a user changes their "nick" on IRC which is their user name.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_USERNAME] = g_param_spec_string(
		"username", "username",
		"The username of the contact",
		NULL,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleContactInfo:display-name:
	 *
	 * The display name for this contact. This is generally set by the person
	 * the contact is representing and controlled via the protocol plugin.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_DISPLAY_NAME] = g_param_spec_string(
		"display-name", "display-name",
		"The display name of the contact",
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleContactInfo:alias:
	 *
	 * The alias for this contact. This is controlled by the libpurple user and
	 * may be used by the protocol if it allows for aliasing.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_ALIAS] = g_param_spec_string(
		"alias", "alias",
		"The alias of the contact.",
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleContactInfo:color:
	 *
	 * The color for this contact. This is an RGB hex code that user interfaces
	 * can use when rendering the contact. This may also be controlled via a
	 * protocol plugin in the event that the protocol allows people to set a
	 * highlight/branding color.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_COLOR] = g_param_spec_string(
		"color", "color",
		"The color to use when rendering the contact.",
		NULL,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleContactInfo:avatar:
	 *
	 * The avatar for this contact. This is typically controlled by the protocol
	 * and should only be read by others.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_AVATAR] = g_param_spec_object(
		"avatar", "avatar",
		"The avatar of the contact",
		GDK_TYPE_PIXBUF,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleContactInfo:presence:
	 *
	 * The [class@Purple.Presence] for this contact. This is typically
	 * controlled by the protocol and should only be read by others.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_PRESENCE] = g_param_spec_object(
		"presence", "presence",
		"The presence of the contact",
		PURPLE_TYPE_PRESENCE,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleContactInfo:tags:
	 *
	 * The [class@Purple.Tags] for this contact.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_TAGS] = g_param_spec_object(
		"tags", "tags",
		"The tags for the contact",
		PURPLE_TYPE_TAGS,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleContactInfo:person:
	 *
	 * The [class@Purple.Person] that this contact belongs to.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_PERSON] = g_param_spec_object(
		"person", "person",
		"The person this contact belongs to.",
		PURPLE_TYPE_PERSON,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleContactInfo:permission:
	 *
	 * The permission level for the contact.
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_PERMISSION] = g_param_spec_enum(
		"permission", "permission",
		"The permission level of the contact",
		PURPLE_TYPE_CONTACT_INFO_PERMISSION,
		PURPLE_CONTACT_INFO_PERMISSION_UNSET,
		G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleContactInfo:name-for-display:
	 *
	 * The name that the user interface should display for this contact info.
	 *
	 * This will first check [property@Purple.ContactInfo:alias] and return
	 * that if it is set.
	 *
	 * Next, if the [property@Purple.ContactInfo:person] points to a valid
	 * [class@Purple.Person], the alias of [class@Purple.Person] will be
	 * returned if it is set.
	 *
	 * Otherwise, this will be set to the first set property from the following
	 * list:
	 *
	 *  * [property@Purple.ContactInfo:display-name]
	 *  * [property@Purple.ContactInfo:username]
	 *  * [property@Purple.ContactInfo:id]
	 *
	 * Since: 3.0.0
	 */
	properties[PROP_NAME_FOR_DISPLAY] = g_param_spec_string(
		"name-for-display", "name-for-display",
		"The name that should be displayed for the contact info",
		NULL,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);

	/**
	 * PurpleContactInfo::presence-changed:
	 * @info: The instance.
	 * @presence: The presence that was changed.
	 * @pspec: The [class@GObject.ParamSpec] for the property that changed.
	 *
	 * This is a propagation of the notify signal from @presence. This means
	 * that your callback will be called when anything in the presence changes.
	 *
	 * This also supports details, so you can specify the signal name as
	 * something like `presence-changed::message` and your callback will only
	 * be called when the message property of @presence has been changed.
	 *
	 * Since: 3.0.0
	 */
	signals[SIG_PRESENCE_CHANGED] = g_signal_new_class_handler(
		"presence-changed",
		G_OBJECT_CLASS_TYPE(klass),
		G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
		NULL,
		NULL,
		NULL,
		NULL,
		G_TYPE_NONE,
		2,
		PURPLE_TYPE_PRESENCE,
		G_TYPE_PARAM);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
PurpleContactInfo *
purple_contact_info_new(const gchar *id) {
	return g_object_new(
		PURPLE_TYPE_CONTACT_INFO,
		"id", id,
		NULL);
}

const gchar *
purple_contact_info_get_id(PurpleContactInfo *info) {
	PurpleContactInfoPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_CONTACT_INFO(info), NULL);

	priv = purple_contact_info_get_instance_private(info);

	return priv->id;
}

void
purple_contact_info_set_id(PurpleContactInfo *info, const gchar *id) {
	PurpleContactInfoPrivate *priv = NULL;
	gboolean changed = FALSE;

	g_return_if_fail(PURPLE_IS_CONTACT_INFO(info));

	priv = purple_contact_info_get_instance_private(info);

	changed = !purple_strequal(priv->id, id);

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

	if(changed) {
		g_object_freeze_notify(G_OBJECT(info));

		g_object_notify_by_pspec(G_OBJECT(info), properties[PROP_ID]);

		purple_contact_info_update_name_for_display(info);

		g_object_thaw_notify(G_OBJECT(info));
	}
}

const gchar *
purple_contact_info_get_username(PurpleContactInfo *info) {
	PurpleContactInfoPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_CONTACT_INFO(info), NULL);

	priv = purple_contact_info_get_instance_private(info);

	return priv->username;
}

void
purple_contact_info_set_username(PurpleContactInfo *info,
                                 const gchar *username)
{
	PurpleContactInfoPrivate *priv = NULL;
	gboolean changed = FALSE;

	g_return_if_fail(PURPLE_IS_CONTACT_INFO(info));

	priv = purple_contact_info_get_instance_private(info);

	changed = !purple_strequal(priv->username, username);

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

	if(changed) {
		g_object_freeze_notify(G_OBJECT(info));

		g_object_notify_by_pspec(G_OBJECT(info), properties[PROP_USERNAME]);

		purple_contact_info_update_name_for_display(info);

		g_object_thaw_notify(G_OBJECT(info));
	}
}

const gchar *
purple_contact_info_get_display_name(PurpleContactInfo *info) {
	PurpleContactInfoPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_CONTACT_INFO(info), NULL);

	priv = purple_contact_info_get_instance_private(info);

	return priv->display_name;
}

void
purple_contact_info_set_display_name(PurpleContactInfo *info,
                                     const gchar *display_name)
{
	PurpleContactInfoPrivate *priv = NULL;
	gboolean changed = FALSE;

	g_return_if_fail(PURPLE_IS_CONTACT_INFO(info));

	priv = purple_contact_info_get_instance_private(info);

	changed = !purple_strequal(priv->display_name, display_name);

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

	if(changed) {
		g_object_freeze_notify(G_OBJECT(info));

		g_object_notify_by_pspec(G_OBJECT(info), properties[PROP_DISPLAY_NAME]);

		purple_contact_info_update_name_for_display(info);

		g_object_thaw_notify(G_OBJECT(info));
	}
}

const gchar *
purple_contact_info_get_alias(PurpleContactInfo *info) {
	PurpleContactInfoPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_CONTACT_INFO(info), NULL);

	priv = purple_contact_info_get_instance_private(info);

	return priv->alias;
}

void
purple_contact_info_set_alias(PurpleContactInfo *info, const gchar *alias) {
	PurpleContactInfoPrivate *priv = NULL;
	gboolean changed = FALSE;

	g_return_if_fail(PURPLE_IS_CONTACT_INFO(info));

	priv = purple_contact_info_get_instance_private(info);

	changed = !purple_strequal(priv->alias, alias);

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

	if(changed) {
		g_object_freeze_notify(G_OBJECT(info));

		g_object_notify_by_pspec(G_OBJECT(info), properties[PROP_ALIAS]);

		purple_contact_info_update_name_for_display(info);

		g_object_thaw_notify(G_OBJECT(info));
	}
}

const char *
purple_contact_info_get_color(PurpleContactInfo *info) {
	PurpleContactInfoPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_CONTACT_INFO(info), NULL);

	priv = purple_contact_info_get_instance_private(info);

	return priv->color;
}

void
purple_contact_info_set_color(PurpleContactInfo *info, const char *color) {
	PurpleContactInfoPrivate *priv = NULL;

	g_return_if_fail(PURPLE_IS_CONTACT_INFO(info));

	priv = purple_contact_info_get_instance_private(info);

	if(!purple_strequal(priv->color, color)) {
		g_free(priv->color);
		priv->color = g_strdup(color);

		g_object_notify_by_pspec(G_OBJECT(info), properties[PROP_COLOR]);
	}
}

GdkPixbuf *
purple_contact_info_get_avatar(PurpleContactInfo *info) {
	PurpleContactInfoPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_CONTACT_INFO(info), NULL);

	priv = purple_contact_info_get_instance_private(info);

	return priv->avatar;
}

void
purple_contact_info_set_avatar(PurpleContactInfo *info, GdkPixbuf *avatar) {
	PurpleContactInfoPrivate *priv = NULL;

	g_return_if_fail(PURPLE_IS_CONTACT_INFO(info));

	priv = purple_contact_info_get_instance_private(info);

	if(g_set_object(&priv->avatar, avatar)) {
		g_object_notify_by_pspec(G_OBJECT(info), properties[PROP_AVATAR]);
	}
}

PurplePresence *
purple_contact_info_get_presence(PurpleContactInfo *info) {
	PurpleContactInfoPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_CONTACT_INFO(info), NULL);

	priv = purple_contact_info_get_instance_private(info);

	return priv->presence;
}

PurpleTags *
purple_contact_info_get_tags(PurpleContactInfo *info) {
	PurpleContactInfoPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_CONTACT_INFO(info), NULL);

	priv = purple_contact_info_get_instance_private(info);

	return priv->tags;
}

void
purple_contact_info_set_person(PurpleContactInfo *info, PurplePerson *person) {
	PurpleContactInfoPrivate *priv = NULL;

	g_return_if_fail(PURPLE_IS_CONTACT_INFO(info));

	priv = purple_contact_info_get_instance_private(info);

	if(g_set_object(&priv->person, person)) {
		/* If we got a new person, we need to connect to the notify::alias
		 * signal.
		 */
		if(PURPLE_IS_PERSON(priv->person)) {
			g_signal_connect_object(priv->person, "notify::alias",
			                        G_CALLBACK(purple_contact_info_person_alias_changed_cb),
			                        info, 0);
		}

		/* Freeze notifications as the person update could change the
		 * name-for-display property.
		 */
		g_object_freeze_notify(G_OBJECT(info));

		g_object_notify_by_pspec(G_OBJECT(info), properties[PROP_PERSON]);

		/* Update the name-for-display property */
		purple_contact_info_update_name_for_display(info);

		g_object_thaw_notify(G_OBJECT(info));
	}
}

PurplePerson *
purple_contact_info_get_person(PurpleContactInfo *info) {
	PurpleContactInfoPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_CONTACT_INFO(info), NULL);

	priv = purple_contact_info_get_instance_private(info);

	return priv->person;
}

PurpleContactInfoPermission
purple_contact_info_get_permission(PurpleContactInfo *info) {
	PurpleContactInfoPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_CONTACT_INFO(info),
	                     PURPLE_CONTACT_INFO_PERMISSION_UNSET);

	priv = purple_contact_info_get_instance_private(info);

	return priv->permission;
}

void
purple_contact_info_set_permission(PurpleContactInfo *info,
                                   PurpleContactInfoPermission permission)
{
	PurpleContactInfoPrivate *priv = NULL;

	g_return_if_fail(PURPLE_IS_CONTACT_INFO(info));

	priv = purple_contact_info_get_instance_private(info);

	priv->permission = permission;

	g_object_notify_by_pspec(G_OBJECT(info), properties[PROP_PERMISSION]);
}

const char *
purple_contact_info_get_name_for_display(PurpleContactInfo *info) {
	PurpleContactInfoPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_CONTACT_INFO(info), NULL);

	priv = purple_contact_info_get_instance_private(info);

	return priv->name_for_display;
}

int
purple_contact_info_compare(PurpleContactInfo *a, PurpleContactInfo *b) {
	PurplePerson *person_a = NULL;
	PurplePerson *person_b = NULL;
	const char *name_a = NULL;
	const char *name_b = NULL;

	/* Check for NULL values. */
	if(a != NULL && b == NULL) {
		return -1;
	} else if(a == NULL && b != NULL) {
		return 1;
	} else if(a == NULL && b == NULL) {
		return 0;
	}

	/* Check if the contacts have persons associated with them. */
	person_a = purple_contact_info_get_person(a);
	person_b = purple_contact_info_get_person(b);

	if(person_a != NULL && person_b == NULL) {
		return -1;
	} else if(person_a == NULL && person_b != NULL) {
		return 1;
	}

	/* Finally get the names for the displaying and compare those. */
	name_a = purple_contact_info_get_name_for_display(a);
	name_b = purple_contact_info_get_name_for_display(b);

	return purple_utf8_strcasecmp(name_a, name_b);
}

gboolean
purple_contact_info_matches(PurpleContactInfo *info, const char *needle) {
	PurpleContactInfoPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_CONTACT_INFO(info), FALSE);

	if(purple_strempty(needle)) {
		return TRUE;
	}

	priv = purple_contact_info_get_instance_private(info);

	if(!purple_strempty(priv->username)) {
		if(purple_strmatches(needle, priv->username)) {
			return TRUE;
		}
	}

	if(!purple_strempty(priv->alias)) {
		if(purple_strmatches(needle, priv->alias)) {
			return TRUE;
		}
	}

	if(!purple_strempty(priv->display_name)) {
		if(purple_strmatches(needle, priv->display_name)) {
			return TRUE;
		}
	}

	/* Nothing matched, so return FALSE. */
	return FALSE;
}