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