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 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
388 G_CALLBACK (contact_list_newchannel_cb),
391 /* Get existing channels */
392 if (!tp_conn_list_channels (DBUS_G_PROXY (priv->tp_conn),
395 gossip_debug (DEBUG_DOMAIN,
396 "Failed to get list of open channels: %s",
397 error ? error->message : "No error given");
398 g_clear_error (&error);
402 for (i = 0; channels->len > i; i++) {
403 GValueArray *chan_struct;
404 const gchar *object_path;
405 const gchar *chan_iface;
406 TelepathyHandleType handle_type;
409 chan_struct = g_ptr_array_index (channels, i);
410 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
411 chan_iface = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
412 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
413 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
415 contact_list_newchannel_cb (DBUS_G_PROXY (priv->tp_conn),
416 object_path, chan_iface,
420 g_value_array_free (chan_struct);
423 g_ptr_array_free (channels, TRUE);
427 empathy_contact_list_get_account (EmpathyContactList *list)
429 EmpathyContactListPriv *priv;
431 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
433 priv = GET_PRIV (list);
435 return priv->account;
439 empathy_contact_list_get_own (EmpathyContactList *list)
441 EmpathyContactListPriv *priv;
443 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
445 priv = GET_PRIV (list);
447 return priv->own_contact;
451 empathy_contact_list_find (EmpathyContactList *list,
454 EmpathyContactListPriv *priv;
455 GossipContact *contact;
457 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
459 priv = GET_PRIV (list);
461 contact = g_hash_table_find (priv->contacts,
462 (GHRFunc) contact_list_find_foreach,
469 empathy_contact_list_add (EmpathyContactList *list,
471 const gchar *message)
473 EmpathyContactListPriv *priv;
475 g_return_if_fail (EMPATHY_IS_CONTACT_LIST (list));
477 priv = GET_PRIV (list);
479 gossip_telepathy_group_add_member (priv->subscribe, handle, message);
483 empathy_contact_list_remove (EmpathyContactList *list,
486 EmpathyContactListPriv *priv;
488 g_return_if_fail (EMPATHY_IS_CONTACT_LIST (list));
490 priv = GET_PRIV (list);
492 gossip_telepathy_group_remove_member (priv->subscribe, handle, "");
493 gossip_telepathy_group_remove_member (priv->publish, handle, "");
494 gossip_telepathy_group_remove_member (priv->known, handle, "");
498 empathy_contact_list_get_from_id (EmpathyContactList *list,
501 EmpathyContactListPriv *priv;
502 GossipContact *contact;
503 const gchar *contact_ids[] = {id, NULL};
506 GError *error = NULL;
508 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
509 g_return_val_if_fail (id != NULL, NULL);
511 priv = GET_PRIV (list);
513 contact = empathy_contact_list_find (list, id);
518 /* The id is unknown, requests a new handle */
519 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
520 TP_HANDLE_TYPE_CONTACT,
523 gossip_debug (DEBUG_DOMAIN,
524 "RequestHandle for %s failed: %s", id,
525 error ? error->message : "No error given");
526 g_clear_error (&error);
530 handle = g_array_index(handles, guint, 0);
531 g_array_free (handles, TRUE);
533 return empathy_contact_list_get_from_handle (list, handle);
537 empathy_contact_list_get_from_handle (EmpathyContactList *list,
540 GossipContact *contact;
544 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
546 handles = g_array_new (FALSE, FALSE, sizeof (guint));
547 g_array_append_val (handles, handle);
549 contacts = empathy_contact_list_get_from_handles (list, handles);
550 g_array_free (handles, TRUE);
556 contact = contacts->data;
557 g_list_free (contacts);
563 empathy_contact_list_get_from_handles (EmpathyContactList *list,
566 EmpathyContactListPriv *priv;
567 gchar **handles_names;
570 GList *contacts = NULL;
572 GError *error = NULL;
574 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
575 g_return_val_if_fail (handles != NULL, NULL);
577 priv = GET_PRIV (list);
579 /* Search all handles we already have */
580 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
581 for (i = 0; i < handles->len; i++) {
582 GossipContact *contact;
585 handle = g_array_index (handles, guint, i);
586 contact = g_hash_table_lookup (priv->contacts,
587 GUINT_TO_POINTER (handle));
590 contacts = g_list_prepend (contacts,
591 g_object_ref (contact));
593 g_array_append_val (new_handles, handle);
597 if (new_handles->len == 0) {
601 /* Holds all handles we don't have yet.
602 * FIXME: We should release them at some point. */
603 if (!tp_conn_hold_handles (DBUS_G_PROXY (priv->tp_conn),
604 TP_HANDLE_TYPE_CONTACT,
605 new_handles, &error)) {
606 gossip_debug (DEBUG_DOMAIN,
607 "HoldHandles Error: %s",
608 error ? error->message : "No error given");
609 g_clear_error (&error);
610 g_array_free (new_handles, TRUE);
614 /* Get the IDs of all new handles */
615 if (!tp_conn_inspect_handles (DBUS_G_PROXY (priv->tp_conn),
616 TP_HANDLE_TYPE_CONTACT,
620 gossip_debug (DEBUG_DOMAIN,
621 "InspectHandle Error: %s",
622 error ? error->message : "No error given");
623 g_clear_error (&error);
624 g_array_free (new_handles, TRUE);
628 /* Create contact objects */
629 for (i = 0, id = handles_names; *id && i < new_handles->len; id++, i++) {
630 GossipContact *contact;
633 handle = g_array_index (new_handles, guint, i);
634 contact = g_object_new (GOSSIP_TYPE_CONTACT,
635 "account", priv->account,
640 g_signal_connect (contact, "notify::groups",
641 G_CALLBACK (contact_list_groups_updated_cb),
643 g_signal_connect (contact, "notify::subscription",
644 G_CALLBACK (contact_list_subscription_updated_cb),
646 g_signal_connect (contact, "notify::name",
647 G_CALLBACK (contact_list_name_updated_cb),
650 gossip_debug (DEBUG_DOMAIN, "new contact created: %s (%d)",
653 g_hash_table_insert (priv->contacts,
654 GUINT_TO_POINTER (handle),
657 contacts = g_list_prepend (contacts, g_object_ref (contact));
660 contact_list_get_info (list, new_handles);
662 g_array_free (new_handles, TRUE);
663 g_strfreev (handles_names);
669 empathy_contact_list_rename_group (EmpathyContactList *list,
670 const gchar *old_group,
671 const gchar *new_group)
673 EmpathyContactListPriv *priv;
674 GossipTelepathyGroup *group;
677 g_return_if_fail (EMPATHY_IS_CONTACT_LIST (list));
678 g_return_if_fail (old_group != NULL);
679 g_return_if_fail (new_group != NULL);
681 priv = GET_PRIV (list);
683 group = g_hash_table_find (priv->groups,
684 (GHRFunc) contact_list_find_group,
687 /* The group doesn't exists on this account */
691 gossip_debug (DEBUG_DOMAIN, "rename group %s to %s", group, new_group);
693 /* Remove all members from the old group */
694 members = gossip_telepathy_group_get_members (group);
695 gossip_telepathy_group_remove_members (group, members, "");
696 contact_list_group_members_removed_cb (group, members,
698 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
700 g_hash_table_remove (priv->groups,
701 gossip_telepathy_group_get_object_path (group));
703 /* Add all members to the new group */
704 group = contact_list_get_group (list, new_group);
706 gossip_telepathy_group_add_members (group, members, "");
711 empathy_contact_list_get_groups (EmpathyContactList *list)
713 EmpathyContactListPriv *priv;
714 GList *groups = NULL;
716 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
718 priv = GET_PRIV (list);
720 g_hash_table_foreach (priv->groups,
721 (GHFunc) contact_list_get_groups_foreach,
724 groups = g_list_sort (groups, (GCompareFunc) strcmp);
730 empathy_contact_list_get_contacts (EmpathyContactList *list)
732 EmpathyContactListPriv *priv;
733 GList *contacts = NULL;
735 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
737 priv = GET_PRIV (list);
739 /* FIXME: we should only return contacts that are in the contact list */
740 g_hash_table_foreach (priv->contacts,
741 (GHFunc) contact_list_get_contacts_foreach,
748 contact_list_finalize_proxies (EmpathyContactList *list)
750 EmpathyContactListPriv *priv;
752 priv = GET_PRIV (list);
754 g_signal_handlers_disconnect_by_func (priv->tp_conn,
755 contact_list_destroy_cb,
757 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
758 G_CALLBACK (contact_list_newchannel_cb),
761 if (priv->aliasing_iface) {
762 dbus_g_proxy_disconnect_signal (priv->aliasing_iface,
764 G_CALLBACK (contact_list_aliases_update_cb),
768 if (priv->avatars_iface) {
769 dbus_g_proxy_disconnect_signal (priv->avatars_iface,
771 G_CALLBACK (contact_list_avatar_update_cb),
775 if (priv->presence_iface) {
776 dbus_g_proxy_disconnect_signal (priv->presence_iface,
778 G_CALLBACK (contact_list_presence_update_cb),
784 contact_list_destroy_cb (DBusGProxy *proxy,
785 EmpathyContactList *list)
787 EmpathyContactListPriv *priv;
789 priv = GET_PRIV (list);
791 gossip_debug (DEBUG_DOMAIN, "Connection destroyed. "
792 "Account disconnected or CM crashed.");
794 /* DBus proxies should NOT be used anymore */
795 g_object_unref (priv->tp_conn);
796 priv->tp_conn = NULL;
797 priv->aliasing_iface = NULL;
798 priv->avatars_iface = NULL;
799 priv->presence_iface = NULL;
801 /* Remove all contacts */
802 g_hash_table_foreach (priv->contacts,
803 (GHFunc) contact_list_contact_removed_foreach,
805 g_hash_table_remove_all (priv->contacts);
807 /* Tell the world to not use us anymore */
808 g_signal_emit (list, signals[DESTROY], 0);
812 contact_list_contact_removed_foreach (guint handle,
813 GossipContact *contact,
814 EmpathyContactList *list)
816 g_signal_handlers_disconnect_by_func (contact,
817 contact_list_groups_updated_cb,
819 g_signal_handlers_disconnect_by_func (contact,
820 contact_list_subscription_updated_cb,
822 g_signal_handlers_disconnect_by_func (contact,
823 contact_list_name_updated_cb,
826 g_signal_emit (list, signals[CONTACT_REMOVED], 0, contact);
830 contact_list_block_contact (EmpathyContactList *list,
831 GossipContact *contact)
833 g_signal_handlers_block_by_func (contact,
834 contact_list_groups_updated_cb,
836 g_signal_handlers_block_by_func (contact,
837 contact_list_subscription_updated_cb,
839 g_signal_handlers_block_by_func (contact,
840 contact_list_name_updated_cb,
845 contact_list_unblock_contact (EmpathyContactList *list,
846 GossipContact *contact)
848 g_signal_handlers_unblock_by_func (contact,
849 contact_list_groups_updated_cb,
851 g_signal_handlers_unblock_by_func (contact,
852 contact_list_subscription_updated_cb,
854 g_signal_handlers_unblock_by_func (contact,
855 contact_list_name_updated_cb,
860 contact_list_find_foreach (guint handle,
861 GossipContact *contact,
864 if (strcmp (gossip_contact_get_id (contact), id) == 0) {
872 contact_list_newchannel_cb (DBusGProxy *proxy,
873 const gchar *object_path,
874 const gchar *channel_type,
875 TelepathyHandleType handle_type,
876 guint channel_handle,
877 gboolean suppress_handle,
878 EmpathyContactList *list)
880 EmpathyContactListPriv *priv;
881 GossipTelepathyGroup *group;
883 const gchar *bus_name;
886 priv = GET_PRIV (list);
888 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) != 0 ||
893 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn));
894 new_chan = tp_chan_new (tp_get_bus (),
897 channel_type, handle_type, channel_handle);
899 if (handle_type == TP_HANDLE_TYPE_LIST) {
900 ContactListType list_type;
902 list_type = contact_list_get_type (list, new_chan);
903 if (list_type == CONTACT_LIST_TYPE_UNKNOWN) {
904 gossip_debug (DEBUG_DOMAIN, "Unknown contact list channel");
905 g_object_unref (new_chan);
909 gossip_debug (DEBUG_DOMAIN, "New contact list channel of type: %d",
912 group = gossip_telepathy_group_new (new_chan, priv->tp_conn);
915 case CONTACT_LIST_TYPE_KNOWN:
917 g_object_unref (priv->known);
921 case CONTACT_LIST_TYPE_PUBLISH:
923 g_object_unref (priv->publish);
925 priv->publish = group;
927 case CONTACT_LIST_TYPE_SUBSCRIBE:
928 if (priv->subscribe) {
929 g_object_unref (priv->subscribe);
931 priv->subscribe = group;
934 g_assert_not_reached ();
937 /* Connect and setup the new contact-list group */
938 if (list_type == CONTACT_LIST_TYPE_KNOWN ||
939 list_type == CONTACT_LIST_TYPE_SUBSCRIBE) {
940 g_signal_connect (group, "members-added",
941 G_CALLBACK (contact_list_contact_added_cb),
943 g_signal_connect (group, "members-removed",
944 G_CALLBACK (contact_list_contact_removed_cb),
947 members = gossip_telepathy_group_get_members (group);
948 contact_list_contact_added_cb (group, members, 0,
949 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
951 g_array_free (members, TRUE);
953 if (list_type == CONTACT_LIST_TYPE_PUBLISH) {
958 g_signal_connect (group, "local-pending",
959 G_CALLBACK (contact_list_local_pending_cb),
962 info = gossip_telepathy_group_get_local_pending_members_with_info (group);
965 /* This happens with butterfly because
966 * GetLocalPendingMembersWithInfo is not
968 g_object_unref (new_chan);
972 pending = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
973 for (i = 0; info->len > i; i++) {
974 GValueArray *pending_struct;
978 const gchar *message;
980 pending_struct = g_ptr_array_index (info, i);
981 member = g_value_get_uint (g_value_array_get_nth (pending_struct, 0));
982 invitor = g_value_get_uint (g_value_array_get_nth (pending_struct, 1));
983 reason = g_value_get_uint (g_value_array_get_nth (pending_struct, 2));
984 message = g_value_get_string (g_value_array_get_nth (pending_struct, 3));
986 g_array_insert_val (pending, 0, member);
988 contact_list_local_pending_cb (group, pending,
993 g_value_array_free (pending_struct);
996 g_ptr_array_free (info, TRUE);
997 g_array_free (pending, TRUE);
1000 else if (handle_type == TP_HANDLE_TYPE_GROUP) {
1001 const gchar *object_path;
1003 object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (new_chan));
1004 if (g_hash_table_lookup (priv->groups, object_path)) {
1005 g_object_unref (new_chan);
1009 group = gossip_telepathy_group_new (new_chan, priv->tp_conn);
1011 gossip_debug (DEBUG_DOMAIN, "New server-side group channel: %s",
1012 gossip_telepathy_group_get_name (group));
1014 dbus_g_proxy_connect_signal (DBUS_G_PROXY (new_chan), "Closed",
1016 (contact_list_group_channel_closed_cb),
1019 g_hash_table_insert (priv->groups, g_strdup (object_path), group);
1020 g_signal_connect (group, "members-added",
1021 G_CALLBACK (contact_list_group_members_added_cb),
1023 g_signal_connect (group, "members-removed",
1024 G_CALLBACK (contact_list_group_members_removed_cb),
1027 members = gossip_telepathy_group_get_members (group);
1028 contact_list_group_members_added_cb (group, members, 0,
1029 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1031 g_array_free (members, TRUE);
1034 g_object_unref (new_chan);
1037 static ContactListType
1038 contact_list_get_type (EmpathyContactList *list,
1041 EmpathyContactListPriv *priv;
1043 gchar **handle_name;
1044 ContactListType list_type;
1045 GError *error = NULL;
1047 priv = GET_PRIV (list);
1049 handles = g_array_new (FALSE, FALSE, sizeof (guint));
1050 g_array_append_val (handles, list_chan->handle);
1052 if (!tp_conn_inspect_handles (DBUS_G_PROXY (priv->tp_conn),
1053 TP_HANDLE_TYPE_LIST,
1057 gossip_debug (DEBUG_DOMAIN,
1058 "InspectHandle Error: %s",
1059 error ? error->message : "No error given");
1060 g_clear_error (&error);
1061 g_array_free (handles, TRUE);
1062 return CONTACT_LIST_TYPE_UNKNOWN;
1065 if (strcmp (*handle_name, "subscribe") == 0) {
1066 list_type = CONTACT_LIST_TYPE_SUBSCRIBE;
1067 } else if (strcmp (*handle_name, "publish") == 0) {
1068 list_type = CONTACT_LIST_TYPE_PUBLISH;
1069 } else if (strcmp (*handle_name, "known") == 0) {
1070 list_type = CONTACT_LIST_TYPE_KNOWN;
1072 list_type = CONTACT_LIST_TYPE_UNKNOWN;
1075 g_strfreev (handle_name);
1076 g_array_free (handles, TRUE);
1082 contact_list_contact_added_cb (GossipTelepathyGroup *group,
1086 const gchar *message,
1087 EmpathyContactList *list)
1089 EmpathyContactListPriv *priv;
1090 GList *added_list, *l;
1092 priv = GET_PRIV (list);
1094 added_list = empathy_contact_list_get_from_handles (list, handles);
1096 for (l = added_list; l; l = l->next) {
1097 GossipContact *contact;
1099 contact = GOSSIP_CONTACT (l->data);
1100 contact_list_block_contact (list, contact);
1101 gossip_contact_set_subscription (contact, GOSSIP_SUBSCRIPTION_BOTH);
1102 contact_list_unblock_contact (list, contact);
1104 g_signal_emit (list, signals[CONTACT_ADDED], 0, contact);
1106 g_object_unref (contact);
1109 g_list_free (added_list);
1113 contact_list_contact_removed_cb (GossipTelepathyGroup *group,
1117 const gchar *message,
1118 EmpathyContactList *list)
1120 EmpathyContactListPriv *priv;
1121 GList *removed_list, *l;
1123 priv = GET_PRIV (list);
1125 removed_list = empathy_contact_list_get_from_handles (list, handles);
1127 for (l = removed_list; l; l = l->next) {
1128 GossipContact *contact;
1131 contact = GOSSIP_CONTACT (l->data);
1133 handle = gossip_contact_get_handle (contact);
1134 g_hash_table_remove (priv->contacts, GUINT_TO_POINTER (handle));
1136 g_signal_emit (list, signals[CONTACT_REMOVED], 0, contact);
1138 g_object_unref (contact);
1141 g_list_free (removed_list);
1145 contact_list_local_pending_cb (GossipTelepathyGroup *group,
1149 const gchar *message,
1150 EmpathyContactList *list)
1152 EmpathyContactListPriv *priv;
1153 GList *pending_list, *l;
1155 priv = GET_PRIV (list);
1157 pending_list = empathy_contact_list_get_from_handles (list, handles);
1159 for (l = pending_list; l; l = l->next) {
1160 GossipContact *contact;
1162 contact = GOSSIP_CONTACT (l->data);
1164 /* FIXME: Is that the correct way ? */
1165 contact_list_block_contact (list, contact);
1166 gossip_contact_set_subscription (contact, GOSSIP_SUBSCRIPTION_FROM);
1167 contact_list_unblock_contact (list, contact);
1168 g_signal_emit (list, signals[CONTACT_ADDED], 0, contact);
1170 g_object_unref (contact);
1173 g_list_free (pending_list);
1177 contact_list_groups_updated_cb (GossipContact *contact,
1179 EmpathyContactList *list)
1181 EmpathyContactListPriv *priv;
1182 ContactListData data;
1185 priv = GET_PRIV (list);
1187 /* Make sure all groups are created */
1188 groups = gossip_contact_get_groups (contact);
1189 for (l = groups; l; l = l->next) {
1190 contact_list_get_group (list, l->data);
1193 data.handle = gossip_contact_get_handle (contact);
1194 data.new_groups = groups;
1196 g_hash_table_foreach (priv->groups,
1197 (GHFunc) contact_list_update_groups_foreach,
1202 contact_list_subscription_updated_cb (GossipContact *contact,
1204 EmpathyContactList *list)
1206 EmpathyContactListPriv *priv;
1207 GossipSubscription subscription;
1210 priv = GET_PRIV (list);
1212 subscription = gossip_contact_get_subscription (contact);
1213 handle = gossip_contact_get_handle (contact);
1215 /* FIXME: what to do here, I'm a bit lost... */
1217 gossip_telepathy_group_add_member (priv->publish, handle, "");
1219 gossip_telepathy_group_remove_member (priv->publish, handle, "");
1224 contact_list_name_updated_cb (GossipContact *contact,
1226 EmpathyContactList *list)
1228 EmpathyContactListPriv *priv;
1229 GHashTable *new_alias;
1230 const gchar *new_name;
1232 GError *error = NULL;
1234 priv = GET_PRIV (list);
1236 handle = gossip_contact_get_handle (contact);
1237 new_name = gossip_contact_get_name (contact);
1239 gossip_debug (DEBUG_DOMAIN, "renaming handle %d to %s",
1242 new_alias = g_hash_table_new_full (g_direct_hash,
1247 g_hash_table_insert (new_alias,
1248 GUINT_TO_POINTER (handle),
1249 g_strdup (new_name));
1251 if (!tp_conn_iface_aliasing_set_aliases (priv->aliasing_iface,
1254 gossip_debug (DEBUG_DOMAIN,
1255 "Couldn't rename contact: %s",
1256 error ? error->message : "No error given");
1257 g_clear_error (&error);
1260 g_hash_table_destroy (new_alias);
1264 contact_list_update_groups_foreach (gchar *object_path,
1265 GossipTelepathyGroup *group,
1266 ContactListData *data)
1269 gboolean found = FALSE;
1270 const gchar *group_name;
1273 is_member = gossip_telepathy_group_is_member (group, data->handle);
1274 group_name = gossip_telepathy_group_get_name (group);
1276 for (l = data->new_groups; l; l = l->next) {
1277 if (strcmp (group_name, l->data) == 0) {
1283 if (is_member && !found) {
1284 /* We are no longer member of this group */
1285 gossip_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1286 data->handle, group_name);
1287 gossip_telepathy_group_remove_member (group, data->handle, "");
1290 if (!is_member && found) {
1291 /* We are now member of this group */
1292 gossip_debug (DEBUG_DOMAIN, "Contact %d added to group '%s'",
1293 data->handle, group_name);
1294 gossip_telepathy_group_add_member (group, data->handle, "");
1298 static GossipTelepathyGroup *
1299 contact_list_get_group (EmpathyContactList *list,
1302 EmpathyContactListPriv *priv;
1303 GossipTelepathyGroup *group;
1304 TpChan *group_channel;
1307 char *group_object_path;
1308 const char *names[2] = {name, NULL};
1309 GError *error = NULL;
1311 priv = GET_PRIV (list);
1313 group = g_hash_table_find (priv->groups,
1314 (GHRFunc) contact_list_find_group,
1320 gossip_debug (DEBUG_DOMAIN, "creating new group: %s", name);
1322 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
1323 TP_HANDLE_TYPE_GROUP,
1327 gossip_debug (DEBUG_DOMAIN,
1328 "Couldn't request the creation of a new handle for group: %s",
1329 error ? error->message : "No error given");
1330 g_clear_error (&error);
1333 group_handle = g_array_index (handles, guint, 0);
1334 g_array_free (handles, TRUE);
1336 if (!tp_conn_request_channel (DBUS_G_PROXY (priv->tp_conn),
1337 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1338 TP_HANDLE_TYPE_GROUP,
1343 gossip_debug (DEBUG_DOMAIN,
1344 "Couldn't request the creation of a new group channel: %s",
1345 error ? error->message : "No error given");
1346 g_clear_error (&error);
1350 group_channel = tp_chan_new (tp_get_bus (),
1351 dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn)),
1353 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1354 TP_HANDLE_TYPE_GROUP,
1357 dbus_g_proxy_connect_signal (DBUS_G_PROXY (group_channel),
1360 (contact_list_group_channel_closed_cb),
1364 group = gossip_telepathy_group_new (group_channel, priv->tp_conn);
1365 g_hash_table_insert (priv->groups, group_object_path, group);
1366 g_signal_connect (group, "members-added",
1367 G_CALLBACK (contact_list_group_members_added_cb),
1369 g_signal_connect (group, "members-removed",
1370 G_CALLBACK (contact_list_group_members_removed_cb),
1377 contact_list_find_group (gchar *key,
1378 GossipTelepathyGroup *group,
1381 if (strcmp (group_name, gossip_telepathy_group_get_name (group)) == 0) {
1389 contact_list_get_groups_foreach (gchar *key,
1390 GossipTelepathyGroup *group,
1395 name = gossip_telepathy_group_get_name (group);
1396 *groups = g_list_append (*groups, g_strdup (name));
1400 contact_list_group_channel_closed_cb (TpChan *channel,
1401 EmpathyContactList *list)
1403 EmpathyContactListPriv *priv;
1405 priv = GET_PRIV (list);
1407 g_hash_table_remove (priv->groups,
1408 dbus_g_proxy_get_path (DBUS_G_PROXY (channel)));
1412 contact_list_group_members_added_cb (GossipTelepathyGroup *group,
1416 const gchar *message,
1417 EmpathyContactList *list)
1419 EmpathyContactListPriv *priv;
1420 GList *added_list, *l;
1421 const gchar *group_name;
1423 priv = GET_PRIV (list);
1425 group_name = gossip_telepathy_group_get_name (group);
1426 added_list = empathy_contact_list_get_from_handles (list, members);
1428 for (l = added_list; l; l = l->next) {
1429 GossipContact *contact;
1430 GList *contact_groups;
1432 contact = GOSSIP_CONTACT (l->data);
1433 contact_groups = gossip_contact_get_groups (contact);
1435 if (!g_list_find_custom (contact_groups,
1437 (GCompareFunc) strcmp)) {
1438 gossip_debug (DEBUG_DOMAIN, "Contact %s added to group '%s'",
1439 gossip_contact_get_name (contact),
1441 contact_groups = g_list_append (contact_groups,
1442 g_strdup (group_name));
1443 contact_list_block_contact (list, contact);
1444 gossip_contact_set_groups (contact, contact_groups);
1445 contact_list_unblock_contact (list, contact);
1448 g_object_unref (contact);
1451 g_list_free (added_list);
1455 contact_list_group_members_removed_cb (GossipTelepathyGroup *group,
1459 const gchar *message,
1460 EmpathyContactList *list)
1462 EmpathyContactListPriv *priv;
1463 GList *removed_list, *l;
1464 const gchar *group_name;
1466 priv = GET_PRIV (list);
1468 group_name = gossip_telepathy_group_get_name (group);
1469 removed_list = empathy_contact_list_get_from_handles (list, members);
1471 for (l = removed_list; l; l = l->next) {
1472 GossipContact *contact;
1473 GList *contact_groups;
1476 /* FIXME: Does it leak ? */
1477 contact = GOSSIP_CONTACT (l->data);
1478 contact_groups = gossip_contact_get_groups (contact);
1479 contact_groups = g_list_copy (contact_groups);
1481 to_remove = g_list_find_custom (contact_groups,
1483 (GCompareFunc) strcmp);
1485 gossip_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1486 gossip_contact_get_handle (contact),
1488 contact_groups = g_list_remove_link (contact_groups,
1490 contact_list_block_contact (list, contact);
1491 gossip_contact_set_groups (contact, contact_groups);
1492 contact_list_unblock_contact (list, contact);
1495 g_list_free (contact_groups);
1497 g_object_unref (contact);
1500 g_list_free (removed_list);
1504 contact_list_get_contacts_foreach (guint handle,
1505 GossipContact *contact,
1508 *contacts = g_list_append (*contacts, g_object_ref (contact));
1512 contact_list_get_info (EmpathyContactList *list,
1515 EmpathyContactListPriv *priv;
1516 GError *error = NULL;
1518 priv = GET_PRIV (list);
1520 if (priv->presence_iface) {
1521 /* FIXME: We should use GetPresence instead */
1522 if (!tp_conn_iface_presence_request_presence (priv->presence_iface,
1524 gossip_debug (DEBUG_DOMAIN,
1525 "Could not request presences: %s",
1526 error ? error->message : "No error given");
1527 g_clear_error (&error);
1531 if (priv->aliasing_iface) {
1532 ContactListAliasesRequestData *data;
1534 data = g_slice_new (ContactListAliasesRequestData);
1536 data->handles = g_memdup (handles->data, handles->len * sizeof (guint));
1538 tp_conn_iface_aliasing_request_aliases_async (priv->aliasing_iface,
1540 (tp_conn_iface_aliasing_request_aliases_reply)
1541 contact_list_request_aliases_cb,
1545 if (priv->avatars_iface) {
1548 for (i = 0; i < handles->len; i++) {
1551 handle = g_array_index (handles, gint, i);
1552 contact_list_request_avatar (list, handle);
1558 contact_list_request_avatar (EmpathyContactList *list,
1561 EmpathyContactListPriv *priv;
1563 priv = GET_PRIV (list);
1565 /* We queue avatar requests to not send too many dbus async
1566 * calls at once. If we don't we reach the dbus's limit of
1568 priv->avatar_requests_queue = g_list_append (priv->avatar_requests_queue,
1569 GUINT_TO_POINTER (handle));
1570 contact_list_start_avatar_requests (list);
1574 contact_list_start_avatar_requests (EmpathyContactList *list)
1576 EmpathyContactListPriv *priv;
1577 ContactListAvatarRequestData *data;
1579 priv = GET_PRIV (list);
1581 while (n_avatar_requests < MAX_AVATAR_REQUESTS &&
1582 priv->avatar_requests_queue) {
1583 data = g_slice_new (ContactListAvatarRequestData);
1585 data->handle = GPOINTER_TO_UINT (priv->avatar_requests_queue->data);
1587 n_avatar_requests++;
1588 priv->avatar_requests_queue = g_list_remove (priv->avatar_requests_queue,
1589 priv->avatar_requests_queue->data);
1591 tp_conn_iface_avatars_request_avatar_async (priv->avatars_iface,
1593 (tp_conn_iface_avatars_request_avatar_reply)
1594 contact_list_request_avatar_cb,
1600 contact_list_avatar_update_cb (DBusGProxy *proxy,
1603 EmpathyContactList *list)
1605 gossip_debug (DEBUG_DOMAIN, "Changing avatar for %d to %s",
1608 contact_list_request_avatar (list, handle);
1612 contact_list_request_avatar_cb (DBusGProxy *proxy,
1613 GArray *avatar_data,
1616 ContactListAvatarRequestData *data)
1618 GossipContact *contact;
1620 contact = empathy_contact_list_get_from_handle (data->list, data->handle);
1623 gossip_debug (DEBUG_DOMAIN, "Error requesting avatar for %s: %s",
1624 gossip_contact_get_name (contact),
1625 error ? error->message : "No error given");
1627 GossipAvatar *avatar;
1629 avatar = gossip_avatar_new (avatar_data->data,
1632 contact_list_block_contact (data->list, contact);
1633 gossip_contact_set_avatar (contact, avatar);
1634 contact_list_unblock_contact (data->list, contact);
1635 gossip_avatar_unref (avatar);
1638 n_avatar_requests--;
1639 contact_list_start_avatar_requests (data->list);
1641 g_slice_free (ContactListAvatarRequestData, data);
1645 contact_list_aliases_update_cb (DBusGProxy *proxy,
1646 GPtrArray *renamed_handlers,
1647 EmpathyContactList *list)
1651 for (i = 0; renamed_handlers->len > i; i++) {
1654 GValueArray *renamed_struct;
1655 GossipContact *contact;
1657 renamed_struct = g_ptr_array_index (renamed_handlers, i);
1658 handle = g_value_get_uint(g_value_array_get_nth (renamed_struct, 0));
1659 alias = g_value_get_string(g_value_array_get_nth (renamed_struct, 1));
1661 if (alias && *alias == '\0') {
1665 contact = empathy_contact_list_get_from_handle (list, handle);
1666 contact_list_block_contact (list, contact);
1667 gossip_contact_set_name (contact, alias);
1668 contact_list_unblock_contact (list, contact);
1670 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (update cb)",
1676 contact_list_request_aliases_cb (DBusGProxy *proxy,
1677 gchar **contact_names,
1679 ContactListAliasesRequestData *data)
1684 for (name = contact_names; *name && !error; name++) {
1685 GossipContact *contact;
1687 contact = empathy_contact_list_get_from_handle (data->list,
1689 contact_list_block_contact (data->list, contact);
1690 gossip_contact_set_name (contact, *name);
1691 contact_list_unblock_contact (data->list, contact);
1693 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (request cb)",
1694 data->handles[i], *name);
1699 g_free (data->handles);
1700 g_slice_free (ContactListAliasesRequestData, data);
1704 contact_list_presence_update_cb (DBusGProxy *proxy,
1705 GHashTable *handle_table,
1706 EmpathyContactList *list)
1708 g_hash_table_foreach (handle_table,
1709 (GHFunc) contact_list_parse_presence_foreach,
1714 contact_list_parse_presence_foreach (guint handle,
1715 GValueArray *presence_struct,
1716 EmpathyContactList *list)
1718 GHashTable *presences_table;
1719 GossipContact *contact;
1720 GossipPresence *presence = NULL;
1722 contact = empathy_contact_list_get_from_handle (list, handle);
1723 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
1725 g_hash_table_foreach (presences_table,
1726 (GHFunc) contact_list_presences_table_foreach,
1729 gossip_debug (DEBUG_DOMAIN, "Presence changed for %s (%d) to %s (%d)",
1730 gossip_contact_get_name (contact),
1732 presence ? gossip_presence_get_status (presence) : "unset",
1733 presence ? gossip_presence_get_state (presence) : MC_PRESENCE_UNSET);
1735 contact_list_block_contact (list, contact);
1736 gossip_contact_set_presence (contact, presence);
1737 contact_list_unblock_contact (list, contact);
1741 contact_list_presences_table_foreach (const gchar *state_str,
1742 GHashTable *presences_table,
1743 GossipPresence **presence)
1746 const GValue *message;
1748 state = gossip_presence_state_from_str (state_str);
1749 if ((state == MC_PRESENCE_UNSET) || (state == MC_PRESENCE_OFFLINE)) {
1754 g_object_unref (*presence);
1758 *presence = gossip_presence_new ();
1759 gossip_presence_set_state (*presence, state);
1761 message = g_hash_table_lookup (presences_table, "message");
1762 if (message != NULL) {
1763 gossip_presence_set_status (*presence,
1764 g_value_get_string (message));