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);
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 handle = gossip_contact_get_handle (contact);
1280 new_name = gossip_contact_get_name (contact);
1282 gossip_debug (DEBUG_DOMAIN, "renaming handle %d to %s",
1285 new_alias = g_hash_table_new_full (g_direct_hash,
1290 g_hash_table_insert (new_alias,
1291 GUINT_TO_POINTER (handle),
1292 g_strdup (new_name));
1294 if (!tp_conn_iface_aliasing_set_aliases (priv->aliasing_iface,
1297 gossip_debug (DEBUG_DOMAIN,
1298 "Couldn't rename contact: %s",
1299 error ? error->message : "No error given");
1300 g_clear_error (&error);
1303 g_hash_table_destroy (new_alias);
1307 tp_contact_list_update_groups_foreach (gchar *object_path,
1308 GossipTelepathyGroup *group,
1309 TpContactListData *data)
1312 gboolean found = FALSE;
1313 const gchar *group_name;
1316 is_member = gossip_telepathy_group_is_member (group, data->handle);
1317 group_name = gossip_telepathy_group_get_name (group);
1319 for (l = data->new_groups; l; l = l->next) {
1320 if (strcmp (group_name, l->data) == 0) {
1326 if (is_member && !found) {
1327 /* We are no longer member of this group */
1328 gossip_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1329 data->handle, group_name);
1330 gossip_telepathy_group_remove_member (group, data->handle, "");
1333 if (!is_member && found) {
1334 /* We are now member of this group */
1335 gossip_debug (DEBUG_DOMAIN, "Contact %d added to group '%s'",
1336 data->handle, group_name);
1337 gossip_telepathy_group_add_member (group, data->handle, "");
1341 static GossipTelepathyGroup *
1342 tp_contact_list_get_group (EmpathyTpContactList *list,
1345 EmpathyTpContactListPriv *priv;
1346 GossipTelepathyGroup *group;
1347 TpChan *group_channel;
1350 char *group_object_path;
1351 const char *names[2] = {name, NULL};
1352 GError *error = NULL;
1354 priv = GET_PRIV (list);
1356 group = g_hash_table_find (priv->groups,
1357 (GHRFunc) tp_contact_list_find_group,
1363 gossip_debug (DEBUG_DOMAIN, "creating new group: %s", name);
1365 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
1366 TP_HANDLE_TYPE_GROUP,
1370 gossip_debug (DEBUG_DOMAIN,
1371 "Couldn't request the creation of a new handle for group: %s",
1372 error ? error->message : "No error given");
1373 g_clear_error (&error);
1376 group_handle = g_array_index (handles, guint, 0);
1377 g_array_free (handles, TRUE);
1379 if (!tp_conn_request_channel (DBUS_G_PROXY (priv->tp_conn),
1380 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1381 TP_HANDLE_TYPE_GROUP,
1386 gossip_debug (DEBUG_DOMAIN,
1387 "Couldn't request the creation of a new group channel: %s",
1388 error ? error->message : "No error given");
1389 g_clear_error (&error);
1393 group_channel = tp_chan_new (tp_get_bus (),
1394 dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn)),
1396 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1397 TP_HANDLE_TYPE_GROUP,
1400 dbus_g_proxy_connect_signal (DBUS_G_PROXY (group_channel),
1403 (tp_contact_list_group_channel_closed_cb),
1407 group = gossip_telepathy_group_new (group_channel, priv->tp_conn);
1408 g_hash_table_insert (priv->groups, group_object_path, group);
1409 g_signal_connect (group, "members-added",
1410 G_CALLBACK (tp_contact_list_group_members_added_cb),
1412 g_signal_connect (group, "members-removed",
1413 G_CALLBACK (tp_contact_list_group_members_removed_cb),
1420 tp_contact_list_find_group (gchar *key,
1421 GossipTelepathyGroup *group,
1424 if (strcmp (group_name, gossip_telepathy_group_get_name (group)) == 0) {
1432 tp_contact_list_get_groups_foreach (gchar *key,
1433 GossipTelepathyGroup *group,
1438 name = gossip_telepathy_group_get_name (group);
1439 *groups = g_list_append (*groups, g_strdup (name));
1443 tp_contact_list_group_channel_closed_cb (TpChan *channel,
1444 EmpathyTpContactList *list)
1446 EmpathyTpContactListPriv *priv;
1448 priv = GET_PRIV (list);
1450 g_hash_table_remove (priv->groups,
1451 dbus_g_proxy_get_path (DBUS_G_PROXY (channel)));
1455 tp_contact_list_group_members_added_cb (GossipTelepathyGroup *group,
1459 const gchar *message,
1460 EmpathyTpContactList *list)
1462 EmpathyTpContactListPriv *priv;
1463 GList *added_list, *l;
1464 const gchar *group_name;
1466 priv = GET_PRIV (list);
1468 group_name = gossip_telepathy_group_get_name (group);
1469 added_list = empathy_tp_contact_list_get_from_handles (list, members);
1471 for (l = added_list; l; l = l->next) {
1472 GossipContact *contact;
1474 contact = GOSSIP_CONTACT (l->data);
1476 tp_contact_list_block_contact (list, contact);
1477 gossip_contact_add_group (contact, group_name);
1478 tp_contact_list_unblock_contact (list, contact);
1480 g_object_unref (contact);
1483 g_list_free (added_list);
1487 tp_contact_list_group_members_removed_cb (GossipTelepathyGroup *group,
1491 const gchar *message,
1492 EmpathyTpContactList *list)
1494 EmpathyTpContactListPriv *priv;
1495 GList *removed_list, *l;
1496 const gchar *group_name;
1498 priv = GET_PRIV (list);
1500 group_name = gossip_telepathy_group_get_name (group);
1501 removed_list = empathy_tp_contact_list_get_from_handles (list, members);
1503 for (l = removed_list; l; l = l->next) {
1504 GossipContact *contact;
1508 tp_contact_list_block_contact (list, contact);
1509 gossip_contact_remove_group (contact, group_name);
1510 tp_contact_list_unblock_contact (list, contact);
1512 g_object_unref (contact);
1515 g_list_free (removed_list);
1519 tp_contact_list_get_members_foreach (guint handle,
1520 GossipContact *contact,
1523 GossipSubscription subscription;
1525 subscription = gossip_contact_get_subscription (contact);
1526 if (subscription & GOSSIP_SUBSCRIPTION_TO) {
1527 *contacts = g_list_append (*contacts, g_object_ref (contact));
1532 tp_contact_list_get_info (EmpathyTpContactList *list,
1535 EmpathyTpContactListPriv *priv;
1536 GError *error = NULL;
1538 priv = GET_PRIV (list);
1540 if (priv->presence_iface) {
1541 /* FIXME: We should use GetPresence instead */
1542 if (!tp_conn_iface_presence_request_presence (priv->presence_iface,
1544 gossip_debug (DEBUG_DOMAIN,
1545 "Could not request presences: %s",
1546 error ? error->message : "No error given");
1547 g_clear_error (&error);
1551 if (priv->aliasing_iface) {
1552 TpContactListAliasesRequestData *data;
1554 data = g_slice_new (TpContactListAliasesRequestData);
1556 data->handles = g_memdup (handles->data, handles->len * sizeof (guint));
1558 tp_conn_iface_aliasing_request_aliases_async (priv->aliasing_iface,
1560 (tp_conn_iface_aliasing_request_aliases_reply)
1561 tp_contact_list_request_aliases_cb,
1565 if (priv->avatars_iface) {
1568 for (i = 0; i < handles->len; i++) {
1571 handle = g_array_index (handles, gint, i);
1572 tp_contact_list_request_avatar (list, handle);
1578 tp_contact_list_request_avatar (EmpathyTpContactList *list,
1581 EmpathyTpContactListPriv *priv;
1583 priv = GET_PRIV (list);
1585 /* We queue avatar requests to not send too many dbus async
1586 * calls at once. If we don't we reach the dbus's limit of
1588 priv->avatar_requests_queue = g_list_append (priv->avatar_requests_queue,
1589 GUINT_TO_POINTER (handle));
1590 tp_contact_list_start_avatar_requests (list);
1594 tp_contact_list_start_avatar_requests (EmpathyTpContactList *list)
1596 EmpathyTpContactListPriv *priv;
1597 TpContactListAvatarRequestData *data;
1599 priv = GET_PRIV (list);
1601 while (n_avatar_requests < MAX_AVATAR_REQUESTS &&
1602 priv->avatar_requests_queue) {
1603 data = g_slice_new (TpContactListAvatarRequestData);
1605 data->handle = GPOINTER_TO_UINT (priv->avatar_requests_queue->data);
1607 n_avatar_requests++;
1608 priv->avatar_requests_queue = g_list_remove (priv->avatar_requests_queue,
1609 priv->avatar_requests_queue->data);
1611 tp_conn_iface_avatars_request_avatar_async (priv->avatars_iface,
1613 (tp_conn_iface_avatars_request_avatar_reply)
1614 tp_contact_list_request_avatar_cb,
1620 tp_contact_list_avatar_update_cb (DBusGProxy *proxy,
1623 EmpathyTpContactList *list)
1625 EmpathyTpContactListPriv *priv;
1627 priv = GET_PRIV (list);
1629 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1630 /* We don't know this contact, skip */
1634 gossip_debug (DEBUG_DOMAIN, "Changing avatar for %d to %s",
1637 tp_contact_list_request_avatar (list, handle);
1641 tp_contact_list_request_avatar_cb (DBusGProxy *proxy,
1642 GArray *avatar_data,
1645 TpContactListAvatarRequestData *data)
1647 GossipContact *contact;
1649 contact = empathy_tp_contact_list_get_from_handle (data->list, data->handle);
1652 gossip_debug (DEBUG_DOMAIN, "Error requesting avatar for %s: %s",
1653 gossip_contact_get_name (contact),
1654 error ? error->message : "No error given");
1656 GossipAvatar *avatar;
1658 avatar = gossip_avatar_new (avatar_data->data,
1661 tp_contact_list_block_contact (data->list, contact);
1662 gossip_contact_set_avatar (contact, avatar);
1663 tp_contact_list_unblock_contact (data->list, contact);
1664 gossip_avatar_unref (avatar);
1667 n_avatar_requests--;
1668 tp_contact_list_start_avatar_requests (data->list);
1670 g_object_unref (contact);
1671 g_slice_free (TpContactListAvatarRequestData, data);
1675 tp_contact_list_aliases_update_cb (DBusGProxy *proxy,
1676 GPtrArray *renamed_handlers,
1677 EmpathyTpContactList *list)
1679 EmpathyTpContactListPriv *priv;
1682 priv = GET_PRIV (list);
1684 for (i = 0; renamed_handlers->len > i; i++) {
1687 GValueArray *renamed_struct;
1688 GossipContact *contact;
1690 renamed_struct = g_ptr_array_index (renamed_handlers, i);
1691 handle = g_value_get_uint(g_value_array_get_nth (renamed_struct, 0));
1692 alias = g_value_get_string(g_value_array_get_nth (renamed_struct, 1));
1694 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1695 /* We don't know this contact, skip */
1699 if (G_STR_EMPTY (alias)) {
1703 contact = empathy_tp_contact_list_get_from_handle (list, handle);
1704 tp_contact_list_block_contact (list, contact);
1705 gossip_contact_set_name (contact, alias);
1706 tp_contact_list_unblock_contact (list, contact);
1707 g_object_unref (contact);
1709 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (update cb)",
1715 tp_contact_list_request_aliases_cb (DBusGProxy *proxy,
1716 gchar **contact_names,
1718 TpContactListAliasesRequestData *data)
1724 gossip_debug (DEBUG_DOMAIN, "Error requesting aliases: %s",
1728 for (name = contact_names; *name && !error; name++) {
1729 GossipContact *contact;
1731 contact = empathy_tp_contact_list_get_from_handle (data->list,
1733 tp_contact_list_block_contact (data->list, contact);
1734 gossip_contact_set_name (contact, *name);
1735 tp_contact_list_unblock_contact (data->list, contact);
1736 g_object_unref (contact);
1738 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (request cb)",
1739 data->handles[i], *name);
1744 g_free (data->handles);
1745 g_slice_free (TpContactListAliasesRequestData, data);
1749 tp_contact_list_presence_update_cb (DBusGProxy *proxy,
1750 GHashTable *handle_table,
1751 EmpathyTpContactList *list)
1753 g_hash_table_foreach (handle_table,
1754 (GHFunc) tp_contact_list_parse_presence_foreach,
1759 tp_contact_list_parse_presence_foreach (guint handle,
1760 GValueArray *presence_struct,
1761 EmpathyTpContactList *list)
1763 EmpathyTpContactListPriv *priv;
1764 GHashTable *presences_table;
1765 GossipContact *contact;
1766 GossipPresence *presence = NULL;
1768 priv = GET_PRIV (list);
1770 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1771 /* We don't know this contact, skip */
1775 contact = empathy_tp_contact_list_get_from_handle (list, handle);
1776 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
1778 g_hash_table_foreach (presences_table,
1779 (GHFunc) tp_contact_list_presences_table_foreach,
1782 gossip_debug (DEBUG_DOMAIN, "Presence changed for %s (%d) to %s (%d)",
1783 gossip_contact_get_name (contact),
1785 presence ? gossip_presence_get_status (presence) : "unset",
1786 presence ? gossip_presence_get_state (presence) : MC_PRESENCE_UNSET);
1788 tp_contact_list_block_contact (list, contact);
1789 gossip_contact_set_presence (contact, presence);
1790 tp_contact_list_unblock_contact (list, contact);
1792 g_object_unref (contact);
1796 tp_contact_list_presences_table_foreach (const gchar *state_str,
1797 GHashTable *presences_table,
1798 GossipPresence **presence)
1801 const GValue *message;
1803 state = gossip_presence_state_from_str (state_str);
1804 if ((state == MC_PRESENCE_UNSET) || (state == MC_PRESENCE_OFFLINE)) {
1809 g_object_unref (*presence);
1813 *presence = gossip_presence_new ();
1814 gossip_presence_set_state (*presence, state);
1816 message = g_hash_table_lookup (presences_table, "message");
1817 if (message != NULL) {
1818 gossip_presence_set_status (*presence,
1819 g_value_get_string (message));
1824 tp_contact_list_status_changed_cb (MissionControl *mc,
1825 TelepathyConnectionStatus status,
1826 McPresence presence,
1827 TelepathyConnectionStatusReason reason,
1828 const gchar *unique_name,
1829 EmpathyTpContactList *list)
1831 EmpathyTpContactListPriv *priv;
1834 priv = GET_PRIV (list);
1836 account = mc_account_lookup (unique_name);
1837 if (status != TP_CONN_STATUS_DISCONNECTED ||
1838 !gossip_account_equal (account, priv->account) ||
1840 g_object_unref (account);
1844 /* We are disconnected, do just like if the connection was destroyed */
1845 g_signal_handlers_disconnect_by_func (priv->tp_conn,
1846 tp_contact_list_destroy_cb,
1848 tp_contact_list_destroy_cb (DBUS_G_PROXY (priv->tp_conn), list);
1850 g_object_unref (account);