1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
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>
24 * Sjoerd Simons <sjoerd.simons@collabora.co.uk>
31 #include <glib/gi18n-lib.h>
33 #include <telepathy-glib/util.h>
34 #include <libmissioncontrol/mc-enum-types.h>
36 #include "empathy-contact.h"
37 #include "empathy-utils.h"
38 #include "empathy-enum-types.h"
39 #include "empathy-marshal.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;
57 GList *ready_callbacks;
61 EmpathyContactReady ready;
62 EmpathyContactReadyCb *callback;
64 GDestroyNotify destroy;
68 static void contact_finalize (GObject *object);
69 static void contact_get_property (GObject *object, guint param_id,
70 GValue *value, GParamSpec *pspec);
71 static void contact_set_property (GObject *object, guint param_id,
72 const GValue *value, GParamSpec *pspec);
74 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
84 PROP_PRESENCE_MESSAGE,
96 static guint signals[LAST_SIGNAL];
99 empathy_contact_class_init (EmpathyContactClass *class)
101 GObjectClass *object_class;
103 object_class = G_OBJECT_CLASS (class);
105 object_class->finalize = contact_finalize;
106 object_class->get_property = contact_get_property;
107 object_class->set_property = contact_set_property;
109 g_object_class_install_property (object_class,
111 g_param_spec_string ("id",
113 "String identifying contact",
117 g_object_class_install_property (object_class,
119 g_param_spec_string ("name",
121 "The name of the contact",
125 g_object_class_install_property (object_class,
127 g_param_spec_boxed ("avatar",
133 g_object_class_install_property (object_class,
135 g_param_spec_object ("account",
137 "The account associated with the contact",
141 g_object_class_install_property (object_class,
143 g_param_spec_uint ("presence",
145 "Presence of contact",
151 g_object_class_install_property (object_class,
152 PROP_PRESENCE_MESSAGE,
153 g_param_spec_string ("presence-message",
154 "Contact presence message",
155 "Presence message of contact",
159 g_object_class_install_property (object_class,
161 g_param_spec_uint ("handle",
163 "The handle of the contact",
169 g_object_class_install_property (object_class,
171 g_param_spec_flags ("capabilities",
172 "Contact Capabilities",
173 "Capabilities of the contact",
174 EMPATHY_TYPE_CAPABILITIES,
175 EMPATHY_CAPABILITIES_UNKNOWN,
176 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
178 g_object_class_install_property (object_class,
180 g_param_spec_boolean ("is-user",
182 "Is contact the user",
186 g_object_class_install_property (object_class,
188 g_param_spec_flags ("ready",
189 "Contact ready flags",
190 "Flags for ready properties",
191 EMPATHY_TYPE_CONTACT_READY,
192 EMPATHY_CONTACT_READY_NONE,
195 signals[PRESENCE_CHANGED] =
196 g_signal_new ("presence-changed",
197 G_TYPE_FROM_CLASS (class),
201 _empathy_marshal_VOID__ENUM_ENUM,
206 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
210 empathy_contact_init (EmpathyContact *contact)
212 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
213 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
215 contact->priv = priv;
219 contact_finalize (GObject *object)
221 EmpathyContactPriv *priv;
223 priv = GET_PRIV (object);
225 DEBUG ("finalize: %p", object);
229 g_free (priv->presence_message);
232 empathy_avatar_unref (priv->avatar);
235 g_object_unref (priv->account);
237 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
241 contact_get_property (GObject *object,
246 EmpathyContactPriv *priv;
248 priv = GET_PRIV (object);
253 g_value_set_string (value, priv->id);
256 g_value_set_string (value,
257 empathy_contact_get_name (EMPATHY_CONTACT (object)));
260 g_value_set_boxed (value, priv->avatar);
263 g_value_set_object (value, priv->account);
266 g_value_set_uint (value, priv->presence);
268 case PROP_PRESENCE_MESSAGE:
269 g_value_set_string (value, priv->presence_message);
272 g_value_set_uint (value, priv->handle);
274 case PROP_CAPABILITIES:
275 g_value_set_flags (value, priv->capabilities);
278 g_value_set_boolean (value, priv->is_user);
281 g_value_set_flags (value, priv->ready);
284 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
290 contact_set_property (GObject *object,
295 EmpathyContactPriv *priv;
297 priv = GET_PRIV (object);
302 empathy_contact_set_id (EMPATHY_CONTACT (object),
303 g_value_get_string (value));
306 empathy_contact_set_name (EMPATHY_CONTACT (object),
307 g_value_get_string (value));
310 empathy_contact_set_avatar (EMPATHY_CONTACT (object),
311 g_value_get_boxed (value));
314 empathy_contact_set_account (EMPATHY_CONTACT (object),
315 MC_ACCOUNT (g_value_get_object (value)));
318 empathy_contact_set_presence (EMPATHY_CONTACT (object),
319 g_value_get_uint (value));
321 case PROP_PRESENCE_MESSAGE:
322 empathy_contact_set_presence_message (EMPATHY_CONTACT (object),
323 g_value_get_string (value));
326 empathy_contact_set_handle (EMPATHY_CONTACT (object),
327 g_value_get_uint (value));
329 case PROP_CAPABILITIES:
330 empathy_contact_set_capabilities (EMPATHY_CONTACT (object),
331 g_value_get_flags (value));
334 empathy_contact_set_is_user (EMPATHY_CONTACT (object),
335 g_value_get_boolean (value));
338 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
344 contact_is_ready (EmpathyContact *contact, EmpathyContactReady ready)
346 EmpathyContactPriv *priv = GET_PRIV (contact);
348 /* When the name is NULL, empathy_contact_get_name() fallback to the id.
349 * When the caller want to wait the name to be ready, it also want to wait
350 * the id to be ready in case of fallback. */
351 if ((ready & EMPATHY_CONTACT_READY_NAME) && G_STR_EMPTY (priv->name))
352 ready |= EMPATHY_CONTACT_READY_ID;
354 return (priv->ready & ready) == ready;
358 contact_weak_object_notify (gpointer data, GObject *old_object)
360 EmpathyContact *contact = EMPATHY_CONTACT (data);
361 EmpathyContactPriv *priv = GET_PRIV (contact);
365 for (l = priv->ready_callbacks ; l != NULL ; l = ln )
367 ReadyCbData *d = (ReadyCbData *)l->data;
368 ln = g_list_next (l);
370 if (d->weak_object == old_object)
372 if (d->destroy != NULL)
373 d->destroy (d->user_data);
375 priv->ready_callbacks = g_list_delete_link (priv->ready_callbacks,
382 contact_call_ready_callback (EmpathyContact *contact, GError *error,
385 data->callback (contact, error, data->user_data, data->weak_object);
386 if (data->destroy != NULL)
387 data->destroy (data->user_data);
389 if (data->weak_object)
390 g_object_weak_unref (data->weak_object,
391 contact_weak_object_notify, contact);
396 contact_set_ready_flag (EmpathyContact *contact,
397 EmpathyContactReady flag)
399 EmpathyContactPriv *priv = GET_PRIV (contact);
401 if (!(priv->ready & flag))
406 g_object_notify (G_OBJECT (contact), "ready");
408 for (l = priv->ready_callbacks ; l != NULL ; l = ln )
410 ReadyCbData *d = (ReadyCbData *)l->data;
411 ln = g_list_next (l);
413 if (contact_is_ready (contact, d->ready))
415 contact_call_ready_callback (contact, NULL, d);
416 priv->ready_callbacks = g_list_delete_link
417 (priv->ready_callbacks, l);
424 empathy_contact_new (McAccount *account)
426 return g_object_new (EMPATHY_TYPE_CONTACT,
432 empathy_contact_new_full (McAccount *account,
436 return g_object_new (EMPATHY_TYPE_CONTACT,
444 empathy_contact_get_id (EmpathyContact *contact)
446 EmpathyContactPriv *priv;
448 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
450 priv = GET_PRIV (contact);
456 empathy_contact_set_id (EmpathyContact *contact,
459 EmpathyContactPriv *priv;
461 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
462 g_return_if_fail (id != NULL);
464 priv = GET_PRIV (contact);
466 /* We temporally ref the contact because it could be destroyed
467 * during the signal emition */
468 g_object_ref (contact);
469 if (tp_strdiff (id, priv->id))
472 priv->id = g_strdup (id);
474 g_object_notify (G_OBJECT (contact), "id");
475 if (G_STR_EMPTY (priv->name))
476 g_object_notify (G_OBJECT (contact), "name");
478 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_ID);
480 g_object_unref (contact);
484 empathy_contact_get_name (EmpathyContact *contact)
486 EmpathyContactPriv *priv;
488 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
490 priv = GET_PRIV (contact);
492 if (G_STR_EMPTY (priv->name))
493 return empathy_contact_get_id (contact);
499 empathy_contact_set_name (EmpathyContact *contact,
502 EmpathyContactPriv *priv;
504 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
506 priv = GET_PRIV (contact);
508 g_object_ref (contact);
509 if (tp_strdiff (name, priv->name))
512 priv->name = g_strdup (name);
513 g_object_notify (G_OBJECT (contact), "name");
515 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_NAME);
516 g_object_unref (contact);
520 empathy_contact_get_avatar (EmpathyContact *contact)
522 EmpathyContactPriv *priv;
524 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
526 priv = GET_PRIV (contact);
532 empathy_contact_set_avatar (EmpathyContact *contact,
533 EmpathyAvatar *avatar)
535 EmpathyContactPriv *priv;
537 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
539 priv = GET_PRIV (contact);
541 if (priv->avatar == avatar)
546 empathy_avatar_unref (priv->avatar);
551 priv->avatar = empathy_avatar_ref (avatar);
553 g_object_notify (G_OBJECT (contact), "avatar");
557 empathy_contact_get_account (EmpathyContact *contact)
559 EmpathyContactPriv *priv;
561 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
563 priv = GET_PRIV (contact);
565 return priv->account;
569 empathy_contact_set_account (EmpathyContact *contact,
572 EmpathyContactPriv *priv;
574 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
575 g_return_if_fail (MC_IS_ACCOUNT (account));
577 priv = GET_PRIV (contact);
579 if (account == priv->account)
583 g_object_unref (priv->account);
584 priv->account = g_object_ref (account);
586 g_object_notify (G_OBJECT (contact), "account");
590 empathy_contact_get_presence (EmpathyContact *contact)
592 EmpathyContactPriv *priv;
594 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), MC_PRESENCE_UNSET);
596 priv = GET_PRIV (contact);
598 return priv->presence;
602 empathy_contact_set_presence (EmpathyContact *contact,
605 EmpathyContactPriv *priv;
606 McPresence old_presence;
608 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
610 priv = GET_PRIV (contact);
612 if (presence == priv->presence)
615 old_presence = priv->presence;
616 priv->presence = presence;
618 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
620 g_object_notify (G_OBJECT (contact), "presence");
624 empathy_contact_get_presence_message (EmpathyContact *contact)
626 EmpathyContactPriv *priv;
628 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
630 priv = GET_PRIV (contact);
632 return priv->presence_message;
636 empathy_contact_set_presence_message (EmpathyContact *contact,
637 const gchar *message)
639 EmpathyContactPriv *priv = GET_PRIV (contact);
641 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
643 if (!tp_strdiff (message, priv->presence_message))
646 g_free (priv->presence_message);
647 priv->presence_message = g_strdup (message);
649 g_object_notify (G_OBJECT (contact), "presence-message");
653 empathy_contact_get_handle (EmpathyContact *contact)
655 EmpathyContactPriv *priv;
657 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
659 priv = GET_PRIV (contact);
665 empathy_contact_set_handle (EmpathyContact *contact,
668 EmpathyContactPriv *priv;
670 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
672 priv = GET_PRIV (contact);
674 g_object_ref (contact);
675 if (handle != priv->handle)
677 priv->handle = handle;
678 g_object_notify (G_OBJECT (contact), "handle");
680 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_HANDLE);
681 g_object_unref (contact);
685 empathy_contact_get_capabilities (EmpathyContact *contact)
687 EmpathyContactPriv *priv;
689 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
691 priv = GET_PRIV (contact);
693 return priv->capabilities;
697 empathy_contact_set_capabilities (EmpathyContact *contact,
698 EmpathyCapabilities capabilities)
700 EmpathyContactPriv *priv;
702 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
704 priv = GET_PRIV (contact);
706 if (priv->capabilities == capabilities)
709 priv->capabilities = capabilities;
711 g_object_notify (G_OBJECT (contact), "capabilities");
715 empathy_contact_is_user (EmpathyContact *contact)
717 EmpathyContactPriv *priv;
719 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
721 priv = GET_PRIV (contact);
723 return priv->is_user;
727 empathy_contact_set_is_user (EmpathyContact *contact,
730 EmpathyContactPriv *priv;
732 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
734 priv = GET_PRIV (contact);
736 if (priv->is_user == is_user)
739 priv->is_user = is_user;
741 g_object_notify (G_OBJECT (contact), "is-user");
745 empathy_contact_is_online (EmpathyContact *contact)
747 EmpathyContactPriv *priv;
749 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
751 priv = GET_PRIV (contact);
753 return (priv->presence > MC_PRESENCE_OFFLINE);
757 empathy_contact_get_status (EmpathyContact *contact)
759 EmpathyContactPriv *priv;
761 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
763 priv = GET_PRIV (contact);
765 if (priv->presence_message)
766 return priv->presence_message;
768 return empathy_presence_get_default_message (priv->presence);
772 empathy_contact_can_voip (EmpathyContact *contact)
774 EmpathyContactPriv *priv;
776 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
778 priv = GET_PRIV (contact);
780 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
781 EMPATHY_CAPABILITIES_VIDEO);
785 empathy_contact_can_send_files (EmpathyContact *contact)
787 EmpathyContactPriv *priv;
789 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
791 priv = GET_PRIV (contact);
793 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
797 empathy_contact_get_ready (EmpathyContact *contact)
799 EmpathyContactPriv *priv;
801 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
803 priv = GET_PRIV (contact);
809 empathy_contact_equal (gconstpointer v1,
812 McAccount *account_a;
813 McAccount *account_b;
817 g_return_val_if_fail (EMPATHY_IS_CONTACT (v1), FALSE);
818 g_return_val_if_fail (EMPATHY_IS_CONTACT (v2), FALSE);
820 account_a = empathy_contact_get_account (EMPATHY_CONTACT (v1));
821 account_b = empathy_contact_get_account (EMPATHY_CONTACT (v2));
823 id_a = empathy_contact_get_id (EMPATHY_CONTACT (v1));
824 id_b = empathy_contact_get_id (EMPATHY_CONTACT (v2));
826 return empathy_account_equal (account_a, account_b) &&
827 !tp_strdiff (id_a, id_b);
831 empathy_contact_hash (gconstpointer key)
833 EmpathyContactPriv *priv;
835 g_return_val_if_fail (EMPATHY_IS_CONTACT (key), +1);
837 priv = GET_PRIV (EMPATHY_CONTACT (key));
841 priv->hash = empathy_account_hash (priv->account) ^
842 g_str_hash (priv->id);
848 void empathy_contact_call_when_ready (EmpathyContact *contact,
849 EmpathyContactReady ready, EmpathyContactReadyCb *callback,
850 gpointer user_data, GDestroyNotify destroy, GObject *weak_object)
852 EmpathyContactPriv *priv = GET_PRIV (contact);
854 g_return_if_fail (contact != NULL);
855 g_return_if_fail (callback != NULL);
857 if (contact_is_ready (contact, ready))
859 callback (contact, NULL, user_data, weak_object);
865 ReadyCbData *d = g_slice_new0 (ReadyCbData);
867 d->callback = callback;
868 d->user_data = user_data;
869 d->destroy = destroy;
870 d->weak_object = weak_object;
872 if (weak_object != NULL)
873 g_object_weak_ref (weak_object, contact_weak_object_notify, contact);
875 priv->ready_callbacks = g_list_prepend (priv->ready_callbacks, d);
880 contact_is_ready_func (GObject *contact,
883 return contact_is_ready (EMPATHY_CONTACT (contact),
884 GPOINTER_TO_UINT (user_data));
888 empathy_contact_run_until_ready (EmpathyContact *contact,
889 EmpathyContactReady ready,
892 empathy_run_until_ready_full (contact, "notify::ready",
893 contact_is_ready_func, GUINT_TO_POINTER (ready),
898 contact_get_avatar_filename (EmpathyContact *contact,
901 EmpathyContactPriv *priv = GET_PRIV (contact);
904 gchar *token_escaped;
905 gchar *contact_escaped;
907 if (G_STR_EMPTY (priv->id))
910 contact_escaped = tp_escape_as_identifier (priv->id);
911 token_escaped = tp_escape_as_identifier (token);
913 avatar_path = g_build_filename (g_get_user_cache_dir (),
916 mc_account_get_unique_name (priv->account),
919 g_mkdir_with_parents (avatar_path, 0700);
921 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
923 g_free (contact_escaped);
924 g_free (token_escaped);
925 g_free (avatar_path);
931 empathy_contact_load_avatar_data (EmpathyContact *contact,
937 EmpathyAvatar *avatar;
939 GError *error = NULL;
941 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
942 g_return_if_fail (data != NULL);
943 g_return_if_fail (len > 0);
944 g_return_if_fail (format != NULL);
945 g_return_if_fail (!G_STR_EMPTY (token));
947 /* Load and set the avatar */
948 avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
950 empathy_contact_set_avatar (contact, avatar);
951 empathy_avatar_unref (avatar);
953 /* Save to cache if not yet in it */
954 filename = contact_get_avatar_filename (contact, token);
955 if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
957 if (!empathy_avatar_save_to_file (avatar, filename, &error))
959 DEBUG ("Failed to save avatar in cache: %s",
960 error ? error->message : "No error given");
961 g_clear_error (&error);
964 DEBUG ("Avatar saved to %s", filename);
970 empathy_contact_load_avatar_cache (EmpathyContact *contact,
973 EmpathyAvatar *avatar = NULL;
977 GError *error = NULL;
979 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
980 g_return_val_if_fail (!G_STR_EMPTY (token), FALSE);
982 /* Load the avatar from file if it exists */
983 filename = contact_get_avatar_filename (contact, token);
984 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
986 if (!g_file_get_contents (filename, &data, &len, &error))
988 DEBUG ("Failed to load avatar from cache: %s",
989 error ? error->message : "No error given");
990 g_clear_error (&error);
996 DEBUG ("Avatar loaded from %s", filename);
997 avatar = empathy_avatar_new (data, len, NULL, g_strdup (token));
998 empathy_contact_set_avatar (contact, avatar);
999 empathy_avatar_unref (avatar);
1004 return data != NULL;
1008 empathy_avatar_get_type (void)
1010 static GType type_id = 0;
1014 type_id = g_boxed_type_register_static ("EmpathyAvatar",
1015 (GBoxedCopyFunc) empathy_avatar_ref,
1016 (GBoxedFreeFunc) empathy_avatar_unref);
1023 empathy_avatar_new (guchar *data,
1028 EmpathyAvatar *avatar;
1030 avatar = g_slice_new0 (EmpathyAvatar);
1031 avatar->data = data;
1033 avatar->format = format;
1034 avatar->token = token;
1035 avatar->refcount = 1;
1041 empathy_avatar_unref (EmpathyAvatar *avatar)
1043 g_return_if_fail (avatar != NULL);
1046 if (avatar->refcount == 0)
1048 g_free (avatar->data);
1049 g_free (avatar->format);
1050 g_free (avatar->token);
1051 g_slice_free (EmpathyAvatar, avatar);
1056 empathy_avatar_ref (EmpathyAvatar *avatar)
1058 g_return_val_if_fail (avatar != NULL, NULL);
1066 * empathy_avatar_save_to_file:
1067 * @avatar: the avatar
1068 * @filename: name of a file to write avatar to
1069 * @error: return location for a GError, or NULL
1071 * Save the avatar to a file named filename
1073 * Returns: %TRUE on success, %FALSE if an error occurred
1076 empathy_avatar_save_to_file (EmpathyAvatar *self,
1077 const gchar *filename,
1080 return g_file_set_contents (filename, self->data, self->len, error);