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");
516 if (handle_type == NULL || g_value_get_uint (handle_type)
517 != TP_HANDLE_TYPE_CONTACT)
520 /* We can request file transfer channel to contacts. Set the FT caps. */
521 contact = EMPATHY_CONTACT (user_data);
522 caps = empathy_contact_get_capabilities (contact);
523 caps |= EMPATHY_CAPABILITIES_FT;
525 empathy_contact_set_capabilities (contact, caps);
530 empathy_contact_set_account (EmpathyContact *contact,
533 EmpathyContactPriv *priv;
535 TpConnection *connection;
537 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
538 g_return_if_fail (MC_IS_ACCOUNT (account));
540 priv = GET_PRIV (contact);
542 if (account == priv->account)
546 g_object_unref (priv->account);
547 priv->account = g_object_ref (account);
549 /* FIXME: We should use the futur ContactCapabilities interface */
550 /* Can we request FT channel on this connection? */
551 mc = empathy_mission_control_new ();
552 connection = mission_control_get_tpconnection (mc, account, NULL);
554 tp_cli_dbus_properties_call_get (connection, -1,
555 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "RequestableChannelClasses",
556 get_requestable_channel_classes_cb, contact, NULL, G_OBJECT (contact));
558 g_object_notify (G_OBJECT (contact), "account");
561 g_object_unref (connection);
565 empathy_contact_get_presence (EmpathyContact *contact)
567 EmpathyContactPriv *priv;
569 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), MC_PRESENCE_UNSET);
571 priv = GET_PRIV (contact);
573 return priv->presence;
577 empathy_contact_set_presence (EmpathyContact *contact,
580 EmpathyContactPriv *priv;
582 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
584 priv = GET_PRIV (contact);
586 if (presence == priv->presence)
589 priv->presence = presence;
591 g_object_notify (G_OBJECT (contact), "presence");
595 empathy_contact_get_presence_message (EmpathyContact *contact)
597 EmpathyContactPriv *priv;
599 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
601 priv = GET_PRIV (contact);
603 return priv->presence_message;
607 empathy_contact_set_presence_message (EmpathyContact *contact,
608 const gchar *message)
610 EmpathyContactPriv *priv = GET_PRIV (contact);
612 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
614 if (!tp_strdiff (message, priv->presence_message))
617 g_free (priv->presence_message);
618 priv->presence_message = g_strdup (message);
620 g_object_notify (G_OBJECT (contact), "presence-message");
624 empathy_contact_get_handle (EmpathyContact *contact)
626 EmpathyContactPriv *priv;
628 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
630 priv = GET_PRIV (contact);
636 empathy_contact_set_handle (EmpathyContact *contact,
639 EmpathyContactPriv *priv;
641 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
643 priv = GET_PRIV (contact);
645 g_object_ref (contact);
646 if (handle != priv->handle)
648 priv->handle = handle;
649 g_object_notify (G_OBJECT (contact), "handle");
651 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_HANDLE);
652 g_object_unref (contact);
656 empathy_contact_get_capabilities (EmpathyContact *contact)
658 EmpathyContactPriv *priv;
660 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
662 priv = GET_PRIV (contact);
664 return priv->capabilities;
668 empathy_contact_set_capabilities (EmpathyContact *contact,
669 EmpathyCapabilities capabilities)
671 EmpathyContactPriv *priv;
673 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
675 priv = GET_PRIV (contact);
677 if (priv->capabilities == capabilities)
680 priv->capabilities = capabilities;
682 g_object_notify (G_OBJECT (contact), "capabilities");
686 empathy_contact_is_user (EmpathyContact *contact)
688 EmpathyContactPriv *priv;
690 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
692 priv = GET_PRIV (contact);
694 return priv->is_user;
698 empathy_contact_set_is_user (EmpathyContact *contact,
701 EmpathyContactPriv *priv;
703 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
705 priv = GET_PRIV (contact);
707 if (priv->is_user == is_user)
710 priv->is_user = is_user;
712 g_object_notify (G_OBJECT (contact), "is-user");
716 empathy_contact_is_online (EmpathyContact *contact)
718 EmpathyContactPriv *priv;
720 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
722 priv = GET_PRIV (contact);
724 return (priv->presence > MC_PRESENCE_OFFLINE);
728 empathy_contact_get_status (EmpathyContact *contact)
730 EmpathyContactPriv *priv;
732 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
734 priv = GET_PRIV (contact);
736 if (priv->presence_message)
737 return priv->presence_message;
739 return empathy_presence_get_default_message (priv->presence);
743 empathy_contact_can_voip (EmpathyContact *contact)
745 EmpathyContactPriv *priv;
747 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
749 priv = GET_PRIV (contact);
751 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
752 EMPATHY_CAPABILITIES_VIDEO);
756 empathy_contact_can_send_files (EmpathyContact *contact)
758 EmpathyContactPriv *priv;
760 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
762 priv = GET_PRIV (contact);
764 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
768 empathy_contact_get_ready (EmpathyContact *contact)
770 EmpathyContactPriv *priv;
772 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
774 priv = GET_PRIV (contact);
780 empathy_contact_equal (gconstpointer v1,
783 McAccount *account_a;
784 McAccount *account_b;
788 g_return_val_if_fail (EMPATHY_IS_CONTACT (v1), FALSE);
789 g_return_val_if_fail (EMPATHY_IS_CONTACT (v2), FALSE);
791 account_a = empathy_contact_get_account (EMPATHY_CONTACT (v1));
792 account_b = empathy_contact_get_account (EMPATHY_CONTACT (v2));
794 id_a = empathy_contact_get_id (EMPATHY_CONTACT (v1));
795 id_b = empathy_contact_get_id (EMPATHY_CONTACT (v2));
797 return empathy_account_equal (account_a, account_b) &&
798 !tp_strdiff (id_a, id_b);
802 empathy_contact_hash (gconstpointer key)
804 EmpathyContactPriv *priv;
806 g_return_val_if_fail (EMPATHY_IS_CONTACT (key), +1);
808 priv = GET_PRIV (EMPATHY_CONTACT (key));
812 priv->hash = empathy_account_hash (priv->account) ^
813 g_str_hash (priv->id);
820 contact_is_ready_func (GObject *contact,
823 EmpathyContactPriv *priv = GET_PRIV (contact);
824 EmpathyContactReady ready;
826 ready = GPOINTER_TO_UINT (user_data);
828 /* When the name is NULL, empathy_contact_get_name() fallback to the id.
829 * When the caller want to wait the name to be ready, it also want to wait
830 * the id to be ready in case of fallback. */
831 if ((ready & EMPATHY_CONTACT_READY_NAME) && G_STR_EMPTY (priv->name))
832 ready |= EMPATHY_CONTACT_READY_ID;
834 return (priv->ready & ready) == ready;
838 empathy_contact_run_until_ready (EmpathyContact *contact,
839 EmpathyContactReady ready,
842 empathy_run_until_ready_full (contact, "notify::ready",
843 contact_is_ready_func, GUINT_TO_POINTER (ready),
848 contact_get_avatar_filename (EmpathyContact *contact,
851 EmpathyContactPriv *priv = GET_PRIV (contact);
854 gchar *token_escaped;
855 gchar *contact_escaped;
857 if (G_STR_EMPTY (priv->id))
860 contact_escaped = tp_escape_as_identifier (priv->id);
861 token_escaped = tp_escape_as_identifier (token);
863 avatar_path = g_build_filename (g_get_user_cache_dir (),
866 mc_account_get_unique_name (priv->account),
869 g_mkdir_with_parents (avatar_path, 0700);
871 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
873 g_free (contact_escaped);
874 g_free (token_escaped);
875 g_free (avatar_path);
881 empathy_contact_load_avatar_data (EmpathyContact *contact,
887 EmpathyAvatar *avatar;
889 GError *error = NULL;
891 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
892 g_return_if_fail (data != NULL);
893 g_return_if_fail (len > 0);
894 g_return_if_fail (format != NULL);
895 g_return_if_fail (!G_STR_EMPTY (token));
897 /* Load and set the avatar */
898 avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
900 empathy_contact_set_avatar (contact, avatar);
901 empathy_avatar_unref (avatar);
903 /* Save to cache if not yet in it */
904 filename = contact_get_avatar_filename (contact, token);
905 if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
907 if (!empathy_avatar_save_to_file (avatar, filename, &error))
909 DEBUG ("Failed to save avatar in cache: %s",
910 error ? error->message : "No error given");
911 g_clear_error (&error);
914 DEBUG ("Avatar saved to %s", filename);
920 empathy_contact_load_avatar_cache (EmpathyContact *contact,
923 EmpathyAvatar *avatar = NULL;
927 GError *error = NULL;
929 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
930 g_return_val_if_fail (!G_STR_EMPTY (token), FALSE);
932 /* Load the avatar from file if it exists */
933 filename = contact_get_avatar_filename (contact, token);
934 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
936 if (!g_file_get_contents (filename, &data, &len, &error))
938 DEBUG ("Failed to load avatar from cache: %s",
939 error ? error->message : "No error given");
940 g_clear_error (&error);
946 DEBUG ("Avatar loaded from %s", filename);
947 avatar = empathy_avatar_new (data, len, NULL, g_strdup (token));
948 empathy_contact_set_avatar (contact, avatar);
949 empathy_avatar_unref (avatar);
958 empathy_avatar_get_type (void)
960 static GType type_id = 0;
964 type_id = g_boxed_type_register_static ("EmpathyAvatar",
965 (GBoxedCopyFunc) empathy_avatar_ref,
966 (GBoxedFreeFunc) empathy_avatar_unref);
973 empathy_avatar_new (guchar *data,
978 EmpathyAvatar *avatar;
980 avatar = g_slice_new0 (EmpathyAvatar);
983 avatar->format = format;
984 avatar->token = token;
985 avatar->refcount = 1;
991 empathy_avatar_unref (EmpathyAvatar *avatar)
993 g_return_if_fail (avatar != NULL);
996 if (avatar->refcount == 0)
998 g_free (avatar->data);
999 g_free (avatar->format);
1000 g_free (avatar->token);
1001 g_slice_free (EmpathyAvatar, avatar);
1006 empathy_avatar_ref (EmpathyAvatar *avatar)
1008 g_return_val_if_fail (avatar != NULL, NULL);
1016 * empathy_avatar_save_to_file:
1017 * @avatar: the avatar
1018 * @filename: name of a file to write avatar to
1019 * @error: return location for a GError, or NULL
1021 * Save the avatar to a file named filename
1023 * Returns: %TRUE on success, %FALSE if an error occurred
1026 empathy_avatar_save_to_file (EmpathyAvatar *self,
1027 const gchar *filename,
1030 return g_file_set_contents (filename, self->data, self->len, error);