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);
202 empathy_avatar_unref (priv->avatar);
205 g_object_unref (priv->account);
207 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
211 contact_get_property (GObject *object,
216 EmpathyContactPriv *priv;
218 priv = GET_PRIV (object);
223 g_value_set_string (value, priv->id);
226 g_value_set_string (value,
227 empathy_contact_get_name (EMPATHY_CONTACT (object)));
230 g_value_set_boxed (value, priv->avatar);
233 g_value_set_object (value, priv->account);
236 g_value_set_uint (value, priv->presence);
238 case PROP_PRESENCE_MESSAGE:
239 g_value_set_string (value, priv->presence_message);
242 g_value_set_uint (value, priv->handle);
244 case PROP_CAPABILITIES:
245 g_value_set_flags (value, priv->capabilities);
248 g_value_set_boolean (value, priv->is_user);
251 g_value_set_flags (value, priv->ready);
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
260 contact_set_property (GObject *object,
265 EmpathyContactPriv *priv;
267 priv = GET_PRIV (object);
272 empathy_contact_set_id (EMPATHY_CONTACT (object),
273 g_value_get_string (value));
276 empathy_contact_set_name (EMPATHY_CONTACT (object),
277 g_value_get_string (value));
280 empathy_contact_set_avatar (EMPATHY_CONTACT (object),
281 g_value_get_boxed (value));
284 empathy_contact_set_account (EMPATHY_CONTACT (object),
285 MC_ACCOUNT (g_value_get_object (value)));
288 empathy_contact_set_presence (EMPATHY_CONTACT (object),
289 g_value_get_uint (value));
291 case PROP_PRESENCE_MESSAGE:
292 empathy_contact_set_presence_message (EMPATHY_CONTACT (object),
293 g_value_get_string (value));
296 empathy_contact_set_handle (EMPATHY_CONTACT (object),
297 g_value_get_uint (value));
299 case PROP_CAPABILITIES:
300 empathy_contact_set_capabilities (EMPATHY_CONTACT (object),
301 g_value_get_flags (value));
304 empathy_contact_set_is_user (EMPATHY_CONTACT (object),
305 g_value_get_boolean (value));
308 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
314 contact_set_ready_flag (EmpathyContact *contact,
315 EmpathyContactReady flag)
317 EmpathyContactPriv *priv = GET_PRIV (contact);
319 if (!(priv->ready & flag))
322 g_object_notify (G_OBJECT (contact), "ready");
327 empathy_contact_new (McAccount *account)
329 return g_object_new (EMPATHY_TYPE_CONTACT,
335 empathy_contact_new_full (McAccount *account,
339 return g_object_new (EMPATHY_TYPE_CONTACT,
347 empathy_contact_get_id (EmpathyContact *contact)
349 EmpathyContactPriv *priv;
351 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
353 priv = GET_PRIV (contact);
359 empathy_contact_set_id (EmpathyContact *contact,
362 EmpathyContactPriv *priv;
364 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
365 g_return_if_fail (id != NULL);
367 priv = GET_PRIV (contact);
369 /* We temporally ref the contact because it could be destroyed
370 * during the signal emition */
371 g_object_ref (contact);
372 if (tp_strdiff (id, priv->id))
375 priv->id = g_strdup (id);
377 g_object_notify (G_OBJECT (contact), "id");
378 if (G_STR_EMPTY (priv->name))
379 g_object_notify (G_OBJECT (contact), "name");
381 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_ID);
383 g_object_unref (contact);
387 empathy_contact_get_name (EmpathyContact *contact)
389 EmpathyContactPriv *priv;
391 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
393 priv = GET_PRIV (contact);
395 if (G_STR_EMPTY (priv->name))
396 return empathy_contact_get_id (contact);
402 empathy_contact_set_name (EmpathyContact *contact,
405 EmpathyContactPriv *priv;
407 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
409 priv = GET_PRIV (contact);
411 g_object_ref (contact);
412 if (tp_strdiff (name, priv->name))
415 priv->name = g_strdup (name);
416 g_object_notify (G_OBJECT (contact), "name");
418 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_NAME);
419 g_object_unref (contact);
423 empathy_contact_get_avatar (EmpathyContact *contact)
425 EmpathyContactPriv *priv;
427 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
429 priv = GET_PRIV (contact);
435 empathy_contact_set_avatar (EmpathyContact *contact,
436 EmpathyAvatar *avatar)
438 EmpathyContactPriv *priv;
440 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
442 priv = GET_PRIV (contact);
444 if (priv->avatar == avatar)
449 empathy_avatar_unref (priv->avatar);
454 priv->avatar = empathy_avatar_ref (avatar);
456 g_object_notify (G_OBJECT (contact), "avatar");
460 empathy_contact_get_account (EmpathyContact *contact)
462 EmpathyContactPriv *priv;
464 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
466 priv = GET_PRIV (contact);
468 return priv->account;
472 empathy_contact_set_account (EmpathyContact *contact,
475 EmpathyContactPriv *priv;
477 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
478 g_return_if_fail (MC_IS_ACCOUNT (account));
480 priv = GET_PRIV (contact);
482 if (account == priv->account)
486 g_object_unref (priv->account);
487 priv->account = g_object_ref (account);
489 g_object_notify (G_OBJECT (contact), "account");
493 empathy_contact_get_presence (EmpathyContact *contact)
495 EmpathyContactPriv *priv;
497 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), MC_PRESENCE_UNSET);
499 priv = GET_PRIV (contact);
501 return priv->presence;
505 empathy_contact_set_presence (EmpathyContact *contact,
508 EmpathyContactPriv *priv;
510 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
512 priv = GET_PRIV (contact);
514 if (presence == priv->presence)
517 priv->presence = presence;
519 g_object_notify (G_OBJECT (contact), "presence");
523 empathy_contact_get_presence_message (EmpathyContact *contact)
525 EmpathyContactPriv *priv;
527 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
529 priv = GET_PRIV (contact);
531 return priv->presence_message;
535 empathy_contact_set_presence_message (EmpathyContact *contact,
536 const gchar *message)
538 EmpathyContactPriv *priv = GET_PRIV (contact);
540 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
542 if (!tp_strdiff (message, priv->presence_message))
545 g_free (priv->presence_message);
546 priv->presence_message = g_strdup (message);
548 g_object_notify (G_OBJECT (contact), "presence-message");
552 empathy_contact_get_handle (EmpathyContact *contact)
554 EmpathyContactPriv *priv;
556 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
558 priv = GET_PRIV (contact);
564 contact_is_salut (EmpathyContact *contact)
570 account = empathy_contact_get_account (contact);
571 profile = mc_account_get_profile (account);
572 name = mc_profile_get_protocol_name (profile);
574 return (strcmp (name, "local-xmpp") == 0);
578 empathy_contact_set_handle (EmpathyContact *contact,
581 EmpathyContactPriv *priv;
583 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
585 priv = GET_PRIV (contact);
587 g_object_ref (contact);
588 if (handle != priv->handle)
590 priv->handle = handle;
591 g_object_notify (G_OBJECT (contact), "handle");
593 /* FIXME salut does not yet support the Capabilities interface, so for
594 * now we use this hack.
596 if (contact_is_salut (contact))
598 EmpathyCapabilities caps;
600 caps = empathy_contact_get_capabilities (contact);
601 caps |= EMPATHY_CAPABILITIES_FT;
603 empathy_contact_set_capabilities (contact, caps);
606 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_HANDLE);
607 g_object_unref (contact);
611 empathy_contact_get_capabilities (EmpathyContact *contact)
613 EmpathyContactPriv *priv;
615 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
617 priv = GET_PRIV (contact);
619 return priv->capabilities;
623 empathy_contact_set_capabilities (EmpathyContact *contact,
624 EmpathyCapabilities capabilities)
626 EmpathyContactPriv *priv;
628 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
630 priv = GET_PRIV (contact);
632 if (priv->capabilities == capabilities)
635 priv->capabilities = capabilities;
637 g_object_notify (G_OBJECT (contact), "capabilities");
641 empathy_contact_is_user (EmpathyContact *contact)
643 EmpathyContactPriv *priv;
645 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
647 priv = GET_PRIV (contact);
649 return priv->is_user;
653 empathy_contact_set_is_user (EmpathyContact *contact,
656 EmpathyContactPriv *priv;
658 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
660 priv = GET_PRIV (contact);
662 if (priv->is_user == is_user)
665 priv->is_user = is_user;
667 g_object_notify (G_OBJECT (contact), "is-user");
671 empathy_contact_is_online (EmpathyContact *contact)
673 EmpathyContactPriv *priv;
675 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
677 priv = GET_PRIV (contact);
679 return (priv->presence > MC_PRESENCE_OFFLINE);
683 empathy_contact_get_status (EmpathyContact *contact)
685 EmpathyContactPriv *priv;
687 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
689 priv = GET_PRIV (contact);
691 if (priv->presence_message)
692 return priv->presence_message;
694 return empathy_presence_get_default_message (priv->presence);
698 empathy_contact_can_voip (EmpathyContact *contact)
700 EmpathyContactPriv *priv;
702 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
704 priv = GET_PRIV (contact);
706 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
707 EMPATHY_CAPABILITIES_VIDEO);
711 empathy_contact_can_send_files (EmpathyContact *contact)
713 EmpathyContactPriv *priv;
715 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
717 priv = GET_PRIV (contact);
719 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
723 empathy_contact_get_ready (EmpathyContact *contact)
725 EmpathyContactPriv *priv;
727 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
729 priv = GET_PRIV (contact);
735 empathy_contact_equal (gconstpointer v1,
738 McAccount *account_a;
739 McAccount *account_b;
743 g_return_val_if_fail (EMPATHY_IS_CONTACT (v1), FALSE);
744 g_return_val_if_fail (EMPATHY_IS_CONTACT (v2), FALSE);
746 account_a = empathy_contact_get_account (EMPATHY_CONTACT (v1));
747 account_b = empathy_contact_get_account (EMPATHY_CONTACT (v2));
749 id_a = empathy_contact_get_id (EMPATHY_CONTACT (v1));
750 id_b = empathy_contact_get_id (EMPATHY_CONTACT (v2));
752 return empathy_account_equal (account_a, account_b) &&
753 !tp_strdiff (id_a, id_b);
757 empathy_contact_hash (gconstpointer key)
759 EmpathyContactPriv *priv;
761 g_return_val_if_fail (EMPATHY_IS_CONTACT (key), +1);
763 priv = GET_PRIV (EMPATHY_CONTACT (key));
767 priv->hash = empathy_account_hash (priv->account) ^
768 g_str_hash (priv->id);
775 contact_is_ready_func (GObject *contact,
778 EmpathyContactPriv *priv = GET_PRIV (contact);
779 EmpathyContactReady ready;
781 ready = GPOINTER_TO_UINT (user_data);
783 /* When the name is NULL, empathy_contact_get_name() fallback to the id.
784 * When the caller want to wait the name to be ready, it also want to wait
785 * the id to be ready in case of fallback. */
786 if ((ready & EMPATHY_CONTACT_READY_NAME) && G_STR_EMPTY (priv->name))
787 ready |= EMPATHY_CONTACT_READY_ID;
789 return (priv->ready & ready) == ready;
793 empathy_contact_run_until_ready (EmpathyContact *contact,
794 EmpathyContactReady ready,
797 empathy_run_until_ready_full (contact, "notify::ready",
798 contact_is_ready_func, GUINT_TO_POINTER (ready),
803 contact_get_avatar_filename (EmpathyContact *contact,
806 EmpathyContactPriv *priv = GET_PRIV (contact);
809 gchar *token_escaped;
810 gchar *contact_escaped;
812 if (G_STR_EMPTY (priv->id))
815 contact_escaped = tp_escape_as_identifier (priv->id);
816 token_escaped = tp_escape_as_identifier (token);
818 avatar_path = g_build_filename (g_get_user_cache_dir (),
821 mc_account_get_unique_name (priv->account),
824 g_mkdir_with_parents (avatar_path, 0700);
826 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
828 g_free (contact_escaped);
829 g_free (token_escaped);
830 g_free (avatar_path);
836 empathy_contact_load_avatar_data (EmpathyContact *contact,
842 EmpathyAvatar *avatar;
844 GError *error = NULL;
846 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
847 g_return_if_fail (data != NULL);
848 g_return_if_fail (len > 0);
849 g_return_if_fail (format != NULL);
850 g_return_if_fail (!G_STR_EMPTY (token));
852 /* Load and set the avatar */
853 avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
855 empathy_contact_set_avatar (contact, avatar);
856 empathy_avatar_unref (avatar);
858 /* Save to cache if not yet in it */
859 filename = contact_get_avatar_filename (contact, token);
860 if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
862 if (!empathy_avatar_save_to_file (avatar, filename, &error))
864 DEBUG ("Failed to save avatar in cache: %s",
865 error ? error->message : "No error given");
866 g_clear_error (&error);
869 DEBUG ("Avatar saved to %s", filename);
875 empathy_contact_load_avatar_cache (EmpathyContact *contact,
878 EmpathyAvatar *avatar = NULL;
882 GError *error = NULL;
884 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
885 g_return_val_if_fail (!G_STR_EMPTY (token), FALSE);
887 /* Load the avatar from file if it exists */
888 filename = contact_get_avatar_filename (contact, token);
889 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
891 if (!g_file_get_contents (filename, &data, &len, &error))
893 DEBUG ("Failed to load avatar from cache: %s",
894 error ? error->message : "No error given");
895 g_clear_error (&error);
901 DEBUG ("Avatar loaded from %s", filename);
902 avatar = empathy_avatar_new (data, len, NULL, g_strdup (token));
903 empathy_contact_set_avatar (contact, avatar);
904 empathy_avatar_unref (avatar);
913 empathy_avatar_get_type (void)
915 static GType type_id = 0;
919 type_id = g_boxed_type_register_static ("EmpathyAvatar",
920 (GBoxedCopyFunc) empathy_avatar_ref,
921 (GBoxedFreeFunc) empathy_avatar_unref);
928 empathy_avatar_new (guchar *data,
933 EmpathyAvatar *avatar;
935 avatar = g_slice_new0 (EmpathyAvatar);
938 avatar->format = format;
939 avatar->token = token;
940 avatar->refcount = 1;
946 empathy_avatar_unref (EmpathyAvatar *avatar)
948 g_return_if_fail (avatar != NULL);
951 if (avatar->refcount == 0)
953 g_free (avatar->data);
954 g_free (avatar->format);
955 g_free (avatar->token);
956 g_slice_free (EmpathyAvatar, avatar);
961 empathy_avatar_ref (EmpathyAvatar *avatar)
963 g_return_val_if_fail (avatar != NULL, NULL);
971 * empathy_avatar_save_to_file:
972 * @avatar: the avatar
973 * @filename: name of a file to write avatar to
974 * @error: return location for a GError, or NULL
976 * Save the avatar to a file named filename
978 * Returns: %TRUE on success, %FALSE if an error occurred
981 empathy_avatar_save_to_file (EmpathyAvatar *self,
982 const gchar *filename,
985 return g_file_set_contents (filename, self->data, self->len, error);