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-2009 Collabora Ltd.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <glib/gi18n-lib.h>
28 #include <telepathy-glib/channel.h>
29 #include <telepathy-glib/connection.h>
30 #include <telepathy-glib/util.h>
31 #include <telepathy-glib/dbus.h>
32 #include <telepathy-glib/interfaces.h>
34 #include <extensions/extensions.h>
36 #include "empathy-tp-contact-list.h"
37 #include "empathy-tp-contact-factory.h"
38 #include "empathy-contact-list.h"
39 #include "empathy-utils.h"
41 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
42 #include "empathy-debug.h"
44 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactList)
46 TpConnection *connection;
51 /* contact handle (TpHandle) => reffed (EmpathyContact *)
54 * - members of 'subscribe': we receive their presence
55 * - RP of 'subscribe': we asked to receive their presence
56 * - members of 'publish': we send them our presence
57 * - members of 'stored': they are in our roster
60 /* contact handle (TpHandle) => reffed (EmpathyContact *)
62 * Contacts which are local-pending in the publish channel but are NOT in
63 * the members hash table: they asked to receive our presence and we don't
64 * receive theirs or asked to.
65 * That's basically the contacts which asked to add us to their contact
66 * list and we didn't answer it. */
68 /* group name: borrowed (const gchar *) => reffed (TpChannel *) */
70 /* group name: owned (gchar *) => owned GArray of TpHandle */
71 GHashTable *add_to_group;
73 EmpathyContactListFlags flags;
74 } EmpathyTpContactListPriv;
77 TP_CONTACT_LIST_TYPE_PUBLISH,
78 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
79 TP_CONTACT_LIST_TYPE_UNKNOWN
82 static void tp_contact_list_iface_init (EmpathyContactListIface *iface);
89 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
90 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
91 tp_contact_list_iface_init));
94 tp_contact_list_forget_group (EmpathyTpContactList *list,
97 EmpathyTpContactListPriv *priv = GET_PRIV (list);
98 const TpIntSet *members;
100 const gchar *group_name;
102 group_name = tp_channel_get_identifier (channel);
104 /* Signal that all members are not in that group anymore */
105 members = tp_channel_group_get_members (channel);
106 tp_intset_iter_init (&iter, members);
107 while (tp_intset_iter_next (&iter)) {
108 EmpathyContact *contact;
110 contact = g_hash_table_lookup (priv->members,
111 GUINT_TO_POINTER (iter.element));
112 if (contact == NULL) {
116 DEBUG ("Contact %s (%d) removed from group %s",
117 empathy_contact_get_id (contact), iter.element,
119 g_signal_emit_by_name (list, "groups-changed", contact,
126 tp_contact_list_group_invalidated_cb (TpChannel *channel,
130 EmpathyTpContactList *list)
132 EmpathyTpContactListPriv *priv = GET_PRIV (list);
133 const gchar *group_name;
135 group_name = tp_channel_get_identifier (channel);
136 DEBUG ("Group %s invalidated. Message: %s", group_name, message);
138 tp_contact_list_forget_group (list, channel);
140 g_hash_table_remove (priv->groups, group_name);
144 contacts_added_to_group (EmpathyTpContactList *list,
148 EmpathyTpContactListPriv *priv = GET_PRIV (list);
149 const gchar *group_name;
152 group_name = tp_channel_get_identifier (channel);
154 for (i = 0; i < added->len; i++) {
155 EmpathyContact *contact;
158 handle = g_array_index (added, TpHandle, i);
159 contact = g_hash_table_lookup (priv->members,
160 GUINT_TO_POINTER (handle));
161 if (contact == NULL) {
165 DEBUG ("Contact %s (%d) added to group %s",
166 empathy_contact_get_id (contact), handle, group_name);
167 g_signal_emit_by_name (list, "groups-changed", contact,
174 tp_contact_list_group_members_changed_cb (TpChannel *channel,
178 GArray *local_pending,
179 GArray *remote_pending,
182 EmpathyTpContactList *list)
184 EmpathyTpContactListPriv *priv = GET_PRIV (list);
185 const gchar *group_name;
188 contacts_added_to_group (list, channel, added);
190 group_name = tp_channel_get_identifier (channel);
192 for (i = 0; i < removed->len; i++) {
193 EmpathyContact *contact;
196 handle = g_array_index (removed, TpHandle, i);
197 contact = g_hash_table_lookup (priv->members,
198 GUINT_TO_POINTER (handle));
199 if (contact == NULL) {
203 DEBUG ("Contact %s (%d) removed from group %s",
204 empathy_contact_get_id (contact), handle, group_name);
206 g_signal_emit_by_name (list, "groups-changed", contact,
213 tp_contact_list_group_ready_cb (TpChannel *channel,
217 EmpathyTpContactListPriv *priv = GET_PRIV (list);
218 TpChannel *old_group;
219 const gchar *group_name;
220 const TpIntSet *members;
224 DEBUG ("Error: %s", error->message);
225 g_object_unref (channel);
229 group_name = tp_channel_get_identifier (channel);
231 /* If there's already a group with this name in the table, we can't
232 * just let it be replaced. Replacing it causes it to be unreffed,
233 * which causes it to be invalidated (see
234 * <https://bugs.freedesktop.org/show_bug.cgi?id=22119>), which causes
235 * it to be removed from the hash table again, which causes it to be
238 old_group = g_hash_table_lookup (priv->groups, group_name);
240 if (old_group != NULL) {
241 DEBUG ("Discarding old group %s (%p)", group_name, old_group);
242 g_hash_table_steal (priv->groups, group_name);
243 tp_contact_list_forget_group (list, old_group);
244 g_object_unref (old_group);
247 /* Pass the reference on the TpChannel to priv->groups */
248 g_hash_table_insert (priv->groups, (gpointer) group_name, channel);
249 DEBUG ("Group %s added", group_name);
251 g_signal_connect (channel, "group-members-changed",
252 G_CALLBACK (tp_contact_list_group_members_changed_cb),
255 g_signal_connect (channel, "invalidated",
256 G_CALLBACK (tp_contact_list_group_invalidated_cb),
259 if (priv->add_to_group) {
262 handles = g_hash_table_lookup (priv->add_to_group, group_name);
264 DEBUG ("Adding initial members to group %s", group_name);
265 tp_cli_channel_interface_group_call_add_members (channel,
266 -1, handles, NULL, NULL, NULL, NULL, NULL);
267 g_hash_table_remove (priv->add_to_group, group_name);
271 /* Get initial members of the group */
272 members = tp_channel_group_get_members (channel);
273 g_assert (members != NULL);
274 arr = tp_intset_to_array (members);
275 contacts_added_to_group (list, channel, arr);
276 g_array_free (arr, TRUE);
280 tp_contact_list_group_add_channel (EmpathyTpContactList *list,
281 const gchar *object_path,
282 GHashTable *properties)
284 EmpathyTpContactListPriv *priv = GET_PRIV (list);
286 GError *error = NULL;
288 channel = tp_channel_new_from_properties (priv->connection,
289 object_path, properties, &error);
290 if (channel == NULL) {
291 DEBUG ("Failed to create group channel: %s", error->message);
292 g_error_free (error);
296 /* Give the ref to the callback */
297 tp_channel_call_when_ready (channel,
298 tp_contact_list_group_ready_cb,
303 tp_contact_list_group_request_channel_cb (TpConnection *connection,
304 const gchar *object_path,
309 /* The new channel will be handled in NewChannel cb. Here we only
310 * handle the error if RequestChannel failed */
312 DEBUG ("Error: %s", error->message);
318 tp_contact_list_group_request_handles_cb (TpConnection *connection,
319 const GArray *handles,
324 TpHandle channel_handle;
327 DEBUG ("Error: %s", error->message);
331 channel_handle = g_array_index (handles, TpHandle, 0);
332 tp_cli_connection_call_request_channel (connection, -1,
333 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
334 TP_HANDLE_TYPE_GROUP,
337 tp_contact_list_group_request_channel_cb,
342 /* This function takes ownership of handles array */
344 tp_contact_list_group_add (EmpathyTpContactList *list,
345 const gchar *group_name,
348 EmpathyTpContactListPriv *priv = GET_PRIV (list);
350 const gchar *names[] = {group_name, NULL};
352 /* Search the channel for that group name */
353 channel = g_hash_table_lookup (priv->groups, group_name);
355 tp_cli_channel_interface_group_call_add_members (channel, -1,
356 handles, NULL, NULL, NULL, NULL, NULL);
357 g_array_free (handles, TRUE);
361 /* That group does not exist yet, we have to:
362 * 1) Request an handle for the group name
363 * 2) Request a channel
364 * 3) When NewChannel is emitted, add handles in members
366 g_hash_table_insert (priv->add_to_group,
367 g_strdup (group_name),
369 tp_cli_connection_call_request_handles (priv->connection, -1,
370 TP_HANDLE_TYPE_GROUP, names,
371 tp_contact_list_group_request_handles_cb,
377 got_added_members_cb (TpConnection *connection,
379 EmpathyContact * const * contacts,
381 const TpHandle *failed,
386 EmpathyTpContactListPriv *priv = GET_PRIV (list);
390 DEBUG ("Error: %s", error->message);
394 for (i = 0; i < n_contacts; i++) {
395 EmpathyContact *contact = contacts[i];
396 TpHandle handle = empathy_contact_get_handle (contact);
398 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
401 /* Add to the list and emit signal */
402 g_hash_table_insert (priv->members, GUINT_TO_POINTER (handle),
403 g_object_ref (contact));
404 g_signal_emit_by_name (list, "members-changed", contact,
410 add_to_members (EmpathyTpContactList *list,
413 EmpathyTpContactListPriv *priv = GET_PRIV (list);
417 if (handles->len == 0)
420 request = g_array_new (FALSE, FALSE, sizeof (TpHandle));
422 for (i = 0; i < handles->len; i++) {
423 TpHandle handle = g_array_index (handles, TpHandle, i);
425 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
428 g_array_append_val (request, handle);
431 if (request->len > 0) {
432 empathy_tp_contact_factory_get_from_handles (priv->connection,
433 request->len, (TpHandle *) request->data,
434 got_added_members_cb, NULL, NULL, G_OBJECT (list));
437 g_array_free (request, TRUE);
441 tp_contact_list_got_local_pending_cb (TpConnection *connection,
443 EmpathyContact * const * contacts,
445 const TpHandle *failed,
450 EmpathyTpContactListPriv *priv = GET_PRIV (list);
454 DEBUG ("Error: %s", error->message);
458 for (i = 0; i < n_contacts; i++) {
459 EmpathyContact *contact = contacts[i];
461 const gchar *message;
462 TpChannelGroupChangeReason reason;
463 const TpIntSet *members, *remote_pending;
465 handle = empathy_contact_get_handle (contact);
466 members = tp_channel_group_get_members (priv->subscribe);
467 remote_pending = tp_channel_group_get_remote_pending (priv->subscribe);
469 if (tp_intset_is_member (members, handle) ||
470 tp_intset_is_member (remote_pending, handle)) {
471 GArray handles = {(gchar *) &handle, 1};
473 /* This contact is already subscribed, or user requested
474 * to subscribe, auto accept. */
475 tp_cli_channel_interface_group_call_add_members (priv->publish,
476 -1, &handles, NULL, NULL, NULL, NULL, NULL);
478 else if (tp_channel_group_get_local_pending_info (priv->publish,
483 /* Add contact to pendings */
484 g_hash_table_insert (priv->pendings, GUINT_TO_POINTER (handle),
485 g_object_ref (contact));
486 g_signal_emit_by_name (list, "pendings-changed", contact,
487 contact, reason, message, TRUE);
493 tp_contact_list_remove_handle (EmpathyTpContactList *list,
497 EmpathyTpContactListPriv *priv = GET_PRIV (list);
498 EmpathyContact *contact;
501 if (table == priv->pendings)
502 sig = "pendings-changed";
503 else if (table == priv->members)
504 sig = "members-changed";
508 contact = g_hash_table_lookup (table, GUINT_TO_POINTER (handle));
510 g_object_ref (contact);
511 g_hash_table_remove (table, GUINT_TO_POINTER (handle));
512 g_signal_emit_by_name (list, sig, contact, 0, 0, NULL,
514 g_object_unref (contact);
519 remove_from_member_if_needed (EmpathyTpContactList *list,
522 /* remove contact from members if it's not in publish and subscribe */
523 EmpathyTpContactListPriv *priv = GET_PRIV (list);
524 const TpIntSet *members;
526 members = tp_channel_group_get_members (priv->subscribe);
527 if (tp_intset_is_member (members, handle))
530 members = tp_channel_group_get_remote_pending (priv->subscribe);
531 if (tp_intset_is_member (members, handle))
534 members = tp_channel_group_get_members (priv->publish);
535 if (tp_intset_is_member (members, handle))
538 tp_contact_list_remove_handle (list, priv->members, handle);
542 tp_contact_list_publish_group_members_changed_cb (TpChannel *channel,
546 GArray *local_pending,
547 GArray *remote_pending,
549 TpChannelGroupChangeReason reason,
550 EmpathyTpContactList *list)
552 EmpathyTpContactListPriv *priv = GET_PRIV (list);
555 /* We now send our presence to those contacts, remove them from pendings */
556 add_to_members (list, added);
557 for (i = 0; i < added->len; i++) {
558 tp_contact_list_remove_handle (list, priv->pendings,
559 g_array_index (added, TpHandle, i));
562 /* We refuse to send our presence to those contacts, remove from pendings */
563 for (i = 0; i < removed->len; i++) {
564 TpHandle handle = g_array_index (removed, TpHandle, i);
566 tp_contact_list_remove_handle (list, priv->pendings, handle);
567 remove_from_member_if_needed (list, handle);
570 /* Those contacts want our presence, auto accept those that are already
571 * member, otherwise add in pendings. */
572 if (local_pending->len > 0) {
573 empathy_tp_contact_factory_get_from_handles (priv->connection,
574 local_pending->len, (TpHandle *) local_pending->data,
575 tp_contact_list_got_local_pending_cb, NULL, NULL,
581 tp_contact_list_get_alias_flags_cb (TpConnection *connection,
587 EmpathyTpContactListPriv *priv = GET_PRIV (list);
590 DEBUG ("Error: %s", error->message);
594 if (flags & TP_CONNECTION_ALIAS_FLAG_USER_SET) {
595 priv->flags |= EMPATHY_CONTACT_LIST_CAN_ALIAS;
600 tp_contact_list_get_requestablechannelclasses_cb (TpProxy *connection,
606 EmpathyTpContactListPriv *priv = GET_PRIV (list);
611 DEBUG ("Error: %s", error->message);
615 classes = g_value_get_boxed (value);
616 for (i = 0; i < classes->len; i++) {
617 GValueArray *class = g_ptr_array_index (classes, i);
619 const char *channel_type;
622 props = g_value_get_boxed (g_value_array_get_nth (class, 0));
624 channel_type = tp_asv_get_string (props,
625 TP_IFACE_CHANNEL ".ChannelType");
626 handle_type = tp_asv_get_uint32 (props,
627 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
629 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) &&
630 handle_type == TP_HANDLE_TYPE_GROUP) {
631 DEBUG ("Got channel class for a contact group");
632 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
639 tp_contact_list_subscribe_group_members_changed_cb (TpChannel *channel,
643 GArray *local_pending,
644 GArray *remote_pending,
647 EmpathyTpContactList *list)
649 EmpathyTpContactListPriv *priv = GET_PRIV (list);
653 /* We now get the presence of those contacts, add them to members */
654 add_to_members (list, added);
656 /* Those contacts refuse to send us their presence, remove from members. */
657 for (i = 0; i < removed->len; i++) {
658 remove_from_member_if_needed (list, g_array_index (removed, TpHandle, i));
661 /* We want those contacts in our contact list but we don't get their
662 * presence yet. Add to members anyway. */
663 add_to_members (list, remote_pending);
665 /* Implicitly accept pending request of contacts which are now members. */
666 if (priv->publish == NULL)
669 accept = g_array_new (FALSE, FALSE, sizeof (TpHandle));
670 for (i = 0; i < added->len; i++) {
671 TpHandle handle = g_array_index (added, TpHandle, i);
673 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle)) != NULL)
674 g_array_append_val (accept, handle);
677 for (i = 0; i < remote_pending->len; i++) {
678 TpHandle handle = g_array_index (added, TpHandle, i);
680 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle)) != NULL)
681 g_array_append_val (accept, handle);
684 tp_cli_channel_interface_group_call_add_members (priv->publish,
685 -1, accept, NULL, NULL, NULL, NULL, NULL);
687 g_array_free (accept, TRUE);
691 tp_contact_list_store_group_members_changed_cb (TpChannel *channel,
695 GArray *local_pending,
696 GArray *remote_pending,
699 EmpathyTpContactList *list)
703 add_to_members (list, added);
705 for (i = 0; i < removed->len; i++) {
706 remove_from_member_if_needed (list, g_array_index (removed, TpHandle, i));
711 tp_contact_list_finalize (GObject *object)
713 EmpathyTpContactListPriv *priv;
714 EmpathyTpContactList *list;
718 list = EMPATHY_TP_CONTACT_LIST (object);
719 priv = GET_PRIV (list);
721 DEBUG ("finalize: %p", object);
723 if (priv->subscribe) {
724 g_object_unref (priv->subscribe);
727 g_object_unref (priv->publish);
730 g_object_unref (priv->stored);
733 if (priv->connection) {
734 g_object_unref (priv->connection);
737 g_hash_table_iter_init (&iter, priv->groups);
738 while (g_hash_table_iter_next (&iter, NULL, &channel)) {
739 g_signal_handlers_disconnect_by_func (channel,
740 tp_contact_list_group_invalidated_cb, list);
743 g_hash_table_destroy (priv->groups);
744 g_hash_table_destroy (priv->members);
745 g_hash_table_destroy (priv->pendings);
746 g_hash_table_destroy (priv->add_to_group);
748 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
752 got_list_channel (EmpathyTpContactList *list,
755 EmpathyTpContactListPriv *priv = GET_PRIV (list);
758 /* We requested that channel by providing TargetID property, so it's
759 * guaranteed that tp_channel_get_identifier will return it. */
760 id = tp_channel_get_identifier (channel);
762 /* TpChannel emits initial set of members just before being ready */
763 if (!tp_strdiff (id, "stored")) {
764 if (priv->stored != NULL)
766 priv->stored = g_object_ref (channel);
767 g_signal_connect (priv->stored, "group-members-changed",
768 G_CALLBACK (tp_contact_list_store_group_members_changed_cb),
770 } else if (!tp_strdiff (id, "publish")) {
771 if (priv->publish != NULL)
773 priv->publish = g_object_ref (channel);
774 g_signal_connect (priv->publish, "group-members-changed",
775 G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
777 } else if (!tp_strdiff (id, "subscribe")) {
778 if (priv->subscribe != NULL)
780 priv->subscribe = g_object_ref (channel);
781 g_signal_connect (priv->subscribe, "group-members-changed",
782 G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
788 list_ensure_channel_cb (TpConnection *conn,
791 GHashTable *properties,
794 GObject *weak_object)
796 EmpathyTpContactList *list = user_data;
800 DEBUG ("failed: %s\n", error->message);
804 channel = tp_channel_new_from_properties (conn, path, properties, NULL);
805 got_list_channel (list, channel);
806 g_object_unref (channel);
810 iterate_on_channels (EmpathyTpContactList *list,
811 const GPtrArray *channels)
815 for (i = 0; i < channels->len ; i++) {
816 GValueArray *arr = g_ptr_array_index (channels, i);
818 GHashTable *properties;
819 TpHandleType handle_type;
821 path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
822 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
824 if (tp_strdiff (tp_asv_get_string (properties,
825 TP_IFACE_CHANNEL ".ChannelType"),
826 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST))
829 if (tp_asv_get_string (properties, TP_IFACE_CHANNEL ".TargetID") == NULL)
832 handle_type = tp_asv_get_uint32 (properties,
833 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
835 if (handle_type != TP_HANDLE_TYPE_GROUP)
838 tp_contact_list_group_add_channel (list, path, properties);
843 new_channels_cb (TpConnection *conn,
844 const GPtrArray *channels,
846 GObject *weak_object)
848 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
850 iterate_on_channels (list, channels);
854 got_channels_cb (TpProxy *conn,
858 GObject *weak_object)
860 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
861 const GPtrArray *channels;
864 DEBUG ("Get Channels property failed: %s", error->message);
868 channels = g_value_get_boxed (out);
869 iterate_on_channels (list, channels);
873 conn_ready_cb (TpConnection *connection,
877 EmpathyTpContactList *list = data;
878 EmpathyTpContactListPriv *priv = GET_PRIV (list);
882 DEBUG ("failed: %s", error->message);
886 /* Look for existing group channels */
887 tp_cli_dbus_properties_call_get (connection, -1,
888 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels", got_channels_cb,
889 NULL, NULL, G_OBJECT (list));
891 request = tp_asv_new (
892 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
893 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_LIST,
896 /* Watch the NewChannels signal so if ensuring list channels fails (for
897 * example because the server is slow and the D-Bus call timeouts before CM
898 * fetches the roster), we have a chance to get them later. */
899 tp_cli_connection_interface_requests_connect_to_new_channels (
900 priv->connection, new_channels_cb, NULL, NULL, G_OBJECT (list), NULL);
902 /* Request the 'stored' list. */
903 tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "stored");
904 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
905 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
907 /* Request the 'publish' list. */
908 tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "publish");
909 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
910 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
912 /* Request the 'subscribe' list. */
913 tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "subscribe");
914 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
915 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
917 g_hash_table_unref (request);
920 g_object_unref (list);
924 tp_contact_list_constructed (GObject *list)
926 EmpathyTpContactListPriv *priv = GET_PRIV (list);
928 /* call GetAliasFlags */
929 if (tp_proxy_has_interface_by_id (priv->connection,
930 TP_IFACE_QUARK_CONNECTION_INTERFACE_ALIASING)) {
931 tp_cli_connection_interface_aliasing_call_get_alias_flags (
934 tp_contact_list_get_alias_flags_cb,
939 /* lookup RequestableChannelClasses */
940 if (tp_proxy_has_interface_by_id (priv->connection,
941 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS)) {
942 tp_cli_dbus_properties_call_get (priv->connection,
944 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
945 "RequestableChannelClasses",
946 tp_contact_list_get_requestablechannelclasses_cb,
950 /* we just don't know... better mark the flag just in case */
951 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
954 tp_connection_call_when_ready (priv->connection, conn_ready_cb,
955 g_object_ref (list));
959 tp_contact_list_get_property (GObject *object,
964 EmpathyTpContactListPriv *priv = GET_PRIV (object);
967 case PROP_CONNECTION:
968 g_value_set_object (value, priv->connection);
971 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
977 tp_contact_list_set_property (GObject *object,
982 EmpathyTpContactListPriv *priv = GET_PRIV (object);
985 case PROP_CONNECTION:
986 priv->connection = g_value_dup_object (value);
989 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
995 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
997 GObjectClass *object_class = G_OBJECT_CLASS (klass);
999 object_class->finalize = tp_contact_list_finalize;
1000 object_class->constructed = tp_contact_list_constructed;
1001 object_class->get_property = tp_contact_list_get_property;
1002 object_class->set_property = tp_contact_list_set_property;
1004 g_object_class_install_property (object_class,
1006 g_param_spec_object ("connection",
1008 "The connection associated with the contact list",
1011 G_PARAM_CONSTRUCT_ONLY));
1013 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
1017 tp_contact_list_array_free (gpointer handles)
1019 g_array_free (handles, TRUE);
1023 empathy_tp_contact_list_init (EmpathyTpContactList *list)
1025 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
1026 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
1030 /* Map group's name to group's TpChannel. The group name string is owned
1031 * by the TpChannel object */
1032 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1034 (GDestroyNotify) g_object_unref);
1036 /* Map contact's handle to EmpathyContact object */
1037 priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1039 (GDestroyNotify) g_object_unref);
1041 /* Map contact's handle to EmpathyContact object */
1042 priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1044 (GDestroyNotify) g_object_unref);
1046 /* Map group's name to GArray of handle */
1047 priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
1049 tp_contact_list_array_free);
1052 EmpathyTpContactList *
1053 empathy_tp_contact_list_new (TpConnection *connection)
1055 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
1056 "connection", connection,
1061 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
1063 EmpathyTpContactListPriv *priv;
1065 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
1067 priv = GET_PRIV (list);
1069 return priv->connection;
1073 tp_contact_list_add (EmpathyContactList *list,
1074 EmpathyContact *contact,
1075 const gchar *message)
1077 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1079 GArray handles = {(gchar *) &handle, 1};
1081 handle = empathy_contact_get_handle (contact);
1083 if (priv->subscribe) {
1084 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
1085 -1, &handles, message, NULL, NULL, NULL, NULL);
1088 if (priv->publish) {
1089 TpChannelGroupFlags flags = tp_channel_group_get_flags (priv->subscribe);
1090 if (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD ||
1091 g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
1092 tp_cli_channel_interface_group_call_add_members (priv->publish,
1093 -1, &handles, message, NULL, NULL, NULL, NULL);
1097 /* We want to unblock the contact */
1098 if (tp_proxy_has_interface_by_id (priv->connection,
1099 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING)) {
1100 TpContact *tp_contact = empathy_contact_get_tp_contact (contact);
1102 if (tp_contact != NULL)
1103 tp_contact_unblock_async (tp_contact, NULL, NULL);
1108 tp_contact_list_remove (EmpathyContactList *list,
1109 EmpathyContact *contact,
1110 const gchar *message)
1112 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1114 GArray handles = {(gchar *) &handle, 1};
1116 handle = empathy_contact_get_handle (contact);
1118 /* FIXME: this is racy if tp_contact_list_remove is called before the
1119 * 'stored' list has been retrieved. */
1120 if (priv->stored != NULL) {
1121 tp_cli_channel_interface_group_call_remove_members (priv->stored,
1122 -1, &handles, message, NULL, NULL, NULL, NULL);
1125 if (priv->subscribe) {
1126 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
1127 -1, &handles, message, NULL, NULL, NULL, NULL);
1129 if (priv->publish) {
1130 tp_cli_channel_interface_group_call_remove_members (priv->publish,
1131 -1, &handles, message, NULL, NULL, NULL, NULL);
1136 tp_contact_list_get_members (EmpathyContactList *list)
1138 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1141 ret = g_hash_table_get_values (priv->members);
1142 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1147 tp_contact_list_get_pendings (EmpathyContactList *list)
1149 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1152 ret = g_hash_table_get_values (priv->pendings);
1153 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1158 tp_contact_list_get_all_groups (EmpathyContactList *list)
1160 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1163 ret = g_hash_table_get_keys (priv->groups);
1164 for (l = ret; l; l = l->next) {
1165 l->data = g_strdup (l->data);
1172 tp_contact_list_get_groups (EmpathyContactList *list,
1173 EmpathyContact *contact)
1175 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1177 GHashTableIter iter;
1178 gpointer group_name;
1182 handle = empathy_contact_get_handle (contact);
1183 g_hash_table_iter_init (&iter, priv->groups);
1184 while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
1185 const TpIntSet *members;
1187 members = tp_channel_group_get_members (channel);
1188 if (tp_intset_is_member (members, handle)) {
1189 ret = g_list_prepend (ret, g_strdup (group_name));
1197 tp_contact_list_add_to_group (EmpathyContactList *list,
1198 EmpathyContact *contact,
1199 const gchar *group_name)
1204 handle = empathy_contact_get_handle (contact);
1205 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1206 g_array_append_val (handles, handle);
1207 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1208 group_name, handles);
1212 tp_contact_list_remove_from_group (EmpathyContactList *list,
1213 EmpathyContact *contact,
1214 const gchar *group_name)
1216 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1219 GArray handles = {(gchar *) &handle, 1};
1221 channel = g_hash_table_lookup (priv->groups, group_name);
1222 if (channel == NULL) {
1226 handle = empathy_contact_get_handle (contact);
1227 DEBUG ("remove contact %s (%d) from group %s",
1228 empathy_contact_get_id (contact), handle, group_name);
1230 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1231 &handles, NULL, NULL, NULL, NULL, NULL);
1235 tp_contact_list_rename_group (EmpathyContactList *list,
1236 const gchar *old_group_name,
1237 const gchar *new_group_name)
1239 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1241 const TpIntSet *members;
1244 channel = g_hash_table_lookup (priv->groups, old_group_name);
1245 if (channel == NULL) {
1249 DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1251 /* Remove all members and close the old channel */
1252 members = tp_channel_group_get_members (channel);
1253 handles = tp_intset_to_array (members);
1254 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1255 handles, NULL, NULL, NULL, NULL, NULL);
1256 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1258 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1259 new_group_name, handles);
1263 tp_contact_list_remove_group (EmpathyContactList *list,
1264 const gchar *group_name)
1266 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1268 const TpIntSet *members;
1271 channel = g_hash_table_lookup (priv->groups, group_name);
1272 if (channel == NULL) {
1276 DEBUG ("remove group %s", group_name);
1278 /* Remove all members and close the channel */
1279 members = tp_channel_group_get_members (channel);
1280 handles = tp_intset_to_array (members);
1281 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1282 handles, NULL, NULL, NULL, NULL, NULL);
1283 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1284 g_array_free (handles, TRUE);
1287 static EmpathyContactListFlags
1288 tp_contact_list_get_flags (EmpathyContactList *list)
1290 EmpathyTpContactListPriv *priv;
1291 EmpathyContactListFlags flags;
1293 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1295 priv = GET_PRIV (list);
1296 flags = priv->flags;
1298 if (priv->subscribe != NULL) {
1299 TpChannelGroupFlags group_flags;
1301 group_flags = tp_channel_group_get_flags (priv->subscribe);
1303 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1304 flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1307 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1308 flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1311 if (group_flags & TP_CHANNEL_GROUP_FLAG_MESSAGE_ADD) {
1312 flags |= EMPATHY_CONTACT_LIST_MESSAGE_ADD;
1320 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1322 iface->add = tp_contact_list_add;
1323 iface->remove = tp_contact_list_remove;
1324 iface->get_members = tp_contact_list_get_members;
1325 iface->get_pendings = tp_contact_list_get_pendings;
1326 iface->get_all_groups = tp_contact_list_get_all_groups;
1327 iface->get_groups = tp_contact_list_get_groups;
1328 iface->add_to_group = tp_contact_list_add_to_group;
1329 iface->remove_from_group = tp_contact_list_remove_from_group;
1330 iface->rename_group = tp_contact_list_rename_group;
1331 iface->remove_group = tp_contact_list_remove_group;
1332 iface->get_flags = tp_contact_list_get_flags;
1336 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1338 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1339 GHashTableIter iter;
1342 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1344 /* Remove all contacts */
1345 g_hash_table_iter_init (&iter, priv->members);
1346 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1347 g_signal_emit_by_name (list, "members-changed", contact,
1351 g_hash_table_remove_all (priv->members);
1353 g_hash_table_iter_init (&iter, priv->pendings);
1354 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1355 g_signal_emit_by_name (list, "pendings-changed", contact,
1359 g_hash_table_remove_all (priv->pendings);