Also accept local-pending of publish if adding a contact
[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;
50         GHashTable     *pendings;
51         GHashTable     *groups;
52 } EmpathyTpContactListPriv;
53
54 typedef enum {
55         TP_CONTACT_LIST_TYPE_PUBLISH,
56         TP_CONTACT_LIST_TYPE_SUBSCRIBE,
57         TP_CONTACT_LIST_TYPE_UNKNOWN
58 } TpContactListType;
59
60 static void tp_contact_list_iface_init         (EmpathyContactListIface   *iface);
61
62 enum {
63         PROP_0,
64         PROP_CONNECTION,
65 };
66
67 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
68                          G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
69                                                 tp_contact_list_iface_init));
70
71 static void
72 tp_contact_list_group_invalidated_cb (TpChannel *channel,
73                                       guint      domain,
74                                       gint       code,
75                                       gchar     *message,
76                                       EmpathyTpContactList *list)
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         DEBUG ("Group %s invalidated. Message: %s", group_name, message);
85
86         /* Signal that all members are not in that group anymore */
87         members = tp_channel_group_get_members (channel);
88         tp_intset_iter_init (&iter, members);
89         while (tp_intset_iter_next (&iter)) {
90                 EmpathyContact *contact;
91
92                 contact = g_hash_table_lookup (priv->members,
93                                                GUINT_TO_POINTER (iter.element));
94                 if (contact == NULL) {
95                         continue;
96                 }
97
98                 DEBUG ("Contact %s (%d) removed from group %s",
99                         empathy_contact_get_id (contact), iter.element,
100                         group_name);
101                 g_signal_emit_by_name (list, "groups-changed", contact,
102                                        group_name,
103                                        FALSE);
104         }
105
106         g_hash_table_remove (priv->groups, group_name);
107 }
108
109 static void
110 tp_contact_list_group_ready_cb (TpChannel *channel,
111                                 const GError *error,
112                                 gpointer list)
113 {
114         EmpathyTpContactListPriv *priv = GET_PRIV (list);
115
116         if (error) {
117                 DEBUG ("Error: %s", error->message);
118                 g_object_unref (channel);
119                 return;
120         }
121         
122         DEBUG ("Add group %s", tp_channel_get_identifier (channel));
123         g_hash_table_insert (priv->groups,
124                              (gpointer) tp_channel_get_identifier (channel),
125                              channel);
126
127         g_signal_connect (channel, "invalidated",
128                           G_CALLBACK (tp_contact_list_group_invalidated_cb),
129                           list);
130 }
131
132 static void
133 tp_contact_list_group_members_changed_cb (TpChannel     *channel,
134                                           gchar         *message,
135                                           GArray        *added,
136                                           GArray        *removed,
137                                           GArray        *local_pending,
138                                           GArray        *remote_pending,
139                                           guint          actor,
140                                           guint          reason,
141                                           EmpathyTpContactList *list)
142 {
143         EmpathyTpContactListPriv  *priv = GET_PRIV (list);
144         const gchar *group_name;
145         gint i;
146
147         group_name = tp_channel_get_identifier (channel);
148
149         for (i = 0; i < added->len; i++) {
150                 EmpathyContact *contact;
151                 TpHandle handle;
152
153                 handle = g_array_index (added, TpHandle, i);
154                 contact = g_hash_table_lookup (priv->members,
155                                                GUINT_TO_POINTER (handle));
156                 if (contact == NULL) {
157                         continue;
158                 }
159
160                 DEBUG ("Contact %s (%d) added to group %s",
161                         empathy_contact_get_id (contact), handle, group_name);
162                 g_signal_emit_by_name (list, "groups-changed", contact,
163                                        group_name,
164                                        TRUE);
165         }       
166
167         for (i = 0; i < removed->len; i++) {
168                 EmpathyContact *contact;
169                 TpHandle handle;
170
171                 handle = g_array_index (removed, TpHandle, i);
172                 contact = g_hash_table_lookup (priv->members,
173                                                GUINT_TO_POINTER (handle));
174                 if (contact == NULL) {
175                         continue;
176                 }
177
178                 DEBUG ("Contact %s (%d) removed from group %s",
179                         empathy_contact_get_id (contact), handle, group_name);
180
181                 g_signal_emit_by_name (list, "groups-changed", contact,
182                                        group_name,
183                                        FALSE);
184         }       
185 }
186
187 static TpChannel *
188 tp_contact_list_group_add_channel (EmpathyTpContactList *list,
189                                    const gchar          *object_path,
190                                    const gchar          *channel_type,
191                                    TpHandleType          handle_type,
192                                    guint                 handle)
193 {
194         EmpathyTpContactListPriv *priv = GET_PRIV (list);
195         TpChannel                *channel;
196
197         /* Only accept server-side contact groups */
198         if (tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) ||
199             handle_type != TP_HANDLE_TYPE_GROUP) {
200                 return NULL;
201         }
202
203         channel = tp_channel_new (priv->connection,
204                                   object_path, channel_type,
205                                   handle_type, handle, NULL);
206
207         /* TpChannel emits initial set of members just before being ready */
208         g_signal_connect (channel, "group-members-changed",
209                           G_CALLBACK (tp_contact_list_group_members_changed_cb),
210                           list);
211
212         /* Give the ref to the callback */
213         tp_channel_call_when_ready (channel,
214                                     tp_contact_list_group_ready_cb,
215                                     list);
216
217         return channel;
218 }
219
220 typedef struct {
221         GArray *handles;
222         TpHandle channel_handle;
223         guint ref_count;
224 } GroupAddData;
225
226 static void
227 tp_contact_list_group_add_data_unref (gpointer user_data)
228 {
229         GroupAddData *data = user_data;
230
231         if (--data->ref_count == 0) {
232                 g_array_free (data->handles, TRUE);
233                 g_slice_free (GroupAddData, data);
234         }
235 }
236
237 static void
238 tp_contact_list_group_add_ready_cb (TpChannel    *channel,
239                                     const GError *error,
240                                     gpointer      user_data)
241 {
242         GroupAddData *data = user_data;
243
244         if (error) {
245                 tp_contact_list_group_add_data_unref (data);
246                 return;
247         }
248
249         tp_cli_channel_interface_group_call_add_members (channel, -1,
250                 data->handles, NULL, NULL, NULL, NULL, NULL);
251         tp_contact_list_group_add_data_unref (data);
252 }
253
254 static void
255 tp_contact_list_group_request_channel_cb (TpConnection *connection,
256                                           const gchar  *object_path,
257                                           const GError *error,
258                                           gpointer      user_data,
259                                           GObject      *list)
260 {
261         GroupAddData *data = user_data;
262         TpChannel *channel;
263
264         if (error) {
265                 DEBUG ("Error: %s", error->message);
266                 return;
267         }
268
269         channel = tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
270                                                      object_path,
271                                                      TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
272                                                      TP_HANDLE_TYPE_GROUP,
273                                                      data->channel_handle);
274
275         data->ref_count++;
276         tp_channel_call_when_ready (channel,
277                                     tp_contact_list_group_add_ready_cb,
278                                     data);
279 }
280
281 static void
282 tp_contact_list_group_request_handles_cb (TpConnection *connection,
283                                           const GArray *handles,
284                                           const GError *error,
285                                           gpointer      user_data,
286                                           GObject      *list)
287 {
288         GroupAddData *data = user_data;
289
290         if (error) {
291                 DEBUG ("Error: %s", error->message);
292                 return;
293         }
294
295         data->channel_handle = g_array_index (handles, TpHandle, 0);
296         data->ref_count++;
297         tp_cli_connection_call_request_channel (connection, -1,
298                                                 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
299                                                 TP_HANDLE_TYPE_GROUP,
300                                                 data->channel_handle,
301                                                 TRUE,
302                                                 tp_contact_list_group_request_channel_cb,
303                                                 data, tp_contact_list_group_add_data_unref,
304                                                 list);
305 }
306
307 static void
308 tp_contact_list_group_add (EmpathyTpContactList *list,
309                            const gchar          *group_name,
310                            GArray               *handles)
311 {
312         EmpathyTpContactListPriv *priv = GET_PRIV (list);
313         TpChannel                *channel;
314         const gchar              *names[] = {group_name, NULL};
315         GroupAddData             *data;
316
317         channel = g_hash_table_lookup (priv->groups, group_name);
318         if (channel) {
319                 tp_cli_channel_interface_group_call_add_members (channel, -1,
320                         handles, NULL, NULL, NULL, NULL, NULL);
321                 g_array_free (handles, TRUE);
322                 return;
323         }
324
325         data = g_slice_new0 (GroupAddData);
326         data->handles = handles;
327         data->ref_count = 1;
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                                                 data, tp_contact_list_group_add_data_unref,
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         if (!suppress_handler) {
636                 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
637                                                    object_path, channel_type,
638                                                    handle_type, handle);
639         }
640 }
641
642 static void
643 tp_contact_list_list_channels_cb (TpConnection    *connection,
644                                   const GPtrArray *channels,
645                                   const GError    *error,
646                                   gpointer         user_data,
647                                   GObject         *list)
648 {
649         guint i;
650
651         if (error) {
652                 DEBUG ("Error: %s", error->message);
653                 return;
654         }
655
656         for (i = 0; i < channels->len; i++) {
657                 GValueArray  *chan_struct;
658                 const gchar  *object_path;
659                 const gchar  *channel_type;
660                 TpHandleType  handle_type;
661                 guint         handle;
662
663                 chan_struct = g_ptr_array_index (channels, i);
664                 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
665                 channel_type = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
666                 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
667                 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
668
669                 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
670                                                    object_path, channel_type,
671                                                    handle_type, handle);
672         }
673 }
674
675 static void
676 tp_contact_list_finalize (GObject *object)
677 {
678         EmpathyTpContactListPriv *priv;
679         EmpathyTpContactList     *list;
680         GHashTableIter            iter;
681         gpointer                  channel;
682
683         list = EMPATHY_TP_CONTACT_LIST (object);
684         priv = GET_PRIV (list);
685
686         DEBUG ("finalize: %p", object);
687
688         if (priv->subscribe) {
689                 g_object_unref (priv->subscribe);
690         }
691         if (priv->publish) {
692                 g_object_unref (priv->publish);
693         }
694
695         if (priv->connection) {
696                 g_object_unref (priv->connection);
697         }
698
699         if (priv->factory) {
700                 g_object_unref (priv->factory);
701         }
702
703         g_hash_table_iter_init (&iter, priv->groups);
704         while (g_hash_table_iter_next (&iter, NULL, &channel)) {
705                 g_signal_handlers_disconnect_by_func (channel,
706                         tp_contact_list_group_invalidated_cb, list);
707         }
708
709         g_hash_table_destroy (priv->groups);
710         g_hash_table_destroy (priv->members);
711         g_hash_table_destroy (priv->pendings);
712
713         G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
714 }
715
716 static void
717 tp_contact_list_constructed (GObject *list)
718 {
719
720         EmpathyTpContactListPriv *priv = GET_PRIV (list);
721         gchar                    *protocol_name = NULL;
722         const gchar              *names[] = {NULL, NULL};
723
724         priv->factory = empathy_tp_contact_factory_dup_singleton (priv->connection);
725
726         names[0] = "publish";
727         tp_cli_connection_call_request_handles (priv->connection,
728                                                 -1,
729                                                 TP_HANDLE_TYPE_LIST,
730                                                 names,
731                                                 tp_contact_list_publish_request_handle_cb,
732                                                 NULL, NULL,
733                                                 G_OBJECT (list));
734         names[0] = "subscribe";
735         tp_cli_connection_call_request_handles (priv->connection,
736                                                 -1,
737                                                 TP_HANDLE_TYPE_LIST,
738                                                 names,
739                                                 tp_contact_list_subscribe_request_handle_cb,
740                                                 NULL, NULL,
741                                                 G_OBJECT (list));
742
743         tp_cli_connection_call_list_channels (priv->connection, -1,
744                                               tp_contact_list_list_channels_cb,
745                                               NULL, NULL,
746                                               list);
747
748         tp_cli_connection_connect_to_new_channel (priv->connection,
749                                                   tp_contact_list_new_channel_cb,
750                                                   NULL, NULL,
751                                                   list, NULL);
752
753         /* Check for protocols that does not support contact groups. We can
754          * put all contacts into a special group in that case.
755          * FIXME: Default group should be an information in the profile
756          * FIXME: replace with tp_connection_parse_object_path once released */
757         protocol_name = empathy_connection_get_protocol (priv->connection, NULL);
758         if (!tp_strdiff (protocol_name, "local-xmpp")) {
759                 priv->protocol_group = _("People nearby");
760         }
761         g_free (protocol_name);
762 }
763
764 static void
765 tp_contact_list_get_property (GObject    *object,
766                               guint       param_id,
767                               GValue     *value,
768                               GParamSpec *pspec)
769 {
770         EmpathyTpContactListPriv *priv = GET_PRIV (object);
771
772         switch (param_id) {
773         case PROP_CONNECTION:
774                 g_value_set_object (value, priv->connection);
775                 break;
776         default:
777                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
778                 break;
779         };
780 }
781
782 static void
783 tp_contact_list_set_property (GObject      *object,
784                               guint         param_id,
785                               const GValue *value,
786                               GParamSpec   *pspec)
787 {
788         EmpathyTpContactListPriv *priv = GET_PRIV (object);
789
790         switch (param_id) {
791         case PROP_CONNECTION:
792                 priv->connection = g_value_dup_object (value);
793                 break;
794         default:
795                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
796                 break;
797         };
798 }
799
800 static void
801 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
802 {
803         GObjectClass *object_class = G_OBJECT_CLASS (klass);
804
805         object_class->finalize = tp_contact_list_finalize;
806         object_class->constructed = tp_contact_list_constructed;
807         object_class->get_property = tp_contact_list_get_property;
808         object_class->set_property = tp_contact_list_set_property;
809
810         g_object_class_install_property (object_class,
811                                          PROP_CONNECTION,
812                                          g_param_spec_object ("connection",
813                                                               "The Connection",
814                                                               "The connection associated with the contact list",
815                                                               TP_TYPE_CONNECTION,
816                                                               G_PARAM_READWRITE |
817                                                               G_PARAM_CONSTRUCT_ONLY));
818
819         g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
820 }
821
822 static void
823 empathy_tp_contact_list_init (EmpathyTpContactList *list)
824 {
825         EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
826                 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
827
828         list->priv = priv;
829
830         /* Map group's name to group's channel */
831         priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
832                                               NULL,
833                                               (GDestroyNotify) g_object_unref);
834
835         /* Map contact's handle to EmpathyContact object */
836         priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
837                                                NULL,
838                                                (GDestroyNotify) g_object_unref);
839
840         /* Map contact's handle to EmpathyContact object */
841         priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
842                                                 NULL,
843                                                 (GDestroyNotify) g_object_unref);
844 }
845
846 EmpathyTpContactList *
847 empathy_tp_contact_list_new (TpConnection *connection)
848 {
849         return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
850                              "connection", connection,
851                              NULL);
852 }
853
854 TpConnection *
855 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
856 {
857         EmpathyTpContactListPriv *priv;
858
859         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
860
861         priv = GET_PRIV (list);
862
863         return priv->connection;
864 }
865
866 static void
867 tp_contact_list_add (EmpathyContactList *list,
868                      EmpathyContact     *contact,
869                      const gchar        *message)
870 {
871         EmpathyTpContactListPriv *priv = GET_PRIV (list);
872         TpHandle handle;
873         GArray handles = {(gchar *) &handle, 1};
874
875         handle = empathy_contact_get_handle (contact);
876         if (priv->subscribe) {
877                 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
878                         -1, &handles, message, NULL, NULL, NULL, NULL);
879         }
880         if (priv->publish &&
881             g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
882                 tp_cli_channel_interface_group_call_add_members (priv->publish,
883                         -1, &handles, message, NULL, NULL, NULL, NULL);
884         }
885 }
886
887 static void
888 tp_contact_list_remove (EmpathyContactList *list,
889                         EmpathyContact     *contact,
890                         const gchar        *message)
891 {
892         EmpathyTpContactListPriv *priv = GET_PRIV (list);
893         TpHandle handle;
894         GArray handles = {(gchar *) &handle, 1};
895
896         handle = empathy_contact_get_handle (contact);
897         if (priv->subscribe) {
898                 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
899                         -1, &handles, message, NULL, NULL, NULL, NULL);
900         }
901         if (priv->publish) {
902                 tp_cli_channel_interface_group_call_remove_members (priv->publish,
903                         -1, &handles, message, NULL, NULL, NULL, NULL);
904         }
905 }
906
907 static GList *
908 tp_contact_list_get_members (EmpathyContactList *list)
909 {
910         EmpathyTpContactListPriv *priv = GET_PRIV (list);
911         GList *ret;
912
913         ret = g_hash_table_get_values (priv->members);
914         g_list_foreach (ret, (GFunc) g_object_ref, NULL);
915         return ret;
916 }
917
918 static GList *
919 tp_contact_list_get_pendings (EmpathyContactList *list)
920 {
921         EmpathyTpContactListPriv *priv = GET_PRIV (list);
922         GList *ret;
923
924         ret = g_hash_table_get_values (priv->pendings);
925         g_list_foreach (ret, (GFunc) g_object_ref, NULL);
926         return ret;
927 }
928
929 static GList *
930 tp_contact_list_get_all_groups (EmpathyContactList *list)
931 {
932         EmpathyTpContactListPriv *priv = GET_PRIV (list);
933         GList                    *ret, *l;
934
935         ret = g_hash_table_get_keys (priv->groups);
936         for (l = ret; l; l = l->next) {
937                 l->data = g_strdup (l->data);
938         }
939
940         if (priv->protocol_group) {
941                 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
942         }
943
944         return ret;
945 }
946
947 static GList *
948 tp_contact_list_get_groups (EmpathyContactList *list,
949                             EmpathyContact     *contact)
950 {
951         EmpathyTpContactListPriv  *priv = GET_PRIV (list);
952         GList                     *ret = NULL;
953         GHashTableIter             iter;
954         gpointer                   group_name;
955         gpointer                   channel;
956         TpHandle                   handle;
957
958         handle = empathy_contact_get_handle (contact);
959         g_hash_table_iter_init (&iter, priv->groups);
960         while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
961                 const TpIntSet *members;
962
963                 members = tp_channel_group_get_members (channel);
964                 if (tp_intset_is_member (members, handle)) {
965                         ret = g_list_prepend (ret, g_strdup (group_name));
966                 }
967         }
968
969         if (priv->protocol_group) {
970                 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
971         }
972
973         return ret;
974 }
975
976 static void
977 tp_contact_list_add_to_group (EmpathyContactList *list,
978                               EmpathyContact     *contact,
979                               const gchar        *group_name)
980 {
981         TpHandle handle;
982         GArray *handles;
983
984         handle = empathy_contact_get_handle (contact);
985         handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
986         g_array_append_val (handles, handle);
987         tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
988                                    group_name, handles);
989 }
990
991 static void
992 tp_contact_list_remove_from_group (EmpathyContactList *list,
993                                    EmpathyContact     *contact,
994                                    const gchar        *group_name)
995 {
996         EmpathyTpContactListPriv *priv = GET_PRIV (list);
997         TpChannel                *channel;
998         TpHandle                  handle;
999         GArray                    handles = {(gchar *) &handle, 1};
1000
1001         channel = g_hash_table_lookup (priv->groups, group_name);
1002         if (channel == NULL) {
1003                 return;
1004         }
1005
1006         handle = empathy_contact_get_handle (contact);
1007         DEBUG ("remove contact %s (%d) from group %s",
1008                 empathy_contact_get_id (contact), handle, group_name);
1009
1010         tp_cli_channel_interface_group_call_remove_members (channel, -1,
1011                 &handles, NULL, NULL, NULL, NULL, NULL);
1012 }
1013
1014 static void
1015 tp_contact_list_rename_group (EmpathyContactList *list,
1016                               const gchar        *old_group_name,
1017                               const gchar        *new_group_name)
1018 {
1019         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1020         TpChannel                *channel;
1021         const TpIntSet           *members;
1022         GArray                   *handles;
1023
1024         channel = g_hash_table_lookup (priv->groups, old_group_name);
1025         if (channel == NULL) {
1026                 return;
1027         }
1028
1029         DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1030
1031         /* Remove all members and close the old channel */
1032         members = tp_channel_group_get_members (channel);
1033         handles = tp_intset_to_array (members);
1034         tp_cli_channel_interface_group_call_remove_members (channel, -1,
1035                 handles, NULL, NULL, NULL, NULL, NULL);
1036         tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1037
1038         tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1039                                    new_group_name, handles);
1040 }
1041
1042 static void
1043 tp_contact_list_remove_group (EmpathyContactList *list,
1044                               const gchar *group_name)
1045 {
1046         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1047         TpChannel                *channel;
1048         const TpIntSet           *members;
1049         GArray                   *handles;
1050
1051         channel = g_hash_table_lookup (priv->groups, group_name);
1052         if (channel == NULL) {
1053                 return;
1054         }
1055
1056         DEBUG ("remove group %s", group_name);
1057
1058         /* Remove all members and close the channel */
1059         members = tp_channel_group_get_members (channel);
1060         handles = tp_intset_to_array (members);
1061         tp_cli_channel_interface_group_call_remove_members (channel, -1,
1062                 handles, NULL, NULL, NULL, NULL, NULL);
1063         tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1064         g_array_free (handles, TRUE);
1065 }
1066
1067 static void
1068 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1069 {
1070         iface->add               = tp_contact_list_add;
1071         iface->remove            = tp_contact_list_remove;
1072         iface->get_members       = tp_contact_list_get_members;
1073         iface->get_pendings      = tp_contact_list_get_pendings;
1074         iface->get_all_groups    = tp_contact_list_get_all_groups;
1075         iface->get_groups        = tp_contact_list_get_groups;
1076         iface->add_to_group      = tp_contact_list_add_to_group;
1077         iface->remove_from_group = tp_contact_list_remove_from_group;
1078         iface->rename_group      = tp_contact_list_rename_group;
1079         iface->remove_group      = tp_contact_list_remove_group;
1080 }
1081
1082 gboolean
1083 empathy_tp_contact_list_can_add (EmpathyTpContactList *list)
1084 {
1085         EmpathyTpContactListPriv *priv;
1086         TpChannelGroupFlags       flags;
1087
1088         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1089
1090         priv = GET_PRIV (list);
1091
1092         if (priv->subscribe == NULL)
1093                 return FALSE;
1094
1095         flags = tp_channel_group_get_flags (priv->subscribe);
1096         return (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) != 0;
1097 }
1098
1099 void
1100 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1101 {
1102         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1103         GHashTableIter            iter;
1104         gpointer                  contact;
1105
1106         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1107
1108         /* Remove all contacts */
1109         g_hash_table_iter_init (&iter, priv->members);
1110         while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1111                 g_signal_emit_by_name (list, "members-changed", contact,
1112                                        NULL, 0, NULL,
1113                                        FALSE);
1114         }
1115         g_hash_table_remove_all (priv->members);
1116
1117         g_hash_table_iter_init (&iter, priv->pendings);
1118         while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1119                 g_signal_emit_by_name (list, "pendings-changed", contact,
1120                                        NULL, 0, NULL,
1121                                        FALSE);
1122         }
1123         g_hash_table_remove_all (priv->pendings);
1124 }
1125