1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Xavier Claessens <xclaesse@gmail.com>
4 * Copyright (C) 2007 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: Xavier Claessens <xclaesse@gmail.com>
28 #include <libtelepathy/tp-helpers.h>
29 #include <libtelepathy/tp-conn.h>
30 #include <libtelepathy/tp-chan.h>
31 #include <libtelepathy/tp-chan-type-contact-list-gen.h>
32 #include <libtelepathy/tp-conn-iface-aliasing-gen.h>
33 #include <libtelepathy/tp-conn-iface-presence-gen.h>
34 #include <libtelepathy/tp-conn-iface-avatars-gen.h>
36 #include "empathy-tp-contact-list.h"
37 #include "empathy-contact-list.h"
38 #include "gossip-telepathy-group.h"
39 #include "gossip-debug.h"
40 #include "gossip-utils.h"
42 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
43 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv))
45 #define DEBUG_DOMAIN "TpContactList"
46 #define MAX_AVATAR_REQUESTS 10
48 struct _EmpathyTpContactListPriv {
52 GossipContact *user_contact;
55 GossipTelepathyGroup *publish;
56 GossipTelepathyGroup *subscribe;
62 DBusGProxy *aliasing_iface;
63 DBusGProxy *avatars_iface;
64 DBusGProxy *presence_iface;
66 GList *avatar_requests_queue;
70 TP_CONTACT_LIST_TYPE_PUBLISH,
71 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
72 TP_CONTACT_LIST_TYPE_UNKNOWN,
73 TP_CONTACT_LIST_TYPE_COUNT
82 EmpathyTpContactList *list;
84 } TpContactListAvatarRequestData;
87 EmpathyTpContactList *list;
89 } TpContactListAliasesRequestData;
91 static void empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass);
92 static void tp_contact_list_iface_init (EmpathyContactListIface *iface);
93 static void empathy_tp_contact_list_init (EmpathyTpContactList *list);
94 static void tp_contact_list_finalize (GObject *object);
95 static void tp_contact_list_finalize_proxies (EmpathyTpContactList *list);
96 static void tp_contact_list_setup (EmpathyContactList *list);
97 static GossipContact * tp_contact_list_find (EmpathyContactList *list,
99 static void tp_contact_list_add (EmpathyContactList *list,
100 GossipContact *contact,
101 const gchar *message);
102 static void tp_contact_list_remove (EmpathyContactList *list,
103 GossipContact *contact,
104 const gchar *message);
105 static GList * tp_contact_list_get_members (EmpathyContactList *list);
106 static GList * tp_contact_list_get_local_pending (EmpathyContactList *list);
107 static void tp_contact_list_remove_local_pending (EmpathyTpContactList *list,
108 GossipContact *contact);
109 static void tp_contact_list_contact_removed_foreach (guint handle,
110 GossipContact *contact,
111 EmpathyTpContactList *list);
112 static void tp_contact_list_destroy_cb (DBusGProxy *proxy,
113 EmpathyTpContactList *list);
114 static gboolean tp_contact_list_find_foreach (guint handle,
115 GossipContact *contact,
117 static void tp_contact_list_newchannel_cb (DBusGProxy *proxy,
118 const gchar *object_path,
119 const gchar *channel_type,
120 TelepathyHandleType handle_type,
121 guint channel_handle,
122 gboolean suppress_handle,
123 EmpathyTpContactList *list);
124 static TpContactListType tp_contact_list_get_type (EmpathyTpContactList *list,
125 GossipTelepathyGroup *group);
126 static void tp_contact_list_added_cb (GossipTelepathyGroup *group,
130 const gchar *message,
131 EmpathyTpContactList *list);
132 static void tp_contact_list_removed_cb (GossipTelepathyGroup *group,
136 const gchar *message,
137 EmpathyTpContactList *list);
138 static void tp_contact_list_pending_cb (GossipTelepathyGroup *group,
142 const gchar *message,
143 EmpathyTpContactList *list);
144 static void tp_contact_list_groups_updated_cb (GossipContact *contact,
146 EmpathyTpContactList *list);
147 static void tp_contact_list_name_updated_cb (GossipContact *contact,
149 EmpathyTpContactList *list);
150 static void tp_contact_list_update_groups_foreach (gchar *object_path,
151 GossipTelepathyGroup *group,
152 TpContactListData *data);
153 static GossipTelepathyGroup * tp_contact_list_get_group (EmpathyTpContactList *list,
155 static gboolean tp_contact_list_find_group (gchar *key,
156 GossipTelepathyGroup *group,
158 static void tp_contact_list_get_groups_foreach (gchar *key,
159 GossipTelepathyGroup *group,
161 static void tp_contact_list_group_channel_closed_cb (TpChan *channel,
162 EmpathyTpContactList *list);
163 static void tp_contact_list_group_members_added_cb (GossipTelepathyGroup *group,
167 const gchar *message,
168 EmpathyTpContactList *list);
169 static void tp_contact_list_group_members_removed_cb (GossipTelepathyGroup *group,
173 const gchar *message,
174 EmpathyTpContactList *list);
175 static void tp_contact_list_get_members_foreach (guint handle,
176 GossipContact *contact,
178 static void tp_contact_list_get_info (EmpathyTpContactList *list,
180 static void tp_contact_list_request_avatar (EmpathyTpContactList *list,
182 static void tp_contact_list_start_avatar_requests (EmpathyTpContactList *list);
183 static void tp_contact_list_avatar_update_cb (DBusGProxy *proxy,
186 EmpathyTpContactList *list);
187 static void tp_contact_list_request_avatar_cb (DBusGProxy *proxy,
191 TpContactListAvatarRequestData *data);
192 static void tp_contact_list_aliases_update_cb (DBusGProxy *proxy,
194 EmpathyTpContactList *list);
195 static void tp_contact_list_request_aliases_cb (DBusGProxy *proxy,
196 gchar **contact_names,
198 TpContactListAliasesRequestData *data);
199 static void tp_contact_list_presence_update_cb (DBusGProxy *proxy,
200 GHashTable *handle_table,
201 EmpathyTpContactList *list);
202 static void tp_contact_list_parse_presence_foreach (guint handle,
203 GValueArray *presence_struct,
204 EmpathyTpContactList *list);
205 static void tp_contact_list_presences_table_foreach (const gchar *state_str,
206 GHashTable *presences_table,
207 GossipPresence **presence);
208 static void tp_contact_list_status_changed_cb (MissionControl *mc,
209 TelepathyConnectionStatus status,
211 TelepathyConnectionStatusReason reason,
212 const gchar *unique_name,
213 EmpathyTpContactList *list);
220 static guint signals[LAST_SIGNAL];
221 static guint n_avatar_requests = 0;
223 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
224 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
225 tp_contact_list_iface_init));
228 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
230 GObjectClass *object_class = G_OBJECT_CLASS (klass);
232 object_class->finalize = tp_contact_list_finalize;
235 g_signal_new ("destroy",
236 G_TYPE_FROM_CLASS (klass),
240 g_cclosure_marshal_VOID__VOID,
244 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
248 tp_contact_list_iface_init (EmpathyContactListIface *iface)
250 iface->setup = tp_contact_list_setup;
251 iface->find = tp_contact_list_find;
252 iface->add = tp_contact_list_add;
253 iface->remove = tp_contact_list_remove;
254 iface->get_members = tp_contact_list_get_members;
255 iface->get_local_pending = tp_contact_list_get_local_pending;
259 empathy_tp_contact_list_init (EmpathyTpContactList *list)
261 EmpathyTpContactListPriv *priv;
263 priv = GET_PRIV (list);
265 priv->groups = g_hash_table_new_full (g_str_hash,
267 (GDestroyNotify) g_free,
268 (GDestroyNotify) g_object_unref);
269 priv->contacts = g_hash_table_new_full (g_direct_hash,
272 (GDestroyNotify) g_object_unref);
276 tp_contact_list_finalize (GObject *object)
278 EmpathyTpContactListPriv *priv;
279 EmpathyTpContactList *list;
281 list = EMPATHY_TP_CONTACT_LIST (object);
282 priv = GET_PRIV (list);
284 gossip_debug (DEBUG_DOMAIN, "finalize: %p", object);
286 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
287 "AccountStatusChanged",
288 G_CALLBACK (tp_contact_list_status_changed_cb),
291 tp_contact_list_finalize_proxies (list);
294 g_object_unref (priv->tp_conn);
296 if (priv->subscribe) {
297 g_object_unref (priv->subscribe);
300 g_object_unref (priv->publish);
303 g_object_unref (priv->account);
304 g_object_unref (priv->user_contact);
305 g_object_unref (priv->mc);
306 g_hash_table_destroy (priv->groups);
307 g_hash_table_destroy (priv->contacts);
309 g_list_foreach (priv->local_pending, (GFunc) empathy_contact_list_info_free, NULL);
310 g_list_free (priv->local_pending);
312 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
315 EmpathyTpContactList *
316 empathy_tp_contact_list_new (McAccount *account)
318 EmpathyTpContactListPriv *priv;
319 EmpathyTpContactList *list;
322 GError *error = NULL;
324 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
326 mc = gossip_mission_control_new ();
328 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
329 /* The account is not connected, nothing to do. */
333 list = g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST, NULL);
334 priv = GET_PRIV (list);
336 priv->tp_conn = mission_control_get_connection (mc, account, NULL);
337 priv->account = g_object_ref (account);
340 g_signal_connect (priv->tp_conn, "destroy",
341 G_CALLBACK (tp_contact_list_destroy_cb),
343 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
344 "AccountStatusChanged",
345 G_CALLBACK (tp_contact_list_status_changed_cb),
348 priv->aliasing_iface = tp_conn_get_interface (priv->tp_conn,
349 TELEPATHY_CONN_IFACE_ALIASING_QUARK);
350 priv->avatars_iface = tp_conn_get_interface (priv->tp_conn,
351 TELEPATHY_CONN_IFACE_AVATARS_QUARK);
352 priv->presence_iface = tp_conn_get_interface (priv->tp_conn,
353 TELEPATHY_CONN_IFACE_PRESENCE_QUARK);
355 if (priv->aliasing_iface) {
356 dbus_g_proxy_connect_signal (priv->aliasing_iface,
358 G_CALLBACK (tp_contact_list_aliases_update_cb),
362 if (priv->avatars_iface) {
363 dbus_g_proxy_connect_signal (priv->avatars_iface,
365 G_CALLBACK (tp_contact_list_avatar_update_cb),
369 if (priv->presence_iface) {
370 dbus_g_proxy_connect_signal (priv->presence_iface,
372 G_CALLBACK (tp_contact_list_presence_update_cb),
376 /* Get our own handle and contact */
377 if (!tp_conn_get_self_handle (DBUS_G_PROXY (priv->tp_conn),
379 gossip_debug (DEBUG_DOMAIN, "GetSelfHandle Error: %s",
380 error ? error->message : "No error given");
381 g_clear_error (&error);
383 priv->user_contact = empathy_tp_contact_list_get_from_handle (list, handle);
384 gossip_contact_set_is_user (priv->user_contact, TRUE);
391 tp_contact_list_setup (EmpathyContactList *list)
393 EmpathyTpContactListPriv *priv;
395 GError *error = NULL;
398 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
400 priv = GET_PRIV (list);
402 gossip_debug (DEBUG_DOMAIN, "setup contact list: %p", list);
405 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
406 G_CALLBACK (tp_contact_list_newchannel_cb),
409 /* Get existing channels */
410 if (!tp_conn_list_channels (DBUS_G_PROXY (priv->tp_conn),
413 gossip_debug (DEBUG_DOMAIN,
414 "Failed to get list of open channels: %s",
415 error ? error->message : "No error given");
416 g_clear_error (&error);
420 for (i = 0; channels->len > i; i++) {
421 GValueArray *chan_struct;
422 const gchar *object_path;
423 const gchar *chan_iface;
424 TelepathyHandleType handle_type;
427 chan_struct = g_ptr_array_index (channels, i);
428 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
429 chan_iface = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
430 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
431 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
433 tp_contact_list_newchannel_cb (DBUS_G_PROXY (priv->tp_conn),
434 object_path, chan_iface,
437 EMPATHY_TP_CONTACT_LIST (list));
439 g_value_array_free (chan_struct);
442 g_ptr_array_free (channels, TRUE);
445 static GossipContact *
446 tp_contact_list_find (EmpathyContactList *list,
449 EmpathyTpContactListPriv *priv;
451 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
453 priv = GET_PRIV (list);
455 return g_hash_table_find (priv->contacts,
456 (GHRFunc) tp_contact_list_find_foreach,
461 tp_contact_list_add (EmpathyContactList *list,
462 GossipContact *contact,
463 const gchar *message)
465 EmpathyTpContactListPriv *priv;
468 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
470 priv = GET_PRIV (list);
472 handle = gossip_contact_get_handle (contact);
473 gossip_telepathy_group_add_member (priv->subscribe, handle, message);
474 gossip_telepathy_group_add_member (priv->publish, handle, message);
478 tp_contact_list_remove (EmpathyContactList *list,
479 GossipContact *contact,
480 const gchar *message)
482 EmpathyTpContactListPriv *priv;
485 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
487 priv = GET_PRIV (list);
489 handle = gossip_contact_get_handle (contact);
490 gossip_telepathy_group_remove_member (priv->subscribe, handle, message);
491 gossip_telepathy_group_remove_member (priv->publish, handle, message);
495 tp_contact_list_get_members (EmpathyContactList *list)
497 EmpathyTpContactListPriv *priv;
498 GList *contacts = NULL;
500 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
502 priv = GET_PRIV (list);
504 g_hash_table_foreach (priv->contacts,
505 (GHFunc) tp_contact_list_get_members_foreach,
512 tp_contact_list_get_local_pending (EmpathyContactList *list)
514 EmpathyTpContactListPriv *priv;
516 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
518 priv = GET_PRIV (list);
520 return g_list_copy (priv->local_pending);
524 empathy_tp_contact_list_get_account (EmpathyTpContactList *list)
526 EmpathyTpContactListPriv *priv;
528 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
530 priv = GET_PRIV (list);
532 return priv->account;
536 empathy_tp_contact_list_get_user (EmpathyTpContactList *list)
538 EmpathyTpContactListPriv *priv;
540 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
542 priv = GET_PRIV (list);
544 return priv->user_contact;
548 empathy_tp_contact_list_get_from_id (EmpathyTpContactList *list,
551 EmpathyTpContactListPriv *priv;
552 GossipContact *contact;
553 const gchar *contact_ids[] = {id, NULL};
556 GError *error = NULL;
558 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
559 g_return_val_if_fail (id != NULL, NULL);
561 priv = GET_PRIV (list);
563 contact = tp_contact_list_find (EMPATHY_CONTACT_LIST (list), id);
565 return g_object_ref (contact);
568 /* The id is unknown, requests a new handle */
569 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
570 TP_HANDLE_TYPE_CONTACT,
573 gossip_debug (DEBUG_DOMAIN,
574 "RequestHandle for %s failed: %s", id,
575 error ? error->message : "No error given");
576 g_clear_error (&error);
580 handle = g_array_index(handles, guint, 0);
581 g_array_free (handles, TRUE);
583 return empathy_tp_contact_list_get_from_handle (list, handle);
587 empathy_tp_contact_list_get_from_handle (EmpathyTpContactList *list,
590 GossipContact *contact;
594 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
596 handles = g_array_new (FALSE, FALSE, sizeof (guint));
597 g_array_append_val (handles, handle);
599 contacts = empathy_tp_contact_list_get_from_handles (list, handles);
600 g_array_free (handles, TRUE);
606 contact = contacts->data;
607 g_list_free (contacts);
613 empathy_tp_contact_list_get_from_handles (EmpathyTpContactList *list,
616 EmpathyTpContactListPriv *priv;
617 gchar **handles_names;
620 GList *contacts = NULL;
622 GError *error = NULL;
624 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
625 g_return_val_if_fail (handles != NULL, NULL);
627 priv = GET_PRIV (list);
629 /* Search all handles we already have */
630 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
631 for (i = 0; i < handles->len; i++) {
632 GossipContact *contact;
635 handle = g_array_index (handles, guint, i);
641 contact = g_hash_table_lookup (priv->contacts,
642 GUINT_TO_POINTER (handle));
645 contacts = g_list_prepend (contacts,
646 g_object_ref (contact));
648 g_array_append_val (new_handles, handle);
652 if (new_handles->len == 0) {
653 g_array_free (new_handles, TRUE);
657 /* Holds all handles we don't have yet.
658 * FIXME: We should release them at some point. */
659 if (!tp_conn_hold_handles (DBUS_G_PROXY (priv->tp_conn),
660 TP_HANDLE_TYPE_CONTACT,
661 new_handles, &error)) {
662 gossip_debug (DEBUG_DOMAIN,
663 "HoldHandles Error: %s",
664 error ? error->message : "No error given");
665 g_clear_error (&error);
666 g_array_free (new_handles, TRUE);
670 /* Get the IDs of all new handles */
671 if (!tp_conn_inspect_handles (DBUS_G_PROXY (priv->tp_conn),
672 TP_HANDLE_TYPE_CONTACT,
676 gossip_debug (DEBUG_DOMAIN,
677 "InspectHandle Error: %s",
678 error ? error->message : "No error given");
679 g_clear_error (&error);
680 g_array_free (new_handles, TRUE);
684 /* Create contact objects */
685 for (i = 0, id = handles_names; *id && i < new_handles->len; id++, i++) {
686 GossipContact *contact;
689 handle = g_array_index (new_handles, guint, i);
690 contact = g_object_new (GOSSIP_TYPE_CONTACT,
691 "account", priv->account,
696 if (!priv->presence_iface) {
697 GossipPresence *presence;
699 /* We have no presence iface, set default presence
701 presence = gossip_presence_new_full (MC_PRESENCE_AVAILABLE,
704 gossip_contact_set_presence (contact, presence);
705 g_object_unref (presence);
708 g_signal_connect (contact, "notify::groups",
709 G_CALLBACK (tp_contact_list_groups_updated_cb),
711 g_signal_connect (contact, "notify::name",
712 G_CALLBACK (tp_contact_list_name_updated_cb),
715 gossip_debug (DEBUG_DOMAIN, "new contact created: %s (%d)",
718 g_hash_table_insert (priv->contacts,
719 GUINT_TO_POINTER (handle),
722 contacts = g_list_prepend (contacts, g_object_ref (contact));
725 tp_contact_list_get_info (list, new_handles);
727 g_array_free (new_handles, TRUE);
728 g_strfreev (handles_names);
734 empathy_tp_contact_list_rename_group (EmpathyTpContactList *list,
735 const gchar *old_group,
736 const gchar *new_group)
738 EmpathyTpContactListPriv *priv;
739 GossipTelepathyGroup *group;
742 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
743 g_return_if_fail (old_group != NULL);
744 g_return_if_fail (new_group != NULL);
746 priv = GET_PRIV (list);
748 group = g_hash_table_find (priv->groups,
749 (GHRFunc) tp_contact_list_find_group,
752 /* The group doesn't exists on this account */
756 gossip_debug (DEBUG_DOMAIN, "rename group %s to %s", group, new_group);
758 /* Remove all members from the old group */
759 members = gossip_telepathy_group_get_members (group);
760 gossip_telepathy_group_remove_members (group, members, "");
761 tp_contact_list_group_members_removed_cb (group, members,
763 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
765 g_hash_table_remove (priv->groups,
766 gossip_telepathy_group_get_object_path (group));
768 /* Add all members to the new group */
769 group = tp_contact_list_get_group (list, new_group);
771 gossip_telepathy_group_add_members (group, members, "");
776 empathy_tp_contact_list_get_groups (EmpathyTpContactList *list)
778 EmpathyTpContactListPriv *priv;
779 GList *groups = NULL;
781 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
783 priv = GET_PRIV (list);
785 g_hash_table_foreach (priv->groups,
786 (GHFunc) tp_contact_list_get_groups_foreach,
789 groups = g_list_sort (groups, (GCompareFunc) strcmp);
795 tp_contact_list_finalize_proxies (EmpathyTpContactList *list)
797 EmpathyTpContactListPriv *priv;
799 priv = GET_PRIV (list);
802 g_signal_handlers_disconnect_by_func (priv->tp_conn,
803 tp_contact_list_destroy_cb,
805 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
806 G_CALLBACK (tp_contact_list_newchannel_cb),
810 if (priv->aliasing_iface) {
811 dbus_g_proxy_disconnect_signal (priv->aliasing_iface,
813 G_CALLBACK (tp_contact_list_aliases_update_cb),
817 if (priv->avatars_iface) {
818 dbus_g_proxy_disconnect_signal (priv->avatars_iface,
820 G_CALLBACK (tp_contact_list_avatar_update_cb),
824 if (priv->presence_iface) {
825 dbus_g_proxy_disconnect_signal (priv->presence_iface,
827 G_CALLBACK (tp_contact_list_presence_update_cb),
833 tp_contact_list_destroy_cb (DBusGProxy *proxy,
834 EmpathyTpContactList *list)
836 EmpathyTpContactListPriv *priv;
838 priv = GET_PRIV (list);
840 gossip_debug (DEBUG_DOMAIN, "Connection destroyed... "
841 "Account disconnected or CM crashed");
843 /* DBus proxies should NOT be used anymore */
844 g_object_unref (priv->tp_conn);
845 priv->tp_conn = NULL;
846 priv->aliasing_iface = NULL;
847 priv->avatars_iface = NULL;
848 priv->presence_iface = NULL;
850 /* Remove all contacts */
851 g_hash_table_foreach (priv->contacts,
852 (GHFunc) tp_contact_list_contact_removed_foreach,
854 g_hash_table_remove_all (priv->contacts);
856 /* Tell the world to not use us anymore */
857 g_signal_emit (list, signals[DESTROY], 0);
861 tp_contact_list_contact_removed_foreach (guint handle,
862 GossipContact *contact,
863 EmpathyTpContactList *list)
865 g_signal_handlers_disconnect_by_func (contact,
866 tp_contact_list_groups_updated_cb,
868 g_signal_handlers_disconnect_by_func (contact,
869 tp_contact_list_name_updated_cb,
872 g_signal_emit_by_name (list, "contact-removed", contact);
876 tp_contact_list_block_contact (EmpathyTpContactList *list,
877 GossipContact *contact)
879 g_signal_handlers_block_by_func (contact,
880 tp_contact_list_groups_updated_cb,
882 g_signal_handlers_block_by_func (contact,
883 tp_contact_list_name_updated_cb,
888 tp_contact_list_unblock_contact (EmpathyTpContactList *list,
889 GossipContact *contact)
891 g_signal_handlers_unblock_by_func (contact,
892 tp_contact_list_groups_updated_cb,
894 g_signal_handlers_unblock_by_func (contact,
895 tp_contact_list_name_updated_cb,
900 tp_contact_list_find_foreach (guint handle,
901 GossipContact *contact,
904 if (strcmp (gossip_contact_get_id (contact), id) == 0) {
912 tp_contact_list_newchannel_cb (DBusGProxy *proxy,
913 const gchar *object_path,
914 const gchar *channel_type,
915 TelepathyHandleType handle_type,
916 guint channel_handle,
917 gboolean suppress_handle,
918 EmpathyTpContactList *list)
920 EmpathyTpContactListPriv *priv;
921 GossipTelepathyGroup *group;
923 const gchar *bus_name;
926 priv = GET_PRIV (list);
928 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) != 0 ||
934 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn));
935 new_chan = tp_chan_new (tp_get_bus (),
938 channel_type, handle_type, channel_handle);
940 if (handle_type == TP_HANDLE_TYPE_LIST) {
941 TpContactListType list_type;
943 group = gossip_telepathy_group_new (new_chan, priv->tp_conn);
945 list_type = tp_contact_list_get_type (list, group);
946 if (list_type == TP_CONTACT_LIST_TYPE_UNKNOWN) {
947 gossip_debug (DEBUG_DOMAIN,
948 "Type of contact list channel unknown: %s",
949 gossip_telepathy_group_get_name (group));
951 g_object_unref (new_chan);
952 g_object_unref (group);
955 gossip_debug (DEBUG_DOMAIN,
956 "New contact list channel of type: %d",
960 g_signal_connect (group, "members-added",
961 G_CALLBACK (tp_contact_list_added_cb),
963 g_signal_connect (group, "members-removed",
964 G_CALLBACK (tp_contact_list_removed_cb),
967 members = gossip_telepathy_group_get_members (group);
968 tp_contact_list_added_cb (group, members, 0,
969 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
971 g_array_free (members, TRUE);
973 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
977 g_object_unref (priv->publish);
979 priv->publish = group;
981 /* Makes no sense to be in remote-pending */
982 g_signal_connect (group, "local-pending",
983 G_CALLBACK (tp_contact_list_pending_cb),
986 pendings = gossip_telepathy_group_get_local_pending_members_with_info (group);
990 pending = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
991 for (l = pendings; l; l = l->next) {
992 GossipTpGroupInfo *info;
996 g_array_insert_val (pending, 0, info->member);
997 tp_contact_list_pending_cb (group, pending,
1003 g_array_free (pending, TRUE);
1004 gossip_telepathy_group_info_list_free (pendings);
1007 else if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1008 if (priv->subscribe) {
1009 g_object_unref (priv->subscribe);
1011 priv->subscribe = group;
1013 g_assert_not_reached ();
1016 else if (handle_type == TP_HANDLE_TYPE_GROUP) {
1017 const gchar *object_path;
1019 object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (new_chan));
1020 if (g_hash_table_lookup (priv->groups, object_path)) {
1021 g_object_unref (new_chan);
1025 group = gossip_telepathy_group_new (new_chan, priv->tp_conn);
1027 gossip_debug (DEBUG_DOMAIN, "New server-side group channel: %s",
1028 gossip_telepathy_group_get_name (group));
1030 dbus_g_proxy_connect_signal (DBUS_G_PROXY (new_chan), "Closed",
1032 (tp_contact_list_group_channel_closed_cb),
1035 g_hash_table_insert (priv->groups, g_strdup (object_path), group);
1036 g_signal_connect (group, "members-added",
1037 G_CALLBACK (tp_contact_list_group_members_added_cb),
1039 g_signal_connect (group, "members-removed",
1040 G_CALLBACK (tp_contact_list_group_members_removed_cb),
1043 members = gossip_telepathy_group_get_members (group);
1044 tp_contact_list_group_members_added_cb (group, members, 0,
1045 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1047 g_array_free (members, TRUE);
1050 g_object_unref (new_chan);
1053 static TpContactListType
1054 tp_contact_list_get_type (EmpathyTpContactList *list,
1055 GossipTelepathyGroup *group)
1057 EmpathyTpContactListPriv *priv;
1058 TpContactListType list_type;
1061 priv = GET_PRIV (list);
1063 name = gossip_telepathy_group_get_name (group);
1064 if (strcmp (name, "subscribe") == 0) {
1065 list_type = TP_CONTACT_LIST_TYPE_SUBSCRIBE;
1066 } else if (strcmp (name, "publish") == 0) {
1067 list_type = TP_CONTACT_LIST_TYPE_PUBLISH;
1069 list_type = TP_CONTACT_LIST_TYPE_UNKNOWN;
1076 tp_contact_list_added_cb (GossipTelepathyGroup *group,
1080 const gchar *message,
1081 EmpathyTpContactList *list)
1083 EmpathyTpContactListPriv *priv;
1084 GList *added_list, *l;
1085 TpContactListType list_type;
1087 priv = GET_PRIV (list);
1089 list_type = tp_contact_list_get_type (list, group);
1091 added_list = empathy_tp_contact_list_get_from_handles (list, handles);
1092 for (l = added_list; l; l = l->next) {
1093 GossipContact *contact;
1094 GossipSubscription subscription;
1096 contact = GOSSIP_CONTACT (l->data);
1098 gossip_debug (DEBUG_DOMAIN, "Contact '%s' added to list type %d",
1099 gossip_contact_get_name (contact),
1102 subscription = gossip_contact_get_subscription (contact);
1103 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1104 subscription |= GOSSIP_SUBSCRIPTION_FROM;
1106 else if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1107 subscription |= GOSSIP_SUBSCRIPTION_TO;
1110 tp_contact_list_block_contact (list, contact);
1111 gossip_contact_set_subscription (contact, subscription);
1112 tp_contact_list_unblock_contact (list, contact);
1114 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1115 tp_contact_list_remove_local_pending (list, contact);
1116 g_signal_emit_by_name (list, "contact-added", contact);
1119 g_object_unref (contact);
1122 g_list_free (added_list);
1126 tp_contact_list_removed_cb (GossipTelepathyGroup *group,
1130 const gchar *message,
1131 EmpathyTpContactList *list)
1133 EmpathyTpContactListPriv *priv;
1134 GList *removed_list, *l;
1135 TpContactListType list_type;
1137 priv = GET_PRIV (list);
1139 list_type = tp_contact_list_get_type (list, group);
1141 removed_list = empathy_tp_contact_list_get_from_handles (list, handles);
1142 for (l = removed_list; l; l = l->next) {
1143 GossipContact *contact;
1144 GossipSubscription subscription;
1146 contact = GOSSIP_CONTACT (l->data);
1148 gossip_debug (DEBUG_DOMAIN, "Contact '%s' removed from list type %d",
1149 gossip_contact_get_name (contact),
1152 subscription = gossip_contact_get_subscription (contact);
1153 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1154 subscription &= !GOSSIP_SUBSCRIPTION_FROM;
1156 else if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1157 subscription &= !GOSSIP_SUBSCRIPTION_TO;
1160 tp_contact_list_block_contact (list, contact);
1161 gossip_contact_set_subscription (contact, subscription);
1162 tp_contact_list_unblock_contact (list, contact);
1164 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1165 tp_contact_list_remove_local_pending (list, contact);
1166 g_signal_emit_by_name (list, "contact-removed", contact);
1168 g_object_unref (contact);
1171 g_list_free (removed_list);
1175 tp_contact_list_pending_cb (GossipTelepathyGroup *group,
1179 const gchar *message,
1180 EmpathyTpContactList *list)
1182 EmpathyTpContactListPriv *priv;
1183 GList *pending_list, *l;
1184 TpContactListType list_type;
1186 priv = GET_PRIV (list);
1188 list_type = tp_contact_list_get_type (list, group);
1190 pending_list = empathy_tp_contact_list_get_from_handles (list, handles);
1191 for (l = pending_list; l; l = l->next) {
1192 GossipContact *contact;
1194 contact = GOSSIP_CONTACT (l->data);
1196 gossip_debug (DEBUG_DOMAIN, "Contact '%s' pending in list type %d",
1197 gossip_contact_get_name (contact),
1200 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1201 EmpathyContactListInfo *info;
1203 info = empathy_contact_list_info_new (contact, message);
1204 priv->local_pending = g_list_prepend (priv->local_pending,
1207 g_signal_emit_by_name (list, "local-pending",
1211 g_object_unref (contact);
1214 g_list_free (pending_list);
1218 tp_contact_list_remove_local_pending (EmpathyTpContactList *list,
1219 GossipContact *contact)
1221 EmpathyTpContactListPriv *priv;
1224 priv = GET_PRIV (list);
1226 for (l = priv->local_pending; l; l = l->next) {
1227 EmpathyContactListInfo *info;
1230 if (gossip_contact_equal (contact, info->contact)) {
1231 gossip_debug (DEBUG_DOMAIN, "Contact no more local-pending: %s",
1232 gossip_contact_get_name (contact));
1234 priv->local_pending = g_list_delete_link (priv->local_pending, l);
1235 empathy_contact_list_info_free (info);
1242 tp_contact_list_groups_updated_cb (GossipContact *contact,
1244 EmpathyTpContactList *list)
1246 EmpathyTpContactListPriv *priv;
1247 TpContactListData data;
1250 priv = GET_PRIV (list);
1252 /* Make sure all groups are created */
1253 groups = gossip_contact_get_groups (contact);
1254 for (l = groups; l; l = l->next) {
1255 tp_contact_list_get_group (list, l->data);
1258 data.handle = gossip_contact_get_handle (contact);
1259 data.new_groups = groups;
1261 g_hash_table_foreach (priv->groups,
1262 (GHFunc) tp_contact_list_update_groups_foreach,
1267 tp_contact_list_name_updated_cb (GossipContact *contact,
1269 EmpathyTpContactList *list)
1271 EmpathyTpContactListPriv *priv;
1272 GHashTable *new_alias;
1273 const gchar *new_name;
1275 GError *error = NULL;
1277 priv = GET_PRIV (list);
1279 if (!priv->aliasing_iface) {
1283 handle = gossip_contact_get_handle (contact);
1284 new_name = gossip_contact_get_name (contact);
1286 gossip_debug (DEBUG_DOMAIN, "renaming handle %d to %s",
1289 new_alias = g_hash_table_new_full (g_direct_hash,
1294 g_hash_table_insert (new_alias,
1295 GUINT_TO_POINTER (handle),
1296 g_strdup (new_name));
1298 if (!tp_conn_iface_aliasing_set_aliases (priv->aliasing_iface,
1301 gossip_debug (DEBUG_DOMAIN,
1302 "Couldn't rename contact: %s",
1303 error ? error->message : "No error given");
1304 g_clear_error (&error);
1307 g_hash_table_destroy (new_alias);
1311 tp_contact_list_update_groups_foreach (gchar *object_path,
1312 GossipTelepathyGroup *group,
1313 TpContactListData *data)
1316 gboolean found = FALSE;
1317 const gchar *group_name;
1320 is_member = gossip_telepathy_group_is_member (group, data->handle);
1321 group_name = gossip_telepathy_group_get_name (group);
1323 for (l = data->new_groups; l; l = l->next) {
1324 if (strcmp (group_name, l->data) == 0) {
1330 if (is_member && !found) {
1331 /* We are no longer member of this group */
1332 gossip_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1333 data->handle, group_name);
1334 gossip_telepathy_group_remove_member (group, data->handle, "");
1337 if (!is_member && found) {
1338 /* We are now member of this group */
1339 gossip_debug (DEBUG_DOMAIN, "Contact %d added to group '%s'",
1340 data->handle, group_name);
1341 gossip_telepathy_group_add_member (group, data->handle, "");
1345 static GossipTelepathyGroup *
1346 tp_contact_list_get_group (EmpathyTpContactList *list,
1349 EmpathyTpContactListPriv *priv;
1350 GossipTelepathyGroup *group;
1351 TpChan *group_channel;
1354 char *group_object_path;
1355 const char *names[2] = {name, NULL};
1356 GError *error = NULL;
1358 priv = GET_PRIV (list);
1360 group = g_hash_table_find (priv->groups,
1361 (GHRFunc) tp_contact_list_find_group,
1367 gossip_debug (DEBUG_DOMAIN, "creating new group: %s", name);
1369 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
1370 TP_HANDLE_TYPE_GROUP,
1374 gossip_debug (DEBUG_DOMAIN,
1375 "Couldn't request the creation of a new handle for group: %s",
1376 error ? error->message : "No error given");
1377 g_clear_error (&error);
1380 group_handle = g_array_index (handles, guint, 0);
1381 g_array_free (handles, TRUE);
1383 if (!tp_conn_request_channel (DBUS_G_PROXY (priv->tp_conn),
1384 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1385 TP_HANDLE_TYPE_GROUP,
1390 gossip_debug (DEBUG_DOMAIN,
1391 "Couldn't request the creation of a new group channel: %s",
1392 error ? error->message : "No error given");
1393 g_clear_error (&error);
1397 group_channel = tp_chan_new (tp_get_bus (),
1398 dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn)),
1400 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1401 TP_HANDLE_TYPE_GROUP,
1404 dbus_g_proxy_connect_signal (DBUS_G_PROXY (group_channel),
1407 (tp_contact_list_group_channel_closed_cb),
1411 group = gossip_telepathy_group_new (group_channel, priv->tp_conn);
1412 g_hash_table_insert (priv->groups, group_object_path, group);
1413 g_signal_connect (group, "members-added",
1414 G_CALLBACK (tp_contact_list_group_members_added_cb),
1416 g_signal_connect (group, "members-removed",
1417 G_CALLBACK (tp_contact_list_group_members_removed_cb),
1424 tp_contact_list_find_group (gchar *key,
1425 GossipTelepathyGroup *group,
1428 if (strcmp (group_name, gossip_telepathy_group_get_name (group)) == 0) {
1436 tp_contact_list_get_groups_foreach (gchar *key,
1437 GossipTelepathyGroup *group,
1442 name = gossip_telepathy_group_get_name (group);
1443 *groups = g_list_append (*groups, g_strdup (name));
1447 tp_contact_list_group_channel_closed_cb (TpChan *channel,
1448 EmpathyTpContactList *list)
1450 EmpathyTpContactListPriv *priv;
1452 priv = GET_PRIV (list);
1454 g_hash_table_remove (priv->groups,
1455 dbus_g_proxy_get_path (DBUS_G_PROXY (channel)));
1459 tp_contact_list_group_members_added_cb (GossipTelepathyGroup *group,
1463 const gchar *message,
1464 EmpathyTpContactList *list)
1466 EmpathyTpContactListPriv *priv;
1467 GList *added_list, *l;
1468 const gchar *group_name;
1470 priv = GET_PRIV (list);
1472 group_name = gossip_telepathy_group_get_name (group);
1473 added_list = empathy_tp_contact_list_get_from_handles (list, members);
1475 for (l = added_list; l; l = l->next) {
1476 GossipContact *contact;
1478 contact = GOSSIP_CONTACT (l->data);
1480 tp_contact_list_block_contact (list, contact);
1481 gossip_contact_add_group (contact, group_name);
1482 tp_contact_list_unblock_contact (list, contact);
1484 g_object_unref (contact);
1487 g_list_free (added_list);
1491 tp_contact_list_group_members_removed_cb (GossipTelepathyGroup *group,
1495 const gchar *message,
1496 EmpathyTpContactList *list)
1498 EmpathyTpContactListPriv *priv;
1499 GList *removed_list, *l;
1500 const gchar *group_name;
1502 priv = GET_PRIV (list);
1504 group_name = gossip_telepathy_group_get_name (group);
1505 removed_list = empathy_tp_contact_list_get_from_handles (list, members);
1507 for (l = removed_list; l; l = l->next) {
1508 GossipContact *contact;
1512 tp_contact_list_block_contact (list, contact);
1513 gossip_contact_remove_group (contact, group_name);
1514 tp_contact_list_unblock_contact (list, contact);
1516 g_object_unref (contact);
1519 g_list_free (removed_list);
1523 tp_contact_list_get_members_foreach (guint handle,
1524 GossipContact *contact,
1527 GossipSubscription subscription;
1529 subscription = gossip_contact_get_subscription (contact);
1530 if (subscription & GOSSIP_SUBSCRIPTION_TO) {
1531 *contacts = g_list_append (*contacts, g_object_ref (contact));
1536 tp_contact_list_get_info (EmpathyTpContactList *list,
1539 EmpathyTpContactListPriv *priv;
1540 GError *error = NULL;
1542 priv = GET_PRIV (list);
1544 if (priv->presence_iface) {
1545 /* FIXME: We should use GetPresence instead */
1546 if (!tp_conn_iface_presence_request_presence (priv->presence_iface,
1548 gossip_debug (DEBUG_DOMAIN,
1549 "Could not request presences: %s",
1550 error ? error->message : "No error given");
1551 g_clear_error (&error);
1555 if (priv->aliasing_iface) {
1556 TpContactListAliasesRequestData *data;
1558 data = g_slice_new (TpContactListAliasesRequestData);
1560 data->handles = g_memdup (handles->data, handles->len * sizeof (guint));
1562 tp_conn_iface_aliasing_request_aliases_async (priv->aliasing_iface,
1564 (tp_conn_iface_aliasing_request_aliases_reply)
1565 tp_contact_list_request_aliases_cb,
1569 if (priv->avatars_iface) {
1572 for (i = 0; i < handles->len; i++) {
1575 handle = g_array_index (handles, gint, i);
1576 tp_contact_list_request_avatar (list, handle);
1582 tp_contact_list_request_avatar (EmpathyTpContactList *list,
1585 EmpathyTpContactListPriv *priv;
1587 priv = GET_PRIV (list);
1589 /* We queue avatar requests to not send too many dbus async
1590 * calls at once. If we don't we reach the dbus's limit of
1592 priv->avatar_requests_queue = g_list_append (priv->avatar_requests_queue,
1593 GUINT_TO_POINTER (handle));
1594 tp_contact_list_start_avatar_requests (list);
1598 tp_contact_list_start_avatar_requests (EmpathyTpContactList *list)
1600 EmpathyTpContactListPriv *priv;
1601 TpContactListAvatarRequestData *data;
1603 priv = GET_PRIV (list);
1605 while (n_avatar_requests < MAX_AVATAR_REQUESTS &&
1606 priv->avatar_requests_queue) {
1607 data = g_slice_new (TpContactListAvatarRequestData);
1609 data->handle = GPOINTER_TO_UINT (priv->avatar_requests_queue->data);
1611 n_avatar_requests++;
1612 priv->avatar_requests_queue = g_list_remove (priv->avatar_requests_queue,
1613 priv->avatar_requests_queue->data);
1615 tp_conn_iface_avatars_request_avatar_async (priv->avatars_iface,
1617 (tp_conn_iface_avatars_request_avatar_reply)
1618 tp_contact_list_request_avatar_cb,
1624 tp_contact_list_avatar_update_cb (DBusGProxy *proxy,
1627 EmpathyTpContactList *list)
1629 EmpathyTpContactListPriv *priv;
1631 priv = GET_PRIV (list);
1633 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1634 /* We don't know this contact, skip */
1638 gossip_debug (DEBUG_DOMAIN, "Changing avatar for %d to %s",
1641 tp_contact_list_request_avatar (list, handle);
1645 tp_contact_list_request_avatar_cb (DBusGProxy *proxy,
1646 GArray *avatar_data,
1649 TpContactListAvatarRequestData *data)
1651 GossipContact *contact;
1653 contact = empathy_tp_contact_list_get_from_handle (data->list, data->handle);
1656 gossip_debug (DEBUG_DOMAIN, "Error requesting avatar for %s: %s",
1657 gossip_contact_get_name (contact),
1658 error ? error->message : "No error given");
1660 GossipAvatar *avatar;
1662 avatar = gossip_avatar_new (avatar_data->data,
1665 tp_contact_list_block_contact (data->list, contact);
1666 gossip_contact_set_avatar (contact, avatar);
1667 tp_contact_list_unblock_contact (data->list, contact);
1668 gossip_avatar_unref (avatar);
1671 n_avatar_requests--;
1672 tp_contact_list_start_avatar_requests (data->list);
1674 g_object_unref (contact);
1675 g_slice_free (TpContactListAvatarRequestData, data);
1679 tp_contact_list_aliases_update_cb (DBusGProxy *proxy,
1680 GPtrArray *renamed_handlers,
1681 EmpathyTpContactList *list)
1683 EmpathyTpContactListPriv *priv;
1686 priv = GET_PRIV (list);
1688 for (i = 0; renamed_handlers->len > i; i++) {
1691 GValueArray *renamed_struct;
1692 GossipContact *contact;
1694 renamed_struct = g_ptr_array_index (renamed_handlers, i);
1695 handle = g_value_get_uint(g_value_array_get_nth (renamed_struct, 0));
1696 alias = g_value_get_string(g_value_array_get_nth (renamed_struct, 1));
1698 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1699 /* We don't know this contact, skip */
1703 if (G_STR_EMPTY (alias)) {
1707 contact = empathy_tp_contact_list_get_from_handle (list, handle);
1708 tp_contact_list_block_contact (list, contact);
1709 gossip_contact_set_name (contact, alias);
1710 tp_contact_list_unblock_contact (list, contact);
1711 g_object_unref (contact);
1713 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (update cb)",
1719 tp_contact_list_request_aliases_cb (DBusGProxy *proxy,
1720 gchar **contact_names,
1722 TpContactListAliasesRequestData *data)
1728 gossip_debug (DEBUG_DOMAIN, "Error requesting aliases: %s",
1732 for (name = contact_names; *name && !error; name++) {
1733 GossipContact *contact;
1735 contact = empathy_tp_contact_list_get_from_handle (data->list,
1737 tp_contact_list_block_contact (data->list, contact);
1738 gossip_contact_set_name (contact, *name);
1739 tp_contact_list_unblock_contact (data->list, contact);
1740 g_object_unref (contact);
1742 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (request cb)",
1743 data->handles[i], *name);
1748 g_free (data->handles);
1749 g_slice_free (TpContactListAliasesRequestData, data);
1753 tp_contact_list_presence_update_cb (DBusGProxy *proxy,
1754 GHashTable *handle_table,
1755 EmpathyTpContactList *list)
1757 g_hash_table_foreach (handle_table,
1758 (GHFunc) tp_contact_list_parse_presence_foreach,
1763 tp_contact_list_parse_presence_foreach (guint handle,
1764 GValueArray *presence_struct,
1765 EmpathyTpContactList *list)
1767 EmpathyTpContactListPriv *priv;
1768 GHashTable *presences_table;
1769 GossipContact *contact;
1770 GossipPresence *presence = NULL;
1772 priv = GET_PRIV (list);
1774 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1775 /* We don't know this contact, skip */
1779 contact = empathy_tp_contact_list_get_from_handle (list, handle);
1780 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
1782 g_hash_table_foreach (presences_table,
1783 (GHFunc) tp_contact_list_presences_table_foreach,
1786 gossip_debug (DEBUG_DOMAIN, "Presence changed for %s (%d) to %s (%d)",
1787 gossip_contact_get_name (contact),
1789 presence ? gossip_presence_get_status (presence) : "unset",
1790 presence ? gossip_presence_get_state (presence) : MC_PRESENCE_UNSET);
1792 tp_contact_list_block_contact (list, contact);
1793 gossip_contact_set_presence (contact, presence);
1794 tp_contact_list_unblock_contact (list, contact);
1796 g_object_unref (contact);
1800 tp_contact_list_presences_table_foreach (const gchar *state_str,
1801 GHashTable *presences_table,
1802 GossipPresence **presence)
1805 const GValue *message;
1807 state = gossip_presence_state_from_str (state_str);
1808 if ((state == MC_PRESENCE_UNSET) || (state == MC_PRESENCE_OFFLINE)) {
1813 g_object_unref (*presence);
1817 *presence = gossip_presence_new ();
1818 gossip_presence_set_state (*presence, state);
1820 message = g_hash_table_lookup (presences_table, "message");
1821 if (message != NULL) {
1822 gossip_presence_set_status (*presence,
1823 g_value_get_string (message));
1828 tp_contact_list_status_changed_cb (MissionControl *mc,
1829 TelepathyConnectionStatus status,
1830 McPresence presence,
1831 TelepathyConnectionStatusReason reason,
1832 const gchar *unique_name,
1833 EmpathyTpContactList *list)
1835 EmpathyTpContactListPriv *priv;
1838 priv = GET_PRIV (list);
1840 account = mc_account_lookup (unique_name);
1841 if (status != TP_CONN_STATUS_DISCONNECTED ||
1842 !gossip_account_equal (account, priv->account) ||
1844 g_object_unref (account);
1848 /* We are disconnected, do just like if the connection was destroyed */
1849 g_signal_handlers_disconnect_by_func (priv->tp_conn,
1850 tp_contact_list_destroy_cb,
1852 tp_contact_list_destroy_cb (DBUS_G_PROXY (priv->tp_conn), list);
1854 g_object_unref (account);