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 EmpContactBlockingCapabilities caps;
824 g_return_if_fail (G_VALUE_HOLDS_UINT (value));
826 caps = g_value_get_uint (value);
828 if (caps & EMP_CONTACT_BLOCKING_CAPABILITY_CAN_REPORT_ABUSIVE) {
829 DEBUG ("Connection can report abusive contacts");
830 priv->flags |= EMPATHY_CONTACT_LIST_CAN_REPORT_ABUSIVE;
835 iterate_on_channels (EmpathyTpContactList *list,
836 const GPtrArray *channels)
840 for (i = 0; i < channels->len ; i++) {
841 GValueArray *arr = g_ptr_array_index (channels, i);
843 GHashTable *properties;
844 TpHandleType handle_type;
846 path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
847 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
849 if (tp_strdiff (tp_asv_get_string (properties,
850 TP_IFACE_CHANNEL ".ChannelType"),
851 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST))
854 if (tp_asv_get_string (properties, TP_IFACE_CHANNEL ".TargetID") == NULL)
857 handle_type = tp_asv_get_uint32 (properties,
858 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
860 if (handle_type != TP_HANDLE_TYPE_GROUP)
863 tp_contact_list_group_add_channel (list, path, properties);
868 new_channels_cb (TpConnection *conn,
869 const GPtrArray *channels,
871 GObject *weak_object)
873 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
875 iterate_on_channels (list, channels);
879 got_channels_cb (TpProxy *conn,
883 GObject *weak_object)
885 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
886 const GPtrArray *channels;
889 DEBUG ("Get Channels property failed: %s", error->message);
893 channels = g_value_get_boxed (out);
894 iterate_on_channels (list, channels);
898 conn_ready_cb (TpConnection *connection,
902 EmpathyTpContactList *list = data;
903 EmpathyTpContactListPriv *priv = GET_PRIV (list);
907 DEBUG ("failed: %s", error->message);
911 /* Look for existing group channels */
912 tp_cli_dbus_properties_call_get (connection, -1,
913 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels", got_channels_cb,
914 NULL, NULL, G_OBJECT (list));
916 request = tp_asv_new (
917 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
918 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_LIST,
921 /* Watch the NewChannels signal so if ensuring list channels fails (for
922 * example because the server is slow and the D-Bus call timeouts before CM
923 * fetches the roster), we have a chance to get them later. */
924 tp_cli_connection_interface_requests_connect_to_new_channels (
925 priv->connection, new_channels_cb, NULL, NULL, G_OBJECT (list), NULL);
927 /* Request the 'stored' list. */
928 tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "stored");
929 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
930 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
932 /* Request the 'publish' list. */
933 tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "publish");
934 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
935 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
937 /* Request the 'subscribe' list. */
938 tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "subscribe");
939 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
940 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
942 /* Request the 'deny' list */
943 tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "deny");
944 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
945 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
947 g_hash_table_unref (request);
949 /* Find out if we support reporting abusive contacts --
950 * this is done via the new Conn.I.ContactBlocking interface */
951 if (tp_proxy_has_interface_by_id (priv->connection,
952 EMP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING)) {
953 DEBUG ("Have Conn.I.ContactBlocking");
955 tp_cli_dbus_properties_call_get (priv->connection, -1,
956 EMP_IFACE_CONNECTION_INTERFACE_CONTACT_BLOCKING,
957 "ContactBlockingCapabilities",
958 list_get_contact_blocking_capabilities_cb,
959 NULL, NULL, G_OBJECT (list));
962 g_object_unref (list);
966 tp_contact_list_constructed (GObject *list)
968 EmpathyTpContactListPriv *priv = GET_PRIV (list);
970 /* call GetAliasFlags */
971 if (tp_proxy_has_interface_by_id (priv->connection,
972 TP_IFACE_QUARK_CONNECTION_INTERFACE_ALIASING)) {
973 tp_cli_connection_interface_aliasing_call_get_alias_flags (
976 tp_contact_list_get_alias_flags_cb,
981 /* lookup RequestableChannelClasses */
982 if (tp_proxy_has_interface_by_id (priv->connection,
983 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS)) {
984 tp_cli_dbus_properties_call_get (priv->connection,
986 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
987 "RequestableChannelClasses",
988 tp_contact_list_get_requestablechannelclasses_cb,
992 /* we just don't know... better mark the flag just in case */
993 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
996 tp_connection_call_when_ready (priv->connection, conn_ready_cb,
997 g_object_ref (list));
1001 tp_contact_list_get_property (GObject *object,
1006 EmpathyTpContactListPriv *priv = GET_PRIV (object);
1009 case PROP_CONNECTION:
1010 g_value_set_object (value, priv->connection);
1013 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1019 tp_contact_list_set_property (GObject *object,
1021 const GValue *value,
1024 EmpathyTpContactListPriv *priv = GET_PRIV (object);
1027 case PROP_CONNECTION:
1028 priv->connection = g_value_dup_object (value);
1031 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1037 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
1039 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1041 object_class->finalize = tp_contact_list_finalize;
1042 object_class->constructed = tp_contact_list_constructed;
1043 object_class->get_property = tp_contact_list_get_property;
1044 object_class->set_property = tp_contact_list_set_property;
1046 g_object_class_install_property (object_class,
1048 g_param_spec_object ("connection",
1050 "The connection associated with the contact list",
1053 G_PARAM_CONSTRUCT_ONLY));
1055 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
1059 tp_contact_list_array_free (gpointer handles)
1061 g_array_free (handles, TRUE);
1065 empathy_tp_contact_list_init (EmpathyTpContactList *list)
1067 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
1068 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
1072 /* Map group's name to group's TpChannel. The group name string is owned
1073 * by the TpChannel object */
1074 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1076 (GDestroyNotify) g_object_unref);
1078 /* Map contact's handle to EmpathyContact object */
1079 priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1081 (GDestroyNotify) g_object_unref);
1083 /* Map contact's handle to EmpathyContact object */
1084 priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1086 (GDestroyNotify) g_object_unref);
1088 /* Map group's name to GArray of handle */
1089 priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
1091 tp_contact_list_array_free);
1094 EmpathyTpContactList *
1095 empathy_tp_contact_list_new (TpConnection *connection)
1097 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
1098 "connection", connection,
1103 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
1105 EmpathyTpContactListPriv *priv;
1107 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
1109 priv = GET_PRIV (list);
1111 return priv->connection;
1115 tp_contact_list_add (EmpathyContactList *list,
1116 EmpathyContact *contact,
1117 const gchar *message)
1119 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1121 GArray handles = {(gchar *) &handle, 1};
1123 handle = empathy_contact_get_handle (contact);
1124 if (priv->subscribe) {
1125 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
1126 -1, &handles, message, NULL, NULL, NULL, NULL);
1128 if (priv->publish) {
1129 TpChannelGroupFlags flags = tp_channel_group_get_flags (priv->subscribe);
1130 if (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD ||
1131 g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
1132 tp_cli_channel_interface_group_call_add_members (priv->publish,
1133 -1, &handles, message, NULL, NULL, NULL, NULL);
1139 tp_contact_list_remove (EmpathyContactList *list,
1140 EmpathyContact *contact,
1141 const gchar *message)
1143 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1145 GArray handles = {(gchar *) &handle, 1};
1147 handle = empathy_contact_get_handle (contact);
1149 /* FIXME: this is racy if tp_contact_list_remove is called before the
1150 * 'stored' list has been retrieved. */
1151 if (priv->stored != NULL) {
1152 tp_cli_channel_interface_group_call_remove_members (priv->stored,
1153 -1, &handles, message, NULL, NULL, NULL, NULL);
1156 if (priv->subscribe) {
1157 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
1158 -1, &handles, message, NULL, NULL, NULL, NULL);
1160 if (priv->publish) {
1161 tp_cli_channel_interface_group_call_remove_members (priv->publish,
1162 -1, &handles, message, NULL, NULL, NULL, NULL);
1167 tp_contact_list_get_members (EmpathyContactList *list)
1169 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1172 ret = g_hash_table_get_values (priv->members);
1173 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1178 tp_contact_list_get_pendings (EmpathyContactList *list)
1180 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1183 ret = g_hash_table_get_values (priv->pendings);
1184 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1189 tp_contact_list_get_all_groups (EmpathyContactList *list)
1191 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1194 ret = g_hash_table_get_keys (priv->groups);
1195 for (l = ret; l; l = l->next) {
1196 l->data = g_strdup (l->data);
1203 tp_contact_list_get_groups (EmpathyContactList *list,
1204 EmpathyContact *contact)
1206 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1208 GHashTableIter iter;
1209 gpointer group_name;
1213 handle = empathy_contact_get_handle (contact);
1214 g_hash_table_iter_init (&iter, priv->groups);
1215 while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
1216 const TpIntSet *members;
1218 members = tp_channel_group_get_members (channel);
1219 if (tp_intset_is_member (members, handle)) {
1220 ret = g_list_prepend (ret, g_strdup (group_name));
1228 tp_contact_list_add_to_group (EmpathyContactList *list,
1229 EmpathyContact *contact,
1230 const gchar *group_name)
1235 handle = empathy_contact_get_handle (contact);
1236 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1237 g_array_append_val (handles, handle);
1238 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1239 group_name, handles);
1243 tp_contact_list_remove_from_group (EmpathyContactList *list,
1244 EmpathyContact *contact,
1245 const gchar *group_name)
1247 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1250 GArray handles = {(gchar *) &handle, 1};
1252 channel = g_hash_table_lookup (priv->groups, group_name);
1253 if (channel == NULL) {
1257 handle = empathy_contact_get_handle (contact);
1258 DEBUG ("remove contact %s (%d) from group %s",
1259 empathy_contact_get_id (contact), handle, group_name);
1261 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1262 &handles, NULL, NULL, NULL, NULL, NULL);
1266 tp_contact_list_rename_group (EmpathyContactList *list,
1267 const gchar *old_group_name,
1268 const gchar *new_group_name)
1270 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1272 const TpIntSet *members;
1275 channel = g_hash_table_lookup (priv->groups, old_group_name);
1276 if (channel == NULL) {
1280 DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1282 /* Remove all members and close the old channel */
1283 members = tp_channel_group_get_members (channel);
1284 handles = tp_intset_to_array (members);
1285 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1286 handles, NULL, NULL, NULL, NULL, NULL);
1287 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1289 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1290 new_group_name, handles);
1294 tp_contact_list_remove_group (EmpathyContactList *list,
1295 const gchar *group_name)
1297 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1299 const TpIntSet *members;
1302 channel = g_hash_table_lookup (priv->groups, group_name);
1303 if (channel == NULL) {
1307 DEBUG ("remove group %s", group_name);
1309 /* Remove all members and close the channel */
1310 members = tp_channel_group_get_members (channel);
1311 handles = tp_intset_to_array (members);
1312 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1313 handles, NULL, NULL, NULL, NULL, NULL);
1314 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1315 g_array_free (handles, TRUE);
1318 static EmpathyContactListFlags
1319 tp_contact_list_get_flags (EmpathyContactList *list)
1321 EmpathyTpContactListPriv *priv;
1322 EmpathyContactListFlags flags;
1324 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1326 priv = GET_PRIV (list);
1327 flags = priv->flags;
1329 if (priv->subscribe != NULL) {
1330 TpChannelGroupFlags group_flags;
1332 group_flags = tp_channel_group_get_flags (priv->subscribe);
1334 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1335 flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1338 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1339 flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1343 if (priv->deny != NULL)
1344 flags |= EMPATHY_CONTACT_LIST_CAN_BLOCK;
1350 tp_contact_list_set_blocked (EmpathyContactList *list,
1351 EmpathyContact *contact,
1355 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1356 TpHandle handle = empathy_contact_get_handle (contact);
1357 GArray handles = { (char *) &handle, 1 };
1359 g_return_if_fail (TP_IS_CHANNEL (priv->deny));
1361 if (blocked && abusive) {
1362 /* we have to do this via the new interface */
1363 g_return_if_fail (priv->flags &
1364 EMPATHY_CONTACT_LIST_CAN_REPORT_ABUSIVE);
1366 emp_cli_connection_interface_contact_blocking_call_block_contacts (
1367 TP_PROXY (priv->connection), -1,
1368 &handles, TRUE, NULL, NULL, NULL, NULL);
1369 } else if (blocked) {
1370 tp_cli_channel_interface_group_call_add_members (
1372 &handles, NULL, NULL, NULL, NULL, NULL);
1374 tp_cli_channel_interface_group_call_remove_members (
1376 &handles, NULL, NULL, NULL, NULL, NULL);
1381 tp_contact_list_get_blocked (EmpathyContactList *list,
1382 EmpathyContact *contact)
1384 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1386 g_return_val_if_fail (TP_IS_CHANNEL (priv->deny), FALSE);
1388 return tp_intset_is_member (tp_channel_group_get_members (priv->deny),
1389 empathy_contact_get_handle (contact));
1393 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1395 iface->add = tp_contact_list_add;
1396 iface->remove = tp_contact_list_remove;
1397 iface->get_members = tp_contact_list_get_members;
1398 iface->get_pendings = tp_contact_list_get_pendings;
1399 iface->get_all_groups = tp_contact_list_get_all_groups;
1400 iface->get_groups = tp_contact_list_get_groups;
1401 iface->add_to_group = tp_contact_list_add_to_group;
1402 iface->remove_from_group = tp_contact_list_remove_from_group;
1403 iface->rename_group = tp_contact_list_rename_group;
1404 iface->remove_group = tp_contact_list_remove_group;
1405 iface->get_flags = tp_contact_list_get_flags;
1406 iface->set_blocked = tp_contact_list_set_blocked;
1407 iface->get_blocked = tp_contact_list_get_blocked;
1411 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1413 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1414 GHashTableIter iter;
1417 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1419 /* Remove all contacts */
1420 g_hash_table_iter_init (&iter, priv->members);
1421 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1422 g_signal_emit_by_name (list, "members-changed", contact,
1426 g_hash_table_remove_all (priv->members);
1428 g_hash_table_iter_init (&iter, priv->pendings);
1429 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1430 g_signal_emit_by_name (list, "pendings-changed", contact,
1434 g_hash_table_remove_all (priv->pendings);