build: remove -enumtypes rules
[empathy-mirror.git] / libempathy / empathy-tp-contact-list.c
blob1cd2ba4caded1dfb337defedbce6afe008e946a8
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
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>
23 #include <config.h>
25 #include <string.h>
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 <extensions/extensions.h>
36 #include "empathy-tp-contact-list.h"
37 #include "empathy-tp-contact-factory.h"
38 #include "empathy-contact-list.h"
39 #include "empathy-utils.h"
41 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
42 #include "empathy-debug.h"
44 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactList)
45 typedef struct {
46 TpConnection *connection;
48 TpChannel *publish;
49 TpChannel *subscribe;
50 TpChannel *stored;
51 TpChannel *deny;
52 /* contact handle (TpHandle) => reffed (EmpathyContact *)
54 * Union of:
55 * - members of 'subscribe': we receive their presence
56 * - RP of 'subscribe': we asked to receive their presence
57 * - members of 'publish': we send them our presence
58 * - members of 'stored': they are in our roster
60 GHashTable *members;
61 /* contact handle (TpHandle) => reffed (EmpathyContact *)
63 * Contacts which are local-pending in the publish channel but are NOT in
64 * the members hash table: they asked to receive our presence and we don't
65 * receive theirs or asked to.
66 * That's basically the contacts which asked to add us to their contact
67 * list and we didn't answer it. */
68 GHashTable *pendings;
69 /* group name: borrowed (const gchar *) => reffed (TpChannel *) */
70 GHashTable *groups;
71 /* group name: owned (gchar *) => owned GArray of TpHandle */
72 GHashTable *add_to_group;
74 EmpathyContactListFlags flags;
75 } EmpathyTpContactListPriv;
77 typedef enum {
78 TP_CONTACT_LIST_TYPE_PUBLISH,
79 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
80 TP_CONTACT_LIST_TYPE_UNKNOWN
81 } TpContactListType;
83 static void tp_contact_list_iface_init (EmpathyContactListIface *iface);
85 enum {
86 PROP_0,
87 PROP_CONNECTION,
90 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
91 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
92 tp_contact_list_iface_init));
94 static void
95 tp_contact_list_forget_group (EmpathyTpContactList *list,
96 TpChannel *channel)
98 EmpathyTpContactListPriv *priv = GET_PRIV (list);
99 const TpIntSet *members;
100 TpIntSetIter iter;
101 const gchar *group_name;
103 group_name = tp_channel_get_identifier (channel);
105 /* Signal that all members are not in that group anymore */
106 members = tp_channel_group_get_members (channel);
107 tp_intset_iter_init (&iter, members);
108 while (tp_intset_iter_next (&iter)) {
109 EmpathyContact *contact;
111 contact = g_hash_table_lookup (priv->members,
112 GUINT_TO_POINTER (iter.element));
113 if (contact == NULL) {
114 continue;
117 DEBUG ("Contact %s (%d) removed from group %s",
118 empathy_contact_get_id (contact), iter.element,
119 group_name);
120 g_signal_emit_by_name (list, "groups-changed", contact,
121 group_name,
122 FALSE);
126 static void
127 tp_contact_list_group_invalidated_cb (TpChannel *channel,
128 guint domain,
129 gint code,
130 gchar *message,
131 EmpathyTpContactList *list)
133 EmpathyTpContactListPriv *priv = GET_PRIV (list);
134 const gchar *group_name;
136 group_name = tp_channel_get_identifier (channel);
137 DEBUG ("Group %s invalidated. Message: %s", group_name, message);
139 tp_contact_list_forget_group (list, channel);
141 g_hash_table_remove (priv->groups, group_name);
144 static void
145 contacts_added_to_group (EmpathyTpContactList *list,
146 TpChannel *channel,
147 GArray *added)
149 EmpathyTpContactListPriv *priv = GET_PRIV (list);
150 const gchar *group_name;
151 guint i;
153 group_name = tp_channel_get_identifier (channel);
155 for (i = 0; i < added->len; i++) {
156 EmpathyContact *contact;
157 TpHandle handle;
159 handle = g_array_index (added, TpHandle, i);
160 contact = g_hash_table_lookup (priv->members,
161 GUINT_TO_POINTER (handle));
162 if (contact == NULL) {
163 continue;
166 DEBUG ("Contact %s (%d) added to group %s",
167 empathy_contact_get_id (contact), handle, group_name);
168 g_signal_emit_by_name (list, "groups-changed", contact,
169 group_name,
170 TRUE);
174 static void
175 tp_contact_list_group_members_changed_cb (TpChannel *channel,
176 gchar *message,
177 GArray *added,
178 GArray *removed,
179 GArray *local_pending,
180 GArray *remote_pending,
181 guint actor,
182 guint reason,
183 EmpathyTpContactList *list)
185 EmpathyTpContactListPriv *priv = GET_PRIV (list);
186 const gchar *group_name;
187 guint i;
189 contacts_added_to_group (list, channel, added);
191 group_name = tp_channel_get_identifier (channel);
193 for (i = 0; i < removed->len; i++) {
194 EmpathyContact *contact;
195 TpHandle handle;
197 handle = g_array_index (removed, TpHandle, i);
198 contact = g_hash_table_lookup (priv->members,
199 GUINT_TO_POINTER (handle));
200 if (contact == NULL) {
201 continue;
204 DEBUG ("Contact %s (%d) removed from group %s",
205 empathy_contact_get_id (contact), handle, group_name);
207 g_signal_emit_by_name (list, "groups-changed", contact,
208 group_name,
209 FALSE);
213 static void
214 tp_contact_list_group_ready_cb (TpChannel *channel,
215 const GError *error,
216 gpointer list)
218 EmpathyTpContactListPriv *priv = GET_PRIV (list);
219 TpChannel *old_group;
220 const gchar *group_name;
221 const TpIntSet *members;
222 GArray *arr;
224 if (error) {
225 DEBUG ("Error: %s", error->message);
226 g_object_unref (channel);
227 return;
230 group_name = tp_channel_get_identifier (channel);
232 /* If there's already a group with this name in the table, we can't
233 * just let it be replaced. Replacing it causes it to be unreffed,
234 * which causes it to be invalidated (see
235 * <https://bugs.freedesktop.org/show_bug.cgi?id=22119>), which causes
236 * it to be removed from the hash table again, which causes it to be
237 * unreffed again.
239 old_group = g_hash_table_lookup (priv->groups, group_name);
241 if (old_group != NULL) {
242 DEBUG ("Discarding old group %s (%p)", group_name, old_group);
243 g_hash_table_steal (priv->groups, group_name);
244 tp_contact_list_forget_group (list, old_group);
245 g_object_unref (old_group);
248 /* Pass the reference on the TpChannel to priv->groups */
249 g_hash_table_insert (priv->groups, (gpointer) group_name, channel);
250 DEBUG ("Group %s added", group_name);
252 g_signal_connect (channel, "group-members-changed",
253 G_CALLBACK (tp_contact_list_group_members_changed_cb),
254 list);
256 g_signal_connect (channel, "invalidated",
257 G_CALLBACK (tp_contact_list_group_invalidated_cb),
258 list);
260 if (priv->add_to_group) {
261 GArray *handles;
263 handles = g_hash_table_lookup (priv->add_to_group, group_name);
264 if (handles) {
265 DEBUG ("Adding initial members to group %s", group_name);
266 tp_cli_channel_interface_group_call_add_members (channel,
267 -1, handles, NULL, NULL, NULL, NULL, NULL);
268 g_hash_table_remove (priv->add_to_group, group_name);
272 /* Get initial members of the group */
273 members = tp_channel_group_get_members (channel);
274 g_assert (members != NULL);
275 arr = tp_intset_to_array (members);
276 contacts_added_to_group (list, channel, arr);
277 g_array_free (arr, TRUE);
280 static void
281 tp_contact_list_group_add_channel (EmpathyTpContactList *list,
282 const gchar *object_path,
283 GHashTable *properties)
285 EmpathyTpContactListPriv *priv = GET_PRIV (list);
286 TpChannel *channel;
287 GError *error = NULL;
289 channel = tp_channel_new_from_properties (priv->connection,
290 object_path, properties, &error);
291 if (channel == NULL) {
292 DEBUG ("Failed to create group channel: %s", error->message);
293 g_error_free (error);
294 return;
297 /* Give the ref to the callback */
298 tp_channel_call_when_ready (channel,
299 tp_contact_list_group_ready_cb,
300 list);
303 static void
304 tp_contact_list_group_request_channel_cb (TpConnection *connection,
305 const gchar *object_path,
306 const GError *error,
307 gpointer user_data,
308 GObject *list)
310 /* The new channel will be handled in NewChannel cb. Here we only
311 * handle the error if RequestChannel failed */
312 if (error) {
313 DEBUG ("Error: %s", error->message);
314 return;
318 static void
319 tp_contact_list_group_request_handles_cb (TpConnection *connection,
320 const GArray *handles,
321 const GError *error,
322 gpointer user_data,
323 GObject *list)
325 TpHandle channel_handle;
327 if (error) {
328 DEBUG ("Error: %s", error->message);
329 return;
332 channel_handle = g_array_index (handles, TpHandle, 0);
333 tp_cli_connection_call_request_channel (connection, -1,
334 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
335 TP_HANDLE_TYPE_GROUP,
336 channel_handle,
337 TRUE,
338 tp_contact_list_group_request_channel_cb,
339 NULL, NULL,
340 list);
343 /* This function takes ownership of handles array */
344 static void
345 tp_contact_list_group_add (EmpathyTpContactList *list,
346 const gchar *group_name,
347 GArray *handles)
349 EmpathyTpContactListPriv *priv = GET_PRIV (list);
350 TpChannel *channel;
351 const gchar *names[] = {group_name, NULL};
353 /* Search the channel for that group name */
354 channel = g_hash_table_lookup (priv->groups, group_name);
355 if (channel) {
356 tp_cli_channel_interface_group_call_add_members (channel, -1,
357 handles, NULL, NULL, NULL, NULL, NULL);
358 g_array_free (handles, TRUE);
359 return;
362 /* That group does not exist yet, we have to:
363 * 1) Request an handle for the group name
364 * 2) Request a channel
365 * 3) When NewChannel is emitted, add handles in members
367 g_hash_table_insert (priv->add_to_group,
368 g_strdup (group_name),
369 handles);
370 tp_cli_connection_call_request_handles (priv->connection, -1,
371 TP_HANDLE_TYPE_GROUP, names,
372 tp_contact_list_group_request_handles_cb,
373 NULL, NULL,
374 G_OBJECT (list));
377 static void
378 got_added_members_cb (TpConnection *connection,
379 guint n_contacts,
380 EmpathyContact * const * contacts,
381 guint n_failed,
382 const TpHandle *failed,
383 const GError *error,
384 gpointer user_data,
385 GObject *list)
387 EmpathyTpContactListPriv *priv = GET_PRIV (list);
388 guint i;
390 if (error) {
391 DEBUG ("Error: %s", error->message);
392 return;
395 for (i = 0; i < n_contacts; i++) {
396 EmpathyContact *contact = contacts[i];
397 TpHandle handle = empathy_contact_get_handle (contact);
399 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
400 continue;
402 /* Add to the list and emit signal */
403 g_hash_table_insert (priv->members, GUINT_TO_POINTER (handle),
404 g_object_ref (contact));
405 g_signal_emit_by_name (list, "members-changed", contact,
406 0, 0, NULL, TRUE);
410 static void
411 add_to_members (EmpathyTpContactList *list,
412 GArray *handles)
414 EmpathyTpContactListPriv *priv = GET_PRIV (list);
415 GArray *request;
416 guint i;
418 if (handles->len == 0)
419 return;
421 request = g_array_new (FALSE, FALSE, sizeof (TpHandle));
423 for (i = 0; i < handles->len; i++) {
424 TpHandle handle = g_array_index (handles, TpHandle, i);
426 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
427 continue;
429 g_array_append_val (request, handle);
432 if (request->len > 0) {
433 empathy_tp_contact_factory_get_from_handles (priv->connection,
434 request->len, (TpHandle *) request->data,
435 got_added_members_cb, NULL, NULL, G_OBJECT (list));
438 g_array_free (request, TRUE);
441 static void
442 tp_contact_list_got_local_pending_cb (TpConnection *connection,
443 guint n_contacts,
444 EmpathyContact * const * contacts,
445 guint n_failed,
446 const TpHandle *failed,
447 const GError *error,
448 gpointer user_data,
449 GObject *list)
451 EmpathyTpContactListPriv *priv = GET_PRIV (list);
452 guint i;
454 if (error) {
455 DEBUG ("Error: %s", error->message);
456 return;
459 for (i = 0; i < n_contacts; i++) {
460 EmpathyContact *contact = contacts[i];
461 TpHandle handle;
462 const gchar *message;
463 TpChannelGroupChangeReason reason;
464 const TpIntSet *members, *remote_pending;
466 handle = empathy_contact_get_handle (contact);
467 members = tp_channel_group_get_members (priv->subscribe);
468 remote_pending = tp_channel_group_get_remote_pending (priv->subscribe);
470 if (tp_intset_is_member (members, handle) ||
471 tp_intset_is_member (remote_pending, handle)) {
472 GArray handles = {(gchar *) &handle, 1};
474 /* This contact is already subscribed, or user requested
475 * to subscribe, auto accept. */
476 tp_cli_channel_interface_group_call_add_members (priv->publish,
477 -1, &handles, NULL, NULL, NULL, NULL, NULL);
479 else if (tp_channel_group_get_local_pending_info (priv->publish,
480 handle,
481 NULL,
482 &reason,
483 &message)) {
484 /* Add contact to pendings */
485 g_hash_table_insert (priv->pendings, GUINT_TO_POINTER (handle),
486 g_object_ref (contact));
487 g_signal_emit_by_name (list, "pendings-changed", contact,
488 contact, reason, message, TRUE);
493 static void
494 tp_contact_list_remove_handle (EmpathyTpContactList *list,
495 GHashTable *table,
496 TpHandle handle)
498 EmpathyTpContactListPriv *priv = GET_PRIV (list);
499 EmpathyContact *contact;
500 const gchar *sig;
502 if (table == priv->pendings)
503 sig = "pendings-changed";
504 else if (table == priv->members)
505 sig = "members-changed";
506 else
507 return;
509 contact = g_hash_table_lookup (table, GUINT_TO_POINTER (handle));
510 if (contact) {
511 g_object_ref (contact);
512 g_hash_table_remove (table, GUINT_TO_POINTER (handle));
513 g_signal_emit_by_name (list, sig, contact, 0, 0, NULL,
514 FALSE);
515 g_object_unref (contact);
519 static void
520 remove_from_member_if_needed (EmpathyTpContactList *list,
521 TpHandle handle)
523 /* remove contact from members if it's not in publish and subscribe */
524 EmpathyTpContactListPriv *priv = GET_PRIV (list);
525 const TpIntSet *members;
527 members = tp_channel_group_get_members (priv->subscribe);
528 if (tp_intset_is_member (members, handle))
529 return;
531 members = tp_channel_group_get_remote_pending (priv->subscribe);
532 if (tp_intset_is_member (members, handle))
533 return;
535 members = tp_channel_group_get_members (priv->publish);
536 if (tp_intset_is_member (members, handle))
537 return;
539 tp_contact_list_remove_handle (list, priv->members, handle);
542 static void
543 tp_contact_list_publish_group_members_changed_cb (TpChannel *channel,
544 gchar *message,
545 GArray *added,
546 GArray *removed,
547 GArray *local_pending,
548 GArray *remote_pending,
549 TpHandle actor,
550 TpChannelGroupChangeReason reason,
551 EmpathyTpContactList *list)
553 EmpathyTpContactListPriv *priv = GET_PRIV (list);
554 guint i;
556 /* We now send our presence to those contacts, remove them from pendings */
557 add_to_members (list, added);
558 for (i = 0; i < added->len; i++) {
559 tp_contact_list_remove_handle (list, priv->pendings,
560 g_array_index (added, TpHandle, i));
563 /* We refuse to send our presence to those contacts, remove from pendings */
564 for (i = 0; i < removed->len; i++) {
565 TpHandle handle = g_array_index (removed, TpHandle, i);
567 tp_contact_list_remove_handle (list, priv->pendings, handle);
568 remove_from_member_if_needed (list, handle);
571 /* Those contacts want our presence, auto accept those that are already
572 * member, otherwise add in pendings. */
573 if (local_pending->len > 0) {
574 empathy_tp_contact_factory_get_from_handles (priv->connection,
575 local_pending->len, (TpHandle *) local_pending->data,
576 tp_contact_list_got_local_pending_cb, NULL, NULL,
577 G_OBJECT (list));
581 static void
582 tp_contact_list_get_alias_flags_cb (TpConnection *connection,
583 guint flags,
584 const GError *error,
585 gpointer user_data,
586 GObject *list)
588 EmpathyTpContactListPriv *priv = GET_PRIV (list);
590 if (error) {
591 DEBUG ("Error: %s", error->message);
592 return;
595 if (flags & TP_CONNECTION_ALIAS_FLAG_USER_SET) {
596 priv->flags |= EMPATHY_CONTACT_LIST_CAN_ALIAS;
600 static void
601 tp_contact_list_get_requestablechannelclasses_cb (TpProxy *connection,
602 const GValue *value,
603 const GError *error,
604 gpointer user_data,
605 GObject *list)
607 EmpathyTpContactListPriv *priv = GET_PRIV (list);
608 GPtrArray *classes;
609 guint i;
611 if (error) {
612 DEBUG ("Error: %s", error->message);
613 return;
616 classes = g_value_get_boxed (value);
617 for (i = 0; i < classes->len; i++) {
618 GValueArray *class = g_ptr_array_index (classes, i);
619 GHashTable *props;
620 const char *channel_type;
621 guint handle_type;
623 props = g_value_get_boxed (g_value_array_get_nth (class, 0));
625 channel_type = tp_asv_get_string (props,
626 TP_IFACE_CHANNEL ".ChannelType");
627 handle_type = tp_asv_get_uint32 (props,
628 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
630 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) &&
631 handle_type == TP_HANDLE_TYPE_GROUP) {
632 DEBUG ("Got channel class for a contact group");
633 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
634 break;
639 static void
640 tp_contact_list_subscribe_group_members_changed_cb (TpChannel *channel,
641 gchar *message,
642 GArray *added,
643 GArray *removed,
644 GArray *local_pending,
645 GArray *remote_pending,
646 guint actor,
647 guint reason,
648 EmpathyTpContactList *list)
650 EmpathyTpContactListPriv *priv = GET_PRIV (list);
651 guint i;
652 GArray *accept;
654 /* We now get the presence of those contacts, add them to members */
655 add_to_members (list, added);
657 /* Those contacts refuse to send us their presence, remove from members. */
658 for (i = 0; i < removed->len; i++) {
659 remove_from_member_if_needed (list, g_array_index (removed, TpHandle, i));
662 /* We want those contacts in our contact list but we don't get their
663 * presence yet. Add to members anyway. */
664 add_to_members (list, remote_pending);
666 /* Implicitly accept pending request of contacts which are now members. */
667 if (priv->publish == NULL)
668 return;
670 accept = g_array_new (FALSE, FALSE, sizeof (TpHandle));
671 for (i = 0; i < added->len; i++) {
672 TpHandle handle = g_array_index (added, TpHandle, i);
674 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle)) != NULL)
675 g_array_append_val (accept, handle);
678 for (i = 0; i < remote_pending->len; i++) {
679 TpHandle handle = g_array_index (added, TpHandle, i);
681 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle)) != NULL)
682 g_array_append_val (accept, handle);
685 tp_cli_channel_interface_group_call_add_members (priv->publish,
686 -1, accept, NULL, NULL, NULL, NULL, NULL);
688 g_array_free (accept, TRUE);
691 static void
692 tp_contact_list_store_group_members_changed_cb (TpChannel *channel,
693 gchar *message,
694 GArray *added,
695 GArray *removed,
696 GArray *local_pending,
697 GArray *remote_pending,
698 guint actor,
699 guint reason,
700 EmpathyTpContactList *list)
702 guint i;
704 add_to_members (list, added);
706 for (i = 0; i < removed->len; i++) {
707 remove_from_member_if_needed (list, g_array_index (removed, TpHandle, i));
711 static void
712 tp_contact_list_finalize (GObject *object)
714 EmpathyTpContactListPriv *priv;
715 EmpathyTpContactList *list;
716 GHashTableIter iter;
717 gpointer channel;
719 list = EMPATHY_TP_CONTACT_LIST (object);
720 priv = GET_PRIV (list);
722 DEBUG ("finalize: %p", object);
724 if (priv->subscribe) {
725 g_object_unref (priv->subscribe);
727 if (priv->publish) {
728 g_object_unref (priv->publish);
730 if (priv->stored) {
731 g_object_unref (priv->stored);
734 if (priv->deny) {
735 g_object_unref (priv->deny);
738 if (priv->connection) {
739 g_object_unref (priv->connection);
742 g_hash_table_iter_init (&iter, priv->groups);
743 while (g_hash_table_iter_next (&iter, NULL, &channel)) {
744 g_signal_handlers_disconnect_by_func (channel,
745 tp_contact_list_group_invalidated_cb, list);
748 g_hash_table_destroy (priv->groups);
749 g_hash_table_destroy (priv->members);
750 g_hash_table_destroy (priv->pendings);
751 g_hash_table_destroy (priv->add_to_group);
753 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
756 static void
757 got_list_channel (EmpathyTpContactList *list,
758 TpChannel *channel)
760 EmpathyTpContactListPriv *priv = GET_PRIV (list);
761 const gchar *id;
763 /* We requested that channel by providing TargetID property, so it's
764 * guaranteed that tp_channel_get_identifier will return it. */
765 id = tp_channel_get_identifier (channel);
767 /* TpChannel emits initial set of members just before being ready */
768 if (!tp_strdiff (id, "stored")) {
769 if (priv->stored != NULL)
770 return;
771 priv->stored = g_object_ref (channel);
772 g_signal_connect (priv->stored, "group-members-changed",
773 G_CALLBACK (tp_contact_list_store_group_members_changed_cb),
774 list);
775 } else if (!tp_strdiff (id, "publish")) {
776 if (priv->publish != NULL)
777 return;
778 priv->publish = g_object_ref (channel);
779 g_signal_connect (priv->publish, "group-members-changed",
780 G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
781 list);
782 } else if (!tp_strdiff (id, "subscribe")) {
783 if (priv->subscribe != NULL)
784 return;
785 priv->subscribe = g_object_ref (channel);
786 g_signal_connect (priv->subscribe, "group-members-changed",
787 G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
788 list);
789 } else if (!tp_strdiff (id, "deny")) {
790 if (priv->deny != NULL)
791 return;
792 DEBUG ("Got 'deny' channel");
793 priv->deny = g_object_ref (channel);
797 static void
798 list_ensure_channel_cb (TpConnection *conn,
799 gboolean yours,
800 const gchar *path,
801 GHashTable *properties,
802 const GError *error,
803 gpointer user_data,
804 GObject *weak_object)
806 EmpathyTpContactList *list = user_data;
807 TpChannel *channel;
809 if (error != NULL) {
810 DEBUG ("failed: %s\n", error->message);
811 return;
814 channel = tp_channel_new_from_properties (conn, path, properties, NULL);
815 got_list_channel (list, channel);
816 g_object_unref (channel);
819 static void
820 list_get_contact_blocking_capabilities_cb (TpProxy *conn,
821 const GValue *value,
822 const GError *in_error,
823 gpointer user_data,
824 GObject *weak_object)
826 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
827 EmpathyTpContactListPriv *priv = GET_PRIV (list);
828 TpContactBlockingCapabilities caps;
830 if (in_error != NULL) {
831 DEBUG ("Get(Blocking, ContactBlockingCapabilities) on %s failed: %s",
832 tp_proxy_get_object_path (conn),
833 in_error->message);
834 return;
837 if (!G_VALUE_HOLDS_UINT (value)) {
838 DEBUG ("Get(Blocking, ContactBlockingCapabilities) on %s returned a %s, not a uint",
839 tp_proxy_get_object_path (conn),
840 G_VALUE_TYPE_NAME (value));
841 return;
844 caps = g_value_get_uint (value);
846 if (caps & TP_CONTACT_BLOCKING_CAPABILITY_CAN_REPORT_ABUSIVE) {
847 DEBUG ("Connection can report abusive contacts");
848 priv->flags |= EMPATHY_CONTACT_LIST_CAN_REPORT_ABUSIVE;
852 static void
853 iterate_on_channels (EmpathyTpContactList *list,
854 const GPtrArray *channels)
856 guint i;
858 for (i = 0; i < channels->len ; i++) {
859 GValueArray *arr = g_ptr_array_index (channels, i);
860 const gchar *path;
861 GHashTable *properties;
862 TpHandleType handle_type;
864 path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
865 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
867 if (tp_strdiff (tp_asv_get_string (properties,
868 TP_IFACE_CHANNEL ".ChannelType"),
869 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST))
870 continue;
872 if (tp_asv_get_string (properties, TP_IFACE_CHANNEL ".TargetID") == NULL)
873 continue;
875 handle_type = tp_asv_get_uint32 (properties,
876 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
878 if (handle_type != TP_HANDLE_TYPE_GROUP)
879 continue;
881 tp_contact_list_group_add_channel (list, path, properties);
885 static void
886 new_channels_cb (TpConnection *conn,
887 const GPtrArray *channels,
888 gpointer user_data,
889 GObject *weak_object)
891 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
893 iterate_on_channels (list, channels);
896 static void
897 got_channels_cb (TpProxy *conn,
898 const GValue *out,
899 const GError *error,
900 gpointer user_data,
901 GObject *weak_object)
903 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
904 const GPtrArray *channels;
906 if (error != NULL) {
907 DEBUG ("Get Channels property failed: %s", error->message);
908 return;
911 channels = g_value_get_boxed (out);
912 iterate_on_channels (list, channels);
915 static void
916 conn_ready_cb (TpConnection *connection,
917 const GError *error,
918 gpointer data)
920 EmpathyTpContactList *list = data;
921 EmpathyTpContactListPriv *priv = GET_PRIV (list);
922 GHashTable *request;
924 if (error != NULL) {
925 DEBUG ("failed: %s", error->message);
926 goto out;
929 /* Look for existing group channels */
930 tp_cli_dbus_properties_call_get (connection, -1,
931 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels", got_channels_cb,
932 NULL, NULL, G_OBJECT (list));
934 request = tp_asv_new (
935 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
936 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_LIST,
937 NULL);
939 /* Watch the NewChannels signal so if ensuring list channels fails (for
940 * example because the server is slow and the D-Bus call timeouts before CM
941 * fetches the roster), we have a chance to get them later. */
942 tp_cli_connection_interface_requests_connect_to_new_channels (
943 priv->connection, new_channels_cb, NULL, NULL, G_OBJECT (list), NULL);
945 /* Request the 'stored' list. */
946 tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "stored");
947 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
948 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
950 /* Request the 'publish' list. */
951 tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "publish");
952 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
953 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
955 /* Request the 'subscribe' list. */
956 tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "subscribe");
957 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
958 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
960 /* Request the 'deny' list */
961 tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "deny");
962 tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
963 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
965 g_hash_table_unref (request);
967 /* Find out if we support reporting abusive contacts --
968 * this is done via the new Conn.I.ContactBlocking interface */
969 if (tp_proxy_has_interface_by_id (priv->connection,
970 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING)) {
971 DEBUG ("Have Conn.I.ContactBlocking");
973 tp_cli_dbus_properties_call_get (priv->connection, -1,
974 TP_IFACE_CONNECTION_INTERFACE_CONTACT_BLOCKING,
975 "ContactBlockingCapabilities",
976 list_get_contact_blocking_capabilities_cb,
977 NULL, NULL, G_OBJECT (list));
979 out:
980 g_object_unref (list);
983 static void
984 tp_contact_list_constructed (GObject *list)
986 EmpathyTpContactListPriv *priv = GET_PRIV (list);
988 /* call GetAliasFlags */
989 if (tp_proxy_has_interface_by_id (priv->connection,
990 TP_IFACE_QUARK_CONNECTION_INTERFACE_ALIASING)) {
991 tp_cli_connection_interface_aliasing_call_get_alias_flags (
992 priv->connection,
994 tp_contact_list_get_alias_flags_cb,
995 NULL, NULL,
996 G_OBJECT (list));
999 /* lookup RequestableChannelClasses */
1000 if (tp_proxy_has_interface_by_id (priv->connection,
1001 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS)) {
1002 tp_cli_dbus_properties_call_get (priv->connection,
1004 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
1005 "RequestableChannelClasses",
1006 tp_contact_list_get_requestablechannelclasses_cb,
1007 NULL, NULL,
1008 G_OBJECT (list));
1009 } else {
1010 /* we just don't know... better mark the flag just in case */
1011 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
1014 tp_connection_call_when_ready (priv->connection, conn_ready_cb,
1015 g_object_ref (list));
1018 static void
1019 tp_contact_list_get_property (GObject *object,
1020 guint param_id,
1021 GValue *value,
1022 GParamSpec *pspec)
1024 EmpathyTpContactListPriv *priv = GET_PRIV (object);
1026 switch (param_id) {
1027 case PROP_CONNECTION:
1028 g_value_set_object (value, priv->connection);
1029 break;
1030 default:
1031 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1032 break;
1036 static void
1037 tp_contact_list_set_property (GObject *object,
1038 guint param_id,
1039 const GValue *value,
1040 GParamSpec *pspec)
1042 EmpathyTpContactListPriv *priv = GET_PRIV (object);
1044 switch (param_id) {
1045 case PROP_CONNECTION:
1046 priv->connection = g_value_dup_object (value);
1047 break;
1048 default:
1049 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1050 break;
1054 static void
1055 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
1057 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1059 object_class->finalize = tp_contact_list_finalize;
1060 object_class->constructed = tp_contact_list_constructed;
1061 object_class->get_property = tp_contact_list_get_property;
1062 object_class->set_property = tp_contact_list_set_property;
1064 g_object_class_install_property (object_class,
1065 PROP_CONNECTION,
1066 g_param_spec_object ("connection",
1067 "The Connection",
1068 "The connection associated with the contact list",
1069 TP_TYPE_CONNECTION,
1070 G_PARAM_READWRITE |
1071 G_PARAM_CONSTRUCT_ONLY));
1073 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
1076 static void
1077 tp_contact_list_array_free (gpointer handles)
1079 g_array_free (handles, TRUE);
1082 static void
1083 empathy_tp_contact_list_init (EmpathyTpContactList *list)
1085 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
1086 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
1088 list->priv = priv;
1090 /* Map group's name to group's TpChannel. The group name string is owned
1091 * by the TpChannel object */
1092 priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1093 NULL,
1094 (GDestroyNotify) g_object_unref);
1096 /* Map contact's handle to EmpathyContact object */
1097 priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1098 NULL,
1099 (GDestroyNotify) g_object_unref);
1101 /* Map contact's handle to EmpathyContact object */
1102 priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1103 NULL,
1104 (GDestroyNotify) g_object_unref);
1106 /* Map group's name to GArray of handle */
1107 priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
1108 g_free,
1109 tp_contact_list_array_free);
1112 EmpathyTpContactList *
1113 empathy_tp_contact_list_new (TpConnection *connection)
1115 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
1116 "connection", connection,
1117 NULL);
1120 TpConnection *
1121 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
1123 EmpathyTpContactListPriv *priv;
1125 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
1127 priv = GET_PRIV (list);
1129 return priv->connection;
1132 static void
1133 tp_contact_list_add (EmpathyContactList *list,
1134 EmpathyContact *contact,
1135 const gchar *message)
1137 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1138 TpHandle handle;
1139 GArray handles = {(gchar *) &handle, 1};
1141 handle = empathy_contact_get_handle (contact);
1143 if (priv->subscribe) {
1144 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
1145 -1, &handles, message, NULL, NULL, NULL, NULL);
1148 if (priv->publish) {
1149 TpChannelGroupFlags flags = tp_channel_group_get_flags (priv->subscribe);
1150 if (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD ||
1151 g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
1152 tp_cli_channel_interface_group_call_add_members (priv->publish,
1153 -1, &handles, message, NULL, NULL, NULL, NULL);
1157 if (priv->deny) {
1158 tp_cli_channel_interface_group_call_remove_members (
1159 priv->deny, -1, &handles, message,
1160 NULL, NULL, NULL, NULL);
1164 static void
1165 tp_contact_list_remove (EmpathyContactList *list,
1166 EmpathyContact *contact,
1167 const gchar *message)
1169 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1170 TpHandle handle;
1171 GArray handles = {(gchar *) &handle, 1};
1173 handle = empathy_contact_get_handle (contact);
1175 /* FIXME: this is racy if tp_contact_list_remove is called before the
1176 * 'stored' list has been retrieved. */
1177 if (priv->stored != NULL) {
1178 tp_cli_channel_interface_group_call_remove_members (priv->stored,
1179 -1, &handles, message, NULL, NULL, NULL, NULL);
1182 if (priv->subscribe) {
1183 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
1184 -1, &handles, message, NULL, NULL, NULL, NULL);
1186 if (priv->publish) {
1187 tp_cli_channel_interface_group_call_remove_members (priv->publish,
1188 -1, &handles, message, NULL, NULL, NULL, NULL);
1192 static GList *
1193 tp_contact_list_get_members (EmpathyContactList *list)
1195 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1196 GList *ret;
1198 ret = g_hash_table_get_values (priv->members);
1199 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1200 return ret;
1203 static GList *
1204 tp_contact_list_get_pendings (EmpathyContactList *list)
1206 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1207 GList *ret;
1209 ret = g_hash_table_get_values (priv->pendings);
1210 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1211 return ret;
1214 static GList *
1215 tp_contact_list_get_all_groups (EmpathyContactList *list)
1217 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1218 GList *ret, *l;
1220 ret = g_hash_table_get_keys (priv->groups);
1221 for (l = ret; l; l = l->next) {
1222 l->data = g_strdup (l->data);
1225 return ret;
1228 static GList *
1229 tp_contact_list_get_groups (EmpathyContactList *list,
1230 EmpathyContact *contact)
1232 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1233 GList *ret = NULL;
1234 GHashTableIter iter;
1235 gpointer group_name;
1236 gpointer channel;
1237 TpHandle handle;
1239 handle = empathy_contact_get_handle (contact);
1240 g_hash_table_iter_init (&iter, priv->groups);
1241 while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
1242 const TpIntSet *members;
1244 members = tp_channel_group_get_members (channel);
1245 if (tp_intset_is_member (members, handle)) {
1246 ret = g_list_prepend (ret, g_strdup (group_name));
1250 return ret;
1253 static void
1254 tp_contact_list_add_to_group (EmpathyContactList *list,
1255 EmpathyContact *contact,
1256 const gchar *group_name)
1258 TpHandle handle;
1259 GArray *handles;
1261 handle = empathy_contact_get_handle (contact);
1262 handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1263 g_array_append_val (handles, handle);
1264 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1265 group_name, handles);
1268 static void
1269 tp_contact_list_remove_from_group (EmpathyContactList *list,
1270 EmpathyContact *contact,
1271 const gchar *group_name)
1273 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1274 TpChannel *channel;
1275 TpHandle handle;
1276 GArray handles = {(gchar *) &handle, 1};
1278 channel = g_hash_table_lookup (priv->groups, group_name);
1279 if (channel == NULL) {
1280 return;
1283 handle = empathy_contact_get_handle (contact);
1284 DEBUG ("remove contact %s (%d) from group %s",
1285 empathy_contact_get_id (contact), handle, group_name);
1287 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1288 &handles, NULL, NULL, NULL, NULL, NULL);
1291 static void
1292 tp_contact_list_rename_group (EmpathyContactList *list,
1293 const gchar *old_group_name,
1294 const gchar *new_group_name)
1296 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1297 TpChannel *channel;
1298 const TpIntSet *members;
1299 GArray *handles;
1301 channel = g_hash_table_lookup (priv->groups, old_group_name);
1302 if (channel == NULL) {
1303 return;
1306 DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1308 /* Remove all members and close the old channel */
1309 members = tp_channel_group_get_members (channel);
1310 handles = tp_intset_to_array (members);
1311 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1312 handles, NULL, NULL, NULL, NULL, NULL);
1313 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1315 tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1316 new_group_name, handles);
1319 static void
1320 tp_contact_list_remove_group (EmpathyContactList *list,
1321 const gchar *group_name)
1323 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1324 TpChannel *channel;
1325 const TpIntSet *members;
1326 GArray *handles;
1328 channel = g_hash_table_lookup (priv->groups, group_name);
1329 if (channel == NULL) {
1330 return;
1333 DEBUG ("remove group %s", group_name);
1335 /* Remove all members and close the channel */
1336 members = tp_channel_group_get_members (channel);
1337 handles = tp_intset_to_array (members);
1338 tp_cli_channel_interface_group_call_remove_members (channel, -1,
1339 handles, NULL, NULL, NULL, NULL, NULL);
1340 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1341 g_array_free (handles, TRUE);
1344 static EmpathyContactListFlags
1345 tp_contact_list_get_flags (EmpathyContactList *list)
1347 EmpathyTpContactListPriv *priv;
1348 EmpathyContactListFlags flags;
1350 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1352 priv = GET_PRIV (list);
1353 flags = priv->flags;
1355 if (priv->subscribe != NULL) {
1356 TpChannelGroupFlags group_flags;
1358 group_flags = tp_channel_group_get_flags (priv->subscribe);
1360 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1361 flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1364 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1365 flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1368 if (group_flags & TP_CHANNEL_GROUP_FLAG_MESSAGE_ADD) {
1369 flags |= EMPATHY_CONTACT_LIST_MESSAGE_ADD;
1373 if (priv->deny != NULL)
1374 flags |= EMPATHY_CONTACT_LIST_CAN_BLOCK;
1376 return flags;
1379 static void
1380 tp_contact_list_set_blocked (EmpathyContactList *list,
1381 EmpathyContact *contact,
1382 gboolean blocked,
1383 gboolean abusive)
1385 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1386 TpHandle handle = empathy_contact_get_handle (contact);
1387 GArray handles = { (char *) &handle, 1 };
1389 g_return_if_fail (TP_IS_CHANNEL (priv->deny));
1391 if (blocked && abusive) {
1392 /* we have to do this via the new interface */
1393 g_return_if_fail (priv->flags &
1394 EMPATHY_CONTACT_LIST_CAN_REPORT_ABUSIVE);
1396 tp_cli_connection_interface_contact_blocking_call_block_contacts (
1397 priv->connection, -1,
1398 &handles, TRUE, NULL, NULL, NULL, NULL);
1399 } else if (blocked) {
1400 tp_cli_channel_interface_group_call_add_members (
1401 priv->deny, -1,
1402 &handles, NULL, NULL, NULL, NULL, NULL);
1403 } else {
1404 tp_cli_channel_interface_group_call_remove_members (
1405 priv->deny, -1,
1406 &handles, NULL, NULL, NULL, NULL, NULL);
1410 static gboolean
1411 tp_contact_list_get_blocked (EmpathyContactList *list,
1412 EmpathyContact *contact)
1414 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1416 g_return_val_if_fail (TP_IS_CHANNEL (priv->deny), FALSE);
1418 return tp_intset_is_member (tp_channel_group_get_members (priv->deny),
1419 empathy_contact_get_handle (contact));
1422 static void
1423 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1425 iface->add = tp_contact_list_add;
1426 iface->remove = tp_contact_list_remove;
1427 iface->get_members = tp_contact_list_get_members;
1428 iface->get_pendings = tp_contact_list_get_pendings;
1429 iface->get_all_groups = tp_contact_list_get_all_groups;
1430 iface->get_groups = tp_contact_list_get_groups;
1431 iface->add_to_group = tp_contact_list_add_to_group;
1432 iface->remove_from_group = tp_contact_list_remove_from_group;
1433 iface->rename_group = tp_contact_list_rename_group;
1434 iface->remove_group = tp_contact_list_remove_group;
1435 iface->get_flags = tp_contact_list_get_flags;
1436 iface->set_blocked = tp_contact_list_set_blocked;
1437 iface->get_blocked = tp_contact_list_get_blocked;
1440 void
1441 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1443 EmpathyTpContactListPriv *priv = GET_PRIV (list);
1444 GHashTableIter iter;
1445 gpointer contact;
1447 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1449 /* Remove all contacts */
1450 g_hash_table_iter_init (&iter, priv->members);
1451 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1452 g_signal_emit_by_name (list, "members-changed", contact,
1453 NULL, 0, NULL,
1454 FALSE);
1456 g_hash_table_remove_all (priv->members);
1458 g_hash_table_iter_init (&iter, priv->pendings);
1459 while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1460 g_signal_emit_by_name (list, "pendings-changed", contact,
1461 NULL, 0, NULL,
1462 FALSE);
1464 g_hash_table_remove_all (priv->pendings);