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;
51 GHashTable *members; /* handle -> EmpathyContact */
52 GHashTable *pendings; /* handle -> EmpathyContact */
53 GHashTable *groups; /* group name -> TpChannel */
54 GHashTable *add_to_group; /* group name -> GArray of handles */
56 EmpathyContactListFlags flags;
58 TpProxySignalConnection *new_channels_sig;
59 } EmpathyTpContactListPriv;
62 TP_CONTACT_LIST_TYPE_PUBLISH,
63 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
64 TP_CONTACT_LIST_TYPE_UNKNOWN
67 static void tp_contact_list_iface_init (EmpathyContactListIface *iface);
74 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
75 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
76 tp_contact_list_iface_init));
79 tp_contact_list_forget_group (EmpathyTpContactList *list,
82 EmpathyTpContactListPriv *priv = GET_PRIV (list);
83 const TpIntSet *members;
85 const gchar *group_name;
87 group_name = tp_channel_get_identifier (channel);
89 /* Signal that all members are not in that group anymore */
90 members = tp_channel_group_get_members (channel);
91 tp_intset_iter_init (&iter, members);
92 while (tp_intset_iter_next (&iter)) {
93 EmpathyContact *contact;
95 contact = g_hash_table_lookup (priv->members,
96 GUINT_TO_POINTER (iter.element));
97 if (contact == NULL) {
101 DEBUG ("Contact %s (%d) removed from group %s",
102 empathy_contact_get_id (contact), iter.element,
104 g_signal_emit_by_name (list, "groups-changed", contact,
111 tp_contact_list_group_invalidated_cb (TpChannel *channel,
115 EmpathyTpContactList *list)
117 EmpathyTpContactListPriv *priv = GET_PRIV (list);
118 const gchar *group_name;
120 group_name = tp_channel_get_identifier (channel);
121 DEBUG ("Group %s invalidated. Message: %s", group_name, message);
123 tp_contact_list_forget_group (list, channel);
125 g_hash_table_remove (priv->groups, group_name);
129 contacts_added_to_group (EmpathyTpContactList *list,
133 EmpathyTpContactListPriv *priv = GET_PRIV (list);
134 const gchar *group_name;
137 group_name = tp_channel_get_identifier (channel);
139 for (i = 0; i < added->len; i++) {
140 EmpathyContact *contact;
143 handle = g_array_index (added, TpHandle, i);
144 contact = g_hash_table_lookup (priv->members,
145 GUINT_TO_POINTER (handle));
146 if (contact == NULL) {
150 DEBUG ("Contact %s (%d) added to group %s",
151 empathy_contact_get_id (contact), handle, group_name);
152 g_signal_emit_by_name (list, "groups-changed", contact,
159 tp_contact_list_group_members_changed_cb (TpChannel *channel,
163 GArray *local_pending,
164 GArray *remote_pending,
167 EmpathyTpContactList *list)
169 EmpathyTpContactListPriv *priv = GET_PRIV (list);
170 const gchar *group_name;
173 contacts_added_to_group (list, channel, added);
175 group_name = tp_channel_get_identifier (channel);
177 for (i = 0; i < removed->len; i++) {
178 EmpathyContact *contact;
181 handle = g_array_index (removed, TpHandle, i);
182 contact = g_hash_table_lookup (priv->members,
183 GUINT_TO_POINTER (handle));
184 if (contact == NULL) {
188 DEBUG ("Contact %s (%d) removed from group %s",
189 empathy_contact_get_id (contact), handle, group_name);
191 g_signal_emit_by_name (list, "groups-changed", contact,
198 tp_contact_list_group_ready_cb (TpChannel *channel,
202 EmpathyTpContactListPriv *priv = GET_PRIV (list);
203 TpChannel *old_group;
204 const gchar *group_name;
205 const TpIntSet *members;
209 DEBUG ("Error: %s", error->message);
210 g_object_unref (channel);
214 group_name = tp_channel_get_identifier (channel);
216 /* If there's already a group with this name in the table, we can't
217 * just let it be replaced. Replacing it causes it to be unreffed,
218 * which causes it to be invalidated (see
219 * <https://bugs.freedesktop.org/show_bug.cgi?id=22119>), which causes
220 * it to be removed from the hash table again, which causes it to be
223 old_group = g_hash_table_lookup (priv->groups, group_name);
225 if (old_group != NULL) {
226 DEBUG ("Discarding old group %s (%p)", group_name, old_group);
227 g_hash_table_steal (priv->groups, group_name);
228 tp_contact_list_forget_group (list, old_group);
229 g_object_unref (old_group);
232 g_hash_table_insert (priv->groups, (gpointer) group_name, channel);
233 DEBUG ("Group %s added", group_name);
235 g_signal_connect (channel, "group-members-changed",
236 G_CALLBACK (tp_contact_list_group_members_changed_cb),
239 g_signal_connect (channel, "invalidated",
240 G_CALLBACK (tp_contact_list_group_invalidated_cb),
243 if (priv->add_to_group) {
246 handles = g_hash_table_lookup (priv->add_to_group, group_name);
248 DEBUG ("Adding initial members to group %s", group_name);
249 tp_cli_channel_interface_group_call_add_members (channel,
250 -1, handles, NULL, NULL, NULL, NULL, NULL);
251 g_hash_table_remove (priv->add_to_group, group_name);
255 /* Get initial members of the group */
256 members = tp_channel_group_get_members (channel);
257 g_assert (members != NULL);
258 arr = tp_intset_to_array (members);
259 contacts_added_to_group (list, channel, arr);
260 g_array_free (arr, TRUE);
264 tp_contact_list_group_add_channel (EmpathyTpContactList *list,
265 const gchar *object_path,
266 const gchar *channel_type,
267 TpHandleType handle_type,
270 EmpathyTpContactListPriv *priv = GET_PRIV (list);
273 /* Only accept server-side contact groups */
274 if (tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) ||
275 handle_type != TP_HANDLE_TYPE_GROUP) {
279 channel = tp_channel_new (priv->connection,
280 object_path, channel_type,
281 handle_type, handle, NULL);
283 /* Give the ref to the callback */
284 tp_channel_call_when_ready (channel,
285 tp_contact_list_group_ready_cb,
290 tp_contact_list_group_request_channel_cb (TpConnection *connection,
291 const gchar *object_path,
296 /* The new channel will be handled in NewChannel cb. Here we only
297 * handle the error if RequestChannel failed */
299 DEBUG ("Error: %s", error->message);
305 tp_contact_list_group_request_handles_cb (TpConnection *connection,
306 const GArray *handles,
311 TpHandle channel_handle;
314 DEBUG ("Error: %s", error->message);
318 channel_handle = g_array_index (handles, TpHandle, 0);
319 tp_cli_connection_call_request_channel (connection, -1,
320 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
321 TP_HANDLE_TYPE_GROUP,
324 tp_contact_list_group_request_channel_cb,
329 /* This function takes ownership of handles array */
331 tp_contact_list_group_add (EmpathyTpContactList *list,
332 const gchar *group_name,
335 EmpathyTpContactListPriv *priv = GET_PRIV (list);
337 const gchar *names[] = {group_name, NULL};
339 /* Search the channel for that group name */
340 channel = g_hash_table_lookup (priv->groups, group_name);
342 tp_cli_channel_interface_group_call_add_members (channel, -1,
343 handles, NULL, NULL, NULL, NULL, NULL);
344 g_array_free (handles, TRUE);
348 /* That group does not exist yet, we have to:
349 * 1) Request an handle for the group name
350 * 2) Request a channel
351 * 3) When NewChannel is emitted, add handles in members
353 g_hash_table_insert (priv->add_to_group,
354 g_strdup (group_name),
356 tp_cli_connection_call_request_handles (priv->connection, -1,
357 TP_HANDLE_TYPE_GROUP, names,
358 tp_contact_list_group_request_handles_cb,
364 tp_contact_list_got_added_members_cb (EmpathyTpContactFactory *factory,
366 EmpathyContact * const * contacts,
368 const TpHandle *failed,
373 EmpathyTpContactListPriv *priv = GET_PRIV (list);
377 DEBUG ("Error: %s", error->message);
381 for (i = 0; i < n_contacts; i++) {
382 EmpathyContact *contact = contacts[i];
385 handle = empathy_contact_get_handle (contact);
386 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
389 /* Add to the list and emit signal */
390 g_hash_table_insert (priv->members, GUINT_TO_POINTER (handle),
391 g_object_ref (contact));
392 g_signal_emit_by_name (list, "members-changed", contact,
395 /* This contact is now member, implicitly accept pending. */
396 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
397 GArray handles = {(gchar *) &handle, 1};
399 tp_cli_channel_interface_group_call_add_members (priv->publish,
400 -1, &handles, NULL, NULL, NULL, NULL, NULL);
406 tp_contact_list_got_local_pending_cb (EmpathyTpContactFactory *factory,
408 EmpathyContact * const * contacts,
410 const TpHandle *failed,
415 EmpathyTpContactListPriv *priv = GET_PRIV (list);
419 DEBUG ("Error: %s", error->message);
423 for (i = 0; i < n_contacts; i++) {
424 EmpathyContact *contact = contacts[i];
426 const gchar *message;
427 TpChannelGroupChangeReason reason;
429 handle = empathy_contact_get_handle (contact);
430 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle))) {
431 GArray handles = {(gchar *) &handle, 1};
433 /* This contact is already member, auto accept. */
434 tp_cli_channel_interface_group_call_add_members (priv->publish,
435 -1, &handles, NULL, NULL, NULL, NULL, NULL);
437 else if (tp_channel_group_get_local_pending_info (priv->publish,
442 /* Add contact to pendings */
443 g_hash_table_insert (priv->pendings, GUINT_TO_POINTER (handle),
444 g_object_ref (contact));
445 g_signal_emit_by_name (list, "pendings-changed", contact,
446 contact, reason, message, TRUE);
452 tp_contact_list_remove_handle (EmpathyTpContactList *list,
456 EmpathyTpContactListPriv *priv = GET_PRIV (list);
457 EmpathyContact *contact;
460 if (table == priv->pendings)
461 sig = "pendings-changed";
462 else if (table == priv->members)
463 sig = "members-changed";
467 contact = g_hash_table_lookup (table, GUINT_TO_POINTER (handle));
469 g_object_ref (contact);
470 g_hash_table_remove (table, GUINT_TO_POINTER (handle));
471 g_signal_emit_by_name (list, sig, contact, 0, 0, NULL,
473 g_object_unref (contact);
478 tp_contact_list_publish_group_members_changed_cb (TpChannel *channel,
482 GArray *local_pending,
483 GArray *remote_pending,
485 TpChannelGroupChangeReason reason,
486 EmpathyTpContactList *list)
488 EmpathyTpContactListPriv *priv = GET_PRIV (list);
491 /* We now send our presence to those contacts, remove them from pendings */
492 for (i = 0; i < added->len; i++) {
493 tp_contact_list_remove_handle (list, priv->pendings,
494 g_array_index (added, TpHandle, i));
497 /* We refuse to send our presence to those contacts, remove from pendings */
498 for (i = 0; i < removed->len; i++) {
499 tp_contact_list_remove_handle (list, priv->pendings,
500 g_array_index (removed, TpHandle, i));
503 /* Those contacts want our presence, auto accept those that are already
504 * member, otherwise add in pendings. */
505 if (local_pending->len > 0) {
506 empathy_tp_contact_factory_get_from_handles (priv->factory,
507 local_pending->len, (TpHandle *) local_pending->data,
508 tp_contact_list_got_local_pending_cb, NULL, NULL,
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_subscribe_group_members_changed_cb (TpChannel *channel,
576 GArray *local_pending,
577 GArray *remote_pending,
580 EmpathyTpContactList *list)
582 EmpathyTpContactListPriv *priv = GET_PRIV (list);
585 /* We now get the presence of those contacts, add them to members */
586 if (added->len > 0) {
587 empathy_tp_contact_factory_get_from_handles (priv->factory,
588 added->len, (TpHandle *) added->data,
589 tp_contact_list_got_added_members_cb, NULL, NULL,
593 /* Those contacts refuse to send us their presence, remove from members. */
594 for (i = 0; i < removed->len; i++) {
595 tp_contact_list_remove_handle (list, priv->members,
596 g_array_index (removed, TpHandle, i));
599 /* We want those contacts in our contact list but we don't get their
600 * presence yet. Add to members anyway. */
601 if (remote_pending->len > 0) {
602 empathy_tp_contact_factory_get_from_handles (priv->factory,
603 remote_pending->len, (TpHandle *) remote_pending->data,
604 tp_contact_list_got_added_members_cb, NULL, NULL,
610 tp_contact_list_new_channel_cb (TpConnection *proxy,
611 const gchar *object_path,
612 const gchar *channel_type,
615 gboolean suppress_handler,
619 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
620 object_path, channel_type,
621 handle_type, handle);
625 tp_contact_list_list_channels_cb (TpConnection *connection,
626 const GPtrArray *channels,
634 DEBUG ("Error: %s", error->message);
638 for (i = 0; i < channels->len; i++) {
639 GValueArray *chan_struct;
640 const gchar *object_path;
641 const gchar *channel_type;
642 TpHandleType handle_type;
645 chan_struct = g_ptr_array_index (channels, i);
646 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
647 channel_type = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
648 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
649 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
651 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
652 object_path, channel_type,
653 handle_type, handle);
658 tp_contact_list_finalize (GObject *object)
660 EmpathyTpContactListPriv *priv;
661 EmpathyTpContactList *list;
665 list = EMPATHY_TP_CONTACT_LIST (object);
666 priv = GET_PRIV (list);
668 DEBUG ("finalize: %p", object);
670 if (priv->subscribe) {
671 g_object_unref (priv->subscribe);
674 g_object_unref (priv->publish);
677 g_object_unref (priv->stored);
680 if (priv->connection) {
681 g_object_unref (priv->connection);
685 g_object_unref (priv->factory);
688 g_hash_table_iter_init (&iter, priv->groups);
689 while (g_hash_table_iter_next (&iter, NULL, &channel)) {
690 g_signal_handlers_disconnect_by_func (channel,
691 tp_contact_list_group_invalidated_cb, list);
694 g_hash_table_destroy (priv->groups);
695 g_hash_table_destroy (priv->members);
696 g_hash_table_destroy (priv->pendings);
697 g_hash_table_destroy (priv->add_to_group);
699 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
703 received_all_list_channels (EmpathyTpContactList *self)
705 EmpathyTpContactListPriv *priv = GET_PRIV (self);
707 return (priv->stored != NULL && priv->publish != NULL &&
708 priv->subscribe != NULL);
712 got_list_channel (EmpathyTpContactList *list,
715 EmpathyTpContactListPriv *priv = GET_PRIV (list);
718 /* We requested that channel by providing TargetID property, so it's
719 * guaranteed that tp_channel_get_identifier will return it. */
720 id = tp_channel_get_identifier (channel);
722 /* TpChannel emits initial set of members just before being ready */
723 if (!tp_strdiff (id, "stored")) {
724 if (priv->stored != NULL)
726 priv->stored = g_object_ref (channel);
727 } else if (!tp_strdiff (id, "publish")) {
728 if (priv->publish != NULL)
730 priv->publish = g_object_ref (channel);
731 g_signal_connect (priv->publish, "group-members-changed",
732 G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
734 } else if (!tp_strdiff (id, "subscribe")) {
735 if (priv->subscribe != NULL)
737 priv->subscribe = g_object_ref (channel);
738 g_signal_connect (priv->subscribe, "group-members-changed",
739 G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
743 if (received_all_list_channels (list) && priv->new_channels_sig != NULL) {
744 /* We don't need to watch NewChannels anymore */
745 tp_proxy_signal_connection_disconnect (priv->new_channels_sig);
746 priv->new_channels_sig = NULL;
751 list_ensure_channel_cb (TpConnection *conn,
754 GHashTable *properties,
757 GObject *weak_object)
759 EmpathyTpContactList *list = user_data;
763 DEBUG ("failed: %s\n", error->message);
767 channel = tp_channel_new_from_properties (conn, path, properties, NULL);
768 got_list_channel (list, channel);
769 g_object_unref (channel);
773 new_channels_cb (TpConnection *conn,
774 const GPtrArray *channels,
776 GObject *weak_object)
778 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
781 for (i = 0; i < channels->len ; i++) {
782 GValueArray *arr = g_ptr_array_index (channels, i);
784 GHashTable *properties;
788 path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
789 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
791 if (tp_strdiff (tp_asv_get_string (properties,
792 TP_IFACE_CHANNEL ".ChannelType"),
793 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST))
796 if (tp_asv_get_uint32 (properties,
797 TP_IFACE_CHANNEL ".TargetHandleType", NULL)
798 != TP_HANDLE_TYPE_LIST)
801 id = tp_asv_get_string (properties,
802 TP_IFACE_CHANNEL ".TargetID");
806 channel = tp_channel_new_from_properties (conn, path,
808 got_list_channel (list, channel);
809 g_object_unref (channel);
814 conn_ready_cb (TpConnection *connection,
818 EmpathyTpContactList *list = data;
819 EmpathyTpContactListPriv *priv = GET_PRIV (list);
823 DEBUG ("failed: %s", error->message);
827 request = tp_asv_new (
828 TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
829 TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_UINT, TP_HANDLE_TYPE_LIST,
832 /* Watch the NewChannels signal so if ensuring list channels fails (for
833 * example because the server is slow and the D-Bus call timeouts before CM
834 * fetches the roster), we have a chance to get them later. */
835 priv->new_channels_sig =
836 tp_cli_connection_interface_requests_connect_to_new_channels (
837 priv->connection, new_channels_cb, NULL, NULL, G_OBJECT (list), NULL);
839 /* Request the 'stored' list. */
840 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "stored");
841 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
842 -1, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
844 /* Request the 'publish' list. */
845 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "publish");
846 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
847 -1, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
849 /* Request the 'subscribe' list. */
850 tp_asv_set_static_string (request, TP_IFACE_CHANNEL ".TargetID", "subscribe");
851 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
852 -1, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
854 g_hash_table_unref (request);
856 g_object_unref (list);
860 tp_contact_list_constructed (GObject *list)
862 EmpathyTpContactListPriv *priv = GET_PRIV (list);
863 gchar *protocol_name = NULL;
865 priv->factory = empathy_tp_contact_factory_dup_singleton (priv->connection);
867 /* call GetAliasFlags */
868 if (tp_proxy_has_interface_by_id (priv->connection,
869 TP_IFACE_QUARK_CONNECTION_INTERFACE_ALIASING)) {
870 tp_cli_connection_interface_aliasing_call_get_alias_flags (
873 tp_contact_list_get_alias_flags_cb,
878 /* lookup RequestableChannelClasses */
879 if (tp_proxy_has_interface_by_id (priv->connection,
880 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS)) {
881 tp_cli_dbus_properties_call_get (priv->connection,
883 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
884 "RequestableChannelClasses",
885 tp_contact_list_get_requestablechannelclasses_cb,
889 /* we just don't know... better mark the flag just in case */
890 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
893 tp_connection_call_when_ready (priv->connection, conn_ready_cb,
894 g_object_ref (list));
896 tp_cli_connection_call_list_channels (priv->connection, -1,
897 tp_contact_list_list_channels_cb,
901 tp_cli_connection_connect_to_new_channel (priv->connection,
902 tp_contact_list_new_channel_cb,
906 /* Check for protocols that does not support contact groups. We can
907 * put all contacts into a special group in that case.
908 * FIXME: Default group should be an information in the profile */
909 tp_connection_parse_object_path (priv->connection, &protocol_name, NULL);
910 if (!tp_strdiff (protocol_name, "local-xmpp")) {
911 priv->protocol_group = _("People nearby");
913 g_free (protocol_name);
917 tp_contact_list_get_property (GObject *object,
922 EmpathyTpContactListPriv *priv = GET_PRIV (object);
925 case PROP_CONNECTION:
926 g_value_set_object (value, priv->connection);
929 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
935 tp_contact_list_set_property (GObject *object,
940 EmpathyTpContactListPriv *priv = GET_PRIV (object);
943 case PROP_CONNECTION:
944 priv->connection = g_value_dup_object (value);
947 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
953 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
955 GObjectClass *object_class = G_OBJECT_CLASS (klass);
957 object_class->finalize = tp_contact_list_finalize;
958 object_class->constructed = tp_contact_list_constructed;
959 object_class->get_property = tp_contact_list_get_property;
960 object_class->set_property = tp_contact_list_set_property;
962 g_object_class_install_property (object_class,
964 g_param_spec_object ("connection",
966 "The connection associated with the contact list",
969 G_PARAM_CONSTRUCT_ONLY));
971 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
975 tp_contact_list_array_free (gpointer handles)
977 g_array_free (handles, TRUE);
981 empathy_tp_contact_list_init (EmpathyTpContactList *list)
983 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
984 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
988 /* Map group's name to group's TpChannel. The group name string is owned
989 * by the TpChannel object */
990 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
992 (GDestroyNotify) g_object_unref);
994 /* Map contact's handle to EmpathyContact object */
995 priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
997 (GDestroyNotify) g_object_unref);
999 /* Map contact's handle to EmpathyContact object */
1000 priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1002 (GDestroyNotify) g_object_unref);
1004 /* Map group's name to GArray of handle */
1005 priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
1007 tp_contact_list_array_free);
1010 EmpathyTpContactList *
1011 empathy_tp_contact_list_new (TpConnection *connection)
1013 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
1014 "connection", connection,
1019 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
1021 EmpathyTpContactListPriv *priv;
1023 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
1025 priv = GET_PRIV (list);
1027 return priv->connection;
1031 tp_contact_list_add (EmpathyContactList *list,
1032 EmpathyContact *contact,
1033 const gchar *message)
1035 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1037 GArray handles = {(gchar *) &handle, 1};
1039 handle = empathy_contact_get_handle (contact);
1040 if (priv->subscribe) {
1041 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
1042 -1, &handles, message, NULL, NULL, NULL, NULL);
1044 if (priv->publish) {
1045 TpChannelGroupFlags flags = tp_channel_group_get_flags (priv->subscribe);
1046 if (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD ||
1047 g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
1048 tp_cli_channel_interface_group_call_add_members (priv->publish,
1049 -1, &handles, message, NULL, NULL, NULL, NULL);
1055 tp_contact_list_remove (EmpathyContactList *list,
1056 EmpathyContact *contact,
1057 const gchar *message)
1059 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1061 GArray handles = {(gchar *) &handle, 1};
1063 handle = empathy_contact_get_handle (contact);
1065 /* FIXME: this is racy if tp_contact_list_remove is called before the
1066 * 'stored' list has been retrieved. */
1067 if (priv->stored != NULL) {
1068 tp_cli_channel_interface_group_call_remove_members (priv->stored,
1069 -1, &handles, message, NULL, NULL, NULL, NULL);
1072 if (priv->subscribe) {
1073 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
1074 -1, &handles, message, NULL, NULL, NULL, NULL);
1076 if (priv->publish) {
1077 tp_cli_channel_interface_group_call_remove_members (priv->publish,
1078 -1, &handles, message, NULL, NULL, NULL, NULL);
1083 tp_contact_list_get_members (EmpathyContactList *list)
1085 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1088 ret = g_hash_table_get_values (priv->members);
1089 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1094 tp_contact_list_get_pendings (EmpathyContactList *list)
1096 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1099 ret = g_hash_table_get_values (priv->pendings);
1100 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1105 tp_contact_list_get_all_groups (EmpathyContactList *list)
1107 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1110 ret = g_hash_table_get_keys (priv->groups);
1111 for (l = ret; l; l = l->next) {
1112 l->data = g_strdup (l->data);
1115 if (priv->protocol_group) {
1116 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
1123 tp_contact_list_get_groups (EmpathyContactList *list,
1124 EmpathyContact *contact)
1126 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1128 GHashTableIter iter;
1129 gpointer group_name;
1133 handle = empathy_contact_get_handle (contact);
1134 g_hash_table_iter_init (&iter, priv->groups);
1135 while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
1136 const TpIntSet *members;
1138 members = tp_channel_group_get_members (channel);
1139 if (tp_intset_is_member (members, handle)) {
1140 ret = g_list_prepend (ret, g_strdup (group_name));
1144 if (priv->protocol_group) {
1145 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
1152 tp_contact_list_add_to_group (EmpathyContactList *list,
1153 EmpathyContact *contact,
1154 const gchar *group_name)
1159 handle = empathy_contact_get_handle (contact);
1160 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1161 g_array_append_val (handles, handle);
1162 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1163 group_name, handles);
1167 tp_contact_list_remove_from_group (EmpathyContactList *list,
1168 EmpathyContact *contact,
1169 const gchar *group_name)
1171 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1174 GArray handles = {(gchar *) &handle, 1};
1176 channel = g_hash_table_lookup (priv->groups, group_name);
1177 if (channel == NULL) {
1181 handle = empathy_contact_get_handle (contact);
1182 DEBUG ("remove contact %s (%d) from group %s",
1183 empathy_contact_get_id (contact), handle, group_name);
1185 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1186 &handles, NULL, NULL, NULL, NULL, NULL);
1190 tp_contact_list_rename_group (EmpathyContactList *list,
1191 const gchar *old_group_name,
1192 const gchar *new_group_name)
1194 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1196 const TpIntSet *members;
1199 channel = g_hash_table_lookup (priv->groups, old_group_name);
1200 if (channel == NULL) {
1204 DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1206 /* Remove all members and close the old channel */
1207 members = tp_channel_group_get_members (channel);
1208 handles = tp_intset_to_array (members);
1209 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1210 handles, NULL, NULL, NULL, NULL, NULL);
1211 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1213 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1214 new_group_name, handles);
1218 tp_contact_list_remove_group (EmpathyContactList *list,
1219 const gchar *group_name)
1221 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1223 const TpIntSet *members;
1226 channel = g_hash_table_lookup (priv->groups, group_name);
1227 if (channel == NULL) {
1231 DEBUG ("remove group %s", group_name);
1233 /* Remove all members and close the channel */
1234 members = tp_channel_group_get_members (channel);
1235 handles = tp_intset_to_array (members);
1236 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1237 handles, NULL, NULL, NULL, NULL, NULL);
1238 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1239 g_array_free (handles, TRUE);
1242 static EmpathyContactListFlags
1243 tp_contact_list_get_flags (EmpathyContactList *list)
1245 EmpathyTpContactListPriv *priv;
1246 EmpathyContactListFlags flags;
1248 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1250 priv = GET_PRIV (list);
1251 flags = priv->flags;
1253 if (priv->subscribe != NULL) {
1254 TpChannelGroupFlags group_flags;
1256 group_flags = tp_channel_group_get_flags (priv->subscribe);
1258 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1259 flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1262 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1263 flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1271 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1273 iface->add = tp_contact_list_add;
1274 iface->remove = tp_contact_list_remove;
1275 iface->get_members = tp_contact_list_get_members;
1276 iface->get_pendings = tp_contact_list_get_pendings;
1277 iface->get_all_groups = tp_contact_list_get_all_groups;
1278 iface->get_groups = tp_contact_list_get_groups;
1279 iface->add_to_group = tp_contact_list_add_to_group;
1280 iface->remove_from_group = tp_contact_list_remove_from_group;
1281 iface->rename_group = tp_contact_list_rename_group;
1282 iface->remove_group = tp_contact_list_remove_group;
1283 iface->get_flags = tp_contact_list_get_flags;
1287 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1289 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1290 GHashTableIter iter;
1293 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1295 /* Remove all contacts */
1296 g_hash_table_iter_init (&iter, priv->members);
1297 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1298 g_signal_emit_by_name (list, "members-changed", contact,
1302 g_hash_table_remove_all (priv->members);
1304 g_hash_table_iter_init (&iter, priv->pendings);
1305 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1306 g_signal_emit_by_name (list, "pendings-changed", contact,
1310 g_hash_table_remove_all (priv->pendings);