1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Xavier Claessens <xclaesse@gmail.com>
4 * Copyright (C) 2007-2009 Collabora Ltd.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <glib/gi18n-lib.h>
28 #include <telepathy-glib/channel.h>
29 #include <telepathy-glib/connection.h>
30 #include <telepathy-glib/util.h>
31 #include <telepathy-glib/dbus.h>
32 #include <telepathy-glib/interfaces.h>
34 #include <extensions/extensions.h>
36 #include "empathy-tp-contact-list.h"
37 #include "empathy-tp-contact-factory.h"
38 #include "empathy-contact-list.h"
39 #include "empathy-utils.h"
41 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
42 #include "empathy-debug.h"
44 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactList)
46 TpConnection *connection;
51 /* contact handle (TpHandle) => reffed (EmpathyContact *)
54 * - members of 'subscribe': we receive their presence
55 * - RP of 'subscribe': we asked to receive their presence
56 * - members of 'publish': we send them our presence
57 * - members of 'stored': they are in our roster
60 /* contact handle (TpHandle) => reffed (EmpathyContact *)
62 * Contacts which are local-pending in the publish channel but are NOT in
63 * the members hash table: they asked to receive our presence and we don't
64 * receive theirs or asked to.
65 * That's basically the contacts which asked to add us to their contact
66 * list and we didn't answer it. */
68 /* group name: borrowed (const gchar *) => reffed (TpChannel *) */
70 /* group name: owned (gchar *) => owned GArray of TpHandle */
71 GHashTable *add_to_group;
72 } EmpathyTpContactListPriv;
75 TP_CONTACT_LIST_TYPE_PUBLISH,
76 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
77 TP_CONTACT_LIST_TYPE_UNKNOWN
80 static void tp_contact_list_iface_init (EmpathyContactListIface *iface);
87 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
88 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
89 tp_contact_list_iface_init));
92 tp_contact_list_forget_group (EmpathyTpContactList *list,
95 EmpathyTpContactListPriv *priv = GET_PRIV (list);
96 const TpIntSet *members;
98 const gchar *group_name;
100 group_name = tp_channel_get_identifier (channel);
102 /* Signal that all members are not in that group anymore */
103 members = tp_channel_group_get_members (channel);
104 tp_intset_iter_init (&iter, members);
105 while (tp_intset_iter_next (&iter)) {
106 EmpathyContact *contact;
108 contact = g_hash_table_lookup (priv->members,
109 GUINT_TO_POINTER (iter.element));
110 if (contact == NULL) {
114 DEBUG ("Contact %s (%d) removed from group %s",
115 empathy_contact_get_id (contact), iter.element,
117 g_signal_emit_by_name (list, "groups-changed", contact,
124 tp_contact_list_group_invalidated_cb (TpChannel *channel,
128 EmpathyTpContactList *list)
130 EmpathyTpContactListPriv *priv = GET_PRIV (list);
131 const gchar *group_name;
133 group_name = tp_channel_get_identifier (channel);
134 DEBUG ("Group %s invalidated. Message: %s", group_name, message);
136 tp_contact_list_forget_group (list, channel);
138 g_hash_table_remove (priv->groups, group_name);
142 contacts_added_to_group (EmpathyTpContactList *list,
146 EmpathyTpContactListPriv *priv = GET_PRIV (list);
147 const gchar *group_name;
150 group_name = tp_channel_get_identifier (channel);
152 for (i = 0; i < added->len; i++) {
153 EmpathyContact *contact;
156 handle = g_array_index (added, TpHandle, i);
157 contact = g_hash_table_lookup (priv->members,
158 GUINT_TO_POINTER (handle));
159 if (contact == NULL) {
163 DEBUG ("Contact %s (%d) added to group %s",
164 empathy_contact_get_id (contact), handle, group_name);
165 g_signal_emit_by_name (list, "groups-changed", contact,
172 tp_contact_list_group_members_changed_cb (TpChannel *channel,
176 GArray *local_pending,
177 GArray *remote_pending,
180 EmpathyTpContactList *list)
182 EmpathyTpContactListPriv *priv = GET_PRIV (list);
183 const gchar *group_name;
186 contacts_added_to_group (list, channel, added);
188 group_name = tp_channel_get_identifier (channel);
190 for (i = 0; i < removed->len; i++) {
191 EmpathyContact *contact;
194 handle = g_array_index (removed, TpHandle, i);
195 contact = g_hash_table_lookup (priv->members,
196 GUINT_TO_POINTER (handle));
197 if (contact == NULL) {
201 DEBUG ("Contact %s (%d) removed from group %s",
202 empathy_contact_get_id (contact), handle, group_name);
204 g_signal_emit_by_name (list, "groups-changed", contact,
211 tp_contact_list_group_ready_cb (TpChannel *channel,
215 EmpathyTpContactListPriv *priv = GET_PRIV (list);
216 TpChannel *old_group;
217 const gchar *group_name;
218 const TpIntSet *members;
222 DEBUG ("Error: %s", error->message);
223 g_object_unref (channel);
227 group_name = tp_channel_get_identifier (channel);
229 /* If there's already a group with this name in the table, we can't
230 * just let it be replaced. Replacing it causes it to be unreffed,
231 * which causes it to be invalidated (see
232 * <https://bugs.freedesktop.org/show_bug.cgi?id=22119>), which causes
233 * it to be removed from the hash table again, which causes it to be
236 old_group = g_hash_table_lookup (priv->groups, group_name);
238 if (old_group != NULL) {
239 DEBUG ("Discarding old group %s (%p)", group_name, old_group);
240 g_hash_table_steal (priv->groups, group_name);
241 tp_contact_list_forget_group (list, old_group);
242 g_object_unref (old_group);
245 /* Pass the reference on the TpChannel to priv->groups */
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);
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_unref (handles);
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 (TpConnection *connection,
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->connection,
431 request->len, (TpHandle *) request->data,
432 got_added_members_cb, NULL, NULL, G_OBJECT (list));
435 g_array_unref (request);
439 tp_contact_list_got_local_pending_cb (TpConnection *connection,
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;
461 const TpIntSet *members, *remote_pending;
463 handle = empathy_contact_get_handle (contact);
464 members = tp_channel_group_get_members (priv->subscribe);
465 remote_pending = tp_channel_group_get_remote_pending (priv->subscribe);
467 if (tp_intset_is_member (members, handle) ||
468 tp_intset_is_member (remote_pending, handle)) {
469 GArray handles = {(gchar *) &handle, 1};
471 /* This contact is already subscribed, or user requested
472 * to subscribe, auto accept. */
473 tp_cli_channel_interface_group_call_add_members (priv->publish,
474 -1, &handles, NULL, NULL, NULL, NULL, NULL);
476 else if (tp_channel_group_get_local_pending_info (priv->publish,
481 /* Add contact to pendings */
482 g_hash_table_insert (priv->pendings, GUINT_TO_POINTER (handle),
483 g_object_ref (contact));
484 g_signal_emit_by_name (list, "pendings-changed", contact,
485 contact, reason, message, TRUE);
491 tp_contact_list_remove_handle (EmpathyTpContactList *list,
495 EmpathyTpContactListPriv *priv = GET_PRIV (list);
496 EmpathyContact *contact;
499 if (table == priv->pendings)
500 sig = "pendings-changed";
501 else if (table == priv->members)
502 sig = "members-changed";
506 contact = g_hash_table_lookup (table, GUINT_TO_POINTER (handle));
508 g_object_ref (contact);
509 g_hash_table_remove (table, GUINT_TO_POINTER (handle));
510 g_signal_emit_by_name (list, sig, contact, 0, 0, NULL,
512 g_object_unref (contact);
517 remove_from_member_if_needed (EmpathyTpContactList *list,
520 /* remove contact from members if it's not in publish and subscribe */
521 EmpathyTpContactListPriv *priv = GET_PRIV (list);
522 const TpIntSet *members;
524 members = tp_channel_group_get_members (priv->subscribe);
525 if (tp_intset_is_member (members, handle))
528 members = tp_channel_group_get_remote_pending (priv->subscribe);
529 if (tp_intset_is_member (members, handle))
532 members = tp_channel_group_get_members (priv->publish);
533 if (tp_intset_is_member (members, handle))
536 tp_contact_list_remove_handle (list, priv->members, handle);
540 tp_contact_list_publish_group_members_changed_cb (TpChannel *channel,
544 GArray *local_pending,
545 GArray *remote_pending,
547 TpChannelGroupChangeReason reason,
548 EmpathyTpContactList *list)
550 EmpathyTpContactListPriv *priv = GET_PRIV (list);
553 /* We now send our presence to those contacts, remove them from pendings */
554 add_to_members (list, added);
555 for (i = 0; i < added->len; i++) {
556 tp_contact_list_remove_handle (list, priv->pendings,
557 g_array_index (added, TpHandle, i));
560 /* We refuse to send our presence to those contacts, remove from pendings */
561 for (i = 0; i < removed->len; i++) {
562 TpHandle handle = g_array_index (removed, TpHandle, i);
564 tp_contact_list_remove_handle (list, priv->pendings, handle);
565 remove_from_member_if_needed (list, handle);
568 /* Those contacts want our presence, auto accept those that are already
569 * member, otherwise add in pendings. */
570 if (local_pending->len > 0) {
571 empathy_tp_contact_factory_get_from_handles (priv->connection,
572 local_pending->len, (TpHandle *) local_pending->data,
573 tp_contact_list_got_local_pending_cb, NULL, NULL,
579 tp_contact_list_subscribe_group_members_changed_cb (TpChannel *channel,
583 GArray *local_pending,
584 GArray *remote_pending,
587 EmpathyTpContactList *list)
589 EmpathyTpContactListPriv *priv = GET_PRIV (list);
593 /* We now get the presence of those contacts, add them to members */
594 add_to_members (list, added);
596 /* Those contacts refuse to send us their presence, remove from members. */
597 for (i = 0; i < removed->len; i++) {
598 remove_from_member_if_needed (list, g_array_index (removed, TpHandle, i));
601 /* We want those contacts in our contact list but we don't get their
602 * presence yet. Add to members anyway. */
603 add_to_members (list, remote_pending);
605 /* Implicitly accept pending request of contacts which are now members. */
606 if (priv->publish == NULL)
609 accept = g_array_new (FALSE, FALSE, sizeof (TpHandle));
610 for (i = 0; i < added->len; i++) {
611 TpHandle handle = g_array_index (added, TpHandle, i);
613 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle)) != NULL)
614 g_array_append_val (accept, handle);
617 for (i = 0; i < remote_pending->len; i++) {
618 TpHandle handle = g_array_index (added, TpHandle, i);
620 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle)) != NULL)
621 g_array_append_val (accept, handle);
624 tp_cli_channel_interface_group_call_add_members (priv->publish,
625 -1, accept, NULL, NULL, NULL, NULL, NULL);
627 g_array_unref (accept);
631 tp_contact_list_store_group_members_changed_cb (TpChannel *channel,
635 GArray *local_pending,
636 GArray *remote_pending,
639 EmpathyTpContactList *list)
643 add_to_members (list, added);
645 for (i = 0; i < removed->len; i++) {
646 remove_from_member_if_needed (list, g_array_index (removed, TpHandle, i));
651 tp_contact_list_finalize (GObject *object)
653 EmpathyTpContactListPriv *priv;
654 EmpathyTpContactList *list;
658 list = EMPATHY_TP_CONTACT_LIST (object);
659 priv = GET_PRIV (list);
661 DEBUG ("finalize: %p", object);
663 if (priv->subscribe) {
664 g_object_unref (priv->subscribe);
667 g_object_unref (priv->publish);
670 g_object_unref (priv->stored);
673 if (priv->connection) {
674 g_object_unref (priv->connection);
677 g_hash_table_iter_init (&iter, priv->groups);
678 while (g_hash_table_iter_next (&iter, NULL, &channel)) {
679 g_signal_handlers_disconnect_by_func (channel,
680 tp_contact_list_group_invalidated_cb, list);
683 g_hash_table_unref (priv->groups);
684 g_hash_table_unref (priv->members);
685 g_hash_table_unref (priv->pendings);
686 g_hash_table_unref (priv->add_to_group);
688 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
692 got_list_channel (EmpathyTpContactList *list,
695 EmpathyTpContactListPriv *priv = GET_PRIV (list);
698 /* We requested that channel by providing TargetID property, so it's
699 * guaranteed that tp_channel_get_identifier will return it. */
700 id = tp_channel_get_identifier (channel);
702 /* TpChannel emits initial set of members just before being ready */
703 if (!tp_strdiff (id, "stored")) {
704 if (priv->stored != NULL)
706 priv->stored = g_object_ref (channel);
707 g_signal_connect (priv->stored, "group-members-changed",
708 G_CALLBACK (tp_contact_list_store_group_members_changed_cb),
710 } else if (!tp_strdiff (id, "publish")) {
711 if (priv->publish != NULL)
713 priv->publish = g_object_ref (channel);
714 g_signal_connect (priv->publish, "group-members-changed",
715 G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
717 } else if (!tp_strdiff (id, "subscribe")) {
718 if (priv->subscribe != NULL)
720 priv->subscribe = g_object_ref (channel);
721 g_signal_connect (priv->subscribe, "group-members-changed",
722 G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
728 list_ensure_channel_cb (TpConnection *conn,
731 GHashTable *properties,
734 GObject *weak_object)
736 EmpathyTpContactList *list = user_data;
740 DEBUG ("failed: %s\n", error->message);
744 channel = tp_channel_new_from_properties (conn, path, properties, NULL);
745 got_list_channel (list, channel);
746 g_object_unref (channel);
750 iterate_on_channels (EmpathyTpContactList *list,
751 const GPtrArray *channels)
755 for (i = 0; i < channels->len ; i++) {
756 GValueArray *arr = g_ptr_array_index (channels, i);
758 GHashTable *properties;
759 TpHandleType handle_type;
761 path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
762 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
764 if (tp_strdiff (tp_asv_get_string (properties,
765 TP_IFACE_CHANNEL ".ChannelType"),
766 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST))
769 if (tp_asv_get_string (properties, TP_IFACE_CHANNEL ".TargetID") == NULL)
772 handle_type = tp_asv_get_uint32 (properties,
773 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
775 if (handle_type != TP_HANDLE_TYPE_GROUP)
778 tp_contact_list_group_add_channel (list, path, properties);
783 new_channels_cb (TpConnection *conn,
784 const GPtrArray *channels,
786 GObject *weak_object)
788 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
790 iterate_on_channels (list, channels);
794 got_channels_cb (TpProxy *conn,
798 GObject *weak_object)
800 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
801 const GPtrArray *channels;
804 DEBUG ("Get Channels property failed: %s", error->message);
808 channels = g_value_get_boxed (out);
809 iterate_on_channels (list, channels);
813 conn_ready_cb (TpConnection *connection,
817 EmpathyTpContactList *list = data;
818 EmpathyTpContactListPriv *priv = GET_PRIV (list);
822 DEBUG ("failed: %s", error->message);
826 /* Look for existing group channels */
827 tp_cli_dbus_properties_call_get (connection, -1,
828 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels", got_channels_cb,
829 NULL, NULL, G_OBJECT (list));
831 request = tp_asv_new (
832 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
833 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_LIST,
836 /* Watch the NewChannels signal so if ensuring list channels fails (for
837 * example because the server is slow and the D-Bus call timeouts before CM
838 * fetches the roster), we have a chance to get them later. */
839 tp_cli_connection_interface_requests_connect_to_new_channels (
840 priv->connection, new_channels_cb, NULL, NULL, G_OBJECT (list), NULL);
842 /* Request the 'stored' list. */
843 tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "stored");
844 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
845 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
847 /* Request the 'publish' list. */
848 tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "publish");
849 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
850 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
852 /* Request the 'subscribe' list. */
853 tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "subscribe");
854 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
855 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
857 g_hash_table_unref (request);
860 g_object_unref (list);
864 tp_contact_list_constructed (GObject *list)
866 EmpathyTpContactListPriv *priv = GET_PRIV (list);
868 tp_connection_call_when_ready (priv->connection, conn_ready_cb,
869 g_object_ref (list));
873 tp_contact_list_get_property (GObject *object,
878 EmpathyTpContactListPriv *priv = GET_PRIV (object);
881 case PROP_CONNECTION:
882 g_value_set_object (value, priv->connection);
885 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
891 tp_contact_list_set_property (GObject *object,
896 EmpathyTpContactListPriv *priv = GET_PRIV (object);
899 case PROP_CONNECTION:
900 priv->connection = g_value_dup_object (value);
903 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
909 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
911 GObjectClass *object_class = G_OBJECT_CLASS (klass);
913 object_class->finalize = tp_contact_list_finalize;
914 object_class->constructed = tp_contact_list_constructed;
915 object_class->get_property = tp_contact_list_get_property;
916 object_class->set_property = tp_contact_list_set_property;
918 g_object_class_install_property (object_class,
920 g_param_spec_object ("connection",
922 "The connection associated with the contact list",
925 G_PARAM_CONSTRUCT_ONLY));
927 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
931 tp_contact_list_array_free (gpointer handles)
933 g_array_unref (handles);
937 empathy_tp_contact_list_init (EmpathyTpContactList *list)
939 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
940 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
944 /* Map group's name to group's TpChannel. The group name string is owned
945 * by the TpChannel object */
946 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
948 (GDestroyNotify) g_object_unref);
950 /* Map contact's handle to EmpathyContact object */
951 priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
953 (GDestroyNotify) g_object_unref);
955 /* Map contact's handle to EmpathyContact object */
956 priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
958 (GDestroyNotify) g_object_unref);
960 /* Map group's name to GArray of handle */
961 priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
963 tp_contact_list_array_free);
966 EmpathyTpContactList *
967 empathy_tp_contact_list_new (TpConnection *connection)
969 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
970 "connection", connection,
975 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
977 EmpathyTpContactListPriv *priv;
979 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
981 priv = GET_PRIV (list);
983 return priv->connection;
987 tp_contact_list_add (EmpathyContactList *list,
988 EmpathyContact *contact,
989 const gchar *message)
991 EmpathyTpContactListPriv *priv = GET_PRIV (list);
993 GArray handles = {(gchar *) &handle, 1};
995 handle = empathy_contact_get_handle (contact);
997 if (priv->subscribe) {
998 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
999 -1, &handles, message, NULL, NULL, NULL, NULL);
1002 if (priv->publish) {
1003 TpChannelGroupFlags flags = tp_channel_group_get_flags (priv->subscribe);
1004 if (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD ||
1005 g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
1006 tp_cli_channel_interface_group_call_add_members (priv->publish,
1007 -1, &handles, message, NULL, NULL, NULL, NULL);
1011 /* We want to unblock the contact */
1012 if (tp_proxy_has_interface_by_id (priv->connection,
1013 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING)) {
1014 TpContact *tp_contact = empathy_contact_get_tp_contact (contact);
1016 if (tp_contact != NULL)
1017 tp_contact_unblock_async (tp_contact, NULL, NULL);
1022 tp_contact_list_remove (EmpathyContactList *list,
1023 EmpathyContact *contact,
1024 const gchar *message)
1026 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1028 GArray handles = {(gchar *) &handle, 1};
1030 handle = empathy_contact_get_handle (contact);
1032 /* FIXME: this is racy if tp_contact_list_remove is called before the
1033 * 'stored' list has been retrieved. */
1034 if (priv->stored != NULL) {
1035 tp_cli_channel_interface_group_call_remove_members (priv->stored,
1036 -1, &handles, message, NULL, NULL, NULL, NULL);
1039 if (priv->subscribe) {
1040 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
1041 -1, &handles, message, NULL, NULL, NULL, NULL);
1043 if (priv->publish) {
1044 tp_cli_channel_interface_group_call_remove_members (priv->publish,
1045 -1, &handles, message, NULL, NULL, NULL, NULL);
1050 tp_contact_list_get_members (EmpathyContactList *list)
1052 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1055 ret = g_hash_table_get_values (priv->members);
1056 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1061 tp_contact_list_get_pendings (EmpathyContactList *list)
1063 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1066 ret = g_hash_table_get_values (priv->pendings);
1067 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1072 tp_contact_list_get_groups (EmpathyContactList *list,
1073 EmpathyContact *contact)
1075 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1077 GHashTableIter iter;
1078 gpointer group_name;
1082 handle = empathy_contact_get_handle (contact);
1083 g_hash_table_iter_init (&iter, priv->groups);
1084 while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
1085 const TpIntSet *members;
1087 members = tp_channel_group_get_members (channel);
1088 if (tp_intset_is_member (members, handle)) {
1089 ret = g_list_prepend (ret, g_strdup (group_name));
1097 tp_contact_list_add_to_group (EmpathyContactList *list,
1098 EmpathyContact *contact,
1099 const gchar *group_name)
1104 handle = empathy_contact_get_handle (contact);
1105 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1106 g_array_append_val (handles, handle);
1107 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1108 group_name, handles);
1112 tp_contact_list_remove_from_group (EmpathyContactList *list,
1113 EmpathyContact *contact,
1114 const gchar *group_name)
1116 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1119 GArray handles = {(gchar *) &handle, 1};
1121 channel = g_hash_table_lookup (priv->groups, group_name);
1122 if (channel == NULL) {
1126 handle = empathy_contact_get_handle (contact);
1127 DEBUG ("remove contact %s (%d) from group %s",
1128 empathy_contact_get_id (contact), handle, group_name);
1130 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1131 &handles, NULL, NULL, NULL, NULL, NULL);
1135 tp_contact_list_rename_group (EmpathyContactList *list,
1136 const gchar *old_group_name,
1137 const gchar *new_group_name)
1139 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1141 const TpIntSet *members;
1144 channel = g_hash_table_lookup (priv->groups, old_group_name);
1145 if (channel == NULL) {
1149 DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1151 /* Remove all members and close the old channel */
1152 members = tp_channel_group_get_members (channel);
1153 handles = tp_intset_to_array (members);
1154 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1155 handles, NULL, NULL, NULL, NULL, NULL);
1156 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1158 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1159 new_group_name, handles);
1163 tp_contact_list_remove_group (EmpathyContactList *list,
1164 const gchar *group_name)
1166 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1168 const TpIntSet *members;
1171 channel = g_hash_table_lookup (priv->groups, group_name);
1172 if (channel == NULL) {
1176 DEBUG ("remove group %s", group_name);
1178 /* Remove all members and close the channel */
1179 members = tp_channel_group_get_members (channel);
1180 handles = tp_intset_to_array (members);
1181 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1182 handles, NULL, NULL, NULL, NULL, NULL);
1183 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1184 g_array_unref (handles);
1188 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1190 iface->add = tp_contact_list_add;
1191 iface->remove = tp_contact_list_remove;
1192 iface->get_members = tp_contact_list_get_members;
1193 iface->get_pendings = tp_contact_list_get_pendings;
1194 iface->get_groups = tp_contact_list_get_groups;
1195 iface->add_to_group = tp_contact_list_add_to_group;
1196 iface->remove_from_group = tp_contact_list_remove_from_group;
1197 iface->rename_group = tp_contact_list_rename_group;
1198 iface->remove_group = tp_contact_list_remove_group;
1202 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1204 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1205 GHashTableIter iter;
1208 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1210 /* Remove all contacts */
1211 g_hash_table_iter_init (&iter, priv->members);
1212 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1213 g_signal_emit_by_name (list, "members-changed", contact,
1217 g_hash_table_remove_all (priv->members);
1219 g_hash_table_iter_init (&iter, priv->pendings);
1220 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1221 g_signal_emit_by_name (list, "pendings-changed", contact,
1225 g_hash_table_remove_all (priv->pendings);