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;
50 GHashTable *members; /* handle -> EmpathyContact */
51 GHashTable *pendings; /* handle -> EmpathyContact */
52 GHashTable *groups; /* group name -> TpChannel */
53 GHashTable *add_to_group; /* group name -> GArray of handles */
54 } EmpathyTpContactListPriv;
57 TP_CONTACT_LIST_TYPE_PUBLISH,
58 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
59 TP_CONTACT_LIST_TYPE_UNKNOWN
62 static void tp_contact_list_iface_init (EmpathyContactListIface *iface);
69 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
70 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
71 tp_contact_list_iface_init));
74 tp_contact_list_forget_group (EmpathyTpContactList *list,
77 EmpathyTpContactListPriv *priv = GET_PRIV (list);
78 const TpIntSet *members;
80 const gchar *group_name;
82 group_name = tp_channel_get_identifier (channel);
84 /* Signal that all members are not in that group anymore */
85 members = tp_channel_group_get_members (channel);
86 tp_intset_iter_init (&iter, members);
87 while (tp_intset_iter_next (&iter)) {
88 EmpathyContact *contact;
90 contact = g_hash_table_lookup (priv->members,
91 GUINT_TO_POINTER (iter.element));
92 if (contact == NULL) {
96 DEBUG ("Contact %s (%d) removed from group %s",
97 empathy_contact_get_id (contact), iter.element,
99 g_signal_emit_by_name (list, "groups-changed", contact,
106 tp_contact_list_group_invalidated_cb (TpChannel *channel,
110 EmpathyTpContactList *list)
112 EmpathyTpContactListPriv *priv = GET_PRIV (list);
113 const gchar *group_name;
115 group_name = tp_channel_get_identifier (channel);
116 DEBUG ("Group %s invalidated. Message: %s", group_name, message);
118 tp_contact_list_forget_group (list, channel);
120 g_hash_table_remove (priv->groups, group_name);
124 tp_contact_list_group_ready_cb (TpChannel *channel,
128 EmpathyTpContactListPriv *priv = GET_PRIV (list);
129 TpChannel *old_group;
130 const gchar *group_name;
133 DEBUG ("Error: %s", error->message);
134 g_object_unref (channel);
138 group_name = tp_channel_get_identifier (channel);
140 /* If there's already a group with this name in the table, we can't
141 * just let it be replaced. Replacing it causes it to be unreffed,
142 * which causes it to be invalidated (see
143 * <https://bugs.freedesktop.org/show_bug.cgi?id=22119>), which causes
144 * it to be removed from the hash table again, which causes it to be
147 old_group = g_hash_table_lookup (priv->groups, group_name);
149 if (old_group != NULL) {
150 DEBUG ("Discarding old group %s (%p)", group_name, old_group);
151 g_hash_table_steal (priv->groups, group_name);
152 tp_contact_list_forget_group (list, old_group);
153 g_object_unref (old_group);
156 g_hash_table_insert (priv->groups, (gpointer) group_name, channel);
157 DEBUG ("Group %s added", group_name);
159 g_signal_connect (channel, "invalidated",
160 G_CALLBACK (tp_contact_list_group_invalidated_cb),
163 if (priv->add_to_group) {
166 handles = g_hash_table_lookup (priv->add_to_group, group_name);
168 DEBUG ("Adding initial members to group %s", group_name);
169 tp_cli_channel_interface_group_call_add_members (channel,
170 -1, handles, NULL, NULL, NULL, NULL, NULL);
171 g_hash_table_remove (priv->add_to_group, group_name);
177 tp_contact_list_group_members_changed_cb (TpChannel *channel,
181 GArray *local_pending,
182 GArray *remote_pending,
185 EmpathyTpContactList *list)
187 EmpathyTpContactListPriv *priv = GET_PRIV (list);
188 const gchar *group_name;
191 group_name = tp_channel_get_identifier (channel);
193 for (i = 0; i < added->len; i++) {
194 EmpathyContact *contact;
197 handle = g_array_index (added, TpHandle, i);
198 contact = g_hash_table_lookup (priv->members,
199 GUINT_TO_POINTER (handle));
200 if (contact == NULL) {
204 DEBUG ("Contact %s (%d) added to group %s",
205 empathy_contact_get_id (contact), handle, group_name);
206 g_signal_emit_by_name (list, "groups-changed", contact,
211 for (i = 0; i < removed->len; i++) {
212 EmpathyContact *contact;
215 handle = g_array_index (removed, TpHandle, i);
216 contact = g_hash_table_lookup (priv->members,
217 GUINT_TO_POINTER (handle));
218 if (contact == NULL) {
222 DEBUG ("Contact %s (%d) removed from group %s",
223 empathy_contact_get_id (contact), handle, group_name);
225 g_signal_emit_by_name (list, "groups-changed", contact,
232 tp_contact_list_group_add_channel (EmpathyTpContactList *list,
233 const gchar *object_path,
234 const gchar *channel_type,
235 TpHandleType handle_type,
238 EmpathyTpContactListPriv *priv = GET_PRIV (list);
241 /* Only accept server-side contact groups */
242 if (tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) ||
243 handle_type != TP_HANDLE_TYPE_GROUP) {
247 channel = tp_channel_new (priv->connection,
248 object_path, channel_type,
249 handle_type, handle, NULL);
251 /* TpChannel emits initial set of members just before being ready */
252 g_signal_connect (channel, "group-members-changed",
253 G_CALLBACK (tp_contact_list_group_members_changed_cb),
256 /* Give the ref to the callback */
257 tp_channel_call_when_ready (channel,
258 tp_contact_list_group_ready_cb,
263 tp_contact_list_group_request_channel_cb (TpConnection *connection,
264 const gchar *object_path,
269 /* The new channel will be handled in NewChannel cb. Here we only
270 * handle the error if RequestChannel failed */
272 DEBUG ("Error: %s", error->message);
278 tp_contact_list_group_request_handles_cb (TpConnection *connection,
279 const GArray *handles,
284 TpHandle channel_handle;
287 DEBUG ("Error: %s", error->message);
291 channel_handle = g_array_index (handles, TpHandle, 0);
292 tp_cli_connection_call_request_channel (connection, -1,
293 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
294 TP_HANDLE_TYPE_GROUP,
297 tp_contact_list_group_request_channel_cb,
302 /* This function takes ownership of handles array */
304 tp_contact_list_group_add (EmpathyTpContactList *list,
305 const gchar *group_name,
308 EmpathyTpContactListPriv *priv = GET_PRIV (list);
310 const gchar *names[] = {group_name, NULL};
312 /* Search the channel for that group name */
313 channel = g_hash_table_lookup (priv->groups, group_name);
315 tp_cli_channel_interface_group_call_add_members (channel, -1,
316 handles, NULL, NULL, NULL, NULL, NULL);
317 g_array_free (handles, TRUE);
321 /* That group does not exist yet, we have to:
322 * 1) Request an handle for the group name
323 * 2) Request a channel
324 * 3) When NewChannel is emitted, add handles in members
326 g_hash_table_insert (priv->add_to_group,
327 g_strdup (group_name),
329 tp_cli_connection_call_request_handles (priv->connection, -1,
330 TP_HANDLE_TYPE_GROUP, names,
331 tp_contact_list_group_request_handles_cb,
337 tp_contact_list_got_added_members_cb (EmpathyTpContactFactory *factory,
339 EmpathyContact * const * contacts,
341 const TpHandle *failed,
346 EmpathyTpContactListPriv *priv = GET_PRIV (list);
350 DEBUG ("Error: %s", error->message);
354 for (i = 0; i < n_contacts; i++) {
355 EmpathyContact *contact = contacts[i];
358 handle = empathy_contact_get_handle (contact);
359 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
362 /* Add to the list and emit signal */
363 g_hash_table_insert (priv->members, GUINT_TO_POINTER (handle),
364 g_object_ref (contact));
365 g_signal_emit_by_name (list, "members-changed", contact,
368 /* This contact is now member, implicitly accept pending. */
369 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
370 GArray handles = {(gchar *) &handle, 1};
372 tp_cli_channel_interface_group_call_add_members (priv->publish,
373 -1, &handles, NULL, NULL, NULL, NULL, NULL);
379 tp_contact_list_got_local_pending_cb (EmpathyTpContactFactory *factory,
381 EmpathyContact * const * contacts,
383 const TpHandle *failed,
388 EmpathyTpContactListPriv *priv = GET_PRIV (list);
392 DEBUG ("Error: %s", error->message);
396 for (i = 0; i < n_contacts; i++) {
397 EmpathyContact *contact = contacts[i];
399 const gchar *message;
400 TpChannelGroupChangeReason reason;
402 handle = empathy_contact_get_handle (contact);
403 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle))) {
404 GArray handles = {(gchar *) &handle, 1};
406 /* This contact is already member, auto accept. */
407 tp_cli_channel_interface_group_call_add_members (priv->publish,
408 -1, &handles, NULL, NULL, NULL, NULL, NULL);
410 else if (tp_channel_group_get_local_pending_info (priv->publish,
415 /* Add contact to pendings */
416 g_hash_table_insert (priv->pendings, GUINT_TO_POINTER (handle),
417 g_object_ref (contact));
418 g_signal_emit_by_name (list, "pendings-changed", contact,
419 contact, reason, message, TRUE);
425 tp_contact_list_remove_handle (EmpathyTpContactList *list,
429 EmpathyTpContactListPriv *priv = GET_PRIV (list);
430 EmpathyContact *contact;
433 if (table == priv->pendings)
434 signal = "pendings-changed";
435 else if (table == priv->members)
436 signal = "members-changed";
440 contact = g_hash_table_lookup (table, GUINT_TO_POINTER (handle));
442 g_object_ref (contact);
443 g_hash_table_remove (table, GUINT_TO_POINTER (handle));
444 g_signal_emit_by_name (list, signal, contact, 0, 0, NULL,
446 g_object_unref (contact);
451 tp_contact_list_publish_group_members_changed_cb (TpChannel *channel,
455 GArray *local_pending,
456 GArray *remote_pending,
458 TpChannelGroupChangeReason reason,
459 EmpathyTpContactList *list)
461 EmpathyTpContactListPriv *priv = GET_PRIV (list);
464 /* We now send our presence to those contacts, remove them from pendings */
465 for (i = 0; i < added->len; i++) {
466 tp_contact_list_remove_handle (list, priv->pendings,
467 g_array_index (added, TpHandle, i));
470 /* We refuse to send our presence to those contacts, remove from pendings */
471 for (i = 0; i < removed->len; i++) {
472 tp_contact_list_remove_handle (list, priv->pendings,
473 g_array_index (added, TpHandle, i));
476 /* Those contacts want our presence, auto accept those that are already
477 * member, otherwise add in pendings. */
478 if (local_pending->len > 0) {
479 empathy_tp_contact_factory_get_from_handles (priv->factory,
480 local_pending->len, (TpHandle *) local_pending->data,
481 tp_contact_list_got_local_pending_cb, NULL, NULL,
487 tp_contact_list_publish_request_channel_cb (TpConnection *connection,
488 const gchar *object_path,
493 EmpathyTpContactListPriv *priv = GET_PRIV (list);
496 DEBUG ("Error: %s", error->message);
500 priv->publish = tp_channel_new (connection, object_path,
501 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
503 GPOINTER_TO_UINT (user_data),
506 /* TpChannel emits initial set of members just before being ready */
507 g_signal_connect (priv->publish, "group-members-changed",
508 G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
513 tp_contact_list_publish_request_handle_cb (TpConnection *connection,
514 const GArray *handles,
522 DEBUG ("Error: %s", error->message);
526 handle = g_array_index (handles, TpHandle, 0);
527 tp_cli_connection_call_request_channel (connection, -1,
528 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
532 tp_contact_list_publish_request_channel_cb,
533 GUINT_TO_POINTER (handle), NULL,
538 tp_contact_list_subscribe_group_members_changed_cb (TpChannel *channel,
542 GArray *local_pending,
543 GArray *remote_pending,
546 EmpathyTpContactList *list)
548 EmpathyTpContactListPriv *priv = GET_PRIV (list);
551 /* We now get the presence of those contacts, add them to members */
552 if (added->len > 0) {
553 empathy_tp_contact_factory_get_from_handles (priv->factory,
554 added->len, (TpHandle *) added->data,
555 tp_contact_list_got_added_members_cb, NULL, NULL,
559 /* Those contacts refuse to send us their presence, remove from members. */
560 for (i = 0; i < removed->len; i++) {
561 tp_contact_list_remove_handle (list, priv->members,
562 g_array_index (added, TpHandle, i));
565 /* We want those contacts in our contact list but we don't get their
566 * presence yet. Add to members anyway. */
567 if (remote_pending->len > 0) {
568 empathy_tp_contact_factory_get_from_handles (priv->factory,
569 remote_pending->len, (TpHandle *) remote_pending->data,
570 tp_contact_list_got_added_members_cb, NULL, NULL,
576 tp_contact_list_subscribe_request_channel_cb (TpConnection *connection,
577 const gchar *object_path,
582 EmpathyTpContactListPriv *priv = GET_PRIV (list);
585 DEBUG ("Error: %s", error->message);
589 priv->subscribe = tp_channel_new (connection, object_path,
590 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
592 GPOINTER_TO_UINT (user_data),
595 /* TpChannel emits initial set of members just before being ready */
596 g_signal_connect (priv->subscribe, "group-members-changed",
597 G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
602 tp_contact_list_subscribe_request_handle_cb (TpConnection *connection,
603 const GArray *handles,
611 DEBUG ("Error: %s", error->message);
615 handle = g_array_index (handles, TpHandle, 0);
616 tp_cli_connection_call_request_channel (connection, -1,
617 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
621 tp_contact_list_subscribe_request_channel_cb,
622 GUINT_TO_POINTER (handle), NULL,
627 tp_contact_list_new_channel_cb (TpConnection *proxy,
628 const gchar *object_path,
629 const gchar *channel_type,
632 gboolean suppress_handler,
636 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
637 object_path, channel_type,
638 handle_type, handle);
642 tp_contact_list_list_channels_cb (TpConnection *connection,
643 const GPtrArray *channels,
651 DEBUG ("Error: %s", error->message);
655 for (i = 0; i < channels->len; i++) {
656 GValueArray *chan_struct;
657 const gchar *object_path;
658 const gchar *channel_type;
659 TpHandleType handle_type;
662 chan_struct = g_ptr_array_index (channels, i);
663 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
664 channel_type = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
665 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
666 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
668 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
669 object_path, channel_type,
670 handle_type, handle);
675 tp_contact_list_finalize (GObject *object)
677 EmpathyTpContactListPriv *priv;
678 EmpathyTpContactList *list;
682 list = EMPATHY_TP_CONTACT_LIST (object);
683 priv = GET_PRIV (list);
685 DEBUG ("finalize: %p", object);
687 if (priv->subscribe) {
688 g_object_unref (priv->subscribe);
691 g_object_unref (priv->publish);
694 if (priv->connection) {
695 g_object_unref (priv->connection);
699 g_object_unref (priv->factory);
702 g_hash_table_iter_init (&iter, priv->groups);
703 while (g_hash_table_iter_next (&iter, NULL, &channel)) {
704 g_signal_handlers_disconnect_by_func (channel,
705 tp_contact_list_group_invalidated_cb, list);
708 g_hash_table_destroy (priv->groups);
709 g_hash_table_destroy (priv->members);
710 g_hash_table_destroy (priv->pendings);
711 g_hash_table_destroy (priv->add_to_group);
713 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
717 tp_contact_list_constructed (GObject *list)
720 EmpathyTpContactListPriv *priv = GET_PRIV (list);
721 gchar *protocol_name = NULL;
722 const gchar *names[] = {NULL, NULL};
724 priv->factory = empathy_tp_contact_factory_dup_singleton (priv->connection);
726 names[0] = "publish";
727 tp_cli_connection_call_request_handles (priv->connection,
731 tp_contact_list_publish_request_handle_cb,
734 names[0] = "subscribe";
735 tp_cli_connection_call_request_handles (priv->connection,
739 tp_contact_list_subscribe_request_handle_cb,
743 tp_cli_connection_call_list_channels (priv->connection, -1,
744 tp_contact_list_list_channels_cb,
748 tp_cli_connection_connect_to_new_channel (priv->connection,
749 tp_contact_list_new_channel_cb,
753 /* Check for protocols that does not support contact groups. We can
754 * put all contacts into a special group in that case.
755 * FIXME: Default group should be an information in the profile */
756 tp_connection_parse_object_path (priv->connection, &protocol_name, NULL);
757 if (!tp_strdiff (protocol_name, "local-xmpp")) {
758 priv->protocol_group = _("People nearby");
760 g_free (protocol_name);
764 tp_contact_list_get_property (GObject *object,
769 EmpathyTpContactListPriv *priv = GET_PRIV (object);
772 case PROP_CONNECTION:
773 g_value_set_object (value, priv->connection);
776 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
782 tp_contact_list_set_property (GObject *object,
787 EmpathyTpContactListPriv *priv = GET_PRIV (object);
790 case PROP_CONNECTION:
791 priv->connection = g_value_dup_object (value);
794 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
800 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
802 GObjectClass *object_class = G_OBJECT_CLASS (klass);
804 object_class->finalize = tp_contact_list_finalize;
805 object_class->constructed = tp_contact_list_constructed;
806 object_class->get_property = tp_contact_list_get_property;
807 object_class->set_property = tp_contact_list_set_property;
809 g_object_class_install_property (object_class,
811 g_param_spec_object ("connection",
813 "The connection associated with the contact list",
816 G_PARAM_CONSTRUCT_ONLY));
818 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
822 tp_contact_list_array_free (gpointer handles)
824 g_array_free (handles, TRUE);
828 empathy_tp_contact_list_init (EmpathyTpContactList *list)
830 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
831 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
835 /* Map group's name to group's TpChannel. The group name string is owned
836 * by the TpChannel object */
837 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
839 (GDestroyNotify) g_object_unref);
841 /* Map contact's handle to EmpathyContact object */
842 priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
844 (GDestroyNotify) g_object_unref);
846 /* Map contact's handle to EmpathyContact object */
847 priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
849 (GDestroyNotify) g_object_unref);
851 /* Map group's name to GArray of handle */
852 priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
854 tp_contact_list_array_free);
857 EmpathyTpContactList *
858 empathy_tp_contact_list_new (TpConnection *connection)
860 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
861 "connection", connection,
866 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
868 EmpathyTpContactListPriv *priv;
870 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
872 priv = GET_PRIV (list);
874 return priv->connection;
878 tp_contact_list_add (EmpathyContactList *list,
879 EmpathyContact *contact,
880 const gchar *message)
882 EmpathyTpContactListPriv *priv = GET_PRIV (list);
884 GArray handles = {(gchar *) &handle, 1};
886 handle = empathy_contact_get_handle (contact);
887 if (priv->subscribe) {
888 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
889 -1, &handles, message, NULL, NULL, NULL, NULL);
892 g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
893 tp_cli_channel_interface_group_call_add_members (priv->publish,
894 -1, &handles, message, NULL, NULL, NULL, NULL);
899 tp_contact_list_remove (EmpathyContactList *list,
900 EmpathyContact *contact,
901 const gchar *message)
903 EmpathyTpContactListPriv *priv = GET_PRIV (list);
905 GArray handles = {(gchar *) &handle, 1};
907 handle = empathy_contact_get_handle (contact);
908 if (priv->subscribe) {
909 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
910 -1, &handles, message, NULL, NULL, NULL, NULL);
913 tp_cli_channel_interface_group_call_remove_members (priv->publish,
914 -1, &handles, message, NULL, NULL, NULL, NULL);
919 tp_contact_list_get_members (EmpathyContactList *list)
921 EmpathyTpContactListPriv *priv = GET_PRIV (list);
924 ret = g_hash_table_get_values (priv->members);
925 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
930 tp_contact_list_get_pendings (EmpathyContactList *list)
932 EmpathyTpContactListPriv *priv = GET_PRIV (list);
935 ret = g_hash_table_get_values (priv->pendings);
936 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
941 tp_contact_list_get_all_groups (EmpathyContactList *list)
943 EmpathyTpContactListPriv *priv = GET_PRIV (list);
946 ret = g_hash_table_get_keys (priv->groups);
947 for (l = ret; l; l = l->next) {
948 l->data = g_strdup (l->data);
951 if (priv->protocol_group) {
952 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
959 tp_contact_list_get_groups (EmpathyContactList *list,
960 EmpathyContact *contact)
962 EmpathyTpContactListPriv *priv = GET_PRIV (list);
969 handle = empathy_contact_get_handle (contact);
970 g_hash_table_iter_init (&iter, priv->groups);
971 while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
972 const TpIntSet *members;
974 members = tp_channel_group_get_members (channel);
975 if (tp_intset_is_member (members, handle)) {
976 ret = g_list_prepend (ret, g_strdup (group_name));
980 if (priv->protocol_group) {
981 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
988 tp_contact_list_add_to_group (EmpathyContactList *list,
989 EmpathyContact *contact,
990 const gchar *group_name)
995 handle = empathy_contact_get_handle (contact);
996 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
997 g_array_append_val (handles, handle);
998 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
999 group_name, handles);
1003 tp_contact_list_remove_from_group (EmpathyContactList *list,
1004 EmpathyContact *contact,
1005 const gchar *group_name)
1007 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1010 GArray handles = {(gchar *) &handle, 1};
1012 channel = g_hash_table_lookup (priv->groups, group_name);
1013 if (channel == NULL) {
1017 handle = empathy_contact_get_handle (contact);
1018 DEBUG ("remove contact %s (%d) from group %s",
1019 empathy_contact_get_id (contact), handle, group_name);
1021 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1022 &handles, NULL, NULL, NULL, NULL, NULL);
1026 tp_contact_list_rename_group (EmpathyContactList *list,
1027 const gchar *old_group_name,
1028 const gchar *new_group_name)
1030 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1032 const TpIntSet *members;
1035 channel = g_hash_table_lookup (priv->groups, old_group_name);
1036 if (channel == NULL) {
1040 DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1042 /* Remove all members and close the old channel */
1043 members = tp_channel_group_get_members (channel);
1044 handles = tp_intset_to_array (members);
1045 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1046 handles, NULL, NULL, NULL, NULL, NULL);
1047 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1049 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1050 new_group_name, handles);
1054 tp_contact_list_remove_group (EmpathyContactList *list,
1055 const gchar *group_name)
1057 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1059 const TpIntSet *members;
1062 channel = g_hash_table_lookup (priv->groups, group_name);
1063 if (channel == NULL) {
1067 DEBUG ("remove group %s", group_name);
1069 /* Remove all members and close the channel */
1070 members = tp_channel_group_get_members (channel);
1071 handles = tp_intset_to_array (members);
1072 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1073 handles, NULL, NULL, NULL, NULL, NULL);
1074 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1075 g_array_free (handles, TRUE);
1079 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1081 iface->add = tp_contact_list_add;
1082 iface->remove = tp_contact_list_remove;
1083 iface->get_members = tp_contact_list_get_members;
1084 iface->get_pendings = tp_contact_list_get_pendings;
1085 iface->get_all_groups = tp_contact_list_get_all_groups;
1086 iface->get_groups = tp_contact_list_get_groups;
1087 iface->add_to_group = tp_contact_list_add_to_group;
1088 iface->remove_from_group = tp_contact_list_remove_from_group;
1089 iface->rename_group = tp_contact_list_rename_group;
1090 iface->remove_group = tp_contact_list_remove_group;
1094 empathy_tp_contact_list_can_add (EmpathyTpContactList *list)
1096 EmpathyTpContactListPriv *priv;
1097 TpChannelGroupFlags flags;
1099 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1101 priv = GET_PRIV (list);
1103 if (priv->subscribe == NULL)
1106 flags = tp_channel_group_get_flags (priv->subscribe);
1107 return (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) != 0;
1111 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1113 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1114 GHashTableIter iter;
1117 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1119 /* Remove all contacts */
1120 g_hash_table_iter_init (&iter, priv->members);
1121 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1122 g_signal_emit_by_name (list, "members-changed", contact,
1126 g_hash_table_remove_all (priv->members);
1128 g_hash_table_iter_init (&iter, priv->pendings);
1129 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1130 g_signal_emit_by_name (list, "pendings-changed", contact,
1134 g_hash_table_remove_all (priv->pendings);