]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-contact-list.c
c81279043c3a43e0d44c6aca05945d28e8f2c494
[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                 /* FIXME: this adds the handle to the roster */
384                 priv->user_contact = empathy_tp_contact_list_get_from_handle (list, handle);
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         handle = gossip_contact_get_handle (contact);
1280         new_name = gossip_contact_get_name (contact);
1281
1282         gossip_debug (DEBUG_DOMAIN, "renaming handle %d to %s",
1283                       handle, new_name);
1284
1285         new_alias = g_hash_table_new_full (g_direct_hash,
1286                                            g_direct_equal,
1287                                            NULL,
1288                                            g_free);
1289
1290         g_hash_table_insert (new_alias,
1291                              GUINT_TO_POINTER (handle),
1292                              g_strdup (new_name));
1293
1294         if (!tp_conn_iface_aliasing_set_aliases (priv->aliasing_iface,
1295                                                  new_alias,
1296                                                  &error)) {
1297                 gossip_debug (DEBUG_DOMAIN, 
1298                               "Couldn't rename contact: %s",
1299                               error ? error->message : "No error given");
1300                 g_clear_error (&error);
1301         }
1302
1303         g_hash_table_destroy (new_alias);
1304 }
1305
1306 static void
1307 tp_contact_list_update_groups_foreach (gchar                *object_path,
1308                                        GossipTelepathyGroup *group,
1309                                        TpContactListData    *data)
1310 {
1311         gboolean     is_member;
1312         gboolean     found = FALSE;
1313         const gchar *group_name;
1314         GList       *l;
1315
1316         is_member = gossip_telepathy_group_is_member (group, data->handle);
1317         group_name = gossip_telepathy_group_get_name (group);
1318
1319         for (l = data->new_groups; l; l = l->next) {
1320                 if (strcmp (group_name, l->data) == 0) {
1321                         found = TRUE;
1322                         break;
1323                 }
1324         }
1325
1326         if (is_member && !found) {
1327                 /* We are no longer member of this group */
1328                 gossip_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1329                               data->handle, group_name);
1330                 gossip_telepathy_group_remove_member (group, data->handle, "");
1331         }
1332
1333         if (!is_member && found) {
1334                 /* We are now member of this group */
1335                 gossip_debug (DEBUG_DOMAIN, "Contact %d added to group '%s'",
1336                               data->handle, group_name);
1337                 gossip_telepathy_group_add_member (group, data->handle, "");
1338         }
1339 }
1340
1341 static GossipTelepathyGroup *
1342 tp_contact_list_get_group (EmpathyTpContactList *list,
1343                            const gchar          *name)
1344 {
1345         EmpathyTpContactListPriv *priv;
1346         GossipTelepathyGroup     *group;
1347         TpChan                   *group_channel;
1348         GArray                   *handles;
1349         guint                     group_handle;
1350         char                     *group_object_path;
1351         const char               *names[2] = {name, NULL};
1352         GError                   *error = NULL;
1353
1354         priv = GET_PRIV (list);
1355
1356         group = g_hash_table_find (priv->groups,
1357                                    (GHRFunc) tp_contact_list_find_group,
1358                                    (gchar*) name);
1359         if (group) {
1360                 return group;
1361         }
1362
1363         gossip_debug (DEBUG_DOMAIN, "creating new group: %s", name);
1364
1365         if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
1366                                       TP_HANDLE_TYPE_GROUP,
1367                                       names,
1368                                       &handles,
1369                                       &error)) {
1370                 gossip_debug (DEBUG_DOMAIN,
1371                               "Couldn't request the creation of a new handle for group: %s",
1372                               error ? error->message : "No error given");
1373                 g_clear_error (&error);
1374                 return NULL;
1375         }
1376         group_handle = g_array_index (handles, guint, 0);
1377         g_array_free (handles, TRUE);
1378
1379         if (!tp_conn_request_channel (DBUS_G_PROXY (priv->tp_conn),
1380                                       TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1381                                       TP_HANDLE_TYPE_GROUP,
1382                                       group_handle,
1383                                       FALSE,
1384                                       &group_object_path,
1385                                       &error)) {
1386                 gossip_debug (DEBUG_DOMAIN,
1387                               "Couldn't request the creation of a new group channel: %s",
1388                               error ? error->message : "No error given");
1389                 g_clear_error (&error);
1390                 return NULL;
1391         }
1392
1393         group_channel = tp_chan_new (tp_get_bus (),
1394                                      dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn)),
1395                                      group_object_path,
1396                                      TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1397                                      TP_HANDLE_TYPE_GROUP,
1398                                      group_handle);
1399
1400         dbus_g_proxy_connect_signal (DBUS_G_PROXY (group_channel),
1401                                      "Closed",
1402                                      G_CALLBACK
1403                                      (tp_contact_list_group_channel_closed_cb),
1404                                      list,
1405                                      NULL);
1406
1407         group = gossip_telepathy_group_new (group_channel, priv->tp_conn);
1408         g_hash_table_insert (priv->groups, group_object_path, group);
1409         g_signal_connect (group, "members-added",
1410                           G_CALLBACK (tp_contact_list_group_members_added_cb),
1411                           list);
1412         g_signal_connect (group, "members-removed",
1413                           G_CALLBACK (tp_contact_list_group_members_removed_cb),
1414                           list);
1415
1416         return group;
1417 }
1418
1419 static gboolean
1420 tp_contact_list_find_group (gchar                 *key,
1421                             GossipTelepathyGroup  *group,
1422                             gchar                 *group_name)
1423 {
1424         if (strcmp (group_name, gossip_telepathy_group_get_name (group)) == 0) {
1425                 return TRUE;
1426         }
1427
1428         return FALSE;
1429 }
1430
1431 static void
1432 tp_contact_list_get_groups_foreach (gchar                 *key,
1433                                     GossipTelepathyGroup  *group,
1434                                     GList                **groups)
1435 {
1436         const gchar *name;
1437
1438         name = gossip_telepathy_group_get_name (group);
1439         *groups = g_list_append (*groups, g_strdup (name));
1440 }
1441
1442 static void
1443 tp_contact_list_group_channel_closed_cb (TpChan             *channel,
1444                                          EmpathyTpContactList *list)
1445 {
1446         EmpathyTpContactListPriv *priv;
1447
1448         priv = GET_PRIV (list);
1449
1450         g_hash_table_remove (priv->groups,
1451                              dbus_g_proxy_get_path (DBUS_G_PROXY (channel)));
1452 }
1453
1454 static void
1455 tp_contact_list_group_members_added_cb (GossipTelepathyGroup *group,
1456                                         GArray               *members,
1457                                         guint                 actor_handle,
1458                                         guint                 reason,
1459                                         const gchar          *message,
1460                                         EmpathyTpContactList *list)
1461 {
1462         EmpathyTpContactListPriv *priv;
1463         GList                    *added_list, *l;
1464         const gchar              *group_name;
1465
1466         priv = GET_PRIV (list);
1467
1468         group_name = gossip_telepathy_group_get_name (group);
1469         added_list = empathy_tp_contact_list_get_from_handles (list, members);
1470
1471         for (l = added_list; l; l = l->next) {
1472                 GossipContact *contact;
1473
1474                 contact = GOSSIP_CONTACT (l->data);
1475
1476                 tp_contact_list_block_contact (list, contact);
1477                 gossip_contact_add_group (contact, group_name);
1478                 tp_contact_list_unblock_contact (list, contact);
1479
1480                 g_object_unref (contact);
1481         }
1482
1483         g_list_free (added_list);
1484 }
1485
1486 static void
1487 tp_contact_list_group_members_removed_cb (GossipTelepathyGroup *group,
1488                                           GArray               *members,
1489                                           guint                 actor_handle,
1490                                           guint                 reason,
1491                                           const gchar          *message,
1492                                           EmpathyTpContactList *list)
1493 {
1494         EmpathyTpContactListPriv *priv;
1495         GList                    *removed_list, *l;
1496         const gchar              *group_name;
1497
1498         priv = GET_PRIV (list);
1499
1500         group_name = gossip_telepathy_group_get_name (group);
1501         removed_list = empathy_tp_contact_list_get_from_handles (list, members);
1502
1503         for (l = removed_list; l; l = l->next) {
1504                 GossipContact *contact;
1505
1506                 contact = l->data;
1507
1508                 tp_contact_list_block_contact (list, contact);
1509                 gossip_contact_remove_group (contact, group_name);
1510                 tp_contact_list_unblock_contact (list, contact);
1511
1512                 g_object_unref (contact);
1513         }
1514
1515         g_list_free (removed_list);
1516 }
1517
1518 static void
1519 tp_contact_list_get_members_foreach (guint           handle,
1520                                       GossipContact  *contact,
1521                                       GList         **contacts)
1522 {
1523         GossipSubscription subscription;
1524
1525         subscription = gossip_contact_get_subscription (contact);
1526         if (subscription & GOSSIP_SUBSCRIPTION_TO) {
1527                 *contacts = g_list_append (*contacts, g_object_ref (contact));
1528         }
1529 }
1530
1531 static void
1532 tp_contact_list_get_info (EmpathyTpContactList *list,
1533                           GArray               *handles)
1534 {
1535         EmpathyTpContactListPriv *priv;
1536         GError                   *error = NULL;
1537
1538         priv = GET_PRIV (list);
1539
1540         if (priv->presence_iface) {
1541                 /* FIXME: We should use GetPresence instead */
1542                 if (!tp_conn_iface_presence_request_presence (priv->presence_iface,
1543                                                               handles, &error)) {
1544                         gossip_debug (DEBUG_DOMAIN, 
1545                                       "Could not request presences: %s",
1546                                       error ? error->message : "No error given");
1547                         g_clear_error (&error);
1548                 }
1549         }
1550
1551         if (priv->aliasing_iface) {
1552                 TpContactListAliasesRequestData *data;
1553
1554                 data = g_slice_new (TpContactListAliasesRequestData);
1555                 data->list = list;
1556                 data->handles = g_memdup (handles->data, handles->len * sizeof (guint));
1557
1558                 tp_conn_iface_aliasing_request_aliases_async (priv->aliasing_iface,
1559                                                               handles,
1560                                                               (tp_conn_iface_aliasing_request_aliases_reply)
1561                                                               tp_contact_list_request_aliases_cb,
1562                                                               data);
1563         }
1564
1565         if (priv->avatars_iface) {
1566                 guint i;
1567
1568                 for (i = 0; i < handles->len; i++) {
1569                         guint handle;
1570
1571                         handle = g_array_index (handles, gint, i);
1572                         tp_contact_list_request_avatar (list, handle);
1573                 }
1574         }
1575 }
1576
1577 static void
1578 tp_contact_list_request_avatar (EmpathyTpContactList *list,
1579                                 guint                 handle)
1580 {
1581         EmpathyTpContactListPriv *priv;
1582
1583         priv = GET_PRIV (list);
1584         
1585         /* We queue avatar requests to not send too many dbus async
1586          * calls at once. If we don't we reach the dbus's limit of
1587          * pending calls */
1588         priv->avatar_requests_queue = g_list_append (priv->avatar_requests_queue,
1589                                                      GUINT_TO_POINTER (handle));
1590         tp_contact_list_start_avatar_requests (list);
1591 }
1592
1593 static void
1594 tp_contact_list_start_avatar_requests (EmpathyTpContactList *list)
1595 {
1596         EmpathyTpContactListPriv       *priv;
1597         TpContactListAvatarRequestData *data;
1598
1599         priv = GET_PRIV (list);
1600
1601         while (n_avatar_requests <  MAX_AVATAR_REQUESTS &&
1602                priv->avatar_requests_queue) {
1603                 data = g_slice_new (TpContactListAvatarRequestData);
1604                 data->list = list;
1605                 data->handle = GPOINTER_TO_UINT (priv->avatar_requests_queue->data);
1606
1607                 n_avatar_requests++;
1608                 priv->avatar_requests_queue = g_list_remove (priv->avatar_requests_queue,
1609                                                              priv->avatar_requests_queue->data);
1610
1611                 tp_conn_iface_avatars_request_avatar_async (priv->avatars_iface,
1612                                                             data->handle,
1613                                                             (tp_conn_iface_avatars_request_avatar_reply)
1614                                                             tp_contact_list_request_avatar_cb,
1615                                                             data);
1616         }
1617 }
1618
1619 static void
1620 tp_contact_list_avatar_update_cb (DBusGProxy           *proxy,
1621                                   guint                 handle,
1622                                   gchar                *new_token,
1623                                   EmpathyTpContactList *list)
1624 {
1625         EmpathyTpContactListPriv *priv;
1626
1627         priv = GET_PRIV (list);
1628
1629         if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1630                 /* We don't know this contact, skip */
1631                 return;
1632         }
1633
1634         gossip_debug (DEBUG_DOMAIN, "Changing avatar for %d to %s",
1635                       handle, new_token);
1636
1637         tp_contact_list_request_avatar (list, handle);
1638 }
1639
1640 static void
1641 tp_contact_list_request_avatar_cb (DBusGProxy                     *proxy,
1642                                    GArray                         *avatar_data,
1643                                    gchar                          *mime_type,
1644                                    GError                         *error,
1645                                    TpContactListAvatarRequestData *data)
1646 {
1647         GossipContact *contact;
1648
1649         contact = empathy_tp_contact_list_get_from_handle (data->list, data->handle);
1650
1651         if (error) {
1652                 gossip_debug (DEBUG_DOMAIN, "Error requesting avatar for %s: %s",
1653                               gossip_contact_get_name (contact),
1654                               error ? error->message : "No error given");
1655         } else {
1656                 GossipAvatar *avatar;
1657
1658                 avatar = gossip_avatar_new (avatar_data->data,
1659                                             avatar_data->len,
1660                                             mime_type);
1661                 tp_contact_list_block_contact (data->list, contact);
1662                 gossip_contact_set_avatar (contact, avatar);
1663                 tp_contact_list_unblock_contact (data->list, contact);
1664                 gossip_avatar_unref (avatar);
1665         }
1666
1667         n_avatar_requests--;
1668         tp_contact_list_start_avatar_requests (data->list);
1669
1670         g_object_unref (contact);
1671         g_slice_free (TpContactListAvatarRequestData, data);
1672 }
1673
1674 static void
1675 tp_contact_list_aliases_update_cb (DBusGProxy           *proxy,
1676                                    GPtrArray            *renamed_handlers,
1677                                    EmpathyTpContactList *list)
1678 {
1679         EmpathyTpContactListPriv *priv;
1680         guint                     i;
1681
1682         priv = GET_PRIV (list);
1683
1684         for (i = 0; renamed_handlers->len > i; i++) {
1685                 guint          handle;
1686                 const gchar   *alias;
1687                 GValueArray   *renamed_struct;
1688                 GossipContact *contact;
1689
1690                 renamed_struct = g_ptr_array_index (renamed_handlers, i);
1691                 handle = g_value_get_uint(g_value_array_get_nth (renamed_struct, 0));
1692                 alias = g_value_get_string(g_value_array_get_nth (renamed_struct, 1));
1693
1694                 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1695                         /* We don't know this contact, skip */
1696                         continue;
1697                 }
1698
1699                 if (G_STR_EMPTY (alias)) {
1700                         alias = NULL;
1701                 }
1702
1703                 contact = empathy_tp_contact_list_get_from_handle (list, handle);
1704                 tp_contact_list_block_contact (list, contact);
1705                 gossip_contact_set_name (contact, alias);
1706                 tp_contact_list_unblock_contact (list, contact);
1707                 g_object_unref (contact);
1708
1709                 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (update cb)",
1710                               handle, alias);
1711         }
1712 }
1713
1714 static void
1715 tp_contact_list_request_aliases_cb (DBusGProxy                       *proxy,
1716                                     gchar                           **contact_names,
1717                                     GError                           *error,
1718                                     TpContactListAliasesRequestData  *data)
1719 {
1720         guint   i = 0;
1721         gchar **name;
1722
1723         if (error) {
1724                 gossip_debug (DEBUG_DOMAIN, "Error requesting aliases: %s",
1725                               error->message);
1726         }
1727
1728         for (name = contact_names; *name && !error; name++) {
1729                 GossipContact *contact;
1730
1731                 contact = empathy_tp_contact_list_get_from_handle (data->list,
1732                                                                    data->handles[i]);
1733                 tp_contact_list_block_contact (data->list, contact);
1734                 gossip_contact_set_name (contact, *name);
1735                 tp_contact_list_unblock_contact (data->list, contact);
1736                 g_object_unref (contact);
1737
1738                 gossip_debug (DEBUG_DOMAIN, "contact %d renamed to %s (request cb)",
1739                               data->handles[i], *name);
1740
1741                 i++;
1742         }
1743
1744         g_free (data->handles);
1745         g_slice_free (TpContactListAliasesRequestData, data);
1746 }
1747
1748 static void
1749 tp_contact_list_presence_update_cb (DBusGProxy           *proxy,
1750                                     GHashTable           *handle_table,
1751                                     EmpathyTpContactList *list)
1752 {
1753         g_hash_table_foreach (handle_table,
1754                               (GHFunc) tp_contact_list_parse_presence_foreach,
1755                               list);
1756 }
1757
1758 static void
1759 tp_contact_list_parse_presence_foreach (guint                 handle,
1760                                         GValueArray          *presence_struct,
1761                                         EmpathyTpContactList *list)
1762 {
1763         EmpathyTpContactListPriv *priv;
1764         GHashTable     *presences_table;
1765         GossipContact  *contact;
1766         GossipPresence *presence = NULL;
1767
1768         priv = GET_PRIV (list);
1769
1770         if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1771                 /* We don't know this contact, skip */
1772                 return;
1773         }
1774
1775         contact = empathy_tp_contact_list_get_from_handle (list, handle);
1776         presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
1777
1778         g_hash_table_foreach (presences_table,
1779                               (GHFunc) tp_contact_list_presences_table_foreach,
1780                               &presence);
1781
1782         gossip_debug (DEBUG_DOMAIN, "Presence changed for %s (%d) to %s (%d)",
1783                       gossip_contact_get_name (contact),
1784                       handle,
1785                       presence ? gossip_presence_get_status (presence) : "unset",
1786                       presence ? gossip_presence_get_state (presence) : MC_PRESENCE_UNSET);
1787
1788         tp_contact_list_block_contact (list, contact);
1789         gossip_contact_set_presence (contact, presence);
1790         tp_contact_list_unblock_contact (list, contact);
1791
1792         g_object_unref (contact);
1793 }
1794
1795 static void
1796 tp_contact_list_presences_table_foreach (const gchar     *state_str,
1797                                          GHashTable      *presences_table,
1798                                          GossipPresence **presence)
1799 {
1800         McPresence    state;
1801         const GValue *message;
1802
1803         state = gossip_presence_state_from_str (state_str);
1804         if ((state == MC_PRESENCE_UNSET) || (state == MC_PRESENCE_OFFLINE)) {
1805                 return;
1806         }
1807
1808         if (*presence) {
1809                 g_object_unref (*presence);
1810                 *presence = NULL;
1811         }
1812
1813         *presence = gossip_presence_new ();
1814         gossip_presence_set_state (*presence, state);
1815
1816         message = g_hash_table_lookup (presences_table, "message");
1817         if (message != NULL) {
1818                 gossip_presence_set_status (*presence,
1819                                             g_value_get_string (message));
1820         }
1821 }
1822
1823 static void
1824 tp_contact_list_status_changed_cb (MissionControl                  *mc,
1825                                    TelepathyConnectionStatus        status,
1826                                    McPresence                       presence,
1827                                    TelepathyConnectionStatusReason  reason,
1828                                    const gchar                     *unique_name,
1829                                    EmpathyTpContactList            *list)
1830 {
1831         EmpathyTpContactListPriv *priv;
1832         McAccount                *account;
1833
1834         priv = GET_PRIV (list);
1835
1836         account = mc_account_lookup (unique_name);
1837         if (status != TP_CONN_STATUS_DISCONNECTED ||
1838             !gossip_account_equal (account, priv->account) ||
1839             !priv->tp_conn) {
1840                 g_object_unref (account);
1841                 return;
1842         }
1843
1844         /* We are disconnected, do just like if the connection was destroyed */
1845         g_signal_handlers_disconnect_by_func (priv->tp_conn,
1846                                               tp_contact_list_destroy_cb,
1847                                               list);
1848         tp_contact_list_destroy_cb (DBUS_G_PROXY (priv->tp_conn), list);
1849
1850         g_object_unref (account);
1851 }
1852