]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-group.c
Use empathy_file_lookup for glade files since some are in libempathy-gtk/ and others...
[empathy.git] / libempathy / empathy-tp-group.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2006 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 <dbus/dbus-glib.h>
26 #include <libtelepathy/tp-chan.h>
27 #include <libtelepathy/tp-chan-gen.h>
28 #include <libtelepathy/tp-chan-iface-group-gen.h>
29 #include <libtelepathy/tp-conn.h>
30 #include <telepathy-glib/interfaces.h>
31
32 #include "empathy-tp-group.h"
33 #include "empathy-contact-factory.h"
34 #include "empathy-debug.h"
35 #include "empathy-utils.h"
36 #include "empathy-marshal.h"
37
38 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
39                        EMPATHY_TYPE_TP_GROUP, EmpathyTpGroupPriv))
40
41 #define DEBUG_DOMAIN "TpGroup"
42
43 struct _EmpathyTpGroupPriv {
44         EmpathyContactFactory *factory;
45         McAccount             *account;
46         DBusGProxy            *group_iface;
47         TpChan                *tp_chan;
48         gchar                 *group_name;
49         guint                  self_handle;
50
51         GList                 *members;
52         GList                 *local_pendings;
53         GList                 *remote_pendings;
54 };
55
56 static void empathy_tp_group_class_init (EmpathyTpGroupClass *klass);
57 static void empathy_tp_group_init       (EmpathyTpGroup      *group);
58
59 enum {
60         MEMBER_ADDED,
61         MEMBER_REMOVED,
62         LOCAL_PENDING,
63         REMOTE_PENDING,
64         DESTROY,
65         LAST_SIGNAL
66 };
67
68 static guint signals[LAST_SIGNAL];
69
70 G_DEFINE_TYPE (EmpathyTpGroup, empathy_tp_group, G_TYPE_OBJECT);
71
72 static EmpathyContact *
73 tp_group_get_contact (EmpathyTpGroup *group,
74                       guint           handle)
75 {
76         EmpathyTpGroupPriv *priv = GET_PRIV (group);
77         EmpathyContact     *contact = NULL;
78         
79         if (handle != 0) {
80                 contact = empathy_contact_factory_get_from_handle (priv->factory,
81                                                                    priv->account,
82                                                                    handle);
83         }
84
85         if (contact && handle == priv->self_handle) {
86                 empathy_contact_set_is_user (contact, TRUE);
87         }
88
89         return contact;
90 }
91
92 static GList *
93 tp_group_get_contacts (EmpathyTpGroup *group,
94                        GArray         *handles)
95 {
96         EmpathyTpGroupPriv *priv = GET_PRIV (group);
97         GList              *contacts,  *l;
98
99         if (!handles) {
100                 return NULL;
101         }
102
103         contacts = empathy_contact_factory_get_from_handles (priv->factory,
104                                                              priv->account,
105                                                              handles);
106
107         /* FIXME: Only useful if the group has a different self handle than
108          * the connection, otherwise the contact factory already set that
109          * property. That can be known using group flags. */
110         for (l = contacts; l; l = l->next) {
111                 if (empathy_contact_get_handle (l->data) == priv->self_handle) {
112                         empathy_contact_set_is_user (l->data, TRUE);
113                 }
114         }
115
116         return contacts;
117 }
118
119 EmpathyPendingInfo *
120 empathy_pending_info_new (EmpathyContact *member,
121                           EmpathyContact *actor,
122                           const gchar    *message)
123 {
124         EmpathyPendingInfo *info;
125
126         info = g_slice_new0 (EmpathyPendingInfo);
127
128         if (member) {
129                 info->member = g_object_ref (member);
130         }
131         if (actor) {
132                 info->actor = g_object_ref (actor);
133         }
134         if (message) {
135                 info->message = g_strdup (message);
136         }
137
138         return info;
139 }
140
141 void
142 empathy_pending_info_free (EmpathyPendingInfo *info)
143 {
144         if (!info) {
145                 return;
146         }
147
148         if (info->member) {
149                 g_object_unref (info->member);
150         }
151         if (info->actor) {
152                 g_object_unref (info->actor);
153         }
154         g_free (info->message);
155
156         g_slice_free (EmpathyPendingInfo, info);
157 }
158
159 static gint
160 tp_group_local_pending_find (gconstpointer a,
161                              gconstpointer b)
162 {
163         const EmpathyPendingInfo *info = a;
164
165         return (info->member != b);
166 }
167
168 static void
169 tp_group_remove_from_pendings (EmpathyTpGroup *group,
170                                EmpathyContact *contact)
171 {
172         EmpathyTpGroupPriv *priv = GET_PRIV (group);
173         GList              *l;
174
175         /* local pending */
176         l = g_list_find_custom (priv->local_pendings,
177                                 contact,
178                                 tp_group_local_pending_find);
179         if (l) {
180                 empathy_pending_info_free (l->data);
181                 priv->local_pendings = g_list_delete_link (priv->local_pendings, l);
182         }
183
184         /* remote pending */
185         l = g_list_find (priv->remote_pendings, contact);
186         if (l) {
187                 g_object_unref (l->data);
188                 priv->remote_pendings = g_list_delete_link (priv->remote_pendings, l);
189         }
190 }
191
192 static void
193 tp_group_members_changed_cb (DBusGProxy     *group_iface,
194                              const gchar    *message,
195                              GArray         *added,
196                              GArray         *removed,
197                              GArray         *local_pending,
198                              GArray         *remote_pending,
199                              guint           actor,
200                              guint           reason,
201                              EmpathyTpGroup *group)
202 {
203         EmpathyTpGroupPriv *priv = GET_PRIV (group);
204         EmpathyContact     *actor_contact = NULL;
205         GList              *contacts, *l, *ll;
206
207         actor_contact = tp_group_get_contact (group, actor);
208
209         empathy_debug (DEBUG_DOMAIN, "Members changed for list %s:\n"
210                                      "  added-len=%d, current-len=%d\n"
211                                      "  removed-len=%d\n"
212                                      "  local-pending-len=%d, current-len=%d\n"
213                                      "  remote-pending-len=%d, current-len=%d",
214                        empathy_tp_group_get_name (group),
215                        added ? added->len : 0, g_list_length (priv->members),
216                        removed ? removed->len : 0,
217                        local_pending ? local_pending->len : 0,
218                        g_list_length (priv->local_pendings),
219                        remote_pending ? remote_pending->len : 0,
220                        g_list_length (priv->remote_pendings));
221
222         /* Contacts added */
223         contacts = tp_group_get_contacts (group, added);
224         for (l = contacts; l; l = l->next) {
225                 tp_group_remove_from_pendings (group, l->data);
226
227                 /* If the contact is not yet member, add it and emit signal */
228                 if (!g_list_find (priv->members, l->data)) {
229                         priv->members = g_list_prepend (priv->members,
230                                                         g_object_ref (l->data));
231                         g_signal_emit (group, signals[MEMBER_ADDED], 0,
232                                        l->data, actor_contact, reason, message);
233                 }
234                 g_object_unref (l->data);
235         }
236         g_list_free (contacts);
237
238         /* Contacts removed */
239         contacts = tp_group_get_contacts (group, removed);
240         for (l = contacts; l; l = l->next) {
241                 tp_group_remove_from_pendings (group, l->data);
242
243                 /* If the contact is member, remove it and emit signal */
244                 if ((ll = g_list_find (priv->members, l->data))) {
245                         g_object_unref (ll->data);
246                         priv->members = g_list_delete_link (priv->members, ll);
247                         g_signal_emit (group, signals[MEMBER_REMOVED], 0,
248                                        l->data, actor_contact, reason, message);
249                 }
250                 g_object_unref (l->data);
251         }
252         g_list_free (contacts);
253
254         /* Contacts local pending */
255         contacts = tp_group_get_contacts (group, local_pending);
256         for (l = contacts; l; l = l->next) {
257                 /* If the contact is not yet local-pending, add it and emit signal */
258                 if (!g_list_find_custom (priv->local_pendings, l->data,
259                                          tp_group_local_pending_find)) {
260                         EmpathyPendingInfo *info;
261
262                         info = empathy_pending_info_new (l->data,
263                                                          actor_contact,
264                                                          message);
265
266                         priv->local_pendings = g_list_prepend (priv->local_pendings, info);
267                         g_signal_emit (group, signals[LOCAL_PENDING], 0,
268                                        l->data, actor_contact, reason, message);
269                 }
270                 g_object_unref (l->data);
271         }
272         g_list_free (contacts);
273
274         /* Contacts remote pending */
275         contacts = tp_group_get_contacts (group, remote_pending);
276         for (l = contacts; l; l = l->next) {
277                 /* If the contact is not yet remote-pending, add it and emit signal */
278                 if (!g_list_find (priv->remote_pendings, l->data)) {
279                         priv->remote_pendings = g_list_prepend (priv->remote_pendings,
280                                                                 g_object_ref (l->data));
281                         g_signal_emit (group, signals[REMOTE_PENDING], 0,
282                                        l->data, actor_contact, reason, message);
283                 }
284                 g_object_unref (l->data);
285         }
286         g_list_free (contacts);
287
288         if (actor_contact) {
289                 g_object_unref (actor_contact);
290         }
291
292         empathy_debug (DEBUG_DOMAIN, "Members changed done for list %s:\n"
293                                      "  members-len=%d\n"
294                                      "  local-pendings-len=%d\n"
295                                      "  remote-pendings-len=%d",
296                        empathy_tp_group_get_name (group),
297                        g_list_length (priv->members),
298                        g_list_length (priv->local_pendings),
299                        g_list_length (priv->remote_pendings));
300 }
301
302 static void
303 tp_group_destroy_cb (TpChan         *tp_chan,
304                      EmpathyTpGroup *group)
305 {
306         EmpathyTpGroupPriv *priv = GET_PRIV (group);
307
308         empathy_debug (DEBUG_DOMAIN, "Account disconnected or CM crashed");
309
310         g_object_unref (priv->tp_chan);
311         priv->group_iface = NULL;
312         priv->tp_chan = NULL;
313
314         g_signal_emit (group, signals[DESTROY], 0);
315 }
316
317 static void tp_group_closed_cb (DBusGProxy     *proxy,
318                                 EmpathyTpGroup *group);
319
320 static void
321 tp_group_disconnect (EmpathyTpGroup *group)
322 {
323         EmpathyTpGroupPriv *priv = GET_PRIV (group);
324
325         if (priv->group_iface) {
326                 dbus_g_proxy_disconnect_signal (priv->group_iface, "MembersChanged",
327                                                 G_CALLBACK (tp_group_members_changed_cb),
328                                                 group);
329         }
330         if (priv->tp_chan) {
331                 g_signal_handlers_disconnect_by_func (priv->tp_chan,
332                                                       tp_group_destroy_cb,
333                                                       group);
334                 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->tp_chan), "Closed",
335                                                 G_CALLBACK (tp_group_closed_cb),
336                                                 group);
337         }
338 }
339
340 static void
341 tp_group_closed_cb (DBusGProxy     *proxy,
342                     EmpathyTpGroup *group)
343 {
344         tp_group_disconnect (group);
345         tp_group_destroy_cb (TELEPATHY_CHAN (proxy), group);
346 }
347
348 static void
349 tp_group_get_members_cb (DBusGProxy *proxy,
350                          GArray     *handles,
351                          GError     *error,
352                          gpointer    user_data)
353 {
354         EmpathyTpGroup     *group = user_data;
355         EmpathyTpGroupPriv *priv = GET_PRIV (group);
356
357         if (error) {
358                 empathy_debug (DEBUG_DOMAIN, "Failed to get members: %s",
359                                error->message);
360                 g_object_unref (group);
361                 return;
362         }
363
364         tp_group_members_changed_cb (priv->group_iface,
365                                      NULL,    /* message */
366                                      handles, /* added */
367                                      NULL,    /* removed */
368                                      NULL,    /* local_pending */
369                                      NULL,    /* remote_pending */
370                                      0,       /* actor */
371                                      0,       /* reason */
372                                      group);
373
374         g_array_free (handles, TRUE);
375         g_object_unref (group);
376 }
377
378 static void
379 tp_group_get_local_pending_cb (DBusGProxy *proxy,
380                                GPtrArray  *array,
381                                GError     *error,
382                                gpointer    user_data)
383 {
384         EmpathyTpGroup     *group = user_data;
385         EmpathyTpGroupPriv *priv = GET_PRIV (group);
386         GArray             *handles;
387         guint               i = 0;
388         
389         if (error) {
390                 empathy_debug (DEBUG_DOMAIN, "Failed to get local pendings: %s",
391                                error->message);
392                 g_object_unref (group);
393                 return;
394         }
395
396         handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
397         g_array_append_val (handles, i);
398         for (i = 0; array->len > i; i++) {
399                 GValueArray        *pending_struct;
400                 const gchar        *message;
401                 guint               member_handle;
402                 guint               actor_handle;
403                 guint               reason;
404
405                 pending_struct = g_ptr_array_index (array, i);
406                 member_handle = g_value_get_uint (g_value_array_get_nth (pending_struct, 0));
407                 actor_handle = g_value_get_uint (g_value_array_get_nth (pending_struct, 1));
408                 reason = g_value_get_uint (g_value_array_get_nth (pending_struct, 2));
409                 message = g_value_get_string (g_value_array_get_nth (pending_struct, 3));
410
411                 g_array_index (handles, guint, 0) = member_handle;
412                 tp_group_members_changed_cb (priv->group_iface,
413                                              message,      /* message */
414                                              NULL,         /* added */
415                                              NULL,         /* removed */
416                                              handles,      /* local_pending */
417                                              NULL,         /* remote_pending */
418                                              actor_handle, /* actor */
419                                              reason,       /* reason */
420                                              group);
421
422                 g_value_array_free (pending_struct);
423         }
424         g_ptr_array_free (array, TRUE);
425         g_array_free (handles, TRUE);
426         g_object_unref (group);
427 }
428
429 static void
430 tp_group_get_remote_pending_cb (DBusGProxy *proxy,
431                                 GArray     *handles,
432                                 GError     *error,
433                                 gpointer    user_data)
434 {
435         EmpathyTpGroup     *group = user_data;
436         EmpathyTpGroupPriv *priv = GET_PRIV (group);
437         
438         if (error) {
439                 empathy_debug (DEBUG_DOMAIN, "Failed to get remote pendings: %s",
440                                error->message);
441                 g_object_unref (group);
442                 return;
443         }
444
445         tp_group_members_changed_cb (priv->group_iface,
446                                      NULL,    /* message */
447                                      NULL,    /* added */
448                                      NULL,    /* removed */
449                                      NULL,    /* local_pending */
450                                      handles, /* remote_pending */
451                                      0,       /* actor */
452                                      0,       /* reason */
453                                      group);
454
455         g_array_free (handles, TRUE);
456         g_object_unref (group);
457 }
458
459 static void
460 tp_group_ready_cb (EmpathyTpGroup *group)
461 {
462         EmpathyTpGroupPriv      *priv = GET_PRIV (group);
463         EmpathyTpContactFactory *tp_factory;
464
465         empathy_debug (DEBUG_DOMAIN, "Factory ready, we can get members %p %p", group, priv);
466
467         tp_factory = empathy_contact_factory_get_tp_factory (priv->factory, priv->account);
468         g_signal_handlers_disconnect_by_func (tp_factory, tp_group_ready_cb, group);
469
470         dbus_g_proxy_connect_signal (priv->group_iface, "MembersChanged",
471                                      G_CALLBACK (tp_group_members_changed_cb),
472                                      group, NULL);
473
474         tp_chan_iface_group_get_members_async (priv->group_iface,
475                                                tp_group_get_members_cb,
476                                                g_object_ref (group));
477         tp_chan_iface_group_get_local_pending_members_with_info_async (priv->group_iface,
478                                                                        tp_group_get_local_pending_cb,
479                                                                        g_object_ref (group));
480         tp_chan_iface_group_get_remote_pending_members_async (priv->group_iface,
481                                                               tp_group_get_remote_pending_cb,
482                                                               g_object_ref (group));
483 }
484
485 static void
486 tp_group_finalize (GObject *object)
487 {
488         EmpathyTpGroupPriv      *priv = GET_PRIV (object);
489         EmpathyTpContactFactory *tp_factory;
490
491         empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
492
493         tp_group_disconnect (EMPATHY_TP_GROUP (object));
494
495         tp_factory = empathy_contact_factory_get_tp_factory (priv->factory, priv->account);
496         g_signal_handlers_disconnect_by_func (tp_factory, tp_group_ready_cb, object);
497
498         if (priv->tp_chan) {
499                 g_object_unref (priv->tp_chan);
500         }
501         if (priv->account) {
502                 g_object_unref (priv->account);
503         }
504         if (priv->factory) {
505                 g_object_unref (priv->factory);
506         }
507         g_free (priv->group_name);
508
509         g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
510         g_list_free (priv->members);
511
512         g_list_foreach (priv->local_pendings, (GFunc) empathy_pending_info_free, NULL);
513         g_list_free (priv->local_pendings);
514
515         g_list_foreach (priv->remote_pendings, (GFunc) g_object_unref, NULL);
516         g_list_free (priv->remote_pendings);
517
518         G_OBJECT_CLASS (empathy_tp_group_parent_class)->finalize (object);
519 }
520
521 static void
522 empathy_tp_group_class_init (EmpathyTpGroupClass *klass)
523 {
524         GObjectClass *object_class = G_OBJECT_CLASS (klass);
525
526         object_class->finalize = tp_group_finalize;
527
528         signals[MEMBER_ADDED] =
529                 g_signal_new ("member-added",
530                               G_TYPE_FROM_CLASS (klass),
531                               G_SIGNAL_RUN_LAST,
532                               0,
533                               NULL, NULL,
534                               _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
535                               G_TYPE_NONE,
536                               4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
537
538         signals[MEMBER_REMOVED] =
539                 g_signal_new ("member-removed",
540                               G_TYPE_FROM_CLASS (klass),
541                               G_SIGNAL_RUN_LAST,
542                               0,
543                               NULL, NULL,
544                               _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
545                               G_TYPE_NONE,
546                               4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
547
548         signals[LOCAL_PENDING] =
549                 g_signal_new ("local-pending",
550                               G_TYPE_FROM_CLASS (klass),
551                               G_SIGNAL_RUN_LAST,
552                               0,
553                               NULL, NULL,
554                               _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
555                               G_TYPE_NONE,
556                               4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
557
558         signals[REMOTE_PENDING] =
559                 g_signal_new ("remote-pending",
560                               G_TYPE_FROM_CLASS (klass),
561                               G_SIGNAL_RUN_LAST,
562                               0,
563                               NULL, NULL,
564                               _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
565                               G_TYPE_NONE,
566                               4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
567
568         signals[DESTROY] =
569                 g_signal_new ("destroy",
570                               G_TYPE_FROM_CLASS (klass),
571                               G_SIGNAL_RUN_LAST,
572                               0,
573                               NULL, NULL,
574                               g_cclosure_marshal_VOID__VOID,
575                               G_TYPE_NONE,
576                               0);
577
578         g_type_class_add_private (object_class, sizeof (EmpathyTpGroupPriv));
579 }
580
581 static void
582 empathy_tp_group_init (EmpathyTpGroup *group)
583 {
584 }
585
586 EmpathyTpGroup *
587 empathy_tp_group_new (McAccount *account,
588                       TpChan    *tp_chan)
589 {
590         EmpathyTpGroup          *group;
591         EmpathyTpGroupPriv      *priv;
592         EmpathyTpContactFactory *tp_factory;
593         DBusGProxy              *group_iface;
594         GError                  *error = NULL;
595
596         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
597         g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
598
599         group_iface = tp_chan_get_interface (tp_chan,
600                                              TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
601         g_return_val_if_fail (group_iface != NULL, NULL);
602
603         group = g_object_new (EMPATHY_TYPE_TP_GROUP, NULL);
604         priv = GET_PRIV (group);
605
606         priv->account = g_object_ref (account);
607         priv->tp_chan = g_object_ref (tp_chan);
608         priv->group_iface = group_iface;
609         priv->factory = empathy_contact_factory_new ();
610
611         if (!tp_chan_iface_group_get_self_handle (priv->group_iface,
612                                                   &priv->self_handle,
613                                                   &error)) {
614                 empathy_debug (DEBUG_DOMAIN, 
615                               "Failed to get self handle: %s",
616                               error ? error->message : "No error given");
617                 g_clear_error (&error);
618         }
619
620         tp_factory = empathy_contact_factory_get_tp_factory (priv->factory,
621                                                              priv->account);
622         if (empathy_tp_contact_factory_is_ready (tp_factory)) {
623                 tp_group_ready_cb (group);
624         } else {
625                 g_signal_connect_swapped (tp_factory, "notify::ready",
626                                           G_CALLBACK (tp_group_ready_cb),
627                                           group);
628         }
629
630         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_chan), "Closed",
631                                      G_CALLBACK (tp_group_closed_cb),
632                                      group, NULL);
633         g_signal_connect (priv->tp_chan, "destroy",
634                           G_CALLBACK (tp_group_destroy_cb),
635                           group);
636
637         return group;
638 }
639
640 static void
641 tp_group_async_cb (DBusGProxy *proxy, GError *error, gpointer user_data)
642 {
643         const gchar *msg = user_data;
644
645         if (error) {
646                 empathy_debug (DEBUG_DOMAIN, "%s: %s", msg, error->message);
647         }
648 }
649
650 void
651 empathy_tp_group_close (EmpathyTpGroup *group)
652 {
653         EmpathyTpGroupPriv *priv = GET_PRIV (group);
654
655         g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
656
657         tp_chan_close_async (DBUS_G_PROXY (priv->tp_chan),
658                              tp_group_async_cb,
659                              "Failed to close");
660 }
661
662 static GArray *
663 tp_group_get_handles (GList *contacts)
664 {
665         GArray *handles;
666         guint   length;
667         GList  *l;
668
669         length = g_list_length (contacts);
670         handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
671
672         for (l = contacts; l; l = l->next) {
673                 guint handle;
674
675                 handle = empathy_contact_get_handle (l->data);
676                 g_array_append_val (handles, handle);
677         }
678
679         return handles;
680 }
681
682 void
683 empathy_tp_group_add_members (EmpathyTpGroup *group,
684                               GList          *contacts,
685                               const gchar    *message)
686 {
687         EmpathyTpGroupPriv *priv = GET_PRIV (group);
688         GArray             *handles;
689
690         g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
691         g_return_if_fail (contacts != NULL);
692
693         handles = tp_group_get_handles (contacts);
694         tp_chan_iface_group_add_members_async (priv->group_iface,
695                                                handles,
696                                                message,
697                                                tp_group_async_cb,
698                                                "Failed to add members");
699
700         g_array_free (handles, TRUE);
701 }
702
703 void
704 empathy_tp_group_add_member (EmpathyTpGroup *group,
705                              EmpathyContact *contact,
706                              const gchar    *message)
707 {
708         EmpathyTpGroupPriv *priv = GET_PRIV (group);
709         GArray             *handles;
710         guint               handle;
711
712         g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
713         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
714
715         handle = empathy_contact_get_handle (contact);
716         handles = g_array_new (FALSE, FALSE, sizeof (guint));
717         g_array_append_val (handles, handle);
718
719         tp_chan_iface_group_add_members_async (priv->group_iface,
720                                                handles,
721                                                message,
722                                                tp_group_async_cb,
723                                                "Failed to add member");
724
725         g_array_free (handles, TRUE);
726 }
727
728 void
729 empathy_tp_group_remove_members (EmpathyTpGroup *group,
730                                  GList          *contacts,
731                                  const gchar    *message)
732 {
733         EmpathyTpGroupPriv *priv = GET_PRIV (group);
734         GArray             *handles;
735
736         g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
737         g_return_if_fail (contacts != NULL);
738
739         handles = tp_group_get_handles (contacts);
740         tp_chan_iface_group_remove_members_async (priv->group_iface,
741                                                   handles,
742                                                   message,
743                                                   tp_group_async_cb,
744                                                   "Failed to remove members");
745
746         g_array_free (handles, TRUE);
747 }
748
749 void
750 empathy_tp_group_remove_member (EmpathyTpGroup *group,
751                                 EmpathyContact *contact,
752                                 const gchar    *message)
753 {
754         EmpathyTpGroupPriv *priv = GET_PRIV (group);
755         GArray             *handles;
756         guint               handle;
757
758         g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
759         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
760
761         handle = empathy_contact_get_handle (contact);
762         handles = g_array_new (FALSE, FALSE, sizeof (guint));
763         g_array_append_val (handles, handle);
764
765         tp_chan_iface_group_remove_members_async (priv->group_iface,
766                                                   handles,
767                                                   message,
768                                                   tp_group_async_cb,
769                                                   "Failed to remove member");
770
771         g_array_free (handles, TRUE);
772 }
773
774 GList *
775 empathy_tp_group_get_members (EmpathyTpGroup *group)
776 {
777         EmpathyTpGroupPriv *priv = GET_PRIV (group);
778
779         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
780
781         g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
782
783         return g_list_copy (priv->members);
784 }
785
786 GList *
787 empathy_tp_group_get_local_pendings (EmpathyTpGroup *group)
788 {
789         EmpathyTpGroupPriv *priv = GET_PRIV (group);
790         GList              *pendings = NULL, *l;
791
792         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
793
794         for (l = priv->local_pendings; l; l = l->next) {
795                 EmpathyPendingInfo *info;
796                 EmpathyPendingInfo *new_info;
797
798                 info = l->data;
799                 new_info = empathy_pending_info_new (info->member,
800                                                      info->actor,
801                                                      info->message);
802                 pendings = g_list_prepend (pendings, new_info);
803         }
804
805         return pendings;
806 }
807
808 GList *
809 empathy_tp_group_get_remote_pendings (EmpathyTpGroup *group)
810 {
811         EmpathyTpGroupPriv *priv = GET_PRIV (group);
812
813         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
814
815         g_list_foreach (priv->remote_pendings, (GFunc) g_object_ref, NULL);
816
817         return g_list_copy (priv->remote_pendings);
818 }
819
820 const gchar *
821 empathy_tp_group_get_name (EmpathyTpGroup *group)
822 {
823         EmpathyTpGroupPriv *priv;
824
825         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
826
827         priv = GET_PRIV (group);
828
829         /* Lazy initialisation */
830         if (!priv->group_name && priv->tp_chan->handle != 0) {
831                 priv->group_name = empathy_inspect_channel (priv->account, priv->tp_chan);
832         }
833
834         return priv->group_name;
835 }
836
837 EmpathyContact *
838 empathy_tp_group_get_self_contact (EmpathyTpGroup *group)
839 {
840         EmpathyTpGroupPriv *priv = GET_PRIV (group);
841
842         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
843
844         return tp_group_get_contact (group, priv->self_handle);
845 }
846
847 const gchar *
848 empathy_tp_group_get_object_path (EmpathyTpGroup *group)
849 {
850         EmpathyTpGroupPriv *priv;
851
852         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
853
854         priv = GET_PRIV (group);
855
856         return dbus_g_proxy_get_path (DBUS_G_PROXY (priv->tp_chan));
857 }
858
859 TpChan *
860 empathy_tp_group_get_channel (EmpathyTpGroup *group)
861 {
862         EmpathyTpGroupPriv *priv;
863
864         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
865
866         priv = GET_PRIV (group);
867
868         return priv->tp_chan;
869 }
870
871 gboolean
872 empathy_tp_group_is_member (EmpathyTpGroup *group,
873                             EmpathyContact *contact)
874 {
875         EmpathyTpGroupPriv *priv = GET_PRIV (group);
876
877         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
878         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
879
880         return g_list_find (priv->members, contact) != NULL;
881 }
882