1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2004-2007 Imendio AB
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Mikael Hallendal <micke@imendio.com>
21 * Martyn Russell <martyn@imendio.com>
28 #include <glib/gi18n.h>
30 #include "gossip-contact.h"
31 #include "gossip-utils.h"
32 #include "gossip-debug.h"
34 #define DEBUG_DOMAIN "Contact"
36 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_CONTACT, GossipContactPriv))
38 typedef struct _GossipContactPriv GossipContactPriv;
40 struct _GossipContactPriv {
46 GossipSubscription subscription;
51 static void contact_class_init (GossipContactClass *class);
52 static void contact_init (GossipContact *contact);
53 static void contact_finalize (GObject *object);
54 static void contact_get_property (GObject *object,
58 static void contact_set_property (GObject *object,
62 static void contact_set_presences (GossipContact *contact,
77 static gpointer parent_class = NULL;
80 gossip_contact_get_gtype (void)
82 static GType type = 0;
85 static const GTypeInfo info = {
86 sizeof (GossipContactClass),
88 NULL, /* base_finalize */
89 (GClassInitFunc) contact_class_init,
90 NULL, /* class_finalize */
91 NULL, /* class_data */
92 sizeof (GossipContact),
94 (GInstanceInitFunc) contact_init
97 type = g_type_register_static (G_TYPE_OBJECT,
106 contact_class_init (GossipContactClass *class)
108 GObjectClass *object_class;
110 object_class = G_OBJECT_CLASS (class);
111 parent_class = g_type_class_peek_parent (class);
113 object_class->finalize = contact_finalize;
114 object_class->get_property = contact_get_property;
115 object_class->set_property = contact_set_property;
117 g_object_class_install_property (object_class,
119 g_param_spec_string ("name",
121 "The name of the contact",
125 g_object_class_install_property (object_class,
127 g_param_spec_string ("id",
129 "String identifying contact",
133 g_object_class_install_property (object_class,
135 g_param_spec_pointer ("presences",
137 "Presences of contact",
140 g_object_class_install_property (object_class,
142 g_param_spec_pointer ("groups",
147 g_object_class_install_property (object_class,
149 g_param_spec_int ("subscription",
150 "Contact Subscription",
151 "The subscription status of the contact",
152 GOSSIP_SUBSCRIPTION_NONE,
153 GOSSIP_SUBSCRIPTION_BOTH,
154 GOSSIP_SUBSCRIPTION_NONE,
157 g_object_class_install_property (object_class,
159 g_param_spec_boxed ("avatar",
165 g_object_class_install_property (object_class,
167 g_param_spec_object ("account",
169 "The account associated with the contact",
173 g_object_class_install_property (object_class,
175 g_param_spec_uint ("handle",
177 "The handle of the contact",
183 g_type_class_add_private (object_class, sizeof (GossipContactPriv));
187 contact_init (GossipContact *contact)
189 GossipContactPriv *priv;
191 priv = GET_PRIV (contact);
195 priv->presences = NULL;
196 priv->account = NULL;
203 contact_finalize (GObject *object)
205 GossipContactPriv *priv;
207 priv = GET_PRIV (object);
209 gossip_debug (DEBUG_DOMAIN, "finalize: %p", object);
215 gossip_avatar_unref (priv->avatar);
218 if (priv->presences) {
219 g_list_foreach (priv->presences, (GFunc) g_object_unref, NULL);
220 g_list_free (priv->presences);
224 g_list_foreach (priv->groups, (GFunc) g_free, NULL);
225 g_list_free (priv->groups);
229 g_object_unref (priv->account);
232 (G_OBJECT_CLASS (parent_class)->finalize) (object);
236 contact_get_property (GObject *object,
241 GossipContactPriv *priv;
243 priv = GET_PRIV (object);
247 g_value_set_string (value,
248 gossip_contact_get_name (GOSSIP_CONTACT (object)));
251 g_value_set_string (value,
252 gossip_contact_get_id (GOSSIP_CONTACT (object)));
255 g_value_set_pointer (value, priv->presences);
258 g_value_set_pointer (value, priv->groups);
260 case PROP_SUBSCRIPTION:
261 g_value_set_int (value, priv->subscription);
264 g_value_set_boxed (value, priv->avatar);
267 g_value_set_object (value, priv->account);
270 g_value_set_uint (value, priv->handle);
273 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
279 contact_set_property (GObject *object,
284 GossipContactPriv *priv;
286 priv = GET_PRIV (object);
290 gossip_contact_set_name (GOSSIP_CONTACT (object),
291 g_value_get_string (value));
294 gossip_contact_set_id (GOSSIP_CONTACT (object),
295 g_value_get_string (value));
298 contact_set_presences (GOSSIP_CONTACT (object),
299 g_value_get_pointer (value));
302 gossip_contact_set_groups (GOSSIP_CONTACT (object),
303 g_value_get_pointer (value));
305 case PROP_SUBSCRIPTION:
306 gossip_contact_set_subscription (GOSSIP_CONTACT (object),
307 g_value_get_int (value));
310 gossip_contact_set_avatar (GOSSIP_CONTACT (object),
311 g_value_get_boxed (value));
314 gossip_contact_set_account (GOSSIP_CONTACT (object),
315 MC_ACCOUNT (g_value_get_object (value)));
318 gossip_contact_set_handle (GOSSIP_CONTACT (object),
319 g_value_get_uint (value));
322 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
328 gossip_contact_new (McAccount *account)
330 return g_object_new (GOSSIP_TYPE_CONTACT,
336 gossip_contact_new_full (McAccount *account,
340 return g_object_new (GOSSIP_TYPE_CONTACT,
348 gossip_contact_get_id (GossipContact *contact)
350 GossipContactPriv *priv;
352 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), "");
354 priv = GET_PRIV (contact);
364 gossip_contact_get_name (GossipContact *contact)
366 GossipContactPriv *priv;
368 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), "");
370 priv = GET_PRIV (contact);
372 if (priv->name == NULL) {
373 return gossip_contact_get_id (contact);
380 gossip_contact_get_avatar (GossipContact *contact)
382 GossipContactPriv *priv;
384 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
386 priv = GET_PRIV (contact);
392 gossip_contact_get_account (GossipContact *contact)
394 GossipContactPriv *priv;
396 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
398 priv = GET_PRIV (contact);
400 return priv->account;
404 gossip_contact_get_active_presence (GossipContact *contact)
406 GossipContactPriv *priv;
408 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
410 priv = GET_PRIV (contact);
412 if (priv->presences) {
413 /* Highest priority of the presences is first */
414 return GOSSIP_PRESENCE (priv->presences->data);
421 gossip_contact_get_presence_for_resource (GossipContact *contact,
422 const gchar *resource)
424 GossipContactPriv *priv;
427 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
428 g_return_val_if_fail (resource != NULL, NULL);
430 priv = GET_PRIV (contact);
432 for (l = priv->presences; l; l = l->next) {
435 p_res = gossip_presence_get_resource (GOSSIP_PRESENCE (l->data));
436 if (p_res && strcmp (resource, p_res) == 0) {
437 return GOSSIP_PRESENCE (l->data);
445 gossip_contact_get_presence_list (GossipContact *contact)
447 GossipContactPriv *priv;
449 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
451 priv = GET_PRIV (contact);
453 return priv->presences;
457 gossip_contact_get_groups (GossipContact *contact)
459 GossipContactPriv *priv;
461 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
463 priv = GET_PRIV (contact);
469 gossip_contact_get_subscription (GossipContact *contact)
471 GossipContactPriv *priv;
473 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact),
474 GOSSIP_SUBSCRIPTION_NONE);
476 priv = GET_PRIV (contact);
478 return priv->subscription;
482 gossip_contact_get_handle (GossipContact *contact)
484 GossipContactPriv *priv;
486 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), 0);
488 priv = GET_PRIV (contact);
494 gossip_contact_set_id (GossipContact *contact,
497 GossipContactPriv *priv;
499 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
500 g_return_if_fail (id != NULL);
502 priv = GET_PRIV (contact);
505 priv->id = g_strdup (id);
507 g_object_notify (G_OBJECT (contact), "id");
511 gossip_contact_set_name (GossipContact *contact,
514 GossipContactPriv *priv;
516 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
517 g_return_if_fail (name != NULL);
519 priv = GET_PRIV (contact);
522 priv->name = g_strdup (name);
524 g_object_notify (G_OBJECT (contact), "name");
528 contact_set_presences (GossipContact *contact,
531 GossipContactPriv *priv;
533 priv = GET_PRIV (contact);
535 if (priv->presences) {
536 g_list_foreach (priv->presences, (GFunc) g_object_unref, NULL);
537 g_list_free (priv->presences);
540 priv->presences = g_list_copy (presences);
541 g_list_foreach (priv->presences, (GFunc) g_object_ref, NULL);
543 g_object_notify (G_OBJECT (contact), "presences");
547 gossip_contact_set_avatar (GossipContact *contact,
548 GossipAvatar *avatar)
550 GossipContactPriv *priv;
552 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
554 priv = GET_PRIV (contact);
557 gossip_avatar_unref (priv->avatar);
562 priv->avatar = gossip_avatar_ref (avatar);
565 g_object_notify (G_OBJECT (contact), "avatar");
569 gossip_contact_set_account (GossipContact *contact,
572 GossipContactPriv *priv;
574 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
575 g_return_if_fail (MC_IS_ACCOUNT (account));
577 priv = GET_PRIV (contact);
580 g_object_unref (priv->account);
584 priv->account = g_object_ref (account);
586 priv->account = NULL;
589 g_object_notify (G_OBJECT (contact), "account");
593 gossip_contact_add_presence (GossipContact *contact,
594 GossipPresence *presence)
596 GossipContactPriv *priv;
597 GossipPresence *this_presence;
600 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
601 g_return_if_fail (GOSSIP_IS_PRESENCE (presence));
603 priv = GET_PRIV (contact);
605 for (l = priv->presences; l && presence; l = l->next) {
606 this_presence = l->data;
608 if (gossip_presence_resource_equal (this_presence, presence)) {
611 ref_count = G_OBJECT (presence)->ref_count;
613 /* Remove old presence for this resource, we
614 * would use g_list_remove_all() here but we
615 * want to make sure we unref for each
616 * instance we find it in the list.
618 priv->presences = g_list_remove (priv->presences, this_presence);
619 g_object_unref (this_presence);
621 if (!priv->presences || ref_count <= 1) {
625 /* Reset list to beginnging to make sure we
626 * didn't miss any duplicates.
632 /* Add new presence */
633 priv->presences = g_list_insert_sorted (priv->presences,
634 g_object_ref (presence),
635 gossip_presence_sort_func);
637 g_object_notify (G_OBJECT (contact), "presences");
641 gossip_contact_remove_presence (GossipContact *contact,
642 GossipPresence *presence)
644 GossipContactPriv *priv;
645 GossipPresence *this_presence;
648 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
649 g_return_if_fail (GOSSIP_IS_PRESENCE (presence));
651 priv = GET_PRIV (contact);
653 for (l = priv->presences; l; l = l->next) {
654 this_presence = l->data;
656 if (gossip_presence_resource_equal (this_presence, presence)) {
659 ref_count = G_OBJECT (presence)->ref_count;
661 /* Remove old presence for this resource, we
662 * would use g_list_remove_all() here but we
663 * want to make sure we unref for each
664 * instance we find it in the list.
666 priv->presences = g_list_remove (priv->presences, this_presence);
667 g_object_unref (this_presence);
669 if (!priv->presences || ref_count <= 1) {
673 /* Reset list to beginnging to make sure we
674 * didn't miss any duplicates.
680 priv->presences = g_list_sort (priv->presences,
681 gossip_presence_sort_func);
683 g_object_notify (G_OBJECT (contact), "presences");
687 gossip_contact_set_groups (GossipContact *contact,
690 GossipContactPriv *priv;
691 GList *old_groups, *l;
693 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
695 priv = GET_PRIV (contact);
697 old_groups = priv->groups;
700 for (l = groups; l; l = l->next) {
701 priv->groups = g_list_append (priv->groups,
705 g_list_foreach (old_groups, (GFunc) g_free, NULL);
706 g_list_free (old_groups);
708 g_object_notify (G_OBJECT (contact), "groups");
712 gossip_contact_set_subscription (GossipContact *contact,
713 GossipSubscription subscription)
715 GossipContactPriv *priv;
717 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
719 priv = GET_PRIV (contact);
721 priv->subscription = subscription;
723 g_object_notify (G_OBJECT (contact), "subscription");
727 gossip_contact_set_handle (GossipContact *contact,
730 GossipContactPriv *priv;
732 priv = GET_PRIV (contact);
734 priv->handle = handle;
736 g_object_notify (G_OBJECT (contact), "handle");
740 gossip_contact_is_online (GossipContact *contact)
742 GossipContactPriv *priv;
744 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), FALSE);
746 priv = GET_PRIV (contact);
748 if (priv->presences == NULL) {
756 gossip_contact_get_status (GossipContact *contact)
758 GossipContactPriv *priv;
760 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), "");
762 priv = GET_PRIV (contact);
764 if (priv->presences) {
768 p = GOSSIP_PRESENCE (priv->presences->data);
769 status = gossip_presence_get_status (p);
771 status = gossip_presence_state_get_default_status (gossip_presence_get_state (p));
780 gossip_contact_equal (gconstpointer v1,
783 McAccount *account_a;
784 McAccount *account_b;
788 g_return_val_if_fail (GOSSIP_IS_CONTACT (v1), FALSE);
789 g_return_val_if_fail (GOSSIP_IS_CONTACT (v2), FALSE);
791 account_a = gossip_contact_get_account (GOSSIP_CONTACT (v1));
792 account_b = gossip_contact_get_account (GOSSIP_CONTACT (v2));
794 id_a = gossip_contact_get_id (GOSSIP_CONTACT (v1));
795 id_b = gossip_contact_get_id (GOSSIP_CONTACT (v2));
797 return gossip_account_equal (account_a, account_b) && g_str_equal (id_a, id_b);
801 gossip_contact_hash (gconstpointer key)
803 GossipContactPriv *priv;
806 g_return_val_if_fail (GOSSIP_IS_CONTACT (key), +1);
808 priv = GET_PRIV (GOSSIP_CONTACT (key));
810 hash = gossip_account_hash (gossip_contact_get_account (GOSSIP_CONTACT (key)));
811 hash += g_str_hash (gossip_contact_get_id (GOSSIP_CONTACT (key)));