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
;
464 const TpIntSet
*members
, *remote_pending
;
466 handle
= empathy_contact_get_handle (contact
);
467 members
= tp_channel_group_get_members (priv
->subscribe
);
468 remote_pending
= tp_channel_group_get_remote_pending (priv
->subscribe
);
470 if (tp_intset_is_member (members
, handle
) ||
471 tp_intset_is_member (remote_pending
, handle
)) {
472 GArray handles
= {(gchar
*) &handle
, 1};
474 /* This contact is already subscribed, or user requested
475 * to subscribe, auto accept. */
476 tp_cli_channel_interface_group_call_add_members (priv
->publish
,
477 -1, &handles
, NULL
, NULL
, NULL
, NULL
, NULL
);
479 else if (tp_channel_group_get_local_pending_info (priv
->publish
,
484 /* Add contact to pendings */
485 g_hash_table_insert (priv
->pendings
, GUINT_TO_POINTER (handle
),
486 g_object_ref (contact
));
487 g_signal_emit_by_name (list
, "pendings-changed", contact
,
488 contact
, reason
, message
, TRUE
);
494 tp_contact_list_remove_handle (EmpathyTpContactList
*list
,
498 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
499 EmpathyContact
*contact
;
502 if (table
== priv
->pendings
)
503 sig
= "pendings-changed";
504 else if (table
== priv
->members
)
505 sig
= "members-changed";
509 contact
= g_hash_table_lookup (table
, GUINT_TO_POINTER (handle
));
511 g_object_ref (contact
);
512 g_hash_table_remove (table
, GUINT_TO_POINTER (handle
));
513 g_signal_emit_by_name (list
, sig
, contact
, 0, 0, NULL
,
515 g_object_unref (contact
);
520 remove_from_member_if_needed (EmpathyTpContactList
*list
,
523 /* remove contact from members if it's not in publish and subscribe */
524 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
525 const TpIntSet
*members
;
527 members
= tp_channel_group_get_members (priv
->subscribe
);
528 if (tp_intset_is_member (members
, handle
))
531 members
= tp_channel_group_get_remote_pending (priv
->subscribe
);
532 if (tp_intset_is_member (members
, handle
))
535 members
= tp_channel_group_get_members (priv
->publish
);
536 if (tp_intset_is_member (members
, handle
))
539 tp_contact_list_remove_handle (list
, priv
->members
, handle
);
543 tp_contact_list_publish_group_members_changed_cb (TpChannel
*channel
,
547 GArray
*local_pending
,
548 GArray
*remote_pending
,
550 TpChannelGroupChangeReason reason
,
551 EmpathyTpContactList
*list
)
553 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
556 /* We now send our presence to those contacts, remove them from pendings */
557 add_to_members (list
, added
);
558 for (i
= 0; i
< added
->len
; i
++) {
559 tp_contact_list_remove_handle (list
, priv
->pendings
,
560 g_array_index (added
, TpHandle
, i
));
563 /* We refuse to send our presence to those contacts, remove from pendings */
564 for (i
= 0; i
< removed
->len
; i
++) {
565 TpHandle handle
= g_array_index (removed
, TpHandle
, i
);
567 tp_contact_list_remove_handle (list
, priv
->pendings
, handle
);
568 remove_from_member_if_needed (list
, handle
);
571 /* Those contacts want our presence, auto accept those that are already
572 * member, otherwise add in pendings. */
573 if (local_pending
->len
> 0) {
574 empathy_tp_contact_factory_get_from_handles (priv
->connection
,
575 local_pending
->len
, (TpHandle
*) local_pending
->data
,
576 tp_contact_list_got_local_pending_cb
, NULL
, NULL
,
582 tp_contact_list_get_alias_flags_cb (TpConnection
*connection
,
588 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
591 DEBUG ("Error: %s", error
->message
);
595 if (flags
& TP_CONNECTION_ALIAS_FLAG_USER_SET
) {
596 priv
->flags
|= EMPATHY_CONTACT_LIST_CAN_ALIAS
;
601 tp_contact_list_get_requestablechannelclasses_cb (TpProxy
*connection
,
607 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
612 DEBUG ("Error: %s", error
->message
);
616 classes
= g_value_get_boxed (value
);
617 for (i
= 0; i
< classes
->len
; i
++) {
618 GValueArray
*class = g_ptr_array_index (classes
, i
);
620 const char *channel_type
;
623 props
= g_value_get_boxed (g_value_array_get_nth (class, 0));
625 channel_type
= tp_asv_get_string (props
,
626 TP_IFACE_CHANNEL
".ChannelType");
627 handle_type
= tp_asv_get_uint32 (props
,
628 TP_IFACE_CHANNEL
".TargetHandleType", NULL
);
630 if (!tp_strdiff (channel_type
, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST
) &&
631 handle_type
== TP_HANDLE_TYPE_GROUP
) {
632 DEBUG ("Got channel class for a contact group");
633 priv
->flags
|= EMPATHY_CONTACT_LIST_CAN_GROUP
;
640 tp_contact_list_subscribe_group_members_changed_cb (TpChannel
*channel
,
644 GArray
*local_pending
,
645 GArray
*remote_pending
,
648 EmpathyTpContactList
*list
)
650 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
654 /* We now get the presence of those contacts, add them to members */
655 add_to_members (list
, added
);
657 /* Those contacts refuse to send us their presence, remove from members. */
658 for (i
= 0; i
< removed
->len
; i
++) {
659 remove_from_member_if_needed (list
, g_array_index (removed
, TpHandle
, i
));
662 /* We want those contacts in our contact list but we don't get their
663 * presence yet. Add to members anyway. */
664 add_to_members (list
, remote_pending
);
666 /* Implicitly accept pending request of contacts which are now members. */
667 if (priv
->publish
== NULL
)
670 accept
= g_array_new (FALSE
, FALSE
, sizeof (TpHandle
));
671 for (i
= 0; i
< added
->len
; i
++) {
672 TpHandle handle
= g_array_index (added
, TpHandle
, i
);
674 if (g_hash_table_lookup (priv
->pendings
, GUINT_TO_POINTER (handle
)) != NULL
)
675 g_array_append_val (accept
, handle
);
678 for (i
= 0; i
< remote_pending
->len
; i
++) {
679 TpHandle handle
= g_array_index (added
, TpHandle
, i
);
681 if (g_hash_table_lookup (priv
->pendings
, GUINT_TO_POINTER (handle
)) != NULL
)
682 g_array_append_val (accept
, handle
);
685 tp_cli_channel_interface_group_call_add_members (priv
->publish
,
686 -1, accept
, NULL
, NULL
, NULL
, NULL
, NULL
);
688 g_array_free (accept
, TRUE
);
692 tp_contact_list_store_group_members_changed_cb (TpChannel
*channel
,
696 GArray
*local_pending
,
697 GArray
*remote_pending
,
700 EmpathyTpContactList
*list
)
704 add_to_members (list
, added
);
706 for (i
= 0; i
< removed
->len
; i
++) {
707 remove_from_member_if_needed (list
, g_array_index (removed
, TpHandle
, i
));
712 tp_contact_list_finalize (GObject
*object
)
714 EmpathyTpContactListPriv
*priv
;
715 EmpathyTpContactList
*list
;
719 list
= EMPATHY_TP_CONTACT_LIST (object
);
720 priv
= GET_PRIV (list
);
722 DEBUG ("finalize: %p", object
);
724 if (priv
->subscribe
) {
725 g_object_unref (priv
->subscribe
);
728 g_object_unref (priv
->publish
);
731 g_object_unref (priv
->stored
);
735 g_object_unref (priv
->deny
);
738 if (priv
->connection
) {
739 g_object_unref (priv
->connection
);
742 g_hash_table_iter_init (&iter
, priv
->groups
);
743 while (g_hash_table_iter_next (&iter
, NULL
, &channel
)) {
744 g_signal_handlers_disconnect_by_func (channel
,
745 tp_contact_list_group_invalidated_cb
, list
);
748 g_hash_table_destroy (priv
->groups
);
749 g_hash_table_destroy (priv
->members
);
750 g_hash_table_destroy (priv
->pendings
);
751 g_hash_table_destroy (priv
->add_to_group
);
753 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class
)->finalize (object
);
757 got_list_channel (EmpathyTpContactList
*list
,
760 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
763 /* We requested that channel by providing TargetID property, so it's
764 * guaranteed that tp_channel_get_identifier will return it. */
765 id
= tp_channel_get_identifier (channel
);
767 /* TpChannel emits initial set of members just before being ready */
768 if (!tp_strdiff (id
, "stored")) {
769 if (priv
->stored
!= NULL
)
771 priv
->stored
= g_object_ref (channel
);
772 g_signal_connect (priv
->stored
, "group-members-changed",
773 G_CALLBACK (tp_contact_list_store_group_members_changed_cb
),
775 } else if (!tp_strdiff (id
, "publish")) {
776 if (priv
->publish
!= NULL
)
778 priv
->publish
= g_object_ref (channel
);
779 g_signal_connect (priv
->publish
, "group-members-changed",
780 G_CALLBACK (tp_contact_list_publish_group_members_changed_cb
),
782 } else if (!tp_strdiff (id
, "subscribe")) {
783 if (priv
->subscribe
!= NULL
)
785 priv
->subscribe
= g_object_ref (channel
);
786 g_signal_connect (priv
->subscribe
, "group-members-changed",
787 G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb
),
789 } else if (!tp_strdiff (id
, "deny")) {
790 if (priv
->deny
!= NULL
)
792 DEBUG ("Got 'deny' channel");
793 priv
->deny
= g_object_ref (channel
);
798 list_ensure_channel_cb (TpConnection
*conn
,
801 GHashTable
*properties
,
804 GObject
*weak_object
)
806 EmpathyTpContactList
*list
= user_data
;
810 DEBUG ("failed: %s\n", error
->message
);
814 channel
= tp_channel_new_from_properties (conn
, path
, properties
, NULL
);
815 got_list_channel (list
, channel
);
816 g_object_unref (channel
);
820 list_get_contact_blocking_capabilities_cb (TpProxy
*conn
,
822 const GError
*in_error
,
824 GObject
*weak_object
)
826 EmpathyTpContactList
*list
= EMPATHY_TP_CONTACT_LIST (weak_object
);
827 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
828 TpContactBlockingCapabilities caps
;
830 if (in_error
!= NULL
) {
831 DEBUG ("Get(Blocking, ContactBlockingCapabilities) on %s failed: %s",
832 tp_proxy_get_object_path (conn
),
837 if (!G_VALUE_HOLDS_UINT (value
)) {
838 DEBUG ("Get(Blocking, ContactBlockingCapabilities) on %s returned a %s, not a uint",
839 tp_proxy_get_object_path (conn
),
840 G_VALUE_TYPE_NAME (value
));
844 caps
= g_value_get_uint (value
);
846 if (caps
& TP_CONTACT_BLOCKING_CAPABILITY_CAN_REPORT_ABUSIVE
) {
847 DEBUG ("Connection can report abusive contacts");
848 priv
->flags
|= EMPATHY_CONTACT_LIST_CAN_REPORT_ABUSIVE
;
853 iterate_on_channels (EmpathyTpContactList
*list
,
854 const GPtrArray
*channels
)
858 for (i
= 0; i
< channels
->len
; i
++) {
859 GValueArray
*arr
= g_ptr_array_index (channels
, i
);
861 GHashTable
*properties
;
862 TpHandleType handle_type
;
864 path
= g_value_get_boxed (g_value_array_get_nth (arr
, 0));
865 properties
= g_value_get_boxed (g_value_array_get_nth (arr
, 1));
867 if (tp_strdiff (tp_asv_get_string (properties
,
868 TP_IFACE_CHANNEL
".ChannelType"),
869 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST
))
872 if (tp_asv_get_string (properties
, TP_IFACE_CHANNEL
".TargetID") == NULL
)
875 handle_type
= tp_asv_get_uint32 (properties
,
876 TP_IFACE_CHANNEL
".TargetHandleType", NULL
);
878 if (handle_type
!= TP_HANDLE_TYPE_GROUP
)
881 tp_contact_list_group_add_channel (list
, path
, properties
);
886 new_channels_cb (TpConnection
*conn
,
887 const GPtrArray
*channels
,
889 GObject
*weak_object
)
891 EmpathyTpContactList
*list
= EMPATHY_TP_CONTACT_LIST (weak_object
);
893 iterate_on_channels (list
, channels
);
897 got_channels_cb (TpProxy
*conn
,
901 GObject
*weak_object
)
903 EmpathyTpContactList
*list
= EMPATHY_TP_CONTACT_LIST (weak_object
);
904 const GPtrArray
*channels
;
907 DEBUG ("Get Channels property failed: %s", error
->message
);
911 channels
= g_value_get_boxed (out
);
912 iterate_on_channels (list
, channels
);
916 conn_ready_cb (TpConnection
*connection
,
920 EmpathyTpContactList
*list
= data
;
921 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
925 DEBUG ("failed: %s", error
->message
);
929 /* Look for existing group channels */
930 tp_cli_dbus_properties_call_get (connection
, -1,
931 TP_IFACE_CONNECTION_INTERFACE_REQUESTS
, "Channels", got_channels_cb
,
932 NULL
, NULL
, G_OBJECT (list
));
934 request
= tp_asv_new (
935 TP_PROP_CHANNEL_CHANNEL_TYPE
, G_TYPE_STRING
, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST
,
936 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE
, G_TYPE_UINT
, TP_HANDLE_TYPE_LIST
,
939 /* Watch the NewChannels signal so if ensuring list channels fails (for
940 * example because the server is slow and the D-Bus call timeouts before CM
941 * fetches the roster), we have a chance to get them later. */
942 tp_cli_connection_interface_requests_connect_to_new_channels (
943 priv
->connection
, new_channels_cb
, NULL
, NULL
, G_OBJECT (list
), NULL
);
945 /* Request the 'stored' list. */
946 tp_asv_set_static_string (request
, TP_PROP_CHANNEL_TARGET_ID
, "stored");
947 tp_cli_connection_interface_requests_call_ensure_channel (priv
->connection
,
948 G_MAXINT
, request
, list_ensure_channel_cb
, list
, NULL
, G_OBJECT (list
));
950 /* Request the 'publish' list. */
951 tp_asv_set_static_string (request
, TP_PROP_CHANNEL_TARGET_ID
, "publish");
952 tp_cli_connection_interface_requests_call_ensure_channel (priv
->connection
,
953 G_MAXINT
, request
, list_ensure_channel_cb
, list
, NULL
, G_OBJECT (list
));
955 /* Request the 'subscribe' list. */
956 tp_asv_set_static_string (request
, TP_PROP_CHANNEL_TARGET_ID
, "subscribe");
957 tp_cli_connection_interface_requests_call_ensure_channel (priv
->connection
,
958 G_MAXINT
, request
, list_ensure_channel_cb
, list
, NULL
, G_OBJECT (list
));
960 /* Request the 'deny' list */
961 tp_asv_set_static_string (request
, TP_PROP_CHANNEL_TARGET_ID
, "deny");
962 tp_cli_connection_interface_requests_call_ensure_channel (priv
->connection
,
963 G_MAXINT
, request
, list_ensure_channel_cb
, list
, NULL
, G_OBJECT (list
));
965 g_hash_table_unref (request
);
967 /* Find out if we support reporting abusive contacts --
968 * this is done via the new Conn.I.ContactBlocking interface */
969 if (tp_proxy_has_interface_by_id (priv
->connection
,
970 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING
)) {
971 DEBUG ("Have Conn.I.ContactBlocking");
973 tp_cli_dbus_properties_call_get (priv
->connection
, -1,
974 TP_IFACE_CONNECTION_INTERFACE_CONTACT_BLOCKING
,
975 "ContactBlockingCapabilities",
976 list_get_contact_blocking_capabilities_cb
,
977 NULL
, NULL
, G_OBJECT (list
));
980 g_object_unref (list
);
984 tp_contact_list_constructed (GObject
*list
)
986 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
988 /* call GetAliasFlags */
989 if (tp_proxy_has_interface_by_id (priv
->connection
,
990 TP_IFACE_QUARK_CONNECTION_INTERFACE_ALIASING
)) {
991 tp_cli_connection_interface_aliasing_call_get_alias_flags (
994 tp_contact_list_get_alias_flags_cb
,
999 /* lookup RequestableChannelClasses */
1000 if (tp_proxy_has_interface_by_id (priv
->connection
,
1001 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS
)) {
1002 tp_cli_dbus_properties_call_get (priv
->connection
,
1004 TP_IFACE_CONNECTION_INTERFACE_REQUESTS
,
1005 "RequestableChannelClasses",
1006 tp_contact_list_get_requestablechannelclasses_cb
,
1010 /* we just don't know... better mark the flag just in case */
1011 priv
->flags
|= EMPATHY_CONTACT_LIST_CAN_GROUP
;
1014 tp_connection_call_when_ready (priv
->connection
, conn_ready_cb
,
1015 g_object_ref (list
));
1019 tp_contact_list_get_property (GObject
*object
,
1024 EmpathyTpContactListPriv
*priv
= GET_PRIV (object
);
1027 case PROP_CONNECTION
:
1028 g_value_set_object (value
, priv
->connection
);
1031 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
1037 tp_contact_list_set_property (GObject
*object
,
1039 const GValue
*value
,
1042 EmpathyTpContactListPriv
*priv
= GET_PRIV (object
);
1045 case PROP_CONNECTION
:
1046 priv
->connection
= g_value_dup_object (value
);
1049 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
1055 empathy_tp_contact_list_class_init (EmpathyTpContactListClass
*klass
)
1057 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
1059 object_class
->finalize
= tp_contact_list_finalize
;
1060 object_class
->constructed
= tp_contact_list_constructed
;
1061 object_class
->get_property
= tp_contact_list_get_property
;
1062 object_class
->set_property
= tp_contact_list_set_property
;
1064 g_object_class_install_property (object_class
,
1066 g_param_spec_object ("connection",
1068 "The connection associated with the contact list",
1071 G_PARAM_CONSTRUCT_ONLY
));
1073 g_type_class_add_private (object_class
, sizeof (EmpathyTpContactListPriv
));
1077 tp_contact_list_array_free (gpointer handles
)
1079 g_array_free (handles
, TRUE
);
1083 empathy_tp_contact_list_init (EmpathyTpContactList
*list
)
1085 EmpathyTpContactListPriv
*priv
= G_TYPE_INSTANCE_GET_PRIVATE (list
,
1086 EMPATHY_TYPE_TP_CONTACT_LIST
, EmpathyTpContactListPriv
);
1090 /* Map group's name to group's TpChannel. The group name string is owned
1091 * by the TpChannel object */
1092 priv
->groups
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
1094 (GDestroyNotify
) g_object_unref
);
1096 /* Map contact's handle to EmpathyContact object */
1097 priv
->members
= g_hash_table_new_full (g_direct_hash
, g_direct_equal
,
1099 (GDestroyNotify
) g_object_unref
);
1101 /* Map contact's handle to EmpathyContact object */
1102 priv
->pendings
= g_hash_table_new_full (g_direct_hash
, g_direct_equal
,
1104 (GDestroyNotify
) g_object_unref
);
1106 /* Map group's name to GArray of handle */
1107 priv
->add_to_group
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
1109 tp_contact_list_array_free
);
1112 EmpathyTpContactList
*
1113 empathy_tp_contact_list_new (TpConnection
*connection
)
1115 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST
,
1116 "connection", connection
,
1121 empathy_tp_contact_list_get_connection (EmpathyTpContactList
*list
)
1123 EmpathyTpContactListPriv
*priv
;
1125 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list
), NULL
);
1127 priv
= GET_PRIV (list
);
1129 return priv
->connection
;
1133 tp_contact_list_add (EmpathyContactList
*list
,
1134 EmpathyContact
*contact
,
1135 const gchar
*message
)
1137 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
1139 GArray handles
= {(gchar
*) &handle
, 1};
1141 handle
= empathy_contact_get_handle (contact
);
1143 if (priv
->subscribe
) {
1144 tp_cli_channel_interface_group_call_add_members (priv
->subscribe
,
1145 -1, &handles
, message
, NULL
, NULL
, NULL
, NULL
);
1148 if (priv
->publish
) {
1149 TpChannelGroupFlags flags
= tp_channel_group_get_flags (priv
->subscribe
);
1150 if (flags
& TP_CHANNEL_GROUP_FLAG_CAN_ADD
||
1151 g_hash_table_lookup (priv
->pendings
, GUINT_TO_POINTER (handle
))) {
1152 tp_cli_channel_interface_group_call_add_members (priv
->publish
,
1153 -1, &handles
, message
, NULL
, NULL
, NULL
, NULL
);
1158 tp_cli_channel_interface_group_call_remove_members (
1159 priv
->deny
, -1, &handles
, message
,
1160 NULL
, NULL
, NULL
, NULL
);
1165 tp_contact_list_remove (EmpathyContactList
*list
,
1166 EmpathyContact
*contact
,
1167 const gchar
*message
)
1169 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
1171 GArray handles
= {(gchar
*) &handle
, 1};
1173 handle
= empathy_contact_get_handle (contact
);
1175 /* FIXME: this is racy if tp_contact_list_remove is called before the
1176 * 'stored' list has been retrieved. */
1177 if (priv
->stored
!= NULL
) {
1178 tp_cli_channel_interface_group_call_remove_members (priv
->stored
,
1179 -1, &handles
, message
, NULL
, NULL
, NULL
, NULL
);
1182 if (priv
->subscribe
) {
1183 tp_cli_channel_interface_group_call_remove_members (priv
->subscribe
,
1184 -1, &handles
, message
, NULL
, NULL
, NULL
, NULL
);
1186 if (priv
->publish
) {
1187 tp_cli_channel_interface_group_call_remove_members (priv
->publish
,
1188 -1, &handles
, message
, NULL
, NULL
, NULL
, NULL
);
1193 tp_contact_list_get_members (EmpathyContactList
*list
)
1195 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
1198 ret
= g_hash_table_get_values (priv
->members
);
1199 g_list_foreach (ret
, (GFunc
) g_object_ref
, NULL
);
1204 tp_contact_list_get_pendings (EmpathyContactList
*list
)
1206 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
1209 ret
= g_hash_table_get_values (priv
->pendings
);
1210 g_list_foreach (ret
, (GFunc
) g_object_ref
, NULL
);
1215 tp_contact_list_get_all_groups (EmpathyContactList
*list
)
1217 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
1220 ret
= g_hash_table_get_keys (priv
->groups
);
1221 for (l
= ret
; l
; l
= l
->next
) {
1222 l
->data
= g_strdup (l
->data
);
1229 tp_contact_list_get_groups (EmpathyContactList
*list
,
1230 EmpathyContact
*contact
)
1232 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
1234 GHashTableIter iter
;
1235 gpointer group_name
;
1239 handle
= empathy_contact_get_handle (contact
);
1240 g_hash_table_iter_init (&iter
, priv
->groups
);
1241 while (g_hash_table_iter_next (&iter
, &group_name
, &channel
)) {
1242 const TpIntSet
*members
;
1244 members
= tp_channel_group_get_members (channel
);
1245 if (tp_intset_is_member (members
, handle
)) {
1246 ret
= g_list_prepend (ret
, g_strdup (group_name
));
1254 tp_contact_list_add_to_group (EmpathyContactList
*list
,
1255 EmpathyContact
*contact
,
1256 const gchar
*group_name
)
1261 handle
= empathy_contact_get_handle (contact
);
1262 handles
= g_array_sized_new (FALSE
, FALSE
, sizeof (TpHandle
), 1);
1263 g_array_append_val (handles
, handle
);
1264 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list
),
1265 group_name
, handles
);
1269 tp_contact_list_remove_from_group (EmpathyContactList
*list
,
1270 EmpathyContact
*contact
,
1271 const gchar
*group_name
)
1273 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
1276 GArray handles
= {(gchar
*) &handle
, 1};
1278 channel
= g_hash_table_lookup (priv
->groups
, group_name
);
1279 if (channel
== NULL
) {
1283 handle
= empathy_contact_get_handle (contact
);
1284 DEBUG ("remove contact %s (%d) from group %s",
1285 empathy_contact_get_id (contact
), handle
, group_name
);
1287 tp_cli_channel_interface_group_call_remove_members (channel
, -1,
1288 &handles
, NULL
, NULL
, NULL
, NULL
, NULL
);
1292 tp_contact_list_rename_group (EmpathyContactList
*list
,
1293 const gchar
*old_group_name
,
1294 const gchar
*new_group_name
)
1296 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
1298 const TpIntSet
*members
;
1301 channel
= g_hash_table_lookup (priv
->groups
, old_group_name
);
1302 if (channel
== NULL
) {
1306 DEBUG ("rename group %s to %s", old_group_name
, new_group_name
);
1308 /* Remove all members and close the old channel */
1309 members
= tp_channel_group_get_members (channel
);
1310 handles
= tp_intset_to_array (members
);
1311 tp_cli_channel_interface_group_call_remove_members (channel
, -1,
1312 handles
, NULL
, NULL
, NULL
, NULL
, NULL
);
1313 tp_cli_channel_call_close (channel
, -1, NULL
, NULL
, NULL
, NULL
);
1315 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list
),
1316 new_group_name
, handles
);
1320 tp_contact_list_remove_group (EmpathyContactList
*list
,
1321 const gchar
*group_name
)
1323 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
1325 const TpIntSet
*members
;
1328 channel
= g_hash_table_lookup (priv
->groups
, group_name
);
1329 if (channel
== NULL
) {
1333 DEBUG ("remove group %s", group_name
);
1335 /* Remove all members and close the channel */
1336 members
= tp_channel_group_get_members (channel
);
1337 handles
= tp_intset_to_array (members
);
1338 tp_cli_channel_interface_group_call_remove_members (channel
, -1,
1339 handles
, NULL
, NULL
, NULL
, NULL
, NULL
);
1340 tp_cli_channel_call_close (channel
, -1, NULL
, NULL
, NULL
, NULL
);
1341 g_array_free (handles
, TRUE
);
1344 static EmpathyContactListFlags
1345 tp_contact_list_get_flags (EmpathyContactList
*list
)
1347 EmpathyTpContactListPriv
*priv
;
1348 EmpathyContactListFlags flags
;
1350 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list
), FALSE
);
1352 priv
= GET_PRIV (list
);
1353 flags
= priv
->flags
;
1355 if (priv
->subscribe
!= NULL
) {
1356 TpChannelGroupFlags group_flags
;
1358 group_flags
= tp_channel_group_get_flags (priv
->subscribe
);
1360 if (group_flags
& TP_CHANNEL_GROUP_FLAG_CAN_ADD
) {
1361 flags
|= EMPATHY_CONTACT_LIST_CAN_ADD
;
1364 if (group_flags
& TP_CHANNEL_GROUP_FLAG_CAN_REMOVE
) {
1365 flags
|= EMPATHY_CONTACT_LIST_CAN_REMOVE
;
1368 if (group_flags
& TP_CHANNEL_GROUP_FLAG_MESSAGE_ADD
) {
1369 flags
|= EMPATHY_CONTACT_LIST_MESSAGE_ADD
;
1373 if (priv
->deny
!= NULL
)
1374 flags
|= EMPATHY_CONTACT_LIST_CAN_BLOCK
;
1380 tp_contact_list_set_blocked (EmpathyContactList
*list
,
1381 EmpathyContact
*contact
,
1385 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
1386 TpHandle handle
= empathy_contact_get_handle (contact
);
1387 GArray handles
= { (char *) &handle
, 1 };
1389 g_return_if_fail (TP_IS_CHANNEL (priv
->deny
));
1391 if (blocked
&& abusive
) {
1392 /* we have to do this via the new interface */
1393 g_return_if_fail (priv
->flags
&
1394 EMPATHY_CONTACT_LIST_CAN_REPORT_ABUSIVE
);
1396 tp_cli_connection_interface_contact_blocking_call_block_contacts (
1397 priv
->connection
, -1,
1398 &handles
, TRUE
, NULL
, NULL
, NULL
, NULL
);
1399 } else if (blocked
) {
1400 tp_cli_channel_interface_group_call_add_members (
1402 &handles
, NULL
, NULL
, NULL
, NULL
, NULL
);
1404 tp_cli_channel_interface_group_call_remove_members (
1406 &handles
, NULL
, NULL
, NULL
, NULL
, NULL
);
1411 tp_contact_list_get_blocked (EmpathyContactList
*list
,
1412 EmpathyContact
*contact
)
1414 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
1416 g_return_val_if_fail (TP_IS_CHANNEL (priv
->deny
), FALSE
);
1418 return tp_intset_is_member (tp_channel_group_get_members (priv
->deny
),
1419 empathy_contact_get_handle (contact
));
1423 tp_contact_list_iface_init (EmpathyContactListIface
*iface
)
1425 iface
->add
= tp_contact_list_add
;
1426 iface
->remove
= tp_contact_list_remove
;
1427 iface
->get_members
= tp_contact_list_get_members
;
1428 iface
->get_pendings
= tp_contact_list_get_pendings
;
1429 iface
->get_all_groups
= tp_contact_list_get_all_groups
;
1430 iface
->get_groups
= tp_contact_list_get_groups
;
1431 iface
->add_to_group
= tp_contact_list_add_to_group
;
1432 iface
->remove_from_group
= tp_contact_list_remove_from_group
;
1433 iface
->rename_group
= tp_contact_list_rename_group
;
1434 iface
->remove_group
= tp_contact_list_remove_group
;
1435 iface
->get_flags
= tp_contact_list_get_flags
;
1436 iface
->set_blocked
= tp_contact_list_set_blocked
;
1437 iface
->get_blocked
= tp_contact_list_get_blocked
;
1441 empathy_tp_contact_list_remove_all (EmpathyTpContactList
*list
)
1443 EmpathyTpContactListPriv
*priv
= GET_PRIV (list
);
1444 GHashTableIter iter
;
1447 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list
));
1449 /* Remove all contacts */
1450 g_hash_table_iter_init (&iter
, priv
->members
);
1451 while (g_hash_table_iter_next (&iter
, NULL
, &contact
)) {
1452 g_signal_emit_by_name (list
, "members-changed", contact
,
1456 g_hash_table_remove_all (priv
->members
);
1458 g_hash_table_iter_init (&iter
, priv
->pendings
);
1459 while (g_hash_table_iter_next (&iter
, NULL
, &contact
)) {
1460 g_signal_emit_by_name (list
, "pendings-changed", contact
,
1464 g_hash_table_remove_all (priv
->pendings
);