]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-group.c
really fix protocol group
[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                 g_object_unref (group);
362                 return;
363         }
364
365         tp_group_members_changed_cb (priv->group_iface,
366                                      NULL,    /* message */
367                                      handles, /* added */
368                                      NULL,    /* removed */
369                                      NULL,    /* local_pending */
370                                      NULL,    /* remote_pending */
371                                      0,       /* actor */
372                                      0,       /* reason */
373                                      group);
374
375         g_array_free (handles, TRUE);
376         g_object_unref (group);
377 }
378
379 static void
380 tp_group_get_local_pending_cb (DBusGProxy *proxy,
381                                GPtrArray  *array,
382                                GError     *error,
383                                gpointer    user_data)
384 {
385         EmpathyTpGroup     *group = user_data;
386         EmpathyTpGroupPriv *priv = GET_PRIV (group);
387         GArray             *handles;
388         guint               i;
389         
390         if (error) {
391                 empathy_debug (DEBUG_DOMAIN, "Failed to get local pendings: %s",
392                                error->message);
393                 g_object_unref (group);
394                 return;
395         }
396
397         handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
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_insert_val (handles, 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;
564
565         g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
566         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
567
568         group_iface = tp_chan_get_interface (tp_chan,
569                                              TELEPATHY_CHAN_IFACE_GROUP_QUARK);
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         tp_chan_close_async (DBUS_G_PROXY (priv->tp_chan),
628                              tp_group_async_cb,
629                              "Failed to close");
630 }
631
632 static GArray *
633 tp_group_get_handles (GList *contacts)
634 {
635         GArray *handles;
636         guint   length;
637         GList  *l;
638
639         length = g_list_length (contacts);
640         handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
641
642         for (l = contacts; l; l = l->next) {
643                 guint handle;
644
645                 handle = empathy_contact_get_handle (l->data);
646                 g_array_append_val (handles, handle);
647         }
648
649         return handles;
650 }
651
652 void
653 empathy_tp_group_add_members (EmpathyTpGroup *group,
654                               GList          *contacts,
655                               const gchar    *message)
656 {
657         EmpathyTpGroupPriv *priv = GET_PRIV (group);
658         GArray             *handles;
659
660         g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
661         g_return_if_fail (contacts != NULL);
662
663         handles = tp_group_get_handles (contacts);
664         tp_chan_iface_group_add_members_async (priv->group_iface,
665                                                handles,
666                                                message,
667                                                tp_group_async_cb,
668                                                "Failed to add members");
669
670         g_array_free (handles, TRUE);
671 }
672
673 void
674 empathy_tp_group_add_member (EmpathyTpGroup *group,
675                              EmpathyContact *contact,
676                              const gchar    *message)
677 {
678         EmpathyTpGroupPriv *priv = GET_PRIV (group);
679         GArray             *handles;
680         guint               handle;
681
682         handle = empathy_contact_get_handle (contact);
683         handles = g_array_new (FALSE, FALSE, sizeof (guint));
684         g_array_append_val (handles, handle);
685
686         tp_chan_iface_group_add_members_async (priv->group_iface,
687                                                handles,
688                                                message,
689                                                tp_group_async_cb,
690                                                "Failed to add member");
691
692         g_array_free (handles, TRUE);
693 }
694
695 void
696 empathy_tp_group_remove_members (EmpathyTpGroup *group,
697                                  GList          *contacts,
698                                  const gchar    *message)
699 {
700         EmpathyTpGroupPriv *priv = GET_PRIV (group);
701         GArray             *handles;
702
703         g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
704         g_return_if_fail (contacts != NULL);
705
706         handles = tp_group_get_handles (contacts);
707         tp_chan_iface_group_remove_members_async (priv->group_iface,
708                                                   handles,
709                                                   message,
710                                                   tp_group_async_cb,
711                                                   "Failed to remove members");
712
713         g_array_free (handles, TRUE);
714 }
715
716 void
717 empathy_tp_group_remove_member (EmpathyTpGroup *group,
718                                 EmpathyContact *contact,
719                                 const gchar    *message)
720 {
721         EmpathyTpGroupPriv *priv = GET_PRIV (group);
722         GArray             *handles;
723         guint               handle;
724
725         handle = empathy_contact_get_handle (contact);
726         handles = g_array_new (FALSE, FALSE, sizeof (guint));
727         g_array_append_val (handles, handle);
728
729         tp_chan_iface_group_remove_members_async (priv->group_iface,
730                                                   handles,
731                                                   message,
732                                                   tp_group_async_cb,
733                                                   "Failed to remove member");
734
735         g_array_free (handles, TRUE);
736 }
737
738 GList *
739 empathy_tp_group_get_members (EmpathyTpGroup *group)
740 {
741         EmpathyTpGroupPriv *priv = GET_PRIV (group);
742
743         g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
744
745         return g_list_copy (priv->members);
746 }
747
748 GList *
749 empathy_tp_group_get_local_pendings (EmpathyTpGroup *group)
750 {
751         EmpathyTpGroupPriv *priv = GET_PRIV (group);
752         GList              *pendings = NULL, *l;
753
754         for (l = priv->local_pendings; l; l = l->next) {
755                 EmpathyPendingInfo *info;
756                 EmpathyPendingInfo *new_info;
757
758                 info = l->data;
759                 new_info = empathy_pending_info_new (info->member,
760                                                      info->actor,
761                                                      info->message);
762                 pendings = g_list_prepend (pendings, new_info);
763         }
764
765         return pendings;
766 }
767
768 GList *
769 empathy_tp_group_get_remote_pendings (EmpathyTpGroup *group)
770 {
771         EmpathyTpGroupPriv *priv = GET_PRIV (group);
772
773         g_list_foreach (priv->remote_pendings, (GFunc) g_object_ref, NULL);
774
775         return g_list_copy (priv->remote_pendings);
776 }
777
778 const gchar *
779 empathy_tp_group_get_name (EmpathyTpGroup *group)
780 {
781         EmpathyTpGroupPriv *priv;
782
783         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
784
785         priv = GET_PRIV (group);
786
787         /* Lazy initialisation */
788         if (!priv->group_name && priv->tp_chan->handle != 0) {
789                 priv->group_name = empathy_inspect_channel (priv->account, priv->tp_chan);
790         }
791
792         return priv->group_name;
793 }
794
795 EmpathyContact *
796 empathy_tp_group_get_self_contact (EmpathyTpGroup *group)
797 {
798         EmpathyTpGroupPriv *priv = GET_PRIV (group);
799
800         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
801
802         return tp_group_get_contact (group, priv->self_handle);
803 }
804
805 const gchar *
806 empathy_tp_group_get_object_path (EmpathyTpGroup *group)
807 {
808         EmpathyTpGroupPriv *priv;
809
810         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
811
812         priv = GET_PRIV (group);
813
814         return dbus_g_proxy_get_path (DBUS_G_PROXY (priv->tp_chan));
815 }
816
817 gboolean
818 empathy_tp_group_is_member (EmpathyTpGroup *group,
819                             EmpathyContact *contact)
820 {
821         EmpathyTpGroupPriv *priv = GET_PRIV (group);
822
823         return g_list_find (priv->members, contact) != NULL;
824 }
825