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