]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-contact-list.c
Add back protocol group.
[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_destroy_cb (TpConn               *tp_conn,
496                             EmpathyTpContactList *list)
497 {
498         EmpathyTpContactListPriv *priv = GET_PRIV (list);
499         GList                    *l;
500
501         empathy_debug (DEBUG_DOMAIN, "Account disconnected or CM crashed");
502
503         /* DBus proxie should NOT be used anymore */
504         g_object_unref (priv->tp_conn);
505         priv->tp_conn = NULL;
506
507         /* Remove all contacts */
508         for (l = priv->members; l; l = l->next) {
509                 g_signal_emit_by_name (list, "members-changed", l->data,
510                                        NULL, 0, NULL,
511                                        FALSE);
512                 g_object_unref (l->data);
513         }
514         for (l = priv->pendings; l; l = l->next) {
515                 g_signal_emit_by_name (list, "pendings-changed", l->data,
516                                        NULL, 0, NULL,
517                                        FALSE);
518                 g_object_unref (l->data);
519         }
520         g_list_free (priv->members);
521         g_list_free (priv->pendings);
522         priv->members = NULL;
523         priv->pendings = NULL;
524
525         /* Tell the world to not use us anymore */
526         g_signal_emit (list, signals[DESTROY], 0);
527 }
528
529 static void
530 tp_contact_list_disconnect (EmpathyTpContactList *list)
531 {
532         EmpathyTpContactListPriv *priv = GET_PRIV (list);
533
534         if (priv->tp_conn) {
535                 g_signal_handlers_disconnect_by_func (priv->tp_conn,
536                                                       tp_contact_list_destroy_cb,
537                                                       list);
538                 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
539                                                 G_CALLBACK (tp_contact_list_newchannel_cb),
540                                                 list);
541         }
542 }
543
544 static void
545 tp_contact_list_status_changed_cb (MissionControl                  *mc,
546                                    TelepathyConnectionStatus        status,
547                                    McPresence                       presence,
548                                    TelepathyConnectionStatusReason  reason,
549                                    const gchar                     *unique_name,
550                                    EmpathyTpContactList            *list)
551 {
552         EmpathyTpContactListPriv *priv = GET_PRIV (list);
553         McAccount                *account;
554
555         account = mc_account_lookup (unique_name);
556         if (status != TP_CONN_STATUS_CONNECTED &&
557             empathy_account_equal (account, priv->account)) {
558                 /* We are disconnected */
559                 tp_contact_list_disconnect (list);
560                 tp_contact_list_destroy_cb (priv->tp_conn, list);
561         }
562
563         g_object_unref (account);
564 }
565
566 static void
567 tp_contact_list_group_list_free (GList **groups)
568 {
569         g_list_foreach (*groups, (GFunc) g_free, NULL);
570         g_list_free (*groups);
571         g_slice_free (GList*, groups);
572 }
573
574 static void
575 tp_contact_list_finalize (GObject *object)
576 {
577         EmpathyTpContactListPriv *priv;
578         EmpathyTpContactList     *list;
579
580         list = EMPATHY_TP_CONTACT_LIST (object);
581         priv = GET_PRIV (list);
582
583         empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
584
585         tp_contact_list_disconnect (list);
586
587         if (priv->mc) {
588                 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
589                                                 "AccountStatusChanged",
590                                                 G_CALLBACK (tp_contact_list_status_changed_cb),
591                                                 list);
592                 g_object_unref (priv->mc);
593         }
594
595         if (priv->subscribe) {
596                 g_object_unref (priv->subscribe);
597         }
598         if (priv->publish) {
599                 g_object_unref (priv->publish);
600         }
601         if (priv->account) {
602                 g_object_unref (priv->account);
603         }
604         if (priv->tp_conn) {
605                 g_object_unref (priv->tp_conn);
606         }
607
608         g_hash_table_destroy (priv->contacts_groups);
609         g_list_foreach (priv->groups, (GFunc) g_object_unref, NULL);
610         g_list_free (priv->groups);
611         g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
612         g_list_free (priv->members);
613         g_list_foreach (priv->pendings, (GFunc) g_object_unref, NULL);
614         g_list_free (priv->pendings);
615
616         G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
617 }
618
619 static void
620 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
621 {
622         GObjectClass *object_class = G_OBJECT_CLASS (klass);
623
624         object_class->finalize = tp_contact_list_finalize;
625
626         signals[DESTROY] =
627                 g_signal_new ("destroy",
628                               G_TYPE_FROM_CLASS (klass),
629                               G_SIGNAL_RUN_LAST,
630                               0,
631                               NULL, NULL,
632                               g_cclosure_marshal_VOID__VOID,
633                               G_TYPE_NONE,
634                               0);
635
636         g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
637 }
638
639 static void
640 empathy_tp_contact_list_init (EmpathyTpContactList *list)
641 {
642 }
643
644 static void
645 tp_contact_list_setup (EmpathyTpContactList *list)
646 {
647         EmpathyTpContactListPriv *priv = GET_PRIV (list);
648         GPtrArray                *channels;
649         guint                     i;
650         GError                   *error = NULL;
651
652         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
653
654         /* Get existing channels */
655         if (!tp_conn_list_channels (DBUS_G_PROXY (priv->tp_conn),
656                                     &channels,
657                                     &error)) {
658                 empathy_debug (DEBUG_DOMAIN,
659                               "Failed to get list of open channels: %s",
660                               error ? error->message : "No error given");
661                 g_clear_error (&error);
662                 return;
663         }
664
665         for (i = 0; i < channels->len; i++) {
666                 GValueArray         *chan_struct;
667                 const gchar         *object_path;
668                 const gchar         *chan_iface;
669                 TelepathyHandleType  handle_type;
670                 guint                handle;
671
672                 chan_struct = g_ptr_array_index (channels, i);
673                 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
674                 chan_iface = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
675                 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
676                 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
677
678                 tp_contact_list_newchannel_cb (DBUS_G_PROXY (priv->tp_conn),
679                                                object_path, chan_iface,
680                                                handle_type, handle,
681                                                FALSE,
682                                                list);
683
684                 g_value_array_free (chan_struct);
685         }
686         g_ptr_array_free (channels, TRUE);
687 }
688
689 EmpathyTpContactList *
690 empathy_tp_contact_list_new (McAccount *account)
691 {
692         EmpathyTpContactListPriv *priv;
693         EmpathyTpContactList     *list;
694         MissionControl           *mc;
695         TpConn                   *tp_conn = NULL;
696         McProfile                *profile;
697         const gchar              *protocol_name;
698
699         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
700
701         mc = empathy_mission_control_new ();
702
703         /* status==0 means CONNECTED */
704         if (mission_control_get_connection_status (mc, account, NULL) == 0) {
705                 tp_conn = mission_control_get_connection (mc, account, NULL);
706         }
707         if (!tp_conn) {
708                 /* The account is not connected, nothing to do. */
709                 g_object_unref (mc);
710                 return NULL;
711         }
712
713         list = g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST, NULL);
714         priv = GET_PRIV (list);
715
716         priv->tp_conn = tp_conn;
717         priv->account = g_object_ref (account);
718         priv->mc = mc;
719         priv->contacts_groups = g_hash_table_new_full (empathy_contact_hash,
720                                                        empathy_contact_equal,
721                                                        (GDestroyNotify) g_object_unref,
722                                                        (GDestroyNotify) tp_contact_list_group_list_free);
723
724         /* Check for protocols that does not support contact groups. We can
725          * put all contacts into a special group in that case.
726          * FIXME: Default group should be an information in the profile */
727         profile = mc_account_get_profile (account);
728         protocol_name = mc_profile_get_protocol_name (profile);
729         if (strcmp (protocol_name, "local-xmpp") == 0) {
730                 priv->protocol_group = _("People nearby");
731         }
732         g_object_unref (profile);
733
734         /* Connect signals */
735         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
736                                      "AccountStatusChanged",
737                                      G_CALLBACK (tp_contact_list_status_changed_cb),
738                                      list, NULL);
739         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
740                                      G_CALLBACK (tp_contact_list_newchannel_cb),
741                                      list, NULL);
742         g_signal_connect (priv->tp_conn, "destroy",
743                           G_CALLBACK (tp_contact_list_destroy_cb),
744                           list);
745
746         tp_contact_list_setup (list);
747
748         return list;
749 }
750
751 McAccount *
752 empathy_tp_contact_list_get_account (EmpathyTpContactList *list)
753 {
754         EmpathyTpContactListPriv *priv;
755
756         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
757
758         priv = GET_PRIV (list);
759
760         return priv->account;
761 }
762
763 static void
764 tp_contact_list_add (EmpathyContactList *list,
765                      EmpathyContact     *contact,
766                      const gchar        *message)
767 {
768         EmpathyTpContactListPriv *priv = GET_PRIV (list);
769
770         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
771
772         empathy_tp_group_add_member (priv->subscribe, contact, message);
773         if (g_list_find (priv->pendings, contact)) {
774                 empathy_tp_group_add_member (priv->publish, contact, message);          
775         }
776 }
777
778 static void
779 tp_contact_list_remove (EmpathyContactList *list,
780                         EmpathyContact     *contact,
781                         const gchar        *message)
782 {
783         EmpathyTpContactListPriv *priv = GET_PRIV (list);
784
785         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
786
787         empathy_tp_group_remove_member (priv->subscribe, contact, message);
788         empathy_tp_group_remove_member (priv->publish, contact, message);               
789 }
790
791 static GList *
792 tp_contact_list_get_members (EmpathyContactList *list)
793 {
794         EmpathyTpContactListPriv *priv = GET_PRIV (list);
795
796         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
797
798         g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
799         return g_list_copy (priv->members);
800 }
801
802 static GList *
803 tp_contact_list_get_pendings (EmpathyContactList *list)
804 {
805         EmpathyTpContactListPriv *priv = GET_PRIV (list);
806
807         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
808
809         g_list_foreach (priv->pendings, (GFunc) g_object_ref, NULL);
810         return g_list_copy (priv->pendings);
811 }
812
813 static GList *
814 tp_contact_list_get_all_groups (EmpathyContactList *list)
815 {
816         EmpathyTpContactListPriv *priv = GET_PRIV (list);
817         GList                    *groups = NULL, *l;
818
819         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
820
821         for (l = priv->groups; l; l = l->next) {
822                 const gchar *name;
823
824                 name = empathy_tp_group_get_name (l->data);
825                 groups = g_list_prepend (groups, g_strdup (name));
826         }
827
828         if (priv->protocol_group) {
829                 groups = g_list_prepend (groups, g_strdup (priv->protocol_group));
830         }
831
832         return groups;
833 }
834
835 static GList *
836 tp_contact_list_get_groups (EmpathyContactList *list,
837                             EmpathyContact     *contact)
838 {
839         EmpathyTpContactListPriv  *priv = GET_PRIV (list);
840         GList                    **groups;
841         GList                     *ret = NULL, *l;
842
843         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
844
845         groups = g_hash_table_lookup (priv->contacts_groups, contact);
846         if (!groups) {
847                 return NULL;
848         }
849
850         for (l = *groups; l; l = l->next) {
851                 ret = g_list_prepend (ret, g_strdup (l->data));
852         }
853
854         if (priv->protocol_group) {
855                 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
856         }
857
858         return ret;
859 }
860
861 static EmpathyTpGroup *
862 tp_contact_list_get_group (EmpathyTpContactList *list,
863                            const gchar          *group)
864 {
865         EmpathyTpContactListPriv *priv = GET_PRIV (list);
866         EmpathyTpGroup           *tp_group;
867         gchar                    *object_path;
868         guint                     handle;
869         GArray                   *handles;
870         const char               *names[2] = {group, NULL};
871         GError                   *error = NULL;
872
873         tp_group = tp_contact_list_find_group (list, group);
874         if (tp_group) {
875                 return tp_group;
876         }
877
878         empathy_debug (DEBUG_DOMAIN, "creating new group: %s", group);
879
880         if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
881                                       TP_HANDLE_TYPE_GROUP,
882                                       names,
883                                       &handles,
884                                       &error)) {
885                 empathy_debug (DEBUG_DOMAIN,
886                               "Failed to RequestHandles: %s",
887                               error ? error->message : "No error given");
888                 g_clear_error (&error);
889                 return NULL;
890         }
891         handle = g_array_index (handles, guint, 0);
892         g_array_free (handles, TRUE);
893
894         if (!tp_conn_request_channel (DBUS_G_PROXY (priv->tp_conn),
895                                       TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
896                                       TP_HANDLE_TYPE_GROUP,
897                                       handle,
898                                       FALSE,
899                                       &object_path,
900                                       &error)) {
901                 empathy_debug (DEBUG_DOMAIN,
902                               "Failed to RequestChannel: %s",
903                               error ? error->message : "No error given");
904                 g_clear_error (&error);
905                 return NULL;
906         }
907
908         tp_contact_list_newchannel_cb (DBUS_G_PROXY (priv->tp_conn),
909                                        object_path,
910                                        TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
911                                        TP_HANDLE_TYPE_GROUP,
912                                        handle, FALSE,
913                                        list);
914         g_free (object_path);
915
916         return tp_contact_list_find_group (list, group);
917 }
918
919 static void
920 tp_contact_list_add_to_group (EmpathyContactList *list,
921                               EmpathyContact     *contact,
922                               const gchar        *group)
923 {
924         EmpathyTpGroup *tp_group;
925
926         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
927
928         tp_group = tp_contact_list_get_group (EMPATHY_TP_CONTACT_LIST (list),
929                                               group);
930
931         empathy_tp_group_add_member (tp_group, contact, "");
932 }
933
934 static void
935 tp_contact_list_remove_from_group (EmpathyContactList *list,
936                                    EmpathyContact     *contact,
937                                    const gchar        *group)
938 {
939         EmpathyTpGroup *tp_group;
940
941         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
942
943         tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
944                                                group);
945
946         if (tp_group) {
947                 empathy_tp_group_remove_member (tp_group, contact, "");
948         }
949 }
950
951 static void
952 tp_contact_list_rename_group (EmpathyContactList *list,
953                               const gchar        *old_group,
954                               const gchar        *new_group)
955 {
956         EmpathyTpGroup *tp_group;
957         GList          *members;
958
959         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
960
961         tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
962                                                old_group);
963         if (!tp_group) {
964                 return;
965         }
966
967         empathy_debug (DEBUG_DOMAIN, "rename group %s to %s", old_group, new_group);
968
969         /* Remove all members from the old group */
970         members = empathy_tp_group_get_members (tp_group);
971         empathy_tp_group_remove_members (tp_group, members, "");
972         empathy_tp_group_close (tp_group);
973
974         /* Add all members to the new group */
975         tp_group = tp_contact_list_get_group (EMPATHY_TP_CONTACT_LIST (list),
976                                               new_group);
977         empathy_tp_group_add_members (tp_group, members, "");
978
979         g_list_foreach (members, (GFunc) g_object_unref, NULL);
980         g_list_free (members);
981 }
982
983 static void
984 tp_contact_list_iface_init (EmpathyContactListIface *iface)
985 {
986         iface->add               = tp_contact_list_add;
987         iface->remove            = tp_contact_list_remove;
988         iface->get_members       = tp_contact_list_get_members;
989         iface->get_pendings      = tp_contact_list_get_pendings;
990         iface->get_all_groups    = tp_contact_list_get_all_groups;
991         iface->get_groups        = tp_contact_list_get_groups;
992         iface->add_to_group      = tp_contact_list_add_to_group;
993         iface->remove_from_group = tp_contact_list_remove_from_group;
994         iface->rename_group      = tp_contact_list_rename_group;
995 }
996