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