]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-contact-list.c
05515456403627e0fb7b926c6d76a5df2678a021
[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         /* contact handle (TpHandle) => reffed (EmpathyContact *)
52          *
53          * Union of:
54          *  - members of 'subscribe': we receive their presence
55          *  - RP of 'subscribe': we asked to receive their presence
56          *  - members of 'publish': we send them our presence
57          *  - members of 'stored': they are in our roster
58          */
59         GHashTable     *members;
60         /* contact handle (TpHandle) => reffed (EmpathyContact *)
61          *
62          * Contacts which are local-pending in the publish channel but are NOT in
63          * the members hash table: they asked to receive our presence and we don't
64          * receive theirs or asked to.
65          * That's basically the contacts which asked to add us to their contact
66          * list and we didn't answer it. */
67         GHashTable     *pendings;
68         /* group name: borrowed (const gchar *)  => reffed (TpChannel *) */
69         GHashTable     *groups;
70         /* group name: owned (gchar *) => owned GArray of TpHandle */
71         GHashTable     *add_to_group;
72
73         EmpathyContactListFlags flags;
74 } EmpathyTpContactListPriv;
75
76 typedef enum {
77         TP_CONTACT_LIST_TYPE_PUBLISH,
78         TP_CONTACT_LIST_TYPE_SUBSCRIBE,
79         TP_CONTACT_LIST_TYPE_UNKNOWN
80 } TpContactListType;
81
82 static void tp_contact_list_iface_init         (EmpathyContactListIface   *iface);
83
84 enum {
85         PROP_0,
86         PROP_CONNECTION,
87 };
88
89 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
90                          G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
91                                                 tp_contact_list_iface_init));
92
93 static void
94 tp_contact_list_forget_group (EmpathyTpContactList *list,
95                               TpChannel *channel)
96 {
97         EmpathyTpContactListPriv *priv = GET_PRIV (list);
98         const TpIntSet *members;
99         TpIntSetIter iter;
100         const gchar *group_name;
101
102         group_name = tp_channel_get_identifier (channel);
103
104         /* Signal that all members are not in that group anymore */
105         members = tp_channel_group_get_members (channel);
106         tp_intset_iter_init (&iter, members);
107         while (tp_intset_iter_next (&iter)) {
108                 EmpathyContact *contact;
109
110                 contact = g_hash_table_lookup (priv->members,
111                                                GUINT_TO_POINTER (iter.element));
112                 if (contact == NULL) {
113                         continue;
114                 }
115
116                 DEBUG ("Contact %s (%d) removed from group %s",
117                         empathy_contact_get_id (contact), iter.element,
118                         group_name);
119                 g_signal_emit_by_name (list, "groups-changed", contact,
120                                        group_name,
121                                        FALSE);
122         }
123 }
124
125 static void
126 tp_contact_list_group_invalidated_cb (TpChannel *channel,
127                                       guint      domain,
128                                       gint       code,
129                                       gchar     *message,
130                                       EmpathyTpContactList *list)
131 {
132         EmpathyTpContactListPriv *priv = GET_PRIV (list);
133         const gchar *group_name;
134
135         group_name = tp_channel_get_identifier (channel);
136         DEBUG ("Group %s invalidated. Message: %s", group_name, message);
137
138         tp_contact_list_forget_group (list, channel);
139
140         g_hash_table_remove (priv->groups, group_name);
141 }
142
143 static void
144 contacts_added_to_group (EmpathyTpContactList *list,
145                          TpChannel *channel,
146                          GArray *added)
147 {
148         EmpathyTpContactListPriv *priv = GET_PRIV (list);
149         const gchar *group_name;
150         guint i;
151
152         group_name = tp_channel_get_identifier (channel);
153
154         for (i = 0; i < added->len; i++) {
155                 EmpathyContact *contact;
156                 TpHandle handle;
157
158                 handle = g_array_index (added, TpHandle, i);
159                 contact = g_hash_table_lookup (priv->members,
160                                                GUINT_TO_POINTER (handle));
161                 if (contact == NULL) {
162                         continue;
163                 }
164
165                 DEBUG ("Contact %s (%d) added to group %s",
166                         empathy_contact_get_id (contact), handle, group_name);
167                 g_signal_emit_by_name (list, "groups-changed", contact,
168                                        group_name,
169                                        TRUE);
170         }
171 }
172
173 static void
174 tp_contact_list_group_members_changed_cb (TpChannel     *channel,
175                                           gchar         *message,
176                                           GArray        *added,
177                                           GArray        *removed,
178                                           GArray        *local_pending,
179                                           GArray        *remote_pending,
180                                           guint          actor,
181                                           guint          reason,
182                                           EmpathyTpContactList *list)
183 {
184         EmpathyTpContactListPriv  *priv = GET_PRIV (list);
185         const gchar *group_name;
186         guint i;
187
188         contacts_added_to_group (list, channel, added);
189
190         group_name = tp_channel_get_identifier (channel);
191
192         for (i = 0; i < removed->len; i++) {
193                 EmpathyContact *contact;
194                 TpHandle handle;
195
196                 handle = g_array_index (removed, TpHandle, i);
197                 contact = g_hash_table_lookup (priv->members,
198                                                GUINT_TO_POINTER (handle));
199                 if (contact == NULL) {
200                         continue;
201                 }
202
203                 DEBUG ("Contact %s (%d) removed from group %s",
204                         empathy_contact_get_id (contact), handle, group_name);
205
206                 g_signal_emit_by_name (list, "groups-changed", contact,
207                                        group_name,
208                                        FALSE);
209         }
210 }
211
212 static void
213 tp_contact_list_group_ready_cb (TpChannel *channel,
214                                 const GError *error,
215                                 gpointer list)
216 {
217         EmpathyTpContactListPriv *priv = GET_PRIV (list);
218         TpChannel *old_group;
219         const gchar *group_name;
220         const TpIntSet *members;
221         GArray *arr;
222
223         if (error) {
224                 DEBUG ("Error: %s", error->message);
225                 g_object_unref (channel);
226                 return;
227         }
228
229         group_name = tp_channel_get_identifier (channel);
230
231         /* If there's already a group with this name in the table, we can't
232          * just let it be replaced. Replacing it causes it to be unreffed,
233          * which causes it to be invalidated (see
234          * <https://bugs.freedesktop.org/show_bug.cgi?id=22119>), which causes
235          * it to be removed from the hash table again, which causes it to be
236          * unreffed again.
237          */
238         old_group = g_hash_table_lookup (priv->groups, group_name);
239
240         if (old_group != NULL) {
241                 DEBUG ("Discarding old group %s (%p)", group_name, old_group);
242                 g_hash_table_steal (priv->groups, group_name);
243                 tp_contact_list_forget_group (list, old_group);
244                 g_object_unref (old_group);
245         }
246
247         /* Pass the reference on the TpChannel to priv->groups */
248         g_hash_table_insert (priv->groups, (gpointer) group_name, channel);
249         DEBUG ("Group %s added", group_name);
250
251         g_signal_connect (channel, "group-members-changed",
252                           G_CALLBACK (tp_contact_list_group_members_changed_cb),
253                           list);
254
255         g_signal_connect (channel, "invalidated",
256                           G_CALLBACK (tp_contact_list_group_invalidated_cb),
257                           list);
258
259         if (priv->add_to_group) {
260                 GArray *handles;
261
262                 handles = g_hash_table_lookup (priv->add_to_group, group_name);
263                 if (handles) {
264                         DEBUG ("Adding initial members to group %s", group_name);
265                         tp_cli_channel_interface_group_call_add_members (channel,
266                                 -1, handles, NULL, NULL, NULL, NULL, NULL);
267                         g_hash_table_remove (priv->add_to_group, group_name);
268                 }
269         }
270
271         /* Get initial members of the group */
272         members = tp_channel_group_get_members (channel);
273         g_assert (members != NULL);
274         arr = tp_intset_to_array (members);
275         contacts_added_to_group (list, channel, arr);
276         g_array_unref (arr);
277 }
278
279 static void
280 tp_contact_list_group_add_channel (EmpathyTpContactList *list,
281                                    const gchar          *object_path,
282                                    GHashTable           *properties)
283 {
284         EmpathyTpContactListPriv *priv = GET_PRIV (list);
285         TpChannel                *channel;
286         GError *error = NULL;
287
288         channel = tp_channel_new_from_properties (priv->connection,
289                                   object_path, properties, &error);
290         if (channel == NULL) {
291                 DEBUG ("Failed to create group channel: %s", error->message);
292                 g_error_free (error);
293                 return;
294         }
295
296         /* Give the ref to the callback */
297         tp_channel_call_when_ready (channel,
298                                     tp_contact_list_group_ready_cb,
299                                     list);
300 }
301
302 static void
303 tp_contact_list_group_request_channel_cb (TpConnection *connection,
304                                           const gchar  *object_path,
305                                           const GError *error,
306                                           gpointer      user_data,
307                                           GObject      *list)
308 {
309         /* The new channel will be handled in NewChannel cb. Here we only
310          * handle the error if RequestChannel failed */
311         if (error) {
312                 DEBUG ("Error: %s", error->message);
313                 return;
314         }
315 }
316
317 static void
318 tp_contact_list_group_request_handles_cb (TpConnection *connection,
319                                           const GArray *handles,
320                                           const GError *error,
321                                           gpointer      user_data,
322                                           GObject      *list)
323 {
324         TpHandle channel_handle;
325
326         if (error) {
327                 DEBUG ("Error: %s", error->message);
328                 return;
329         }
330
331         channel_handle = g_array_index (handles, TpHandle, 0);
332         tp_cli_connection_call_request_channel (connection, -1,
333                                                 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
334                                                 TP_HANDLE_TYPE_GROUP,
335                                                 channel_handle,
336                                                 TRUE,
337                                                 tp_contact_list_group_request_channel_cb,
338                                                 NULL, NULL,
339                                                 list);
340 }
341
342 /* This function takes ownership of handles array */
343 static void
344 tp_contact_list_group_add (EmpathyTpContactList *list,
345                            const gchar          *group_name,
346                            GArray               *handles)
347 {
348         EmpathyTpContactListPriv *priv = GET_PRIV (list);
349         TpChannel                *channel;
350         const gchar              *names[] = {group_name, NULL};
351
352         /* Search the channel for that group name */
353         channel = g_hash_table_lookup (priv->groups, group_name);
354         if (channel) {
355                 tp_cli_channel_interface_group_call_add_members (channel, -1,
356                         handles, NULL, NULL, NULL, NULL, NULL);
357                 g_array_unref (handles);
358                 return;
359         }
360
361         /* That group does not exist yet, we have to:
362          * 1) Request an handle for the group name
363          * 2) Request a channel
364          * 3) When NewChannel is emitted, add handles in members
365          */
366         g_hash_table_insert (priv->add_to_group,
367                              g_strdup (group_name),
368                              handles);
369         tp_cli_connection_call_request_handles (priv->connection, -1,
370                                                 TP_HANDLE_TYPE_GROUP, names,
371                                                 tp_contact_list_group_request_handles_cb,
372                                                 NULL, NULL,
373                                                 G_OBJECT (list));
374 }
375
376 static void
377 got_added_members_cb (TpConnection            *connection,
378                       guint                    n_contacts,
379                       EmpathyContact * const * contacts,
380                       guint                    n_failed,
381                       const TpHandle          *failed,
382                       const GError            *error,
383                       gpointer                 user_data,
384                       GObject                 *list)
385 {
386         EmpathyTpContactListPriv *priv = GET_PRIV (list);
387         guint i;
388
389         if (error) {
390                 DEBUG ("Error: %s", error->message);
391                 return;
392         }
393
394         for (i = 0; i < n_contacts; i++) {
395                 EmpathyContact *contact = contacts[i];
396                 TpHandle handle = empathy_contact_get_handle (contact);
397
398                 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
399                         continue;
400
401                 /* Add to the list and emit signal */
402                 g_hash_table_insert (priv->members, GUINT_TO_POINTER (handle),
403                                      g_object_ref (contact));
404                 g_signal_emit_by_name (list, "members-changed", contact,
405                                        0, 0, NULL, TRUE);
406         }
407 }
408
409 static void
410 add_to_members (EmpathyTpContactList *list,
411                 GArray *handles)
412 {
413         EmpathyTpContactListPriv *priv = GET_PRIV (list);
414         GArray *request;
415         guint i;
416
417         if (handles->len == 0)
418                 return;
419
420         request = g_array_new (FALSE, FALSE, sizeof (TpHandle));
421
422         for (i = 0; i < handles->len; i++) {
423                 TpHandle handle = g_array_index (handles, TpHandle, i);
424
425                 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
426                         continue;
427
428                 g_array_append_val (request, handle);
429         }
430
431         if (request->len > 0) {
432                         empathy_tp_contact_factory_get_from_handles (priv->connection,
433                                 request->len, (TpHandle *) request->data,
434                                 got_added_members_cb, NULL, NULL, G_OBJECT (list));
435         }
436
437         g_array_unref (request);
438 }
439
440 static void
441 tp_contact_list_got_local_pending_cb (TpConnection            *connection,
442                                       guint                    n_contacts,
443                                       EmpathyContact * const * contacts,
444                                       guint                    n_failed,
445                                       const TpHandle          *failed,
446                                       const GError            *error,
447                                       gpointer                 user_data,
448                                       GObject                 *list)
449 {
450         EmpathyTpContactListPriv *priv = GET_PRIV (list);
451         guint i;
452
453         if (error) {
454                 DEBUG ("Error: %s", error->message);
455                 return;
456         }
457
458         for (i = 0; i < n_contacts; i++) {
459                 EmpathyContact *contact = contacts[i];
460                 TpHandle handle;
461                 const gchar *message;
462                 TpChannelGroupChangeReason reason;
463                 const TpIntSet *members, *remote_pending;
464
465                 handle = empathy_contact_get_handle (contact);
466                 members = tp_channel_group_get_members (priv->subscribe);
467                 remote_pending = tp_channel_group_get_remote_pending (priv->subscribe);
468
469                 if (tp_intset_is_member (members, handle) ||
470                     tp_intset_is_member (remote_pending, handle)) {
471                         GArray handles = {(gchar *) &handle, 1};
472
473                         /* This contact is already subscribed, or user requested
474                          * to subscribe, auto accept. */
475                         tp_cli_channel_interface_group_call_add_members (priv->publish,
476                                 -1, &handles, NULL, NULL, NULL, NULL, NULL);
477                 }
478                 else if (tp_channel_group_get_local_pending_info (priv->publish,
479                                                                   handle,
480                                                                   NULL,
481                                                                   &reason,
482                                                                   &message)) {
483                         /* Add contact to pendings */
484                         g_hash_table_insert (priv->pendings, GUINT_TO_POINTER (handle),
485                                              g_object_ref (contact));
486                         g_signal_emit_by_name (list, "pendings-changed", contact,
487                                                contact, reason, message, TRUE);
488                 }
489         }
490 }
491
492 static void
493 tp_contact_list_remove_handle (EmpathyTpContactList *list,
494                                GHashTable *table,
495                                TpHandle handle)
496 {
497         EmpathyTpContactListPriv *priv = GET_PRIV (list);
498         EmpathyContact *contact;
499         const gchar *sig;
500
501         if (table == priv->pendings)
502                 sig = "pendings-changed";
503         else if (table == priv->members)
504                 sig = "members-changed";
505         else
506                 return;
507
508         contact = g_hash_table_lookup (table, GUINT_TO_POINTER (handle));
509         if (contact) {
510                 g_object_ref (contact);
511                 g_hash_table_remove (table, GUINT_TO_POINTER (handle));
512                 g_signal_emit_by_name (list, sig, contact, 0, 0, NULL,
513                                        FALSE);
514                 g_object_unref (contact);
515         }
516 }
517
518 static void
519 remove_from_member_if_needed (EmpathyTpContactList *list,
520                               TpHandle handle)
521 {
522         /* remove contact from members if it's not in publish and subscribe */
523         EmpathyTpContactListPriv *priv = GET_PRIV (list);
524         const TpIntSet *members;
525
526         members = tp_channel_group_get_members (priv->subscribe);
527         if (tp_intset_is_member (members, handle))
528                 return;
529
530         members = tp_channel_group_get_remote_pending (priv->subscribe);
531         if (tp_intset_is_member (members, handle))
532                 return;
533
534         members = tp_channel_group_get_members (priv->publish);
535         if (tp_intset_is_member (members, handle))
536                 return;
537
538         tp_contact_list_remove_handle (list, priv->members, handle);
539 }
540
541 static void
542 tp_contact_list_publish_group_members_changed_cb (TpChannel     *channel,
543                                                   gchar         *message,
544                                                   GArray        *added,
545                                                   GArray        *removed,
546                                                   GArray        *local_pending,
547                                                   GArray        *remote_pending,
548                                                   TpHandle       actor,
549                                                   TpChannelGroupChangeReason reason,
550                                                   EmpathyTpContactList *list)
551 {
552         EmpathyTpContactListPriv *priv = GET_PRIV (list);
553         guint i;
554
555         /* We now send our presence to those contacts, remove them from pendings */
556         add_to_members (list, added);
557         for (i = 0; i < added->len; i++) {
558                 tp_contact_list_remove_handle (list, priv->pendings,
559                         g_array_index (added, TpHandle, i));
560         }
561
562         /* We refuse to send our presence to those contacts, remove from pendings */
563         for (i = 0; i < removed->len; i++) {
564                 TpHandle handle = g_array_index (removed, TpHandle, i);
565
566                 tp_contact_list_remove_handle (list, priv->pendings, handle);
567                 remove_from_member_if_needed (list, handle);
568         }
569
570         /* Those contacts want our presence, auto accept those that are already
571          * member, otherwise add in pendings. */
572         if (local_pending->len > 0) {
573                 empathy_tp_contact_factory_get_from_handles (priv->connection,
574                         local_pending->len, (TpHandle *) local_pending->data,
575                         tp_contact_list_got_local_pending_cb, NULL, NULL,
576                         G_OBJECT (list));
577         }
578 }
579
580 static void
581 tp_contact_list_get_alias_flags_cb (TpConnection *connection,
582                                     guint         flags,
583                                     const GError *error,
584                                     gpointer      user_data,
585                                     GObject      *list)
586 {
587         EmpathyTpContactListPriv *priv = GET_PRIV (list);
588
589         if (error) {
590                 DEBUG ("Error: %s", error->message);
591                 return;
592         }
593
594         if (flags & TP_CONNECTION_ALIAS_FLAG_USER_SET) {
595                 priv->flags |= EMPATHY_CONTACT_LIST_CAN_ALIAS;
596         }
597 }
598
599 static void
600 tp_contact_list_get_requestablechannelclasses_cb (TpProxy      *connection,
601                                                   const GValue *value,
602                                                   const GError *error,
603                                                   gpointer      user_data,
604                                                   GObject      *list)
605 {
606         EmpathyTpContactListPriv *priv = GET_PRIV (list);
607         GPtrArray *classes;
608         guint i;
609
610         if (error) {
611                 DEBUG ("Error: %s", error->message);
612                 return;
613         }
614
615         classes = g_value_get_boxed (value);
616         for (i = 0; i < classes->len; i++) {
617                 GValueArray *class = g_ptr_array_index (classes, i);
618                 GHashTable *props;
619                 const char *channel_type;
620                 guint handle_type;
621
622                 props = g_value_get_boxed (g_value_array_get_nth (class, 0));
623
624                 channel_type = tp_asv_get_string (props,
625                                 TP_IFACE_CHANNEL ".ChannelType");
626                 handle_type = tp_asv_get_uint32 (props,
627                                 TP_IFACE_CHANNEL ".TargetHandleType", NULL);
628
629                 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) &&
630                     handle_type == TP_HANDLE_TYPE_GROUP) {
631                         DEBUG ("Got channel class for a contact group");
632                         priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
633                         break;
634                 }
635         }
636 }
637
638 static void
639 tp_contact_list_subscribe_group_members_changed_cb (TpChannel     *channel,
640                                                     gchar         *message,
641                                                     GArray        *added,
642                                                     GArray        *removed,
643                                                     GArray        *local_pending,
644                                                     GArray        *remote_pending,
645                                                     guint          actor,
646                                                     guint          reason,
647                                                     EmpathyTpContactList *list)
648 {
649         EmpathyTpContactListPriv *priv = GET_PRIV (list);
650         guint i;
651         GArray *accept;
652
653         /* We now get the presence of those contacts, add them to members */
654         add_to_members (list, added);
655
656         /* Those contacts refuse to send us their presence, remove from members. */
657         for (i = 0; i < removed->len; i++) {
658                 remove_from_member_if_needed (list, g_array_index (removed, TpHandle, i));
659         }
660
661         /* We want those contacts in our contact list but we don't get their
662          * presence yet. Add to members anyway. */
663         add_to_members (list, remote_pending);
664
665         /* Implicitly accept pending request of contacts which are now members. */
666         if (priv->publish == NULL)
667                 return;
668
669         accept = g_array_new (FALSE, FALSE, sizeof (TpHandle));
670         for (i = 0; i < added->len; i++) {
671                 TpHandle handle = g_array_index (added, TpHandle, i);
672
673                 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle)) != NULL)
674                         g_array_append_val (accept, handle);
675         }
676
677         for (i = 0; i < remote_pending->len; i++) {
678                 TpHandle handle = g_array_index (added, TpHandle, i);
679
680                 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle)) != NULL)
681                         g_array_append_val (accept, handle);
682         }
683
684         tp_cli_channel_interface_group_call_add_members (priv->publish,
685                 -1, accept, NULL, NULL, NULL, NULL, NULL);
686
687         g_array_unref (accept);
688 }
689
690 static void
691 tp_contact_list_store_group_members_changed_cb (TpChannel     *channel,
692                                                 gchar         *message,
693                                                 GArray        *added,
694                                                 GArray        *removed,
695                                                 GArray        *local_pending,
696                                                 GArray        *remote_pending,
697                                                 guint          actor,
698                                                 guint          reason,
699                                                 EmpathyTpContactList *list)
700 {
701         guint i;
702
703         add_to_members (list, added);
704
705         for (i = 0; i < removed->len; i++) {
706                 remove_from_member_if_needed (list, g_array_index (removed, TpHandle, i));
707         }
708 }
709
710 static void
711 tp_contact_list_finalize (GObject *object)
712 {
713         EmpathyTpContactListPriv *priv;
714         EmpathyTpContactList     *list;
715         GHashTableIter            iter;
716         gpointer                  channel;
717
718         list = EMPATHY_TP_CONTACT_LIST (object);
719         priv = GET_PRIV (list);
720
721         DEBUG ("finalize: %p", object);
722
723         if (priv->subscribe) {
724                 g_object_unref (priv->subscribe);
725         }
726         if (priv->publish) {
727                 g_object_unref (priv->publish);
728         }
729         if (priv->stored) {
730                 g_object_unref (priv->stored);
731         }
732
733         if (priv->connection) {
734                 g_object_unref (priv->connection);
735         }
736
737         g_hash_table_iter_init (&iter, priv->groups);
738         while (g_hash_table_iter_next (&iter, NULL, &channel)) {
739                 g_signal_handlers_disconnect_by_func (channel,
740                         tp_contact_list_group_invalidated_cb, list);
741         }
742
743         g_hash_table_unref (priv->groups);
744         g_hash_table_unref (priv->members);
745         g_hash_table_unref (priv->pendings);
746         g_hash_table_unref (priv->add_to_group);
747
748         G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
749 }
750
751 static void
752 got_list_channel (EmpathyTpContactList *list,
753                   TpChannel *channel)
754 {
755         EmpathyTpContactListPriv *priv = GET_PRIV (list);
756         const gchar *id;
757
758         /* We requested that channel by providing TargetID property, so it's
759          * guaranteed that tp_channel_get_identifier will return it. */
760         id = tp_channel_get_identifier (channel);
761
762         /* TpChannel emits initial set of members just before being ready */
763         if (!tp_strdiff (id, "stored")) {
764                 if (priv->stored != NULL)
765                         return;
766                 priv->stored = g_object_ref (channel);
767                 g_signal_connect (priv->stored, "group-members-changed",
768                                   G_CALLBACK (tp_contact_list_store_group_members_changed_cb),
769                                   list);
770         } else if (!tp_strdiff (id, "publish")) {
771                 if (priv->publish != NULL)
772                         return;
773                 priv->publish = g_object_ref (channel);
774                 g_signal_connect (priv->publish, "group-members-changed",
775                                   G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
776                                   list);
777         } else if (!tp_strdiff (id, "subscribe")) {
778                 if (priv->subscribe != NULL)
779                         return;
780                 priv->subscribe = g_object_ref (channel);
781                 g_signal_connect (priv->subscribe, "group-members-changed",
782                                   G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
783                                   list);
784         }
785 }
786
787 static void
788 list_ensure_channel_cb (TpConnection *conn,
789                         gboolean yours,
790                         const gchar *path,
791                         GHashTable *properties,
792                         const GError *error,
793                         gpointer user_data,
794                         GObject *weak_object)
795 {
796         EmpathyTpContactList *list = user_data;
797         TpChannel *channel;
798
799         if (error != NULL) {
800                 DEBUG ("failed: %s\n", error->message);
801                 return;
802         }
803
804         channel = tp_channel_new_from_properties (conn, path, properties, NULL);
805         got_list_channel (list, channel);
806         g_object_unref (channel);
807 }
808
809 static void
810 iterate_on_channels (EmpathyTpContactList *list,
811                      const GPtrArray *channels)
812 {
813         guint i;
814
815         for (i = 0; i < channels->len ; i++) {
816                 GValueArray *arr = g_ptr_array_index (channels, i);
817                 const gchar *path;
818                 GHashTable *properties;
819                 TpHandleType handle_type;
820
821                 path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
822                 properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
823
824                 if (tp_strdiff (tp_asv_get_string (properties,
825                                 TP_IFACE_CHANNEL ".ChannelType"),
826                     TP_IFACE_CHANNEL_TYPE_CONTACT_LIST))
827                         continue;
828
829                 if (tp_asv_get_string (properties, TP_IFACE_CHANNEL ".TargetID") == NULL)
830                         continue;
831
832                 handle_type = tp_asv_get_uint32 (properties,
833                         TP_IFACE_CHANNEL ".TargetHandleType", NULL);
834
835                 if (handle_type != TP_HANDLE_TYPE_GROUP)
836                         continue;
837
838                 tp_contact_list_group_add_channel (list, path, properties);
839         }
840 }
841
842 static void
843 new_channels_cb (TpConnection *conn,
844                  const GPtrArray *channels,
845                  gpointer user_data,
846                  GObject *weak_object)
847 {
848         EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
849
850         iterate_on_channels (list, channels);
851 }
852
853 static void
854 got_channels_cb (TpProxy *conn,
855                  const GValue *out,
856                  const GError *error,
857                  gpointer user_data,
858                  GObject *weak_object)
859 {
860         EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
861         const GPtrArray *channels;
862
863         if (error != NULL) {
864                 DEBUG ("Get Channels property failed: %s", error->message);
865                 return;
866         }
867
868         channels = g_value_get_boxed (out);
869         iterate_on_channels (list, channels);
870 }
871
872 static void
873 conn_ready_cb (TpConnection *connection,
874                const GError *error,
875                gpointer data)
876 {
877         EmpathyTpContactList *list = data;
878         EmpathyTpContactListPriv *priv = GET_PRIV (list);
879         GHashTable *request;
880
881         if (error != NULL) {
882                 DEBUG ("failed: %s", error->message);
883                 goto out;
884         }
885
886         /* Look for existing group channels */
887         tp_cli_dbus_properties_call_get (connection, -1,
888                 TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels", got_channels_cb,
889                 NULL, NULL, G_OBJECT (list));
890
891         request = tp_asv_new (
892                 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
893                 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_LIST,
894                 NULL);
895
896         /* Watch the NewChannels signal so if ensuring list channels fails (for
897          * example because the server is slow and the D-Bus call timeouts before CM
898          * fetches the roster), we have a chance to get them later. */
899         tp_cli_connection_interface_requests_connect_to_new_channels (
900                 priv->connection, new_channels_cb, NULL, NULL, G_OBJECT (list), NULL);
901
902         /* Request the 'stored' list. */
903         tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "stored");
904         tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
905                 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
906
907         /* Request the 'publish' list. */
908         tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "publish");
909         tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
910                 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
911
912         /* Request the 'subscribe' list. */
913         tp_asv_set_static_string (request, TP_PROP_CHANNEL_TARGET_ID, "subscribe");
914         tp_cli_connection_interface_requests_call_ensure_channel (priv->connection,
915                 G_MAXINT, request, list_ensure_channel_cb, list, NULL, G_OBJECT (list));
916
917         g_hash_table_unref (request);
918
919 out:
920         g_object_unref (list);
921 }
922
923 static void
924 tp_contact_list_constructed (GObject *list)
925 {
926         EmpathyTpContactListPriv *priv = GET_PRIV (list);
927
928         /* call GetAliasFlags */
929         if (tp_proxy_has_interface_by_id (priv->connection,
930                                 TP_IFACE_QUARK_CONNECTION_INTERFACE_ALIASING)) {
931                 tp_cli_connection_interface_aliasing_call_get_alias_flags (
932                                 priv->connection,
933                                 -1,
934                                 tp_contact_list_get_alias_flags_cb,
935                                 NULL, NULL,
936                                 G_OBJECT (list));
937         }
938
939         /* lookup RequestableChannelClasses */
940         if (tp_proxy_has_interface_by_id (priv->connection,
941                                 TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS)) {
942                 tp_cli_dbus_properties_call_get (priv->connection,
943                                 -1,
944                                 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
945                                 "RequestableChannelClasses",
946                                 tp_contact_list_get_requestablechannelclasses_cb,
947                                 NULL, NULL,
948                                 G_OBJECT (list));
949         } else {
950                 /* we just don't know... better mark the flag just in case */
951                 priv->flags |= EMPATHY_CONTACT_LIST_CAN_GROUP;
952         }
953
954         tp_connection_call_when_ready (priv->connection, conn_ready_cb,
955                 g_object_ref (list));
956 }
957
958 static void
959 tp_contact_list_get_property (GObject    *object,
960                               guint       param_id,
961                               GValue     *value,
962                               GParamSpec *pspec)
963 {
964         EmpathyTpContactListPriv *priv = GET_PRIV (object);
965
966         switch (param_id) {
967         case PROP_CONNECTION:
968                 g_value_set_object (value, priv->connection);
969                 break;
970         default:
971                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
972                 break;
973         };
974 }
975
976 static void
977 tp_contact_list_set_property (GObject      *object,
978                               guint         param_id,
979                               const GValue *value,
980                               GParamSpec   *pspec)
981 {
982         EmpathyTpContactListPriv *priv = GET_PRIV (object);
983
984         switch (param_id) {
985         case PROP_CONNECTION:
986                 priv->connection = g_value_dup_object (value);
987                 break;
988         default:
989                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
990                 break;
991         };
992 }
993
994 static void
995 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
996 {
997         GObjectClass *object_class = G_OBJECT_CLASS (klass);
998
999         object_class->finalize = tp_contact_list_finalize;
1000         object_class->constructed = tp_contact_list_constructed;
1001         object_class->get_property = tp_contact_list_get_property;
1002         object_class->set_property = tp_contact_list_set_property;
1003
1004         g_object_class_install_property (object_class,
1005                                          PROP_CONNECTION,
1006                                          g_param_spec_object ("connection",
1007                                                               "The Connection",
1008                                                               "The connection associated with the contact list",
1009                                                               TP_TYPE_CONNECTION,
1010                                                               G_PARAM_READWRITE |
1011                                                               G_PARAM_CONSTRUCT_ONLY));
1012
1013         g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
1014 }
1015
1016 static void
1017 tp_contact_list_array_free (gpointer handles)
1018 {
1019         g_array_unref (handles);
1020 }
1021
1022 static void
1023 empathy_tp_contact_list_init (EmpathyTpContactList *list)
1024 {
1025         EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
1026                 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
1027
1028         list->priv = priv;
1029
1030         /* Map group's name to group's TpChannel. The group name string is owned
1031          * by the TpChannel object */
1032         priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1033                                               NULL,
1034                                               (GDestroyNotify) g_object_unref);
1035
1036         /* Map contact's handle to EmpathyContact object */
1037         priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1038                                                NULL,
1039                                                (GDestroyNotify) g_object_unref);
1040
1041         /* Map contact's handle to EmpathyContact object */
1042         priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1043                                                 NULL,
1044                                                 (GDestroyNotify) g_object_unref);
1045
1046         /* Map group's name to GArray of handle */
1047         priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
1048                                                     g_free,
1049                                                     tp_contact_list_array_free);
1050 }
1051
1052 EmpathyTpContactList *
1053 empathy_tp_contact_list_new (TpConnection *connection)
1054 {
1055         return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
1056                              "connection", connection,
1057                              NULL);
1058 }
1059
1060 TpConnection *
1061 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
1062 {
1063         EmpathyTpContactListPriv *priv;
1064
1065         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
1066
1067         priv = GET_PRIV (list);
1068
1069         return priv->connection;
1070 }
1071
1072 static void
1073 tp_contact_list_add (EmpathyContactList *list,
1074                      EmpathyContact     *contact,
1075                      const gchar        *message)
1076 {
1077         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1078         TpHandle handle;
1079         GArray handles = {(gchar *) &handle, 1};
1080
1081         handle = empathy_contact_get_handle (contact);
1082
1083         if (priv->subscribe) {
1084                 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
1085                         -1, &handles, message, NULL, NULL, NULL, NULL);
1086         }
1087
1088         if (priv->publish) {
1089                 TpChannelGroupFlags flags = tp_channel_group_get_flags (priv->subscribe);
1090                 if (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD ||
1091                     g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
1092                         tp_cli_channel_interface_group_call_add_members (priv->publish,
1093                                 -1, &handles, message, NULL, NULL, NULL, NULL);
1094                 }
1095         }
1096
1097         /* We want to unblock the contact */
1098         if (tp_proxy_has_interface_by_id (priv->connection,
1099                 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING)) {
1100                 TpContact *tp_contact = empathy_contact_get_tp_contact (contact);
1101
1102                 if (tp_contact != NULL)
1103                         tp_contact_unblock_async (tp_contact, NULL, NULL);
1104         }
1105 }
1106
1107 static void
1108 tp_contact_list_remove (EmpathyContactList *list,
1109                         EmpathyContact     *contact,
1110                         const gchar        *message)
1111 {
1112         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1113         TpHandle handle;
1114         GArray handles = {(gchar *) &handle, 1};
1115
1116         handle = empathy_contact_get_handle (contact);
1117
1118         /* FIXME: this is racy if tp_contact_list_remove is called before the
1119          * 'stored' list has been retrieved. */
1120         if (priv->stored != NULL) {
1121                 tp_cli_channel_interface_group_call_remove_members (priv->stored,
1122                         -1, &handles, message, NULL, NULL, NULL, NULL);
1123         }
1124
1125         if (priv->subscribe) {
1126                 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
1127                         -1, &handles, message, NULL, NULL, NULL, NULL);
1128         }
1129         if (priv->publish) {
1130                 tp_cli_channel_interface_group_call_remove_members (priv->publish,
1131                         -1, &handles, message, NULL, NULL, NULL, NULL);
1132         }
1133 }
1134
1135 static GList *
1136 tp_contact_list_get_members (EmpathyContactList *list)
1137 {
1138         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1139         GList *ret;
1140
1141         ret = g_hash_table_get_values (priv->members);
1142         g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1143         return ret;
1144 }
1145
1146 static GList *
1147 tp_contact_list_get_pendings (EmpathyContactList *list)
1148 {
1149         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1150         GList *ret;
1151
1152         ret = g_hash_table_get_values (priv->pendings);
1153         g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1154         return ret;
1155 }
1156
1157 static GList *
1158 tp_contact_list_get_all_groups (EmpathyContactList *list)
1159 {
1160         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1161         GList                    *ret, *l;
1162
1163         ret = g_hash_table_get_keys (priv->groups);
1164         for (l = ret; l; l = l->next) {
1165                 l->data = g_strdup (l->data);
1166         }
1167
1168         return ret;
1169 }
1170
1171 static GList *
1172 tp_contact_list_get_groups (EmpathyContactList *list,
1173                             EmpathyContact     *contact)
1174 {
1175         EmpathyTpContactListPriv  *priv = GET_PRIV (list);
1176         GList                     *ret = NULL;
1177         GHashTableIter             iter;
1178         gpointer                   group_name;
1179         gpointer                   channel;
1180         TpHandle                   handle;
1181
1182         handle = empathy_contact_get_handle (contact);
1183         g_hash_table_iter_init (&iter, priv->groups);
1184         while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
1185                 const TpIntSet *members;
1186
1187                 members = tp_channel_group_get_members (channel);
1188                 if (tp_intset_is_member (members, handle)) {
1189                         ret = g_list_prepend (ret, g_strdup (group_name));
1190                 }
1191         }
1192
1193         return ret;
1194 }
1195
1196 static void
1197 tp_contact_list_add_to_group (EmpathyContactList *list,
1198                               EmpathyContact     *contact,
1199                               const gchar        *group_name)
1200 {
1201         TpHandle handle;
1202         GArray *handles;
1203
1204         handle = empathy_contact_get_handle (contact);
1205         handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1206         g_array_append_val (handles, handle);
1207         tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1208                                    group_name, handles);
1209 }
1210
1211 static void
1212 tp_contact_list_remove_from_group (EmpathyContactList *list,
1213                                    EmpathyContact     *contact,
1214                                    const gchar        *group_name)
1215 {
1216         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1217         TpChannel                *channel;
1218         TpHandle                  handle;
1219         GArray                    handles = {(gchar *) &handle, 1};
1220
1221         channel = g_hash_table_lookup (priv->groups, group_name);
1222         if (channel == NULL) {
1223                 return;
1224         }
1225
1226         handle = empathy_contact_get_handle (contact);
1227         DEBUG ("remove contact %s (%d) from group %s",
1228                 empathy_contact_get_id (contact), handle, group_name);
1229
1230         tp_cli_channel_interface_group_call_remove_members (channel, -1,
1231                 &handles, NULL, NULL, NULL, NULL, NULL);
1232 }
1233
1234 static void
1235 tp_contact_list_rename_group (EmpathyContactList *list,
1236                               const gchar        *old_group_name,
1237                               const gchar        *new_group_name)
1238 {
1239         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1240         TpChannel                *channel;
1241         const TpIntSet           *members;
1242         GArray                   *handles;
1243
1244         channel = g_hash_table_lookup (priv->groups, old_group_name);
1245         if (channel == NULL) {
1246                 return;
1247         }
1248
1249         DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1250
1251         /* Remove all members and close the old channel */
1252         members = tp_channel_group_get_members (channel);
1253         handles = tp_intset_to_array (members);
1254         tp_cli_channel_interface_group_call_remove_members (channel, -1,
1255                 handles, NULL, NULL, NULL, NULL, NULL);
1256         tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1257
1258         tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1259                                    new_group_name, handles);
1260 }
1261
1262 static void
1263 tp_contact_list_remove_group (EmpathyContactList *list,
1264                               const gchar *group_name)
1265 {
1266         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1267         TpChannel                *channel;
1268         const TpIntSet           *members;
1269         GArray                   *handles;
1270
1271         channel = g_hash_table_lookup (priv->groups, group_name);
1272         if (channel == NULL) {
1273                 return;
1274         }
1275
1276         DEBUG ("remove group %s", group_name);
1277
1278         /* Remove all members and close the channel */
1279         members = tp_channel_group_get_members (channel);
1280         handles = tp_intset_to_array (members);
1281         tp_cli_channel_interface_group_call_remove_members (channel, -1,
1282                 handles, NULL, NULL, NULL, NULL, NULL);
1283         tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1284         g_array_unref (handles);
1285 }
1286
1287 static EmpathyContactListFlags
1288 tp_contact_list_get_flags (EmpathyContactList *list)
1289 {
1290         EmpathyTpContactListPriv *priv;
1291         EmpathyContactListFlags flags;
1292
1293         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1294
1295         priv = GET_PRIV (list);
1296         flags = priv->flags;
1297
1298         if (priv->subscribe != NULL) {
1299                 TpChannelGroupFlags group_flags;
1300
1301                 group_flags = tp_channel_group_get_flags (priv->subscribe);
1302
1303                 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1304                         flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1305                 }
1306
1307                 if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1308                         flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1309                 }
1310
1311                 if (group_flags & TP_CHANNEL_GROUP_FLAG_MESSAGE_ADD) {
1312                         flags |= EMPATHY_CONTACT_LIST_MESSAGE_ADD;
1313                 }
1314         }
1315
1316         return flags;
1317 }
1318
1319 static void
1320 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1321 {
1322         iface->add               = tp_contact_list_add;
1323         iface->remove            = tp_contact_list_remove;
1324         iface->get_members       = tp_contact_list_get_members;
1325         iface->get_pendings      = tp_contact_list_get_pendings;
1326         iface->get_all_groups    = tp_contact_list_get_all_groups;
1327         iface->get_groups        = tp_contact_list_get_groups;
1328         iface->add_to_group      = tp_contact_list_add_to_group;
1329         iface->remove_from_group = tp_contact_list_remove_from_group;
1330         iface->rename_group      = tp_contact_list_rename_group;
1331         iface->remove_group      = tp_contact_list_remove_group;
1332         iface->get_flags         = tp_contact_list_get_flags;
1333 }
1334
1335 void
1336 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1337 {
1338         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1339         GHashTableIter            iter;
1340         gpointer                  contact;
1341
1342         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1343
1344         /* Remove all contacts */
1345         g_hash_table_iter_init (&iter, priv->members);
1346         while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1347                 g_signal_emit_by_name (list, "members-changed", contact,
1348                                        NULL, 0, NULL,
1349                                        FALSE);
1350         }
1351         g_hash_table_remove_all (priv->members);
1352
1353         g_hash_table_iter_init (&iter, priv->pendings);
1354         while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1355                 g_signal_emit_by_name (list, "pendings-changed", contact,
1356                                        NULL, 0, NULL,
1357                                        FALSE);
1358         }
1359         g_hash_table_remove_all (priv->pendings);
1360 }