]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-contact-list.c
1cd2ba4caded1dfb337defedbce6afe008e946a8
[empathy.git] / libempathy / empathy-tp-contact-list.c
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.
5  *
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.
10  *
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.
15  *
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
19  *
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26 #include <glib/gi18n-lib.h>
27
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>
33
34 #include <extensions/extensions.h>
35
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"
40
41 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
42 #include "empathy-debug.h"
43
44 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactList)
45 typedef struct {
46         TpConnection   *connection;
47
48         TpChannel      *publish;
49         TpChannel      *subscribe;
50         TpChannel      *stored;
51         TpChannel      *deny;
52         /* contact handle (TpHandle) => reffed (EmpathyContact *)
53          *
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
59          */
60         GHashTable     *members;
61         /* contact handle (TpHandle) => reffed (EmpathyContact *)
62          *
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;
73
74         EmpathyContactListFlags flags;
75 } EmpathyTpContactListPriv;
76
77 typedef enum {
78         TP_CONTACT_LIST_TYPE_PUBLISH,
79         TP_CONTACT_LIST_TYPE_SUBSCRIBE,
80         TP_CONTACT_LIST_TYPE_UNKNOWN
81 } TpContactListType;
82
83 static void tp_contact_list_iface_init         (EmpathyContactListIface   *iface);
84
85 enum {
86         PROP_0,
87         PROP_CONNECTION,
88 };
89
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));
93
94 static void
95 tp_contact_list_forget_group (EmpathyTpContactList *list,
96                               TpChannel *channel)
97 {
98         EmpathyTpContactListPriv *priv = GET_PRIV (list);
99         const TpIntSet *members;
100         TpIntSetIter iter;
101         const gchar *group_name;
102
103         group_name = tp_channel_get_identifier (channel);
104
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;
110
111                 contact = g_hash_table_lookup (priv->members,
112                                                GUINT_TO_POINTER (iter.element));
113                 if (contact == NULL) {
114                         continue;
115                 }
116
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);
123         }
124 }
125
126 static void
127 tp_contact_list_group_invalidated_cb (TpChannel *channel,
128                                       guint      domain,
129                                       gint       code,
130                                       gchar     *message,
131                                       EmpathyTpContactList *list)
132 {
133         EmpathyTpContactListPriv *priv = GET_PRIV (list);
134         const gchar *group_name;
135
136         group_name = tp_channel_get_identifier (channel);
137         DEBUG ("Group %s invalidated. Message: %s", group_name, message);
138
139         tp_contact_list_forget_group (list, channel);
140
141         g_hash_table_remove (priv->groups, group_name);
142 }
143
144 static void
145 contacts_added_to_group (EmpathyTpContactList *list,
146                          TpChannel *channel,
147                          GArray *added)
148 {
149         EmpathyTpContactListPriv *priv = GET_PRIV (list);
150         const gchar *group_name;
151         guint i;
152
153         group_name = tp_channel_get_identifier (channel);
154
155         for (i = 0; i < added->len; i++) {
156                 EmpathyContact *contact;
157                 TpHandle handle;
158
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;
164                 }
165
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);
171         }
172 }
173
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)
184 {
185         EmpathyTpContactListPriv  *priv = GET_PRIV (list);
186         const gchar *group_name;
187         guint i;
188
189         contacts_added_to_group (list, channel, added);
190
191         group_name = tp_channel_get_identifier (channel);
192
193         for (i = 0; i < removed->len; i++) {
194                 EmpathyContact *contact;
195                 TpHandle handle;
196
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;
202                 }
203
204                 DEBUG ("Contact %s (%d) removed from group %s",
205                         empathy_contact_get_id (contact), handle, group_name);
206
207                 g_signal_emit_by_name (list, "groups-changed", contact,
208                                        group_name,
209                                        FALSE);
210         }
211 }
212
213 static void
214 tp_contact_list_group_ready_cb (TpChannel *channel,
215                                 const GError *error,
216                                 gpointer list)
217 {
218         EmpathyTpContactListPriv *priv = GET_PRIV (list);
219         TpChannel *old_group;
220         const gchar *group_name;
221         const TpIntSet *members;
222         GArray *arr;
223
224         if (error) {
225                 DEBUG ("Error: %s", error->message);
226                 g_object_unref (channel);
227                 return;
228         }
229
230         group_name = tp_channel_get_identifier (channel);
231
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.
238          */
239         old_group = g_hash_table_lookup (priv->groups, group_name);
240
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);
246         }
247
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);
251
252         g_signal_connect (channel, "group-members-changed",
253                           G_CALLBACK (tp_contact_list_group_members_changed_cb),
254                           list);
255
256         g_signal_connect (channel, "invalidated",
257                           G_CALLBACK (tp_contact_list_group_invalidated_cb),
258                           list);
259
260         if (priv->add_to_group) {
261                 GArray *handles;
262
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);
269                 }
270         }
271
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);
278 }
279
280 static void
281 tp_contact_list_group_add_channel (EmpathyTpContactList *list,
282                                    const gchar          *object_path,
283                                    GHashTable           *properties)
284 {
285         EmpathyTpContactListPriv *priv = GET_PRIV (list);
286         TpChannel                *channel;
287         GError *error = NULL;
288
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;
295         }
296
297         /* Give the ref to the callback */
298         tp_channel_call_when_ready (channel,
299                                     tp_contact_list_group_ready_cb,
300                                     list);
301 }
302
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)
309 {
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;
315         }
316 }
317
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)
324 {
325         TpHandle channel_handle;
326
327         if (error) {
328                 DEBUG ("Error: %s", error->message);
329                 return;
330         }
331
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);
341 }
342
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)
348 {
349         EmpathyTpContactListPriv *priv = GET_PRIV (list);
350         TpChannel                *channel;
351         const gchar              *names[] = {group_name, NULL};
352
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;
360         }
361
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
366          */
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));
375 }
376
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)
386 {
387         EmpathyTpContactListPriv *priv = GET_PRIV (list);
388         guint i;
389
390         if (error) {
391                 DEBUG ("Error: %s", error->message);
392                 return;
393         }
394
395         for (i = 0; i < n_contacts; i++) {
396                 EmpathyContact *contact = contacts[i];
397                 TpHandle handle = empathy_contact_get_handle (contact);
398
399                 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
400                         continue;
401
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);
407         }
408 }
409
410 static void
411 add_to_members (EmpathyTpContactList *list,
412                 GArray *handles)
413 {
414         EmpathyTpContactListPriv *priv = GET_PRIV (list);
415         GArray *request;
416         guint i;
417
418         if (handles->len == 0)
419                 return;
420
421         request = g_array_new (FALSE, FALSE, sizeof (TpHandle));
422
423         for (i = 0; i < handles->len; i++) {
424                 TpHandle handle = g_array_index (handles, TpHandle, i);
425
426                 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
427                         continue;
428
429                 g_array_append_val (request, handle);
430         }
431
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));
436         }
437
438         g_array_free (request, TRUE);
439 }
440
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)
450 {
451         EmpathyTpContactListPriv *priv = GET_PRIV (list);
452         guint i;
453
454         if (error) {
455                 DEBUG ("Error: %s", error->message);
456                 return;
457         }
458
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;
465
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);
469
470                 if (tp_intset_is_member (members, handle) ||
471                     tp_intset_is_member (remote_pending, handle)) {
472                         GArray handles = {(gchar *) &handle, 1};
473
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);
478                 }
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);
489                 }
490         }
491 }
492
493 static void
494 tp_contact_list_remove_handle (EmpathyTpContactList *list,
495                                GHashTable *table,
496                                TpHandle handle)
497 {
498         EmpathyTpContactListPriv *priv = GET_PRIV (list);
499         EmpathyContact *contact;
500         const gchar *sig;
501
502         if (table == priv->pendings)
503                 sig = "pendings-changed";
504         else if (table == priv->members)
505                 sig = "members-changed";
506         else
507                 return;
508
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);
516         }
517 }
518
519 static void
520 remove_from_member_if_needed (EmpathyTpContactList *list,
521                               TpHandle handle)
522 {
523         /* remove contact from members if it's not in publish and subscribe */
524         EmpathyTpContactListPriv *priv = GET_PRIV (list);
525         const TpIntSet *members;
526
527         members = tp_channel_group_get_members (priv->subscribe);
528         if (tp_intset_is_member (members, handle))
529                 return;
530
531         members = tp_channel_group_get_remote_pending (priv->subscribe);
532         if (tp_intset_is_member (members, handle))
533                 return;
534
535         members = tp_channel_group_get_members (priv->publish);
536         if (tp_intset_is_member (members, handle))
537                 return;
538
539         tp_contact_list_remove_handle (list, priv->members, handle);
540 }
541
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)
552 {
553         EmpathyTpContactListPriv *priv = GET_PRIV (list);
554         guint i;
555
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));
561         }
562
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);
566
567                 tp_contact_list_remove_handle (list, priv->pendings, handle);
568                 remove_from_member_if_needed (list, handle);
569         }
570
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));
578         }
579 }
580
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)
587 {
588         EmpathyTpContactListPriv *priv = GET_PRIV (list);
589
590         if (error) {
591                 DEBUG ("Error: %s", error->message);
592                 return;
593         }
594
595         if (flags & TP_CONNECTION_ALIAS_FLAG_USER_SET) {
596                 priv->flags |= EMPATHY_CONTACT_LIST_CAN_ALIAS;
597         }
598 }
599
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)
606 {
607         EmpathyTpContactListPriv *priv = GET_PRIV (list);
608         GPtrArray *classes;
609         guint i;
610
611         if (error) {
612                 DEBUG ("Error: %s", error->message);
613                 return;
614         }
615
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;
622
623                 props = g_value_get_boxed (g_value_array_get_nth (class, 0));
624
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);
629
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;
635                 }
636         }
637 }
638
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)
649 {
650         EmpathyTpContactListPriv *priv = GET_PRIV (list);
651         guint i;
652         GArray *accept;
653
654         /* We now get the presence of those contacts, add them to members */
655         add_to_members (list, added);
656
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));
660         }
661
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);
665
666         /* Implicitly accept pending request of contacts which are now members. */
667         if (priv->publish == NULL)
668                 return;
669
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);
673
674                 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle)) != NULL)
675                         g_array_append_val (accept, handle);
676         }
677
678         for (i = 0; i < remote_pending->len; i++) {
679                 TpHandle handle = g_array_index (added, TpHandle, i);
680
681                 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle)) != NULL)
682                         g_array_append_val (accept, handle);
683         }
684
685         tp_cli_channel_interface_group_call_add_members (priv->publish,
686                 -1, accept, NULL, NULL, NULL, NULL, NULL);
687
688         g_array_free (accept, TRUE);
689 }
690
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)
701 {
702         guint i;
703
704         add_to_members (list, added);
705
706         for (i = 0; i < removed->len; i++) {
707                 remove_from_member_if_needed (list, g_array_index (removed, TpHandle, i));
708         }
709 }
710
711 static void
712 tp_contact_list_finalize (GObject *object)
713 {
714         EmpathyTpContactListPriv *priv;
715         EmpathyTpContactList     *list;
716         GHashTableIter            iter;
717         gpointer                  channel;
718
719         list = EMPATHY_TP_CONTACT_LIST (object);
720         priv = GET_PRIV (list);
721
722         DEBUG ("finalize: %p", object);
723
724         if (priv->subscribe) {
725                 g_object_unref (priv->subscribe);
726         }
727         if (priv->publish) {
728                 g_object_unref (priv->publish);
729         }
730         if (priv->stored) {
731                 g_object_unref (priv->stored);
732         }
733
734         if (priv->deny) {
735                 g_object_unref (priv->deny);
736         }
737
738         if (priv->connection) {
739                 g_object_unref (priv->connection);
740         }
741
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);
746         }
747
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);
752
753         G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
754 }
755
756 static void
757 got_list_channel (EmpathyTpContactList *list,
758                   TpChannel *channel)
759 {
760         EmpathyTpContactListPriv *priv = GET_PRIV (list);
761         const gchar *id;
762
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);
766
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);
794         }
795 }
796
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)
805 {
806         EmpathyTpContactList *list = user_data;
807         TpChannel *channel;
808
809         if (error != NULL) {
810                 DEBUG ("failed: %s\n", error->message);
811                 return;
812         }
813
814         channel = tp_channel_new_from_properties (conn, path, properties, NULL);
815         got_list_channel (list, channel);
816         g_object_unref (channel);
817 }
818
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)
825 {
826         EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
827         EmpathyTpContactListPriv *priv = GET_PRIV (list);
828         TpContactBlockingCapabilities caps;
829
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;
835         }
836
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;
842         }
843
844         caps = g_value_get_uint (value);
845
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;
849         }
850 }
851
852 static void
853 iterate_on_channels (EmpathyTpContactList *list,
854                      const GPtrArray *channels)
855 {
856         guint i;
857
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;
863
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));
866
867                 if (tp_strdiff (tp_asv_get_string (properties,
868                                 TP_IFACE_CHANNEL ".ChannelType"),
869                     TP_IFACE_CHANNEL_TYPE_CONTACT_LIST))
870                         continue;
871
872                 if (tp_asv_get_string (properties, TP_IFACE_CHANNEL ".TargetID") == NULL)
873                         continue;
874
875                 handle_type = tp_asv_get_uint32 (properties,
876                         TP_IFACE_CHANNEL ".TargetHandleType", NULL);
877
878                 if (handle_type != TP_HANDLE_TYPE_GROUP)
879                         continue;
880
881                 tp_contact_list_group_add_channel (list, path, properties);
882         }
883 }
884
885 static void
886 new_channels_cb (TpConnection *conn,
887                  const GPtrArray *channels,
888                  gpointer user_data,
889                  GObject *weak_object)
890 {
891         EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
892
893         iterate_on_channels (list, channels);
894 }
895
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)
902 {
903         EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
904         const GPtrArray *channels;
905
906         if (error != NULL) {
907                 DEBUG ("Get Channels property failed: %s", error->message);
908                 return;
909         }
910
911         channels = g_value_get_boxed (out);
912         iterate_on_channels (list, channels);
913 }
914
915 static void
916 conn_ready_cb (TpConnection *connection,
917                const GError *error,
918                gpointer data)
919 {
920         EmpathyTpContactList *list = data;
921         EmpathyTpContactListPriv *priv = GET_PRIV (list);
922         GHashTable *request;
923
924         if (error != NULL) {
925                 DEBUG ("failed: %s", error->message);
926                 goto out;
927         }
928
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));
933
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);
938
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);
944
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));
949
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));
954
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));
959
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));
964
965         g_hash_table_unref (request);
966
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");
972
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));
978         }
979 out:
980         g_object_unref (list);
981 }
982
983 static void
984 tp_contact_list_constructed (GObject *list)
985 {
986         EmpathyTpContactListPriv *priv = GET_PRIV (list);
987
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,
993                                 -1,
994                                 tp_contact_list_get_alias_flags_cb,
995                                 NULL, NULL,
996                                 G_OBJECT (list));
997         }
998
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,
1003                                 -1,
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;
1012         }
1013
1014         tp_connection_call_when_ready (priv->connection, conn_ready_cb,
1015                 g_object_ref (list));
1016 }
1017
1018 static void
1019 tp_contact_list_get_property (GObject    *object,
1020                               guint       param_id,
1021                               GValue     *value,
1022                               GParamSpec *pspec)
1023 {
1024         EmpathyTpContactListPriv *priv = GET_PRIV (object);
1025
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;
1033         };
1034 }
1035
1036 static void
1037 tp_contact_list_set_property (GObject      *object,
1038                               guint         param_id,
1039                               const GValue *value,
1040                               GParamSpec   *pspec)
1041 {
1042         EmpathyTpContactListPriv *priv = GET_PRIV (object);
1043
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;
1051         };
1052 }
1053
1054 static void
1055 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
1056 {
1057         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1058
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;
1063
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));
1072
1073         g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
1074 }
1075
1076 static void
1077 tp_contact_list_array_free (gpointer handles)
1078 {
1079         g_array_free (handles, TRUE);
1080 }
1081
1082 static void
1083 empathy_tp_contact_list_init (EmpathyTpContactList *list)
1084 {
1085         EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
1086                 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
1087
1088         list->priv = priv;
1089
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);
1095
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);
1100
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);
1105
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);
1110 }
1111
1112 EmpathyTpContactList *
1113 empathy_tp_contact_list_new (TpConnection *connection)
1114 {
1115         return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
1116                              "connection", connection,
1117                              NULL);
1118 }
1119
1120 TpConnection *
1121 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
1122 {
1123         EmpathyTpContactListPriv *priv;
1124
1125         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
1126
1127         priv = GET_PRIV (list);
1128
1129         return priv->connection;
1130 }
1131
1132 static void
1133 tp_contact_list_add (EmpathyContactList *list,
1134                      EmpathyContact     *contact,
1135                      const gchar        *message)
1136 {
1137         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1138         TpHandle handle;
1139         GArray handles = {(gchar *) &handle, 1};
1140
1141         handle = empathy_contact_get_handle (contact);
1142
1143         if (priv->subscribe) {
1144                 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
1145                         -1, &handles, message, NULL, NULL, NULL, NULL);
1146         }
1147
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);
1154                 }
1155         }
1156
1157         if (priv->deny) {
1158                 tp_cli_channel_interface_group_call_remove_members (
1159                         priv->deny, -1, &handles, message,
1160                         NULL, NULL, NULL, NULL);
1161         }
1162 }
1163
1164 static void
1165 tp_contact_list_remove (EmpathyContactList *list,
1166                         EmpathyContact     *contact,
1167                         const gchar        *message)
1168 {
1169         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1170         TpHandle handle;
1171         GArray handles = {(gchar *) &handle, 1};
1172
1173         handle = empathy_contact_get_handle (contact);
1174
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);
1180         }
1181
1182         if (priv->subscribe) {
1183                 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
1184                         -1, &handles, message, NULL, NULL, NULL, NULL);
1185         }
1186         if (priv->publish) {
1187                 tp_cli_channel_interface_group_call_remove_members (priv->publish,
1188                         -1, &handles, message, NULL, NULL, NULL, NULL);
1189         }
1190 }
1191
1192 static GList *
1193 tp_contact_list_get_members (EmpathyContactList *list)
1194 {
1195         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1196         GList *ret;
1197
1198         ret = g_hash_table_get_values (priv->members);
1199         g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1200         return ret;
1201 }
1202
1203 static GList *
1204 tp_contact_list_get_pendings (EmpathyContactList *list)
1205 {
1206         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1207         GList *ret;
1208
1209         ret = g_hash_table_get_values (priv->pendings);
1210         g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1211         return ret;
1212 }
1213
1214 static GList *
1215 tp_contact_list_get_all_groups (EmpathyContactList *list)
1216 {
1217         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1218         GList                    *ret, *l;
1219
1220         ret = g_hash_table_get_keys (priv->groups);
1221         for (l = ret; l; l = l->next) {
1222                 l->data = g_strdup (l->data);
1223         }
1224
1225         return ret;
1226 }
1227
1228 static GList *
1229 tp_contact_list_get_groups (EmpathyContactList *list,
1230                             EmpathyContact     *contact)
1231 {
1232         EmpathyTpContactListPriv  *priv = GET_PRIV (list);
1233         GList                     *ret = NULL;
1234         GHashTableIter             iter;
1235         gpointer                   group_name;
1236         gpointer                   channel;
1237         TpHandle                   handle;
1238
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;
1243
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));
1247                 }
1248         }
1249
1250         return ret;
1251 }
1252
1253 static void
1254 tp_contact_list_add_to_group (EmpathyContactList *list,
1255                               EmpathyContact     *contact,
1256                               const gchar        *group_name)
1257 {
1258         TpHandle handle;
1259         GArray *handles;
1260
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);
1266 }
1267
1268 static void
1269 tp_contact_list_remove_from_group (EmpathyContactList *list,
1270                                    EmpathyContact     *contact,
1271                                    const gchar        *group_name)
1272 {
1273         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1274         TpChannel                *channel;
1275         TpHandle                  handle;
1276         GArray                    handles = {(gchar *) &handle, 1};
1277
1278         channel = g_hash_table_lookup (priv->groups, group_name);
1279         if (channel == NULL) {
1280                 return;
1281         }
1282
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);
1286
1287         tp_cli_channel_interface_group_call_remove_members (channel, -1,
1288                 &handles, NULL, NULL, NULL, NULL, NULL);
1289 }
1290
1291 static void
1292 tp_contact_list_rename_group (EmpathyContactList *list,
1293                               const gchar        *old_group_name,
1294                               const gchar        *new_group_name)
1295 {
1296         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1297         TpChannel                *channel;
1298         const TpIntSet           *members;
1299         GArray                   *handles;
1300
1301         channel = g_hash_table_lookup (priv->groups, old_group_name);
1302         if (channel == NULL) {
1303                 return;
1304         }
1305
1306         DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1307
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);
1314
1315         tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1316                                    new_group_name, handles);
1317 }
1318
1319 static void
1320 tp_contact_list_remove_group (EmpathyContactList *list,
1321                               const gchar *group_name)
1322 {
1323         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1324         TpChannel                *channel;
1325         const TpIntSet           *members;
1326         GArray                   *handles;
1327
1328         channel = g_hash_table_lookup (priv->groups, group_name);
1329         if (channel == NULL) {
1330                 return;
1331         }
1332
1333         DEBUG ("remove group %s", group_name);
1334
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);
1342 }
1343
1344 static EmpathyContactListFlags
1345 tp_contact_list_get_flags (EmpathyContactList *list)
1346 {
1347         EmpathyTpContactListPriv *priv;
1348         EmpathyContactListFlags flags;
1349
1350         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1351
1352         priv = GET_PRIV (list);
1353         flags = priv->flags;
1354
1355         if (priv->subscribe != NULL) {
1356                 TpChannelGroupFlags group_flags;
1357
1358                 group_flags = tp_channel_group_get_flags (priv->subscribe);
1359
1360                 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1361                         flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1362                 }
1363
1364                 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1365                         flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1366                 }
1367
1368                 if (group_flags & TP_CHANNEL_GROUP_FLAG_MESSAGE_ADD) {
1369                         flags |= EMPATHY_CONTACT_LIST_MESSAGE_ADD;
1370                 }
1371         }
1372
1373         if (priv->deny != NULL)
1374                 flags |= EMPATHY_CONTACT_LIST_CAN_BLOCK;
1375
1376         return flags;
1377 }
1378
1379 static void
1380 tp_contact_list_set_blocked (EmpathyContactList *list,
1381                              EmpathyContact     *contact,
1382                              gboolean            blocked,
1383                              gboolean            abusive)
1384 {
1385         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1386         TpHandle handle = empathy_contact_get_handle (contact);
1387         GArray handles = { (char *) &handle, 1 };
1388
1389         g_return_if_fail (TP_IS_CHANNEL (priv->deny));
1390
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);
1395
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);
1407         }
1408 }
1409
1410 static gboolean
1411 tp_contact_list_get_blocked (EmpathyContactList *list,
1412                              EmpathyContact     *contact)
1413 {
1414         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1415
1416         g_return_val_if_fail (TP_IS_CHANNEL (priv->deny), FALSE);
1417
1418         return tp_intset_is_member (tp_channel_group_get_members (priv->deny),
1419                                     empathy_contact_get_handle (contact));
1420 }
1421
1422 static void
1423 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1424 {
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;
1438 }
1439
1440 void
1441 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1442 {
1443         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1444         GHashTableIter            iter;
1445         gpointer                  contact;
1446
1447         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1448
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);
1455         }
1456         g_hash_table_remove_all (priv->members);
1457
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);
1463         }
1464         g_hash_table_remove_all (priv->pendings);
1465 }