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 "empathy-tp-contact-list.h"
35 #include "empathy-tp-contact-factory.h"
36 #include "empathy-contact-list.h"
37 #include "empathy-utils.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
40 #include "empathy-debug.h"
42 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactList)
44 EmpathyTpContactFactory *factory;
45 TpConnection *connection;
50 /* contact handle (TpHandle) => reffed (EmpathyContact *)
52 * Contacts which are members or remote-pending in the subscribe channel:
53 * we are receiving their presence or we asked to receive it. */
55 /* contact handle (TpHandle) => reffed (EmpathyContact *)
57 * Contacts which are local-pending in the publish channel but are NOT in
58 * the members hash table: they asked to receive our presence and we don't
59 * receive theirs or asked to.
60 * That's basically the contacts which asked to add us to their contact
61 * list and we didn't answer it. */
63 /* group name: borrowed (const gchar *) => reffed (TpChannel *) */
65 /* group name: owned (gchar *) => owned GArray of TpHandle */
66 GHashTable *add_to_group;
68 EmpathyContactListFlags flags;
70 TpProxySignalConnection *new_channels_sig;
71 } EmpathyTpContactListPriv;
74 TP_CONTACT_LIST_TYPE_PUBLISH,
75 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
76 TP_CONTACT_LIST_TYPE_UNKNOWN
79 static void tp_contact_list_iface_init (EmpathyContactListIface *iface);
86 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
87 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
88 tp_contact_list_iface_init));
91 tp_contact_list_forget_group (EmpathyTpContactList *list,
94 EmpathyTpContactListPriv *priv = GET_PRIV (list);
95 const TpIntSet *members;
97 const gchar *group_name;
99 group_name = tp_channel_get_identifier (channel);
101 /* Signal that all members are not in that group anymore */
102 members = tp_channel_group_get_members (channel);
103 tp_intset_iter_init (&iter, members);
104 while (tp_intset_iter_next (&iter)) {
105 EmpathyContact *contact;
107 contact = g_hash_table_lookup (priv->members,
108 GUINT_TO_POINTER (iter.element));
109 if (contact == NULL) {
113 DEBUG ("Contact %s (%d) removed from group %s",
114 empathy_contact_get_id (contact), iter.element,
116 g_signal_emit_by_name (list, "groups-changed", contact,
123 tp_contact_list_group_invalidated_cb (TpChannel *channel,
127 EmpathyTpContactList *list)
129 EmpathyTpContactListPriv *priv = GET_PRIV (list);
130 const gchar *group_name;
132 group_name = tp_channel_get_identifier (channel);
133 DEBUG ("Group %s invalidated. Message: %s", group_name, message);
135 tp_contact_list_forget_group (list, channel);
137 g_hash_table_remove (priv->groups, group_name);
141 contacts_added_to_group (EmpathyTpContactList *list,
145 EmpathyTpContactListPriv *priv = GET_PRIV (list);
146 const gchar *group_name;
149 group_name = tp_channel_get_identifier (channel);
151 for (i = 0; i < added->len; i++) {
152 EmpathyContact *contact;
155 handle = g_array_index (added, TpHandle, i);
156 contact = g_hash_table_lookup (priv->members,
157 GUINT_TO_POINTER (handle));
158 if (contact == NULL) {
162 DEBUG ("Contact %s (%d) added to group %s",
163 empathy_contact_get_id (contact), handle, group_name);
164 g_signal_emit_by_name (list, "groups-changed", contact,
171 tp_contact_list_group_members_changed_cb (TpChannel *channel,
175 GArray *local_pending,
176 GArray *remote_pending,
179 EmpathyTpContactList *list)
181 EmpathyTpContactListPriv *priv = GET_PRIV (list);
182 const gchar *group_name;
185 contacts_added_to_group (list, channel, added);
187 group_name = tp_channel_get_identifier (channel);
189 for (i = 0; i < removed->len; i++) {
190 EmpathyContact *contact;
193 handle = g_array_index (removed, TpHandle, i);
194 contact = g_hash_table_lookup (priv->members,
195 GUINT_TO_POINTER (handle));
196 if (contact == NULL) {
200 DEBUG ("Contact %s (%d) removed from group %s",
201 empathy_contact_get_id (contact), handle, group_name);
203 g_signal_emit_by_name (list, "groups-changed", contact,
210 tp_contact_list_group_ready_cb (TpChannel *channel,
214 EmpathyTpContactListPriv *priv = GET_PRIV (list);
215 TpChannel *old_group;
216 const gchar *group_name;
217 const TpIntSet *members;
221 DEBUG ("Error: %s", error->message);
222 g_object_unref (channel);
226 group_name = tp_channel_get_identifier (channel);
228 /* If there's already a group with this name in the table, we can't
229 * just let it be replaced. Replacing it causes it to be unreffed,
230 * which causes it to be invalidated (see
231 * <https://bugs.freedesktop.org/show_bug.cgi?id=22119>), which causes
232 * it to be removed from the hash table again, which causes it to be
235 old_group = g_hash_table_lookup (priv->groups, group_name);
237 if (old_group != NULL) {
238 DEBUG ("Discarding old group %s (%p)", group_name, old_group);
239 g_hash_table_steal (priv->groups, group_name);
240 tp_contact_list_forget_group (list, old_group);
241 g_object_unref (old_group);
244 g_hash_table_insert (priv->groups, (gpointer) group_name, channel);
245 DEBUG ("Group %s added", group_name);
247 g_signal_connect (channel, "group-members-changed",
248 G_CALLBACK (tp_contact_list_group_members_changed_cb),
251 g_signal_connect (channel, "invalidated",
252 G_CALLBACK (tp_contact_list_group_invalidated_cb),
255 if (priv->add_to_group) {
258 handles = g_hash_table_lookup (priv->add_to_group, group_name);
260 DEBUG ("Adding initial members to group %s", group_name);
261 tp_cli_channel_interface_group_call_add_members (channel,
262 -1, handles, NULL, NULL, NULL, NULL, NULL);
263 g_hash_table_remove (priv->add_to_group, group_name);
267 /* Get initial members of the group */
268 members = tp_channel_group_get_members (channel);
269 g_assert (members != NULL);
270 arr = tp_intset_to_array (members);
271 contacts_added_to_group (list, channel, arr);
272 g_array_free (arr, TRUE);
276 tp_contact_list_group_add_channel (EmpathyTpContactList *list,
277 const gchar *object_path,
278 const gchar *channel_type,
279 TpHandleType handle_type,
282 EmpathyTpContactListPriv *priv = GET_PRIV (list);
285 /* Only accept server-side contact groups */
286 if (tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) ||
287 handle_type != TP_HANDLE_TYPE_GROUP) {
291 channel = tp_channel_new (priv->connection,
292 object_path, channel_type,
293 handle_type, handle, NULL);
295 /* Give the ref to the callback */
296 tp_channel_call_when_ready (channel,
297 tp_contact_list_group_ready_cb,
302 tp_contact_list_group_request_channel_cb (TpConnection *connection,
303 const gchar *object_path,
308 /* The new channel will be handled in NewChannel cb. Here we only
309 * handle the error if RequestChannel failed */
311 DEBUG ("Error: %s", error->message);
317 tp_contact_list_group_request_handles_cb (TpConnection *connection,
318 const GArray *handles,
323 TpHandle channel_handle;
326 DEBUG ("Error: %s", error->message);
330 channel_handle = g_array_index (handles, TpHandle, 0);
331 tp_cli_connection_call_request_channel (connection, -1,
332 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
333 TP_HANDLE_TYPE_GROUP,
336 tp_contact_list_group_request_channel_cb,
341 /* This function takes ownership of handles array */
343 tp_contact_list_group_add (EmpathyTpContactList *list,
344 const gchar *group_name,
347 EmpathyTpContactListPriv *priv = GET_PRIV (list);
349 const gchar *names[] = {group_name, NULL};
351 /* Search the channel for that group name */
352 channel = g_hash_table_lookup (priv->groups, group_name);
354 tp_cli_channel_interface_group_call_add_members (channel, -1,
355 handles, NULL, NULL, NULL, NULL, NULL);
356 g_array_free (handles, TRUE);
360 /* That group does not exist yet, we have to:
361 * 1) Request an handle for the group name
362 * 2) Request a channel
363 * 3) When NewChannel is emitted, add handles in members
365 g_hash_table_insert (priv->add_to_group,
366 g_strdup (group_name),
368 tp_cli_connection_call_request_handles (priv->connection, -1,
369 TP_HANDLE_TYPE_GROUP, names,
370 tp_contact_list_group_request_handles_cb,
376 tp_contact_list_got_added_members_cb (EmpathyTpContactFactory *factory,
378 EmpathyContact * const * contacts,
380 const TpHandle *failed,
385 EmpathyTpContactListPriv *priv = GET_PRIV (list);
389 DEBUG ("Error: %s", error->message);
393 for (i = 0; i < n_contacts; i++) {
394 EmpathyContact *contact = contacts[i];
397 handle = empathy_contact_get_handle (contact);
398 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
401 /* Add to the list and emit signal */
402 g_hash_table_insert (priv->members, GUINT_TO_POINTER (handle),
403 g_object_ref (contact));
404 g_signal_emit_by_name (list, "members-changed", contact,
407 /* This contact is now member, implicitly accept pending. */
408 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
409 GArray handles = {(gchar *) &handle, 1};
411 tp_cli_channel_interface_group_call_add_members (priv->publish,
412 -1, &handles, NULL, NULL, NULL, NULL, NULL);
418 tp_contact_list_got_local_pending_cb (EmpathyTpContactFactory *factory,
420 EmpathyContact * const * contacts,
422 const TpHandle *failed,
427 EmpathyTpContactListPriv *priv = GET_PRIV (list);
431 DEBUG ("Error: %s", error->message);
435 for (i = 0; i < n_contacts; i++) {
436 EmpathyContact *contact = contacts[i];
438 const gchar *message;
439 TpChannelGroupChangeReason reason;
441 handle = empathy_contact_get_handle (contact);
442 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle))) {
443 GArray handles = {(gchar *) &handle, 1};
445 /* This contact is already member, auto accept. */
446 tp_cli_channel_interface_group_call_add_members (priv->publish,
447 -1, &handles, NULL, NULL, NULL, NULL, NULL);
449 else if (tp_channel_group_get_local_pending_info (priv->publish,
454 /* Add contact to pendings */
455 g_hash_table_insert (priv->pendings, GUINT_TO_POINTER (handle),
456 g_object_ref (contact));
457 g_signal_emit_by_name (list, "pendings-changed", contact,
458 contact, reason, message, TRUE);
464 tp_contact_list_remove_handle (EmpathyTpContactList *list,
468 EmpathyTpContactListPriv *priv = GET_PRIV (list);
469 EmpathyContact *contact;
472 if (table == priv->pendings)
473 sig = "pendings-changed";
474 else if (table == priv->members)
475 sig = "members-changed";
479 contact = g_hash_table_lookup (table, GUINT_TO_POINTER (handle));
481 g_object_ref (contact);
482 g_hash_table_remove (table, GUINT_TO_POINTER (handle));
483 g_signal_emit_by_name (list, sig, contact, 0, 0, NULL,
485 g_object_unref (contact);
490 tp_contact_list_publish_group_members_changed_cb (TpChannel *channel,
494 GArray *local_pending,
495 GArray *remote_pending,
497 TpChannelGroupChangeReason reason,
498 EmpathyTpContactList *list)
500 EmpathyTpContactListPriv *priv = GET_PRIV (list);
503 /* We now send our presence to those contacts, remove them from pendings */
504 for (i = 0; i < added->len; i++) {
505 tp_contact_list_remove_handle (list, priv->pendings,
506 g_array_index (added, TpHandle, i));
509 /* We refuse to send our presence to those contacts, remove from pendings */
510 for (i = 0; i < removed->len; i++) {
511 tp_contact_list_remove_handle (list, priv->pendings,
512 g_array_index (removed, TpHandle, i));
515 /* Those contacts want our presence, auto accept those that are already
516 * member, otherwise add in pendings. */
517 if (local_pending->len > 0) {
518 empathy_tp_contact_factory_get_from_handles (priv->factory,
519 local_pending->len, (TpHandle *) local_pending->data,
520 tp_contact_list_got_local_pending_cb, NULL, NULL,
526 tp_contact_list_get_alias_flags_cb (TpConnection *connection,
532 EmpathyTpContactListPriv *priv = GET_PRIV (list);
535 DEBUG ("Error: %s", error->message);
539 if (flags & TP_CONNECTION_ALIAS_FLAG_USER_SET) {
540 priv->flags |= EMPATHY_CONTACT_LIST_CAN_ALIAS;
545 tp_contact_list_get_requestablechannelclasses_cb (TpProxy *connection,
551 EmpathyTpContactListPriv *priv = GET_PRIV (list);
556 DEBUG ("Error: %s", error->message);
560 classes = g_value_get_boxed (value);
561 for (i = 0; i < classes->len; i++) {
562 GValueArray *class = g_ptr_array_index (classes, i);
564 const char *channel_type;
567 props = g_value_get_boxed (g_value_array_get_nth (class, 0));
569 channel_type = tp_asv_get_string (props,
570 TP_IFACE_CHANNEL ".ChannelType");
571 handle_type = tp_asv_get_uint32 (props,
572 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
574 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) &&
575 handle_type == TP_HANDLE_TYPE_GROUP) {
576 DEBUG ("Got channel class for a contact group");
577 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
584 tp_contact_list_subscribe_group_members_changed_cb (TpChannel *channel,
588 GArray *local_pending,
589 GArray *remote_pending,
592 EmpathyTpContactList *list)
594 EmpathyTpContactListPriv *priv = GET_PRIV (list);
597 /* We now get the presence of those contacts, add them to members */
598 if (added->len > 0) {
599 empathy_tp_contact_factory_get_from_handles (priv->factory,
600 added->len, (TpHandle *) added->data,
601 tp_contact_list_got_added_members_cb, NULL, NULL,
605 /* Those contacts refuse to send us their presence, remove from members. */
606 for (i = 0; i < removed->len; i++) {
607 tp_contact_list_remove_handle (list, priv->members,
608 g_array_index (removed, TpHandle, i));
611 /* We want those contacts in our contact list but we don't get their
612 * presence yet. Add to members anyway. */
613 if (remote_pending->len > 0) {
614 empathy_tp_contact_factory_get_from_handles (priv->factory,
615 remote_pending->len, (TpHandle *) remote_pending->data,
616 tp_contact_list_got_added_members_cb, NULL, NULL,
622 tp_contact_list_new_channel_cb (TpConnection *proxy,
623 const gchar *object_path,
624 const gchar *channel_type,
627 gboolean suppress_handler,
631 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
632 object_path, channel_type,
633 handle_type, handle);
637 tp_contact_list_list_channels_cb (TpConnection *connection,
638 const GPtrArray *channels,
646 DEBUG ("Error: %s", error->message);
650 for (i = 0; i < channels->len; i++) {
651 GValueArray *chan_struct;
652 const gchar *object_path;
653 const gchar *channel_type;
654 TpHandleType handle_type;
657 chan_struct = g_ptr_array_index (channels, i);
658 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
659 channel_type = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
660 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
661 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
663 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
664 object_path, channel_type,
665 handle_type, handle);
670 tp_contact_list_finalize (GObject *object)
672 EmpathyTpContactListPriv *priv;
673 EmpathyTpContactList *list;
677 list = EMPATHY_TP_CONTACT_LIST (object);
678 priv = GET_PRIV (list);
680 DEBUG ("finalize: %p", object);
682 if (priv->subscribe) {
683 g_object_unref (priv->subscribe);
686 g_object_unref (priv->publish);
689 g_object_unref (priv->stored);
692 if (priv->connection) {
693 g_object_unref (priv->connection);
697 g_object_unref (priv->factory);
700 g_hash_table_iter_init (&iter, priv->groups);
701 while (g_hash_table_iter_next (&iter, NULL, &channel)) {
702 g_signal_handlers_disconnect_by_func (channel,
703 tp_contact_list_group_invalidated_cb, list);
706 g_hash_table_destroy (priv->groups);
707 g_hash_table_destroy (priv->members);
708 g_hash_table_destroy (priv->pendings);
709 g_hash_table_destroy (priv->add_to_group);
711 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
715 received_all_list_channels (EmpathyTpContactList *self)
717 EmpathyTpContactListPriv *priv = GET_PRIV (self);
719 return (priv->stored != NULL && priv->publish != NULL &&
720 priv->subscribe != NULL);
724 got_list_channel (EmpathyTpContactList *list,
727 EmpathyTpContactListPriv *priv = GET_PRIV (list);
730 /* We requested that channel by providing TargetID property, so it's
731 * guaranteed that tp_channel_get_identifier will return it. */
732 id = tp_channel_get_identifier (channel);
734 /* TpChannel emits initial set of members just before being ready */
735 if (!tp_strdiff (id, "stored")) {
736 if (priv->stored != NULL)
738 priv->stored = g_object_ref (channel);
739 } else if (!tp_strdiff (id, "publish")) {
740 if (priv->publish != NULL)
742 priv->publish = g_object_ref (channel);
743 g_signal_connect (priv->publish, "group-members-changed",
744 G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
746 } else if (!tp_strdiff (id, "subscribe")) {
747 if (priv->subscribe != NULL)
749 priv->subscribe = g_object_ref (channel);
750 g_signal_connect (priv->subscribe, "group-members-changed",
751 G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
755 if (received_all_list_channels (list) && priv->new_channels_sig != NULL) {
756 /* We don't need to watch NewChannels anymore */
757 tp_proxy_signal_connection_disconnect (priv->new_channels_sig);
758 priv->new_channels_sig = NULL;
763 list_ensure_channel_cb (TpConnection *conn,
766 GHashTable *properties,
769 GObject *weak_object)
771 EmpathyTpContactList *list = user_data;
775 DEBUG ("failed: %s\n", error->message);
779 channel = tp_channel_new_from_properties (conn, path, properties, NULL);
780 got_list_channel (list, channel);
781 g_object_unref (channel);
785 new_channels_cb (TpConnection *conn,
786 const GPtrArray *channels,
788 GObject *weak_object)
790 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
793 for (i = 0; i < channels->len ; i++) {
794 GValueArray *arr = g_ptr_array_index (channels, i);
796 GHashTable *properties;
800 path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
801 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
803 if (tp_strdiff (tp_asv_get_string (properties,
804 TP_IFACE_CHANNEL ".ChannelType"),
805 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST))
808 if (tp_asv_get_uint32 (properties,
809 TP_IFACE_CHANNEL ".TargetHandleType", NULL)
810 != TP_HANDLE_TYPE_LIST)
813 id = tp_asv_get_string (properties,
814 TP_IFACE_CHANNEL ".TargetID");
818 channel = tp_channel_new_from_properties (conn, path,
820 got_list_channel (list, channel);
821 g_object_unref (channel);
826 conn_ready_cb (TpConnection *connection,
830 EmpathyTpContactList *list = data;
831 EmpathyTpContactListPriv *priv = GET_PRIV (list);
835 DEBUG ("failed: %s", error->message);
839 request = tp_asv_new (
840 TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
841 TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_UINT, TP_HANDLE_TYPE_LIST,
844 /* Watch the NewChannels signal so if ensuring list channels fails (for
845 * example because the server is slow and the D-Bus call timeouts before CM
846 * fetches the roster), we have a chance to get them later. */
847 priv->new_channels_sig =
848 tp_cli_connection_interface_requests_connect_to_new_channels (
849 priv->connection, new_channels_cb, NULL, NULL, G_OBJECT (list), NULL);
851 /* Request the 'stored' list. */
852 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "stored");
853 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
854 -1, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
856 /* Request the 'publish' list. */
857 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "publish");
858 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
859 -1, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
861 /* Request the 'subscribe' list. */
862 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "subscribe");
863 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
864 -1, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
866 g_hash_table_unref (request);
868 g_object_unref (list);
872 tp_contact_list_constructed (GObject *list)
874 EmpathyTpContactListPriv *priv = GET_PRIV (list);
876 priv->factory = empathy_tp_contact_factory_dup_singleton (priv->connection);
878 /* call GetAliasFlags */
879 if (tp_proxy_has_interface_by_id (priv->connection,
880 TP_IFACE_QUARK_CONNECTION_INTERFACE_ALIASING)) {
881 tp_cli_connection_interface_aliasing_call_get_alias_flags (
884 tp_contact_list_get_alias_flags_cb,
889 /* lookup RequestableChannelClasses */
890 if (tp_proxy_has_interface_by_id (priv->connection,
891 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS)) {
892 tp_cli_dbus_properties_call_get (priv->connection,
894 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
895 "RequestableChannelClasses",
896 tp_contact_list_get_requestablechannelclasses_cb,
900 /* we just don't know... better mark the flag just in case */
901 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
904 tp_connection_call_when_ready (priv->connection, conn_ready_cb,
905 g_object_ref (list));
907 tp_cli_connection_call_list_channels (priv->connection, -1,
908 tp_contact_list_list_channels_cb,
912 tp_cli_connection_connect_to_new_channel (priv->connection,
913 tp_contact_list_new_channel_cb,
919 tp_contact_list_get_property (GObject *object,
924 EmpathyTpContactListPriv *priv = GET_PRIV (object);
927 case PROP_CONNECTION:
928 g_value_set_object (value, priv->connection);
931 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
937 tp_contact_list_set_property (GObject *object,
942 EmpathyTpContactListPriv *priv = GET_PRIV (object);
945 case PROP_CONNECTION:
946 priv->connection = g_value_dup_object (value);
949 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
955 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
957 GObjectClass *object_class = G_OBJECT_CLASS (klass);
959 object_class->finalize = tp_contact_list_finalize;
960 object_class->constructed = tp_contact_list_constructed;
961 object_class->get_property = tp_contact_list_get_property;
962 object_class->set_property = tp_contact_list_set_property;
964 g_object_class_install_property (object_class,
966 g_param_spec_object ("connection",
968 "The connection associated with the contact list",
971 G_PARAM_CONSTRUCT_ONLY));
973 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
977 tp_contact_list_array_free (gpointer handles)
979 g_array_free (handles, TRUE);
983 empathy_tp_contact_list_init (EmpathyTpContactList *list)
985 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
986 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
990 /* Map group's name to group's TpChannel. The group name string is owned
991 * by the TpChannel object */
992 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
994 (GDestroyNotify) g_object_unref);
996 /* Map contact's handle to EmpathyContact object */
997 priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
999 (GDestroyNotify) g_object_unref);
1001 /* Map contact's handle to EmpathyContact object */
1002 priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1004 (GDestroyNotify) g_object_unref);
1006 /* Map group's name to GArray of handle */
1007 priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
1009 tp_contact_list_array_free);
1012 EmpathyTpContactList *
1013 empathy_tp_contact_list_new (TpConnection *connection)
1015 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
1016 "connection", connection,
1021 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
1023 EmpathyTpContactListPriv *priv;
1025 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
1027 priv = GET_PRIV (list);
1029 return priv->connection;
1033 tp_contact_list_add (EmpathyContactList *list,
1034 EmpathyContact *contact,
1035 const gchar *message)
1037 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1039 GArray handles = {(gchar *) &handle, 1};
1041 handle = empathy_contact_get_handle (contact);
1042 if (priv->subscribe) {
1043 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
1044 -1, &handles, message, NULL, NULL, NULL, NULL);
1046 if (priv->publish) {
1047 TpChannelGroupFlags flags = tp_channel_group_get_flags (priv->subscribe);
1048 if (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD ||
1049 g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
1050 tp_cli_channel_interface_group_call_add_members (priv->publish,
1051 -1, &handles, message, NULL, NULL, NULL, NULL);
1057 tp_contact_list_remove (EmpathyContactList *list,
1058 EmpathyContact *contact,
1059 const gchar *message)
1061 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1063 GArray handles = {(gchar *) &handle, 1};
1065 handle = empathy_contact_get_handle (contact);
1067 /* FIXME: this is racy if tp_contact_list_remove is called before the
1068 * 'stored' list has been retrieved. */
1069 if (priv->stored != NULL) {
1070 tp_cli_channel_interface_group_call_remove_members (priv->stored,
1071 -1, &handles, message, NULL, NULL, NULL, NULL);
1074 if (priv->subscribe) {
1075 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
1076 -1, &handles, message, NULL, NULL, NULL, NULL);
1078 if (priv->publish) {
1079 tp_cli_channel_interface_group_call_remove_members (priv->publish,
1080 -1, &handles, message, NULL, NULL, NULL, NULL);
1085 tp_contact_list_get_members (EmpathyContactList *list)
1087 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1090 ret = g_hash_table_get_values (priv->members);
1091 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1096 tp_contact_list_get_pendings (EmpathyContactList *list)
1098 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1101 ret = g_hash_table_get_values (priv->pendings);
1102 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1107 tp_contact_list_get_all_groups (EmpathyContactList *list)
1109 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1112 ret = g_hash_table_get_keys (priv->groups);
1113 for (l = ret; l; l = l->next) {
1114 l->data = g_strdup (l->data);
1121 tp_contact_list_get_groups (EmpathyContactList *list,
1122 EmpathyContact *contact)
1124 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1126 GHashTableIter iter;
1127 gpointer group_name;
1131 handle = empathy_contact_get_handle (contact);
1132 g_hash_table_iter_init (&iter, priv->groups);
1133 while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
1134 const TpIntSet *members;
1136 members = tp_channel_group_get_members (channel);
1137 if (tp_intset_is_member (members, handle)) {
1138 ret = g_list_prepend (ret, g_strdup (group_name));
1146 tp_contact_list_add_to_group (EmpathyContactList *list,
1147 EmpathyContact *contact,
1148 const gchar *group_name)
1153 handle = empathy_contact_get_handle (contact);
1154 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1155 g_array_append_val (handles, handle);
1156 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1157 group_name, handles);
1161 tp_contact_list_remove_from_group (EmpathyContactList *list,
1162 EmpathyContact *contact,
1163 const gchar *group_name)
1165 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1168 GArray handles = {(gchar *) &handle, 1};
1170 channel = g_hash_table_lookup (priv->groups, group_name);
1171 if (channel == NULL) {
1175 handle = empathy_contact_get_handle (contact);
1176 DEBUG ("remove contact %s (%d) from group %s",
1177 empathy_contact_get_id (contact), handle, group_name);
1179 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1180 &handles, NULL, NULL, NULL, NULL, NULL);
1184 tp_contact_list_rename_group (EmpathyContactList *list,
1185 const gchar *old_group_name,
1186 const gchar *new_group_name)
1188 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1190 const TpIntSet *members;
1193 channel = g_hash_table_lookup (priv->groups, old_group_name);
1194 if (channel == NULL) {
1198 DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1200 /* Remove all members and close the old channel */
1201 members = tp_channel_group_get_members (channel);
1202 handles = tp_intset_to_array (members);
1203 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1204 handles, NULL, NULL, NULL, NULL, NULL);
1205 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1207 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1208 new_group_name, handles);
1212 tp_contact_list_remove_group (EmpathyContactList *list,
1213 const gchar *group_name)
1215 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1217 const TpIntSet *members;
1220 channel = g_hash_table_lookup (priv->groups, group_name);
1221 if (channel == NULL) {
1225 DEBUG ("remove group %s", group_name);
1227 /* Remove all members and close the channel */
1228 members = tp_channel_group_get_members (channel);
1229 handles = tp_intset_to_array (members);
1230 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1231 handles, NULL, NULL, NULL, NULL, NULL);
1232 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1233 g_array_free (handles, TRUE);
1236 static EmpathyContactListFlags
1237 tp_contact_list_get_flags (EmpathyContactList *list)
1239 EmpathyTpContactListPriv *priv;
1240 EmpathyContactListFlags flags;
1242 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1244 priv = GET_PRIV (list);
1245 flags = priv->flags;
1247 if (priv->subscribe != NULL) {
1248 TpChannelGroupFlags group_flags;
1250 group_flags = tp_channel_group_get_flags (priv->subscribe);
1252 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1253 flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1256 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1257 flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1265 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1267 iface->add = tp_contact_list_add;
1268 iface->remove = tp_contact_list_remove;
1269 iface->get_members = tp_contact_list_get_members;
1270 iface->get_pendings = tp_contact_list_get_pendings;
1271 iface->get_all_groups = tp_contact_list_get_all_groups;
1272 iface->get_groups = tp_contact_list_get_groups;
1273 iface->add_to_group = tp_contact_list_add_to_group;
1274 iface->remove_from_group = tp_contact_list_remove_from_group;
1275 iface->rename_group = tp_contact_list_rename_group;
1276 iface->remove_group = tp_contact_list_remove_group;
1277 iface->get_flags = tp_contact_list_get_flags;
1281 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1283 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1284 GHashTableIter iter;
1287 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1289 /* Remove all contacts */
1290 g_hash_table_iter_init (&iter, priv->members);
1291 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1292 g_signal_emit_by_name (list, "members-changed", contact,
1296 g_hash_table_remove_all (priv->members);
1298 g_hash_table_iter_init (&iter, priv->pendings);
1299 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1300 g_signal_emit_by_name (list, "pendings-changed", contact,
1304 g_hash_table_remove_all (priv->pendings);