]> git.0d.be Git - empathy.git/blob - libempathy/empathy-contact-list.c
c3d3171d4e3655cec8e8c0a876d90d08d42ad298
[empathy.git] / libempathy / empathy-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 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program 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  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  * 
21  * Authors: Xavier Claessens <xclaesse@gmail.com>
22  */
23
24 #include <config.h>
25
26 #include <string.h>
27
28 #include <libtelepathy/tp-conn.h>
29 #include <libtelepathy/tp-chan.h>
30 #include <libtelepathy/tp-helpers.h>
31 #include <libtelepathy/tp-chan-type-contact-list-gen.h>
32 #include <libtelepathy/tp-conn-iface-aliasing-gen.h>
33 #include <libtelepathy/tp-conn-iface-presence-gen.h>
34 #include <libtelepathy/tp-conn-iface-avatars-gen.h>
35
36 #include "empathy-contact-list.h"
37 #include "gossip-debug.h"
38 #include "gossip-telepathy-group.h"
39
40 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
41                        EMPATHY_TYPE_CONTACT_LIST, EmpathyContactListPriv))
42
43 #define DEBUG_DOMAIN "ContactList"
44 #define MAX_AVATAR_REQUESTS 10
45
46 struct _EmpathyContactListPriv {
47         TpConn               *tp_conn;
48         McAccount            *account;
49         GossipContact        *own_contact;
50
51         GossipTelepathyGroup *known;
52         GossipTelepathyGroup *publish;
53         GossipTelepathyGroup *subscribe;
54
55         GHashTable           *groups;
56         GHashTable           *contacts;
57
58         DBusGProxy           *aliasing_iface;
59         DBusGProxy           *avatars_iface;
60         DBusGProxy           *presence_iface;
61
62         GList                *avatar_requests_queue;
63 };
64
65 typedef enum {
66         CONTACT_LIST_TYPE_KNOWN,
67         CONTACT_LIST_TYPE_PUBLISH,
68         CONTACT_LIST_TYPE_SUBSCRIBE,
69         CONTACT_LIST_TYPE_UNKNOWN,
70         CONTACT_LIST_TYPE_COUNT
71 } ContactListType;
72
73 typedef struct {
74         guint  handle;
75         GList *new_groups;
76 } ContactListData;
77
78 typedef struct {
79         EmpathyContactList *list;
80         guint                 handle;
81 } ContactListAvatarRequestData;
82
83 typedef struct {
84         EmpathyContactList *list;
85         guint                *handles;
86 } ContactListAliasesRequestData;
87
88 static void                   empathy_contact_list_class_init       (EmpathyContactListClass       *klass);
89 static void                   empathy_contact_list_init             (EmpathyContactList            *list);
90 static void                   contact_list_finalize                 (GObject                       *object);
91 static void                   contact_list_finalize_proxies         (EmpathyContactList            *list);
92 static void                   contact_list_contact_removed_foreach  (guint                          handle,
93                                                                      GossipContact                 *contact,
94                                                                      EmpathyContactList            *list);
95 static void                   contact_list_destroy_cb               (DBusGProxy                    *proxy,
96                                                                      EmpathyContactList            *list);
97 static gboolean               contact_list_find_foreach             (guint                          handle,
98                                                                      GossipContact                 *contact,
99                                                                      gchar                         *id);
100 static void                   contact_list_newchannel_cb            (DBusGProxy                    *proxy,
101                                                                      const gchar                   *object_path,
102                                                                      const gchar                   *channel_type,
103                                                                      TelepathyHandleType            handle_type,
104                                                                      guint                          channel_handle,
105                                                                      gboolean                       suppress_handle,
106                                                                      EmpathyContactList            *list);
107 static ContactListType        contact_list_get_type                 (EmpathyContactList            *list,
108                                                                      TpChan                        *list_chan);
109 static void                   contact_list_contact_added_cb         (GossipTelepathyGroup          *group,
110                                                                      GArray                        *handles,
111                                                                      guint                          actor_handle,
112                                                                      guint                          reason,
113                                                                      const gchar                   *message,
114                                                                      EmpathyContactList            *list);
115 static void                   contact_list_contact_removed_cb       (GossipTelepathyGroup          *group,
116                                                                      GArray                        *handles,
117                                                                      guint                          actor_handle,
118                                                                      guint                          reason,
119                                                                      const gchar                   *message,
120                                                                      EmpathyContactList            *list);
121 static void                   contact_list_local_pending_cb         (GossipTelepathyGroup          *group,
122                                                                      GArray                        *handles,
123                                                                      guint                          actor_handle,
124                                                                      guint                          reason,
125                                                                      const gchar                   *message,
126                                                                      EmpathyContactList            *list);
127 static void                   contact_list_groups_updated_cb        (GossipContact                 *contact,
128                                                                      GParamSpec                    *param,
129                                                                      EmpathyContactList            *list);
130 static void                   contact_list_subscription_updated_cb  (GossipContact                 *contact,
131                                                                      GParamSpec                    *param,
132                                                                      EmpathyContactList            *list);
133 static void                   contact_list_name_updated_cb          (GossipContact                 *contact,
134                                                                      GParamSpec                    *param,
135                                                                      EmpathyContactList            *list);
136 static void                   contact_list_update_groups_foreach    (gchar                         *object_path,
137                                                                      GossipTelepathyGroup          *group,
138                                                                      ContactListData               *data);
139 static GossipTelepathyGroup * contact_list_get_group                (EmpathyContactList            *list,
140                                                                      const gchar                   *name);
141 static gboolean               contact_list_find_group               (gchar                         *key,
142                                                                      GossipTelepathyGroup          *group,
143                                                                      gchar                         *group_name);
144 static void                   contact_list_get_groups_foreach       (gchar                         *key,
145                                                                      GossipTelepathyGroup          *group,
146                                                                      GList                        **groups);
147 static void                   contact_list_group_channel_closed_cb  (TpChan                        *channel,
148                                                                      EmpathyContactList            *list);
149 static void                   contact_list_group_members_added_cb   (GossipTelepathyGroup          *group,
150                                                                      GArray                        *members,
151                                                                      guint                          actor_handle,
152                                                                      guint                          reason,
153                                                                      const gchar                   *message,
154                                                                      EmpathyContactList            *list);
155 static void                   contact_list_group_members_removed_cb (GossipTelepathyGroup          *group,
156                                                                      GArray                        *members,
157                                                                      guint                          actor_handle,
158                                                                      guint                          reason,
159                                                                      const gchar                   *message,
160                                                                      EmpathyContactList            *list);
161 static void                   contact_list_get_contacts_foreach     (guint                          handle,
162                                                                      GossipContact                 *contact,
163                                                                      GList                        **contacts);
164 static void                   contact_list_get_info                 (EmpathyContactList            *list,
165                                                                      GArray                        *handles);
166 static void                   contact_list_request_avatar           (EmpathyContactList            *list,
167                                                                      guint                          handle);
168 static void                   contact_list_start_avatar_requests    (EmpathyContactList            *list);
169 static void                   contact_list_avatar_update_cb         (DBusGProxy                    *proxy,
170                                                                      guint                          handle,
171                                                                      gchar                         *new_token,
172                                                                      EmpathyContactList            *list);
173 static void                   contact_list_request_avatar_cb        (DBusGProxy                    *proxy,
174                                                                      GArray                        *avatar_data,
175                                                                      gchar                         *mime_type,
176                                                                      GError                        *error,
177                                                                      ContactListAvatarRequestData  *data);
178 static void                   contact_list_aliases_update_cb        (DBusGProxy                    *proxy,
179                                                                      GPtrArray                     *handlers,
180                                                                      EmpathyContactList            *list);
181 static void                   contact_list_request_aliases_cb       (DBusGProxy                    *proxy,
182                                                                      gchar                        **contact_names,
183                                                                      GError                        *error,
184                                                                      ContactListAliasesRequestData *data);
185 static void                   contact_list_presence_update_cb       (DBusGProxy                    *proxy,
186                                                                      GHashTable                    *handle_table,
187                                                                      EmpathyContactList            *list);
188 static void                   contact_list_parse_presence_foreach   (guint                          handle,
189                                                                      GValueArray                   *presence_struct,
190                                                                      EmpathyContactList            *list);
191 static void                   contact_list_presences_table_foreach  (const gchar                   *state_str,
192                                                                      GHashTable                    *presences_table,
193                                                                      GossipPresence               **presence);
194
195 enum {
196         CONTACT_ADDED,
197         CONTACT_REMOVED,
198         DESTROY,
199         LAST_SIGNAL
200 };
201
202 static guint signals[LAST_SIGNAL];
203 static guint n_avatar_requests = 0;
204
205 G_DEFINE_TYPE (EmpathyContactList, empathy_contact_list, G_TYPE_OBJECT);
206
207 static void
208 empathy_contact_list_class_init (EmpathyContactListClass *klass)
209 {
210         GObjectClass *object_class = G_OBJECT_CLASS (klass);
211
212         object_class->finalize = contact_list_finalize;
213
214         signals[CONTACT_ADDED] =
215                 g_signal_new ("contact-added",
216                               G_TYPE_FROM_CLASS (klass),
217                               G_SIGNAL_RUN_LAST,
218                               0,
219                               NULL, NULL,
220                               g_cclosure_marshal_VOID__OBJECT,
221                               G_TYPE_NONE,
222                               1, GOSSIP_TYPE_CONTACT);
223
224         signals[CONTACT_REMOVED] =
225                 g_signal_new ("contact-removed",
226                               G_TYPE_FROM_CLASS (klass),
227                               G_SIGNAL_RUN_LAST,
228                               0,
229                               NULL, NULL,
230                               g_cclosure_marshal_VOID__OBJECT,
231                               G_TYPE_NONE,
232                               1, GOSSIP_TYPE_CONTACT);
233
234         signals[DESTROY] =
235                 g_signal_new ("destroy",
236                               G_TYPE_FROM_CLASS (klass),
237                               G_SIGNAL_RUN_LAST,
238                               0,
239                               NULL, NULL,
240                               g_cclosure_marshal_VOID__VOID,
241                               G_TYPE_NONE,
242                               0);
243
244         g_type_class_add_private (object_class, sizeof (EmpathyContactListPriv));
245 }
246
247 static void
248 empathy_contact_list_init (EmpathyContactList *list)
249 {
250         EmpathyContactListPriv *priv;
251
252         priv = GET_PRIV (list);
253
254         priv->groups = g_hash_table_new_full (g_str_hash,
255                                               g_str_equal,
256                                               (GDestroyNotify) g_free,
257                                               (GDestroyNotify) g_object_unref);
258         priv->contacts = g_hash_table_new_full (g_direct_hash,
259                                                 g_direct_equal,
260                                                 NULL,
261                                                 (GDestroyNotify) g_object_unref);
262 }
263
264
265 static void
266 contact_list_finalize (GObject *object)
267 {
268         EmpathyContactListPriv *priv;
269         EmpathyContactList     *list;
270
271         list = EMPATHY_CONTACT_LIST (object);
272         priv = GET_PRIV (list);
273
274         gossip_debug (DEBUG_DOMAIN, "finalize: %p", object);
275
276         if (priv->tp_conn) {
277                 contact_list_finalize_proxies (list);
278                 g_object_unref (priv->tp_conn);
279         }
280
281         if (priv->known) {
282                 g_object_unref (priv->known);
283         }
284
285         if (priv->subscribe) {
286                 g_object_unref (priv->subscribe);
287         }
288
289         if (priv->publish) {
290                 g_object_unref (priv->publish);
291         }
292
293         g_object_unref (priv->account);
294         g_object_unref (priv->own_contact);
295         g_hash_table_destroy (priv->groups);
296         g_hash_table_destroy (priv->contacts);
297
298         G_OBJECT_CLASS (empathy_contact_list_parent_class)->finalize (object);
299 }
300
301 EmpathyContactList *
302 empathy_contact_list_new (McAccount *account)
303 {
304         EmpathyContactListPriv *priv;
305         EmpathyContactList     *list;
306         MissionControl         *mc;
307         TpConn                 *tp_conn;
308         guint                   handle;
309         GError                 *error = NULL;
310
311         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
312
313         mc = mission_control_new (tp_get_bus ());
314
315         if (mission_control_get_connection_status (mc, account, NULL) != 0) {
316                 /* The account is not connected, nothing to do. */
317                 return NULL;
318         }
319
320         tp_conn = mission_control_get_connection (mc, account, NULL);
321         g_object_unref (mc);
322         g_return_val_if_fail (tp_conn != NULL, NULL);
323
324         list = g_object_new (EMPATHY_TYPE_CONTACT_LIST, NULL);
325         priv = GET_PRIV (list);
326
327         priv->tp_conn = tp_conn;
328         priv->account = g_object_ref (account);
329
330         g_signal_connect (priv->tp_conn, "destroy",
331                           G_CALLBACK (contact_list_destroy_cb),
332                           list);
333
334         priv->aliasing_iface = tp_conn_get_interface (priv->tp_conn,
335                                                       TELEPATHY_CONN_IFACE_ALIASING_QUARK);
336         priv->avatars_iface = tp_conn_get_interface (priv->tp_conn,
337                                                      TELEPATHY_CONN_IFACE_AVATARS_QUARK);
338         priv->presence_iface = tp_conn_get_interface (priv->tp_conn,
339                                                       TELEPATHY_CONN_IFACE_PRESENCE_QUARK);
340
341         if (priv->aliasing_iface) {
342                 dbus_g_proxy_connect_signal (priv->aliasing_iface,
343                                              "AliasesChanged",
344                                              G_CALLBACK (contact_list_aliases_update_cb),
345                                              list, NULL);
346         }
347
348         if (priv->avatars_iface) {
349                 dbus_g_proxy_connect_signal (priv->avatars_iface,
350                                              "AvatarUpdated",
351                                              G_CALLBACK (contact_list_avatar_update_cb),
352                                              list, NULL);
353         }
354
355         if (priv->presence_iface) {
356                 dbus_g_proxy_connect_signal (priv->presence_iface,
357                                              "PresenceUpdate",
358                                              G_CALLBACK (contact_list_presence_update_cb),
359                                              list, NULL);
360         }
361
362         /* Get our own handle and contact */
363         if (!tp_conn_get_self_handle (DBUS_G_PROXY (priv->tp_conn),
364                                       &handle, &error)) {
365                 gossip_debug (DEBUG_DOMAIN, "GetSelfHandle Error: %s",
366                               error ? error->message : "No error given");
367                 g_clear_error (&error);
368         } else {
369                 priv->own_contact = empathy_contact_list_get_from_handle (list, handle);
370         }
371
372         return list;
373 }
374
375 void
376 empathy_contact_list_setup (EmpathyContactList *list)
377 {
378         EmpathyContactListPriv *priv;
379         GPtrArray                *channels;
380         GError                   *error = NULL;
381         guint                     i;
382
383         g_return_if_fail (EMPATHY_IS_CONTACT_LIST (list));
384
385         priv = GET_PRIV (list);
386
387         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
388                                      G_CALLBACK (contact_list_newchannel_cb),
389                                      list, NULL);
390
391         /* Get existing channels */
392         if (!tp_conn_list_channels (DBUS_G_PROXY (priv->tp_conn),
393                                     &channels,
394                                     &error)) {
395                 gossip_debug (DEBUG_DOMAIN,
396                               "Failed to get list of open channels: %s",
397                               error ? error->message : "No error given");
398                 g_clear_error (&error);
399                 return;
400         }
401
402         for (i = 0; channels->len > i; i++) {
403                 GValueArray         *chan_struct;
404                 const gchar         *object_path;
405                 const gchar         *chan_iface;
406                 TelepathyHandleType  handle_type;
407                 guint                handle;
408
409                 chan_struct = g_ptr_array_index (channels, i);
410                 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
411                 chan_iface = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
412                 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
413                 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
414
415                 contact_list_newchannel_cb (DBUS_G_PROXY (priv->tp_conn),
416                                             object_path, chan_iface,
417                                             handle_type, handle,
418                                             FALSE, list);
419
420                 g_value_array_free (chan_struct);
421         }
422
423         g_ptr_array_free (channels, TRUE);
424 }
425
426 McAccount *
427 empathy_contact_list_get_account (EmpathyContactList *list)
428 {
429         EmpathyContactListPriv *priv;
430
431         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
432
433         priv = GET_PRIV (list);
434
435         return priv->account;
436 }
437
438 GossipContact *
439 empathy_contact_list_get_own (EmpathyContactList *list)
440 {
441         EmpathyContactListPriv *priv;
442
443         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
444
445         priv = GET_PRIV (list);
446         
447         return priv->own_contact;
448 }
449
450 GossipContact *
451 empathy_contact_list_find (EmpathyContactList *list,
452                            const gchar        *id)
453 {
454         EmpathyContactListPriv *priv;
455         GossipContact            *contact;
456
457         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
458
459         priv = GET_PRIV (list);
460
461         contact = g_hash_table_find (priv->contacts,
462                                      (GHRFunc) contact_list_find_foreach,
463                                      (gchar*) id);
464
465         return NULL;
466 }
467
468 void
469 empathy_contact_list_add (EmpathyContactList *list,
470                           guint               handle,
471                           const gchar        *message)
472 {
473         EmpathyContactListPriv *priv;
474
475         g_return_if_fail (EMPATHY_IS_CONTACT_LIST (list));
476
477         priv = GET_PRIV (list);
478
479         gossip_telepathy_group_add_member (priv->subscribe, handle, message);
480 }
481
482 void
483 empathy_contact_list_remove (EmpathyContactList *list,
484                              guint               handle)
485 {
486         EmpathyContactListPriv *priv;
487
488         g_return_if_fail (EMPATHY_IS_CONTACT_LIST (list));
489
490         priv = GET_PRIV (list);
491
492         gossip_telepathy_group_remove_member (priv->subscribe, handle, "");
493         gossip_telepathy_group_remove_member (priv->publish, handle, "");
494         gossip_telepathy_group_remove_member (priv->known, handle, "");
495 }
496
497 GossipContact *
498 empathy_contact_list_get_from_id (EmpathyContactList *list,
499                                   const gchar        *id)
500 {
501         EmpathyContactListPriv *priv;
502         GossipContact          *contact;
503         const gchar            *contact_ids[] = {id, NULL};
504         GArray                 *handles;
505         guint                   handle;
506         GError                 *error = NULL;
507
508         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
509         g_return_val_if_fail (id != NULL, NULL);
510         
511         priv = GET_PRIV (list);
512
513         contact = empathy_contact_list_find (list, id);
514         if (contact) {
515                 return contact;
516         }
517
518         /* The id is unknown, requests a new handle */
519         if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
520                                       TP_HANDLE_TYPE_CONTACT,
521                                       contact_ids,
522                                       &handles, &error)) {
523                 gossip_debug (DEBUG_DOMAIN, 
524                               "RequestHandle for %s failed: %s", id,
525                               error ? error->message : "No error given");
526                 g_clear_error (&error);
527                 return 0;
528         }
529
530         handle = g_array_index(handles, guint, 0);
531         g_array_free (handles, TRUE);
532
533         return empathy_contact_list_get_from_handle (list, handle);
534 }
535
536 GossipContact *
537 empathy_contact_list_get_from_handle (EmpathyContactList *list,
538                                       guint               handle)
539 {
540         GossipContact *contact;
541         GArray        *handles;
542         GList         *contacts;
543
544         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
545
546         handles = g_array_new (FALSE, FALSE, sizeof (guint));
547         g_array_append_val (handles, handle);
548
549         contacts = empathy_contact_list_get_from_handles (list, handles);
550         g_array_free (handles, TRUE);
551
552         if (!contacts) {
553                 return NULL;
554         }
555
556         contact = contacts->data;
557         g_list_free (contacts);
558
559         return contact;
560 }
561
562 GList *
563 empathy_contact_list_get_from_handles (EmpathyContactList *list,
564                                        GArray             *handles)
565 {
566         EmpathyContactListPriv  *priv;
567         gchar                  **handles_names;
568         gchar                  **id;
569         GArray                  *new_handles;
570         GList                   *contacts = NULL;
571         guint                    i;
572         GError                  *error = NULL;
573
574         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
575         g_return_val_if_fail (handles != NULL, NULL);
576
577         priv = GET_PRIV (list);
578
579         /* Search all handles we already have */
580         new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
581         for (i = 0; i < handles->len; i++) {
582                 GossipContact *contact;
583                 guint          handle;
584
585                 handle = g_array_index (handles, guint, i);
586                 contact = g_hash_table_lookup (priv->contacts,
587                                                GUINT_TO_POINTER (handle));
588
589                 if (contact) {
590                         contacts = g_list_prepend (contacts,
591                                                    g_object_ref (contact));
592                 } else {
593                         g_array_append_val (new_handles, handle);
594                 }
595         }
596
597         if (new_handles->len == 0) {
598                 return contacts;
599         }
600
601         /* Holds all handles we don't have yet.
602          * FIXME: We should release them at some point. */
603         if (!tp_conn_hold_handles (DBUS_G_PROXY (priv->tp_conn),
604                                    TP_HANDLE_TYPE_CONTACT,
605                                    new_handles, &error)) {
606                 gossip_debug (DEBUG_DOMAIN, 
607                               "HoldHandles Error: %s",
608                               error ? error->message : "No error given");
609                 g_clear_error (&error);
610                 g_array_free (new_handles, TRUE);
611                 return contacts;
612         }
613
614         /* Get the IDs of all new handles */
615         if (!tp_conn_inspect_handles (DBUS_G_PROXY (priv->tp_conn),
616                                       TP_HANDLE_TYPE_CONTACT,
617                                       new_handles,
618                                       &handles_names,
619                                       &error)) {
620                 gossip_debug (DEBUG_DOMAIN, 
621                               "InspectHandle Error: %s",
622                               error ? error->message : "No error given");
623                 g_clear_error (&error);
624                 g_array_free (new_handles, TRUE);
625                 return contacts;
626         }
627
628         /* Create contact objects */
629         for (i = 0, id = handles_names; *id && i < new_handles->len; id++, i++) {
630                 GossipContact *contact;
631                 guint          handle;
632
633                 handle = g_array_index (new_handles, guint, i);
634                 contact = g_object_new (GOSSIP_TYPE_CONTACT,
635                                         "account", priv->account,
636                                         "id", *id,
637                                         "handle", handle,
638                                         NULL);
639
640                 g_signal_connect (contact, "notify::groups",
641                                   G_CALLBACK (contact_list_groups_updated_cb),
642                                   list);
643                 g_signal_connect (contact, "notify::subscription",
644                                   G_CALLBACK (contact_list_subscription_updated_cb),
645                                   list);
646                 g_signal_connect (contact, "notify::name",
647                                   G_CALLBACK (contact_list_name_updated_cb),
648                                   list);
649
650                 gossip_debug (DEBUG_DOMAIN, "new contact created: %s (%d)",
651                               *id, handle);
652
653                 g_hash_table_insert (priv->contacts,
654                                      GUINT_TO_POINTER (handle),
655                                      contact);
656
657                 contacts = g_list_prepend (contacts, g_object_ref (contact));
658         }
659
660         contact_list_get_info (list, new_handles);
661
662         g_array_free (new_handles, TRUE);
663         g_strfreev (handles_names);
664
665         return contacts;
666 }
667
668 void
669 empathy_contact_list_rename_group (EmpathyContactList *list,
670                                    const gchar        *old_group,
671                                    const gchar        *new_group)
672 {
673         EmpathyContactListPriv *priv;
674         GossipTelepathyGroup   *group;
675         GArray                 *members;
676
677         g_return_if_fail (EMPATHY_IS_CONTACT_LIST (list));
678         g_return_if_fail (old_group != NULL);
679         g_return_if_fail (new_group != NULL);
680
681         priv = GET_PRIV (list);
682
683         group = g_hash_table_find (priv->groups,
684                                    (GHRFunc) contact_list_find_group,
685                                    (gchar*) old_group);
686         if (!group) {
687                 /* The group doesn't exists on this account */
688                 return;
689         }
690
691         gossip_debug (DEBUG_DOMAIN, "rename group %s to %s", group, new_group);
692
693         /* Remove all members from the old group */
694         members = gossip_telepathy_group_get_members (group);
695         gossip_telepathy_group_remove_members (group, members, "");
696         contact_list_group_members_removed_cb (group, members, 
697                                                0, 
698                                                TP_CHANNEL_GROUP_CHANGE_REASON_NONE, 
699                                                NULL, list);
700         g_hash_table_remove (priv->groups,
701                              gossip_telepathy_group_get_object_path (group));
702
703         /* Add all members to the new group */
704         group = contact_list_get_group (list, new_group);
705         if (group) {
706                 gossip_telepathy_group_add_members (group, members, "");
707         }
708 }
709
710 GList *
711 empathy_contact_list_get_groups (EmpathyContactList *list)
712 {
713         EmpathyContactListPriv *priv;
714         GList                    *groups = NULL;
715
716         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
717
718         priv = GET_PRIV (list);
719
720         g_hash_table_foreach (priv->groups,
721                               (GHFunc) contact_list_get_groups_foreach,
722                               &groups);
723
724         groups = g_list_sort (groups, (GCompareFunc) strcmp);
725
726         return groups;
727 }
728
729 GList *
730 empathy_contact_list_get_contacts (EmpathyContactList *list)
731 {
732         EmpathyContactListPriv *priv;
733         GList                    *contacts = NULL;
734
735         g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (list), NULL);
736
737         priv = GET_PRIV (list);
738
739         /* FIXME: we should only return contacts that are in the contact list */
740         g_hash_table_foreach (priv->contacts,
741                               (GHFunc) contact_list_get_contacts_foreach,
742                               &contacts);
743
744         return contacts;
745 }
746
747 static void
748 contact_list_finalize_proxies (EmpathyContactList *list)
749 {
750         EmpathyContactListPriv *priv;
751
752         priv = GET_PRIV (list);
753
754         g_signal_handlers_disconnect_by_func (priv->tp_conn,
755                                               contact_list_destroy_cb,
756                                               list);
757         dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
758                                         G_CALLBACK (contact_list_newchannel_cb),
759                                         list);
760
761         if (priv->aliasing_iface) {
762                 dbus_g_proxy_disconnect_signal (priv->aliasing_iface,
763                                                 "AliasesChanged",
764                                                 G_CALLBACK (contact_list_aliases_update_cb),
765                                                 list);
766         }
767
768         if (priv->avatars_iface) {
769                 dbus_g_proxy_disconnect_signal (priv->avatars_iface,
770                                                 "AvatarUpdated",
771                                                 G_CALLBACK (contact_list_avatar_update_cb),
772                                                 list);
773         }
774
775         if (priv->presence_iface) {
776                 dbus_g_proxy_disconnect_signal (priv->presence_iface,
777                                                 "PresenceUpdate",
778                                                 G_CALLBACK (contact_list_presence_update_cb),
779                                                 list);
780         }
781 }
782
783 static void
784 contact_list_destroy_cb (DBusGProxy           *proxy,
785                          EmpathyContactList *list)
786 {
787         EmpathyContactListPriv *priv;
788
789         priv = GET_PRIV (list);
790
791         gossip_debug (DEBUG_DOMAIN, "Connection destroyed. "
792                                     "Account disconnected or CM crashed.");
793
794         /* DBus proxies should NOT be used anymore */
795         g_object_unref (priv->tp_conn);
796         priv->tp_conn = NULL;
797         priv->aliasing_iface = NULL;
798         priv->avatars_iface = NULL;
799         priv->presence_iface = NULL;
800
801         /* Remove all contacts */
802         g_hash_table_foreach (priv->contacts,
803                               (GHFunc) contact_list_contact_removed_foreach,
804                               list);
805         g_hash_table_remove_all (priv->contacts);
806
807         /* Tell the world to not use us anymore */
808         g_signal_emit (list, signals[DESTROY], 0);
809 }
810
811 static void
812 contact_list_contact_removed_foreach (guint                 handle,
813                                       GossipContact        *contact,
814                                       EmpathyContactList *list)
815 {
816         g_signal_handlers_disconnect_by_func (contact,
817                                               contact_list_groups_updated_cb,
818                                               list);
819         g_signal_handlers_disconnect_by_func (contact,
820                                               contact_list_subscription_updated_cb,
821                                               list);
822         g_signal_handlers_disconnect_by_func (contact,
823                                               contact_list_name_updated_cb,
824                                               list);
825
826         g_signal_emit (list, signals[CONTACT_REMOVED], 0, contact);
827 }
828
829 static void
830 contact_list_block_contact (EmpathyContactList *list,
831                             GossipContact        *contact)
832 {
833         g_signal_handlers_block_by_func (contact,
834                                          contact_list_groups_updated_cb,
835                                          list);
836         g_signal_handlers_block_by_func (contact,
837                                          contact_list_subscription_updated_cb,
838                                          list);
839         g_signal_handlers_block_by_func (contact,
840                                          contact_list_name_updated_cb,
841                                          list);
842 }
843
844 static void
845 contact_list_unblock_contact (EmpathyContactList *list,
846                               GossipContact        *contact)
847 {
848         g_signal_handlers_unblock_by_func (contact,
849                                            contact_list_groups_updated_cb,
850                                            list);
851         g_signal_handlers_unblock_by_func (contact,
852                                            contact_list_subscription_updated_cb,
853                                            list);
854         g_signal_handlers_unblock_by_func (contact,
855                                            contact_list_name_updated_cb,
856                                            list);
857 }
858
859 static gboolean
860 contact_list_find_foreach (guint          handle,
861                            GossipContact *contact,
862                            gchar         *id)
863 {
864         if (strcmp (gossip_contact_get_id (contact), id) == 0) {
865                 return TRUE;
866         }
867
868         return FALSE;
869 }
870
871 static void
872 contact_list_newchannel_cb (DBusGProxy          *proxy,
873                             const gchar         *object_path,
874                             const gchar         *channel_type,
875                             TelepathyHandleType  handle_type,
876                             guint                channel_handle,
877                             gboolean             suppress_handle,
878                             EmpathyContactList  *list)
879 {
880         EmpathyContactListPriv *priv;
881         GossipTelepathyGroup   *group;
882         TpChan                 *new_chan;
883         const gchar            *bus_name;
884         GArray                 *members;
885
886         priv = GET_PRIV (list);
887
888         if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) != 0 ||
889             suppress_handle) {
890                 return;
891         }
892
893         bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn));
894         new_chan = tp_chan_new (tp_get_bus (),
895                                 bus_name,
896                                 object_path,
897                                 channel_type, handle_type, channel_handle);
898
899         if (handle_type == TP_HANDLE_TYPE_LIST) {
900                 ContactListType list_type;
901
902                 list_type = contact_list_get_type (list, new_chan);
903                 if (list_type == CONTACT_LIST_TYPE_UNKNOWN) {
904                         gossip_debug (DEBUG_DOMAIN, "Unknown contact list channel");
905                         g_object_unref (new_chan);
906                         return;
907                 }
908
909                 gossip_debug (DEBUG_DOMAIN, "New contact list channel of type: %d",
910                               list_type);
911
912                 group = gossip_telepathy_group_new (new_chan, priv->tp_conn);
913
914                 switch (list_type) {
915                 case CONTACT_LIST_TYPE_KNOWN:
916                         if (priv->known) {
917                                 g_object_unref (priv->known);
918                         }
919                         priv->known = group;
920                         break;
921                 case CONTACT_LIST_TYPE_PUBLISH:
922                         if (priv->publish) {
923                                 g_object_unref (priv->publish);
924                         }
925                         priv->publish = group;
926                         break;
927                 case CONTACT_LIST_TYPE_SUBSCRIBE:
928                         if (priv->subscribe) {
929                                 g_object_unref (priv->subscribe);
930                         }
931                         priv->subscribe = group;
932                         break;
933                 default:
934                         g_assert_not_reached ();
935                 }
936
937                 /* Connect and setup the new contact-list group */
938                 if (list_type == CONTACT_LIST_TYPE_KNOWN ||
939                     list_type == CONTACT_LIST_TYPE_SUBSCRIBE) {
940                         g_signal_connect (group, "members-added",
941                                           G_CALLBACK (contact_list_contact_added_cb),
942                                           list);
943                         g_signal_connect (group, "members-removed",
944                                           G_CALLBACK (contact_list_contact_removed_cb),
945                                           list);
946
947                         members = gossip_telepathy_group_get_members (group);
948                         contact_list_contact_added_cb (group, members, 0,
949                                                        TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
950                                                        NULL, list);
951                         g_array_free (members, TRUE);
952                 }
953                 if (list_type == CONTACT_LIST_TYPE_PUBLISH) {
954                         GPtrArray *info;
955                         GArray    *pending; 
956                         guint      i;
957
958                         g_signal_connect (group, "local-pending",
959                                           G_CALLBACK (contact_list_local_pending_cb),
960                                           list);
961
962                         info = gossip_telepathy_group_get_local_pending_members_with_info (group);
963
964                         if (!info) {
965                                 /* This happens with butterfly because
966                                  * GetLocalPendingMembersWithInfo is not 
967                                  * implemented */
968                                 g_object_unref (new_chan);
969                                 return;
970                         }
971
972                         pending = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
973                         for (i = 0; info->len > i; i++) {
974                                 GValueArray *pending_struct;
975                                 guint        member;
976                                 guint        invitor;
977                                 guint        reason;
978                                 const gchar *message;
979
980                                 pending_struct = g_ptr_array_index (info, i);
981                                 member = g_value_get_uint (g_value_array_get_nth (pending_struct, 0));
982                                 invitor = g_value_get_uint (g_value_array_get_nth (pending_struct, 1));
983                                 reason = g_value_get_uint (g_value_array_get_nth (pending_struct, 2));
984                                 message = g_value_get_string (g_value_array_get_nth (pending_struct, 3));
985
986                                 g_array_insert_val (pending, 0, member);
987
988                                 contact_list_local_pending_cb (group, pending,
989                                                                invitor,
990                                                                reason,
991                                                                message, list);
992
993                                 g_value_array_free (pending_struct);
994                         }
995
996                         g_ptr_array_free (info, TRUE);
997                         g_array_free (pending, TRUE);
998                 }
999         }
1000         else if (handle_type == TP_HANDLE_TYPE_GROUP) {
1001                 const gchar *object_path;
1002
1003                 object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (new_chan));
1004                 if (g_hash_table_lookup (priv->groups, object_path)) {
1005                         g_object_unref (new_chan);
1006                         return;
1007                 }
1008
1009                 group = gossip_telepathy_group_new (new_chan, priv->tp_conn);
1010
1011                 gossip_debug (DEBUG_DOMAIN, "New server-side group channel: %s",
1012                               gossip_telepathy_group_get_name (group));
1013
1014                 dbus_g_proxy_connect_signal (DBUS_G_PROXY (new_chan), "Closed",
1015                                              G_CALLBACK
1016                                              (contact_list_group_channel_closed_cb),
1017                                              list, NULL);
1018
1019                 g_hash_table_insert (priv->groups, g_strdup (object_path), group);
1020                 g_signal_connect (group, "members-added",
1021                                   G_CALLBACK (contact_list_group_members_added_cb),
1022                                   list);
1023                 g_signal_connect (group, "members-removed",
1024                                   G_CALLBACK (contact_list_group_members_removed_cb),
1025                                   list);
1026
1027                 members = gossip_telepathy_group_get_members (group);
1028                 contact_list_group_members_added_cb (group, members, 0,
1029                                                      TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1030                                                      NULL, list);
1031                 g_array_free (members, TRUE);
1032         }
1033
1034         g_object_unref (new_chan);
1035 }
1036
1037 static ContactListType
1038 contact_list_get_type (EmpathyContactList *list,
1039                        TpChan             *list_chan)
1040 {
1041         EmpathyContactListPriv  *priv;
1042         GArray                  *handles;
1043         gchar                  **handle_name;
1044         ContactListType          list_type;
1045         GError                  *error = NULL;
1046
1047         priv = GET_PRIV (list);
1048
1049         handles = g_array_new (FALSE, FALSE, sizeof (guint));
1050         g_array_append_val (handles, list_chan->handle);
1051
1052         if (!tp_conn_inspect_handles (DBUS_G_PROXY (priv->tp_conn),
1053                                       TP_HANDLE_TYPE_LIST,
1054                                       handles,
1055                                       &handle_name,
1056                                       &error)) {
1057                 gossip_debug (DEBUG_DOMAIN, 
1058                               "InspectHandle Error: %s",
1059                               error ? error->message : "No error given");
1060                 g_clear_error (&error);
1061                 g_array_free (handles, TRUE);
1062                 return CONTACT_LIST_TYPE_UNKNOWN;
1063         }
1064
1065         if (strcmp (*handle_name, "subscribe") == 0) {
1066                 list_type = CONTACT_LIST_TYPE_SUBSCRIBE;
1067         } else if (strcmp (*handle_name, "publish") == 0) {
1068                 list_type = CONTACT_LIST_TYPE_PUBLISH;
1069         } else if (strcmp (*handle_name, "known") == 0) {
1070                 list_type = CONTACT_LIST_TYPE_KNOWN;
1071         } else {
1072                 list_type = CONTACT_LIST_TYPE_UNKNOWN;
1073         }
1074
1075         g_strfreev (handle_name);
1076         g_array_free (handles, TRUE);
1077
1078         return list_type;
1079 }
1080
1081 static void
1082 contact_list_contact_added_cb (GossipTelepathyGroup *group,
1083                                GArray               *handles,
1084                                guint                 actor_handle,
1085                                guint                 reason,
1086                                const gchar          *message,
1087                                EmpathyContactList   *list)
1088 {
1089         EmpathyContactListPriv *priv;
1090         GList                  *added_list, *l;
1091
1092         priv = GET_PRIV (list);
1093
1094         added_list = empathy_contact_list_get_from_handles (list, handles);
1095
1096         for (l = added_list; l; l = l->next) {
1097                 GossipContact *contact;
1098
1099                 contact = GOSSIP_CONTACT (l->data);
1100                 contact_list_block_contact (list, contact);
1101                 gossip_contact_set_subscription (contact, GOSSIP_SUBSCRIPTION_BOTH);
1102                 contact_list_unblock_contact (list, contact);
1103
1104                 g_signal_emit (list, signals[CONTACT_ADDED], 0, contact);
1105
1106                 g_object_unref (contact);
1107         }
1108
1109         g_list_free (added_list);
1110 }
1111
1112 static void
1113 contact_list_contact_removed_cb (GossipTelepathyGroup *group,
1114                                  GArray               *handles,
1115                                  guint                 actor_handle,
1116                                  guint                 reason,
1117                                  const gchar          *message,
1118                                  EmpathyContactList   *list)
1119 {
1120         EmpathyContactListPriv *priv;
1121         GList                  *removed_list, *l;
1122
1123         priv = GET_PRIV (list);
1124
1125         removed_list = empathy_contact_list_get_from_handles (list, handles);
1126
1127         for (l = removed_list; l; l = l->next) {
1128                 GossipContact *contact;
1129                 guint          handle;
1130
1131                 contact = GOSSIP_CONTACT (l->data);
1132
1133                 handle = gossip_contact_get_handle (contact);
1134                 g_hash_table_remove (priv->contacts, GUINT_TO_POINTER (handle));
1135
1136                 g_signal_emit (list, signals[CONTACT_REMOVED], 0, contact);
1137
1138                 g_object_unref (contact);
1139         }
1140
1141         g_list_free (removed_list);
1142 }
1143
1144 static void
1145 contact_list_local_pending_cb (GossipTelepathyGroup *group,
1146                                GArray               *handles,
1147                                guint                 actor_handle,
1148                                guint                 reason,
1149                                const gchar          *message,
1150                                EmpathyContactList   *list)
1151 {
1152         EmpathyContactListPriv *priv;
1153         GList                  *pending_list, *l;
1154
1155         priv = GET_PRIV (list);
1156
1157         pending_list = empathy_contact_list_get_from_handles (list, handles);
1158
1159         for (l = pending_list; l; l = l->next) {
1160                 GossipContact *contact;
1161
1162                 contact = GOSSIP_CONTACT (l->data);
1163
1164                 /* FIXME: Is that the correct way ? */
1165                 contact_list_block_contact (list, contact);
1166                 gossip_contact_set_subscription (contact, GOSSIP_SUBSCRIPTION_FROM);
1167                 contact_list_unblock_contact (list, contact);
1168                 g_signal_emit (list, signals[CONTACT_ADDED], 0, contact);
1169
1170                 g_object_unref (contact);
1171         }
1172
1173         g_list_free (pending_list);
1174 }
1175
1176 static void
1177 contact_list_groups_updated_cb (GossipContact      *contact,
1178                                 GParamSpec         *param,
1179                                 EmpathyContactList *list)
1180 {
1181         EmpathyContactListPriv *priv;
1182         ContactListData         data;
1183         GList                  *groups, *l;
1184
1185         priv = GET_PRIV (list);
1186
1187         /* Make sure all groups are created */
1188         groups = gossip_contact_get_groups (contact);
1189         for (l = groups; l; l = l->next) {
1190                 contact_list_get_group (list, l->data);
1191         }
1192
1193         data.handle = gossip_contact_get_handle (contact);
1194         data.new_groups = groups;
1195
1196         g_hash_table_foreach (priv->groups,
1197                               (GHFunc) contact_list_update_groups_foreach,
1198                               &data);
1199 }
1200
1201 static void
1202 contact_list_subscription_updated_cb (GossipContact      *contact,
1203                                       GParamSpec         *param,
1204                                       EmpathyContactList *list)
1205 {
1206         EmpathyContactListPriv *priv;
1207         GossipSubscription      subscription;
1208         guint                   handle;
1209
1210         priv = GET_PRIV (list);
1211
1212         subscription = gossip_contact_get_subscription (contact);
1213         handle = gossip_contact_get_handle (contact);
1214
1215         /* FIXME: what to do here, I'm a bit lost... */
1216         if (subscription) {
1217                 gossip_telepathy_group_add_member (priv->publish, handle, "");
1218         } else {
1219                 gossip_telepathy_group_remove_member (priv->publish, handle, "");
1220         }
1221 }
1222
1223 static void
1224 contact_list_name_updated_cb (GossipContact      *contact,
1225                               GParamSpec         *param,
1226                               EmpathyContactList *list)
1227 {
1228         EmpathyContactListPriv *priv;
1229         GHashTable             *new_alias;
1230         const gchar            *new_name;
1231         guint                   handle;
1232         GError                 *error = NULL;
1233
1234         priv = GET_PRIV (list);
1235         
1236         handle = gossip_contact_get_handle (contact);
1237         new_name = gossip_contact_get_name (contact);
1238
1239         gossip_debug (DEBUG_DOMAIN, "renaming handle %d to %s",
1240                       handle, new_name);
1241
1242         new_alias = g_hash_table_new_full (g_direct_hash,
1243                                            g_direct_equal,
1244                                            NULL,
1245                                            g_free);
1246
1247         g_hash_table_insert (new_alias,
1248                              GUINT_TO_POINTER (handle),
1249                              g_strdup (new_name));
1250
1251         if (!tp_conn_iface_aliasing_set_aliases (priv->aliasing_iface,
1252                                                  new_alias,
1253                                                  &error)) {
1254                 gossip_debug (DEBUG_DOMAIN, 
1255                               "Couldn't rename contact: %s",
1256                               error ? error->message : "No error given");
1257                 g_clear_error (&error);
1258         }
1259
1260         g_hash_table_destroy (new_alias);
1261 }
1262
1263 static void
1264 contact_list_update_groups_foreach (gchar                *object_path,
1265                                     GossipTelepathyGroup *group,
1266                                     ContactListData      *data)
1267 {
1268         gboolean     is_member;
1269         gboolean     found = FALSE;
1270         const gchar *group_name;
1271         GList       *l;
1272
1273         is_member = gossip_telepathy_group_is_member (group, data->handle);
1274         group_name = gossip_telepathy_group_get_name (group);
1275
1276         for (l = data->new_groups; l; l = l->next) {
1277                 if (strcmp (group_name, l->data) == 0) {
1278                         found = TRUE;
1279                         break;
1280                 }
1281         }
1282
1283         if (is_member && !found) {
1284                 /* We are no longer member of this group */
1285                 gossip_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1286                               data->handle, group_name);
1287                 gossip_telepathy_group_remove_member (group, data->handle, "");
1288         }
1289
1290         if (!is_member && found) {
1291                 /* We are now member of this group */
1292                 gossip_debug (DEBUG_DOMAIN, "Contact %d added to group '%s'",
1293                               data->handle, group_name);
1294                 gossip_telepathy_group_add_member (group, data->handle, "");
1295         }
1296 }
1297
1298 static GossipTelepathyGroup *
1299 contact_list_get_group (EmpathyContactList *list,
1300                         const gchar        *name)
1301 {
1302         EmpathyContactListPriv *priv;
1303         GossipTelepathyGroup   *group;
1304         TpChan                 *group_channel;
1305         GArray                 *handles;
1306         guint                   group_handle;
1307         char                   *group_object_path;
1308         const char             *names[2] = {name, NULL};
1309         GError                 *error = NULL;
1310
1311         priv = GET_PRIV (list);
1312
1313         group = g_hash_table_find (priv->groups,
1314                                    (GHRFunc) contact_list_find_group,
1315                                    (gchar*) name);
1316         if (group) {
1317                 return group;
1318         }
1319
1320         gossip_debug (DEBUG_DOMAIN, "creating new group: %s", name);
1321
1322         if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
1323                                       TP_HANDLE_TYPE_GROUP,
1324                                       names,
1325                                       &handles,
1326                                       &error)) {
1327                 gossip_debug (DEBUG_DOMAIN,
1328                               "Couldn't request the creation of a new handle for group: %s",
1329                               error ? error->message : "No error given");
1330                 g_clear_error (&error);
1331                 return NULL;
1332         }
1333         group_handle = g_array_index (handles, guint, 0);
1334         g_array_free (handles, TRUE);
1335
1336         if (!tp_conn_request_channel (DBUS_G_PROXY (priv->tp_conn),
1337                                       TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1338                                       TP_HANDLE_TYPE_GROUP,
1339                                       group_handle,
1340                                       FALSE,
1341                                       &group_object_path,
1342                                       &error)) {
1343                 gossip_debug (DEBUG_DOMAIN,
1344                               "Couldn't request the creation of a new group channel: %s",
1345                               error ? error->message : "No error given");
1346                 g_clear_error (&error);
1347                 return NULL;
1348         }
1349
1350         group_channel = tp_chan_new (tp_get_bus (),
1351                                      dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn)),
1352                                      group_object_path,
1353                                      TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1354                                      TP_HANDLE_TYPE_GROUP,
1355                                      group_handle);
1356
1357         dbus_g_proxy_connect_signal (DBUS_G_PROXY (group_channel),
1358                                      "Closed",
1359                                      G_CALLBACK
1360                                      (contact_list_group_channel_closed_cb),
1361                                      list,
1362                                      NULL);
1363
1364         group = gossip_telepathy_group_new (group_channel, priv->tp_conn);
1365         g_hash_table_insert (priv->groups, group_object_path, group);
1366         g_signal_connect (group, "members-added",
1367                           G_CALLBACK (contact_list_group_members_added_cb),
1368                           list);
1369         g_signal_connect (group, "members-removed",
1370                           G_CALLBACK (contact_list_group_members_removed_cb),
1371                           list);
1372
1373         return group;
1374 }
1375
1376 static gboolean
1377 contact_list_find_group (gchar                 *key,
1378                          GossipTelepathyGroup  *group,
1379                          gchar                 *group_name)
1380 {
1381         if (strcmp (group_name, gossip_telepathy_group_get_name (group)) == 0) {
1382                 return TRUE;
1383         }
1384
1385         return FALSE;
1386 }
1387
1388 static void
1389 contact_list_get_groups_foreach (gchar                 *key,
1390                                  GossipTelepathyGroup  *group,
1391                                  GList                **groups)
1392 {
1393         const gchar *name;
1394
1395         name = gossip_telepathy_group_get_name (group);
1396         *groups = g_list_append (*groups, g_strdup (name));
1397 }
1398
1399 static void
1400 contact_list_group_channel_closed_cb (TpChan             *channel,
1401                                       EmpathyContactList *list)
1402 {
1403         EmpathyContactListPriv *priv;
1404
1405         priv = GET_PRIV (list);
1406
1407         g_hash_table_remove (priv->groups,
1408                              dbus_g_proxy_get_path (DBUS_G_PROXY (channel)));
1409 }
1410
1411 static void
1412 contact_list_group_members_added_cb (GossipTelepathyGroup *group,
1413                                      GArray               *members,
1414                                      guint                 actor_handle,
1415                                      guint                 reason,
1416                                      const gchar          *message,
1417                                      EmpathyContactList   *list)
1418 {
1419         EmpathyContactListPriv *priv;
1420         GList                  *added_list, *l;
1421         const gchar            *group_name;
1422
1423         priv = GET_PRIV (list);
1424
1425         group_name = gossip_telepathy_group_get_name (group);
1426         added_list = empathy_contact_list_get_from_handles (list, members);
1427
1428         for (l = added_list; l; l = l->next) {
1429                 GossipContact *contact;
1430                 GList         *contact_groups;
1431
1432                 contact = GOSSIP_CONTACT (l->data);
1433                 contact_groups = gossip_contact_get_groups (contact);
1434
1435                 if (!g_list_find_custom (contact_groups,
1436                                          group_name,
1437                                          (GCompareFunc) strcmp)) {
1438                         gossip_debug (DEBUG_DOMAIN, "Contact %s added to group '%s'",
1439                                       gossip_contact_get_name (contact),
1440                                       group_name);
1441                         contact_groups = g_list_append (contact_groups,
1442                                                         g_strdup (group_name));
1443                         contact_list_block_contact (list, contact);
1444                         gossip_contact_set_groups (contact, contact_groups);
1445                         contact_list_unblock_contact (list, contact);
1446                 }
1447
1448                 g_object_unref (contact);
1449         }
1450
1451         g_list_free (added_list);
1452 }
1453
1454 static void
1455 contact_list_group_members_removed_cb (GossipTelepathyGroup *group,
1456                                        GArray               *members,
1457                                        guint                 actor_handle,
1458                                        guint                 reason,
1459                                        const gchar          *message,
1460                                        EmpathyContactList   *list)
1461 {
1462         EmpathyContactListPriv *priv;
1463         GList                  *removed_list, *l;
1464         const gchar            *group_name;
1465
1466         priv = GET_PRIV (list);
1467
1468         group_name = gossip_telepathy_group_get_name (group);
1469         removed_list = empathy_contact_list_get_from_handles (list, members);
1470
1471         for (l = removed_list; l; l = l->next) {
1472                 GossipContact *contact;
1473                 GList         *contact_groups;
1474                 GList         *to_remove;
1475
1476                 /* FIXME: Does it leak ? */
1477                 contact = GOSSIP_CONTACT (l->data);
1478                 contact_groups = gossip_contact_get_groups (contact);
1479                 contact_groups = g_list_copy (contact_groups);
1480
1481                 to_remove = g_list_find_custom (contact_groups,
1482                                                 group_name,
1483                                                 (GCompareFunc) strcmp);
1484                 if (to_remove) {
1485                         gossip_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1486                                       gossip_contact_get_handle (contact),
1487                                       group_name);
1488                         contact_groups = g_list_remove_link (contact_groups,
1489                                                              to_remove);
1490                         contact_list_block_contact (list, contact);
1491                         gossip_contact_set_groups (contact, contact_groups);
1492                         contact_list_unblock_contact (list, contact);
1493                 }
1494
1495                 g_list_free (contact_groups);
1496
1497                 g_object_unref (contact);
1498         }
1499
1500         g_list_free (removed_list);
1501 }
1502
1503 static void
1504 contact_list_get_contacts_foreach (guint           handle,
1505                                    GossipContact  *contact,
1506                                    GList         **contacts)
1507 {
1508         *contacts = g_list_append (*contacts, g_object_ref (contact));
1509 }
1510
1511 static void
1512 contact_list_get_info (EmpathyContactList *list,
1513                        GArray             *handles)
1514 {
1515         EmpathyContactListPriv *priv;
1516         GError                 *error = NULL;
1517
1518         priv = GET_PRIV (list);
1519
1520         if (priv->presence_iface) {
1521                 /* FIXME: We should use GetPresence instead */
1522                 if (!tp_conn_iface_presence_request_presence (priv->presence_iface,
1523                                                               handles, &error)) {
1524                         gossip_debug (DEBUG_DOMAIN, 
1525                                       "Could not request presences: %s",
1526                                       error ? error->message : "No error given");
1527                         g_clear_error (&error);
1528                 }
1529         }
1530
1531         if (priv->aliasing_iface) {
1532                 ContactListAliasesRequestData *data;
1533
1534                 data = g_slice_new (ContactListAliasesRequestData);
1535                 data->list = list;
1536                 data->handles = g_memdup (handles->data, handles->len * sizeof (guint));
1537
1538                 tp_conn_iface_aliasing_request_aliases_async (priv->aliasing_iface,
1539                                                               handles,
1540                                                               (tp_conn_iface_aliasing_request_aliases_reply)
1541                                                               contact_list_request_aliases_cb,
1542                                                               data);
1543         }
1544
1545         if (priv->avatars_iface) {
1546                 guint i;
1547
1548                 for (i = 0; i < handles->len; i++) {
1549                         guint handle;
1550
1551                         handle = g_array_index (handles, gint, i);
1552                         contact_list_request_avatar (list, handle);
1553                 }
1554         }
1555 }
1556
1557 static void
1558 contact_list_request_avatar (EmpathyContactList *list,
1559                              guint               handle)
1560 {
1561         EmpathyContactListPriv *priv;
1562
1563         priv = GET_PRIV (list);
1564         
1565         /* We queue avatar requests to not send too many dbus async
1566          * calls at once. If we don't we reach the dbus's limit of
1567          * pending calls */
1568         priv->avatar_requests_queue = g_list_append (priv->avatar_requests_queue,
1569                                                      GUINT_TO_POINTER (handle));
1570         contact_list_start_avatar_requests (list);
1571 }
1572
1573 static void
1574 contact_list_start_avatar_requests (EmpathyContactList *list)
1575 {
1576         EmpathyContactListPriv       *priv;
1577         ContactListAvatarRequestData *data;
1578
1579         priv = GET_PRIV (list);
1580
1581         while (n_avatar_requests <  MAX_AVATAR_REQUESTS &&
1582                priv->avatar_requests_queue) {
1583                 data = g_slice_new (ContactListAvatarRequestData);
1584                 data->list = list;
1585                 data->handle = GPOINTER_TO_UINT (priv->avatar_requests_queue->data);
1586
1587                 n_avatar_requests++;
1588                 priv->avatar_requests_queue = g_list_remove (priv->avatar_requests_queue,
1589                                                              priv->avatar_requests_queue->data);
1590
1591                 tp_conn_iface_avatars_request_avatar_async (priv->avatars_iface,
1592                                                             data->handle,
1593                                                             (tp_conn_iface_avatars_request_avatar_reply)
1594                                                             contact_list_request_avatar_cb,
1595                                                             data);
1596         }
1597 }
1598
1599 static void
1600 contact_list_avatar_update_cb (DBusGProxy         *proxy,
1601                                guint               handle,
1602                                gchar              *new_token,
1603                                EmpathyContactList *list)
1604 {
1605         gossip_debug (DEBUG_DOMAIN, "Changing avatar for %d to %s",
1606                       handle, new_token);
1607
1608         contact_list_request_avatar (list, handle);
1609 }
1610
1611 static void
1612 contact_list_request_avatar_cb (DBusGProxy                   *proxy,
1613                                 GArray                       *avatar_data,
1614                                 gchar                        *mime_type,
1615                                 GError                       *error,
1616                                 ContactListAvatarRequestData *data)
1617 {
1618         GossipContact *contact;
1619
1620         contact = empathy_contact_list_get_from_handle (data->list, data->handle);
1621
1622         if (error) {
1623                 gossip_debug (DEBUG_DOMAIN, "Error requesting avatar for %s: %s",
1624                               gossip_contact_get_name (contact),
1625                               error ? error->message : "No error given");
1626         } else {
1627                 GossipAvatar *avatar;
1628
1629                 avatar = gossip_avatar_new (avatar_data->data,
1630                                             avatar_data->len,
1631                                             mime_type);
1632                 contact_list_block_contact (data->list, contact);
1633                 gossip_contact_set_avatar (contact, avatar);
1634                 contact_list_unblock_contact (data->list, contact);
1635                 gossip_avatar_unref (avatar);
1636         }
1637
1638         n_avatar_requests--;
1639         contact_list_start_avatar_requests (data->list);
1640
1641         g_slice_free (ContactListAvatarRequestData, data);
1642 }
1643
1644 static void
1645 contact_list_aliases_update_cb (DBusGProxy         *proxy,
1646                                 GPtrArray          *renamed_handlers,
1647                                 EmpathyContactList *list)
1648 {
1649         gint i;
1650
1651         for (i = 0; renamed_handlers->len > i; i++) {
1652                 guint          handle;
1653                 const gchar   *alias;
1654                 GValueArray   *renamed_struct;
1655                 GossipContact *contact;
1656
1657                 renamed_struct = g_ptr_array_index (renamed_handlers, i);
1658                 handle = g_value_get_uint(g_value_array_get_nth (renamed_struct, 0));
1659                 alias = g_value_get_string(g_value_array_get_nth (renamed_struct, 1));
1660
1661                 if (alias && *alias == '\0') {
1662                         alias = NULL;
1663                 }
1664
1665                 contact = empathy_contact_list_get_from_handle (list, handle);
1666                 contact_list_block_contact (list, contact);
1667                 gossip_contact_set_name (contact, alias);
1668                 contact_list_unblock_contact (list, contact);
1669
1670                 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (update cb)",
1671                               handle, alias);
1672         }
1673 }
1674
1675 static void
1676 contact_list_request_aliases_cb (DBusGProxy                     *proxy,
1677                                  gchar                         **contact_names,
1678                                  GError                         *error,
1679                                  ContactListAliasesRequestData  *data)
1680 {
1681         guint   i = 0;
1682         gchar **name;
1683
1684         for (name = contact_names; *name && !error; name++) {
1685                 GossipContact *contact;
1686
1687                 contact = empathy_contact_list_get_from_handle (data->list,
1688                                                                 data->handles[i]);
1689                 contact_list_block_contact (data->list, contact);
1690                 gossip_contact_set_name (contact, *name);
1691                 contact_list_unblock_contact (data->list, contact);
1692
1693                 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (request cb)",
1694                               data->handles[i], *name);
1695
1696                 i++;
1697         }
1698
1699         g_free (data->handles);
1700         g_slice_free (ContactListAliasesRequestData, data);
1701 }
1702
1703 static void
1704 contact_list_presence_update_cb (DBusGProxy         *proxy,
1705                                  GHashTable         *handle_table,
1706                                  EmpathyContactList *list)
1707 {
1708         g_hash_table_foreach (handle_table,
1709                               (GHFunc) contact_list_parse_presence_foreach,
1710                               list);
1711 }
1712
1713 static void
1714 contact_list_parse_presence_foreach (guint               handle,
1715                                      GValueArray        *presence_struct,
1716                                      EmpathyContactList *list)
1717 {
1718         GHashTable     *presences_table;
1719         GossipContact  *contact;
1720         GossipPresence *presence = NULL;
1721
1722         contact = empathy_contact_list_get_from_handle (list, handle);
1723         presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
1724
1725         g_hash_table_foreach (presences_table,
1726                               (GHFunc) contact_list_presences_table_foreach,
1727                               &presence);
1728
1729         gossip_debug (DEBUG_DOMAIN, "Presence changed for %s (%d) to %s (%d)",
1730                       gossip_contact_get_name (contact),
1731                       handle,
1732                       presence ? gossip_presence_get_status (presence) : "unset",
1733                       presence ? gossip_presence_get_state (presence) : MC_PRESENCE_UNSET);
1734
1735         contact_list_block_contact (list, contact);
1736         gossip_contact_set_presence (contact, presence);
1737         contact_list_unblock_contact (list, contact);
1738 }
1739
1740 static void
1741 contact_list_presences_table_foreach (const gchar     *state_str,
1742                                       GHashTable      *presences_table,
1743                                       GossipPresence **presence)
1744 {
1745         McPresence    state;
1746         const GValue *message;
1747
1748         state = gossip_presence_state_from_str (state_str);
1749         if ((state == MC_PRESENCE_UNSET) || (state == MC_PRESENCE_OFFLINE)) {
1750                 return;
1751         }
1752
1753         if (*presence) {
1754                 g_object_unref (*presence);
1755                 *presence = NULL;
1756         }
1757
1758         *presence = gossip_presence_new ();
1759         gossip_presence_set_state (*presence, state);
1760
1761         message = g_hash_table_lookup (presences_table, "message");
1762         if (message != NULL) {
1763                 gossip_presence_set_status (*presence,
1764                                             g_value_get_string (message));
1765         }
1766 }
1767