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>
30 #include <glib/gi18n-lib.h>
32 #include <telepathy-glib/util.h>
33 #include <libmissioncontrol/mc-enum-types.h>
35 #include "empathy-contact.h"
36 #include "empathy-utils.h"
37 #include "empathy-enum-types.h"
38 #include "empathy-marshal.h"
40 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
41 #include "empathy-debug.h"
43 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContact)
47 EmpathyAvatar *avatar;
50 gchar *presence_message;
52 EmpathyCapabilities capabilities;
55 EmpathyContactReady ready;
56 GList *ready_callbacks;
60 EmpathyContactReady ready;
61 EmpathyContactReadyCb *callback;
65 static void contact_finalize (GObject *object);
66 static void contact_get_property (GObject *object, guint param_id,
67 GValue *value, GParamSpec *pspec);
68 static void contact_set_property (GObject *object, guint param_id,
69 const GValue *value, GParamSpec *pspec);
71 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
81 PROP_PRESENCE_MESSAGE,
93 static guint signals[LAST_SIGNAL];
96 empathy_contact_class_init (EmpathyContactClass *class)
98 GObjectClass *object_class;
100 object_class = G_OBJECT_CLASS (class);
102 object_class->finalize = contact_finalize;
103 object_class->get_property = contact_get_property;
104 object_class->set_property = contact_set_property;
106 g_object_class_install_property (object_class,
108 g_param_spec_string ("id",
110 "String identifying contact",
114 g_object_class_install_property (object_class,
116 g_param_spec_string ("name",
118 "The name of the contact",
122 g_object_class_install_property (object_class,
124 g_param_spec_boxed ("avatar",
130 g_object_class_install_property (object_class,
132 g_param_spec_object ("account",
134 "The account associated with the contact",
138 g_object_class_install_property (object_class,
140 g_param_spec_uint ("presence",
142 "Presence of contact",
148 g_object_class_install_property (object_class,
149 PROP_PRESENCE_MESSAGE,
150 g_param_spec_string ("presence-message",
151 "Contact presence message",
152 "Presence message of contact",
156 g_object_class_install_property (object_class,
158 g_param_spec_uint ("handle",
160 "The handle of the contact",
166 g_object_class_install_property (object_class,
168 g_param_spec_flags ("capabilities",
169 "Contact Capabilities",
170 "Capabilities of the contact",
171 EMPATHY_TYPE_CAPABILITIES,
172 EMPATHY_CAPABILITIES_UNKNOWN,
173 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
175 g_object_class_install_property (object_class,
177 g_param_spec_boolean ("is-user",
179 "Is contact the user",
183 g_object_class_install_property (object_class,
185 g_param_spec_flags ("ready",
186 "Contact ready flags",
187 "Flags for ready properties",
188 EMPATHY_TYPE_CONTACT_READY,
189 EMPATHY_CONTACT_READY_NONE,
192 signals[PRESENCE_CHANGED] =
193 g_signal_new ("presence-changed",
194 G_TYPE_FROM_CLASS (class),
198 _empathy_marshal_VOID__ENUM_ENUM,
203 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
207 empathy_contact_init (EmpathyContact *contact)
209 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
210 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
212 contact->priv = priv;
216 contact_finalize (GObject *object)
218 EmpathyContactPriv *priv;
220 priv = GET_PRIV (object);
222 DEBUG ("finalize: %p", object);
226 g_free (priv->presence_message);
229 empathy_avatar_unref (priv->avatar);
232 g_object_unref (priv->account);
234 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
238 contact_get_property (GObject *object,
243 EmpathyContactPriv *priv;
245 priv = GET_PRIV (object);
250 g_value_set_string (value, priv->id);
253 g_value_set_string (value,
254 empathy_contact_get_name (EMPATHY_CONTACT (object)));
257 g_value_set_boxed (value, priv->avatar);
260 g_value_set_object (value, priv->account);
263 g_value_set_uint (value, priv->presence);
265 case PROP_PRESENCE_MESSAGE:
266 g_value_set_string (value, priv->presence_message);
269 g_value_set_uint (value, priv->handle);
271 case PROP_CAPABILITIES:
272 g_value_set_flags (value, priv->capabilities);
275 g_value_set_boolean (value, priv->is_user);
278 g_value_set_flags (value, priv->ready);
281 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
287 contact_set_property (GObject *object,
292 EmpathyContactPriv *priv;
294 priv = GET_PRIV (object);
299 empathy_contact_set_id (EMPATHY_CONTACT (object),
300 g_value_get_string (value));
303 empathy_contact_set_name (EMPATHY_CONTACT (object),
304 g_value_get_string (value));
307 empathy_contact_set_avatar (EMPATHY_CONTACT (object),
308 g_value_get_boxed (value));
311 empathy_contact_set_account (EMPATHY_CONTACT (object),
312 MC_ACCOUNT (g_value_get_object (value)));
315 empathy_contact_set_presence (EMPATHY_CONTACT (object),
316 g_value_get_uint (value));
318 case PROP_PRESENCE_MESSAGE:
319 empathy_contact_set_presence_message (EMPATHY_CONTACT (object),
320 g_value_get_string (value));
323 empathy_contact_set_handle (EMPATHY_CONTACT (object),
324 g_value_get_uint (value));
326 case PROP_CAPABILITIES:
327 empathy_contact_set_capabilities (EMPATHY_CONTACT (object),
328 g_value_get_flags (value));
331 empathy_contact_set_is_user (EMPATHY_CONTACT (object),
332 g_value_get_boolean (value));
335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
341 contact_is_ready (EmpathyContact *contact, EmpathyContactReady ready)
343 EmpathyContactPriv *priv = GET_PRIV (contact);
345 /* When the name is NULL, empathy_contact_get_name() fallback to the id.
346 * When the caller want to wait the name to be ready, it also want to wait
347 * the id to be ready in case of fallback. */
348 if ((ready & EMPATHY_CONTACT_READY_NAME) && G_STR_EMPTY (priv->name))
349 ready |= EMPATHY_CONTACT_READY_ID;
351 return (priv->ready & ready) == ready;
356 contact_set_ready_flag (EmpathyContact *contact,
357 EmpathyContactReady flag)
359 EmpathyContactPriv *priv = GET_PRIV (contact);
361 if (!(priv->ready & flag))
366 g_object_notify (G_OBJECT (contact), "ready");
368 for (l = priv->ready_callbacks ; l != NULL ; l = ln )
370 ReadyCbData *d = (ReadyCbData *)l->data;
371 ln = g_list_next (l);
373 if (contact_is_ready (contact, d->ready))
375 d->callback (contact, d->user_data);
377 priv->ready_callbacks = g_list_delete_link
378 (priv->ready_callbacks, l);
385 empathy_contact_new (McAccount *account)
387 return g_object_new (EMPATHY_TYPE_CONTACT,
393 empathy_contact_new_full (McAccount *account,
397 return g_object_new (EMPATHY_TYPE_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);
417 empathy_contact_set_id (EmpathyContact *contact,
420 EmpathyContactPriv *priv;
422 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
423 g_return_if_fail (id != NULL);
425 priv = GET_PRIV (contact);
427 /* We temporally ref the contact because it could be destroyed
428 * during the signal emition */
429 g_object_ref (contact);
430 if (tp_strdiff (id, priv->id))
433 priv->id = g_strdup (id);
435 g_object_notify (G_OBJECT (contact), "id");
436 if (G_STR_EMPTY (priv->name))
437 g_object_notify (G_OBJECT (contact), "name");
439 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_ID);
441 g_object_unref (contact);
445 empathy_contact_get_name (EmpathyContact *contact)
447 EmpathyContactPriv *priv;
449 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
451 priv = GET_PRIV (contact);
453 if (G_STR_EMPTY (priv->name))
454 return empathy_contact_get_id (contact);
460 empathy_contact_set_name (EmpathyContact *contact,
463 EmpathyContactPriv *priv;
465 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
467 priv = GET_PRIV (contact);
469 g_object_ref (contact);
470 if (tp_strdiff (name, priv->name))
473 priv->name = g_strdup (name);
474 g_object_notify (G_OBJECT (contact), "name");
476 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_NAME);
477 g_object_unref (contact);
481 empathy_contact_get_avatar (EmpathyContact *contact)
483 EmpathyContactPriv *priv;
485 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
487 priv = GET_PRIV (contact);
493 empathy_contact_set_avatar (EmpathyContact *contact,
494 EmpathyAvatar *avatar)
496 EmpathyContactPriv *priv;
498 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
500 priv = GET_PRIV (contact);
502 if (priv->avatar == avatar)
507 empathy_avatar_unref (priv->avatar);
512 priv->avatar = empathy_avatar_ref (avatar);
514 g_object_notify (G_OBJECT (contact), "avatar");
518 empathy_contact_get_account (EmpathyContact *contact)
520 EmpathyContactPriv *priv;
522 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
524 priv = GET_PRIV (contact);
526 return priv->account;
530 empathy_contact_set_account (EmpathyContact *contact,
533 EmpathyContactPriv *priv;
535 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
536 g_return_if_fail (MC_IS_ACCOUNT (account));
538 priv = GET_PRIV (contact);
540 if (account == priv->account)
544 g_object_unref (priv->account);
545 priv->account = g_object_ref (account);
547 g_object_notify (G_OBJECT (contact), "account");
551 empathy_contact_get_presence (EmpathyContact *contact)
553 EmpathyContactPriv *priv;
555 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), MC_PRESENCE_UNSET);
557 priv = GET_PRIV (contact);
559 return priv->presence;
563 empathy_contact_set_presence (EmpathyContact *contact,
566 EmpathyContactPriv *priv;
567 McPresence old_presence;
569 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
571 priv = GET_PRIV (contact);
573 if (presence == priv->presence)
576 old_presence = priv->presence;
577 priv->presence = presence;
579 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
581 g_object_notify (G_OBJECT (contact), "presence");
585 empathy_contact_get_presence_message (EmpathyContact *contact)
587 EmpathyContactPriv *priv;
589 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
591 priv = GET_PRIV (contact);
593 return priv->presence_message;
597 empathy_contact_set_presence_message (EmpathyContact *contact,
598 const gchar *message)
600 EmpathyContactPriv *priv = GET_PRIV (contact);
602 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
604 if (!tp_strdiff (message, priv->presence_message))
607 g_free (priv->presence_message);
608 priv->presence_message = g_strdup (message);
610 g_object_notify (G_OBJECT (contact), "presence-message");
614 empathy_contact_get_handle (EmpathyContact *contact)
616 EmpathyContactPriv *priv;
618 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
620 priv = GET_PRIV (contact);
626 empathy_contact_set_handle (EmpathyContact *contact,
629 EmpathyContactPriv *priv;
631 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
633 priv = GET_PRIV (contact);
635 g_object_ref (contact);
636 if (handle != priv->handle)
638 priv->handle = handle;
639 g_object_notify (G_OBJECT (contact), "handle");
641 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_HANDLE);
642 g_object_unref (contact);
646 empathy_contact_get_capabilities (EmpathyContact *contact)
648 EmpathyContactPriv *priv;
650 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
652 priv = GET_PRIV (contact);
654 return priv->capabilities;
658 empathy_contact_set_capabilities (EmpathyContact *contact,
659 EmpathyCapabilities capabilities)
661 EmpathyContactPriv *priv;
663 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
665 priv = GET_PRIV (contact);
667 if (priv->capabilities == capabilities)
670 priv->capabilities = capabilities;
672 g_object_notify (G_OBJECT (contact), "capabilities");
676 empathy_contact_is_user (EmpathyContact *contact)
678 EmpathyContactPriv *priv;
680 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
682 priv = GET_PRIV (contact);
684 return priv->is_user;
688 empathy_contact_set_is_user (EmpathyContact *contact,
691 EmpathyContactPriv *priv;
693 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
695 priv = GET_PRIV (contact);
697 if (priv->is_user == is_user)
700 priv->is_user = is_user;
702 g_object_notify (G_OBJECT (contact), "is-user");
706 empathy_contact_is_online (EmpathyContact *contact)
708 EmpathyContactPriv *priv;
710 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
712 priv = GET_PRIV (contact);
714 return (priv->presence > MC_PRESENCE_OFFLINE);
718 empathy_contact_get_status (EmpathyContact *contact)
720 EmpathyContactPriv *priv;
722 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
724 priv = GET_PRIV (contact);
726 if (priv->presence_message)
727 return priv->presence_message;
729 return empathy_presence_get_default_message (priv->presence);
733 empathy_contact_can_voip (EmpathyContact *contact)
735 EmpathyContactPriv *priv;
737 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
739 priv = GET_PRIV (contact);
741 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
742 EMPATHY_CAPABILITIES_VIDEO);
746 empathy_contact_can_send_files (EmpathyContact *contact)
748 EmpathyContactPriv *priv;
750 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
752 priv = GET_PRIV (contact);
754 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
758 empathy_contact_get_ready (EmpathyContact *contact)
760 EmpathyContactPriv *priv;
762 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
764 priv = GET_PRIV (contact);
770 empathy_contact_equal (gconstpointer v1,
773 McAccount *account_a;
774 McAccount *account_b;
778 g_return_val_if_fail (EMPATHY_IS_CONTACT (v1), FALSE);
779 g_return_val_if_fail (EMPATHY_IS_CONTACT (v2), FALSE);
781 account_a = empathy_contact_get_account (EMPATHY_CONTACT (v1));
782 account_b = empathy_contact_get_account (EMPATHY_CONTACT (v2));
784 id_a = empathy_contact_get_id (EMPATHY_CONTACT (v1));
785 id_b = empathy_contact_get_id (EMPATHY_CONTACT (v2));
787 return empathy_account_equal (account_a, account_b) &&
788 !tp_strdiff (id_a, id_b);
792 empathy_contact_hash (gconstpointer key)
794 EmpathyContactPriv *priv;
796 g_return_val_if_fail (EMPATHY_IS_CONTACT (key), +1);
798 priv = GET_PRIV (EMPATHY_CONTACT (key));
802 priv->hash = empathy_account_hash (priv->account) ^
803 g_str_hash (priv->id);
809 void empathy_contact_call_when_ready (EmpathyContact *contact,
810 EmpathyContactReady ready, EmpathyContactReadyCb *callback, gpointer
813 EmpathyContactPriv *priv = GET_PRIV (contact);
815 g_return_if_fail (contact != NULL);
816 g_return_if_fail (callback != NULL);
818 if (contact_is_ready (contact, ready))
820 callback (contact, user_data);
824 ReadyCbData *d = g_slice_new0 (ReadyCbData);
826 d->callback = callback;
827 d->user_data = user_data;
828 priv->ready_callbacks = g_list_prepend (priv->ready_callbacks,
834 contact_is_ready_func (GObject *contact,
837 return contact_is_ready (EMPATHY_CONTACT (contact),
838 GPOINTER_TO_UINT (user_data));
842 empathy_contact_run_until_ready (EmpathyContact *contact,
843 EmpathyContactReady ready,
846 empathy_run_until_ready_full (contact, "notify::ready",
847 contact_is_ready_func, GUINT_TO_POINTER (ready),
852 contact_get_avatar_filename (EmpathyContact *contact,
855 EmpathyContactPriv *priv = GET_PRIV (contact);
858 gchar *token_escaped;
859 gchar *contact_escaped;
861 if (G_STR_EMPTY (priv->id))
864 contact_escaped = tp_escape_as_identifier (priv->id);
865 token_escaped = tp_escape_as_identifier (token);
867 avatar_path = g_build_filename (g_get_user_cache_dir (),
870 mc_account_get_unique_name (priv->account),
873 g_mkdir_with_parents (avatar_path, 0700);
875 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
877 g_free (contact_escaped);
878 g_free (token_escaped);
879 g_free (avatar_path);
885 empathy_contact_load_avatar_data (EmpathyContact *contact,
891 EmpathyAvatar *avatar;
893 GError *error = NULL;
895 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
896 g_return_if_fail (data != NULL);
897 g_return_if_fail (len > 0);
898 g_return_if_fail (format != NULL);
899 g_return_if_fail (!G_STR_EMPTY (token));
901 /* Load and set the avatar */
902 avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
904 empathy_contact_set_avatar (contact, avatar);
905 empathy_avatar_unref (avatar);
907 /* Save to cache if not yet in it */
908 filename = contact_get_avatar_filename (contact, token);
909 if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
911 if (!empathy_avatar_save_to_file (avatar, filename, &error))
913 DEBUG ("Failed to save avatar in cache: %s",
914 error ? error->message : "No error given");
915 g_clear_error (&error);
918 DEBUG ("Avatar saved to %s", filename);
924 empathy_contact_load_avatar_cache (EmpathyContact *contact,
927 EmpathyAvatar *avatar = NULL;
931 GError *error = NULL;
933 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
934 g_return_val_if_fail (!G_STR_EMPTY (token), FALSE);
936 /* Load the avatar from file if it exists */
937 filename = contact_get_avatar_filename (contact, token);
938 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
940 if (!g_file_get_contents (filename, &data, &len, &error))
942 DEBUG ("Failed to load avatar from cache: %s",
943 error ? error->message : "No error given");
944 g_clear_error (&error);
950 DEBUG ("Avatar loaded from %s", filename);
951 avatar = empathy_avatar_new (data, len, NULL, g_strdup (token));
952 empathy_contact_set_avatar (contact, avatar);
953 empathy_avatar_unref (avatar);
962 empathy_avatar_get_type (void)
964 static GType type_id = 0;
968 type_id = g_boxed_type_register_static ("EmpathyAvatar",
969 (GBoxedCopyFunc) empathy_avatar_ref,
970 (GBoxedFreeFunc) empathy_avatar_unref);
977 empathy_avatar_new (guchar *data,
982 EmpathyAvatar *avatar;
984 avatar = g_slice_new0 (EmpathyAvatar);
987 avatar->format = format;
988 avatar->token = token;
989 avatar->refcount = 1;
995 empathy_avatar_unref (EmpathyAvatar *avatar)
997 g_return_if_fail (avatar != NULL);
1000 if (avatar->refcount == 0)
1002 g_free (avatar->data);
1003 g_free (avatar->format);
1004 g_free (avatar->token);
1005 g_slice_free (EmpathyAvatar, avatar);
1010 empathy_avatar_ref (EmpathyAvatar *avatar)
1012 g_return_val_if_fail (avatar != NULL, NULL);
1020 * empathy_avatar_save_to_file:
1021 * @avatar: the avatar
1022 * @filename: name of a file to write avatar to
1023 * @error: return location for a GError, or NULL
1025 * Save the avatar to a file named filename
1027 * Returns: %TRUE on success, %FALSE if an error occurred
1030 empathy_avatar_save_to_file (EmpathyAvatar *self,
1031 const gchar *filename,
1034 return g_file_set_contents (filename, self->data, self->len, error);