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