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