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;
52 /* contact handle (TpHandle) => reffed (EmpathyContact *)
55 * - members of 'subscribe': we receive their presence
56 * - RP of 'subscribe': we asked to receive their presence
57 * - members of 'publish': we send them our presence
58 * - members of 'stored': they are in our roster
61 /* contact handle (TpHandle) => reffed (EmpathyContact *)
63 * Contacts which are local-pending in the publish channel but are NOT in
64 * the members hash table: they asked to receive our presence and we don't
65 * receive theirs or asked to.
66 * That's basically the contacts which asked to add us to their contact
67 * list and we didn't answer it. */
69 /* group name: borrowed (const gchar *) => reffed (TpChannel *) */
71 /* group name: owned (gchar *) => owned GArray of TpHandle */
72 GHashTable *add_to_group;
74 EmpathyContactListFlags flags;
75 } EmpathyTpContactListPriv;
78 TP_CONTACT_LIST_TYPE_PUBLISH,
79 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
80 TP_CONTACT_LIST_TYPE_UNKNOWN
83 static void tp_contact_list_iface_init (EmpathyContactListIface *iface);
90 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
91 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
92 tp_contact_list_iface_init));
95 tp_contact_list_forget_group (EmpathyTpContactList *list,
98 EmpathyTpContactListPriv *priv = GET_PRIV (list);
99 const TpIntSet *members;
101 const gchar *group_name;
103 group_name = tp_channel_get_identifier (channel);
105 /* Signal that all members are not in that group anymore */
106 members = tp_channel_group_get_members (channel);
107 tp_intset_iter_init (&iter, members);
108 while (tp_intset_iter_next (&iter)) {
109 EmpathyContact *contact;
111 contact = g_hash_table_lookup (priv->members,
112 GUINT_TO_POINTER (iter.element));
113 if (contact == NULL) {
117 DEBUG ("Contact %s (%d) removed from group %s",
118 empathy_contact_get_id (contact), iter.element,
120 g_signal_emit_by_name (list, "groups-changed", contact,
127 tp_contact_list_group_invalidated_cb (TpChannel *channel,
131 EmpathyTpContactList *list)
133 EmpathyTpContactListPriv *priv = GET_PRIV (list);
134 const gchar *group_name;
136 group_name = tp_channel_get_identifier (channel);
137 DEBUG ("Group %s invalidated. Message: %s", group_name, message);
139 tp_contact_list_forget_group (list, channel);
141 g_hash_table_remove (priv->groups, group_name);
145 contacts_added_to_group (EmpathyTpContactList *list,
149 EmpathyTpContactListPriv *priv = GET_PRIV (list);
150 const gchar *group_name;
153 group_name = tp_channel_get_identifier (channel);
155 for (i = 0; i < added->len; i++) {
156 EmpathyContact *contact;
159 handle = g_array_index (added, TpHandle, i);
160 contact = g_hash_table_lookup (priv->members,
161 GUINT_TO_POINTER (handle));
162 if (contact == NULL) {
166 DEBUG ("Contact %s (%d) added to group %s",
167 empathy_contact_get_id (contact), handle, group_name);
168 g_signal_emit_by_name (list, "groups-changed", contact,
175 tp_contact_list_group_members_changed_cb (TpChannel *channel,
179 GArray *local_pending,
180 GArray *remote_pending,
183 EmpathyTpContactList *list)
185 EmpathyTpContactListPriv *priv = GET_PRIV (list);
186 const gchar *group_name;
189 contacts_added_to_group (list, channel, added);
191 group_name = tp_channel_get_identifier (channel);
193 for (i = 0; i < removed->len; i++) {
194 EmpathyContact *contact;
197 handle = g_array_index (removed, TpHandle, i);
198 contact = g_hash_table_lookup (priv->members,
199 GUINT_TO_POINTER (handle));
200 if (contact == NULL) {
204 DEBUG ("Contact %s (%d) removed from group %s",
205 empathy_contact_get_id (contact), handle, group_name);
207 g_signal_emit_by_name (list, "groups-changed", contact,
214 tp_contact_list_group_ready_cb (TpChannel *channel,
218 EmpathyTpContactListPriv *priv = GET_PRIV (list);
219 TpChannel *old_group;
220 const gchar *group_name;
221 const TpIntSet *members;
225 DEBUG ("Error: %s", error->message);
226 g_object_unref (channel);
230 group_name = tp_channel_get_identifier (channel);
232 /* If there's already a group with this name in the table, we can't
233 * just let it be replaced. Replacing it causes it to be unreffed,
234 * which causes it to be invalidated (see
235 * <https://bugs.freedesktop.org/show_bug.cgi?id=22119>), which causes
236 * it to be removed from the hash table again, which causes it to be
239 old_group = g_hash_table_lookup (priv->groups, group_name);
241 if (old_group != NULL) {
242 DEBUG ("Discarding old group %s (%p)", group_name, old_group);
243 g_hash_table_steal (priv->groups, group_name);
244 tp_contact_list_forget_group (list, old_group);
245 g_object_unref (old_group);
248 /* Pass the reference on the TpChannel to priv->groups */
249 g_hash_table_insert (priv->groups, (gpointer) group_name, channel);
250 DEBUG ("Group %s added", group_name);
252 g_signal_connect (channel, "group-members-changed",
253 G_CALLBACK (tp_contact_list_group_members_changed_cb),
256 g_signal_connect (channel, "invalidated",
257 G_CALLBACK (tp_contact_list_group_invalidated_cb),
260 if (priv->add_to_group) {
263 handles = g_hash_table_lookup (priv->add_to_group, group_name);
265 DEBUG ("Adding initial members to group %s", group_name);
266 tp_cli_channel_interface_group_call_add_members (channel,
267 -1, handles, NULL, NULL, NULL, NULL, NULL);
268 g_hash_table_remove (priv->add_to_group, group_name);
272 /* Get initial members of the group */
273 members = tp_channel_group_get_members (channel);
274 g_assert (members != NULL);
275 arr = tp_intset_to_array (members);
276 contacts_added_to_group (list, channel, arr);
277 g_array_free (arr, TRUE);
281 tp_contact_list_group_add_channel (EmpathyTpContactList *list,
282 const gchar *object_path,
283 GHashTable *properties)
285 EmpathyTpContactListPriv *priv = GET_PRIV (list);
287 GError *error = NULL;
289 channel = tp_channel_new_from_properties (priv->connection,
290 object_path, properties, &error);
291 if (channel == NULL) {
292 DEBUG ("Failed to create group channel: %s", error->message);
293 g_error_free (error);
297 /* Give the ref to the callback */
298 tp_channel_call_when_ready (channel,
299 tp_contact_list_group_ready_cb,
304 tp_contact_list_group_request_channel_cb (TpConnection *connection,
305 const gchar *object_path,
310 /* The new channel will be handled in NewChannel cb. Here we only
311 * handle the error if RequestChannel failed */
313 DEBUG ("Error: %s", error->message);
319 tp_contact_list_group_request_handles_cb (TpConnection *connection,
320 const GArray *handles,
325 TpHandle channel_handle;
328 DEBUG ("Error: %s", error->message);
332 channel_handle = g_array_index (handles, TpHandle, 0);
333 tp_cli_connection_call_request_channel (connection, -1,
334 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
335 TP_HANDLE_TYPE_GROUP,
338 tp_contact_list_group_request_channel_cb,
343 /* This function takes ownership of handles array */
345 tp_contact_list_group_add (EmpathyTpContactList *list,
346 const gchar *group_name,
349 EmpathyTpContactListPriv *priv = GET_PRIV (list);
351 const gchar *names[] = {group_name, NULL};
353 /* Search the channel for that group name */
354 channel = g_hash_table_lookup (priv->groups, group_name);
356 tp_cli_channel_interface_group_call_add_members (channel, -1,
357 handles, NULL, NULL, NULL, NULL, NULL);
358 g_array_free (handles, TRUE);
362 /* That group does not exist yet, we have to:
363 * 1) Request an handle for the group name
364 * 2) Request a channel
365 * 3) When NewChannel is emitted, add handles in members
367 g_hash_table_insert (priv->add_to_group,
368 g_strdup (group_name),
370 tp_cli_connection_call_request_handles (priv->connection, -1,
371 TP_HANDLE_TYPE_GROUP, names,
372 tp_contact_list_group_request_handles_cb,
378 got_added_members_cb (TpConnection *connection,
380 EmpathyContact * const * contacts,
382 const TpHandle *failed,
387 EmpathyTpContactListPriv *priv = GET_PRIV (list);
391 DEBUG ("Error: %s", error->message);
395 for (i = 0; i < n_contacts; i++) {
396 EmpathyContact *contact = contacts[i];
397 TpHandle handle = empathy_contact_get_handle (contact);
399 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
402 /* Add to the list and emit signal */
403 g_hash_table_insert (priv->members, GUINT_TO_POINTER (handle),
404 g_object_ref (contact));
405 g_signal_emit_by_name (list, "members-changed", contact,
411 add_to_members (EmpathyTpContactList *list,
414 EmpathyTpContactListPriv *priv = GET_PRIV (list);
418 if (handles->len == 0)
421 request = g_array_new (FALSE, FALSE, sizeof (TpHandle));
423 for (i = 0; i < handles->len; i++) {
424 TpHandle handle = g_array_index (handles, TpHandle, i);
426 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
429 g_array_append_val (request, handle);
432 if (request->len > 0) {
433 empathy_tp_contact_factory_get_from_handles (priv->connection,
434 request->len, (TpHandle *) request->data,
435 got_added_members_cb, NULL, NULL, G_OBJECT (list));
438 g_array_free (request, TRUE);
442 tp_contact_list_got_local_pending_cb (TpConnection *connection,
444 EmpathyContact * const * contacts,
446 const TpHandle *failed,
451 EmpathyTpContactListPriv *priv = GET_PRIV (list);
455 DEBUG ("Error: %s", error->message);
459 for (i = 0; i < n_contacts; i++) {
460 EmpathyContact *contact = contacts[i];
462 const gchar *message;
463 TpChannelGroupChangeReason reason;
465 handle = empathy_contact_get_handle (contact);
466 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle))) {
467 GArray handles = {(gchar *) &handle, 1};
469 /* This contact is already member, auto accept. */
470 tp_cli_channel_interface_group_call_add_members (priv->publish,
471 -1, &handles, NULL, NULL, NULL, NULL, NULL);
473 else if (tp_channel_group_get_local_pending_info (priv->publish,
478 /* Add contact to pendings */
479 g_hash_table_insert (priv->pendings, GUINT_TO_POINTER (handle),
480 g_object_ref (contact));
481 g_signal_emit_by_name (list, "pendings-changed", contact,
482 contact, reason, message, TRUE);
488 tp_contact_list_remove_handle (EmpathyTpContactList *list,
492 EmpathyTpContactListPriv *priv = GET_PRIV (list);
493 EmpathyContact *contact;
496 if (table == priv->pendings)
497 sig = "pendings-changed";
498 else if (table == priv->members)
499 sig = "members-changed";
503 contact = g_hash_table_lookup (table, GUINT_TO_POINTER (handle));
505 g_object_ref (contact);
506 g_hash_table_remove (table, GUINT_TO_POINTER (handle));
507 g_signal_emit_by_name (list, sig, contact, 0, 0, NULL,
509 g_object_unref (contact);
514 remove_from_member_if_needed (EmpathyTpContactList *list,
517 /* remove contact from members if it's not in publish and subscribe */
518 EmpathyTpContactListPriv *priv = GET_PRIV (list);
519 const TpIntSet *members;
521 members = tp_channel_group_get_members (priv->subscribe);
522 if (tp_intset_is_member (members, handle))
525 members = tp_channel_group_get_remote_pending (priv->subscribe);
526 if (tp_intset_is_member (members, handle))
529 members = tp_channel_group_get_members (priv->publish);
530 if (tp_intset_is_member (members, handle))
533 tp_contact_list_remove_handle (list, priv->members, handle);
537 tp_contact_list_publish_group_members_changed_cb (TpChannel *channel,
541 GArray *local_pending,
542 GArray *remote_pending,
544 TpChannelGroupChangeReason reason,
545 EmpathyTpContactList *list)
547 EmpathyTpContactListPriv *priv = GET_PRIV (list);
550 /* We now send our presence to those contacts, remove them from pendings */
551 add_to_members (list, added);
552 for (i = 0; i < added->len; i++) {
553 tp_contact_list_remove_handle (list, priv->pendings,
554 g_array_index (added, TpHandle, i));
557 /* We refuse to send our presence to those contacts, remove from pendings */
558 for (i = 0; i < removed->len; i++) {
559 TpHandle handle = g_array_index (removed, TpHandle, i);
561 tp_contact_list_remove_handle (list, priv->pendings, handle);
562 remove_from_member_if_needed (list, handle);
565 /* Those contacts want our presence, auto accept those that are already
566 * member, otherwise add in pendings. */
567 if (local_pending->len > 0) {
568 empathy_tp_contact_factory_get_from_handles (priv->connection,
569 local_pending->len, (TpHandle *) local_pending->data,
570 tp_contact_list_got_local_pending_cb, NULL, NULL,
576 tp_contact_list_get_alias_flags_cb (TpConnection *connection,
582 EmpathyTpContactListPriv *priv = GET_PRIV (list);
585 DEBUG ("Error: %s", error->message);
589 if (flags & TP_CONNECTION_ALIAS_FLAG_USER_SET) {
590 priv->flags |= EMPATHY_CONTACT_LIST_CAN_ALIAS;
595 tp_contact_list_get_requestablechannelclasses_cb (TpProxy *connection,
601 EmpathyTpContactListPriv *priv = GET_PRIV (list);
606 DEBUG ("Error: %s", error->message);
610 classes = g_value_get_boxed (value);
611 for (i = 0; i < classes->len; i++) {
612 GValueArray *class = g_ptr_array_index (classes, i);
614 const char *channel_type;
617 props = g_value_get_boxed (g_value_array_get_nth (class, 0));
619 channel_type = tp_asv_get_string (props,
620 TP_IFACE_CHANNEL ".ChannelType");
621 handle_type = tp_asv_get_uint32 (props,
622 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
624 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) &&
625 handle_type == TP_HANDLE_TYPE_GROUP) {
626 DEBUG ("Got channel class for a contact group");
627 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
634 tp_contact_list_subscribe_group_members_changed_cb (TpChannel *channel,
638 GArray *local_pending,
639 GArray *remote_pending,
642 EmpathyTpContactList *list)
644 EmpathyTpContactListPriv *priv = GET_PRIV (list);
648 /* We now get the presence of those contacts, add them to members */
649 add_to_members (list, added);
651 /* Those contacts refuse to send us their presence, remove from members. */
652 for (i = 0; i < removed->len; i++) {
653 remove_from_member_if_needed (list, g_array_index (removed, TpHandle, i));
656 /* We want those contacts in our contact list but we don't get their
657 * presence yet. Add to members anyway. */
658 add_to_members (list, remote_pending);
660 /* Implicitly accept pending request of contacts which are now members. */
661 if (priv->publish == NULL)
664 accept = g_array_new (FALSE, FALSE, sizeof (TpHandle));
665 for (i = 0; i < added->len; i++) {
666 TpHandle handle = g_array_index (added, TpHandle, i);
668 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle)) != NULL)
669 g_array_append_val (accept, handle);
672 for (i = 0; i < remote_pending->len; i++) {
673 TpHandle handle = g_array_index (added, TpHandle, i);
675 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle)) != NULL)
676 g_array_append_val (accept, handle);
679 tp_cli_channel_interface_group_call_add_members (priv->publish,
680 -1, accept, NULL, NULL, NULL, NULL, NULL);
682 g_array_free (accept, TRUE);
686 tp_contact_list_store_group_members_changed_cb (TpChannel *channel,
690 GArray *local_pending,
691 GArray *remote_pending,
694 EmpathyTpContactList *list)
698 add_to_members (list, added);
700 for (i = 0; i < removed->len; i++) {
701 remove_from_member_if_needed (list, g_array_index (removed, TpHandle, i));
706 tp_contact_list_finalize (GObject *object)
708 EmpathyTpContactListPriv *priv;
709 EmpathyTpContactList *list;
713 list = EMPATHY_TP_CONTACT_LIST (object);
714 priv = GET_PRIV (list);
716 DEBUG ("finalize: %p", object);
718 if (priv->subscribe) {
719 g_object_unref (priv->subscribe);
722 g_object_unref (priv->publish);
725 g_object_unref (priv->stored);
729 g_object_unref (priv->deny);
732 if (priv->connection) {
733 g_object_unref (priv->connection);
736 g_hash_table_iter_init (&iter, priv->groups);
737 while (g_hash_table_iter_next (&iter, NULL, &channel)) {
738 g_signal_handlers_disconnect_by_func (channel,
739 tp_contact_list_group_invalidated_cb, list);
742 g_hash_table_destroy (priv->groups);
743 g_hash_table_destroy (priv->members);
744 g_hash_table_destroy (priv->pendings);
745 g_hash_table_destroy (priv->add_to_group);
747 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
751 got_list_channel (EmpathyTpContactList *list,
754 EmpathyTpContactListPriv *priv = GET_PRIV (list);
757 /* We requested that channel by providing TargetID property, so it's
758 * guaranteed that tp_channel_get_identifier will return it. */
759 id = tp_channel_get_identifier (channel);
761 /* TpChannel emits initial set of members just before being ready */
762 if (!tp_strdiff (id, "stored")) {
763 if (priv->stored != NULL)
765 priv->stored = g_object_ref (channel);
766 g_signal_connect (priv->stored, "group-members-changed",
767 G_CALLBACK (tp_contact_list_store_group_members_changed_cb),
769 } else if (!tp_strdiff (id, "publish")) {
770 if (priv->publish != NULL)
772 priv->publish = g_object_ref (channel);
773 g_signal_connect (priv->publish, "group-members-changed",
774 G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
776 } else if (!tp_strdiff (id, "subscribe")) {
777 if (priv->subscribe != NULL)
779 priv->subscribe = g_object_ref (channel);
780 g_signal_connect (priv->subscribe, "group-members-changed",
781 G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
783 } else if (!tp_strdiff (id, "deny")) {
784 if (priv->deny != NULL)
786 DEBUG ("Got 'deny' channel");
787 priv->deny = g_object_ref (channel);
792 list_ensure_channel_cb (TpConnection *conn,
795 GHashTable *properties,
798 GObject *weak_object)
800 EmpathyTpContactList *list = user_data;
804 DEBUG ("failed: %s\n", error->message);
808 channel = tp_channel_new_from_properties (conn, path, properties, NULL);
809 got_list_channel (list, channel);
810 g_object_unref (channel);
814 list_get_contact_blocking_capabilities_cb (TpProxy *conn,
816 const GError *in_error,
818 GObject *weak_object)
820 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
821 EmpathyTpContactListPriv *priv = GET_PRIV (list);
822 TpContactBlockingCapabilities caps;
824 if (in_error != NULL) {
825 DEBUG ("Get(Blocking, ContactBlockingCapabilities) on %s failed: %s",
826 tp_proxy_get_object_path (conn),
831 if (!G_VALUE_HOLDS_UINT (value)) {
832 DEBUG ("Get(Blocking, ContactBlockingCapabilities) on %s returned a %s, not a uint",
833 tp_proxy_get_object_path (conn),
834 G_VALUE_TYPE_NAME (value));
838 caps = g_value_get_uint (value);
840 if (caps & TP_CONTACT_BLOCKING_CAPABILITY_CAN_REPORT_ABUSIVE) {
841 DEBUG ("Connection can report abusive contacts");
842 priv->flags |= EMPATHY_CONTACT_LIST_CAN_REPORT_ABUSIVE;
847 iterate_on_channels (EmpathyTpContactList *list,
848 const GPtrArray *channels)
852 for (i = 0; i < channels->len ; i++) {
853 GValueArray *arr = g_ptr_array_index (channels, i);
855 GHashTable *properties;
856 TpHandleType handle_type;
858 path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
859 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
861 if (tp_strdiff (tp_asv_get_string (properties,
862 TP_IFACE_CHANNEL ".ChannelType"),
863 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST))
866 if (tp_asv_get_string (properties, TP_IFACE_CHANNEL ".TargetID") == NULL)
869 handle_type = tp_asv_get_uint32 (properties,
870 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
872 if (handle_type != TP_HANDLE_TYPE_GROUP)
875 tp_contact_list_group_add_channel (list, path, properties);
880 new_channels_cb (TpConnection *conn,
881 const GPtrArray *channels,
883 GObject *weak_object)
885 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
887 iterate_on_channels (list, channels);
891 got_channels_cb (TpProxy *conn,
895 GObject *weak_object)
897 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
898 const GPtrArray *channels;
901 DEBUG ("Get Channels property failed: %s", error->message);
905 channels = g_value_get_boxed (out);
906 iterate_on_channels (list, channels);
910 conn_ready_cb (TpConnection *connection,
914 EmpathyTpContactList *list = data;
915 EmpathyTpContactListPriv *priv = GET_PRIV (list);
919 DEBUG ("failed: %s", error->message);
923 /* Look for existing group channels */
924 tp_cli_dbus_properties_call_get (connection, -1,
925 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels", got_channels_cb,
926 NULL, NULL, G_OBJECT (list));
928 request = tp_asv_new (
929 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
930 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_LIST,
933 /* Watch the NewChannels signal so if ensuring list channels fails (for
934 * example because the server is slow and the D-Bus call timeouts before CM
935 * fetches the roster), we have a chance to get them later. */
936 tp_cli_connection_interface_requests_connect_to_new_channels (
937 priv->connection, new_channels_cb, NULL, NULL, G_OBJECT (list), NULL);
939 /* Request the 'stored' list. */
940 tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "stored");
941 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
942 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
944 /* Request the 'publish' list. */
945 tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "publish");
946 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
947 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
949 /* Request the 'subscribe' list. */
950 tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "subscribe");
951 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
952 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
954 /* Request the 'deny' list */
955 tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "deny");
956 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
957 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
959 g_hash_table_unref (request);
961 /* Find out if we support reporting abusive contacts --
962 * this is done via the new Conn.I.ContactBlocking interface */
963 if (tp_proxy_has_interface_by_id (priv->connection,
964 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING)) {
965 DEBUG ("Have Conn.I.ContactBlocking");
967 tp_cli_dbus_properties_call_get (priv->connection, -1,
968 TP_IFACE_CONNECTION_INTERFACE_CONTACT_BLOCKING,
969 "ContactBlockingCapabilities",
970 list_get_contact_blocking_capabilities_cb,
971 NULL, NULL, G_OBJECT (list));
974 g_object_unref (list);
978 tp_contact_list_constructed (GObject *list)
980 EmpathyTpContactListPriv *priv = GET_PRIV (list);
982 /* call GetAliasFlags */
983 if (tp_proxy_has_interface_by_id (priv->connection,
984 TP_IFACE_QUARK_CONNECTION_INTERFACE_ALIASING)) {
985 tp_cli_connection_interface_aliasing_call_get_alias_flags (
988 tp_contact_list_get_alias_flags_cb,
993 /* lookup RequestableChannelClasses */
994 if (tp_proxy_has_interface_by_id (priv->connection,
995 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS)) {
996 tp_cli_dbus_properties_call_get (priv->connection,
998 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
999 "RequestableChannelClasses",
1000 tp_contact_list_get_requestablechannelclasses_cb,
1004 /* we just don't know... better mark the flag just in case */
1005 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
1008 tp_connection_call_when_ready (priv->connection, conn_ready_cb,
1009 g_object_ref (list));
1013 tp_contact_list_get_property (GObject *object,
1018 EmpathyTpContactListPriv *priv = GET_PRIV (object);
1021 case PROP_CONNECTION:
1022 g_value_set_object (value, priv->connection);
1025 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1031 tp_contact_list_set_property (GObject *object,
1033 const GValue *value,
1036 EmpathyTpContactListPriv *priv = GET_PRIV (object);
1039 case PROP_CONNECTION:
1040 priv->connection = g_value_dup_object (value);
1043 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1049 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
1051 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1053 object_class->finalize = tp_contact_list_finalize;
1054 object_class->constructed = tp_contact_list_constructed;
1055 object_class->get_property = tp_contact_list_get_property;
1056 object_class->set_property = tp_contact_list_set_property;
1058 g_object_class_install_property (object_class,
1060 g_param_spec_object ("connection",
1062 "The connection associated with the contact list",
1065 G_PARAM_CONSTRUCT_ONLY));
1067 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
1071 tp_contact_list_array_free (gpointer handles)
1073 g_array_free (handles, TRUE);
1077 empathy_tp_contact_list_init (EmpathyTpContactList *list)
1079 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
1080 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
1084 /* Map group's name to group's TpChannel. The group name string is owned
1085 * by the TpChannel object */
1086 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1088 (GDestroyNotify) g_object_unref);
1090 /* Map contact's handle to EmpathyContact object */
1091 priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1093 (GDestroyNotify) g_object_unref);
1095 /* Map contact's handle to EmpathyContact object */
1096 priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1098 (GDestroyNotify) g_object_unref);
1100 /* Map group's name to GArray of handle */
1101 priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
1103 tp_contact_list_array_free);
1106 EmpathyTpContactList *
1107 empathy_tp_contact_list_new (TpConnection *connection)
1109 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
1110 "connection", connection,
1115 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
1117 EmpathyTpContactListPriv *priv;
1119 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
1121 priv = GET_PRIV (list);
1123 return priv->connection;
1127 tp_contact_list_add (EmpathyContactList *list,
1128 EmpathyContact *contact,
1129 const gchar *message)
1131 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1133 GArray handles = {(gchar *) &handle, 1};
1135 handle = empathy_contact_get_handle (contact);
1137 if (priv->subscribe) {
1138 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
1139 -1, &handles, message, NULL, NULL, NULL, NULL);
1142 if (priv->publish) {
1143 TpChannelGroupFlags flags = tp_channel_group_get_flags (priv->subscribe);
1144 if (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD ||
1145 g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
1146 tp_cli_channel_interface_group_call_add_members (priv->publish,
1147 -1, &handles, message, NULL, NULL, NULL, NULL);
1152 tp_cli_channel_interface_group_call_remove_members (
1153 priv->deny, -1, &handles, message,
1154 NULL, NULL, NULL, NULL);
1159 tp_contact_list_remove (EmpathyContactList *list,
1160 EmpathyContact *contact,
1161 const gchar *message)
1163 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1165 GArray handles = {(gchar *) &handle, 1};
1167 handle = empathy_contact_get_handle (contact);
1169 /* FIXME: this is racy if tp_contact_list_remove is called before the
1170 * 'stored' list has been retrieved. */
1171 if (priv->stored != NULL) {
1172 tp_cli_channel_interface_group_call_remove_members (priv->stored,
1173 -1, &handles, message, NULL, NULL, NULL, NULL);
1176 if (priv->subscribe) {
1177 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
1178 -1, &handles, message, NULL, NULL, NULL, NULL);
1180 if (priv->publish) {
1181 tp_cli_channel_interface_group_call_remove_members (priv->publish,
1182 -1, &handles, message, NULL, NULL, NULL, NULL);
1187 tp_contact_list_get_members (EmpathyContactList *list)
1189 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1192 ret = g_hash_table_get_values (priv->members);
1193 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1198 tp_contact_list_get_pendings (EmpathyContactList *list)
1200 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1203 ret = g_hash_table_get_values (priv->pendings);
1204 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1209 tp_contact_list_get_all_groups (EmpathyContactList *list)
1211 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1214 ret = g_hash_table_get_keys (priv->groups);
1215 for (l = ret; l; l = l->next) {
1216 l->data = g_strdup (l->data);
1223 tp_contact_list_get_groups (EmpathyContactList *list,
1224 EmpathyContact *contact)
1226 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1228 GHashTableIter iter;
1229 gpointer group_name;
1233 handle = empathy_contact_get_handle (contact);
1234 g_hash_table_iter_init (&iter, priv->groups);
1235 while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
1236 const TpIntSet *members;
1238 members = tp_channel_group_get_members (channel);
1239 if (tp_intset_is_member (members, handle)) {
1240 ret = g_list_prepend (ret, g_strdup (group_name));
1248 tp_contact_list_add_to_group (EmpathyContactList *list,
1249 EmpathyContact *contact,
1250 const gchar *group_name)
1255 handle = empathy_contact_get_handle (contact);
1256 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1257 g_array_append_val (handles, handle);
1258 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1259 group_name, handles);
1263 tp_contact_list_remove_from_group (EmpathyContactList *list,
1264 EmpathyContact *contact,
1265 const gchar *group_name)
1267 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1270 GArray handles = {(gchar *) &handle, 1};
1272 channel = g_hash_table_lookup (priv->groups, group_name);
1273 if (channel == NULL) {
1277 handle = empathy_contact_get_handle (contact);
1278 DEBUG ("remove contact %s (%d) from group %s",
1279 empathy_contact_get_id (contact), handle, group_name);
1281 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1282 &handles, NULL, NULL, NULL, NULL, NULL);
1286 tp_contact_list_rename_group (EmpathyContactList *list,
1287 const gchar *old_group_name,
1288 const gchar *new_group_name)
1290 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1292 const TpIntSet *members;
1295 channel = g_hash_table_lookup (priv->groups, old_group_name);
1296 if (channel == NULL) {
1300 DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1302 /* Remove all members and close the old channel */
1303 members = tp_channel_group_get_members (channel);
1304 handles = tp_intset_to_array (members);
1305 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1306 handles, NULL, NULL, NULL, NULL, NULL);
1307 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1309 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1310 new_group_name, handles);
1314 tp_contact_list_remove_group (EmpathyContactList *list,
1315 const gchar *group_name)
1317 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1319 const TpIntSet *members;
1322 channel = g_hash_table_lookup (priv->groups, group_name);
1323 if (channel == NULL) {
1327 DEBUG ("remove group %s", group_name);
1329 /* Remove all members and close the channel */
1330 members = tp_channel_group_get_members (channel);
1331 handles = tp_intset_to_array (members);
1332 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1333 handles, NULL, NULL, NULL, NULL, NULL);
1334 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1335 g_array_free (handles, TRUE);
1338 static EmpathyContactListFlags
1339 tp_contact_list_get_flags (EmpathyContactList *list)
1341 EmpathyTpContactListPriv *priv;
1342 EmpathyContactListFlags flags;
1344 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1346 priv = GET_PRIV (list);
1347 flags = priv->flags;
1349 if (priv->subscribe != NULL) {
1350 TpChannelGroupFlags group_flags;
1352 group_flags = tp_channel_group_get_flags (priv->subscribe);
1354 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1355 flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1358 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1359 flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1363 if (priv->deny != NULL)
1364 flags |= EMPATHY_CONTACT_LIST_CAN_BLOCK;
1370 tp_contact_list_set_blocked (EmpathyContactList *list,
1371 EmpathyContact *contact,
1375 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1376 TpHandle handle = empathy_contact_get_handle (contact);
1377 GArray handles = { (char *) &handle, 1 };
1379 g_return_if_fail (TP_IS_CHANNEL (priv->deny));
1381 if (blocked && abusive) {
1382 /* we have to do this via the new interface */
1383 g_return_if_fail (priv->flags &
1384 EMPATHY_CONTACT_LIST_CAN_REPORT_ABUSIVE);
1386 tp_cli_connection_interface_contact_blocking_call_block_contacts (
1387 priv->connection, -1,
1388 &handles, TRUE, NULL, NULL, NULL, NULL);
1389 } else if (blocked) {
1390 tp_cli_channel_interface_group_call_add_members (
1392 &handles, NULL, NULL, NULL, NULL, NULL);
1394 tp_cli_channel_interface_group_call_remove_members (
1396 &handles, NULL, NULL, NULL, NULL, NULL);
1401 tp_contact_list_get_blocked (EmpathyContactList *list,
1402 EmpathyContact *contact)
1404 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1406 g_return_val_if_fail (TP_IS_CHANNEL (priv->deny), FALSE);
1408 return tp_intset_is_member (tp_channel_group_get_members (priv->deny),
1409 empathy_contact_get_handle (contact));
1413 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1415 iface->add = tp_contact_list_add;
1416 iface->remove = tp_contact_list_remove;
1417 iface->get_members = tp_contact_list_get_members;
1418 iface->get_pendings = tp_contact_list_get_pendings;
1419 iface->get_all_groups = tp_contact_list_get_all_groups;
1420 iface->get_groups = tp_contact_list_get_groups;
1421 iface->add_to_group = tp_contact_list_add_to_group;
1422 iface->remove_from_group = tp_contact_list_remove_from_group;
1423 iface->rename_group = tp_contact_list_rename_group;
1424 iface->remove_group = tp_contact_list_remove_group;
1425 iface->get_flags = tp_contact_list_get_flags;
1426 iface->set_blocked = tp_contact_list_set_blocked;
1427 iface->get_blocked = tp_contact_list_get_blocked;
1431 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1433 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1434 GHashTableIter iter;
1437 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1439 /* Remove all contacts */
1440 g_hash_table_iter_init (&iter, priv->members);
1441 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1442 g_signal_emit_by_name (list, "members-changed", contact,
1446 g_hash_table_remove_all (priv->members);
1448 g_hash_table_iter_init (&iter, priv->pendings);
1449 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1450 g_signal_emit_by_name (list, "pendings-changed", contact,
1454 g_hash_table_remove_all (priv->pendings);