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, guint param_id,
58 GValue *value, GParamSpec *pspec);
59 static void contact_set_property (GObject *object, guint param_id,
60 const GValue *value, GParamSpec *pspec);
62 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
72 PROP_PRESENCE_MESSAGE,
80 empathy_contact_class_init (EmpathyContactClass *class)
82 GObjectClass *object_class;
84 object_class = G_OBJECT_CLASS (class);
86 object_class->finalize = contact_finalize;
87 object_class->get_property = contact_get_property;
88 object_class->set_property = contact_set_property;
90 g_object_class_install_property (object_class,
92 g_param_spec_string ("id",
94 "String identifying contact",
98 g_object_class_install_property (object_class,
100 g_param_spec_string ("name",
102 "The name of the contact",
106 g_object_class_install_property (object_class,
108 g_param_spec_boxed ("avatar",
114 g_object_class_install_property (object_class,
116 g_param_spec_object ("account",
118 "The account associated with the contact",
122 g_object_class_install_property (object_class,
124 g_param_spec_uint ("presence",
126 "Presence of contact",
132 g_object_class_install_property (object_class,
133 PROP_PRESENCE_MESSAGE,
134 g_param_spec_string ("presence-message",
135 "Contact presence message",
136 "Presence message of contact",
140 g_object_class_install_property (object_class,
142 g_param_spec_uint ("handle",
144 "The handle of the contact",
150 g_object_class_install_property (object_class,
152 g_param_spec_flags ("capabilities",
153 "Contact Capabilities",
154 "Capabilities of the contact",
155 EMPATHY_TYPE_CAPABILITIES,
156 EMPATHY_CAPABILITIES_UNKNOWN,
157 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
159 g_object_class_install_property (object_class,
161 g_param_spec_boolean ("is-user",
163 "Is contact the user",
167 g_object_class_install_property (object_class,
169 g_param_spec_flags ("ready",
170 "Contact ready flags",
171 "Flags for ready properties",
172 EMPATHY_TYPE_CONTACT_READY,
173 EMPATHY_CONTACT_READY_NONE,
176 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
180 empathy_contact_init (EmpathyContact *contact)
182 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
183 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
185 contact->priv = priv;
189 contact_finalize (GObject *object)
191 EmpathyContactPriv *priv;
193 priv = GET_PRIV (object);
195 DEBUG ("finalize: %p", object);
199 g_free (priv->presence_message);
203 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))
324 g_object_notify (G_OBJECT (contact), "ready");
329 empathy_contact_new (McAccount *account)
331 return g_object_new (EMPATHY_TYPE_CONTACT,
337 empathy_contact_new_full (McAccount *account,
341 return g_object_new (EMPATHY_TYPE_CONTACT,
349 empathy_contact_get_id (EmpathyContact *contact)
351 EmpathyContactPriv *priv;
353 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
355 priv = GET_PRIV (contact);
361 empathy_contact_set_id (EmpathyContact *contact,
364 EmpathyContactPriv *priv;
366 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
367 g_return_if_fail (id != NULL);
369 priv = GET_PRIV (contact);
371 /* We temporally ref the contact because it could be destroyed
372 * during the signal emition */
373 g_object_ref (contact);
374 if (tp_strdiff (id, priv->id))
377 priv->id = g_strdup (id);
379 g_object_notify (G_OBJECT (contact), "id");
380 if (G_STR_EMPTY (priv->name))
382 g_object_notify (G_OBJECT (contact), "name");
385 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_ID);
387 g_object_unref (contact);
391 empathy_contact_get_name (EmpathyContact *contact)
393 EmpathyContactPriv *priv;
395 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
397 priv = GET_PRIV (contact);
399 if (G_STR_EMPTY (priv->name))
401 return empathy_contact_get_id (contact);
408 empathy_contact_set_name (EmpathyContact *contact,
411 EmpathyContactPriv *priv;
413 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
415 priv = GET_PRIV (contact);
417 g_object_ref (contact);
418 if (tp_strdiff (name, priv->name))
421 priv->name = g_strdup (name);
422 g_object_notify (G_OBJECT (contact), "name");
424 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_NAME);
425 g_object_unref (contact);
429 empathy_contact_get_avatar (EmpathyContact *contact)
431 EmpathyContactPriv *priv;
433 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
435 priv = GET_PRIV (contact);
441 empathy_contact_set_avatar (EmpathyContact *contact,
442 EmpathyAvatar *avatar)
444 EmpathyContactPriv *priv;
446 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
448 priv = GET_PRIV (contact);
450 if (priv->avatar == avatar)
455 empathy_avatar_unref (priv->avatar);
461 priv->avatar = empathy_avatar_ref (avatar);
464 g_object_notify (G_OBJECT (contact), "avatar");
468 empathy_contact_get_account (EmpathyContact *contact)
470 EmpathyContactPriv *priv;
472 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
474 priv = GET_PRIV (contact);
476 return priv->account;
480 empathy_contact_set_account (EmpathyContact *contact,
483 EmpathyContactPriv *priv;
485 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
486 g_return_if_fail (MC_IS_ACCOUNT (account));
488 priv = GET_PRIV (contact);
490 if (account == priv->account)
495 g_object_unref (priv->account);
497 priv->account = g_object_ref (account);
499 g_object_notify (G_OBJECT (contact), "account");
503 empathy_contact_get_presence (EmpathyContact *contact)
505 EmpathyContactPriv *priv;
507 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), MC_PRESENCE_UNSET);
509 priv = GET_PRIV (contact);
511 return priv->presence;
515 empathy_contact_set_presence (EmpathyContact *contact,
518 EmpathyContactPriv *priv;
520 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
522 priv = GET_PRIV (contact);
524 if (presence == priv->presence)
527 priv->presence = presence;
529 g_object_notify (G_OBJECT (contact), "presence");
533 empathy_contact_get_presence_message (EmpathyContact *contact)
535 EmpathyContactPriv *priv;
537 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
539 priv = GET_PRIV (contact);
541 return priv->presence_message;
545 empathy_contact_set_presence_message (EmpathyContact *contact,
546 const gchar *message)
548 EmpathyContactPriv *priv = GET_PRIV (contact);
550 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
552 if (!tp_strdiff (message, priv->presence_message))
555 g_free (priv->presence_message);
556 priv->presence_message = g_strdup (message);
558 g_object_notify (G_OBJECT (contact), "presence-message");
562 empathy_contact_get_handle (EmpathyContact *contact)
564 EmpathyContactPriv *priv;
566 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
568 priv = GET_PRIV (contact);
574 empathy_contact_set_handle (EmpathyContact *contact,
577 EmpathyContactPriv *priv;
579 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
581 priv = GET_PRIV (contact);
583 g_object_ref (contact);
584 if (handle != priv->handle)
586 priv->handle = handle;
587 g_object_notify (G_OBJECT (contact), "handle");
589 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_HANDLE);
590 g_object_unref (contact);
594 empathy_contact_get_capabilities (EmpathyContact *contact)
596 EmpathyContactPriv *priv;
598 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
600 priv = GET_PRIV (contact);
602 return priv->capabilities;
606 empathy_contact_set_capabilities (EmpathyContact *contact,
607 EmpathyCapabilities capabilities)
609 EmpathyContactPriv *priv;
611 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
613 priv = GET_PRIV (contact);
615 if (priv->capabilities == capabilities)
618 priv->capabilities = capabilities;
620 g_object_notify (G_OBJECT (contact), "capabilities");
624 empathy_contact_is_user (EmpathyContact *contact)
626 EmpathyContactPriv *priv;
628 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
630 priv = GET_PRIV (contact);
632 return priv->is_user;
636 empathy_contact_set_is_user (EmpathyContact *contact,
639 EmpathyContactPriv *priv;
641 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
643 priv = GET_PRIV (contact);
645 if (priv->is_user == is_user)
648 priv->is_user = is_user;
650 g_object_notify (G_OBJECT (contact), "is-user");
654 empathy_contact_is_online (EmpathyContact *contact)
656 EmpathyContactPriv *priv;
658 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
660 priv = GET_PRIV (contact);
662 return (priv->presence > MC_PRESENCE_OFFLINE);
666 empathy_contact_get_status (EmpathyContact *contact)
668 EmpathyContactPriv *priv;
670 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
672 priv = GET_PRIV (contact);
674 if (priv->presence_message)
675 return priv->presence_message;
677 return empathy_presence_get_default_message (priv->presence);
681 empathy_contact_can_voip (EmpathyContact *contact)
683 EmpathyContactPriv *priv;
685 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
687 priv = GET_PRIV (contact);
689 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
690 EMPATHY_CAPABILITIES_VIDEO);
694 empathy_contact_get_ready (EmpathyContact *contact)
696 EmpathyContactPriv *priv;
698 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
700 priv = GET_PRIV (contact);
706 empathy_contact_equal (gconstpointer v1,
709 McAccount *account_a;
710 McAccount *account_b;
714 g_return_val_if_fail (EMPATHY_IS_CONTACT (v1), FALSE);
715 g_return_val_if_fail (EMPATHY_IS_CONTACT (v2), FALSE);
717 account_a = empathy_contact_get_account (EMPATHY_CONTACT (v1));
718 account_b = empathy_contact_get_account (EMPATHY_CONTACT (v2));
720 id_a = empathy_contact_get_id (EMPATHY_CONTACT (v1));
721 id_b = empathy_contact_get_id (EMPATHY_CONTACT (v2));
723 return empathy_account_equal (account_a, account_b) &&
724 !tp_strdiff (id_a, id_b);
728 empathy_contact_hash (gconstpointer key)
730 EmpathyContactPriv *priv;
732 g_return_val_if_fail (EMPATHY_IS_CONTACT (key), +1);
734 priv = GET_PRIV (EMPATHY_CONTACT (key));
736 if (priv->hash == 0) {
737 priv->hash = empathy_account_hash (priv->account) ^
738 g_str_hash (priv->id);
745 contact_is_ready_func (GObject *contact,
748 EmpathyContactPriv *priv = GET_PRIV (contact);
749 EmpathyContactReady ready;
751 ready = GPOINTER_TO_UINT (user_data);
753 /* When the name is NULL, empathy_contact_get_name() fallback to the id.
754 * When the caller want to wait the name to be ready, it also want to wait
755 * the id to be ready in case of fallback. */
756 if ((ready & EMPATHY_CONTACT_READY_NAME) && G_STR_EMPTY (priv->name)) {
757 ready |= EMPATHY_CONTACT_READY_ID;
760 return (priv->ready & ready) == ready;
764 empathy_contact_run_until_ready (EmpathyContact *contact,
765 EmpathyContactReady ready,
768 empathy_run_until_ready_full (contact, "notify::ready",
769 contact_is_ready_func, GUINT_TO_POINTER (ready),
774 contact_get_avatar_filename (EmpathyContact *contact,
777 EmpathyContactPriv *priv = GET_PRIV (contact);
780 gchar *token_escaped;
781 gchar *contact_escaped;
783 if (G_STR_EMPTY (priv->id))
786 contact_escaped = tp_escape_as_identifier (priv->id);
787 token_escaped = tp_escape_as_identifier (token);
789 avatar_path = g_build_filename (g_get_user_cache_dir (),
792 mc_account_get_unique_name (priv->account),
795 g_mkdir_with_parents (avatar_path, 0700);
797 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
799 g_free (contact_escaped);
800 g_free (token_escaped);
801 g_free (avatar_path);
807 empathy_contact_load_avatar_data (EmpathyContact *contact,
813 EmpathyAvatar *avatar;
815 GError *error = NULL;
817 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
818 g_return_if_fail (data != NULL);
819 g_return_if_fail (len > 0);
820 g_return_if_fail (format != NULL);
821 g_return_if_fail (!G_STR_EMPTY (token));
823 /* Load and set the avatar */
824 avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
826 empathy_contact_set_avatar (contact, avatar);
827 empathy_avatar_unref (avatar);
829 /* Save to cache if not yet in it */
830 filename = contact_get_avatar_filename (contact, token);
831 if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
833 if (!empathy_avatar_save_to_file (avatar, filename, &error))
835 DEBUG ("Failed to save avatar in cache: %s",
836 error ? error->message : "No error given");
837 g_clear_error (&error);
841 DEBUG ("Avatar saved to %s", filename);
848 empathy_contact_load_avatar_cache (EmpathyContact *contact,
851 EmpathyAvatar *avatar = NULL;
855 GError *error = NULL;
857 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
858 g_return_val_if_fail (!G_STR_EMPTY (token), FALSE);
860 /* Load the avatar from file if it exists */
861 filename = contact_get_avatar_filename (contact, token);
862 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
864 if (!g_file_get_contents (filename, &data, &len, &error))
866 DEBUG ("Failed to load avatar from cache: %s",
867 error ? error->message : "No error given");
868 g_clear_error (&error);
874 DEBUG ("Avatar loaded from %s", filename);
875 avatar = empathy_avatar_new (data, len, NULL, g_strdup (token));
876 empathy_contact_set_avatar (contact, avatar);
877 empathy_avatar_unref (avatar);
886 empathy_avatar_get_type (void)
888 static GType type_id = 0;
892 type_id = g_boxed_type_register_static ("EmpathyAvatar",
893 (GBoxedCopyFunc) empathy_avatar_ref,
894 (GBoxedFreeFunc) empathy_avatar_unref);
901 empathy_avatar_new (guchar *data,
906 EmpathyAvatar *avatar;
908 avatar = g_slice_new0 (EmpathyAvatar);
911 avatar->format = format;
912 avatar->token = token;
913 avatar->refcount = 1;
919 empathy_avatar_unref (EmpathyAvatar *avatar)
921 g_return_if_fail (avatar != NULL);
924 if (avatar->refcount == 0)
926 g_free (avatar->data);
927 g_free (avatar->format);
928 g_free (avatar->token);
929 g_slice_free (EmpathyAvatar, avatar);
934 empathy_avatar_ref (EmpathyAvatar *avatar)
936 g_return_val_if_fail (avatar != NULL, NULL);
944 * empathy_avatar_save_to_file:
945 * @avatar: the avatar
946 * @filename: name of a file to write avatar to
947 * @error: return location for a GError, or NULL
949 * Save the avatar to a file named filename
951 * Returns: %TRUE on success, %FALSE if an error occurred
954 empathy_avatar_save_to_file (EmpathyAvatar *self,
955 const gchar *filename,
958 return g_file_set_contents (filename, self->data, self->len, error);