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) {
644 g_array_free (new_handles, TRUE);
648 /* Holds all handles we don't have yet.
649 * FIXME: We should release them at some point. */
650 if (!tp_conn_hold_handles (DBUS_G_PROXY (priv->tp_conn),
651 TP_HANDLE_TYPE_CONTACT,
652 new_handles, &error)) {
653 gossip_debug (DEBUG_DOMAIN,
654 "HoldHandles Error: %s",
655 error ? error->message : "No error given");
656 g_clear_error (&error);
657 g_array_free (new_handles, TRUE);
661 /* Get the IDs of all new handles */
662 if (!tp_conn_inspect_handles (DBUS_G_PROXY (priv->tp_conn),
663 TP_HANDLE_TYPE_CONTACT,
667 gossip_debug (DEBUG_DOMAIN,
668 "InspectHandle Error: %s",
669 error ? error->message : "No error given");
670 g_clear_error (&error);
671 g_array_free (new_handles, TRUE);
675 /* Create contact objects */
676 for (i = 0, id = handles_names; *id && i < new_handles->len; id++, i++) {
677 GossipContact *contact;
680 handle = g_array_index (new_handles, guint, i);
681 contact = g_object_new (GOSSIP_TYPE_CONTACT,
682 "account", priv->account,
687 g_signal_connect (contact, "notify::groups",
688 G_CALLBACK (tp_contact_list_groups_updated_cb),
690 g_signal_connect (contact, "notify::subscription",
691 G_CALLBACK (tp_contact_list_subscription_updated_cb),
693 g_signal_connect (contact, "notify::name",
694 G_CALLBACK (tp_contact_list_name_updated_cb),
697 gossip_debug (DEBUG_DOMAIN, "new contact created: %s (%d)",
700 g_hash_table_insert (priv->contacts,
701 GUINT_TO_POINTER (handle),
704 contacts = g_list_prepend (contacts, g_object_ref (contact));
707 tp_contact_list_get_info (list, new_handles);
709 g_array_free (new_handles, TRUE);
710 g_strfreev (handles_names);
716 empathy_tp_contact_list_rename_group (EmpathyTpContactList *list,
717 const gchar *old_group,
718 const gchar *new_group)
720 EmpathyTpContactListPriv *priv;
721 GossipTelepathyGroup *group;
724 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
725 g_return_if_fail (old_group != NULL);
726 g_return_if_fail (new_group != NULL);
728 priv = GET_PRIV (list);
730 group = g_hash_table_find (priv->groups,
731 (GHRFunc) tp_contact_list_find_group,
734 /* The group doesn't exists on this account */
738 gossip_debug (DEBUG_DOMAIN, "rename group %s to %s", group, new_group);
740 /* Remove all members from the old group */
741 members = gossip_telepathy_group_get_members (group);
742 gossip_telepathy_group_remove_members (group, members, "");
743 tp_contact_list_group_members_removed_cb (group, members,
745 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
747 g_hash_table_remove (priv->groups,
748 gossip_telepathy_group_get_object_path (group));
750 /* Add all members to the new group */
751 group = tp_contact_list_get_group (list, new_group);
753 gossip_telepathy_group_add_members (group, members, "");
758 empathy_tp_contact_list_get_groups (EmpathyTpContactList *list)
760 EmpathyTpContactListPriv *priv;
761 GList *groups = NULL;
763 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
765 priv = GET_PRIV (list);
767 g_hash_table_foreach (priv->groups,
768 (GHFunc) tp_contact_list_get_groups_foreach,
771 groups = g_list_sort (groups, (GCompareFunc) strcmp);
777 tp_contact_list_finalize_proxies (EmpathyTpContactList *list)
779 EmpathyTpContactListPriv *priv;
781 priv = GET_PRIV (list);
784 g_signal_handlers_disconnect_by_func (priv->tp_conn,
785 tp_contact_list_destroy_cb,
787 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
788 G_CALLBACK (tp_contact_list_newchannel_cb),
792 if (priv->aliasing_iface) {
793 dbus_g_proxy_disconnect_signal (priv->aliasing_iface,
795 G_CALLBACK (tp_contact_list_aliases_update_cb),
799 if (priv->avatars_iface) {
800 dbus_g_proxy_disconnect_signal (priv->avatars_iface,
802 G_CALLBACK (tp_contact_list_avatar_update_cb),
806 if (priv->presence_iface) {
807 dbus_g_proxy_disconnect_signal (priv->presence_iface,
809 G_CALLBACK (tp_contact_list_presence_update_cb),
815 tp_contact_list_destroy_cb (DBusGProxy *proxy,
816 EmpathyTpContactList *list)
818 EmpathyTpContactListPriv *priv;
820 priv = GET_PRIV (list);
822 gossip_debug (DEBUG_DOMAIN, "Connection destroyed... "
823 "Account disconnected or CM crashed");
825 /* DBus proxies should NOT be used anymore */
826 g_object_unref (priv->tp_conn);
827 priv->tp_conn = NULL;
828 priv->aliasing_iface = NULL;
829 priv->avatars_iface = NULL;
830 priv->presence_iface = NULL;
832 /* Remove all contacts */
833 g_hash_table_foreach (priv->contacts,
834 (GHFunc) tp_contact_list_contact_removed_foreach,
836 g_hash_table_remove_all (priv->contacts);
838 /* Tell the world to not use us anymore */
839 g_signal_emit (list, signals[DESTROY], 0);
843 tp_contact_list_contact_removed_foreach (guint handle,
844 GossipContact *contact,
845 EmpathyTpContactList *list)
847 g_signal_handlers_disconnect_by_func (contact,
848 tp_contact_list_groups_updated_cb,
850 g_signal_handlers_disconnect_by_func (contact,
851 tp_contact_list_subscription_updated_cb,
853 g_signal_handlers_disconnect_by_func (contact,
854 tp_contact_list_name_updated_cb,
857 g_signal_emit_by_name (list, "contact-removed", contact);
861 tp_contact_list_block_contact (EmpathyTpContactList *list,
862 GossipContact *contact)
864 g_signal_handlers_block_by_func (contact,
865 tp_contact_list_groups_updated_cb,
867 g_signal_handlers_block_by_func (contact,
868 tp_contact_list_subscription_updated_cb,
870 g_signal_handlers_block_by_func (contact,
871 tp_contact_list_name_updated_cb,
876 tp_contact_list_unblock_contact (EmpathyTpContactList *list,
877 GossipContact *contact)
879 g_signal_handlers_unblock_by_func (contact,
880 tp_contact_list_groups_updated_cb,
882 g_signal_handlers_unblock_by_func (contact,
883 tp_contact_list_subscription_updated_cb,
885 g_signal_handlers_unblock_by_func (contact,
886 tp_contact_list_name_updated_cb,
891 tp_contact_list_find_foreach (guint handle,
892 GossipContact *contact,
895 if (strcmp (gossip_contact_get_id (contact), id) == 0) {
903 tp_contact_list_newchannel_cb (DBusGProxy *proxy,
904 const gchar *object_path,
905 const gchar *channel_type,
906 TelepathyHandleType handle_type,
907 guint channel_handle,
908 gboolean suppress_handle,
909 EmpathyTpContactList *list)
911 EmpathyTpContactListPriv *priv;
912 GossipTelepathyGroup *group;
914 const gchar *bus_name;
917 priv = GET_PRIV (list);
919 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) != 0 ||
925 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn));
926 new_chan = tp_chan_new (tp_get_bus (),
929 channel_type, handle_type, channel_handle);
931 if (handle_type == TP_HANDLE_TYPE_LIST) {
932 TpContactListType list_type;
934 list_type = tp_contact_list_get_type (list, new_chan);
935 if (list_type == TP_CONTACT_LIST_TYPE_UNKNOWN) {
936 gossip_debug (DEBUG_DOMAIN, "Unknown contact list channel");
937 g_object_unref (new_chan);
941 gossip_debug (DEBUG_DOMAIN, "New contact list channel of type: %d",
944 group = gossip_telepathy_group_new (new_chan, priv->tp_conn);
947 case TP_CONTACT_LIST_TYPE_KNOWN:
949 g_object_unref (priv->known);
953 case TP_CONTACT_LIST_TYPE_PUBLISH:
955 g_object_unref (priv->publish);
957 priv->publish = group;
959 case TP_CONTACT_LIST_TYPE_SUBSCRIBE:
960 if (priv->subscribe) {
961 g_object_unref (priv->subscribe);
963 priv->subscribe = group;
966 g_assert_not_reached ();
969 /* Connect and setup the new contact-list group */
970 if (list_type == TP_CONTACT_LIST_TYPE_KNOWN ||
971 list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
972 g_signal_connect (group, "members-added",
973 G_CALLBACK (tp_contact_list_contact_added_cb),
975 g_signal_connect (group, "members-removed",
976 G_CALLBACK (tp_contact_list_contact_removed_cb),
979 members = gossip_telepathy_group_get_members (group);
980 tp_contact_list_contact_added_cb (group, members, 0,
981 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
983 g_array_free (members, TRUE);
985 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
989 g_signal_connect (group, "local-pending",
990 G_CALLBACK (tp_contact_list_local_pending_cb),
993 members = gossip_telepathy_group_get_local_pending_members_with_info (group);
995 g_object_unref (new_chan);
999 pending = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1000 for (l = members; l; l = l->next) {
1001 GossipTpGroupInfo *info;
1005 g_array_insert_val (pending, 0, info->member);
1006 tp_contact_list_local_pending_cb (group, pending,
1013 gossip_telepathy_group_info_list_free (members);
1014 g_array_free (pending, TRUE);
1017 else if (handle_type == TP_HANDLE_TYPE_GROUP) {
1018 const gchar *object_path;
1020 object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (new_chan));
1021 if (g_hash_table_lookup (priv->groups, object_path)) {
1022 g_object_unref (new_chan);
1026 group = gossip_telepathy_group_new (new_chan, priv->tp_conn);
1028 gossip_debug (DEBUG_DOMAIN, "New server-side group channel: %s",
1029 gossip_telepathy_group_get_name (group));
1031 dbus_g_proxy_connect_signal (DBUS_G_PROXY (new_chan), "Closed",
1033 (tp_contact_list_group_channel_closed_cb),
1036 g_hash_table_insert (priv->groups, g_strdup (object_path), group);
1037 g_signal_connect (group, "members-added",
1038 G_CALLBACK (tp_contact_list_group_members_added_cb),
1040 g_signal_connect (group, "members-removed",
1041 G_CALLBACK (tp_contact_list_group_members_removed_cb),
1044 members = gossip_telepathy_group_get_members (group);
1045 tp_contact_list_group_members_added_cb (group, members, 0,
1046 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1048 g_array_free (members, TRUE);
1051 g_object_unref (new_chan);
1054 static TpContactListType
1055 tp_contact_list_get_type (EmpathyTpContactList *list,
1058 EmpathyTpContactListPriv *priv;
1060 gchar **handle_name;
1061 TpContactListType list_type;
1062 GError *error = NULL;
1064 priv = GET_PRIV (list);
1066 handles = g_array_new (FALSE, FALSE, sizeof (guint));
1067 g_array_append_val (handles, list_chan->handle);
1069 if (!tp_conn_inspect_handles (DBUS_G_PROXY (priv->tp_conn),
1070 TP_HANDLE_TYPE_LIST,
1074 gossip_debug (DEBUG_DOMAIN,
1075 "InspectHandle Error: %s",
1076 error ? error->message : "No error given");
1077 g_clear_error (&error);
1078 g_array_free (handles, TRUE);
1079 return TP_CONTACT_LIST_TYPE_UNKNOWN;
1082 if (strcmp (*handle_name, "subscribe") == 0) {
1083 list_type = TP_CONTACT_LIST_TYPE_SUBSCRIBE;
1084 } else if (strcmp (*handle_name, "publish") == 0) {
1085 list_type = TP_CONTACT_LIST_TYPE_PUBLISH;
1086 } else if (strcmp (*handle_name, "known") == 0) {
1087 list_type = TP_CONTACT_LIST_TYPE_KNOWN;
1089 list_type = TP_CONTACT_LIST_TYPE_UNKNOWN;
1092 g_strfreev (handle_name);
1093 g_array_free (handles, TRUE);
1099 tp_contact_list_contact_added_cb (GossipTelepathyGroup *group,
1103 const gchar *message,
1104 EmpathyTpContactList *list)
1106 EmpathyTpContactListPriv *priv;
1107 GList *added_list, *l;
1109 priv = GET_PRIV (list);
1111 added_list = empathy_tp_contact_list_get_from_handles (list, handles);
1113 for (l = added_list; l; l = l->next) {
1114 GossipContact *contact;
1116 contact = GOSSIP_CONTACT (l->data);
1117 tp_contact_list_block_contact (list, contact);
1118 gossip_contact_set_subscription (contact, GOSSIP_SUBSCRIPTION_BOTH);
1119 tp_contact_list_unblock_contact (list, contact);
1121 g_signal_emit_by_name (list, "contact-added", contact);
1123 g_object_unref (contact);
1126 g_list_free (added_list);
1130 tp_contact_list_contact_removed_cb (GossipTelepathyGroup *group,
1134 const gchar *message,
1135 EmpathyTpContactList *list)
1137 EmpathyTpContactListPriv *priv;
1138 GList *removed_list, *l;
1140 priv = GET_PRIV (list);
1142 removed_list = empathy_tp_contact_list_get_from_handles (list, handles);
1144 for (l = removed_list; l; l = l->next) {
1145 GossipContact *contact;
1148 contact = GOSSIP_CONTACT (l->data);
1150 handle = gossip_contact_get_handle (contact);
1151 g_hash_table_remove (priv->contacts, GUINT_TO_POINTER (handle));
1153 g_signal_emit_by_name (list, "contact-removed", contact);
1155 g_object_unref (contact);
1158 g_list_free (removed_list);
1162 tp_contact_list_local_pending_cb (GossipTelepathyGroup *group,
1166 const gchar *message,
1167 EmpathyTpContactList *list)
1169 EmpathyTpContactListPriv *priv;
1170 GList *pending_list, *l;
1172 priv = GET_PRIV (list);
1174 pending_list = empathy_tp_contact_list_get_from_handles (list, handles);
1176 for (l = pending_list; l; l = l->next) {
1177 GossipContact *contact;
1179 contact = GOSSIP_CONTACT (l->data);
1181 /* FIXME: Is that the correct way ? */
1182 tp_contact_list_block_contact (list, contact);
1183 gossip_contact_set_subscription (contact, GOSSIP_SUBSCRIPTION_FROM);
1184 tp_contact_list_unblock_contact (list, contact);
1185 g_signal_emit_by_name (list, "contact-added", contact);
1187 g_object_unref (contact);
1190 g_list_free (pending_list);
1194 tp_contact_list_groups_updated_cb (GossipContact *contact,
1196 EmpathyTpContactList *list)
1198 EmpathyTpContactListPriv *priv;
1199 TpContactListData data;
1202 priv = GET_PRIV (list);
1204 /* Make sure all groups are created */
1205 groups = gossip_contact_get_groups (contact);
1206 for (l = groups; l; l = l->next) {
1207 tp_contact_list_get_group (list, l->data);
1210 data.handle = gossip_contact_get_handle (contact);
1211 data.new_groups = groups;
1213 g_hash_table_foreach (priv->groups,
1214 (GHFunc) tp_contact_list_update_groups_foreach,
1219 tp_contact_list_subscription_updated_cb (GossipContact *contact,
1221 EmpathyTpContactList *list)
1223 EmpathyTpContactListPriv *priv;
1224 GossipSubscription subscription;
1227 priv = GET_PRIV (list);
1229 subscription = gossip_contact_get_subscription (contact);
1230 handle = gossip_contact_get_handle (contact);
1232 /* FIXME: what to do here, I'm a bit lost... */
1234 gossip_telepathy_group_add_member (priv->publish, handle, "");
1236 gossip_telepathy_group_remove_member (priv->publish, handle, "");
1241 tp_contact_list_name_updated_cb (GossipContact *contact,
1243 EmpathyTpContactList *list)
1245 EmpathyTpContactListPriv *priv;
1246 GHashTable *new_alias;
1247 const gchar *new_name;
1249 GError *error = NULL;
1251 priv = GET_PRIV (list);
1253 handle = gossip_contact_get_handle (contact);
1254 new_name = gossip_contact_get_name (contact);
1256 gossip_debug (DEBUG_DOMAIN, "renaming handle %d to %s",
1259 new_alias = g_hash_table_new_full (g_direct_hash,
1264 g_hash_table_insert (new_alias,
1265 GUINT_TO_POINTER (handle),
1266 g_strdup (new_name));
1268 if (!tp_conn_iface_aliasing_set_aliases (priv->aliasing_iface,
1271 gossip_debug (DEBUG_DOMAIN,
1272 "Couldn't rename contact: %s",
1273 error ? error->message : "No error given");
1274 g_clear_error (&error);
1277 g_hash_table_destroy (new_alias);
1281 tp_contact_list_update_groups_foreach (gchar *object_path,
1282 GossipTelepathyGroup *group,
1283 TpContactListData *data)
1286 gboolean found = FALSE;
1287 const gchar *group_name;
1290 is_member = gossip_telepathy_group_is_member (group, data->handle);
1291 group_name = gossip_telepathy_group_get_name (group);
1293 for (l = data->new_groups; l; l = l->next) {
1294 if (strcmp (group_name, l->data) == 0) {
1300 if (is_member && !found) {
1301 /* We are no longer member of this group */
1302 gossip_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1303 data->handle, group_name);
1304 gossip_telepathy_group_remove_member (group, data->handle, "");
1307 if (!is_member && found) {
1308 /* We are now member of this group */
1309 gossip_debug (DEBUG_DOMAIN, "Contact %d added to group '%s'",
1310 data->handle, group_name);
1311 gossip_telepathy_group_add_member (group, data->handle, "");
1315 static GossipTelepathyGroup *
1316 tp_contact_list_get_group (EmpathyTpContactList *list,
1319 EmpathyTpContactListPriv *priv;
1320 GossipTelepathyGroup *group;
1321 TpChan *group_channel;
1324 char *group_object_path;
1325 const char *names[2] = {name, NULL};
1326 GError *error = NULL;
1328 priv = GET_PRIV (list);
1330 group = g_hash_table_find (priv->groups,
1331 (GHRFunc) tp_contact_list_find_group,
1337 gossip_debug (DEBUG_DOMAIN, "creating new group: %s", name);
1339 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
1340 TP_HANDLE_TYPE_GROUP,
1344 gossip_debug (DEBUG_DOMAIN,
1345 "Couldn't request the creation of a new handle for group: %s",
1346 error ? error->message : "No error given");
1347 g_clear_error (&error);
1350 group_handle = g_array_index (handles, guint, 0);
1351 g_array_free (handles, TRUE);
1353 if (!tp_conn_request_channel (DBUS_G_PROXY (priv->tp_conn),
1354 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1355 TP_HANDLE_TYPE_GROUP,
1360 gossip_debug (DEBUG_DOMAIN,
1361 "Couldn't request the creation of a new group channel: %s",
1362 error ? error->message : "No error given");
1363 g_clear_error (&error);
1367 group_channel = tp_chan_new (tp_get_bus (),
1368 dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn)),
1370 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1371 TP_HANDLE_TYPE_GROUP,
1374 dbus_g_proxy_connect_signal (DBUS_G_PROXY (group_channel),
1377 (tp_contact_list_group_channel_closed_cb),
1381 group = gossip_telepathy_group_new (group_channel, priv->tp_conn);
1382 g_hash_table_insert (priv->groups, group_object_path, group);
1383 g_signal_connect (group, "members-added",
1384 G_CALLBACK (tp_contact_list_group_members_added_cb),
1386 g_signal_connect (group, "members-removed",
1387 G_CALLBACK (tp_contact_list_group_members_removed_cb),
1394 tp_contact_list_find_group (gchar *key,
1395 GossipTelepathyGroup *group,
1398 if (strcmp (group_name, gossip_telepathy_group_get_name (group)) == 0) {
1406 tp_contact_list_get_groups_foreach (gchar *key,
1407 GossipTelepathyGroup *group,
1412 name = gossip_telepathy_group_get_name (group);
1413 *groups = g_list_append (*groups, g_strdup (name));
1417 tp_contact_list_group_channel_closed_cb (TpChan *channel,
1418 EmpathyTpContactList *list)
1420 EmpathyTpContactListPriv *priv;
1422 priv = GET_PRIV (list);
1424 g_hash_table_remove (priv->groups,
1425 dbus_g_proxy_get_path (DBUS_G_PROXY (channel)));
1429 tp_contact_list_group_members_added_cb (GossipTelepathyGroup *group,
1433 const gchar *message,
1434 EmpathyTpContactList *list)
1436 EmpathyTpContactListPriv *priv;
1437 GList *added_list, *l;
1438 const gchar *group_name;
1440 priv = GET_PRIV (list);
1442 group_name = gossip_telepathy_group_get_name (group);
1443 added_list = empathy_tp_contact_list_get_from_handles (list, members);
1445 for (l = added_list; l; l = l->next) {
1446 GossipContact *contact;
1447 GList *contact_groups;
1449 contact = GOSSIP_CONTACT (l->data);
1450 contact_groups = gossip_contact_get_groups (contact);
1452 /* FIXME: this leaks */
1453 if (!g_list_find_custom (contact_groups,
1455 (GCompareFunc) strcmp)) {
1456 gossip_debug (DEBUG_DOMAIN, "Contact %s added to group '%s'",
1457 gossip_contact_get_name (contact),
1459 contact_groups = g_list_append (contact_groups,
1460 g_strdup (group_name));
1461 tp_contact_list_block_contact (list, contact);
1462 gossip_contact_set_groups (contact, contact_groups);
1463 tp_contact_list_unblock_contact (list, contact);
1466 g_object_unref (contact);
1469 g_list_free (added_list);
1473 tp_contact_list_group_members_removed_cb (GossipTelepathyGroup *group,
1477 const gchar *message,
1478 EmpathyTpContactList *list)
1480 EmpathyTpContactListPriv *priv;
1481 GList *removed_list, *l;
1482 const gchar *group_name;
1484 priv = GET_PRIV (list);
1486 group_name = gossip_telepathy_group_get_name (group);
1487 removed_list = empathy_tp_contact_list_get_from_handles (list, members);
1489 for (l = removed_list; l; l = l->next) {
1490 GossipContact *contact;
1491 GList *contact_groups;
1494 /* FIXME: Does it leak ? */
1495 contact = GOSSIP_CONTACT (l->data);
1496 contact_groups = gossip_contact_get_groups (contact);
1497 contact_groups = g_list_copy (contact_groups);
1499 to_remove = g_list_find_custom (contact_groups,
1501 (GCompareFunc) strcmp);
1503 gossip_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1504 gossip_contact_get_handle (contact),
1506 contact_groups = g_list_remove_link (contact_groups,
1508 tp_contact_list_block_contact (list, contact);
1509 gossip_contact_set_groups (contact, contact_groups);
1510 tp_contact_list_unblock_contact (list, contact);
1513 g_list_free (contact_groups);
1515 g_object_unref (contact);
1518 g_list_free (removed_list);
1522 tp_contact_list_get_contacts_foreach (guint handle,
1523 GossipContact *contact,
1526 *contacts = g_list_append (*contacts, g_object_ref (contact));
1530 tp_contact_list_get_info (EmpathyTpContactList *list,
1533 EmpathyTpContactListPriv *priv;
1534 GError *error = NULL;
1536 priv = GET_PRIV (list);
1538 if (priv->presence_iface) {
1539 /* FIXME: We should use GetPresence instead */
1540 if (!tp_conn_iface_presence_request_presence (priv->presence_iface,
1542 gossip_debug (DEBUG_DOMAIN,
1543 "Could not request presences: %s",
1544 error ? error->message : "No error given");
1545 g_clear_error (&error);
1549 if (priv->aliasing_iface) {
1550 TpContactListAliasesRequestData *data;
1552 data = g_slice_new (TpContactListAliasesRequestData);
1554 data->handles = g_memdup (handles->data, handles->len * sizeof (guint));
1556 tp_conn_iface_aliasing_request_aliases_async (priv->aliasing_iface,
1558 (tp_conn_iface_aliasing_request_aliases_reply)
1559 tp_contact_list_request_aliases_cb,
1563 if (priv->avatars_iface) {
1566 for (i = 0; i < handles->len; i++) {
1569 handle = g_array_index (handles, gint, i);
1570 tp_contact_list_request_avatar (list, handle);
1576 tp_contact_list_request_avatar (EmpathyTpContactList *list,
1579 EmpathyTpContactListPriv *priv;
1581 priv = GET_PRIV (list);
1583 /* We queue avatar requests to not send too many dbus async
1584 * calls at once. If we don't we reach the dbus's limit of
1586 priv->avatar_requests_queue = g_list_append (priv->avatar_requests_queue,
1587 GUINT_TO_POINTER (handle));
1588 tp_contact_list_start_avatar_requests (list);
1592 tp_contact_list_start_avatar_requests (EmpathyTpContactList *list)
1594 EmpathyTpContactListPriv *priv;
1595 TpContactListAvatarRequestData *data;
1597 priv = GET_PRIV (list);
1599 while (n_avatar_requests < MAX_AVATAR_REQUESTS &&
1600 priv->avatar_requests_queue) {
1601 data = g_slice_new (TpContactListAvatarRequestData);
1603 data->handle = GPOINTER_TO_UINT (priv->avatar_requests_queue->data);
1605 n_avatar_requests++;
1606 priv->avatar_requests_queue = g_list_remove (priv->avatar_requests_queue,
1607 priv->avatar_requests_queue->data);
1609 tp_conn_iface_avatars_request_avatar_async (priv->avatars_iface,
1611 (tp_conn_iface_avatars_request_avatar_reply)
1612 tp_contact_list_request_avatar_cb,
1618 tp_contact_list_avatar_update_cb (DBusGProxy *proxy,
1621 EmpathyTpContactList *list)
1623 EmpathyTpContactListPriv *priv;
1625 priv = GET_PRIV (list);
1627 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1628 /* We don't know this contact, skip */
1632 gossip_debug (DEBUG_DOMAIN, "Changing avatar for %d to %s",
1635 tp_contact_list_request_avatar (list, handle);
1639 tp_contact_list_request_avatar_cb (DBusGProxy *proxy,
1640 GArray *avatar_data,
1643 TpContactListAvatarRequestData *data)
1645 GossipContact *contact;
1647 contact = empathy_tp_contact_list_get_from_handle (data->list, data->handle);
1650 gossip_debug (DEBUG_DOMAIN, "Error requesting avatar for %s: %s",
1651 gossip_contact_get_name (contact),
1652 error ? error->message : "No error given");
1654 GossipAvatar *avatar;
1656 avatar = gossip_avatar_new (avatar_data->data,
1659 tp_contact_list_block_contact (data->list, contact);
1660 gossip_contact_set_avatar (contact, avatar);
1661 tp_contact_list_unblock_contact (data->list, contact);
1662 gossip_avatar_unref (avatar);
1665 n_avatar_requests--;
1666 tp_contact_list_start_avatar_requests (data->list);
1668 g_object_unref (contact);
1669 g_slice_free (TpContactListAvatarRequestData, data);
1673 tp_contact_list_aliases_update_cb (DBusGProxy *proxy,
1674 GPtrArray *renamed_handlers,
1675 EmpathyTpContactList *list)
1677 EmpathyTpContactListPriv *priv;
1680 priv = GET_PRIV (list);
1682 for (i = 0; renamed_handlers->len > i; i++) {
1685 GValueArray *renamed_struct;
1686 GossipContact *contact;
1688 renamed_struct = g_ptr_array_index (renamed_handlers, i);
1689 handle = g_value_get_uint(g_value_array_get_nth (renamed_struct, 0));
1690 alias = g_value_get_string(g_value_array_get_nth (renamed_struct, 1));
1692 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1693 /* We don't know this contact, skip */
1697 if (G_STR_EMPTY (alias)) {
1701 contact = empathy_tp_contact_list_get_from_handle (list, handle);
1702 tp_contact_list_block_contact (list, contact);
1703 gossip_contact_set_name (contact, alias);
1704 tp_contact_list_unblock_contact (list, contact);
1705 g_object_unref (contact);
1707 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (update cb)",
1713 tp_contact_list_request_aliases_cb (DBusGProxy *proxy,
1714 gchar **contact_names,
1716 TpContactListAliasesRequestData *data)
1722 gossip_debug (DEBUG_DOMAIN, "Error requesting aliases: %s",
1726 for (name = contact_names; *name && !error; name++) {
1727 GossipContact *contact;
1729 contact = empathy_tp_contact_list_get_from_handle (data->list,
1731 tp_contact_list_block_contact (data->list, contact);
1732 gossip_contact_set_name (contact, *name);
1733 tp_contact_list_unblock_contact (data->list, contact);
1734 g_object_unref (contact);
1736 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (request cb)",
1737 data->handles[i], *name);
1742 g_free (data->handles);
1743 g_slice_free (TpContactListAliasesRequestData, data);
1747 tp_contact_list_presence_update_cb (DBusGProxy *proxy,
1748 GHashTable *handle_table,
1749 EmpathyTpContactList *list)
1751 g_hash_table_foreach (handle_table,
1752 (GHFunc) tp_contact_list_parse_presence_foreach,
1757 tp_contact_list_parse_presence_foreach (guint handle,
1758 GValueArray *presence_struct,
1759 EmpathyTpContactList *list)
1761 EmpathyTpContactListPriv *priv;
1762 GHashTable *presences_table;
1763 GossipContact *contact;
1764 GossipPresence *presence = NULL;
1766 priv = GET_PRIV (list);
1768 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1769 /* We don't know this contact, skip */
1773 contact = empathy_tp_contact_list_get_from_handle (list, handle);
1774 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
1776 g_hash_table_foreach (presences_table,
1777 (GHFunc) tp_contact_list_presences_table_foreach,
1780 gossip_debug (DEBUG_DOMAIN, "Presence changed for %s (%d) to %s (%d)",
1781 gossip_contact_get_name (contact),
1783 presence ? gossip_presence_get_status (presence) : "unset",
1784 presence ? gossip_presence_get_state (presence) : MC_PRESENCE_UNSET);
1786 tp_contact_list_block_contact (list, contact);
1787 gossip_contact_set_presence (contact, presence);
1788 tp_contact_list_unblock_contact (list, contact);
1790 g_object_unref (contact);
1794 tp_contact_list_presences_table_foreach (const gchar *state_str,
1795 GHashTable *presences_table,
1796 GossipPresence **presence)
1799 const GValue *message;
1801 state = gossip_presence_state_from_str (state_str);
1802 if ((state == MC_PRESENCE_UNSET) || (state == MC_PRESENCE_OFFLINE)) {
1807 g_object_unref (*presence);
1811 *presence = gossip_presence_new ();
1812 gossip_presence_set_state (*presence, state);
1814 message = g_hash_table_lookup (presences_table, "message");
1815 if (message != NULL) {
1816 gossip_presence_set_status (*presence,
1817 g_value_get_string (message));
1822 tp_contact_list_status_changed_cb (MissionControl *mc,
1823 TelepathyConnectionStatus status,
1824 McPresence presence,
1825 TelepathyConnectionStatusReason reason,
1826 const gchar *unique_name,
1827 EmpathyTpContactList *list)
1829 EmpathyTpContactListPriv *priv;
1832 priv = GET_PRIV (list);
1834 account = mc_account_lookup (unique_name);
1835 if (status != TP_CONN_STATUS_DISCONNECTED ||
1836 !gossip_account_equal (account, priv->account)) {
1837 g_object_unref (account);
1841 /* We are disconnected, do just like if the connection was destroyed */
1842 g_signal_handlers_disconnect_by_func (priv->tp_conn,
1843 tp_contact_list_destroy_cb,
1845 tp_contact_list_destroy_cb (DBUS_G_PROXY (priv->tp_conn), list);
1847 g_object_unref (account);