]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-contact-list.c
Rename all filenames starting with "gossip" by "empathy", change namespace
[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         GList          *avatar_requests_queue;
68 };
69
70 typedef enum {
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 EmpathyContact *   tp_contact_list_find                     (EmpathyContactList              *list,
99                                                                     const gchar                     *id);
100 static void               tp_contact_list_add                      (EmpathyContactList              *list,
101                                                                     EmpathyContact                  *contact,
102                                                                     const gchar                     *message);
103 static void               tp_contact_list_remove                   (EmpathyContactList              *list,
104                                                                     EmpathyContact                  *contact,
105                                                                     const gchar                     *message);
106 static GList *            tp_contact_list_get_members              (EmpathyContactList              *list);
107 static GList *            tp_contact_list_get_local_pending        (EmpathyContactList              *list);
108 static void               tp_contact_list_process_pending          (EmpathyContactList              *list,
109                                                                     EmpathyContact                  *contact,
110                                                                     gboolean                         accept);
111 static void               tp_contact_list_remove_local_pending     (EmpathyTpContactList            *list,
112                                                                     EmpathyContact                  *contact);
113 static void               tp_contact_list_contact_removed_foreach  (guint                            handle,
114                                                                     EmpathyContact                  *contact,
115                                                                     EmpathyTpContactList            *list);
116 static void               tp_contact_list_destroy_cb               (DBusGProxy                      *proxy,
117                                                                     EmpathyTpContactList            *list);
118 static gboolean           tp_contact_list_find_foreach             (guint                            handle,
119                                                                     EmpathyContact                  *contact,
120                                                                     gchar                           *id);
121 static void               tp_contact_list_newchannel_cb            (DBusGProxy                      *proxy,
122                                                                     const gchar                     *object_path,
123                                                                     const gchar                     *channel_type,
124                                                                     TelepathyHandleType              handle_type,
125                                                                     guint                            channel_handle,
126                                                                     gboolean                         suppress_handle,
127                                                                     EmpathyTpContactList            *list);
128 static TpContactListType  tp_contact_list_get_type                 (EmpathyTpContactList            *list,
129                                                                     EmpathyTpGroup                  *group);
130 static void               tp_contact_list_added_cb                 (EmpathyTpGroup                  *group,
131                                                                     GArray                          *handles,
132                                                                     guint                            actor_handle,
133                                                                     guint                            reason,
134                                                                     const gchar                     *message,
135                                                                     EmpathyTpContactList            *list);
136 static void               tp_contact_list_removed_cb               (EmpathyTpGroup                  *group,
137                                                                     GArray                          *handles,
138                                                                     guint                            actor_handle,
139                                                                     guint                            reason,
140                                                                     const gchar                     *message,
141                                                                     EmpathyTpContactList            *list);
142 static void               tp_contact_list_pending_cb               (EmpathyTpGroup                  *group,
143                                                                     GArray                          *handles,
144                                                                     guint                            actor_handle,
145                                                                     guint                            reason,
146                                                                     const gchar                     *message,
147                                                                     EmpathyTpContactList            *list);
148 static void               tp_contact_list_groups_updated_cb        (EmpathyContact                  *contact,
149                                                                     GParamSpec                      *param,
150                                                                     EmpathyTpContactList            *list);
151 static void               tp_contact_list_name_updated_cb          (EmpathyContact                  *contact,
152                                                                     GParamSpec                      *param,
153                                                                     EmpathyTpContactList            *list);
154 static void               tp_contact_list_update_groups_foreach    (gchar                           *object_path,
155                                                                     EmpathyTpGroup                  *group,
156                                                                     TpContactListData               *data);
157 static EmpathyTpGroup *   tp_contact_list_get_group                (EmpathyTpContactList            *list,
158                                                                     const gchar                     *name);
159 static gboolean           tp_contact_list_find_group               (gchar                           *key,
160                                                                     EmpathyTpGroup                  *group,
161                                                                     gchar                           *group_name);
162 static void               tp_contact_list_get_groups_foreach       (gchar                           *key,
163                                                                     EmpathyTpGroup                  *group,
164                                                                     GList                          **groups);
165 static void               tp_contact_list_group_channel_closed_cb  (TpChan                          *channel,
166                                                                     EmpathyTpContactList            *list);
167 static void               tp_contact_list_group_members_added_cb   (EmpathyTpGroup                  *group,
168                                                                     GArray                          *members,
169                                                                     guint                            actor_handle,
170                                                                     guint                            reason,
171                                                                     const gchar                     *message,
172                                                                     EmpathyTpContactList            *list);
173 static void               tp_contact_list_group_members_removed_cb (EmpathyTpGroup                  *group,
174                                                                     GArray                          *members,
175                                                                     guint                            actor_handle,
176                                                                     guint                            reason,
177                                                                     const gchar                     *message,
178                                                                     EmpathyTpContactList            *list);
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                                                                     EmpathyPresence                 **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_members       = tp_contact_list_get_members;
256         iface->get_local_pending = tp_contact_list_get_local_pending;
257         iface->process_pending   = tp_contact_list_process_pending;
258 }
259
260 static void
261 empathy_tp_contact_list_init (EmpathyTpContactList *list)
262 {
263         EmpathyTpContactListPriv *priv;
264
265         priv = GET_PRIV (list);
266
267         priv->groups = g_hash_table_new_full (g_str_hash,
268                                               g_str_equal,
269                                               (GDestroyNotify) g_free,
270                                               (GDestroyNotify) g_object_unref);
271         priv->contacts = g_hash_table_new_full (g_direct_hash,
272                                                 g_direct_equal,
273                                                 NULL,
274                                                 (GDestroyNotify) g_object_unref);
275 }
276
277 static void
278 tp_contact_list_finalize (GObject *object)
279 {
280         EmpathyTpContactListPriv *priv;
281         EmpathyTpContactList     *list;
282
283         list = EMPATHY_TP_CONTACT_LIST (object);
284         priv = GET_PRIV (list);
285
286         empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
287
288         dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
289                                         "AccountStatusChanged",
290                                         G_CALLBACK (tp_contact_list_status_changed_cb),
291                                         list);
292
293         tp_contact_list_finalize_proxies (list);
294
295         if (priv->tp_conn) {
296                 g_object_unref (priv->tp_conn);
297         }
298         if (priv->subscribe) {
299                 g_object_unref (priv->subscribe);
300         }
301         if (priv->publish) {
302                 g_object_unref (priv->publish);
303         }
304
305         g_object_unref (priv->account);
306         g_object_unref (priv->user_contact);
307         g_object_unref (priv->mc);
308         g_hash_table_destroy (priv->groups);
309         g_hash_table_destroy (priv->contacts);
310
311         g_list_foreach (priv->local_pending, (GFunc) empathy_contact_list_info_free, NULL);
312         g_list_free (priv->local_pending);
313
314         g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
315         g_list_free (priv->members);
316
317         G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
318 }
319
320 EmpathyTpContactList *
321 empathy_tp_contact_list_new (McAccount *account)
322 {
323         EmpathyTpContactListPriv *priv;
324         EmpathyTpContactList     *list;
325         MissionControl           *mc;
326         guint                     handle;
327         GError                   *error = NULL;
328
329         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
330
331         mc = empathy_mission_control_new ();
332
333         if (mission_control_get_connection_status (mc, account, NULL) != 0) {
334                 /* The account is not connected, nothing to do. */
335                 return NULL;
336         }
337
338         list = g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST, NULL);
339         priv = GET_PRIV (list);
340
341         priv->tp_conn = mission_control_get_connection (mc, account, NULL);
342         priv->account = g_object_ref (account);
343         priv->mc = mc;
344
345         g_signal_connect (priv->tp_conn, "destroy",
346                           G_CALLBACK (tp_contact_list_destroy_cb),
347                           list);
348         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
349                                      "AccountStatusChanged",
350                                      G_CALLBACK (tp_contact_list_status_changed_cb),
351                                      list, NULL);
352
353         priv->aliasing_iface = tp_conn_get_interface (priv->tp_conn,
354                                                       TELEPATHY_CONN_IFACE_ALIASING_QUARK);
355         priv->avatars_iface = tp_conn_get_interface (priv->tp_conn,
356                                                      TELEPATHY_CONN_IFACE_AVATARS_QUARK);
357         priv->presence_iface = tp_conn_get_interface (priv->tp_conn,
358                                                       TELEPATHY_CONN_IFACE_PRESENCE_QUARK);
359
360         if (priv->aliasing_iface) {
361                 dbus_g_proxy_connect_signal (priv->aliasing_iface,
362                                              "AliasesChanged",
363                                              G_CALLBACK (tp_contact_list_aliases_update_cb),
364                                              list, NULL);
365         }
366
367         if (priv->avatars_iface) {
368                 dbus_g_proxy_connect_signal (priv->avatars_iface,
369                                              "AvatarUpdated",
370                                              G_CALLBACK (tp_contact_list_avatar_update_cb),
371                                              list, NULL);
372         }
373
374         if (priv->presence_iface) {
375                 dbus_g_proxy_connect_signal (priv->presence_iface,
376                                              "PresenceUpdate",
377                                              G_CALLBACK (tp_contact_list_presence_update_cb),
378                                              list, NULL);
379         }
380
381         /* Get our own handle and contact */
382         if (!tp_conn_get_self_handle (DBUS_G_PROXY (priv->tp_conn),
383                                       &handle, &error)) {
384                 empathy_debug (DEBUG_DOMAIN, "GetSelfHandle Error: %s",
385                               error ? error->message : "No error given");
386                 g_clear_error (&error);
387         } else {
388                 priv->user_contact = empathy_tp_contact_list_get_from_handle (list, handle);
389                 empathy_contact_set_is_user (priv->user_contact, TRUE);
390         }
391
392         return list;
393 }
394
395 static void
396 tp_contact_list_setup (EmpathyContactList *list)
397 {
398         EmpathyTpContactListPriv *priv;
399         GPtrArray                *channels;
400         GError                   *error = NULL;
401         guint                     i;
402
403         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
404
405         priv = GET_PRIV (list);
406
407         empathy_debug (DEBUG_DOMAIN, "setup contact list: %p", list);
408
409         priv->setup = TRUE;
410         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
411                                      G_CALLBACK (tp_contact_list_newchannel_cb),
412                                      list, NULL);
413
414         /* Get existing channels */
415         if (!tp_conn_list_channels (DBUS_G_PROXY (priv->tp_conn),
416                                     &channels,
417                                     &error)) {
418                 empathy_debug (DEBUG_DOMAIN,
419                               "Failed to get list of open channels: %s",
420                               error ? error->message : "No error given");
421                 g_clear_error (&error);
422                 return;
423         }
424
425         for (i = 0; channels->len > i; i++) {
426                 GValueArray         *chan_struct;
427                 const gchar         *object_path;
428                 const gchar         *chan_iface;
429                 TelepathyHandleType  handle_type;
430                 guint                handle;
431
432                 chan_struct = g_ptr_array_index (channels, i);
433                 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
434                 chan_iface = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
435                 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
436                 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
437
438                 tp_contact_list_newchannel_cb (DBUS_G_PROXY (priv->tp_conn),
439                                                object_path, chan_iface,
440                                                handle_type, handle,
441                                                FALSE,
442                                                EMPATHY_TP_CONTACT_LIST (list));
443
444                 g_value_array_free (chan_struct);
445         }
446
447         g_ptr_array_free (channels, TRUE);
448 }
449
450 static EmpathyContact *
451 tp_contact_list_find (EmpathyContactList *list,
452                       const gchar        *id)
453 {
454         EmpathyTpContactListPriv *priv;
455
456         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
457
458         priv = GET_PRIV (list);
459
460         return g_hash_table_find (priv->contacts,
461                                   (GHRFunc) tp_contact_list_find_foreach,
462                                   (gchar*) id);
463 }
464
465 static void
466 tp_contact_list_add (EmpathyContactList *list,
467                      EmpathyContact      *contact,
468                      const gchar        *message)
469 {
470         EmpathyTpContactListPriv *priv;
471         guint                     handle;
472
473         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
474
475         priv = GET_PRIV (list);
476
477         handle = empathy_contact_get_handle (contact);
478         empathy_tp_group_add_member (priv->subscribe, handle, message);
479 }
480
481 static void
482 tp_contact_list_remove (EmpathyContactList *list,
483                         EmpathyContact      *contact,
484                         const gchar        *message)
485 {
486         EmpathyTpContactListPriv *priv;
487         guint                     handle;
488
489         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
490
491         priv = GET_PRIV (list);
492
493         handle = empathy_contact_get_handle (contact);
494         empathy_tp_group_remove_member (priv->subscribe, handle, message);
495 }
496
497 static GList *
498 tp_contact_list_get_members (EmpathyContactList *list)
499 {
500         EmpathyTpContactListPriv *priv;
501
502         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
503
504         priv = GET_PRIV (list);
505
506         g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
507         return g_list_copy (priv->members);
508 }
509
510 static GList *
511 tp_contact_list_get_local_pending (EmpathyContactList *list)
512 {
513         EmpathyTpContactListPriv *priv;
514
515         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
516
517         priv = GET_PRIV (list);
518
519         return g_list_copy (priv->local_pending);
520 }
521
522 static void
523 tp_contact_list_process_pending (EmpathyContactList *list,
524                                  EmpathyContact      *contact,
525                                  gboolean            accept)
526 {
527         EmpathyTpContactListPriv *priv;
528         guint                     handle;
529
530         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
531         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
532
533         priv = GET_PRIV (list);
534
535         handle = empathy_contact_get_handle (contact);
536         if (accept) {
537                 empathy_tp_group_add_member (priv->publish, 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                         EmpathyContactListInfo *info;
1248
1249                         info = empathy_contact_list_info_new (contact, message);
1250                         priv->local_pending = g_list_prepend (priv->local_pending,
1251                                                               info);
1252
1253                         g_signal_emit_by_name (list, "local-pending",
1254                                                contact, message);
1255                 }
1256                 else if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1257                         if (!g_list_find (priv->members, contact)) {
1258                                 priv->members = g_list_prepend (priv->members,
1259                                                                 g_object_ref (contact));
1260                                 g_signal_emit_by_name (list, "contact-added", contact);
1261                         }
1262                 }
1263
1264                 g_object_unref (contact);
1265         }
1266
1267         g_list_free (pending_list);
1268 }
1269
1270 static void
1271 tp_contact_list_remove_local_pending (EmpathyTpContactList *list,
1272                                       EmpathyContact        *contact)
1273 {
1274         EmpathyTpContactListPriv *priv;
1275         GList                    *l;
1276
1277         priv = GET_PRIV (list);
1278
1279         for (l = priv->local_pending; l; l = l->next) {
1280                 EmpathyContactListInfo *info;
1281
1282                 info = l->data;
1283                 if (empathy_contact_equal (contact, info->contact)) {
1284                         empathy_debug (DEBUG_DOMAIN, "Contact no more local-pending: %s",
1285                                       empathy_contact_get_name (contact));
1286
1287                         priv->local_pending = g_list_delete_link (priv->local_pending, l);
1288                         empathy_contact_list_info_free (info);
1289                         break;
1290                 }
1291         }
1292 }
1293
1294 static void
1295 tp_contact_list_groups_updated_cb (EmpathyContact       *contact,
1296                                    GParamSpec           *param,
1297                                    EmpathyTpContactList *list)
1298 {
1299         EmpathyTpContactListPriv *priv;
1300         TpContactListData         data;
1301         GList                    *groups, *l;
1302
1303         priv = GET_PRIV (list);
1304
1305         /* Make sure all groups are created */
1306         groups = empathy_contact_get_groups (contact);
1307         for (l = groups; l; l = l->next) {
1308                 tp_contact_list_get_group (list, l->data);
1309         }
1310
1311         data.handle = empathy_contact_get_handle (contact);
1312         data.new_groups = groups;
1313
1314         g_hash_table_foreach (priv->groups,
1315                               (GHFunc) tp_contact_list_update_groups_foreach,
1316                               &data);
1317 }
1318
1319 static void
1320 tp_contact_list_name_updated_cb (EmpathyContact        *contact,
1321                                  GParamSpec           *param,
1322                                  EmpathyTpContactList *list)
1323 {
1324         EmpathyTpContactListPriv *priv;
1325         GHashTable               *new_alias;
1326         const gchar              *new_name;
1327         guint                     handle;
1328         GError                   *error = NULL;
1329
1330         priv = GET_PRIV (list);
1331         
1332         if (!priv->aliasing_iface) {
1333                 return;
1334         }
1335
1336         handle = empathy_contact_get_handle (contact);
1337         new_name = empathy_contact_get_name (contact);
1338
1339         empathy_debug (DEBUG_DOMAIN, "renaming handle %d to %s",
1340                       handle, new_name);
1341
1342         new_alias = g_hash_table_new_full (g_direct_hash,
1343                                            g_direct_equal,
1344                                            NULL,
1345                                            g_free);
1346
1347         g_hash_table_insert (new_alias,
1348                              GUINT_TO_POINTER (handle),
1349                              g_strdup (new_name));
1350
1351         if (!tp_conn_iface_aliasing_set_aliases (priv->aliasing_iface,
1352                                                  new_alias,
1353                                                  &error)) {
1354                 empathy_debug (DEBUG_DOMAIN, 
1355                               "Couldn't rename contact: %s",
1356                               error ? error->message : "No error given");
1357                 g_clear_error (&error);
1358         }
1359
1360         g_hash_table_destroy (new_alias);
1361 }
1362
1363 static void
1364 tp_contact_list_update_groups_foreach (gchar             *object_path,
1365                                        EmpathyTpGroup    *group,
1366                                        TpContactListData *data)
1367 {
1368         gboolean     is_member;
1369         gboolean     found = FALSE;
1370         const gchar *group_name;
1371         GList       *l;
1372
1373         is_member = empathy_tp_group_is_member (group, data->handle);
1374         group_name = empathy_tp_group_get_name (group);
1375
1376         for (l = data->new_groups; l; l = l->next) {
1377                 if (strcmp (group_name, l->data) == 0) {
1378                         found = TRUE;
1379                         break;
1380                 }
1381         }
1382
1383         if (is_member && !found) {
1384                 /* We are no longer member of this group */
1385                 empathy_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1386                               data->handle, group_name);
1387                 empathy_tp_group_remove_member (group, data->handle, "");
1388         }
1389
1390         if (!is_member && found) {
1391                 /* We are now member of this group */
1392                 empathy_debug (DEBUG_DOMAIN, "Contact %d added to group '%s'",
1393                               data->handle, group_name);
1394                 empathy_tp_group_add_member (group, data->handle, "");
1395         }
1396 }
1397
1398 static EmpathyTpGroup *
1399 tp_contact_list_get_group (EmpathyTpContactList *list,
1400                            const gchar          *name)
1401 {
1402         EmpathyTpContactListPriv *priv;
1403         EmpathyTpGroup           *group;
1404         TpChan                   *group_channel;
1405         GArray                   *handles;
1406         guint                     group_handle;
1407         char                     *group_object_path;
1408         const char               *names[2] = {name, NULL};
1409         GError                   *error = NULL;
1410
1411         priv = GET_PRIV (list);
1412
1413         group = g_hash_table_find (priv->groups,
1414                                    (GHRFunc) tp_contact_list_find_group,
1415                                    (gchar*) name);
1416         if (group) {
1417                 return group;
1418         }
1419
1420         empathy_debug (DEBUG_DOMAIN, "creating new group: %s", name);
1421
1422         if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
1423                                       TP_HANDLE_TYPE_GROUP,
1424                                       names,
1425                                       &handles,
1426                                       &error)) {
1427                 empathy_debug (DEBUG_DOMAIN,
1428                               "Couldn't request the creation of a new handle for group: %s",
1429                               error ? error->message : "No error given");
1430                 g_clear_error (&error);
1431                 return NULL;
1432         }
1433         group_handle = g_array_index (handles, guint, 0);
1434         g_array_free (handles, TRUE);
1435
1436         if (!tp_conn_request_channel (DBUS_G_PROXY (priv->tp_conn),
1437                                       TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1438                                       TP_HANDLE_TYPE_GROUP,
1439                                       group_handle,
1440                                       FALSE,
1441                                       &group_object_path,
1442                                       &error)) {
1443                 empathy_debug (DEBUG_DOMAIN,
1444                               "Couldn't request the creation of a new group channel: %s",
1445                               error ? error->message : "No error given");
1446                 g_clear_error (&error);
1447                 return NULL;
1448         }
1449
1450         group_channel = tp_chan_new (tp_get_bus (),
1451                                      dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn)),
1452                                      group_object_path,
1453                                      TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1454                                      TP_HANDLE_TYPE_GROUP,
1455                                      group_handle);
1456
1457         dbus_g_proxy_connect_signal (DBUS_G_PROXY (group_channel),
1458                                      "Closed",
1459                                      G_CALLBACK
1460                                      (tp_contact_list_group_channel_closed_cb),
1461                                      list,
1462                                      NULL);
1463
1464         group = empathy_tp_group_new (group_channel, priv->tp_conn);
1465         g_hash_table_insert (priv->groups, group_object_path, group);
1466         g_signal_connect (group, "members-added",
1467                           G_CALLBACK (tp_contact_list_group_members_added_cb),
1468                           list);
1469         g_signal_connect (group, "members-removed",
1470                           G_CALLBACK (tp_contact_list_group_members_removed_cb),
1471                           list);
1472
1473         return group;
1474 }
1475
1476 static gboolean
1477 tp_contact_list_find_group (gchar          *key,
1478                             EmpathyTpGroup *group,
1479                             gchar          *group_name)
1480 {
1481         if (strcmp (group_name, empathy_tp_group_get_name (group)) == 0) {
1482                 return TRUE;
1483         }
1484
1485         return FALSE;
1486 }
1487
1488 static void
1489 tp_contact_list_get_groups_foreach (gchar          *key,
1490                                     EmpathyTpGroup *group,
1491                                     GList          **groups)
1492 {
1493         const gchar *name;
1494
1495         name = empathy_tp_group_get_name (group);
1496         *groups = g_list_append (*groups, g_strdup (name));
1497 }
1498
1499 static void
1500 tp_contact_list_group_channel_closed_cb (TpChan             *channel,
1501                                          EmpathyTpContactList *list)
1502 {
1503         EmpathyTpContactListPriv *priv;
1504
1505         priv = GET_PRIV (list);
1506
1507         g_hash_table_remove (priv->groups,
1508                              dbus_g_proxy_get_path (DBUS_G_PROXY (channel)));
1509 }
1510
1511 static void
1512 tp_contact_list_group_members_added_cb (EmpathyTpGroup       *group,
1513                                         GArray               *members,
1514                                         guint                 actor_handle,
1515                                         guint                 reason,
1516                                         const gchar          *message,
1517                                         EmpathyTpContactList *list)
1518 {
1519         EmpathyTpContactListPriv *priv;
1520         GList                    *added_list, *l;
1521         const gchar              *group_name;
1522
1523         priv = GET_PRIV (list);
1524
1525         group_name = empathy_tp_group_get_name (group);
1526         added_list = empathy_tp_contact_list_get_from_handles (list, members);
1527
1528         for (l = added_list; l; l = l->next) {
1529                 EmpathyContact *contact;
1530
1531                 contact = EMPATHY_CONTACT (l->data);
1532
1533                 tp_contact_list_block_contact (list, contact);
1534                 empathy_contact_add_group (contact, group_name);
1535                 tp_contact_list_unblock_contact (list, contact);
1536
1537                 g_object_unref (contact);
1538         }
1539
1540         g_list_free (added_list);
1541 }
1542
1543 static void
1544 tp_contact_list_group_members_removed_cb (EmpathyTpGroup       *group,
1545                                           GArray               *members,
1546                                           guint                 actor_handle,
1547                                           guint                 reason,
1548                                           const gchar          *message,
1549                                           EmpathyTpContactList *list)
1550 {
1551         EmpathyTpContactListPriv *priv;
1552         GList                    *removed_list, *l;
1553         const gchar              *group_name;
1554
1555         priv = GET_PRIV (list);
1556
1557         group_name = empathy_tp_group_get_name (group);
1558         removed_list = empathy_tp_contact_list_get_from_handles (list, members);
1559
1560         for (l = removed_list; l; l = l->next) {
1561                 EmpathyContact *contact;
1562
1563                 contact = l->data;
1564
1565                 tp_contact_list_block_contact (list, contact);
1566                 empathy_contact_remove_group (contact, group_name);
1567                 tp_contact_list_unblock_contact (list, contact);
1568
1569                 g_object_unref (contact);
1570         }
1571
1572         g_list_free (removed_list);
1573 }
1574
1575 static void
1576 tp_contact_list_get_info (EmpathyTpContactList *list,
1577                           GArray               *handles)
1578 {
1579         EmpathyTpContactListPriv *priv;
1580         GError                   *error = NULL;
1581
1582         priv = GET_PRIV (list);
1583
1584         if (priv->presence_iface) {
1585                 /* FIXME: We should use GetPresence instead */
1586                 if (!tp_conn_iface_presence_request_presence (priv->presence_iface,
1587                                                               handles, &error)) {
1588                         empathy_debug (DEBUG_DOMAIN, 
1589                                       "Could not request presences: %s",
1590                                       error ? error->message : "No error given");
1591                         g_clear_error (&error);
1592                 }
1593         }
1594
1595         if (priv->aliasing_iface) {
1596                 TpContactListAliasesRequestData *data;
1597
1598                 data = g_slice_new (TpContactListAliasesRequestData);
1599                 data->list = list;
1600                 data->handles = g_memdup (handles->data, handles->len * sizeof (guint));
1601
1602                 tp_conn_iface_aliasing_request_aliases_async (priv->aliasing_iface,
1603                                                               handles,
1604                                                               (tp_conn_iface_aliasing_request_aliases_reply)
1605                                                               tp_contact_list_request_aliases_cb,
1606                                                               data);
1607         }
1608
1609         if (priv->avatars_iface) {
1610                 guint i;
1611
1612                 for (i = 0; i < handles->len; i++) {
1613                         guint handle;
1614
1615                         handle = g_array_index (handles, gint, i);
1616                         tp_contact_list_request_avatar (list, handle);
1617                 }
1618         }
1619 }
1620
1621 static void
1622 tp_contact_list_request_avatar (EmpathyTpContactList *list,
1623                                 guint                 handle)
1624 {
1625         EmpathyTpContactListPriv *priv;
1626
1627         priv = GET_PRIV (list);
1628         
1629         /* We queue avatar requests to not send too many dbus async
1630          * calls at once. If we don't we reach the dbus's limit of
1631          * pending calls */
1632         priv->avatar_requests_queue = g_list_append (priv->avatar_requests_queue,
1633                                                      GUINT_TO_POINTER (handle));
1634         tp_contact_list_start_avatar_requests (list);
1635 }
1636
1637 static void
1638 tp_contact_list_start_avatar_requests (EmpathyTpContactList *list)
1639 {
1640         EmpathyTpContactListPriv       *priv;
1641         TpContactListAvatarRequestData *data;
1642
1643         priv = GET_PRIV (list);
1644
1645         while (n_avatar_requests <  MAX_AVATAR_REQUESTS &&
1646                priv->avatar_requests_queue) {
1647                 data = g_slice_new (TpContactListAvatarRequestData);
1648                 data->list = list;
1649                 data->handle = GPOINTER_TO_UINT (priv->avatar_requests_queue->data);
1650
1651                 n_avatar_requests++;
1652                 priv->avatar_requests_queue = g_list_remove (priv->avatar_requests_queue,
1653                                                              priv->avatar_requests_queue->data);
1654
1655                 tp_conn_iface_avatars_request_avatar_async (priv->avatars_iface,
1656                                                             data->handle,
1657                                                             (tp_conn_iface_avatars_request_avatar_reply)
1658                                                             tp_contact_list_request_avatar_cb,
1659                                                             data);
1660         }
1661 }
1662
1663 static void
1664 tp_contact_list_avatar_update_cb (DBusGProxy           *proxy,
1665                                   guint                 handle,
1666                                   gchar                *new_token,
1667                                   EmpathyTpContactList *list)
1668 {
1669         EmpathyTpContactListPriv *priv;
1670
1671         priv = GET_PRIV (list);
1672
1673         if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1674                 /* We don't know this contact, skip */
1675                 return;
1676         }
1677
1678         empathy_debug (DEBUG_DOMAIN, "Changing avatar for %d to %s",
1679                       handle, new_token);
1680
1681         tp_contact_list_request_avatar (list, handle);
1682 }
1683
1684 static void
1685 tp_contact_list_request_avatar_cb (DBusGProxy                     *proxy,
1686                                    GArray                         *avatar_data,
1687                                    gchar                          *mime_type,
1688                                    GError                         *error,
1689                                    TpContactListAvatarRequestData *data)
1690 {
1691         EmpathyContact *contact;
1692
1693         contact = empathy_tp_contact_list_get_from_handle (data->list, data->handle);
1694
1695         if (error) {
1696                 empathy_debug (DEBUG_DOMAIN, "Error requesting avatar for %s: %s",
1697                               empathy_contact_get_name (contact),
1698                               error ? error->message : "No error given");
1699         } else {
1700                 EmpathyAvatar *avatar;
1701
1702                 avatar = empathy_avatar_new (avatar_data->data,
1703                                             avatar_data->len,
1704                                             mime_type);
1705                 tp_contact_list_block_contact (data->list, contact);
1706                 empathy_contact_set_avatar (contact, avatar);
1707                 tp_contact_list_unblock_contact (data->list, contact);
1708                 empathy_avatar_unref (avatar);
1709         }
1710
1711         n_avatar_requests--;
1712         tp_contact_list_start_avatar_requests (data->list);
1713
1714         g_object_unref (contact);
1715         g_slice_free (TpContactListAvatarRequestData, data);
1716 }
1717
1718 static void
1719 tp_contact_list_aliases_update_cb (DBusGProxy           *proxy,
1720                                    GPtrArray            *renamed_handlers,
1721                                    EmpathyTpContactList *list)
1722 {
1723         EmpathyTpContactListPriv *priv;
1724         guint                     i;
1725
1726         priv = GET_PRIV (list);
1727
1728         for (i = 0; renamed_handlers->len > i; i++) {
1729                 guint          handle;
1730                 const gchar   *alias;
1731                 GValueArray   *renamed_struct;
1732                 EmpathyContact *contact;
1733
1734                 renamed_struct = g_ptr_array_index (renamed_handlers, i);
1735                 handle = g_value_get_uint(g_value_array_get_nth (renamed_struct, 0));
1736                 alias = g_value_get_string(g_value_array_get_nth (renamed_struct, 1));
1737
1738                 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1739                         /* We don't know this contact, skip */
1740                         continue;
1741                 }
1742
1743                 if (G_STR_EMPTY (alias)) {
1744                         alias = NULL;
1745                 }
1746
1747                 contact = empathy_tp_contact_list_get_from_handle (list, handle);
1748                 tp_contact_list_block_contact (list, contact);
1749                 empathy_contact_set_name (contact, alias);
1750                 tp_contact_list_unblock_contact (list, contact);
1751                 g_object_unref (contact);
1752
1753                 empathy_debug (DEBUG_DOMAIN, "contact %d renamed to %s (update cb)",
1754                               handle, alias);
1755         }
1756 }
1757
1758 static void
1759 tp_contact_list_request_aliases_cb (DBusGProxy                       *proxy,
1760                                     gchar                           **contact_names,
1761                                     GError                           *error,
1762                                     TpContactListAliasesRequestData  *data)
1763 {
1764         guint   i = 0;
1765         gchar **name;
1766
1767         if (error) {
1768                 empathy_debug (DEBUG_DOMAIN, "Error requesting aliases: %s",
1769                               error->message);
1770         }
1771
1772         for (name = contact_names; *name && !error; name++) {
1773                 EmpathyContact *contact;
1774
1775                 contact = empathy_tp_contact_list_get_from_handle (data->list,
1776                                                                    data->handles[i]);
1777                 tp_contact_list_block_contact (data->list, contact);
1778                 empathy_contact_set_name (contact, *name);
1779                 tp_contact_list_unblock_contact (data->list, contact);
1780                 g_object_unref (contact);
1781
1782                 empathy_debug (DEBUG_DOMAIN, "contact %d renamed to %s (request cb)",
1783                               data->handles[i], *name);
1784
1785                 i++;
1786         }
1787
1788         g_free (data->handles);
1789         g_slice_free (TpContactListAliasesRequestData, data);
1790 }
1791
1792 static void
1793 tp_contact_list_presence_update_cb (DBusGProxy           *proxy,
1794                                     GHashTable           *handle_table,
1795                                     EmpathyTpContactList *list)
1796 {
1797         g_hash_table_foreach (handle_table,
1798                               (GHFunc) tp_contact_list_parse_presence_foreach,
1799                               list);
1800 }
1801
1802 static void
1803 tp_contact_list_parse_presence_foreach (guint                 handle,
1804                                         GValueArray          *presence_struct,
1805                                         EmpathyTpContactList *list)
1806 {
1807         EmpathyTpContactListPriv *priv;
1808         GHashTable     *presences_table;
1809         EmpathyContact  *contact;
1810         EmpathyPresence *presence = NULL;
1811
1812         priv = GET_PRIV (list);
1813
1814         if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1815                 /* We don't know this contact, skip */
1816                 return;
1817         }
1818
1819         contact = empathy_tp_contact_list_get_from_handle (list, handle);
1820         presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
1821
1822         g_hash_table_foreach (presences_table,
1823                               (GHFunc) tp_contact_list_presences_table_foreach,
1824                               &presence);
1825
1826         empathy_debug (DEBUG_DOMAIN, "Presence changed for %s (%d) to %s (%d)",
1827                       empathy_contact_get_name (contact),
1828                       handle,
1829                       presence ? empathy_presence_get_status (presence) : "unset",
1830                       presence ? empathy_presence_get_state (presence) : MC_PRESENCE_UNSET);
1831
1832         tp_contact_list_block_contact (list, contact);
1833         empathy_contact_set_presence (contact, presence);
1834         tp_contact_list_unblock_contact (list, contact);
1835
1836         g_object_unref (contact);
1837 }
1838
1839 static void
1840 tp_contact_list_presences_table_foreach (const gchar     *state_str,
1841                                          GHashTable      *presences_table,
1842                                          EmpathyPresence **presence)
1843 {
1844         McPresence    state;
1845         const GValue *message;
1846
1847         state = empathy_presence_state_from_str (state_str);
1848         if ((state == MC_PRESENCE_UNSET) || (state == MC_PRESENCE_OFFLINE)) {
1849                 return;
1850         }
1851
1852         if (*presence) {
1853                 g_object_unref (*presence);
1854                 *presence = NULL;
1855         }
1856
1857         *presence = empathy_presence_new ();
1858         empathy_presence_set_state (*presence, state);
1859
1860         message = g_hash_table_lookup (presences_table, "message");
1861         if (message != NULL) {
1862                 empathy_presence_set_status (*presence,
1863                                             g_value_get_string (message));
1864         }
1865 }
1866
1867 static void
1868 tp_contact_list_status_changed_cb (MissionControl                  *mc,
1869                                    TelepathyConnectionStatus        status,
1870                                    McPresence                       presence,
1871                                    TelepathyConnectionStatusReason  reason,
1872                                    const gchar                     *unique_name,
1873                                    EmpathyTpContactList            *list)
1874 {
1875         EmpathyTpContactListPriv *priv;
1876         McAccount                *account;
1877
1878         priv = GET_PRIV (list);
1879
1880         account = mc_account_lookup (unique_name);
1881         if (status != TP_CONN_STATUS_DISCONNECTED ||
1882             !empathy_account_equal (account, priv->account) ||
1883             !priv->tp_conn) {
1884                 g_object_unref (account);
1885                 return;
1886         }
1887
1888         /* We are disconnected, do just like if the connection was destroyed */
1889         g_signal_handlers_disconnect_by_func (priv->tp_conn,
1890                                               tp_contact_list_destroy_cb,
1891                                               list);
1892         tp_contact_list_destroy_cb (DBUS_G_PROXY (priv->tp_conn), list);
1893
1894         g_object_unref (account);
1895 }
1896