]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-contact-list.c
11990a9a212c4d63356f56b8d6690c0074ec5097
[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
465                 handle = empathy_contact_get_handle (contact);
466                 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle))) {
467                         GArray handles = {(gchar *) &handle, 1};
468
469                         /* This contact is already member, auto accept. */
470                         tp_cli_channel_interface_group_call_add_members (priv->publish,
471                                 -1, &handles, NULL, NULL, NULL, NULL, NULL);
472                 }
473                 else if (tp_channel_group_get_local_pending_info (priv->publish,
474                                                                   handle,
475                                                                   NULL,
476                                                                   &reason,
477                                                                   &message)) {
478                         /* Add contact to pendings */
479                         g_hash_table_insert (priv->pendings, GUINT_TO_POINTER (handle),
480                                              g_object_ref (contact));
481                         g_signal_emit_by_name (list, "pendings-changed", contact,
482                                                contact, reason, message, TRUE);
483                 }
484         }
485 }
486
487 static void
488 tp_contact_list_remove_handle (EmpathyTpContactList *list,
489                                GHashTable *table,
490                                TpHandle handle)
491 {
492         EmpathyTpContactListPriv *priv = GET_PRIV (list);
493         EmpathyContact *contact;
494         const gchar *sig;
495
496         if (table == priv->pendings)
497                 sig = "pendings-changed";
498         else if (table == priv->members)
499                 sig = "members-changed";
500         else
501                 return;
502
503         contact = g_hash_table_lookup (table, GUINT_TO_POINTER (handle));
504         if (contact) {
505                 g_object_ref (contact);
506                 g_hash_table_remove (table, GUINT_TO_POINTER (handle));
507                 g_signal_emit_by_name (list, sig, contact, 0, 0, NULL,
508                                        FALSE);
509                 g_object_unref (contact);
510         }
511 }
512
513 static void
514 remove_from_member_if_needed (EmpathyTpContactList *list,
515                               TpHandle handle)
516 {
517         /* remove contact from members if it's not in publish and subscribe */
518         EmpathyTpContactListPriv *priv = GET_PRIV (list);
519         const TpIntSet *members;
520
521         members = tp_channel_group_get_members (priv->subscribe);
522         if (tp_intset_is_member (members, handle))
523                 return;
524
525         members = tp_channel_group_get_remote_pending (priv->subscribe);
526         if (tp_intset_is_member (members, handle))
527                 return;
528
529         members = tp_channel_group_get_members (priv->publish);
530         if (tp_intset_is_member (members, handle))
531                 return;
532
533         tp_contact_list_remove_handle (list, priv->members, handle);
534 }
535
536 static void
537 tp_contact_list_publish_group_members_changed_cb (TpChannel     *channel,
538                                                   gchar         *message,
539                                                   GArray        *added,
540                                                   GArray        *removed,
541                                                   GArray        *local_pending,
542                                                   GArray        *remote_pending,
543                                                   TpHandle       actor,
544                                                   TpChannelGroupChangeReason reason,
545                                                   EmpathyTpContactList *list)
546 {
547         EmpathyTpContactListPriv *priv = GET_PRIV (list);
548         guint i;
549
550         /* We now send our presence to those contacts, remove them from pendings */
551         add_to_members (list, added);
552         for (i = 0; i < added->len; i++) {
553                 tp_contact_list_remove_handle (list, priv->pendings,
554                         g_array_index (added, TpHandle, i));
555         }
556
557         /* We refuse to send our presence to those contacts, remove from pendings */
558         for (i = 0; i < removed->len; i++) {
559                 TpHandle handle = g_array_index (removed, TpHandle, i);
560
561                 tp_contact_list_remove_handle (list, priv->pendings, handle);
562                 remove_from_member_if_needed (list, handle);
563         }
564
565         /* Those contacts want our presence, auto accept those that are already
566          * member, otherwise add in pendings. */
567         if (local_pending->len > 0) {
568                 empathy_tp_contact_factory_get_from_handles (priv->connection,
569                         local_pending->len, (TpHandle *) local_pending->data,
570                         tp_contact_list_got_local_pending_cb, NULL, NULL,
571                         G_OBJECT (list));
572         }
573 }
574
575 static void
576 tp_contact_list_get_alias_flags_cb (TpConnection *connection,
577                                     guint         flags,
578                                     const GError *error,
579                                     gpointer      user_data,
580                                     GObject      *list)
581 {
582         EmpathyTpContactListPriv *priv = GET_PRIV (list);
583
584         if (error) {
585                 DEBUG ("Error: %s", error->message);
586                 return;
587         }
588
589         if (flags & TP_CONNECTION_ALIAS_FLAG_USER_SET) {
590                 priv->flags |= EMPATHY_CONTACT_LIST_CAN_ALIAS;
591         }
592 }
593
594 static void
595 tp_contact_list_get_requestablechannelclasses_cb (TpProxy      *connection,
596                                                   const GValue *value,
597                                                   const GError *error,
598                                                   gpointer      user_data,
599                                                   GObject      *list)
600 {
601         EmpathyTpContactListPriv *priv = GET_PRIV (list);
602         GPtrArray *classes;
603         guint i;
604
605         if (error) {
606                 DEBUG ("Error: %s", error->message);
607                 return;
608         }
609
610         classes = g_value_get_boxed (value);
611         for (i = 0; i < classes->len; i++) {
612                 GValueArray *class = g_ptr_array_index (classes, i);
613                 GHashTable *props;
614                 const char *channel_type;
615                 guint handle_type;
616
617                 props = g_value_get_boxed (g_value_array_get_nth (class, 0));
618
619                 channel_type = tp_asv_get_string (props,
620                                 TP_IFACE_CHANNEL ".ChannelType");
621                 handle_type = tp_asv_get_uint32 (props,
622                                 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
623
624                 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) &&
625                     handle_type == TP_HANDLE_TYPE_GROUP) {
626                         DEBUG ("Got channel class for a contact group");
627                         priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
628                         break;
629                 }
630         }
631 }
632
633 static void
634 tp_contact_list_subscribe_group_members_changed_cb (TpChannel     *channel,
635                                                     gchar         *message,
636                                                     GArray        *added,
637                                                     GArray        *removed,
638                                                     GArray        *local_pending,
639                                                     GArray        *remote_pending,
640                                                     guint          actor,
641                                                     guint          reason,
642                                                     EmpathyTpContactList *list)
643 {
644         EmpathyTpContactListPriv *priv = GET_PRIV (list);
645         guint i;
646         GArray *accept;
647
648         /* We now get the presence of those contacts, add them to members */
649         add_to_members (list, added);
650
651         /* Those contacts refuse to send us their presence, remove from members. */
652         for (i = 0; i < removed->len; i++) {
653                 remove_from_member_if_needed (list, g_array_index (removed, TpHandle, i));
654         }
655
656         /* We want those contacts in our contact list but we don't get their
657          * presence yet. Add to members anyway. */
658         add_to_members (list, remote_pending);
659
660         /* Implicitly accept pending request of contacts which are now members. */
661         if (priv->publish == NULL)
662                 return;
663
664         accept = g_array_new (FALSE, FALSE, sizeof (TpHandle));
665         for (i = 0; i < added->len; i++) {
666                 TpHandle handle = g_array_index (added, TpHandle, i);
667
668                 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle)) != NULL)
669                         g_array_append_val (accept, handle);
670         }
671
672         for (i = 0; i < remote_pending->len; i++) {
673                 TpHandle handle = g_array_index (added, TpHandle, i);
674
675                 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle)) != NULL)
676                         g_array_append_val (accept, handle);
677         }
678
679         tp_cli_channel_interface_group_call_add_members (priv->publish,
680                 -1, accept, NULL, NULL, NULL, NULL, NULL);
681
682         g_array_free (accept, TRUE);
683 }
684
685 static void
686 tp_contact_list_store_group_members_changed_cb (TpChannel     *channel,
687                                                 gchar         *message,
688                                                 GArray        *added,
689                                                 GArray        *removed,
690                                                 GArray        *local_pending,
691                                                 GArray        *remote_pending,
692                                                 guint          actor,
693                                                 guint          reason,
694                                                 EmpathyTpContactList *list)
695 {
696         guint i;
697
698         add_to_members (list, added);
699
700         for (i = 0; i < removed->len; i++) {
701                 remove_from_member_if_needed (list, g_array_index (removed, TpHandle, i));
702         }
703 }
704
705 static void
706 tp_contact_list_finalize (GObject *object)
707 {
708         EmpathyTpContactListPriv *priv;
709         EmpathyTpContactList     *list;
710         GHashTableIter            iter;
711         gpointer                  channel;
712
713         list = EMPATHY_TP_CONTACT_LIST (object);
714         priv = GET_PRIV (list);
715
716         DEBUG ("finalize: %p", object);
717
718         if (priv->subscribe) {
719                 g_object_unref (priv->subscribe);
720         }
721         if (priv->publish) {
722                 g_object_unref (priv->publish);
723         }
724         if (priv->stored) {
725                 g_object_unref (priv->stored);
726         }
727
728         if (priv->deny) {
729                 g_object_unref (priv->deny);
730         }
731
732         if (priv->connection) {
733                 g_object_unref (priv->connection);
734         }
735
736         g_hash_table_iter_init (&iter, priv->groups);
737         while (g_hash_table_iter_next (&iter, NULL, &channel)) {
738                 g_signal_handlers_disconnect_by_func (channel,
739                         tp_contact_list_group_invalidated_cb, list);
740         }
741
742         g_hash_table_destroy (priv->groups);
743         g_hash_table_destroy (priv->members);
744         g_hash_table_destroy (priv->pendings);
745         g_hash_table_destroy (priv->add_to_group);
746
747         G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
748 }
749
750 static void
751 got_list_channel (EmpathyTpContactList *list,
752                   TpChannel *channel)
753 {
754         EmpathyTpContactListPriv *priv = GET_PRIV (list);
755         const gchar *id;
756
757         /* We requested that channel by providing TargetID property, so it's
758          * guaranteed that tp_channel_get_identifier will return it. */
759         id = tp_channel_get_identifier (channel);
760
761         /* TpChannel emits initial set of members just before being ready */
762         if (!tp_strdiff (id, "stored")) {
763                 if (priv->stored != NULL)
764                         return;
765                 priv->stored = g_object_ref (channel);
766                 g_signal_connect (priv->stored, "group-members-changed",
767                                   G_CALLBACK (tp_contact_list_store_group_members_changed_cb),
768                                   list);
769         } else if (!tp_strdiff (id, "publish")) {
770                 if (priv->publish != NULL)
771                         return;
772                 priv->publish = g_object_ref (channel);
773                 g_signal_connect (priv->publish, "group-members-changed",
774                                   G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
775                                   list);
776         } else if (!tp_strdiff (id, "subscribe")) {
777                 if (priv->subscribe != NULL)
778                         return;
779                 priv->subscribe = g_object_ref (channel);
780                 g_signal_connect (priv->subscribe, "group-members-changed",
781                                   G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
782                                   list);
783         } else if (!tp_strdiff (id, "deny")) {
784                 if (priv->deny != NULL)
785                         return;
786                 DEBUG ("Got 'deny' channel");
787                 priv->deny = g_object_ref (channel);
788         }
789 }
790
791 static void
792 list_ensure_channel_cb (TpConnection *conn,
793                         gboolean yours,
794                         const gchar *path,
795                         GHashTable *properties,
796                         const GError *error,
797                         gpointer user_data,
798                         GObject *weak_object)
799 {
800         EmpathyTpContactList *list = user_data;
801         TpChannel *channel;
802
803         if (error != NULL) {
804                 DEBUG ("failed: %s\n", error->message);
805                 return;
806         }
807
808         channel = tp_channel_new_from_properties (conn, path, properties, NULL);
809         got_list_channel (list, channel);
810         g_object_unref (channel);
811 }
812
813 static void
814 list_get_contact_blocking_capabilities_cb (TpProxy *conn,
815                                            const GValue *value,
816                                            const GError *in_error,
817                                            gpointer user_data,
818                                            GObject *weak_object)
819 {
820         EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
821         EmpathyTpContactListPriv *priv = GET_PRIV (list);
822         TpContactBlockingCapabilities caps;
823
824         if (in_error != NULL) {
825                 DEBUG ("Get(Blocking, ContactBlockingCapabilities) on %s failed: %s",
826                        tp_proxy_get_object_path (conn),
827                        in_error->message);
828                 return;
829         }
830
831         if (!G_VALUE_HOLDS_UINT (value)) {
832                 DEBUG ("Get(Blocking, ContactBlockingCapabilities) on %s returned a %s, not a uint",
833                        tp_proxy_get_object_path (conn),
834                        G_VALUE_TYPE_NAME (value));
835                 return;
836         }
837
838         caps = g_value_get_uint (value);
839
840         if (caps & TP_CONTACT_BLOCKING_CAPABILITY_CAN_REPORT_ABUSIVE) {
841                 DEBUG ("Connection can report abusive contacts");
842                 priv->flags |= EMPATHY_CONTACT_LIST_CAN_REPORT_ABUSIVE;
843         }
844 }
845
846 static void
847 iterate_on_channels (EmpathyTpContactList *list,
848                      const GPtrArray *channels)
849 {
850         guint i;
851
852         for (i = 0; i < channels->len ; i++) {
853                 GValueArray *arr = g_ptr_array_index (channels, i);
854                 const gchar *path;
855                 GHashTable *properties;
856                 TpHandleType handle_type;
857
858                 path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
859                 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
860
861                 if (tp_strdiff (tp_asv_get_string (properties,
862                                 TP_IFACE_CHANNEL ".ChannelType"),
863                     TP_IFACE_CHANNEL_TYPE_CONTACT_LIST))
864                         continue;
865
866                 if (tp_asv_get_string (properties, TP_IFACE_CHANNEL ".TargetID") == NULL)
867                         continue;
868
869                 handle_type = tp_asv_get_uint32 (properties,
870                         TP_IFACE_CHANNEL ".TargetHandleType", NULL);
871
872                 if (handle_type != TP_HANDLE_TYPE_GROUP)
873                         continue;
874
875                 tp_contact_list_group_add_channel (list, path, properties);
876         }
877 }
878
879 static void
880 new_channels_cb (TpConnection *conn,
881                  const GPtrArray *channels,
882                  gpointer user_data,
883                  GObject *weak_object)
884 {
885         EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
886
887         iterate_on_channels (list, channels);
888 }
889
890 static void
891 got_channels_cb (TpProxy *conn,
892                  const GValue *out,
893                  const GError *error,
894                  gpointer user_data,
895                  GObject *weak_object)
896 {
897         EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
898         const GPtrArray *channels;
899
900         if (error != NULL) {
901                 DEBUG ("Get Channels property failed: %s", error->message);
902                 return;
903         }
904
905         channels = g_value_get_boxed (out);
906         iterate_on_channels (list, channels);
907 }
908
909 static void
910 conn_ready_cb (TpConnection *connection,
911                const GError *error,
912                gpointer data)
913 {
914         EmpathyTpContactList *list = data;
915         EmpathyTpContactListPriv *priv = GET_PRIV (list);
916         GHashTable *request;
917
918         if (error != NULL) {
919                 DEBUG ("failed: %s", error->message);
920                 goto out;
921         }
922
923         /* Look for existing group channels */
924         tp_cli_dbus_properties_call_get (connection, -1,
925                 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels", got_channels_cb,
926                 NULL, NULL, G_OBJECT (list));
927
928         request = tp_asv_new (
929                 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
930                 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_LIST,
931                 NULL);
932
933         /* Watch the NewChannels signal so if ensuring list channels fails (for
934          * example because the server is slow and the D-Bus call timeouts before CM
935          * fetches the roster), we have a chance to get them later. */
936         tp_cli_connection_interface_requests_connect_to_new_channels (
937                 priv->connection, new_channels_cb, NULL, NULL, G_OBJECT (list), NULL);
938
939         /* Request the 'stored' list. */
940         tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "stored");
941         tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
942                 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
943
944         /* Request the 'publish' list. */
945         tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "publish");
946         tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
947                 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
948
949         /* Request the 'subscribe' list. */
950         tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "subscribe");
951         tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
952                 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
953
954         /* Request the 'deny' list */
955         tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "deny");
956         tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
957                 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
958
959         g_hash_table_unref (request);
960
961         /* Find out if we support reporting abusive contacts --
962          * this is done via the new Conn.I.ContactBlocking interface */
963         if (tp_proxy_has_interface_by_id (priv->connection,
964             TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING)) {
965                 DEBUG ("Have Conn.I.ContactBlocking");
966
967                 tp_cli_dbus_properties_call_get (priv->connection, -1,
968                         TP_IFACE_CONNECTION_INTERFACE_CONTACT_BLOCKING,
969                         "ContactBlockingCapabilities",
970                         list_get_contact_blocking_capabilities_cb,
971                         NULL, NULL, G_OBJECT (list));
972         }
973 out:
974         g_object_unref (list);
975 }
976
977 static void
978 tp_contact_list_constructed (GObject *list)
979 {
980         EmpathyTpContactListPriv *priv = GET_PRIV (list);
981
982         /* call GetAliasFlags */
983         if (tp_proxy_has_interface_by_id (priv->connection,
984                                 TP_IFACE_QUARK_CONNECTION_INTERFACE_ALIASING)) {
985                 tp_cli_connection_interface_aliasing_call_get_alias_flags (
986                                 priv->connection,
987                                 -1,
988                                 tp_contact_list_get_alias_flags_cb,
989                                 NULL, NULL,
990                                 G_OBJECT (list));
991         }
992
993         /* lookup RequestableChannelClasses */
994         if (tp_proxy_has_interface_by_id (priv->connection,
995                                 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS)) {
996                 tp_cli_dbus_properties_call_get (priv->connection,
997                                 -1,
998                                 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
999                                 "RequestableChannelClasses",
1000                                 tp_contact_list_get_requestablechannelclasses_cb,
1001                                 NULL, NULL,
1002                                 G_OBJECT (list));
1003         } else {
1004                 /* we just don't know... better mark the flag just in case */
1005                 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
1006         }
1007
1008         tp_connection_call_when_ready (priv->connection, conn_ready_cb,
1009                 g_object_ref (list));
1010 }
1011
1012 static void
1013 tp_contact_list_get_property (GObject    *object,
1014                               guint       param_id,
1015                               GValue     *value,
1016                               GParamSpec *pspec)
1017 {
1018         EmpathyTpContactListPriv *priv = GET_PRIV (object);
1019
1020         switch (param_id) {
1021         case PROP_CONNECTION:
1022                 g_value_set_object (value, priv->connection);
1023                 break;
1024         default:
1025                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1026                 break;
1027         };
1028 }
1029
1030 static void
1031 tp_contact_list_set_property (GObject      *object,
1032                               guint         param_id,
1033                               const GValue *value,
1034                               GParamSpec   *pspec)
1035 {
1036         EmpathyTpContactListPriv *priv = GET_PRIV (object);
1037
1038         switch (param_id) {
1039         case PROP_CONNECTION:
1040                 priv->connection = g_value_dup_object (value);
1041                 break;
1042         default:
1043                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1044                 break;
1045         };
1046 }
1047
1048 static void
1049 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
1050 {
1051         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1052
1053         object_class->finalize = tp_contact_list_finalize;
1054         object_class->constructed = tp_contact_list_constructed;
1055         object_class->get_property = tp_contact_list_get_property;
1056         object_class->set_property = tp_contact_list_set_property;
1057
1058         g_object_class_install_property (object_class,
1059                                          PROP_CONNECTION,
1060                                          g_param_spec_object ("connection",
1061                                                               "The Connection",
1062                                                               "The connection associated with the contact list",
1063                                                               TP_TYPE_CONNECTION,
1064                                                               G_PARAM_READWRITE |
1065                                                               G_PARAM_CONSTRUCT_ONLY));
1066
1067         g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
1068 }
1069
1070 static void
1071 tp_contact_list_array_free (gpointer handles)
1072 {
1073         g_array_free (handles, TRUE);
1074 }
1075
1076 static void
1077 empathy_tp_contact_list_init (EmpathyTpContactList *list)
1078 {
1079         EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
1080                 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
1081
1082         list->priv = priv;
1083
1084         /* Map group's name to group's TpChannel. The group name string is owned
1085          * by the TpChannel object */
1086         priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1087                                               NULL,
1088                                               (GDestroyNotify) g_object_unref);
1089
1090         /* Map contact's handle to EmpathyContact object */
1091         priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1092                                                NULL,
1093                                                (GDestroyNotify) g_object_unref);
1094
1095         /* Map contact's handle to EmpathyContact object */
1096         priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1097                                                 NULL,
1098                                                 (GDestroyNotify) g_object_unref);
1099
1100         /* Map group's name to GArray of handle */
1101         priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
1102                                                     g_free,
1103                                                     tp_contact_list_array_free);
1104 }
1105
1106 EmpathyTpContactList *
1107 empathy_tp_contact_list_new (TpConnection *connection)
1108 {
1109         return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
1110                              "connection", connection,
1111                              NULL);
1112 }
1113
1114 TpConnection *
1115 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
1116 {
1117         EmpathyTpContactListPriv *priv;
1118
1119         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
1120
1121         priv = GET_PRIV (list);
1122
1123         return priv->connection;
1124 }
1125
1126 static void
1127 tp_contact_list_add (EmpathyContactList *list,
1128                      EmpathyContact     *contact,
1129                      const gchar        *message)
1130 {
1131         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1132         TpHandle handle;
1133         GArray handles = {(gchar *) &handle, 1};
1134
1135         handle = empathy_contact_get_handle (contact);
1136
1137         if (priv->subscribe) {
1138                 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
1139                         -1, &handles, message, NULL, NULL, NULL, NULL);
1140         }
1141
1142         if (priv->publish) {
1143                 TpChannelGroupFlags flags = tp_channel_group_get_flags (priv->subscribe);
1144                 if (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD ||
1145                     g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
1146                         tp_cli_channel_interface_group_call_add_members (priv->publish,
1147                                 -1, &handles, message, NULL, NULL, NULL, NULL);
1148                 }
1149         }
1150
1151         if (priv->deny) {
1152                 tp_cli_channel_interface_group_call_remove_members (
1153                         priv->deny, -1, &handles, message,
1154                         NULL, NULL, NULL, NULL);
1155         }
1156 }
1157
1158 static void
1159 tp_contact_list_remove (EmpathyContactList *list,
1160                         EmpathyContact     *contact,
1161                         const gchar        *message)
1162 {
1163         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1164         TpHandle handle;
1165         GArray handles = {(gchar *) &handle, 1};
1166
1167         handle = empathy_contact_get_handle (contact);
1168
1169         /* FIXME: this is racy if tp_contact_list_remove is called before the
1170          * 'stored' list has been retrieved. */
1171         if (priv->stored != NULL) {
1172                 tp_cli_channel_interface_group_call_remove_members (priv->stored,
1173                         -1, &handles, message, NULL, NULL, NULL, NULL);
1174         }
1175
1176         if (priv->subscribe) {
1177                 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
1178                         -1, &handles, message, NULL, NULL, NULL, NULL);
1179         }
1180         if (priv->publish) {
1181                 tp_cli_channel_interface_group_call_remove_members (priv->publish,
1182                         -1, &handles, message, NULL, NULL, NULL, NULL);
1183         }
1184 }
1185
1186 static GList *
1187 tp_contact_list_get_members (EmpathyContactList *list)
1188 {
1189         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1190         GList *ret;
1191
1192         ret = g_hash_table_get_values (priv->members);
1193         g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1194         return ret;
1195 }
1196
1197 static GList *
1198 tp_contact_list_get_pendings (EmpathyContactList *list)
1199 {
1200         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1201         GList *ret;
1202
1203         ret = g_hash_table_get_values (priv->pendings);
1204         g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1205         return ret;
1206 }
1207
1208 static GList *
1209 tp_contact_list_get_all_groups (EmpathyContactList *list)
1210 {
1211         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1212         GList                    *ret, *l;
1213
1214         ret = g_hash_table_get_keys (priv->groups);
1215         for (l = ret; l; l = l->next) {
1216                 l->data = g_strdup (l->data);
1217         }
1218
1219         return ret;
1220 }
1221
1222 static GList *
1223 tp_contact_list_get_groups (EmpathyContactList *list,
1224                             EmpathyContact     *contact)
1225 {
1226         EmpathyTpContactListPriv  *priv = GET_PRIV (list);
1227         GList                     *ret = NULL;
1228         GHashTableIter             iter;
1229         gpointer                   group_name;
1230         gpointer                   channel;
1231         TpHandle                   handle;
1232
1233         handle = empathy_contact_get_handle (contact);
1234         g_hash_table_iter_init (&iter, priv->groups);
1235         while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
1236                 const TpIntSet *members;
1237
1238                 members = tp_channel_group_get_members (channel);
1239                 if (tp_intset_is_member (members, handle)) {
1240                         ret = g_list_prepend (ret, g_strdup (group_name));
1241                 }
1242         }
1243
1244         return ret;
1245 }
1246
1247 static void
1248 tp_contact_list_add_to_group (EmpathyContactList *list,
1249                               EmpathyContact     *contact,
1250                               const gchar        *group_name)
1251 {
1252         TpHandle handle;
1253         GArray *handles;
1254
1255         handle = empathy_contact_get_handle (contact);
1256         handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1257         g_array_append_val (handles, handle);
1258         tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1259                                    group_name, handles);
1260 }
1261
1262 static void
1263 tp_contact_list_remove_from_group (EmpathyContactList *list,
1264                                    EmpathyContact     *contact,
1265                                    const gchar        *group_name)
1266 {
1267         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1268         TpChannel                *channel;
1269         TpHandle                  handle;
1270         GArray                    handles = {(gchar *) &handle, 1};
1271
1272         channel = g_hash_table_lookup (priv->groups, group_name);
1273         if (channel == NULL) {
1274                 return;
1275         }
1276
1277         handle = empathy_contact_get_handle (contact);
1278         DEBUG ("remove contact %s (%d) from group %s",
1279                 empathy_contact_get_id (contact), handle, group_name);
1280
1281         tp_cli_channel_interface_group_call_remove_members (channel, -1,
1282                 &handles, NULL, NULL, NULL, NULL, NULL);
1283 }
1284
1285 static void
1286 tp_contact_list_rename_group (EmpathyContactList *list,
1287                               const gchar        *old_group_name,
1288                               const gchar        *new_group_name)
1289 {
1290         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1291         TpChannel                *channel;
1292         const TpIntSet           *members;
1293         GArray                   *handles;
1294
1295         channel = g_hash_table_lookup (priv->groups, old_group_name);
1296         if (channel == NULL) {
1297                 return;
1298         }
1299
1300         DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1301
1302         /* Remove all members and close the old channel */
1303         members = tp_channel_group_get_members (channel);
1304         handles = tp_intset_to_array (members);
1305         tp_cli_channel_interface_group_call_remove_members (channel, -1,
1306                 handles, NULL, NULL, NULL, NULL, NULL);
1307         tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1308
1309         tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1310                                    new_group_name, handles);
1311 }
1312
1313 static void
1314 tp_contact_list_remove_group (EmpathyContactList *list,
1315                               const gchar *group_name)
1316 {
1317         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1318         TpChannel                *channel;
1319         const TpIntSet           *members;
1320         GArray                   *handles;
1321
1322         channel = g_hash_table_lookup (priv->groups, group_name);
1323         if (channel == NULL) {
1324                 return;
1325         }
1326
1327         DEBUG ("remove group %s", group_name);
1328
1329         /* Remove all members and close the channel */
1330         members = tp_channel_group_get_members (channel);
1331         handles = tp_intset_to_array (members);
1332         tp_cli_channel_interface_group_call_remove_members (channel, -1,
1333                 handles, NULL, NULL, NULL, NULL, NULL);
1334         tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1335         g_array_free (handles, TRUE);
1336 }
1337
1338 static EmpathyContactListFlags
1339 tp_contact_list_get_flags (EmpathyContactList *list)
1340 {
1341         EmpathyTpContactListPriv *priv;
1342         EmpathyContactListFlags flags;
1343
1344         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1345
1346         priv = GET_PRIV (list);
1347         flags = priv->flags;
1348
1349         if (priv->subscribe != NULL) {
1350                 TpChannelGroupFlags group_flags;
1351
1352                 group_flags = tp_channel_group_get_flags (priv->subscribe);
1353
1354                 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1355                         flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1356                 }
1357
1358                 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1359                         flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1360                 }
1361         }
1362
1363         if (priv->deny != NULL)
1364                 flags |= EMPATHY_CONTACT_LIST_CAN_BLOCK;
1365
1366         return flags;
1367 }
1368
1369 static void
1370 tp_contact_list_set_blocked (EmpathyContactList *list,
1371                              EmpathyContact     *contact,
1372                              gboolean            blocked,
1373                              gboolean            abusive)
1374 {
1375         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1376         TpHandle handle = empathy_contact_get_handle (contact);
1377         GArray handles = { (char *) &handle, 1 };
1378
1379         g_return_if_fail (TP_IS_CHANNEL (priv->deny));
1380
1381         if (blocked && abusive) {
1382                 /* we have to do this via the new interface */
1383                 g_return_if_fail (priv->flags &
1384                                 EMPATHY_CONTACT_LIST_CAN_REPORT_ABUSIVE);
1385
1386                 tp_cli_connection_interface_contact_blocking_call_block_contacts (
1387                         priv->connection, -1,
1388                         &handles, TRUE, NULL, NULL, NULL, NULL);
1389         } else if (blocked) {
1390                 tp_cli_channel_interface_group_call_add_members (
1391                         priv->deny, -1,
1392                         &handles, NULL, NULL, NULL, NULL, NULL);
1393         } else {
1394                 tp_cli_channel_interface_group_call_remove_members (
1395                         priv->deny, -1,
1396                         &handles, NULL, NULL, NULL, NULL, NULL);
1397         }
1398 }
1399
1400 static gboolean
1401 tp_contact_list_get_blocked (EmpathyContactList *list,
1402                              EmpathyContact     *contact)
1403 {
1404         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1405
1406         g_return_val_if_fail (TP_IS_CHANNEL (priv->deny), FALSE);
1407
1408         return tp_intset_is_member (tp_channel_group_get_members (priv->deny),
1409                                     empathy_contact_get_handle (contact));
1410 }
1411
1412 static void
1413 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1414 {
1415         iface->add               = tp_contact_list_add;
1416         iface->remove            = tp_contact_list_remove;
1417         iface->get_members       = tp_contact_list_get_members;
1418         iface->get_pendings      = tp_contact_list_get_pendings;
1419         iface->get_all_groups    = tp_contact_list_get_all_groups;
1420         iface->get_groups        = tp_contact_list_get_groups;
1421         iface->add_to_group      = tp_contact_list_add_to_group;
1422         iface->remove_from_group = tp_contact_list_remove_from_group;
1423         iface->rename_group      = tp_contact_list_rename_group;
1424         iface->remove_group      = tp_contact_list_remove_group;
1425         iface->get_flags         = tp_contact_list_get_flags;
1426         iface->set_blocked       = tp_contact_list_set_blocked;
1427         iface->get_blocked       = tp_contact_list_get_blocked;
1428 }
1429
1430 void
1431 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1432 {
1433         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1434         GHashTableIter            iter;
1435         gpointer                  contact;
1436
1437         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1438
1439         /* Remove all contacts */
1440         g_hash_table_iter_init (&iter, priv->members);
1441         while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1442                 g_signal_emit_by_name (list, "members-changed", contact,
1443                                        NULL, 0, NULL,
1444                                        FALSE);
1445         }
1446         g_hash_table_remove_all (priv->members);
1447
1448         g_hash_table_iter_init (&iter, priv->pendings);
1449         while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1450                 g_signal_emit_by_name (list, "pendings-changed", contact,
1451                                        NULL, 0, NULL,
1452                                        FALSE);
1453         }
1454         g_hash_table_remove_all (priv->pendings);
1455 }