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 empathy_contact_set_handle (EmpathyContact *contact,
567 EmpathyContactPriv *priv;
569 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
571 priv = GET_PRIV (contact);
573 g_object_ref (contact);
574 if (handle != priv->handle)
576 priv->handle = handle;
577 g_object_notify (G_OBJECT (contact), "handle");
579 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_HANDLE);
580 g_object_unref (contact);
584 empathy_contact_get_capabilities (EmpathyContact *contact)
586 EmpathyContactPriv *priv;
588 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
590 priv = GET_PRIV (contact);
592 return priv->capabilities;
596 empathy_contact_set_capabilities (EmpathyContact *contact,
597 EmpathyCapabilities capabilities)
599 EmpathyContactPriv *priv;
601 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
603 priv = GET_PRIV (contact);
605 if (priv->capabilities == capabilities)
608 priv->capabilities = capabilities;
610 g_object_notify (G_OBJECT (contact), "capabilities");
614 empathy_contact_is_user (EmpathyContact *contact)
616 EmpathyContactPriv *priv;
618 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
620 priv = GET_PRIV (contact);
622 return priv->is_user;
626 empathy_contact_set_is_user (EmpathyContact *contact,
629 EmpathyContactPriv *priv;
631 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
633 priv = GET_PRIV (contact);
635 if (priv->is_user == is_user)
638 priv->is_user = is_user;
640 g_object_notify (G_OBJECT (contact), "is-user");
644 empathy_contact_is_online (EmpathyContact *contact)
646 EmpathyContactPriv *priv;
648 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
650 priv = GET_PRIV (contact);
652 return (priv->presence > MC_PRESENCE_OFFLINE);
656 empathy_contact_get_status (EmpathyContact *contact)
658 EmpathyContactPriv *priv;
660 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
662 priv = GET_PRIV (contact);
664 if (priv->presence_message)
665 return priv->presence_message;
667 return empathy_presence_get_default_message (priv->presence);
671 empathy_contact_can_voip (EmpathyContact *contact)
673 EmpathyContactPriv *priv;
675 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
677 priv = GET_PRIV (contact);
679 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
680 EMPATHY_CAPABILITIES_VIDEO);
684 empathy_contact_get_ready (EmpathyContact *contact)
686 EmpathyContactPriv *priv;
688 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
690 priv = GET_PRIV (contact);
696 empathy_contact_equal (gconstpointer v1,
699 McAccount *account_a;
700 McAccount *account_b;
704 g_return_val_if_fail (EMPATHY_IS_CONTACT (v1), FALSE);
705 g_return_val_if_fail (EMPATHY_IS_CONTACT (v2), FALSE);
707 account_a = empathy_contact_get_account (EMPATHY_CONTACT (v1));
708 account_b = empathy_contact_get_account (EMPATHY_CONTACT (v2));
710 id_a = empathy_contact_get_id (EMPATHY_CONTACT (v1));
711 id_b = empathy_contact_get_id (EMPATHY_CONTACT (v2));
713 return empathy_account_equal (account_a, account_b) &&
714 !tp_strdiff (id_a, id_b);
718 empathy_contact_hash (gconstpointer key)
720 EmpathyContactPriv *priv;
722 g_return_val_if_fail (EMPATHY_IS_CONTACT (key), +1);
724 priv = GET_PRIV (EMPATHY_CONTACT (key));
728 priv->hash = empathy_account_hash (priv->account) ^
729 g_str_hash (priv->id);
736 contact_is_ready_func (GObject *contact,
739 EmpathyContactPriv *priv = GET_PRIV (contact);
740 EmpathyContactReady ready;
742 ready = GPOINTER_TO_UINT (user_data);
744 /* When the name is NULL, empathy_contact_get_name() fallback to the id.
745 * When the caller want to wait the name to be ready, it also want to wait
746 * the id to be ready in case of fallback. */
747 if ((ready & EMPATHY_CONTACT_READY_NAME) && G_STR_EMPTY (priv->name))
748 ready |= EMPATHY_CONTACT_READY_ID;
750 return (priv->ready & ready) == ready;
754 empathy_contact_run_until_ready (EmpathyContact *contact,
755 EmpathyContactReady ready,
758 empathy_run_until_ready_full (contact, "notify::ready",
759 contact_is_ready_func, GUINT_TO_POINTER (ready),
764 contact_get_avatar_filename (EmpathyContact *contact,
767 EmpathyContactPriv *priv = GET_PRIV (contact);
770 gchar *token_escaped;
771 gchar *contact_escaped;
773 if (G_STR_EMPTY (priv->id))
776 contact_escaped = tp_escape_as_identifier (priv->id);
777 token_escaped = tp_escape_as_identifier (token);
779 avatar_path = g_build_filename (g_get_user_cache_dir (),
782 mc_account_get_unique_name (priv->account),
785 g_mkdir_with_parents (avatar_path, 0700);
787 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
789 g_free (contact_escaped);
790 g_free (token_escaped);
791 g_free (avatar_path);
797 empathy_contact_load_avatar_data (EmpathyContact *contact,
803 EmpathyAvatar *avatar;
805 GError *error = NULL;
807 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
808 g_return_if_fail (data != NULL);
809 g_return_if_fail (len > 0);
810 g_return_if_fail (format != NULL);
811 g_return_if_fail (!G_STR_EMPTY (token));
813 /* Load and set the avatar */
814 avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
816 empathy_contact_set_avatar (contact, avatar);
817 empathy_avatar_unref (avatar);
819 /* Save to cache if not yet in it */
820 filename = contact_get_avatar_filename (contact, token);
821 if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
823 if (!empathy_avatar_save_to_file (avatar, filename, &error))
825 DEBUG ("Failed to save avatar in cache: %s",
826 error ? error->message : "No error given");
827 g_clear_error (&error);
830 DEBUG ("Avatar saved to %s", filename);
836 empathy_contact_load_avatar_cache (EmpathyContact *contact,
839 EmpathyAvatar *avatar = NULL;
843 GError *error = NULL;
845 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
846 g_return_val_if_fail (!G_STR_EMPTY (token), FALSE);
848 /* Load the avatar from file if it exists */
849 filename = contact_get_avatar_filename (contact, token);
850 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
852 if (!g_file_get_contents (filename, &data, &len, &error))
854 DEBUG ("Failed to load avatar from cache: %s",
855 error ? error->message : "No error given");
856 g_clear_error (&error);
862 DEBUG ("Avatar loaded from %s", filename);
863 avatar = empathy_avatar_new (data, len, NULL, g_strdup (token));
864 empathy_contact_set_avatar (contact, avatar);
865 empathy_avatar_unref (avatar);
874 empathy_avatar_get_type (void)
876 static GType type_id = 0;
880 type_id = g_boxed_type_register_static ("EmpathyAvatar",
881 (GBoxedCopyFunc) empathy_avatar_ref,
882 (GBoxedFreeFunc) empathy_avatar_unref);
889 empathy_avatar_new (guchar *data,
894 EmpathyAvatar *avatar;
896 avatar = g_slice_new0 (EmpathyAvatar);
899 avatar->format = format;
900 avatar->token = token;
901 avatar->refcount = 1;
907 empathy_avatar_unref (EmpathyAvatar *avatar)
909 g_return_if_fail (avatar != NULL);
912 if (avatar->refcount == 0)
914 g_free (avatar->data);
915 g_free (avatar->format);
916 g_free (avatar->token);
917 g_slice_free (EmpathyAvatar, avatar);
922 empathy_avatar_ref (EmpathyAvatar *avatar)
924 g_return_val_if_fail (avatar != NULL, NULL);
932 * empathy_avatar_save_to_file:
933 * @avatar: the avatar
934 * @filename: name of a file to write avatar to
935 * @error: return location for a GError, or NULL
937 * Save the avatar to a file named filename
939 * Returns: %TRUE on success, %FALSE if an error occurred
942 empathy_avatar_save_to_file (EmpathyAvatar *self,
943 const gchar *filename,
946 return g_file_set_contents (filename, self->data, self->len, error);