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;
46 const gchar *protocol_group;
51 GHashTable *members; /* handle -> EmpathyContact */
52 GHashTable *pendings; /* handle -> EmpathyContact */
53 GHashTable *groups; /* group name -> TpChannel */
54 GHashTable *add_to_group; /* group name -> GArray of handles */
56 EmpathyContactListFlags flags;
57 } EmpathyTpContactListPriv;
60 TP_CONTACT_LIST_TYPE_PUBLISH,
61 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
62 TP_CONTACT_LIST_TYPE_UNKNOWN
65 static void tp_contact_list_iface_init (EmpathyContactListIface *iface);
72 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
73 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
74 tp_contact_list_iface_init));
77 tp_contact_list_forget_group (EmpathyTpContactList *list,
80 EmpathyTpContactListPriv *priv = GET_PRIV (list);
81 const TpIntSet *members;
83 const gchar *group_name;
85 group_name = tp_channel_get_identifier (channel);
87 /* Signal that all members are not in that group anymore */
88 members = tp_channel_group_get_members (channel);
89 tp_intset_iter_init (&iter, members);
90 while (tp_intset_iter_next (&iter)) {
91 EmpathyContact *contact;
93 contact = g_hash_table_lookup (priv->members,
94 GUINT_TO_POINTER (iter.element));
95 if (contact == NULL) {
99 DEBUG ("Contact %s (%d) removed from group %s",
100 empathy_contact_get_id (contact), iter.element,
102 g_signal_emit_by_name (list, "groups-changed", contact,
109 tp_contact_list_group_invalidated_cb (TpChannel *channel,
113 EmpathyTpContactList *list)
115 EmpathyTpContactListPriv *priv = GET_PRIV (list);
116 const gchar *group_name;
118 group_name = tp_channel_get_identifier (channel);
119 DEBUG ("Group %s invalidated. Message: %s", group_name, message);
121 tp_contact_list_forget_group (list, channel);
123 g_hash_table_remove (priv->groups, group_name);
127 contacts_added_to_group (EmpathyTpContactList *list,
131 EmpathyTpContactListPriv *priv = GET_PRIV (list);
132 const gchar *group_name;
135 group_name = tp_channel_get_identifier (channel);
137 for (i = 0; i < added->len; i++) {
138 EmpathyContact *contact;
141 handle = g_array_index (added, TpHandle, i);
142 contact = g_hash_table_lookup (priv->members,
143 GUINT_TO_POINTER (handle));
144 if (contact == NULL) {
148 DEBUG ("Contact %s (%d) added to group %s",
149 empathy_contact_get_id (contact), handle, group_name);
150 g_signal_emit_by_name (list, "groups-changed", contact,
157 tp_contact_list_group_members_changed_cb (TpChannel *channel,
161 GArray *local_pending,
162 GArray *remote_pending,
165 EmpathyTpContactList *list)
167 EmpathyTpContactListPriv *priv = GET_PRIV (list);
168 const gchar *group_name;
171 contacts_added_to_group (list, channel, added);
173 group_name = tp_channel_get_identifier (channel);
175 for (i = 0; i < removed->len; i++) {
176 EmpathyContact *contact;
179 handle = g_array_index (removed, TpHandle, i);
180 contact = g_hash_table_lookup (priv->members,
181 GUINT_TO_POINTER (handle));
182 if (contact == NULL) {
186 DEBUG ("Contact %s (%d) removed from group %s",
187 empathy_contact_get_id (contact), handle, group_name);
189 g_signal_emit_by_name (list, "groups-changed", contact,
196 tp_contact_list_group_ready_cb (TpChannel *channel,
200 EmpathyTpContactListPriv *priv = GET_PRIV (list);
201 TpChannel *old_group;
202 const gchar *group_name;
203 const TpIntSet *members;
207 DEBUG ("Error: %s", error->message);
208 g_object_unref (channel);
212 group_name = tp_channel_get_identifier (channel);
214 /* If there's already a group with this name in the table, we can't
215 * just let it be replaced. Replacing it causes it to be unreffed,
216 * which causes it to be invalidated (see
217 * <https://bugs.freedesktop.org/show_bug.cgi?id=22119>), which causes
218 * it to be removed from the hash table again, which causes it to be
221 old_group = g_hash_table_lookup (priv->groups, group_name);
223 if (old_group != NULL) {
224 DEBUG ("Discarding old group %s (%p)", group_name, old_group);
225 g_hash_table_steal (priv->groups, group_name);
226 tp_contact_list_forget_group (list, old_group);
227 g_object_unref (old_group);
230 g_hash_table_insert (priv->groups, (gpointer) group_name, channel);
231 DEBUG ("Group %s added", group_name);
233 g_signal_connect (channel, "group-members-changed",
234 G_CALLBACK (tp_contact_list_group_members_changed_cb),
237 g_signal_connect (channel, "invalidated",
238 G_CALLBACK (tp_contact_list_group_invalidated_cb),
241 if (priv->add_to_group) {
244 handles = g_hash_table_lookup (priv->add_to_group, group_name);
246 DEBUG ("Adding initial members to group %s", group_name);
247 tp_cli_channel_interface_group_call_add_members (channel,
248 -1, handles, NULL, NULL, NULL, NULL, NULL);
249 g_hash_table_remove (priv->add_to_group, group_name);
253 /* Get initial members of the group */
254 members = tp_channel_group_get_members (channel);
255 g_assert (members != NULL);
256 arr = tp_intset_to_array (members);
257 contacts_added_to_group (list, channel, arr);
258 g_array_free (arr, TRUE);
262 tp_contact_list_group_add_channel (EmpathyTpContactList *list,
263 const gchar *object_path,
264 const gchar *channel_type,
265 TpHandleType handle_type,
268 EmpathyTpContactListPriv *priv = GET_PRIV (list);
271 /* Only accept server-side contact groups */
272 if (tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) ||
273 handle_type != TP_HANDLE_TYPE_GROUP) {
277 channel = tp_channel_new (priv->connection,
278 object_path, channel_type,
279 handle_type, handle, NULL);
281 /* Give the ref to the callback */
282 tp_channel_call_when_ready (channel,
283 tp_contact_list_group_ready_cb,
288 tp_contact_list_group_request_channel_cb (TpConnection *connection,
289 const gchar *object_path,
294 /* The new channel will be handled in NewChannel cb. Here we only
295 * handle the error if RequestChannel failed */
297 DEBUG ("Error: %s", error->message);
303 tp_contact_list_group_request_handles_cb (TpConnection *connection,
304 const GArray *handles,
309 TpHandle channel_handle;
312 DEBUG ("Error: %s", error->message);
316 channel_handle = g_array_index (handles, TpHandle, 0);
317 tp_cli_connection_call_request_channel (connection, -1,
318 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
319 TP_HANDLE_TYPE_GROUP,
322 tp_contact_list_group_request_channel_cb,
327 /* This function takes ownership of handles array */
329 tp_contact_list_group_add (EmpathyTpContactList *list,
330 const gchar *group_name,
333 EmpathyTpContactListPriv *priv = GET_PRIV (list);
335 const gchar *names[] = {group_name, NULL};
337 /* Search the channel for that group name */
338 channel = g_hash_table_lookup (priv->groups, group_name);
340 tp_cli_channel_interface_group_call_add_members (channel, -1,
341 handles, NULL, NULL, NULL, NULL, NULL);
342 g_array_free (handles, TRUE);
346 /* That group does not exist yet, we have to:
347 * 1) Request an handle for the group name
348 * 2) Request a channel
349 * 3) When NewChannel is emitted, add handles in members
351 g_hash_table_insert (priv->add_to_group,
352 g_strdup (group_name),
354 tp_cli_connection_call_request_handles (priv->connection, -1,
355 TP_HANDLE_TYPE_GROUP, names,
356 tp_contact_list_group_request_handles_cb,
362 tp_contact_list_got_added_members_cb (EmpathyTpContactFactory *factory,
364 EmpathyContact * const * contacts,
366 const TpHandle *failed,
371 EmpathyTpContactListPriv *priv = GET_PRIV (list);
375 DEBUG ("Error: %s", error->message);
379 for (i = 0; i < n_contacts; i++) {
380 EmpathyContact *contact = contacts[i];
383 handle = empathy_contact_get_handle (contact);
384 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
387 /* Add to the list and emit signal */
388 g_hash_table_insert (priv->members, GUINT_TO_POINTER (handle),
389 g_object_ref (contact));
390 g_signal_emit_by_name (list, "members-changed", contact,
393 /* This contact is now member, implicitly accept pending. */
394 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
395 GArray handles = {(gchar *) &handle, 1};
397 tp_cli_channel_interface_group_call_add_members (priv->publish,
398 -1, &handles, NULL, NULL, NULL, NULL, NULL);
404 tp_contact_list_got_local_pending_cb (EmpathyTpContactFactory *factory,
406 EmpathyContact * const * contacts,
408 const TpHandle *failed,
413 EmpathyTpContactListPriv *priv = GET_PRIV (list);
417 DEBUG ("Error: %s", error->message);
421 for (i = 0; i < n_contacts; i++) {
422 EmpathyContact *contact = contacts[i];
424 const gchar *message;
425 TpChannelGroupChangeReason reason;
427 handle = empathy_contact_get_handle (contact);
428 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle))) {
429 GArray handles = {(gchar *) &handle, 1};
431 /* This contact is already member, auto accept. */
432 tp_cli_channel_interface_group_call_add_members (priv->publish,
433 -1, &handles, NULL, NULL, NULL, NULL, NULL);
435 else if (tp_channel_group_get_local_pending_info (priv->publish,
440 /* Add contact to pendings */
441 g_hash_table_insert (priv->pendings, GUINT_TO_POINTER (handle),
442 g_object_ref (contact));
443 g_signal_emit_by_name (list, "pendings-changed", contact,
444 contact, reason, message, TRUE);
450 tp_contact_list_remove_handle (EmpathyTpContactList *list,
454 EmpathyTpContactListPriv *priv = GET_PRIV (list);
455 EmpathyContact *contact;
458 if (table == priv->pendings)
459 sig = "pendings-changed";
460 else if (table == priv->members)
461 sig = "members-changed";
465 contact = g_hash_table_lookup (table, GUINT_TO_POINTER (handle));
467 g_object_ref (contact);
468 g_hash_table_remove (table, GUINT_TO_POINTER (handle));
469 g_signal_emit_by_name (list, sig, contact, 0, 0, NULL,
471 g_object_unref (contact);
476 tp_contact_list_publish_group_members_changed_cb (TpChannel *channel,
480 GArray *local_pending,
481 GArray *remote_pending,
483 TpChannelGroupChangeReason reason,
484 EmpathyTpContactList *list)
486 EmpathyTpContactListPriv *priv = GET_PRIV (list);
489 /* We now send our presence to those contacts, remove them from pendings */
490 for (i = 0; i < added->len; i++) {
491 tp_contact_list_remove_handle (list, priv->pendings,
492 g_array_index (added, TpHandle, i));
495 /* We refuse to send our presence to those contacts, remove from pendings */
496 for (i = 0; i < removed->len; i++) {
497 tp_contact_list_remove_handle (list, priv->pendings,
498 g_array_index (removed, TpHandle, i));
501 /* Those contacts want our presence, auto accept those that are already
502 * member, otherwise add in pendings. */
503 if (local_pending->len > 0) {
504 empathy_tp_contact_factory_get_from_handles (priv->factory,
505 local_pending->len, (TpHandle *) local_pending->data,
506 tp_contact_list_got_local_pending_cb, NULL, NULL,
512 tp_contact_list_get_alias_flags_cb (TpConnection *connection,
518 EmpathyTpContactListPriv *priv = GET_PRIV (list);
521 DEBUG ("Error: %s", error->message);
525 if (flags & TP_CONNECTION_ALIAS_FLAG_USER_SET) {
526 priv->flags |= EMPATHY_CONTACT_LIST_CAN_ALIAS;
531 tp_contact_list_get_requestablechannelclasses_cb (TpProxy *connection,
537 EmpathyTpContactListPriv *priv = GET_PRIV (list);
542 DEBUG ("Error: %s", error->message);
546 classes = g_value_get_boxed (value);
547 for (i = 0; i < classes->len; i++) {
548 GValueArray *class = g_ptr_array_index (classes, i);
550 const char *channel_type;
553 props = g_value_get_boxed (g_value_array_get_nth (class, 0));
555 channel_type = tp_asv_get_string (props,
556 TP_IFACE_CHANNEL ".ChannelType");
557 handle_type = tp_asv_get_uint32 (props,
558 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
560 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) &&
561 handle_type == TP_HANDLE_TYPE_GROUP) {
562 DEBUG ("Got channel class for a contact group");
563 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
570 tp_contact_list_subscribe_group_members_changed_cb (TpChannel *channel,
574 GArray *local_pending,
575 GArray *remote_pending,
578 EmpathyTpContactList *list)
580 EmpathyTpContactListPriv *priv = GET_PRIV (list);
583 /* We now get the presence of those contacts, add them to members */
584 if (added->len > 0) {
585 empathy_tp_contact_factory_get_from_handles (priv->factory,
586 added->len, (TpHandle *) added->data,
587 tp_contact_list_got_added_members_cb, NULL, NULL,
591 /* Those contacts refuse to send us their presence, remove from members. */
592 for (i = 0; i < removed->len; i++) {
593 tp_contact_list_remove_handle (list, priv->members,
594 g_array_index (removed, TpHandle, i));
597 /* We want those contacts in our contact list but we don't get their
598 * presence yet. Add to members anyway. */
599 if (remote_pending->len > 0) {
600 empathy_tp_contact_factory_get_from_handles (priv->factory,
601 remote_pending->len, (TpHandle *) remote_pending->data,
602 tp_contact_list_got_added_members_cb, NULL, NULL,
608 tp_contact_list_new_channel_cb (TpConnection *proxy,
609 const gchar *object_path,
610 const gchar *channel_type,
613 gboolean suppress_handler,
617 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
618 object_path, channel_type,
619 handle_type, handle);
623 tp_contact_list_list_channels_cb (TpConnection *connection,
624 const GPtrArray *channels,
632 DEBUG ("Error: %s", error->message);
636 for (i = 0; i < channels->len; i++) {
637 GValueArray *chan_struct;
638 const gchar *object_path;
639 const gchar *channel_type;
640 TpHandleType handle_type;
643 chan_struct = g_ptr_array_index (channels, i);
644 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
645 channel_type = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
646 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
647 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
649 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
650 object_path, channel_type,
651 handle_type, handle);
656 tp_contact_list_finalize (GObject *object)
658 EmpathyTpContactListPriv *priv;
659 EmpathyTpContactList *list;
663 list = EMPATHY_TP_CONTACT_LIST (object);
664 priv = GET_PRIV (list);
666 DEBUG ("finalize: %p", object);
668 if (priv->subscribe) {
669 g_object_unref (priv->subscribe);
672 g_object_unref (priv->publish);
675 g_object_unref (priv->stored);
678 if (priv->connection) {
679 g_object_unref (priv->connection);
683 g_object_unref (priv->factory);
686 g_hash_table_iter_init (&iter, priv->groups);
687 while (g_hash_table_iter_next (&iter, NULL, &channel)) {
688 g_signal_handlers_disconnect_by_func (channel,
689 tp_contact_list_group_invalidated_cb, list);
692 g_hash_table_destroy (priv->groups);
693 g_hash_table_destroy (priv->members);
694 g_hash_table_destroy (priv->pendings);
695 g_hash_table_destroy (priv->add_to_group);
697 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
701 got_list_channel (EmpathyTpContactList *list,
704 EmpathyTpContactListPriv *priv = GET_PRIV (list);
707 /* We requested that channel by providing TargetID property, so it's
708 * guaranteed that tp_channel_get_identifier will return it. */
709 id = tp_channel_get_identifier (channel);
711 /* TpChannel emits initial set of members just before being ready */
712 if (!tp_strdiff (id, "stored")) {
713 if (priv->stored != NULL)
715 priv->stored = g_object_ref (channel);
716 } else if (!tp_strdiff (id, "publish")) {
717 if (priv->publish != NULL)
719 priv->publish = g_object_ref (channel);
720 g_signal_connect (priv->publish, "group-members-changed",
721 G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
723 } else if (!tp_strdiff (id, "subscribe")) {
724 if (priv->subscribe != NULL)
726 priv->subscribe = g_object_ref (channel);
727 g_signal_connect (priv->subscribe, "group-members-changed",
728 G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
734 list_ensure_channel_cb (TpConnection *conn,
737 GHashTable *properties,
740 GObject *weak_object)
742 EmpathyTpContactList *list = user_data;
746 DEBUG ("failed: %s\n", error->message);
750 channel = tp_channel_new_from_properties (conn, path, properties, NULL);
751 got_list_channel (list, channel);
752 g_object_unref (channel);
756 new_channels_cb (TpConnection *conn,
757 const GPtrArray *channels,
759 GObject *weak_object)
761 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
764 for (i = 0; i < channels->len ; i++) {
765 GValueArray *arr = g_ptr_array_index (channels, i);
767 GHashTable *properties;
771 path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
772 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
774 if (tp_strdiff (tp_asv_get_string (properties,
775 TP_IFACE_CHANNEL ".ChannelType"),
776 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST))
779 if (tp_asv_get_uint32 (properties,
780 TP_IFACE_CHANNEL ".TargetHandleType", NULL)
781 != TP_HANDLE_TYPE_LIST)
784 id = tp_asv_get_string (properties,
785 TP_IFACE_CHANNEL ".TargetID");
789 channel = tp_channel_new_from_properties (conn, path,
791 got_list_channel (list, channel);
792 g_object_unref (channel);
797 conn_ready_cb (TpConnection *connection,
801 EmpathyTpContactList *list = data;
802 EmpathyTpContactListPriv *priv = GET_PRIV (list);
806 DEBUG ("failed: %s", error->message);
810 request = tp_asv_new (
811 TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
812 TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_UINT, TP_HANDLE_TYPE_LIST,
815 /* Watch the NewChannels signal so if ensuring list channels fails (for
816 * example because the server is slow and the D-Bus call timeouts before CM
817 * fetches the roster), we have a chance to get them later. */
818 tp_cli_connection_interface_requests_connect_to_new_channels (
819 priv->connection, new_channels_cb, NULL, NULL, G_OBJECT (list), NULL);
821 /* Request the 'stored' list. */
822 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "stored");
823 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
824 -1, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
826 /* Request the 'publish' list. */
827 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "publish");
828 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
829 -1, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
831 /* Request the 'subscribe' list. */
832 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "subscribe");
833 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
834 -1, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
836 g_hash_table_unref (request);
838 g_object_unref (list);
842 tp_contact_list_constructed (GObject *list)
844 EmpathyTpContactListPriv *priv = GET_PRIV (list);
845 gchar *protocol_name = NULL;
847 priv->factory = empathy_tp_contact_factory_dup_singleton (priv->connection);
849 /* call GetAliasFlags */
850 if (tp_proxy_has_interface_by_id (priv->connection,
851 TP_IFACE_QUARK_CONNECTION_INTERFACE_ALIASING)) {
852 tp_cli_connection_interface_aliasing_call_get_alias_flags (
855 tp_contact_list_get_alias_flags_cb,
860 /* lookup RequestableChannelClasses */
861 if (tp_proxy_has_interface_by_id (priv->connection,
862 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS)) {
863 tp_cli_dbus_properties_call_get (priv->connection,
865 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
866 "RequestableChannelClasses",
867 tp_contact_list_get_requestablechannelclasses_cb,
871 /* we just don't know... better mark the flag just in case */
872 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
875 tp_connection_call_when_ready (priv->connection, conn_ready_cb,
876 g_object_ref (list));
878 tp_cli_connection_call_list_channels (priv->connection, -1,
879 tp_contact_list_list_channels_cb,
883 tp_cli_connection_connect_to_new_channel (priv->connection,
884 tp_contact_list_new_channel_cb,
888 /* Check for protocols that does not support contact groups. We can
889 * put all contacts into a special group in that case.
890 * FIXME: Default group should be an information in the profile */
891 tp_connection_parse_object_path (priv->connection, &protocol_name, NULL);
892 if (!tp_strdiff (protocol_name, "local-xmpp")) {
893 priv->protocol_group = _("People nearby");
895 g_free (protocol_name);
899 tp_contact_list_get_property (GObject *object,
904 EmpathyTpContactListPriv *priv = GET_PRIV (object);
907 case PROP_CONNECTION:
908 g_value_set_object (value, priv->connection);
911 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
917 tp_contact_list_set_property (GObject *object,
922 EmpathyTpContactListPriv *priv = GET_PRIV (object);
925 case PROP_CONNECTION:
926 priv->connection = g_value_dup_object (value);
929 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
935 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
937 GObjectClass *object_class = G_OBJECT_CLASS (klass);
939 object_class->finalize = tp_contact_list_finalize;
940 object_class->constructed = tp_contact_list_constructed;
941 object_class->get_property = tp_contact_list_get_property;
942 object_class->set_property = tp_contact_list_set_property;
944 g_object_class_install_property (object_class,
946 g_param_spec_object ("connection",
948 "The connection associated with the contact list",
951 G_PARAM_CONSTRUCT_ONLY));
953 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
957 tp_contact_list_array_free (gpointer handles)
959 g_array_free (handles, TRUE);
963 empathy_tp_contact_list_init (EmpathyTpContactList *list)
965 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
966 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
970 /* Map group's name to group's TpChannel. The group name string is owned
971 * by the TpChannel object */
972 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
974 (GDestroyNotify) g_object_unref);
976 /* Map contact's handle to EmpathyContact object */
977 priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
979 (GDestroyNotify) g_object_unref);
981 /* Map contact's handle to EmpathyContact object */
982 priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
984 (GDestroyNotify) g_object_unref);
986 /* Map group's name to GArray of handle */
987 priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
989 tp_contact_list_array_free);
992 EmpathyTpContactList *
993 empathy_tp_contact_list_new (TpConnection *connection)
995 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
996 "connection", connection,
1001 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
1003 EmpathyTpContactListPriv *priv;
1005 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
1007 priv = GET_PRIV (list);
1009 return priv->connection;
1013 tp_contact_list_add (EmpathyContactList *list,
1014 EmpathyContact *contact,
1015 const gchar *message)
1017 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1019 GArray handles = {(gchar *) &handle, 1};
1021 handle = empathy_contact_get_handle (contact);
1022 if (priv->subscribe) {
1023 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
1024 -1, &handles, message, NULL, NULL, NULL, NULL);
1026 if (priv->publish) {
1027 TpChannelGroupFlags flags = tp_channel_group_get_flags (priv->subscribe);
1028 if (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD ||
1029 g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
1030 tp_cli_channel_interface_group_call_add_members (priv->publish,
1031 -1, &handles, message, NULL, NULL, NULL, NULL);
1037 tp_contact_list_remove (EmpathyContactList *list,
1038 EmpathyContact *contact,
1039 const gchar *message)
1041 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1043 GArray handles = {(gchar *) &handle, 1};
1045 handle = empathy_contact_get_handle (contact);
1047 /* FIXME: this is racy if tp_contact_list_remove is called before the
1048 * 'stored' list has been retrieved. */
1049 if (priv->stored != NULL) {
1050 tp_cli_channel_interface_group_call_remove_members (priv->stored,
1051 -1, &handles, message, NULL, NULL, NULL, NULL);
1054 if (priv->subscribe) {
1055 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
1056 -1, &handles, message, NULL, NULL, NULL, NULL);
1058 if (priv->publish) {
1059 tp_cli_channel_interface_group_call_remove_members (priv->publish,
1060 -1, &handles, message, NULL, NULL, NULL, NULL);
1065 tp_contact_list_get_members (EmpathyContactList *list)
1067 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1070 ret = g_hash_table_get_values (priv->members);
1071 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1076 tp_contact_list_get_pendings (EmpathyContactList *list)
1078 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1081 ret = g_hash_table_get_values (priv->pendings);
1082 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1087 tp_contact_list_get_all_groups (EmpathyContactList *list)
1089 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1092 ret = g_hash_table_get_keys (priv->groups);
1093 for (l = ret; l; l = l->next) {
1094 l->data = g_strdup (l->data);
1097 if (priv->protocol_group) {
1098 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
1105 tp_contact_list_get_groups (EmpathyContactList *list,
1106 EmpathyContact *contact)
1108 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1110 GHashTableIter iter;
1111 gpointer group_name;
1115 handle = empathy_contact_get_handle (contact);
1116 g_hash_table_iter_init (&iter, priv->groups);
1117 while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
1118 const TpIntSet *members;
1120 members = tp_channel_group_get_members (channel);
1121 if (tp_intset_is_member (members, handle)) {
1122 ret = g_list_prepend (ret, g_strdup (group_name));
1126 if (priv->protocol_group) {
1127 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
1134 tp_contact_list_add_to_group (EmpathyContactList *list,
1135 EmpathyContact *contact,
1136 const gchar *group_name)
1141 handle = empathy_contact_get_handle (contact);
1142 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1143 g_array_append_val (handles, handle);
1144 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1145 group_name, handles);
1149 tp_contact_list_remove_from_group (EmpathyContactList *list,
1150 EmpathyContact *contact,
1151 const gchar *group_name)
1153 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1156 GArray handles = {(gchar *) &handle, 1};
1158 channel = g_hash_table_lookup (priv->groups, group_name);
1159 if (channel == NULL) {
1163 handle = empathy_contact_get_handle (contact);
1164 DEBUG ("remove contact %s (%d) from group %s",
1165 empathy_contact_get_id (contact), handle, group_name);
1167 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1168 &handles, NULL, NULL, NULL, NULL, NULL);
1172 tp_contact_list_rename_group (EmpathyContactList *list,
1173 const gchar *old_group_name,
1174 const gchar *new_group_name)
1176 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1178 const TpIntSet *members;
1181 channel = g_hash_table_lookup (priv->groups, old_group_name);
1182 if (channel == NULL) {
1186 DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1188 /* Remove all members and close the old channel */
1189 members = tp_channel_group_get_members (channel);
1190 handles = tp_intset_to_array (members);
1191 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1192 handles, NULL, NULL, NULL, NULL, NULL);
1193 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1195 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1196 new_group_name, handles);
1200 tp_contact_list_remove_group (EmpathyContactList *list,
1201 const gchar *group_name)
1203 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1205 const TpIntSet *members;
1208 channel = g_hash_table_lookup (priv->groups, group_name);
1209 if (channel == NULL) {
1213 DEBUG ("remove group %s", group_name);
1215 /* Remove all members and close the channel */
1216 members = tp_channel_group_get_members (channel);
1217 handles = tp_intset_to_array (members);
1218 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1219 handles, NULL, NULL, NULL, NULL, NULL);
1220 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1221 g_array_free (handles, TRUE);
1224 static EmpathyContactListFlags
1225 tp_contact_list_get_flags (EmpathyContactList *list)
1227 EmpathyTpContactListPriv *priv;
1228 EmpathyContactListFlags flags;
1230 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1232 priv = GET_PRIV (list);
1233 flags = priv->flags;
1235 if (priv->subscribe != NULL) {
1236 TpChannelGroupFlags group_flags;
1238 group_flags = tp_channel_group_get_flags (priv->subscribe);
1240 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1241 flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1244 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1245 flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1253 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1255 iface->add = tp_contact_list_add;
1256 iface->remove = tp_contact_list_remove;
1257 iface->get_members = tp_contact_list_get_members;
1258 iface->get_pendings = tp_contact_list_get_pendings;
1259 iface->get_all_groups = tp_contact_list_get_all_groups;
1260 iface->get_groups = tp_contact_list_get_groups;
1261 iface->add_to_group = tp_contact_list_add_to_group;
1262 iface->remove_from_group = tp_contact_list_remove_from_group;
1263 iface->rename_group = tp_contact_list_rename_group;
1264 iface->remove_group = tp_contact_list_remove_group;
1265 iface->get_flags = tp_contact_list_get_flags;
1269 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1271 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1272 GHashTableIter iter;
1275 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1277 /* Remove all contacts */
1278 g_hash_table_iter_init (&iter, priv->members);
1279 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1280 g_signal_emit_by_name (list, "members-changed", contact,
1284 g_hash_table_remove_all (priv->members);
1286 g_hash_table_iter_init (&iter, priv->pendings);
1287 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1288 g_signal_emit_by_name (list, "pendings-changed", contact,
1292 g_hash_table_remove_all (priv->pendings);