1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <telepathy-glib/util.h>
27 #include <telepathy-glib/connection.h>
28 #include <libmissioncontrol/mission-control.h>
30 #include "empathy-tp-contact-factory.h"
31 #include "empathy-utils.h"
33 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
34 #include "empathy-debug.h"
36 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
37 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv))
39 struct _EmpathyTpContactFactoryPriv {
42 TpConnection *connection;
50 static void empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass);
51 static void empathy_tp_contact_factory_init (EmpathyTpContactFactory *factory);
53 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
61 static EmpathyContact *
62 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
65 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
68 for (l = priv->contacts; l; l = l->next) {
69 if (empathy_contact_get_handle (l->data) == handle) {
77 static EmpathyContact *
78 tp_contact_factory_find_by_id (EmpathyTpContactFactory *tp_factory,
81 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
84 for (l = priv->contacts; l; l = l->next) {
85 if (!tp_strdiff (empathy_contact_get_id (l->data), id)) {
94 tp_contact_factory_weak_notify (gpointer data,
95 GObject *where_the_object_was)
97 EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
99 DEBUG ("Remove finalized contact %p", where_the_object_was);
101 priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
105 tp_contact_factory_presences_table_foreach (const gchar *state_str,
106 GHashTable *presences_table,
107 EmpathyContact *contact)
109 const GValue *message;
110 const gchar *message_str = NULL;
112 empathy_contact_set_presence (contact,
113 empathy_presence_from_str (state_str));
115 message = g_hash_table_lookup (presences_table, "message");
117 message_str = g_value_get_string (message);
120 if (!G_STR_EMPTY (message_str)) {
121 empathy_contact_set_presence_message (contact, message_str);
123 empathy_contact_set_presence_message (contact, NULL);
128 tp_contact_factory_parse_presence_foreach (guint handle,
129 GValueArray *presence_struct,
130 EmpathyTpContactFactory *tp_factory)
132 GHashTable *presences_table;
133 EmpathyContact *contact;
135 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
140 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
142 g_hash_table_foreach (presences_table,
143 (GHFunc) tp_contact_factory_presences_table_foreach,
146 DEBUG ("Changing presence for contact %s (%d) to '%s' (%d)",
147 empathy_contact_get_id (contact),
149 empathy_contact_get_presence_message (contact),
150 empathy_contact_get_presence (contact));
154 tp_contact_factory_get_presence_cb (TpConnection *connection,
155 GHashTable *handle_table,
161 DEBUG ("Error getting presence: %s", error->message);
162 if (error->domain == TP_DBUS_ERRORS &&
163 error->code == TP_DBUS_ERROR_NO_INTERFACE) {
164 guint *handles = user_data;
166 /* We have no presence iface, set default presence
168 while (*handles != 0) {
169 EmpathyContact *contact;
171 contact = tp_contact_factory_find_by_handle (
172 (EmpathyTpContactFactory*) tp_factory,
175 empathy_contact_set_presence (contact,
176 MC_PRESENCE_AVAILABLE);
186 g_hash_table_foreach (handle_table,
187 (GHFunc) tp_contact_factory_parse_presence_foreach,
188 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
192 tp_contact_factory_presence_update_cb (TpConnection *connection,
193 GHashTable *handle_table,
197 g_hash_table_foreach (handle_table,
198 (GHFunc) tp_contact_factory_parse_presence_foreach,
199 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
203 tp_contact_factory_set_aliases_cb (TpConnection *connection,
209 DEBUG ("Error setting alias: %s", error->message);
214 tp_contact_factory_request_aliases_cb (TpConnection *connection,
215 const gchar **contact_names,
220 guint *handles = user_data;
225 DEBUG ("Error requesting aliases: %s", error->message);
227 /* If we failed to get alias set it to NULL, like that if
228 * someone is waiting for the name to be ready it won't wait
230 while (*handles != 0) {
231 EmpathyContact *contact;
233 contact = tp_contact_factory_find_by_handle (
234 (EmpathyTpContactFactory*) tp_factory,
237 empathy_contact_set_name (contact, NULL);
245 for (name = contact_names; *name; name++) {
246 EmpathyContact *contact;
248 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
254 DEBUG ("Renaming contact %s (%d) to %s (request cb)",
255 empathy_contact_get_id (contact),
256 empathy_contact_get_handle (contact),
259 empathy_contact_set_name (contact, *name);
266 tp_contact_factory_aliases_changed_cb (TpConnection *connection,
267 const GPtrArray *renamed_handlers,
269 GObject *weak_object)
271 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
274 for (i = 0; renamed_handlers->len > i; i++) {
277 GValueArray *renamed_struct;
278 EmpathyContact *contact;
280 renamed_struct = g_ptr_array_index (renamed_handlers, i);
281 handle = g_value_get_uint (g_value_array_get_nth (renamed_struct, 0));
282 alias = g_value_get_string (g_value_array_get_nth (renamed_struct, 1));
283 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
286 /* We don't know this contact, skip */
290 DEBUG ("Renaming contact %s (%d) to %s (changed cb)",
291 empathy_contact_get_id (contact),
294 empathy_contact_set_name (contact, alias);
299 tp_contact_factory_set_avatar_cb (TpConnection *connection,
306 DEBUG ("Error setting avatar: %s", error->message);
311 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
317 DEBUG ("Error clearing avatar: %s", error->message);
322 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
325 const GArray *avatar_data,
326 const gchar *mime_type,
330 EmpathyContact *contact;
331 EmpathyAvatar *avatar;
333 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
339 DEBUG ("Avatar retrieved for contact %s (%d)",
340 empathy_contact_get_id (contact),
343 avatar = empathy_avatar_new (avatar_data->data,
348 empathy_contact_set_avatar (contact, avatar);
349 empathy_avatar_unref (avatar);
353 tp_contact_factory_request_avatars_cb (TpConnection *connection,
359 DEBUG ("Error requesting avatars: %s", error->message);
364 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
368 EmpathyContact *contact;
369 EmpathyAvatar *avatar;
371 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
376 /* Check if we have an avatar */
377 if (G_STR_EMPTY (token)) {
378 empathy_contact_set_avatar (contact, NULL);
382 /* Check if the avatar changed */
383 avatar = empathy_contact_get_avatar (contact);
384 if (avatar && !tp_strdiff (avatar->token, token)) {
388 /* The avatar changed, search the new one in the cache */
389 avatar = empathy_avatar_new_from_cache (token);
391 /* Got from cache, use it */
392 empathy_contact_set_avatar (contact, avatar);
393 empathy_avatar_unref (avatar);
397 /* Avatar is not up-to-date, we have to request it. */
402 EmpathyTpContactFactory *tp_factory;
407 tp_contact_factory_avatar_tokens_foreach (gpointer key,
411 TokensData *data = user_data;
412 const gchar *token = value;
413 guint handle = GPOINTER_TO_UINT (key);
415 if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
417 g_array_append_val (data->handles, handle);
422 tp_contact_factory_get_known_avatar_tokens_cb (TpConnection *connection,
431 DEBUG ("Error getting known avatars tokens: %s", error->message);
435 data.tp_factory = EMPATHY_TP_CONTACT_FACTORY (tp_factory);
436 data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
437 g_hash_table_foreach (tokens,
438 tp_contact_factory_avatar_tokens_foreach,
441 DEBUG ("Got %d tokens, need to request %d avatars",
442 g_hash_table_size (tokens), data.handles->len);
444 /* Request needed avatars */
445 if (data.handles->len > 0) {
446 tp_cli_connection_interface_avatars_call_request_avatars (connection,
449 tp_contact_factory_request_avatars_cb,
454 g_array_free (data.handles, TRUE);
458 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
460 const gchar *new_token,
466 if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
467 handle, new_token)) {
468 /* Avatar was cached, nothing to do */
472 DEBUG ("Need to request avatar for token %s", new_token);
474 handles = g_array_new (FALSE, FALSE, sizeof (guint));
475 g_array_append_val (handles, handle);
477 tp_cli_connection_interface_avatars_call_request_avatars (connection,
480 tp_contact_factory_request_avatars_cb,
483 g_array_free (handles, TRUE);
487 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
489 const gchar *channel_type,
493 EmpathyContact *contact;
494 EmpathyCapabilities capabilities;
496 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
501 capabilities = empathy_contact_get_capabilities (contact);
502 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
504 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
505 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
506 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
507 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
508 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
510 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
511 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
515 DEBUG ("Changing capabilities for contact %s (%d) to %d",
516 empathy_contact_get_id (contact),
517 empathy_contact_get_handle (contact),
520 empathy_contact_set_capabilities (contact, capabilities);
524 tp_contact_factory_get_capabilities_cb (TpConnection *connection,
525 const GPtrArray *capabilities,
528 GObject *weak_object)
530 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
534 DEBUG ("Error getting capabilities: %s", error->message);
535 /* FIXME Should set the capabilities of the contacts for which this request
536 * originated to NONE */
540 for (i = 0; i < capabilities->len; i++) {
543 const gchar *channel_type;
547 values = g_ptr_array_index (capabilities, i);
548 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
549 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
550 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
551 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
553 tp_contact_factory_update_capabilities (tp_factory,
562 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
563 const GPtrArray *capabilities,
565 GObject *weak_object)
567 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
570 for (i = 0; i < capabilities->len; i++) {
573 const gchar *channel_type;
577 values = g_ptr_array_index (capabilities, i);
578 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
579 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
580 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
581 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
583 tp_contact_factory_update_capabilities (tp_factory,
592 tp_contact_factory_request_everything (EmpathyTpContactFactory *tp_factory,
593 const GArray *handles)
595 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
598 g_return_if_fail (priv->ready);
600 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
601 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
602 tp_cli_connection_interface_presence_call_get_presence (priv->connection,
605 tp_contact_factory_get_presence_cb,
607 G_OBJECT (tp_factory));
609 /* FIXME: Sometimes the dbus call timesout because CM takes
610 * too much time to request all aliases from the server,
611 * that's why we increase the timeout here. See fd.o bug #14795 */
612 dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
613 g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
614 tp_cli_connection_interface_aliasing_call_request_aliases (priv->connection,
617 tp_contact_factory_request_aliases_cb,
619 G_OBJECT (tp_factory));
621 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (priv->connection,
624 tp_contact_factory_get_known_avatar_tokens_cb,
626 G_OBJECT (tp_factory));
628 tp_cli_connection_interface_capabilities_call_get_capabilities (priv->connection,
631 tp_contact_factory_get_capabilities_cb,
633 G_OBJECT (tp_factory));
637 tp_contact_factory_list_free (gpointer data)
641 g_list_foreach (l, (GFunc) g_object_unref, NULL);
646 tp_contact_factory_request_handles_cb (TpConnection *connection,
647 const GArray *handles,
652 GList *contacts = user_data;
657 DEBUG ("Failed to request handles: %s", error->message);
661 for (l = contacts; l; l = l->next) {
664 handle = g_array_index (handles, guint, i);
665 empathy_contact_set_handle (l->data, handle);
670 tp_contact_factory_request_everything (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
675 tp_contact_factory_inspect_handles_cb (TpConnection *connection,
682 GList *contacts = user_data;
686 DEBUG ("Failed to inspect handles: %s", error->message);
691 for (l = contacts; l; l = l->next) {
692 empathy_contact_set_id (l->data, *id);
698 tp_contact_factory_disconnect_contact_foreach (gpointer data,
701 EmpathyContact *contact = data;
703 empathy_contact_set_presence (contact, MC_PRESENCE_UNSET);
704 empathy_contact_set_handle (contact, 0);
708 tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factory)
710 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
712 DEBUG ("Connection invalidated");
714 g_object_unref (priv->connection);
715 priv->connection = NULL;
717 g_object_notify (G_OBJECT (tp_factory), "ready");
720 g_list_foreach (priv->contacts,
721 tp_contact_factory_disconnect_contact_foreach,
727 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
733 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
735 GArray *handle_needed;
737 GList *handle_needed_contacts = NULL;
738 GList *id_needed_contacts = NULL;
741 DEBUG ("Failed to get self handles: %s", error->message);
745 DEBUG ("Connection ready");
747 empathy_contact_set_handle (priv->user, handle);
749 g_object_notify (tp_factory, "ready");
751 /* Connect signals */
752 tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
753 tp_contact_factory_aliases_changed_cb,
755 G_OBJECT (tp_factory),
757 tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
758 tp_contact_factory_avatar_updated_cb,
760 G_OBJECT (tp_factory),
762 tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
763 tp_contact_factory_avatar_retrieved_cb,
765 G_OBJECT (tp_factory),
767 tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
768 tp_contact_factory_presence_update_cb,
770 G_OBJECT (tp_factory),
772 tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
773 tp_contact_factory_capabilities_changed_cb,
775 G_OBJECT (tp_factory),
778 /* Request needed info for all existing contacts */
779 handle_needed = g_array_new (TRUE, FALSE, sizeof (gchar*));
780 id_needed = g_array_new (FALSE, FALSE, sizeof (guint));
781 for (l = priv->contacts; l; l = l->next) {
782 EmpathyContact *contact;
787 handle = empathy_contact_get_handle (contact);
788 id = empathy_contact_get_id (contact);
790 g_assert (!G_STR_EMPTY (id));
791 g_array_append_val (handle_needed, id);
792 handle_needed_contacts = g_list_prepend (handle_needed_contacts,
793 g_object_ref (contact));
795 if (G_STR_EMPTY (id)) {
796 g_array_append_val (id_needed, handle);
797 id_needed_contacts = g_list_prepend (id_needed_contacts,
798 g_object_ref (contact));
801 handle_needed_contacts = g_list_reverse (handle_needed_contacts);
802 id_needed_contacts = g_list_reverse (id_needed_contacts);
804 tp_cli_connection_call_request_handles (priv->connection,
806 TP_HANDLE_TYPE_CONTACT,
807 (const gchar**) handle_needed->data,
808 tp_contact_factory_request_handles_cb,
809 handle_needed_contacts, tp_contact_factory_list_free,
810 G_OBJECT (tp_factory));
812 tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
814 tp_cli_connection_call_inspect_handles (priv->connection,
816 TP_HANDLE_TYPE_CONTACT,
818 tp_contact_factory_inspect_handles_cb,
819 id_needed_contacts, tp_contact_factory_list_free,
820 G_OBJECT (tp_factory));
822 g_array_free (handle_needed, TRUE);
823 g_array_free (id_needed, TRUE);
827 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
829 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
831 /* Get our own handle */
832 tp_cli_connection_call_get_self_handle (priv->connection,
834 tp_contact_factory_got_self_handle_cb,
836 G_OBJECT (tp_factory));
840 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
842 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
843 gboolean connection_ready;
845 if (priv->connection) {
846 /* We already have our connection object */
850 priv->connection = mission_control_get_tpconnection (priv->mc, priv->account, NULL);
851 if (!priv->connection) {
855 /* We got a new connection, wait for it to be ready */
856 g_signal_connect_swapped (priv->connection, "invalidated",
857 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
860 g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
861 if (connection_ready) {
862 tp_contact_factory_connection_ready_cb (tp_factory);
864 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
865 G_CALLBACK (tp_contact_factory_connection_ready_cb),
871 tp_contact_factory_status_changed_cb (MissionControl *mc,
872 TpConnectionStatus status,
874 TpConnectionStatusReason reason,
875 const gchar *unique_name,
876 EmpathyTpContactFactory *tp_factory)
878 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
881 account = mc_account_lookup (unique_name);
882 if (account && empathy_account_equal (account, priv->account)) {
883 tp_contact_factory_status_updated (tp_factory);
885 g_object_unref (account);
889 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
890 EmpathyContact *contact)
892 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
894 g_object_weak_ref (G_OBJECT (contact),
895 tp_contact_factory_weak_notify,
897 priv->contacts = g_list_prepend (priv->contacts, contact);
899 DEBUG ("Contact added: %s (%d)",
900 empathy_contact_get_id (contact),
901 empathy_contact_get_handle (contact));
905 tp_contact_factory_hold_handles_cb (TpConnection *connection,
911 DEBUG ("Failed to hold handles: %s", error->message);
916 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
918 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
920 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
922 return g_object_ref (priv->user);
926 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
929 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
930 EmpathyContact *contact;
932 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
933 g_return_val_if_fail (id != NULL, NULL);
935 /* Check if the contact already exists */
936 contact = tp_contact_factory_find_by_id (tp_factory, id);
938 return g_object_ref (contact);
941 /* Create new contact */
942 contact = g_object_new (EMPATHY_TYPE_CONTACT,
943 "account", priv->account,
946 tp_contact_factory_add_contact (tp_factory, contact);
949 const gchar *contact_ids[] = {id, NULL};
952 contacts = g_list_prepend (NULL, g_object_ref (contact));
953 tp_cli_connection_call_request_handles (priv->connection,
955 TP_HANDLE_TYPE_CONTACT,
957 tp_contact_factory_request_handles_cb,
958 contacts, tp_contact_factory_list_free,
959 G_OBJECT (tp_factory));
966 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
969 EmpathyContact *contact;
973 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
975 handles = g_array_new (FALSE, FALSE, sizeof (guint));
976 g_array_append_val (handles, handle);
978 contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
979 g_array_free (handles, TRUE);
981 contact = contacts ? contacts->data : NULL;
982 g_list_free (contacts);
988 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
989 const GArray *handles)
991 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
992 GList *contacts = NULL;
994 GList *new_contacts = NULL;
997 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
998 g_return_val_if_fail (handles != NULL, NULL);
1000 /* Search all contacts we already have */
1001 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
1002 for (i = 0; i < handles->len; i++) {
1003 EmpathyContact *contact;
1006 handle = g_array_index (handles, guint, i);
1011 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1013 contacts = g_list_prepend (contacts, g_object_ref (contact));
1015 g_array_append_val (new_handles, handle);
1019 if (new_handles->len == 0) {
1020 g_array_free (new_handles, TRUE);
1024 /* Create new contacts */
1025 for (i = 0; i < new_handles->len; i++) {
1026 EmpathyContact *contact;
1029 handle = g_array_index (new_handles, guint, i);
1031 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1032 "account", priv->account,
1035 tp_contact_factory_add_contact (tp_factory, contact);
1036 contacts = g_list_prepend (contacts, contact);
1037 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1039 new_contacts = g_list_reverse (new_contacts);
1042 /* Get the IDs of all new handles */
1043 tp_cli_connection_call_inspect_handles (priv->connection,
1045 TP_HANDLE_TYPE_CONTACT,
1047 tp_contact_factory_inspect_handles_cb,
1048 new_contacts, tp_contact_factory_list_free,
1049 G_OBJECT (tp_factory));
1051 /* Hold all new handles. */
1052 /* FIXME: Should be unholded when removed from the factory */
1053 tp_cli_connection_call_hold_handles (priv->connection,
1055 TP_HANDLE_TYPE_CONTACT,
1057 tp_contact_factory_hold_handles_cb,
1059 G_OBJECT (tp_factory));
1061 tp_contact_factory_request_everything (tp_factory, new_handles);
1068 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1069 EmpathyContact *contact,
1072 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1073 GHashTable *new_alias;
1076 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1077 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1078 g_return_if_fail (priv->ready);
1079 g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1082 handle = empathy_contact_get_handle (contact);
1084 DEBUG ("Setting alias for contact %s (%d) to %s",
1085 empathy_contact_get_id (contact),
1088 new_alias = g_hash_table_new_full (g_direct_hash,
1093 g_hash_table_insert (new_alias,
1094 GUINT_TO_POINTER (handle),
1097 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1100 tp_contact_factory_set_aliases_cb,
1102 G_OBJECT (tp_factory));
1104 g_hash_table_destroy (new_alias);
1108 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1111 const gchar *mime_type)
1113 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1115 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1116 g_return_if_fail (priv->ready);
1118 if (data && size > 0 && size < G_MAXUINT) {
1121 avatar.data = (gchar*) data;
1124 DEBUG ("Setting avatar on account %s",
1125 mc_account_get_unique_name (priv->account));
1127 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1131 tp_contact_factory_set_avatar_cb,
1133 G_OBJECT (tp_factory));
1135 DEBUG ("Clearing avatar on account %s",
1136 mc_account_get_unique_name (priv->account));
1138 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1140 tp_contact_factory_clear_avatar_cb,
1142 G_OBJECT (tp_factory));
1147 empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
1149 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1151 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1157 tp_contact_factory_get_property (GObject *object,
1162 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1166 g_value_set_object (value, priv->account);
1169 g_value_set_boolean (value, priv->ready);
1172 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1178 tp_contact_factory_set_property (GObject *object,
1180 const GValue *value,
1183 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1187 priv->account = g_object_ref (g_value_get_object (value));
1190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1196 tp_contact_factory_finalize (GObject *object)
1198 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1201 DEBUG ("Finalized: %p (%s)", object,
1202 mc_account_get_normalized_name (priv->account));
1204 empathy_disconnect_account_status_changed (priv->token);
1206 for (l = priv->contacts; l; l = l->next) {
1207 g_object_weak_unref (G_OBJECT (l->data),
1208 tp_contact_factory_weak_notify,
1212 g_list_free (priv->contacts);
1213 g_object_unref (priv->mc);
1214 g_object_unref (priv->account);
1215 g_object_unref (priv->user);
1217 if (priv->connection) {
1218 g_signal_handlers_disconnect_by_func (priv->connection,
1219 tp_contact_factory_connection_invalidated_cb,
1221 g_object_unref (priv->connection);
1224 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1228 tp_contact_factory_constructor (GType type,
1230 GObjectConstructParam *props)
1232 GObject *tp_factory;
1233 EmpathyTpContactFactoryPriv *priv;
1235 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1236 priv = GET_PRIV (tp_factory);
1238 priv->ready = FALSE;
1239 priv->user = empathy_contact_new (priv->account);
1240 empathy_contact_set_is_user (priv->user, TRUE);
1241 tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1242 tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1248 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1250 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1252 object_class->finalize = tp_contact_factory_finalize;
1253 object_class->constructor = tp_contact_factory_constructor;
1254 object_class->get_property = tp_contact_factory_get_property;
1255 object_class->set_property = tp_contact_factory_set_property;
1257 g_object_class_install_property (object_class,
1259 g_param_spec_object ("account",
1260 "Factory's Account",
1261 "The account associated with the factory",
1264 G_PARAM_CONSTRUCT_ONLY));
1265 g_object_class_install_property (object_class,
1267 g_param_spec_boolean ("ready",
1268 "Wheter the factor is ready",
1269 "Is the factory ready",
1273 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1277 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1279 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1281 priv->mc = empathy_mission_control_new ();
1282 priv->token = empathy_connect_to_account_status_changed (priv->mc,
1283 G_CALLBACK (tp_contact_factory_status_changed_cb),
1287 EmpathyTpContactFactory *
1288 empathy_tp_contact_factory_new (McAccount *account)
1290 return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,