Updated de translation (Andre Klapper).
[empathy-mirror.git] / libempathy / empathy-tp-group.c
blobca7ff491f7a6f1db6c849fc8d3fc390928a9706a
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2006 Xavier Claessens <xclaesse@gmail.com>
4 * Copyright (C) 2007-2008 Collabora Ltd.
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.
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.
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
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
23 #include <config.h>
25 #include <libmissioncontrol/mc-account.h>
27 #include <telepathy-glib/channel.h>
28 #include <telepathy-glib/util.h>
29 #include <telepathy-glib/interfaces.h>
31 #include "empathy-tp-group.h"
32 #include "empathy-contact-factory.h"
33 #include "empathy-utils.h"
34 #include "empathy-marshal.h"
36 #define DEBUG_FLAG EMPATHY_DEBUG_TP
37 #include "empathy-debug.h"
39 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpGroup)
40 typedef struct {
41 TpChannel *channel;
42 gboolean ready;
44 EmpathyContactFactory *factory;
45 McAccount *account;
46 gchar *group_name;
47 guint self_handle;
48 GList *members;
49 GList *local_pendings;
50 GList *remote_pendings;
51 } EmpathyTpGroupPriv;
53 enum {
54 MEMBER_ADDED,
55 MEMBER_REMOVED,
56 LOCAL_PENDING,
57 REMOTE_PENDING,
58 DESTROY,
59 LAST_SIGNAL
62 enum {
63 PROP_0,
64 PROP_CHANNEL,
65 PROP_READY
68 static guint signals[LAST_SIGNAL];
70 G_DEFINE_TYPE (EmpathyTpGroup, empathy_tp_group, G_TYPE_OBJECT);
72 static EmpathyContact *
73 tp_group_get_contact (EmpathyTpGroup *group,
74 guint handle)
76 EmpathyTpGroupPriv *priv = GET_PRIV (group);
77 EmpathyContact *contact = NULL;
79 if (handle != 0) {
80 contact = empathy_contact_factory_get_from_handle (priv->factory,
81 priv->account,
82 handle);
85 if (contact && handle == priv->self_handle) {
86 empathy_contact_set_is_user (contact, TRUE);
89 return contact;
92 static GList *
93 tp_group_get_contacts (EmpathyTpGroup *group,
94 const GArray *handles)
96 EmpathyTpGroupPriv *priv = GET_PRIV (group);
97 GList *contacts, *l;
99 if (!handles) {
100 return NULL;
103 contacts = empathy_contact_factory_get_from_handles (priv->factory,
104 priv->account,
105 handles);
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);
116 return contacts;
119 EmpathyPendingInfo *
120 empathy_pending_info_new (EmpathyContact *member,
121 EmpathyContact *actor,
122 const gchar *message)
124 EmpathyPendingInfo *info;
126 info = g_slice_new0 (EmpathyPendingInfo);
128 if (member) {
129 info->member = g_object_ref (member);
131 if (actor) {
132 info->actor = g_object_ref (actor);
134 if (message) {
135 info->message = g_strdup (message);
138 return info;
141 void
142 empathy_pending_info_free (EmpathyPendingInfo *info)
144 if (!info) {
145 return;
148 if (info->member) {
149 g_object_unref (info->member);
151 if (info->actor) {
152 g_object_unref (info->actor);
154 g_free (info->message);
156 g_slice_free (EmpathyPendingInfo, info);
159 static gint
160 tp_group_local_pending_find (gconstpointer a,
161 gconstpointer b)
163 const EmpathyPendingInfo *info = a;
165 return (info->member != b);
168 static void
169 tp_group_remove_from_pendings (EmpathyTpGroup *group,
170 EmpathyContact *contact)
172 EmpathyTpGroupPriv *priv = GET_PRIV (group);
173 GList *l;
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);
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);
192 static void
193 tp_group_update_members (EmpathyTpGroup *group,
194 const gchar *message,
195 const GArray *added,
196 const GArray *removed,
197 const GArray *local_pending,
198 const GArray *remote_pending,
199 guint actor,
200 guint reason)
202 EmpathyTpGroupPriv *priv = GET_PRIV (group);
203 EmpathyContact *actor_contact = NULL;
204 GList *contacts, *l, *ll;
206 actor_contact = tp_group_get_contact (group, actor);
208 DEBUG ("Members changed for list %s:\n"
209 " added-len=%d, current-len=%d\n"
210 " removed-len=%d\n"
211 " local-pending-len=%d, current-len=%d\n"
212 " remote-pending-len=%d, current-len=%d",
213 priv->group_name, added ? added->len : 0,
214 g_list_length (priv->members), removed ? removed->len : 0,
215 local_pending ? local_pending->len : 0,
216 g_list_length (priv->local_pendings),
217 remote_pending ? remote_pending->len : 0,
218 g_list_length (priv->remote_pendings));
220 /* Contacts added */
221 contacts = tp_group_get_contacts (group, added);
222 for (l = contacts; l; l = l->next) {
223 tp_group_remove_from_pendings (group, l->data);
225 /* If the contact is not yet member, add it and emit signal */
226 if (!g_list_find (priv->members, l->data)) {
227 priv->members = g_list_prepend (priv->members,
228 g_object_ref (l->data));
229 g_signal_emit (group, signals[MEMBER_ADDED], 0,
230 l->data, actor_contact, reason, message);
232 g_object_unref (l->data);
234 g_list_free (contacts);
236 /* Contacts removed */
237 contacts = tp_group_get_contacts (group, removed);
238 for (l = contacts; l; l = l->next) {
239 tp_group_remove_from_pendings (group, l->data);
241 /* If the contact is member, remove it and emit signal */
242 if ((ll = g_list_find (priv->members, l->data))) {
243 g_object_unref (ll->data);
244 priv->members = g_list_delete_link (priv->members, ll);
245 g_signal_emit (group, signals[MEMBER_REMOVED], 0,
246 l->data, actor_contact, reason, message);
248 g_object_unref (l->data);
250 g_list_free (contacts);
252 /* Contacts local pending */
253 contacts = tp_group_get_contacts (group, local_pending);
254 for (l = contacts; l; l = l->next) {
255 /* If the contact is not yet local-pending, add it and emit signal */
256 if (!g_list_find_custom (priv->local_pendings, l->data,
257 tp_group_local_pending_find)) {
258 EmpathyPendingInfo *info;
260 info = empathy_pending_info_new (l->data,
261 actor_contact,
262 message);
264 priv->local_pendings = g_list_prepend (priv->local_pendings, info);
265 g_signal_emit (group, signals[LOCAL_PENDING], 0,
266 l->data, actor_contact, reason, message);
268 g_object_unref (l->data);
270 g_list_free (contacts);
272 /* Contacts remote pending */
273 contacts = tp_group_get_contacts (group, remote_pending);
274 for (l = contacts; l; l = l->next) {
275 /* If the contact is not yet remote-pending, add it and emit signal */
276 if (!g_list_find (priv->remote_pendings, l->data)) {
277 priv->remote_pendings = g_list_prepend (priv->remote_pendings,
278 g_object_ref (l->data));
279 g_signal_emit (group, signals[REMOTE_PENDING], 0,
280 l->data, actor_contact, reason, message);
282 g_object_unref (l->data);
284 g_list_free (contacts);
286 if (actor_contact) {
287 g_object_unref (actor_contact);
290 DEBUG ("Members changed done for list %s:\n"
291 " members-len=%d\n"
292 " local-pendings-len=%d\n"
293 " remote-pendings-len=%d",
294 priv->group_name, g_list_length (priv->members),
295 g_list_length (priv->local_pendings),
296 g_list_length (priv->remote_pendings));
299 static void
300 tp_group_members_changed_cb (TpChannel *channel,
301 const gchar *message,
302 const GArray *added,
303 const GArray *removed,
304 const GArray *local_pending,
305 const GArray *remote_pending,
306 guint actor,
307 guint reason,
308 gpointer user_data,
309 GObject *group)
311 EmpathyTpGroupPriv *priv = GET_PRIV (group);
313 if (priv->ready) {
314 tp_group_update_members (EMPATHY_TP_GROUP (group), message,
315 added, removed,
316 local_pending, remote_pending,
317 actor, reason);
321 static void
322 tp_group_get_members_cb (TpChannel *channel,
323 const GArray *handles,
324 const GError *error,
325 gpointer user_data,
326 GObject *group)
328 EmpathyTpGroupPriv *priv = GET_PRIV (group);
330 if (error) {
331 DEBUG ("Failed to get members: %s", error->message);
332 return;
335 tp_group_update_members (EMPATHY_TP_GROUP (group),
336 NULL, /* message */
337 handles, /* added */
338 NULL, /* removed */
339 NULL, /* local_pending */
340 NULL, /* remote_pending */
341 0, /* actor */
342 0); /* reason */
344 DEBUG ("Ready");
345 priv->ready = TRUE;
346 g_object_notify (group, "ready");
349 static void
350 tp_group_get_local_pending_cb (TpChannel *channel,
351 const GPtrArray *array,
352 const GError *error,
353 gpointer user_data,
354 GObject *group)
356 GArray *handles;
357 guint i = 0;
359 if (error) {
360 DEBUG ("Failed to get local pendings: %s", error->message);
361 return;
364 handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
365 g_array_append_val (handles, i);
366 for (i = 0; array->len > i; i++) {
367 GValueArray *pending_struct;
368 const gchar *message;
369 guint member_handle;
370 guint actor_handle;
371 guint reason;
373 pending_struct = g_ptr_array_index (array, i);
374 member_handle = g_value_get_uint (g_value_array_get_nth (pending_struct, 0));
375 actor_handle = g_value_get_uint (g_value_array_get_nth (pending_struct, 1));
376 reason = g_value_get_uint (g_value_array_get_nth (pending_struct, 2));
377 message = g_value_get_string (g_value_array_get_nth (pending_struct, 3));
379 g_array_index (handles, guint, 0) = member_handle;
381 tp_group_update_members (EMPATHY_TP_GROUP (group),
382 message, /* message */
383 NULL, /* added */
384 NULL, /* removed */
385 handles, /* local_pending */
386 NULL, /* remote_pending */
387 actor_handle, /* actor */
388 reason); /* reason */
390 g_array_free (handles, TRUE);
393 static void
394 tp_group_get_remote_pending_cb (TpChannel *channel,
395 const GArray *handles,
396 const GError *error,
397 gpointer user_data,
398 GObject *group)
400 if (error) {
401 DEBUG ("Failed to get remote pendings: %s", error->message);
402 return;
405 tp_group_update_members (EMPATHY_TP_GROUP (group),
406 NULL, /* message */
407 NULL, /* added */
408 NULL, /* removed */
409 NULL, /* local_pending */
410 handles, /* remote_pending */
411 0, /* actor */
412 0); /* reason */
415 static void
416 tp_group_inspect_handles_cb (TpConnection *connection,
417 const gchar **names,
418 const GError *error,
419 gpointer user_data,
420 GObject *group)
422 EmpathyTpGroupPriv *priv = GET_PRIV (group);
424 if (error) {
425 DEBUG ("Failed to inspect channel handle: %s", error->message);
426 return;
429 priv->group_name = g_strdup (*names);
432 static void
433 tp_group_invalidated_cb (TpProxy *proxy,
434 guint domain,
435 gint code,
436 gchar *message,
437 EmpathyTpGroup *group)
439 DEBUG ("Channel invalidated: %s", message);
440 g_signal_emit (group, signals[DESTROY], 0);
443 static void
444 tp_group_get_self_handle_cb (TpChannel *proxy,
445 guint handle,
446 const GError *error,
447 gpointer user_data,
448 GObject *group)
450 EmpathyTpGroupPriv *priv = GET_PRIV (group);
451 TpConnection *connection;
452 guint channel_handle;
453 guint channel_handle_type;
454 GArray *handles;
456 if (error) {
457 DEBUG ("Failed to get self handle: %s", error->message);
458 return;
461 priv->self_handle = handle;
462 tp_cli_channel_interface_group_connect_to_members_changed (priv->channel,
463 tp_group_members_changed_cb,
464 NULL, NULL,
465 group, NULL);
467 /* GetMembers is called last, so it will be the last to get the reply,
468 * so we'll be ready once that call return. */
469 g_object_get (priv->channel,
470 "connection", &connection,
471 "handle-type", &channel_handle_type,
472 "handle", &channel_handle,
473 NULL);
474 handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
475 g_array_prepend_val (handles, channel_handle);
476 tp_cli_connection_call_inspect_handles (connection, -1,
477 channel_handle_type,
478 handles,
479 tp_group_inspect_handles_cb,
480 NULL, NULL,
481 group);
482 g_array_free (handles, TRUE);
484 tp_cli_channel_interface_group_call_get_local_pending_members_with_info
485 (priv->channel, -1,
486 tp_group_get_local_pending_cb,
487 NULL, NULL,
488 group);
489 tp_cli_channel_interface_group_call_get_remote_pending_members
490 (priv->channel, -1,
491 tp_group_get_remote_pending_cb,
492 NULL, NULL,
493 group);
494 tp_cli_channel_interface_group_call_get_members (priv->channel, -1,
495 tp_group_get_members_cb,
496 NULL, NULL,
497 group);
500 static void
501 tp_group_factory_ready_cb (EmpathyTpGroup *group)
503 EmpathyTpGroupPriv *priv = GET_PRIV (group);
504 EmpathyTpContactFactory *tp_factory;
506 tp_factory = empathy_contact_factory_get_tp_factory (priv->factory, priv->account);
507 g_signal_handlers_disconnect_by_func (tp_factory, tp_group_factory_ready_cb, group);
508 tp_cli_channel_interface_group_call_get_self_handle (priv->channel, -1,
509 tp_group_get_self_handle_cb,
510 NULL, NULL,
511 G_OBJECT (group));
514 static void
515 tp_group_channel_ready_cb (EmpathyTpGroup *group)
517 EmpathyTpGroupPriv *priv = GET_PRIV (group);
518 EmpathyTpContactFactory *tp_factory;
520 tp_factory = empathy_contact_factory_get_tp_factory (priv->factory,
521 priv->account);
522 if (empathy_tp_contact_factory_is_ready (tp_factory)) {
523 tp_group_factory_ready_cb (group);
524 } else {
525 g_signal_connect_swapped (tp_factory, "notify::ready",
526 G_CALLBACK (tp_group_factory_ready_cb),
527 group);
531 static void
532 tp_group_finalize (GObject *object)
534 EmpathyTpGroupPriv *priv = GET_PRIV (object);
535 EmpathyTpContactFactory *tp_factory;
537 DEBUG ("finalize: %p", object);
539 tp_factory = empathy_contact_factory_get_tp_factory (priv->factory, priv->account);
540 g_signal_handlers_disconnect_by_func (tp_factory, tp_group_factory_ready_cb, object);
542 if (priv->channel) {
543 g_signal_handlers_disconnect_by_func (priv->channel,
544 tp_group_invalidated_cb,
545 object);
546 g_object_unref (priv->channel);
548 if (priv->account) {
549 g_object_unref (priv->account);
551 if (priv->factory) {
552 g_object_unref (priv->factory);
554 g_free (priv->group_name);
556 g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
557 g_list_free (priv->members);
559 g_list_foreach (priv->local_pendings, (GFunc) empathy_pending_info_free, NULL);
560 g_list_free (priv->local_pendings);
562 g_list_foreach (priv->remote_pendings, (GFunc) g_object_unref, NULL);
563 g_list_free (priv->remote_pendings);
565 G_OBJECT_CLASS (empathy_tp_group_parent_class)->finalize (object);
568 static void
569 tp_group_constructed (GObject *group)
571 EmpathyTpGroupPriv *priv = GET_PRIV (group);
572 gboolean channel_ready;
574 priv->factory = empathy_contact_factory_new ();
575 priv->account = empathy_channel_get_account (priv->channel);
577 g_signal_connect (priv->channel, "invalidated",
578 G_CALLBACK (tp_group_invalidated_cb),
579 group);
581 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
582 if (channel_ready) {
583 tp_group_channel_ready_cb (EMPATHY_TP_GROUP (group));
584 } else {
585 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
586 G_CALLBACK (tp_group_channel_ready_cb),
587 group);
591 static void
592 tp_group_get_property (GObject *object,
593 guint param_id,
594 GValue *value,
595 GParamSpec *pspec)
597 EmpathyTpGroupPriv *priv = GET_PRIV (object);
599 switch (param_id) {
600 case PROP_CHANNEL:
601 g_value_set_object (value, priv->channel);
602 break;
603 case PROP_READY:
604 g_value_set_boolean (value, priv->ready);
605 break;
606 default:
607 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
608 break;
612 static void
613 tp_group_set_property (GObject *object,
614 guint param_id,
615 const GValue *value,
616 GParamSpec *pspec)
618 EmpathyTpGroupPriv *priv = GET_PRIV (object);
620 switch (param_id) {
621 case PROP_CHANNEL:
622 priv->channel = g_object_ref (g_value_get_object (value));
623 break;
624 default:
625 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
626 break;
630 static void
631 empathy_tp_group_class_init (EmpathyTpGroupClass *klass)
633 GObjectClass *object_class = G_OBJECT_CLASS (klass);
635 object_class->finalize = tp_group_finalize;
636 object_class->constructed = tp_group_constructed;
637 object_class->get_property = tp_group_get_property;
638 object_class->set_property = tp_group_set_property;
640 g_object_class_install_property (object_class,
641 PROP_CHANNEL,
642 g_param_spec_object ("channel",
643 "telepathy channel",
644 "The channel for the group",
645 TP_TYPE_CHANNEL,
646 G_PARAM_READWRITE |
647 G_PARAM_CONSTRUCT_ONLY));
648 g_object_class_install_property (object_class,
649 PROP_READY,
650 g_param_spec_boolean ("ready",
651 "Is the object ready",
652 "This object can't be used until this becomes true",
653 FALSE,
654 G_PARAM_READABLE));
656 signals[MEMBER_ADDED] =
657 g_signal_new ("member-added",
658 G_TYPE_FROM_CLASS (klass),
659 G_SIGNAL_RUN_LAST,
661 NULL, NULL,
662 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
663 G_TYPE_NONE,
664 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
666 signals[MEMBER_REMOVED] =
667 g_signal_new ("member-removed",
668 G_TYPE_FROM_CLASS (klass),
669 G_SIGNAL_RUN_LAST,
671 NULL, NULL,
672 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
673 G_TYPE_NONE,
674 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
676 signals[LOCAL_PENDING] =
677 g_signal_new ("local-pending",
678 G_TYPE_FROM_CLASS (klass),
679 G_SIGNAL_RUN_LAST,
681 NULL, NULL,
682 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
683 G_TYPE_NONE,
684 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
686 signals[REMOTE_PENDING] =
687 g_signal_new ("remote-pending",
688 G_TYPE_FROM_CLASS (klass),
689 G_SIGNAL_RUN_LAST,
691 NULL, NULL,
692 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
693 G_TYPE_NONE,
694 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
696 signals[DESTROY] =
697 g_signal_new ("destroy",
698 G_TYPE_FROM_CLASS (klass),
699 G_SIGNAL_RUN_LAST,
701 NULL, NULL,
702 g_cclosure_marshal_VOID__VOID,
703 G_TYPE_NONE,
706 g_type_class_add_private (object_class, sizeof (EmpathyTpGroupPriv));
709 static void
710 empathy_tp_group_init (EmpathyTpGroup *group)
712 EmpathyTpGroupPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (group,
713 EMPATHY_TYPE_TP_GROUP, EmpathyTpGroupPriv);
715 group->priv = priv;
718 EmpathyTpGroup *
719 empathy_tp_group_new (TpChannel *channel)
721 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
723 return g_object_new (EMPATHY_TYPE_TP_GROUP,
724 "channel", channel,
725 NULL);
728 static void
729 tp_group_async_cb (TpChannel *channel,
730 const GError *error,
731 gpointer user_data,
732 GObject *weak_object)
734 if (error) {
735 DEBUG ("%s: %s", (gchar*) user_data, error->message);
739 void
740 empathy_tp_group_close (EmpathyTpGroup *group)
742 EmpathyTpGroupPriv *priv = GET_PRIV (group);
744 g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
745 g_return_if_fail (priv->ready);
747 tp_cli_channel_call_close (priv->channel, -1,
748 tp_group_async_cb,
749 "Failed to close", NULL,
750 G_OBJECT (group));
753 static GArray *
754 tp_group_get_handles (GList *contacts)
756 GArray *handles;
757 guint length;
758 GList *l;
760 length = g_list_length (contacts);
761 handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
763 for (l = contacts; l; l = l->next) {
764 guint handle;
766 handle = empathy_contact_get_handle (l->data);
767 g_array_append_val (handles, handle);
770 return handles;
773 void
774 empathy_tp_group_add_members (EmpathyTpGroup *group,
775 GList *contacts,
776 const gchar *message)
778 EmpathyTpGroupPriv *priv = GET_PRIV (group);
779 GArray *handles;
781 g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
782 g_return_if_fail (contacts != NULL);
783 g_return_if_fail (priv->ready);
785 handles = tp_group_get_handles (contacts);
786 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
787 handles,
788 message,
789 tp_group_async_cb,
790 "Failed to add members", NULL,
791 G_OBJECT (group));
792 g_array_free (handles, TRUE);
795 void
796 empathy_tp_group_remove_members (EmpathyTpGroup *group,
797 GList *contacts,
798 const gchar *message)
800 EmpathyTpGroupPriv *priv = GET_PRIV (group);
801 GArray *handles;
803 g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
804 g_return_if_fail (contacts != NULL);
805 g_return_if_fail (priv->ready);
807 handles = tp_group_get_handles (contacts);
808 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
809 handles,
810 message,
811 tp_group_async_cb,
812 "Failed to remove members", NULL,
813 G_OBJECT (group));
814 g_array_free (handles, TRUE);
817 void
818 empathy_tp_group_add_member (EmpathyTpGroup *group,
819 EmpathyContact *contact,
820 const gchar *message)
822 GList *contacts;
824 contacts = g_list_prepend (NULL, contact);
825 empathy_tp_group_add_members (group, contacts, message);
826 g_list_free (contacts);
829 void
830 empathy_tp_group_remove_member (EmpathyTpGroup *group,
831 EmpathyContact *contact,
832 const gchar *message)
834 GList *contacts;
836 contacts = g_list_prepend (NULL, contact);
837 empathy_tp_group_remove_members (group, contacts, message);
838 g_list_free (contacts);
841 GList *
842 empathy_tp_group_get_members (EmpathyTpGroup *group)
844 EmpathyTpGroupPriv *priv = GET_PRIV (group);
846 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
848 g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
850 return g_list_copy (priv->members);
853 GList *
854 empathy_tp_group_get_local_pendings (EmpathyTpGroup *group)
856 EmpathyTpGroupPriv *priv = GET_PRIV (group);
857 GList *pendings = NULL, *l;
859 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
861 for (l = priv->local_pendings; l; l = l->next) {
862 EmpathyPendingInfo *info;
863 EmpathyPendingInfo *new_info;
865 info = l->data;
866 new_info = empathy_pending_info_new (info->member,
867 info->actor,
868 info->message);
869 pendings = g_list_prepend (pendings, new_info);
872 return pendings;
875 GList *
876 empathy_tp_group_get_remote_pendings (EmpathyTpGroup *group)
878 EmpathyTpGroupPriv *priv = GET_PRIV (group);
880 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
882 g_list_foreach (priv->remote_pendings, (GFunc) g_object_ref, NULL);
884 return g_list_copy (priv->remote_pendings);
887 const gchar *
888 empathy_tp_group_get_name (EmpathyTpGroup *group)
890 EmpathyTpGroupPriv *priv = GET_PRIV (group);
892 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
893 g_return_val_if_fail (priv->ready, NULL);
895 return priv->group_name;
898 EmpathyContact *
899 empathy_tp_group_get_self_contact (EmpathyTpGroup *group)
901 EmpathyTpGroupPriv *priv = GET_PRIV (group);
903 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
904 g_return_val_if_fail (priv->ready, NULL);
906 return tp_group_get_contact (group, priv->self_handle);
909 gboolean
910 empathy_tp_group_is_member (EmpathyTpGroup *group,
911 EmpathyContact *contact)
913 EmpathyTpGroupPriv *priv = GET_PRIV (group);
915 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
916 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
918 return g_list_find (priv->members, contact) != NULL;
921 gboolean
922 empathy_tp_group_is_ready (EmpathyTpGroup *group)
924 EmpathyTpGroupPriv *priv = GET_PRIV (group);
926 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
928 return priv->ready;
931 EmpathyPendingInfo *
932 empathy_tp_group_get_invitation (EmpathyTpGroup *group,
933 EmpathyContact **remote_contact)
935 EmpathyTpGroupPriv *priv = GET_PRIV (group);
936 EmpathyContact *contact = NULL;
937 EmpathyPendingInfo *invitation = NULL;
938 GList *l;
940 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
941 g_return_val_if_fail (priv->ready, NULL);
943 for (l = priv->local_pendings; l; l = l->next) {
944 EmpathyPendingInfo *info = l->data;
946 if (empathy_contact_is_user (info->member)) {
947 invitation = info;
948 break;
952 if (invitation) {
953 contact = invitation->actor;
955 if (!invitation) {
956 if (priv->remote_pendings) {
957 contact = priv->remote_pendings->data;
959 else if (priv->members) {
960 contact = priv->members->data;
964 if (remote_contact && contact) {
965 *remote_contact = g_object_ref (contact);
968 return invitation;