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