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);
464 priv->id = g_strdup (id);
466 g_object_notify (G_OBJECT (contact), "id");
470 gossip_contact_set_name (GossipContact *contact,
473 GossipContactPriv *priv;
475 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
476 g_return_if_fail (name != NULL);
478 priv = GET_PRIV (contact);
481 priv->name = g_strdup (name);
483 g_object_notify (G_OBJECT (contact), "name");
487 gossip_contact_set_avatar (GossipContact *contact,
488 GossipAvatar *avatar)
490 GossipContactPriv *priv;
492 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
494 priv = GET_PRIV (contact);
497 gossip_avatar_unref (priv->avatar);
502 priv->avatar = gossip_avatar_ref (avatar);
505 g_object_notify (G_OBJECT (contact), "avatar");
509 gossip_contact_set_account (GossipContact *contact,
512 GossipContactPriv *priv;
514 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
515 g_return_if_fail (MC_IS_ACCOUNT (account));
517 priv = GET_PRIV (contact);
520 g_object_unref (priv->account);
522 priv->account = g_object_ref (account);
524 g_object_notify (G_OBJECT (contact), "account");
528 gossip_contact_set_presence (GossipContact *contact,
529 GossipPresence *presence)
531 GossipContactPriv *priv;
533 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
535 priv = GET_PRIV (contact);
537 if (priv->presence) {
538 g_object_unref (priv->presence);
539 priv->presence = NULL;
543 priv->presence = g_object_ref (presence);
546 g_object_notify (G_OBJECT (contact), "presence");
550 gossip_contact_set_groups (GossipContact *contact,
553 GossipContactPriv *priv;
554 GList *old_groups, *l;
556 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
558 priv = GET_PRIV (contact);
560 old_groups = priv->groups;
563 for (l = groups; l; l = l->next) {
564 priv->groups = g_list_append (priv->groups,
568 g_list_foreach (old_groups, (GFunc) g_free, NULL);
569 g_list_free (old_groups);
571 g_object_notify (G_OBJECT (contact), "groups");
575 gossip_contact_set_subscription (GossipContact *contact,
576 GossipSubscription subscription)
578 GossipContactPriv *priv;
580 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
582 priv = GET_PRIV (contact);
584 priv->subscription = subscription;
586 g_object_notify (G_OBJECT (contact), "subscription");
590 gossip_contact_set_handle (GossipContact *contact,
593 GossipContactPriv *priv;
595 priv = GET_PRIV (contact);
597 priv->handle = handle;
599 g_object_notify (G_OBJECT (contact), "handle");
603 gossip_contact_is_online (GossipContact *contact)
605 GossipContactPriv *priv;
607 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), FALSE);
609 priv = GET_PRIV (contact);
611 return (priv->presence != NULL);
615 gossip_contact_is_in_group (GossipContact *contact,
618 GossipContactPriv *priv;
620 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), FALSE);
621 g_return_val_if_fail (!G_STR_EMPTY (group), FALSE);
623 priv = GET_PRIV (contact);
625 if (g_list_find_custom (priv->groups, group, (GCompareFunc) strcmp)) {
633 gossip_contact_get_status (GossipContact *contact)
635 GossipContactPriv *priv;
637 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), "");
639 priv = GET_PRIV (contact);
641 if (priv->presence) {
644 status = gossip_presence_get_status (priv->presence);
648 state = gossip_presence_get_state (priv->presence);
649 status = gossip_presence_state_get_default_status (state);
659 gossip_contact_get_user (GossipContact *contact)
661 GossipContactPriv *priv;
662 EmpathyContactManager *manager;
663 GossipContact *user_contact;
665 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
667 priv = GET_PRIV (contact);
669 manager = empathy_contact_manager_new ();
670 user_contact = empathy_contact_manager_get_user (manager, priv->account);
671 g_object_unref (manager);
677 gossip_contact_equal (gconstpointer v1,
680 McAccount *account_a;
681 McAccount *account_b;
685 g_return_val_if_fail (GOSSIP_IS_CONTACT (v1), FALSE);
686 g_return_val_if_fail (GOSSIP_IS_CONTACT (v2), FALSE);
688 account_a = gossip_contact_get_account (GOSSIP_CONTACT (v1));
689 account_b = gossip_contact_get_account (GOSSIP_CONTACT (v2));
691 id_a = gossip_contact_get_id (GOSSIP_CONTACT (v1));
692 id_b = gossip_contact_get_id (GOSSIP_CONTACT (v2));
694 return gossip_account_equal (account_a, account_b) && g_str_equal (id_a, id_b);
698 gossip_contact_hash (gconstpointer key)
700 GossipContactPriv *priv;
703 g_return_val_if_fail (GOSSIP_IS_CONTACT (key), +1);
705 priv = GET_PRIV (GOSSIP_CONTACT (key));
707 hash = gossip_account_hash (gossip_contact_get_account (GOSSIP_CONTACT (key)));
708 hash += g_str_hash (gossip_contact_get_id (GOSSIP_CONTACT (key)));