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/util.h>
28 #include <telepathy-glib/interfaces.h>
30 #include "empathy-tp-group.h"
31 #include "empathy-contact-factory.h"
32 #include "empathy-utils.h"
33 #include "empathy-marshal.h"
35 #define DEBUG_FLAG EMPATHY_DEBUG_TP
36 #include "empathy-debug.h"
38 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpGroup)
43 EmpathyContactFactory *factory;
48 GList *local_pendings;
49 GList *remote_pendings;
67 static guint signals[LAST_SIGNAL];
69 G_DEFINE_TYPE (EmpathyTpGroup, empathy_tp_group, G_TYPE_OBJECT);
71 static EmpathyContact *
72 tp_group_get_contact (EmpathyTpGroup *group,
75 EmpathyTpGroupPriv *priv = GET_PRIV (group);
76 EmpathyContact *contact = NULL;
79 contact = empathy_contact_factory_get_from_handle (priv->factory,
84 if (contact && handle == priv->self_handle) {
85 empathy_contact_set_is_user (contact, TRUE);
92 tp_group_get_contacts (EmpathyTpGroup *group,
93 const GArray *handles)
95 EmpathyTpGroupPriv *priv = GET_PRIV (group);
102 contacts = empathy_contact_factory_get_from_handles (priv->factory,
106 /* FIXME: Only useful if the group has a different self handle than
107 * the connection, otherwise the contact factory already set that
108 * property. That can be known using group flags. */
109 for (l = contacts; l; l = l->next) {
110 if (empathy_contact_get_handle (l->data) == priv->self_handle) {
111 empathy_contact_set_is_user (l->data, TRUE);
119 empathy_pending_info_new (EmpathyContact *member,
120 EmpathyContact *actor,
121 const gchar *message)
123 EmpathyPendingInfo *info;
125 info = g_slice_new0 (EmpathyPendingInfo);
128 info->member = g_object_ref (member);
131 info->actor = g_object_ref (actor);
134 info->message = g_strdup (message);
141 empathy_pending_info_free (EmpathyPendingInfo *info)
148 g_object_unref (info->member);
151 g_object_unref (info->actor);
153 g_free (info->message);
155 g_slice_free (EmpathyPendingInfo, info);
159 tp_group_local_pending_find (gconstpointer a,
162 const EmpathyPendingInfo *info = a;
164 return (info->member != b);
168 tp_group_remove_from_pendings (EmpathyTpGroup *group,
169 EmpathyContact *contact)
171 EmpathyTpGroupPriv *priv = GET_PRIV (group);
175 l = g_list_find_custom (priv->local_pendings,
177 tp_group_local_pending_find);
179 empathy_pending_info_free (l->data);
180 priv->local_pendings = g_list_delete_link (priv->local_pendings, l);
184 l = g_list_find (priv->remote_pendings, contact);
186 g_object_unref (l->data);
187 priv->remote_pendings = g_list_delete_link (priv->remote_pendings, l);
192 tp_group_update_members (EmpathyTpGroup *group,
193 const gchar *message,
195 const GArray *removed,
196 const GArray *local_pending,
197 const GArray *remote_pending,
201 EmpathyTpGroupPriv *priv = GET_PRIV (group);
202 EmpathyContact *actor_contact = NULL;
203 GList *contacts, *l, *ll;
205 actor_contact = tp_group_get_contact (group, actor);
207 DEBUG ("Members changed for list %s:\n"
208 " added-len=%d, current-len=%d\n"
210 " local-pending-len=%d, current-len=%d\n"
211 " remote-pending-len=%d, current-len=%d",
212 priv->group_name, added ? added->len : 0,
213 g_list_length (priv->members), removed ? removed->len : 0,
214 local_pending ? local_pending->len : 0,
215 g_list_length (priv->local_pendings),
216 remote_pending ? remote_pending->len : 0,
217 g_list_length (priv->remote_pendings));
220 contacts = tp_group_get_contacts (group, added);
221 for (l = contacts; l; l = l->next) {
222 tp_group_remove_from_pendings (group, l->data);
224 /* If the contact is not yet member, add it and emit signal */
225 if (!g_list_find (priv->members, l->data)) {
226 priv->members = g_list_prepend (priv->members,
227 g_object_ref (l->data));
228 g_signal_emit (group, signals[MEMBER_ADDED], 0,
229 l->data, actor_contact, reason, message);
231 g_object_unref (l->data);
233 g_list_free (contacts);
235 /* Contacts removed */
236 contacts = tp_group_get_contacts (group, removed);
237 for (l = contacts; l; l = l->next) {
238 tp_group_remove_from_pendings (group, l->data);
240 /* If the contact is member, remove it and emit signal */
241 if ((ll = g_list_find (priv->members, l->data))) {
242 g_object_unref (ll->data);
243 priv->members = g_list_delete_link (priv->members, ll);
244 g_signal_emit (group, signals[MEMBER_REMOVED], 0,
245 l->data, actor_contact, reason, message);
247 g_object_unref (l->data);
249 g_list_free (contacts);
251 /* Contacts local pending */
252 contacts = tp_group_get_contacts (group, local_pending);
253 for (l = contacts; l; l = l->next) {
254 /* If the contact is not yet local-pending, add it and emit signal */
255 if (!g_list_find_custom (priv->local_pendings, l->data,
256 tp_group_local_pending_find)) {
257 EmpathyPendingInfo *info;
259 info = empathy_pending_info_new (l->data,
263 priv->local_pendings = g_list_prepend (priv->local_pendings, info);
264 g_signal_emit (group, signals[LOCAL_PENDING], 0,
265 l->data, actor_contact, reason, message);
267 g_object_unref (l->data);
269 g_list_free (contacts);
271 /* Contacts remote pending */
272 contacts = tp_group_get_contacts (group, remote_pending);
273 for (l = contacts; l; l = l->next) {
274 /* If the contact is not yet remote-pending, add it and emit signal */
275 if (!g_list_find (priv->remote_pendings, l->data)) {
276 priv->remote_pendings = g_list_prepend (priv->remote_pendings,
277 g_object_ref (l->data));
278 g_signal_emit (group, signals[REMOTE_PENDING], 0,
279 l->data, actor_contact, reason, message);
281 g_object_unref (l->data);
283 g_list_free (contacts);
286 g_object_unref (actor_contact);
289 DEBUG ("Members changed done for list %s:\n"
291 " local-pendings-len=%d\n"
292 " remote-pendings-len=%d",
293 priv->group_name, g_list_length (priv->members),
294 g_list_length (priv->local_pendings),
295 g_list_length (priv->remote_pendings));
299 tp_group_members_changed_cb (TpChannel *channel,
300 const gchar *message,
302 const GArray *removed,
303 const GArray *local_pending,
304 const GArray *remote_pending,
310 EmpathyTpGroupPriv *priv = GET_PRIV (group);
313 tp_group_update_members (EMPATHY_TP_GROUP (group), message,
315 local_pending, remote_pending,
321 tp_group_get_members_cb (TpChannel *channel,
322 const GArray *handles,
327 EmpathyTpGroupPriv *priv = GET_PRIV (group);
330 DEBUG ("Failed to get members: %s", error->message);
334 tp_group_update_members (EMPATHY_TP_GROUP (group),
338 NULL, /* local_pending */
339 NULL, /* remote_pending */
345 g_object_notify (group, "ready");
349 tp_group_get_local_pending_cb (TpChannel *channel,
350 const GPtrArray *array,
359 DEBUG ("Failed to get local pendings: %s", error->message);
363 handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
364 g_array_append_val (handles, i);
365 for (i = 0; array->len > i; i++) {
366 GValueArray *pending_struct;
367 const gchar *message;
372 pending_struct = g_ptr_array_index (array, i);
373 member_handle = g_value_get_uint (g_value_array_get_nth (pending_struct, 0));
374 actor_handle = g_value_get_uint (g_value_array_get_nth (pending_struct, 1));
375 reason = g_value_get_uint (g_value_array_get_nth (pending_struct, 2));
376 message = g_value_get_string (g_value_array_get_nth (pending_struct, 3));
378 g_array_index (handles, guint, 0) = member_handle;
380 tp_group_update_members (EMPATHY_TP_GROUP (group),
381 message, /* message */
384 handles, /* local_pending */
385 NULL, /* remote_pending */
386 actor_handle, /* actor */
387 reason); /* reason */
389 g_array_free (handles, TRUE);
393 tp_group_get_remote_pending_cb (TpChannel *channel,
394 const GArray *handles,
400 DEBUG ("Failed to get remote pendings: %s", error->message);
404 tp_group_update_members (EMPATHY_TP_GROUP (group),
408 NULL, /* local_pending */
409 handles, /* remote_pending */
415 tp_group_inspect_handles_cb (TpConnection *connection,
421 EmpathyTpGroupPriv *priv = GET_PRIV (group);
424 DEBUG ("Failed to inspect channel handle: %s", error->message);
428 priv->group_name = g_strdup (*names);
432 tp_group_invalidated_cb (TpProxy *proxy,
436 EmpathyTpGroup *group)
438 DEBUG ("Channel invalidated: %s", message);
439 g_signal_emit (group, signals[DESTROY], 0);
443 tp_group_get_self_handle_cb (TpChannel *proxy,
449 EmpathyTpGroupPriv *priv = GET_PRIV (group);
450 TpConnection *connection;
451 guint channel_handle;
452 guint channel_handle_type;
456 DEBUG ("Failed to get self handle: %s", error->message);
460 priv->self_handle = handle;
461 tp_cli_channel_interface_group_connect_to_members_changed (priv->channel,
462 tp_group_members_changed_cb,
466 /* GetMembers is called last, so it will be the last to get the reply,
467 * so we'll be ready once that call return. */
468 g_object_get (priv->channel,
469 "connection", &connection,
470 "handle-type", &channel_handle_type,
471 "handle", &channel_handle,
473 handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
474 g_array_prepend_val (handles, channel_handle);
475 tp_cli_connection_call_inspect_handles (connection, -1,
478 tp_group_inspect_handles_cb,
481 g_array_free (handles, TRUE);
483 tp_cli_channel_interface_group_call_get_local_pending_members_with_info
485 tp_group_get_local_pending_cb,
488 tp_cli_channel_interface_group_call_get_remote_pending_members
490 tp_group_get_remote_pending_cb,
493 tp_cli_channel_interface_group_call_get_members (priv->channel, -1,
494 tp_group_get_members_cb,
500 tp_group_factory_ready_cb (EmpathyTpGroup *group)
502 EmpathyTpGroupPriv *priv = GET_PRIV (group);
503 EmpathyTpContactFactory *tp_factory;
505 tp_factory = empathy_contact_factory_get_tp_factory (priv->factory, priv->account);
506 g_signal_handlers_disconnect_by_func (tp_factory, tp_group_factory_ready_cb, group);
507 tp_cli_channel_interface_group_call_get_self_handle (priv->channel, -1,
508 tp_group_get_self_handle_cb,
514 tp_group_channel_ready_cb (EmpathyTpGroup *group)
516 EmpathyTpGroupPriv *priv = GET_PRIV (group);
517 EmpathyTpContactFactory *tp_factory;
519 tp_factory = empathy_contact_factory_get_tp_factory (priv->factory,
521 if (empathy_tp_contact_factory_is_ready (tp_factory)) {
522 tp_group_factory_ready_cb (group);
524 g_signal_connect_swapped (tp_factory, "notify::ready",
525 G_CALLBACK (tp_group_factory_ready_cb),
531 tp_group_finalize (GObject *object)
533 EmpathyTpGroupPriv *priv = GET_PRIV (object);
534 EmpathyTpContactFactory *tp_factory;
536 DEBUG ("finalize: %p", object);
538 tp_factory = empathy_contact_factory_get_tp_factory (priv->factory, priv->account);
539 g_signal_handlers_disconnect_by_func (tp_factory, tp_group_factory_ready_cb, object);
542 g_signal_handlers_disconnect_by_func (priv->channel,
543 tp_group_invalidated_cb,
545 g_object_unref (priv->channel);
548 g_object_unref (priv->account);
551 g_object_unref (priv->factory);
553 g_free (priv->group_name);
555 g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
556 g_list_free (priv->members);
558 g_list_foreach (priv->local_pendings, (GFunc) empathy_pending_info_free, NULL);
559 g_list_free (priv->local_pendings);
561 g_list_foreach (priv->remote_pendings, (GFunc) g_object_unref, NULL);
562 g_list_free (priv->remote_pendings);
564 G_OBJECT_CLASS (empathy_tp_group_parent_class)->finalize (object);
568 tp_group_constructed (GObject *group)
570 EmpathyTpGroupPriv *priv = GET_PRIV (group);
571 gboolean channel_ready;
573 priv->factory = empathy_contact_factory_dup_singleton ();
574 priv->account = empathy_channel_get_account (priv->channel);
576 g_signal_connect (priv->channel, "invalidated",
577 G_CALLBACK (tp_group_invalidated_cb),
580 g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
582 tp_group_channel_ready_cb (EMPATHY_TP_GROUP (group));
584 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
585 G_CALLBACK (tp_group_channel_ready_cb),
591 tp_group_get_property (GObject *object,
596 EmpathyTpGroupPriv *priv = GET_PRIV (object);
600 g_value_set_object (value, priv->channel);
603 g_value_set_boolean (value, priv->ready);
606 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
612 tp_group_set_property (GObject *object,
617 EmpathyTpGroupPriv *priv = GET_PRIV (object);
621 priv->channel = g_object_ref (g_value_get_object (value));
624 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
630 empathy_tp_group_class_init (EmpathyTpGroupClass *klass)
632 GObjectClass *object_class = G_OBJECT_CLASS (klass);
634 object_class->finalize = tp_group_finalize;
635 object_class->constructed = tp_group_constructed;
636 object_class->get_property = tp_group_get_property;
637 object_class->set_property = tp_group_set_property;
639 g_object_class_install_property (object_class,
641 g_param_spec_object ("channel",
643 "The channel for the group",
646 G_PARAM_CONSTRUCT_ONLY));
647 g_object_class_install_property (object_class,
649 g_param_spec_boolean ("ready",
650 "Is the object ready",
651 "This object can't be used until this becomes true",
655 signals[MEMBER_ADDED] =
656 g_signal_new ("member-added",
657 G_TYPE_FROM_CLASS (klass),
661 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
663 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
665 signals[MEMBER_REMOVED] =
666 g_signal_new ("member-removed",
667 G_TYPE_FROM_CLASS (klass),
671 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
673 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
675 signals[LOCAL_PENDING] =
676 g_signal_new ("local-pending",
677 G_TYPE_FROM_CLASS (klass),
681 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
683 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
685 signals[REMOTE_PENDING] =
686 g_signal_new ("remote-pending",
687 G_TYPE_FROM_CLASS (klass),
691 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
693 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
696 g_signal_new ("destroy",
697 G_TYPE_FROM_CLASS (klass),
701 g_cclosure_marshal_VOID__VOID,
705 g_type_class_add_private (object_class, sizeof (EmpathyTpGroupPriv));
709 empathy_tp_group_init (EmpathyTpGroup *group)
711 EmpathyTpGroupPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (group,
712 EMPATHY_TYPE_TP_GROUP, EmpathyTpGroupPriv);
718 empathy_tp_group_new (TpChannel *channel)
720 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
722 return g_object_new (EMPATHY_TYPE_TP_GROUP,
728 tp_group_async_cb (TpChannel *channel,
731 GObject *weak_object)
734 DEBUG ("%s: %s", (gchar*) user_data, error->message);
739 empathy_tp_group_close (EmpathyTpGroup *group)
741 EmpathyTpGroupPriv *priv = GET_PRIV (group);
743 g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
744 g_return_if_fail (priv->ready);
746 tp_cli_channel_call_close (priv->channel, -1,
748 "Failed to close", NULL,
753 tp_group_get_handles (GList *contacts)
759 length = g_list_length (contacts);
760 handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
762 for (l = contacts; l; l = l->next) {
765 handle = empathy_contact_get_handle (l->data);
766 g_array_append_val (handles, handle);
773 empathy_tp_group_add_members (EmpathyTpGroup *group,
775 const gchar *message)
777 EmpathyTpGroupPriv *priv = GET_PRIV (group);
780 g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
781 g_return_if_fail (contacts != NULL);
782 g_return_if_fail (priv->ready);
784 handles = tp_group_get_handles (contacts);
785 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
789 "Failed to add members", NULL,
791 g_array_free (handles, TRUE);
795 empathy_tp_group_remove_members (EmpathyTpGroup *group,
797 const gchar *message)
799 EmpathyTpGroupPriv *priv = GET_PRIV (group);
802 g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
803 g_return_if_fail (contacts != NULL);
804 g_return_if_fail (priv->ready);
806 handles = tp_group_get_handles (contacts);
807 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
811 "Failed to remove members", NULL,
813 g_array_free (handles, TRUE);
817 empathy_tp_group_add_member (EmpathyTpGroup *group,
818 EmpathyContact *contact,
819 const gchar *message)
823 contacts = g_list_prepend (NULL, contact);
824 empathy_tp_group_add_members (group, contacts, message);
825 g_list_free (contacts);
829 empathy_tp_group_remove_member (EmpathyTpGroup *group,
830 EmpathyContact *contact,
831 const gchar *message)
835 contacts = g_list_prepend (NULL, contact);
836 empathy_tp_group_remove_members (group, contacts, message);
837 g_list_free (contacts);
841 empathy_tp_group_get_members (EmpathyTpGroup *group)
843 EmpathyTpGroupPriv *priv = GET_PRIV (group);
845 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
847 g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
849 return g_list_copy (priv->members);
853 empathy_tp_group_get_local_pendings (EmpathyTpGroup *group)
855 EmpathyTpGroupPriv *priv = GET_PRIV (group);
856 GList *pendings = NULL, *l;
858 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
860 for (l = priv->local_pendings; l; l = l->next) {
861 EmpathyPendingInfo *info;
862 EmpathyPendingInfo *new_info;
865 new_info = empathy_pending_info_new (info->member,
868 pendings = g_list_prepend (pendings, new_info);
875 empathy_tp_group_get_remote_pendings (EmpathyTpGroup *group)
877 EmpathyTpGroupPriv *priv = GET_PRIV (group);
879 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
881 g_list_foreach (priv->remote_pendings, (GFunc) g_object_ref, NULL);
883 return g_list_copy (priv->remote_pendings);
887 empathy_tp_group_get_name (EmpathyTpGroup *group)
889 EmpathyTpGroupPriv *priv = GET_PRIV (group);
891 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
892 g_return_val_if_fail (priv->ready, NULL);
894 return priv->group_name;
898 empathy_tp_group_get_self_contact (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 tp_group_get_contact (group, priv->self_handle);
909 empathy_tp_group_is_member (EmpathyTpGroup *group,
910 EmpathyContact *contact)
912 EmpathyTpGroupPriv *priv = GET_PRIV (group);
914 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
915 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
917 return g_list_find (priv->members, contact) != NULL;
921 empathy_tp_group_is_ready (EmpathyTpGroup *group)
923 EmpathyTpGroupPriv *priv = GET_PRIV (group);
925 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
931 empathy_tp_group_get_invitation (EmpathyTpGroup *group,
932 EmpathyContact **remote_contact)
934 EmpathyTpGroupPriv *priv = GET_PRIV (group);
935 EmpathyContact *contact = NULL;
936 EmpathyPendingInfo *invitation = NULL;
939 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
940 g_return_val_if_fail (priv->ready, NULL);
942 for (l = priv->local_pendings; l; l = l->next) {
943 EmpathyPendingInfo *info = l->data;
945 if (empathy_contact_is_user (info->member)) {
952 contact = invitation->actor;
955 if (priv->remote_pendings) {
956 contact = priv->remote_pendings->data;
958 else if (priv->members) {
959 contact = priv->members->data;
963 if (remote_contact && contact) {
964 *remote_contact = g_object_ref (contact);
971 empathy_tp_group_get_flags (EmpathyTpGroup *self)
973 EmpathyTpGroupPriv *priv = GET_PRIV (self);
975 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (self), 0);
977 if (priv->channel == NULL)
980 return tp_channel_group_get_flags (priv->channel);