]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-contact-list.c
Keep a priv pointer in the object struct instead of using G_TYPE_INSTANCE_GET_PRIVATE...
[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.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         EmpathyTpGroup           *group;
389         TpChannel                *channel;
390
391         if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) != 0 ||
392             (handle_type != TP_HANDLE_TYPE_LIST &&
393              handle_type != TP_HANDLE_TYPE_GROUP)) {
394                 return;
395         }
396
397         channel = tp_channel_new (priv->connection,
398                                   object_path, channel_type,
399                                   handle_type, handle, NULL);
400
401         group = empathy_tp_group_new (channel);
402         empathy_run_until_ready (group);
403         g_object_unref (channel);
404
405         if (handle_type == TP_HANDLE_TYPE_LIST) {
406                 TpContactListType  list_type;
407                 GList             *contacts, *l;
408
409                 list_type = tp_contact_list_get_type (list, group);
410                 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH && !priv->publish) {
411                         priv->publish = g_object_ref (group);
412
413                         /* Publish is the list of contacts to who we send our
414                          * presence. Makes no sense to be in remote-pending */
415                         g_signal_connect (group, "local-pending",
416                                           G_CALLBACK (tp_contact_list_pending_cb),
417                                           list);
418
419                         contacts = empathy_tp_group_get_local_pendings (group);
420                         for (l = contacts; l; l = l->next) {
421                                 EmpathyPendingInfo *info = l->data;
422
423                                 tp_contact_list_pending_cb (group,
424                                                             info->member,
425                                                             info->actor,
426                                                             0,
427                                                             info->message,
428                                                             list);
429                                 empathy_pending_info_free (info);
430                         }
431                         g_list_free (contacts);
432                 }
433                 else if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE && !priv->subscribe) {
434                         priv->subscribe = g_object_ref (group);
435
436                         /* Subscribe is the list of contacts from who we
437                          * receive presence. Makes no sense to be in
438                          * local-pending */
439                         g_signal_connect (group, "remote-pending",
440                                           G_CALLBACK (tp_contact_list_pending_cb),
441                                           list);
442
443                         contacts = empathy_tp_group_get_remote_pendings (group);
444                         for (l = contacts; l; l = l->next) {
445                                 tp_contact_list_pending_cb (group,
446                                                             l->data,
447                                                             NULL, 0,
448                                                             NULL, list);
449                                 g_object_unref (l->data);
450                         }
451                         g_list_free (contacts);
452                 } else {
453                         DEBUG ("Type of contact list channel unknown or aleady "
454                                 "have that list: %s",
455                                 empathy_tp_group_get_name (group));
456                         goto OUT;
457                 }
458                 DEBUG ("New contact list channel of type: %d", list_type);
459
460                 g_signal_connect (group, "member-added",
461                                   G_CALLBACK (tp_contact_list_added_cb),
462                                   list);
463                 g_signal_connect (group, "member-removed",
464                                   G_CALLBACK (tp_contact_list_removed_cb),
465                                   list);
466
467                 contacts = empathy_tp_group_get_members (group);
468                 for (l = contacts; l; l = l->next) {
469                         tp_contact_list_added_cb (group,
470                                                   l->data,
471                                                   NULL, 0, NULL,
472                                                   list);
473                         g_object_unref (l->data);
474                 }
475                 g_list_free (contacts);
476         }
477         else if (handle_type == TP_HANDLE_TYPE_GROUP) {
478                 const gchar *group_name;
479                 GList       *contacts, *l;
480
481                 /* Check if already exists */
482                 group_name = empathy_tp_group_get_name (group);
483                 if (tp_contact_list_find_group (list, group_name)) {
484                         goto OUT;
485                 }
486
487                 DEBUG ("New server-side group channel: %s", group_name);
488
489                 priv->groups = g_list_prepend (priv->groups, g_object_ref (group));
490
491                 g_signal_connect (group, "member-added",
492                                   G_CALLBACK (tp_contact_list_group_member_added_cb),
493                                   list);
494                 g_signal_connect (group, "member-removed",
495                                   G_CALLBACK (tp_contact_list_group_member_removed_cb),
496                                   list);
497                 g_signal_connect (group, "destroy",
498                                   G_CALLBACK (tp_contact_list_group_destroy_cb),
499                                   list);
500
501                 contacts = empathy_tp_group_get_members (group);
502                 for (l = contacts; l; l = l->next) {
503                         tp_contact_list_group_member_added_cb (group, l->data,
504                                                                NULL, 0, NULL,
505                                                                list);
506                         g_object_unref (l->data);
507                 }
508                 g_list_free (contacts);
509         } else {
510                 DEBUG ("Unknown handle type (%d) for contact list channel",
511                         handle_type);
512         }
513
514 OUT:
515         g_object_unref (group);
516 }
517
518 static void
519 tp_contact_list_new_channel_cb (TpConnection *proxy,
520                                 const gchar  *object_path,
521                                 const gchar  *channel_type,
522                                 guint         handle_type,
523                                 guint         handle,
524                                 gboolean      suppress_handler,
525                                 gpointer      user_data,
526                                 GObject      *list)
527 {
528         EmpathyTpContactListPriv *priv = GET_PRIV (list);
529
530         if (!suppress_handler && priv->ready) {
531                 tp_contact_list_add_channel (EMPATHY_TP_CONTACT_LIST (list),
532                                              object_path, channel_type,
533                                              handle_type, handle);
534         }
535 }
536
537 static void
538 tp_contact_list_list_channels_cb (TpConnection    *connection,
539                                   const GPtrArray *channels,
540                                   const GError    *error,
541                                   gpointer         user_data,
542                                   GObject         *list)
543 {
544         EmpathyTpContactListPriv *priv = GET_PRIV (list);
545         guint                     i;
546
547         if (error) {
548                 DEBUG ("Failed to get list of open channels: %s",
549                         error ? error->message : "No error given");
550                 return;
551         }
552
553         for (i = 0; i < channels->len; i++) {
554                 GValueArray  *chan_struct;
555                 const gchar  *object_path;
556                 const gchar  *channel_type;
557                 TpHandleType  handle_type;
558                 guint         handle;
559
560                 chan_struct = g_ptr_array_index (channels, i);
561                 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
562                 channel_type = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
563                 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
564                 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
565
566                 tp_contact_list_add_channel (EMPATHY_TP_CONTACT_LIST (list),
567                                              object_path, channel_type,
568                                              handle_type, handle);
569         }
570
571         priv->ready = TRUE;
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         DEBUG ("finalize: %p", object);
584
585         if (priv->subscribe) {
586                 g_object_unref (priv->subscribe);
587         }
588         if (priv->publish) {
589                 g_object_unref (priv->publish);
590         }
591         if (priv->account) {
592                 g_object_unref (priv->account);
593         }
594         if (priv->connection) {
595                 g_signal_handlers_disconnect_by_func (priv->connection,
596                                                       tp_contact_list_invalidated_cb,
597                                                       object);
598                 g_object_unref (priv->connection);
599         }
600
601         g_hash_table_destroy (priv->contacts_groups);
602         g_list_foreach (priv->groups, (GFunc) g_object_unref, NULL);
603         g_list_free (priv->groups);
604         g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
605         g_list_free (priv->members);
606         g_list_foreach (priv->pendings, (GFunc) g_object_unref, NULL);
607         g_list_free (priv->pendings);
608
609         G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
610 }
611
612 static void
613 tp_contact_list_ready_cb (EmpathyTpContactList *list)
614 {
615         EmpathyTpContactListPriv *priv = GET_PRIV (list);
616
617         tp_cli_connection_call_list_channels (priv->connection, -1,
618                                               tp_contact_list_list_channels_cb,
619                                               NULL, NULL,
620                                               G_OBJECT (list));
621
622         tp_cli_connection_connect_to_new_channel (priv->connection,
623                                                   tp_contact_list_new_channel_cb,
624                                                   NULL, NULL,
625                                                   G_OBJECT (list), NULL);
626 }
627
628 static void
629 tp_contact_list_constructed (GObject *list)
630 {
631
632         EmpathyTpContactListPriv *priv = GET_PRIV (list);
633         MissionControl           *mc;
634         guint                     status;
635         gboolean                  ready;
636         McProfile                *profile;
637         const gchar              *protocol_name;
638
639         /* Get the connection. status==0 means CONNECTED */
640         mc = empathy_mission_control_new ();
641         status = mission_control_get_connection_status (mc, priv->account, NULL);
642         g_return_if_fail (status == 0);
643         priv->connection = mission_control_get_tpconnection (mc, priv->account, NULL);
644         g_return_if_fail (priv->connection != NULL);
645         g_object_unref (mc);
646
647         g_signal_connect (priv->connection, "invalidated",
648                           G_CALLBACK (tp_contact_list_invalidated_cb),
649                           list);
650         g_object_get (priv->connection, "connection-ready", &ready, NULL);
651         if (ready) {
652                 tp_contact_list_ready_cb (EMPATHY_TP_CONTACT_LIST (list));
653         } else {
654                 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
655                                           G_CALLBACK (tp_contact_list_ready_cb),
656                                           list);
657         }
658
659         /* Check for protocols that does not support contact groups. We can
660          * put all contacts into a special group in that case.
661          * FIXME: Default group should be an information in the profile */
662         profile = mc_account_get_profile (priv->account);
663         protocol_name = mc_profile_get_protocol_name (profile);
664         if (strcmp (protocol_name, "local-xmpp") == 0) {
665                 priv->protocol_group = _("People nearby");
666         }
667         g_object_unref (profile);
668 }
669
670 static void
671 tp_contact_list_get_property (GObject    *object,
672                               guint       param_id,
673                               GValue     *value,
674                               GParamSpec *pspec)
675 {
676         EmpathyTpContactListPriv *priv = GET_PRIV (object);
677
678         switch (param_id) {
679         case PROP_ACCOUNT:
680                 g_value_set_object (value, priv->account);
681                 break;
682         default:
683                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
684                 break;
685         };
686 }
687
688 static void
689 tp_contact_list_set_property (GObject      *object,
690                               guint         param_id,
691                               const GValue *value,
692                               GParamSpec   *pspec)
693 {
694         EmpathyTpContactListPriv *priv = GET_PRIV (object);
695
696         switch (param_id) {
697         case PROP_ACCOUNT:
698                 priv->account = g_object_ref (g_value_get_object (value));
699                 break;
700         default:
701                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
702                 break;
703         };
704 }
705
706 static void
707 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
708 {
709         GObjectClass *object_class = G_OBJECT_CLASS (klass);
710
711         object_class->finalize = tp_contact_list_finalize;
712         object_class->constructed = tp_contact_list_constructed;
713         object_class->get_property = tp_contact_list_get_property;
714         object_class->set_property = tp_contact_list_set_property;
715
716         g_object_class_install_property (object_class,
717                                          PROP_ACCOUNT,
718                                          g_param_spec_object ("account",
719                                                               "The Account",
720                                                               "The account associated with the contact list",
721                                                               MC_TYPE_ACCOUNT,
722                                                               G_PARAM_READWRITE |
723                                                               G_PARAM_CONSTRUCT_ONLY));
724
725         signals[DESTROY] =
726                 g_signal_new ("destroy",
727                               G_TYPE_FROM_CLASS (klass),
728                               G_SIGNAL_RUN_LAST,
729                               0,
730                               NULL, NULL,
731                               g_cclosure_marshal_VOID__VOID,
732                               G_TYPE_NONE,
733                               0);
734
735         g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
736 }
737
738 static void
739 empathy_tp_contact_list_init (EmpathyTpContactList *list)
740 {
741         EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
742                 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
743
744         list->priv = priv;
745         priv->contacts_groups = g_hash_table_new_full (g_direct_hash,
746                                                        g_direct_equal,
747                                                        (GDestroyNotify) g_object_unref,
748                                                        (GDestroyNotify) tp_contact_list_group_list_free);
749 }
750
751 EmpathyTpContactList *
752 empathy_tp_contact_list_new (McAccount *account)
753 {
754         return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
755                              "account", account,
756                              NULL);
757 }
758
759 McAccount *
760 empathy_tp_contact_list_get_account (EmpathyTpContactList *list)
761 {
762         EmpathyTpContactListPriv *priv;
763
764         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
765
766         priv = GET_PRIV (list);
767
768         return priv->account;
769 }
770
771 static void
772 tp_contact_list_add (EmpathyContactList *list,
773                      EmpathyContact     *contact,
774                      const gchar        *message)
775 {
776         EmpathyTpContactListPriv *priv = GET_PRIV (list);
777
778         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
779
780         empathy_tp_group_add_member (priv->subscribe, contact, message);
781         if (g_list_find (priv->pendings, contact)) {
782                 empathy_tp_group_add_member (priv->publish, contact, message);          
783         }
784 }
785
786 static void
787 tp_contact_list_remove (EmpathyContactList *list,
788                         EmpathyContact     *contact,
789                         const gchar        *message)
790 {
791         EmpathyTpContactListPriv *priv = GET_PRIV (list);
792
793         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
794
795         empathy_tp_group_remove_member (priv->subscribe, contact, message);
796         empathy_tp_group_remove_member (priv->publish, contact, message);               
797 }
798
799 static GList *
800 tp_contact_list_get_members (EmpathyContactList *list)
801 {
802         EmpathyTpContactListPriv *priv = GET_PRIV (list);
803
804         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
805
806         g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
807         return g_list_copy (priv->members);
808 }
809
810 static GList *
811 tp_contact_list_get_pendings (EmpathyContactList *list)
812 {
813         EmpathyTpContactListPriv *priv = GET_PRIV (list);
814
815         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
816
817         g_list_foreach (priv->pendings, (GFunc) g_object_ref, NULL);
818         return g_list_copy (priv->pendings);
819 }
820
821 static GList *
822 tp_contact_list_get_all_groups (EmpathyContactList *list)
823 {
824         EmpathyTpContactListPriv *priv = GET_PRIV (list);
825         GList                    *groups = NULL, *l;
826
827         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
828
829         if (priv->protocol_group) {
830                 groups = g_list_prepend (groups, g_strdup (priv->protocol_group));
831         }
832
833         for (l = priv->groups; l; l = l->next) {
834                 const gchar *name;
835
836                 name = empathy_tp_group_get_name (l->data);
837                 groups = g_list_prepend (groups, g_strdup (name));
838         }
839
840         return groups;
841 }
842
843 static GList *
844 tp_contact_list_get_groups (EmpathyContactList *list,
845                             EmpathyContact     *contact)
846 {
847         EmpathyTpContactListPriv  *priv = GET_PRIV (list);
848         GList                    **groups;
849         GList                     *ret = NULL, *l;
850
851         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
852
853         if (priv->protocol_group) {
854                 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
855         }
856
857         groups = g_hash_table_lookup (priv->contacts_groups, contact);
858         if (!groups) {
859                 return ret;
860         }
861
862         for (l = *groups; l; l = l->next) {
863                 ret = g_list_prepend (ret, g_strdup (l->data));
864         }
865
866
867         return ret;
868 }
869
870 static EmpathyTpGroup *
871 tp_contact_list_get_group (EmpathyTpContactList *list,
872                            const gchar          *group)
873 {
874         EmpathyTpContactListPriv *priv = GET_PRIV (list);
875         EmpathyTpGroup           *tp_group;
876         gchar                    *object_path;
877         guint                     handle;
878         GArray                   *handles;
879         const char               *names[2] = {group, NULL};
880         GError                   *error = NULL;
881
882         tp_group = tp_contact_list_find_group (list, group);
883         if (tp_group) {
884                 return tp_group;
885         }
886
887         DEBUG ("creating new group: %s", group);
888
889         if (!tp_cli_connection_run_request_handles (priv->connection, -1,
890                                                     TP_HANDLE_TYPE_GROUP,
891                                                     names,
892                                                     &handles,
893                                                     &error, NULL)) {
894                 DEBUG ("Failed to RequestHandles: %s",
895                         error ? error->message : "No error given");
896                 g_clear_error (&error);
897                 return NULL;
898         }
899         handle = g_array_index (handles, guint, 0);
900         g_array_free (handles, TRUE);
901
902         if (!tp_cli_connection_run_request_channel (priv->connection, -1,
903                                                     TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
904                                                     TP_HANDLE_TYPE_GROUP,
905                                                     handle,
906                                                     TRUE,
907                                                     &object_path,
908                                                     &error, NULL)) {
909                 DEBUG ("Failed to RequestChannel: %s",
910                         error ? error->message : "No error given");
911                 g_clear_error (&error);
912                 return NULL;
913         }
914
915         tp_contact_list_add_channel (EMPATHY_TP_CONTACT_LIST (list),
916                                      object_path,
917                                      TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
918                                      TP_HANDLE_TYPE_GROUP, handle);
919
920         g_free (object_path);
921
922         return tp_contact_list_find_group (list, group);
923 }
924
925 static void
926 tp_contact_list_add_to_group (EmpathyContactList *list,
927                               EmpathyContact     *contact,
928                               const gchar        *group)
929 {
930         EmpathyTpGroup *tp_group;
931
932         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
933
934         tp_group = tp_contact_list_get_group (EMPATHY_TP_CONTACT_LIST (list),
935                                               group);
936
937         empathy_tp_group_add_member (tp_group, contact, "");
938 }
939
940 static void
941 tp_contact_list_remove_from_group (EmpathyContactList *list,
942                                    EmpathyContact     *contact,
943                                    const gchar        *group)
944 {
945         EmpathyTpGroup *tp_group;
946
947         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
948
949         tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
950                                                group);
951
952         if (tp_group) {
953                 empathy_tp_group_remove_member (tp_group, contact, "");
954         }
955 }
956
957 static void
958 tp_contact_list_rename_group (EmpathyContactList *list,
959                               const gchar        *old_group,
960                               const gchar        *new_group)
961 {
962         EmpathyTpGroup *tp_group;
963         GList          *members;
964
965         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
966
967         tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
968                                                old_group);
969         if (!tp_group) {
970                 return;
971         }
972
973         DEBUG ("rename group %s to %s", old_group, new_group);
974
975         /* Remove all members from the old group */
976         members = empathy_tp_group_get_members (tp_group);
977         empathy_tp_group_remove_members (tp_group, members, "");
978         empathy_tp_group_close (tp_group);
979
980         /* Add all members to the new group */
981         tp_group = tp_contact_list_get_group (EMPATHY_TP_CONTACT_LIST (list),
982                                               new_group);
983         empathy_tp_group_add_members (tp_group, members, "");
984
985         g_list_foreach (members, (GFunc) g_object_unref, NULL);
986         g_list_free (members);
987 }
988
989 static void
990 tp_contact_list_remove_group (EmpathyContactList *list,
991                               const gchar *group)
992 {
993         EmpathyTpGroup *tp_group;
994         GList          *members;
995
996         g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
997
998         tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
999                                                group);
1000         
1001         if (!tp_group) {
1002                 return;
1003         }
1004
1005         DEBUG ("remove group %s", group);
1006
1007         /* Remove all members of the group */
1008         members = empathy_tp_group_get_members (tp_group);
1009         empathy_tp_group_remove_members (tp_group, members, "");
1010         empathy_tp_group_close (tp_group);
1011
1012         g_list_foreach (members, (GFunc) g_object_unref, NULL);
1013         g_list_free (members);
1014 }
1015
1016 static void
1017 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1018 {
1019         iface->add               = tp_contact_list_add;
1020         iface->remove            = tp_contact_list_remove;
1021         iface->get_members       = tp_contact_list_get_members;
1022         iface->get_pendings      = tp_contact_list_get_pendings;
1023         iface->get_all_groups    = tp_contact_list_get_all_groups;
1024         iface->get_groups        = tp_contact_list_get_groups;
1025         iface->add_to_group      = tp_contact_list_add_to_group;
1026         iface->remove_from_group = tp_contact_list_remove_from_group;
1027         iface->rename_group      = tp_contact_list_rename_group;
1028         iface->remove_group      = tp_contact_list_remove_group;
1029 }
1030