1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
3 * Copyright (C) 2007-2009 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
20 * Sjoerd Simons <sjoerd.simons@collabora.co.uk>
27 #include <glib/gi18n-lib.h>
29 #include <telepathy-glib/util.h>
30 #include <libmissioncontrol/mc-enum-types.h>
32 #include "empathy-contact.h"
33 #include "empathy-account-manager.h"
34 #include "empathy-utils.h"
35 #include "empathy-enum-types.h"
36 #include "empathy-marshal.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
39 #include "empathy-debug.h"
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
43 TpContact *tp_contact;
47 EmpathyAvatar *avatar;
49 gchar *presence_message;
51 EmpathyCapabilities capabilities;
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);
73 PROP_PRESENCE_MESSAGE,
84 static guint signals[LAST_SIGNAL];
87 tp_contact_notify_cb (TpContact *tp_contact,
91 EmpathyContactPriv *priv = GET_PRIV (contact);
93 /* Forward property notifications */
94 if (!tp_strdiff (param->name, "alias"))
95 g_object_notify (contact, "name");
96 else if (!tp_strdiff (param->name, "presence-type")) {
99 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
100 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, priv->presence);
101 priv->presence = presence;
102 g_object_notify (contact, "presence");
104 else if (!tp_strdiff (param->name, "presence-message"))
105 g_object_notify (contact, "presence-message");
106 else if (!tp_strdiff (param->name, "identifier"))
107 g_object_notify (contact, "id");
108 else if (!tp_strdiff (param->name, "handle"))
109 g_object_notify (contact, "handle");
113 contact_dispose (GObject *object)
115 EmpathyContactPriv *priv = GET_PRIV (object);
117 if (priv->tp_contact)
119 g_signal_handlers_disconnect_by_func (priv->tp_contact,
120 tp_contact_notify_cb, object);
121 g_object_unref (priv->tp_contact);
123 priv->tp_contact = NULL;
126 g_object_unref (priv->account);
127 priv->account = NULL;
129 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
133 empathy_contact_class_init (EmpathyContactClass *class)
135 GObjectClass *object_class;
137 object_class = G_OBJECT_CLASS (class);
139 object_class->finalize = contact_finalize;
140 object_class->dispose = contact_dispose;
141 object_class->get_property = contact_get_property;
142 object_class->set_property = contact_set_property;
144 g_object_class_install_property (object_class,
146 g_param_spec_object ("tp-contact",
148 "The TpContact associated with the contact",
150 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
152 g_object_class_install_property (object_class,
154 g_param_spec_object ("account",
156 "The account associated with the contact",
158 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
160 g_object_class_install_property (object_class,
162 g_param_spec_string ("id",
164 "String identifying contact",
168 g_object_class_install_property (object_class,
170 g_param_spec_string ("name",
172 "The name of the contact",
176 g_object_class_install_property (object_class,
178 g_param_spec_boxed ("avatar",
184 g_object_class_install_property (object_class,
186 g_param_spec_uint ("presence",
188 "Presence of contact",
194 g_object_class_install_property (object_class,
195 PROP_PRESENCE_MESSAGE,
196 g_param_spec_string ("presence-message",
197 "Contact presence message",
198 "Presence message of contact",
202 g_object_class_install_property (object_class,
204 g_param_spec_uint ("handle",
206 "The handle of the contact",
212 g_object_class_install_property (object_class,
214 g_param_spec_flags ("capabilities",
215 "Contact Capabilities",
216 "Capabilities of the contact",
217 EMPATHY_TYPE_CAPABILITIES,
218 EMPATHY_CAPABILITIES_UNKNOWN,
219 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
221 g_object_class_install_property (object_class,
223 g_param_spec_boolean ("is-user",
225 "Is contact the user",
229 signals[PRESENCE_CHANGED] =
230 g_signal_new ("presence-changed",
231 G_TYPE_FROM_CLASS (class),
235 _empathy_marshal_VOID__ENUM_ENUM,
240 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
244 empathy_contact_init (EmpathyContact *contact)
246 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
247 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
249 contact->priv = priv;
253 contact_finalize (GObject *object)
255 EmpathyContactPriv *priv;
257 priv = GET_PRIV (object);
259 DEBUG ("finalize: %p", object);
263 g_free (priv->presence_message);
266 empathy_avatar_unref (priv->avatar);
268 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
272 set_tp_contact (EmpathyContact *contact,
273 TpContact *tp_contact)
275 EmpathyContactPriv *priv = GET_PRIV (contact);
277 g_assert (priv->tp_contact == NULL);
278 priv->tp_contact = g_object_ref (tp_contact);
279 priv->presence = empathy_contact_get_presence (contact);
281 g_signal_connect (priv->tp_contact, "notify",
282 G_CALLBACK (tp_contact_notify_cb), contact);
286 contact_get_property (GObject *object,
291 EmpathyContact *contact = EMPATHY_CONTACT (object);
295 case PROP_TP_CONTACT:
296 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
299 g_value_set_object (value, empathy_contact_get_account (contact));
302 g_value_set_string (value, empathy_contact_get_id (contact));
305 g_value_set_string (value, empathy_contact_get_name (contact));
308 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
311 g_value_set_uint (value, empathy_contact_get_presence (contact));
313 case PROP_PRESENCE_MESSAGE:
314 g_value_set_string (value, empathy_contact_get_presence_message (contact));
317 g_value_set_uint (value, empathy_contact_get_handle (contact));
319 case PROP_CAPABILITIES:
320 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
323 g_value_set_boolean (value, empathy_contact_is_user (contact));
326 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
332 contact_set_property (GObject *object,
337 EmpathyContact *contact = EMPATHY_CONTACT (object);
338 EmpathyContactPriv *priv = GET_PRIV (object);
342 case PROP_TP_CONTACT:
343 set_tp_contact (contact, g_value_get_object (value));
346 g_assert (priv->account == NULL);
347 priv->account = g_value_dup_object (value);
350 empathy_contact_set_id (contact, g_value_get_string (value));
353 empathy_contact_set_name (contact, g_value_get_string (value));
356 empathy_contact_set_avatar (contact, g_value_get_boxed (value));
359 empathy_contact_set_presence (contact, g_value_get_uint (value));
361 case PROP_PRESENCE_MESSAGE:
362 empathy_contact_set_presence_message (contact, g_value_get_string (value));
365 empathy_contact_set_handle (contact, g_value_get_uint (value));
367 case PROP_CAPABILITIES:
368 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
371 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
374 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
380 empathy_contact_new (TpContact *tp_contact)
382 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
384 return g_object_new (EMPATHY_TYPE_CONTACT,
385 "tp-contact", tp_contact,
390 empathy_contact_get_tp_contact (EmpathyContact *contact)
392 EmpathyContactPriv *priv;
394 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
396 priv = GET_PRIV (contact);
398 return priv->tp_contact;
402 empathy_contact_get_id (EmpathyContact *contact)
404 EmpathyContactPriv *priv;
406 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
408 priv = GET_PRIV (contact);
410 if (priv->tp_contact != NULL)
411 return tp_contact_get_identifier (priv->tp_contact);
417 empathy_contact_set_id (EmpathyContact *contact,
420 EmpathyContactPriv *priv;
422 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
423 g_return_if_fail (id != NULL);
425 priv = GET_PRIV (contact);
427 /* We temporally ref the contact because it could be destroyed
428 * during the signal emition */
429 g_object_ref (contact);
430 if (tp_strdiff (id, priv->id))
433 priv->id = g_strdup (id);
435 g_object_notify (G_OBJECT (contact), "id");
436 if (EMP_STR_EMPTY (priv->name))
437 g_object_notify (G_OBJECT (contact), "name");
440 g_object_unref (contact);
444 empathy_contact_get_name (EmpathyContact *contact)
446 EmpathyContactPriv *priv;
448 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
450 priv = GET_PRIV (contact);
452 if (priv->tp_contact != NULL)
453 return tp_contact_get_alias (priv->tp_contact);
455 if (EMP_STR_EMPTY (priv->name))
456 return empathy_contact_get_id (contact);
462 empathy_contact_set_name (EmpathyContact *contact,
465 EmpathyContactPriv *priv;
467 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
469 priv = GET_PRIV (contact);
471 g_object_ref (contact);
472 if (tp_strdiff (name, priv->name))
475 priv->name = g_strdup (name);
476 g_object_notify (G_OBJECT (contact), "name");
478 g_object_unref (contact);
482 empathy_contact_get_avatar (EmpathyContact *contact)
484 EmpathyContactPriv *priv;
486 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
488 priv = GET_PRIV (contact);
494 empathy_contact_set_avatar (EmpathyContact *contact,
495 EmpathyAvatar *avatar)
497 EmpathyContactPriv *priv;
499 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
501 priv = GET_PRIV (contact);
503 if (priv->avatar == avatar)
508 empathy_avatar_unref (priv->avatar);
513 priv->avatar = empathy_avatar_ref (avatar);
515 g_object_notify (G_OBJECT (contact), "avatar");
519 empathy_contact_get_account (EmpathyContact *contact)
521 EmpathyContactPriv *priv;
523 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
525 priv = GET_PRIV (contact);
527 if (priv->account != NULL)
528 return priv->account;
530 if (priv->tp_contact != NULL)
532 EmpathyAccountManager *manager;
533 TpConnection *connection;
535 /* FIXME: This assume the account manager already exists */
536 manager = empathy_account_manager_dup_singleton ();
537 connection = tp_contact_get_connection (priv->tp_contact);
538 priv->account = empathy_account_manager_get_account (manager, connection);
539 g_object_unref (manager);
541 return g_object_ref (priv->account);
548 empathy_contact_get_connection (EmpathyContact *contact)
550 EmpathyContactPriv *priv;
552 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
554 priv = GET_PRIV (contact);
556 if (priv->tp_contact != NULL)
557 return tp_contact_get_connection (priv->tp_contact);
563 presence_type_to_mc_presence (TpConnectionPresenceType type)
567 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
568 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
569 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
570 return MC_PRESENCE_UNSET;
571 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
572 return MC_PRESENCE_OFFLINE;
573 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
574 return MC_PRESENCE_AVAILABLE;
575 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
576 return MC_PRESENCE_AWAY;
577 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
578 return MC_PRESENCE_EXTENDED_AWAY;
579 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
580 return MC_PRESENCE_HIDDEN;
581 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
582 return MC_PRESENCE_DO_NOT_DISTURB;
585 return MC_PRESENCE_UNSET;
589 empathy_contact_get_presence (EmpathyContact *contact)
591 EmpathyContactPriv *priv;
593 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), MC_PRESENCE_UNSET);
595 priv = GET_PRIV (contact);
597 if (priv->tp_contact != NULL)
598 return presence_type_to_mc_presence (tp_contact_get_presence_type (
601 return priv->presence;
605 empathy_contact_set_presence (EmpathyContact *contact,
608 EmpathyContactPriv *priv;
609 McPresence old_presence;
611 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
613 priv = GET_PRIV (contact);
615 if (presence == priv->presence)
618 old_presence = priv->presence;
619 priv->presence = presence;
621 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
623 g_object_notify (G_OBJECT (contact), "presence");
627 empathy_contact_get_presence_message (EmpathyContact *contact)
629 EmpathyContactPriv *priv;
631 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
633 priv = GET_PRIV (contact);
635 if (priv->tp_contact != NULL)
636 return tp_contact_get_presence_message (priv->tp_contact);
638 return priv->presence_message;
642 empathy_contact_set_presence_message (EmpathyContact *contact,
643 const gchar *message)
645 EmpathyContactPriv *priv = GET_PRIV (contact);
647 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
649 if (!tp_strdiff (message, priv->presence_message))
652 g_free (priv->presence_message);
653 priv->presence_message = g_strdup (message);
655 g_object_notify (G_OBJECT (contact), "presence-message");
659 empathy_contact_get_handle (EmpathyContact *contact)
661 EmpathyContactPriv *priv;
663 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
665 priv = GET_PRIV (contact);
667 if (priv->tp_contact != NULL)
668 return tp_contact_get_handle (priv->tp_contact);
674 empathy_contact_set_handle (EmpathyContact *contact,
677 EmpathyContactPriv *priv;
679 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
681 priv = GET_PRIV (contact);
683 g_object_ref (contact);
684 if (handle != priv->handle)
686 priv->handle = handle;
687 g_object_notify (G_OBJECT (contact), "handle");
689 g_object_unref (contact);
693 empathy_contact_get_capabilities (EmpathyContact *contact)
695 EmpathyContactPriv *priv;
697 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
699 priv = GET_PRIV (contact);
701 return priv->capabilities;
705 empathy_contact_set_capabilities (EmpathyContact *contact,
706 EmpathyCapabilities capabilities)
708 EmpathyContactPriv *priv;
710 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
712 priv = GET_PRIV (contact);
714 if (priv->capabilities == capabilities)
717 priv->capabilities = capabilities;
719 g_object_notify (G_OBJECT (contact), "capabilities");
723 empathy_contact_is_user (EmpathyContact *contact)
725 EmpathyContactPriv *priv;
727 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
729 priv = GET_PRIV (contact);
731 return priv->is_user;
735 empathy_contact_set_is_user (EmpathyContact *contact,
738 EmpathyContactPriv *priv;
740 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
742 priv = GET_PRIV (contact);
744 if (priv->is_user == is_user)
747 priv->is_user = is_user;
749 g_object_notify (G_OBJECT (contact), "is-user");
753 empathy_contact_is_online (EmpathyContact *contact)
755 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
757 return (empathy_contact_get_presence (contact) > MC_PRESENCE_OFFLINE);
761 empathy_contact_get_status (EmpathyContact *contact)
763 const gchar *message;
765 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
767 message = empathy_contact_get_presence_message (contact);
771 return empathy_presence_get_default_message (
772 empathy_contact_get_presence (contact));
776 empathy_contact_can_voip (EmpathyContact *contact)
778 EmpathyContactPriv *priv;
780 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
782 priv = GET_PRIV (contact);
784 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
785 EMPATHY_CAPABILITIES_VIDEO);
789 empathy_contact_can_send_files (EmpathyContact *contact)
791 EmpathyContactPriv *priv;
793 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
795 priv = GET_PRIV (contact);
797 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
801 contact_get_avatar_filename (EmpathyContact *contact,
804 EmpathyContactPriv *priv = GET_PRIV (contact);
808 gchar *token_escaped;
809 gchar *contact_escaped;
811 if (EMP_STR_EMPTY (priv->id))
814 contact_escaped = tp_escape_as_identifier (priv->id);
815 token_escaped = tp_escape_as_identifier (token);
816 account = empathy_contact_get_account (contact);
818 /* FIXME: Do not use the account, but proto/cm instead */
819 avatar_path = g_build_filename (g_get_user_cache_dir (),
822 mc_account_get_unique_name (account),
825 g_mkdir_with_parents (avatar_path, 0700);
827 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
829 g_free (contact_escaped);
830 g_free (token_escaped);
831 g_free (avatar_path);
837 empathy_contact_load_avatar_data (EmpathyContact *contact,
843 EmpathyAvatar *avatar;
845 GError *error = NULL;
847 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
848 g_return_if_fail (data != NULL);
849 g_return_if_fail (len > 0);
850 g_return_if_fail (format != NULL);
851 g_return_if_fail (!EMP_STR_EMPTY (token));
853 /* Load and set the avatar */
854 avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
856 empathy_contact_set_avatar (contact, avatar);
857 empathy_avatar_unref (avatar);
859 /* Save to cache if not yet in it */
860 filename = contact_get_avatar_filename (contact, token);
861 if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
863 if (!empathy_avatar_save_to_file (avatar, filename, &error))
865 DEBUG ("Failed to save avatar in cache: %s",
866 error ? error->message : "No error given");
867 g_clear_error (&error);
870 DEBUG ("Avatar saved to %s", filename);
876 empathy_contact_load_avatar_cache (EmpathyContact *contact,
879 EmpathyAvatar *avatar = NULL;
883 GError *error = NULL;
885 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
886 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
888 /* Load the avatar from file if it exists */
889 filename = contact_get_avatar_filename (contact, token);
890 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
892 if (!g_file_get_contents (filename, &data, &len, &error))
894 DEBUG ("Failed to load avatar from cache: %s",
895 error ? error->message : "No error given");
896 g_clear_error (&error);
902 DEBUG ("Avatar loaded from %s", filename);
903 avatar = empathy_avatar_new (data, len, NULL, g_strdup (token));
904 empathy_contact_set_avatar (contact, avatar);
905 empathy_avatar_unref (avatar);
914 empathy_avatar_get_type (void)
916 static GType type_id = 0;
920 type_id = g_boxed_type_register_static ("EmpathyAvatar",
921 (GBoxedCopyFunc) empathy_avatar_ref,
922 (GBoxedFreeFunc) empathy_avatar_unref);
929 empathy_avatar_new (guchar *data,
934 EmpathyAvatar *avatar;
936 avatar = g_slice_new0 (EmpathyAvatar);
939 avatar->format = format;
940 avatar->token = token;
941 avatar->refcount = 1;
947 empathy_avatar_unref (EmpathyAvatar *avatar)
949 g_return_if_fail (avatar != NULL);
952 if (avatar->refcount == 0)
954 g_free (avatar->data);
955 g_free (avatar->format);
956 g_free (avatar->token);
957 g_slice_free (EmpathyAvatar, avatar);
962 empathy_avatar_ref (EmpathyAvatar *avatar)
964 g_return_val_if_fail (avatar != NULL, NULL);
972 * empathy_avatar_save_to_file:
973 * @avatar: the avatar
974 * @filename: name of a file to write avatar to
975 * @error: return location for a GError, or NULL
977 * Save the avatar to a file named filename
979 * Returns: %TRUE on success, %FALSE if an error occurred
982 empathy_avatar_save_to_file (EmpathyAvatar *self,
983 const gchar *filename,
986 return g_file_set_contents (filename, self->data, self->len, error);