]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-contact-list.c
Use gi18n-lib.h instead of gi18n.h for libraries.
[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-2008 Collabora Ltd.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  * 
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26 #include <glib/gi18n-lib.h>
27
28 #include <telepathy-glib/channel.h>
29 #include <telepathy-glib/connection.h>
30 #include <telepathy-glib/util.h>
31 #include <telepathy-glib/dbus.h>
32
33 #include "empathy-tp-contact-list.h"
34 #include "empathy-contact-list.h"
35 #include "empathy-tp-group.h"
36 #include "empathy-utils.h"
37
38 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
39 #include "empathy-debug.h"
40
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactList)
42 typedef struct {
43         McAccount      *account;
44         TpConnection   *connection;
45         const gchar    *protocol_group;
46         gboolean        ready;
47
48         EmpathyTpGroup *publish;
49         EmpathyTpGroup *subscribe;
50         GList          *members;
51         GList          *pendings;
52
53         GList          *groups;
54         GHashTable     *contacts_groups;
55 } EmpathyTpContactListPriv;
56
57 typedef enum {
58         TP_CONTACT_LIST_TYPE_PUBLISH,
59         TP_CONTACT_LIST_TYPE_SUBSCRIBE,
60         TP_CONTACT_LIST_TYPE_UNKNOWN
61 } TpContactListType;
62
63 static void tp_contact_list_iface_init         (EmpathyContactListIface   *iface);
64
65 enum {
66         DESTROY,
67         LAST_SIGNAL
68 };
69
70 enum {
71         PROP_0,
72         PROP_ACCOUNT,
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         DEBUG ("Group destroyed: %s", empathy_tp_group_get_name (group));
88
89         priv->groups = g_list_remove (priv->groups, group);
90         g_object_unref (group);
91 }
92
93 static void
94 tp_contact_list_group_member_added_cb (EmpathyTpGroup       *group,
95                                        EmpathyContact       *contact,
96                                        EmpathyContact       *actor,
97                                        guint                 reason,
98                                        const gchar          *message,
99                                        EmpathyTpContactList *list)
100 {
101         EmpathyTpContactListPriv  *priv = GET_PRIV (list);
102         const gchar               *group_name;
103         GList                    **groups;
104
105         if (!g_list_find (priv->members, contact)) {
106                 return;
107         }
108
109         groups = g_hash_table_lookup (priv->contacts_groups, contact);
110         if (!groups) {
111                 groups = g_slice_new0 (GList*);
112                 g_hash_table_insert (priv->contacts_groups,
113                                      g_object_ref (contact),
114                                      groups);
115         }
116
117         group_name = empathy_tp_group_get_name (group);
118         if (!g_list_find_custom (*groups, group_name, (GCompareFunc) strcmp)) {
119                 DEBUG ("Contact %s (%d) added to group %s",
120                         empathy_contact_get_id (contact),
121                         empathy_contact_get_handle (contact),
122                         group_name);
123                 *groups = g_list_prepend (*groups, g_strdup (group_name));
124                 g_signal_emit_by_name (list, "groups-changed", contact,
125                                        group_name,
126                                        TRUE);
127         }
128 }
129
130 static void
131 tp_contact_list_group_member_removed_cb (EmpathyTpGroup       *group,
132                                          EmpathyContact       *contact,
133                                          EmpathyContact       *actor,
134                                          guint                 reason,
135                                          const gchar          *message,
136                                          EmpathyTpContactList *list)
137 {
138         EmpathyTpContactListPriv  *priv = GET_PRIV (list);
139         const gchar               *group_name;
140         GList                    **groups, *l;
141
142         if (!g_list_find (priv->members, contact)) {
143                 return;
144         }
145
146         groups = g_hash_table_lookup (priv->contacts_groups, contact);
147         if (!groups) {
148                 return;
149         }
150
151         group_name = empathy_tp_group_get_name (group);
152         if ((l = g_list_find_custom (*groups, group_name, (GCompareFunc) strcmp))) {
153                 DEBUG ("Contact %s (%d) removed from group %s",
154                         empathy_contact_get_id (contact),
155                         empathy_contact_get_handle (contact),
156                         group_name);
157                 *groups = g_list_delete_link (*groups, l);
158                 g_signal_emit_by_name (list, "groups-changed", contact,
159                                        group_name,
160                                        FALSE);
161         }
162 }
163
164 static EmpathyTpGroup *
165 tp_contact_list_find_group (EmpathyTpContactList *list,
166                             const gchar          *group)
167 {
168         EmpathyTpContactListPriv *priv = GET_PRIV (list);
169         GList                    *l;
170
171         for (l = priv->groups; l; l = l->next) {
172                 if (!tp_strdiff (group, empathy_tp_group_get_name (l->data))) {
173                         return l->data;
174                 }
175         }
176         return NULL;
177 }
178
179 static TpContactListType
180 tp_contact_list_get_type (EmpathyTpContactList *list,
181                           EmpathyTpGroup       *group)
182 {
183         const gchar *name;
184
185         name = empathy_tp_group_get_name (group);
186         if (!tp_strdiff (name, "subscribe")) {
187                 return TP_CONTACT_LIST_TYPE_SUBSCRIBE;
188         } else if (!tp_strdiff (name, "publish")) {
189                 return TP_CONTACT_LIST_TYPE_PUBLISH;
190         }
191
192         return TP_CONTACT_LIST_TYPE_UNKNOWN;
193 }
194
195 static void
196 tp_contact_list_add_member (EmpathyTpContactList *list,
197                             EmpathyContact       *contact,
198                             EmpathyContact       *actor,
199                             guint                 reason,
200                             const gchar          *message)
201 {
202         EmpathyTpContactListPriv *priv = GET_PRIV (list);
203         GList                    *l;
204
205         /* Add to the list and emit signal */
206         priv->members = g_list_prepend (priv->members, g_object_ref (contact));
207         g_signal_emit_by_name (list, "members-changed",
208                                contact, actor, reason, message,
209                                TRUE);
210
211         /* This contact is now member, implicitly accept pending. */
212         if (g_list_find (priv->pendings, contact)) {
213                 empathy_tp_group_add_member (priv->publish, contact, "");
214         }
215
216         /* Update groups of the contact */
217         for (l = priv->groups; l; l = l->next) {
218                 if (empathy_tp_group_is_member (l->data, contact)) {
219                         tp_contact_list_group_member_added_cb (l->data, contact,
220                                                                NULL, 0, NULL, 
221                                                                list);
222                 }
223         }
224 }
225
226 static void
227 tp_contact_list_added_cb (EmpathyTpGroup       *group,
228                           EmpathyContact       *contact,
229                           EmpathyContact       *actor,
230                           guint                 reason,
231                           const gchar          *message,
232                           EmpathyTpContactList *list)
233 {
234         EmpathyTpContactListPriv *priv = GET_PRIV (list);
235         TpContactListType         list_type;
236
237         list_type = tp_contact_list_get_type (list, group);
238         DEBUG ("Contact %s (%d) added to list type %d",
239                 empathy_contact_get_id (contact),
240                 empathy_contact_get_handle (contact),
241                 list_type);
242
243         /* We now get the presence of that contact, add it to members */
244         if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE &&
245             !g_list_find (priv->members, contact)) {
246                 tp_contact_list_add_member (list, contact, actor, reason, message);
247         }
248
249         /* We now send our presence to that contact, remove it from pendings */
250         if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH &&
251             g_list_find (priv->pendings, contact)) {
252                 g_signal_emit_by_name (list, "pendings-changed",
253                                        contact, actor, reason, message,
254                                        FALSE);
255                 priv->pendings = g_list_remove (priv->pendings, contact);
256                 g_object_unref (contact);
257         }
258 }
259
260 static void
261 tp_contact_list_removed_cb (EmpathyTpGroup       *group,
262                             EmpathyContact       *contact,
263                             EmpathyContact       *actor,
264                             guint                 reason,
265                             const gchar          *message,
266                             EmpathyTpContactList *list)
267 {
268         EmpathyTpContactListPriv *priv = GET_PRIV (list);
269         TpContactListType         list_type;
270
271         list_type = tp_contact_list_get_type (list, group);
272         DEBUG ("Contact %s (%d) removed from list type %d",
273                 empathy_contact_get_id (contact),
274                 empathy_contact_get_handle (contact),
275                 list_type);
276
277         /* This contact refuses to send us his presence, remove from members. */
278         if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE &&
279             g_list_find (priv->members, contact)) {
280                 g_signal_emit_by_name (list, "members-changed",
281                                        contact, actor, reason, message,
282                                        FALSE);
283                 priv->members = g_list_remove (priv->members, contact);
284                 g_object_unref (contact);
285         }
286
287         /* We refuse to send our presence to that contact, remove from pendings */
288         if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH &&
289             g_list_find (priv->pendings, contact)) {
290                 g_signal_emit_by_name (list, "pendings-changed",
291                                        contact, actor, reason, message,
292                                        FALSE);
293                 priv->pendings = g_list_remove (priv->pendings, contact);
294                 g_object_unref (contact);
295         }
296 }
297
298 static void
299 tp_contact_list_pending_cb (EmpathyTpGroup       *group,
300                             EmpathyContact       *contact,
301                             EmpathyContact       *actor,
302                             guint                 reason,
303                             const gchar          *message,
304                             EmpathyTpContactList *list)
305 {
306         EmpathyTpContactListPriv *priv = GET_PRIV (list);
307         TpContactListType         list_type;
308
309         list_type = tp_contact_list_get_type (list, group);
310         DEBUG ("Contact %s (%d) pending in list type %d",
311                 empathy_contact_get_id (contact),
312                 empathy_contact_get_handle (contact),
313                 list_type);
314
315         /* We want this contact in our contact list but we don't get its 
316          * presence yet. Add to members anyway. */
317         if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE &&
318             !g_list_find (priv->members, contact)) {
319                 tp_contact_list_add_member (list, contact, actor, reason, message);
320         }
321
322         /* This contact wants our presence, auto accept if he is member,
323          * otherwise he is pending. */
324         if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH &&
325             !g_list_find (priv->pendings, contact)) {
326                 if (g_list_find (priv->members, contact)) {
327                         empathy_tp_group_add_member (priv->publish, contact, "");
328                 } else {
329                         priv->pendings = g_list_prepend (priv->pendings,
330                                                          g_object_ref (contact));
331                         g_signal_emit_by_name (list, "pendings-changed",
332                                                contact, actor, reason, message,
333                                                TRUE);
334                 }
335         }
336 }
337
338 static void
339 tp_contact_list_invalidated_cb (TpConnection         *connection,
340                                 guint                 domain,
341                                 gint                  code,
342                                 gchar                *message,
343                                 EmpathyTpContactList *list)
344 {
345         EmpathyTpContactListPriv *priv = GET_PRIV (list);
346         GList                    *l;
347
348         DEBUG ("Connection invalidated");
349
350         /* Remove all contacts */
351         for (l = priv->members; l; l = l->next) {
352                 g_signal_emit_by_name (list, "members-changed", l->data,
353                                        NULL, 0, NULL,
354                                        FALSE);
355                 g_object_unref (l->data);
356         }
357         for (l = priv->pendings; l; l = l->next) {
358                 g_signal_emit_by_name (list, "pendings-changed", l->data,
359                                        NULL, 0, NULL,
360                                        FALSE);
361                 g_object_unref (l->data);
362         }
363         g_list_free (priv->members);
364         g_list_free (priv->pendings);
365         priv->members = NULL;
366         priv->pendings = NULL;
367
368         /* Tell the world to not use us anymore */
369         g_signal_emit (list, signals[DESTROY], 0);
370 }
371
372 static void
373 tp_contact_list_group_list_free (GList **groups)
374 {
375         g_list_foreach (*groups, (GFunc) g_free, NULL);
376         g_list_free (*groups);
377         g_slice_free (GList*, groups);
378 }
379
380 static void
381 tp_contact_list_add_channel (EmpathyTpContactList *list,
382                              const gchar          *object_path,
383                              const gchar          *channel_type,
384                              TpHandleType          handle_type,
385                              guint                 handle)
386 {
387         EmpathyTpContactListPriv *priv = GET_PRIV (list);
388         TpChannel                *channel;
389         EmpathyTpGroup           *group;
390         const gchar              *group_name;
391         GList                    *contacts, *l;
392
393         if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) != 0 ||
394             handle_type != TP_HANDLE_TYPE_GROUP) {
395                 return;
396         }
397
398         channel = tp_channel_new (priv->connection,
399                                   object_path, channel_type,
400                                   handle_type, handle, NULL);
401
402         group = empathy_tp_group_new (channel);
403         empathy_run_until_ready (group);
404         g_object_unref (channel);
405
406         /* Check if already exists */
407         group_name = empathy_tp_group_get_name (group);
408         if (tp_contact_list_find_group (list, group_name)) {
409                 g_object_unref (group);
410                 return;
411         }
412
413         /* Add the group */
414         DEBUG ("New server-side group: %s", group_name);
415         priv->groups = g_list_prepend (priv->groups, group);
416         g_signal_connect (group, "member-added",
417                           G_CALLBACK (tp_contact_list_group_member_added_cb),
418                           list);
419         g_signal_connect (group, "member-removed",
420                           G_CALLBACK (tp_contact_list_group_member_removed_cb),
421                           list);
422         g_signal_connect (group, "destroy",
423                           G_CALLBACK (tp_contact_list_group_destroy_cb),
424                           list);
425
426         /* Get initial members */
427         contacts = empathy_tp_group_get_members (group);
428         for (l = contacts; l; l = l->next) {
429                 tp_contact_list_group_member_added_cb (group, l->data,
430                                                        NULL, 0, NULL,
431                                                        list);
432                 g_object_unref (l->data);
433         }
434         g_list_free (contacts);
435 }
436
437 static void
438 tp_contact_list_new_channel_cb (TpConnection *proxy,
439                                 const gchar  *object_path,
440                                 const gchar  *channel_type,
441                                 guint         handle_type,
442                                 guint         handle,
443                                 gboolean      suppress_handler,
444                                 gpointer      user_data,
445                                 GObject      *list)
446 {
447         EmpathyTpContactListPriv *priv = GET_PRIV (list);
448
449         if (!suppress_handler && priv->ready) {
450                 tp_contact_list_add_channel (EMPATHY_TP_CONTACT_LIST (list),
451                                              object_path, channel_type,
452                                              handle_type, handle);
453         }
454 }
455
456 static void
457 tp_contact_list_list_channels_cb (TpConnection    *connection,
458                                   const GPtrArray *channels,
459                                   const GError    *error,
460                                   gpointer         user_data,
461                                   GObject         *list)
462 {
463         EmpathyTpContactListPriv *priv = GET_PRIV (list);
464         guint                     i;
465
466         if (error) {
467                 DEBUG ("Error: %s", error->message);
468                 return;
469         }
470
471         for (i = 0; i < channels->len; i++) {
472                 GValueArray  *chan_struct;
473                 const gchar  *object_path;
474                 const gchar  *channel_type;
475                 TpHandleType  handle_type;
476                 guint         handle;
477
478                 chan_struct = g_ptr_array_index (channels, i);
479                 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
480                 channel_type = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
481                 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
482                 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
483
484                 tp_contact_list_add_channel (EMPATHY_TP_CONTACT_LIST (list),
485                                              object_path, channel_type,
486                                              handle_type, handle);
487         }
488
489         priv->ready = TRUE;
490 }
491
492 static void
493 tp_contact_list_request_channel_cb (TpConnection *connection,
494                                     const gchar  *object_path,
495                                     const GError *error,
496                                     gpointer      user_data,
497                                     GObject      *weak_object)
498 {
499         EmpathyTpContactList     *list = EMPATHY_TP_CONTACT_LIST (weak_object);
500         EmpathyTpContactListPriv *priv = GET_PRIV (list);
501         EmpathyTpGroup           *group;
502         TpChannel                *channel;
503         TpContactListType         list_type;
504         GList                    *contacts, *l;
505
506         if (error) {
507                 DEBUG ("Error: %s", error->message);
508                 return;
509         }
510
511         channel = tp_channel_new (connection, object_path,
512                                   TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
513                                   TP_HANDLE_TYPE_LIST,
514                                   GPOINTER_TO_UINT (user_data),
515                                   NULL);
516         group = empathy_tp_group_new (channel);
517         empathy_run_until_ready (group);
518
519         list_type = tp_contact_list_get_type (list, group);
520         if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH && !priv->publish) {
521                 DEBUG ("Got publish list");
522                 priv->publish = group;
523
524                 /* Publish is the list of contacts to who we send our
525                  * presence. Makes no sense to be in remote-pending */
526                 g_signal_connect (group, "local-pending",
527                                   G_CALLBACK (tp_contact_list_pending_cb),
528                                   list);
529
530                 contacts = empathy_tp_group_get_local_pendings (group);
531                 for (l = contacts; l; l = l->next) {
532                         EmpathyPendingInfo *info = l->data;
533                                 tp_contact_list_pending_cb (group,
534                                                     info->member,
535                                                     info->actor,
536                                                     0,
537                                                     info->message,
538                                                     list);
539                         empathy_pending_info_free (info);
540                 }
541                 g_list_free (contacts);
542         }
543         else if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE && !priv->subscribe) {
544                 DEBUG ("Got subscribe list");
545                 priv->subscribe = group;
546
547                 /* Subscribe is the list of contacts from who we
548                  * receive presence. Makes no sense to be in
549                  * local-pending */
550                 g_signal_connect (group, "remote-pending",
551                                   G_CALLBACK (tp_contact_list_pending_cb),
552                                   list);
553
554                 contacts = empathy_tp_group_get_remote_pendings (group);
555                 for (l = contacts; l; l = l->next) {
556                         tp_contact_list_pending_cb (group,
557                                                     l->data,
558                                                     NULL, 0,
559                                                     NULL, list);
560                         g_object_unref (l->data);
561                 }
562                 g_list_free (contacts);
563         } else {
564                 DEBUG ("Type of contact list channel unknown or aleady "
565                         "have that list: %s",
566                         empathy_tp_group_get_name (group));
567                 g_object_unref (group);
568                 return;
569         }
570
571         /* For all list types when need to get members */
572         g_signal_connect (group, "member-added",
573                           G_CALLBACK (tp_contact_list_added_cb),
574                           list);
575         g_signal_connect (group, "member-removed",
576                           G_CALLBACK (tp_contact_list_removed_cb),
577                           list);
578
579         contacts = empathy_tp_group_get_members (group);
580         for (l = contacts; l; l = l->next) {
581                 tp_contact_list_added_cb (group,
582                                           l->data,
583                                           NULL, 0, NULL,
584                                           list);
585                 g_object_unref (l->data);
586         }
587         g_list_free (contacts);
588 }
589
590 static void
591 tp_contact_list_request_handle_cb (TpConnection *connection,
592                                    const GArray *handles,
593                                    const GError *error,
594                                    gpointer      user_data,
595                                    GObject      *list)
596 {
597         guint handle;
598
599         if (error) {
600                 DEBUG ("Error: %s", error->message);
601                 return;
602         }
603
604         handle = g_array_index (handles, guint, 0);
605         tp_cli_connection_call_request_channel (connection, -1,
606                                                 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
607                                                 TP_HANDLE_TYPE_LIST,
608                                                 handle,
609                                                 TRUE,
610                                                 tp_contact_list_request_channel_cb,
611                                                 GUINT_TO_POINTER (handle), NULL,
612                                                 list);
613 }
614
615 static void
616 tp_contact_list_request_list (EmpathyTpContactList *list,
617                               const gchar          *type)
618 {
619         EmpathyTpContactListPriv *priv = GET_PRIV (list);
620         const gchar *names[] = {type, NULL};
621
622         tp_cli_connection_call_request_handles (priv->connection,
623                                                 -1,
624                                                 TP_HANDLE_TYPE_LIST,
625                                                 names,
626                                                 tp_contact_list_request_handle_cb,
627                                                 NULL, NULL,
628                                                 G_OBJECT (list));
629 }
630
631 static void
632 tp_contact_list_finalize (GObject *object)
633 {
634         EmpathyTpContactListPriv *priv;
635         EmpathyTpContactList     *list;
636
637         list = EMPATHY_TP_CONTACT_LIST (object);
638         priv = GET_PRIV (list);
639
640         DEBUG ("finalize: %p", object);
641
642         if (priv->subscribe) {
643                 g_object_unref (priv->subscribe);
644         }
645         if (priv->publish) {
646                 g_object_unref (priv->publish);
647         }
648         if (priv->account) {
649                 g_object_unref (priv->account);
650         }
651         if (priv->connection) {
652                 g_signal_handlers_disconnect_by_func (priv->connection,
653                                                       tp_contact_list_invalidated_cb,
654                                                       object);
655                 g_object_unref (priv->connection);
656         }
657
658         g_hash_table_destroy (priv->contacts_groups);
659         g_list_foreach (priv->groups, (GFunc) g_object_unref, NULL);
660         g_list_free (priv->groups);
661         g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
662         g_list_free (priv->members);
663         g_list_foreach (priv->pendings, (GFunc) g_object_unref, NULL);
664         g_list_free (priv->pendings);
665
666         G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
667 }
668
669 static void
670 tp_contact_list_connection_ready (TpConnection *connection,
671                                   const GError *error,
672                                   gpointer      list)
673 {
674         EmpathyTpContactListPriv *priv = GET_PRIV (list);
675
676         if (error) {
677                 tp_contact_list_invalidated_cb (connection,
678                                                 error->domain,
679                                                 error->code,
680                                                 error->message,
681                                                 EMPATHY_TP_CONTACT_LIST (list));
682                 return;
683         }
684
685         g_signal_connect (priv->connection, "invalidated",
686                           G_CALLBACK (tp_contact_list_invalidated_cb),
687                           list);
688
689         tp_contact_list_request_list (list, "publish");
690         tp_contact_list_request_list (list, "subscribe");
691
692         tp_cli_connection_call_list_channels (priv->connection, -1,
693                                               tp_contact_list_list_channels_cb,
694                                               NULL, NULL,
695                                               list);
696
697         tp_cli_connection_connect_to_new_channel (priv->connection,
698                                                   tp_contact_list_new_channel_cb,
699                                                   NULL, NULL,
700                                                   list, NULL);
701 }
702
703 static void
704 tp_contact_list_constructed (GObject *list)
705 {
706
707         EmpathyTpContactListPriv *priv = GET_PRIV (list);
708         MissionControl           *mc;
709         guint                     status;
710         McProfile                *profile;
711         const gchar              *protocol_name;
712
713         /* Get the connection. status==0 means CONNECTED */
714         mc = empathy_mission_control_new ();
715         status = mission_control_get_connection_status (mc, priv->account, NULL);
716         g_return_if_fail (status == 0);
717         priv->connection = mission_control_get_tpconnection (mc, priv->account, NULL);
718         g_return_if_fail (priv->connection != NULL);
719         g_object_unref (mc);
720
721         tp_connection_call_when_ready (priv->connection,
722                                        tp_contact_list_connection_ready,
723                                        list);
724
725         /* Check for protocols that does not support contact groups. We can
726          * put all contacts into a special group in that case.
727          * FIXME: Default group should be an information in the profile */
728         profile = mc_account_get_profile (priv->account);
729         protocol_name = mc_profile_get_protocol_name (profile);
730         if (strcmp (protocol_name, "local-xmpp") == 0) {
731                 priv->protocol_group = _("People nearby");
732         }
733         g_object_unref (profile);
734 }
735
736 static void
737 tp_contact_list_get_property (GObject    *object,
738                               guint       param_id,
739                               GValue     *value,
740                               GParamSpec *pspec)
741 {
742         EmpathyTpContactListPriv *priv = GET_PRIV (object);
743
744         switch (param_id) {
745         case PROP_ACCOUNT:
746                 g_value_set_object (value, priv->account);
747                 break;
748         default:
749                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
750                 break;
751         };
752 }
753
754 static void
755 tp_contact_list_set_property (GObject      *object,
756                               guint         param_id,
757                               const GValue *value,
758                               GParamSpec   *pspec)
759 {
760         EmpathyTpContactListPriv *priv = GET_PRIV (object);
761
762         switch (param_id) {
763         case PROP_ACCOUNT:
764                 priv->account = g_object_ref (g_value_get_object (value));
765                 break;
766         default:
767                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
768                 break;
769         };
770 }
771
772 static void
773 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
774 {
775         GObjectClass *object_class = G_OBJECT_CLASS (klass);
776
777         object_class->finalize = tp_contact_list_finalize;
778         object_class->constructed = tp_contact_list_constructed;
779         object_class->get_property = tp_contact_list_get_property;
780         object_class->set_property = tp_contact_list_set_property;
781
782         g_object_class_install_property (object_class,
783                                          PROP_ACCOUNT,
784                                          g_param_spec_object ("account",
785                                                               "The Account",
786                                                               "The account associated with the contact list",
787                                                               MC_TYPE_ACCOUNT,
788                                                               G_PARAM_READWRITE |
789                                                               G_PARAM_CONSTRUCT_ONLY));
790
791         signals[DESTROY] =
792                 g_signal_new ("destroy",
793                               G_TYPE_FROM_CLASS (klass),
794                               G_SIGNAL_RUN_LAST,
795                               0,
796                               NULL, NULL,
797                               g_cclosure_marshal_VOID__VOID,
798                               G_TYPE_NONE,
799                               0);
800
801         g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
802 }
803
804 static void
805 empathy_tp_contact_list_init (EmpathyTpContactList *list)
806 {
807         EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
808                 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
809
810         list->priv = priv;
811         priv->contacts_groups = g_hash_table_new_full (g_direct_hash,
812                                                        g_direct_equal,
813                                                        (GDestroyNotify) g_object_unref,
814                                                        (GDestroyNotify) tp_contact_list_group_list_free);
815 }
816
817 EmpathyTpContactList *
818 empathy_tp_contact_list_new (McAccount *account)
819 {
820         return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
821                              "account", account,
822                              NULL);
823 }
824
825 McAccount *
826 empathy_tp_contact_list_get_account (EmpathyTpContactList *list)
827 {
828         EmpathyTpContactListPriv *priv;
829
830         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
831
832         priv = GET_PRIV (list);
833
834         return priv->account;
835 }
836
837 static void
838 tp_contact_list_add (EmpathyContactList *list,
839                      EmpathyContact     *contact,
840                      const gchar        *message)
841 {
842         EmpathyTpContactListPriv *priv = GET_PRIV (list);
843
844         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
845
846         if (priv->subscribe) {
847                 empathy_tp_group_add_member (priv->subscribe, contact, message);
848         }
849
850         if (priv->publish && g_list_find (priv->pendings, contact)) {
851                 empathy_tp_group_add_member (priv->publish, contact, message);          
852         }
853 }
854
855 static void
856 tp_contact_list_remove (EmpathyContactList *list,
857                         EmpathyContact     *contact,
858                         const gchar        *message)
859 {
860         EmpathyTpContactListPriv *priv = GET_PRIV (list);
861
862         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
863
864         if (priv->subscribe) {
865                 empathy_tp_group_remove_member (priv->subscribe, contact, message);
866         }
867         if (priv->publish) {
868                 empathy_tp_group_remove_member (priv->publish, contact, message);               
869         }
870 }
871
872 static GList *
873 tp_contact_list_get_members (EmpathyContactList *list)
874 {
875         EmpathyTpContactListPriv *priv = GET_PRIV (list);
876
877         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
878
879         g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
880         return g_list_copy (priv->members);
881 }
882
883 static GList *
884 tp_contact_list_get_pendings (EmpathyContactList *list)
885 {
886         EmpathyTpContactListPriv *priv = GET_PRIV (list);
887
888         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
889
890         g_list_foreach (priv->pendings, (GFunc) g_object_ref, NULL);
891         return g_list_copy (priv->pendings);
892 }
893
894 static GList *
895 tp_contact_list_get_all_groups (EmpathyContactList *list)
896 {
897         EmpathyTpContactListPriv *priv = GET_PRIV (list);
898         GList                    *groups = NULL, *l;
899
900         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
901
902         if (priv->protocol_group) {
903                 groups = g_list_prepend (groups, g_strdup (priv->protocol_group));
904         }
905
906         for (l = priv->groups; l; l = l->next) {
907                 const gchar *name;
908
909                 name = empathy_tp_group_get_name (l->data);
910                 groups = g_list_prepend (groups, g_strdup (name));
911         }
912
913         return groups;
914 }
915
916 static GList *
917 tp_contact_list_get_groups (EmpathyContactList *list,
918                             EmpathyContact     *contact)
919 {
920         EmpathyTpContactListPriv  *priv = GET_PRIV (list);
921         GList                    **groups;
922         GList                     *ret = NULL, *l;
923
924         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
925
926         if (priv->protocol_group) {
927                 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
928         }
929
930         groups = g_hash_table_lookup (priv->contacts_groups, contact);
931         if (!groups) {
932                 return ret;
933         }
934
935         for (l = *groups; l; l = l->next) {
936                 ret = g_list_prepend (ret, g_strdup (l->data));
937         }
938
939
940         return ret;
941 }
942
943 static EmpathyTpGroup *
944 tp_contact_list_get_group (EmpathyTpContactList *list,
945                            const gchar          *group)
946 {
947         EmpathyTpContactListPriv *priv = GET_PRIV (list);
948         EmpathyTpGroup           *tp_group;
949         gchar                    *object_path;
950         guint                     handle;
951         GArray                   *handles;
952         const char               *names[2] = {group, NULL};
953         GError                   *error = NULL;
954
955         tp_group = tp_contact_list_find_group (list, group);
956         if (tp_group) {
957                 return tp_group;
958         }
959
960         DEBUG ("creating new group: %s", group);
961
962         if (!tp_cli_connection_run_request_handles (priv->connection, -1,
963                                                     TP_HANDLE_TYPE_GROUP,
964                                                     names,
965                                                     &handles,
966                                                     &error, NULL)) {
967                 DEBUG ("Failed to RequestHandles: %s",
968                         error ? error->message : "No error given");
969                 g_clear_error (&error);
970                 return NULL;
971         }
972         handle = g_array_index (handles, guint, 0);
973         g_array_free (handles, TRUE);
974
975         if (!tp_cli_connection_run_request_channel (priv->connection, -1,
976                                                     TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
977                                                     TP_HANDLE_TYPE_GROUP,
978                                                     handle,
979                                                     TRUE,
980                                                     &object_path,
981                                                     &error, NULL)) {
982                 DEBUG ("Failed to RequestChannel: %s",
983                         error ? error->message : "No error given");
984                 g_clear_error (&error);
985                 return NULL;
986         }
987
988         tp_contact_list_add_channel (EMPATHY_TP_CONTACT_LIST (list),
989                                      object_path,
990                                      TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
991                                      TP_HANDLE_TYPE_GROUP, handle);
992
993         g_free (object_path);
994
995         return tp_contact_list_find_group (list, group);
996 }
997
998 static void
999 tp_contact_list_add_to_group (EmpathyContactList *list,
1000                               EmpathyContact     *contact,
1001                               const gchar        *group)
1002 {
1003         EmpathyTpGroup *tp_group;
1004
1005         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1006
1007         tp_group = tp_contact_list_get_group (EMPATHY_TP_CONTACT_LIST (list),
1008                                               group);
1009
1010         if (tp_group) {
1011                 empathy_tp_group_add_member (tp_group, contact, "");
1012         }
1013 }
1014
1015 static void
1016 tp_contact_list_remove_from_group (EmpathyContactList *list,
1017                                    EmpathyContact     *contact,
1018                                    const gchar        *group)
1019 {
1020         EmpathyTpGroup *tp_group;
1021
1022         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1023
1024         tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
1025                                                group);
1026
1027         if (tp_group) {
1028                 empathy_tp_group_remove_member (tp_group, contact, "");
1029         }
1030 }
1031
1032 static void
1033 tp_contact_list_rename_group (EmpathyContactList *list,
1034                               const gchar        *old_group,
1035                               const gchar        *new_group)
1036 {
1037         EmpathyTpGroup *tp_group;
1038         GList          *members;
1039
1040         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1041
1042         tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
1043                                                old_group);
1044         if (!tp_group) {
1045                 return;
1046         }
1047
1048         DEBUG ("rename group %s to %s", old_group, new_group);
1049
1050         /* Remove all members from the old group */
1051         members = empathy_tp_group_get_members (tp_group);
1052         empathy_tp_group_remove_members (tp_group, members, "");
1053         empathy_tp_group_close (tp_group);
1054
1055         /* Add all members to the new group */
1056         tp_group = tp_contact_list_get_group (EMPATHY_TP_CONTACT_LIST (list),
1057                                               new_group);
1058         empathy_tp_group_add_members (tp_group, members, "");
1059
1060         g_list_foreach (members, (GFunc) g_object_unref, NULL);
1061         g_list_free (members);
1062 }
1063
1064 static void
1065 tp_contact_list_remove_group (EmpathyContactList *list,
1066                               const gchar *group)
1067 {
1068         EmpathyTpGroup *tp_group;
1069         GList          *members;
1070
1071         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1072
1073         tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
1074                                                group);
1075         
1076         if (!tp_group) {
1077                 return;
1078         }
1079
1080         DEBUG ("remove group %s", group);
1081
1082         /* Remove all members of the group */
1083         members = empathy_tp_group_get_members (tp_group);
1084         empathy_tp_group_remove_members (tp_group, members, "");
1085         empathy_tp_group_close (tp_group);
1086
1087         g_list_foreach (members, (GFunc) g_object_unref, NULL);
1088         g_list_free (members);
1089 }
1090
1091 static void
1092 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1093 {
1094         iface->add               = tp_contact_list_add;
1095         iface->remove            = tp_contact_list_remove;
1096         iface->get_members       = tp_contact_list_get_members;
1097         iface->get_pendings      = tp_contact_list_get_pendings;
1098         iface->get_all_groups    = tp_contact_list_get_all_groups;
1099         iface->get_groups        = tp_contact_list_get_groups;
1100         iface->add_to_group      = tp_contact_list_add_to_group;
1101         iface->remove_from_group = tp_contact_list_remove_from_group;
1102         iface->rename_group      = tp_contact_list_rename_group;
1103         iface->remove_group      = tp_contact_list_remove_group;
1104 }
1105
1106 gboolean
1107 empathy_tp_contact_list_can_add (EmpathyTpContactList *list)
1108 {
1109         EmpathyTpContactListPriv *priv;
1110         TpChannelGroupFlags       flags;
1111
1112         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), FALSE);
1113
1114         priv = GET_PRIV (list);
1115
1116         if (priv->subscribe == NULL)
1117                 return FALSE;
1118
1119         flags = empathy_tp_group_get_flags (priv->subscribe);
1120         return (flags & TP_CHANNEL_GROUP_FLAG_CAN_ADD) != 0;
1121 }