]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-contact-list.c
Completely reworked ContactList API. Fixes bug #471611, bug #467280, bug #459540...
[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 #include <glib/gi18n.h>
28
29 #include <libtelepathy/tp-helpers.h>
30 #include <libtelepathy/tp-conn.h>
31 #include <libtelepathy/tp-chan.h>
32 #include <libtelepathy/tp-chan-type-contact-list-gen.h>
33
34 #include "empathy-tp-contact-list.h"
35 #include "empathy-contact-list.h"
36 #include "empathy-tp-group.h"
37 #include "empathy-debug.h"
38 #include "empathy-utils.h"
39
40 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
41                        EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv))
42
43 #define DEBUG_DOMAIN "TpContactList"
44
45 struct _EmpathyTpContactListPriv {
46         TpConn         *tp_conn;
47         McAccount      *account;
48         MissionControl *mc;
49         const gchar    *protocol_group;
50
51         EmpathyTpGroup *publish;
52         EmpathyTpGroup *subscribe;
53         GList          *members;
54         GList          *pendings;
55
56         GList          *groups;
57         GHashTable     *contacts_groups;
58 };
59
60 typedef enum {
61         TP_CONTACT_LIST_TYPE_PUBLISH,
62         TP_CONTACT_LIST_TYPE_SUBSCRIBE,
63         TP_CONTACT_LIST_TYPE_UNKNOWN
64 } TpContactListType;
65
66 static void empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass);
67 static void empathy_tp_contact_list_init       (EmpathyTpContactList      *list);
68 static void tp_contact_list_iface_init         (EmpathyContactListIface   *iface);
69
70 enum {
71         DESTROY,
72         LAST_SIGNAL
73 };
74
75 static guint signals[LAST_SIGNAL];
76
77 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
78                          G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
79                                                 tp_contact_list_iface_init));
80
81 static void
82 tp_contact_list_group_destroy_cb (EmpathyTpGroup       *group,
83                                   EmpathyTpContactList *list)
84 {
85         EmpathyTpContactListPriv *priv = GET_PRIV (list);
86
87         empathy_debug (DEBUG_DOMAIN, "Group destroyed: %s",
88                        empathy_tp_group_get_name (group));
89
90         priv->groups = g_list_remove (priv->groups, group);
91         g_object_unref (group);
92 }
93
94 static void
95 tp_contact_list_group_member_added_cb (EmpathyTpGroup       *group,
96                                        EmpathyContact       *contact,
97                                        EmpathyContact       *actor,
98                                        guint                 reason,
99                                        const gchar          *message,
100                                        EmpathyTpContactList *list)
101 {
102         EmpathyTpContactListPriv  *priv = GET_PRIV (list);
103         const gchar               *group_name;
104         GList                    **groups;
105
106         if (!g_list_find (priv->members, contact)) {
107                 return;
108         }
109
110         groups = g_hash_table_lookup (priv->contacts_groups, contact);
111         if (!groups) {
112                 groups = g_slice_new0 (GList*);
113                 g_hash_table_insert (priv->contacts_groups,
114                                      g_object_ref (contact),
115                                      groups);
116         }
117
118         group_name = empathy_tp_group_get_name (group);
119         if (!g_list_find_custom (*groups, group_name, (GCompareFunc) strcmp)) {
120                 empathy_debug (DEBUG_DOMAIN, "Contact %s (%d) added to group %s",
121                                empathy_contact_get_id (contact),
122                                empathy_contact_get_handle (contact),
123                                group_name);
124                 *groups = g_list_prepend (*groups, g_strdup (group_name));
125                 g_signal_emit_by_name (list, "groups-changed", contact,
126                                        group_name,
127                                        TRUE);
128         }
129 }
130
131 static void
132 tp_contact_list_group_member_removed_cb (EmpathyTpGroup       *group,
133                                          EmpathyContact       *contact,
134                                          EmpathyContact       *actor,
135                                          guint                 reason,
136                                          const gchar          *message,
137                                          EmpathyTpContactList *list)
138 {
139         EmpathyTpContactListPriv  *priv = GET_PRIV (list);
140         const gchar               *group_name;
141         GList                    **groups, *l;
142
143         if (!g_list_find (priv->members, contact)) {
144                 return;
145         }
146
147         groups = g_hash_table_lookup (priv->contacts_groups, contact);
148         if (!groups) {
149                 return;
150         }
151
152         group_name = empathy_tp_group_get_name (group);
153         if ((l = g_list_find_custom (*groups, group_name, (GCompareFunc) strcmp))) {
154                 empathy_debug (DEBUG_DOMAIN, "Contact %s (%d) removed from group %s",
155                                empathy_contact_get_id (contact),
156                                empathy_contact_get_handle (contact),
157                                group_name);
158                 *groups = g_list_delete_link (*groups, l);
159                 g_signal_emit_by_name (list, "groups-changed", contact,
160                                        group_name,
161                                        FALSE);
162         }
163 }
164
165 static EmpathyTpGroup *
166 tp_contact_list_find_group (EmpathyTpContactList *list,
167                             const gchar          *group)
168 {
169         EmpathyTpContactListPriv *priv = GET_PRIV (list);
170         GList                    *l;
171
172         for (l = priv->groups; l; l = l->next) {
173                 if (strcmp (group, empathy_tp_group_get_name (l->data)) == 0) {
174                         return l->data;
175                 }
176         }
177         return NULL;
178 }
179
180 static TpContactListType
181 tp_contact_list_get_type (EmpathyTpContactList *list,
182                           EmpathyTpGroup       *group)
183 {
184         EmpathyTpContactListPriv *priv;
185         TpContactListType         list_type;
186         const gchar              *name;
187
188         priv = GET_PRIV (list);
189
190         name = empathy_tp_group_get_name (group);
191         if (strcmp (name, "subscribe") == 0) {
192                 list_type = TP_CONTACT_LIST_TYPE_SUBSCRIBE;
193         } else if (strcmp (name, "publish") == 0) {
194                 list_type = TP_CONTACT_LIST_TYPE_PUBLISH;
195         } else {
196                 list_type = TP_CONTACT_LIST_TYPE_UNKNOWN;
197         }
198
199         return list_type;
200 }
201
202 static void
203 tp_contact_list_add_member (EmpathyTpContactList *list,
204                             EmpathyContact       *contact,
205                             EmpathyContact       *actor,
206                             guint                 reason,
207                             const gchar          *message)
208 {
209         EmpathyTpContactListPriv *priv = GET_PRIV (list);
210         GList                    *l;
211
212         /* Add to the list and emit signal */
213         priv->members = g_list_prepend (priv->members, g_object_ref (contact));
214         g_signal_emit_by_name (list, "members-changed",
215                                contact, actor, reason, message,
216                                TRUE);
217
218         /* This contact is now member, implicitly accept pending. */
219         if (g_list_find (priv->pendings, contact)) {
220                 empathy_tp_group_add_member (priv->publish, contact, "");
221         }
222
223         /* Update groups of the contact */
224         for (l = priv->groups; l; l = l->next) {
225                 if (empathy_tp_group_is_member (l->data, contact)) {
226                         tp_contact_list_group_member_added_cb (l->data, contact,
227                                                                NULL, 0, NULL, 
228                                                                list);
229                 }
230         }
231 }
232
233 static void
234 tp_contact_list_added_cb (EmpathyTpGroup       *group,
235                           EmpathyContact       *contact,
236                           EmpathyContact       *actor,
237                           guint                 reason,
238                           const gchar          *message,
239                           EmpathyTpContactList *list)
240 {
241         EmpathyTpContactListPriv *priv = GET_PRIV (list);
242         TpContactListType         list_type;
243
244         list_type = tp_contact_list_get_type (list, group);
245         empathy_debug (DEBUG_DOMAIN, "Contact %s (%d) added to list type %d",
246                       empathy_contact_get_id (contact),
247                       empathy_contact_get_handle (contact),
248                       list_type);
249
250         /* We now get the presence of that contact, add it to members */
251         if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE &&
252             !g_list_find (priv->members, contact)) {
253                 tp_contact_list_add_member (list, contact, actor, reason, message);
254         }
255
256         /* We now send our presence to that contact, remove it from pendings */
257         if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH &&
258             g_list_find (priv->pendings, contact)) {
259                 g_signal_emit_by_name (list, "pendings-changed",
260                                        contact, actor, reason, message,
261                                        FALSE);
262                 priv->pendings = g_list_remove (priv->pendings, contact);
263                 g_object_unref (contact);
264         }
265 }
266
267 static void
268 tp_contact_list_removed_cb (EmpathyTpGroup       *group,
269                             EmpathyContact       *contact,
270                             EmpathyContact       *actor,
271                             guint                 reason,
272                             const gchar          *message,
273                             EmpathyTpContactList *list)
274 {
275         EmpathyTpContactListPriv *priv = GET_PRIV (list);
276         TpContactListType         list_type;
277
278         list_type = tp_contact_list_get_type (list, group);
279         empathy_debug (DEBUG_DOMAIN, "Contact %s (%d) removed from list type %d",
280                       empathy_contact_get_id (contact),
281                       empathy_contact_get_handle (contact),
282                       list_type);
283
284         /* This contact refuses to send us his presence, remove from members. */
285         if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE &&
286             g_list_find (priv->members, contact)) {
287                 g_signal_emit_by_name (list, "members-changed",
288                                        contact, actor, reason, message,
289                                        FALSE);
290                 priv->members = g_list_remove (priv->members, contact);
291                 g_object_unref (contact);
292         }
293
294         /* We refuse to send our presence to that contact, remove from pendings */
295         if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH &&
296             g_list_find (priv->pendings, contact)) {
297                 g_signal_emit_by_name (list, "pendings-changed",
298                                        contact, actor, reason, message,
299                                        FALSE);
300                 priv->pendings = g_list_remove (priv->pendings, contact);
301                 g_object_unref (contact);
302         }
303 }
304
305 static void
306 tp_contact_list_pending_cb (EmpathyTpGroup       *group,
307                             EmpathyContact       *contact,
308                             EmpathyContact       *actor,
309                             guint                 reason,
310                             const gchar          *message,
311                             EmpathyTpContactList *list)
312 {
313         EmpathyTpContactListPriv *priv = GET_PRIV (list);
314         TpContactListType         list_type;
315
316         list_type = tp_contact_list_get_type (list, group);
317         empathy_debug (DEBUG_DOMAIN, "Contact %s (%d) pending in list type %d",
318                       empathy_contact_get_id (contact),
319                       empathy_contact_get_handle (contact),
320                       list_type);
321
322         /* We want this contact in our contact list but we don't get its 
323          * presence yet. Add to members anyway. */
324         if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE &&
325             !g_list_find (priv->members, contact)) {
326                 tp_contact_list_add_member (list, contact, actor, reason, message);
327         }
328
329         /* This contact wants our presence, auto accept if he is member,
330          * otherwise he is pending. */
331         if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH &&
332             !g_list_find (priv->pendings, contact)) {
333                 if (g_list_find (priv->members, contact)) {
334                         empathy_tp_group_add_member (priv->publish, contact, "");
335                 } else {
336                         priv->pendings = g_list_prepend (priv->pendings,
337                                                          g_object_ref (contact));
338                         g_signal_emit_by_name (list, "pendings-changed",
339                                                contact, actor, reason, message,
340                                                TRUE);
341                 }
342         }
343 }
344
345 static void
346 tp_contact_list_newchannel_cb (DBusGProxy           *proxy,
347                                const gchar          *object_path,
348                                const gchar          *channel_type,
349                                TelepathyHandleType   handle_type,
350                                guint                 channel_handle,
351                                gboolean              suppress_handle,
352                                EmpathyTpContactList *list)
353 {
354         EmpathyTpContactListPriv *priv = GET_PRIV (list);
355         EmpathyTpGroup           *group;
356         TpChan                   *new_chan;
357         const gchar              *bus_name;
358
359         if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) != 0 ||
360             suppress_handle) {
361                 return;
362         }
363
364         bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn));
365         new_chan = tp_chan_new (tp_get_bus (),
366                                 bus_name,
367                                 object_path,
368                                 channel_type,
369                                 handle_type,
370                                 channel_handle);
371         g_return_if_fail (TELEPATHY_IS_CHAN (new_chan));
372
373         group = empathy_tp_group_new (priv->account, new_chan);
374         g_object_unref (new_chan);
375
376         if (handle_type == TP_HANDLE_TYPE_LIST) {
377                 TpContactListType  list_type;
378                 GList             *contacts, *l;
379
380                 list_type = tp_contact_list_get_type (list, group);
381                 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH && !priv->publish) {
382                         priv->publish = group;
383
384                         /* Publish is the list of contacts to who we send our
385                          * presence. Makes no sense to be in remote-pending */
386                         g_signal_connect (group, "local-pending",
387                                           G_CALLBACK (tp_contact_list_pending_cb),
388                                           list);
389
390                         contacts = empathy_tp_group_get_local_pendings (group);
391                         for (l = contacts; l; l = l->next) {
392                                 EmpathyPendingInfo *info = l->data;
393
394                                 tp_contact_list_pending_cb (group,
395                                                             info->member,
396                                                             info->actor,
397                                                             0,
398                                                             info->message,
399                                                             list);
400                                 empathy_pending_info_free (info);
401                         }
402                         g_list_free (contacts);
403                 }
404                 else if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE && !priv->subscribe) {
405                         priv->subscribe = group;
406
407                         /* Subscribe is the list of contacts from who we
408                          * receive presence. Makes no sense to be in
409                          * local-pending */
410                         g_signal_connect (group, "remote-pending",
411                                           G_CALLBACK (tp_contact_list_pending_cb),
412                                           list);
413
414                         contacts = empathy_tp_group_get_remote_pendings (group);
415                         for (l = contacts; l; l = l->next) {
416                                 tp_contact_list_pending_cb (group,
417                                                             l->data,
418                                                             NULL, 0,
419                                                             NULL, list);
420                                 g_object_unref (l->data);
421                         }
422                         g_list_free (contacts);
423                 } else {
424                         empathy_debug (DEBUG_DOMAIN,
425                                       "Type of contact list channel unknown "
426                                       "or aleady have that list: %s",
427                                       empathy_tp_group_get_name (group));
428                         g_object_unref (group);
429                         return;
430                 }
431                 empathy_debug (DEBUG_DOMAIN,
432                                "New contact list channel of type: %d",
433                                list_type);
434
435                 g_signal_connect (group, "member-added",
436                                   G_CALLBACK (tp_contact_list_added_cb),
437                                   list);
438                 g_signal_connect (group, "member-removed",
439                                   G_CALLBACK (tp_contact_list_removed_cb),
440                                   list);
441
442                 contacts = empathy_tp_group_get_members (group);
443                 for (l = contacts; l; l = l->next) {
444                         tp_contact_list_added_cb (group,
445                                                   l->data,
446                                                   NULL, 0, NULL,
447                                                   list);
448                         g_object_unref (l->data);
449                 }
450                 g_list_free (contacts);
451         }
452         else if (handle_type == TP_HANDLE_TYPE_GROUP) {
453                 const gchar *group_name;
454                 GList       *contacts, *l;
455
456                 /* Check if already exists */
457                 group_name = empathy_tp_group_get_name (group);
458                 if (tp_contact_list_find_group (list, group_name)) {
459                         g_object_unref (group);
460                         return;
461                 }
462
463                 empathy_debug (DEBUG_DOMAIN, "New server-side group channel: %s",
464                                group_name);
465
466                 priv->groups = g_list_prepend (priv->groups, group);
467
468                 g_signal_connect (group, "member-added",
469                                   G_CALLBACK (tp_contact_list_group_member_added_cb),
470                                   list);
471                 g_signal_connect (group, "member-removed",
472                                   G_CALLBACK (tp_contact_list_group_member_removed_cb),
473                                   list);
474                 g_signal_connect (group, "destroy",
475                                   G_CALLBACK (tp_contact_list_group_destroy_cb),
476                                   list);
477
478                 contacts = empathy_tp_group_get_members (group);
479                 for (l = contacts; l; l = l->next) {
480                         tp_contact_list_group_member_added_cb (group, l->data,
481                                                                NULL, 0, NULL,
482                                                                list);
483                         g_object_unref (l->data);
484                 }
485                 g_list_free (contacts);
486         } else {
487                 empathy_debug (DEBUG_DOMAIN,
488                                "Unknown handle type (%d) for contact list channel",
489                                handle_type);
490                 g_object_unref (group);
491         }
492 }
493
494 static void
495 tp_contact_list_remove_all (EmpathyTpContactList *list)
496 {
497         EmpathyTpContactListPriv *priv = GET_PRIV (list);
498         GList                    *l;
499
500         for (l = priv->members; l; l = l->next) {
501                 g_signal_emit_by_name (list, "members-changed", l->data,
502                                        NULL, 0, NULL,
503                                        FALSE);
504                 g_object_unref (l->data);
505         }
506         for (l = priv->pendings; l; l = l->next) {
507                 g_signal_emit_by_name (list, "pendings-changed", l->data,
508                                        NULL, 0, NULL,
509                                        FALSE);
510                 g_object_unref (l->data);
511         }
512
513         g_list_free (priv->members);
514         g_list_free (priv->pendings);
515         priv->members = NULL;
516         priv->pendings = NULL;
517 }
518
519 static void
520 tp_contact_list_destroy_cb (TpConn               *tp_conn,
521                             EmpathyTpContactList *list)
522 {
523         EmpathyTpContactListPriv *priv = GET_PRIV (list);
524
525         empathy_debug (DEBUG_DOMAIN, "Account disconnected or CM crashed");
526
527         /* DBus proxie should NOT be used anymore */
528         g_object_unref (priv->tp_conn);
529         priv->tp_conn = NULL;
530
531         tp_contact_list_remove_all (list);
532
533         /* Tell the world to not use us anymore */
534         g_signal_emit (list, signals[DESTROY], 0);
535 }
536
537 static void
538 tp_contact_list_disconnect (EmpathyTpContactList *list)
539 {
540         EmpathyTpContactListPriv *priv = GET_PRIV (list);
541
542         if (priv->tp_conn) {
543                 g_signal_handlers_disconnect_by_func (priv->tp_conn,
544                                                       tp_contact_list_destroy_cb,
545                                                       list);
546                 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
547                                                 G_CALLBACK (tp_contact_list_newchannel_cb),
548                                                 list);
549         }
550 }
551
552 static void
553 tp_contact_list_status_changed_cb (MissionControl                  *mc,
554                                    TelepathyConnectionStatus        status,
555                                    McPresence                       presence,
556                                    TelepathyConnectionStatusReason  reason,
557                                    const gchar                     *unique_name,
558                                    EmpathyTpContactList            *list)
559 {
560         EmpathyTpContactListPriv *priv = GET_PRIV (list);
561         McAccount                *account;
562
563         account = mc_account_lookup (unique_name);
564         if (status != TP_CONN_STATUS_CONNECTED &&
565             empathy_account_equal (account, priv->account)) {
566                 /* We are disconnected */
567                 tp_contact_list_disconnect (list);
568                 tp_contact_list_destroy_cb (priv->tp_conn, list);
569         }
570
571         g_object_unref (account);
572 }
573
574 static void
575 tp_contact_list_group_list_free (GList **groups)
576 {
577         g_list_foreach (*groups, (GFunc) g_free, NULL);
578         g_list_free (*groups);
579         g_slice_free (GList*, groups);
580 }
581
582 static void
583 tp_contact_list_finalize (GObject *object)
584 {
585         EmpathyTpContactListPriv *priv;
586         EmpathyTpContactList     *list;
587
588         list = EMPATHY_TP_CONTACT_LIST (object);
589         priv = GET_PRIV (list);
590
591         empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
592
593         tp_contact_list_disconnect (list);
594         tp_contact_list_remove_all (list);
595
596         if (priv->mc) {
597                 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
598                                                 "AccountStatusChanged",
599                                                 G_CALLBACK (tp_contact_list_status_changed_cb),
600                                                 list);
601                 g_object_unref (priv->mc);
602         }
603
604         if (priv->subscribe) {
605                 g_object_unref (priv->subscribe);
606         }
607         if (priv->publish) {
608                 g_object_unref (priv->publish);
609         }
610         if (priv->account) {
611                 g_object_unref (priv->account);
612         }
613         if (priv->tp_conn) {
614                 g_object_unref (priv->tp_conn);
615         }
616
617         g_hash_table_destroy (priv->contacts_groups);
618         g_list_foreach (priv->groups, (GFunc) g_object_unref, NULL);
619         g_list_free (priv->groups);
620
621         G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
622 }
623
624 static void
625 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
626 {
627         GObjectClass *object_class = G_OBJECT_CLASS (klass);
628
629         object_class->finalize = tp_contact_list_finalize;
630
631         signals[DESTROY] =
632                 g_signal_new ("destroy",
633                               G_TYPE_FROM_CLASS (klass),
634                               G_SIGNAL_RUN_LAST,
635                               0,
636                               NULL, NULL,
637                               g_cclosure_marshal_VOID__VOID,
638                               G_TYPE_NONE,
639                               0);
640
641         g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
642 }
643
644 static void
645 empathy_tp_contact_list_init (EmpathyTpContactList *list)
646 {
647 }
648
649 static void
650 tp_contact_list_setup (EmpathyTpContactList *list)
651 {
652         EmpathyTpContactListPriv *priv = GET_PRIV (list);
653         GPtrArray                *channels;
654         guint                     i;
655         GError                   *error = NULL;
656
657         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
658
659         /* Get existing channels */
660         if (!tp_conn_list_channels (DBUS_G_PROXY (priv->tp_conn),
661                                     &channels,
662                                     &error)) {
663                 empathy_debug (DEBUG_DOMAIN,
664                               "Failed to get list of open channels: %s",
665                               error ? error->message : "No error given");
666                 g_clear_error (&error);
667                 return;
668         }
669
670         for (i = 0; i < channels->len; i++) {
671                 GValueArray         *chan_struct;
672                 const gchar         *object_path;
673                 const gchar         *chan_iface;
674                 TelepathyHandleType  handle_type;
675                 guint                handle;
676
677                 chan_struct = g_ptr_array_index (channels, i);
678                 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
679                 chan_iface = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
680                 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
681                 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
682
683                 tp_contact_list_newchannel_cb (DBUS_G_PROXY (priv->tp_conn),
684                                                object_path, chan_iface,
685                                                handle_type, handle,
686                                                FALSE,
687                                                list);
688
689                 g_value_array_free (chan_struct);
690         }
691         g_ptr_array_free (channels, TRUE);
692 }
693
694 EmpathyTpContactList *
695 empathy_tp_contact_list_new (McAccount *account)
696 {
697         EmpathyTpContactListPriv *priv;
698         EmpathyTpContactList     *list;
699         MissionControl           *mc;
700         TpConn                   *tp_conn = NULL;
701         McProfile                *profile;
702         const gchar              *protocol_name;
703
704         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
705
706         mc = empathy_mission_control_new ();
707
708         /* status==0 means CONNECTED */
709         if (mission_control_get_connection_status (mc, account, NULL) == 0) {
710                 tp_conn = mission_control_get_connection (mc, account, NULL);
711         }
712         if (!tp_conn) {
713                 /* The account is not connected, nothing to do. */
714                 g_object_unref (mc);
715                 return NULL;
716         }
717
718         list = g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST, NULL);
719         priv = GET_PRIV (list);
720
721         priv->tp_conn = tp_conn;
722         priv->account = g_object_ref (account);
723         priv->mc = mc;
724         priv->contacts_groups = g_hash_table_new_full (empathy_contact_hash,
725                                                        empathy_contact_equal,
726                                                        (GDestroyNotify) g_object_unref,
727                                                        (GDestroyNotify) tp_contact_list_group_list_free);
728
729         /* Check for protocols that does not support contact groups. We can
730          * put all contacts into a special group in that case.
731          * FIXME: Default group should be an information in the profile */
732         profile = mc_account_get_profile (account);
733         protocol_name = mc_profile_get_protocol_name (profile);
734         if (strcmp (protocol_name, "local-xmpp") == 0) {
735                 priv->protocol_group = _("People nearby");
736         }
737         g_object_unref (profile);
738
739         /* Connect signals */
740         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
741                                      "AccountStatusChanged",
742                                      G_CALLBACK (tp_contact_list_status_changed_cb),
743                                      list, NULL);
744         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
745                                      G_CALLBACK (tp_contact_list_newchannel_cb),
746                                      list, NULL);
747         g_signal_connect (priv->tp_conn, "destroy",
748                           G_CALLBACK (tp_contact_list_destroy_cb),
749                           list);
750
751         tp_contact_list_setup (list);
752
753         return list;
754 }
755
756 McAccount *
757 empathy_tp_contact_list_get_account (EmpathyTpContactList *list)
758 {
759         EmpathyTpContactListPriv *priv;
760
761         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
762
763         priv = GET_PRIV (list);
764
765         return priv->account;
766 }
767
768 static void
769 tp_contact_list_add (EmpathyContactList *list,
770                      EmpathyContact     *contact,
771                      const gchar        *message)
772 {
773         EmpathyTpContactListPriv *priv = GET_PRIV (list);
774
775         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
776
777         empathy_tp_group_add_member (priv->subscribe, contact, message);
778         if (g_list_find (priv->pendings, contact)) {
779                 empathy_tp_group_add_member (priv->publish, contact, message);          
780         }
781 }
782
783 static void
784 tp_contact_list_remove (EmpathyContactList *list,
785                         EmpathyContact     *contact,
786                         const gchar        *message)
787 {
788         EmpathyTpContactListPriv *priv = GET_PRIV (list);
789
790         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
791
792         empathy_tp_group_remove_member (priv->subscribe, contact, message);
793         empathy_tp_group_remove_member (priv->publish, contact, message);               
794 }
795
796 static GList *
797 tp_contact_list_get_members (EmpathyContactList *list)
798 {
799         EmpathyTpContactListPriv *priv = GET_PRIV (list);
800
801         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
802
803         g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
804         return g_list_copy (priv->members);
805 }
806
807 static GList *
808 tp_contact_list_get_pendings (EmpathyContactList *list)
809 {
810         EmpathyTpContactListPriv *priv = GET_PRIV (list);
811
812         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
813
814         g_list_foreach (priv->pendings, (GFunc) g_object_ref, NULL);
815         return g_list_copy (priv->pendings);
816 }
817
818 static GList *
819 tp_contact_list_get_all_groups (EmpathyContactList *list)
820 {
821         EmpathyTpContactListPriv *priv = GET_PRIV (list);
822         GList                    *groups = NULL, *l;
823
824         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
825
826         for (l = priv->groups; l; l = l->next) {
827                 const gchar *name;
828
829                 name = empathy_tp_group_get_name (l->data);
830                 groups = g_list_prepend (groups, g_strdup (name));
831         }
832
833         return groups;
834 }
835
836 static GList *
837 tp_contact_list_get_groups (EmpathyContactList *list,
838                             EmpathyContact     *contact)
839 {
840         EmpathyTpContactListPriv  *priv = GET_PRIV (list);
841         GList                    **groups;
842         GList                     *ret = NULL, *l;
843
844         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
845
846         groups = g_hash_table_lookup (priv->contacts_groups, contact);
847         if (!groups) {
848                 return NULL;
849         }
850
851         for (l = *groups; l; l = l->next) {
852                 ret = g_list_prepend (ret, g_strdup (l->data));
853         }
854
855         return ret;
856 }
857
858 static EmpathyTpGroup *
859 tp_contact_list_get_group (EmpathyTpContactList *list,
860                            const gchar          *group)
861 {
862         EmpathyTpContactListPriv *priv = GET_PRIV (list);
863         EmpathyTpGroup           *tp_group;
864         gchar                    *object_path;
865         guint                     handle;
866         GArray                   *handles;
867         const char               *names[2] = {group, NULL};
868         GError                   *error = NULL;
869
870         tp_group = tp_contact_list_find_group (list, group);
871         if (tp_group) {
872                 return tp_group;
873         }
874
875         empathy_debug (DEBUG_DOMAIN, "creating new group: %s", group);
876
877         if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
878                                       TP_HANDLE_TYPE_GROUP,
879                                       names,
880                                       &handles,
881                                       &error)) {
882                 empathy_debug (DEBUG_DOMAIN,
883                               "Failed to RequestHandles: %s",
884                               error ? error->message : "No error given");
885                 g_clear_error (&error);
886                 return NULL;
887         }
888         handle = g_array_index (handles, guint, 0);
889         g_array_free (handles, TRUE);
890
891         if (!tp_conn_request_channel (DBUS_G_PROXY (priv->tp_conn),
892                                       TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
893                                       TP_HANDLE_TYPE_GROUP,
894                                       handle,
895                                       FALSE,
896                                       &object_path,
897                                       &error)) {
898                 empathy_debug (DEBUG_DOMAIN,
899                               "Failed to RequestChannel: %s",
900                               error ? error->message : "No error given");
901                 g_clear_error (&error);
902                 return NULL;
903         }
904
905         tp_contact_list_newchannel_cb (DBUS_G_PROXY (priv->tp_conn),
906                                        object_path,
907                                        TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
908                                        TP_HANDLE_TYPE_GROUP,
909                                        handle, FALSE,
910                                        list);
911         g_free (object_path);
912
913         return tp_contact_list_find_group (list, group);
914 }
915
916 static void
917 tp_contact_list_add_to_group (EmpathyContactList *list,
918                               EmpathyContact     *contact,
919                               const gchar        *group)
920 {
921         EmpathyTpGroup *tp_group;
922
923         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
924
925         tp_group = tp_contact_list_get_group (EMPATHY_TP_CONTACT_LIST (list),
926                                               group);
927
928         empathy_tp_group_add_member (tp_group, contact, "");
929 }
930
931 static void
932 tp_contact_list_remove_from_group (EmpathyContactList *list,
933                                    EmpathyContact     *contact,
934                                    const gchar        *group)
935 {
936         EmpathyTpGroup *tp_group;
937
938         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
939
940         tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
941                                                group);
942
943         if (tp_group) {
944                 empathy_tp_group_remove_member (tp_group, contact, "");
945         }
946 }
947
948 static void
949 tp_contact_list_rename_group (EmpathyContactList *list,
950                               const gchar        *old_group,
951                               const gchar        *new_group)
952 {
953         EmpathyTpGroup *tp_group;
954         GList          *members;
955
956         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
957
958         tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
959                                                old_group);
960         if (!tp_group) {
961                 return;
962         }
963
964         empathy_debug (DEBUG_DOMAIN, "rename group %s to %s", old_group, new_group);
965
966         /* Remove all members from the old group */
967         members = empathy_tp_group_get_members (tp_group);
968         empathy_tp_group_remove_members (tp_group, members, "");
969         empathy_tp_group_close (tp_group);
970
971         /* Add all members to the new group */
972         tp_group = tp_contact_list_get_group (EMPATHY_TP_CONTACT_LIST (list),
973                                               new_group);
974         empathy_tp_group_add_members (tp_group, members, "");
975
976         g_list_foreach (members, (GFunc) g_object_unref, NULL);
977         g_list_free (members);
978 }
979
980 static void
981 tp_contact_list_iface_init (EmpathyContactListIface *iface)
982 {
983         iface->add               = tp_contact_list_add;
984         iface->remove            = tp_contact_list_remove;
985         iface->get_members       = tp_contact_list_get_members;
986         iface->get_pendings      = tp_contact_list_get_pendings;
987         iface->get_all_groups    = tp_contact_list_get_all_groups;
988         iface->get_groups        = tp_contact_list_get_groups;
989         iface->add_to_group      = tp_contact_list_add_to_group;
990         iface->remove_from_group = tp_contact_list_remove_from_group;
991         iface->rename_group      = tp_contact_list_rename_group;
992 }
993