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 add_to_members (EmpathyTpContactList *list,
377 EmpathyContact *contact)
379 EmpathyTpContactListPriv *priv = GET_PRIV (list);
382 handle = empathy_contact_get_handle (contact);
383 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
386 /* Add to the list and emit signal */
387 g_hash_table_insert (priv->members, GUINT_TO_POINTER (handle),
388 g_object_ref (contact));
389 g_signal_emit_by_name (list, "members-changed", contact,
394 tp_contact_list_got_added_members_cb (EmpathyTpContactFactory *factory,
396 EmpathyContact * const * contacts,
398 const TpHandle *failed,
403 EmpathyTpContactListPriv *priv = GET_PRIV (list);
407 DEBUG ("Error: %s", error->message);
411 for (i = 0; i < n_contacts; i++) {
412 EmpathyContact *contact = contacts[i];
415 add_to_members (EMPATHY_TP_CONTACT_LIST (list), contact);
417 handle = empathy_contact_get_handle (contact);
418 /* This contact is now member, implicitly accept pending. */
419 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
420 GArray handles = {(gchar *) &handle, 1};
422 tp_cli_channel_interface_group_call_add_members (priv->publish,
423 -1, &handles, NULL, NULL, NULL, NULL, NULL);
429 tp_contact_list_got_local_pending_cb (EmpathyTpContactFactory *factory,
431 EmpathyContact * const * contacts,
433 const TpHandle *failed,
438 EmpathyTpContactListPriv *priv = GET_PRIV (list);
442 DEBUG ("Error: %s", error->message);
446 for (i = 0; i < n_contacts; i++) {
447 EmpathyContact *contact = contacts[i];
449 const gchar *message;
450 TpChannelGroupChangeReason reason;
452 handle = empathy_contact_get_handle (contact);
453 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle))) {
454 GArray handles = {(gchar *) &handle, 1};
456 /* This contact is already member, auto accept. */
457 tp_cli_channel_interface_group_call_add_members (priv->publish,
458 -1, &handles, NULL, NULL, NULL, NULL, NULL);
460 else if (tp_channel_group_get_local_pending_info (priv->publish,
465 /* Add contact to pendings */
466 g_hash_table_insert (priv->pendings, GUINT_TO_POINTER (handle),
467 g_object_ref (contact));
468 g_signal_emit_by_name (list, "pendings-changed", contact,
469 contact, reason, message, TRUE);
475 tp_contact_list_remove_handle (EmpathyTpContactList *list,
479 EmpathyTpContactListPriv *priv = GET_PRIV (list);
480 EmpathyContact *contact;
483 if (table == priv->pendings)
484 sig = "pendings-changed";
485 else if (table == priv->members)
486 sig = "members-changed";
490 contact = g_hash_table_lookup (table, GUINT_TO_POINTER (handle));
492 g_object_ref (contact);
493 g_hash_table_remove (table, GUINT_TO_POINTER (handle));
494 g_signal_emit_by_name (list, sig, contact, 0, 0, NULL,
496 g_object_unref (contact);
501 tp_contact_list_publish_group_members_changed_cb (TpChannel *channel,
505 GArray *local_pending,
506 GArray *remote_pending,
508 TpChannelGroupChangeReason reason,
509 EmpathyTpContactList *list)
511 EmpathyTpContactListPriv *priv = GET_PRIV (list);
514 /* We now send our presence to those contacts, remove them from pendings */
515 for (i = 0; i < added->len; i++) {
516 tp_contact_list_remove_handle (list, priv->pendings,
517 g_array_index (added, TpHandle, i));
520 /* We refuse to send our presence to those contacts, remove from pendings */
521 for (i = 0; i < removed->len; i++) {
522 tp_contact_list_remove_handle (list, priv->pendings,
523 g_array_index (removed, TpHandle, i));
526 /* Those contacts want our presence, auto accept those that are already
527 * member, otherwise add in pendings. */
528 if (local_pending->len > 0) {
529 empathy_tp_contact_factory_get_from_handles (priv->factory,
530 local_pending->len, (TpHandle *) local_pending->data,
531 tp_contact_list_got_local_pending_cb, NULL, NULL,
537 tp_contact_list_get_alias_flags_cb (TpConnection *connection,
543 EmpathyTpContactListPriv *priv = GET_PRIV (list);
546 DEBUG ("Error: %s", error->message);
550 if (flags & TP_CONNECTION_ALIAS_FLAG_USER_SET) {
551 priv->flags |= EMPATHY_CONTACT_LIST_CAN_ALIAS;
556 tp_contact_list_get_requestablechannelclasses_cb (TpProxy *connection,
562 EmpathyTpContactListPriv *priv = GET_PRIV (list);
567 DEBUG ("Error: %s", error->message);
571 classes = g_value_get_boxed (value);
572 for (i = 0; i < classes->len; i++) {
573 GValueArray *class = g_ptr_array_index (classes, i);
575 const char *channel_type;
578 props = g_value_get_boxed (g_value_array_get_nth (class, 0));
580 channel_type = tp_asv_get_string (props,
581 TP_IFACE_CHANNEL ".ChannelType");
582 handle_type = tp_asv_get_uint32 (props,
583 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
585 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) &&
586 handle_type == TP_HANDLE_TYPE_GROUP) {
587 DEBUG ("Got channel class for a contact group");
588 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
595 tp_contact_list_subscribe_group_members_changed_cb (TpChannel *channel,
599 GArray *local_pending,
600 GArray *remote_pending,
603 EmpathyTpContactList *list)
605 EmpathyTpContactListPriv *priv = GET_PRIV (list);
608 /* We now get the presence of those contacts, add them to members */
609 if (added->len > 0) {
610 empathy_tp_contact_factory_get_from_handles (priv->factory,
611 added->len, (TpHandle *) added->data,
612 tp_contact_list_got_added_members_cb, NULL, NULL,
616 /* Those contacts refuse to send us their presence, remove from members. */
617 for (i = 0; i < removed->len; i++) {
618 tp_contact_list_remove_handle (list, priv->members,
619 g_array_index (removed, TpHandle, i));
622 /* We want those contacts in our contact list but we don't get their
623 * presence yet. Add to members anyway. */
624 if (remote_pending->len > 0) {
625 empathy_tp_contact_factory_get_from_handles (priv->factory,
626 remote_pending->len, (TpHandle *) remote_pending->data,
627 tp_contact_list_got_added_members_cb, NULL, NULL,
633 tp_contact_list_new_channel_cb (TpConnection *proxy,
634 const gchar *object_path,
635 const gchar *channel_type,
638 gboolean suppress_handler,
642 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
643 object_path, channel_type,
644 handle_type, handle);
648 tp_contact_list_list_channels_cb (TpConnection *connection,
649 const GPtrArray *channels,
657 DEBUG ("Error: %s", error->message);
661 for (i = 0; i < channels->len; i++) {
662 GValueArray *chan_struct;
663 const gchar *object_path;
664 const gchar *channel_type;
665 TpHandleType handle_type;
668 chan_struct = g_ptr_array_index (channels, i);
669 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
670 channel_type = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
671 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
672 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
674 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
675 object_path, channel_type,
676 handle_type, handle);
681 tp_contact_list_finalize (GObject *object)
683 EmpathyTpContactListPriv *priv;
684 EmpathyTpContactList *list;
688 list = EMPATHY_TP_CONTACT_LIST (object);
689 priv = GET_PRIV (list);
691 DEBUG ("finalize: %p", object);
693 if (priv->subscribe) {
694 g_object_unref (priv->subscribe);
697 g_object_unref (priv->publish);
700 g_object_unref (priv->stored);
703 if (priv->connection) {
704 g_object_unref (priv->connection);
708 g_object_unref (priv->factory);
711 g_hash_table_iter_init (&iter, priv->groups);
712 while (g_hash_table_iter_next (&iter, NULL, &channel)) {
713 g_signal_handlers_disconnect_by_func (channel,
714 tp_contact_list_group_invalidated_cb, list);
717 g_hash_table_destroy (priv->groups);
718 g_hash_table_destroy (priv->members);
719 g_hash_table_destroy (priv->pendings);
720 g_hash_table_destroy (priv->add_to_group);
722 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
726 received_all_list_channels (EmpathyTpContactList *self)
728 EmpathyTpContactListPriv *priv = GET_PRIV (self);
730 return (priv->stored != NULL && priv->publish != NULL &&
731 priv->subscribe != NULL);
735 got_list_channel (EmpathyTpContactList *list,
738 EmpathyTpContactListPriv *priv = GET_PRIV (list);
741 /* We requested that channel by providing TargetID property, so it's
742 * guaranteed that tp_channel_get_identifier will return it. */
743 id = tp_channel_get_identifier (channel);
745 /* TpChannel emits initial set of members just before being ready */
746 if (!tp_strdiff (id, "stored")) {
747 if (priv->stored != NULL)
749 priv->stored = g_object_ref (channel);
750 } else if (!tp_strdiff (id, "publish")) {
751 if (priv->publish != NULL)
753 priv->publish = g_object_ref (channel);
754 g_signal_connect (priv->publish, "group-members-changed",
755 G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
757 } else if (!tp_strdiff (id, "subscribe")) {
758 if (priv->subscribe != NULL)
760 priv->subscribe = g_object_ref (channel);
761 g_signal_connect (priv->subscribe, "group-members-changed",
762 G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
766 if (received_all_list_channels (list) && priv->new_channels_sig != NULL) {
767 /* We don't need to watch NewChannels anymore */
768 tp_proxy_signal_connection_disconnect (priv->new_channels_sig);
769 priv->new_channels_sig = NULL;
774 list_ensure_channel_cb (TpConnection *conn,
777 GHashTable *properties,
780 GObject *weak_object)
782 EmpathyTpContactList *list = user_data;
786 DEBUG ("failed: %s\n", error->message);
790 channel = tp_channel_new_from_properties (conn, path, properties, NULL);
791 got_list_channel (list, channel);
792 g_object_unref (channel);
796 new_channels_cb (TpConnection *conn,
797 const GPtrArray *channels,
799 GObject *weak_object)
801 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
804 for (i = 0; i < channels->len ; i++) {
805 GValueArray *arr = g_ptr_array_index (channels, i);
807 GHashTable *properties;
811 path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
812 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
814 if (tp_strdiff (tp_asv_get_string (properties,
815 TP_IFACE_CHANNEL ".ChannelType"),
816 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST))
819 if (tp_asv_get_uint32 (properties,
820 TP_IFACE_CHANNEL ".TargetHandleType", NULL)
821 != TP_HANDLE_TYPE_LIST)
824 id = tp_asv_get_string (properties,
825 TP_IFACE_CHANNEL ".TargetID");
829 channel = tp_channel_new_from_properties (conn, path,
831 got_list_channel (list, channel);
832 g_object_unref (channel);
837 conn_ready_cb (TpConnection *connection,
841 EmpathyTpContactList *list = data;
842 EmpathyTpContactListPriv *priv = GET_PRIV (list);
846 DEBUG ("failed: %s", error->message);
850 request = tp_asv_new (
851 TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
852 TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_UINT, TP_HANDLE_TYPE_LIST,
855 /* Watch the NewChannels signal so if ensuring list channels fails (for
856 * example because the server is slow and the D-Bus call timeouts before CM
857 * fetches the roster), we have a chance to get them later. */
858 priv->new_channels_sig =
859 tp_cli_connection_interface_requests_connect_to_new_channels (
860 priv->connection, new_channels_cb, NULL, NULL, G_OBJECT (list), NULL);
862 /* Request the 'stored' list. */
863 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "stored");
864 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
865 -1, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
867 /* Request the 'publish' list. */
868 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "publish");
869 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
870 -1, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
872 /* Request the 'subscribe' list. */
873 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "subscribe");
874 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
875 -1, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
877 g_hash_table_unref (request);
879 g_object_unref (list);
883 tp_contact_list_constructed (GObject *list)
885 EmpathyTpContactListPriv *priv = GET_PRIV (list);
887 priv->factory = empathy_tp_contact_factory_dup_singleton (priv->connection);
889 /* call GetAliasFlags */
890 if (tp_proxy_has_interface_by_id (priv->connection,
891 TP_IFACE_QUARK_CONNECTION_INTERFACE_ALIASING)) {
892 tp_cli_connection_interface_aliasing_call_get_alias_flags (
895 tp_contact_list_get_alias_flags_cb,
900 /* lookup RequestableChannelClasses */
901 if (tp_proxy_has_interface_by_id (priv->connection,
902 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS)) {
903 tp_cli_dbus_properties_call_get (priv->connection,
905 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
906 "RequestableChannelClasses",
907 tp_contact_list_get_requestablechannelclasses_cb,
911 /* we just don't know... better mark the flag just in case */
912 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
915 tp_connection_call_when_ready (priv->connection, conn_ready_cb,
916 g_object_ref (list));
918 tp_cli_connection_call_list_channels (priv->connection, -1,
919 tp_contact_list_list_channels_cb,
923 tp_cli_connection_connect_to_new_channel (priv->connection,
924 tp_contact_list_new_channel_cb,
930 tp_contact_list_get_property (GObject *object,
935 EmpathyTpContactListPriv *priv = GET_PRIV (object);
938 case PROP_CONNECTION:
939 g_value_set_object (value, priv->connection);
942 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
948 tp_contact_list_set_property (GObject *object,
953 EmpathyTpContactListPriv *priv = GET_PRIV (object);
956 case PROP_CONNECTION:
957 priv->connection = g_value_dup_object (value);
960 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
966 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
968 GObjectClass *object_class = G_OBJECT_CLASS (klass);
970 object_class->finalize = tp_contact_list_finalize;
971 object_class->constructed = tp_contact_list_constructed;
972 object_class->get_property = tp_contact_list_get_property;
973 object_class->set_property = tp_contact_list_set_property;
975 g_object_class_install_property (object_class,
977 g_param_spec_object ("connection",
979 "The connection associated with the contact list",
982 G_PARAM_CONSTRUCT_ONLY));
984 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
988 tp_contact_list_array_free (gpointer handles)
990 g_array_free (handles, TRUE);
994 empathy_tp_contact_list_init (EmpathyTpContactList *list)
996 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
997 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
1001 /* Map group's name to group's TpChannel. The group name string is owned
1002 * by the TpChannel object */
1003 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1005 (GDestroyNotify) g_object_unref);
1007 /* Map contact's handle to EmpathyContact object */
1008 priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1010 (GDestroyNotify) g_object_unref);
1012 /* Map contact's handle to EmpathyContact object */
1013 priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1015 (GDestroyNotify) g_object_unref);
1017 /* Map group's name to GArray of handle */
1018 priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
1020 tp_contact_list_array_free);
1023 EmpathyTpContactList *
1024 empathy_tp_contact_list_new (TpConnection *connection)
1026 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
1027 "connection", connection,
1032 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
1034 EmpathyTpContactListPriv *priv;
1036 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
1038 priv = GET_PRIV (list);
1040 return priv->connection;
1044 tp_contact_list_add (EmpathyContactList *list,
1045 EmpathyContact *contact,
1046 const gchar *message)
1048 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1050 GArray handles = {(gchar *) &handle, 1};
1052 handle = empathy_contact_get_handle (contact);
1053 if (priv->subscribe) {
1054 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
1055 -1, &handles, message, NULL, NULL, NULL, NULL);
1057 if (priv->publish) {
1058 TpChannelGroupFlags flags = tp_channel_group_get_flags (priv->subscribe);
1059 if (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD ||
1060 g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
1061 tp_cli_channel_interface_group_call_add_members (priv->publish,
1062 -1, &handles, message, NULL, NULL, NULL, NULL);
1068 tp_contact_list_remove (EmpathyContactList *list,
1069 EmpathyContact *contact,
1070 const gchar *message)
1072 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1074 GArray handles = {(gchar *) &handle, 1};
1076 handle = empathy_contact_get_handle (contact);
1078 /* FIXME: this is racy if tp_contact_list_remove is called before the
1079 * 'stored' list has been retrieved. */
1080 if (priv->stored != NULL) {
1081 tp_cli_channel_interface_group_call_remove_members (priv->stored,
1082 -1, &handles, message, NULL, NULL, NULL, NULL);
1085 if (priv->subscribe) {
1086 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
1087 -1, &handles, message, NULL, NULL, NULL, NULL);
1089 if (priv->publish) {
1090 tp_cli_channel_interface_group_call_remove_members (priv->publish,
1091 -1, &handles, message, NULL, NULL, NULL, NULL);
1096 tp_contact_list_get_members (EmpathyContactList *list)
1098 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1101 ret = g_hash_table_get_values (priv->members);
1102 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1107 tp_contact_list_get_pendings (EmpathyContactList *list)
1109 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1112 ret = g_hash_table_get_values (priv->pendings);
1113 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1118 tp_contact_list_get_all_groups (EmpathyContactList *list)
1120 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1123 ret = g_hash_table_get_keys (priv->groups);
1124 for (l = ret; l; l = l->next) {
1125 l->data = g_strdup (l->data);
1132 tp_contact_list_get_groups (EmpathyContactList *list,
1133 EmpathyContact *contact)
1135 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1137 GHashTableIter iter;
1138 gpointer group_name;
1142 handle = empathy_contact_get_handle (contact);
1143 g_hash_table_iter_init (&iter, priv->groups);
1144 while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
1145 const TpIntSet *members;
1147 members = tp_channel_group_get_members (channel);
1148 if (tp_intset_is_member (members, handle)) {
1149 ret = g_list_prepend (ret, g_strdup (group_name));
1157 tp_contact_list_add_to_group (EmpathyContactList *list,
1158 EmpathyContact *contact,
1159 const gchar *group_name)
1164 handle = empathy_contact_get_handle (contact);
1165 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1166 g_array_append_val (handles, handle);
1167 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1168 group_name, handles);
1172 tp_contact_list_remove_from_group (EmpathyContactList *list,
1173 EmpathyContact *contact,
1174 const gchar *group_name)
1176 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1179 GArray handles = {(gchar *) &handle, 1};
1181 channel = g_hash_table_lookup (priv->groups, group_name);
1182 if (channel == NULL) {
1186 handle = empathy_contact_get_handle (contact);
1187 DEBUG ("remove contact %s (%d) from group %s",
1188 empathy_contact_get_id (contact), handle, group_name);
1190 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1191 &handles, NULL, NULL, NULL, NULL, NULL);
1195 tp_contact_list_rename_group (EmpathyContactList *list,
1196 const gchar *old_group_name,
1197 const gchar *new_group_name)
1199 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1201 const TpIntSet *members;
1204 channel = g_hash_table_lookup (priv->groups, old_group_name);
1205 if (channel == NULL) {
1209 DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1211 /* Remove all members and close the old channel */
1212 members = tp_channel_group_get_members (channel);
1213 handles = tp_intset_to_array (members);
1214 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1215 handles, NULL, NULL, NULL, NULL, NULL);
1216 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1218 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1219 new_group_name, handles);
1223 tp_contact_list_remove_group (EmpathyContactList *list,
1224 const gchar *group_name)
1226 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1228 const TpIntSet *members;
1231 channel = g_hash_table_lookup (priv->groups, group_name);
1232 if (channel == NULL) {
1236 DEBUG ("remove group %s", group_name);
1238 /* Remove all members and close the channel */
1239 members = tp_channel_group_get_members (channel);
1240 handles = tp_intset_to_array (members);
1241 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1242 handles, NULL, NULL, NULL, NULL, NULL);
1243 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1244 g_array_free (handles, TRUE);
1247 static EmpathyContactListFlags
1248 tp_contact_list_get_flags (EmpathyContactList *list)
1250 EmpathyTpContactListPriv *priv;
1251 EmpathyContactListFlags flags;
1253 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1255 priv = GET_PRIV (list);
1256 flags = priv->flags;
1258 if (priv->subscribe != NULL) {
1259 TpChannelGroupFlags group_flags;
1261 group_flags = tp_channel_group_get_flags (priv->subscribe);
1263 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1264 flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1267 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1268 flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1276 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1278 iface->add = tp_contact_list_add;
1279 iface->remove = tp_contact_list_remove;
1280 iface->get_members = tp_contact_list_get_members;
1281 iface->get_pendings = tp_contact_list_get_pendings;
1282 iface->get_all_groups = tp_contact_list_get_all_groups;
1283 iface->get_groups = tp_contact_list_get_groups;
1284 iface->add_to_group = tp_contact_list_add_to_group;
1285 iface->remove_from_group = tp_contact_list_remove_from_group;
1286 iface->rename_group = tp_contact_list_rename_group;
1287 iface->remove_group = tp_contact_list_remove_group;
1288 iface->get_flags = tp_contact_list_get_flags;
1292 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1294 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1295 GHashTableIter iter;
1298 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1300 /* Remove all contacts */
1301 g_hash_table_iter_init (&iter, priv->members);
1302 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1303 g_signal_emit_by_name (list, "members-changed", contact,
1307 g_hash_table_remove_all (priv->members);
1309 g_hash_table_iter_init (&iter, priv->pendings);
1310 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1311 g_signal_emit_by_name (list, "pendings-changed", contact,
1315 g_hash_table_remove_all (priv->pendings);