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