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