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-conn.h>
29 #include <libtelepathy/tp-chan.h>
30 #include <libtelepathy/tp-helpers.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-contact-list.h"
37 #include "gossip-debug.h"
38 #include "gossip-telepathy-group.h"
40 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
41 EMPATHY_TYPE_CONTACT_LIST, EmpathyContactListPriv))
43 #define DEBUG_DOMAIN "ContactList"
44 #define MAX_AVATAR_REQUESTS 10
46 struct _EmpathyContactListPriv {
49 GossipContact *own_contact;
51 GossipTelepathyGroup *known;
52 GossipTelepathyGroup *publish;
53 GossipTelepathyGroup *subscribe;
58 DBusGProxy *aliasing_iface;
59 DBusGProxy *avatars_iface;
60 DBusGProxy *presence_iface;
62 GList *avatar_requests_queue;
66 CONTACT_LIST_TYPE_KNOWN,
67 CONTACT_LIST_TYPE_PUBLISH,
68 CONTACT_LIST_TYPE_SUBSCRIBE,
69 CONTACT_LIST_TYPE_UNKNOWN,
70 CONTACT_LIST_TYPE_COUNT
79 EmpathyContactList *list;
81 } ContactListAvatarRequestData;
84 EmpathyContactList *list;
86 } ContactListAliasesRequestData;
88 static void empathy_contact_list_class_init (EmpathyContactListClass *klass);
89 static void empathy_contact_list_init (EmpathyContactList *list);
90 static void contact_list_finalize (GObject *object);
91 static void contact_list_finalize_proxies (EmpathyContactList *list);
92 static void contact_list_contact_removed_foreach (guint handle,
93 GossipContact *contact,
94 EmpathyContactList *list);
95 static void contact_list_destroy_cb (DBusGProxy *proxy,
96 EmpathyContactList *list);
97 static gboolean contact_list_find_foreach (guint handle,
98 GossipContact *contact,
100 static void contact_list_newchannel_cb (DBusGProxy *proxy,
101 const gchar *object_path,
102 const gchar *channel_type,
103 TelepathyHandleType handle_type,
104 guint channel_handle,
105 gboolean suppress_handle,
106 EmpathyContactList *list);
107 static ContactListType contact_list_get_type (EmpathyContactList *list,
109 static void contact_list_contact_added_cb (GossipTelepathyGroup *group,
113 const gchar *message,
114 EmpathyContactList *list);
115 static void contact_list_contact_removed_cb (GossipTelepathyGroup *group,
119 const gchar *message,
120 EmpathyContactList *list);
121 static void contact_list_local_pending_cb (GossipTelepathyGroup *group,
125 const gchar *message,
126 EmpathyContactList *list);
127 static void contact_list_groups_updated_cb (GossipContact *contact,
129 EmpathyContactList *list);
130 static void contact_list_subscription_updated_cb (GossipContact *contact,
132 EmpathyContactList *list);
133 static void contact_list_name_updated_cb (GossipContact *contact,
135 EmpathyContactList *list);
136 static void contact_list_update_groups_foreach (gchar *object_path,
137 GossipTelepathyGroup *group,
138 ContactListData *data);
139 static GossipTelepathyGroup * contact_list_get_group (EmpathyContactList *list,
141 static gboolean contact_list_find_group (gchar *key,
142 GossipTelepathyGroup *group,
144 static void contact_list_get_groups_foreach (gchar *key,
145 GossipTelepathyGroup *group,
147 static void contact_list_group_channel_closed_cb (TpChan *channel,
148 EmpathyContactList *list);
149 static void contact_list_group_members_added_cb (GossipTelepathyGroup *group,
153 const gchar *message,
154 EmpathyContactList *list);
155 static void contact_list_group_members_removed_cb (GossipTelepathyGroup *group,
159 const gchar *message,
160 EmpathyContactList *list);
161 static void contact_list_get_contacts_foreach (guint handle,
162 GossipContact *contact,
164 static void contact_list_get_info (EmpathyContactList *list,
166 static void contact_list_request_avatar (EmpathyContactList *list,
168 static void contact_list_start_avatar_requests (EmpathyContactList *list);
169 static void contact_list_avatar_update_cb (DBusGProxy *proxy,
172 EmpathyContactList *list);
173 static void contact_list_request_avatar_cb (DBusGProxy *proxy,
177 ContactListAvatarRequestData *data);
178 static void contact_list_aliases_update_cb (DBusGProxy *proxy,
180 EmpathyContactList *list);
181 static void contact_list_request_aliases_cb (DBusGProxy *proxy,
182 gchar **contact_names,
184 ContactListAliasesRequestData *data);
185 static void contact_list_presence_update_cb (DBusGProxy *proxy,
186 GHashTable *handle_table,
187 EmpathyContactList *list);
188 static void contact_list_parse_presence_foreach (guint handle,
189 GValueArray *presence_struct,
190 EmpathyContactList *list);
191 static void contact_list_presences_table_foreach (const gchar *state_str,
192 GHashTable *presences_table,
193 GossipPresence **presence);
202 static guint signals[LAST_SIGNAL];
203 static guint n_avatar_requests = 0;
205 G_DEFINE_TYPE (EmpathyContactList, empathy_contact_list, G_TYPE_OBJECT);
208 empathy_contact_list_class_init (EmpathyContactListClass *klass)
210 GObjectClass *object_class = G_OBJECT_CLASS (klass);
212 object_class->finalize = contact_list_finalize;
214 signals[CONTACT_ADDED] =
215 g_signal_new ("contact-added",
216 G_TYPE_FROM_CLASS (klass),
220 g_cclosure_marshal_VOID__OBJECT,
222 1, GOSSIP_TYPE_CONTACT);
224 signals[CONTACT_REMOVED] =
225 g_signal_new ("contact-removed",
226 G_TYPE_FROM_CLASS (klass),
230 g_cclosure_marshal_VOID__OBJECT,
232 1, GOSSIP_TYPE_CONTACT);
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 (EmpathyContactListPriv));
248 empathy_contact_list_init (EmpathyContactList *list)
250 EmpathyContactListPriv *priv;
252 priv = GET_PRIV (list);
254 priv->groups = g_hash_table_new_full (g_str_hash,
256 (GDestroyNotify) g_free,
257 (GDestroyNotify) g_object_unref);
258 priv->contacts = g_hash_table_new_full (g_direct_hash,
261 (GDestroyNotify) g_object_unref);
266 contact_list_finalize (GObject *object)
268 EmpathyContactListPriv *priv;
269 EmpathyContactList *list;
271 list = EMPATHY_CONTACT_LIST (object);
272 priv = GET_PRIV (list);
274 gossip_debug (DEBUG_DOMAIN, "finalize: %p", object);
277 contact_list_finalize_proxies (list);
278 g_object_unref (priv->tp_conn);
282 g_object_unref (priv->known);
285 if (priv->subscribe) {
286 g_object_unref (priv->subscribe);
290 g_object_unref (priv->publish);
293 g_object_unref (priv->account);
294 g_object_unref (priv->own_contact);
295 g_hash_table_destroy (priv->groups);
296 g_hash_table_destroy (priv->contacts);
298 G_OBJECT_CLASS (empathy_contact_list_parent_class)->finalize (object);
302 empathy_contact_list_new (McAccount *account)
304 EmpathyContactListPriv *priv;
305 EmpathyContactList *list;
309 GError *error = NULL;
311 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
313 mc = mission_control_new (tp_get_bus ());
315 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
316 /* The account is not connected, nothing to do. */
320 tp_conn = mission_control_get_connection (mc, account, NULL);
322 g_return_val_if_fail (tp_conn != NULL, NULL);
324 list = g_object_new (EMPATHY_TYPE_CONTACT_LIST, NULL);
325 priv = GET_PRIV (list);
327 priv->tp_conn = tp_conn;
328 priv->account = g_object_ref (account);
330 g_signal_connect (priv->tp_conn, "destroy",
331 G_CALLBACK (contact_list_destroy_cb),
334 priv->aliasing_iface = tp_conn_get_interface (priv->tp_conn,
335 TELEPATHY_CONN_IFACE_ALIASING_QUARK);
336 priv->avatars_iface = tp_conn_get_interface (priv->tp_conn,
337 TELEPATHY_CONN_IFACE_AVATARS_QUARK);
338 priv->presence_iface = tp_conn_get_interface (priv->tp_conn,
339 TELEPATHY_CONN_IFACE_PRESENCE_QUARK);
341 if (priv->aliasing_iface) {
342 dbus_g_proxy_connect_signal (priv->aliasing_iface,
344 G_CALLBACK (contact_list_aliases_update_cb),
348 if (priv->avatars_iface) {
349 dbus_g_proxy_connect_signal (priv->avatars_iface,
351 G_CALLBACK (contact_list_avatar_update_cb),
355 if (priv->presence_iface) {
356 dbus_g_proxy_connect_signal (priv->presence_iface,
358 G_CALLBACK (contact_list_presence_update_cb),
362 /* Get our own handle and contact */
363 if (!tp_conn_get_self_handle (DBUS_G_PROXY (priv->tp_conn),
365 gossip_debug (DEBUG_DOMAIN, "GetSelfHandle Error: %s",
366 error ? error->message : "No error given");
367 g_clear_error (&error);
369 priv->own_contact = empathy_contact_list_get_from_handle (list, handle);
376 empathy_contact_list_setup (EmpathyContactList *list)
378 EmpathyContactListPriv *priv;
380 GError *error = NULL;
383 g_return_if_fail (EMPATHY_IS_CONTACT_LIST (list));
385 priv = GET_PRIV (list);
387 gossip_debug (DEBUG_DOMAIN, "setup contact list: %p", list);
389 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
390 G_CALLBACK (contact_list_newchannel_cb),
393 /* Get existing channels */
394 if (!tp_conn_list_channels (DBUS_G_PROXY (priv->tp_conn),
397 gossip_debug (DEBUG_DOMAIN,
398 "Failed to get list of open channels: %s",
399 error ? error->message : "No error given");
400 g_clear_error (&error);
404 for (i = 0; channels->len > i; i++) {
405 GValueArray *chan_struct;
406 const gchar *object_path;
407 const gchar *chan_iface;
408 TelepathyHandleType handle_type;
411 chan_struct = g_ptr_array_index (channels, i);
412 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
413 chan_iface = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
414 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
415 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
417 contact_list_newchannel_cb (DBUS_G_PROXY (priv->tp_conn),
418 object_path, chan_iface,
422 g_value_array_free (chan_struct);
425 g_ptr_array_free (channels, TRUE);
429 empathy_contact_list_get_account (EmpathyContactList *list)
431 EmpathyContactListPriv *priv;
433 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
435 priv = GET_PRIV (list);
437 return priv->account;
441 empathy_contact_list_get_own (EmpathyContactList *list)
443 EmpathyContactListPriv *priv;
445 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
447 priv = GET_PRIV (list);
449 return priv->own_contact;
453 empathy_contact_list_find (EmpathyContactList *list,
456 EmpathyContactListPriv *priv;
457 GossipContact *contact;
459 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
461 priv = GET_PRIV (list);
463 contact = g_hash_table_find (priv->contacts,
464 (GHRFunc) contact_list_find_foreach,
471 empathy_contact_list_add (EmpathyContactList *list,
473 const gchar *message)
475 EmpathyContactListPriv *priv;
477 g_return_if_fail (EMPATHY_IS_CONTACT_LIST (list));
479 priv = GET_PRIV (list);
481 gossip_telepathy_group_add_member (priv->subscribe, handle, message);
485 empathy_contact_list_remove (EmpathyContactList *list,
488 EmpathyContactListPriv *priv;
490 g_return_if_fail (EMPATHY_IS_CONTACT_LIST (list));
492 priv = GET_PRIV (list);
494 gossip_telepathy_group_remove_member (priv->subscribe, handle, "");
495 gossip_telepathy_group_remove_member (priv->publish, handle, "");
496 gossip_telepathy_group_remove_member (priv->known, handle, "");
500 empathy_contact_list_get_from_id (EmpathyContactList *list,
503 EmpathyContactListPriv *priv;
504 GossipContact *contact;
505 const gchar *contact_ids[] = {id, NULL};
508 GError *error = NULL;
510 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
511 g_return_val_if_fail (id != NULL, NULL);
513 priv = GET_PRIV (list);
515 contact = empathy_contact_list_find (list, id);
520 /* The id is unknown, requests a new handle */
521 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
522 TP_HANDLE_TYPE_CONTACT,
525 gossip_debug (DEBUG_DOMAIN,
526 "RequestHandle for %s failed: %s", id,
527 error ? error->message : "No error given");
528 g_clear_error (&error);
532 handle = g_array_index(handles, guint, 0);
533 g_array_free (handles, TRUE);
535 return empathy_contact_list_get_from_handle (list, handle);
539 empathy_contact_list_get_from_handle (EmpathyContactList *list,
542 GossipContact *contact;
546 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
548 handles = g_array_new (FALSE, FALSE, sizeof (guint));
549 g_array_append_val (handles, handle);
551 contacts = empathy_contact_list_get_from_handles (list, handles);
552 g_array_free (handles, TRUE);
558 contact = contacts->data;
559 g_list_free (contacts);
565 empathy_contact_list_get_from_handles (EmpathyContactList *list,
568 EmpathyContactListPriv *priv;
569 gchar **handles_names;
572 GList *contacts = NULL;
574 GError *error = NULL;
576 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
577 g_return_val_if_fail (handles != NULL, NULL);
579 priv = GET_PRIV (list);
581 /* Search all handles we already have */
582 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
583 for (i = 0; i < handles->len; i++) {
584 GossipContact *contact;
587 handle = g_array_index (handles, guint, i);
588 contact = g_hash_table_lookup (priv->contacts,
589 GUINT_TO_POINTER (handle));
592 contacts = g_list_prepend (contacts,
593 g_object_ref (contact));
595 g_array_append_val (new_handles, handle);
599 if (new_handles->len == 0) {
603 /* Holds all handles we don't have yet.
604 * FIXME: We should release them at some point. */
605 if (!tp_conn_hold_handles (DBUS_G_PROXY (priv->tp_conn),
606 TP_HANDLE_TYPE_CONTACT,
607 new_handles, &error)) {
608 gossip_debug (DEBUG_DOMAIN,
609 "HoldHandles Error: %s",
610 error ? error->message : "No error given");
611 g_clear_error (&error);
612 g_array_free (new_handles, TRUE);
616 /* Get the IDs of all new handles */
617 if (!tp_conn_inspect_handles (DBUS_G_PROXY (priv->tp_conn),
618 TP_HANDLE_TYPE_CONTACT,
622 gossip_debug (DEBUG_DOMAIN,
623 "InspectHandle Error: %s",
624 error ? error->message : "No error given");
625 g_clear_error (&error);
626 g_array_free (new_handles, TRUE);
630 /* Create contact objects */
631 for (i = 0, id = handles_names; *id && i < new_handles->len; id++, i++) {
632 GossipContact *contact;
635 handle = g_array_index (new_handles, guint, i);
636 contact = g_object_new (GOSSIP_TYPE_CONTACT,
637 "account", priv->account,
642 g_signal_connect (contact, "notify::groups",
643 G_CALLBACK (contact_list_groups_updated_cb),
645 g_signal_connect (contact, "notify::subscription",
646 G_CALLBACK (contact_list_subscription_updated_cb),
648 g_signal_connect (contact, "notify::name",
649 G_CALLBACK (contact_list_name_updated_cb),
652 gossip_debug (DEBUG_DOMAIN, "new contact created: %s (%d)",
655 g_hash_table_insert (priv->contacts,
656 GUINT_TO_POINTER (handle),
659 contacts = g_list_prepend (contacts, g_object_ref (contact));
662 contact_list_get_info (list, new_handles);
664 g_array_free (new_handles, TRUE);
665 g_strfreev (handles_names);
671 empathy_contact_list_rename_group (EmpathyContactList *list,
672 const gchar *old_group,
673 const gchar *new_group)
675 EmpathyContactListPriv *priv;
676 GossipTelepathyGroup *group;
679 g_return_if_fail (EMPATHY_IS_CONTACT_LIST (list));
680 g_return_if_fail (old_group != NULL);
681 g_return_if_fail (new_group != NULL);
683 priv = GET_PRIV (list);
685 group = g_hash_table_find (priv->groups,
686 (GHRFunc) contact_list_find_group,
689 /* The group doesn't exists on this account */
693 gossip_debug (DEBUG_DOMAIN, "rename group %s to %s", group, new_group);
695 /* Remove all members from the old group */
696 members = gossip_telepathy_group_get_members (group);
697 gossip_telepathy_group_remove_members (group, members, "");
698 contact_list_group_members_removed_cb (group, members,
700 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
702 g_hash_table_remove (priv->groups,
703 gossip_telepathy_group_get_object_path (group));
705 /* Add all members to the new group */
706 group = contact_list_get_group (list, new_group);
708 gossip_telepathy_group_add_members (group, members, "");
713 empathy_contact_list_get_groups (EmpathyContactList *list)
715 EmpathyContactListPriv *priv;
716 GList *groups = NULL;
718 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
720 priv = GET_PRIV (list);
722 g_hash_table_foreach (priv->groups,
723 (GHFunc) contact_list_get_groups_foreach,
726 groups = g_list_sort (groups, (GCompareFunc) strcmp);
732 empathy_contact_list_get_contacts (EmpathyContactList *list)
734 EmpathyContactListPriv *priv;
735 GList *contacts = NULL;
737 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
739 priv = GET_PRIV (list);
741 /* FIXME: we should only return contacts that are in the contact list */
742 g_hash_table_foreach (priv->contacts,
743 (GHFunc) contact_list_get_contacts_foreach,
750 contact_list_finalize_proxies (EmpathyContactList *list)
752 EmpathyContactListPriv *priv;
754 priv = GET_PRIV (list);
756 g_signal_handlers_disconnect_by_func (priv->tp_conn,
757 contact_list_destroy_cb,
759 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
760 G_CALLBACK (contact_list_newchannel_cb),
763 if (priv->aliasing_iface) {
764 dbus_g_proxy_disconnect_signal (priv->aliasing_iface,
766 G_CALLBACK (contact_list_aliases_update_cb),
770 if (priv->avatars_iface) {
771 dbus_g_proxy_disconnect_signal (priv->avatars_iface,
773 G_CALLBACK (contact_list_avatar_update_cb),
777 if (priv->presence_iface) {
778 dbus_g_proxy_disconnect_signal (priv->presence_iface,
780 G_CALLBACK (contact_list_presence_update_cb),
786 contact_list_destroy_cb (DBusGProxy *proxy,
787 EmpathyContactList *list)
789 EmpathyContactListPriv *priv;
791 priv = GET_PRIV (list);
793 gossip_debug (DEBUG_DOMAIN, "Connection destroyed. "
794 "Account disconnected or CM crashed.");
796 /* DBus proxies should NOT be used anymore */
797 g_object_unref (priv->tp_conn);
798 priv->tp_conn = NULL;
799 priv->aliasing_iface = NULL;
800 priv->avatars_iface = NULL;
801 priv->presence_iface = NULL;
803 /* Remove all contacts */
804 g_hash_table_foreach (priv->contacts,
805 (GHFunc) contact_list_contact_removed_foreach,
807 g_hash_table_remove_all (priv->contacts);
809 /* Tell the world to not use us anymore */
810 g_signal_emit (list, signals[DESTROY], 0);
814 contact_list_contact_removed_foreach (guint handle,
815 GossipContact *contact,
816 EmpathyContactList *list)
818 g_signal_handlers_disconnect_by_func (contact,
819 contact_list_groups_updated_cb,
821 g_signal_handlers_disconnect_by_func (contact,
822 contact_list_subscription_updated_cb,
824 g_signal_handlers_disconnect_by_func (contact,
825 contact_list_name_updated_cb,
828 g_signal_emit (list, signals[CONTACT_REMOVED], 0, contact);
832 contact_list_block_contact (EmpathyContactList *list,
833 GossipContact *contact)
835 g_signal_handlers_block_by_func (contact,
836 contact_list_groups_updated_cb,
838 g_signal_handlers_block_by_func (contact,
839 contact_list_subscription_updated_cb,
841 g_signal_handlers_block_by_func (contact,
842 contact_list_name_updated_cb,
847 contact_list_unblock_contact (EmpathyContactList *list,
848 GossipContact *contact)
850 g_signal_handlers_unblock_by_func (contact,
851 contact_list_groups_updated_cb,
853 g_signal_handlers_unblock_by_func (contact,
854 contact_list_subscription_updated_cb,
856 g_signal_handlers_unblock_by_func (contact,
857 contact_list_name_updated_cb,
862 contact_list_find_foreach (guint handle,
863 GossipContact *contact,
866 if (strcmp (gossip_contact_get_id (contact), id) == 0) {
874 contact_list_newchannel_cb (DBusGProxy *proxy,
875 const gchar *object_path,
876 const gchar *channel_type,
877 TelepathyHandleType handle_type,
878 guint channel_handle,
879 gboolean suppress_handle,
880 EmpathyContactList *list)
882 EmpathyContactListPriv *priv;
883 GossipTelepathyGroup *group;
885 const gchar *bus_name;
888 priv = GET_PRIV (list);
890 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) != 0 ||
895 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn));
896 new_chan = tp_chan_new (tp_get_bus (),
899 channel_type, handle_type, channel_handle);
901 if (handle_type == TP_HANDLE_TYPE_LIST) {
902 ContactListType list_type;
904 list_type = contact_list_get_type (list, new_chan);
905 if (list_type == CONTACT_LIST_TYPE_UNKNOWN) {
906 gossip_debug (DEBUG_DOMAIN, "Unknown contact list channel");
907 g_object_unref (new_chan);
911 gossip_debug (DEBUG_DOMAIN, "New contact list channel of type: %d",
914 group = gossip_telepathy_group_new (new_chan, priv->tp_conn);
917 case CONTACT_LIST_TYPE_KNOWN:
919 g_object_unref (priv->known);
923 case CONTACT_LIST_TYPE_PUBLISH:
925 g_object_unref (priv->publish);
927 priv->publish = group;
929 case CONTACT_LIST_TYPE_SUBSCRIBE:
930 if (priv->subscribe) {
931 g_object_unref (priv->subscribe);
933 priv->subscribe = group;
936 g_assert_not_reached ();
939 /* Connect and setup the new contact-list group */
940 if (list_type == CONTACT_LIST_TYPE_KNOWN ||
941 list_type == CONTACT_LIST_TYPE_SUBSCRIBE) {
942 g_signal_connect (group, "members-added",
943 G_CALLBACK (contact_list_contact_added_cb),
945 g_signal_connect (group, "members-removed",
946 G_CALLBACK (contact_list_contact_removed_cb),
949 members = gossip_telepathy_group_get_members (group);
950 contact_list_contact_added_cb (group, members, 0,
951 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
953 g_array_free (members, TRUE);
955 if (list_type == CONTACT_LIST_TYPE_PUBLISH) {
960 g_signal_connect (group, "local-pending",
961 G_CALLBACK (contact_list_local_pending_cb),
964 info = gossip_telepathy_group_get_local_pending_members_with_info (group);
967 /* This happens with butterfly because
968 * GetLocalPendingMembersWithInfo is not
970 g_object_unref (new_chan);
974 pending = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
975 for (i = 0; info->len > i; i++) {
976 GValueArray *pending_struct;
980 const gchar *message;
982 pending_struct = g_ptr_array_index (info, i);
983 member = g_value_get_uint (g_value_array_get_nth (pending_struct, 0));
984 invitor = g_value_get_uint (g_value_array_get_nth (pending_struct, 1));
985 reason = g_value_get_uint (g_value_array_get_nth (pending_struct, 2));
986 message = g_value_get_string (g_value_array_get_nth (pending_struct, 3));
988 g_array_insert_val (pending, 0, member);
990 contact_list_local_pending_cb (group, pending,
995 g_value_array_free (pending_struct);
998 g_ptr_array_free (info, TRUE);
999 g_array_free (pending, TRUE);
1002 else if (handle_type == TP_HANDLE_TYPE_GROUP) {
1003 const gchar *object_path;
1005 object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (new_chan));
1006 if (g_hash_table_lookup (priv->groups, object_path)) {
1007 g_object_unref (new_chan);
1011 group = gossip_telepathy_group_new (new_chan, priv->tp_conn);
1013 gossip_debug (DEBUG_DOMAIN, "New server-side group channel: %s",
1014 gossip_telepathy_group_get_name (group));
1016 dbus_g_proxy_connect_signal (DBUS_G_PROXY (new_chan), "Closed",
1018 (contact_list_group_channel_closed_cb),
1021 g_hash_table_insert (priv->groups, g_strdup (object_path), group);
1022 g_signal_connect (group, "members-added",
1023 G_CALLBACK (contact_list_group_members_added_cb),
1025 g_signal_connect (group, "members-removed",
1026 G_CALLBACK (contact_list_group_members_removed_cb),
1029 members = gossip_telepathy_group_get_members (group);
1030 contact_list_group_members_added_cb (group, members, 0,
1031 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1033 g_array_free (members, TRUE);
1036 g_object_unref (new_chan);
1039 static ContactListType
1040 contact_list_get_type (EmpathyContactList *list,
1043 EmpathyContactListPriv *priv;
1045 gchar **handle_name;
1046 ContactListType list_type;
1047 GError *error = NULL;
1049 priv = GET_PRIV (list);
1051 handles = g_array_new (FALSE, FALSE, sizeof (guint));
1052 g_array_append_val (handles, list_chan->handle);
1054 if (!tp_conn_inspect_handles (DBUS_G_PROXY (priv->tp_conn),
1055 TP_HANDLE_TYPE_LIST,
1059 gossip_debug (DEBUG_DOMAIN,
1060 "InspectHandle Error: %s",
1061 error ? error->message : "No error given");
1062 g_clear_error (&error);
1063 g_array_free (handles, TRUE);
1064 return CONTACT_LIST_TYPE_UNKNOWN;
1067 if (strcmp (*handle_name, "subscribe") == 0) {
1068 list_type = CONTACT_LIST_TYPE_SUBSCRIBE;
1069 } else if (strcmp (*handle_name, "publish") == 0) {
1070 list_type = CONTACT_LIST_TYPE_PUBLISH;
1071 } else if (strcmp (*handle_name, "known") == 0) {
1072 list_type = CONTACT_LIST_TYPE_KNOWN;
1074 list_type = CONTACT_LIST_TYPE_UNKNOWN;
1077 g_strfreev (handle_name);
1078 g_array_free (handles, TRUE);
1084 contact_list_contact_added_cb (GossipTelepathyGroup *group,
1088 const gchar *message,
1089 EmpathyContactList *list)
1091 EmpathyContactListPriv *priv;
1092 GList *added_list, *l;
1094 priv = GET_PRIV (list);
1096 added_list = empathy_contact_list_get_from_handles (list, handles);
1098 for (l = added_list; l; l = l->next) {
1099 GossipContact *contact;
1101 contact = GOSSIP_CONTACT (l->data);
1102 contact_list_block_contact (list, contact);
1103 gossip_contact_set_subscription (contact, GOSSIP_SUBSCRIPTION_BOTH);
1104 contact_list_unblock_contact (list, contact);
1106 g_signal_emit (list, signals[CONTACT_ADDED], 0, contact);
1108 g_object_unref (contact);
1111 g_list_free (added_list);
1115 contact_list_contact_removed_cb (GossipTelepathyGroup *group,
1119 const gchar *message,
1120 EmpathyContactList *list)
1122 EmpathyContactListPriv *priv;
1123 GList *removed_list, *l;
1125 priv = GET_PRIV (list);
1127 removed_list = empathy_contact_list_get_from_handles (list, handles);
1129 for (l = removed_list; l; l = l->next) {
1130 GossipContact *contact;
1133 contact = GOSSIP_CONTACT (l->data);
1135 handle = gossip_contact_get_handle (contact);
1136 g_hash_table_remove (priv->contacts, GUINT_TO_POINTER (handle));
1138 g_signal_emit (list, signals[CONTACT_REMOVED], 0, contact);
1140 g_object_unref (contact);
1143 g_list_free (removed_list);
1147 contact_list_local_pending_cb (GossipTelepathyGroup *group,
1151 const gchar *message,
1152 EmpathyContactList *list)
1154 EmpathyContactListPriv *priv;
1155 GList *pending_list, *l;
1157 priv = GET_PRIV (list);
1159 pending_list = empathy_contact_list_get_from_handles (list, handles);
1161 for (l = pending_list; l; l = l->next) {
1162 GossipContact *contact;
1164 contact = GOSSIP_CONTACT (l->data);
1166 /* FIXME: Is that the correct way ? */
1167 contact_list_block_contact (list, contact);
1168 gossip_contact_set_subscription (contact, GOSSIP_SUBSCRIPTION_FROM);
1169 contact_list_unblock_contact (list, contact);
1170 g_signal_emit (list, signals[CONTACT_ADDED], 0, contact);
1172 g_object_unref (contact);
1175 g_list_free (pending_list);
1179 contact_list_groups_updated_cb (GossipContact *contact,
1181 EmpathyContactList *list)
1183 EmpathyContactListPriv *priv;
1184 ContactListData data;
1187 priv = GET_PRIV (list);
1189 /* Make sure all groups are created */
1190 groups = gossip_contact_get_groups (contact);
1191 for (l = groups; l; l = l->next) {
1192 contact_list_get_group (list, l->data);
1195 data.handle = gossip_contact_get_handle (contact);
1196 data.new_groups = groups;
1198 g_hash_table_foreach (priv->groups,
1199 (GHFunc) contact_list_update_groups_foreach,
1204 contact_list_subscription_updated_cb (GossipContact *contact,
1206 EmpathyContactList *list)
1208 EmpathyContactListPriv *priv;
1209 GossipSubscription subscription;
1212 priv = GET_PRIV (list);
1214 subscription = gossip_contact_get_subscription (contact);
1215 handle = gossip_contact_get_handle (contact);
1217 /* FIXME: what to do here, I'm a bit lost... */
1219 gossip_telepathy_group_add_member (priv->publish, handle, "");
1221 gossip_telepathy_group_remove_member (priv->publish, handle, "");
1226 contact_list_name_updated_cb (GossipContact *contact,
1228 EmpathyContactList *list)
1230 EmpathyContactListPriv *priv;
1231 GHashTable *new_alias;
1232 const gchar *new_name;
1234 GError *error = NULL;
1236 priv = GET_PRIV (list);
1238 handle = gossip_contact_get_handle (contact);
1239 new_name = gossip_contact_get_name (contact);
1241 gossip_debug (DEBUG_DOMAIN, "renaming handle %d to %s",
1244 new_alias = g_hash_table_new_full (g_direct_hash,
1249 g_hash_table_insert (new_alias,
1250 GUINT_TO_POINTER (handle),
1251 g_strdup (new_name));
1253 if (!tp_conn_iface_aliasing_set_aliases (priv->aliasing_iface,
1256 gossip_debug (DEBUG_DOMAIN,
1257 "Couldn't rename contact: %s",
1258 error ? error->message : "No error given");
1259 g_clear_error (&error);
1262 g_hash_table_destroy (new_alias);
1266 contact_list_update_groups_foreach (gchar *object_path,
1267 GossipTelepathyGroup *group,
1268 ContactListData *data)
1271 gboolean found = FALSE;
1272 const gchar *group_name;
1275 is_member = gossip_telepathy_group_is_member (group, data->handle);
1276 group_name = gossip_telepathy_group_get_name (group);
1278 for (l = data->new_groups; l; l = l->next) {
1279 if (strcmp (group_name, l->data) == 0) {
1285 if (is_member && !found) {
1286 /* We are no longer member of this group */
1287 gossip_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1288 data->handle, group_name);
1289 gossip_telepathy_group_remove_member (group, data->handle, "");
1292 if (!is_member && found) {
1293 /* We are now member of this group */
1294 gossip_debug (DEBUG_DOMAIN, "Contact %d added to group '%s'",
1295 data->handle, group_name);
1296 gossip_telepathy_group_add_member (group, data->handle, "");
1300 static GossipTelepathyGroup *
1301 contact_list_get_group (EmpathyContactList *list,
1304 EmpathyContactListPriv *priv;
1305 GossipTelepathyGroup *group;
1306 TpChan *group_channel;
1309 char *group_object_path;
1310 const char *names[2] = {name, NULL};
1311 GError *error = NULL;
1313 priv = GET_PRIV (list);
1315 group = g_hash_table_find (priv->groups,
1316 (GHRFunc) contact_list_find_group,
1322 gossip_debug (DEBUG_DOMAIN, "creating new group: %s", name);
1324 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
1325 TP_HANDLE_TYPE_GROUP,
1329 gossip_debug (DEBUG_DOMAIN,
1330 "Couldn't request the creation of a new handle for group: %s",
1331 error ? error->message : "No error given");
1332 g_clear_error (&error);
1335 group_handle = g_array_index (handles, guint, 0);
1336 g_array_free (handles, TRUE);
1338 if (!tp_conn_request_channel (DBUS_G_PROXY (priv->tp_conn),
1339 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1340 TP_HANDLE_TYPE_GROUP,
1345 gossip_debug (DEBUG_DOMAIN,
1346 "Couldn't request the creation of a new group channel: %s",
1347 error ? error->message : "No error given");
1348 g_clear_error (&error);
1352 group_channel = tp_chan_new (tp_get_bus (),
1353 dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn)),
1355 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1356 TP_HANDLE_TYPE_GROUP,
1359 dbus_g_proxy_connect_signal (DBUS_G_PROXY (group_channel),
1362 (contact_list_group_channel_closed_cb),
1366 group = gossip_telepathy_group_new (group_channel, priv->tp_conn);
1367 g_hash_table_insert (priv->groups, group_object_path, group);
1368 g_signal_connect (group, "members-added",
1369 G_CALLBACK (contact_list_group_members_added_cb),
1371 g_signal_connect (group, "members-removed",
1372 G_CALLBACK (contact_list_group_members_removed_cb),
1379 contact_list_find_group (gchar *key,
1380 GossipTelepathyGroup *group,
1383 if (strcmp (group_name, gossip_telepathy_group_get_name (group)) == 0) {
1391 contact_list_get_groups_foreach (gchar *key,
1392 GossipTelepathyGroup *group,
1397 name = gossip_telepathy_group_get_name (group);
1398 *groups = g_list_append (*groups, g_strdup (name));
1402 contact_list_group_channel_closed_cb (TpChan *channel,
1403 EmpathyContactList *list)
1405 EmpathyContactListPriv *priv;
1407 priv = GET_PRIV (list);
1409 g_hash_table_remove (priv->groups,
1410 dbus_g_proxy_get_path (DBUS_G_PROXY (channel)));
1414 contact_list_group_members_added_cb (GossipTelepathyGroup *group,
1418 const gchar *message,
1419 EmpathyContactList *list)
1421 EmpathyContactListPriv *priv;
1422 GList *added_list, *l;
1423 const gchar *group_name;
1425 priv = GET_PRIV (list);
1427 group_name = gossip_telepathy_group_get_name (group);
1428 added_list = empathy_contact_list_get_from_handles (list, members);
1430 for (l = added_list; l; l = l->next) {
1431 GossipContact *contact;
1432 GList *contact_groups;
1434 contact = GOSSIP_CONTACT (l->data);
1435 contact_groups = gossip_contact_get_groups (contact);
1437 if (!g_list_find_custom (contact_groups,
1439 (GCompareFunc) strcmp)) {
1440 gossip_debug (DEBUG_DOMAIN, "Contact %s added to group '%s'",
1441 gossip_contact_get_name (contact),
1443 contact_groups = g_list_append (contact_groups,
1444 g_strdup (group_name));
1445 contact_list_block_contact (list, contact);
1446 gossip_contact_set_groups (contact, contact_groups);
1447 contact_list_unblock_contact (list, contact);
1450 g_object_unref (contact);
1453 g_list_free (added_list);
1457 contact_list_group_members_removed_cb (GossipTelepathyGroup *group,
1461 const gchar *message,
1462 EmpathyContactList *list)
1464 EmpathyContactListPriv *priv;
1465 GList *removed_list, *l;
1466 const gchar *group_name;
1468 priv = GET_PRIV (list);
1470 group_name = gossip_telepathy_group_get_name (group);
1471 removed_list = empathy_contact_list_get_from_handles (list, members);
1473 for (l = removed_list; l; l = l->next) {
1474 GossipContact *contact;
1475 GList *contact_groups;
1478 /* FIXME: Does it leak ? */
1479 contact = GOSSIP_CONTACT (l->data);
1480 contact_groups = gossip_contact_get_groups (contact);
1481 contact_groups = g_list_copy (contact_groups);
1483 to_remove = g_list_find_custom (contact_groups,
1485 (GCompareFunc) strcmp);
1487 gossip_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1488 gossip_contact_get_handle (contact),
1490 contact_groups = g_list_remove_link (contact_groups,
1492 contact_list_block_contact (list, contact);
1493 gossip_contact_set_groups (contact, contact_groups);
1494 contact_list_unblock_contact (list, contact);
1497 g_list_free (contact_groups);
1499 g_object_unref (contact);
1502 g_list_free (removed_list);
1506 contact_list_get_contacts_foreach (guint handle,
1507 GossipContact *contact,
1510 *contacts = g_list_append (*contacts, g_object_ref (contact));
1514 contact_list_get_info (EmpathyContactList *list,
1517 EmpathyContactListPriv *priv;
1518 GError *error = NULL;
1520 priv = GET_PRIV (list);
1522 if (priv->presence_iface) {
1523 /* FIXME: We should use GetPresence instead */
1524 if (!tp_conn_iface_presence_request_presence (priv->presence_iface,
1526 gossip_debug (DEBUG_DOMAIN,
1527 "Could not request presences: %s",
1528 error ? error->message : "No error given");
1529 g_clear_error (&error);
1533 if (priv->aliasing_iface) {
1534 ContactListAliasesRequestData *data;
1536 data = g_slice_new (ContactListAliasesRequestData);
1538 data->handles = g_memdup (handles->data, handles->len * sizeof (guint));
1540 tp_conn_iface_aliasing_request_aliases_async (priv->aliasing_iface,
1542 (tp_conn_iface_aliasing_request_aliases_reply)
1543 contact_list_request_aliases_cb,
1547 if (priv->avatars_iface) {
1550 for (i = 0; i < handles->len; i++) {
1553 handle = g_array_index (handles, gint, i);
1554 contact_list_request_avatar (list, handle);
1560 contact_list_request_avatar (EmpathyContactList *list,
1563 EmpathyContactListPriv *priv;
1565 priv = GET_PRIV (list);
1567 /* We queue avatar requests to not send too many dbus async
1568 * calls at once. If we don't we reach the dbus's limit of
1570 priv->avatar_requests_queue = g_list_append (priv->avatar_requests_queue,
1571 GUINT_TO_POINTER (handle));
1572 contact_list_start_avatar_requests (list);
1576 contact_list_start_avatar_requests (EmpathyContactList *list)
1578 EmpathyContactListPriv *priv;
1579 ContactListAvatarRequestData *data;
1581 priv = GET_PRIV (list);
1583 while (n_avatar_requests < MAX_AVATAR_REQUESTS &&
1584 priv->avatar_requests_queue) {
1585 data = g_slice_new (ContactListAvatarRequestData);
1587 data->handle = GPOINTER_TO_UINT (priv->avatar_requests_queue->data);
1589 n_avatar_requests++;
1590 priv->avatar_requests_queue = g_list_remove (priv->avatar_requests_queue,
1591 priv->avatar_requests_queue->data);
1593 tp_conn_iface_avatars_request_avatar_async (priv->avatars_iface,
1595 (tp_conn_iface_avatars_request_avatar_reply)
1596 contact_list_request_avatar_cb,
1602 contact_list_avatar_update_cb (DBusGProxy *proxy,
1605 EmpathyContactList *list)
1607 gossip_debug (DEBUG_DOMAIN, "Changing avatar for %d to %s",
1610 contact_list_request_avatar (list, handle);
1614 contact_list_request_avatar_cb (DBusGProxy *proxy,
1615 GArray *avatar_data,
1618 ContactListAvatarRequestData *data)
1620 GossipContact *contact;
1622 contact = empathy_contact_list_get_from_handle (data->list, data->handle);
1625 gossip_debug (DEBUG_DOMAIN, "Error requesting avatar for %s: %s",
1626 gossip_contact_get_name (contact),
1627 error ? error->message : "No error given");
1629 GossipAvatar *avatar;
1631 avatar = gossip_avatar_new (avatar_data->data,
1634 contact_list_block_contact (data->list, contact);
1635 gossip_contact_set_avatar (contact, avatar);
1636 contact_list_unblock_contact (data->list, contact);
1637 gossip_avatar_unref (avatar);
1640 n_avatar_requests--;
1641 contact_list_start_avatar_requests (data->list);
1643 g_slice_free (ContactListAvatarRequestData, data);
1647 contact_list_aliases_update_cb (DBusGProxy *proxy,
1648 GPtrArray *renamed_handlers,
1649 EmpathyContactList *list)
1653 for (i = 0; renamed_handlers->len > i; i++) {
1656 GValueArray *renamed_struct;
1657 GossipContact *contact;
1659 renamed_struct = g_ptr_array_index (renamed_handlers, i);
1660 handle = g_value_get_uint(g_value_array_get_nth (renamed_struct, 0));
1661 alias = g_value_get_string(g_value_array_get_nth (renamed_struct, 1));
1663 if (alias && *alias == '\0') {
1667 contact = empathy_contact_list_get_from_handle (list, handle);
1668 contact_list_block_contact (list, contact);
1669 gossip_contact_set_name (contact, alias);
1670 contact_list_unblock_contact (list, contact);
1672 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (update cb)",
1678 contact_list_request_aliases_cb (DBusGProxy *proxy,
1679 gchar **contact_names,
1681 ContactListAliasesRequestData *data)
1686 for (name = contact_names; *name && !error; name++) {
1687 GossipContact *contact;
1689 contact = empathy_contact_list_get_from_handle (data->list,
1691 contact_list_block_contact (data->list, contact);
1692 gossip_contact_set_name (contact, *name);
1693 contact_list_unblock_contact (data->list, contact);
1695 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (request cb)",
1696 data->handles[i], *name);
1701 g_free (data->handles);
1702 g_slice_free (ContactListAliasesRequestData, data);
1706 contact_list_presence_update_cb (DBusGProxy *proxy,
1707 GHashTable *handle_table,
1708 EmpathyContactList *list)
1710 g_hash_table_foreach (handle_table,
1711 (GHFunc) contact_list_parse_presence_foreach,
1716 contact_list_parse_presence_foreach (guint handle,
1717 GValueArray *presence_struct,
1718 EmpathyContactList *list)
1720 GHashTable *presences_table;
1721 GossipContact *contact;
1722 GossipPresence *presence = NULL;
1724 contact = empathy_contact_list_get_from_handle (list, handle);
1725 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
1727 g_hash_table_foreach (presences_table,
1728 (GHFunc) contact_list_presences_table_foreach,
1731 gossip_debug (DEBUG_DOMAIN, "Presence changed for %s (%d) to %s (%d)",
1732 gossip_contact_get_name (contact),
1734 presence ? gossip_presence_get_status (presence) : "unset",
1735 presence ? gossip_presence_get_state (presence) : MC_PRESENCE_UNSET);
1737 contact_list_block_contact (list, contact);
1738 gossip_contact_set_presence (contact, presence);
1739 contact_list_unblock_contact (list, contact);
1743 contact_list_presences_table_foreach (const gchar *state_str,
1744 GHashTable *presences_table,
1745 GossipPresence **presence)
1748 const GValue *message;
1750 state = gossip_presence_state_from_str (state_str);
1751 if ((state == MC_PRESENCE_UNSET) || (state == MC_PRESENCE_OFFLINE)) {
1756 g_object_unref (*presence);
1760 *presence = gossip_presence_new ();
1761 gossip_presence_set_state (*presence, state);
1763 message = g_hash_table_lookup (presences_table, "message");
1764 if (message != NULL) {
1765 gossip_presence_set_status (*presence,
1766 g_value_get_string (message));