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>
33 #include "empathy-tp-contact-list.h"
34 #include "empathy-tp-contact-factory.h"
35 #include "empathy-contact-list.h"
36 #include "empathy-utils.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
39 #include "empathy-debug.h"
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactList)
43 EmpathyTpContactFactory *factory;
44 TpConnection *connection;
45 const gchar *protocol_group;
49 GHashTable *members; /* handle -> EmpathyContact */
50 GHashTable *pendings; /* handle -> EmpathyContact */
51 GHashTable *groups; /* group name -> TpChannel */
52 GHashTable *add_to_group; /* group name -> GArray of handles */
54 EmpathyContactListFlags flags;
55 } EmpathyTpContactListPriv;
58 TP_CONTACT_LIST_TYPE_PUBLISH,
59 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
60 TP_CONTACT_LIST_TYPE_UNKNOWN
63 static void tp_contact_list_iface_init (EmpathyContactListIface *iface);
70 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
71 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
72 tp_contact_list_iface_init));
75 tp_contact_list_forget_group (EmpathyTpContactList *list,
78 EmpathyTpContactListPriv *priv = GET_PRIV (list);
79 const TpIntSet *members;
81 const gchar *group_name;
83 group_name = tp_channel_get_identifier (channel);
85 /* Signal that all members are not in that group anymore */
86 members = tp_channel_group_get_members (channel);
87 tp_intset_iter_init (&iter, members);
88 while (tp_intset_iter_next (&iter)) {
89 EmpathyContact *contact;
91 contact = g_hash_table_lookup (priv->members,
92 GUINT_TO_POINTER (iter.element));
93 if (contact == NULL) {
97 DEBUG ("Contact %s (%d) removed from group %s",
98 empathy_contact_get_id (contact), iter.element,
100 g_signal_emit_by_name (list, "groups-changed", contact,
107 tp_contact_list_group_invalidated_cb (TpChannel *channel,
111 EmpathyTpContactList *list)
113 EmpathyTpContactListPriv *priv = GET_PRIV (list);
114 const gchar *group_name;
116 group_name = tp_channel_get_identifier (channel);
117 DEBUG ("Group %s invalidated. Message: %s", group_name, message);
119 tp_contact_list_forget_group (list, channel);
121 g_hash_table_remove (priv->groups, group_name);
125 tp_contact_list_group_ready_cb (TpChannel *channel,
129 EmpathyTpContactListPriv *priv = GET_PRIV (list);
130 TpChannel *old_group;
131 const gchar *group_name;
134 DEBUG ("Error: %s", error->message);
135 g_object_unref (channel);
139 group_name = tp_channel_get_identifier (channel);
141 /* If there's already a group with this name in the table, we can't
142 * just let it be replaced. Replacing it causes it to be unreffed,
143 * which causes it to be invalidated (see
144 * <https://bugs.freedesktop.org/show_bug.cgi?id=22119>), which causes
145 * it to be removed from the hash table again, which causes it to be
148 old_group = g_hash_table_lookup (priv->groups, group_name);
150 if (old_group != NULL) {
151 DEBUG ("Discarding old group %s (%p)", group_name, old_group);
152 g_hash_table_steal (priv->groups, group_name);
153 tp_contact_list_forget_group (list, old_group);
154 g_object_unref (old_group);
157 g_hash_table_insert (priv->groups, (gpointer) group_name, channel);
158 DEBUG ("Group %s added", group_name);
160 g_signal_connect (channel, "invalidated",
161 G_CALLBACK (tp_contact_list_group_invalidated_cb),
164 if (priv->add_to_group) {
167 handles = g_hash_table_lookup (priv->add_to_group, group_name);
169 DEBUG ("Adding initial members to group %s", group_name);
170 tp_cli_channel_interface_group_call_add_members (channel,
171 -1, handles, NULL, NULL, NULL, NULL, NULL);
172 g_hash_table_remove (priv->add_to_group, group_name);
178 tp_contact_list_group_members_changed_cb (TpChannel *channel,
182 GArray *local_pending,
183 GArray *remote_pending,
186 EmpathyTpContactList *list)
188 EmpathyTpContactListPriv *priv = GET_PRIV (list);
189 const gchar *group_name;
192 group_name = tp_channel_get_identifier (channel);
194 for (i = 0; i < added->len; i++) {
195 EmpathyContact *contact;
198 handle = g_array_index (added, TpHandle, i);
199 contact = g_hash_table_lookup (priv->members,
200 GUINT_TO_POINTER (handle));
201 if (contact == NULL) {
205 DEBUG ("Contact %s (%d) added to group %s",
206 empathy_contact_get_id (contact), handle, group_name);
207 g_signal_emit_by_name (list, "groups-changed", contact,
212 for (i = 0; i < removed->len; i++) {
213 EmpathyContact *contact;
216 handle = g_array_index (removed, TpHandle, i);
217 contact = g_hash_table_lookup (priv->members,
218 GUINT_TO_POINTER (handle));
219 if (contact == NULL) {
223 DEBUG ("Contact %s (%d) removed from group %s",
224 empathy_contact_get_id (contact), handle, group_name);
226 g_signal_emit_by_name (list, "groups-changed", contact,
233 tp_contact_list_group_add_channel (EmpathyTpContactList *list,
234 const gchar *object_path,
235 const gchar *channel_type,
236 TpHandleType handle_type,
239 EmpathyTpContactListPriv *priv = GET_PRIV (list);
242 /* Only accept server-side contact groups */
243 if (tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) ||
244 handle_type != TP_HANDLE_TYPE_GROUP) {
248 channel = tp_channel_new (priv->connection,
249 object_path, channel_type,
250 handle_type, handle, NULL);
252 /* TpChannel emits initial set of members just before being ready */
253 g_signal_connect (channel, "group-members-changed",
254 G_CALLBACK (tp_contact_list_group_members_changed_cb),
257 /* Give the ref to the callback */
258 tp_channel_call_when_ready (channel,
259 tp_contact_list_group_ready_cb,
264 tp_contact_list_group_request_channel_cb (TpConnection *connection,
265 const gchar *object_path,
270 /* The new channel will be handled in NewChannel cb. Here we only
271 * handle the error if RequestChannel failed */
273 DEBUG ("Error: %s", error->message);
279 tp_contact_list_group_request_handles_cb (TpConnection *connection,
280 const GArray *handles,
285 TpHandle channel_handle;
288 DEBUG ("Error: %s", error->message);
292 channel_handle = g_array_index (handles, TpHandle, 0);
293 tp_cli_connection_call_request_channel (connection, -1,
294 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
295 TP_HANDLE_TYPE_GROUP,
298 tp_contact_list_group_request_channel_cb,
303 /* This function takes ownership of handles array */
305 tp_contact_list_group_add (EmpathyTpContactList *list,
306 const gchar *group_name,
309 EmpathyTpContactListPriv *priv = GET_PRIV (list);
311 const gchar *names[] = {group_name, NULL};
313 /* Search the channel for that group name */
314 channel = g_hash_table_lookup (priv->groups, group_name);
316 tp_cli_channel_interface_group_call_add_members (channel, -1,
317 handles, NULL, NULL, NULL, NULL, NULL);
318 g_array_free (handles, TRUE);
322 /* That group does not exist yet, we have to:
323 * 1) Request an handle for the group name
324 * 2) Request a channel
325 * 3) When NewChannel is emitted, add handles in members
327 g_hash_table_insert (priv->add_to_group,
328 g_strdup (group_name),
330 tp_cli_connection_call_request_handles (priv->connection, -1,
331 TP_HANDLE_TYPE_GROUP, names,
332 tp_contact_list_group_request_handles_cb,
338 tp_contact_list_got_added_members_cb (EmpathyTpContactFactory *factory,
340 EmpathyContact * const * contacts,
342 const TpHandle *failed,
347 EmpathyTpContactListPriv *priv = GET_PRIV (list);
351 DEBUG ("Error: %s", error->message);
355 for (i = 0; i < n_contacts; i++) {
356 EmpathyContact *contact = contacts[i];
359 handle = empathy_contact_get_handle (contact);
360 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
363 /* Add to the list and emit signal */
364 g_hash_table_insert (priv->members, GUINT_TO_POINTER (handle),
365 g_object_ref (contact));
366 g_signal_emit_by_name (list, "members-changed", contact,
369 /* This contact is now member, implicitly accept pending. */
370 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
371 GArray handles = {(gchar *) &handle, 1};
373 tp_cli_channel_interface_group_call_add_members (priv->publish,
374 -1, &handles, NULL, NULL, NULL, NULL, NULL);
380 tp_contact_list_got_local_pending_cb (EmpathyTpContactFactory *factory,
382 EmpathyContact * const * contacts,
384 const TpHandle *failed,
389 EmpathyTpContactListPriv *priv = GET_PRIV (list);
393 DEBUG ("Error: %s", error->message);
397 for (i = 0; i < n_contacts; i++) {
398 EmpathyContact *contact = contacts[i];
400 const gchar *message;
401 TpChannelGroupChangeReason reason;
403 handle = empathy_contact_get_handle (contact);
404 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle))) {
405 GArray handles = {(gchar *) &handle, 1};
407 /* This contact is already member, auto accept. */
408 tp_cli_channel_interface_group_call_add_members (priv->publish,
409 -1, &handles, NULL, NULL, NULL, NULL, NULL);
411 else if (tp_channel_group_get_local_pending_info (priv->publish,
416 /* Add contact to pendings */
417 g_hash_table_insert (priv->pendings, GUINT_TO_POINTER (handle),
418 g_object_ref (contact));
419 g_signal_emit_by_name (list, "pendings-changed", contact,
420 contact, reason, message, TRUE);
426 tp_contact_list_remove_handle (EmpathyTpContactList *list,
430 EmpathyTpContactListPriv *priv = GET_PRIV (list);
431 EmpathyContact *contact;
434 if (table == priv->pendings)
435 signal = "pendings-changed";
436 else if (table == priv->members)
437 signal = "members-changed";
441 contact = g_hash_table_lookup (table, GUINT_TO_POINTER (handle));
443 g_object_ref (contact);
444 g_hash_table_remove (table, GUINT_TO_POINTER (handle));
445 g_signal_emit_by_name (list, signal, contact, 0, 0, NULL,
447 g_object_unref (contact);
452 tp_contact_list_publish_group_members_changed_cb (TpChannel *channel,
456 GArray *local_pending,
457 GArray *remote_pending,
459 TpChannelGroupChangeReason reason,
460 EmpathyTpContactList *list)
462 EmpathyTpContactListPriv *priv = GET_PRIV (list);
465 /* We now send our presence to those contacts, remove them from pendings */
466 for (i = 0; i < added->len; i++) {
467 tp_contact_list_remove_handle (list, priv->pendings,
468 g_array_index (added, TpHandle, i));
471 /* We refuse to send our presence to those contacts, remove from pendings */
472 for (i = 0; i < removed->len; i++) {
473 tp_contact_list_remove_handle (list, priv->pendings,
474 g_array_index (added, TpHandle, i));
477 /* Those contacts want our presence, auto accept those that are already
478 * member, otherwise add in pendings. */
479 if (local_pending->len > 0) {
480 empathy_tp_contact_factory_get_from_handles (priv->factory,
481 local_pending->len, (TpHandle *) local_pending->data,
482 tp_contact_list_got_local_pending_cb, NULL, NULL,
488 tp_contact_list_publish_request_channel_cb (TpConnection *connection,
489 const gchar *object_path,
494 EmpathyTpContactListPriv *priv = GET_PRIV (list);
497 DEBUG ("Error: %s", error->message);
501 priv->publish = tp_channel_new (connection, object_path,
502 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
504 GPOINTER_TO_UINT (user_data),
507 /* TpChannel emits initial set of members just before being ready */
508 g_signal_connect (priv->publish, "group-members-changed",
509 G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
514 tp_contact_list_get_alias_flags_cb (TpConnection *connection,
520 EmpathyTpContactListPriv *priv = GET_PRIV (list);
523 DEBUG ("Error: %s", error->message);
527 if (flags & TP_CONNECTION_ALIAS_FLAG_USER_SET) {
528 priv->flags |= EMPATHY_CONTACT_LIST_CAN_ALIAS;
533 tp_contact_list_publish_request_handle_cb (TpConnection *connection,
534 const GArray *handles,
542 DEBUG ("Error: %s", error->message);
546 handle = g_array_index (handles, TpHandle, 0);
547 tp_cli_connection_call_request_channel (connection, -1,
548 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
552 tp_contact_list_publish_request_channel_cb,
553 GUINT_TO_POINTER (handle), NULL,
558 tp_contact_list_subscribe_group_members_changed_cb (TpChannel *channel,
562 GArray *local_pending,
563 GArray *remote_pending,
566 EmpathyTpContactList *list)
568 EmpathyTpContactListPriv *priv = GET_PRIV (list);
571 /* We now get the presence of those contacts, add them to members */
572 if (added->len > 0) {
573 empathy_tp_contact_factory_get_from_handles (priv->factory,
574 added->len, (TpHandle *) added->data,
575 tp_contact_list_got_added_members_cb, NULL, NULL,
579 /* Those contacts refuse to send us their presence, remove from members. */
580 for (i = 0; i < removed->len; i++) {
581 tp_contact_list_remove_handle (list, priv->members,
582 g_array_index (added, TpHandle, i));
585 /* We want those contacts in our contact list but we don't get their
586 * presence yet. Add to members anyway. */
587 if (remote_pending->len > 0) {
588 empathy_tp_contact_factory_get_from_handles (priv->factory,
589 remote_pending->len, (TpHandle *) remote_pending->data,
590 tp_contact_list_got_added_members_cb, NULL, NULL,
596 tp_contact_list_subscribe_request_channel_cb (TpConnection *connection,
597 const gchar *object_path,
602 EmpathyTpContactListPriv *priv = GET_PRIV (list);
605 DEBUG ("Error: %s", error->message);
609 priv->subscribe = tp_channel_new (connection, object_path,
610 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
612 GPOINTER_TO_UINT (user_data),
615 /* TpChannel emits initial set of members just before being ready */
616 g_signal_connect (priv->subscribe, "group-members-changed",
617 G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
622 tp_contact_list_subscribe_request_handle_cb (TpConnection *connection,
623 const GArray *handles,
631 DEBUG ("Error: %s", error->message);
635 handle = g_array_index (handles, TpHandle, 0);
636 tp_cli_connection_call_request_channel (connection, -1,
637 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
641 tp_contact_list_subscribe_request_channel_cb,
642 GUINT_TO_POINTER (handle), NULL,
647 tp_contact_list_new_channel_cb (TpConnection *proxy,
648 const gchar *object_path,
649 const gchar *channel_type,
652 gboolean suppress_handler,
656 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
657 object_path, channel_type,
658 handle_type, handle);
662 tp_contact_list_list_channels_cb (TpConnection *connection,
663 const GPtrArray *channels,
671 DEBUG ("Error: %s", error->message);
675 for (i = 0; i < channels->len; i++) {
676 GValueArray *chan_struct;
677 const gchar *object_path;
678 const gchar *channel_type;
679 TpHandleType handle_type;
682 chan_struct = g_ptr_array_index (channels, i);
683 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
684 channel_type = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
685 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
686 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
688 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
689 object_path, channel_type,
690 handle_type, handle);
695 tp_contact_list_finalize (GObject *object)
697 EmpathyTpContactListPriv *priv;
698 EmpathyTpContactList *list;
702 list = EMPATHY_TP_CONTACT_LIST (object);
703 priv = GET_PRIV (list);
705 DEBUG ("finalize: %p", object);
707 if (priv->subscribe) {
708 g_object_unref (priv->subscribe);
711 g_object_unref (priv->publish);
714 if (priv->connection) {
715 g_object_unref (priv->connection);
719 g_object_unref (priv->factory);
722 g_hash_table_iter_init (&iter, priv->groups);
723 while (g_hash_table_iter_next (&iter, NULL, &channel)) {
724 g_signal_handlers_disconnect_by_func (channel,
725 tp_contact_list_group_invalidated_cb, list);
728 g_hash_table_destroy (priv->groups);
729 g_hash_table_destroy (priv->members);
730 g_hash_table_destroy (priv->pendings);
731 g_hash_table_destroy (priv->add_to_group);
733 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
737 tp_contact_list_constructed (GObject *list)
740 EmpathyTpContactListPriv *priv = GET_PRIV (list);
741 gchar *protocol_name = NULL;
742 const gchar *names[] = {NULL, NULL};
744 priv->factory = empathy_tp_contact_factory_dup_singleton (priv->connection);
746 /* call GetAliasFlags() */
747 if (tp_proxy_has_interface_by_id (priv->connection,
748 TP_IFACE_QUARK_CONNECTION_INTERFACE_ALIASING)) {
749 tp_cli_connection_interface_aliasing_call_get_alias_flags (
752 tp_contact_list_get_alias_flags_cb,
757 /* FIXME: lookup RequestableChannelClasses */
759 names[0] = "publish";
760 tp_cli_connection_call_request_handles (priv->connection,
764 tp_contact_list_publish_request_handle_cb,
767 names[0] = "subscribe";
768 tp_cli_connection_call_request_handles (priv->connection,
772 tp_contact_list_subscribe_request_handle_cb,
776 tp_cli_connection_call_list_channels (priv->connection, -1,
777 tp_contact_list_list_channels_cb,
781 tp_cli_connection_connect_to_new_channel (priv->connection,
782 tp_contact_list_new_channel_cb,
786 /* Check for protocols that does not support contact groups. We can
787 * put all contacts into a special group in that case.
788 * FIXME: Default group should be an information in the profile */
789 tp_connection_parse_object_path (priv->connection, &protocol_name, NULL);
790 if (!tp_strdiff (protocol_name, "local-xmpp")) {
791 priv->protocol_group = _("People nearby");
793 g_free (protocol_name);
797 tp_contact_list_get_property (GObject *object,
802 EmpathyTpContactListPriv *priv = GET_PRIV (object);
805 case PROP_CONNECTION:
806 g_value_set_object (value, priv->connection);
809 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
815 tp_contact_list_set_property (GObject *object,
820 EmpathyTpContactListPriv *priv = GET_PRIV (object);
823 case PROP_CONNECTION:
824 priv->connection = g_value_dup_object (value);
827 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
833 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
835 GObjectClass *object_class = G_OBJECT_CLASS (klass);
837 object_class->finalize = tp_contact_list_finalize;
838 object_class->constructed = tp_contact_list_constructed;
839 object_class->get_property = tp_contact_list_get_property;
840 object_class->set_property = tp_contact_list_set_property;
842 g_object_class_install_property (object_class,
844 g_param_spec_object ("connection",
846 "The connection associated with the contact list",
849 G_PARAM_CONSTRUCT_ONLY));
851 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
855 tp_contact_list_array_free (gpointer handles)
857 g_array_free (handles, TRUE);
861 empathy_tp_contact_list_init (EmpathyTpContactList *list)
863 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
864 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
868 /* Map group's name to group's TpChannel. The group name string is owned
869 * by the TpChannel object */
870 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
872 (GDestroyNotify) g_object_unref);
874 /* Map contact's handle to EmpathyContact object */
875 priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
877 (GDestroyNotify) g_object_unref);
879 /* Map contact's handle to EmpathyContact object */
880 priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
882 (GDestroyNotify) g_object_unref);
884 /* Map group's name to GArray of handle */
885 priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
887 tp_contact_list_array_free);
890 EmpathyTpContactList *
891 empathy_tp_contact_list_new (TpConnection *connection)
893 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
894 "connection", connection,
899 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
901 EmpathyTpContactListPriv *priv;
903 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
905 priv = GET_PRIV (list);
907 return priv->connection;
911 tp_contact_list_add (EmpathyContactList *list,
912 EmpathyContact *contact,
913 const gchar *message)
915 EmpathyTpContactListPriv *priv = GET_PRIV (list);
917 GArray handles = {(gchar *) &handle, 1};
919 handle = empathy_contact_get_handle (contact);
920 if (priv->subscribe) {
921 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
922 -1, &handles, message, NULL, NULL, NULL, NULL);
925 g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
926 tp_cli_channel_interface_group_call_add_members (priv->publish,
927 -1, &handles, message, NULL, NULL, NULL, NULL);
932 tp_contact_list_remove (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_remove_members (priv->subscribe,
943 -1, &handles, message, NULL, NULL, NULL, NULL);
946 tp_cli_channel_interface_group_call_remove_members (priv->publish,
947 -1, &handles, message, NULL, NULL, NULL, NULL);
952 tp_contact_list_get_members (EmpathyContactList *list)
954 EmpathyTpContactListPriv *priv = GET_PRIV (list);
957 ret = g_hash_table_get_values (priv->members);
958 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
963 tp_contact_list_get_pendings (EmpathyContactList *list)
965 EmpathyTpContactListPriv *priv = GET_PRIV (list);
968 ret = g_hash_table_get_values (priv->pendings);
969 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
974 tp_contact_list_get_all_groups (EmpathyContactList *list)
976 EmpathyTpContactListPriv *priv = GET_PRIV (list);
979 ret = g_hash_table_get_keys (priv->groups);
980 for (l = ret; l; l = l->next) {
981 l->data = g_strdup (l->data);
984 if (priv->protocol_group) {
985 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
992 tp_contact_list_get_groups (EmpathyContactList *list,
993 EmpathyContact *contact)
995 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1002 handle = empathy_contact_get_handle (contact);
1003 g_hash_table_iter_init (&iter, priv->groups);
1004 while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
1005 const TpIntSet *members;
1007 members = tp_channel_group_get_members (channel);
1008 if (tp_intset_is_member (members, handle)) {
1009 ret = g_list_prepend (ret, g_strdup (group_name));
1013 if (priv->protocol_group) {
1014 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
1021 tp_contact_list_add_to_group (EmpathyContactList *list,
1022 EmpathyContact *contact,
1023 const gchar *group_name)
1028 handle = empathy_contact_get_handle (contact);
1029 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1030 g_array_append_val (handles, handle);
1031 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1032 group_name, handles);
1036 tp_contact_list_remove_from_group (EmpathyContactList *list,
1037 EmpathyContact *contact,
1038 const gchar *group_name)
1040 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1043 GArray handles = {(gchar *) &handle, 1};
1045 channel = g_hash_table_lookup (priv->groups, group_name);
1046 if (channel == NULL) {
1050 handle = empathy_contact_get_handle (contact);
1051 DEBUG ("remove contact %s (%d) from group %s",
1052 empathy_contact_get_id (contact), handle, group_name);
1054 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1055 &handles, NULL, NULL, NULL, NULL, NULL);
1059 tp_contact_list_rename_group (EmpathyContactList *list,
1060 const gchar *old_group_name,
1061 const gchar *new_group_name)
1063 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1065 const TpIntSet *members;
1068 channel = g_hash_table_lookup (priv->groups, old_group_name);
1069 if (channel == NULL) {
1073 DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1075 /* Remove all members and close the old channel */
1076 members = tp_channel_group_get_members (channel);
1077 handles = tp_intset_to_array (members);
1078 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1079 handles, NULL, NULL, NULL, NULL, NULL);
1080 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1082 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1083 new_group_name, handles);
1087 tp_contact_list_remove_group (EmpathyContactList *list,
1088 const gchar *group_name)
1090 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1092 const TpIntSet *members;
1095 channel = g_hash_table_lookup (priv->groups, group_name);
1096 if (channel == NULL) {
1100 DEBUG ("remove group %s", group_name);
1102 /* Remove all members and close the channel */
1103 members = tp_channel_group_get_members (channel);
1104 handles = tp_intset_to_array (members);
1105 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1106 handles, NULL, NULL, NULL, NULL, NULL);
1107 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1108 g_array_free (handles, TRUE);
1111 static EmpathyContactListFlags
1112 tp_contact_list_get_flags (EmpathyContactList *list)
1114 EmpathyTpContactListPriv *priv;
1115 EmpathyContactListFlags flags;
1116 TpChannelGroupFlags group_flags;
1118 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1120 priv = GET_PRIV (list);
1121 flags = priv->flags;
1123 group_flags = tp_channel_group_get_flags (priv->subscribe);
1125 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1126 flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1129 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1130 flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1137 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1139 iface->add = tp_contact_list_add;
1140 iface->remove = tp_contact_list_remove;
1141 iface->get_members = tp_contact_list_get_members;
1142 iface->get_pendings = tp_contact_list_get_pendings;
1143 iface->get_all_groups = tp_contact_list_get_all_groups;
1144 iface->get_groups = tp_contact_list_get_groups;
1145 iface->add_to_group = tp_contact_list_add_to_group;
1146 iface->remove_from_group = tp_contact_list_remove_from_group;
1147 iface->rename_group = tp_contact_list_rename_group;
1148 iface->remove_group = tp_contact_list_remove_group;
1149 iface->get_flags = tp_contact_list_get_flags;
1153 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1155 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1156 GHashTableIter iter;
1159 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1161 /* Remove all contacts */
1162 g_hash_table_iter_init (&iter, priv->members);
1163 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1164 g_signal_emit_by_name (list, "members-changed", contact,
1168 g_hash_table_remove_all (priv->members);
1170 g_hash_table_iter_init (&iter, priv->pendings);
1171 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1172 g_signal_emit_by_name (list, "pendings-changed", contact,
1176 g_hash_table_remove_all (priv->pendings);