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 tp_contact_list_group_ready_cb (TpChannel *channel,
131 EmpathyTpContactListPriv *priv = GET_PRIV (list);
132 TpChannel *old_group;
133 const gchar *group_name;
136 DEBUG ("Error: %s", error->message);
137 g_object_unref (channel);
141 group_name = tp_channel_get_identifier (channel);
143 /* If there's already a group with this name in the table, we can't
144 * just let it be replaced. Replacing it causes it to be unreffed,
145 * which causes it to be invalidated (see
146 * <https://bugs.freedesktop.org/show_bug.cgi?id=22119>), which causes
147 * it to be removed from the hash table again, which causes it to be
150 old_group = g_hash_table_lookup (priv->groups, group_name);
152 if (old_group != NULL) {
153 DEBUG ("Discarding old group %s (%p)", group_name, old_group);
154 g_hash_table_steal (priv->groups, group_name);
155 tp_contact_list_forget_group (list, old_group);
156 g_object_unref (old_group);
159 g_hash_table_insert (priv->groups, (gpointer) group_name, channel);
160 DEBUG ("Group %s added", group_name);
162 g_signal_connect (channel, "invalidated",
163 G_CALLBACK (tp_contact_list_group_invalidated_cb),
166 if (priv->add_to_group) {
169 handles = g_hash_table_lookup (priv->add_to_group, group_name);
171 DEBUG ("Adding initial members to group %s", group_name);
172 tp_cli_channel_interface_group_call_add_members (channel,
173 -1, handles, NULL, NULL, NULL, NULL, NULL);
174 g_hash_table_remove (priv->add_to_group, group_name);
180 tp_contact_list_group_members_changed_cb (TpChannel *channel,
184 GArray *local_pending,
185 GArray *remote_pending,
188 EmpathyTpContactList *list)
190 EmpathyTpContactListPriv *priv = GET_PRIV (list);
191 const gchar *group_name;
194 group_name = tp_channel_get_identifier (channel);
196 for (i = 0; i < added->len; i++) {
197 EmpathyContact *contact;
200 handle = g_array_index (added, TpHandle, i);
201 contact = g_hash_table_lookup (priv->members,
202 GUINT_TO_POINTER (handle));
203 if (contact == NULL) {
207 DEBUG ("Contact %s (%d) added to group %s",
208 empathy_contact_get_id (contact), handle, group_name);
209 g_signal_emit_by_name (list, "groups-changed", contact,
214 for (i = 0; i < removed->len; i++) {
215 EmpathyContact *contact;
218 handle = g_array_index (removed, TpHandle, i);
219 contact = g_hash_table_lookup (priv->members,
220 GUINT_TO_POINTER (handle));
221 if (contact == NULL) {
225 DEBUG ("Contact %s (%d) removed from group %s",
226 empathy_contact_get_id (contact), handle, group_name);
228 g_signal_emit_by_name (list, "groups-changed", contact,
235 tp_contact_list_group_add_channel (EmpathyTpContactList *list,
236 const gchar *object_path,
237 const gchar *channel_type,
238 TpHandleType handle_type,
241 EmpathyTpContactListPriv *priv = GET_PRIV (list);
244 /* Only accept server-side contact groups */
245 if (tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) ||
246 handle_type != TP_HANDLE_TYPE_GROUP) {
250 channel = tp_channel_new (priv->connection,
251 object_path, channel_type,
252 handle_type, handle, NULL);
254 /* TpChannel emits initial set of members just before being ready */
255 g_signal_connect (channel, "group-members-changed",
256 G_CALLBACK (tp_contact_list_group_members_changed_cb),
259 /* Give the ref to the callback */
260 tp_channel_call_when_ready (channel,
261 tp_contact_list_group_ready_cb,
266 tp_contact_list_group_request_channel_cb (TpConnection *connection,
267 const gchar *object_path,
272 /* The new channel will be handled in NewChannel cb. Here we only
273 * handle the error if RequestChannel failed */
275 DEBUG ("Error: %s", error->message);
281 tp_contact_list_group_request_handles_cb (TpConnection *connection,
282 const GArray *handles,
287 TpHandle channel_handle;
290 DEBUG ("Error: %s", error->message);
294 channel_handle = g_array_index (handles, TpHandle, 0);
295 tp_cli_connection_call_request_channel (connection, -1,
296 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
297 TP_HANDLE_TYPE_GROUP,
300 tp_contact_list_group_request_channel_cb,
305 /* This function takes ownership of handles array */
307 tp_contact_list_group_add (EmpathyTpContactList *list,
308 const gchar *group_name,
311 EmpathyTpContactListPriv *priv = GET_PRIV (list);
313 const gchar *names[] = {group_name, NULL};
315 /* Search the channel for that group name */
316 channel = g_hash_table_lookup (priv->groups, group_name);
318 tp_cli_channel_interface_group_call_add_members (channel, -1,
319 handles, NULL, NULL, NULL, NULL, NULL);
320 g_array_free (handles, TRUE);
324 /* That group does not exist yet, we have to:
325 * 1) Request an handle for the group name
326 * 2) Request a channel
327 * 3) When NewChannel is emitted, add handles in members
329 g_hash_table_insert (priv->add_to_group,
330 g_strdup (group_name),
332 tp_cli_connection_call_request_handles (priv->connection, -1,
333 TP_HANDLE_TYPE_GROUP, names,
334 tp_contact_list_group_request_handles_cb,
340 tp_contact_list_got_added_members_cb (EmpathyTpContactFactory *factory,
342 EmpathyContact * const * contacts,
344 const TpHandle *failed,
349 EmpathyTpContactListPriv *priv = GET_PRIV (list);
353 DEBUG ("Error: %s", error->message);
357 for (i = 0; i < n_contacts; i++) {
358 EmpathyContact *contact = contacts[i];
361 handle = empathy_contact_get_handle (contact);
362 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
365 /* Add to the list and emit signal */
366 g_hash_table_insert (priv->members, GUINT_TO_POINTER (handle),
367 g_object_ref (contact));
368 g_signal_emit_by_name (list, "members-changed", contact,
371 /* This contact is now member, implicitly accept pending. */
372 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
373 GArray handles = {(gchar *) &handle, 1};
375 tp_cli_channel_interface_group_call_add_members (priv->publish,
376 -1, &handles, NULL, NULL, NULL, NULL, NULL);
382 tp_contact_list_got_local_pending_cb (EmpathyTpContactFactory *factory,
384 EmpathyContact * const * contacts,
386 const TpHandle *failed,
391 EmpathyTpContactListPriv *priv = GET_PRIV (list);
395 DEBUG ("Error: %s", error->message);
399 for (i = 0; i < n_contacts; i++) {
400 EmpathyContact *contact = contacts[i];
402 const gchar *message;
403 TpChannelGroupChangeReason reason;
405 handle = empathy_contact_get_handle (contact);
406 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle))) {
407 GArray handles = {(gchar *) &handle, 1};
409 /* This contact is already member, auto accept. */
410 tp_cli_channel_interface_group_call_add_members (priv->publish,
411 -1, &handles, NULL, NULL, NULL, NULL, NULL);
413 else if (tp_channel_group_get_local_pending_info (priv->publish,
418 /* Add contact to pendings */
419 g_hash_table_insert (priv->pendings, GUINT_TO_POINTER (handle),
420 g_object_ref (contact));
421 g_signal_emit_by_name (list, "pendings-changed", contact,
422 contact, reason, message, TRUE);
428 tp_contact_list_remove_handle (EmpathyTpContactList *list,
432 EmpathyTpContactListPriv *priv = GET_PRIV (list);
433 EmpathyContact *contact;
436 if (table == priv->pendings)
437 sig = "pendings-changed";
438 else if (table == priv->members)
439 sig = "members-changed";
443 contact = g_hash_table_lookup (table, GUINT_TO_POINTER (handle));
445 g_object_ref (contact);
446 g_hash_table_remove (table, GUINT_TO_POINTER (handle));
447 g_signal_emit_by_name (list, sig, contact, 0, 0, NULL,
449 g_object_unref (contact);
454 tp_contact_list_publish_group_members_changed_cb (TpChannel *channel,
458 GArray *local_pending,
459 GArray *remote_pending,
461 TpChannelGroupChangeReason reason,
462 EmpathyTpContactList *list)
464 EmpathyTpContactListPriv *priv = GET_PRIV (list);
467 /* We now send our presence to those contacts, remove them from pendings */
468 for (i = 0; i < added->len; i++) {
469 tp_contact_list_remove_handle (list, priv->pendings,
470 g_array_index (added, TpHandle, i));
473 /* We refuse to send our presence to those contacts, remove from pendings */
474 for (i = 0; i < removed->len; i++) {
475 tp_contact_list_remove_handle (list, priv->pendings,
476 g_array_index (removed, TpHandle, i));
479 /* Those contacts want our presence, auto accept those that are already
480 * member, otherwise add in pendings. */
481 if (local_pending->len > 0) {
482 empathy_tp_contact_factory_get_from_handles (priv->factory,
483 local_pending->len, (TpHandle *) local_pending->data,
484 tp_contact_list_got_local_pending_cb, NULL, NULL,
490 tp_contact_list_get_alias_flags_cb (TpConnection *connection,
496 EmpathyTpContactListPriv *priv = GET_PRIV (list);
499 DEBUG ("Error: %s", error->message);
503 if (flags & TP_CONNECTION_ALIAS_FLAG_USER_SET) {
504 priv->flags |= EMPATHY_CONTACT_LIST_CAN_ALIAS;
509 tp_contact_list_get_requestablechannelclasses_cb (TpProxy *connection,
515 EmpathyTpContactListPriv *priv = GET_PRIV (list);
520 DEBUG ("Error: %s", error->message);
524 classes = g_value_get_boxed (value);
525 for (i = 0; i < classes->len; i++) {
526 GValueArray *class = g_ptr_array_index (classes, i);
528 const char *channel_type;
531 props = g_value_get_boxed (g_value_array_get_nth (class, 0));
533 channel_type = tp_asv_get_string (props,
534 TP_IFACE_CHANNEL ".ChannelType");
535 handle_type = tp_asv_get_uint32 (props,
536 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
538 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) &&
539 handle_type == TP_HANDLE_TYPE_GROUP) {
540 DEBUG ("Got channel class for a contact group");
541 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
548 tp_contact_list_subscribe_group_members_changed_cb (TpChannel *channel,
552 GArray *local_pending,
553 GArray *remote_pending,
556 EmpathyTpContactList *list)
558 EmpathyTpContactListPriv *priv = GET_PRIV (list);
561 /* We now get the presence of those contacts, add them to members */
562 if (added->len > 0) {
563 empathy_tp_contact_factory_get_from_handles (priv->factory,
564 added->len, (TpHandle *) added->data,
565 tp_contact_list_got_added_members_cb, NULL, NULL,
569 /* Those contacts refuse to send us their presence, remove from members. */
570 for (i = 0; i < removed->len; i++) {
571 tp_contact_list_remove_handle (list, priv->members,
572 g_array_index (removed, TpHandle, i));
575 /* We want those contacts in our contact list but we don't get their
576 * presence yet. Add to members anyway. */
577 if (remote_pending->len > 0) {
578 empathy_tp_contact_factory_get_from_handles (priv->factory,
579 remote_pending->len, (TpHandle *) remote_pending->data,
580 tp_contact_list_got_added_members_cb, NULL, NULL,
586 tp_contact_list_new_channel_cb (TpConnection *proxy,
587 const gchar *object_path,
588 const gchar *channel_type,
591 gboolean suppress_handler,
595 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
596 object_path, channel_type,
597 handle_type, handle);
601 tp_contact_list_list_channels_cb (TpConnection *connection,
602 const GPtrArray *channels,
610 DEBUG ("Error: %s", error->message);
614 for (i = 0; i < channels->len; i++) {
615 GValueArray *chan_struct;
616 const gchar *object_path;
617 const gchar *channel_type;
618 TpHandleType handle_type;
621 chan_struct = g_ptr_array_index (channels, i);
622 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
623 channel_type = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
624 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
625 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
627 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
628 object_path, channel_type,
629 handle_type, handle);
634 tp_contact_list_finalize (GObject *object)
636 EmpathyTpContactListPriv *priv;
637 EmpathyTpContactList *list;
641 list = EMPATHY_TP_CONTACT_LIST (object);
642 priv = GET_PRIV (list);
644 DEBUG ("finalize: %p", object);
646 if (priv->subscribe) {
647 g_object_unref (priv->subscribe);
650 g_object_unref (priv->publish);
653 g_object_unref (priv->stored);
656 if (priv->connection) {
657 g_object_unref (priv->connection);
661 g_object_unref (priv->factory);
664 g_hash_table_iter_init (&iter, priv->groups);
665 while (g_hash_table_iter_next (&iter, NULL, &channel)) {
666 g_signal_handlers_disconnect_by_func (channel,
667 tp_contact_list_group_invalidated_cb, list);
670 g_hash_table_destroy (priv->groups);
671 g_hash_table_destroy (priv->members);
672 g_hash_table_destroy (priv->pendings);
673 g_hash_table_destroy (priv->add_to_group);
675 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
679 list_ensure_channel_cb (TpConnection *conn,
682 GHashTable *properties,
685 GObject *weak_object)
687 EmpathyTpContactList *list = user_data;
688 EmpathyTpContactListPriv *priv = GET_PRIV (list);
693 DEBUG ("failed: %s\n", error->message);
697 /* We requested that channel by providing TargetID property, so it's
698 * guaranteed that tp_channel_get_identifier will return it. */
699 channel = tp_channel_new_from_properties (conn, path, properties, NULL);
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 priv->stored = channel;
705 } else if (!tp_strdiff (id, "publish")) {
706 priv->publish = channel;
707 g_signal_connect (priv->publish, "group-members-changed",
708 G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
710 } else if (!tp_strdiff (id, "subscribe")) {
711 priv->subscribe = channel;
712 g_signal_connect (priv->subscribe, "group-members-changed",
713 G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
716 g_warn_if_reached ();
717 g_object_unref (channel);
722 conn_ready_cb (TpConnection *connection,
726 EmpathyTpContactList *list = data;
727 EmpathyTpContactListPriv *priv = GET_PRIV (list);
731 DEBUG ("failed: %s", error->message);
735 request = tp_asv_new (
736 TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
737 TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_UINT, TP_HANDLE_TYPE_LIST,
740 /* Request the 'stored' list. */
741 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "stored");
742 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
743 -1, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
745 /* Request the 'publish' list. */
746 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "publish");
747 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
748 -1, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
750 /* Request the 'subscribe' list. */
751 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "subscribe");
752 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
753 -1, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
755 g_hash_table_unref (request);
757 g_object_unref (list);
761 tp_contact_list_constructed (GObject *list)
763 EmpathyTpContactListPriv *priv = GET_PRIV (list);
764 gchar *protocol_name = NULL;
766 priv->factory = empathy_tp_contact_factory_dup_singleton (priv->connection);
768 /* call GetAliasFlags */
769 if (tp_proxy_has_interface_by_id (priv->connection,
770 TP_IFACE_QUARK_CONNECTION_INTERFACE_ALIASING)) {
771 tp_cli_connection_interface_aliasing_call_get_alias_flags (
774 tp_contact_list_get_alias_flags_cb,
779 /* lookup RequestableChannelClasses */
780 if (tp_proxy_has_interface_by_id (priv->connection,
781 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS)) {
782 tp_cli_dbus_properties_call_get (priv->connection,
784 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
785 "RequestableChannelClasses",
786 tp_contact_list_get_requestablechannelclasses_cb,
790 /* we just don't know... better mark the flag just in case */
791 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
794 tp_connection_call_when_ready (priv->connection, conn_ready_cb,
795 g_object_ref (list));
797 tp_cli_connection_call_list_channels (priv->connection, -1,
798 tp_contact_list_list_channels_cb,
802 tp_cli_connection_connect_to_new_channel (priv->connection,
803 tp_contact_list_new_channel_cb,
807 /* Check for protocols that does not support contact groups. We can
808 * put all contacts into a special group in that case.
809 * FIXME: Default group should be an information in the profile */
810 tp_connection_parse_object_path (priv->connection, &protocol_name, NULL);
811 if (!tp_strdiff (protocol_name, "local-xmpp")) {
812 priv->protocol_group = _("People nearby");
814 g_free (protocol_name);
818 tp_contact_list_get_property (GObject *object,
823 EmpathyTpContactListPriv *priv = GET_PRIV (object);
826 case PROP_CONNECTION:
827 g_value_set_object (value, priv->connection);
830 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
836 tp_contact_list_set_property (GObject *object,
841 EmpathyTpContactListPriv *priv = GET_PRIV (object);
844 case PROP_CONNECTION:
845 priv->connection = g_value_dup_object (value);
848 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
854 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
856 GObjectClass *object_class = G_OBJECT_CLASS (klass);
858 object_class->finalize = tp_contact_list_finalize;
859 object_class->constructed = tp_contact_list_constructed;
860 object_class->get_property = tp_contact_list_get_property;
861 object_class->set_property = tp_contact_list_set_property;
863 g_object_class_install_property (object_class,
865 g_param_spec_object ("connection",
867 "The connection associated with the contact list",
870 G_PARAM_CONSTRUCT_ONLY));
872 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
876 tp_contact_list_array_free (gpointer handles)
878 g_array_free (handles, TRUE);
882 empathy_tp_contact_list_init (EmpathyTpContactList *list)
884 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
885 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
889 /* Map group's name to group's TpChannel. The group name string is owned
890 * by the TpChannel object */
891 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
893 (GDestroyNotify) g_object_unref);
895 /* Map contact's handle to EmpathyContact object */
896 priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
898 (GDestroyNotify) g_object_unref);
900 /* Map contact's handle to EmpathyContact object */
901 priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
903 (GDestroyNotify) g_object_unref);
905 /* Map group's name to GArray of handle */
906 priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
908 tp_contact_list_array_free);
911 EmpathyTpContactList *
912 empathy_tp_contact_list_new (TpConnection *connection)
914 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
915 "connection", connection,
920 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
922 EmpathyTpContactListPriv *priv;
924 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
926 priv = GET_PRIV (list);
928 return priv->connection;
932 tp_contact_list_add (EmpathyContactList *list,
933 EmpathyContact *contact,
934 const gchar *message)
936 EmpathyTpContactListPriv *priv = GET_PRIV (list);
938 GArray handles = {(gchar *) &handle, 1};
940 handle = empathy_contact_get_handle (contact);
941 if (priv->subscribe) {
942 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
943 -1, &handles, message, NULL, NULL, NULL, NULL);
946 TpChannelGroupFlags flags = tp_channel_group_get_flags (priv->subscribe);
947 if (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD ||
948 g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
949 tp_cli_channel_interface_group_call_add_members (priv->publish,
950 -1, &handles, message, NULL, NULL, NULL, NULL);
956 tp_contact_list_remove (EmpathyContactList *list,
957 EmpathyContact *contact,
958 const gchar *message)
960 EmpathyTpContactListPriv *priv = GET_PRIV (list);
962 GArray handles = {(gchar *) &handle, 1};
964 handle = empathy_contact_get_handle (contact);
966 /* FIXME: this is racy if tp_contact_list_remove is called before the
967 * 'stored' list has been retrieved. */
968 if (priv->stored != NULL) {
969 tp_cli_channel_interface_group_call_remove_members (priv->stored,
970 -1, &handles, message, NULL, NULL, NULL, NULL);
971 /* Contact will be removed from 'publish' and 'subscribe' too */
975 if (priv->subscribe) {
976 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
977 -1, &handles, message, NULL, NULL, NULL, NULL);
980 tp_cli_channel_interface_group_call_remove_members (priv->publish,
981 -1, &handles, message, NULL, NULL, NULL, NULL);
986 tp_contact_list_get_members (EmpathyContactList *list)
988 EmpathyTpContactListPriv *priv = GET_PRIV (list);
991 ret = g_hash_table_get_values (priv->members);
992 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
997 tp_contact_list_get_pendings (EmpathyContactList *list)
999 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1002 ret = g_hash_table_get_values (priv->pendings);
1003 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1008 tp_contact_list_get_all_groups (EmpathyContactList *list)
1010 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1013 ret = g_hash_table_get_keys (priv->groups);
1014 for (l = ret; l; l = l->next) {
1015 l->data = g_strdup (l->data);
1018 if (priv->protocol_group) {
1019 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
1026 tp_contact_list_get_groups (EmpathyContactList *list,
1027 EmpathyContact *contact)
1029 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1031 GHashTableIter iter;
1032 gpointer group_name;
1036 handle = empathy_contact_get_handle (contact);
1037 g_hash_table_iter_init (&iter, priv->groups);
1038 while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
1039 const TpIntSet *members;
1041 members = tp_channel_group_get_members (channel);
1042 if (tp_intset_is_member (members, handle)) {
1043 ret = g_list_prepend (ret, g_strdup (group_name));
1047 if (priv->protocol_group) {
1048 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
1055 tp_contact_list_add_to_group (EmpathyContactList *list,
1056 EmpathyContact *contact,
1057 const gchar *group_name)
1062 handle = empathy_contact_get_handle (contact);
1063 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1064 g_array_append_val (handles, handle);
1065 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1066 group_name, handles);
1070 tp_contact_list_remove_from_group (EmpathyContactList *list,
1071 EmpathyContact *contact,
1072 const gchar *group_name)
1074 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1077 GArray handles = {(gchar *) &handle, 1};
1079 channel = g_hash_table_lookup (priv->groups, group_name);
1080 if (channel == NULL) {
1084 handle = empathy_contact_get_handle (contact);
1085 DEBUG ("remove contact %s (%d) from group %s",
1086 empathy_contact_get_id (contact), handle, group_name);
1088 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1089 &handles, NULL, NULL, NULL, NULL, NULL);
1093 tp_contact_list_rename_group (EmpathyContactList *list,
1094 const gchar *old_group_name,
1095 const gchar *new_group_name)
1097 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1099 const TpIntSet *members;
1102 channel = g_hash_table_lookup (priv->groups, old_group_name);
1103 if (channel == NULL) {
1107 DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1109 /* Remove all members and close the old channel */
1110 members = tp_channel_group_get_members (channel);
1111 handles = tp_intset_to_array (members);
1112 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1113 handles, NULL, NULL, NULL, NULL, NULL);
1114 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1116 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1117 new_group_name, handles);
1121 tp_contact_list_remove_group (EmpathyContactList *list,
1122 const gchar *group_name)
1124 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1126 const TpIntSet *members;
1129 channel = g_hash_table_lookup (priv->groups, group_name);
1130 if (channel == NULL) {
1134 DEBUG ("remove group %s", group_name);
1136 /* Remove all members and close the channel */
1137 members = tp_channel_group_get_members (channel);
1138 handles = tp_intset_to_array (members);
1139 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1140 handles, NULL, NULL, NULL, NULL, NULL);
1141 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1142 g_array_free (handles, TRUE);
1145 static EmpathyContactListFlags
1146 tp_contact_list_get_flags (EmpathyContactList *list)
1148 EmpathyTpContactListPriv *priv;
1149 EmpathyContactListFlags flags;
1151 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1153 priv = GET_PRIV (list);
1154 flags = priv->flags;
1156 if (priv->subscribe != NULL) {
1157 TpChannelGroupFlags group_flags;
1159 group_flags = tp_channel_group_get_flags (priv->subscribe);
1161 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1162 flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1165 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1166 flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1174 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1176 iface->add = tp_contact_list_add;
1177 iface->remove = tp_contact_list_remove;
1178 iface->get_members = tp_contact_list_get_members;
1179 iface->get_pendings = tp_contact_list_get_pendings;
1180 iface->get_all_groups = tp_contact_list_get_all_groups;
1181 iface->get_groups = tp_contact_list_get_groups;
1182 iface->add_to_group = tp_contact_list_add_to_group;
1183 iface->remove_from_group = tp_contact_list_remove_from_group;
1184 iface->rename_group = tp_contact_list_rename_group;
1185 iface->remove_group = tp_contact_list_remove_group;
1186 iface->get_flags = tp_contact_list_get_flags;
1190 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1192 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1193 GHashTableIter iter;
1196 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1198 /* Remove all contacts */
1199 g_hash_table_iter_init (&iter, priv->members);
1200 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1201 g_signal_emit_by_name (list, "members-changed", contact,
1205 g_hash_table_remove_all (priv->members);
1207 g_hash_table_iter_init (&iter, priv->pendings);
1208 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1209 g_signal_emit_by_name (list, "pendings-changed", contact,
1213 g_hash_table_remove_all (priv->pendings);