]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-group.c
Do not export symbols outside the empathy_ namespace.
[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 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-constants.h>
30 #include <libtelepathy/tp-conn.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 && empathy_contact_get_handle (contact) == 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_finalize (GObject *object)
461 {
462         EmpathyTpGroupPriv *priv = GET_PRIV (object);
463
464         empathy_debug (DEBUG_DOMAIN, "finalize: %p");
465
466         tp_group_disconnect (EMPATHY_TP_GROUP (object));
467
468         if (priv->tp_chan) {
469                 g_object_unref (priv->tp_chan);
470         }
471         if (priv->account) {
472                 g_object_unref (priv->account);
473         }
474         if (priv->factory) {
475                 g_object_unref (priv->factory);
476         }
477         g_free (priv->group_name);
478
479         g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
480         g_list_free (priv->members);
481
482         g_list_foreach (priv->local_pendings, (GFunc) empathy_pending_info_free, NULL);
483         g_list_free (priv->local_pendings);
484
485         g_list_foreach (priv->remote_pendings, (GFunc) g_object_unref, NULL);
486         g_list_free (priv->remote_pendings);
487
488         G_OBJECT_CLASS (empathy_tp_group_parent_class)->finalize (object);
489 }
490
491 static void
492 empathy_tp_group_class_init (EmpathyTpGroupClass *klass)
493 {
494         GObjectClass *object_class = G_OBJECT_CLASS (klass);
495
496         object_class->finalize = tp_group_finalize;
497
498         signals[MEMBER_ADDED] =
499                 g_signal_new ("member-added",
500                               G_TYPE_FROM_CLASS (klass),
501                               G_SIGNAL_RUN_LAST,
502                               0,
503                               NULL, NULL,
504                               _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
505                               G_TYPE_NONE,
506                               4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
507
508         signals[MEMBER_REMOVED] =
509                 g_signal_new ("member-removed",
510                               G_TYPE_FROM_CLASS (klass),
511                               G_SIGNAL_RUN_LAST,
512                               0,
513                               NULL, NULL,
514                               _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
515                               G_TYPE_NONE,
516                               4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
517
518         signals[LOCAL_PENDING] =
519                 g_signal_new ("local-pending",
520                               G_TYPE_FROM_CLASS (klass),
521                               G_SIGNAL_RUN_LAST,
522                               0,
523                               NULL, NULL,
524                               _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
525                               G_TYPE_NONE,
526                               4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
527
528         signals[REMOTE_PENDING] =
529                 g_signal_new ("remote-pending",
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[DESTROY] =
539                 g_signal_new ("destroy",
540                               G_TYPE_FROM_CLASS (klass),
541                               G_SIGNAL_RUN_LAST,
542                               0,
543                               NULL, NULL,
544                               g_cclosure_marshal_VOID__VOID,
545                               G_TYPE_NONE,
546                               0);
547
548         g_type_class_add_private (object_class, sizeof (EmpathyTpGroupPriv));
549 }
550
551 static void
552 empathy_tp_group_init (EmpathyTpGroup *group)
553 {
554 }
555
556 EmpathyTpGroup *
557 empathy_tp_group_new (McAccount *account,
558                       TpChan    *tp_chan)
559 {
560         EmpathyTpGroup     *group;
561         EmpathyTpGroupPriv *priv;
562         DBusGProxy         *group_iface;
563         GError             *error = NULL;
564
565         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
566         g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
567
568         group_iface = tp_chan_get_interface (tp_chan,
569                                              TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
570         g_return_val_if_fail (group_iface != NULL, NULL);
571
572         group = g_object_new (EMPATHY_TYPE_TP_GROUP, NULL);
573         priv = GET_PRIV (group);
574
575         priv->account = g_object_ref (account);
576         priv->tp_chan = g_object_ref (tp_chan);
577         priv->group_iface = group_iface;
578         priv->factory = empathy_contact_factory_new ();
579
580         if (!tp_chan_iface_group_get_self_handle (priv->group_iface,
581                                                   &priv->self_handle,
582                                                   &error)) {
583                 empathy_debug (DEBUG_DOMAIN, 
584                               "Failed to get self handle: %s",
585                               error ? error->message : "No error given");
586                 g_clear_error (&error);
587         }
588
589         dbus_g_proxy_connect_signal (priv->group_iface, "MembersChanged",
590                                      G_CALLBACK (tp_group_members_changed_cb),
591                                      group, NULL);
592         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_chan), "Closed",
593                                      G_CALLBACK (tp_group_closed_cb),
594                                      group, NULL);
595         g_signal_connect (priv->tp_chan, "destroy",
596                           G_CALLBACK (tp_group_destroy_cb),
597                           group);
598
599         tp_chan_iface_group_get_members_async (priv->group_iface,
600                                                tp_group_get_members_cb,
601                                                g_object_ref (group));
602         tp_chan_iface_group_get_local_pending_members_with_info_async (priv->group_iface,
603                                                                        tp_group_get_local_pending_cb,
604                                                                        g_object_ref (group));
605         tp_chan_iface_group_get_remote_pending_members_async (priv->group_iface,
606                                                               tp_group_get_remote_pending_cb,
607                                                               g_object_ref (group));
608
609         return group;
610 }
611
612 static void
613 tp_group_async_cb (DBusGProxy *proxy, GError *error, gpointer user_data)
614 {
615         const gchar *msg = user_data;
616
617         if (error) {
618                 empathy_debug (DEBUG_DOMAIN, "%s: %s", msg, error->message);
619         }
620 }
621
622 void
623 empathy_tp_group_close (EmpathyTpGroup *group)
624 {
625         EmpathyTpGroupPriv *priv = GET_PRIV (group);
626
627         g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
628
629         tp_chan_close_async (DBUS_G_PROXY (priv->tp_chan),
630                              tp_group_async_cb,
631                              "Failed to close");
632 }
633
634 static GArray *
635 tp_group_get_handles (GList *contacts)
636 {
637         GArray *handles;
638         guint   length;
639         GList  *l;
640
641         length = g_list_length (contacts);
642         handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
643
644         for (l = contacts; l; l = l->next) {
645                 guint handle;
646
647                 handle = empathy_contact_get_handle (l->data);
648                 g_array_append_val (handles, handle);
649         }
650
651         return handles;
652 }
653
654 void
655 empathy_tp_group_add_members (EmpathyTpGroup *group,
656                               GList          *contacts,
657                               const gchar    *message)
658 {
659         EmpathyTpGroupPriv *priv = GET_PRIV (group);
660         GArray             *handles;
661
662         g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
663         g_return_if_fail (contacts != NULL);
664
665         handles = tp_group_get_handles (contacts);
666         tp_chan_iface_group_add_members_async (priv->group_iface,
667                                                handles,
668                                                message,
669                                                tp_group_async_cb,
670                                                "Failed to add members");
671
672         g_array_free (handles, TRUE);
673 }
674
675 void
676 empathy_tp_group_add_member (EmpathyTpGroup *group,
677                              EmpathyContact *contact,
678                              const gchar    *message)
679 {
680         EmpathyTpGroupPriv *priv = GET_PRIV (group);
681         GArray             *handles;
682         guint               handle;
683
684         g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
685         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
686
687         handle = empathy_contact_get_handle (contact);
688         handles = g_array_new (FALSE, FALSE, sizeof (guint));
689         g_array_append_val (handles, handle);
690
691         tp_chan_iface_group_add_members_async (priv->group_iface,
692                                                handles,
693                                                message,
694                                                tp_group_async_cb,
695                                                "Failed to add member");
696
697         g_array_free (handles, TRUE);
698 }
699
700 void
701 empathy_tp_group_remove_members (EmpathyTpGroup *group,
702                                  GList          *contacts,
703                                  const gchar    *message)
704 {
705         EmpathyTpGroupPriv *priv = GET_PRIV (group);
706         GArray             *handles;
707
708         g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
709         g_return_if_fail (contacts != NULL);
710
711         handles = tp_group_get_handles (contacts);
712         tp_chan_iface_group_remove_members_async (priv->group_iface,
713                                                   handles,
714                                                   message,
715                                                   tp_group_async_cb,
716                                                   "Failed to remove members");
717
718         g_array_free (handles, TRUE);
719 }
720
721 void
722 empathy_tp_group_remove_member (EmpathyTpGroup *group,
723                                 EmpathyContact *contact,
724                                 const gchar    *message)
725 {
726         EmpathyTpGroupPriv *priv = GET_PRIV (group);
727         GArray             *handles;
728         guint               handle;
729
730         g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
731         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
732
733         handle = empathy_contact_get_handle (contact);
734         handles = g_array_new (FALSE, FALSE, sizeof (guint));
735         g_array_append_val (handles, handle);
736
737         tp_chan_iface_group_remove_members_async (priv->group_iface,
738                                                   handles,
739                                                   message,
740                                                   tp_group_async_cb,
741                                                   "Failed to remove member");
742
743         g_array_free (handles, TRUE);
744 }
745
746 GList *
747 empathy_tp_group_get_members (EmpathyTpGroup *group)
748 {
749         EmpathyTpGroupPriv *priv = GET_PRIV (group);
750
751         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
752
753         g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
754
755         return g_list_copy (priv->members);
756 }
757
758 GList *
759 empathy_tp_group_get_local_pendings (EmpathyTpGroup *group)
760 {
761         EmpathyTpGroupPriv *priv = GET_PRIV (group);
762         GList              *pendings = NULL, *l;
763
764         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
765
766         for (l = priv->local_pendings; l; l = l->next) {
767                 EmpathyPendingInfo *info;
768                 EmpathyPendingInfo *new_info;
769
770                 info = l->data;
771                 new_info = empathy_pending_info_new (info->member,
772                                                      info->actor,
773                                                      info->message);
774                 pendings = g_list_prepend (pendings, new_info);
775         }
776
777         return pendings;
778 }
779
780 GList *
781 empathy_tp_group_get_remote_pendings (EmpathyTpGroup *group)
782 {
783         EmpathyTpGroupPriv *priv = GET_PRIV (group);
784
785         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
786
787         g_list_foreach (priv->remote_pendings, (GFunc) g_object_ref, NULL);
788
789         return g_list_copy (priv->remote_pendings);
790 }
791
792 const gchar *
793 empathy_tp_group_get_name (EmpathyTpGroup *group)
794 {
795         EmpathyTpGroupPriv *priv;
796
797         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
798
799         priv = GET_PRIV (group);
800
801         /* Lazy initialisation */
802         if (!priv->group_name && priv->tp_chan->handle != 0) {
803                 priv->group_name = empathy_inspect_channel (priv->account, priv->tp_chan);
804         }
805
806         return priv->group_name;
807 }
808
809 EmpathyContact *
810 empathy_tp_group_get_self_contact (EmpathyTpGroup *group)
811 {
812         EmpathyTpGroupPriv *priv = GET_PRIV (group);
813
814         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
815
816         return tp_group_get_contact (group, priv->self_handle);
817 }
818
819 const gchar *
820 empathy_tp_group_get_object_path (EmpathyTpGroup *group)
821 {
822         EmpathyTpGroupPriv *priv;
823
824         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
825
826         priv = GET_PRIV (group);
827
828         return dbus_g_proxy_get_path (DBUS_G_PROXY (priv->tp_chan));
829 }
830
831 gboolean
832 empathy_tp_group_is_member (EmpathyTpGroup *group,
833                             EmpathyContact *contact)
834 {
835         EmpathyTpGroupPriv *priv = GET_PRIV (group);
836
837         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
838         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
839
840         return g_list_find (priv->members, contact) != NULL;
841 }
842