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>
26 #include <glib/gi18n-lib.h>
28 #include <telepathy-glib/util.h>
29 #include <libmissioncontrol/mc-enum-types.h>
31 #include "empathy-contact.h"
32 #include "empathy-account-manager.h"
33 #include "empathy-utils.h"
34 #include "empathy-enum-types.h"
35 #include "empathy-marshal.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
38 #include "empathy-debug.h"
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
42 TpContact *tp_contact;
46 EmpathyAvatar *avatar;
48 gchar *presence_message;
50 EmpathyCapabilities capabilities;
55 static void contact_finalize (GObject *object);
56 static void contact_get_property (GObject *object, guint param_id,
57 GValue *value, GParamSpec *pspec);
58 static void contact_set_property (GObject *object, guint param_id,
59 const GValue *value, GParamSpec *pspec);
61 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
72 PROP_PRESENCE_MESSAGE,
83 static guint signals[LAST_SIGNAL];
86 tp_contact_notify_cb (TpContact *tp_contact,
90 EmpathyContactPriv *priv = GET_PRIV (contact);
92 /* Forward property notifications */
93 if (!tp_strdiff (param->name, "alias"))
94 g_object_notify (contact, "name");
95 else if (!tp_strdiff (param->name, "presence-type")) {
98 presence = empathy_contact_get_presence (EMPATHY_CONTACT (contact));
99 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, priv->presence);
100 priv->presence = presence;
101 g_object_notify (contact, "presence");
103 else if (!tp_strdiff (param->name, "presence-message"))
104 g_object_notify (contact, "presence-message");
105 else if (!tp_strdiff (param->name, "identifier"))
106 g_object_notify (contact, "id");
107 else if (!tp_strdiff (param->name, "handle"))
108 g_object_notify (contact, "handle");
112 contact_dispose (GObject *object)
114 EmpathyContactPriv *priv = GET_PRIV (object);
116 if (priv->tp_contact)
118 g_signal_handlers_disconnect_by_func (priv->tp_contact,
119 tp_contact_notify_cb, object);
120 g_object_unref (priv->tp_contact);
122 priv->tp_contact = NULL;
125 g_object_unref (priv->account);
126 priv->account = NULL;
128 G_OBJECT_CLASS (empathy_contact_parent_class)->dispose (object);
132 empathy_contact_class_init (EmpathyContactClass *class)
134 GObjectClass *object_class;
136 object_class = G_OBJECT_CLASS (class);
138 object_class->finalize = contact_finalize;
139 object_class->dispose = contact_dispose;
140 object_class->get_property = contact_get_property;
141 object_class->set_property = contact_set_property;
143 g_object_class_install_property (object_class,
145 g_param_spec_object ("tp-contact",
147 "The TpContact associated with the contact",
149 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
151 g_object_class_install_property (object_class,
153 g_param_spec_object ("account",
155 "The account associated with the contact",
157 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
159 g_object_class_install_property (object_class,
161 g_param_spec_string ("id",
163 "String identifying contact",
165 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
167 g_object_class_install_property (object_class,
169 g_param_spec_string ("name",
171 "The name of the contact",
173 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175 g_object_class_install_property (object_class,
177 g_param_spec_boxed ("avatar",
181 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183 g_object_class_install_property (object_class,
185 g_param_spec_uint ("presence",
187 "Presence of contact",
191 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
193 g_object_class_install_property (object_class,
194 PROP_PRESENCE_MESSAGE,
195 g_param_spec_string ("presence-message",
196 "Contact presence message",
197 "Presence message of contact",
199 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
201 g_object_class_install_property (object_class,
203 g_param_spec_uint ("handle",
205 "The handle of the contact",
209 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
211 g_object_class_install_property (object_class,
213 g_param_spec_flags ("capabilities",
214 "Contact Capabilities",
215 "Capabilities of the contact",
216 EMPATHY_TYPE_CAPABILITIES,
217 EMPATHY_CAPABILITIES_UNKNOWN,
218 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
220 g_object_class_install_property (object_class,
222 g_param_spec_boolean ("is-user",
224 "Is contact the user",
226 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
228 signals[PRESENCE_CHANGED] =
229 g_signal_new ("presence-changed",
230 G_TYPE_FROM_CLASS (class),
234 _empathy_marshal_VOID__ENUM_ENUM,
239 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
243 empathy_contact_init (EmpathyContact *contact)
245 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
246 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
248 contact->priv = priv;
252 contact_finalize (GObject *object)
254 EmpathyContactPriv *priv;
256 priv = GET_PRIV (object);
258 DEBUG ("finalize: %p", object);
262 g_free (priv->presence_message);
265 empathy_avatar_unref (priv->avatar);
267 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
271 set_tp_contact (EmpathyContact *contact,
272 TpContact *tp_contact)
274 EmpathyContactPriv *priv = GET_PRIV (contact);
276 if (tp_contact == NULL)
279 g_assert (priv->tp_contact == NULL);
280 priv->tp_contact = g_object_ref (tp_contact);
281 priv->presence = empathy_contact_get_presence (contact);
283 g_signal_connect (priv->tp_contact, "notify",
284 G_CALLBACK (tp_contact_notify_cb), contact);
288 contact_get_property (GObject *object,
293 EmpathyContact *contact = EMPATHY_CONTACT (object);
297 case PROP_TP_CONTACT:
298 g_value_set_object (value, empathy_contact_get_tp_contact (contact));
301 g_value_set_object (value, empathy_contact_get_account (contact));
304 g_value_set_string (value, empathy_contact_get_id (contact));
307 g_value_set_string (value, empathy_contact_get_name (contact));
310 g_value_set_boxed (value, empathy_contact_get_avatar (contact));
313 g_value_set_uint (value, empathy_contact_get_presence (contact));
315 case PROP_PRESENCE_MESSAGE:
316 g_value_set_string (value, empathy_contact_get_presence_message (contact));
319 g_value_set_uint (value, empathy_contact_get_handle (contact));
321 case PROP_CAPABILITIES:
322 g_value_set_flags (value, empathy_contact_get_capabilities (contact));
325 g_value_set_boolean (value, empathy_contact_is_user (contact));
328 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
334 contact_set_property (GObject *object,
339 EmpathyContact *contact = EMPATHY_CONTACT (object);
340 EmpathyContactPriv *priv = GET_PRIV (object);
344 case PROP_TP_CONTACT:
345 set_tp_contact (contact, g_value_get_object (value));
348 g_assert (priv->account == NULL);
349 priv->account = g_value_dup_object (value);
352 empathy_contact_set_id (contact, g_value_get_string (value));
355 empathy_contact_set_name (contact, g_value_get_string (value));
358 empathy_contact_set_avatar (contact, g_value_get_boxed (value));
361 empathy_contact_set_presence (contact, g_value_get_uint (value));
363 case PROP_PRESENCE_MESSAGE:
364 empathy_contact_set_presence_message (contact, g_value_get_string (value));
367 empathy_contact_set_handle (contact, g_value_get_uint (value));
369 case PROP_CAPABILITIES:
370 empathy_contact_set_capabilities (contact, g_value_get_flags (value));
373 empathy_contact_set_is_user (contact, g_value_get_boolean (value));
376 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
382 empathy_contact_new (TpContact *tp_contact)
384 g_return_val_if_fail (TP_IS_CONTACT (tp_contact), NULL);
386 return g_object_new (EMPATHY_TYPE_CONTACT,
387 "tp-contact", tp_contact,
392 empathy_contact_new_static (McAccount *account,
397 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
398 g_return_val_if_fail (id != NULL, NULL);
400 return g_object_new (EMPATHY_TYPE_CONTACT,
409 empathy_contact_get_tp_contact (EmpathyContact *contact)
411 EmpathyContactPriv *priv;
413 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
415 priv = GET_PRIV (contact);
417 return priv->tp_contact;
421 empathy_contact_get_id (EmpathyContact *contact)
423 EmpathyContactPriv *priv;
425 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
427 priv = GET_PRIV (contact);
429 if (priv->tp_contact != NULL)
430 return tp_contact_get_identifier (priv->tp_contact);
436 empathy_contact_set_id (EmpathyContact *contact,
439 EmpathyContactPriv *priv;
441 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
442 g_return_if_fail (id != NULL);
444 priv = GET_PRIV (contact);
446 /* We temporally ref the contact because it could be destroyed
447 * during the signal emition */
448 g_object_ref (contact);
449 if (tp_strdiff (id, priv->id))
452 priv->id = g_strdup (id);
454 g_object_notify (G_OBJECT (contact), "id");
455 if (EMP_STR_EMPTY (priv->name))
456 g_object_notify (G_OBJECT (contact), "name");
459 g_object_unref (contact);
463 empathy_contact_get_name (EmpathyContact *contact)
465 EmpathyContactPriv *priv;
467 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
469 priv = GET_PRIV (contact);
471 if (priv->tp_contact != NULL)
472 return tp_contact_get_alias (priv->tp_contact);
474 if (EMP_STR_EMPTY (priv->name))
475 return empathy_contact_get_id (contact);
481 empathy_contact_set_name (EmpathyContact *contact,
484 EmpathyContactPriv *priv;
486 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
488 priv = GET_PRIV (contact);
490 g_object_ref (contact);
491 if (tp_strdiff (name, priv->name))
494 priv->name = g_strdup (name);
495 g_object_notify (G_OBJECT (contact), "name");
497 g_object_unref (contact);
501 empathy_contact_get_avatar (EmpathyContact *contact)
503 EmpathyContactPriv *priv;
505 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
507 priv = GET_PRIV (contact);
513 empathy_contact_set_avatar (EmpathyContact *contact,
514 EmpathyAvatar *avatar)
516 EmpathyContactPriv *priv;
518 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
520 priv = GET_PRIV (contact);
522 if (priv->avatar == avatar)
527 empathy_avatar_unref (priv->avatar);
532 priv->avatar = empathy_avatar_ref (avatar);
534 g_object_notify (G_OBJECT (contact), "avatar");
538 empathy_contact_get_account (EmpathyContact *contact)
540 EmpathyContactPriv *priv;
542 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
544 priv = GET_PRIV (contact);
546 if (priv->account == NULL && priv->tp_contact != NULL)
548 EmpathyAccountManager *manager;
549 TpConnection *connection;
551 /* FIXME: This assume the account manager already exists */
552 manager = empathy_account_manager_dup_singleton ();
553 connection = tp_contact_get_connection (priv->tp_contact);
554 priv->account = empathy_account_manager_get_account (manager, connection);
555 g_object_ref (priv->account);
556 g_object_unref (manager);
559 return priv->account;
563 empathy_contact_get_connection (EmpathyContact *contact)
565 EmpathyContactPriv *priv;
567 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
569 priv = GET_PRIV (contact);
571 if (priv->tp_contact != NULL)
572 return tp_contact_get_connection (priv->tp_contact);
578 presence_type_to_mc_presence (TpConnectionPresenceType type)
582 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
583 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
584 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
585 return MC_PRESENCE_UNSET;
586 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
587 return MC_PRESENCE_OFFLINE;
588 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
589 return MC_PRESENCE_AVAILABLE;
590 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
591 return MC_PRESENCE_AWAY;
592 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
593 return MC_PRESENCE_EXTENDED_AWAY;
594 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
595 return MC_PRESENCE_HIDDEN;
596 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
597 return MC_PRESENCE_DO_NOT_DISTURB;
600 return MC_PRESENCE_UNSET;
604 empathy_contact_get_presence (EmpathyContact *contact)
606 EmpathyContactPriv *priv;
608 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), MC_PRESENCE_UNSET);
610 priv = GET_PRIV (contact);
612 if (priv->tp_contact != NULL)
613 return presence_type_to_mc_presence (tp_contact_get_presence_type (
616 return priv->presence;
620 empathy_contact_set_presence (EmpathyContact *contact,
623 EmpathyContactPriv *priv;
624 McPresence old_presence;
626 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
628 priv = GET_PRIV (contact);
630 if (presence == priv->presence)
633 old_presence = priv->presence;
634 priv->presence = presence;
636 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
638 g_object_notify (G_OBJECT (contact), "presence");
642 empathy_contact_get_presence_message (EmpathyContact *contact)
644 EmpathyContactPriv *priv;
646 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
648 priv = GET_PRIV (contact);
650 if (priv->tp_contact != NULL)
651 return tp_contact_get_presence_message (priv->tp_contact);
653 return priv->presence_message;
657 empathy_contact_set_presence_message (EmpathyContact *contact,
658 const gchar *message)
660 EmpathyContactPriv *priv = GET_PRIV (contact);
662 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
664 if (!tp_strdiff (message, priv->presence_message))
667 g_free (priv->presence_message);
668 priv->presence_message = g_strdup (message);
670 g_object_notify (G_OBJECT (contact), "presence-message");
674 empathy_contact_get_handle (EmpathyContact *contact)
676 EmpathyContactPriv *priv;
678 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
680 priv = GET_PRIV (contact);
682 if (priv->tp_contact != NULL)
683 return tp_contact_get_handle (priv->tp_contact);
689 empathy_contact_set_handle (EmpathyContact *contact,
692 EmpathyContactPriv *priv;
694 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
696 priv = GET_PRIV (contact);
698 g_object_ref (contact);
699 if (handle != priv->handle)
701 priv->handle = handle;
702 g_object_notify (G_OBJECT (contact), "handle");
704 g_object_unref (contact);
708 empathy_contact_get_capabilities (EmpathyContact *contact)
710 EmpathyContactPriv *priv;
712 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
714 priv = GET_PRIV (contact);
716 return priv->capabilities;
720 empathy_contact_set_capabilities (EmpathyContact *contact,
721 EmpathyCapabilities capabilities)
723 EmpathyContactPriv *priv;
725 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
727 priv = GET_PRIV (contact);
729 if (priv->capabilities == capabilities)
732 priv->capabilities = capabilities;
734 g_object_notify (G_OBJECT (contact), "capabilities");
738 empathy_contact_is_user (EmpathyContact *contact)
740 EmpathyContactPriv *priv;
742 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
744 priv = GET_PRIV (contact);
746 return priv->is_user;
750 empathy_contact_set_is_user (EmpathyContact *contact,
753 EmpathyContactPriv *priv;
755 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
757 priv = GET_PRIV (contact);
759 if (priv->is_user == is_user)
762 priv->is_user = is_user;
764 g_object_notify (G_OBJECT (contact), "is-user");
768 empathy_contact_is_online (EmpathyContact *contact)
770 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
772 return (empathy_contact_get_presence (contact) > MC_PRESENCE_OFFLINE);
776 empathy_contact_get_status (EmpathyContact *contact)
778 const gchar *message;
780 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
782 message = empathy_contact_get_presence_message (contact);
783 if (!EMP_STR_EMPTY (message))
786 return empathy_presence_get_default_message (
787 empathy_contact_get_presence (contact));
791 empathy_contact_can_voip (EmpathyContact *contact)
793 EmpathyContactPriv *priv;
795 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
797 priv = GET_PRIV (contact);
799 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
800 EMPATHY_CAPABILITIES_VIDEO);
804 empathy_contact_can_send_files (EmpathyContact *contact)
806 EmpathyContactPriv *priv;
808 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
810 priv = GET_PRIV (contact);
812 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
816 contact_get_avatar_filename (EmpathyContact *contact,
819 EmpathyContactPriv *priv = GET_PRIV (contact);
823 gchar *token_escaped;
824 gchar *contact_escaped;
826 if (EMP_STR_EMPTY (priv->id))
829 contact_escaped = tp_escape_as_identifier (priv->id);
830 token_escaped = tp_escape_as_identifier (token);
831 account = empathy_contact_get_account (contact);
833 /* FIXME: Do not use the account, but proto/cm instead */
834 avatar_path = g_build_filename (g_get_user_cache_dir (),
837 mc_account_get_unique_name (account),
840 g_mkdir_with_parents (avatar_path, 0700);
842 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
844 g_free (contact_escaped);
845 g_free (token_escaped);
846 g_free (avatar_path);
852 empathy_contact_load_avatar_data (EmpathyContact *contact,
858 EmpathyAvatar *avatar;
860 GError *error = NULL;
862 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
863 g_return_if_fail (data != NULL);
864 g_return_if_fail (len > 0);
865 g_return_if_fail (format != NULL);
866 g_return_if_fail (!EMP_STR_EMPTY (token));
868 /* Load and set the avatar */
869 avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
871 empathy_contact_set_avatar (contact, avatar);
872 empathy_avatar_unref (avatar);
874 /* Save to cache if not yet in it */
875 filename = contact_get_avatar_filename (contact, token);
876 if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
878 if (!empathy_avatar_save_to_file (avatar, filename, &error))
880 DEBUG ("Failed to save avatar in cache: %s",
881 error ? error->message : "No error given");
882 g_clear_error (&error);
885 DEBUG ("Avatar saved to %s", filename);
891 empathy_contact_load_avatar_cache (EmpathyContact *contact,
894 EmpathyAvatar *avatar = NULL;
898 GError *error = NULL;
900 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
901 g_return_val_if_fail (!EMP_STR_EMPTY (token), FALSE);
903 /* Load the avatar from file if it exists */
904 filename = contact_get_avatar_filename (contact, token);
905 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
907 if (!g_file_get_contents (filename, &data, &len, &error))
909 DEBUG ("Failed to load avatar from cache: %s",
910 error ? error->message : "No error given");
911 g_clear_error (&error);
917 DEBUG ("Avatar loaded from %s", filename);
918 avatar = empathy_avatar_new (data, len, NULL, g_strdup (token));
919 empathy_contact_set_avatar (contact, avatar);
920 empathy_avatar_unref (avatar);
929 empathy_avatar_get_type (void)
931 static GType type_id = 0;
935 type_id = g_boxed_type_register_static ("EmpathyAvatar",
936 (GBoxedCopyFunc) empathy_avatar_ref,
937 (GBoxedFreeFunc) empathy_avatar_unref);
944 empathy_avatar_new (guchar *data,
949 EmpathyAvatar *avatar;
951 avatar = g_slice_new0 (EmpathyAvatar);
954 avatar->format = format;
955 avatar->token = token;
956 avatar->refcount = 1;
962 empathy_avatar_unref (EmpathyAvatar *avatar)
964 g_return_if_fail (avatar != NULL);
967 if (avatar->refcount == 0)
969 g_free (avatar->data);
970 g_free (avatar->format);
971 g_free (avatar->token);
972 g_slice_free (EmpathyAvatar, avatar);
977 empathy_avatar_ref (EmpathyAvatar *avatar)
979 g_return_val_if_fail (avatar != NULL, NULL);
987 * empathy_avatar_save_to_file:
988 * @avatar: the avatar
989 * @filename: name of a file to write avatar to
990 * @error: return location for a GError, or NULL
992 * Save the avatar to a file named filename
994 * Returns: %TRUE on success, %FALSE if an error occurred
997 empathy_avatar_save_to_file (EmpathyAvatar *self,
998 const gchar *filename,
1001 return g_file_set_contents (filename, self->data, self->len, error);