1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2004 Imendio AB
4 * Copyright (C) 2007 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Xavier Claessens <xclaesse@gmail.com>
30 #include <glib/gi18n.h>
32 #include "empathy-contact.h"
33 #include "empathy-utils.h"
34 #include "empathy-debug.h"
35 #include "empathy-enum-types.h"
37 #define DEBUG_DOMAIN "Contact"
39 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CONTACT, EmpathyContactPriv))
41 typedef struct _EmpathyContactPriv EmpathyContactPriv;
43 struct _EmpathyContactPriv {
46 EmpathyAvatar *avatar;
48 EmpathyPresence *presence;
50 EmpathyCapabilities capabilities;
54 static void empathy_contact_class_init (EmpathyContactClass *class);
55 static void empathy_contact_init (EmpathyContact *contact);
56 static void contact_finalize (GObject *object);
57 static void contact_get_property (GObject *object,
61 static void contact_set_property (GObject *object,
66 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
83 empathy_contact_class_init (EmpathyContactClass *class)
85 GObjectClass *object_class;
87 object_class = G_OBJECT_CLASS (class);
89 object_class->finalize = contact_finalize;
90 object_class->get_property = contact_get_property;
91 object_class->set_property = contact_set_property;
93 g_object_class_install_property (object_class,
95 g_param_spec_string ("id",
97 "String identifying contact",
101 g_object_class_install_property (object_class,
103 g_param_spec_string ("name",
105 "The name of the contact",
109 g_object_class_install_property (object_class,
111 g_param_spec_boxed ("avatar",
117 g_object_class_install_property (object_class,
119 g_param_spec_object ("account",
121 "The account associated with the contact",
125 g_object_class_install_property (object_class,
127 g_param_spec_object ("presence",
129 "Presence of contact",
130 EMPATHY_TYPE_PRESENCE,
133 g_object_class_install_property (object_class,
135 g_param_spec_uint ("handle",
137 "The handle of the contact",
143 g_object_class_install_property (object_class,
145 g_param_spec_flags ("capabilities",
146 "Contact Capabilities",
147 "Capabilities of the contact",
148 EMPATHY_TYPE_CAPABILITIES,
152 g_object_class_install_property (object_class,
154 g_param_spec_boolean ("is-user",
156 "Is contact the user",
160 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
164 empathy_contact_init (EmpathyContact *contact)
169 contact_finalize (GObject *object)
171 EmpathyContactPriv *priv;
173 priv = GET_PRIV (object);
175 empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
181 empathy_avatar_unref (priv->avatar);
184 if (priv->presence) {
185 g_object_unref (priv->presence);
189 g_object_unref (priv->account);
192 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
196 contact_get_property (GObject *object,
201 EmpathyContactPriv *priv;
203 priv = GET_PRIV (object);
207 g_value_set_string (value,
208 empathy_contact_get_id (EMPATHY_CONTACT (object)));
211 g_value_set_string (value,
212 empathy_contact_get_name (EMPATHY_CONTACT (object)));
215 g_value_set_boxed (value, priv->avatar);
218 g_value_set_object (value, priv->account);
221 g_value_set_object (value, priv->presence);
224 g_value_set_uint (value, priv->handle);
226 case PROP_CAPABILITIES:
227 g_value_set_flags (value, priv->capabilities);
230 g_value_set_boolean (value, priv->is_user);
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
239 contact_set_property (GObject *object,
244 EmpathyContactPriv *priv;
246 priv = GET_PRIV (object);
250 empathy_contact_set_id (EMPATHY_CONTACT (object),
251 g_value_get_string (value));
254 empathy_contact_set_name (EMPATHY_CONTACT (object),
255 g_value_get_string (value));
258 empathy_contact_set_avatar (EMPATHY_CONTACT (object),
259 g_value_get_boxed (value));
262 empathy_contact_set_account (EMPATHY_CONTACT (object),
263 MC_ACCOUNT (g_value_get_object (value)));
266 empathy_contact_set_presence (EMPATHY_CONTACT (object),
267 EMPATHY_PRESENCE (g_value_get_object (value)));
270 empathy_contact_set_handle (EMPATHY_CONTACT (object),
271 g_value_get_uint (value));
273 case PROP_CAPABILITIES:
274 empathy_contact_set_capabilities (EMPATHY_CONTACT (object),
275 g_value_get_flags (value));
278 empathy_contact_set_is_user (EMPATHY_CONTACT (object),
279 g_value_get_boolean (value));
282 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
288 empathy_contact_new (McAccount *account)
290 return g_object_new (EMPATHY_TYPE_CONTACT,
296 empathy_contact_new_full (McAccount *account,
300 return g_object_new (EMPATHY_TYPE_CONTACT,
308 empathy_contact_get_id (EmpathyContact *contact)
310 EmpathyContactPriv *priv;
312 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
314 priv = GET_PRIV (contact);
324 empathy_contact_set_id (EmpathyContact *contact,
327 EmpathyContactPriv *priv;
329 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
330 g_return_if_fail (id != NULL);
332 priv = GET_PRIV (contact);
334 if (priv->id && strcmp (id, priv->id) == 0) {
339 priv->id = g_strdup (id);
341 g_object_notify (G_OBJECT (contact), "id");
345 empathy_contact_get_name (EmpathyContact *contact)
347 EmpathyContactPriv *priv;
349 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
351 priv = GET_PRIV (contact);
353 if (G_STR_EMPTY (priv->name)) {
354 return empathy_contact_get_id (contact);
361 empathy_contact_set_name (EmpathyContact *contact,
364 EmpathyContactPriv *priv;
366 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
367 g_return_if_fail (name != NULL);
369 priv = GET_PRIV (contact);
371 if (priv->name && strcmp (name, priv->name) == 0) {
376 priv->name = g_strdup (name);
378 g_object_notify (G_OBJECT (contact), "name");
382 empathy_contact_get_avatar (EmpathyContact *contact)
384 EmpathyContactPriv *priv;
386 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
388 priv = GET_PRIV (contact);
394 empathy_contact_set_avatar (EmpathyContact *contact,
395 EmpathyAvatar *avatar)
397 EmpathyContactPriv *priv;
399 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
401 priv = GET_PRIV (contact);
403 if (priv->avatar == avatar) {
408 empathy_avatar_unref (priv->avatar);
413 priv->avatar = empathy_avatar_ref (avatar);
416 g_object_notify (G_OBJECT (contact), "avatar");
420 empathy_contact_get_account (EmpathyContact *contact)
422 EmpathyContactPriv *priv;
424 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
426 priv = GET_PRIV (contact);
428 return priv->account;
432 empathy_contact_set_account (EmpathyContact *contact,
435 EmpathyContactPriv *priv;
437 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
438 g_return_if_fail (MC_IS_ACCOUNT (account));
440 priv = GET_PRIV (contact);
442 if (account == priv->account) {
447 g_object_unref (priv->account);
449 priv->account = g_object_ref (account);
451 g_object_notify (G_OBJECT (contact), "account");
455 empathy_contact_get_presence (EmpathyContact *contact)
457 EmpathyContactPriv *priv;
459 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
461 priv = GET_PRIV (contact);
463 return priv->presence;
467 empathy_contact_set_presence (EmpathyContact *contact,
468 EmpathyPresence *presence)
470 EmpathyContactPriv *priv;
472 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
474 priv = GET_PRIV (contact);
476 if (presence == priv->presence) {
480 if (priv->presence) {
481 g_object_unref (priv->presence);
482 priv->presence = NULL;
486 priv->presence = g_object_ref (presence);
489 g_object_notify (G_OBJECT (contact), "presence");
493 empathy_contact_get_handle (EmpathyContact *contact)
495 EmpathyContactPriv *priv;
497 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
499 priv = GET_PRIV (contact);
505 empathy_contact_set_handle (EmpathyContact *contact,
508 EmpathyContactPriv *priv;
510 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
512 priv = GET_PRIV (contact);
514 if (priv->handle == handle) {
518 priv->handle = handle;
520 g_object_notify (G_OBJECT (contact), "handle");
524 empathy_contact_get_capabilities (EmpathyContact *contact)
526 EmpathyContactPriv *priv;
528 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
530 priv = GET_PRIV (contact);
532 return priv->capabilities;
536 empathy_contact_set_capabilities (EmpathyContact *contact,
537 EmpathyCapabilities capabilities)
539 EmpathyContactPriv *priv;
541 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
543 priv = GET_PRIV (contact);
545 if (priv->capabilities == capabilities) {
549 priv->capabilities = capabilities;
551 g_object_notify (G_OBJECT (contact), "capabilities");
555 empathy_contact_is_user (EmpathyContact *contact)
557 EmpathyContactPriv *priv;
559 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
561 priv = GET_PRIV (contact);
563 return priv->is_user;
567 empathy_contact_set_is_user (EmpathyContact *contact,
570 EmpathyContactPriv *priv;
572 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
574 priv = GET_PRIV (contact);
576 if (priv->is_user == is_user) {
580 priv->is_user = is_user;
582 g_object_notify (G_OBJECT (contact), "is-user");
586 empathy_contact_is_online (EmpathyContact *contact)
588 EmpathyContactPriv *priv;
590 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
592 priv = GET_PRIV (contact);
594 if (!priv->presence) {
598 return (empathy_presence_get_state (priv->presence) > MC_PRESENCE_OFFLINE);
602 empathy_contact_get_status (EmpathyContact *contact)
604 EmpathyContactPriv *priv;
606 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
608 priv = GET_PRIV (contact);
610 if (priv->presence) {
613 status = empathy_presence_get_status (priv->presence);
617 state = empathy_presence_get_state (priv->presence);
618 status = empathy_presence_state_get_default_status (state);
624 return empathy_presence_state_get_default_status (MC_PRESENCE_OFFLINE);
628 empathy_contact_can_voip (EmpathyContact *contact)
630 EmpathyContactPriv *priv;
632 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
634 priv = GET_PRIV (contact);
636 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
637 EMPATHY_CAPABILITIES_VIDEO);
641 empathy_contact_equal (gconstpointer v1,
644 McAccount *account_a;
645 McAccount *account_b;
649 g_return_val_if_fail (EMPATHY_IS_CONTACT (v1), FALSE);
650 g_return_val_if_fail (EMPATHY_IS_CONTACT (v2), FALSE);
652 account_a = empathy_contact_get_account (EMPATHY_CONTACT (v1));
653 account_b = empathy_contact_get_account (EMPATHY_CONTACT (v2));
655 id_a = empathy_contact_get_id (EMPATHY_CONTACT (v1));
656 id_b = empathy_contact_get_id (EMPATHY_CONTACT (v2));
658 return empathy_account_equal (account_a, account_b) && g_str_equal (id_a, id_b);
662 empathy_contact_hash (gconstpointer key)
664 EmpathyContactPriv *priv;
667 g_return_val_if_fail (EMPATHY_IS_CONTACT (key), +1);
669 priv = GET_PRIV (EMPATHY_CONTACT (key));
671 hash = empathy_account_hash (empathy_contact_get_account (EMPATHY_CONTACT (key)));
672 hash += g_str_hash (empathy_contact_get_id (EMPATHY_CONTACT (key)));