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