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;
58 static void contact_finalize (GObject *object);
59 static void contact_get_property (GObject *object, guint param_id,
60 GValue *value, GParamSpec *pspec);
61 static void contact_set_property (GObject *object, guint param_id,
62 const GValue *value, GParamSpec *pspec);
64 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
74 PROP_PRESENCE_MESSAGE,
86 static guint signals[LAST_SIGNAL];
89 empathy_contact_class_init (EmpathyContactClass *class)
91 GObjectClass *object_class;
93 object_class = G_OBJECT_CLASS (class);
95 object_class->finalize = contact_finalize;
96 object_class->get_property = contact_get_property;
97 object_class->set_property = contact_set_property;
99 g_object_class_install_property (object_class,
101 g_param_spec_string ("id",
103 "String identifying contact",
107 g_object_class_install_property (object_class,
109 g_param_spec_string ("name",
111 "The name of the contact",
115 g_object_class_install_property (object_class,
117 g_param_spec_boxed ("avatar",
123 g_object_class_install_property (object_class,
125 g_param_spec_object ("account",
127 "The account associated with the contact",
131 g_object_class_install_property (object_class,
133 g_param_spec_uint ("presence",
135 "Presence of contact",
141 g_object_class_install_property (object_class,
142 PROP_PRESENCE_MESSAGE,
143 g_param_spec_string ("presence-message",
144 "Contact presence message",
145 "Presence message of contact",
149 g_object_class_install_property (object_class,
151 g_param_spec_uint ("handle",
153 "The handle of the contact",
159 g_object_class_install_property (object_class,
161 g_param_spec_flags ("capabilities",
162 "Contact Capabilities",
163 "Capabilities of the contact",
164 EMPATHY_TYPE_CAPABILITIES,
165 EMPATHY_CAPABILITIES_UNKNOWN,
166 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
168 g_object_class_install_property (object_class,
170 g_param_spec_boolean ("is-user",
172 "Is contact the user",
176 g_object_class_install_property (object_class,
178 g_param_spec_flags ("ready",
179 "Contact ready flags",
180 "Flags for ready properties",
181 EMPATHY_TYPE_CONTACT_READY,
182 EMPATHY_CONTACT_READY_NONE,
185 signals[PRESENCE_CHANGED] =
186 g_signal_new ("presence-changed",
187 G_TYPE_FROM_CLASS (class),
191 _empathy_marshal_VOID__ENUM_ENUM,
196 g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
200 empathy_contact_init (EmpathyContact *contact)
202 EmpathyContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (contact,
203 EMPATHY_TYPE_CONTACT, EmpathyContactPriv);
205 contact->priv = priv;
209 contact_finalize (GObject *object)
211 EmpathyContactPriv *priv;
213 priv = GET_PRIV (object);
215 DEBUG ("finalize: %p", object);
219 g_free (priv->presence_message);
222 empathy_avatar_unref (priv->avatar);
225 g_object_unref (priv->account);
227 G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
231 contact_get_property (GObject *object,
236 EmpathyContactPriv *priv;
238 priv = GET_PRIV (object);
243 g_value_set_string (value, priv->id);
246 g_value_set_string (value,
247 empathy_contact_get_name (EMPATHY_CONTACT (object)));
250 g_value_set_boxed (value, priv->avatar);
253 g_value_set_object (value, priv->account);
256 g_value_set_uint (value, priv->presence);
258 case PROP_PRESENCE_MESSAGE:
259 g_value_set_string (value, priv->presence_message);
262 g_value_set_uint (value, priv->handle);
264 case PROP_CAPABILITIES:
265 g_value_set_flags (value, priv->capabilities);
268 g_value_set_boolean (value, priv->is_user);
271 g_value_set_flags (value, priv->ready);
274 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
280 contact_set_property (GObject *object,
285 EmpathyContactPriv *priv;
287 priv = GET_PRIV (object);
292 empathy_contact_set_id (EMPATHY_CONTACT (object),
293 g_value_get_string (value));
296 empathy_contact_set_name (EMPATHY_CONTACT (object),
297 g_value_get_string (value));
300 empathy_contact_set_avatar (EMPATHY_CONTACT (object),
301 g_value_get_boxed (value));
304 empathy_contact_set_account (EMPATHY_CONTACT (object),
305 MC_ACCOUNT (g_value_get_object (value)));
308 empathy_contact_set_presence (EMPATHY_CONTACT (object),
309 g_value_get_uint (value));
311 case PROP_PRESENCE_MESSAGE:
312 empathy_contact_set_presence_message (EMPATHY_CONTACT (object),
313 g_value_get_string (value));
316 empathy_contact_set_handle (EMPATHY_CONTACT (object),
317 g_value_get_uint (value));
319 case PROP_CAPABILITIES:
320 empathy_contact_set_capabilities (EMPATHY_CONTACT (object),
321 g_value_get_flags (value));
324 empathy_contact_set_is_user (EMPATHY_CONTACT (object),
325 g_value_get_boolean (value));
328 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
334 contact_set_ready_flag (EmpathyContact *contact,
335 EmpathyContactReady flag)
337 EmpathyContactPriv *priv = GET_PRIV (contact);
339 if (!(priv->ready & flag))
342 g_object_notify (G_OBJECT (contact), "ready");
347 empathy_contact_new (McAccount *account)
349 return g_object_new (EMPATHY_TYPE_CONTACT,
355 empathy_contact_new_full (McAccount *account,
359 return g_object_new (EMPATHY_TYPE_CONTACT,
367 empathy_contact_get_id (EmpathyContact *contact)
369 EmpathyContactPriv *priv;
371 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
373 priv = GET_PRIV (contact);
379 empathy_contact_set_id (EmpathyContact *contact,
382 EmpathyContactPriv *priv;
384 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
385 g_return_if_fail (id != NULL);
387 priv = GET_PRIV (contact);
389 /* We temporally ref the contact because it could be destroyed
390 * during the signal emition */
391 g_object_ref (contact);
392 if (tp_strdiff (id, priv->id))
395 priv->id = g_strdup (id);
397 g_object_notify (G_OBJECT (contact), "id");
398 if (G_STR_EMPTY (priv->name))
399 g_object_notify (G_OBJECT (contact), "name");
401 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_ID);
403 g_object_unref (contact);
407 empathy_contact_get_name (EmpathyContact *contact)
409 EmpathyContactPriv *priv;
411 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
413 priv = GET_PRIV (contact);
415 if (G_STR_EMPTY (priv->name))
416 return empathy_contact_get_id (contact);
422 empathy_contact_set_name (EmpathyContact *contact,
425 EmpathyContactPriv *priv;
427 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
429 priv = GET_PRIV (contact);
431 g_object_ref (contact);
432 if (tp_strdiff (name, priv->name))
435 priv->name = g_strdup (name);
436 g_object_notify (G_OBJECT (contact), "name");
438 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_NAME);
439 g_object_unref (contact);
443 empathy_contact_get_avatar (EmpathyContact *contact)
445 EmpathyContactPriv *priv;
447 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
449 priv = GET_PRIV (contact);
455 empathy_contact_set_avatar (EmpathyContact *contact,
456 EmpathyAvatar *avatar)
458 EmpathyContactPriv *priv;
460 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
462 priv = GET_PRIV (contact);
464 if (priv->avatar == avatar)
469 empathy_avatar_unref (priv->avatar);
474 priv->avatar = empathy_avatar_ref (avatar);
476 g_object_notify (G_OBJECT (contact), "avatar");
480 empathy_contact_get_account (EmpathyContact *contact)
482 EmpathyContactPriv *priv;
484 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
486 priv = GET_PRIV (contact);
488 return priv->account;
492 empathy_contact_set_account (EmpathyContact *contact,
495 EmpathyContactPriv *priv;
497 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
498 g_return_if_fail (MC_IS_ACCOUNT (account));
500 priv = GET_PRIV (contact);
502 if (account == priv->account)
506 g_object_unref (priv->account);
507 priv->account = g_object_ref (account);
509 g_object_notify (G_OBJECT (contact), "account");
513 empathy_contact_get_presence (EmpathyContact *contact)
515 EmpathyContactPriv *priv;
517 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), MC_PRESENCE_UNSET);
519 priv = GET_PRIV (contact);
521 return priv->presence;
525 empathy_contact_set_presence (EmpathyContact *contact,
528 EmpathyContactPriv *priv;
529 McPresence old_presence;
531 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
533 priv = GET_PRIV (contact);
535 if (presence == priv->presence)
538 old_presence = priv->presence;
539 priv->presence = presence;
541 g_signal_emit (contact, signals[PRESENCE_CHANGED], 0, presence, old_presence);
543 g_object_notify (G_OBJECT (contact), "presence");
547 empathy_contact_get_presence_message (EmpathyContact *contact)
549 EmpathyContactPriv *priv;
551 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
553 priv = GET_PRIV (contact);
555 return priv->presence_message;
559 empathy_contact_set_presence_message (EmpathyContact *contact,
560 const gchar *message)
562 EmpathyContactPriv *priv = GET_PRIV (contact);
564 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
566 if (!tp_strdiff (message, priv->presence_message))
569 g_free (priv->presence_message);
570 priv->presence_message = g_strdup (message);
572 g_object_notify (G_OBJECT (contact), "presence-message");
576 empathy_contact_get_handle (EmpathyContact *contact)
578 EmpathyContactPriv *priv;
580 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
582 priv = GET_PRIV (contact);
588 empathy_contact_set_handle (EmpathyContact *contact,
591 EmpathyContactPriv *priv;
593 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
595 priv = GET_PRIV (contact);
597 g_object_ref (contact);
598 if (handle != priv->handle)
600 priv->handle = handle;
601 g_object_notify (G_OBJECT (contact), "handle");
603 contact_set_ready_flag (contact, EMPATHY_CONTACT_READY_HANDLE);
604 g_object_unref (contact);
608 empathy_contact_get_capabilities (EmpathyContact *contact)
610 EmpathyContactPriv *priv;
612 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
614 priv = GET_PRIV (contact);
616 return priv->capabilities;
620 empathy_contact_set_capabilities (EmpathyContact *contact,
621 EmpathyCapabilities capabilities)
623 EmpathyContactPriv *priv;
625 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
627 priv = GET_PRIV (contact);
629 if (priv->capabilities == capabilities)
632 priv->capabilities = capabilities;
634 g_object_notify (G_OBJECT (contact), "capabilities");
638 empathy_contact_is_user (EmpathyContact *contact)
640 EmpathyContactPriv *priv;
642 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
644 priv = GET_PRIV (contact);
646 return priv->is_user;
650 empathy_contact_set_is_user (EmpathyContact *contact,
653 EmpathyContactPriv *priv;
655 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
657 priv = GET_PRIV (contact);
659 if (priv->is_user == is_user)
662 priv->is_user = is_user;
664 g_object_notify (G_OBJECT (contact), "is-user");
668 empathy_contact_is_online (EmpathyContact *contact)
670 EmpathyContactPriv *priv;
672 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
674 priv = GET_PRIV (contact);
676 return (priv->presence > MC_PRESENCE_OFFLINE);
680 empathy_contact_get_status (EmpathyContact *contact)
682 EmpathyContactPriv *priv;
684 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
686 priv = GET_PRIV (contact);
688 if (priv->presence_message)
689 return priv->presence_message;
691 return empathy_presence_get_default_message (priv->presence);
695 empathy_contact_can_voip (EmpathyContact *contact)
697 EmpathyContactPriv *priv;
699 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
701 priv = GET_PRIV (contact);
703 return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
704 EMPATHY_CAPABILITIES_VIDEO);
708 empathy_contact_can_send_files (EmpathyContact *contact)
710 EmpathyContactPriv *priv;
712 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
714 priv = GET_PRIV (contact);
716 return priv->capabilities & EMPATHY_CAPABILITIES_FT;
720 empathy_contact_get_ready (EmpathyContact *contact)
722 EmpathyContactPriv *priv;
724 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
726 priv = GET_PRIV (contact);
732 empathy_contact_equal (gconstpointer v1,
735 McAccount *account_a;
736 McAccount *account_b;
740 g_return_val_if_fail (EMPATHY_IS_CONTACT (v1), FALSE);
741 g_return_val_if_fail (EMPATHY_IS_CONTACT (v2), FALSE);
743 account_a = empathy_contact_get_account (EMPATHY_CONTACT (v1));
744 account_b = empathy_contact_get_account (EMPATHY_CONTACT (v2));
746 id_a = empathy_contact_get_id (EMPATHY_CONTACT (v1));
747 id_b = empathy_contact_get_id (EMPATHY_CONTACT (v2));
749 return empathy_account_equal (account_a, account_b) &&
750 !tp_strdiff (id_a, id_b);
754 empathy_contact_hash (gconstpointer key)
756 EmpathyContactPriv *priv;
758 g_return_val_if_fail (EMPATHY_IS_CONTACT (key), +1);
760 priv = GET_PRIV (EMPATHY_CONTACT (key));
764 priv->hash = empathy_account_hash (priv->account) ^
765 g_str_hash (priv->id);
772 contact_is_ready_func (GObject *contact,
775 EmpathyContactPriv *priv = GET_PRIV (contact);
776 EmpathyContactReady ready;
778 ready = GPOINTER_TO_UINT (user_data);
780 /* When the name is NULL, empathy_contact_get_name() fallback to the id.
781 * When the caller want to wait the name to be ready, it also want to wait
782 * the id to be ready in case of fallback. */
783 if ((ready & EMPATHY_CONTACT_READY_NAME) && G_STR_EMPTY (priv->name))
784 ready |= EMPATHY_CONTACT_READY_ID;
786 return (priv->ready & ready) == ready;
790 empathy_contact_run_until_ready (EmpathyContact *contact,
791 EmpathyContactReady ready,
794 empathy_run_until_ready_full (contact, "notify::ready",
795 contact_is_ready_func, GUINT_TO_POINTER (ready),
800 contact_get_avatar_filename (EmpathyContact *contact,
803 EmpathyContactPriv *priv = GET_PRIV (contact);
806 gchar *token_escaped;
807 gchar *contact_escaped;
809 if (G_STR_EMPTY (priv->id))
812 contact_escaped = tp_escape_as_identifier (priv->id);
813 token_escaped = tp_escape_as_identifier (token);
815 avatar_path = g_build_filename (g_get_user_cache_dir (),
818 mc_account_get_unique_name (priv->account),
821 g_mkdir_with_parents (avatar_path, 0700);
823 avatar_file = g_build_filename (avatar_path, token_escaped, NULL);
825 g_free (contact_escaped);
826 g_free (token_escaped);
827 g_free (avatar_path);
833 empathy_contact_load_avatar_data (EmpathyContact *contact,
839 EmpathyAvatar *avatar;
841 GError *error = NULL;
843 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
844 g_return_if_fail (data != NULL);
845 g_return_if_fail (len > 0);
846 g_return_if_fail (format != NULL);
847 g_return_if_fail (!G_STR_EMPTY (token));
849 /* Load and set the avatar */
850 avatar = empathy_avatar_new (g_memdup (data, len), len, g_strdup (format),
852 empathy_contact_set_avatar (contact, avatar);
853 empathy_avatar_unref (avatar);
855 /* Save to cache if not yet in it */
856 filename = contact_get_avatar_filename (contact, token);
857 if (filename && !g_file_test (filename, G_FILE_TEST_EXISTS))
859 if (!empathy_avatar_save_to_file (avatar, filename, &error))
861 DEBUG ("Failed to save avatar in cache: %s",
862 error ? error->message : "No error given");
863 g_clear_error (&error);
866 DEBUG ("Avatar saved to %s", filename);
872 empathy_contact_load_avatar_cache (EmpathyContact *contact,
875 EmpathyAvatar *avatar = NULL;
879 GError *error = NULL;
881 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
882 g_return_val_if_fail (!G_STR_EMPTY (token), FALSE);
884 /* Load the avatar from file if it exists */
885 filename = contact_get_avatar_filename (contact, token);
886 if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
888 if (!g_file_get_contents (filename, &data, &len, &error))
890 DEBUG ("Failed to load avatar from cache: %s",
891 error ? error->message : "No error given");
892 g_clear_error (&error);
898 DEBUG ("Avatar loaded from %s", filename);
899 avatar = empathy_avatar_new (data, len, NULL, g_strdup (token));
900 empathy_contact_set_avatar (contact, avatar);
901 empathy_avatar_unref (avatar);
910 empathy_avatar_get_type (void)
912 static GType type_id = 0;
916 type_id = g_boxed_type_register_static ("EmpathyAvatar",
917 (GBoxedCopyFunc) empathy_avatar_ref,
918 (GBoxedFreeFunc) empathy_avatar_unref);
925 empathy_avatar_new (guchar *data,
930 EmpathyAvatar *avatar;
932 avatar = g_slice_new0 (EmpathyAvatar);
935 avatar->format = format;
936 avatar->token = token;
937 avatar->refcount = 1;
943 empathy_avatar_unref (EmpathyAvatar *avatar)
945 g_return_if_fail (avatar != NULL);
948 if (avatar->refcount == 0)
950 g_free (avatar->data);
951 g_free (avatar->format);
952 g_free (avatar->token);
953 g_slice_free (EmpathyAvatar, avatar);
958 empathy_avatar_ref (EmpathyAvatar *avatar)
960 g_return_val_if_fail (avatar != NULL, NULL);
968 * empathy_avatar_save_to_file:
969 * @avatar: the avatar
970 * @filename: name of a file to write avatar to
971 * @error: return location for a GError, or NULL
973 * Save the avatar to a file named filename
975 * Returns: %TRUE on success, %FALSE if an error occurred
978 empathy_avatar_save_to_file (EmpathyAvatar *self,
979 const gchar *filename,
982 return g_file_set_contents (filename, self->data, self->len, error);