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 *)
53 * - members of 'subscribe': we receive their presence
54 * - RP of 'subscribe': we asked to receive their presence
55 * - members of 'publish': we send them our presence
56 * - members of 'stored': they are in our roster
59 /* contact handle (TpHandle) => reffed (EmpathyContact *)
61 * Contacts which are local-pending in the publish channel but are NOT in
62 * the members hash table: they asked to receive our presence and we don't
63 * receive theirs or asked to.
64 * That's basically the contacts which asked to add us to their contact
65 * list and we didn't answer it. */
67 /* group name: borrowed (const gchar *) => reffed (TpChannel *) */
69 /* group name: owned (gchar *) => owned GArray of TpHandle */
70 GHashTable *add_to_group;
72 EmpathyContactListFlags flags;
73 } EmpathyTpContactListPriv;
76 TP_CONTACT_LIST_TYPE_PUBLISH,
77 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
78 TP_CONTACT_LIST_TYPE_UNKNOWN
81 static void tp_contact_list_iface_init (EmpathyContactListIface *iface);
88 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
89 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
90 tp_contact_list_iface_init));
93 tp_contact_list_forget_group (EmpathyTpContactList *list,
96 EmpathyTpContactListPriv *priv = GET_PRIV (list);
97 const TpIntSet *members;
99 const gchar *group_name;
101 group_name = tp_channel_get_identifier (channel);
103 /* Signal that all members are not in that group anymore */
104 members = tp_channel_group_get_members (channel);
105 tp_intset_iter_init (&iter, members);
106 while (tp_intset_iter_next (&iter)) {
107 EmpathyContact *contact;
109 contact = g_hash_table_lookup (priv->members,
110 GUINT_TO_POINTER (iter.element));
111 if (contact == NULL) {
115 DEBUG ("Contact %s (%d) removed from group %s",
116 empathy_contact_get_id (contact), iter.element,
118 g_signal_emit_by_name (list, "groups-changed", contact,
125 tp_contact_list_group_invalidated_cb (TpChannel *channel,
129 EmpathyTpContactList *list)
131 EmpathyTpContactListPriv *priv = GET_PRIV (list);
132 const gchar *group_name;
134 group_name = tp_channel_get_identifier (channel);
135 DEBUG ("Group %s invalidated. Message: %s", group_name, message);
137 tp_contact_list_forget_group (list, channel);
139 g_hash_table_remove (priv->groups, group_name);
143 contacts_added_to_group (EmpathyTpContactList *list,
147 EmpathyTpContactListPriv *priv = GET_PRIV (list);
148 const gchar *group_name;
151 group_name = tp_channel_get_identifier (channel);
153 for (i = 0; i < added->len; i++) {
154 EmpathyContact *contact;
157 handle = g_array_index (added, TpHandle, i);
158 contact = g_hash_table_lookup (priv->members,
159 GUINT_TO_POINTER (handle));
160 if (contact == NULL) {
164 DEBUG ("Contact %s (%d) added to group %s",
165 empathy_contact_get_id (contact), handle, group_name);
166 g_signal_emit_by_name (list, "groups-changed", contact,
173 tp_contact_list_group_members_changed_cb (TpChannel *channel,
177 GArray *local_pending,
178 GArray *remote_pending,
181 EmpathyTpContactList *list)
183 EmpathyTpContactListPriv *priv = GET_PRIV (list);
184 const gchar *group_name;
187 contacts_added_to_group (list, channel, added);
189 group_name = tp_channel_get_identifier (channel);
191 for (i = 0; i < removed->len; i++) {
192 EmpathyContact *contact;
195 handle = g_array_index (removed, TpHandle, i);
196 contact = g_hash_table_lookup (priv->members,
197 GUINT_TO_POINTER (handle));
198 if (contact == NULL) {
202 DEBUG ("Contact %s (%d) removed from group %s",
203 empathy_contact_get_id (contact), handle, group_name);
205 g_signal_emit_by_name (list, "groups-changed", contact,
212 tp_contact_list_group_ready_cb (TpChannel *channel,
216 EmpathyTpContactListPriv *priv = GET_PRIV (list);
217 TpChannel *old_group;
218 const gchar *group_name;
219 const TpIntSet *members;
223 DEBUG ("Error: %s", error->message);
224 g_object_unref (channel);
228 group_name = tp_channel_get_identifier (channel);
230 /* If there's already a group with this name in the table, we can't
231 * just let it be replaced. Replacing it causes it to be unreffed,
232 * which causes it to be invalidated (see
233 * <https://bugs.freedesktop.org/show_bug.cgi?id=22119>), which causes
234 * it to be removed from the hash table again, which causes it to be
237 old_group = g_hash_table_lookup (priv->groups, group_name);
239 if (old_group != NULL) {
240 DEBUG ("Discarding old group %s (%p)", group_name, old_group);
241 g_hash_table_steal (priv->groups, group_name);
242 tp_contact_list_forget_group (list, old_group);
243 g_object_unref (old_group);
246 g_hash_table_insert (priv->groups, (gpointer) group_name, channel);
247 DEBUG ("Group %s added", group_name);
249 g_signal_connect (channel, "group-members-changed",
250 G_CALLBACK (tp_contact_list_group_members_changed_cb),
253 g_signal_connect (channel, "invalidated",
254 G_CALLBACK (tp_contact_list_group_invalidated_cb),
257 if (priv->add_to_group) {
260 handles = g_hash_table_lookup (priv->add_to_group, group_name);
262 DEBUG ("Adding initial members to group %s", group_name);
263 tp_cli_channel_interface_group_call_add_members (channel,
264 -1, handles, NULL, NULL, NULL, NULL, NULL);
265 g_hash_table_remove (priv->add_to_group, group_name);
269 /* Get initial members of the group */
270 members = tp_channel_group_get_members (channel);
271 g_assert (members != NULL);
272 arr = tp_intset_to_array (members);
273 contacts_added_to_group (list, channel, arr);
274 g_array_free (arr, TRUE);
278 tp_contact_list_group_add_channel (EmpathyTpContactList *list,
279 const gchar *object_path,
280 GHashTable *properties)
282 EmpathyTpContactListPriv *priv = GET_PRIV (list);
284 GError *error = NULL;
286 channel = tp_channel_new_from_properties (priv->connection,
287 object_path, properties, &error);
288 if (channel == NULL) {
289 DEBUG ("Failed to create group channel: %s", error->message);
290 g_error_free (error);
294 /* Give the ref to the callback */
295 tp_channel_call_when_ready (channel,
296 tp_contact_list_group_ready_cb,
301 tp_contact_list_group_request_channel_cb (TpConnection *connection,
302 const gchar *object_path,
307 /* The new channel will be handled in NewChannel cb. Here we only
308 * handle the error if RequestChannel failed */
310 DEBUG ("Error: %s", error->message);
316 tp_contact_list_group_request_handles_cb (TpConnection *connection,
317 const GArray *handles,
322 TpHandle channel_handle;
325 DEBUG ("Error: %s", error->message);
329 channel_handle = g_array_index (handles, TpHandle, 0);
330 tp_cli_connection_call_request_channel (connection, -1,
331 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
332 TP_HANDLE_TYPE_GROUP,
335 tp_contact_list_group_request_channel_cb,
340 /* This function takes ownership of handles array */
342 tp_contact_list_group_add (EmpathyTpContactList *list,
343 const gchar *group_name,
346 EmpathyTpContactListPriv *priv = GET_PRIV (list);
348 const gchar *names[] = {group_name, NULL};
350 /* Search the channel for that group name */
351 channel = g_hash_table_lookup (priv->groups, group_name);
353 tp_cli_channel_interface_group_call_add_members (channel, -1,
354 handles, NULL, NULL, NULL, NULL, NULL);
355 g_array_free (handles, TRUE);
359 /* That group does not exist yet, we have to:
360 * 1) Request an handle for the group name
361 * 2) Request a channel
362 * 3) When NewChannel is emitted, add handles in members
364 g_hash_table_insert (priv->add_to_group,
365 g_strdup (group_name),
367 tp_cli_connection_call_request_handles (priv->connection, -1,
368 TP_HANDLE_TYPE_GROUP, names,
369 tp_contact_list_group_request_handles_cb,
375 got_added_members_cb (EmpathyTpContactFactory *factory,
377 EmpathyContact * const * contacts,
379 const TpHandle *failed,
384 EmpathyTpContactListPriv *priv = GET_PRIV (list);
388 DEBUG ("Error: %s", error->message);
392 for (i = 0; i < n_contacts; i++) {
393 EmpathyContact *contact = contacts[i];
394 TpHandle handle = empathy_contact_get_handle (contact);
396 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
399 /* Add to the list and emit signal */
400 g_hash_table_insert (priv->members, GUINT_TO_POINTER (handle),
401 g_object_ref (contact));
402 g_signal_emit_by_name (list, "members-changed", contact,
408 add_to_members (EmpathyTpContactList *list,
411 EmpathyTpContactListPriv *priv = GET_PRIV (list);
415 if (handles->len == 0)
418 request = g_array_new (FALSE, FALSE, sizeof (TpHandle));
420 for (i = 0; i < handles->len; i++) {
421 TpHandle handle = g_array_index (handles, TpHandle, i);
423 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
426 g_array_append_val (request, handle);
429 if (request->len > 0) {
430 empathy_tp_contact_factory_get_from_handles (priv->factory,
431 request->len, (TpHandle *) request->data,
432 got_added_members_cb, NULL, NULL, G_OBJECT (list));
435 g_array_free (request, TRUE);
439 tp_contact_list_got_local_pending_cb (EmpathyTpContactFactory *factory,
441 EmpathyContact * const * contacts,
443 const TpHandle *failed,
448 EmpathyTpContactListPriv *priv = GET_PRIV (list);
452 DEBUG ("Error: %s", error->message);
456 for (i = 0; i < n_contacts; i++) {
457 EmpathyContact *contact = contacts[i];
459 const gchar *message;
460 TpChannelGroupChangeReason reason;
462 handle = empathy_contact_get_handle (contact);
463 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle))) {
464 GArray handles = {(gchar *) &handle, 1};
466 /* This contact is already member, auto accept. */
467 tp_cli_channel_interface_group_call_add_members (priv->publish,
468 -1, &handles, NULL, NULL, NULL, NULL, NULL);
470 else if (tp_channel_group_get_local_pending_info (priv->publish,
475 /* Add contact to pendings */
476 g_hash_table_insert (priv->pendings, GUINT_TO_POINTER (handle),
477 g_object_ref (contact));
478 g_signal_emit_by_name (list, "pendings-changed", contact,
479 contact, reason, message, TRUE);
485 tp_contact_list_remove_handle (EmpathyTpContactList *list,
489 EmpathyTpContactListPriv *priv = GET_PRIV (list);
490 EmpathyContact *contact;
493 if (table == priv->pendings)
494 sig = "pendings-changed";
495 else if (table == priv->members)
496 sig = "members-changed";
500 contact = g_hash_table_lookup (table, GUINT_TO_POINTER (handle));
502 g_object_ref (contact);
503 g_hash_table_remove (table, GUINT_TO_POINTER (handle));
504 g_signal_emit_by_name (list, sig, contact, 0, 0, NULL,
506 g_object_unref (contact);
511 remove_from_member_if_needed (EmpathyTpContactList *list,
514 /* remove contact from members if it's not in publish and subscribe */
515 EmpathyTpContactListPriv *priv = GET_PRIV (list);
516 const TpIntSet *members;
518 members = tp_channel_group_get_members (priv->subscribe);
519 if (tp_intset_is_member (members, handle))
522 members = tp_channel_group_get_remote_pending (priv->subscribe);
523 if (tp_intset_is_member (members, handle))
526 members = tp_channel_group_get_members (priv->publish);
527 if (tp_intset_is_member (members, handle))
530 tp_contact_list_remove_handle (list, priv->members, handle);
534 tp_contact_list_publish_group_members_changed_cb (TpChannel *channel,
538 GArray *local_pending,
539 GArray *remote_pending,
541 TpChannelGroupChangeReason reason,
542 EmpathyTpContactList *list)
544 EmpathyTpContactListPriv *priv = GET_PRIV (list);
547 /* We now send our presence to those contacts, remove them from pendings */
548 add_to_members (list, added);
549 for (i = 0; i < added->len; i++) {
550 tp_contact_list_remove_handle (list, priv->pendings,
551 g_array_index (added, TpHandle, i));
554 /* We refuse to send our presence to those contacts, remove from pendings */
555 for (i = 0; i < removed->len; i++) {
556 TpHandle handle = g_array_index (removed, TpHandle, i);
558 tp_contact_list_remove_handle (list, priv->pendings, handle);
559 remove_from_member_if_needed (list, handle);
562 /* Those contacts want our presence, auto accept those that are already
563 * member, otherwise add in pendings. */
564 if (local_pending->len > 0) {
565 empathy_tp_contact_factory_get_from_handles (priv->factory,
566 local_pending->len, (TpHandle *) local_pending->data,
567 tp_contact_list_got_local_pending_cb, NULL, NULL,
573 tp_contact_list_get_alias_flags_cb (TpConnection *connection,
579 EmpathyTpContactListPriv *priv = GET_PRIV (list);
582 DEBUG ("Error: %s", error->message);
586 if (flags & TP_CONNECTION_ALIAS_FLAG_USER_SET) {
587 priv->flags |= EMPATHY_CONTACT_LIST_CAN_ALIAS;
592 tp_contact_list_get_requestablechannelclasses_cb (TpProxy *connection,
598 EmpathyTpContactListPriv *priv = GET_PRIV (list);
603 DEBUG ("Error: %s", error->message);
607 classes = g_value_get_boxed (value);
608 for (i = 0; i < classes->len; i++) {
609 GValueArray *class = g_ptr_array_index (classes, i);
611 const char *channel_type;
614 props = g_value_get_boxed (g_value_array_get_nth (class, 0));
616 channel_type = tp_asv_get_string (props,
617 TP_IFACE_CHANNEL ".ChannelType");
618 handle_type = tp_asv_get_uint32 (props,
619 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
621 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) &&
622 handle_type == TP_HANDLE_TYPE_GROUP) {
623 DEBUG ("Got channel class for a contact group");
624 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
631 tp_contact_list_subscribe_group_members_changed_cb (TpChannel *channel,
635 GArray *local_pending,
636 GArray *remote_pending,
639 EmpathyTpContactList *list)
641 EmpathyTpContactListPriv *priv = GET_PRIV (list);
645 /* We now get the presence of those contacts, add them to members */
646 add_to_members (list, added);
648 /* Those contacts refuse to send us their presence, remove from members. */
649 for (i = 0; i < removed->len; i++) {
650 remove_from_member_if_needed (list, g_array_index (removed, TpHandle, i));
653 /* We want those contacts in our contact list but we don't get their
654 * presence yet. Add to members anyway. */
655 add_to_members (list, remote_pending);
657 /* Implicitly accept pending request of contacts which are now members. */
658 accept = g_array_new (FALSE, FALSE, sizeof (TpHandle));
659 for (i = 0; i < added->len; i++) {
660 TpHandle handle = g_array_index (added, TpHandle, i);
662 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle)) != NULL)
663 g_array_append_val (accept, handle);
666 for (i = 0; i < remote_pending->len; i++) {
667 TpHandle handle = g_array_index (added, TpHandle, i);
669 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle)) != NULL)
670 g_array_append_val (accept, handle);
673 tp_cli_channel_interface_group_call_add_members (priv->publish,
674 -1, accept, NULL, NULL, NULL, NULL, NULL);
676 g_array_free (accept, TRUE);
680 tp_contact_list_store_group_members_changed_cb (TpChannel *channel,
684 GArray *local_pending,
685 GArray *remote_pending,
688 EmpathyTpContactList *list)
692 add_to_members (list, added);
694 for (i = 0; i < removed->len; i++) {
695 remove_from_member_if_needed (list, g_array_index (removed, TpHandle, i));
700 tp_contact_list_finalize (GObject *object)
702 EmpathyTpContactListPriv *priv;
703 EmpathyTpContactList *list;
707 list = EMPATHY_TP_CONTACT_LIST (object);
708 priv = GET_PRIV (list);
710 DEBUG ("finalize: %p", object);
712 if (priv->subscribe) {
713 g_object_unref (priv->subscribe);
716 g_object_unref (priv->publish);
719 g_object_unref (priv->stored);
722 if (priv->connection) {
723 g_object_unref (priv->connection);
727 g_object_unref (priv->factory);
730 g_hash_table_iter_init (&iter, priv->groups);
731 while (g_hash_table_iter_next (&iter, NULL, &channel)) {
732 g_signal_handlers_disconnect_by_func (channel,
733 tp_contact_list_group_invalidated_cb, list);
736 g_hash_table_destroy (priv->groups);
737 g_hash_table_destroy (priv->members);
738 g_hash_table_destroy (priv->pendings);
739 g_hash_table_destroy (priv->add_to_group);
741 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
745 got_list_channel (EmpathyTpContactList *list,
748 EmpathyTpContactListPriv *priv = GET_PRIV (list);
751 /* We requested that channel by providing TargetID property, so it's
752 * guaranteed that tp_channel_get_identifier will return it. */
753 id = tp_channel_get_identifier (channel);
755 /* TpChannel emits initial set of members just before being ready */
756 if (!tp_strdiff (id, "stored")) {
757 if (priv->stored != NULL)
759 priv->stored = g_object_ref (channel);
760 g_signal_connect (priv->stored, "group-members-changed",
761 G_CALLBACK (tp_contact_list_store_group_members_changed_cb),
763 } else if (!tp_strdiff (id, "publish")) {
764 if (priv->publish != NULL)
766 priv->publish = g_object_ref (channel);
767 g_signal_connect (priv->publish, "group-members-changed",
768 G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
770 } else if (!tp_strdiff (id, "subscribe")) {
771 if (priv->subscribe != NULL)
773 priv->subscribe = g_object_ref (channel);
774 g_signal_connect (priv->subscribe, "group-members-changed",
775 G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
781 list_ensure_channel_cb (TpConnection *conn,
784 GHashTable *properties,
787 GObject *weak_object)
789 EmpathyTpContactList *list = user_data;
793 DEBUG ("failed: %s\n", error->message);
797 channel = tp_channel_new_from_properties (conn, path, properties, NULL);
798 got_list_channel (list, channel);
799 g_object_unref (channel);
803 new_channels_cb (TpConnection *conn,
804 const GPtrArray *channels,
806 GObject *weak_object)
808 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
811 for (i = 0; i < channels->len ; i++) {
812 GValueArray *arr = g_ptr_array_index (channels, i);
814 GHashTable *properties;
815 TpHandleType handle_type;
817 path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
818 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
820 if (tp_strdiff (tp_asv_get_string (properties,
821 TP_IFACE_CHANNEL ".ChannelType"),
822 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST))
825 if (tp_asv_get_string (properties, TP_IFACE_CHANNEL ".TargetID") == NULL)
828 handle_type = tp_asv_get_uint32 (properties,
829 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
831 if (handle_type == TP_HANDLE_TYPE_GROUP) {
832 tp_contact_list_group_add_channel (list, path, properties);
838 got_channels_cb (TpProxy *conn,
842 GObject *weak_object)
844 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
846 const GPtrArray *channels;
849 DEBUG ("Get Channels property failed: %s", error->message);
853 channels = g_value_get_boxed (out);
854 for (i = 0; i < channels->len ; i++) {
855 GValueArray *arr = g_ptr_array_index (channels, i);
857 GHashTable *properties;
859 path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
860 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
862 if (tp_strdiff (tp_asv_get_string (properties,
863 TP_IFACE_CHANNEL ".ChannelType"),
864 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST))
867 if (tp_asv_get_uint32 (properties,
868 TP_IFACE_CHANNEL ".TargetHandleType", NULL) != TP_HANDLE_TYPE_GROUP)
871 tp_contact_list_group_add_channel (list, path, properties);
876 conn_ready_cb (TpConnection *connection,
880 EmpathyTpContactList *list = data;
881 EmpathyTpContactListPriv *priv = GET_PRIV (list);
885 DEBUG ("failed: %s", error->message);
889 /* Look for existing group channels */
890 tp_cli_dbus_properties_call_get (connection, -1,
891 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels", got_channels_cb,
892 NULL, NULL, G_OBJECT (list));
894 request = tp_asv_new (
895 TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
896 TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_UINT, TP_HANDLE_TYPE_LIST,
899 /* Watch the NewChannels signal so if ensuring list channels fails (for
900 * example because the server is slow and the D-Bus call timeouts before CM
901 * fetches the roster), we have a chance to get them later. */
902 tp_cli_connection_interface_requests_connect_to_new_channels (
903 priv->connection, new_channels_cb, NULL, NULL, G_OBJECT (list), NULL);
905 /* Request the 'stored' list. */
906 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "stored");
907 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
908 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
910 /* Request the 'publish' list. */
911 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "publish");
912 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
913 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
915 /* Request the 'subscribe' list. */
916 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "subscribe");
917 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
918 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
920 g_hash_table_unref (request);
922 g_object_unref (list);
926 tp_contact_list_constructed (GObject *list)
928 EmpathyTpContactListPriv *priv = GET_PRIV (list);
930 priv->factory = empathy_tp_contact_factory_dup_singleton (priv->connection);
932 /* call GetAliasFlags */
933 if (tp_proxy_has_interface_by_id (priv->connection,
934 TP_IFACE_QUARK_CONNECTION_INTERFACE_ALIASING)) {
935 tp_cli_connection_interface_aliasing_call_get_alias_flags (
938 tp_contact_list_get_alias_flags_cb,
943 /* lookup RequestableChannelClasses */
944 if (tp_proxy_has_interface_by_id (priv->connection,
945 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS)) {
946 tp_cli_dbus_properties_call_get (priv->connection,
948 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
949 "RequestableChannelClasses",
950 tp_contact_list_get_requestablechannelclasses_cb,
954 /* we just don't know... better mark the flag just in case */
955 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
958 tp_connection_call_when_ready (priv->connection, conn_ready_cb,
959 g_object_ref (list));
963 tp_contact_list_get_property (GObject *object,
968 EmpathyTpContactListPriv *priv = GET_PRIV (object);
971 case PROP_CONNECTION:
972 g_value_set_object (value, priv->connection);
975 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
981 tp_contact_list_set_property (GObject *object,
986 EmpathyTpContactListPriv *priv = GET_PRIV (object);
989 case PROP_CONNECTION:
990 priv->connection = g_value_dup_object (value);
993 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
999 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
1001 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1003 object_class->finalize = tp_contact_list_finalize;
1004 object_class->constructed = tp_contact_list_constructed;
1005 object_class->get_property = tp_contact_list_get_property;
1006 object_class->set_property = tp_contact_list_set_property;
1008 g_object_class_install_property (object_class,
1010 g_param_spec_object ("connection",
1012 "The connection associated with the contact list",
1015 G_PARAM_CONSTRUCT_ONLY));
1017 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
1021 tp_contact_list_array_free (gpointer handles)
1023 g_array_free (handles, TRUE);
1027 empathy_tp_contact_list_init (EmpathyTpContactList *list)
1029 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
1030 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
1034 /* Map group's name to group's TpChannel. The group name string is owned
1035 * by the TpChannel object */
1036 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1038 (GDestroyNotify) g_object_unref);
1040 /* Map contact's handle to EmpathyContact object */
1041 priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1043 (GDestroyNotify) g_object_unref);
1045 /* Map contact's handle to EmpathyContact object */
1046 priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1048 (GDestroyNotify) g_object_unref);
1050 /* Map group's name to GArray of handle */
1051 priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
1053 tp_contact_list_array_free);
1056 EmpathyTpContactList *
1057 empathy_tp_contact_list_new (TpConnection *connection)
1059 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
1060 "connection", connection,
1065 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
1067 EmpathyTpContactListPriv *priv;
1069 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
1071 priv = GET_PRIV (list);
1073 return priv->connection;
1077 tp_contact_list_add (EmpathyContactList *list,
1078 EmpathyContact *contact,
1079 const gchar *message)
1081 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1083 GArray handles = {(gchar *) &handle, 1};
1085 handle = empathy_contact_get_handle (contact);
1086 if (priv->subscribe) {
1087 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
1088 -1, &handles, message, NULL, NULL, NULL, NULL);
1090 if (priv->publish) {
1091 TpChannelGroupFlags flags = tp_channel_group_get_flags (priv->subscribe);
1092 if (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD ||
1093 g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
1094 tp_cli_channel_interface_group_call_add_members (priv->publish,
1095 -1, &handles, message, NULL, NULL, NULL, NULL);
1101 tp_contact_list_remove (EmpathyContactList *list,
1102 EmpathyContact *contact,
1103 const gchar *message)
1105 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1107 GArray handles = {(gchar *) &handle, 1};
1109 handle = empathy_contact_get_handle (contact);
1111 /* FIXME: this is racy if tp_contact_list_remove is called before the
1112 * 'stored' list has been retrieved. */
1113 if (priv->stored != NULL) {
1114 tp_cli_channel_interface_group_call_remove_members (priv->stored,
1115 -1, &handles, message, NULL, NULL, NULL, NULL);
1118 if (priv->subscribe) {
1119 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
1120 -1, &handles, message, NULL, NULL, NULL, NULL);
1122 if (priv->publish) {
1123 tp_cli_channel_interface_group_call_remove_members (priv->publish,
1124 -1, &handles, message, NULL, NULL, NULL, NULL);
1129 tp_contact_list_get_members (EmpathyContactList *list)
1131 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1134 ret = g_hash_table_get_values (priv->members);
1135 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1140 tp_contact_list_get_pendings (EmpathyContactList *list)
1142 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1145 ret = g_hash_table_get_values (priv->pendings);
1146 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1151 tp_contact_list_get_all_groups (EmpathyContactList *list)
1153 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1156 ret = g_hash_table_get_keys (priv->groups);
1157 for (l = ret; l; l = l->next) {
1158 l->data = g_strdup (l->data);
1165 tp_contact_list_get_groups (EmpathyContactList *list,
1166 EmpathyContact *contact)
1168 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1170 GHashTableIter iter;
1171 gpointer group_name;
1175 handle = empathy_contact_get_handle (contact);
1176 g_hash_table_iter_init (&iter, priv->groups);
1177 while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
1178 const TpIntSet *members;
1180 members = tp_channel_group_get_members (channel);
1181 if (tp_intset_is_member (members, handle)) {
1182 ret = g_list_prepend (ret, g_strdup (group_name));
1190 tp_contact_list_add_to_group (EmpathyContactList *list,
1191 EmpathyContact *contact,
1192 const gchar *group_name)
1197 handle = empathy_contact_get_handle (contact);
1198 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1199 g_array_append_val (handles, handle);
1200 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1201 group_name, handles);
1205 tp_contact_list_remove_from_group (EmpathyContactList *list,
1206 EmpathyContact *contact,
1207 const gchar *group_name)
1209 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1212 GArray handles = {(gchar *) &handle, 1};
1214 channel = g_hash_table_lookup (priv->groups, group_name);
1215 if (channel == NULL) {
1219 handle = empathy_contact_get_handle (contact);
1220 DEBUG ("remove contact %s (%d) from group %s",
1221 empathy_contact_get_id (contact), handle, group_name);
1223 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1224 &handles, NULL, NULL, NULL, NULL, NULL);
1228 tp_contact_list_rename_group (EmpathyContactList *list,
1229 const gchar *old_group_name,
1230 const gchar *new_group_name)
1232 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1234 const TpIntSet *members;
1237 channel = g_hash_table_lookup (priv->groups, old_group_name);
1238 if (channel == NULL) {
1242 DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1244 /* Remove all members and close the old channel */
1245 members = tp_channel_group_get_members (channel);
1246 handles = tp_intset_to_array (members);
1247 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1248 handles, NULL, NULL, NULL, NULL, NULL);
1249 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1251 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1252 new_group_name, handles);
1256 tp_contact_list_remove_group (EmpathyContactList *list,
1257 const gchar *group_name)
1259 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1261 const TpIntSet *members;
1264 channel = g_hash_table_lookup (priv->groups, group_name);
1265 if (channel == NULL) {
1269 DEBUG ("remove group %s", group_name);
1271 /* Remove all members and close the channel */
1272 members = tp_channel_group_get_members (channel);
1273 handles = tp_intset_to_array (members);
1274 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1275 handles, NULL, NULL, NULL, NULL, NULL);
1276 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1277 g_array_free (handles, TRUE);
1280 static EmpathyContactListFlags
1281 tp_contact_list_get_flags (EmpathyContactList *list)
1283 EmpathyTpContactListPriv *priv;
1284 EmpathyContactListFlags flags;
1286 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1288 priv = GET_PRIV (list);
1289 flags = priv->flags;
1291 if (priv->subscribe != NULL) {
1292 TpChannelGroupFlags group_flags;
1294 group_flags = tp_channel_group_get_flags (priv->subscribe);
1296 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1297 flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1300 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1301 flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1309 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1311 iface->add = tp_contact_list_add;
1312 iface->remove = tp_contact_list_remove;
1313 iface->get_members = tp_contact_list_get_members;
1314 iface->get_pendings = tp_contact_list_get_pendings;
1315 iface->get_all_groups = tp_contact_list_get_all_groups;
1316 iface->get_groups = tp_contact_list_get_groups;
1317 iface->add_to_group = tp_contact_list_add_to_group;
1318 iface->remove_from_group = tp_contact_list_remove_from_group;
1319 iface->rename_group = tp_contact_list_rename_group;
1320 iface->remove_group = tp_contact_list_remove_group;
1321 iface->get_flags = tp_contact_list_get_flags;
1325 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1327 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1328 GHashTableIter iter;
1331 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1333 /* Remove all contacts */
1334 g_hash_table_iter_init (&iter, priv->members);
1335 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1336 g_signal_emit_by_name (list, "members-changed", contact,
1340 g_hash_table_remove_all (priv->members);
1342 g_hash_table_iter_init (&iter, priv->pendings);
1343 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1344 g_signal_emit_by_name (list, "pendings-changed", contact,
1348 g_hash_table_remove_all (priv->pendings);