1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Xavier Claessens <xclaesse@gmail.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
25 #include <libtelepathy/tp-conn.h>
26 #include <libtelepathy/tp-chan.h>
27 #include <libtelepathy/tp-helpers.h>
28 #include <libtelepathy/tp-chan-type-contact-list-gen.h>
29 #include <libtelepathy/tp-conn-iface-aliasing-gen.h>
30 #include <libtelepathy/tp-conn-iface-presence-gen.h>
31 #include <libtelepathy/tp-conn-iface-avatars-gen.h>
33 #include "empathy-contact-list.h"
34 #include "empathy-session.h"
35 #include "gossip-debug.h"
36 #include "gossip-telepathy-group.h"
38 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
39 EMPATHY_TYPE_CONTACT_LIST, EmpathyContactListPriv))
41 #define DEBUG_DOMAIN "ContactList"
42 #define MAX_AVATAR_REQUESTS 10
44 struct _EmpathyContactListPriv {
47 GossipContact *own_contact;
49 GossipTelepathyGroup *known;
50 GossipTelepathyGroup *publish;
51 GossipTelepathyGroup *subscribe;
56 DBusGProxy *aliasing_iface;
57 DBusGProxy *avatars_iface;
58 DBusGProxy *presence_iface;
60 GList *avatar_requests_queue;
64 CONTACT_LIST_TYPE_KNOWN,
65 CONTACT_LIST_TYPE_PUBLISH,
66 CONTACT_LIST_TYPE_SUBSCRIBE,
67 CONTACT_LIST_TYPE_UNKNOWN,
68 CONTACT_LIST_TYPE_COUNT
77 EmpathyContactList *list;
79 } ContactListAvatarRequestData;
82 EmpathyContactList *list;
84 } ContactListAliasesRequestData;
86 static void empathy_contact_list_class_init (EmpathyContactListClass *klass);
87 static void empathy_contact_list_init (EmpathyContactList *list);
88 static void contact_list_finalize (GObject *object);
89 static void contact_list_finalize_proxies (EmpathyContactList *list);
90 static void contact_list_contact_removed_foreach (guint handle,
91 GossipContact *contact,
92 EmpathyContactList *list);
93 static void contact_list_destroy_cb (DBusGProxy *proxy,
94 EmpathyContactList *list);
95 static gboolean contact_list_find_foreach (guint handle,
96 GossipContact *contact,
98 static void contact_list_newchannel_cb (DBusGProxy *proxy,
99 const gchar *object_path,
100 const gchar *channel_type,
101 TelepathyHandleType handle_type,
102 guint channel_handle,
103 gboolean suppress_handle,
104 EmpathyContactList *list);
105 static ContactListType contact_list_get_type (EmpathyContactList *list,
107 static void contact_list_contact_added_cb (GossipTelepathyGroup *group,
111 const gchar *message,
112 EmpathyContactList *list);
113 static void contact_list_contact_removed_cb (GossipTelepathyGroup *group,
117 const gchar *message,
118 EmpathyContactList *list);
119 static void contact_list_local_pending_cb (GossipTelepathyGroup *group,
123 const gchar *message,
124 EmpathyContactList *list);
125 static void contact_list_groups_updated_cb (GossipContact *contact,
127 EmpathyContactList *list);
128 static void contact_list_subscription_updated_cb (GossipContact *contact,
130 EmpathyContactList *list);
131 static void contact_list_name_updated_cb (GossipContact *contact,
133 EmpathyContactList *list);
134 static void contact_list_update_groups_foreach (gchar *object_path,
135 GossipTelepathyGroup *group,
136 ContactListData *data);
137 static GossipTelepathyGroup * contact_list_get_group (EmpathyContactList *list,
139 static gboolean contact_list_find_group (gchar *key,
140 GossipTelepathyGroup *group,
142 static void contact_list_get_groups_foreach (gchar *key,
143 GossipTelepathyGroup *group,
145 static void contact_list_group_channel_closed_cb (TpChan *channel,
146 EmpathyContactList *list);
147 static void contact_list_group_members_added_cb (GossipTelepathyGroup *group,
151 const gchar *message,
152 EmpathyContactList *list);
153 static void contact_list_group_members_removed_cb (GossipTelepathyGroup *group,
157 const gchar *message,
158 EmpathyContactList *list);
159 static void contact_list_get_contacts_foreach (guint handle,
160 GossipContact *contact,
162 static void contact_list_get_info (EmpathyContactList *list,
164 static void contact_list_request_avatar (EmpathyContactList *list,
166 static void contact_list_start_avatar_requests (EmpathyContactList *list);
167 static void contact_list_avatar_update_cb (DBusGProxy *proxy,
170 EmpathyContactList *list);
171 static void contact_list_request_avatar_cb (DBusGProxy *proxy,
175 ContactListAvatarRequestData *data);
176 static void contact_list_aliases_update_cb (DBusGProxy *proxy,
178 EmpathyContactList *list);
179 static void contact_list_request_aliases_cb (DBusGProxy *proxy,
180 gchar **contact_names,
182 ContactListAliasesRequestData *data);
183 static void contact_list_presence_update_cb (DBusGProxy *proxy,
184 GHashTable *handle_table,
185 EmpathyContactList *list);
186 static void contact_list_parse_presence_foreach (guint handle,
187 GValueArray *presence_struct,
188 EmpathyContactList *list);
189 static void contact_list_presences_table_foreach (const gchar *state_str,
190 GHashTable *presences_table,
191 GossipPresence **presence);
192 static GossipPresenceState contact_list_presence_state_from_str (const gchar *str);
201 static guint signals[LAST_SIGNAL];
202 static guint n_avatar_requests = 0;
204 G_DEFINE_TYPE (EmpathyContactList, empathy_contact_list, G_TYPE_OBJECT);
207 empathy_contact_list_class_init (EmpathyContactListClass *klass)
209 GObjectClass *object_class = G_OBJECT_CLASS (klass);
211 object_class->finalize = contact_list_finalize;
213 signals[CONTACT_ADDED] =
214 g_signal_new ("contact-added",
215 G_TYPE_FROM_CLASS (klass),
219 g_cclosure_marshal_VOID__OBJECT,
221 1, GOSSIP_TYPE_CONTACT);
223 signals[CONTACT_REMOVED] =
224 g_signal_new ("contact-removed",
225 G_TYPE_FROM_CLASS (klass),
229 g_cclosure_marshal_VOID__OBJECT,
231 1, GOSSIP_TYPE_CONTACT);
234 g_signal_new ("destroy",
235 G_TYPE_FROM_CLASS (klass),
239 g_cclosure_marshal_VOID__VOID,
243 g_type_class_add_private (object_class, sizeof (EmpathyContactListPriv));
247 empathy_contact_list_init (EmpathyContactList *list)
249 EmpathyContactListPriv *priv;
251 priv = GET_PRIV (list);
253 priv->groups = g_hash_table_new_full (g_str_hash,
255 (GDestroyNotify) g_free,
256 (GDestroyNotify) g_object_unref);
257 priv->contacts = g_hash_table_new_full (g_direct_hash,
260 (GDestroyNotify) g_object_unref);
265 contact_list_finalize (GObject *object)
267 EmpathyContactListPriv *priv;
268 EmpathyContactList *list;
270 list = EMPATHY_CONTACT_LIST (object);
271 priv = GET_PRIV (list);
273 gossip_debug (DEBUG_DOMAIN, "finalize: %p", object);
276 contact_list_finalize_proxies (list);
277 g_object_unref (priv->tp_conn);
281 g_object_unref (priv->known);
284 if (priv->subscribe) {
285 g_object_unref (priv->subscribe);
289 g_object_unref (priv->publish);
292 g_object_unref (priv->account);
293 g_object_unref (priv->own_contact);
294 g_hash_table_destroy (priv->groups);
295 g_hash_table_destroy (priv->contacts);
297 G_OBJECT_CLASS (empathy_contact_list_parent_class)->finalize (object);
301 empathy_contact_list_new (McAccount *account)
303 EmpathyContactListPriv *priv;
304 EmpathyContactList *list;
308 GError *error = NULL;
310 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
312 mc = empathy_session_get_mission_control ();
314 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
315 /* The account is not connected, nothing to do. */
319 tp_conn = mission_control_get_connection (mc, account, NULL);
320 g_return_val_if_fail (tp_conn != NULL, NULL);
322 list = g_object_new (EMPATHY_TYPE_CONTACT_LIST, NULL);
323 priv = GET_PRIV (list);
325 priv->tp_conn = tp_conn;
326 priv->account = g_object_ref (account);
328 g_signal_connect (priv->tp_conn, "destroy",
329 G_CALLBACK (contact_list_destroy_cb),
332 priv->aliasing_iface = tp_conn_get_interface (priv->tp_conn,
333 TELEPATHY_CONN_IFACE_ALIASING_QUARK);
334 priv->avatars_iface = tp_conn_get_interface (priv->tp_conn,
335 TELEPATHY_CONN_IFACE_AVATARS_QUARK);
336 priv->presence_iface = tp_conn_get_interface (priv->tp_conn,
337 TELEPATHY_CONN_IFACE_PRESENCE_QUARK);
339 if (priv->aliasing_iface) {
340 dbus_g_proxy_connect_signal (priv->aliasing_iface,
342 G_CALLBACK (contact_list_aliases_update_cb),
346 if (priv->avatars_iface) {
347 dbus_g_proxy_connect_signal (priv->avatars_iface,
349 G_CALLBACK (contact_list_avatar_update_cb),
353 if (priv->presence_iface) {
354 dbus_g_proxy_connect_signal (priv->presence_iface,
356 G_CALLBACK (contact_list_presence_update_cb),
360 /* Get our own handle and contact */
361 if (!tp_conn_get_self_handle (DBUS_G_PROXY (priv->tp_conn),
363 gossip_debug (DEBUG_DOMAIN, "GetSelfHandle Error: %s",
364 error ? error->message : "No error given");
365 g_clear_error (&error);
367 priv->own_contact = empathy_contact_list_get_from_handle (list, handle);
374 empathy_contact_list_setup (EmpathyContactList *list)
376 EmpathyContactListPriv *priv;
378 GError *error = NULL;
381 g_return_if_fail (EMPATHY_IS_CONTACT_LIST (list));
383 priv = GET_PRIV (list);
385 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
386 G_CALLBACK (contact_list_newchannel_cb),
389 /* Get existing channels */
390 if (!tp_conn_list_channels (DBUS_G_PROXY (priv->tp_conn),
393 gossip_debug (DEBUG_DOMAIN,
394 "Failed to get list of open channels: %s",
395 error ? error->message : "No error given");
396 g_clear_error (&error);
400 for (i = 0; channels->len > i; i++) {
401 GValueArray *chan_struct;
402 const gchar *object_path;
403 const gchar *chan_iface;
404 TelepathyHandleType handle_type;
407 chan_struct = g_ptr_array_index (channels, i);
408 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
409 chan_iface = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
410 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
411 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
413 contact_list_newchannel_cb (DBUS_G_PROXY (priv->tp_conn),
414 object_path, chan_iface,
418 g_value_array_free (chan_struct);
421 g_ptr_array_free (channels, TRUE);
425 empathy_contact_list_get_account (EmpathyContactList *list)
427 EmpathyContactListPriv *priv;
429 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
431 priv = GET_PRIV (list);
433 return priv->account;
437 empathy_contact_list_get_own (EmpathyContactList *list)
439 EmpathyContactListPriv *priv;
441 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
443 priv = GET_PRIV (list);
445 return priv->own_contact;
449 empathy_contact_list_find (EmpathyContactList *list,
452 EmpathyContactListPriv *priv;
453 GossipContact *contact;
455 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
457 priv = GET_PRIV (list);
459 contact = g_hash_table_find (priv->contacts,
460 (GHRFunc) contact_list_find_foreach,
467 empathy_contact_list_add (EmpathyContactList *list,
469 const gchar *message)
471 EmpathyContactListPriv *priv;
473 g_return_if_fail (EMPATHY_IS_CONTACT_LIST (list));
475 priv = GET_PRIV (list);
477 gossip_telepathy_group_add_member (priv->subscribe, handle, message);
481 empathy_contact_list_remove (EmpathyContactList *list,
484 EmpathyContactListPriv *priv;
486 g_return_if_fail (EMPATHY_IS_CONTACT_LIST (list));
488 priv = GET_PRIV (list);
490 gossip_telepathy_group_remove_member (priv->subscribe, handle, "");
491 gossip_telepathy_group_remove_member (priv->publish, handle, "");
492 gossip_telepathy_group_remove_member (priv->known, handle, "");
496 empathy_contact_list_get_from_id (EmpathyContactList *list,
499 EmpathyContactListPriv *priv;
500 GossipContact *contact;
501 const gchar *contact_ids[] = {id, NULL};
504 GError *error = NULL;
506 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
507 g_return_val_if_fail (id != NULL, NULL);
509 priv = GET_PRIV (list);
511 contact = empathy_contact_list_find (list, id);
516 /* The id is unknown, requests a new handle */
517 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
518 TP_HANDLE_TYPE_CONTACT,
521 gossip_debug (DEBUG_DOMAIN,
522 "RequestHandle for %s failed: %s", id,
523 error ? error->message : "No error given");
524 g_clear_error (&error);
528 handle = g_array_index(handles, guint, 0);
529 g_array_free (handles, TRUE);
531 return empathy_contact_list_get_from_handle (list, handle);
535 empathy_contact_list_get_from_handle (EmpathyContactList *list,
538 GossipContact *contact;
542 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
544 handles = g_array_new (FALSE, FALSE, sizeof (guint));
545 g_array_append_val (handles, handle);
547 contacts = empathy_contact_list_get_from_handles (list, handles);
548 g_array_free (handles, TRUE);
554 contact = contacts->data;
555 g_list_free (contacts);
561 empathy_contact_list_get_from_handles (EmpathyContactList *list,
564 EmpathyContactListPriv *priv;
565 gchar **handles_names;
568 GList *contacts = NULL;
570 GError *error = NULL;
572 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
573 g_return_val_if_fail (handles != NULL, NULL);
575 priv = GET_PRIV (list);
577 /* Search all handles we already have */
578 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
579 for (i = 0; i < handles->len; i++) {
580 GossipContact *contact;
583 handle = g_array_index (handles, guint, i);
584 contact = g_hash_table_lookup (priv->contacts,
585 GUINT_TO_POINTER (handle));
588 contacts = g_list_prepend (contacts,
589 g_object_ref (contact));
591 g_array_append_val (new_handles, handle);
595 if (new_handles->len == 0) {
599 /* Holds all handles we don't have yet.
600 * FIXME: We should release them at some point. */
601 if (!tp_conn_hold_handles (DBUS_G_PROXY (priv->tp_conn),
602 TP_HANDLE_TYPE_CONTACT,
603 new_handles, &error)) {
604 gossip_debug (DEBUG_DOMAIN,
605 "HoldHandles Error: %s",
606 error ? error->message : "No error given");
607 g_clear_error (&error);
608 g_array_free (new_handles, TRUE);
612 /* Get the IDs of all new handles */
613 if (!tp_conn_inspect_handles (DBUS_G_PROXY (priv->tp_conn),
614 TP_HANDLE_TYPE_CONTACT,
618 gossip_debug (DEBUG_DOMAIN,
619 "InspectHandle Error: %s",
620 error ? error->message : "No error given");
621 g_clear_error (&error);
622 g_array_free (new_handles, TRUE);
626 /* Create contact objects */
627 for (i = 0, id = handles_names; *id && i < new_handles->len; id++, i++) {
628 GossipContact *contact;
631 handle = g_array_index (new_handles, guint, i);
632 contact = g_object_new (GOSSIP_TYPE_CONTACT,
633 "account", priv->account,
638 g_signal_connect (contact, "notify::groups",
639 G_CALLBACK (contact_list_groups_updated_cb),
641 g_signal_connect (contact, "notify::subscription",
642 G_CALLBACK (contact_list_subscription_updated_cb),
644 g_signal_connect (contact, "notify::name",
645 G_CALLBACK (contact_list_name_updated_cb),
648 gossip_debug (DEBUG_DOMAIN, "new contact created: %s (%d)",
651 g_hash_table_insert (priv->contacts,
652 GUINT_TO_POINTER (handle),
655 contacts = g_list_prepend (contacts, g_object_ref (contact));
658 contact_list_get_info (list, new_handles);
660 g_array_free (new_handles, TRUE);
661 g_strfreev (handles_names);
667 empathy_contact_list_rename_group (EmpathyContactList *list,
668 const gchar *old_group,
669 const gchar *new_group)
671 EmpathyContactListPriv *priv;
672 GossipTelepathyGroup *group;
675 g_return_if_fail (EMPATHY_IS_CONTACT_LIST (list));
676 g_return_if_fail (old_group != NULL);
677 g_return_if_fail (new_group != NULL);
679 priv = GET_PRIV (list);
681 group = g_hash_table_find (priv->groups,
682 (GHRFunc) contact_list_find_group,
685 /* The group doesn't exists on this account */
689 gossip_debug (DEBUG_DOMAIN, "rename group %s to %s", group, new_group);
691 /* Remove all members from the old group */
692 members = gossip_telepathy_group_get_members (group);
693 gossip_telepathy_group_remove_members (group, members, "");
694 contact_list_group_members_removed_cb (group, members,
696 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
698 g_hash_table_remove (priv->groups,
699 gossip_telepathy_group_get_object_path (group));
701 /* Add all members to the new group */
702 group = contact_list_get_group (list, new_group);
704 gossip_telepathy_group_add_members (group, members, "");
709 empathy_contact_list_get_groups (EmpathyContactList *list)
711 EmpathyContactListPriv *priv;
712 GList *groups = NULL;
714 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
716 priv = GET_PRIV (list);
718 g_hash_table_foreach (priv->groups,
719 (GHFunc) contact_list_get_groups_foreach,
722 groups = g_list_sort (groups, (GCompareFunc) strcmp);
728 empathy_contact_list_get_contacts (EmpathyContactList *list)
730 EmpathyContactListPriv *priv;
731 GList *contacts = NULL;
733 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
735 priv = GET_PRIV (list);
737 /* FIXME: we should only return contacts that are in the contact list */
738 g_hash_table_foreach (priv->contacts,
739 (GHFunc) contact_list_get_contacts_foreach,
746 contact_list_finalize_proxies (EmpathyContactList *list)
748 EmpathyContactListPriv *priv;
750 priv = GET_PRIV (list);
752 g_signal_handlers_disconnect_by_func (priv->tp_conn,
753 contact_list_destroy_cb,
755 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
756 G_CALLBACK (contact_list_newchannel_cb),
759 if (priv->aliasing_iface) {
760 dbus_g_proxy_disconnect_signal (priv->aliasing_iface,
762 G_CALLBACK (contact_list_aliases_update_cb),
766 if (priv->avatars_iface) {
767 dbus_g_proxy_disconnect_signal (priv->avatars_iface,
769 G_CALLBACK (contact_list_avatar_update_cb),
773 if (priv->presence_iface) {
774 dbus_g_proxy_disconnect_signal (priv->presence_iface,
776 G_CALLBACK (contact_list_presence_update_cb),
782 contact_list_destroy_cb (DBusGProxy *proxy,
783 EmpathyContactList *list)
785 EmpathyContactListPriv *priv;
787 priv = GET_PRIV (list);
789 gossip_debug (DEBUG_DOMAIN, "Connection destroyed. "
790 "Account disconnected or CM crashed.");
792 /* DBus proxies should NOT be used anymore */
793 g_object_unref (priv->tp_conn);
794 priv->tp_conn = NULL;
795 priv->aliasing_iface = NULL;
796 priv->avatars_iface = NULL;
797 priv->presence_iface = NULL;
799 /* Remove all contacts */
800 g_hash_table_foreach (priv->contacts,
801 (GHFunc) contact_list_contact_removed_foreach,
803 g_hash_table_remove_all (priv->contacts);
805 /* Tell the world to not use us anymore */
806 g_signal_emit (list, signals[DESTROY], 0);
810 contact_list_contact_removed_foreach (guint handle,
811 GossipContact *contact,
812 EmpathyContactList *list)
814 g_signal_handlers_disconnect_by_func (contact,
815 contact_list_groups_updated_cb,
817 g_signal_handlers_disconnect_by_func (contact,
818 contact_list_subscription_updated_cb,
820 g_signal_handlers_disconnect_by_func (contact,
821 contact_list_name_updated_cb,
824 g_signal_emit (list, signals[CONTACT_REMOVED], 0, contact);
828 contact_list_block_contact (EmpathyContactList *list,
829 GossipContact *contact)
831 g_signal_handlers_block_by_func (contact,
832 contact_list_groups_updated_cb,
834 g_signal_handlers_block_by_func (contact,
835 contact_list_subscription_updated_cb,
837 g_signal_handlers_block_by_func (contact,
838 contact_list_name_updated_cb,
843 contact_list_unblock_contact (EmpathyContactList *list,
844 GossipContact *contact)
846 g_signal_handlers_unblock_by_func (contact,
847 contact_list_groups_updated_cb,
849 g_signal_handlers_unblock_by_func (contact,
850 contact_list_subscription_updated_cb,
852 g_signal_handlers_unblock_by_func (contact,
853 contact_list_name_updated_cb,
858 contact_list_find_foreach (guint handle,
859 GossipContact *contact,
862 if (strcmp (gossip_contact_get_id (contact), id) == 0) {
870 contact_list_newchannel_cb (DBusGProxy *proxy,
871 const gchar *object_path,
872 const gchar *channel_type,
873 TelepathyHandleType handle_type,
874 guint channel_handle,
875 gboolean suppress_handle,
876 EmpathyContactList *list)
878 EmpathyContactListPriv *priv;
879 GossipTelepathyGroup *group;
881 const gchar *bus_name;
884 priv = GET_PRIV (list);
886 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) != 0 ||
891 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn));
892 new_chan = tp_chan_new (tp_get_bus (),
895 channel_type, handle_type, channel_handle);
897 if (handle_type == TP_HANDLE_TYPE_LIST) {
898 ContactListType list_type;
900 list_type = contact_list_get_type (list, new_chan);
901 if (list_type == CONTACT_LIST_TYPE_UNKNOWN) {
902 gossip_debug (DEBUG_DOMAIN, "Unknown contact list channel");
903 g_object_unref (new_chan);
907 gossip_debug (DEBUG_DOMAIN, "New contact list channel of type: %d",
910 group = gossip_telepathy_group_new (new_chan, priv->tp_conn);
913 case CONTACT_LIST_TYPE_KNOWN:
915 g_object_unref (priv->known);
919 case CONTACT_LIST_TYPE_PUBLISH:
921 g_object_unref (priv->publish);
923 priv->publish = group;
925 case CONTACT_LIST_TYPE_SUBSCRIBE:
926 if (priv->subscribe) {
927 g_object_unref (priv->subscribe);
929 priv->subscribe = group;
932 g_assert_not_reached ();
935 /* Connect and setup the new contact-list group */
936 if (list_type == CONTACT_LIST_TYPE_KNOWN ||
937 list_type == CONTACT_LIST_TYPE_SUBSCRIBE) {
938 g_signal_connect (group, "members-added",
939 G_CALLBACK (contact_list_contact_added_cb),
941 g_signal_connect (group, "members-removed",
942 G_CALLBACK (contact_list_contact_removed_cb),
945 members = gossip_telepathy_group_get_members (group);
946 contact_list_contact_added_cb (group, members, 0,
947 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
949 g_array_free (members, TRUE);
951 if (list_type == CONTACT_LIST_TYPE_PUBLISH) {
956 g_signal_connect (group, "local-pending",
957 G_CALLBACK (contact_list_local_pending_cb),
960 info = gossip_telepathy_group_get_local_pending_members_with_info (group);
963 /* This happens with butterfly because
964 * GetLocalPendingMembersWithInfo is not
966 g_object_unref (new_chan);
970 pending = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
971 for (i = 0; info->len > i; i++) {
972 GValueArray *pending_struct;
976 const gchar *message;
978 pending_struct = g_ptr_array_index (info, i);
979 member = g_value_get_uint (g_value_array_get_nth (pending_struct, 0));
980 invitor = g_value_get_uint (g_value_array_get_nth (pending_struct, 1));
981 reason = g_value_get_uint (g_value_array_get_nth (pending_struct, 2));
982 message = g_value_get_string (g_value_array_get_nth (pending_struct, 3));
984 g_array_insert_val (pending, 0, member);
986 contact_list_local_pending_cb (group, pending,
991 g_value_array_free (pending_struct);
994 g_ptr_array_free (info, TRUE);
995 g_array_free (pending, TRUE);
998 else if (handle_type == TP_HANDLE_TYPE_GROUP) {
999 const gchar *object_path;
1001 object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (new_chan));
1002 if (g_hash_table_lookup (priv->groups, object_path)) {
1003 g_object_unref (new_chan);
1007 group = gossip_telepathy_group_new (new_chan, priv->tp_conn);
1009 gossip_debug (DEBUG_DOMAIN, "New server-side group channel: %s",
1010 gossip_telepathy_group_get_name (group));
1012 dbus_g_proxy_connect_signal (DBUS_G_PROXY (new_chan), "Closed",
1014 (contact_list_group_channel_closed_cb),
1017 g_hash_table_insert (priv->groups, g_strdup (object_path), group);
1018 g_signal_connect (group, "members-added",
1019 G_CALLBACK (contact_list_group_members_added_cb),
1021 g_signal_connect (group, "members-removed",
1022 G_CALLBACK (contact_list_group_members_removed_cb),
1025 members = gossip_telepathy_group_get_members (group);
1026 contact_list_group_members_added_cb (group, members, 0,
1027 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1029 g_array_free (members, TRUE);
1032 g_object_unref (new_chan);
1035 static ContactListType
1036 contact_list_get_type (EmpathyContactList *list,
1039 EmpathyContactListPriv *priv;
1041 gchar **handle_name;
1042 ContactListType list_type;
1043 GError *error = NULL;
1045 priv = GET_PRIV (list);
1047 handles = g_array_new (FALSE, FALSE, sizeof (guint));
1048 g_array_append_val (handles, list_chan->handle);
1050 if (!tp_conn_inspect_handles (DBUS_G_PROXY (priv->tp_conn),
1051 TP_HANDLE_TYPE_LIST,
1055 gossip_debug (DEBUG_DOMAIN,
1056 "InspectHandle Error: %s",
1057 error ? error->message : "No error given");
1058 g_clear_error (&error);
1059 g_array_free (handles, TRUE);
1060 return CONTACT_LIST_TYPE_UNKNOWN;
1063 if (strcmp (*handle_name, "subscribe") == 0) {
1064 list_type = CONTACT_LIST_TYPE_SUBSCRIBE;
1065 } else if (strcmp (*handle_name, "publish") == 0) {
1066 list_type = CONTACT_LIST_TYPE_PUBLISH;
1067 } else if (strcmp (*handle_name, "known") == 0) {
1068 list_type = CONTACT_LIST_TYPE_KNOWN;
1070 list_type = CONTACT_LIST_TYPE_UNKNOWN;
1073 g_strfreev (handle_name);
1074 g_array_free (handles, TRUE);
1080 contact_list_contact_added_cb (GossipTelepathyGroup *group,
1084 const gchar *message,
1085 EmpathyContactList *list)
1087 EmpathyContactListPriv *priv;
1088 GList *added_list, *l;
1090 priv = GET_PRIV (list);
1092 added_list = empathy_contact_list_get_from_handles (list, handles);
1094 for (l = added_list; l; l = l->next) {
1095 GossipContact *contact;
1097 contact = GOSSIP_CONTACT (l->data);
1098 contact_list_block_contact (list, contact);
1099 gossip_contact_set_subscription (contact, GOSSIP_SUBSCRIPTION_BOTH);
1100 contact_list_unblock_contact (list, contact);
1102 g_signal_emit (list, signals[CONTACT_ADDED], 0, contact);
1104 g_object_unref (contact);
1107 g_list_free (added_list);
1111 contact_list_contact_removed_cb (GossipTelepathyGroup *group,
1115 const gchar *message,
1116 EmpathyContactList *list)
1118 EmpathyContactListPriv *priv;
1119 GList *removed_list, *l;
1121 priv = GET_PRIV (list);
1123 removed_list = empathy_contact_list_get_from_handles (list, handles);
1125 for (l = removed_list; l; l = l->next) {
1126 GossipContact *contact;
1129 contact = GOSSIP_CONTACT (l->data);
1131 handle = gossip_contact_get_handle (contact);
1132 g_hash_table_remove (priv->contacts, GUINT_TO_POINTER (handle));
1134 g_signal_emit (list, signals[CONTACT_REMOVED], 0, contact);
1136 g_object_unref (contact);
1139 g_list_free (removed_list);
1143 contact_list_local_pending_cb (GossipTelepathyGroup *group,
1147 const gchar *message,
1148 EmpathyContactList *list)
1150 EmpathyContactListPriv *priv;
1151 GList *pending_list, *l;
1153 priv = GET_PRIV (list);
1155 pending_list = empathy_contact_list_get_from_handles (list, handles);
1157 for (l = pending_list; l; l = l->next) {
1158 GossipContact *contact;
1160 contact = GOSSIP_CONTACT (l->data);
1162 /* FIXME: Is that the correct way ? */
1163 contact_list_block_contact (list, contact);
1164 gossip_contact_set_subscription (contact, GOSSIP_SUBSCRIPTION_FROM);
1165 contact_list_unblock_contact (list, contact);
1166 g_signal_emit (list, signals[CONTACT_ADDED], 0, contact);
1168 g_object_unref (contact);
1171 g_list_free (pending_list);
1175 contact_list_groups_updated_cb (GossipContact *contact,
1177 EmpathyContactList *list)
1179 EmpathyContactListPriv *priv;
1180 ContactListData data;
1183 priv = GET_PRIV (list);
1185 /* Make sure all groups are created */
1186 groups = gossip_contact_get_groups (contact);
1187 for (l = groups; l; l = l->next) {
1188 contact_list_get_group (list, l->data);
1191 data.handle = gossip_contact_get_handle (contact);
1192 data.new_groups = groups;
1194 g_hash_table_foreach (priv->groups,
1195 (GHFunc) contact_list_update_groups_foreach,
1200 contact_list_subscription_updated_cb (GossipContact *contact,
1202 EmpathyContactList *list)
1204 EmpathyContactListPriv *priv;
1205 GossipSubscription subscription;
1208 priv = GET_PRIV (list);
1210 subscription = gossip_contact_get_subscription (contact);
1211 handle = gossip_contact_get_handle (contact);
1213 /* FIXME: what to do here, I'm a bit lost... */
1215 gossip_telepathy_group_add_member (priv->publish, handle, "");
1217 gossip_telepathy_group_remove_member (priv->publish, handle, "");
1222 contact_list_name_updated_cb (GossipContact *contact,
1224 EmpathyContactList *list)
1226 EmpathyContactListPriv *priv;
1227 GHashTable *new_alias;
1228 const gchar *new_name;
1230 GError *error = NULL;
1232 priv = GET_PRIV (list);
1234 handle = gossip_contact_get_handle (contact);
1235 new_name = gossip_contact_get_name (contact);
1237 gossip_debug (DEBUG_DOMAIN, "renaming handle %d to %s",
1240 new_alias = g_hash_table_new_full (g_direct_hash,
1245 g_hash_table_insert (new_alias,
1246 GUINT_TO_POINTER (handle),
1247 g_strdup (new_name));
1249 if (!tp_conn_iface_aliasing_set_aliases (priv->aliasing_iface,
1252 gossip_debug (DEBUG_DOMAIN,
1253 "Couldn't rename contact: %s",
1254 error ? error->message : "No error given");
1255 g_clear_error (&error);
1258 g_hash_table_destroy (new_alias);
1262 contact_list_update_groups_foreach (gchar *object_path,
1263 GossipTelepathyGroup *group,
1264 ContactListData *data)
1267 gboolean found = FALSE;
1268 const gchar *group_name;
1271 is_member = gossip_telepathy_group_is_member (group, data->handle);
1272 group_name = gossip_telepathy_group_get_name (group);
1274 for (l = data->new_groups; l; l = l->next) {
1275 if (strcmp (group_name, l->data) == 0) {
1281 if (is_member && !found) {
1282 /* We are no longer member of this group */
1283 gossip_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1284 data->handle, group_name);
1285 gossip_telepathy_group_remove_member (group, data->handle, "");
1288 if (!is_member && found) {
1289 /* We are now member of this group */
1290 gossip_debug (DEBUG_DOMAIN, "Contact %d added to group '%s'",
1291 data->handle, group_name);
1292 gossip_telepathy_group_add_member (group, data->handle, "");
1296 static GossipTelepathyGroup *
1297 contact_list_get_group (EmpathyContactList *list,
1300 EmpathyContactListPriv *priv;
1301 GossipTelepathyGroup *group;
1302 TpChan *group_channel;
1305 char *group_object_path;
1306 const char *names[2] = {name, NULL};
1307 GError *error = NULL;
1309 priv = GET_PRIV (list);
1311 group = g_hash_table_find (priv->groups,
1312 (GHRFunc) contact_list_find_group,
1318 gossip_debug (DEBUG_DOMAIN, "creating new group: %s", name);
1320 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
1321 TP_HANDLE_TYPE_GROUP,
1325 gossip_debug (DEBUG_DOMAIN,
1326 "Couldn't request the creation of a new handle for group: %s",
1327 error ? error->message : "No error given");
1328 g_clear_error (&error);
1331 group_handle = g_array_index (handles, guint, 0);
1332 g_array_free (handles, TRUE);
1334 if (!tp_conn_request_channel (DBUS_G_PROXY (priv->tp_conn),
1335 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1336 TP_HANDLE_TYPE_GROUP,
1341 gossip_debug (DEBUG_DOMAIN,
1342 "Couldn't request the creation of a new group channel: %s",
1343 error ? error->message : "No error given");
1344 g_clear_error (&error);
1348 group_channel = tp_chan_new (tp_get_bus (),
1349 dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn)),
1351 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1352 TP_HANDLE_TYPE_GROUP,
1355 dbus_g_proxy_connect_signal (DBUS_G_PROXY (group_channel),
1358 (contact_list_group_channel_closed_cb),
1362 group = gossip_telepathy_group_new (group_channel, priv->tp_conn);
1363 g_hash_table_insert (priv->groups, group_object_path, group);
1364 g_signal_connect (group, "members-added",
1365 G_CALLBACK (contact_list_group_members_added_cb),
1367 g_signal_connect (group, "members-removed",
1368 G_CALLBACK (contact_list_group_members_removed_cb),
1375 contact_list_find_group (gchar *key,
1376 GossipTelepathyGroup *group,
1379 if (strcmp (group_name, gossip_telepathy_group_get_name (group)) == 0) {
1387 contact_list_get_groups_foreach (gchar *key,
1388 GossipTelepathyGroup *group,
1393 name = gossip_telepathy_group_get_name (group);
1394 *groups = g_list_append (*groups, g_strdup (name));
1398 contact_list_group_channel_closed_cb (TpChan *channel,
1399 EmpathyContactList *list)
1401 EmpathyContactListPriv *priv;
1403 priv = GET_PRIV (list);
1405 g_hash_table_remove (priv->groups,
1406 dbus_g_proxy_get_path (DBUS_G_PROXY (channel)));
1410 contact_list_group_members_added_cb (GossipTelepathyGroup *group,
1414 const gchar *message,
1415 EmpathyContactList *list)
1417 EmpathyContactListPriv *priv;
1418 GList *added_list, *l;
1419 const gchar *group_name;
1421 priv = GET_PRIV (list);
1423 group_name = gossip_telepathy_group_get_name (group);
1424 added_list = empathy_contact_list_get_from_handles (list, members);
1426 for (l = added_list; l; l = l->next) {
1427 GossipContact *contact;
1428 GList *contact_groups;
1430 contact = GOSSIP_CONTACT (l->data);
1431 contact_groups = gossip_contact_get_groups (contact);
1433 if (!g_list_find_custom (contact_groups,
1435 (GCompareFunc) strcmp)) {
1436 gossip_debug (DEBUG_DOMAIN, "Contact %s added to group '%s'",
1437 gossip_contact_get_name (contact),
1439 contact_groups = g_list_append (contact_groups,
1440 g_strdup (group_name));
1441 contact_list_block_contact (list, contact);
1442 gossip_contact_set_groups (contact, contact_groups);
1443 contact_list_unblock_contact (list, contact);
1446 g_object_unref (contact);
1449 g_list_free (added_list);
1453 contact_list_group_members_removed_cb (GossipTelepathyGroup *group,
1457 const gchar *message,
1458 EmpathyContactList *list)
1460 EmpathyContactListPriv *priv;
1461 GList *removed_list, *l;
1462 const gchar *group_name;
1464 priv = GET_PRIV (list);
1466 group_name = gossip_telepathy_group_get_name (group);
1467 removed_list = empathy_contact_list_get_from_handles (list, members);
1469 for (l = removed_list; l; l = l->next) {
1470 GossipContact *contact;
1471 GList *contact_groups;
1474 /* FIXME: Does it leak ? */
1475 contact = GOSSIP_CONTACT (l->data);
1476 contact_groups = gossip_contact_get_groups (contact);
1477 contact_groups = g_list_copy (contact_groups);
1479 to_remove = g_list_find_custom (contact_groups,
1481 (GCompareFunc) strcmp);
1483 gossip_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1484 gossip_contact_get_handle (contact),
1486 contact_groups = g_list_remove_link (contact_groups,
1488 contact_list_block_contact (list, contact);
1489 gossip_contact_set_groups (contact, contact_groups);
1490 contact_list_unblock_contact (list, contact);
1493 g_list_free (contact_groups);
1495 g_object_unref (contact);
1498 g_list_free (removed_list);
1502 contact_list_get_contacts_foreach (guint handle,
1503 GossipContact *contact,
1506 *contacts = g_list_append (*contacts, g_object_ref (contact));
1510 contact_list_get_info (EmpathyContactList *list,
1513 EmpathyContactListPriv *priv;
1514 GError *error = NULL;
1516 priv = GET_PRIV (list);
1518 if (priv->presence_iface) {
1519 /* FIXME: We should use GetPresence instead */
1520 if (!tp_conn_iface_presence_request_presence (priv->presence_iface,
1522 gossip_debug (DEBUG_DOMAIN,
1523 "Could not request presences: %s",
1524 error ? error->message : "No error given");
1525 g_clear_error (&error);
1529 if (priv->aliasing_iface) {
1530 ContactListAliasesRequestData *data;
1532 data = g_slice_new (ContactListAliasesRequestData);
1534 data->handles = g_memdup (handles->data, handles->len * sizeof (guint));
1536 tp_conn_iface_aliasing_request_aliases_async (priv->aliasing_iface,
1538 (tp_conn_iface_aliasing_request_aliases_reply)
1539 contact_list_request_aliases_cb,
1543 if (priv->avatars_iface) {
1546 for (i = 0; i < handles->len; i++) {
1549 handle = g_array_index (handles, gint, i);
1550 contact_list_request_avatar (list, handle);
1556 contact_list_request_avatar (EmpathyContactList *list,
1559 EmpathyContactListPriv *priv;
1561 priv = GET_PRIV (list);
1563 /* We queue avatar requests to not send too many dbus async
1564 * calls at once. If we don't we reach the dbus's limit of
1566 priv->avatar_requests_queue = g_list_append (priv->avatar_requests_queue,
1567 GUINT_TO_POINTER (handle));
1568 contact_list_start_avatar_requests (list);
1572 contact_list_start_avatar_requests (EmpathyContactList *list)
1574 EmpathyContactListPriv *priv;
1575 ContactListAvatarRequestData *data;
1577 priv = GET_PRIV (list);
1579 while (n_avatar_requests < MAX_AVATAR_REQUESTS &&
1580 priv->avatar_requests_queue) {
1581 data = g_slice_new (ContactListAvatarRequestData);
1583 data->handle = GPOINTER_TO_UINT (priv->avatar_requests_queue->data);
1585 n_avatar_requests++;
1586 priv->avatar_requests_queue = g_list_remove (priv->avatar_requests_queue,
1587 priv->avatar_requests_queue->data);
1589 tp_conn_iface_avatars_request_avatar_async (priv->avatars_iface,
1591 (tp_conn_iface_avatars_request_avatar_reply)
1592 contact_list_request_avatar_cb,
1598 contact_list_avatar_update_cb (DBusGProxy *proxy,
1601 EmpathyContactList *list)
1603 gossip_debug (DEBUG_DOMAIN, "Changing avatar for %d to %s",
1606 contact_list_request_avatar (list, handle);
1610 contact_list_request_avatar_cb (DBusGProxy *proxy,
1611 GArray *avatar_data,
1614 ContactListAvatarRequestData *data)
1616 GossipContact *contact;
1618 contact = empathy_contact_list_get_from_handle (data->list, data->handle);
1621 gossip_debug (DEBUG_DOMAIN, "Error requesting avatar for %s: %s",
1622 gossip_contact_get_name (contact),
1623 error ? error->message : "No error given");
1625 GossipAvatar *avatar;
1627 avatar = gossip_avatar_new (avatar_data->data,
1630 contact_list_block_contact (data->list, contact);
1631 gossip_contact_set_avatar (contact, avatar);
1632 contact_list_unblock_contact (data->list, contact);
1633 gossip_avatar_unref (avatar);
1636 n_avatar_requests--;
1637 contact_list_start_avatar_requests (data->list);
1639 g_slice_free (ContactListAvatarRequestData, data);
1643 contact_list_aliases_update_cb (DBusGProxy *proxy,
1644 GPtrArray *renamed_handlers,
1645 EmpathyContactList *list)
1649 for (i = 0; renamed_handlers->len > i; i++) {
1652 GValueArray *renamed_struct;
1653 GossipContact *contact;
1655 renamed_struct = g_ptr_array_index (renamed_handlers, i);
1656 handle = g_value_get_uint(g_value_array_get_nth (renamed_struct, 0));
1657 alias = g_value_get_string(g_value_array_get_nth (renamed_struct, 1));
1659 if (alias && *alias == '\0') {
1663 contact = empathy_contact_list_get_from_handle (list, handle);
1664 contact_list_block_contact (list, contact);
1665 gossip_contact_set_name (contact, alias);
1666 contact_list_unblock_contact (list, contact);
1668 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (update cb)",
1674 contact_list_request_aliases_cb (DBusGProxy *proxy,
1675 gchar **contact_names,
1677 ContactListAliasesRequestData *data)
1682 for (name = contact_names; *name && !error; name++) {
1683 GossipContact *contact;
1685 contact = empathy_contact_list_get_from_handle (data->list,
1687 contact_list_block_contact (data->list, contact);
1688 gossip_contact_set_name (contact, *name);
1689 contact_list_unblock_contact (data->list, contact);
1691 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (request cb)",
1692 data->handles[i], *name);
1697 g_free (data->handles);
1698 g_slice_free (ContactListAliasesRequestData, data);
1702 contact_list_presence_update_cb (DBusGProxy *proxy,
1703 GHashTable *handle_table,
1704 EmpathyContactList *list)
1706 g_hash_table_foreach (handle_table,
1707 (GHFunc) contact_list_parse_presence_foreach,
1712 contact_list_parse_presence_foreach (guint handle,
1713 GValueArray *presence_struct,
1714 EmpathyContactList *list)
1716 GHashTable *presences_table;
1717 GossipContact *contact;
1718 GossipPresence *presence = NULL;
1720 contact = empathy_contact_list_get_from_handle (list, handle);
1721 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
1723 g_hash_table_foreach (presences_table,
1724 (GHFunc) contact_list_presences_table_foreach,
1727 gossip_debug (DEBUG_DOMAIN, "Presence changed for %s (%d)",
1728 gossip_contact_get_name (contact),
1731 contact_list_block_contact (list, contact);
1733 gossip_contact_add_presence (contact, presence);
1734 g_object_unref (presence);
1736 g_object_set (contact, "presences", NULL, NULL);
1738 contact_list_unblock_contact (list, contact);
1742 contact_list_presences_table_foreach (const gchar *state_str,
1743 GHashTable *presences_table,
1744 GossipPresence **presence)
1746 GossipPresenceState state;
1747 const GValue *message;
1750 g_object_unref (*presence);
1754 state = contact_list_presence_state_from_str (state_str);
1755 if (state == GOSSIP_PRESENCE_STATE_UNAVAILABLE) {
1759 *presence = gossip_presence_new ();
1760 gossip_presence_set_state (*presence, state);
1762 message = g_hash_table_lookup (presences_table, "message");
1763 if (message != NULL) {
1764 gossip_presence_set_status (*presence,
1765 g_value_get_string (message));
1768 gossip_presence_set_resource (*presence, "");
1771 static GossipPresenceState
1772 contact_list_presence_state_from_str (const gchar *str)
1774 if (strcmp (str, "available") == 0) {
1775 return GOSSIP_PRESENCE_STATE_AVAILABLE;
1776 } else if ((strcmp (str, "dnd") == 0) || (strcmp (str, "busy") == 0)) {
1777 return GOSSIP_PRESENCE_STATE_BUSY;
1778 } else if ((strcmp (str, "away") == 0) || (strcmp (str, "brb") == 0)) {
1779 return GOSSIP_PRESENCE_STATE_AWAY;
1780 } else if (strcmp (str, "xa") == 0) {
1781 return GOSSIP_PRESENCE_STATE_EXT_AWAY;
1782 } else if (strcmp (str, "hidden") == 0) {
1783 return GOSSIP_PRESENCE_STATE_HIDDEN;
1784 } else if (strcmp (str, "offline") == 0) {
1785 return GOSSIP_PRESENCE_STATE_UNAVAILABLE;
1786 } else if (strcmp (str, "chat") == 0) {
1787 /* We don't support chat, so treat it like available. */
1788 return GOSSIP_PRESENCE_STATE_AVAILABLE;
1791 return GOSSIP_PRESENCE_STATE_AVAILABLE;