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