1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
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>
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-debug.h"
34 #include "empathy-utils.h"
35 #include "empathy-marshal.h"
37 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
38 EMPATHY_TYPE_TP_GROUP, EmpathyTpGroupPriv))
40 #define DEBUG_DOMAIN "TpGroup"
42 struct _EmpathyTpGroupPriv {
46 EmpathyContactFactory *factory;
51 GList *local_pendings;
52 GList *remote_pendings;
55 static void empathy_tp_group_class_init (EmpathyTpGroupClass *klass);
56 static void empathy_tp_group_init (EmpathyTpGroup *group);
73 static guint signals[LAST_SIGNAL];
75 G_DEFINE_TYPE (EmpathyTpGroup, empathy_tp_group, G_TYPE_OBJECT);
77 static EmpathyContact *
78 tp_group_get_contact (EmpathyTpGroup *group,
81 EmpathyTpGroupPriv *priv = GET_PRIV (group);
82 EmpathyContact *contact = NULL;
85 contact = empathy_contact_factory_get_from_handle (priv->factory,
90 if (contact && handle == priv->self_handle) {
91 empathy_contact_set_is_user (contact, TRUE);
98 tp_group_get_contacts (EmpathyTpGroup *group,
99 const GArray *handles)
101 EmpathyTpGroupPriv *priv = GET_PRIV (group);
108 contacts = empathy_contact_factory_get_from_handles (priv->factory,
112 /* FIXME: Only useful if the group has a different self handle than
113 * the connection, otherwise the contact factory already set that
114 * property. That can be known using group flags. */
115 for (l = contacts; l; l = l->next) {
116 if (empathy_contact_get_handle (l->data) == priv->self_handle) {
117 empathy_contact_set_is_user (l->data, TRUE);
125 empathy_pending_info_new (EmpathyContact *member,
126 EmpathyContact *actor,
127 const gchar *message)
129 EmpathyPendingInfo *info;
131 info = g_slice_new0 (EmpathyPendingInfo);
134 info->member = g_object_ref (member);
137 info->actor = g_object_ref (actor);
140 info->message = g_strdup (message);
147 empathy_pending_info_free (EmpathyPendingInfo *info)
154 g_object_unref (info->member);
157 g_object_unref (info->actor);
159 g_free (info->message);
161 g_slice_free (EmpathyPendingInfo, info);
165 tp_group_local_pending_find (gconstpointer a,
168 const EmpathyPendingInfo *info = a;
170 return (info->member != b);
174 tp_group_remove_from_pendings (EmpathyTpGroup *group,
175 EmpathyContact *contact)
177 EmpathyTpGroupPriv *priv = GET_PRIV (group);
181 l = g_list_find_custom (priv->local_pendings,
183 tp_group_local_pending_find);
185 empathy_pending_info_free (l->data);
186 priv->local_pendings = g_list_delete_link (priv->local_pendings, l);
190 l = g_list_find (priv->remote_pendings, contact);
192 g_object_unref (l->data);
193 priv->remote_pendings = g_list_delete_link (priv->remote_pendings, l);
198 tp_group_update_members (EmpathyTpGroup *group,
199 const gchar *message,
201 const GArray *removed,
202 const GArray *local_pending,
203 const GArray *remote_pending,
207 EmpathyTpGroupPriv *priv = GET_PRIV (group);
208 EmpathyContact *actor_contact = NULL;
209 GList *contacts, *l, *ll;
211 actor_contact = tp_group_get_contact (group, actor);
213 empathy_debug (DEBUG_DOMAIN, "Members changed for list %s:\n"
214 " added-len=%d, current-len=%d\n"
216 " local-pending-len=%d, current-len=%d\n"
217 " remote-pending-len=%d, current-len=%d",
219 added ? added->len : 0, g_list_length (priv->members),
220 removed ? removed->len : 0,
221 local_pending ? local_pending->len : 0,
222 g_list_length (priv->local_pendings),
223 remote_pending ? remote_pending->len : 0,
224 g_list_length (priv->remote_pendings));
227 contacts = tp_group_get_contacts (group, added);
228 for (l = contacts; l; l = l->next) {
229 tp_group_remove_from_pendings (group, l->data);
231 /* If the contact is not yet member, add it and emit signal */
232 if (!g_list_find (priv->members, l->data)) {
233 priv->members = g_list_prepend (priv->members,
234 g_object_ref (l->data));
235 g_signal_emit (group, signals[MEMBER_ADDED], 0,
236 l->data, actor_contact, reason, message);
238 g_object_unref (l->data);
240 g_list_free (contacts);
242 /* Contacts removed */
243 contacts = tp_group_get_contacts (group, removed);
244 for (l = contacts; l; l = l->next) {
245 tp_group_remove_from_pendings (group, l->data);
247 /* If the contact is member, remove it and emit signal */
248 if ((ll = g_list_find (priv->members, l->data))) {
249 g_object_unref (ll->data);
250 priv->members = g_list_delete_link (priv->members, ll);
251 g_signal_emit (group, signals[MEMBER_REMOVED], 0,
252 l->data, actor_contact, reason, message);
254 g_object_unref (l->data);
256 g_list_free (contacts);
258 /* Contacts local pending */
259 contacts = tp_group_get_contacts (group, local_pending);
260 for (l = contacts; l; l = l->next) {
261 /* If the contact is not yet local-pending, add it and emit signal */
262 if (!g_list_find_custom (priv->local_pendings, l->data,
263 tp_group_local_pending_find)) {
264 EmpathyPendingInfo *info;
266 info = empathy_pending_info_new (l->data,
270 priv->local_pendings = g_list_prepend (priv->local_pendings, info);
271 g_signal_emit (group, signals[LOCAL_PENDING], 0,
272 l->data, actor_contact, reason, message);
274 g_object_unref (l->data);
276 g_list_free (contacts);
278 /* Contacts remote pending */
279 contacts = tp_group_get_contacts (group, remote_pending);
280 for (l = contacts; l; l = l->next) {
281 /* If the contact is not yet remote-pending, add it and emit signal */
282 if (!g_list_find (priv->remote_pendings, l->data)) {
283 priv->remote_pendings = g_list_prepend (priv->remote_pendings,
284 g_object_ref (l->data));
285 g_signal_emit (group, signals[REMOTE_PENDING], 0,
286 l->data, actor_contact, reason, message);
288 g_object_unref (l->data);
290 g_list_free (contacts);
293 g_object_unref (actor_contact);
296 empathy_debug (DEBUG_DOMAIN, "Members changed done for list %s:\n"
298 " local-pendings-len=%d\n"
299 " remote-pendings-len=%d",
301 g_list_length (priv->members),
302 g_list_length (priv->local_pendings),
303 g_list_length (priv->remote_pendings));
307 tp_group_members_changed_cb (TpChannel *channel,
308 const gchar *message,
310 const GArray *removed,
311 const GArray *local_pending,
312 const GArray *remote_pending,
318 EmpathyTpGroupPriv *priv = GET_PRIV (group);
321 tp_group_update_members (EMPATHY_TP_GROUP (group), message,
323 local_pending, remote_pending,
329 tp_group_get_members_cb (TpChannel *channel,
330 const GArray *handles,
335 EmpathyTpGroupPriv *priv = GET_PRIV (group);
338 empathy_debug (DEBUG_DOMAIN, "Failed to get members: %s",
343 tp_group_update_members (EMPATHY_TP_GROUP (group),
347 NULL, /* local_pending */
348 NULL, /* remote_pending */
352 empathy_debug (DEBUG_DOMAIN, "Ready");
354 g_object_notify (group, "ready");
358 tp_group_get_local_pending_cb (TpChannel *channel,
359 const GPtrArray *array,
368 empathy_debug (DEBUG_DOMAIN, "Failed to get local pendings: %s",
373 handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
374 g_array_append_val (handles, i);
375 for (i = 0; array->len > i; i++) {
376 GValueArray *pending_struct;
377 const gchar *message;
382 pending_struct = g_ptr_array_index (array, i);
383 member_handle = g_value_get_uint (g_value_array_get_nth (pending_struct, 0));
384 actor_handle = g_value_get_uint (g_value_array_get_nth (pending_struct, 1));
385 reason = g_value_get_uint (g_value_array_get_nth (pending_struct, 2));
386 message = g_value_get_string (g_value_array_get_nth (pending_struct, 3));
388 g_array_index (handles, guint, 0) = member_handle;
390 tp_group_update_members (EMPATHY_TP_GROUP (group),
391 message, /* message */
394 handles, /* local_pending */
395 NULL, /* remote_pending */
396 actor_handle, /* actor */
397 reason); /* reason */
399 g_array_free (handles, TRUE);
403 tp_group_get_remote_pending_cb (TpChannel *channel,
404 const GArray *handles,
410 empathy_debug (DEBUG_DOMAIN, "Failed to get remote pendings: %s",
415 tp_group_update_members (EMPATHY_TP_GROUP (group),
419 NULL, /* local_pending */
420 handles, /* remote_pending */
426 tp_group_inspect_handles_cb (TpConnection *connection,
432 EmpathyTpGroupPriv *priv = GET_PRIV (group);
435 empathy_debug (DEBUG_DOMAIN, "Failed to inspect channel handle: %s",
440 priv->group_name = g_strdup (*names);
444 tp_group_invalidated_cb (TpProxy *proxy,
448 EmpathyTpGroup *group)
450 empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
451 g_signal_emit (group, signals[DESTROY], 0);
455 tp_group_get_self_handle_cb (TpChannel *proxy,
461 EmpathyTpGroupPriv *priv = GET_PRIV (group);
462 TpConnection *connection;
463 guint channel_handle;
464 guint channel_handle_type;
468 empathy_debug (DEBUG_DOMAIN, "Failed to get self handle: %s",
473 priv->self_handle = handle;
474 tp_cli_channel_interface_group_connect_to_members_changed (priv->channel,
475 tp_group_members_changed_cb,
479 /* GetMembers is called last, so it will be the last to get the reply,
480 * so we'll be ready once that call return. */
481 g_object_get (priv->channel,
482 "connection", &connection,
483 "handle-type", &channel_handle_type,
484 "handle", &channel_handle,
486 handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
487 g_array_prepend_val (handles, channel_handle);
488 tp_cli_connection_call_inspect_handles (connection, -1,
491 tp_group_inspect_handles_cb,
494 g_array_free (handles, TRUE);
496 tp_cli_channel_interface_group_call_get_local_pending_members_with_info
498 tp_group_get_local_pending_cb,
501 tp_cli_channel_interface_group_call_get_remote_pending_members
503 tp_group_get_remote_pending_cb,
506 tp_cli_channel_interface_group_call_get_members (priv->channel, -1,
507 tp_group_get_members_cb,
513 tp_group_factory_ready_cb (EmpathyTpGroup *group)
515 EmpathyTpGroupPriv *priv = GET_PRIV (group);
516 EmpathyTpContactFactory *tp_factory;
518 tp_factory = empathy_contact_factory_get_tp_factory (priv->factory, priv->account);
519 g_signal_handlers_disconnect_by_func (tp_factory, tp_group_factory_ready_cb, group);
520 tp_cli_channel_interface_group_call_get_self_handle (priv->channel, -1,
521 tp_group_get_self_handle_cb,
527 tp_group_channel_ready_cb (EmpathyTpGroup *group)
529 EmpathyTpGroupPriv *priv = GET_PRIV (group);
530 EmpathyTpContactFactory *tp_factory;
532 tp_factory = empathy_contact_factory_get_tp_factory (priv->factory,
534 if (empathy_tp_contact_factory_is_ready (tp_factory)) {
535 tp_group_factory_ready_cb (group);
537 g_signal_connect_swapped (tp_factory, "notify::ready",
538 G_CALLBACK (tp_group_factory_ready_cb),
544 tp_group_finalize (GObject *object)
546 EmpathyTpGroupPriv *priv = GET_PRIV (object);
547 EmpathyTpContactFactory *tp_factory;
549 empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
551 tp_factory = empathy_contact_factory_get_tp_factory (priv->factory, priv->account);
552 g_signal_handlers_disconnect_by_func (tp_factory, tp_group_factory_ready_cb, object);
555 g_signal_handlers_disconnect_by_func (priv->channel,
556 tp_group_invalidated_cb,
558 g_object_unref (priv->channel);
561 g_object_unref (priv->account);
564 g_object_unref (priv->factory);
566 g_free (priv->group_name);
568 g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
569 g_list_free (priv->members);
571 g_list_foreach (priv->local_pendings, (GFunc) empathy_pending_info_free, NULL);
572 g_list_free (priv->local_pendings);
574 g_list_foreach (priv->remote_pendings, (GFunc) g_object_unref, NULL);
575 g_list_free (priv->remote_pendings);
577 G_OBJECT_CLASS (empathy_tp_group_parent_class)->finalize (object);
581 tp_group_constructed (GObject *group)
583 EmpathyTpGroupPriv *priv = GET_PRIV (group);
584 gboolean channel_ready;
586 priv->factory = empathy_contact_factory_new ();
587 priv->account = empathy_channel_get_account (priv->channel);
589 g_signal_connect (priv->channel, "invalidated",
590 G_CALLBACK (tp_group_invalidated_cb),
593 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
595 tp_group_channel_ready_cb (EMPATHY_TP_GROUP (group));
597 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
598 G_CALLBACK (tp_group_channel_ready_cb),
604 tp_group_get_property (GObject *object,
609 EmpathyTpGroupPriv *priv = GET_PRIV (object);
613 g_value_set_object (value, priv->channel);
616 g_value_set_boolean (value, priv->ready);
619 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
625 tp_group_set_property (GObject *object,
630 EmpathyTpGroupPriv *priv = GET_PRIV (object);
634 priv->channel = g_object_ref (g_value_get_object (value));
637 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
643 empathy_tp_group_class_init (EmpathyTpGroupClass *klass)
645 GObjectClass *object_class = G_OBJECT_CLASS (klass);
647 object_class->finalize = tp_group_finalize;
648 object_class->constructed = tp_group_constructed;
649 object_class->get_property = tp_group_get_property;
650 object_class->set_property = tp_group_set_property;
652 g_object_class_install_property (object_class,
654 g_param_spec_object ("channel",
656 "The channel for the group",
659 G_PARAM_CONSTRUCT_ONLY));
660 g_object_class_install_property (object_class,
662 g_param_spec_boolean ("ready",
663 "Is the object ready",
664 "This object can't be used until this becomes true",
668 signals[MEMBER_ADDED] =
669 g_signal_new ("member-added",
670 G_TYPE_FROM_CLASS (klass),
674 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
676 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
678 signals[MEMBER_REMOVED] =
679 g_signal_new ("member-removed",
680 G_TYPE_FROM_CLASS (klass),
684 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
686 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
688 signals[LOCAL_PENDING] =
689 g_signal_new ("local-pending",
690 G_TYPE_FROM_CLASS (klass),
694 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
696 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
698 signals[REMOTE_PENDING] =
699 g_signal_new ("remote-pending",
700 G_TYPE_FROM_CLASS (klass),
704 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
706 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
709 g_signal_new ("destroy",
710 G_TYPE_FROM_CLASS (klass),
714 g_cclosure_marshal_VOID__VOID,
718 g_type_class_add_private (object_class, sizeof (EmpathyTpGroupPriv));
722 empathy_tp_group_init (EmpathyTpGroup *group)
727 empathy_tp_group_new (TpChannel *channel)
729 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
731 return g_object_new (EMPATHY_TYPE_TP_GROUP,
737 tp_group_async_cb (TpChannel *channel,
740 GObject *weak_object)
742 const gchar *msg = user_data;
745 empathy_debug (DEBUG_DOMAIN, "%s: %s", msg, error->message);
750 empathy_tp_group_close (EmpathyTpGroup *group)
752 EmpathyTpGroupPriv *priv = GET_PRIV (group);
754 g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
755 g_return_if_fail (priv->ready);
757 tp_cli_channel_call_close (priv->channel, -1,
759 "Failed to close", NULL,
764 tp_group_get_handles (GList *contacts)
770 length = g_list_length (contacts);
771 handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
773 for (l = contacts; l; l = l->next) {
776 handle = empathy_contact_get_handle (l->data);
777 g_array_append_val (handles, handle);
784 empathy_tp_group_add_members (EmpathyTpGroup *group,
786 const gchar *message)
788 EmpathyTpGroupPriv *priv = GET_PRIV (group);
791 g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
792 g_return_if_fail (contacts != NULL);
793 g_return_if_fail (priv->ready);
795 handles = tp_group_get_handles (contacts);
796 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
800 "Failed to add members", NULL,
802 g_array_free (handles, TRUE);
806 empathy_tp_group_remove_members (EmpathyTpGroup *group,
808 const gchar *message)
810 EmpathyTpGroupPriv *priv = GET_PRIV (group);
813 g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
814 g_return_if_fail (contacts != NULL);
815 g_return_if_fail (priv->ready);
817 handles = tp_group_get_handles (contacts);
818 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
822 "Failed to remove members", NULL,
824 g_array_free (handles, TRUE);
828 empathy_tp_group_add_member (EmpathyTpGroup *group,
829 EmpathyContact *contact,
830 const gchar *message)
834 contacts = g_list_prepend (NULL, contact);
835 empathy_tp_group_add_members (group, contacts, message);
836 g_list_free (contacts);
840 empathy_tp_group_remove_member (EmpathyTpGroup *group,
841 EmpathyContact *contact,
842 const gchar *message)
846 contacts = g_list_prepend (NULL, contact);
847 empathy_tp_group_remove_members (group, contacts, message);
848 g_list_free (contacts);
852 empathy_tp_group_get_members (EmpathyTpGroup *group)
854 EmpathyTpGroupPriv *priv = GET_PRIV (group);
856 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
858 g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
860 return g_list_copy (priv->members);
864 empathy_tp_group_get_local_pendings (EmpathyTpGroup *group)
866 EmpathyTpGroupPriv *priv = GET_PRIV (group);
867 GList *pendings = NULL, *l;
869 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
871 for (l = priv->local_pendings; l; l = l->next) {
872 EmpathyPendingInfo *info;
873 EmpathyPendingInfo *new_info;
876 new_info = empathy_pending_info_new (info->member,
879 pendings = g_list_prepend (pendings, new_info);
886 empathy_tp_group_get_remote_pendings (EmpathyTpGroup *group)
888 EmpathyTpGroupPriv *priv = GET_PRIV (group);
890 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
892 g_list_foreach (priv->remote_pendings, (GFunc) g_object_ref, NULL);
894 return g_list_copy (priv->remote_pendings);
898 empathy_tp_group_get_name (EmpathyTpGroup *group)
900 EmpathyTpGroupPriv *priv = GET_PRIV (group);
902 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
903 g_return_val_if_fail (priv->ready, NULL);
905 return priv->group_name;
909 empathy_tp_group_get_self_contact (EmpathyTpGroup *group)
911 EmpathyTpGroupPriv *priv = GET_PRIV (group);
913 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
914 g_return_val_if_fail (priv->ready, NULL);
916 return tp_group_get_contact (group, priv->self_handle);
920 empathy_tp_group_is_member (EmpathyTpGroup *group,
921 EmpathyContact *contact)
923 EmpathyTpGroupPriv *priv = GET_PRIV (group);
925 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
926 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
928 return g_list_find (priv->members, contact) != NULL;
932 empathy_tp_group_is_ready (EmpathyTpGroup *group)
934 EmpathyTpGroupPriv *priv = GET_PRIV (group);
936 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
942 empathy_tp_group_get_invitation (EmpathyTpGroup *group,
943 EmpathyContact **remote_contact)
945 EmpathyTpGroupPriv *priv = GET_PRIV (group);
946 EmpathyContact *contact = NULL;
947 EmpathyPendingInfo *invitation = NULL;
950 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
951 g_return_val_if_fail (priv->ready, NULL);
953 for (l = priv->local_pendings; l; l = l->next) {
954 EmpathyPendingInfo *info = l->data;
956 if (empathy_contact_is_user (info->member)) {
962 if (invitation && priv->members && !priv->members->next) {
963 contact = priv->members->data;
965 if (!invitation && priv->remote_pendings && !priv->remote_pendings->next) {
966 contact = priv->remote_pendings->data;
969 if (remote_contact && contact) {
970 *remote_contact = g_object_ref (contact);