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 TpConnection *connection;
49 /* contact handle (TpHandle) => reffed (EmpathyContact *)
52 * - members of 'subscribe': we receive their presence
53 * - RP of 'subscribe': we asked to receive their presence
54 * - members of 'publish': we send them our presence
55 * - members of 'stored': they are in our roster
58 /* contact handle (TpHandle) => reffed (EmpathyContact *)
60 * Contacts which are local-pending in the publish channel but are NOT in
61 * the members hash table: they asked to receive our presence and we don't
62 * receive theirs or asked to.
63 * That's basically the contacts which asked to add us to their contact
64 * list and we didn't answer it. */
66 /* group name: borrowed (const gchar *) => reffed (TpChannel *) */
68 /* group name: owned (gchar *) => owned GArray of TpHandle */
69 GHashTable *add_to_group;
71 EmpathyContactListFlags flags;
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);
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 (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_free (request, TRUE);
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;
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->connection,
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 if (priv->publish == NULL)
661 accept = g_array_new (FALSE, FALSE, sizeof (TpHandle));
662 for (i = 0; i < added->len; i++) {
663 TpHandle handle = g_array_index (added, TpHandle, i);
665 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle)) != NULL)
666 g_array_append_val (accept, handle);
669 for (i = 0; i < remote_pending->len; i++) {
670 TpHandle handle = g_array_index (added, TpHandle, i);
672 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle)) != NULL)
673 g_array_append_val (accept, handle);
676 tp_cli_channel_interface_group_call_add_members (priv->publish,
677 -1, accept, NULL, NULL, NULL, NULL, NULL);
679 g_array_free (accept, TRUE);
683 tp_contact_list_store_group_members_changed_cb (TpChannel *channel,
687 GArray *local_pending,
688 GArray *remote_pending,
691 EmpathyTpContactList *list)
695 add_to_members (list, added);
697 for (i = 0; i < removed->len; i++) {
698 remove_from_member_if_needed (list, g_array_index (removed, TpHandle, i));
703 tp_contact_list_finalize (GObject *object)
705 EmpathyTpContactListPriv *priv;
706 EmpathyTpContactList *list;
710 list = EMPATHY_TP_CONTACT_LIST (object);
711 priv = GET_PRIV (list);
713 DEBUG ("finalize: %p", object);
715 if (priv->subscribe) {
716 g_object_unref (priv->subscribe);
719 g_object_unref (priv->publish);
722 g_object_unref (priv->stored);
725 if (priv->connection) {
726 g_object_unref (priv->connection);
729 g_hash_table_iter_init (&iter, priv->groups);
730 while (g_hash_table_iter_next (&iter, NULL, &channel)) {
731 g_signal_handlers_disconnect_by_func (channel,
732 tp_contact_list_group_invalidated_cb, list);
735 g_hash_table_destroy (priv->groups);
736 g_hash_table_destroy (priv->members);
737 g_hash_table_destroy (priv->pendings);
738 g_hash_table_destroy (priv->add_to_group);
740 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
744 got_list_channel (EmpathyTpContactList *list,
747 EmpathyTpContactListPriv *priv = GET_PRIV (list);
750 /* We requested that channel by providing TargetID property, so it's
751 * guaranteed that tp_channel_get_identifier will return it. */
752 id = tp_channel_get_identifier (channel);
754 /* TpChannel emits initial set of members just before being ready */
755 if (!tp_strdiff (id, "stored")) {
756 if (priv->stored != NULL)
758 priv->stored = g_object_ref (channel);
759 g_signal_connect (priv->stored, "group-members-changed",
760 G_CALLBACK (tp_contact_list_store_group_members_changed_cb),
762 } else if (!tp_strdiff (id, "publish")) {
763 if (priv->publish != NULL)
765 priv->publish = g_object_ref (channel);
766 g_signal_connect (priv->publish, "group-members-changed",
767 G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
769 } else if (!tp_strdiff (id, "subscribe")) {
770 if (priv->subscribe != NULL)
772 priv->subscribe = g_object_ref (channel);
773 g_signal_connect (priv->subscribe, "group-members-changed",
774 G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
780 list_ensure_channel_cb (TpConnection *conn,
783 GHashTable *properties,
786 GObject *weak_object)
788 EmpathyTpContactList *list = user_data;
792 DEBUG ("failed: %s\n", error->message);
796 channel = tp_channel_new_from_properties (conn, path, properties, NULL);
797 got_list_channel (list, channel);
798 g_object_unref (channel);
802 iterate_on_channels (EmpathyTpContactList *list,
803 const GPtrArray *channels)
807 for (i = 0; i < channels->len ; i++) {
808 GValueArray *arr = g_ptr_array_index (channels, i);
810 GHashTable *properties;
811 TpHandleType handle_type;
813 path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
814 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
816 if (tp_strdiff (tp_asv_get_string (properties,
817 TP_IFACE_CHANNEL ".ChannelType"),
818 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST))
821 if (tp_asv_get_string (properties, TP_IFACE_CHANNEL ".TargetID") == NULL)
824 handle_type = tp_asv_get_uint32 (properties,
825 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
827 if (handle_type != TP_HANDLE_TYPE_GROUP)
830 tp_contact_list_group_add_channel (list, path, properties);
835 new_channels_cb (TpConnection *conn,
836 const GPtrArray *channels,
838 GObject *weak_object)
840 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
842 iterate_on_channels (list, channels);
846 got_channels_cb (TpProxy *conn,
850 GObject *weak_object)
852 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
853 const GPtrArray *channels;
856 DEBUG ("Get Channels property failed: %s", error->message);
860 channels = g_value_get_boxed (out);
861 iterate_on_channels (list, channels);
865 conn_ready_cb (TpConnection *connection,
869 EmpathyTpContactList *list = data;
870 EmpathyTpContactListPriv *priv = GET_PRIV (list);
874 DEBUG ("failed: %s", error->message);
878 /* Look for existing group channels */
879 tp_cli_dbus_properties_call_get (connection, -1,
880 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels", got_channels_cb,
881 NULL, NULL, G_OBJECT (list));
883 request = tp_asv_new (
884 TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
885 TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_UINT, TP_HANDLE_TYPE_LIST,
888 /* Watch the NewChannels signal so if ensuring list channels fails (for
889 * example because the server is slow and the D-Bus call timeouts before CM
890 * fetches the roster), we have a chance to get them later. */
891 tp_cli_connection_interface_requests_connect_to_new_channels (
892 priv->connection, new_channels_cb, NULL, NULL, G_OBJECT (list), NULL);
894 /* Request the 'stored' list. */
895 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "stored");
896 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
897 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
899 /* Request the 'publish' list. */
900 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "publish");
901 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
902 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
904 /* Request the 'subscribe' list. */
905 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "subscribe");
906 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
907 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
909 g_hash_table_unref (request);
911 g_object_unref (list);
915 tp_contact_list_constructed (GObject *list)
917 EmpathyTpContactListPriv *priv = GET_PRIV (list);
919 /* call GetAliasFlags */
920 if (tp_proxy_has_interface_by_id (priv->connection,
921 TP_IFACE_QUARK_CONNECTION_INTERFACE_ALIASING)) {
922 tp_cli_connection_interface_aliasing_call_get_alias_flags (
925 tp_contact_list_get_alias_flags_cb,
930 /* lookup RequestableChannelClasses */
931 if (tp_proxy_has_interface_by_id (priv->connection,
932 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS)) {
933 tp_cli_dbus_properties_call_get (priv->connection,
935 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
936 "RequestableChannelClasses",
937 tp_contact_list_get_requestablechannelclasses_cb,
941 /* we just don't know... better mark the flag just in case */
942 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
945 tp_connection_call_when_ready (priv->connection, conn_ready_cb,
946 g_object_ref (list));
950 tp_contact_list_get_property (GObject *object,
955 EmpathyTpContactListPriv *priv = GET_PRIV (object);
958 case PROP_CONNECTION:
959 g_value_set_object (value, priv->connection);
962 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
968 tp_contact_list_set_property (GObject *object,
973 EmpathyTpContactListPriv *priv = GET_PRIV (object);
976 case PROP_CONNECTION:
977 priv->connection = g_value_dup_object (value);
980 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
986 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
988 GObjectClass *object_class = G_OBJECT_CLASS (klass);
990 object_class->finalize = tp_contact_list_finalize;
991 object_class->constructed = tp_contact_list_constructed;
992 object_class->get_property = tp_contact_list_get_property;
993 object_class->set_property = tp_contact_list_set_property;
995 g_object_class_install_property (object_class,
997 g_param_spec_object ("connection",
999 "The connection associated with the contact list",
1002 G_PARAM_CONSTRUCT_ONLY));
1004 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
1008 tp_contact_list_array_free (gpointer handles)
1010 g_array_free (handles, TRUE);
1014 empathy_tp_contact_list_init (EmpathyTpContactList *list)
1016 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
1017 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
1021 /* Map group's name to group's TpChannel. The group name string is owned
1022 * by the TpChannel object */
1023 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1025 (GDestroyNotify) g_object_unref);
1027 /* Map contact's handle to EmpathyContact object */
1028 priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1030 (GDestroyNotify) g_object_unref);
1032 /* Map contact's handle to EmpathyContact object */
1033 priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1035 (GDestroyNotify) g_object_unref);
1037 /* Map group's name to GArray of handle */
1038 priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
1040 tp_contact_list_array_free);
1043 EmpathyTpContactList *
1044 empathy_tp_contact_list_new (TpConnection *connection)
1046 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
1047 "connection", connection,
1052 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
1054 EmpathyTpContactListPriv *priv;
1056 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
1058 priv = GET_PRIV (list);
1060 return priv->connection;
1064 tp_contact_list_add (EmpathyContactList *list,
1065 EmpathyContact *contact,
1066 const gchar *message)
1068 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1070 GArray handles = {(gchar *) &handle, 1};
1072 handle = empathy_contact_get_handle (contact);
1073 if (priv->subscribe) {
1074 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
1075 -1, &handles, message, NULL, NULL, NULL, NULL);
1077 if (priv->publish) {
1078 TpChannelGroupFlags flags = tp_channel_group_get_flags (priv->subscribe);
1079 if (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD ||
1080 g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
1081 tp_cli_channel_interface_group_call_add_members (priv->publish,
1082 -1, &handles, message, NULL, NULL, NULL, NULL);
1088 tp_contact_list_remove (EmpathyContactList *list,
1089 EmpathyContact *contact,
1090 const gchar *message)
1092 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1094 GArray handles = {(gchar *) &handle, 1};
1096 handle = empathy_contact_get_handle (contact);
1098 /* FIXME: this is racy if tp_contact_list_remove is called before the
1099 * 'stored' list has been retrieved. */
1100 if (priv->stored != NULL) {
1101 tp_cli_channel_interface_group_call_remove_members (priv->stored,
1102 -1, &handles, message, NULL, NULL, NULL, NULL);
1105 if (priv->subscribe) {
1106 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
1107 -1, &handles, message, NULL, NULL, NULL, NULL);
1109 if (priv->publish) {
1110 tp_cli_channel_interface_group_call_remove_members (priv->publish,
1111 -1, &handles, message, NULL, NULL, NULL, NULL);
1116 tp_contact_list_get_members (EmpathyContactList *list)
1118 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1121 ret = g_hash_table_get_values (priv->members);
1122 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1127 tp_contact_list_get_pendings (EmpathyContactList *list)
1129 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1132 ret = g_hash_table_get_values (priv->pendings);
1133 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1138 tp_contact_list_get_all_groups (EmpathyContactList *list)
1140 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1143 ret = g_hash_table_get_keys (priv->groups);
1144 for (l = ret; l; l = l->next) {
1145 l->data = g_strdup (l->data);
1152 tp_contact_list_get_groups (EmpathyContactList *list,
1153 EmpathyContact *contact)
1155 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1157 GHashTableIter iter;
1158 gpointer group_name;
1162 handle = empathy_contact_get_handle (contact);
1163 g_hash_table_iter_init (&iter, priv->groups);
1164 while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
1165 const TpIntSet *members;
1167 members = tp_channel_group_get_members (channel);
1168 if (tp_intset_is_member (members, handle)) {
1169 ret = g_list_prepend (ret, g_strdup (group_name));
1177 tp_contact_list_add_to_group (EmpathyContactList *list,
1178 EmpathyContact *contact,
1179 const gchar *group_name)
1184 handle = empathy_contact_get_handle (contact);
1185 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1186 g_array_append_val (handles, handle);
1187 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1188 group_name, handles);
1192 tp_contact_list_remove_from_group (EmpathyContactList *list,
1193 EmpathyContact *contact,
1194 const gchar *group_name)
1196 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1199 GArray handles = {(gchar *) &handle, 1};
1201 channel = g_hash_table_lookup (priv->groups, group_name);
1202 if (channel == NULL) {
1206 handle = empathy_contact_get_handle (contact);
1207 DEBUG ("remove contact %s (%d) from group %s",
1208 empathy_contact_get_id (contact), handle, group_name);
1210 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1211 &handles, NULL, NULL, NULL, NULL, NULL);
1215 tp_contact_list_rename_group (EmpathyContactList *list,
1216 const gchar *old_group_name,
1217 const gchar *new_group_name)
1219 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1221 const TpIntSet *members;
1224 channel = g_hash_table_lookup (priv->groups, old_group_name);
1225 if (channel == NULL) {
1229 DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1231 /* Remove all members and close the old channel */
1232 members = tp_channel_group_get_members (channel);
1233 handles = tp_intset_to_array (members);
1234 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1235 handles, NULL, NULL, NULL, NULL, NULL);
1236 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1238 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1239 new_group_name, handles);
1243 tp_contact_list_remove_group (EmpathyContactList *list,
1244 const gchar *group_name)
1246 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1248 const TpIntSet *members;
1251 channel = g_hash_table_lookup (priv->groups, group_name);
1252 if (channel == NULL) {
1256 DEBUG ("remove group %s", group_name);
1258 /* Remove all members and close the channel */
1259 members = tp_channel_group_get_members (channel);
1260 handles = tp_intset_to_array (members);
1261 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1262 handles, NULL, NULL, NULL, NULL, NULL);
1263 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1264 g_array_free (handles, TRUE);
1267 static EmpathyContactListFlags
1268 tp_contact_list_get_flags (EmpathyContactList *list)
1270 EmpathyTpContactListPriv *priv;
1271 EmpathyContactListFlags flags;
1273 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1275 priv = GET_PRIV (list);
1276 flags = priv->flags;
1278 if (priv->subscribe != NULL) {
1279 TpChannelGroupFlags group_flags;
1281 group_flags = tp_channel_group_get_flags (priv->subscribe);
1283 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1284 flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1287 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1288 flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1296 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1298 iface->add = tp_contact_list_add;
1299 iface->remove = tp_contact_list_remove;
1300 iface->get_members = tp_contact_list_get_members;
1301 iface->get_pendings = tp_contact_list_get_pendings;
1302 iface->get_all_groups = tp_contact_list_get_all_groups;
1303 iface->get_groups = tp_contact_list_get_groups;
1304 iface->add_to_group = tp_contact_list_add_to_group;
1305 iface->remove_from_group = tp_contact_list_remove_from_group;
1306 iface->rename_group = tp_contact_list_rename_group;
1307 iface->remove_group = tp_contact_list_remove_group;
1308 iface->get_flags = tp_contact_list_get_flags;
1312 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1314 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1315 GHashTableIter iter;
1318 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1320 /* Remove all contacts */
1321 g_hash_table_iter_init (&iter, priv->members);
1322 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1323 g_signal_emit_by_name (list, "members-changed", contact,
1327 g_hash_table_remove_all (priv->members);
1329 g_hash_table_iter_init (&iter, priv->pendings);
1330 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1331 g_signal_emit_by_name (list, "pendings-changed", contact,
1335 g_hash_table_remove_all (priv->pendings);