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