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