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-utils.h"
34 #include "empathy-marshal.h"
36 #define DEBUG_FLAG EMPATHY_DEBUG_TP
37 #include "empathy-debug.h"
39 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
40 EMPATHY_TYPE_TP_GROUP, EmpathyTpGroupPriv))
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 DEBUG ("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",
218 priv->group_name, added ? added->len : 0,
219 g_list_length (priv->members), removed ? removed->len : 0,
220 local_pending ? local_pending->len : 0,
221 g_list_length (priv->local_pendings),
222 remote_pending ? remote_pending->len : 0,
223 g_list_length (priv->remote_pendings));
226 contacts = tp_group_get_contacts (group, added);
227 for (l = contacts; l; l = l->next) {
228 tp_group_remove_from_pendings (group, l->data);
230 /* If the contact is not yet member, add it and emit signal */
231 if (!g_list_find (priv->members, l->data)) {
232 priv->members = g_list_prepend (priv->members,
233 g_object_ref (l->data));
234 g_signal_emit (group, signals[MEMBER_ADDED], 0,
235 l->data, actor_contact, reason, message);
237 g_object_unref (l->data);
239 g_list_free (contacts);
241 /* Contacts removed */
242 contacts = tp_group_get_contacts (group, removed);
243 for (l = contacts; l; l = l->next) {
244 tp_group_remove_from_pendings (group, l->data);
246 /* If the contact is member, remove it and emit signal */
247 if ((ll = g_list_find (priv->members, l->data))) {
248 g_object_unref (ll->data);
249 priv->members = g_list_delete_link (priv->members, ll);
250 g_signal_emit (group, signals[MEMBER_REMOVED], 0,
251 l->data, actor_contact, reason, message);
253 g_object_unref (l->data);
255 g_list_free (contacts);
257 /* Contacts local pending */
258 contacts = tp_group_get_contacts (group, local_pending);
259 for (l = contacts; l; l = l->next) {
260 /* If the contact is not yet local-pending, add it and emit signal */
261 if (!g_list_find_custom (priv->local_pendings, l->data,
262 tp_group_local_pending_find)) {
263 EmpathyPendingInfo *info;
265 info = empathy_pending_info_new (l->data,
269 priv->local_pendings = g_list_prepend (priv->local_pendings, info);
270 g_signal_emit (group, signals[LOCAL_PENDING], 0,
271 l->data, actor_contact, reason, message);
273 g_object_unref (l->data);
275 g_list_free (contacts);
277 /* Contacts remote pending */
278 contacts = tp_group_get_contacts (group, remote_pending);
279 for (l = contacts; l; l = l->next) {
280 /* If the contact is not yet remote-pending, add it and emit signal */
281 if (!g_list_find (priv->remote_pendings, l->data)) {
282 priv->remote_pendings = g_list_prepend (priv->remote_pendings,
283 g_object_ref (l->data));
284 g_signal_emit (group, signals[REMOTE_PENDING], 0,
285 l->data, actor_contact, reason, message);
287 g_object_unref (l->data);
289 g_list_free (contacts);
292 g_object_unref (actor_contact);
295 DEBUG ("Members changed done for list %s:\n"
297 " local-pendings-len=%d\n"
298 " remote-pendings-len=%d",
299 priv->group_name, g_list_length (priv->members),
300 g_list_length (priv->local_pendings),
301 g_list_length (priv->remote_pendings));
305 tp_group_members_changed_cb (TpChannel *channel,
306 const gchar *message,
308 const GArray *removed,
309 const GArray *local_pending,
310 const GArray *remote_pending,
316 EmpathyTpGroupPriv *priv = GET_PRIV (group);
319 tp_group_update_members (EMPATHY_TP_GROUP (group), message,
321 local_pending, remote_pending,
327 tp_group_get_members_cb (TpChannel *channel,
328 const GArray *handles,
333 EmpathyTpGroupPriv *priv = GET_PRIV (group);
336 DEBUG ("Failed to get members: %s", error->message);
340 tp_group_update_members (EMPATHY_TP_GROUP (group),
344 NULL, /* local_pending */
345 NULL, /* remote_pending */
351 g_object_notify (group, "ready");
355 tp_group_get_local_pending_cb (TpChannel *channel,
356 const GPtrArray *array,
365 DEBUG ("Failed to get local pendings: %s", error->message);
369 handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
370 g_array_append_val (handles, i);
371 for (i = 0; array->len > i; i++) {
372 GValueArray *pending_struct;
373 const gchar *message;
378 pending_struct = g_ptr_array_index (array, i);
379 member_handle = g_value_get_uint (g_value_array_get_nth (pending_struct, 0));
380 actor_handle = g_value_get_uint (g_value_array_get_nth (pending_struct, 1));
381 reason = g_value_get_uint (g_value_array_get_nth (pending_struct, 2));
382 message = g_value_get_string (g_value_array_get_nth (pending_struct, 3));
384 g_array_index (handles, guint, 0) = member_handle;
386 tp_group_update_members (EMPATHY_TP_GROUP (group),
387 message, /* message */
390 handles, /* local_pending */
391 NULL, /* remote_pending */
392 actor_handle, /* actor */
393 reason); /* reason */
395 g_array_free (handles, TRUE);
399 tp_group_get_remote_pending_cb (TpChannel *channel,
400 const GArray *handles,
406 DEBUG ("Failed to get remote pendings: %s", error->message);
410 tp_group_update_members (EMPATHY_TP_GROUP (group),
414 NULL, /* local_pending */
415 handles, /* remote_pending */
421 tp_group_inspect_handles_cb (TpConnection *connection,
427 EmpathyTpGroupPriv *priv = GET_PRIV (group);
430 DEBUG ("Failed to inspect channel handle: %s", error->message);
434 priv->group_name = g_strdup (*names);
438 tp_group_invalidated_cb (TpProxy *proxy,
442 EmpathyTpGroup *group)
444 DEBUG ("Channel invalidated: %s", message);
445 g_signal_emit (group, signals[DESTROY], 0);
449 tp_group_get_self_handle_cb (TpChannel *proxy,
455 EmpathyTpGroupPriv *priv = GET_PRIV (group);
456 TpConnection *connection;
457 guint channel_handle;
458 guint channel_handle_type;
462 DEBUG ("Failed to get self handle: %s", error->message);
466 priv->self_handle = handle;
467 tp_cli_channel_interface_group_connect_to_members_changed (priv->channel,
468 tp_group_members_changed_cb,
472 /* GetMembers is called last, so it will be the last to get the reply,
473 * so we'll be ready once that call return. */
474 g_object_get (priv->channel,
475 "connection", &connection,
476 "handle-type", &channel_handle_type,
477 "handle", &channel_handle,
479 handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
480 g_array_prepend_val (handles, channel_handle);
481 tp_cli_connection_call_inspect_handles (connection, -1,
484 tp_group_inspect_handles_cb,
487 g_array_free (handles, TRUE);
489 tp_cli_channel_interface_group_call_get_local_pending_members_with_info
491 tp_group_get_local_pending_cb,
494 tp_cli_channel_interface_group_call_get_remote_pending_members
496 tp_group_get_remote_pending_cb,
499 tp_cli_channel_interface_group_call_get_members (priv->channel, -1,
500 tp_group_get_members_cb,
506 tp_group_factory_ready_cb (EmpathyTpGroup *group)
508 EmpathyTpGroupPriv *priv = GET_PRIV (group);
509 EmpathyTpContactFactory *tp_factory;
511 tp_factory = empathy_contact_factory_get_tp_factory (priv->factory, priv->account);
512 g_signal_handlers_disconnect_by_func (tp_factory, tp_group_factory_ready_cb, group);
513 tp_cli_channel_interface_group_call_get_self_handle (priv->channel, -1,
514 tp_group_get_self_handle_cb,
520 tp_group_channel_ready_cb (EmpathyTpGroup *group)
522 EmpathyTpGroupPriv *priv = GET_PRIV (group);
523 EmpathyTpContactFactory *tp_factory;
525 tp_factory = empathy_contact_factory_get_tp_factory (priv->factory,
527 if (empathy_tp_contact_factory_is_ready (tp_factory)) {
528 tp_group_factory_ready_cb (group);
530 g_signal_connect_swapped (tp_factory, "notify::ready",
531 G_CALLBACK (tp_group_factory_ready_cb),
537 tp_group_finalize (GObject *object)
539 EmpathyTpGroupPriv *priv = GET_PRIV (object);
540 EmpathyTpContactFactory *tp_factory;
542 DEBUG ("finalize: %p", object);
544 tp_factory = empathy_contact_factory_get_tp_factory (priv->factory, priv->account);
545 g_signal_handlers_disconnect_by_func (tp_factory, tp_group_factory_ready_cb, object);
548 g_signal_handlers_disconnect_by_func (priv->channel,
549 tp_group_invalidated_cb,
551 g_object_unref (priv->channel);
554 g_object_unref (priv->account);
557 g_object_unref (priv->factory);
559 g_free (priv->group_name);
561 g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
562 g_list_free (priv->members);
564 g_list_foreach (priv->local_pendings, (GFunc) empathy_pending_info_free, NULL);
565 g_list_free (priv->local_pendings);
567 g_list_foreach (priv->remote_pendings, (GFunc) g_object_unref, NULL);
568 g_list_free (priv->remote_pendings);
570 G_OBJECT_CLASS (empathy_tp_group_parent_class)->finalize (object);
574 tp_group_constructed (GObject *group)
576 EmpathyTpGroupPriv *priv = GET_PRIV (group);
577 gboolean channel_ready;
579 priv->factory = empathy_contact_factory_new ();
580 priv->account = empathy_channel_get_account (priv->channel);
582 g_signal_connect (priv->channel, "invalidated",
583 G_CALLBACK (tp_group_invalidated_cb),
586 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
588 tp_group_channel_ready_cb (EMPATHY_TP_GROUP (group));
590 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
591 G_CALLBACK (tp_group_channel_ready_cb),
597 tp_group_get_property (GObject *object,
602 EmpathyTpGroupPriv *priv = GET_PRIV (object);
606 g_value_set_object (value, priv->channel);
609 g_value_set_boolean (value, priv->ready);
612 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
618 tp_group_set_property (GObject *object,
623 EmpathyTpGroupPriv *priv = GET_PRIV (object);
627 priv->channel = g_object_ref (g_value_get_object (value));
630 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
636 empathy_tp_group_class_init (EmpathyTpGroupClass *klass)
638 GObjectClass *object_class = G_OBJECT_CLASS (klass);
640 object_class->finalize = tp_group_finalize;
641 object_class->constructed = tp_group_constructed;
642 object_class->get_property = tp_group_get_property;
643 object_class->set_property = tp_group_set_property;
645 g_object_class_install_property (object_class,
647 g_param_spec_object ("channel",
649 "The channel for the group",
652 G_PARAM_CONSTRUCT_ONLY));
653 g_object_class_install_property (object_class,
655 g_param_spec_boolean ("ready",
656 "Is the object ready",
657 "This object can't be used until this becomes true",
661 signals[MEMBER_ADDED] =
662 g_signal_new ("member-added",
663 G_TYPE_FROM_CLASS (klass),
667 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
669 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
671 signals[MEMBER_REMOVED] =
672 g_signal_new ("member-removed",
673 G_TYPE_FROM_CLASS (klass),
677 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
679 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
681 signals[LOCAL_PENDING] =
682 g_signal_new ("local-pending",
683 G_TYPE_FROM_CLASS (klass),
687 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
689 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
691 signals[REMOTE_PENDING] =
692 g_signal_new ("remote-pending",
693 G_TYPE_FROM_CLASS (klass),
697 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
699 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
702 g_signal_new ("destroy",
703 G_TYPE_FROM_CLASS (klass),
707 g_cclosure_marshal_VOID__VOID,
711 g_type_class_add_private (object_class, sizeof (EmpathyTpGroupPriv));
715 empathy_tp_group_init (EmpathyTpGroup *group)
720 empathy_tp_group_new (TpChannel *channel)
722 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
724 return g_object_new (EMPATHY_TYPE_TP_GROUP,
730 tp_group_async_cb (TpChannel *channel,
733 GObject *weak_object)
735 const gchar *msg = user_data;
738 DEBUG ("%s: %s", msg, error->message);
743 empathy_tp_group_close (EmpathyTpGroup *group)
745 EmpathyTpGroupPriv *priv = GET_PRIV (group);
747 g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
748 g_return_if_fail (priv->ready);
750 tp_cli_channel_call_close (priv->channel, -1,
752 "Failed to close", NULL,
757 tp_group_get_handles (GList *contacts)
763 length = g_list_length (contacts);
764 handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
766 for (l = contacts; l; l = l->next) {
769 handle = empathy_contact_get_handle (l->data);
770 g_array_append_val (handles, handle);
777 empathy_tp_group_add_members (EmpathyTpGroup *group,
779 const gchar *message)
781 EmpathyTpGroupPriv *priv = GET_PRIV (group);
784 g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
785 g_return_if_fail (contacts != NULL);
786 g_return_if_fail (priv->ready);
788 handles = tp_group_get_handles (contacts);
789 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
793 "Failed to add members", NULL,
795 g_array_free (handles, TRUE);
799 empathy_tp_group_remove_members (EmpathyTpGroup *group,
801 const gchar *message)
803 EmpathyTpGroupPriv *priv = GET_PRIV (group);
806 g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
807 g_return_if_fail (contacts != NULL);
808 g_return_if_fail (priv->ready);
810 handles = tp_group_get_handles (contacts);
811 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
815 "Failed to remove members", NULL,
817 g_array_free (handles, TRUE);
821 empathy_tp_group_add_member (EmpathyTpGroup *group,
822 EmpathyContact *contact,
823 const gchar *message)
827 contacts = g_list_prepend (NULL, contact);
828 empathy_tp_group_add_members (group, contacts, message);
829 g_list_free (contacts);
833 empathy_tp_group_remove_member (EmpathyTpGroup *group,
834 EmpathyContact *contact,
835 const gchar *message)
839 contacts = g_list_prepend (NULL, contact);
840 empathy_tp_group_remove_members (group, contacts, message);
841 g_list_free (contacts);
845 empathy_tp_group_get_members (EmpathyTpGroup *group)
847 EmpathyTpGroupPriv *priv = GET_PRIV (group);
849 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
851 g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
853 return g_list_copy (priv->members);
857 empathy_tp_group_get_local_pendings (EmpathyTpGroup *group)
859 EmpathyTpGroupPriv *priv = GET_PRIV (group);
860 GList *pendings = NULL, *l;
862 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
864 for (l = priv->local_pendings; l; l = l->next) {
865 EmpathyPendingInfo *info;
866 EmpathyPendingInfo *new_info;
869 new_info = empathy_pending_info_new (info->member,
872 pendings = g_list_prepend (pendings, new_info);
879 empathy_tp_group_get_remote_pendings (EmpathyTpGroup *group)
881 EmpathyTpGroupPriv *priv = GET_PRIV (group);
883 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
885 g_list_foreach (priv->remote_pendings, (GFunc) g_object_ref, NULL);
887 return g_list_copy (priv->remote_pendings);
891 empathy_tp_group_get_name (EmpathyTpGroup *group)
893 EmpathyTpGroupPriv *priv = GET_PRIV (group);
895 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
896 g_return_val_if_fail (priv->ready, NULL);
898 return priv->group_name;
902 empathy_tp_group_get_self_contact (EmpathyTpGroup *group)
904 EmpathyTpGroupPriv *priv = GET_PRIV (group);
906 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
907 g_return_val_if_fail (priv->ready, NULL);
909 return tp_group_get_contact (group, priv->self_handle);
913 empathy_tp_group_is_member (EmpathyTpGroup *group,
914 EmpathyContact *contact)
916 EmpathyTpGroupPriv *priv = GET_PRIV (group);
918 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
919 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
921 return g_list_find (priv->members, contact) != NULL;
925 empathy_tp_group_is_ready (EmpathyTpGroup *group)
927 EmpathyTpGroupPriv *priv = GET_PRIV (group);
929 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
935 empathy_tp_group_get_invitation (EmpathyTpGroup *group,
936 EmpathyContact **remote_contact)
938 EmpathyTpGroupPriv *priv = GET_PRIV (group);
939 EmpathyContact *contact = NULL;
940 EmpathyPendingInfo *invitation = NULL;
943 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
944 g_return_val_if_fail (priv->ready, NULL);
946 for (l = priv->local_pendings; l; l = l->next) {
947 EmpathyPendingInfo *info = l->data;
949 if (empathy_contact_is_user (info->member)) {
955 if (invitation && priv->members && !priv->members->next) {
956 contact = priv->members->data;
958 if (!invitation && priv->remote_pendings && !priv->remote_pendings->next) {
959 contact = priv->remote_pendings->data;
962 if (remote_contact && contact) {
963 *remote_contact = g_object_ref (contact);