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;
57 } EmpathyTpContactListPriv;
60 TP_CONTACT_LIST_TYPE_PUBLISH,
61 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
62 TP_CONTACT_LIST_TYPE_UNKNOWN
65 static void tp_contact_list_iface_init (EmpathyContactListIface *iface);
72 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
73 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
74 tp_contact_list_iface_init));
77 tp_contact_list_forget_group (EmpathyTpContactList *list,
80 EmpathyTpContactListPriv *priv = GET_PRIV (list);
81 const TpIntSet *members;
83 const gchar *group_name;
85 group_name = tp_channel_get_identifier (channel);
87 /* Signal that all members are not in that group anymore */
88 members = tp_channel_group_get_members (channel);
89 tp_intset_iter_init (&iter, members);
90 while (tp_intset_iter_next (&iter)) {
91 EmpathyContact *contact;
93 contact = g_hash_table_lookup (priv->members,
94 GUINT_TO_POINTER (iter.element));
95 if (contact == NULL) {
99 DEBUG ("Contact %s (%d) removed from group %s",
100 empathy_contact_get_id (contact), iter.element,
102 g_signal_emit_by_name (list, "groups-changed", contact,
109 tp_contact_list_group_invalidated_cb (TpChannel *channel,
113 EmpathyTpContactList *list)
115 EmpathyTpContactListPriv *priv = GET_PRIV (list);
116 const gchar *group_name;
118 group_name = tp_channel_get_identifier (channel);
119 DEBUG ("Group %s invalidated. Message: %s", group_name, message);
121 tp_contact_list_forget_group (list, channel);
123 g_hash_table_remove (priv->groups, group_name);
127 tp_contact_list_group_ready_cb (TpChannel *channel,
131 EmpathyTpContactListPriv *priv = GET_PRIV (list);
132 TpChannel *old_group;
133 const gchar *group_name;
136 DEBUG ("Error: %s", error->message);
137 g_object_unref (channel);
141 group_name = tp_channel_get_identifier (channel);
143 /* If there's already a group with this name in the table, we can't
144 * just let it be replaced. Replacing it causes it to be unreffed,
145 * which causes it to be invalidated (see
146 * <https://bugs.freedesktop.org/show_bug.cgi?id=22119>), which causes
147 * it to be removed from the hash table again, which causes it to be
150 old_group = g_hash_table_lookup (priv->groups, group_name);
152 if (old_group != NULL) {
153 DEBUG ("Discarding old group %s (%p)", group_name, old_group);
154 g_hash_table_steal (priv->groups, group_name);
155 tp_contact_list_forget_group (list, old_group);
156 g_object_unref (old_group);
159 g_hash_table_insert (priv->groups, (gpointer) group_name, channel);
160 DEBUG ("Group %s added", group_name);
162 g_signal_connect (channel, "invalidated",
163 G_CALLBACK (tp_contact_list_group_invalidated_cb),
166 if (priv->add_to_group) {
169 handles = g_hash_table_lookup (priv->add_to_group, group_name);
171 DEBUG ("Adding initial members to group %s", group_name);
172 tp_cli_channel_interface_group_call_add_members (channel,
173 -1, handles, NULL, NULL, NULL, NULL, NULL);
174 g_hash_table_remove (priv->add_to_group, group_name);
180 tp_contact_list_group_members_changed_cb (TpChannel *channel,
184 GArray *local_pending,
185 GArray *remote_pending,
188 EmpathyTpContactList *list)
190 EmpathyTpContactListPriv *priv = GET_PRIV (list);
191 const gchar *group_name;
194 group_name = tp_channel_get_identifier (channel);
196 for (i = 0; i < added->len; i++) {
197 EmpathyContact *contact;
200 handle = g_array_index (added, TpHandle, i);
201 contact = g_hash_table_lookup (priv->members,
202 GUINT_TO_POINTER (handle));
203 if (contact == NULL) {
207 DEBUG ("Contact %s (%d) added to group %s",
208 empathy_contact_get_id (contact), handle, group_name);
209 g_signal_emit_by_name (list, "groups-changed", contact,
214 for (i = 0; i < removed->len; i++) {
215 EmpathyContact *contact;
218 handle = g_array_index (removed, TpHandle, i);
219 contact = g_hash_table_lookup (priv->members,
220 GUINT_TO_POINTER (handle));
221 if (contact == NULL) {
225 DEBUG ("Contact %s (%d) removed from group %s",
226 empathy_contact_get_id (contact), handle, group_name);
228 g_signal_emit_by_name (list, "groups-changed", contact,
235 tp_contact_list_group_add_channel (EmpathyTpContactList *list,
236 const gchar *object_path,
237 const gchar *channel_type,
238 TpHandleType handle_type,
241 EmpathyTpContactListPriv *priv = GET_PRIV (list);
244 /* Only accept server-side contact groups */
245 if (tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) ||
246 handle_type != TP_HANDLE_TYPE_GROUP) {
250 channel = tp_channel_new (priv->connection,
251 object_path, channel_type,
252 handle_type, handle, NULL);
254 /* TpChannel emits initial set of members just before being ready */
255 g_signal_connect (channel, "group-members-changed",
256 G_CALLBACK (tp_contact_list_group_members_changed_cb),
259 /* Give the ref to the callback */
260 tp_channel_call_when_ready (channel,
261 tp_contact_list_group_ready_cb,
266 tp_contact_list_group_request_channel_cb (TpConnection *connection,
267 const gchar *object_path,
272 /* The new channel will be handled in NewChannel cb. Here we only
273 * handle the error if RequestChannel failed */
275 DEBUG ("Error: %s", error->message);
281 tp_contact_list_group_request_handles_cb (TpConnection *connection,
282 const GArray *handles,
287 TpHandle channel_handle;
290 DEBUG ("Error: %s", error->message);
294 channel_handle = g_array_index (handles, TpHandle, 0);
295 tp_cli_connection_call_request_channel (connection, -1,
296 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
297 TP_HANDLE_TYPE_GROUP,
300 tp_contact_list_group_request_channel_cb,
305 /* This function takes ownership of handles array */
307 tp_contact_list_group_add (EmpathyTpContactList *list,
308 const gchar *group_name,
311 EmpathyTpContactListPriv *priv = GET_PRIV (list);
313 const gchar *names[] = {group_name, NULL};
315 /* Search the channel for that group name */
316 channel = g_hash_table_lookup (priv->groups, group_name);
318 tp_cli_channel_interface_group_call_add_members (channel, -1,
319 handles, NULL, NULL, NULL, NULL, NULL);
320 g_array_free (handles, TRUE);
324 /* That group does not exist yet, we have to:
325 * 1) Request an handle for the group name
326 * 2) Request a channel
327 * 3) When NewChannel is emitted, add handles in members
329 g_hash_table_insert (priv->add_to_group,
330 g_strdup (group_name),
332 tp_cli_connection_call_request_handles (priv->connection, -1,
333 TP_HANDLE_TYPE_GROUP, names,
334 tp_contact_list_group_request_handles_cb,
340 tp_contact_list_got_added_members_cb (EmpathyTpContactFactory *factory,
342 EmpathyContact * const * contacts,
344 const TpHandle *failed,
349 EmpathyTpContactListPriv *priv = GET_PRIV (list);
353 DEBUG ("Error: %s", error->message);
357 for (i = 0; i < n_contacts; i++) {
358 EmpathyContact *contact = contacts[i];
361 handle = empathy_contact_get_handle (contact);
362 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
365 /* Add to the list and emit signal */
366 g_hash_table_insert (priv->members, GUINT_TO_POINTER (handle),
367 g_object_ref (contact));
368 g_signal_emit_by_name (list, "members-changed", contact,
371 /* This contact is now member, implicitly accept pending. */
372 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
373 GArray handles = {(gchar *) &handle, 1};
375 tp_cli_channel_interface_group_call_add_members (priv->publish,
376 -1, &handles, NULL, NULL, NULL, NULL, NULL);
382 tp_contact_list_got_local_pending_cb (EmpathyTpContactFactory *factory,
384 EmpathyContact * const * contacts,
386 const TpHandle *failed,
391 EmpathyTpContactListPriv *priv = GET_PRIV (list);
395 DEBUG ("Error: %s", error->message);
399 for (i = 0; i < n_contacts; i++) {
400 EmpathyContact *contact = contacts[i];
402 const gchar *message;
403 TpChannelGroupChangeReason reason;
405 handle = empathy_contact_get_handle (contact);
406 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle))) {
407 GArray handles = {(gchar *) &handle, 1};
409 /* This contact is already member, auto accept. */
410 tp_cli_channel_interface_group_call_add_members (priv->publish,
411 -1, &handles, NULL, NULL, NULL, NULL, NULL);
413 else if (tp_channel_group_get_local_pending_info (priv->publish,
418 /* Add contact to pendings */
419 g_hash_table_insert (priv->pendings, GUINT_TO_POINTER (handle),
420 g_object_ref (contact));
421 g_signal_emit_by_name (list, "pendings-changed", contact,
422 contact, reason, message, TRUE);
428 tp_contact_list_remove_handle (EmpathyTpContactList *list,
432 EmpathyTpContactListPriv *priv = GET_PRIV (list);
433 EmpathyContact *contact;
436 if (table == priv->pendings)
437 sig = "pendings-changed";
438 else if (table == priv->members)
439 sig = "members-changed";
443 contact = g_hash_table_lookup (table, GUINT_TO_POINTER (handle));
445 g_object_ref (contact);
446 g_hash_table_remove (table, GUINT_TO_POINTER (handle));
447 g_signal_emit_by_name (list, sig, contact, 0, 0, NULL,
449 g_object_unref (contact);
454 tp_contact_list_publish_group_members_changed_cb (TpChannel *channel,
458 GArray *local_pending,
459 GArray *remote_pending,
461 TpChannelGroupChangeReason reason,
462 EmpathyTpContactList *list)
464 EmpathyTpContactListPriv *priv = GET_PRIV (list);
467 /* We now send our presence to those contacts, remove them from pendings */
468 for (i = 0; i < added->len; i++) {
469 tp_contact_list_remove_handle (list, priv->pendings,
470 g_array_index (added, TpHandle, i));
473 /* We refuse to send our presence to those contacts, remove from pendings */
474 for (i = 0; i < removed->len; i++) {
475 tp_contact_list_remove_handle (list, priv->pendings,
476 g_array_index (removed, TpHandle, i));
479 /* Those contacts want our presence, auto accept those that are already
480 * member, otherwise add in pendings. */
481 if (local_pending->len > 0) {
482 empathy_tp_contact_factory_get_from_handles (priv->factory,
483 local_pending->len, (TpHandle *) local_pending->data,
484 tp_contact_list_got_local_pending_cb, NULL, NULL,
490 tp_contact_list_publish_request_channel_cb (TpConnection *connection,
491 const gchar *object_path,
496 EmpathyTpContactListPriv *priv = GET_PRIV (list);
499 DEBUG ("Error: %s", error->message);
503 priv->publish = tp_channel_new (connection, object_path,
504 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
506 GPOINTER_TO_UINT (user_data),
509 /* TpChannel emits initial set of members just before being ready */
510 g_signal_connect (priv->publish, "group-members-changed",
511 G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
516 tp_contact_list_get_alias_flags_cb (TpConnection *connection,
522 EmpathyTpContactListPriv *priv = GET_PRIV (list);
525 DEBUG ("Error: %s", error->message);
529 if (flags & TP_CONNECTION_ALIAS_FLAG_USER_SET) {
530 priv->flags |= EMPATHY_CONTACT_LIST_CAN_ALIAS;
535 tp_contact_list_get_requestablechannelclasses_cb (TpProxy *connection,
541 EmpathyTpContactListPriv *priv = GET_PRIV (list);
546 DEBUG ("Error: %s", error->message);
550 classes = g_value_get_boxed (value);
551 for (i = 0; i < classes->len; i++) {
552 GValueArray *class = g_ptr_array_index (classes, i);
554 const char *channel_type;
557 props = g_value_get_boxed (g_value_array_get_nth (class, 0));
559 channel_type = tp_asv_get_string (props,
560 TP_IFACE_CHANNEL ".ChannelType");
561 handle_type = tp_asv_get_uint32 (props,
562 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
564 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) &&
565 handle_type == TP_HANDLE_TYPE_GROUP) {
566 DEBUG ("Got channel class for a contact group");
567 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
574 tp_contact_list_publish_request_handle_cb (TpConnection *connection,
575 const GArray *handles,
583 DEBUG ("Error: %s", error->message);
587 handle = g_array_index (handles, TpHandle, 0);
588 tp_cli_connection_call_request_channel (connection, -1,
589 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
593 tp_contact_list_publish_request_channel_cb,
594 GUINT_TO_POINTER (handle), NULL,
599 tp_contact_list_subscribe_group_members_changed_cb (TpChannel *channel,
603 GArray *local_pending,
604 GArray *remote_pending,
607 EmpathyTpContactList *list)
609 EmpathyTpContactListPriv *priv = GET_PRIV (list);
612 /* We now get the presence of those contacts, add them to members */
613 if (added->len > 0) {
614 empathy_tp_contact_factory_get_from_handles (priv->factory,
615 added->len, (TpHandle *) added->data,
616 tp_contact_list_got_added_members_cb, NULL, NULL,
620 /* Those contacts refuse to send us their presence, remove from members. */
621 for (i = 0; i < removed->len; i++) {
622 tp_contact_list_remove_handle (list, priv->members,
623 g_array_index (removed, TpHandle, i));
626 /* We want those contacts in our contact list but we don't get their
627 * presence yet. Add to members anyway. */
628 if (remote_pending->len > 0) {
629 empathy_tp_contact_factory_get_from_handles (priv->factory,
630 remote_pending->len, (TpHandle *) remote_pending->data,
631 tp_contact_list_got_added_members_cb, NULL, NULL,
637 tp_contact_list_subscribe_request_channel_cb (TpConnection *connection,
638 const gchar *object_path,
643 EmpathyTpContactListPriv *priv = GET_PRIV (list);
646 DEBUG ("Error: %s", error->message);
650 priv->subscribe = tp_channel_new (connection, object_path,
651 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
653 GPOINTER_TO_UINT (user_data),
656 /* TpChannel emits initial set of members just before being ready */
657 g_signal_connect (priv->subscribe, "group-members-changed",
658 G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
663 tp_contact_list_subscribe_request_handle_cb (TpConnection *connection,
664 const GArray *handles,
672 DEBUG ("Error: %s", error->message);
676 handle = g_array_index (handles, TpHandle, 0);
677 tp_cli_connection_call_request_channel (connection, -1,
678 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
682 tp_contact_list_subscribe_request_channel_cb,
683 GUINT_TO_POINTER (handle), NULL,
688 tp_contact_list_new_channel_cb (TpConnection *proxy,
689 const gchar *object_path,
690 const gchar *channel_type,
693 gboolean suppress_handler,
697 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
698 object_path, channel_type,
699 handle_type, handle);
703 tp_contact_list_list_channels_cb (TpConnection *connection,
704 const GPtrArray *channels,
712 DEBUG ("Error: %s", error->message);
716 for (i = 0; i < channels->len; i++) {
717 GValueArray *chan_struct;
718 const gchar *object_path;
719 const gchar *channel_type;
720 TpHandleType handle_type;
723 chan_struct = g_ptr_array_index (channels, i);
724 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
725 channel_type = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
726 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
727 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
729 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
730 object_path, channel_type,
731 handle_type, handle);
736 tp_contact_list_finalize (GObject *object)
738 EmpathyTpContactListPriv *priv;
739 EmpathyTpContactList *list;
743 list = EMPATHY_TP_CONTACT_LIST (object);
744 priv = GET_PRIV (list);
746 DEBUG ("finalize: %p", object);
748 if (priv->subscribe) {
749 g_object_unref (priv->subscribe);
752 g_object_unref (priv->publish);
755 g_object_unref (priv->stored);
758 if (priv->connection) {
759 g_object_unref (priv->connection);
763 g_object_unref (priv->factory);
766 g_hash_table_iter_init (&iter, priv->groups);
767 while (g_hash_table_iter_next (&iter, NULL, &channel)) {
768 g_signal_handlers_disconnect_by_func (channel,
769 tp_contact_list_group_invalidated_cb, list);
772 g_hash_table_destroy (priv->groups);
773 g_hash_table_destroy (priv->members);
774 g_hash_table_destroy (priv->pendings);
775 g_hash_table_destroy (priv->add_to_group);
777 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
781 store_create_channel_cb (TpConnection *conn,
783 GHashTable *properties,
786 GObject *weak_object)
788 EmpathyTpContactList *list = user_data;
789 EmpathyTpContactListPriv *priv = GET_PRIV (list);
792 DEBUG ("failed: %s\n", error->message);
796 priv->stored = tp_channel_new_from_properties (conn, path, properties, NULL);
800 conn_ready_cb (TpConnection *connection,
804 EmpathyTpContactList *list = data;
805 EmpathyTpContactListPriv *priv = GET_PRIV (list);
810 DEBUG ("failed: %s", error->message);
814 /* Try to request the 'stored' list. */
815 request = g_hash_table_new_full (g_str_hash, g_str_equal,
816 NULL, (GDestroyNotify) tp_g_value_slice_free);
818 /* org.freedesktop.Telepathy.Channel.ChannelType */
819 value = tp_g_value_slice_new_string (TP_IFACE_CHANNEL_TYPE_CONTACT_LIST);
820 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
822 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
823 value = tp_g_value_slice_new_uint (TP_HANDLE_TYPE_LIST);
824 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
826 /* org.freedesktop.Telepathy.Channel.TargetID */
827 value = tp_g_value_slice_new_string ("stored");
828 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetID", value);
830 tp_cli_connection_interface_requests_call_create_channel (priv->connection,
831 -1, request, store_create_channel_cb, list, NULL, G_OBJECT (list));
833 g_hash_table_unref (request);
836 g_object_unref (list);
840 tp_contact_list_constructed (GObject *list)
842 EmpathyTpContactListPriv *priv = GET_PRIV (list);
843 gchar *protocol_name = NULL;
844 const gchar *names[] = {NULL, NULL};
846 priv->factory = empathy_tp_contact_factory_dup_singleton (priv->connection);
848 /* call GetAliasFlags */
849 if (tp_proxy_has_interface_by_id (priv->connection,
850 TP_IFACE_QUARK_CONNECTION_INTERFACE_ALIASING)) {
851 tp_cli_connection_interface_aliasing_call_get_alias_flags (
854 tp_contact_list_get_alias_flags_cb,
859 /* lookup RequestableChannelClasses */
860 if (tp_proxy_has_interface_by_id (priv->connection,
861 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS)) {
862 tp_cli_dbus_properties_call_get (priv->connection,
864 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
865 "RequestableChannelClasses",
866 tp_contact_list_get_requestablechannelclasses_cb,
870 /* we just don't know... better mark the flag just in case */
871 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
874 names[0] = "publish";
875 tp_cli_connection_call_request_handles (priv->connection,
879 tp_contact_list_publish_request_handle_cb,
882 names[0] = "subscribe";
883 tp_cli_connection_call_request_handles (priv->connection,
887 tp_contact_list_subscribe_request_handle_cb,
892 tp_connection_call_when_ready (priv->connection, conn_ready_cb, list);
894 tp_cli_connection_call_list_channels (priv->connection, -1,
895 tp_contact_list_list_channels_cb,
899 tp_cli_connection_connect_to_new_channel (priv->connection,
900 tp_contact_list_new_channel_cb,
904 /* Check for protocols that does not support contact groups. We can
905 * put all contacts into a special group in that case.
906 * FIXME: Default group should be an information in the profile */
907 tp_connection_parse_object_path (priv->connection, &protocol_name, NULL);
908 if (!tp_strdiff (protocol_name, "local-xmpp")) {
909 priv->protocol_group = _("People nearby");
911 g_free (protocol_name);
915 tp_contact_list_get_property (GObject *object,
920 EmpathyTpContactListPriv *priv = GET_PRIV (object);
923 case PROP_CONNECTION:
924 g_value_set_object (value, priv->connection);
927 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
933 tp_contact_list_set_property (GObject *object,
938 EmpathyTpContactListPriv *priv = GET_PRIV (object);
941 case PROP_CONNECTION:
942 priv->connection = g_value_dup_object (value);
945 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
951 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
953 GObjectClass *object_class = G_OBJECT_CLASS (klass);
955 object_class->finalize = tp_contact_list_finalize;
956 object_class->constructed = tp_contact_list_constructed;
957 object_class->get_property = tp_contact_list_get_property;
958 object_class->set_property = tp_contact_list_set_property;
960 g_object_class_install_property (object_class,
962 g_param_spec_object ("connection",
964 "The connection associated with the contact list",
967 G_PARAM_CONSTRUCT_ONLY));
969 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
973 tp_contact_list_array_free (gpointer handles)
975 g_array_free (handles, TRUE);
979 empathy_tp_contact_list_init (EmpathyTpContactList *list)
981 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
982 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
986 /* Map group's name to group's TpChannel. The group name string is owned
987 * by the TpChannel object */
988 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
990 (GDestroyNotify) g_object_unref);
992 /* Map contact's handle to EmpathyContact object */
993 priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
995 (GDestroyNotify) g_object_unref);
997 /* Map contact's handle to EmpathyContact object */
998 priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1000 (GDestroyNotify) g_object_unref);
1002 /* Map group's name to GArray of handle */
1003 priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
1005 tp_contact_list_array_free);
1008 EmpathyTpContactList *
1009 empathy_tp_contact_list_new (TpConnection *connection)
1011 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
1012 "connection", connection,
1017 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
1019 EmpathyTpContactListPriv *priv;
1021 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
1023 priv = GET_PRIV (list);
1025 return priv->connection;
1029 tp_contact_list_add (EmpathyContactList *list,
1030 EmpathyContact *contact,
1031 const gchar *message)
1033 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1035 GArray handles = {(gchar *) &handle, 1};
1037 handle = empathy_contact_get_handle (contact);
1038 if (priv->subscribe) {
1039 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
1040 -1, &handles, message, NULL, NULL, NULL, NULL);
1042 if (priv->publish) {
1043 TpChannelGroupFlags flags = tp_channel_group_get_flags (priv->subscribe);
1044 if (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD ||
1045 g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
1046 tp_cli_channel_interface_group_call_add_members (priv->publish,
1047 -1, &handles, message, NULL, NULL, NULL, NULL);
1053 tp_contact_list_remove (EmpathyContactList *list,
1054 EmpathyContact *contact,
1055 const gchar *message)
1057 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1059 GArray handles = {(gchar *) &handle, 1};
1061 handle = empathy_contact_get_handle (contact);
1063 if (priv->stored != NULL) {
1064 tp_cli_channel_interface_group_call_remove_members (priv->stored,
1065 -1, &handles, message, NULL, NULL, NULL, NULL);
1066 /* Contact will be removed from 'publish' and 'subscribe' too */
1070 if (priv->subscribe) {
1071 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
1072 -1, &handles, message, NULL, NULL, NULL, NULL);
1074 if (priv->publish) {
1075 tp_cli_channel_interface_group_call_remove_members (priv->publish,
1076 -1, &handles, message, NULL, NULL, NULL, NULL);
1081 tp_contact_list_get_members (EmpathyContactList *list)
1083 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1086 ret = g_hash_table_get_values (priv->members);
1087 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1092 tp_contact_list_get_pendings (EmpathyContactList *list)
1094 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1097 ret = g_hash_table_get_values (priv->pendings);
1098 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1103 tp_contact_list_get_all_groups (EmpathyContactList *list)
1105 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1108 ret = g_hash_table_get_keys (priv->groups);
1109 for (l = ret; l; l = l->next) {
1110 l->data = g_strdup (l->data);
1113 if (priv->protocol_group) {
1114 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
1121 tp_contact_list_get_groups (EmpathyContactList *list,
1122 EmpathyContact *contact)
1124 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1126 GHashTableIter iter;
1127 gpointer group_name;
1131 handle = empathy_contact_get_handle (contact);
1132 g_hash_table_iter_init (&iter, priv->groups);
1133 while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
1134 const TpIntSet *members;
1136 members = tp_channel_group_get_members (channel);
1137 if (tp_intset_is_member (members, handle)) {
1138 ret = g_list_prepend (ret, g_strdup (group_name));
1142 if (priv->protocol_group) {
1143 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
1150 tp_contact_list_add_to_group (EmpathyContactList *list,
1151 EmpathyContact *contact,
1152 const gchar *group_name)
1157 handle = empathy_contact_get_handle (contact);
1158 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1159 g_array_append_val (handles, handle);
1160 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1161 group_name, handles);
1165 tp_contact_list_remove_from_group (EmpathyContactList *list,
1166 EmpathyContact *contact,
1167 const gchar *group_name)
1169 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1172 GArray handles = {(gchar *) &handle, 1};
1174 channel = g_hash_table_lookup (priv->groups, group_name);
1175 if (channel == NULL) {
1179 handle = empathy_contact_get_handle (contact);
1180 DEBUG ("remove contact %s (%d) from group %s",
1181 empathy_contact_get_id (contact), handle, group_name);
1183 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1184 &handles, NULL, NULL, NULL, NULL, NULL);
1188 tp_contact_list_rename_group (EmpathyContactList *list,
1189 const gchar *old_group_name,
1190 const gchar *new_group_name)
1192 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1194 const TpIntSet *members;
1197 channel = g_hash_table_lookup (priv->groups, old_group_name);
1198 if (channel == NULL) {
1202 DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1204 /* Remove all members and close the old channel */
1205 members = tp_channel_group_get_members (channel);
1206 handles = tp_intset_to_array (members);
1207 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1208 handles, NULL, NULL, NULL, NULL, NULL);
1209 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1211 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1212 new_group_name, handles);
1216 tp_contact_list_remove_group (EmpathyContactList *list,
1217 const gchar *group_name)
1219 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1221 const TpIntSet *members;
1224 channel = g_hash_table_lookup (priv->groups, group_name);
1225 if (channel == NULL) {
1229 DEBUG ("remove group %s", group_name);
1231 /* Remove all members and close the channel */
1232 members = tp_channel_group_get_members (channel);
1233 handles = tp_intset_to_array (members);
1234 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1235 handles, NULL, NULL, NULL, NULL, NULL);
1236 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1237 g_array_free (handles, TRUE);
1240 static EmpathyContactListFlags
1241 tp_contact_list_get_flags (EmpathyContactList *list)
1243 EmpathyTpContactListPriv *priv;
1244 EmpathyContactListFlags flags;
1246 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1248 priv = GET_PRIV (list);
1249 flags = priv->flags;
1251 if (priv->subscribe != NULL) {
1252 TpChannelGroupFlags group_flags;
1254 group_flags = tp_channel_group_get_flags (priv->subscribe);
1256 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1257 flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1260 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1261 flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1269 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1271 iface->add = tp_contact_list_add;
1272 iface->remove = tp_contact_list_remove;
1273 iface->get_members = tp_contact_list_get_members;
1274 iface->get_pendings = tp_contact_list_get_pendings;
1275 iface->get_all_groups = tp_contact_list_get_all_groups;
1276 iface->get_groups = tp_contact_list_get_groups;
1277 iface->add_to_group = tp_contact_list_add_to_group;
1278 iface->remove_from_group = tp_contact_list_remove_from_group;
1279 iface->rename_group = tp_contact_list_rename_group;
1280 iface->remove_group = tp_contact_list_remove_group;
1281 iface->get_flags = tp_contact_list_get_flags;
1285 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1287 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1288 GHashTableIter iter;
1291 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1293 /* Remove all contacts */
1294 g_hash_table_iter_init (&iter, priv->members);
1295 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1296 g_signal_emit_by_name (list, "members-changed", contact,
1300 g_hash_table_remove_all (priv->members);
1302 g_hash_table_iter_init (&iter, priv->pendings);
1303 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1304 g_signal_emit_by_name (list, "pendings-changed", contact,
1308 g_hash_table_remove_all (priv->pendings);