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 if (tp_contact == NULL)
280 g_assert (priv->tp_contact == NULL);
281 priv->tp_contact = g_object_ref (tp_contact);
282 priv->presence = empathy_contact_get_presence (contact);
284 g_signal_connect (priv->tp_contact, "notify",
285 G_CALLBACK (tp_contact_notify_cb), contact);
289 contact_get_property (GObject *object,
294 EmpathyContact *contact = EMPATHY_CONTACT (object);
298 case PROP_TP_CONTACT:
299 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
302 g_value_set_object (value, empathy_contact_get_account (contact));
305 g_value_set_string (value, empathy_contact_get_id (contact));
308 g_value_set_string (value, empathy_contact_get_name (contact));
311 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
314 g_value_set_uint (value, empathy_contact_get_presence (contact));
316 case PROP_PRESENCE_MESSAGE:
317 g_value_set_string (value, empathy_contact_get_presence_message (contact));
320 g_value_set_uint (value, empathy_contact_get_handle (contact));
322 case PROP_CAPABILITIES:
323 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
326 g_value_set_boolean (value, empathy_contact_is_user (contact));
329 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
335 contact_set_property (GObject *object,
340 EmpathyContact *contact = EMPATHY_CONTACT (object);
341 EmpathyContactPriv *priv = GET_PRIV (object);
345 case PROP_TP_CONTACT:
346 set_tp_contact (contact, g_value_get_object (value));
349 g_assert (priv->account == NULL);
350 priv->account = g_value_dup_object (value);
353 empathy_contact_set_id (contact, g_value_get_string (value));
356 empathy_contact_set_name (contact, g_value_get_string (value));
359 empathy_contact_set_avatar (contact, g_value_get_boxed (value));
362 empathy_contact_set_presence (contact, g_value_get_uint (value));
364 case PROP_PRESENCE_MESSAGE:
365 empathy_contact_set_presence_message (contact, g_value_get_string (value));
368 empathy_contact_set_handle (contact, g_value_get_uint (value));
370 case PROP_CAPABILITIES:
371 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
374 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
377 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
383 empathy_contact_new (TpContact *tp_contact)
385 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
387 return g_object_new (EMPATHY_TYPE_CONTACT,
388 "tp-contact", tp_contact,
393 empathy_contact_get_tp_contact (EmpathyContact *contact)
395 EmpathyContactPriv *priv;
397 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
399 priv = GET_PRIV (contact);
401 return priv->tp_contact;
405 empathy_contact_get_id (EmpathyContact *contact)
407 EmpathyContactPriv *priv;
409 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
411 priv = GET_PRIV (contact);
413 if (priv->tp_contact != NULL)
414 return tp_contact_get_identifier (priv->tp_contact);
420 empathy_contact_set_id (EmpathyContact *contact,
423 EmpathyContactPriv *priv;
425 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
426 g_return_if_fail (id != NULL);
428 priv = GET_PRIV (contact);
430 /* We temporally ref the contact because it could be destroyed
431 * during the signal emition */
432 g_object_ref (contact);
433 if (tp_strdiff (id, priv->id))
436 priv->id = g_strdup (id);
438 g_object_notify (G_OBJECT (contact), "id");
439 if (EMP_STR_EMPTY (priv->name))
440 g_object_notify (G_OBJECT (contact), "name");
443 g_object_unref (contact);
447 empathy_contact_get_name (EmpathyContact *contact)
449 EmpathyContactPriv *priv;
451 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
453 priv = GET_PRIV (contact);
455 if (priv->tp_contact != NULL)
456 return tp_contact_get_alias (priv->tp_contact);
458 if (EMP_STR_EMPTY (priv->name))
459 return empathy_contact_get_id (contact);
465 empathy_contact_set_name (EmpathyContact *contact,
468 EmpathyContactPriv *priv;
470 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
472 priv = GET_PRIV (contact);
474 g_object_ref (contact);
475 if (tp_strdiff (name, priv->name))
478 priv->name = g_strdup (name);
479 g_object_notify (G_OBJECT (contact), "name");
481 g_object_unref (contact);
485 empathy_contact_get_avatar (EmpathyContact *contact)
487 EmpathyContactPriv *priv;
489 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
491 priv = GET_PRIV (contact);
497 empathy_contact_set_avatar (EmpathyContact *contact,
498 EmpathyAvatar *avatar)
500 EmpathyContactPriv *priv;
502 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
504 priv = GET_PRIV (contact);
506 if (priv->avatar == avatar)
511 empathy_avatar_unref (priv->avatar);
516 priv->avatar = empathy_avatar_ref (avatar);
518 g_object_notify (G_OBJECT (contact), "avatar");
522 empathy_contact_get_account (EmpathyContact *contact)
524 EmpathyContactPriv *priv;
526 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
528 priv = GET_PRIV (contact);
530 if (priv->account != NULL)
531 return priv->account;
533 if (priv->tp_contact != NULL)
535 EmpathyAccountManager *manager;
536 TpConnection *connection;
538 /* FIXME: This assume the account manager already exists */
539 manager = empathy_account_manager_dup_singleton ();
540 connection = tp_contact_get_connection (priv->tp_contact);
541 priv->account = empathy_account_manager_get_account (manager, connection);
542 g_object_unref (manager);
544 return g_object_ref (priv->account);
551 empathy_contact_get_connection (EmpathyContact *contact)
553 EmpathyContactPriv *priv;
555 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
557 priv = GET_PRIV (contact);
559 if (priv->tp_contact != NULL)
560 return tp_contact_get_connection (priv->tp_contact);
566 presence_type_to_mc_presence (TpConnectionPresenceType type)
570 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
571 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
572 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
573 return MC_PRESENCE_UNSET;
574 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
575 return MC_PRESENCE_OFFLINE;
576 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
577 return MC_PRESENCE_AVAILABLE;
578 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
579 return MC_PRESENCE_AWAY;
580 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
581 return MC_PRESENCE_EXTENDED_AWAY;
582 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
583 return MC_PRESENCE_HIDDEN;
584 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
585 return MC_PRESENCE_DO_NOT_DISTURB;
588 return MC_PRESENCE_UNSET;
592 empathy_contact_get_presence (EmpathyContact *contact)
594 EmpathyContactPriv *priv;
596 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), MC_PRESENCE_UNSET);
598 priv = GET_PRIV (contact);
600 if (priv->tp_contact != NULL)
601 return presence_type_to_mc_presence (tp_contact_get_presence_type (
604 return priv->presence;
608 empathy_contact_set_presence (EmpathyContact *contact,
611 EmpathyContactPriv *priv;
612 McPresence old_presence;
614 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
616 priv = GET_PRIV (contact);
618 if (presence == priv->presence)
621 old_presence = priv->presence;
622 priv->presence = presence;
624 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
626 g_object_notify (G_OBJECT (contact), "presence");
630 empathy_contact_get_presence_message (EmpathyContact *contact)
632 EmpathyContactPriv *priv;
634 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
636 priv = GET_PRIV (contact);
638 if (priv->tp_contact != NULL)
639 return tp_contact_get_presence_message (priv->tp_contact);
641 return priv->presence_message;
645 empathy_contact_set_presence_message (EmpathyContact *contact,
646 const gchar *message)
648 EmpathyContactPriv *priv = GET_PRIV (contact);
650 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
652 if (!tp_strdiff (message, priv->presence_message))
655 g_free (priv->presence_message);
656 priv->presence_message = g_strdup (message);
658 g_object_notify (G_OBJECT (contact), "presence-message");
662 empathy_contact_get_handle (EmpathyContact *contact)
664 EmpathyContactPriv *priv;
666 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
668 priv = GET_PRIV (contact);
670 if (priv->tp_contact != NULL)
671 return tp_contact_get_handle (priv->tp_contact);
677 empathy_contact_set_handle (EmpathyContact *contact,
680 EmpathyContactPriv *priv;
682 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
684 priv = GET_PRIV (contact);
686 g_object_ref (contact);
687 if (handle != priv->handle)
689 priv->handle = handle;
690 g_object_notify (G_OBJECT (contact), "handle");
692 g_object_unref (contact);
696 empathy_contact_get_capabilities (EmpathyContact *contact)
698 EmpathyContactPriv *priv;
700 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
702 priv = GET_PRIV (contact);
704 return priv->capabilities;
708 empathy_contact_set_capabilities (EmpathyContact *contact,
709 EmpathyCapabilities capabilities)
711 EmpathyContactPriv *priv;
713 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
715 priv = GET_PRIV (contact);
717 if (priv->capabilities == capabilities)
720 priv->capabilities = capabilities;
722 g_object_notify (G_OBJECT (contact), "capabilities");
726 empathy_contact_is_user (EmpathyContact *contact)
728 EmpathyContactPriv *priv;
730 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
732 priv = GET_PRIV (contact);
734 return priv->is_user;
738 empathy_contact_set_is_user (EmpathyContact *contact,
741 EmpathyContactPriv *priv;
743 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
745 priv = GET_PRIV (contact);
747 if (priv->is_user == is_user)
750 priv->is_user = is_user;
752 g_object_notify (G_OBJECT (contact), "is-user");
756 empathy_contact_is_online (EmpathyContact *contact)
758 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
760 return (empathy_contact_get_presence (contact) > MC_PRESENCE_OFFLINE);
764 empathy_contact_get_status (EmpathyContact *contact)
766 const gchar *message;
768 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
770 message = empathy_contact_get_presence_message (contact);
771 if (!EMP_STR_EMPTY (message))
774 return empathy_presence_get_default_message (
775 empathy_contact_get_presence (contact));
779 empathy_contact_can_voip (EmpathyContact *contact)
781 EmpathyContactPriv *priv;
783 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
785 priv = GET_PRIV (contact);
787 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
788 EMPATHY_CAPABILITIES_VIDEO);
792 empathy_contact_can_send_files (EmpathyContact *contact)
794 EmpathyContactPriv *priv;
796 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
798 priv = GET_PRIV (contact);
800 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
804 contact_get_avatar_filename (EmpathyContact *contact,
807 EmpathyContactPriv *priv = GET_PRIV (contact);
811 gchar *token_escaped;
812 gchar *contact_escaped;
814 if (EMP_STR_EMPTY (priv->id))
817 contact_escaped = tp_escape_as_identifier (priv->id);
818 token_escaped = tp_escape_as_identifier (token);
819 account = empathy_contact_get_account (contact);
821 /* FIXME: Do not use the account, but proto/cm instead */
822 avatar_path = g_build_filename (g_get_user_cache_dir (),
825 mc_account_get_unique_name (account),
828 g_mkdir_with_parents (avatar_path, 0700);
830 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
832 g_free (contact_escaped);
833 g_free (token_escaped);
834 g_free (avatar_path);
840 empathy_contact_load_avatar_data (EmpathyContact *contact,
846 EmpathyAvatar *avatar;
848 GError *error = NULL;
850 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
851 g_return_if_fail (data != NULL);
852 g_return_if_fail (len > 0);
853 g_return_if_fail (format != NULL);
854 g_return_if_fail (!EMP_STR_EMPTY (token));
856 /* Load and set the avatar */
857 avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
859 empathy_contact_set_avatar (contact, avatar);
860 empathy_avatar_unref (avatar);
862 /* Save to cache if not yet in it */
863 filename = contact_get_avatar_filename (contact, token);
864 if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
866 if (!empathy_avatar_save_to_file (avatar, filename, &error))
868 DEBUG ("Failed to save avatar in cache: %s",
869 error ? error->message : "No error given");
870 g_clear_error (&error);
873 DEBUG ("Avatar saved to %s", filename);
879 empathy_contact_load_avatar_cache (EmpathyContact *contact,
882 EmpathyAvatar *avatar = NULL;
886 GError *error = NULL;
888 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
889 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
891 /* Load the avatar from file if it exists */
892 filename = contact_get_avatar_filename (contact, token);
893 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
895 if (!g_file_get_contents (filename, &data, &len, &error))
897 DEBUG ("Failed to load avatar from cache: %s",
898 error ? error->message : "No error given");
899 g_clear_error (&error);
905 DEBUG ("Avatar loaded from %s", filename);
906 avatar = empathy_avatar_new (data, len, NULL, g_strdup (token));
907 empathy_contact_set_avatar (contact, avatar);
908 empathy_avatar_unref (avatar);
917 empathy_avatar_get_type (void)
919 static GType type_id = 0;
923 type_id = g_boxed_type_register_static ("EmpathyAvatar",
924 (GBoxedCopyFunc) empathy_avatar_ref,
925 (GBoxedFreeFunc) empathy_avatar_unref);
932 empathy_avatar_new (guchar *data,
937 EmpathyAvatar *avatar;
939 avatar = g_slice_new0 (EmpathyAvatar);
942 avatar->format = format;
943 avatar->token = token;
944 avatar->refcount = 1;
950 empathy_avatar_unref (EmpathyAvatar *avatar)
952 g_return_if_fail (avatar != NULL);
955 if (avatar->refcount == 0)
957 g_free (avatar->data);
958 g_free (avatar->format);
959 g_free (avatar->token);
960 g_slice_free (EmpathyAvatar, avatar);
965 empathy_avatar_ref (EmpathyAvatar *avatar)
967 g_return_val_if_fail (avatar != NULL, NULL);
975 * empathy_avatar_save_to_file:
976 * @avatar: the avatar
977 * @filename: name of a file to write avatar to
978 * @error: return location for a GError, or NULL
980 * Save the avatar to a file named filename
982 * Returns: %TRUE on success, %FALSE if an error occurred
985 empathy_avatar_save_to_file (EmpathyAvatar *self,
986 const gchar *filename,
989 return g_file_set_contents (filename, self->data, self->len, error);