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