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_get_requestablechannelclasses_cb (TpProxy *connection,
539 EmpathyTpContactListPriv *priv = GET_PRIV (list);
544 DEBUG ("Error: %s", error->message);
548 classes = g_value_get_boxed (value);
549 for (i = 0; i < classes->len; i++) {
550 GValueArray *class = g_ptr_array_index (classes, i);
552 const char *channel_type;
555 props = g_value_get_boxed (g_value_array_get_nth (class, 0));
557 channel_type = tp_asv_get_string (props,
558 TP_IFACE_CHANNEL ".ChannelType");
559 handle_type = tp_asv_get_uint32 (props,
560 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
562 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) &&
563 handle_type == TP_HANDLE_TYPE_GROUP) {
564 DEBUG ("Got channel class for a contact group");
565 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
572 tp_contact_list_publish_request_handle_cb (TpConnection *connection,
573 const GArray *handles,
581 DEBUG ("Error: %s", error->message);
585 handle = g_array_index (handles, TpHandle, 0);
586 tp_cli_connection_call_request_channel (connection, -1,
587 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
591 tp_contact_list_publish_request_channel_cb,
592 GUINT_TO_POINTER (handle), NULL,
597 tp_contact_list_subscribe_group_members_changed_cb (TpChannel *channel,
601 GArray *local_pending,
602 GArray *remote_pending,
605 EmpathyTpContactList *list)
607 EmpathyTpContactListPriv *priv = GET_PRIV (list);
610 /* We now get the presence of those contacts, add them to members */
611 if (added->len > 0) {
612 empathy_tp_contact_factory_get_from_handles (priv->factory,
613 added->len, (TpHandle *) added->data,
614 tp_contact_list_got_added_members_cb, NULL, NULL,
618 /* Those contacts refuse to send us their presence, remove from members. */
619 for (i = 0; i < removed->len; i++) {
620 tp_contact_list_remove_handle (list, priv->members,
621 g_array_index (added, TpHandle, i));
624 /* We want those contacts in our contact list but we don't get their
625 * presence yet. Add to members anyway. */
626 if (remote_pending->len > 0) {
627 empathy_tp_contact_factory_get_from_handles (priv->factory,
628 remote_pending->len, (TpHandle *) remote_pending->data,
629 tp_contact_list_got_added_members_cb, NULL, NULL,
635 tp_contact_list_subscribe_request_channel_cb (TpConnection *connection,
636 const gchar *object_path,
641 EmpathyTpContactListPriv *priv = GET_PRIV (list);
644 DEBUG ("Error: %s", error->message);
648 priv->subscribe = tp_channel_new (connection, object_path,
649 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
651 GPOINTER_TO_UINT (user_data),
654 /* TpChannel emits initial set of members just before being ready */
655 g_signal_connect (priv->subscribe, "group-members-changed",
656 G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
661 tp_contact_list_subscribe_request_handle_cb (TpConnection *connection,
662 const GArray *handles,
670 DEBUG ("Error: %s", error->message);
674 handle = g_array_index (handles, TpHandle, 0);
675 tp_cli_connection_call_request_channel (connection, -1,
676 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
680 tp_contact_list_subscribe_request_channel_cb,
681 GUINT_TO_POINTER (handle), NULL,
686 tp_contact_list_new_channel_cb (TpConnection *proxy,
687 const gchar *object_path,
688 const gchar *channel_type,
691 gboolean suppress_handler,
695 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
696 object_path, channel_type,
697 handle_type, handle);
701 tp_contact_list_list_channels_cb (TpConnection *connection,
702 const GPtrArray *channels,
710 DEBUG ("Error: %s", error->message);
714 for (i = 0; i < channels->len; i++) {
715 GValueArray *chan_struct;
716 const gchar *object_path;
717 const gchar *channel_type;
718 TpHandleType handle_type;
721 chan_struct = g_ptr_array_index (channels, i);
722 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
723 channel_type = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
724 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
725 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
727 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
728 object_path, channel_type,
729 handle_type, handle);
734 tp_contact_list_finalize (GObject *object)
736 EmpathyTpContactListPriv *priv;
737 EmpathyTpContactList *list;
741 list = EMPATHY_TP_CONTACT_LIST (object);
742 priv = GET_PRIV (list);
744 DEBUG ("finalize: %p", object);
746 if (priv->subscribe) {
747 g_object_unref (priv->subscribe);
750 g_object_unref (priv->publish);
753 if (priv->connection) {
754 g_object_unref (priv->connection);
758 g_object_unref (priv->factory);
761 g_hash_table_iter_init (&iter, priv->groups);
762 while (g_hash_table_iter_next (&iter, NULL, &channel)) {
763 g_signal_handlers_disconnect_by_func (channel,
764 tp_contact_list_group_invalidated_cb, list);
767 g_hash_table_destroy (priv->groups);
768 g_hash_table_destroy (priv->members);
769 g_hash_table_destroy (priv->pendings);
770 g_hash_table_destroy (priv->add_to_group);
772 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
776 tp_contact_list_constructed (GObject *list)
779 EmpathyTpContactListPriv *priv = GET_PRIV (list);
780 gchar *protocol_name = NULL;
781 const gchar *names[] = {NULL, NULL};
783 priv->factory = empathy_tp_contact_factory_dup_singleton (priv->connection);
785 /* call GetAliasFlags */
786 if (tp_proxy_has_interface_by_id (priv->connection,
787 TP_IFACE_QUARK_CONNECTION_INTERFACE_ALIASING)) {
788 tp_cli_connection_interface_aliasing_call_get_alias_flags (
791 tp_contact_list_get_alias_flags_cb,
796 /* lookup RequestableChannelClasses */
797 if (tp_proxy_has_interface_by_id (priv->connection,
798 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS)) {
799 tp_cli_dbus_properties_call_get (priv->connection,
801 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
802 "RequestableChannelClasses",
803 tp_contact_list_get_requestablechannelclasses_cb,
807 /* we just don't know... better mark the flag just in case */
808 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
811 names[0] = "publish";
812 tp_cli_connection_call_request_handles (priv->connection,
816 tp_contact_list_publish_request_handle_cb,
819 names[0] = "subscribe";
820 tp_cli_connection_call_request_handles (priv->connection,
824 tp_contact_list_subscribe_request_handle_cb,
828 tp_cli_connection_call_list_channels (priv->connection, -1,
829 tp_contact_list_list_channels_cb,
833 tp_cli_connection_connect_to_new_channel (priv->connection,
834 tp_contact_list_new_channel_cb,
838 /* Check for protocols that does not support contact groups. We can
839 * put all contacts into a special group in that case.
840 * FIXME: Default group should be an information in the profile */
841 tp_connection_parse_object_path (priv->connection, &protocol_name, NULL);
842 if (!tp_strdiff (protocol_name, "local-xmpp")) {
843 priv->protocol_group = _("People nearby");
845 g_free (protocol_name);
849 tp_contact_list_get_property (GObject *object,
854 EmpathyTpContactListPriv *priv = GET_PRIV (object);
857 case PROP_CONNECTION:
858 g_value_set_object (value, priv->connection);
861 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
867 tp_contact_list_set_property (GObject *object,
872 EmpathyTpContactListPriv *priv = GET_PRIV (object);
875 case PROP_CONNECTION:
876 priv->connection = g_value_dup_object (value);
879 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
885 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
887 GObjectClass *object_class = G_OBJECT_CLASS (klass);
889 object_class->finalize = tp_contact_list_finalize;
890 object_class->constructed = tp_contact_list_constructed;
891 object_class->get_property = tp_contact_list_get_property;
892 object_class->set_property = tp_contact_list_set_property;
894 g_object_class_install_property (object_class,
896 g_param_spec_object ("connection",
898 "The connection associated with the contact list",
901 G_PARAM_CONSTRUCT_ONLY));
903 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
907 tp_contact_list_array_free (gpointer handles)
909 g_array_free (handles, TRUE);
913 empathy_tp_contact_list_init (EmpathyTpContactList *list)
915 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
916 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
920 /* Map group's name to group's TpChannel. The group name string is owned
921 * by the TpChannel object */
922 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
924 (GDestroyNotify) g_object_unref);
926 /* Map contact's handle to EmpathyContact object */
927 priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
929 (GDestroyNotify) g_object_unref);
931 /* Map contact's handle to EmpathyContact object */
932 priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
934 (GDestroyNotify) g_object_unref);
936 /* Map group's name to GArray of handle */
937 priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
939 tp_contact_list_array_free);
942 EmpathyTpContactList *
943 empathy_tp_contact_list_new (TpConnection *connection)
945 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
946 "connection", connection,
951 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
953 EmpathyTpContactListPriv *priv;
955 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
957 priv = GET_PRIV (list);
959 return priv->connection;
963 tp_contact_list_add (EmpathyContactList *list,
964 EmpathyContact *contact,
965 const gchar *message)
967 EmpathyTpContactListPriv *priv = GET_PRIV (list);
969 GArray handles = {(gchar *) &handle, 1};
971 handle = empathy_contact_get_handle (contact);
972 if (priv->subscribe) {
973 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
974 -1, &handles, message, NULL, NULL, NULL, NULL);
977 g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
978 tp_cli_channel_interface_group_call_add_members (priv->publish,
979 -1, &handles, message, NULL, NULL, NULL, NULL);
984 tp_contact_list_remove (EmpathyContactList *list,
985 EmpathyContact *contact,
986 const gchar *message)
988 EmpathyTpContactListPriv *priv = GET_PRIV (list);
990 GArray handles = {(gchar *) &handle, 1};
992 handle = empathy_contact_get_handle (contact);
993 if (priv->subscribe) {
994 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
995 -1, &handles, message, NULL, NULL, NULL, NULL);
998 tp_cli_channel_interface_group_call_remove_members (priv->publish,
999 -1, &handles, message, NULL, NULL, NULL, NULL);
1004 tp_contact_list_get_members (EmpathyContactList *list)
1006 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1009 ret = g_hash_table_get_values (priv->members);
1010 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1015 tp_contact_list_get_pendings (EmpathyContactList *list)
1017 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1020 ret = g_hash_table_get_values (priv->pendings);
1021 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1026 tp_contact_list_get_all_groups (EmpathyContactList *list)
1028 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1031 ret = g_hash_table_get_keys (priv->groups);
1032 for (l = ret; l; l = l->next) {
1033 l->data = g_strdup (l->data);
1036 if (priv->protocol_group) {
1037 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
1044 tp_contact_list_get_groups (EmpathyContactList *list,
1045 EmpathyContact *contact)
1047 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1049 GHashTableIter iter;
1050 gpointer group_name;
1054 handle = empathy_contact_get_handle (contact);
1055 g_hash_table_iter_init (&iter, priv->groups);
1056 while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
1057 const TpIntSet *members;
1059 members = tp_channel_group_get_members (channel);
1060 if (tp_intset_is_member (members, handle)) {
1061 ret = g_list_prepend (ret, g_strdup (group_name));
1065 if (priv->protocol_group) {
1066 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
1073 tp_contact_list_add_to_group (EmpathyContactList *list,
1074 EmpathyContact *contact,
1075 const gchar *group_name)
1080 handle = empathy_contact_get_handle (contact);
1081 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1082 g_array_append_val (handles, handle);
1083 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1084 group_name, handles);
1088 tp_contact_list_remove_from_group (EmpathyContactList *list,
1089 EmpathyContact *contact,
1090 const gchar *group_name)
1092 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1095 GArray handles = {(gchar *) &handle, 1};
1097 channel = g_hash_table_lookup (priv->groups, group_name);
1098 if (channel == NULL) {
1102 handle = empathy_contact_get_handle (contact);
1103 DEBUG ("remove contact %s (%d) from group %s",
1104 empathy_contact_get_id (contact), handle, group_name);
1106 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1107 &handles, NULL, NULL, NULL, NULL, NULL);
1111 tp_contact_list_rename_group (EmpathyContactList *list,
1112 const gchar *old_group_name,
1113 const gchar *new_group_name)
1115 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1117 const TpIntSet *members;
1120 channel = g_hash_table_lookup (priv->groups, old_group_name);
1121 if (channel == NULL) {
1125 DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1127 /* Remove all members and close the old channel */
1128 members = tp_channel_group_get_members (channel);
1129 handles = tp_intset_to_array (members);
1130 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1131 handles, NULL, NULL, NULL, NULL, NULL);
1132 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1134 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1135 new_group_name, handles);
1139 tp_contact_list_remove_group (EmpathyContactList *list,
1140 const gchar *group_name)
1142 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1144 const TpIntSet *members;
1147 channel = g_hash_table_lookup (priv->groups, group_name);
1148 if (channel == NULL) {
1152 DEBUG ("remove group %s", group_name);
1154 /* Remove all members and close the channel */
1155 members = tp_channel_group_get_members (channel);
1156 handles = tp_intset_to_array (members);
1157 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1158 handles, NULL, NULL, NULL, NULL, NULL);
1159 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1160 g_array_free (handles, TRUE);
1163 static EmpathyContactListFlags
1164 tp_contact_list_get_flags (EmpathyContactList *list)
1166 EmpathyTpContactListPriv *priv;
1167 EmpathyContactListFlags flags;
1168 TpChannelGroupFlags group_flags;
1170 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1172 priv = GET_PRIV (list);
1173 flags = priv->flags;
1175 group_flags = tp_channel_group_get_flags (priv->subscribe);
1177 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1178 flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1181 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1182 flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1189 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1191 iface->add = tp_contact_list_add;
1192 iface->remove = tp_contact_list_remove;
1193 iface->get_members = tp_contact_list_get_members;
1194 iface->get_pendings = tp_contact_list_get_pendings;
1195 iface->get_all_groups = tp_contact_list_get_all_groups;
1196 iface->get_groups = tp_contact_list_get_groups;
1197 iface->add_to_group = tp_contact_list_add_to_group;
1198 iface->remove_from_group = tp_contact_list_remove_from_group;
1199 iface->rename_group = tp_contact_list_rename_group;
1200 iface->remove_group = tp_contact_list_remove_group;
1201 iface->get_flags = tp_contact_list_get_flags;
1205 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1207 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1208 GHashTableIter iter;
1211 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1213 /* Remove all contacts */
1214 g_hash_table_iter_init (&iter, priv->members);
1215 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1216 g_signal_emit_by_name (list, "members-changed", contact,
1220 g_hash_table_remove_all (priv->members);
1222 g_hash_table_iter_init (&iter, priv->pendings);
1223 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1224 g_signal_emit_by_name (list, "pendings-changed", contact,
1228 g_hash_table_remove_all (priv->pendings);