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 /* FIXME: this adds the handle to the roster */
384 priv->user_contact = empathy_tp_contact_list_get_from_handle (list, handle);
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);
477 tp_contact_list_remove (EmpathyContactList *list,
478 GossipContact *contact,
479 const gchar *message)
481 EmpathyTpContactListPriv *priv;
484 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
486 priv = GET_PRIV (list);
488 handle = gossip_contact_get_handle (contact);
489 gossip_telepathy_group_remove_member (priv->subscribe, handle, message);
490 gossip_telepathy_group_remove_member (priv->publish, handle, message);
494 tp_contact_list_get_members (EmpathyContactList *list)
496 EmpathyTpContactListPriv *priv;
497 GList *contacts = NULL;
499 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
501 priv = GET_PRIV (list);
503 g_hash_table_foreach (priv->contacts,
504 (GHFunc) tp_contact_list_get_members_foreach,
511 tp_contact_list_get_local_pending (EmpathyContactList *list)
513 EmpathyTpContactListPriv *priv;
515 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
517 priv = GET_PRIV (list);
519 return g_list_copy (priv->local_pending);
523 empathy_tp_contact_list_get_account (EmpathyTpContactList *list)
525 EmpathyTpContactListPriv *priv;
527 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
529 priv = GET_PRIV (list);
531 return priv->account;
535 empathy_tp_contact_list_get_user (EmpathyTpContactList *list)
537 EmpathyTpContactListPriv *priv;
539 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
541 priv = GET_PRIV (list);
543 return priv->user_contact;
547 empathy_tp_contact_list_get_from_id (EmpathyTpContactList *list,
550 EmpathyTpContactListPriv *priv;
551 GossipContact *contact;
552 const gchar *contact_ids[] = {id, NULL};
555 GError *error = NULL;
557 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
558 g_return_val_if_fail (id != NULL, NULL);
560 priv = GET_PRIV (list);
562 contact = tp_contact_list_find (EMPATHY_CONTACT_LIST (list), id);
567 /* The id is unknown, requests a new handle */
568 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
569 TP_HANDLE_TYPE_CONTACT,
572 gossip_debug (DEBUG_DOMAIN,
573 "RequestHandle for %s failed: %s", id,
574 error ? error->message : "No error given");
575 g_clear_error (&error);
579 handle = g_array_index(handles, guint, 0);
580 g_array_free (handles, TRUE);
582 return empathy_tp_contact_list_get_from_handle (list, handle);
586 empathy_tp_contact_list_get_from_handle (EmpathyTpContactList *list,
589 GossipContact *contact;
593 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
595 handles = g_array_new (FALSE, FALSE, sizeof (guint));
596 g_array_append_val (handles, handle);
598 contacts = empathy_tp_contact_list_get_from_handles (list, handles);
599 g_array_free (handles, TRUE);
605 contact = contacts->data;
606 g_list_free (contacts);
612 empathy_tp_contact_list_get_from_handles (EmpathyTpContactList *list,
615 EmpathyTpContactListPriv *priv;
616 gchar **handles_names;
619 GList *contacts = NULL;
621 GError *error = NULL;
623 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
624 g_return_val_if_fail (handles != NULL, NULL);
626 priv = GET_PRIV (list);
628 /* Search all handles we already have */
629 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
630 for (i = 0; i < handles->len; i++) {
631 GossipContact *contact;
634 handle = g_array_index (handles, guint, i);
640 contact = g_hash_table_lookup (priv->contacts,
641 GUINT_TO_POINTER (handle));
644 contacts = g_list_prepend (contacts,
645 g_object_ref (contact));
647 g_array_append_val (new_handles, handle);
651 if (new_handles->len == 0) {
652 g_array_free (new_handles, TRUE);
656 /* Holds all handles we don't have yet.
657 * FIXME: We should release them at some point. */
658 if (!tp_conn_hold_handles (DBUS_G_PROXY (priv->tp_conn),
659 TP_HANDLE_TYPE_CONTACT,
660 new_handles, &error)) {
661 gossip_debug (DEBUG_DOMAIN,
662 "HoldHandles Error: %s",
663 error ? error->message : "No error given");
664 g_clear_error (&error);
665 g_array_free (new_handles, TRUE);
669 /* Get the IDs of all new handles */
670 if (!tp_conn_inspect_handles (DBUS_G_PROXY (priv->tp_conn),
671 TP_HANDLE_TYPE_CONTACT,
675 gossip_debug (DEBUG_DOMAIN,
676 "InspectHandle Error: %s",
677 error ? error->message : "No error given");
678 g_clear_error (&error);
679 g_array_free (new_handles, TRUE);
683 /* Create contact objects */
684 for (i = 0, id = handles_names; *id && i < new_handles->len; id++, i++) {
685 GossipContact *contact;
688 handle = g_array_index (new_handles, guint, i);
689 contact = g_object_new (GOSSIP_TYPE_CONTACT,
690 "account", priv->account,
695 if (!priv->presence_iface) {
696 GossipPresence *presence;
698 /* We have no presence iface, set default presence
700 presence = gossip_presence_new_full (MC_PRESENCE_AVAILABLE,
703 gossip_contact_set_presence (contact, presence);
704 g_object_unref (presence);
707 g_signal_connect (contact, "notify::groups",
708 G_CALLBACK (tp_contact_list_groups_updated_cb),
710 g_signal_connect (contact, "notify::name",
711 G_CALLBACK (tp_contact_list_name_updated_cb),
714 gossip_debug (DEBUG_DOMAIN, "new contact created: %s (%d)",
717 g_hash_table_insert (priv->contacts,
718 GUINT_TO_POINTER (handle),
721 contacts = g_list_prepend (contacts, g_object_ref (contact));
724 tp_contact_list_get_info (list, new_handles);
726 g_array_free (new_handles, TRUE);
727 g_strfreev (handles_names);
733 empathy_tp_contact_list_rename_group (EmpathyTpContactList *list,
734 const gchar *old_group,
735 const gchar *new_group)
737 EmpathyTpContactListPriv *priv;
738 GossipTelepathyGroup *group;
741 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
742 g_return_if_fail (old_group != NULL);
743 g_return_if_fail (new_group != NULL);
745 priv = GET_PRIV (list);
747 group = g_hash_table_find (priv->groups,
748 (GHRFunc) tp_contact_list_find_group,
751 /* The group doesn't exists on this account */
755 gossip_debug (DEBUG_DOMAIN, "rename group %s to %s", group, new_group);
757 /* Remove all members from the old group */
758 members = gossip_telepathy_group_get_members (group);
759 gossip_telepathy_group_remove_members (group, members, "");
760 tp_contact_list_group_members_removed_cb (group, members,
762 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
764 g_hash_table_remove (priv->groups,
765 gossip_telepathy_group_get_object_path (group));
767 /* Add all members to the new group */
768 group = tp_contact_list_get_group (list, new_group);
770 gossip_telepathy_group_add_members (group, members, "");
775 empathy_tp_contact_list_get_groups (EmpathyTpContactList *list)
777 EmpathyTpContactListPriv *priv;
778 GList *groups = NULL;
780 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
782 priv = GET_PRIV (list);
784 g_hash_table_foreach (priv->groups,
785 (GHFunc) tp_contact_list_get_groups_foreach,
788 groups = g_list_sort (groups, (GCompareFunc) strcmp);
794 tp_contact_list_finalize_proxies (EmpathyTpContactList *list)
796 EmpathyTpContactListPriv *priv;
798 priv = GET_PRIV (list);
801 g_signal_handlers_disconnect_by_func (priv->tp_conn,
802 tp_contact_list_destroy_cb,
804 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
805 G_CALLBACK (tp_contact_list_newchannel_cb),
809 if (priv->aliasing_iface) {
810 dbus_g_proxy_disconnect_signal (priv->aliasing_iface,
812 G_CALLBACK (tp_contact_list_aliases_update_cb),
816 if (priv->avatars_iface) {
817 dbus_g_proxy_disconnect_signal (priv->avatars_iface,
819 G_CALLBACK (tp_contact_list_avatar_update_cb),
823 if (priv->presence_iface) {
824 dbus_g_proxy_disconnect_signal (priv->presence_iface,
826 G_CALLBACK (tp_contact_list_presence_update_cb),
832 tp_contact_list_destroy_cb (DBusGProxy *proxy,
833 EmpathyTpContactList *list)
835 EmpathyTpContactListPriv *priv;
837 priv = GET_PRIV (list);
839 gossip_debug (DEBUG_DOMAIN, "Connection destroyed... "
840 "Account disconnected or CM crashed");
842 /* DBus proxies should NOT be used anymore */
843 g_object_unref (priv->tp_conn);
844 priv->tp_conn = NULL;
845 priv->aliasing_iface = NULL;
846 priv->avatars_iface = NULL;
847 priv->presence_iface = NULL;
849 /* Remove all contacts */
850 g_hash_table_foreach (priv->contacts,
851 (GHFunc) tp_contact_list_contact_removed_foreach,
853 g_hash_table_remove_all (priv->contacts);
855 /* Tell the world to not use us anymore */
856 g_signal_emit (list, signals[DESTROY], 0);
860 tp_contact_list_contact_removed_foreach (guint handle,
861 GossipContact *contact,
862 EmpathyTpContactList *list)
864 g_signal_handlers_disconnect_by_func (contact,
865 tp_contact_list_groups_updated_cb,
867 g_signal_handlers_disconnect_by_func (contact,
868 tp_contact_list_name_updated_cb,
871 g_signal_emit_by_name (list, "contact-removed", contact);
875 tp_contact_list_block_contact (EmpathyTpContactList *list,
876 GossipContact *contact)
878 g_signal_handlers_block_by_func (contact,
879 tp_contact_list_groups_updated_cb,
881 g_signal_handlers_block_by_func (contact,
882 tp_contact_list_name_updated_cb,
887 tp_contact_list_unblock_contact (EmpathyTpContactList *list,
888 GossipContact *contact)
890 g_signal_handlers_unblock_by_func (contact,
891 tp_contact_list_groups_updated_cb,
893 g_signal_handlers_unblock_by_func (contact,
894 tp_contact_list_name_updated_cb,
899 tp_contact_list_find_foreach (guint handle,
900 GossipContact *contact,
903 if (strcmp (gossip_contact_get_id (contact), id) == 0) {
911 tp_contact_list_newchannel_cb (DBusGProxy *proxy,
912 const gchar *object_path,
913 const gchar *channel_type,
914 TelepathyHandleType handle_type,
915 guint channel_handle,
916 gboolean suppress_handle,
917 EmpathyTpContactList *list)
919 EmpathyTpContactListPriv *priv;
920 GossipTelepathyGroup *group;
922 const gchar *bus_name;
925 priv = GET_PRIV (list);
927 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) != 0 ||
933 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn));
934 new_chan = tp_chan_new (tp_get_bus (),
937 channel_type, handle_type, channel_handle);
939 if (handle_type == TP_HANDLE_TYPE_LIST) {
940 TpContactListType list_type;
942 group = gossip_telepathy_group_new (new_chan, priv->tp_conn);
944 list_type = tp_contact_list_get_type (list, group);
945 if (list_type == TP_CONTACT_LIST_TYPE_UNKNOWN) {
946 gossip_debug (DEBUG_DOMAIN,
947 "Type of contact list channel unknown: %s",
948 gossip_telepathy_group_get_name (group));
950 g_object_unref (new_chan);
951 g_object_unref (group);
954 gossip_debug (DEBUG_DOMAIN,
955 "New contact list channel of type: %d",
959 g_signal_connect (group, "members-added",
960 G_CALLBACK (tp_contact_list_added_cb),
962 g_signal_connect (group, "members-removed",
963 G_CALLBACK (tp_contact_list_removed_cb),
966 members = gossip_telepathy_group_get_members (group);
967 tp_contact_list_added_cb (group, members, 0,
968 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
970 g_array_free (members, TRUE);
972 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
976 g_object_unref (priv->publish);
978 priv->publish = group;
980 /* Makes no sense to be in remote-pending */
981 g_signal_connect (group, "local-pending",
982 G_CALLBACK (tp_contact_list_pending_cb),
985 pendings = gossip_telepathy_group_get_local_pending_members_with_info (group);
989 pending = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
990 for (l = pendings; l; l = l->next) {
991 GossipTpGroupInfo *info;
995 g_array_insert_val (pending, 0, info->member);
996 tp_contact_list_pending_cb (group, pending,
1002 g_array_free (pending, TRUE);
1003 gossip_telepathy_group_info_list_free (pendings);
1006 else if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1007 if (priv->subscribe) {
1008 g_object_unref (priv->subscribe);
1010 priv->subscribe = group;
1012 g_assert_not_reached ();
1015 else if (handle_type == TP_HANDLE_TYPE_GROUP) {
1016 const gchar *object_path;
1018 object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (new_chan));
1019 if (g_hash_table_lookup (priv->groups, object_path)) {
1020 g_object_unref (new_chan);
1024 group = gossip_telepathy_group_new (new_chan, priv->tp_conn);
1026 gossip_debug (DEBUG_DOMAIN, "New server-side group channel: %s",
1027 gossip_telepathy_group_get_name (group));
1029 dbus_g_proxy_connect_signal (DBUS_G_PROXY (new_chan), "Closed",
1031 (tp_contact_list_group_channel_closed_cb),
1034 g_hash_table_insert (priv->groups, g_strdup (object_path), group);
1035 g_signal_connect (group, "members-added",
1036 G_CALLBACK (tp_contact_list_group_members_added_cb),
1038 g_signal_connect (group, "members-removed",
1039 G_CALLBACK (tp_contact_list_group_members_removed_cb),
1042 members = gossip_telepathy_group_get_members (group);
1043 tp_contact_list_group_members_added_cb (group, members, 0,
1044 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1046 g_array_free (members, TRUE);
1049 g_object_unref (new_chan);
1052 static TpContactListType
1053 tp_contact_list_get_type (EmpathyTpContactList *list,
1054 GossipTelepathyGroup *group)
1056 EmpathyTpContactListPriv *priv;
1057 TpContactListType list_type;
1060 priv = GET_PRIV (list);
1062 name = gossip_telepathy_group_get_name (group);
1063 if (strcmp (name, "subscribe") == 0) {
1064 list_type = TP_CONTACT_LIST_TYPE_SUBSCRIBE;
1065 } else if (strcmp (name, "publish") == 0) {
1066 list_type = TP_CONTACT_LIST_TYPE_PUBLISH;
1068 list_type = TP_CONTACT_LIST_TYPE_UNKNOWN;
1075 tp_contact_list_added_cb (GossipTelepathyGroup *group,
1079 const gchar *message,
1080 EmpathyTpContactList *list)
1082 EmpathyTpContactListPriv *priv;
1083 GList *added_list, *l;
1084 TpContactListType list_type;
1086 priv = GET_PRIV (list);
1088 list_type = tp_contact_list_get_type (list, group);
1090 added_list = empathy_tp_contact_list_get_from_handles (list, handles);
1091 for (l = added_list; l; l = l->next) {
1092 GossipContact *contact;
1093 GossipSubscription subscription;
1095 contact = GOSSIP_CONTACT (l->data);
1097 gossip_debug (DEBUG_DOMAIN, "Contact '%s' added to list type %d",
1098 gossip_contact_get_name (contact),
1101 subscription = gossip_contact_get_subscription (contact);
1102 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1103 subscription |= GOSSIP_SUBSCRIPTION_FROM;
1105 else if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1106 subscription |= GOSSIP_SUBSCRIPTION_TO;
1109 tp_contact_list_block_contact (list, contact);
1110 gossip_contact_set_subscription (contact, subscription);
1111 tp_contact_list_unblock_contact (list, contact);
1113 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1114 tp_contact_list_remove_local_pending (list, contact);
1115 g_signal_emit_by_name (list, "contact-added", contact);
1118 g_object_unref (contact);
1121 g_list_free (added_list);
1125 tp_contact_list_removed_cb (GossipTelepathyGroup *group,
1129 const gchar *message,
1130 EmpathyTpContactList *list)
1132 EmpathyTpContactListPriv *priv;
1133 GList *removed_list, *l;
1134 TpContactListType list_type;
1136 priv = GET_PRIV (list);
1138 list_type = tp_contact_list_get_type (list, group);
1140 removed_list = empathy_tp_contact_list_get_from_handles (list, handles);
1141 for (l = removed_list; l; l = l->next) {
1142 GossipContact *contact;
1143 GossipSubscription subscription;
1145 contact = GOSSIP_CONTACT (l->data);
1147 gossip_debug (DEBUG_DOMAIN, "Contact '%s' removed from list type %d",
1148 gossip_contact_get_name (contact),
1151 subscription = gossip_contact_get_subscription (contact);
1152 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1153 subscription &= !GOSSIP_SUBSCRIPTION_FROM;
1155 else if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1156 subscription &= !GOSSIP_SUBSCRIPTION_TO;
1159 tp_contact_list_block_contact (list, contact);
1160 gossip_contact_set_subscription (contact, subscription);
1161 tp_contact_list_unblock_contact (list, contact);
1163 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1164 tp_contact_list_remove_local_pending (list, contact);
1165 g_signal_emit_by_name (list, "contact-removed", contact);
1167 g_object_unref (contact);
1170 g_list_free (removed_list);
1174 tp_contact_list_pending_cb (GossipTelepathyGroup *group,
1178 const gchar *message,
1179 EmpathyTpContactList *list)
1181 EmpathyTpContactListPriv *priv;
1182 GList *pending_list, *l;
1183 TpContactListType list_type;
1185 priv = GET_PRIV (list);
1187 list_type = tp_contact_list_get_type (list, group);
1189 pending_list = empathy_tp_contact_list_get_from_handles (list, handles);
1190 for (l = pending_list; l; l = l->next) {
1191 GossipContact *contact;
1193 contact = GOSSIP_CONTACT (l->data);
1195 gossip_debug (DEBUG_DOMAIN, "Contact '%s' pending in list type %d",
1196 gossip_contact_get_name (contact),
1199 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1200 EmpathyContactListInfo *info;
1202 info = empathy_contact_list_info_new (contact, message);
1203 priv->local_pending = g_list_prepend (priv->local_pending,
1206 g_signal_emit_by_name (list, "local-pending",
1210 g_object_unref (contact);
1213 g_list_free (pending_list);
1217 tp_contact_list_remove_local_pending (EmpathyTpContactList *list,
1218 GossipContact *contact)
1220 EmpathyTpContactListPriv *priv;
1223 priv = GET_PRIV (list);
1225 for (l = priv->local_pending; l; l = l->next) {
1226 EmpathyContactListInfo *info;
1229 if (gossip_contact_equal (contact, info->contact)) {
1230 gossip_debug (DEBUG_DOMAIN, "Contact no more local-pending: %s",
1231 gossip_contact_get_name (contact));
1233 priv->local_pending = g_list_delete_link (priv->local_pending, l);
1234 empathy_contact_list_info_free (info);
1241 tp_contact_list_groups_updated_cb (GossipContact *contact,
1243 EmpathyTpContactList *list)
1245 EmpathyTpContactListPriv *priv;
1246 TpContactListData data;
1249 priv = GET_PRIV (list);
1251 /* Make sure all groups are created */
1252 groups = gossip_contact_get_groups (contact);
1253 for (l = groups; l; l = l->next) {
1254 tp_contact_list_get_group (list, l->data);
1257 data.handle = gossip_contact_get_handle (contact);
1258 data.new_groups = groups;
1260 g_hash_table_foreach (priv->groups,
1261 (GHFunc) tp_contact_list_update_groups_foreach,
1266 tp_contact_list_name_updated_cb (GossipContact *contact,
1268 EmpathyTpContactList *list)
1270 EmpathyTpContactListPriv *priv;
1271 GHashTable *new_alias;
1272 const gchar *new_name;
1274 GError *error = NULL;
1276 priv = GET_PRIV (list);
1278 handle = gossip_contact_get_handle (contact);
1279 new_name = gossip_contact_get_name (contact);
1281 gossip_debug (DEBUG_DOMAIN, "renaming handle %d to %s",
1284 new_alias = g_hash_table_new_full (g_direct_hash,
1289 g_hash_table_insert (new_alias,
1290 GUINT_TO_POINTER (handle),
1291 g_strdup (new_name));
1293 if (!tp_conn_iface_aliasing_set_aliases (priv->aliasing_iface,
1296 gossip_debug (DEBUG_DOMAIN,
1297 "Couldn't rename contact: %s",
1298 error ? error->message : "No error given");
1299 g_clear_error (&error);
1302 g_hash_table_destroy (new_alias);
1306 tp_contact_list_update_groups_foreach (gchar *object_path,
1307 GossipTelepathyGroup *group,
1308 TpContactListData *data)
1311 gboolean found = FALSE;
1312 const gchar *group_name;
1315 is_member = gossip_telepathy_group_is_member (group, data->handle);
1316 group_name = gossip_telepathy_group_get_name (group);
1318 for (l = data->new_groups; l; l = l->next) {
1319 if (strcmp (group_name, l->data) == 0) {
1325 if (is_member && !found) {
1326 /* We are no longer member of this group */
1327 gossip_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1328 data->handle, group_name);
1329 gossip_telepathy_group_remove_member (group, data->handle, "");
1332 if (!is_member && found) {
1333 /* We are now member of this group */
1334 gossip_debug (DEBUG_DOMAIN, "Contact %d added to group '%s'",
1335 data->handle, group_name);
1336 gossip_telepathy_group_add_member (group, data->handle, "");
1340 static GossipTelepathyGroup *
1341 tp_contact_list_get_group (EmpathyTpContactList *list,
1344 EmpathyTpContactListPriv *priv;
1345 GossipTelepathyGroup *group;
1346 TpChan *group_channel;
1349 char *group_object_path;
1350 const char *names[2] = {name, NULL};
1351 GError *error = NULL;
1353 priv = GET_PRIV (list);
1355 group = g_hash_table_find (priv->groups,
1356 (GHRFunc) tp_contact_list_find_group,
1362 gossip_debug (DEBUG_DOMAIN, "creating new group: %s", name);
1364 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
1365 TP_HANDLE_TYPE_GROUP,
1369 gossip_debug (DEBUG_DOMAIN,
1370 "Couldn't request the creation of a new handle for group: %s",
1371 error ? error->message : "No error given");
1372 g_clear_error (&error);
1375 group_handle = g_array_index (handles, guint, 0);
1376 g_array_free (handles, TRUE);
1378 if (!tp_conn_request_channel (DBUS_G_PROXY (priv->tp_conn),
1379 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1380 TP_HANDLE_TYPE_GROUP,
1385 gossip_debug (DEBUG_DOMAIN,
1386 "Couldn't request the creation of a new group channel: %s",
1387 error ? error->message : "No error given");
1388 g_clear_error (&error);
1392 group_channel = tp_chan_new (tp_get_bus (),
1393 dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn)),
1395 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1396 TP_HANDLE_TYPE_GROUP,
1399 dbus_g_proxy_connect_signal (DBUS_G_PROXY (group_channel),
1402 (tp_contact_list_group_channel_closed_cb),
1406 group = gossip_telepathy_group_new (group_channel, priv->tp_conn);
1407 g_hash_table_insert (priv->groups, group_object_path, group);
1408 g_signal_connect (group, "members-added",
1409 G_CALLBACK (tp_contact_list_group_members_added_cb),
1411 g_signal_connect (group, "members-removed",
1412 G_CALLBACK (tp_contact_list_group_members_removed_cb),
1419 tp_contact_list_find_group (gchar *key,
1420 GossipTelepathyGroup *group,
1423 if (strcmp (group_name, gossip_telepathy_group_get_name (group)) == 0) {
1431 tp_contact_list_get_groups_foreach (gchar *key,
1432 GossipTelepathyGroup *group,
1437 name = gossip_telepathy_group_get_name (group);
1438 *groups = g_list_append (*groups, g_strdup (name));
1442 tp_contact_list_group_channel_closed_cb (TpChan *channel,
1443 EmpathyTpContactList *list)
1445 EmpathyTpContactListPriv *priv;
1447 priv = GET_PRIV (list);
1449 g_hash_table_remove (priv->groups,
1450 dbus_g_proxy_get_path (DBUS_G_PROXY (channel)));
1454 tp_contact_list_group_members_added_cb (GossipTelepathyGroup *group,
1458 const gchar *message,
1459 EmpathyTpContactList *list)
1461 EmpathyTpContactListPriv *priv;
1462 GList *added_list, *l;
1463 const gchar *group_name;
1465 priv = GET_PRIV (list);
1467 group_name = gossip_telepathy_group_get_name (group);
1468 added_list = empathy_tp_contact_list_get_from_handles (list, members);
1470 for (l = added_list; l; l = l->next) {
1471 GossipContact *contact;
1472 GList *contact_groups;
1474 contact = GOSSIP_CONTACT (l->data);
1475 contact_groups = gossip_contact_get_groups (contact);
1477 /* FIXME: this leaks */
1478 if (!g_list_find_custom (contact_groups,
1480 (GCompareFunc) strcmp)) {
1481 gossip_debug (DEBUG_DOMAIN, "Contact %s added to group '%s'",
1482 gossip_contact_get_name (contact),
1484 contact_groups = g_list_append (contact_groups,
1485 g_strdup (group_name));
1486 tp_contact_list_block_contact (list, contact);
1487 gossip_contact_set_groups (contact, contact_groups);
1488 tp_contact_list_unblock_contact (list, contact);
1491 g_object_unref (contact);
1494 g_list_free (added_list);
1498 tp_contact_list_group_members_removed_cb (GossipTelepathyGroup *group,
1502 const gchar *message,
1503 EmpathyTpContactList *list)
1505 EmpathyTpContactListPriv *priv;
1506 GList *removed_list, *l;
1507 const gchar *group_name;
1509 priv = GET_PRIV (list);
1511 group_name = gossip_telepathy_group_get_name (group);
1512 removed_list = empathy_tp_contact_list_get_from_handles (list, members);
1514 for (l = removed_list; l; l = l->next) {
1515 GossipContact *contact;
1516 GList *contact_groups;
1519 /* FIXME: Does it leak ? */
1520 contact = GOSSIP_CONTACT (l->data);
1521 contact_groups = gossip_contact_get_groups (contact);
1522 contact_groups = g_list_copy (contact_groups);
1524 to_remove = g_list_find_custom (contact_groups,
1526 (GCompareFunc) strcmp);
1528 gossip_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1529 gossip_contact_get_handle (contact),
1531 contact_groups = g_list_remove_link (contact_groups,
1533 tp_contact_list_block_contact (list, contact);
1534 gossip_contact_set_groups (contact, contact_groups);
1535 tp_contact_list_unblock_contact (list, contact);
1538 g_list_free (contact_groups);
1540 g_object_unref (contact);
1543 g_list_free (removed_list);
1547 tp_contact_list_get_members_foreach (guint handle,
1548 GossipContact *contact,
1551 GossipSubscription subscription;
1553 subscription = gossip_contact_get_subscription (contact);
1554 if (subscription & GOSSIP_SUBSCRIPTION_TO) {
1555 *contacts = g_list_append (*contacts, g_object_ref (contact));
1560 tp_contact_list_get_info (EmpathyTpContactList *list,
1563 EmpathyTpContactListPriv *priv;
1564 GError *error = NULL;
1566 priv = GET_PRIV (list);
1568 if (priv->presence_iface) {
1569 /* FIXME: We should use GetPresence instead */
1570 if (!tp_conn_iface_presence_request_presence (priv->presence_iface,
1572 gossip_debug (DEBUG_DOMAIN,
1573 "Could not request presences: %s",
1574 error ? error->message : "No error given");
1575 g_clear_error (&error);
1579 if (priv->aliasing_iface) {
1580 TpContactListAliasesRequestData *data;
1582 data = g_slice_new (TpContactListAliasesRequestData);
1584 data->handles = g_memdup (handles->data, handles->len * sizeof (guint));
1586 tp_conn_iface_aliasing_request_aliases_async (priv->aliasing_iface,
1588 (tp_conn_iface_aliasing_request_aliases_reply)
1589 tp_contact_list_request_aliases_cb,
1593 if (priv->avatars_iface) {
1596 for (i = 0; i < handles->len; i++) {
1599 handle = g_array_index (handles, gint, i);
1600 tp_contact_list_request_avatar (list, handle);
1606 tp_contact_list_request_avatar (EmpathyTpContactList *list,
1609 EmpathyTpContactListPriv *priv;
1611 priv = GET_PRIV (list);
1613 /* We queue avatar requests to not send too many dbus async
1614 * calls at once. If we don't we reach the dbus's limit of
1616 priv->avatar_requests_queue = g_list_append (priv->avatar_requests_queue,
1617 GUINT_TO_POINTER (handle));
1618 tp_contact_list_start_avatar_requests (list);
1622 tp_contact_list_start_avatar_requests (EmpathyTpContactList *list)
1624 EmpathyTpContactListPriv *priv;
1625 TpContactListAvatarRequestData *data;
1627 priv = GET_PRIV (list);
1629 while (n_avatar_requests < MAX_AVATAR_REQUESTS &&
1630 priv->avatar_requests_queue) {
1631 data = g_slice_new (TpContactListAvatarRequestData);
1633 data->handle = GPOINTER_TO_UINT (priv->avatar_requests_queue->data);
1635 n_avatar_requests++;
1636 priv->avatar_requests_queue = g_list_remove (priv->avatar_requests_queue,
1637 priv->avatar_requests_queue->data);
1639 tp_conn_iface_avatars_request_avatar_async (priv->avatars_iface,
1641 (tp_conn_iface_avatars_request_avatar_reply)
1642 tp_contact_list_request_avatar_cb,
1648 tp_contact_list_avatar_update_cb (DBusGProxy *proxy,
1651 EmpathyTpContactList *list)
1653 EmpathyTpContactListPriv *priv;
1655 priv = GET_PRIV (list);
1657 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1658 /* We don't know this contact, skip */
1662 gossip_debug (DEBUG_DOMAIN, "Changing avatar for %d to %s",
1665 tp_contact_list_request_avatar (list, handle);
1669 tp_contact_list_request_avatar_cb (DBusGProxy *proxy,
1670 GArray *avatar_data,
1673 TpContactListAvatarRequestData *data)
1675 GossipContact *contact;
1677 contact = empathy_tp_contact_list_get_from_handle (data->list, data->handle);
1680 gossip_debug (DEBUG_DOMAIN, "Error requesting avatar for %s: %s",
1681 gossip_contact_get_name (contact),
1682 error ? error->message : "No error given");
1684 GossipAvatar *avatar;
1686 avatar = gossip_avatar_new (avatar_data->data,
1689 tp_contact_list_block_contact (data->list, contact);
1690 gossip_contact_set_avatar (contact, avatar);
1691 tp_contact_list_unblock_contact (data->list, contact);
1692 gossip_avatar_unref (avatar);
1695 n_avatar_requests--;
1696 tp_contact_list_start_avatar_requests (data->list);
1698 g_object_unref (contact);
1699 g_slice_free (TpContactListAvatarRequestData, data);
1703 tp_contact_list_aliases_update_cb (DBusGProxy *proxy,
1704 GPtrArray *renamed_handlers,
1705 EmpathyTpContactList *list)
1707 EmpathyTpContactListPriv *priv;
1710 priv = GET_PRIV (list);
1712 for (i = 0; renamed_handlers->len > i; i++) {
1715 GValueArray *renamed_struct;
1716 GossipContact *contact;
1718 renamed_struct = g_ptr_array_index (renamed_handlers, i);
1719 handle = g_value_get_uint(g_value_array_get_nth (renamed_struct, 0));
1720 alias = g_value_get_string(g_value_array_get_nth (renamed_struct, 1));
1722 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1723 /* We don't know this contact, skip */
1727 if (G_STR_EMPTY (alias)) {
1731 contact = empathy_tp_contact_list_get_from_handle (list, handle);
1732 tp_contact_list_block_contact (list, contact);
1733 gossip_contact_set_name (contact, alias);
1734 tp_contact_list_unblock_contact (list, contact);
1735 g_object_unref (contact);
1737 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (update cb)",
1743 tp_contact_list_request_aliases_cb (DBusGProxy *proxy,
1744 gchar **contact_names,
1746 TpContactListAliasesRequestData *data)
1752 gossip_debug (DEBUG_DOMAIN, "Error requesting aliases: %s",
1756 for (name = contact_names; *name && !error; name++) {
1757 GossipContact *contact;
1759 contact = empathy_tp_contact_list_get_from_handle (data->list,
1761 tp_contact_list_block_contact (data->list, contact);
1762 gossip_contact_set_name (contact, *name);
1763 tp_contact_list_unblock_contact (data->list, contact);
1764 g_object_unref (contact);
1766 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (request cb)",
1767 data->handles[i], *name);
1772 g_free (data->handles);
1773 g_slice_free (TpContactListAliasesRequestData, data);
1777 tp_contact_list_presence_update_cb (DBusGProxy *proxy,
1778 GHashTable *handle_table,
1779 EmpathyTpContactList *list)
1781 g_hash_table_foreach (handle_table,
1782 (GHFunc) tp_contact_list_parse_presence_foreach,
1787 tp_contact_list_parse_presence_foreach (guint handle,
1788 GValueArray *presence_struct,
1789 EmpathyTpContactList *list)
1791 EmpathyTpContactListPriv *priv;
1792 GHashTable *presences_table;
1793 GossipContact *contact;
1794 GossipPresence *presence = NULL;
1796 priv = GET_PRIV (list);
1798 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1799 /* We don't know this contact, skip */
1803 contact = empathy_tp_contact_list_get_from_handle (list, handle);
1804 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
1806 g_hash_table_foreach (presences_table,
1807 (GHFunc) tp_contact_list_presences_table_foreach,
1810 gossip_debug (DEBUG_DOMAIN, "Presence changed for %s (%d) to %s (%d)",
1811 gossip_contact_get_name (contact),
1813 presence ? gossip_presence_get_status (presence) : "unset",
1814 presence ? gossip_presence_get_state (presence) : MC_PRESENCE_UNSET);
1816 tp_contact_list_block_contact (list, contact);
1817 gossip_contact_set_presence (contact, presence);
1818 tp_contact_list_unblock_contact (list, contact);
1820 g_object_unref (contact);
1824 tp_contact_list_presences_table_foreach (const gchar *state_str,
1825 GHashTable *presences_table,
1826 GossipPresence **presence)
1829 const GValue *message;
1831 state = gossip_presence_state_from_str (state_str);
1832 if ((state == MC_PRESENCE_UNSET) || (state == MC_PRESENCE_OFFLINE)) {
1837 g_object_unref (*presence);
1841 *presence = gossip_presence_new ();
1842 gossip_presence_set_state (*presence, state);
1844 message = g_hash_table_lookup (presences_table, "message");
1845 if (message != NULL) {
1846 gossip_presence_set_status (*presence,
1847 g_value_get_string (message));
1852 tp_contact_list_status_changed_cb (MissionControl *mc,
1853 TelepathyConnectionStatus status,
1854 McPresence presence,
1855 TelepathyConnectionStatusReason reason,
1856 const gchar *unique_name,
1857 EmpathyTpContactList *list)
1859 EmpathyTpContactListPriv *priv;
1862 priv = GET_PRIV (list);
1864 account = mc_account_lookup (unique_name);
1865 if (status != TP_CONN_STATUS_DISCONNECTED ||
1866 !gossip_account_equal (account, priv->account) ||
1868 g_object_unref (account);
1872 /* We are disconnected, do just like if the connection was destroyed */
1873 g_signal_handlers_disconnect_by_func (priv->tp_conn,
1874 tp_contact_list_destroy_cb,
1876 tp_contact_list_destroy_cb (DBUS_G_PROXY (priv->tp_conn), list);
1878 g_object_unref (account);