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