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;
52 } EmpathyTpContactListPriv;
55 TP_CONTACT_LIST_TYPE_PUBLISH,
56 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
57 TP_CONTACT_LIST_TYPE_UNKNOWN
60 static void tp_contact_list_iface_init (EmpathyContactListIface *iface);
67 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
68 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
69 tp_contact_list_iface_init));
72 tp_contact_list_group_invalidated_cb (TpChannel *channel,
76 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);
84 DEBUG ("Group %s invalidated. Message: %s", group_name, message);
86 /* Signal that all members are not in that group anymore */
87 members = tp_channel_group_get_members (channel);
88 tp_intset_iter_init (&iter, members);
89 while (tp_intset_iter_next (&iter)) {
90 EmpathyContact *contact;
92 contact = g_hash_table_lookup (priv->members,
93 GUINT_TO_POINTER (iter.element));
94 if (contact == NULL) {
98 DEBUG ("Contact %s (%d) removed from group %s",
99 empathy_contact_get_id (contact), iter.element,
101 g_signal_emit_by_name (list, "groups-changed", contact,
106 g_hash_table_remove (priv->groups, group_name);
110 tp_contact_list_group_ready_cb (TpChannel *channel,
114 EmpathyTpContactListPriv *priv = GET_PRIV (list);
117 DEBUG ("Error: %s", error->message);
118 g_object_unref (channel);
122 DEBUG ("Add group %s", tp_channel_get_identifier (channel));
123 g_hash_table_insert (priv->groups,
124 (gpointer) tp_channel_get_identifier (channel),
127 g_signal_connect (channel, "invalidated",
128 G_CALLBACK (tp_contact_list_group_invalidated_cb),
133 tp_contact_list_group_members_changed_cb (TpChannel *channel,
137 GArray *local_pending,
138 GArray *remote_pending,
141 EmpathyTpContactList *list)
143 EmpathyTpContactListPriv *priv = GET_PRIV (list);
144 const gchar *group_name;
147 group_name = tp_channel_get_identifier (channel);
149 for (i = 0; i < added->len; i++) {
150 EmpathyContact *contact;
153 handle = g_array_index (added, TpHandle, i);
154 contact = g_hash_table_lookup (priv->members,
155 GUINT_TO_POINTER (handle));
156 if (contact == NULL) {
160 DEBUG ("Contact %s (%d) added to group %s",
161 empathy_contact_get_id (contact), handle, group_name);
162 g_signal_emit_by_name (list, "groups-changed", contact,
167 for (i = 0; i < removed->len; i++) {
168 EmpathyContact *contact;
171 handle = g_array_index (removed, TpHandle, i);
172 contact = g_hash_table_lookup (priv->members,
173 GUINT_TO_POINTER (handle));
174 if (contact == NULL) {
178 DEBUG ("Contact %s (%d) removed from group %s",
179 empathy_contact_get_id (contact), handle, group_name);
181 g_signal_emit_by_name (list, "groups-changed", contact,
188 tp_contact_list_group_add_channel (EmpathyTpContactList *list,
189 const gchar *object_path,
190 const gchar *channel_type,
191 TpHandleType handle_type,
194 EmpathyTpContactListPriv *priv = GET_PRIV (list);
197 /* Only accept server-side contact groups */
198 if (tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) ||
199 handle_type != TP_HANDLE_TYPE_GROUP) {
203 channel = tp_channel_new (priv->connection,
204 object_path, channel_type,
205 handle_type, handle, NULL);
207 /* TpChannel emits initial set of members just before being ready */
208 g_signal_connect (channel, "group-members-changed",
209 G_CALLBACK (tp_contact_list_group_members_changed_cb),
212 /* Give the ref to the callback */
213 tp_channel_call_when_ready (channel,
214 tp_contact_list_group_ready_cb,
222 TpHandle channel_handle;
227 tp_contact_list_group_add_data_unref (gpointer user_data)
229 GroupAddData *data = user_data;
231 if (--data->ref_count == 0) {
232 g_array_free (data->handles, TRUE);
233 g_slice_free (GroupAddData, data);
238 tp_contact_list_group_add_ready_cb (TpChannel *channel,
242 GroupAddData *data = user_data;
245 tp_contact_list_group_add_data_unref (data);
249 tp_cli_channel_interface_group_call_add_members (channel, -1,
250 data->handles, NULL, NULL, NULL, NULL, NULL);
251 tp_contact_list_group_add_data_unref (data);
255 tp_contact_list_group_request_channel_cb (TpConnection *connection,
256 const gchar *object_path,
261 GroupAddData *data = user_data;
265 DEBUG ("Error: %s", error->message);
269 channel = tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
271 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
272 TP_HANDLE_TYPE_GROUP,
273 data->channel_handle);
276 tp_channel_call_when_ready (channel,
277 tp_contact_list_group_add_ready_cb,
282 tp_contact_list_group_request_handles_cb (TpConnection *connection,
283 const GArray *handles,
288 GroupAddData *data = user_data;
291 DEBUG ("Error: %s", error->message);
295 data->channel_handle = g_array_index (handles, TpHandle, 1);
297 tp_cli_connection_call_request_channel (connection, -1,
298 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
299 TP_HANDLE_TYPE_GROUP,
300 data->channel_handle,
302 tp_contact_list_group_request_channel_cb,
303 data, tp_contact_list_group_add_data_unref,
308 tp_contact_list_group_add (EmpathyTpContactList *list,
309 const gchar *group_name,
312 EmpathyTpContactListPriv *priv = GET_PRIV (list);
314 const gchar *names[] = {group_name, NULL};
317 channel = g_hash_table_lookup (priv->groups, group_name);
319 tp_cli_channel_interface_group_call_add_members (channel, -1,
320 handles, NULL, NULL, NULL, NULL, NULL);
321 g_array_free (handles, TRUE);
325 data = g_slice_new0 (GroupAddData);
326 data->handles = handles;
328 tp_cli_connection_call_request_handles (priv->connection, -1,
329 TP_HANDLE_TYPE_GROUP, names,
330 tp_contact_list_group_request_handles_cb,
331 data, tp_contact_list_group_add_data_unref,
336 tp_contact_list_got_added_members_cb (EmpathyTpContactFactory *factory,
341 EmpathyTpContactListPriv *priv = GET_PRIV (list);
344 for (l = contacts; l; l = l->next) {
345 EmpathyContact *contact = l->data;
348 handle = empathy_contact_get_handle (contact);
349 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
352 /* Add to the list and emit signal */
353 g_hash_table_insert (priv->members, GUINT_TO_POINTER (handle),
354 g_object_ref (contact));
355 g_signal_emit_by_name (list, "members-changed", contact,
358 /* This contact is now member, implicitly accept pending. */
359 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
360 GArray handles = {(gchar*) &handle, 1};
362 tp_cli_channel_interface_group_call_add_members (priv->publish,
363 -1, &handles, NULL, NULL, NULL, NULL, NULL);
369 tp_contact_list_got_local_pending_cb (EmpathyTpContactFactory *factory,
374 EmpathyTpContactListPriv *priv = GET_PRIV (list);
377 for (l = contacts; l; l = l->next) {
378 EmpathyContact *contact = l->data;
380 const gchar *message;
381 TpChannelGroupChangeReason reason;
383 handle = empathy_contact_get_handle (contact);
384 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle))) {
385 GArray handles = {(gchar*) &handle, 1};
387 /* This contact is already member, auto accept. */
388 tp_cli_channel_interface_group_call_add_members (priv->publish,
389 -1, &handles, NULL, NULL, NULL, NULL, NULL);
391 else if (tp_channel_group_get_local_pending_info (priv->publish,
396 /* Add contact to pendings */
397 g_hash_table_insert (priv->pendings, GUINT_TO_POINTER (handle),
398 g_object_ref (contact));
399 g_signal_emit_by_name (list, "pendings-changed", contact,
400 contact, reason, message, TRUE);
406 tp_contact_list_remove_handle (EmpathyTpContactList *list,
410 EmpathyTpContactListPriv *priv = GET_PRIV (list);
411 EmpathyContact *contact;
414 if (table == priv->pendings)
415 signal = "pendings-changed";
416 else if (table == priv->members)
417 signal = "members-changed";
421 contact = g_hash_table_lookup (table, GUINT_TO_POINTER (handle));
423 g_object_ref (contact);
424 g_hash_table_remove (table, GUINT_TO_POINTER (handle));
425 g_signal_emit_by_name (list, signal, contact, 0, 0, NULL,
427 g_object_unref (contact);
432 tp_contact_list_publish_group_members_changed_cb (TpChannel *channel,
436 GArray *local_pending,
437 GArray *remote_pending,
439 TpChannelGroupChangeReason reason,
440 EmpathyTpContactList *list)
442 EmpathyTpContactListPriv *priv = GET_PRIV (list);
445 /* We now send our presence to those contacts, remove them from pendings */
446 for (i = 0; i < added->len; i++) {
447 tp_contact_list_remove_handle (list, priv->pendings,
448 g_array_index (added, TpHandle, i));
451 /* We refuse to send our presence to those contacts, remove from pendings */
452 for (i = 0; i < removed->len; i++) {
453 tp_contact_list_remove_handle (list, priv->pendings,
454 g_array_index (added, TpHandle, i));
457 /* Those contacts want our presence, auto accept those that are already
458 * member, otherwise add in pendings. */
459 if (local_pending->len > 0) {
460 empathy_tp_contact_factory_get_from_handles (priv->factory,
461 local_pending->len, (TpHandle*) local_pending->data,
462 tp_contact_list_got_local_pending_cb, NULL, NULL,
468 tp_contact_list_publish_request_channel_cb (TpConnection *connection,
469 const gchar *object_path,
474 EmpathyTpContactListPriv *priv = GET_PRIV (list);
477 DEBUG ("Error: %s", error->message);
481 priv->publish = tp_channel_new (connection, object_path,
482 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
484 GPOINTER_TO_UINT (user_data),
487 /* TpChannel emits initial set of members just before being ready */
488 g_signal_connect (priv->publish, "group-members-changed",
489 G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
494 tp_contact_list_publish_request_handle_cb (TpConnection *connection,
495 const GArray *handles,
503 DEBUG ("Error: %s", error->message);
507 handle = g_array_index (handles, TpHandle, 0);
508 tp_cli_connection_call_request_channel (connection, -1,
509 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
513 tp_contact_list_publish_request_channel_cb,
514 GUINT_TO_POINTER (handle), NULL,
519 tp_contact_list_subscribe_group_members_changed_cb (TpChannel *channel,
523 GArray *local_pending,
524 GArray *remote_pending,
527 EmpathyTpContactList *list)
529 EmpathyTpContactListPriv *priv = GET_PRIV (list);
532 /* We now get the presence of those contacts, add them to members */
533 if (added->len > 0) {
534 empathy_tp_contact_factory_get_from_handles (priv->factory,
535 added->len, (TpHandle*) added->data,
536 tp_contact_list_got_added_members_cb, NULL, NULL,
540 /* Those contacts refuse to send us their presence, remove from members. */
541 for (i = 0; i < removed->len; i++) {
542 tp_contact_list_remove_handle (list, priv->members,
543 g_array_index (added, TpHandle, i));
546 /* We want those contacts in our contact list but we don't get their
547 * presence yet. Add to members anyway. */
548 if (remote_pending->len > 0) {
549 empathy_tp_contact_factory_get_from_handles (priv->factory,
550 remote_pending->len, (TpHandle*) remote_pending->data,
551 tp_contact_list_got_added_members_cb, NULL, NULL,
557 tp_contact_list_subscribe_request_channel_cb (TpConnection *connection,
558 const gchar *object_path,
563 EmpathyTpContactListPriv *priv = GET_PRIV (list);
566 DEBUG ("Error: %s", error->message);
570 priv->subscribe = tp_channel_new (connection, object_path,
571 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
573 GPOINTER_TO_UINT (user_data),
576 /* TpChannel emits initial set of members just before being ready */
577 g_signal_connect (priv->subscribe, "group-members-changed",
578 G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
583 tp_contact_list_subscribe_request_handle_cb (TpConnection *connection,
584 const GArray *handles,
592 DEBUG ("Error: %s", error->message);
596 handle = g_array_index (handles, TpHandle, 0);
597 tp_cli_connection_call_request_channel (connection, -1,
598 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
602 tp_contact_list_subscribe_request_channel_cb,
603 GUINT_TO_POINTER (handle), 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 if (priv->connection) {
676 g_object_unref (priv->connection);
680 g_object_unref (priv->factory);
683 g_hash_table_iter_init (&iter, priv->groups);
684 while (g_hash_table_iter_next (&iter, NULL, &channel)) {
685 g_signal_handlers_disconnect_by_func (channel,
686 tp_contact_list_group_invalidated_cb, list);
689 g_hash_table_destroy (priv->groups);
690 g_hash_table_destroy (priv->members);
691 g_hash_table_destroy (priv->pendings);
693 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
697 tp_contact_list_constructed (GObject *list)
700 EmpathyTpContactListPriv *priv = GET_PRIV (list);
701 const gchar *protocol_name = NULL;
702 const gchar *names[] = {NULL, NULL};
704 names[0] = "publish";
705 tp_cli_connection_call_request_handles (priv->connection,
709 tp_contact_list_publish_request_handle_cb,
712 names[0] = "subscribe";
713 tp_cli_connection_call_request_handles (priv->connection,
717 tp_contact_list_subscribe_request_handle_cb,
721 tp_cli_connection_call_list_channels (priv->connection, -1,
722 tp_contact_list_list_channels_cb,
726 tp_cli_connection_connect_to_new_channel (priv->connection,
727 tp_contact_list_new_channel_cb,
731 /* Check for protocols that does not support contact groups. We can
732 * put all contacts into a special group in that case.
733 * FIXME: Default group should be an information in the profile */
734 //protocol_name = tp_connection_get_protocol (priv->connection);
735 if (!tp_strdiff (protocol_name, "local-xmpp") == 0) {
736 priv->protocol_group = _("People nearby");
741 tp_contact_list_get_property (GObject *object,
746 EmpathyTpContactListPriv *priv = GET_PRIV (object);
749 case PROP_CONNECTION:
750 g_value_set_object (value, priv->connection);
753 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
759 tp_contact_list_set_property (GObject *object,
764 EmpathyTpContactListPriv *priv = GET_PRIV (object);
767 case PROP_CONNECTION:
768 priv->connection = g_value_dup_object (value);
771 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
777 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
779 GObjectClass *object_class = G_OBJECT_CLASS (klass);
781 object_class->finalize = tp_contact_list_finalize;
782 object_class->constructed = tp_contact_list_constructed;
783 object_class->get_property = tp_contact_list_get_property;
784 object_class->set_property = tp_contact_list_set_property;
786 g_object_class_install_property (object_class,
788 g_param_spec_object ("connection",
790 "The connection associated with the contact list",
793 G_PARAM_CONSTRUCT_ONLY));
795 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
799 empathy_tp_contact_list_init (EmpathyTpContactList *list)
801 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
802 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
805 priv->factory = empathy_tp_contact_factory_dup_singleton (priv->connection);
807 /* Map group's name to group's channel */
808 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
810 (GDestroyNotify) g_object_unref);
812 /* Map contact's handle to EmpathyContact object */
813 priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
815 (GDestroyNotify) g_object_unref);
817 /* Map contact's handle to EmpathyContact object */
818 priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
820 (GDestroyNotify) g_object_unref);
823 EmpathyTpContactList *
824 empathy_tp_contact_list_new (TpConnection *connection)
826 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
827 "connection", connection,
832 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
834 EmpathyTpContactListPriv *priv;
836 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
838 priv = GET_PRIV (list);
840 return priv->connection;
844 tp_contact_list_add (EmpathyContactList *list,
845 EmpathyContact *contact,
846 const gchar *message)
848 EmpathyTpContactListPriv *priv = GET_PRIV (list);
850 GArray handles = {(gchar *) &handle, 1};
852 handle = empathy_contact_get_handle (contact);
853 if (priv->subscribe) {
854 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
855 -1, &handles, message, NULL, NULL, NULL, NULL);
860 tp_contact_list_remove (EmpathyContactList *list,
861 EmpathyContact *contact,
862 const gchar *message)
864 EmpathyTpContactListPriv *priv = GET_PRIV (list);
866 GArray handles = {(gchar *) &handle, 1};
868 handle = empathy_contact_get_handle (contact);
869 if (priv->subscribe) {
870 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
871 -1, &handles, message, NULL, NULL, NULL, NULL);
874 tp_cli_channel_interface_group_call_remove_members (priv->publish,
875 -1, &handles, message, NULL, NULL, NULL, NULL);
880 tp_contact_list_get_members (EmpathyContactList *list)
882 EmpathyTpContactListPriv *priv = GET_PRIV (list);
885 ret = g_hash_table_get_values (priv->members);
886 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
891 tp_contact_list_get_pendings (EmpathyContactList *list)
893 EmpathyTpContactListPriv *priv = GET_PRIV (list);
896 ret = g_hash_table_get_values (priv->pendings);
897 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
902 tp_contact_list_get_all_groups (EmpathyContactList *list)
904 EmpathyTpContactListPriv *priv = GET_PRIV (list);
907 ret = g_hash_table_get_keys (priv->groups);
908 for (l = ret; l; l = l->next) {
909 l->data = g_strdup (l->data);
912 if (priv->protocol_group) {
913 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
920 tp_contact_list_get_groups (EmpathyContactList *list,
921 EmpathyContact *contact)
923 EmpathyTpContactListPriv *priv = GET_PRIV (list);
930 handle = empathy_contact_get_handle (contact);
931 g_hash_table_iter_init (&iter, priv->groups);
932 while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
933 const TpIntSet *members;
935 members = tp_channel_group_get_members (channel);
936 if (tp_intset_is_member (members, handle)) {
937 ret = g_list_prepend (ret, g_strdup (group_name));
941 if (priv->protocol_group) {
942 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
949 tp_contact_list_add_to_group (EmpathyContactList *list,
950 EmpathyContact *contact,
951 const gchar *group_name)
956 handle = empathy_contact_get_handle (contact);
957 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
958 g_array_append_val (handles, handle);
959 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
960 group_name, handles);
964 tp_contact_list_remove_from_group (EmpathyContactList *list,
965 EmpathyContact *contact,
966 const gchar *group_name)
968 EmpathyTpContactListPriv *priv = GET_PRIV (list);
971 GArray handles = {(gchar *) &handle, 1};
973 channel = g_hash_table_lookup (priv->groups, group_name);
974 if (channel == NULL) {
978 handle = empathy_contact_get_handle (contact);
979 DEBUG ("remove contact %s (%d) from group %s",
980 empathy_contact_get_id (contact), handle, group_name);
982 tp_cli_channel_interface_group_call_remove_members (channel, -1,
983 &handles, NULL, NULL, NULL, NULL, NULL);
987 tp_contact_list_rename_group (EmpathyContactList *list,
988 const gchar *old_group_name,
989 const gchar *new_group_name)
991 EmpathyTpContactListPriv *priv = GET_PRIV (list);
993 const TpIntSet *members;
996 channel = g_hash_table_lookup (priv->groups, old_group_name);
997 if (channel == NULL) {
1001 DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1003 /* Remove all members and close the old channel */
1004 members = tp_channel_group_get_members (channel);
1005 handles = tp_intset_to_array (members);
1006 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1007 handles, NULL, NULL, NULL, NULL, NULL);
1008 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1010 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1011 new_group_name, handles);
1015 tp_contact_list_remove_group (EmpathyContactList *list,
1016 const gchar *group_name)
1018 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1020 const TpIntSet *members;
1023 channel = g_hash_table_lookup (priv->groups, group_name);
1024 if (channel == NULL) {
1028 DEBUG ("remove group %s", group_name);
1030 /* Remove all members and close the channel */
1031 members = tp_channel_group_get_members (channel);
1032 handles = tp_intset_to_array (members);
1033 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1034 handles, NULL, NULL, NULL, NULL, NULL);
1035 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1036 g_array_free (handles, TRUE);
1040 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1042 iface->add = tp_contact_list_add;
1043 iface->remove = tp_contact_list_remove;
1044 iface->get_members = tp_contact_list_get_members;
1045 iface->get_pendings = tp_contact_list_get_pendings;
1046 iface->get_all_groups = tp_contact_list_get_all_groups;
1047 iface->get_groups = tp_contact_list_get_groups;
1048 iface->add_to_group = tp_contact_list_add_to_group;
1049 iface->remove_from_group = tp_contact_list_remove_from_group;
1050 iface->rename_group = tp_contact_list_rename_group;
1051 iface->remove_group = tp_contact_list_remove_group;
1055 empathy_tp_contact_list_can_add (EmpathyTpContactList *list)
1057 EmpathyTpContactListPriv *priv;
1058 TpChannelGroupFlags flags;
1060 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1062 priv = GET_PRIV (list);
1064 if (priv->subscribe == NULL)
1067 flags = tp_channel_group_get_flags (priv->subscribe);
1068 return (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) != 0;
1072 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1074 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1075 GHashTableIter iter;
1078 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1080 /* Remove all contacts */
1081 g_hash_table_iter_init (&iter, priv->members);
1082 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1083 g_signal_emit_by_name (list, "members-changed", contact,
1087 g_hash_table_remove_all (priv->members);
1089 g_hash_table_iter_init (&iter, priv->pendings);
1090 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1091 g_signal_emit_by_name (list, "pendings-changed", contact,
1095 g_hash_table_remove_all (priv->pendings);