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 */
53 } EmpathyTpContactListPriv;
56 TP_CONTACT_LIST_TYPE_PUBLISH,
57 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
58 TP_CONTACT_LIST_TYPE_UNKNOWN
61 static void tp_contact_list_iface_init (EmpathyContactListIface *iface);
68 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
69 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
70 tp_contact_list_iface_init));
73 tp_contact_list_forget_group (EmpathyTpContactList *list,
76 EmpathyTpContactListPriv *priv = GET_PRIV (list);
77 const TpIntSet *members;
79 const gchar *group_name;
81 group_name = tp_channel_get_identifier (channel);
83 /* Signal that all members are not in that group anymore */
84 members = tp_channel_group_get_members (channel);
85 tp_intset_iter_init (&iter, members);
86 while (tp_intset_iter_next (&iter)) {
87 EmpathyContact *contact;
89 contact = g_hash_table_lookup (priv->members,
90 GUINT_TO_POINTER (iter.element));
91 if (contact == NULL) {
95 DEBUG ("Contact %s (%d) removed from group %s",
96 empathy_contact_get_id (contact), iter.element,
98 g_signal_emit_by_name (list, "groups-changed", contact,
105 tp_contact_list_group_invalidated_cb (TpChannel *channel,
109 EmpathyTpContactList *list)
111 EmpathyTpContactListPriv *priv = GET_PRIV (list);
112 const gchar *group_name;
114 group_name = tp_channel_get_identifier (channel);
115 DEBUG ("Group %s invalidated. Message: %s", group_name, message);
117 tp_contact_list_forget_group (list, channel);
119 g_hash_table_remove (priv->groups, group_name);
123 tp_contact_list_group_ready_cb (TpChannel *channel,
127 EmpathyTpContactListPriv *priv = GET_PRIV (list);
128 TpChannel *old_group;
129 const gchar *group_name;
132 DEBUG ("Error: %s", error->message);
133 g_object_unref (channel);
137 group_name = tp_channel_get_identifier (channel);
139 /* If there's already a group with this name in the table, we can't
140 * just let it be replaced. Replacing it causes it to be unreffed,
141 * which causes it to be invalidated (see
142 * <https://bugs.freedesktop.org/show_bug.cgi?id=22119>), which causes
143 * it to be removed from the hash table again, which causes it to be
146 old_group = g_hash_table_lookup (priv->groups, group_name);
148 if (old_group != NULL) {
149 DEBUG ("Discarding old group %s (%p)", group_name, old_group);
150 g_hash_table_steal (priv->groups, group_name);
151 tp_contact_list_forget_group (list, old_group);
152 g_object_unref (old_group);
155 g_hash_table_insert (priv->groups, (gpointer) group_name, channel);
156 DEBUG ("Group %s added", group_name);
158 g_signal_connect (channel, "invalidated",
159 G_CALLBACK (tp_contact_list_group_invalidated_cb),
162 if (priv->add_to_group) {
165 handles = g_hash_table_lookup (priv->add_to_group, group_name);
167 DEBUG ("Adding initial members to group %s", group_name);
168 tp_cli_channel_interface_group_call_add_members (channel,
169 -1, handles, NULL, NULL, NULL, NULL, NULL);
170 g_hash_table_remove (priv->add_to_group, group_name);
176 tp_contact_list_group_members_changed_cb (TpChannel *channel,
180 GArray *local_pending,
181 GArray *remote_pending,
184 EmpathyTpContactList *list)
186 EmpathyTpContactListPriv *priv = GET_PRIV (list);
187 const gchar *group_name;
190 group_name = tp_channel_get_identifier (channel);
192 for (i = 0; i < added->len; i++) {
193 EmpathyContact *contact;
196 handle = g_array_index (added, TpHandle, i);
197 contact = g_hash_table_lookup (priv->members,
198 GUINT_TO_POINTER (handle));
199 if (contact == NULL) {
203 DEBUG ("Contact %s (%d) added to group %s",
204 empathy_contact_get_id (contact), handle, group_name);
205 g_signal_emit_by_name (list, "groups-changed", contact,
210 for (i = 0; i < removed->len; i++) {
211 EmpathyContact *contact;
214 handle = g_array_index (removed, TpHandle, i);
215 contact = g_hash_table_lookup (priv->members,
216 GUINT_TO_POINTER (handle));
217 if (contact == NULL) {
221 DEBUG ("Contact %s (%d) removed from group %s",
222 empathy_contact_get_id (contact), handle, group_name);
224 g_signal_emit_by_name (list, "groups-changed", contact,
231 tp_contact_list_group_add_channel (EmpathyTpContactList *list,
232 const gchar *object_path,
233 const gchar *channel_type,
234 TpHandleType handle_type,
237 EmpathyTpContactListPriv *priv = GET_PRIV (list);
240 /* Only accept server-side contact groups */
241 if (tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) ||
242 handle_type != TP_HANDLE_TYPE_GROUP) {
246 channel = tp_channel_new (priv->connection,
247 object_path, channel_type,
248 handle_type, handle, NULL);
250 /* TpChannel emits initial set of members just before being ready */
251 g_signal_connect (channel, "group-members-changed",
252 G_CALLBACK (tp_contact_list_group_members_changed_cb),
255 /* Give the ref to the callback */
256 tp_channel_call_when_ready (channel,
257 tp_contact_list_group_ready_cb,
262 tp_contact_list_group_request_channel_cb (TpConnection *connection,
263 const gchar *object_path,
268 /* The new channel will be handled in NewChannel cb. Here we only
269 * handle the error if RequestChannel failed */
271 DEBUG ("Error: %s", error->message);
277 tp_contact_list_group_request_handles_cb (TpConnection *connection,
278 const GArray *handles,
283 TpHandle channel_handle;
286 DEBUG ("Error: %s", error->message);
290 channel_handle = g_array_index (handles, TpHandle, 0);
291 tp_cli_connection_call_request_channel (connection, -1,
292 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
293 TP_HANDLE_TYPE_GROUP,
296 tp_contact_list_group_request_channel_cb,
301 /* This function takes ownership of handles array */
303 tp_contact_list_group_add (EmpathyTpContactList *list,
304 const gchar *group_name,
307 EmpathyTpContactListPriv *priv = GET_PRIV (list);
309 const gchar *names[] = {group_name, NULL};
311 /* Search the channel for that group name */
312 channel = g_hash_table_lookup (priv->groups, group_name);
314 tp_cli_channel_interface_group_call_add_members (channel, -1,
315 handles, NULL, NULL, NULL, NULL, NULL);
316 g_array_free (handles, TRUE);
320 /* That group does not exist yet, we have to:
321 * 1) Request an handle for the group name
322 * 2) Request a channel
323 * 3) When NewChannel is emitted, add handles in members
325 g_hash_table_insert (priv->add_to_group,
326 g_strdup (group_name),
328 tp_cli_connection_call_request_handles (priv->connection, -1,
329 TP_HANDLE_TYPE_GROUP, names,
330 tp_contact_list_group_request_handles_cb,
336 tp_contact_list_got_added_members_cb (EmpathyTpContactFactory *factory,
338 EmpathyContact * const * contacts,
340 const TpHandle *failed,
345 EmpathyTpContactListPriv *priv = GET_PRIV (list);
349 DEBUG ("Error: %s", error->message);
353 for (i = 0; i < n_contacts; i++) {
354 EmpathyContact *contact = contacts[i];
357 handle = empathy_contact_get_handle (contact);
358 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
361 /* Add to the list and emit signal */
362 g_hash_table_insert (priv->members, GUINT_TO_POINTER (handle),
363 g_object_ref (contact));
364 g_signal_emit_by_name (list, "members-changed", contact,
367 /* This contact is now member, implicitly accept pending. */
368 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
369 GArray handles = {(gchar *) &handle, 1};
371 tp_cli_channel_interface_group_call_add_members (priv->publish,
372 -1, &handles, NULL, NULL, NULL, NULL, NULL);
378 tp_contact_list_got_local_pending_cb (EmpathyTpContactFactory *factory,
380 EmpathyContact * const * contacts,
382 const TpHandle *failed,
387 EmpathyTpContactListPriv *priv = GET_PRIV (list);
391 DEBUG ("Error: %s", error->message);
395 for (i = 0; i < n_contacts; i++) {
396 EmpathyContact *contact = contacts[i];
398 const gchar *message;
399 TpChannelGroupChangeReason reason;
401 handle = empathy_contact_get_handle (contact);
402 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle))) {
403 GArray handles = {(gchar *) &handle, 1};
405 /* This contact is already member, auto accept. */
406 tp_cli_channel_interface_group_call_add_members (priv->publish,
407 -1, &handles, NULL, NULL, NULL, NULL, NULL);
409 else if (tp_channel_group_get_local_pending_info (priv->publish,
414 /* Add contact to pendings */
415 g_hash_table_insert (priv->pendings, GUINT_TO_POINTER (handle),
416 g_object_ref (contact));
417 g_signal_emit_by_name (list, "pendings-changed", contact,
418 contact, reason, message, TRUE);
424 tp_contact_list_remove_handle (EmpathyTpContactList *list,
428 EmpathyTpContactListPriv *priv = GET_PRIV (list);
429 EmpathyContact *contact;
432 if (table == priv->pendings)
433 signal = "pendings-changed";
434 else if (table == priv->members)
435 signal = "members-changed";
439 contact = g_hash_table_lookup (table, GUINT_TO_POINTER (handle));
441 g_object_ref (contact);
442 g_hash_table_remove (table, GUINT_TO_POINTER (handle));
443 g_signal_emit_by_name (list, signal, contact, 0, 0, NULL,
445 g_object_unref (contact);
450 tp_contact_list_publish_group_members_changed_cb (TpChannel *channel,
454 GArray *local_pending,
455 GArray *remote_pending,
457 TpChannelGroupChangeReason reason,
458 EmpathyTpContactList *list)
460 EmpathyTpContactListPriv *priv = GET_PRIV (list);
463 /* We now send our presence to those contacts, remove them from pendings */
464 for (i = 0; i < added->len; i++) {
465 tp_contact_list_remove_handle (list, priv->pendings,
466 g_array_index (added, TpHandle, i));
469 /* We refuse to send our presence to those contacts, remove from pendings */
470 for (i = 0; i < removed->len; i++) {
471 tp_contact_list_remove_handle (list, priv->pendings,
472 g_array_index (added, TpHandle, i));
475 /* Those contacts want our presence, auto accept those that are already
476 * member, otherwise add in pendings. */
477 if (local_pending->len > 0) {
478 empathy_tp_contact_factory_get_from_handles (priv->factory,
479 local_pending->len, (TpHandle *) local_pending->data,
480 tp_contact_list_got_local_pending_cb, NULL, NULL,
486 tp_contact_list_publish_request_channel_cb (TpConnection *connection,
487 const gchar *object_path,
492 EmpathyTpContactListPriv *priv = GET_PRIV (list);
495 DEBUG ("Error: %s", error->message);
499 priv->publish = tp_channel_new (connection, object_path,
500 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
502 GPOINTER_TO_UINT (user_data),
505 /* TpChannel emits initial set of members just before being ready */
506 g_signal_connect (priv->publish, "group-members-changed",
507 G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
512 tp_contact_list_publish_request_handle_cb (TpConnection *connection,
513 const GArray *handles,
521 DEBUG ("Error: %s", error->message);
525 handle = g_array_index (handles, TpHandle, 0);
526 tp_cli_connection_call_request_channel (connection, -1,
527 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
531 tp_contact_list_publish_request_channel_cb,
532 GUINT_TO_POINTER (handle), NULL,
537 tp_contact_list_subscribe_group_members_changed_cb (TpChannel *channel,
541 GArray *local_pending,
542 GArray *remote_pending,
545 EmpathyTpContactList *list)
547 EmpathyTpContactListPriv *priv = GET_PRIV (list);
550 /* We now get the presence of those contacts, add them to members */
551 if (added->len > 0) {
552 empathy_tp_contact_factory_get_from_handles (priv->factory,
553 added->len, (TpHandle *) added->data,
554 tp_contact_list_got_added_members_cb, NULL, NULL,
558 /* Those contacts refuse to send us their presence, remove from members. */
559 for (i = 0; i < removed->len; i++) {
560 tp_contact_list_remove_handle (list, priv->members,
561 g_array_index (added, TpHandle, i));
564 /* We want those contacts in our contact list but we don't get their
565 * presence yet. Add to members anyway. */
566 if (remote_pending->len > 0) {
567 empathy_tp_contact_factory_get_from_handles (priv->factory,
568 remote_pending->len, (TpHandle *) remote_pending->data,
569 tp_contact_list_got_added_members_cb, NULL, NULL,
575 tp_contact_list_subscribe_request_channel_cb (TpConnection *connection,
576 const gchar *object_path,
581 EmpathyTpContactListPriv *priv = GET_PRIV (list);
584 DEBUG ("Error: %s", error->message);
588 priv->subscribe = tp_channel_new (connection, object_path,
589 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
591 GPOINTER_TO_UINT (user_data),
594 /* TpChannel emits initial set of members just before being ready */
595 g_signal_connect (priv->subscribe, "group-members-changed",
596 G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
601 tp_contact_list_subscribe_request_handle_cb (TpConnection *connection,
602 const GArray *handles,
610 DEBUG ("Error: %s", error->message);
614 handle = g_array_index (handles, TpHandle, 0);
615 tp_cli_connection_call_request_channel (connection, -1,
616 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
620 tp_contact_list_subscribe_request_channel_cb,
621 GUINT_TO_POINTER (handle), NULL,
626 tp_contact_list_new_channel_cb (TpConnection *proxy,
627 const gchar *object_path,
628 const gchar *channel_type,
631 gboolean suppress_handler,
635 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
636 object_path, channel_type,
637 handle_type, handle);
641 tp_contact_list_list_channels_cb (TpConnection *connection,
642 const GPtrArray *channels,
650 DEBUG ("Error: %s", error->message);
654 for (i = 0; i < channels->len; i++) {
655 GValueArray *chan_struct;
656 const gchar *object_path;
657 const gchar *channel_type;
658 TpHandleType handle_type;
661 chan_struct = g_ptr_array_index (channels, i);
662 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
663 channel_type = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
664 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
665 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
667 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
668 object_path, channel_type,
669 handle_type, handle);
674 tp_contact_list_finalize (GObject *object)
676 EmpathyTpContactListPriv *priv;
677 EmpathyTpContactList *list;
681 list = EMPATHY_TP_CONTACT_LIST (object);
682 priv = GET_PRIV (list);
684 DEBUG ("finalize: %p", object);
686 if (priv->subscribe) {
687 g_object_unref (priv->subscribe);
690 g_object_unref (priv->publish);
693 if (priv->connection) {
694 g_object_unref (priv->connection);
698 g_object_unref (priv->factory);
701 g_hash_table_iter_init (&iter, priv->groups);
702 while (g_hash_table_iter_next (&iter, NULL, &channel)) {
703 g_signal_handlers_disconnect_by_func (channel,
704 tp_contact_list_group_invalidated_cb, list);
707 g_hash_table_destroy (priv->groups);
708 g_hash_table_destroy (priv->members);
709 g_hash_table_destroy (priv->pendings);
710 g_hash_table_destroy (priv->add_to_group);
712 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
716 tp_contact_list_constructed (GObject *list)
719 EmpathyTpContactListPriv *priv = GET_PRIV (list);
720 gchar *protocol_name = NULL;
721 const gchar *names[] = {NULL, NULL};
723 priv->factory = empathy_tp_contact_factory_dup_singleton (priv->connection);
725 names[0] = "publish";
726 tp_cli_connection_call_request_handles (priv->connection,
730 tp_contact_list_publish_request_handle_cb,
733 names[0] = "subscribe";
734 tp_cli_connection_call_request_handles (priv->connection,
738 tp_contact_list_subscribe_request_handle_cb,
742 tp_cli_connection_call_list_channels (priv->connection, -1,
743 tp_contact_list_list_channels_cb,
747 tp_cli_connection_connect_to_new_channel (priv->connection,
748 tp_contact_list_new_channel_cb,
752 /* Check for protocols that does not support contact groups. We can
753 * put all contacts into a special group in that case.
754 * FIXME: Default group should be an information in the profile */
755 tp_connection_parse_object_path (priv->connection, &protocol_name, NULL);
756 if (!tp_strdiff (protocol_name, "local-xmpp")) {
757 priv->protocol_group = _("People nearby");
759 g_free (protocol_name);
763 tp_contact_list_get_property (GObject *object,
768 EmpathyTpContactListPriv *priv = GET_PRIV (object);
771 case PROP_CONNECTION:
772 g_value_set_object (value, priv->connection);
775 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
781 tp_contact_list_set_property (GObject *object,
786 EmpathyTpContactListPriv *priv = GET_PRIV (object);
789 case PROP_CONNECTION:
790 priv->connection = g_value_dup_object (value);
793 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
799 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
801 GObjectClass *object_class = G_OBJECT_CLASS (klass);
803 object_class->finalize = tp_contact_list_finalize;
804 object_class->constructed = tp_contact_list_constructed;
805 object_class->get_property = tp_contact_list_get_property;
806 object_class->set_property = tp_contact_list_set_property;
808 g_object_class_install_property (object_class,
810 g_param_spec_object ("connection",
812 "The connection associated with the contact list",
815 G_PARAM_CONSTRUCT_ONLY));
817 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
821 tp_contact_list_array_free (gpointer handles)
823 g_array_free (handles, TRUE);
827 empathy_tp_contact_list_init (EmpathyTpContactList *list)
829 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
830 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
834 /* Map group's name to group's TpChannel. The group name string is owned
835 * by the TpChannel object */
836 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
838 (GDestroyNotify) g_object_unref);
840 /* Map contact's handle to EmpathyContact object */
841 priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
843 (GDestroyNotify) g_object_unref);
845 /* Map contact's handle to EmpathyContact object */
846 priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
848 (GDestroyNotify) g_object_unref);
850 /* Map group's name to GArray of handle */
851 priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
853 tp_contact_list_array_free);
856 EmpathyTpContactList *
857 empathy_tp_contact_list_new (TpConnection *connection)
859 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
860 "connection", connection,
865 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
867 EmpathyTpContactListPriv *priv;
869 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
871 priv = GET_PRIV (list);
873 return priv->connection;
877 tp_contact_list_add (EmpathyContactList *list,
878 EmpathyContact *contact,
879 const gchar *message)
881 EmpathyTpContactListPriv *priv = GET_PRIV (list);
883 GArray handles = {(gchar *) &handle, 1};
885 handle = empathy_contact_get_handle (contact);
886 if (priv->subscribe) {
887 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
888 -1, &handles, message, NULL, NULL, NULL, NULL);
891 g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
892 tp_cli_channel_interface_group_call_add_members (priv->publish,
893 -1, &handles, message, NULL, NULL, NULL, NULL);
898 tp_contact_list_remove (EmpathyContactList *list,
899 EmpathyContact *contact,
900 const gchar *message)
902 EmpathyTpContactListPriv *priv = GET_PRIV (list);
904 GArray handles = {(gchar *) &handle, 1};
906 handle = empathy_contact_get_handle (contact);
907 if (priv->subscribe) {
908 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
909 -1, &handles, message, NULL, NULL, NULL, NULL);
912 tp_cli_channel_interface_group_call_remove_members (priv->publish,
913 -1, &handles, message, NULL, NULL, NULL, NULL);
918 tp_contact_list_get_members (EmpathyContactList *list)
920 EmpathyTpContactListPriv *priv = GET_PRIV (list);
923 ret = g_hash_table_get_values (priv->members);
924 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
929 tp_contact_list_get_pendings (EmpathyContactList *list)
931 EmpathyTpContactListPriv *priv = GET_PRIV (list);
934 ret = g_hash_table_get_values (priv->pendings);
935 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
940 tp_contact_list_get_all_groups (EmpathyContactList *list)
942 EmpathyTpContactListPriv *priv = GET_PRIV (list);
945 ret = g_hash_table_get_keys (priv->groups);
946 for (l = ret; l; l = l->next) {
947 l->data = g_strdup (l->data);
950 if (priv->protocol_group) {
951 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
958 tp_contact_list_get_groups (EmpathyContactList *list,
959 EmpathyContact *contact)
961 EmpathyTpContactListPriv *priv = GET_PRIV (list);
968 handle = empathy_contact_get_handle (contact);
969 g_hash_table_iter_init (&iter, priv->groups);
970 while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
971 const TpIntSet *members;
973 members = tp_channel_group_get_members (channel);
974 if (tp_intset_is_member (members, handle)) {
975 ret = g_list_prepend (ret, g_strdup (group_name));
979 if (priv->protocol_group) {
980 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
987 tp_contact_list_add_to_group (EmpathyContactList *list,
988 EmpathyContact *contact,
989 const gchar *group_name)
994 handle = empathy_contact_get_handle (contact);
995 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
996 g_array_append_val (handles, handle);
997 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
998 group_name, handles);
1002 tp_contact_list_remove_from_group (EmpathyContactList *list,
1003 EmpathyContact *contact,
1004 const gchar *group_name)
1006 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1009 GArray handles = {(gchar *) &handle, 1};
1011 channel = g_hash_table_lookup (priv->groups, group_name);
1012 if (channel == NULL) {
1016 handle = empathy_contact_get_handle (contact);
1017 DEBUG ("remove contact %s (%d) from group %s",
1018 empathy_contact_get_id (contact), handle, group_name);
1020 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1021 &handles, NULL, NULL, NULL, NULL, NULL);
1025 tp_contact_list_rename_group (EmpathyContactList *list,
1026 const gchar *old_group_name,
1027 const gchar *new_group_name)
1029 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1031 const TpIntSet *members;
1034 channel = g_hash_table_lookup (priv->groups, old_group_name);
1035 if (channel == NULL) {
1039 DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1041 /* Remove all members and close the old channel */
1042 members = tp_channel_group_get_members (channel);
1043 handles = tp_intset_to_array (members);
1044 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1045 handles, NULL, NULL, NULL, NULL, NULL);
1046 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1048 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1049 new_group_name, handles);
1053 tp_contact_list_remove_group (EmpathyContactList *list,
1054 const gchar *group_name)
1056 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1058 const TpIntSet *members;
1061 channel = g_hash_table_lookup (priv->groups, group_name);
1062 if (channel == NULL) {
1066 DEBUG ("remove group %s", group_name);
1068 /* Remove all members and close the channel */
1069 members = tp_channel_group_get_members (channel);
1070 handles = tp_intset_to_array (members);
1071 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1072 handles, NULL, NULL, NULL, NULL, NULL);
1073 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1074 g_array_free (handles, TRUE);
1077 static EmpathyContactListFlags
1078 tp_contact_list_get_flags (EmpathyContactList *list)
1080 EmpathyTpContactListPriv *priv;
1081 EmpathyContactListFlags flags = 0;
1082 TpChannelGroupFlags group_flags;
1084 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1086 priv = GET_PRIV (list);
1088 group_flags = tp_channel_group_get_flags (priv->subscribe);
1090 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1091 flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1094 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1095 flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1102 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1104 iface->add = tp_contact_list_add;
1105 iface->remove = tp_contact_list_remove;
1106 iface->get_members = tp_contact_list_get_members;
1107 iface->get_pendings = tp_contact_list_get_pendings;
1108 iface->get_all_groups = tp_contact_list_get_all_groups;
1109 iface->get_groups = tp_contact_list_get_groups;
1110 iface->add_to_group = tp_contact_list_add_to_group;
1111 iface->remove_from_group = tp_contact_list_remove_from_group;
1112 iface->rename_group = tp_contact_list_rename_group;
1113 iface->remove_group = tp_contact_list_remove_group;
1114 iface->get_flags = tp_contact_list_get_flags;
1118 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1120 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1121 GHashTableIter iter;
1124 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1126 /* Remove all contacts */
1127 g_hash_table_iter_init (&iter, priv->members);
1128 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1129 g_signal_emit_by_name (list, "members-changed", contact,
1133 g_hash_table_remove_all (priv->members);
1135 g_hash_table_iter_init (&iter, priv->pendings);
1136 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1137 g_signal_emit_by_name (list, "pendings-changed", contact,
1141 g_hash_table_remove_all (priv->pendings);