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"
33 #include "empathy-contact-manager.h"
35 #define DEBUG_DOMAIN "Contact"
37 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_CONTACT, GossipContactPriv))
39 typedef struct _GossipContactPriv GossipContactPriv;
41 struct _GossipContactPriv {
46 GossipPresence *presence;
48 GossipSubscription subscription;
52 static void contact_class_init (GossipContactClass *class);
53 static void contact_init (GossipContact *contact);
54 static void contact_finalize (GObject *object);
55 static void contact_get_property (GObject *object,
59 static void contact_set_property (GObject *object,
76 static gpointer parent_class = NULL;
79 gossip_contact_get_gtype (void)
81 static GType type = 0;
84 static const GTypeInfo info = {
85 sizeof (GossipContactClass),
87 NULL, /* base_finalize */
88 (GClassInitFunc) contact_class_init,
89 NULL, /* class_finalize */
90 NULL, /* class_data */
91 sizeof (GossipContact),
93 (GInstanceInitFunc) contact_init
96 type = g_type_register_static (G_TYPE_OBJECT,
105 contact_class_init (GossipContactClass *class)
107 GObjectClass *object_class;
109 object_class = G_OBJECT_CLASS (class);
110 parent_class = g_type_class_peek_parent (class);
112 object_class->finalize = contact_finalize;
113 object_class->get_property = contact_get_property;
114 object_class->set_property = contact_set_property;
116 g_object_class_install_property (object_class,
118 g_param_spec_string ("id",
120 "String identifying contact",
124 g_object_class_install_property (object_class,
126 g_param_spec_string ("name",
128 "The name of the contact",
132 g_object_class_install_property (object_class,
134 g_param_spec_boxed ("avatar",
140 g_object_class_install_property (object_class,
142 g_param_spec_object ("account",
144 "The account associated with the contact",
148 g_object_class_install_property (object_class,
150 g_param_spec_object ("presence",
152 "Presence of contact",
153 GOSSIP_TYPE_PRESENCE,
156 g_object_class_install_property (object_class,
158 g_param_spec_pointer ("groups",
163 g_object_class_install_property (object_class,
165 g_param_spec_int ("subscription",
166 "Contact Subscription",
167 "The subscription status of the contact",
168 GOSSIP_SUBSCRIPTION_NONE,
169 GOSSIP_SUBSCRIPTION_BOTH,
170 GOSSIP_SUBSCRIPTION_NONE,
174 g_object_class_install_property (object_class,
176 g_param_spec_uint ("handle",
178 "The handle of the contact",
184 g_type_class_add_private (object_class, sizeof (GossipContactPriv));
188 contact_init (GossipContact *contact)
190 GossipContactPriv *priv;
192 priv = GET_PRIV (contact);
197 priv->account = NULL;
198 priv->presence = NULL;
204 contact_finalize (GObject *object)
206 GossipContactPriv *priv;
208 priv = GET_PRIV (object);
210 gossip_debug (DEBUG_DOMAIN, "finalize: %p", object);
216 gossip_avatar_unref (priv->avatar);
219 if (priv->presence) {
220 g_object_unref (priv->presence);
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_id (GOSSIP_CONTACT (object)));
251 g_value_set_string (value,
252 gossip_contact_get_name (GOSSIP_CONTACT (object)));
255 g_value_set_boxed (value, priv->avatar);
258 g_value_set_object (value, priv->account);
261 g_value_set_object (value, priv->presence);
264 g_value_set_pointer (value, priv->groups);
266 case PROP_SUBSCRIPTION:
267 g_value_set_int (value, priv->subscription);
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_id (GOSSIP_CONTACT (object),
291 g_value_get_string (value));
294 gossip_contact_set_name (GOSSIP_CONTACT (object),
295 g_value_get_string (value));
298 gossip_contact_set_avatar (GOSSIP_CONTACT (object),
299 g_value_get_boxed (value));
302 gossip_contact_set_account (GOSSIP_CONTACT (object),
303 MC_ACCOUNT (g_value_get_object (value)));
306 gossip_contact_set_presence (GOSSIP_CONTACT (object),
307 GOSSIP_PRESENCE (g_value_get_object (value)));
310 gossip_contact_set_groups (GOSSIP_CONTACT (object),
311 g_value_get_pointer (value));
313 case PROP_SUBSCRIPTION:
314 gossip_contact_set_subscription (GOSSIP_CONTACT (object),
315 g_value_get_int (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_presence (GossipContact *contact)
406 GossipContactPriv *priv;
408 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
410 priv = GET_PRIV (contact);
412 return priv->presence;
416 gossip_contact_get_groups (GossipContact *contact)
418 GossipContactPriv *priv;
420 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
422 priv = GET_PRIV (contact);
428 gossip_contact_get_subscription (GossipContact *contact)
430 GossipContactPriv *priv;
432 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact),
433 GOSSIP_SUBSCRIPTION_NONE);
435 priv = GET_PRIV (contact);
437 return priv->subscription;
441 gossip_contact_get_handle (GossipContact *contact)
443 GossipContactPriv *priv;
445 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), 0);
447 priv = GET_PRIV (contact);
453 gossip_contact_set_id (GossipContact *contact,
456 GossipContactPriv *priv;
458 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
459 g_return_if_fail (id != NULL);
461 priv = GET_PRIV (contact);
463 if (priv->id && strcmp (id, priv->id) == 0) {
468 priv->id = g_strdup (id);
470 g_object_notify (G_OBJECT (contact), "id");
474 gossip_contact_set_name (GossipContact *contact,
477 GossipContactPriv *priv;
479 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
480 g_return_if_fail (name != NULL);
482 priv = GET_PRIV (contact);
484 if (priv->name && strcmp (name, priv->name) == 0) {
489 priv->name = g_strdup (name);
491 g_object_notify (G_OBJECT (contact), "name");
495 gossip_contact_set_avatar (GossipContact *contact,
496 GossipAvatar *avatar)
498 GossipContactPriv *priv;
500 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
502 priv = GET_PRIV (contact);
504 if (priv->avatar == avatar) {
509 gossip_avatar_unref (priv->avatar);
514 priv->avatar = gossip_avatar_ref (avatar);
517 g_object_notify (G_OBJECT (contact), "avatar");
521 gossip_contact_set_account (GossipContact *contact,
524 GossipContactPriv *priv;
526 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
527 g_return_if_fail (MC_IS_ACCOUNT (account));
529 priv = GET_PRIV (contact);
531 if (account == priv->account) {
536 g_object_unref (priv->account);
538 priv->account = g_object_ref (account);
540 g_object_notify (G_OBJECT (contact), "account");
544 gossip_contact_set_presence (GossipContact *contact,
545 GossipPresence *presence)
547 GossipContactPriv *priv;
549 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
551 priv = GET_PRIV (contact);
553 if (presence == priv->presence) {
557 if (priv->presence) {
558 g_object_unref (priv->presence);
559 priv->presence = NULL;
563 priv->presence = g_object_ref (presence);
566 g_object_notify (G_OBJECT (contact), "presence");
570 gossip_contact_set_groups (GossipContact *contact,
573 GossipContactPriv *priv;
574 GList *old_groups, *l;
576 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
578 priv = GET_PRIV (contact);
580 old_groups = priv->groups;
583 for (l = groups; l; l = l->next) {
584 priv->groups = g_list_append (priv->groups,
588 g_list_foreach (old_groups, (GFunc) g_free, NULL);
589 g_list_free (old_groups);
591 g_object_notify (G_OBJECT (contact), "groups");
595 gossip_contact_set_subscription (GossipContact *contact,
596 GossipSubscription subscription)
598 GossipContactPriv *priv;
600 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
602 priv = GET_PRIV (contact);
604 if (priv->subscription == subscription) {
608 priv->subscription = subscription;
610 g_object_notify (G_OBJECT (contact), "subscription");
614 gossip_contact_set_handle (GossipContact *contact,
617 GossipContactPriv *priv;
619 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
621 priv = GET_PRIV (contact);
623 if (priv->handle == handle) {
627 priv->handle = handle;
629 g_object_notify (G_OBJECT (contact), "handle");
633 gossip_contact_add_group (GossipContact *contact,
636 GossipContactPriv *priv;
638 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
639 g_return_if_fail (group != NULL);
641 priv = GET_PRIV (contact);
643 if (!g_list_find_custom (priv->groups, group, (GCompareFunc) strcmp)) {
644 priv->groups = g_list_prepend (priv->groups, g_strdup (group));
645 g_object_notify (G_OBJECT (contact), "groups");
650 gossip_contact_remove_group (GossipContact *contact,
653 GossipContactPriv *priv;
656 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
657 g_return_if_fail (group != NULL);
659 priv = GET_PRIV (contact);
661 l = g_list_find_custom (priv->groups, group, (GCompareFunc) strcmp);
664 priv->groups = g_list_delete_link (priv->groups, l);
665 g_object_notify (G_OBJECT (contact), "groups");
670 gossip_contact_is_online (GossipContact *contact)
672 GossipContactPriv *priv;
674 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), FALSE);
676 priv = GET_PRIV (contact);
678 return (priv->presence != NULL);
682 gossip_contact_is_in_group (GossipContact *contact,
685 GossipContactPriv *priv;
687 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), FALSE);
688 g_return_val_if_fail (!G_STR_EMPTY (group), FALSE);
690 priv = GET_PRIV (contact);
692 if (g_list_find_custom (priv->groups, group, (GCompareFunc) strcmp)) {
700 gossip_contact_get_status (GossipContact *contact)
702 GossipContactPriv *priv;
704 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), "");
706 priv = GET_PRIV (contact);
708 if (priv->presence) {
711 status = gossip_presence_get_status (priv->presence);
715 state = gossip_presence_get_state (priv->presence);
716 status = gossip_presence_state_get_default_status (state);
722 return gossip_presence_state_get_default_status (MC_PRESENCE_OFFLINE);
726 gossip_contact_get_user (GossipContact *contact)
728 GossipContactPriv *priv;
729 EmpathyContactManager *manager;
730 GossipContact *user_contact;
732 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
734 priv = GET_PRIV (contact);
736 manager = empathy_contact_manager_new ();
737 user_contact = empathy_contact_manager_get_user (manager, priv->account);
738 g_object_unref (manager);
744 gossip_contact_equal (gconstpointer v1,
747 McAccount *account_a;
748 McAccount *account_b;
752 g_return_val_if_fail (GOSSIP_IS_CONTACT (v1), FALSE);
753 g_return_val_if_fail (GOSSIP_IS_CONTACT (v2), FALSE);
755 account_a = gossip_contact_get_account (GOSSIP_CONTACT (v1));
756 account_b = gossip_contact_get_account (GOSSIP_CONTACT (v2));
758 id_a = gossip_contact_get_id (GOSSIP_CONTACT (v1));
759 id_b = gossip_contact_get_id (GOSSIP_CONTACT (v2));
761 return gossip_account_equal (account_a, account_b) && g_str_equal (id_a, id_b);
765 gossip_contact_hash (gconstpointer key)
767 GossipContactPriv *priv;
770 g_return_val_if_fail (GOSSIP_IS_CONTACT (key), +1);
772 priv = GET_PRIV (GOSSIP_CONTACT (key));
774 hash = gossip_account_hash (gossip_contact_get_account (GOSSIP_CONTACT (key)));
775 hash += g_str_hash (gossip_contact_get_id (GOSSIP_CONTACT (key)));