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 *known;
56 GossipTelepathyGroup *publish;
57 GossipTelepathyGroup *subscribe;
62 DBusGProxy *aliasing_iface;
63 DBusGProxy *avatars_iface;
64 DBusGProxy *presence_iface;
66 GList *avatar_requests_queue;
70 TP_CONTACT_LIST_TYPE_KNOWN,
71 TP_CONTACT_LIST_TYPE_PUBLISH,
72 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
73 TP_CONTACT_LIST_TYPE_UNKNOWN,
74 TP_CONTACT_LIST_TYPE_COUNT
83 EmpathyTpContactList *list;
85 } TpContactListAvatarRequestData;
88 EmpathyTpContactList *list;
90 } TpContactListAliasesRequestData;
92 static void empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass);
93 static void tp_contact_list_iface_init (EmpathyContactListIface *iface);
94 static void empathy_tp_contact_list_init (EmpathyTpContactList *list);
95 static void tp_contact_list_finalize (GObject *object);
96 static void tp_contact_list_finalize_proxies (EmpathyTpContactList *list);
97 static void tp_contact_list_setup (EmpathyContactList *list);
98 static GossipContact * tp_contact_list_find (EmpathyContactList *list,
100 static void tp_contact_list_add (EmpathyContactList *list,
101 GossipContact *contact,
102 const gchar *message);
103 static void tp_contact_list_remove (EmpathyContactList *list,
104 GossipContact *contact,
105 const gchar *message);
106 static GList * tp_contact_list_get_contacts (EmpathyContactList *list);
107 static void tp_contact_list_contact_removed_foreach (guint handle,
108 GossipContact *contact,
109 EmpathyTpContactList *list);
110 static void tp_contact_list_destroy_cb (DBusGProxy *proxy,
111 EmpathyTpContactList *list);
112 static gboolean tp_contact_list_find_foreach (guint handle,
113 GossipContact *contact,
115 static void tp_contact_list_newchannel_cb (DBusGProxy *proxy,
116 const gchar *object_path,
117 const gchar *channel_type,
118 TelepathyHandleType handle_type,
119 guint channel_handle,
120 gboolean suppress_handle,
121 EmpathyTpContactList *list);
122 static TpContactListType tp_contact_list_get_type (EmpathyTpContactList *list,
124 static void tp_contact_list_contact_added_cb (GossipTelepathyGroup *group,
128 const gchar *message,
129 EmpathyTpContactList *list);
130 static void tp_contact_list_contact_removed_cb (GossipTelepathyGroup *group,
134 const gchar *message,
135 EmpathyTpContactList *list);
136 static void tp_contact_list_local_pending_cb (GossipTelepathyGroup *group,
140 const gchar *message,
141 EmpathyTpContactList *list);
142 static void tp_contact_list_groups_updated_cb (GossipContact *contact,
144 EmpathyTpContactList *list);
145 static void tp_contact_list_subscription_updated_cb (GossipContact *contact,
147 EmpathyTpContactList *list);
148 static void tp_contact_list_name_updated_cb (GossipContact *contact,
150 EmpathyTpContactList *list);
151 static void tp_contact_list_update_groups_foreach (gchar *object_path,
152 GossipTelepathyGroup *group,
153 TpContactListData *data);
154 static GossipTelepathyGroup * tp_contact_list_get_group (EmpathyTpContactList *list,
156 static gboolean tp_contact_list_find_group (gchar *key,
157 GossipTelepathyGroup *group,
159 static void tp_contact_list_get_groups_foreach (gchar *key,
160 GossipTelepathyGroup *group,
162 static void tp_contact_list_group_channel_closed_cb (TpChan *channel,
163 EmpathyTpContactList *list);
164 static void tp_contact_list_group_members_added_cb (GossipTelepathyGroup *group,
168 const gchar *message,
169 EmpathyTpContactList *list);
170 static void tp_contact_list_group_members_removed_cb (GossipTelepathyGroup *group,
174 const gchar *message,
175 EmpathyTpContactList *list);
176 static void tp_contact_list_get_contacts_foreach (guint handle,
177 GossipContact *contact,
179 static void tp_contact_list_get_info (EmpathyTpContactList *list,
181 static void tp_contact_list_request_avatar (EmpathyTpContactList *list,
183 static void tp_contact_list_start_avatar_requests (EmpathyTpContactList *list);
184 static void tp_contact_list_avatar_update_cb (DBusGProxy *proxy,
187 EmpathyTpContactList *list);
188 static void tp_contact_list_request_avatar_cb (DBusGProxy *proxy,
192 TpContactListAvatarRequestData *data);
193 static void tp_contact_list_aliases_update_cb (DBusGProxy *proxy,
195 EmpathyTpContactList *list);
196 static void tp_contact_list_request_aliases_cb (DBusGProxy *proxy,
197 gchar **contact_names,
199 TpContactListAliasesRequestData *data);
200 static void tp_contact_list_presence_update_cb (DBusGProxy *proxy,
201 GHashTable *handle_table,
202 EmpathyTpContactList *list);
203 static void tp_contact_list_parse_presence_foreach (guint handle,
204 GValueArray *presence_struct,
205 EmpathyTpContactList *list);
206 static void tp_contact_list_presences_table_foreach (const gchar *state_str,
207 GHashTable *presences_table,
208 GossipPresence **presence);
209 static void tp_contact_list_status_changed_cb (MissionControl *mc,
210 TelepathyConnectionStatus status,
212 TelepathyConnectionStatusReason reason,
213 const gchar *unique_name,
214 EmpathyTpContactList *list);
221 static guint signals[LAST_SIGNAL];
222 static guint n_avatar_requests = 0;
224 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
225 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
226 tp_contact_list_iface_init));
229 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
231 GObjectClass *object_class = G_OBJECT_CLASS (klass);
233 object_class->finalize = tp_contact_list_finalize;
236 g_signal_new ("destroy",
237 G_TYPE_FROM_CLASS (klass),
241 g_cclosure_marshal_VOID__VOID,
245 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
249 tp_contact_list_iface_init (EmpathyContactListIface *iface)
251 iface->setup = tp_contact_list_setup;
252 iface->find = tp_contact_list_find;
253 iface->add = tp_contact_list_add;
254 iface->remove = tp_contact_list_remove;
255 iface->get_contacts = tp_contact_list_get_contacts;
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);
298 g_object_unref (priv->known);
301 if (priv->subscribe) {
302 g_object_unref (priv->subscribe);
306 g_object_unref (priv->publish);
309 g_object_unref (priv->account);
310 g_object_unref (priv->user_contact);
311 g_object_unref (priv->mc);
312 g_hash_table_destroy (priv->groups);
313 g_hash_table_destroy (priv->contacts);
315 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
318 EmpathyTpContactList *
319 empathy_tp_contact_list_new (McAccount *account)
321 EmpathyTpContactListPriv *priv;
322 EmpathyTpContactList *list;
325 GError *error = NULL;
327 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
329 mc = gossip_mission_control_new ();
331 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
332 /* The account is not connected, nothing to do. */
336 list = g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST, NULL);
337 priv = GET_PRIV (list);
339 priv->tp_conn = mission_control_get_connection (mc, account, NULL);
340 priv->account = g_object_ref (account);
343 g_signal_connect (priv->tp_conn, "destroy",
344 G_CALLBACK (tp_contact_list_destroy_cb),
346 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
347 "AccountStatusChanged",
348 G_CALLBACK (tp_contact_list_status_changed_cb),
351 priv->aliasing_iface = tp_conn_get_interface (priv->tp_conn,
352 TELEPATHY_CONN_IFACE_ALIASING_QUARK);
353 priv->avatars_iface = tp_conn_get_interface (priv->tp_conn,
354 TELEPATHY_CONN_IFACE_AVATARS_QUARK);
355 priv->presence_iface = tp_conn_get_interface (priv->tp_conn,
356 TELEPATHY_CONN_IFACE_PRESENCE_QUARK);
358 if (priv->aliasing_iface) {
359 dbus_g_proxy_connect_signal (priv->aliasing_iface,
361 G_CALLBACK (tp_contact_list_aliases_update_cb),
365 if (priv->avatars_iface) {
366 dbus_g_proxy_connect_signal (priv->avatars_iface,
368 G_CALLBACK (tp_contact_list_avatar_update_cb),
372 if (priv->presence_iface) {
373 dbus_g_proxy_connect_signal (priv->presence_iface,
375 G_CALLBACK (tp_contact_list_presence_update_cb),
379 /* Get our own handle and contact */
380 if (!tp_conn_get_self_handle (DBUS_G_PROXY (priv->tp_conn),
382 gossip_debug (DEBUG_DOMAIN, "GetSelfHandle Error: %s",
383 error ? error->message : "No error given");
384 g_clear_error (&error);
386 priv->user_contact = empathy_tp_contact_list_get_from_handle (list, handle);
393 tp_contact_list_setup (EmpathyContactList *list)
395 EmpathyTpContactListPriv *priv;
397 GError *error = NULL;
400 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
402 priv = GET_PRIV (list);
404 gossip_debug (DEBUG_DOMAIN, "setup contact list: %p", list);
407 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
408 G_CALLBACK (tp_contact_list_newchannel_cb),
411 /* Get existing channels */
412 if (!tp_conn_list_channels (DBUS_G_PROXY (priv->tp_conn),
415 gossip_debug (DEBUG_DOMAIN,
416 "Failed to get list of open channels: %s",
417 error ? error->message : "No error given");
418 g_clear_error (&error);
422 for (i = 0; channels->len > i; i++) {
423 GValueArray *chan_struct;
424 const gchar *object_path;
425 const gchar *chan_iface;
426 TelepathyHandleType handle_type;
429 chan_struct = g_ptr_array_index (channels, i);
430 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
431 chan_iface = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
432 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
433 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
435 tp_contact_list_newchannel_cb (DBUS_G_PROXY (priv->tp_conn),
436 object_path, chan_iface,
439 EMPATHY_TP_CONTACT_LIST (list));
441 g_value_array_free (chan_struct);
444 g_ptr_array_free (channels, TRUE);
447 static GossipContact *
448 tp_contact_list_find (EmpathyContactList *list,
451 EmpathyTpContactListPriv *priv;
453 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
455 priv = GET_PRIV (list);
457 return g_hash_table_find (priv->contacts,
458 (GHRFunc) tp_contact_list_find_foreach,
463 tp_contact_list_add (EmpathyContactList *list,
464 GossipContact *contact,
465 const gchar *message)
467 EmpathyTpContactListPriv *priv;
470 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
472 priv = GET_PRIV (list);
474 handle = gossip_contact_get_handle (contact);
475 gossip_telepathy_group_add_member (priv->subscribe, handle, message);
479 tp_contact_list_remove (EmpathyContactList *list,
480 GossipContact *contact,
481 const gchar *message)
483 EmpathyTpContactListPriv *priv;
486 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
488 priv = GET_PRIV (list);
490 handle = gossip_contact_get_handle (contact);
491 gossip_telepathy_group_remove_member (priv->subscribe, handle, message);
492 gossip_telepathy_group_remove_member (priv->publish, handle, message);
493 gossip_telepathy_group_remove_member (priv->known, handle, message);
497 tp_contact_list_get_contacts (EmpathyContactList *list)
499 EmpathyTpContactListPriv *priv;
500 GList *contacts = NULL;
502 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
504 priv = GET_PRIV (list);
506 /* FIXME: we should only return contacts that are in the contact list */
507 g_hash_table_foreach (priv->contacts,
508 (GHFunc) tp_contact_list_get_contacts_foreach,
515 empathy_tp_contact_list_get_account (EmpathyTpContactList *list)
517 EmpathyTpContactListPriv *priv;
519 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
521 priv = GET_PRIV (list);
523 return priv->account;
527 empathy_tp_contact_list_get_user (EmpathyTpContactList *list)
529 EmpathyTpContactListPriv *priv;
531 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
533 priv = GET_PRIV (list);
535 return priv->user_contact;
539 empathy_tp_contact_list_get_from_id (EmpathyTpContactList *list,
542 EmpathyTpContactListPriv *priv;
543 GossipContact *contact;
544 const gchar *contact_ids[] = {id, NULL};
547 GError *error = NULL;
549 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
550 g_return_val_if_fail (id != NULL, NULL);
552 priv = GET_PRIV (list);
554 contact = tp_contact_list_find (EMPATHY_CONTACT_LIST (list), id);
559 /* The id is unknown, requests a new handle */
560 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
561 TP_HANDLE_TYPE_CONTACT,
564 gossip_debug (DEBUG_DOMAIN,
565 "RequestHandle for %s failed: %s", id,
566 error ? error->message : "No error given");
567 g_clear_error (&error);
571 handle = g_array_index(handles, guint, 0);
572 g_array_free (handles, TRUE);
574 return empathy_tp_contact_list_get_from_handle (list, handle);
578 empathy_tp_contact_list_get_from_handle (EmpathyTpContactList *list,
581 GossipContact *contact;
585 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
587 handles = g_array_new (FALSE, FALSE, sizeof (guint));
588 g_array_append_val (handles, handle);
590 contacts = empathy_tp_contact_list_get_from_handles (list, handles);
591 g_array_free (handles, TRUE);
597 contact = contacts->data;
598 g_list_free (contacts);
604 empathy_tp_contact_list_get_from_handles (EmpathyTpContactList *list,
607 EmpathyTpContactListPriv *priv;
608 gchar **handles_names;
611 GList *contacts = NULL;
613 GError *error = NULL;
615 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
616 g_return_val_if_fail (handles != NULL, NULL);
618 priv = GET_PRIV (list);
620 /* Search all handles we already have */
621 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
622 for (i = 0; i < handles->len; i++) {
623 GossipContact *contact;
626 handle = g_array_index (handles, guint, i);
632 contact = g_hash_table_lookup (priv->contacts,
633 GUINT_TO_POINTER (handle));
636 contacts = g_list_prepend (contacts,
637 g_object_ref (contact));
639 g_array_append_val (new_handles, handle);
643 if (new_handles->len == 0) {
647 /* Holds all handles we don't have yet.
648 * FIXME: We should release them at some point. */
649 if (!tp_conn_hold_handles (DBUS_G_PROXY (priv->tp_conn),
650 TP_HANDLE_TYPE_CONTACT,
651 new_handles, &error)) {
652 gossip_debug (DEBUG_DOMAIN,
653 "HoldHandles Error: %s",
654 error ? error->message : "No error given");
655 g_clear_error (&error);
656 g_array_free (new_handles, TRUE);
660 /* Get the IDs of all new handles */
661 if (!tp_conn_inspect_handles (DBUS_G_PROXY (priv->tp_conn),
662 TP_HANDLE_TYPE_CONTACT,
666 gossip_debug (DEBUG_DOMAIN,
667 "InspectHandle Error: %s",
668 error ? error->message : "No error given");
669 g_clear_error (&error);
670 g_array_free (new_handles, TRUE);
674 /* Create contact objects */
675 for (i = 0, id = handles_names; *id && i < new_handles->len; id++, i++) {
676 GossipContact *contact;
679 handle = g_array_index (new_handles, guint, i);
680 contact = g_object_new (GOSSIP_TYPE_CONTACT,
681 "account", priv->account,
686 g_signal_connect (contact, "notify::groups",
687 G_CALLBACK (tp_contact_list_groups_updated_cb),
689 g_signal_connect (contact, "notify::subscription",
690 G_CALLBACK (tp_contact_list_subscription_updated_cb),
692 g_signal_connect (contact, "notify::name",
693 G_CALLBACK (tp_contact_list_name_updated_cb),
696 gossip_debug (DEBUG_DOMAIN, "new contact created: %s (%d)",
699 g_hash_table_insert (priv->contacts,
700 GUINT_TO_POINTER (handle),
703 contacts = g_list_prepend (contacts, g_object_ref (contact));
706 tp_contact_list_get_info (list, new_handles);
708 g_array_free (new_handles, TRUE);
709 g_strfreev (handles_names);
715 empathy_tp_contact_list_rename_group (EmpathyTpContactList *list,
716 const gchar *old_group,
717 const gchar *new_group)
719 EmpathyTpContactListPriv *priv;
720 GossipTelepathyGroup *group;
723 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
724 g_return_if_fail (old_group != NULL);
725 g_return_if_fail (new_group != NULL);
727 priv = GET_PRIV (list);
729 group = g_hash_table_find (priv->groups,
730 (GHRFunc) tp_contact_list_find_group,
733 /* The group doesn't exists on this account */
737 gossip_debug (DEBUG_DOMAIN, "rename group %s to %s", group, new_group);
739 /* Remove all members from the old group */
740 members = gossip_telepathy_group_get_members (group);
741 gossip_telepathy_group_remove_members (group, members, "");
742 tp_contact_list_group_members_removed_cb (group, members,
744 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
746 g_hash_table_remove (priv->groups,
747 gossip_telepathy_group_get_object_path (group));
749 /* Add all members to the new group */
750 group = tp_contact_list_get_group (list, new_group);
752 gossip_telepathy_group_add_members (group, members, "");
757 empathy_tp_contact_list_get_groups (EmpathyTpContactList *list)
759 EmpathyTpContactListPriv *priv;
760 GList *groups = NULL;
762 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
764 priv = GET_PRIV (list);
766 g_hash_table_foreach (priv->groups,
767 (GHFunc) tp_contact_list_get_groups_foreach,
770 groups = g_list_sort (groups, (GCompareFunc) strcmp);
776 tp_contact_list_finalize_proxies (EmpathyTpContactList *list)
778 EmpathyTpContactListPriv *priv;
780 priv = GET_PRIV (list);
783 g_signal_handlers_disconnect_by_func (priv->tp_conn,
784 tp_contact_list_destroy_cb,
786 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
787 G_CALLBACK (tp_contact_list_newchannel_cb),
791 if (priv->aliasing_iface) {
792 dbus_g_proxy_disconnect_signal (priv->aliasing_iface,
794 G_CALLBACK (tp_contact_list_aliases_update_cb),
798 if (priv->avatars_iface) {
799 dbus_g_proxy_disconnect_signal (priv->avatars_iface,
801 G_CALLBACK (tp_contact_list_avatar_update_cb),
805 if (priv->presence_iface) {
806 dbus_g_proxy_disconnect_signal (priv->presence_iface,
808 G_CALLBACK (tp_contact_list_presence_update_cb),
814 tp_contact_list_destroy_cb (DBusGProxy *proxy,
815 EmpathyTpContactList *list)
817 EmpathyTpContactListPriv *priv;
819 priv = GET_PRIV (list);
821 gossip_debug (DEBUG_DOMAIN, "Connection destroyed... "
822 "Account disconnected or CM crashed");
824 /* DBus proxies should NOT be used anymore */
825 g_object_unref (priv->tp_conn);
826 priv->tp_conn = NULL;
827 priv->aliasing_iface = NULL;
828 priv->avatars_iface = NULL;
829 priv->presence_iface = NULL;
831 /* Remove all contacts */
832 g_hash_table_foreach (priv->contacts,
833 (GHFunc) tp_contact_list_contact_removed_foreach,
835 g_hash_table_remove_all (priv->contacts);
837 /* Tell the world to not use us anymore */
838 g_signal_emit (list, signals[DESTROY], 0);
842 tp_contact_list_contact_removed_foreach (guint handle,
843 GossipContact *contact,
844 EmpathyTpContactList *list)
846 g_signal_handlers_disconnect_by_func (contact,
847 tp_contact_list_groups_updated_cb,
849 g_signal_handlers_disconnect_by_func (contact,
850 tp_contact_list_subscription_updated_cb,
852 g_signal_handlers_disconnect_by_func (contact,
853 tp_contact_list_name_updated_cb,
856 g_signal_emit_by_name (list, "contact-removed", contact);
860 tp_contact_list_block_contact (EmpathyTpContactList *list,
861 GossipContact *contact)
863 g_signal_handlers_block_by_func (contact,
864 tp_contact_list_groups_updated_cb,
866 g_signal_handlers_block_by_func (contact,
867 tp_contact_list_subscription_updated_cb,
869 g_signal_handlers_block_by_func (contact,
870 tp_contact_list_name_updated_cb,
875 tp_contact_list_unblock_contact (EmpathyTpContactList *list,
876 GossipContact *contact)
878 g_signal_handlers_unblock_by_func (contact,
879 tp_contact_list_groups_updated_cb,
881 g_signal_handlers_unblock_by_func (contact,
882 tp_contact_list_subscription_updated_cb,
884 g_signal_handlers_unblock_by_func (contact,
885 tp_contact_list_name_updated_cb,
890 tp_contact_list_find_foreach (guint handle,
891 GossipContact *contact,
894 if (strcmp (gossip_contact_get_id (contact), id) == 0) {
902 tp_contact_list_newchannel_cb (DBusGProxy *proxy,
903 const gchar *object_path,
904 const gchar *channel_type,
905 TelepathyHandleType handle_type,
906 guint channel_handle,
907 gboolean suppress_handle,
908 EmpathyTpContactList *list)
910 EmpathyTpContactListPriv *priv;
911 GossipTelepathyGroup *group;
913 const gchar *bus_name;
916 priv = GET_PRIV (list);
918 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) != 0 ||
924 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn));
925 new_chan = tp_chan_new (tp_get_bus (),
928 channel_type, handle_type, channel_handle);
930 if (handle_type == TP_HANDLE_TYPE_LIST) {
931 TpContactListType list_type;
933 list_type = tp_contact_list_get_type (list, new_chan);
934 if (list_type == TP_CONTACT_LIST_TYPE_UNKNOWN) {
935 gossip_debug (DEBUG_DOMAIN, "Unknown contact list channel");
936 g_object_unref (new_chan);
940 gossip_debug (DEBUG_DOMAIN, "New contact list channel of type: %d",
943 group = gossip_telepathy_group_new (new_chan, priv->tp_conn);
946 case TP_CONTACT_LIST_TYPE_KNOWN:
948 g_object_unref (priv->known);
952 case TP_CONTACT_LIST_TYPE_PUBLISH:
954 g_object_unref (priv->publish);
956 priv->publish = group;
958 case TP_CONTACT_LIST_TYPE_SUBSCRIBE:
959 if (priv->subscribe) {
960 g_object_unref (priv->subscribe);
962 priv->subscribe = group;
965 g_assert_not_reached ();
968 /* Connect and setup the new contact-list group */
969 if (list_type == TP_CONTACT_LIST_TYPE_KNOWN ||
970 list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
971 g_signal_connect (group, "members-added",
972 G_CALLBACK (tp_contact_list_contact_added_cb),
974 g_signal_connect (group, "members-removed",
975 G_CALLBACK (tp_contact_list_contact_removed_cb),
978 members = gossip_telepathy_group_get_members (group);
979 tp_contact_list_contact_added_cb (group, members, 0,
980 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
982 g_array_free (members, TRUE);
984 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
988 g_signal_connect (group, "local-pending",
989 G_CALLBACK (tp_contact_list_local_pending_cb),
992 members = gossip_telepathy_group_get_local_pending_members_with_info (group);
994 g_object_unref (new_chan);
998 pending = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
999 for (l = members; l; l = l->next) {
1000 GossipTpGroupInfo *info;
1004 g_array_insert_val (pending, 0, info->member);
1005 tp_contact_list_local_pending_cb (group, pending,
1012 gossip_telepathy_group_info_list_free (members);
1013 g_array_free (pending, TRUE);
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,
1057 EmpathyTpContactListPriv *priv;
1059 gchar **handle_name;
1060 TpContactListType list_type;
1061 GError *error = NULL;
1063 priv = GET_PRIV (list);
1065 handles = g_array_new (FALSE, FALSE, sizeof (guint));
1066 g_array_append_val (handles, list_chan->handle);
1068 if (!tp_conn_inspect_handles (DBUS_G_PROXY (priv->tp_conn),
1069 TP_HANDLE_TYPE_LIST,
1073 gossip_debug (DEBUG_DOMAIN,
1074 "InspectHandle Error: %s",
1075 error ? error->message : "No error given");
1076 g_clear_error (&error);
1077 g_array_free (handles, TRUE);
1078 return TP_CONTACT_LIST_TYPE_UNKNOWN;
1081 if (strcmp (*handle_name, "subscribe") == 0) {
1082 list_type = TP_CONTACT_LIST_TYPE_SUBSCRIBE;
1083 } else if (strcmp (*handle_name, "publish") == 0) {
1084 list_type = TP_CONTACT_LIST_TYPE_PUBLISH;
1085 } else if (strcmp (*handle_name, "known") == 0) {
1086 list_type = TP_CONTACT_LIST_TYPE_KNOWN;
1088 list_type = TP_CONTACT_LIST_TYPE_UNKNOWN;
1091 g_strfreev (handle_name);
1092 g_array_free (handles, TRUE);
1098 tp_contact_list_contact_added_cb (GossipTelepathyGroup *group,
1102 const gchar *message,
1103 EmpathyTpContactList *list)
1105 EmpathyTpContactListPriv *priv;
1106 GList *added_list, *l;
1108 priv = GET_PRIV (list);
1110 added_list = empathy_tp_contact_list_get_from_handles (list, handles);
1112 for (l = added_list; l; l = l->next) {
1113 GossipContact *contact;
1115 contact = GOSSIP_CONTACT (l->data);
1116 tp_contact_list_block_contact (list, contact);
1117 gossip_contact_set_subscription (contact, GOSSIP_SUBSCRIPTION_BOTH);
1118 tp_contact_list_unblock_contact (list, contact);
1120 g_signal_emit_by_name (list, "contact-added", contact);
1122 g_object_unref (contact);
1125 g_list_free (added_list);
1129 tp_contact_list_contact_removed_cb (GossipTelepathyGroup *group,
1133 const gchar *message,
1134 EmpathyTpContactList *list)
1136 EmpathyTpContactListPriv *priv;
1137 GList *removed_list, *l;
1139 priv = GET_PRIV (list);
1141 removed_list = empathy_tp_contact_list_get_from_handles (list, handles);
1143 for (l = removed_list; l; l = l->next) {
1144 GossipContact *contact;
1147 contact = GOSSIP_CONTACT (l->data);
1149 handle = gossip_contact_get_handle (contact);
1150 g_hash_table_remove (priv->contacts, GUINT_TO_POINTER (handle));
1152 g_signal_emit_by_name (list, "contact-removed", contact);
1154 g_object_unref (contact);
1157 g_list_free (removed_list);
1161 tp_contact_list_local_pending_cb (GossipTelepathyGroup *group,
1165 const gchar *message,
1166 EmpathyTpContactList *list)
1168 EmpathyTpContactListPriv *priv;
1169 GList *pending_list, *l;
1171 priv = GET_PRIV (list);
1173 pending_list = empathy_tp_contact_list_get_from_handles (list, handles);
1175 for (l = pending_list; l; l = l->next) {
1176 GossipContact *contact;
1178 contact = GOSSIP_CONTACT (l->data);
1180 /* FIXME: Is that the correct way ? */
1181 tp_contact_list_block_contact (list, contact);
1182 gossip_contact_set_subscription (contact, GOSSIP_SUBSCRIPTION_FROM);
1183 tp_contact_list_unblock_contact (list, contact);
1184 g_signal_emit_by_name (list, "contact-added", contact);
1186 g_object_unref (contact);
1189 g_list_free (pending_list);
1193 tp_contact_list_groups_updated_cb (GossipContact *contact,
1195 EmpathyTpContactList *list)
1197 EmpathyTpContactListPriv *priv;
1198 TpContactListData data;
1201 priv = GET_PRIV (list);
1203 /* Make sure all groups are created */
1204 groups = gossip_contact_get_groups (contact);
1205 for (l = groups; l; l = l->next) {
1206 tp_contact_list_get_group (list, l->data);
1209 data.handle = gossip_contact_get_handle (contact);
1210 data.new_groups = groups;
1212 g_hash_table_foreach (priv->groups,
1213 (GHFunc) tp_contact_list_update_groups_foreach,
1218 tp_contact_list_subscription_updated_cb (GossipContact *contact,
1220 EmpathyTpContactList *list)
1222 EmpathyTpContactListPriv *priv;
1223 GossipSubscription subscription;
1226 priv = GET_PRIV (list);
1228 subscription = gossip_contact_get_subscription (contact);
1229 handle = gossip_contact_get_handle (contact);
1231 /* FIXME: what to do here, I'm a bit lost... */
1233 gossip_telepathy_group_add_member (priv->publish, handle, "");
1235 gossip_telepathy_group_remove_member (priv->publish, handle, "");
1240 tp_contact_list_name_updated_cb (GossipContact *contact,
1242 EmpathyTpContactList *list)
1244 EmpathyTpContactListPriv *priv;
1245 GHashTable *new_alias;
1246 const gchar *new_name;
1248 GError *error = NULL;
1250 priv = GET_PRIV (list);
1252 handle = gossip_contact_get_handle (contact);
1253 new_name = gossip_contact_get_name (contact);
1255 gossip_debug (DEBUG_DOMAIN, "renaming handle %d to %s",
1258 new_alias = g_hash_table_new_full (g_direct_hash,
1263 g_hash_table_insert (new_alias,
1264 GUINT_TO_POINTER (handle),
1265 g_strdup (new_name));
1267 if (!tp_conn_iface_aliasing_set_aliases (priv->aliasing_iface,
1270 gossip_debug (DEBUG_DOMAIN,
1271 "Couldn't rename contact: %s",
1272 error ? error->message : "No error given");
1273 g_clear_error (&error);
1276 g_hash_table_destroy (new_alias);
1280 tp_contact_list_update_groups_foreach (gchar *object_path,
1281 GossipTelepathyGroup *group,
1282 TpContactListData *data)
1285 gboolean found = FALSE;
1286 const gchar *group_name;
1289 is_member = gossip_telepathy_group_is_member (group, data->handle);
1290 group_name = gossip_telepathy_group_get_name (group);
1292 for (l = data->new_groups; l; l = l->next) {
1293 if (strcmp (group_name, l->data) == 0) {
1299 if (is_member && !found) {
1300 /* We are no longer member of this group */
1301 gossip_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1302 data->handle, group_name);
1303 gossip_telepathy_group_remove_member (group, data->handle, "");
1306 if (!is_member && found) {
1307 /* We are now member of this group */
1308 gossip_debug (DEBUG_DOMAIN, "Contact %d added to group '%s'",
1309 data->handle, group_name);
1310 gossip_telepathy_group_add_member (group, data->handle, "");
1314 static GossipTelepathyGroup *
1315 tp_contact_list_get_group (EmpathyTpContactList *list,
1318 EmpathyTpContactListPriv *priv;
1319 GossipTelepathyGroup *group;
1320 TpChan *group_channel;
1323 char *group_object_path;
1324 const char *names[2] = {name, NULL};
1325 GError *error = NULL;
1327 priv = GET_PRIV (list);
1329 group = g_hash_table_find (priv->groups,
1330 (GHRFunc) tp_contact_list_find_group,
1336 gossip_debug (DEBUG_DOMAIN, "creating new group: %s", name);
1338 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
1339 TP_HANDLE_TYPE_GROUP,
1343 gossip_debug (DEBUG_DOMAIN,
1344 "Couldn't request the creation of a new handle for group: %s",
1345 error ? error->message : "No error given");
1346 g_clear_error (&error);
1349 group_handle = g_array_index (handles, guint, 0);
1350 g_array_free (handles, TRUE);
1352 if (!tp_conn_request_channel (DBUS_G_PROXY (priv->tp_conn),
1353 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1354 TP_HANDLE_TYPE_GROUP,
1359 gossip_debug (DEBUG_DOMAIN,
1360 "Couldn't request the creation of a new group channel: %s",
1361 error ? error->message : "No error given");
1362 g_clear_error (&error);
1366 group_channel = tp_chan_new (tp_get_bus (),
1367 dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn)),
1369 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1370 TP_HANDLE_TYPE_GROUP,
1373 dbus_g_proxy_connect_signal (DBUS_G_PROXY (group_channel),
1376 (tp_contact_list_group_channel_closed_cb),
1380 group = gossip_telepathy_group_new (group_channel, priv->tp_conn);
1381 g_hash_table_insert (priv->groups, group_object_path, group);
1382 g_signal_connect (group, "members-added",
1383 G_CALLBACK (tp_contact_list_group_members_added_cb),
1385 g_signal_connect (group, "members-removed",
1386 G_CALLBACK (tp_contact_list_group_members_removed_cb),
1393 tp_contact_list_find_group (gchar *key,
1394 GossipTelepathyGroup *group,
1397 if (strcmp (group_name, gossip_telepathy_group_get_name (group)) == 0) {
1405 tp_contact_list_get_groups_foreach (gchar *key,
1406 GossipTelepathyGroup *group,
1411 name = gossip_telepathy_group_get_name (group);
1412 *groups = g_list_append (*groups, g_strdup (name));
1416 tp_contact_list_group_channel_closed_cb (TpChan *channel,
1417 EmpathyTpContactList *list)
1419 EmpathyTpContactListPriv *priv;
1421 priv = GET_PRIV (list);
1423 g_hash_table_remove (priv->groups,
1424 dbus_g_proxy_get_path (DBUS_G_PROXY (channel)));
1428 tp_contact_list_group_members_added_cb (GossipTelepathyGroup *group,
1432 const gchar *message,
1433 EmpathyTpContactList *list)
1435 EmpathyTpContactListPriv *priv;
1436 GList *added_list, *l;
1437 const gchar *group_name;
1439 priv = GET_PRIV (list);
1441 group_name = gossip_telepathy_group_get_name (group);
1442 added_list = empathy_tp_contact_list_get_from_handles (list, members);
1444 for (l = added_list; l; l = l->next) {
1445 GossipContact *contact;
1446 GList *contact_groups;
1448 contact = GOSSIP_CONTACT (l->data);
1449 contact_groups = gossip_contact_get_groups (contact);
1451 if (!g_list_find_custom (contact_groups,
1453 (GCompareFunc) strcmp)) {
1454 gossip_debug (DEBUG_DOMAIN, "Contact %s added to group '%s'",
1455 gossip_contact_get_name (contact),
1457 contact_groups = g_list_append (contact_groups,
1458 g_strdup (group_name));
1459 tp_contact_list_block_contact (list, contact);
1460 gossip_contact_set_groups (contact, contact_groups);
1461 tp_contact_list_unblock_contact (list, contact);
1464 g_object_unref (contact);
1467 g_list_free (added_list);
1471 tp_contact_list_group_members_removed_cb (GossipTelepathyGroup *group,
1475 const gchar *message,
1476 EmpathyTpContactList *list)
1478 EmpathyTpContactListPriv *priv;
1479 GList *removed_list, *l;
1480 const gchar *group_name;
1482 priv = GET_PRIV (list);
1484 group_name = gossip_telepathy_group_get_name (group);
1485 removed_list = empathy_tp_contact_list_get_from_handles (list, members);
1487 for (l = removed_list; l; l = l->next) {
1488 GossipContact *contact;
1489 GList *contact_groups;
1492 /* FIXME: Does it leak ? */
1493 contact = GOSSIP_CONTACT (l->data);
1494 contact_groups = gossip_contact_get_groups (contact);
1495 contact_groups = g_list_copy (contact_groups);
1497 to_remove = g_list_find_custom (contact_groups,
1499 (GCompareFunc) strcmp);
1501 gossip_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1502 gossip_contact_get_handle (contact),
1504 contact_groups = g_list_remove_link (contact_groups,
1506 tp_contact_list_block_contact (list, contact);
1507 gossip_contact_set_groups (contact, contact_groups);
1508 tp_contact_list_unblock_contact (list, contact);
1511 g_list_free (contact_groups);
1513 g_object_unref (contact);
1516 g_list_free (removed_list);
1520 tp_contact_list_get_contacts_foreach (guint handle,
1521 GossipContact *contact,
1524 *contacts = g_list_append (*contacts, g_object_ref (contact));
1528 tp_contact_list_get_info (EmpathyTpContactList *list,
1531 EmpathyTpContactListPriv *priv;
1532 GError *error = NULL;
1534 priv = GET_PRIV (list);
1536 if (priv->presence_iface) {
1537 /* FIXME: We should use GetPresence instead */
1538 if (!tp_conn_iface_presence_request_presence (priv->presence_iface,
1540 gossip_debug (DEBUG_DOMAIN,
1541 "Could not request presences: %s",
1542 error ? error->message : "No error given");
1543 g_clear_error (&error);
1547 if (priv->aliasing_iface) {
1548 TpContactListAliasesRequestData *data;
1550 data = g_slice_new (TpContactListAliasesRequestData);
1552 data->handles = g_memdup (handles->data, handles->len * sizeof (guint));
1554 tp_conn_iface_aliasing_request_aliases_async (priv->aliasing_iface,
1556 (tp_conn_iface_aliasing_request_aliases_reply)
1557 tp_contact_list_request_aliases_cb,
1561 if (priv->avatars_iface) {
1564 for (i = 0; i < handles->len; i++) {
1567 handle = g_array_index (handles, gint, i);
1568 tp_contact_list_request_avatar (list, handle);
1574 tp_contact_list_request_avatar (EmpathyTpContactList *list,
1577 EmpathyTpContactListPriv *priv;
1579 priv = GET_PRIV (list);
1581 /* We queue avatar requests to not send too many dbus async
1582 * calls at once. If we don't we reach the dbus's limit of
1584 priv->avatar_requests_queue = g_list_append (priv->avatar_requests_queue,
1585 GUINT_TO_POINTER (handle));
1586 tp_contact_list_start_avatar_requests (list);
1590 tp_contact_list_start_avatar_requests (EmpathyTpContactList *list)
1592 EmpathyTpContactListPriv *priv;
1593 TpContactListAvatarRequestData *data;
1595 priv = GET_PRIV (list);
1597 while (n_avatar_requests < MAX_AVATAR_REQUESTS &&
1598 priv->avatar_requests_queue) {
1599 data = g_slice_new (TpContactListAvatarRequestData);
1601 data->handle = GPOINTER_TO_UINT (priv->avatar_requests_queue->data);
1603 n_avatar_requests++;
1604 priv->avatar_requests_queue = g_list_remove (priv->avatar_requests_queue,
1605 priv->avatar_requests_queue->data);
1607 tp_conn_iface_avatars_request_avatar_async (priv->avatars_iface,
1609 (tp_conn_iface_avatars_request_avatar_reply)
1610 tp_contact_list_request_avatar_cb,
1616 tp_contact_list_avatar_update_cb (DBusGProxy *proxy,
1619 EmpathyTpContactList *list)
1621 EmpathyTpContactListPriv *priv;
1623 priv = GET_PRIV (list);
1625 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1626 /* We don't know this contact, skip */
1630 gossip_debug (DEBUG_DOMAIN, "Changing avatar for %d to %s",
1633 tp_contact_list_request_avatar (list, handle);
1637 tp_contact_list_request_avatar_cb (DBusGProxy *proxy,
1638 GArray *avatar_data,
1641 TpContactListAvatarRequestData *data)
1643 GossipContact *contact;
1645 contact = empathy_tp_contact_list_get_from_handle (data->list, data->handle);
1648 gossip_debug (DEBUG_DOMAIN, "Error requesting avatar for %s: %s",
1649 gossip_contact_get_name (contact),
1650 error ? error->message : "No error given");
1652 GossipAvatar *avatar;
1654 avatar = gossip_avatar_new (avatar_data->data,
1657 tp_contact_list_block_contact (data->list, contact);
1658 gossip_contact_set_avatar (contact, avatar);
1659 tp_contact_list_unblock_contact (data->list, contact);
1660 gossip_avatar_unref (avatar);
1663 n_avatar_requests--;
1664 tp_contact_list_start_avatar_requests (data->list);
1666 g_slice_free (TpContactListAvatarRequestData, data);
1670 tp_contact_list_aliases_update_cb (DBusGProxy *proxy,
1671 GPtrArray *renamed_handlers,
1672 EmpathyTpContactList *list)
1674 EmpathyTpContactListPriv *priv;
1677 priv = GET_PRIV (list);
1679 for (i = 0; renamed_handlers->len > i; i++) {
1682 GValueArray *renamed_struct;
1683 GossipContact *contact;
1685 renamed_struct = g_ptr_array_index (renamed_handlers, i);
1686 handle = g_value_get_uint(g_value_array_get_nth (renamed_struct, 0));
1687 alias = g_value_get_string(g_value_array_get_nth (renamed_struct, 1));
1689 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1690 /* We don't know this contact, skip */
1694 if (G_STR_EMPTY (alias)) {
1698 contact = empathy_tp_contact_list_get_from_handle (list, handle);
1699 tp_contact_list_block_contact (list, contact);
1700 gossip_contact_set_name (contact, alias);
1701 tp_contact_list_unblock_contact (list, contact);
1703 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (update cb)",
1709 tp_contact_list_request_aliases_cb (DBusGProxy *proxy,
1710 gchar **contact_names,
1712 TpContactListAliasesRequestData *data)
1718 gossip_debug (DEBUG_DOMAIN, "Error requesting aliases: %s",
1722 for (name = contact_names; *name; name++) {
1723 GossipContact *contact;
1725 contact = empathy_tp_contact_list_get_from_handle (data->list,
1727 tp_contact_list_block_contact (data->list, contact);
1728 gossip_contact_set_name (contact, *name);
1729 tp_contact_list_unblock_contact (data->list, contact);
1731 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (request cb)",
1732 data->handles[i], *name);
1737 g_free (data->handles);
1738 g_slice_free (TpContactListAliasesRequestData, data);
1742 tp_contact_list_presence_update_cb (DBusGProxy *proxy,
1743 GHashTable *handle_table,
1744 EmpathyTpContactList *list)
1746 g_hash_table_foreach (handle_table,
1747 (GHFunc) tp_contact_list_parse_presence_foreach,
1752 tp_contact_list_parse_presence_foreach (guint handle,
1753 GValueArray *presence_struct,
1754 EmpathyTpContactList *list)
1756 EmpathyTpContactListPriv *priv;
1757 GHashTable *presences_table;
1758 GossipContact *contact;
1759 GossipPresence *presence = NULL;
1761 priv = GET_PRIV (list);
1763 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1764 /* We don't know this contact, skip */
1768 contact = empathy_tp_contact_list_get_from_handle (list, handle);
1769 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
1771 g_hash_table_foreach (presences_table,
1772 (GHFunc) tp_contact_list_presences_table_foreach,
1775 gossip_debug (DEBUG_DOMAIN, "Presence changed for %s (%d) to %s (%d)",
1776 gossip_contact_get_name (contact),
1778 presence ? gossip_presence_get_status (presence) : "unset",
1779 presence ? gossip_presence_get_state (presence) : MC_PRESENCE_UNSET);
1781 tp_contact_list_block_contact (list, contact);
1782 gossip_contact_set_presence (contact, presence);
1783 tp_contact_list_unblock_contact (list, contact);
1787 tp_contact_list_presences_table_foreach (const gchar *state_str,
1788 GHashTable *presences_table,
1789 GossipPresence **presence)
1792 const GValue *message;
1794 state = gossip_presence_state_from_str (state_str);
1795 if ((state == MC_PRESENCE_UNSET) || (state == MC_PRESENCE_OFFLINE)) {
1800 g_object_unref (*presence);
1804 *presence = gossip_presence_new ();
1805 gossip_presence_set_state (*presence, state);
1807 message = g_hash_table_lookup (presences_table, "message");
1808 if (message != NULL) {
1809 gossip_presence_set_status (*presence,
1810 g_value_get_string (message));
1815 tp_contact_list_status_changed_cb (MissionControl *mc,
1816 TelepathyConnectionStatus status,
1817 McPresence presence,
1818 TelepathyConnectionStatusReason reason,
1819 const gchar *unique_name,
1820 EmpathyTpContactList *list)
1822 EmpathyTpContactListPriv *priv;
1825 priv = GET_PRIV (list);
1827 account = mc_account_lookup (unique_name);
1828 if (status != TP_CONN_STATUS_DISCONNECTED ||
1829 !gossip_account_equal (account, priv->account)) {
1830 g_object_unref (account);
1834 /* We are disconnected, do just like if the connection was destroyed */
1835 g_signal_handlers_disconnect_by_func (priv->tp_conn,
1836 tp_contact_list_destroy_cb,
1838 tp_contact_list_destroy_cb (DBUS_G_PROXY (priv->tp_conn), list);
1840 g_object_unref (account);