1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2004 Imendio AB
4 * Copyright (C) 2007-2008 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 <telepathy-glib/util.h>
34 #include "empathy-contact.h"
35 #include "empathy-utils.h"
36 #include "empathy-enum-types.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
39 #include "empathy-debug.h"
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
45 EmpathyAvatar *avatar;
48 gchar *presence_message;
50 EmpathyCapabilities capabilities;
53 EmpathyContactReady ready;
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);
75 PROP_PRESENCE_MESSAGE,
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_uint ("presence",
129 "Presence of contact",
135 g_object_class_install_property (object_class,
136 PROP_PRESENCE_MESSAGE,
137 g_param_spec_string ("presence-message",
138 "Contact presence message",
139 "Presence message of contact",
142 g_object_class_install_property (object_class,
144 g_param_spec_uint ("handle",
146 "The handle of the contact",
152 g_object_class_install_property (object_class,
154 g_param_spec_flags ("capabilities",
155 "Contact Capabilities",
156 "Capabilities of the contact",
157 EMPATHY_TYPE_CAPABILITIES,
158 EMPATHY_CAPABILITIES_UNKNOWN,
159 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
161 g_object_class_install_property (object_class,
163 g_param_spec_boolean ("is-user",
165 "Is contact the user",
169 g_object_class_install_property (object_class,
171 g_param_spec_flags ("ready",
172 "Contact ready flags",
173 "Flags for ready properties",
174 EMPATHY_TYPE_CONTACT_READY,
175 EMPATHY_CONTACT_READY_NONE,
178 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
182 empathy_contact_init (EmpathyContact *contact)
184 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
185 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
187 contact->priv = priv;
191 contact_finalize (GObject *object)
193 EmpathyContactPriv *priv;
195 priv = GET_PRIV (object);
197 DEBUG ("finalize: %p", object);
201 g_free (priv->presence_message);
204 empathy_avatar_unref (priv->avatar);
208 g_object_unref (priv->account);
211 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
215 contact_get_property (GObject *object,
220 EmpathyContactPriv *priv;
222 priv = GET_PRIV (object);
226 g_value_set_string (value, priv->id);
229 g_value_set_string (value,
230 empathy_contact_get_name (EMPATHY_CONTACT (object)));
233 g_value_set_boxed (value, priv->avatar);
236 g_value_set_object (value, priv->account);
239 g_value_set_uint (value, priv->presence);
241 case PROP_PRESENCE_MESSAGE:
242 g_value_set_string (value, priv->presence_message);
245 g_value_set_uint (value, priv->handle);
247 case PROP_CAPABILITIES:
248 g_value_set_flags (value, priv->capabilities);
251 g_value_set_boolean (value, priv->is_user);
254 g_value_set_flags (value, priv->ready);
257 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
263 contact_set_property (GObject *object,
268 EmpathyContactPriv *priv;
270 priv = GET_PRIV (object);
274 empathy_contact_set_id (EMPATHY_CONTACT (object),
275 g_value_get_string (value));
278 empathy_contact_set_name (EMPATHY_CONTACT (object),
279 g_value_get_string (value));
282 empathy_contact_set_avatar (EMPATHY_CONTACT (object),
283 g_value_get_boxed (value));
286 empathy_contact_set_account (EMPATHY_CONTACT (object),
287 MC_ACCOUNT (g_value_get_object (value)));
290 empathy_contact_set_presence (EMPATHY_CONTACT (object),
291 g_value_get_uint (value));
293 case PROP_PRESENCE_MESSAGE:
294 empathy_contact_set_presence_message (EMPATHY_CONTACT (object),
295 g_value_get_string (value));
298 empathy_contact_set_handle (EMPATHY_CONTACT (object),
299 g_value_get_uint (value));
301 case PROP_CAPABILITIES:
302 empathy_contact_set_capabilities (EMPATHY_CONTACT (object),
303 g_value_get_flags (value));
306 empathy_contact_set_is_user (EMPATHY_CONTACT (object),
307 g_value_get_boolean (value));
310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
316 contact_set_ready_flag (EmpathyContact *contact,
317 EmpathyContactReady flag)
319 EmpathyContactPriv *priv = GET_PRIV (contact);
321 if (!(priv->ready & flag)) {
323 g_object_notify (G_OBJECT (contact), "ready");
328 empathy_contact_new (McAccount *account)
330 return g_object_new (EMPATHY_TYPE_CONTACT,
336 empathy_contact_new_full (McAccount *account,
340 return g_object_new (EMPATHY_TYPE_CONTACT,
348 empathy_contact_get_id (EmpathyContact *contact)
350 EmpathyContactPriv *priv;
352 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
354 priv = GET_PRIV (contact);
360 empathy_contact_set_id (EmpathyContact *contact,
363 EmpathyContactPriv *priv;
365 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
366 g_return_if_fail (id != NULL);
368 priv = GET_PRIV (contact);
370 /* We temporally ref the contact because it could be destroyed
371 * during the signal emition */
372 g_object_ref (contact);
373 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_ID);
374 if (tp_strdiff (id, priv->id)) {
376 priv->id = g_strdup (id);
378 g_object_notify (G_OBJECT (contact), "id");
379 if (G_STR_EMPTY (priv->name)) {
380 g_object_notify (G_OBJECT (contact), "name");
384 g_object_unref (contact);
388 empathy_contact_get_name (EmpathyContact *contact)
390 EmpathyContactPriv *priv;
392 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
394 priv = GET_PRIV (contact);
396 if (G_STR_EMPTY (priv->name)) {
397 return empathy_contact_get_id (contact);
404 empathy_contact_set_name (EmpathyContact *contact,
407 EmpathyContactPriv *priv;
409 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
411 priv = GET_PRIV (contact);
413 g_object_ref (contact);
414 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_NAME);
415 if (tp_strdiff (name, priv->name)) {
417 priv->name = g_strdup (name);
418 g_object_notify (G_OBJECT (contact), "name");
420 g_object_unref (contact);
424 empathy_contact_get_avatar (EmpathyContact *contact)
426 EmpathyContactPriv *priv;
428 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
430 priv = GET_PRIV (contact);
436 empathy_contact_set_avatar (EmpathyContact *contact,
437 EmpathyAvatar *avatar)
439 EmpathyContactPriv *priv;
441 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
443 priv = GET_PRIV (contact);
445 if (priv->avatar == avatar) {
450 empathy_avatar_unref (priv->avatar);
455 priv->avatar = empathy_avatar_ref (avatar);
458 g_object_notify (G_OBJECT (contact), "avatar");
462 empathy_contact_get_account (EmpathyContact *contact)
464 EmpathyContactPriv *priv;
466 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
468 priv = GET_PRIV (contact);
470 return priv->account;
474 empathy_contact_set_account (EmpathyContact *contact,
477 EmpathyContactPriv *priv;
479 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
480 g_return_if_fail (MC_IS_ACCOUNT (account));
482 priv = GET_PRIV (contact);
484 if (account == priv->account) {
489 g_object_unref (priv->account);
491 priv->account = g_object_ref (account);
493 g_object_notify (G_OBJECT (contact), "account");
497 empathy_contact_get_presence (EmpathyContact *contact)
499 EmpathyContactPriv *priv;
501 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), MC_PRESENCE_UNSET);
503 priv = GET_PRIV (contact);
505 return priv->presence;
509 empathy_contact_set_presence (EmpathyContact *contact,
512 EmpathyContactPriv *priv;
514 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
516 priv = GET_PRIV (contact);
518 if (presence == priv->presence) {
522 priv->presence = presence;
524 g_object_notify (G_OBJECT (contact), "presence");
528 empathy_contact_get_presence_message (EmpathyContact *contact)
530 EmpathyContactPriv *priv;
532 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
534 priv = GET_PRIV (contact);
536 return priv->presence_message;
540 empathy_contact_set_presence_message (EmpathyContact *contact,
541 const gchar *message)
543 EmpathyContactPriv *priv = GET_PRIV (contact);
545 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
547 if (!tp_strdiff (message, priv->presence_message)) {
551 g_free (priv->presence_message);
552 priv->presence_message = g_strdup (message);
554 g_object_notify (G_OBJECT (contact), "presence-message");
558 empathy_contact_get_handle (EmpathyContact *contact)
560 EmpathyContactPriv *priv;
562 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
564 priv = GET_PRIV (contact);
570 empathy_contact_set_handle (EmpathyContact *contact,
573 EmpathyContactPriv *priv;
575 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
577 priv = GET_PRIV (contact);
579 g_object_ref (contact);
580 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_HANDLE);
581 if (handle != priv->handle) {
582 priv->handle = handle;
583 g_object_notify (G_OBJECT (contact), "handle");
585 g_object_unref (contact);
589 empathy_contact_get_capabilities (EmpathyContact *contact)
591 EmpathyContactPriv *priv;
593 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
595 priv = GET_PRIV (contact);
597 return priv->capabilities;
601 empathy_contact_set_capabilities (EmpathyContact *contact,
602 EmpathyCapabilities capabilities)
604 EmpathyContactPriv *priv;
606 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
608 priv = GET_PRIV (contact);
610 if (priv->capabilities == capabilities) {
614 priv->capabilities = capabilities;
616 g_object_notify (G_OBJECT (contact), "capabilities");
620 empathy_contact_is_user (EmpathyContact *contact)
622 EmpathyContactPriv *priv;
624 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
626 priv = GET_PRIV (contact);
628 return priv->is_user;
632 empathy_contact_set_is_user (EmpathyContact *contact,
635 EmpathyContactPriv *priv;
637 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
639 priv = GET_PRIV (contact);
641 if (priv->is_user == is_user) {
645 priv->is_user = is_user;
647 g_object_notify (G_OBJECT (contact), "is-user");
651 empathy_contact_is_online (EmpathyContact *contact)
653 EmpathyContactPriv *priv;
655 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
657 priv = GET_PRIV (contact);
659 return (priv->presence > MC_PRESENCE_OFFLINE);
663 empathy_contact_get_status (EmpathyContact *contact)
665 EmpathyContactPriv *priv;
667 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
669 priv = GET_PRIV (contact);
671 if (priv->presence_message) {
672 return priv->presence_message;
675 return empathy_presence_get_default_message (priv->presence);
679 empathy_contact_can_voip (EmpathyContact *contact)
681 EmpathyContactPriv *priv;
683 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
685 priv = GET_PRIV (contact);
687 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
688 EMPATHY_CAPABILITIES_VIDEO);
692 empathy_contact_get_ready (EmpathyContact *contact)
694 EmpathyContactPriv *priv;
696 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
698 priv = GET_PRIV (contact);
704 empathy_contact_equal (gconstpointer v1,
707 McAccount *account_a;
708 McAccount *account_b;
712 g_return_val_if_fail (EMPATHY_IS_CONTACT (v1), FALSE);
713 g_return_val_if_fail (EMPATHY_IS_CONTACT (v2), FALSE);
715 account_a = empathy_contact_get_account (EMPATHY_CONTACT (v1));
716 account_b = empathy_contact_get_account (EMPATHY_CONTACT (v2));
718 id_a = empathy_contact_get_id (EMPATHY_CONTACT (v1));
719 id_b = empathy_contact_get_id (EMPATHY_CONTACT (v2));
721 return empathy_account_equal (account_a, account_b) && g_str_equal (id_a, id_b);
725 empathy_contact_hash (gconstpointer key)
727 EmpathyContactPriv *priv;
729 g_return_val_if_fail (EMPATHY_IS_CONTACT (key), +1);
731 priv = GET_PRIV (EMPATHY_CONTACT (key));
733 if (priv->hash == 0) {
734 priv->hash = empathy_account_hash (priv->account) + g_str_hash (priv->id);
741 contact_is_ready_func (GObject *contact,
744 EmpathyContactPriv *priv = GET_PRIV (contact);
745 EmpathyContactReady ready;
747 ready = GPOINTER_TO_UINT (user_data);
749 return (priv->ready & ready) == ready;
753 empathy_contact_run_until_ready (EmpathyContact *contact,
754 EmpathyContactReady ready,
757 empathy_run_until_ready_full (contact, "notify::ready",
758 contact_is_ready_func, GUINT_TO_POINTER (ready),
763 contact_get_avatar_filename (EmpathyContact *contact,
766 EmpathyContactPriv *priv = GET_PRIV (contact);
769 gchar *token_escaped;
770 gchar *contact_escaped;
772 contact_escaped = tp_escape_as_identifier (priv->id);
773 token_escaped = tp_escape_as_identifier (token);
775 avatar_path = g_build_filename (g_get_user_cache_dir (),
778 mc_account_get_unique_name (priv->account),
781 g_mkdir_with_parents (avatar_path, 0700);
783 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
785 g_free (contact_escaped);
786 g_free (token_escaped);
787 g_free (avatar_path);
793 empathy_contact_load_avatar_data (EmpathyContact *contact,
799 EmpathyContactPriv *priv = GET_PRIV (contact);
800 EmpathyAvatar *avatar;
802 GError *error = NULL;
804 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
805 g_return_if_fail (!G_STR_EMPTY (priv->id));
806 g_return_if_fail (data != NULL);
807 g_return_if_fail (len > 0);
808 g_return_if_fail (format != NULL);
809 g_return_if_fail (!G_STR_EMPTY (token));
811 /* Load and set the avatar */
812 avatar = empathy_avatar_new (g_memdup (data, len), len,
815 empathy_contact_set_avatar (contact, avatar);
816 empathy_avatar_unref (avatar);
818 /* Save to cache if not yet in it */
819 filename = contact_get_avatar_filename (contact, token);
820 if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
821 if (!g_file_set_contents (filename, data, len, &error)) {
822 DEBUG ("Failed to save avatar in cache: %s",
823 error ? error->message : "No error given");
824 g_clear_error (&error);
826 DEBUG ("Avatar saved to %s", filename);
833 empathy_contact_load_avatar_cache (EmpathyContact *contact,
836 EmpathyContactPriv *priv = GET_PRIV (contact);
837 EmpathyAvatar *avatar = NULL;
841 GError *error = NULL;
843 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
844 g_return_val_if_fail (!G_STR_EMPTY (priv->id), FALSE);
845 g_return_val_if_fail (!G_STR_EMPTY (token), FALSE);
847 /* Load the avatar from file if it exists */
848 filename = contact_get_avatar_filename (contact, token);
849 if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
850 if (!g_file_get_contents (filename, &data, &len, &error)) {
851 DEBUG ("Failed to load avatar from cache: %s",
852 error ? error->message : "No error given");
853 g_clear_error (&error);
858 DEBUG ("Avatar loaded from %s", filename);
859 avatar = empathy_avatar_new (data, len, NULL, g_strdup (token));
860 empathy_contact_set_avatar (contact, avatar);
861 empathy_avatar_unref (avatar);
870 empathy_avatar_get_type (void)
872 static GType type_id = 0;
875 type_id = g_boxed_type_register_static ("EmpathyAvatar",
876 (GBoxedCopyFunc) empathy_avatar_ref,
877 (GBoxedFreeFunc) empathy_avatar_unref);
884 empathy_avatar_new (guchar *data,
889 EmpathyAvatar *avatar;
891 avatar = g_slice_new0 (EmpathyAvatar);
894 avatar->format = format;
895 avatar->token = token;
896 avatar->refcount = 1;
902 empathy_avatar_unref (EmpathyAvatar *avatar)
904 g_return_if_fail (avatar != NULL);
907 if (avatar->refcount == 0) {
908 g_free (avatar->data);
909 g_free (avatar->format);
910 g_free (avatar->token);
911 g_slice_free (EmpathyAvatar, avatar);
916 empathy_avatar_ref (EmpathyAvatar *avatar)
918 g_return_val_if_fail (avatar != NULL, NULL);