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