]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-contact-list.c
call GetAliasFlags() to determine if an alias can be set on a ContactList
[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; /* handle -> EmpathyContact */
50         GHashTable     *pendings; /* handle -> EmpathyContact */
51         GHashTable     *groups; /* group name -> TpChannel */
52         GHashTable     *add_to_group; /* group name -> GArray of handles */
53
54         EmpathyContactListFlags flags;
55 } EmpathyTpContactListPriv;
56
57 typedef enum {
58         TP_CONTACT_LIST_TYPE_PUBLISH,
59         TP_CONTACT_LIST_TYPE_SUBSCRIBE,
60         TP_CONTACT_LIST_TYPE_UNKNOWN
61 } TpContactListType;
62
63 static void tp_contact_list_iface_init         (EmpathyContactListIface   *iface);
64
65 enum {
66         PROP_0,
67         PROP_CONNECTION,
68 };
69
70 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
71                          G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
72                                                 tp_contact_list_iface_init));
73
74 static void
75 tp_contact_list_forget_group (EmpathyTpContactList *list,
76                               TpChannel *channel)
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
85         /* Signal that all members are not in that group anymore */
86         members = tp_channel_group_get_members (channel);
87         tp_intset_iter_init (&iter, members);
88         while (tp_intset_iter_next (&iter)) {
89                 EmpathyContact *contact;
90
91                 contact = g_hash_table_lookup (priv->members,
92                                                GUINT_TO_POINTER (iter.element));
93                 if (contact == NULL) {
94                         continue;
95                 }
96
97                 DEBUG ("Contact %s (%d) removed from group %s",
98                         empathy_contact_get_id (contact), iter.element,
99                         group_name);
100                 g_signal_emit_by_name (list, "groups-changed", contact,
101                                        group_name,
102                                        FALSE);
103         }
104 }
105
106 static void
107 tp_contact_list_group_invalidated_cb (TpChannel *channel,
108                                       guint      domain,
109                                       gint       code,
110                                       gchar     *message,
111                                       EmpathyTpContactList *list)
112 {
113         EmpathyTpContactListPriv *priv = GET_PRIV (list);
114         const gchar *group_name;
115
116         group_name = tp_channel_get_identifier (channel);
117         DEBUG ("Group %s invalidated. Message: %s", group_name, message);
118
119         tp_contact_list_forget_group (list, channel);
120
121         g_hash_table_remove (priv->groups, group_name);
122 }
123
124 static void
125 tp_contact_list_group_ready_cb (TpChannel *channel,
126                                 const GError *error,
127                                 gpointer list)
128 {
129         EmpathyTpContactListPriv *priv = GET_PRIV (list);
130         TpChannel *old_group;
131         const gchar *group_name;
132
133         if (error) {
134                 DEBUG ("Error: %s", error->message);
135                 g_object_unref (channel);
136                 return;
137         }
138
139         group_name = tp_channel_get_identifier (channel);
140
141         /* If there's already a group with this name in the table, we can't
142          * just let it be replaced. Replacing it causes it to be unreffed,
143          * which causes it to be invalidated (see
144          * <https://bugs.freedesktop.org/show_bug.cgi?id=22119>), which causes
145          * it to be removed from the hash table again, which causes it to be
146          * unreffed again.
147          */
148         old_group = g_hash_table_lookup (priv->groups, group_name);
149
150         if (old_group != NULL) {
151                 DEBUG ("Discarding old group %s (%p)", group_name, old_group);
152                 g_hash_table_steal (priv->groups, group_name);
153                 tp_contact_list_forget_group (list, old_group);
154                 g_object_unref (old_group);
155         }
156
157         g_hash_table_insert (priv->groups, (gpointer) group_name, channel);
158         DEBUG ("Group %s added", group_name);
159
160         g_signal_connect (channel, "invalidated",
161                           G_CALLBACK (tp_contact_list_group_invalidated_cb),
162                           list);
163
164         if (priv->add_to_group) {
165                 GArray *handles;
166
167                 handles = g_hash_table_lookup (priv->add_to_group, group_name);
168                 if (handles) {
169                         DEBUG ("Adding initial members to group %s", group_name);
170                         tp_cli_channel_interface_group_call_add_members (channel,
171                                 -1, handles, NULL, NULL, NULL, NULL, NULL);
172                         g_hash_table_remove (priv->add_to_group, group_name);
173                 }
174         }
175 }
176
177 static void
178 tp_contact_list_group_members_changed_cb (TpChannel     *channel,
179                                           gchar         *message,
180                                           GArray        *added,
181                                           GArray        *removed,
182                                           GArray        *local_pending,
183                                           GArray        *remote_pending,
184                                           guint          actor,
185                                           guint          reason,
186                                           EmpathyTpContactList *list)
187 {
188         EmpathyTpContactListPriv  *priv = GET_PRIV (list);
189         const gchar *group_name;
190         gint i;
191
192         group_name = tp_channel_get_identifier (channel);
193
194         for (i = 0; i < added->len; i++) {
195                 EmpathyContact *contact;
196                 TpHandle handle;
197
198                 handle = g_array_index (added, TpHandle, i);
199                 contact = g_hash_table_lookup (priv->members,
200                                                GUINT_TO_POINTER (handle));
201                 if (contact == NULL) {
202                         continue;
203                 }
204
205                 DEBUG ("Contact %s (%d) added to group %s",
206                         empathy_contact_get_id (contact), handle, group_name);
207                 g_signal_emit_by_name (list, "groups-changed", contact,
208                                        group_name,
209                                        TRUE);
210         }
211
212         for (i = 0; i < removed->len; i++) {
213                 EmpathyContact *contact;
214                 TpHandle handle;
215
216                 handle = g_array_index (removed, TpHandle, i);
217                 contact = g_hash_table_lookup (priv->members,
218                                                GUINT_TO_POINTER (handle));
219                 if (contact == NULL) {
220                         continue;
221                 }
222
223                 DEBUG ("Contact %s (%d) removed from group %s",
224                         empathy_contact_get_id (contact), handle, group_name);
225
226                 g_signal_emit_by_name (list, "groups-changed", contact,
227                                        group_name,
228                                        FALSE);
229         }
230 }
231
232 static void
233 tp_contact_list_group_add_channel (EmpathyTpContactList *list,
234                                    const gchar          *object_path,
235                                    const gchar          *channel_type,
236                                    TpHandleType          handle_type,
237                                    guint                 handle)
238 {
239         EmpathyTpContactListPriv *priv = GET_PRIV (list);
240         TpChannel                *channel;
241
242         /* Only accept server-side contact groups */
243         if (tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) ||
244             handle_type != TP_HANDLE_TYPE_GROUP) {
245                 return;
246         }
247
248         channel = tp_channel_new (priv->connection,
249                                   object_path, channel_type,
250                                   handle_type, handle, NULL);
251
252         /* TpChannel emits initial set of members just before being ready */
253         g_signal_connect (channel, "group-members-changed",
254                           G_CALLBACK (tp_contact_list_group_members_changed_cb),
255                           list);
256
257         /* Give the ref to the callback */
258         tp_channel_call_when_ready (channel,
259                                     tp_contact_list_group_ready_cb,
260                                     list);
261 }
262
263 static void
264 tp_contact_list_group_request_channel_cb (TpConnection *connection,
265                                           const gchar  *object_path,
266                                           const GError *error,
267                                           gpointer      user_data,
268                                           GObject      *list)
269 {
270         /* The new channel will be handled in NewChannel cb. Here we only
271          * handle the error if RequestChannel failed */
272         if (error) {
273                 DEBUG ("Error: %s", error->message);
274                 return;
275         }
276 }
277
278 static void
279 tp_contact_list_group_request_handles_cb (TpConnection *connection,
280                                           const GArray *handles,
281                                           const GError *error,
282                                           gpointer      user_data,
283                                           GObject      *list)
284 {
285         TpHandle channel_handle;
286
287         if (error) {
288                 DEBUG ("Error: %s", error->message);
289                 return;
290         }
291
292         channel_handle = g_array_index (handles, TpHandle, 0);
293         tp_cli_connection_call_request_channel (connection, -1,
294                                                 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
295                                                 TP_HANDLE_TYPE_GROUP,
296                                                 channel_handle,
297                                                 TRUE,
298                                                 tp_contact_list_group_request_channel_cb,
299                                                 NULL, NULL,
300                                                 list);
301 }
302
303 /* This function takes ownership of handles array */
304 static void
305 tp_contact_list_group_add (EmpathyTpContactList *list,
306                            const gchar          *group_name,
307                            GArray               *handles)
308 {
309         EmpathyTpContactListPriv *priv = GET_PRIV (list);
310         TpChannel                *channel;
311         const gchar              *names[] = {group_name, NULL};
312
313         /* Search the channel for that group name */
314         channel = g_hash_table_lookup (priv->groups, group_name);
315         if (channel) {
316                 tp_cli_channel_interface_group_call_add_members (channel, -1,
317                         handles, NULL, NULL, NULL, NULL, NULL);
318                 g_array_free (handles, TRUE);
319                 return;
320         }
321
322         /* That group does not exist yet, we have to:
323          * 1) Request an handle for the group name
324          * 2) Request a channel
325          * 3) When NewChannel is emitted, add handles in members
326          */
327         g_hash_table_insert (priv->add_to_group,
328                              g_strdup (group_name),
329                              handles);
330         tp_cli_connection_call_request_handles (priv->connection, -1,
331                                                 TP_HANDLE_TYPE_GROUP, names,
332                                                 tp_contact_list_group_request_handles_cb,
333                                                 NULL, NULL,
334                                                 G_OBJECT (list));
335 }
336
337 static void
338 tp_contact_list_got_added_members_cb (EmpathyTpContactFactory *factory,
339                                       guint                    n_contacts,
340                                       EmpathyContact * const * contacts,
341                                       guint                    n_failed,
342                                       const TpHandle          *failed,
343                                       const GError            *error,
344                                       gpointer                 user_data,
345                                       GObject                 *list)
346 {
347         EmpathyTpContactListPriv *priv = GET_PRIV (list);
348         guint i;
349
350         if (error) {
351                 DEBUG ("Error: %s", error->message);
352                 return;
353         }
354
355         for (i = 0; i < n_contacts; i++) {
356                 EmpathyContact *contact = contacts[i];
357                 TpHandle handle;
358
359                 handle = empathy_contact_get_handle (contact);
360                 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle)))
361                         continue;
362
363                 /* Add to the list and emit signal */
364                 g_hash_table_insert (priv->members, GUINT_TO_POINTER (handle),
365                                      g_object_ref (contact));
366                 g_signal_emit_by_name (list, "members-changed", contact,
367                                        0, 0, NULL, TRUE);
368
369                 /* This contact is now member, implicitly accept pending. */
370                 if (g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
371                         GArray handles = {(gchar *) &handle, 1};
372
373                         tp_cli_channel_interface_group_call_add_members (priv->publish,
374                                 -1, &handles, NULL, NULL, NULL, NULL, NULL);
375                 }
376         }
377 }
378
379 static void
380 tp_contact_list_got_local_pending_cb (EmpathyTpContactFactory *factory,
381                                       guint                    n_contacts,
382                                       EmpathyContact * const * contacts,
383                                       guint                    n_failed,
384                                       const TpHandle          *failed,
385                                       const GError            *error,
386                                       gpointer                 user_data,
387                                       GObject                 *list)
388 {
389         EmpathyTpContactListPriv *priv = GET_PRIV (list);
390         guint i;
391
392         if (error) {
393                 DEBUG ("Error: %s", error->message);
394                 return;
395         }
396
397         for (i = 0; i < n_contacts; i++) {
398                 EmpathyContact *contact = contacts[i];
399                 TpHandle handle;
400                 const gchar *message;
401                 TpChannelGroupChangeReason reason;
402
403                 handle = empathy_contact_get_handle (contact);
404                 if (g_hash_table_lookup (priv->members, GUINT_TO_POINTER (handle))) {
405                         GArray handles = {(gchar *) &handle, 1};
406
407                         /* This contact is already member, auto accept. */
408                         tp_cli_channel_interface_group_call_add_members (priv->publish,
409                                 -1, &handles, NULL, NULL, NULL, NULL, NULL);
410                 }
411                 else if (tp_channel_group_get_local_pending_info (priv->publish,
412                                                                   handle,
413                                                                   NULL,
414                                                                   &reason,
415                                                                   &message)) {
416                         /* Add contact to pendings */
417                         g_hash_table_insert (priv->pendings, GUINT_TO_POINTER (handle),
418                                              g_object_ref (contact));
419                         g_signal_emit_by_name (list, "pendings-changed", contact,
420                                                contact, reason, message, TRUE);
421                 }
422         }
423 }
424
425 static void
426 tp_contact_list_remove_handle (EmpathyTpContactList *list,
427                                GHashTable *table,
428                                TpHandle handle)
429 {
430         EmpathyTpContactListPriv *priv = GET_PRIV (list);
431         EmpathyContact *contact;
432         const gchar *signal;
433
434         if (table == priv->pendings)
435                 signal = "pendings-changed";
436         else if (table == priv->members)
437                 signal = "members-changed";
438         else
439                 return;
440
441         contact = g_hash_table_lookup (table, GUINT_TO_POINTER (handle));
442         if (contact) {
443                 g_object_ref (contact);
444                 g_hash_table_remove (table, GUINT_TO_POINTER (handle));
445                 g_signal_emit_by_name (list, signal, contact, 0, 0, NULL,
446                                        FALSE);
447                 g_object_unref (contact);
448         }
449 }
450
451 static void
452 tp_contact_list_publish_group_members_changed_cb (TpChannel     *channel,
453                                                   gchar         *message,
454                                                   GArray        *added,
455                                                   GArray        *removed,
456                                                   GArray        *local_pending,
457                                                   GArray        *remote_pending,
458                                                   TpHandle       actor,
459                                                   TpChannelGroupChangeReason reason,
460                                                   EmpathyTpContactList *list)
461 {
462         EmpathyTpContactListPriv *priv = GET_PRIV (list);
463         guint i;
464
465         /* We now send our presence to those contacts, remove them from pendings */
466         for (i = 0; i < added->len; i++) {
467                 tp_contact_list_remove_handle (list, priv->pendings,
468                         g_array_index (added, TpHandle, i));
469         }
470
471         /* We refuse to send our presence to those contacts, remove from pendings */
472         for (i = 0; i < removed->len; i++) {
473                 tp_contact_list_remove_handle (list, priv->pendings,
474                         g_array_index (added, TpHandle, i));
475         }
476
477         /* Those contacts want our presence, auto accept those that are already
478          * member, otherwise add in pendings. */
479         if (local_pending->len > 0) {
480                 empathy_tp_contact_factory_get_from_handles (priv->factory,
481                         local_pending->len, (TpHandle *) local_pending->data,
482                         tp_contact_list_got_local_pending_cb, NULL, NULL,
483                         G_OBJECT (list));
484         }
485 }
486
487 static void
488 tp_contact_list_publish_request_channel_cb (TpConnection *connection,
489                                             const gchar  *object_path,
490                                             const GError *error,
491                                             gpointer      user_data,
492                                             GObject      *list)
493 {
494         EmpathyTpContactListPriv *priv = GET_PRIV (list);
495
496         if (error) {
497                 DEBUG ("Error: %s", error->message);
498                 return;
499         }
500
501         priv->publish = tp_channel_new (connection, object_path,
502                                         TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
503                                         TP_HANDLE_TYPE_LIST,
504                                         GPOINTER_TO_UINT (user_data),
505                                         NULL);
506
507         /* TpChannel emits initial set of members just before being ready */
508         g_signal_connect (priv->publish, "group-members-changed",
509                           G_CALLBACK (tp_contact_list_publish_group_members_changed_cb),
510                           list);
511 }
512
513 static void
514 tp_contact_list_get_alias_flags_cb (TpConnection *connection,
515                                     guint         flags,
516                                     const GError *error,
517                                     gpointer      user_data,
518                                     GObject      *list)
519 {
520         EmpathyTpContactListPriv *priv = GET_PRIV (list);
521
522         if (error) {
523                 DEBUG ("Error: %s", error->message);
524                 return;
525         }
526
527         if (flags & TP_CONNECTION_ALIAS_FLAG_USER_SET) {
528                 priv->flags |= EMPATHY_CONTACT_LIST_CAN_ALIAS;
529         }
530 }
531
532 static void
533 tp_contact_list_publish_request_handle_cb (TpConnection *connection,
534                                            const GArray *handles,
535                                            const GError *error,
536                                            gpointer      user_data,
537                                            GObject      *list)
538 {
539         TpHandle handle;
540
541         if (error) {
542                 DEBUG ("Error: %s", error->message);
543                 return;
544         }
545
546         handle = g_array_index (handles, TpHandle, 0);
547         tp_cli_connection_call_request_channel (connection, -1,
548                                                 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
549                                                 TP_HANDLE_TYPE_LIST,
550                                                 handle,
551                                                 TRUE,
552                                                 tp_contact_list_publish_request_channel_cb,
553                                                 GUINT_TO_POINTER (handle), NULL,
554                                                 list);
555 }
556
557 static void
558 tp_contact_list_subscribe_group_members_changed_cb (TpChannel     *channel,
559                                                     gchar         *message,
560                                                     GArray        *added,
561                                                     GArray        *removed,
562                                                     GArray        *local_pending,
563                                                     GArray        *remote_pending,
564                                                     guint          actor,
565                                                     guint          reason,
566                                                     EmpathyTpContactList *list)
567 {
568         EmpathyTpContactListPriv *priv = GET_PRIV (list);
569         guint i;
570
571         /* We now get the presence of those contacts, add them to members */
572         if (added->len > 0) {
573                 empathy_tp_contact_factory_get_from_handles (priv->factory,
574                         added->len, (TpHandle *) added->data,
575                         tp_contact_list_got_added_members_cb, NULL, NULL,
576                         G_OBJECT (list));
577         }
578
579         /* Those contacts refuse to send us their presence, remove from members. */
580         for (i = 0; i < removed->len; i++) {
581                 tp_contact_list_remove_handle (list, priv->members,
582                         g_array_index (added, TpHandle, i));
583         }
584
585         /* We want those contacts in our contact list but we don't get their
586          * presence yet. Add to members anyway. */
587         if (remote_pending->len > 0) {
588                 empathy_tp_contact_factory_get_from_handles (priv->factory,
589                         remote_pending->len, (TpHandle *) remote_pending->data,
590                         tp_contact_list_got_added_members_cb, NULL, NULL,
591                         G_OBJECT (list));
592         }
593 }
594
595 static void
596 tp_contact_list_subscribe_request_channel_cb (TpConnection *connection,
597                                               const gchar  *object_path,
598                                               const GError *error,
599                                               gpointer      user_data,
600                                               GObject      *list)
601 {
602         EmpathyTpContactListPriv *priv = GET_PRIV (list);
603
604         if (error) {
605                 DEBUG ("Error: %s", error->message);
606                 return;
607         }
608
609         priv->subscribe = tp_channel_new (connection, object_path,
610                                           TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
611                                           TP_HANDLE_TYPE_LIST,
612                                           GPOINTER_TO_UINT (user_data),
613                                           NULL);
614
615         /* TpChannel emits initial set of members just before being ready */
616         g_signal_connect (priv->subscribe, "group-members-changed",
617                           G_CALLBACK (tp_contact_list_subscribe_group_members_changed_cb),
618                           list);
619 }
620
621 static void
622 tp_contact_list_subscribe_request_handle_cb (TpConnection *connection,
623                                              const GArray *handles,
624                                              const GError *error,
625                                              gpointer      user_data,
626                                              GObject      *list)
627 {
628         TpHandle handle;
629
630         if (error) {
631                 DEBUG ("Error: %s", error->message);
632                 return;
633         }
634
635         handle = g_array_index (handles, TpHandle, 0);
636         tp_cli_connection_call_request_channel (connection, -1,
637                                                 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
638                                                 TP_HANDLE_TYPE_LIST,
639                                                 handle,
640                                                 TRUE,
641                                                 tp_contact_list_subscribe_request_channel_cb,
642                                                 GUINT_TO_POINTER (handle), NULL,
643                                                 list);
644 }
645
646 static void
647 tp_contact_list_new_channel_cb (TpConnection *proxy,
648                                 const gchar  *object_path,
649                                 const gchar  *channel_type,
650                                 guint         handle_type,
651                                 guint         handle,
652                                 gboolean      suppress_handler,
653                                 gpointer      user_data,
654                                 GObject      *list)
655 {
656         tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
657                                            object_path, channel_type,
658                                            handle_type, handle);
659 }
660
661 static void
662 tp_contact_list_list_channels_cb (TpConnection    *connection,
663                                   const GPtrArray *channels,
664                                   const GError    *error,
665                                   gpointer         user_data,
666                                   GObject         *list)
667 {
668         guint i;
669
670         if (error) {
671                 DEBUG ("Error: %s", error->message);
672                 return;
673         }
674
675         for (i = 0; i < channels->len; i++) {
676                 GValueArray  *chan_struct;
677                 const gchar  *object_path;
678                 const gchar  *channel_type;
679                 TpHandleType  handle_type;
680                 guint         handle;
681
682                 chan_struct = g_ptr_array_index (channels, i);
683                 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
684                 channel_type = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
685                 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
686                 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
687
688                 tp_contact_list_group_add_channel (EMPATHY_TP_CONTACT_LIST (list),
689                                                    object_path, channel_type,
690                                                    handle_type, handle);
691         }
692 }
693
694 static void
695 tp_contact_list_finalize (GObject *object)
696 {
697         EmpathyTpContactListPriv *priv;
698         EmpathyTpContactList     *list;
699         GHashTableIter            iter;
700         gpointer                  channel;
701
702         list = EMPATHY_TP_CONTACT_LIST (object);
703         priv = GET_PRIV (list);
704
705         DEBUG ("finalize: %p", object);
706
707         if (priv->subscribe) {
708                 g_object_unref (priv->subscribe);
709         }
710         if (priv->publish) {
711                 g_object_unref (priv->publish);
712         }
713
714         if (priv->connection) {
715                 g_object_unref (priv->connection);
716         }
717
718         if (priv->factory) {
719                 g_object_unref (priv->factory);
720         }
721
722         g_hash_table_iter_init (&iter, priv->groups);
723         while (g_hash_table_iter_next (&iter, NULL, &channel)) {
724                 g_signal_handlers_disconnect_by_func (channel,
725                         tp_contact_list_group_invalidated_cb, list);
726         }
727
728         g_hash_table_destroy (priv->groups);
729         g_hash_table_destroy (priv->members);
730         g_hash_table_destroy (priv->pendings);
731         g_hash_table_destroy (priv->add_to_group);
732
733         G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
734 }
735
736 static void
737 tp_contact_list_constructed (GObject *list)
738 {
739
740         EmpathyTpContactListPriv *priv = GET_PRIV (list);
741         gchar                    *protocol_name = NULL;
742         const gchar              *names[] = {NULL, NULL};
743
744         priv->factory = empathy_tp_contact_factory_dup_singleton (priv->connection);
745
746         /* call GetAliasFlags() */
747         if (tp_proxy_has_interface_by_id (priv->connection,
748                                 TP_IFACE_QUARK_CONNECTION_INTERFACE_ALIASING)) {
749                 tp_cli_connection_interface_aliasing_call_get_alias_flags (
750                                 priv->connection,
751                                 -1,
752                                 tp_contact_list_get_alias_flags_cb,
753                                 NULL, NULL,
754                                 G_OBJECT (list));
755         }
756
757         /* FIXME: lookup RequestableChannelClasses */
758
759         names[0] = "publish";
760         tp_cli_connection_call_request_handles (priv->connection,
761                                                 -1,
762                                                 TP_HANDLE_TYPE_LIST,
763                                                 names,
764                                                 tp_contact_list_publish_request_handle_cb,
765                                                 NULL, NULL,
766                                                 G_OBJECT (list));
767         names[0] = "subscribe";
768         tp_cli_connection_call_request_handles (priv->connection,
769                                                 -1,
770                                                 TP_HANDLE_TYPE_LIST,
771                                                 names,
772                                                 tp_contact_list_subscribe_request_handle_cb,
773                                                 NULL, NULL,
774                                                 G_OBJECT (list));
775
776         tp_cli_connection_call_list_channels (priv->connection, -1,
777                                               tp_contact_list_list_channels_cb,
778                                               NULL, NULL,
779                                               list);
780
781         tp_cli_connection_connect_to_new_channel (priv->connection,
782                                                   tp_contact_list_new_channel_cb,
783                                                   NULL, NULL,
784                                                   list, NULL);
785
786         /* Check for protocols that does not support contact groups. We can
787          * put all contacts into a special group in that case.
788          * FIXME: Default group should be an information in the profile */
789         tp_connection_parse_object_path (priv->connection, &protocol_name, NULL);
790         if (!tp_strdiff (protocol_name, "local-xmpp")) {
791                 priv->protocol_group = _("People nearby");
792         }
793         g_free (protocol_name);
794 }
795
796 static void
797 tp_contact_list_get_property (GObject    *object,
798                               guint       param_id,
799                               GValue     *value,
800                               GParamSpec *pspec)
801 {
802         EmpathyTpContactListPriv *priv = GET_PRIV (object);
803
804         switch (param_id) {
805         case PROP_CONNECTION:
806                 g_value_set_object (value, priv->connection);
807                 break;
808         default:
809                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
810                 break;
811         };
812 }
813
814 static void
815 tp_contact_list_set_property (GObject      *object,
816                               guint         param_id,
817                               const GValue *value,
818                               GParamSpec   *pspec)
819 {
820         EmpathyTpContactListPriv *priv = GET_PRIV (object);
821
822         switch (param_id) {
823         case PROP_CONNECTION:
824                 priv->connection = g_value_dup_object (value);
825                 break;
826         default:
827                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
828                 break;
829         };
830 }
831
832 static void
833 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
834 {
835         GObjectClass *object_class = G_OBJECT_CLASS (klass);
836
837         object_class->finalize = tp_contact_list_finalize;
838         object_class->constructed = tp_contact_list_constructed;
839         object_class->get_property = tp_contact_list_get_property;
840         object_class->set_property = tp_contact_list_set_property;
841
842         g_object_class_install_property (object_class,
843                                          PROP_CONNECTION,
844                                          g_param_spec_object ("connection",
845                                                               "The Connection",
846                                                               "The connection associated with the contact list",
847                                                               TP_TYPE_CONNECTION,
848                                                               G_PARAM_READWRITE |
849                                                               G_PARAM_CONSTRUCT_ONLY));
850
851         g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
852 }
853
854 static void
855 tp_contact_list_array_free (gpointer handles)
856 {
857         g_array_free (handles, TRUE);
858 }
859
860 static void
861 empathy_tp_contact_list_init (EmpathyTpContactList *list)
862 {
863         EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
864                 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
865
866         list->priv = priv;
867
868         /* Map group's name to group's TpChannel. The group name string is owned
869          * by the TpChannel object */
870         priv->groups = g_hash_table_new_full (g_str_hash, g_str_equal,
871                                               NULL,
872                                               (GDestroyNotify) g_object_unref);
873
874         /* Map contact's handle to EmpathyContact object */
875         priv->members = g_hash_table_new_full (g_direct_hash, g_direct_equal,
876                                                NULL,
877                                                (GDestroyNotify) g_object_unref);
878
879         /* Map contact's handle to EmpathyContact object */
880         priv->pendings = g_hash_table_new_full (g_direct_hash, g_direct_equal,
881                                                 NULL,
882                                                 (GDestroyNotify) g_object_unref);
883
884         /* Map group's name to GArray of handle */
885         priv->add_to_group = g_hash_table_new_full (g_str_hash, g_str_equal,
886                                                     g_free,
887                                                     tp_contact_list_array_free);
888 }
889
890 EmpathyTpContactList *
891 empathy_tp_contact_list_new (TpConnection *connection)
892 {
893         return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
894                              "connection", connection,
895                              NULL);
896 }
897
898 TpConnection *
899 empathy_tp_contact_list_get_connection (EmpathyTpContactList *list)
900 {
901         EmpathyTpContactListPriv *priv;
902
903         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
904
905         priv = GET_PRIV (list);
906
907         return priv->connection;
908 }
909
910 static void
911 tp_contact_list_add (EmpathyContactList *list,
912                      EmpathyContact     *contact,
913                      const gchar        *message)
914 {
915         EmpathyTpContactListPriv *priv = GET_PRIV (list);
916         TpHandle handle;
917         GArray handles = {(gchar *) &handle, 1};
918
919         handle = empathy_contact_get_handle (contact);
920         if (priv->subscribe) {
921                 tp_cli_channel_interface_group_call_add_members (priv->subscribe,
922                         -1, &handles, message, NULL, NULL, NULL, NULL);
923         }
924         if (priv->publish &&
925             g_hash_table_lookup (priv->pendings, GUINT_TO_POINTER (handle))) {
926                 tp_cli_channel_interface_group_call_add_members (priv->publish,
927                         -1, &handles, message, NULL, NULL, NULL, NULL);
928         }
929 }
930
931 static void
932 tp_contact_list_remove (EmpathyContactList *list,
933                         EmpathyContact     *contact,
934                         const gchar        *message)
935 {
936         EmpathyTpContactListPriv *priv = GET_PRIV (list);
937         TpHandle handle;
938         GArray handles = {(gchar *) &handle, 1};
939
940         handle = empathy_contact_get_handle (contact);
941         if (priv->subscribe) {
942                 tp_cli_channel_interface_group_call_remove_members (priv->subscribe,
943                         -1, &handles, message, NULL, NULL, NULL, NULL);
944         }
945         if (priv->publish) {
946                 tp_cli_channel_interface_group_call_remove_members (priv->publish,
947                         -1, &handles, message, NULL, NULL, NULL, NULL);
948         }
949 }
950
951 static GList *
952 tp_contact_list_get_members (EmpathyContactList *list)
953 {
954         EmpathyTpContactListPriv *priv = GET_PRIV (list);
955         GList *ret;
956
957         ret = g_hash_table_get_values (priv->members);
958         g_list_foreach (ret, (GFunc) g_object_ref, NULL);
959         return ret;
960 }
961
962 static GList *
963 tp_contact_list_get_pendings (EmpathyContactList *list)
964 {
965         EmpathyTpContactListPriv *priv = GET_PRIV (list);
966         GList *ret;
967
968         ret = g_hash_table_get_values (priv->pendings);
969         g_list_foreach (ret, (GFunc) g_object_ref, NULL);
970         return ret;
971 }
972
973 static GList *
974 tp_contact_list_get_all_groups (EmpathyContactList *list)
975 {
976         EmpathyTpContactListPriv *priv = GET_PRIV (list);
977         GList                    *ret, *l;
978
979         ret = g_hash_table_get_keys (priv->groups);
980         for (l = ret; l; l = l->next) {
981                 l->data = g_strdup (l->data);
982         }
983
984         if (priv->protocol_group) {
985                 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
986         }
987
988         return ret;
989 }
990
991 static GList *
992 tp_contact_list_get_groups (EmpathyContactList *list,
993                             EmpathyContact     *contact)
994 {
995         EmpathyTpContactListPriv  *priv = GET_PRIV (list);
996         GList                     *ret = NULL;
997         GHashTableIter             iter;
998         gpointer                   group_name;
999         gpointer                   channel;
1000         TpHandle                   handle;
1001
1002         handle = empathy_contact_get_handle (contact);
1003         g_hash_table_iter_init (&iter, priv->groups);
1004         while (g_hash_table_iter_next (&iter, &group_name, &channel)) {
1005                 const TpIntSet *members;
1006
1007                 members = tp_channel_group_get_members (channel);
1008                 if (tp_intset_is_member (members, handle)) {
1009                         ret = g_list_prepend (ret, g_strdup (group_name));
1010                 }
1011         }
1012
1013         if (priv->protocol_group) {
1014                 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
1015         }
1016
1017         return ret;
1018 }
1019
1020 static void
1021 tp_contact_list_add_to_group (EmpathyContactList *list,
1022                               EmpathyContact     *contact,
1023                               const gchar        *group_name)
1024 {
1025         TpHandle handle;
1026         GArray *handles;
1027
1028         handle = empathy_contact_get_handle (contact);
1029         handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
1030         g_array_append_val (handles, handle);
1031         tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1032                                    group_name, handles);
1033 }
1034
1035 static void
1036 tp_contact_list_remove_from_group (EmpathyContactList *list,
1037                                    EmpathyContact     *contact,
1038                                    const gchar        *group_name)
1039 {
1040         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1041         TpChannel                *channel;
1042         TpHandle                  handle;
1043         GArray                    handles = {(gchar *) &handle, 1};
1044
1045         channel = g_hash_table_lookup (priv->groups, group_name);
1046         if (channel == NULL) {
1047                 return;
1048         }
1049
1050         handle = empathy_contact_get_handle (contact);
1051         DEBUG ("remove contact %s (%d) from group %s",
1052                 empathy_contact_get_id (contact), handle, group_name);
1053
1054         tp_cli_channel_interface_group_call_remove_members (channel, -1,
1055                 &handles, NULL, NULL, NULL, NULL, NULL);
1056 }
1057
1058 static void
1059 tp_contact_list_rename_group (EmpathyContactList *list,
1060                               const gchar        *old_group_name,
1061                               const gchar        *new_group_name)
1062 {
1063         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1064         TpChannel                *channel;
1065         const TpIntSet           *members;
1066         GArray                   *handles;
1067
1068         channel = g_hash_table_lookup (priv->groups, old_group_name);
1069         if (channel == NULL) {
1070                 return;
1071         }
1072
1073         DEBUG ("rename group %s to %s", old_group_name, new_group_name);
1074
1075         /* Remove all members and close the old channel */
1076         members = tp_channel_group_get_members (channel);
1077         handles = tp_intset_to_array (members);
1078         tp_cli_channel_interface_group_call_remove_members (channel, -1,
1079                 handles, NULL, NULL, NULL, NULL, NULL);
1080         tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1081
1082         tp_contact_list_group_add (EMPATHY_TP_CONTACT_LIST (list),
1083                                    new_group_name, handles);
1084 }
1085
1086 static void
1087 tp_contact_list_remove_group (EmpathyContactList *list,
1088                               const gchar *group_name)
1089 {
1090         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1091         TpChannel                *channel;
1092         const TpIntSet           *members;
1093         GArray                   *handles;
1094
1095         channel = g_hash_table_lookup (priv->groups, group_name);
1096         if (channel == NULL) {
1097                 return;
1098         }
1099
1100         DEBUG ("remove group %s", group_name);
1101
1102         /* Remove all members and close the channel */
1103         members = tp_channel_group_get_members (channel);
1104         handles = tp_intset_to_array (members);
1105         tp_cli_channel_interface_group_call_remove_members (channel, -1,
1106                 handles, NULL, NULL, NULL, NULL, NULL);
1107         tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
1108         g_array_free (handles, TRUE);
1109 }
1110
1111 static EmpathyContactListFlags
1112 tp_contact_list_get_flags (EmpathyContactList *list)
1113 {
1114         EmpathyTpContactListPriv *priv;
1115         EmpathyContactListFlags flags;
1116         TpChannelGroupFlags group_flags;
1117
1118         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1119
1120         priv = GET_PRIV (list);
1121         flags = priv->flags;
1122
1123         group_flags = tp_channel_group_get_flags (priv->subscribe);
1124
1125         if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) {
1126                 flags |= EMPATHY_CONTACT_LIST_CAN_ADD;
1127         }
1128
1129         if (group_flags & TP_CHANNEL_GROUP_FLAG_CAN_REMOVE) {
1130                 flags |= EMPATHY_CONTACT_LIST_CAN_REMOVE;
1131         }
1132
1133         return flags;
1134 }
1135
1136 static void
1137 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1138 {
1139         iface->add               = tp_contact_list_add;
1140         iface->remove            = tp_contact_list_remove;
1141         iface->get_members       = tp_contact_list_get_members;
1142         iface->get_pendings      = tp_contact_list_get_pendings;
1143         iface->get_all_groups    = tp_contact_list_get_all_groups;
1144         iface->get_groups        = tp_contact_list_get_groups;
1145         iface->add_to_group      = tp_contact_list_add_to_group;
1146         iface->remove_from_group = tp_contact_list_remove_from_group;
1147         iface->rename_group      = tp_contact_list_rename_group;
1148         iface->remove_group      = tp_contact_list_remove_group;
1149         iface->get_flags         = tp_contact_list_get_flags;
1150 }
1151
1152 void
1153 empathy_tp_contact_list_remove_all (EmpathyTpContactList *list)
1154 {
1155         EmpathyTpContactListPriv *priv = GET_PRIV (list);
1156         GHashTableIter            iter;
1157         gpointer                  contact;
1158
1159         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1160
1161         /* Remove all contacts */
1162         g_hash_table_iter_init (&iter, priv->members);
1163         while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1164                 g_signal_emit_by_name (list, "members-changed", contact,
1165                                        NULL, 0, NULL,
1166                                        FALSE);
1167         }
1168         g_hash_table_remove_all (priv->members);
1169
1170         g_hash_table_iter_init (&iter, priv->pendings);
1171         while (g_hash_table_iter_next (&iter, NULL, &contact)) {
1172                 g_signal_emit_by_name (list, "pendings-changed", contact,
1173                                        NULL, 0, NULL,
1174                                        FALSE);
1175         }
1176         g_hash_table_remove_all (priv->pendings);
1177 }
1178