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