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