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