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>
33 #include <telepathy-glib/gtypes.h>
35 #include <extensions/extensions.h>
37 #include "empathy-contact.h"
38 #include "empathy-utils.h"
39 #include "empathy-enum-types.h"
41 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
42 #include "empathy-debug.h"
44 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
48 EmpathyAvatar *avatar;
51 gchar *presence_message;
53 EmpathyCapabilities capabilities;
56 EmpathyContactReady ready;
59 static void contact_finalize (GObject *object);
60 static void contact_get_property (GObject *object, guint param_id,
61 GValue *value, GParamSpec *pspec);
62 static void contact_set_property (GObject *object, guint param_id,
63 const GValue *value, GParamSpec *pspec);
65 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
75 PROP_PRESENCE_MESSAGE,
83 empathy_contact_class_init (EmpathyContactClass *class)
85 GObjectClass *object_class;
87 object_class = G_OBJECT_CLASS (class);
89 object_class->finalize = contact_finalize;
90 object_class->get_property = contact_get_property;
91 object_class->set_property = contact_set_property;
93 g_object_class_install_property (object_class,
95 g_param_spec_string ("id",
97 "String identifying contact",
101 g_object_class_install_property (object_class,
103 g_param_spec_string ("name",
105 "The name of the contact",
109 g_object_class_install_property (object_class,
111 g_param_spec_boxed ("avatar",
117 g_object_class_install_property (object_class,
119 g_param_spec_object ("account",
121 "The account associated with the contact",
125 g_object_class_install_property (object_class,
127 g_param_spec_uint ("presence",
129 "Presence of contact",
135 g_object_class_install_property (object_class,
136 PROP_PRESENCE_MESSAGE,
137 g_param_spec_string ("presence-message",
138 "Contact presence message",
139 "Presence message of contact",
143 g_object_class_install_property (object_class,
145 g_param_spec_uint ("handle",
147 "The handle of the contact",
153 g_object_class_install_property (object_class,
155 g_param_spec_flags ("capabilities",
156 "Contact Capabilities",
157 "Capabilities of the contact",
158 EMPATHY_TYPE_CAPABILITIES,
159 EMPATHY_CAPABILITIES_UNKNOWN,
160 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
162 g_object_class_install_property (object_class,
164 g_param_spec_boolean ("is-user",
166 "Is contact the user",
170 g_object_class_install_property (object_class,
172 g_param_spec_flags ("ready",
173 "Contact ready flags",
174 "Flags for ready properties",
175 EMPATHY_TYPE_CONTACT_READY,
176 EMPATHY_CONTACT_READY_NONE,
179 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
183 empathy_contact_init (EmpathyContact *contact)
185 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
186 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
188 contact->priv = priv;
192 contact_finalize (GObject *object)
194 EmpathyContactPriv *priv;
196 priv = GET_PRIV (object);
198 DEBUG ("finalize: %p", object);
202 g_free (priv->presence_message);
205 empathy_avatar_unref (priv->avatar);
208 g_object_unref (priv->account);
210 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
214 contact_get_property (GObject *object,
219 EmpathyContactPriv *priv;
221 priv = GET_PRIV (object);
226 g_value_set_string (value, priv->id);
229 g_value_set_string (value,
230 empathy_contact_get_name (EMPATHY_CONTACT (object)));
233 g_value_set_boxed (value, priv->avatar);
236 g_value_set_object (value, priv->account);
239 g_value_set_uint (value, priv->presence);
241 case PROP_PRESENCE_MESSAGE:
242 g_value_set_string (value, priv->presence_message);
245 g_value_set_uint (value, priv->handle);
247 case PROP_CAPABILITIES:
248 g_value_set_flags (value, priv->capabilities);
251 g_value_set_boolean (value, priv->is_user);
254 g_value_set_flags (value, priv->ready);
257 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
263 contact_set_property (GObject *object,
268 EmpathyContactPriv *priv;
270 priv = GET_PRIV (object);
275 empathy_contact_set_id (EMPATHY_CONTACT (object),
276 g_value_get_string (value));
279 empathy_contact_set_name (EMPATHY_CONTACT (object),
280 g_value_get_string (value));
283 empathy_contact_set_avatar (EMPATHY_CONTACT (object),
284 g_value_get_boxed (value));
287 empathy_contact_set_account (EMPATHY_CONTACT (object),
288 MC_ACCOUNT (g_value_get_object (value)));
291 empathy_contact_set_presence (EMPATHY_CONTACT (object),
292 g_value_get_uint (value));
294 case PROP_PRESENCE_MESSAGE:
295 empathy_contact_set_presence_message (EMPATHY_CONTACT (object),
296 g_value_get_string (value));
299 empathy_contact_set_handle (EMPATHY_CONTACT (object),
300 g_value_get_uint (value));
302 case PROP_CAPABILITIES:
303 empathy_contact_set_capabilities (EMPATHY_CONTACT (object),
304 g_value_get_flags (value));
307 empathy_contact_set_is_user (EMPATHY_CONTACT (object),
308 g_value_get_boolean (value));
311 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
317 contact_set_ready_flag (EmpathyContact *contact,
318 EmpathyContactReady flag)
320 EmpathyContactPriv *priv = GET_PRIV (contact);
322 if (!(priv->ready & flag))
325 g_object_notify (G_OBJECT (contact), "ready");
330 empathy_contact_new (McAccount *account)
332 return g_object_new (EMPATHY_TYPE_CONTACT,
338 empathy_contact_new_full (McAccount *account,
342 return g_object_new (EMPATHY_TYPE_CONTACT,
350 empathy_contact_get_id (EmpathyContact *contact)
352 EmpathyContactPriv *priv;
354 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
356 priv = GET_PRIV (contact);
362 empathy_contact_set_id (EmpathyContact *contact,
365 EmpathyContactPriv *priv;
367 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
368 g_return_if_fail (id != NULL);
370 priv = GET_PRIV (contact);
372 /* We temporally ref the contact because it could be destroyed
373 * during the signal emition */
374 g_object_ref (contact);
375 if (tp_strdiff (id, priv->id))
378 priv->id = g_strdup (id);
380 g_object_notify (G_OBJECT (contact), "id");
381 if (G_STR_EMPTY (priv->name))
382 g_object_notify (G_OBJECT (contact), "name");
384 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_ID);
386 g_object_unref (contact);
390 empathy_contact_get_name (EmpathyContact *contact)
392 EmpathyContactPriv *priv;
394 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
396 priv = GET_PRIV (contact);
398 if (G_STR_EMPTY (priv->name))
399 return empathy_contact_get_id (contact);
405 empathy_contact_set_name (EmpathyContact *contact,
408 EmpathyContactPriv *priv;
410 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
412 priv = GET_PRIV (contact);
414 g_object_ref (contact);
415 if (tp_strdiff (name, priv->name))
418 priv->name = g_strdup (name);
419 g_object_notify (G_OBJECT (contact), "name");
421 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_NAME);
422 g_object_unref (contact);
426 empathy_contact_get_avatar (EmpathyContact *contact)
428 EmpathyContactPriv *priv;
430 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
432 priv = GET_PRIV (contact);
438 empathy_contact_set_avatar (EmpathyContact *contact,
439 EmpathyAvatar *avatar)
441 EmpathyContactPriv *priv;
443 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
445 priv = GET_PRIV (contact);
447 if (priv->avatar == avatar)
452 empathy_avatar_unref (priv->avatar);
457 priv->avatar = empathy_avatar_ref (avatar);
459 g_object_notify (G_OBJECT (contact), "avatar");
463 empathy_contact_get_account (EmpathyContact *contact)
465 EmpathyContactPriv *priv;
467 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
469 priv = GET_PRIV (contact);
471 return priv->account;
475 get_requestable_channel_classes_cb (TpProxy *connection,
479 GObject *weak_object)
486 DEBUG ("Error: %s", error->message);
490 classes = g_value_get_boxed (value);
492 for (i = 0; i < classes->len; i++)
495 GValue *chan_type, *handle_type;
496 GHashTable *fixed_prop;
497 EmpathyCapabilities caps;
498 EmpathyContact *contact;
500 g_value_init (&class, TP_STRUCT_TYPE_REQUESTABLE_CHANNEL_CLASS);
501 g_value_set_static_boxed (&class, g_ptr_array_index (classes, i));
503 dbus_g_type_struct_get (&class,
507 chan_type = g_hash_table_lookup (fixed_prop,
508 TP_IFACE_CHANNEL ".ChannelType");
509 if (chan_type == NULL || tp_strdiff (g_value_get_string (chan_type),
510 EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
513 handle_type = g_hash_table_lookup (fixed_prop,
514 TP_IFACE_CHANNEL ".TargetHandleType");
515 if (handle_type == NULL || g_value_get_uint (handle_type)
516 != TP_HANDLE_TYPE_CONTACT)
519 /* We can request file transfer channel to contacts. Set the FT caps. */
520 contact = EMPATHY_CONTACT (user_data);
521 caps = empathy_contact_get_capabilities (contact);
522 caps |= EMPATHY_CAPABILITIES_FT;
524 empathy_contact_set_capabilities (contact, caps);
529 empathy_contact_set_account (EmpathyContact *contact,
532 EmpathyContactPriv *priv;
534 TpConnection *connection;
536 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
537 g_return_if_fail (MC_IS_ACCOUNT (account));
539 priv = GET_PRIV (contact);
541 if (account == priv->account)
545 g_object_unref (priv->account);
546 priv->account = g_object_ref (account);
548 /* FIXME: We should use the futur ContactCapabilities interface */
549 /* Can we request FT channel on this connection? */
550 mc = empathy_mission_control_new ();
551 connection = mission_control_get_tpconnection (mc, account, NULL);
553 tp_cli_dbus_properties_call_get (connection, -1,
554 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "RequestableChannelClasses",
555 get_requestable_channel_classes_cb, contact, NULL, G_OBJECT (contact));
557 g_object_notify (G_OBJECT (contact), "account");
560 g_object_unref (connection);
564 empathy_contact_get_presence (EmpathyContact *contact)
566 EmpathyContactPriv *priv;
568 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), MC_PRESENCE_UNSET);
570 priv = GET_PRIV (contact);
572 return priv->presence;
576 empathy_contact_set_presence (EmpathyContact *contact,
579 EmpathyContactPriv *priv;
581 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
583 priv = GET_PRIV (contact);
585 if (presence == priv->presence)
588 priv->presence = presence;
590 g_object_notify (G_OBJECT (contact), "presence");
594 empathy_contact_get_presence_message (EmpathyContact *contact)
596 EmpathyContactPriv *priv;
598 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
600 priv = GET_PRIV (contact);
602 return priv->presence_message;
606 empathy_contact_set_presence_message (EmpathyContact *contact,
607 const gchar *message)
609 EmpathyContactPriv *priv = GET_PRIV (contact);
611 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
613 if (!tp_strdiff (message, priv->presence_message))
616 g_free (priv->presence_message);
617 priv->presence_message = g_strdup (message);
619 g_object_notify (G_OBJECT (contact), "presence-message");
623 empathy_contact_get_handle (EmpathyContact *contact)
625 EmpathyContactPriv *priv;
627 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
629 priv = GET_PRIV (contact);
635 empathy_contact_set_handle (EmpathyContact *contact,
638 EmpathyContactPriv *priv;
640 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
642 priv = GET_PRIV (contact);
644 g_object_ref (contact);
645 if (handle != priv->handle)
647 priv->handle = handle;
648 g_object_notify (G_OBJECT (contact), "handle");
650 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_HANDLE);
651 g_object_unref (contact);
655 empathy_contact_get_capabilities (EmpathyContact *contact)
657 EmpathyContactPriv *priv;
659 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
661 priv = GET_PRIV (contact);
663 return priv->capabilities;
667 empathy_contact_set_capabilities (EmpathyContact *contact,
668 EmpathyCapabilities capabilities)
670 EmpathyContactPriv *priv;
672 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
674 priv = GET_PRIV (contact);
676 if (priv->capabilities == capabilities)
679 priv->capabilities = capabilities;
681 g_object_notify (G_OBJECT (contact), "capabilities");
685 empathy_contact_is_user (EmpathyContact *contact)
687 EmpathyContactPriv *priv;
689 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
691 priv = GET_PRIV (contact);
693 return priv->is_user;
697 empathy_contact_set_is_user (EmpathyContact *contact,
700 EmpathyContactPriv *priv;
702 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
704 priv = GET_PRIV (contact);
706 if (priv->is_user == is_user)
709 priv->is_user = is_user;
711 g_object_notify (G_OBJECT (contact), "is-user");
715 empathy_contact_is_online (EmpathyContact *contact)
717 EmpathyContactPriv *priv;
719 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
721 priv = GET_PRIV (contact);
723 return (priv->presence > MC_PRESENCE_OFFLINE);
727 empathy_contact_get_status (EmpathyContact *contact)
729 EmpathyContactPriv *priv;
731 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
733 priv = GET_PRIV (contact);
735 if (priv->presence_message)
736 return priv->presence_message;
738 return empathy_presence_get_default_message (priv->presence);
742 empathy_contact_can_voip (EmpathyContact *contact)
744 EmpathyContactPriv *priv;
746 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
748 priv = GET_PRIV (contact);
750 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
751 EMPATHY_CAPABILITIES_VIDEO);
755 empathy_contact_can_send_files (EmpathyContact *contact)
757 EmpathyContactPriv *priv;
759 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
761 priv = GET_PRIV (contact);
763 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
767 empathy_contact_get_ready (EmpathyContact *contact)
769 EmpathyContactPriv *priv;
771 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
773 priv = GET_PRIV (contact);
779 empathy_contact_equal (gconstpointer v1,
782 McAccount *account_a;
783 McAccount *account_b;
787 g_return_val_if_fail (EMPATHY_IS_CONTACT (v1), FALSE);
788 g_return_val_if_fail (EMPATHY_IS_CONTACT (v2), FALSE);
790 account_a = empathy_contact_get_account (EMPATHY_CONTACT (v1));
791 account_b = empathy_contact_get_account (EMPATHY_CONTACT (v2));
793 id_a = empathy_contact_get_id (EMPATHY_CONTACT (v1));
794 id_b = empathy_contact_get_id (EMPATHY_CONTACT (v2));
796 return empathy_account_equal (account_a, account_b) &&
797 !tp_strdiff (id_a, id_b);
801 empathy_contact_hash (gconstpointer key)
803 EmpathyContactPriv *priv;
805 g_return_val_if_fail (EMPATHY_IS_CONTACT (key), +1);
807 priv = GET_PRIV (EMPATHY_CONTACT (key));
811 priv->hash = empathy_account_hash (priv->account) ^
812 g_str_hash (priv->id);
819 contact_is_ready_func (GObject *contact,
822 EmpathyContactPriv *priv = GET_PRIV (contact);
823 EmpathyContactReady ready;
825 ready = GPOINTER_TO_UINT (user_data);
827 /* When the name is NULL, empathy_contact_get_name() fallback to the id.
828 * When the caller want to wait the name to be ready, it also want to wait
829 * the id to be ready in case of fallback. */
830 if ((ready & EMPATHY_CONTACT_READY_NAME) && G_STR_EMPTY (priv->name))
831 ready |= EMPATHY_CONTACT_READY_ID;
833 return (priv->ready & ready) == ready;
837 empathy_contact_run_until_ready (EmpathyContact *contact,
838 EmpathyContactReady ready,
841 empathy_run_until_ready_full (contact, "notify::ready",
842 contact_is_ready_func, GUINT_TO_POINTER (ready),
847 contact_get_avatar_filename (EmpathyContact *contact,
850 EmpathyContactPriv *priv = GET_PRIV (contact);
853 gchar *token_escaped;
854 gchar *contact_escaped;
856 if (G_STR_EMPTY (priv->id))
859 contact_escaped = tp_escape_as_identifier (priv->id);
860 token_escaped = tp_escape_as_identifier (token);
862 avatar_path = g_build_filename (g_get_user_cache_dir (),
865 mc_account_get_unique_name (priv->account),
868 g_mkdir_with_parents (avatar_path, 0700);
870 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
872 g_free (contact_escaped);
873 g_free (token_escaped);
874 g_free (avatar_path);
880 empathy_contact_load_avatar_data (EmpathyContact *contact,
886 EmpathyAvatar *avatar;
888 GError *error = NULL;
890 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
891 g_return_if_fail (data != NULL);
892 g_return_if_fail (len > 0);
893 g_return_if_fail (format != NULL);
894 g_return_if_fail (!G_STR_EMPTY (token));
896 /* Load and set the avatar */
897 avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
899 empathy_contact_set_avatar (contact, avatar);
900 empathy_avatar_unref (avatar);
902 /* Save to cache if not yet in it */
903 filename = contact_get_avatar_filename (contact, token);
904 if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
906 if (!empathy_avatar_save_to_file (avatar, filename, &error))
908 DEBUG ("Failed to save avatar in cache: %s",
909 error ? error->message : "No error given");
910 g_clear_error (&error);
913 DEBUG ("Avatar saved to %s", filename);
919 empathy_contact_load_avatar_cache (EmpathyContact *contact,
922 EmpathyAvatar *avatar = NULL;
926 GError *error = NULL;
928 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
929 g_return_val_if_fail (!G_STR_EMPTY (token), FALSE);
931 /* Load the avatar from file if it exists */
932 filename = contact_get_avatar_filename (contact, token);
933 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
935 if (!g_file_get_contents (filename, &data, &len, &error))
937 DEBUG ("Failed to load avatar from cache: %s",
938 error ? error->message : "No error given");
939 g_clear_error (&error);
945 DEBUG ("Avatar loaded from %s", filename);
946 avatar = empathy_avatar_new (data, len, NULL, g_strdup (token));
947 empathy_contact_set_avatar (contact, avatar);
948 empathy_avatar_unref (avatar);
957 empathy_avatar_get_type (void)
959 static GType type_id = 0;
963 type_id = g_boxed_type_register_static ("EmpathyAvatar",
964 (GBoxedCopyFunc) empathy_avatar_ref,
965 (GBoxedFreeFunc) empathy_avatar_unref);
972 empathy_avatar_new (guchar *data,
977 EmpathyAvatar *avatar;
979 avatar = g_slice_new0 (EmpathyAvatar);
982 avatar->format = format;
983 avatar->token = token;
984 avatar->refcount = 1;
990 empathy_avatar_unref (EmpathyAvatar *avatar)
992 g_return_if_fail (avatar != NULL);
995 if (avatar->refcount == 0)
997 g_free (avatar->data);
998 g_free (avatar->format);
999 g_free (avatar->token);
1000 g_slice_free (EmpathyAvatar, avatar);
1005 empathy_avatar_ref (EmpathyAvatar *avatar)
1007 g_return_val_if_fail (avatar != NULL, NULL);
1015 * empathy_avatar_save_to_file:
1016 * @avatar: the avatar
1017 * @filename: name of a file to write avatar to
1018 * @error: return location for a GError, or NULL
1020 * Save the avatar to a file named filename
1022 * Returns: %TRUE on success, %FALSE if an error occurred
1025 empathy_avatar_save_to_file (EmpathyAvatar *self,
1026 const gchar *filename,
1029 return g_file_set_contents (filename, self->data, self->len, error);