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;
53 static void empathy_contact_class_init (EmpathyContactClass *class);
54 static void empathy_contact_init (EmpathyContact *contact);
55 static void contact_finalize (GObject *object);
56 static void contact_get_property (GObject *object,
60 static void contact_set_property (GObject *object,
65 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
81 empathy_contact_class_init (EmpathyContactClass *class)
83 GObjectClass *object_class;
85 object_class = G_OBJECT_CLASS (class);
87 object_class->finalize = contact_finalize;
88 object_class->get_property = contact_get_property;
89 object_class->set_property = contact_set_property;
91 g_object_class_install_property (object_class,
93 g_param_spec_string ("id",
95 "String identifying contact",
99 g_object_class_install_property (object_class,
101 g_param_spec_string ("name",
103 "The name of the contact",
107 g_object_class_install_property (object_class,
109 g_param_spec_boxed ("avatar",
115 g_object_class_install_property (object_class,
117 g_param_spec_object ("account",
119 "The account associated with the contact",
123 g_object_class_install_property (object_class,
125 g_param_spec_object ("presence",
127 "Presence of contact",
128 EMPATHY_TYPE_PRESENCE,
131 g_object_class_install_property (object_class,
133 g_param_spec_uint ("handle",
135 "The handle of the contact",
140 g_object_class_install_property (object_class,
142 g_param_spec_boolean ("is-user",
144 "Is contact the user",
148 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
152 empathy_contact_init (EmpathyContact *contact)
157 contact_finalize (GObject *object)
159 EmpathyContactPriv *priv;
161 priv = GET_PRIV (object);
163 empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
169 empathy_avatar_unref (priv->avatar);
172 if (priv->presence) {
173 g_object_unref (priv->presence);
177 g_object_unref (priv->account);
180 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
184 contact_get_property (GObject *object,
189 EmpathyContactPriv *priv;
191 priv = GET_PRIV (object);
195 g_value_set_string (value,
196 empathy_contact_get_id (EMPATHY_CONTACT (object)));
199 g_value_set_string (value,
200 empathy_contact_get_name (EMPATHY_CONTACT (object)));
203 g_value_set_boxed (value, priv->avatar);
206 g_value_set_object (value, priv->account);
209 g_value_set_object (value, priv->presence);
212 g_value_set_uint (value, priv->handle);
215 g_value_set_boolean (value, priv->is_user);
218 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
224 contact_set_property (GObject *object,
229 EmpathyContactPriv *priv;
231 priv = GET_PRIV (object);
235 empathy_contact_set_id (EMPATHY_CONTACT (object),
236 g_value_get_string (value));
239 empathy_contact_set_name (EMPATHY_CONTACT (object),
240 g_value_get_string (value));
243 empathy_contact_set_avatar (EMPATHY_CONTACT (object),
244 g_value_get_boxed (value));
247 empathy_contact_set_account (EMPATHY_CONTACT (object),
248 MC_ACCOUNT (g_value_get_object (value)));
251 empathy_contact_set_presence (EMPATHY_CONTACT (object),
252 EMPATHY_PRESENCE (g_value_get_object (value)));
255 empathy_contact_set_handle (EMPATHY_CONTACT (object),
256 g_value_get_uint (value));
259 empathy_contact_set_is_user (EMPATHY_CONTACT (object),
260 g_value_get_boolean (value));
263 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
269 empathy_contact_new (McAccount *account)
271 return g_object_new (EMPATHY_TYPE_CONTACT,
277 empathy_contact_new_full (McAccount *account,
281 return g_object_new (EMPATHY_TYPE_CONTACT,
289 empathy_contact_get_id (EmpathyContact *contact)
291 EmpathyContactPriv *priv;
293 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
295 priv = GET_PRIV (contact);
305 empathy_contact_set_id (EmpathyContact *contact,
308 EmpathyContactPriv *priv;
310 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
311 g_return_if_fail (id != NULL);
313 priv = GET_PRIV (contact);
315 if (priv->id && strcmp (id, priv->id) == 0) {
320 priv->id = g_strdup (id);
322 g_object_notify (G_OBJECT (contact), "id");
326 empathy_contact_get_name (EmpathyContact *contact)
328 EmpathyContactPriv *priv;
330 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
332 priv = GET_PRIV (contact);
334 if (G_STR_EMPTY (priv->name)) {
335 return empathy_contact_get_id (contact);
342 empathy_contact_set_name (EmpathyContact *contact,
345 EmpathyContactPriv *priv;
347 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
348 g_return_if_fail (name != NULL);
350 priv = GET_PRIV (contact);
352 if (priv->name && strcmp (name, priv->name) == 0) {
357 priv->name = g_strdup (name);
359 g_object_notify (G_OBJECT (contact), "name");
363 empathy_contact_get_avatar (EmpathyContact *contact)
365 EmpathyContactPriv *priv;
367 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
369 priv = GET_PRIV (contact);
375 empathy_contact_set_avatar (EmpathyContact *contact,
376 EmpathyAvatar *avatar)
378 EmpathyContactPriv *priv;
380 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
382 priv = GET_PRIV (contact);
384 if (priv->avatar == avatar) {
389 empathy_avatar_unref (priv->avatar);
394 priv->avatar = empathy_avatar_ref (avatar);
397 g_object_notify (G_OBJECT (contact), "avatar");
401 empathy_contact_get_account (EmpathyContact *contact)
403 EmpathyContactPriv *priv;
405 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
407 priv = GET_PRIV (contact);
409 return priv->account;
413 empathy_contact_set_account (EmpathyContact *contact,
416 EmpathyContactPriv *priv;
418 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
419 g_return_if_fail (MC_IS_ACCOUNT (account));
421 priv = GET_PRIV (contact);
423 if (account == priv->account) {
428 g_object_unref (priv->account);
430 priv->account = g_object_ref (account);
432 g_object_notify (G_OBJECT (contact), "account");
436 empathy_contact_get_presence (EmpathyContact *contact)
438 EmpathyContactPriv *priv;
440 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
442 priv = GET_PRIV (contact);
444 return priv->presence;
448 empathy_contact_set_presence (EmpathyContact *contact,
449 EmpathyPresence *presence)
451 EmpathyContactPriv *priv;
453 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
455 priv = GET_PRIV (contact);
457 if (presence == priv->presence) {
461 if (priv->presence) {
462 g_object_unref (priv->presence);
463 priv->presence = NULL;
467 priv->presence = g_object_ref (presence);
470 g_object_notify (G_OBJECT (contact), "presence");
474 empathy_contact_get_handle (EmpathyContact *contact)
476 EmpathyContactPriv *priv;
478 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
480 priv = GET_PRIV (contact);
486 empathy_contact_set_handle (EmpathyContact *contact,
489 EmpathyContactPriv *priv;
491 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
493 priv = GET_PRIV (contact);
495 if (priv->handle == handle) {
499 priv->handle = handle;
501 g_object_notify (G_OBJECT (contact), "handle");
505 empathy_contact_is_user (EmpathyContact *contact)
507 EmpathyContactPriv *priv;
509 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
511 priv = GET_PRIV (contact);
513 return priv->is_user;
517 empathy_contact_set_is_user (EmpathyContact *contact,
520 EmpathyContactPriv *priv;
522 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
524 priv = GET_PRIV (contact);
526 if (priv->is_user == is_user) {
530 priv->is_user = is_user;
532 g_object_notify (G_OBJECT (contact), "is-user");
536 empathy_contact_is_online (EmpathyContact *contact)
538 EmpathyContactPriv *priv;
540 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
542 priv = GET_PRIV (contact);
544 if (!priv->presence) {
548 return (empathy_presence_get_state (priv->presence) > MC_PRESENCE_OFFLINE);
552 empathy_contact_get_status (EmpathyContact *contact)
554 EmpathyContactPriv *priv;
556 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
558 priv = GET_PRIV (contact);
560 if (priv->presence) {
563 status = empathy_presence_get_status (priv->presence);
567 state = empathy_presence_get_state (priv->presence);
568 status = empathy_presence_state_get_default_status (state);
574 return empathy_presence_state_get_default_status (MC_PRESENCE_OFFLINE);
578 empathy_contact_equal (gconstpointer v1,
581 McAccount *account_a;
582 McAccount *account_b;
586 g_return_val_if_fail (EMPATHY_IS_CONTACT (v1), FALSE);
587 g_return_val_if_fail (EMPATHY_IS_CONTACT (v2), FALSE);
589 account_a = empathy_contact_get_account (EMPATHY_CONTACT (v1));
590 account_b = empathy_contact_get_account (EMPATHY_CONTACT (v2));
592 id_a = empathy_contact_get_id (EMPATHY_CONTACT (v1));
593 id_b = empathy_contact_get_id (EMPATHY_CONTACT (v2));
595 return empathy_account_equal (account_a, account_b) && g_str_equal (id_a, id_b);
599 empathy_contact_hash (gconstpointer key)
601 EmpathyContactPriv *priv;
604 g_return_val_if_fail (EMPATHY_IS_CONTACT (key), +1);
606 priv = GET_PRIV (EMPATHY_CONTACT (key));
608 hash = empathy_account_hash (empathy_contact_get_account (EMPATHY_CONTACT (key)));
609 hash += g_str_hash (empathy_contact_get_id (EMPATHY_CONTACT (key)));