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