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 <dbus/dbus-glib.h>
26 #include <libtelepathy/tp-chan.h>
27 #include <libtelepathy/tp-chan-gen.h>
28 #include <libtelepathy/tp-chan-iface-group-gen.h>
29 #include <libtelepathy/tp-conn.h>
30 #include <telepathy-glib/interfaces.h>
32 #include "empathy-tp-group.h"
33 #include "empathy-contact-factory.h"
34 #include "empathy-debug.h"
35 #include "empathy-utils.h"
36 #include "empathy-marshal.h"
38 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
39 EMPATHY_TYPE_TP_GROUP, EmpathyTpGroupPriv))
41 #define DEBUG_DOMAIN "TpGroup"
43 struct _EmpathyTpGroupPriv {
44 EmpathyContactFactory *factory;
46 DBusGProxy *group_iface;
52 GList *local_pendings;
53 GList *remote_pendings;
56 static void empathy_tp_group_class_init (EmpathyTpGroupClass *klass);
57 static void empathy_tp_group_init (EmpathyTpGroup *group);
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,
76 EmpathyTpGroupPriv *priv = GET_PRIV (group);
77 EmpathyContact *contact = NULL;
80 contact = empathy_contact_factory_get_from_handle (priv->factory,
85 if (contact && handle == priv->self_handle) {
86 empathy_contact_set_is_user (contact, TRUE);
93 tp_group_get_contacts (EmpathyTpGroup *group,
96 EmpathyTpGroupPriv *priv = GET_PRIV (group);
103 contacts = empathy_contact_factory_get_from_handles (priv->factory,
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);
120 empathy_pending_info_new (EmpathyContact *member,
121 EmpathyContact *actor,
122 const gchar *message)
124 EmpathyPendingInfo *info;
126 info = g_slice_new0 (EmpathyPendingInfo);
129 info->member = g_object_ref (member);
132 info->actor = g_object_ref (actor);
135 info->message = g_strdup (message);
142 empathy_pending_info_free (EmpathyPendingInfo *info)
149 g_object_unref (info->member);
152 g_object_unref (info->actor);
154 g_free (info->message);
156 g_slice_free (EmpathyPendingInfo, info);
160 tp_group_local_pending_find (gconstpointer a,
163 const EmpathyPendingInfo *info = a;
165 return (info->member != b);
169 tp_group_remove_from_pendings (EmpathyTpGroup *group,
170 EmpathyContact *contact)
172 EmpathyTpGroupPriv *priv = GET_PRIV (group);
176 l = g_list_find_custom (priv->local_pendings,
178 tp_group_local_pending_find);
180 empathy_pending_info_free (l->data);
181 priv->local_pendings = g_list_delete_link (priv->local_pendings, l);
185 l = g_list_find (priv->remote_pendings, contact);
187 g_object_unref (l->data);
188 priv->remote_pendings = g_list_delete_link (priv->remote_pendings, l);
193 tp_group_members_changed_cb (DBusGProxy *group_iface,
194 const gchar *message,
197 GArray *local_pending,
198 GArray *remote_pending,
201 EmpathyTpGroup *group)
203 EmpathyTpGroupPriv *priv = GET_PRIV (group);
204 EmpathyContact *actor_contact = NULL;
205 GList *contacts, *l, *ll;
207 actor_contact = tp_group_get_contact (group, actor);
209 empathy_debug (DEBUG_DOMAIN, "Members changed for list %s:\n"
210 " added-len=%d, current-len=%d\n"
212 " local-pending-len=%d, current-len=%d\n"
213 " remote-pending-len=%d, current-len=%d",
214 empathy_tp_group_get_name (group),
215 added ? added->len : 0, g_list_length (priv->members),
216 removed ? removed->len : 0,
217 local_pending ? local_pending->len : 0,
218 g_list_length (priv->local_pendings),
219 remote_pending ? remote_pending->len : 0,
220 g_list_length (priv->remote_pendings));
223 contacts = tp_group_get_contacts (group, added);
224 for (l = contacts; l; l = l->next) {
225 tp_group_remove_from_pendings (group, l->data);
227 /* If the contact is not yet member, add it and emit signal */
228 if (!g_list_find (priv->members, l->data)) {
229 priv->members = g_list_prepend (priv->members,
230 g_object_ref (l->data));
231 g_signal_emit (group, signals[MEMBER_ADDED], 0,
232 l->data, actor_contact, reason, message);
234 g_object_unref (l->data);
236 g_list_free (contacts);
238 /* Contacts removed */
239 contacts = tp_group_get_contacts (group, removed);
240 for (l = contacts; l; l = l->next) {
241 tp_group_remove_from_pendings (group, l->data);
243 /* If the contact is member, remove it and emit signal */
244 if ((ll = g_list_find (priv->members, l->data))) {
245 g_object_unref (ll->data);
246 priv->members = g_list_delete_link (priv->members, ll);
247 g_signal_emit (group, signals[MEMBER_REMOVED], 0,
248 l->data, actor_contact, reason, message);
250 g_object_unref (l->data);
252 g_list_free (contacts);
254 /* Contacts local pending */
255 contacts = tp_group_get_contacts (group, local_pending);
256 for (l = contacts; l; l = l->next) {
257 /* If the contact is not yet local-pending, add it and emit signal */
258 if (!g_list_find_custom (priv->local_pendings, l->data,
259 tp_group_local_pending_find)) {
260 EmpathyPendingInfo *info;
262 info = empathy_pending_info_new (l->data,
266 priv->local_pendings = g_list_prepend (priv->local_pendings, info);
267 g_signal_emit (group, signals[LOCAL_PENDING], 0,
268 l->data, actor_contact, reason, message);
270 g_object_unref (l->data);
272 g_list_free (contacts);
274 /* Contacts remote pending */
275 contacts = tp_group_get_contacts (group, remote_pending);
276 for (l = contacts; l; l = l->next) {
277 /* If the contact is not yet remote-pending, add it and emit signal */
278 if (!g_list_find (priv->remote_pendings, l->data)) {
279 priv->remote_pendings = g_list_prepend (priv->remote_pendings,
280 g_object_ref (l->data));
281 g_signal_emit (group, signals[REMOTE_PENDING], 0,
282 l->data, actor_contact, reason, message);
284 g_object_unref (l->data);
286 g_list_free (contacts);
289 g_object_unref (actor_contact);
292 empathy_debug (DEBUG_DOMAIN, "Members changed done for list %s:\n"
294 " local-pendings-len=%d\n"
295 " remote-pendings-len=%d",
296 empathy_tp_group_get_name (group),
297 g_list_length (priv->members),
298 g_list_length (priv->local_pendings),
299 g_list_length (priv->remote_pendings));
303 tp_group_destroy_cb (TpChan *tp_chan,
304 EmpathyTpGroup *group)
306 EmpathyTpGroupPriv *priv = GET_PRIV (group);
308 empathy_debug (DEBUG_DOMAIN, "Account disconnected or CM crashed");
310 g_object_unref (priv->tp_chan);
311 priv->group_iface = NULL;
312 priv->tp_chan = NULL;
314 g_signal_emit (group, signals[DESTROY], 0);
317 static void tp_group_closed_cb (DBusGProxy *proxy,
318 EmpathyTpGroup *group);
321 tp_group_disconnect (EmpathyTpGroup *group)
323 EmpathyTpGroupPriv *priv = GET_PRIV (group);
325 if (priv->group_iface) {
326 dbus_g_proxy_disconnect_signal (priv->group_iface, "MembersChanged",
327 G_CALLBACK (tp_group_members_changed_cb),
331 g_signal_handlers_disconnect_by_func (priv->tp_chan,
334 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->tp_chan), "Closed",
335 G_CALLBACK (tp_group_closed_cb),
341 tp_group_closed_cb (DBusGProxy *proxy,
342 EmpathyTpGroup *group)
344 tp_group_disconnect (group);
345 tp_group_destroy_cb (TELEPATHY_CHAN (proxy), group);
349 tp_group_get_members_cb (DBusGProxy *proxy,
354 EmpathyTpGroup *group = user_data;
355 EmpathyTpGroupPriv *priv = GET_PRIV (group);
358 empathy_debug (DEBUG_DOMAIN, "Failed to get members: %s",
360 g_object_unref (group);
364 tp_group_members_changed_cb (priv->group_iface,
368 NULL, /* local_pending */
369 NULL, /* remote_pending */
374 g_array_free (handles, TRUE);
375 g_object_unref (group);
379 tp_group_get_local_pending_cb (DBusGProxy *proxy,
384 EmpathyTpGroup *group = user_data;
385 EmpathyTpGroupPriv *priv = GET_PRIV (group);
390 empathy_debug (DEBUG_DOMAIN, "Failed to get local pendings: %s",
392 g_object_unref (group);
396 handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
397 g_array_append_val (handles, i);
398 for (i = 0; array->len > i; i++) {
399 GValueArray *pending_struct;
400 const gchar *message;
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));
411 g_array_index (handles, guint, 0) = member_handle;
412 tp_group_members_changed_cb (priv->group_iface,
413 message, /* message */
416 handles, /* local_pending */
417 NULL, /* remote_pending */
418 actor_handle, /* actor */
422 g_value_array_free (pending_struct);
424 g_ptr_array_free (array, TRUE);
425 g_array_free (handles, TRUE);
426 g_object_unref (group);
430 tp_group_get_remote_pending_cb (DBusGProxy *proxy,
435 EmpathyTpGroup *group = user_data;
436 EmpathyTpGroupPriv *priv = GET_PRIV (group);
439 empathy_debug (DEBUG_DOMAIN, "Failed to get remote pendings: %s",
441 g_object_unref (group);
445 tp_group_members_changed_cb (priv->group_iface,
449 NULL, /* local_pending */
450 handles, /* remote_pending */
455 g_array_free (handles, TRUE);
456 g_object_unref (group);
460 tp_group_ready_cb (EmpathyTpGroup *group)
462 EmpathyTpGroupPriv *priv = GET_PRIV (group);
463 EmpathyTpContactFactory *tp_factory;
465 empathy_debug (DEBUG_DOMAIN, "Factory ready, we can get members %p %p", group, priv);
467 tp_factory = empathy_contact_factory_get_tp_factory (priv->factory, priv->account);
468 g_signal_handlers_disconnect_by_func (tp_factory, tp_group_ready_cb, group);
470 dbus_g_proxy_connect_signal (priv->group_iface, "MembersChanged",
471 G_CALLBACK (tp_group_members_changed_cb),
474 tp_chan_iface_group_get_members_async (priv->group_iface,
475 tp_group_get_members_cb,
476 g_object_ref (group));
477 tp_chan_iface_group_get_local_pending_members_with_info_async (priv->group_iface,
478 tp_group_get_local_pending_cb,
479 g_object_ref (group));
480 tp_chan_iface_group_get_remote_pending_members_async (priv->group_iface,
481 tp_group_get_remote_pending_cb,
482 g_object_ref (group));
486 tp_group_finalize (GObject *object)
488 EmpathyTpGroupPriv *priv = GET_PRIV (object);
489 EmpathyTpContactFactory *tp_factory;
491 empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
493 tp_group_disconnect (EMPATHY_TP_GROUP (object));
495 tp_factory = empathy_contact_factory_get_tp_factory (priv->factory, priv->account);
496 g_signal_handlers_disconnect_by_func (tp_factory, tp_group_ready_cb, object);
499 g_object_unref (priv->tp_chan);
502 g_object_unref (priv->account);
505 g_object_unref (priv->factory);
507 g_free (priv->group_name);
509 g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
510 g_list_free (priv->members);
512 g_list_foreach (priv->local_pendings, (GFunc) empathy_pending_info_free, NULL);
513 g_list_free (priv->local_pendings);
515 g_list_foreach (priv->remote_pendings, (GFunc) g_object_unref, NULL);
516 g_list_free (priv->remote_pendings);
518 G_OBJECT_CLASS (empathy_tp_group_parent_class)->finalize (object);
522 empathy_tp_group_class_init (EmpathyTpGroupClass *klass)
524 GObjectClass *object_class = G_OBJECT_CLASS (klass);
526 object_class->finalize = tp_group_finalize;
528 signals[MEMBER_ADDED] =
529 g_signal_new ("member-added",
530 G_TYPE_FROM_CLASS (klass),
534 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
536 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
538 signals[MEMBER_REMOVED] =
539 g_signal_new ("member-removed",
540 G_TYPE_FROM_CLASS (klass),
544 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
546 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
548 signals[LOCAL_PENDING] =
549 g_signal_new ("local-pending",
550 G_TYPE_FROM_CLASS (klass),
554 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
556 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
558 signals[REMOTE_PENDING] =
559 g_signal_new ("remote-pending",
560 G_TYPE_FROM_CLASS (klass),
564 _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
566 4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
569 g_signal_new ("destroy",
570 G_TYPE_FROM_CLASS (klass),
574 g_cclosure_marshal_VOID__VOID,
578 g_type_class_add_private (object_class, sizeof (EmpathyTpGroupPriv));
582 empathy_tp_group_init (EmpathyTpGroup *group)
587 empathy_tp_group_new (McAccount *account,
590 EmpathyTpGroup *group;
591 EmpathyTpGroupPriv *priv;
592 EmpathyTpContactFactory *tp_factory;
593 DBusGProxy *group_iface;
594 GError *error = NULL;
596 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
597 g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
599 group_iface = tp_chan_get_interface (tp_chan,
600 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
601 g_return_val_if_fail (group_iface != NULL, NULL);
603 group = g_object_new (EMPATHY_TYPE_TP_GROUP, NULL);
604 priv = GET_PRIV (group);
606 priv->account = g_object_ref (account);
607 priv->tp_chan = g_object_ref (tp_chan);
608 priv->group_iface = group_iface;
609 priv->factory = empathy_contact_factory_new ();
611 if (!tp_chan_iface_group_get_self_handle (priv->group_iface,
614 empathy_debug (DEBUG_DOMAIN,
615 "Failed to get self handle: %s",
616 error ? error->message : "No error given");
617 g_clear_error (&error);
620 tp_factory = empathy_contact_factory_get_tp_factory (priv->factory,
622 if (empathy_tp_contact_factory_is_ready (tp_factory)) {
623 tp_group_ready_cb (group);
625 g_signal_connect_swapped (tp_factory, "notify::ready",
626 G_CALLBACK (tp_group_ready_cb),
630 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_chan), "Closed",
631 G_CALLBACK (tp_group_closed_cb),
633 g_signal_connect (priv->tp_chan, "destroy",
634 G_CALLBACK (tp_group_destroy_cb),
641 tp_group_async_cb (DBusGProxy *proxy, GError *error, gpointer user_data)
643 const gchar *msg = user_data;
646 empathy_debug (DEBUG_DOMAIN, "%s: %s", msg, error->message);
651 empathy_tp_group_close (EmpathyTpGroup *group)
653 EmpathyTpGroupPriv *priv = GET_PRIV (group);
655 g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
657 tp_chan_close_async (DBUS_G_PROXY (priv->tp_chan),
663 tp_group_get_handles (GList *contacts)
669 length = g_list_length (contacts);
670 handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
672 for (l = contacts; l; l = l->next) {
675 handle = empathy_contact_get_handle (l->data);
676 g_array_append_val (handles, handle);
683 empathy_tp_group_add_members (EmpathyTpGroup *group,
685 const gchar *message)
687 EmpathyTpGroupPriv *priv = GET_PRIV (group);
690 g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
691 g_return_if_fail (contacts != NULL);
693 handles = tp_group_get_handles (contacts);
694 tp_chan_iface_group_add_members_async (priv->group_iface,
698 "Failed to add members");
700 g_array_free (handles, TRUE);
704 empathy_tp_group_add_member (EmpathyTpGroup *group,
705 EmpathyContact *contact,
706 const gchar *message)
708 EmpathyTpGroupPriv *priv = GET_PRIV (group);
712 g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
713 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
715 handle = empathy_contact_get_handle (contact);
716 handles = g_array_new (FALSE, FALSE, sizeof (guint));
717 g_array_append_val (handles, handle);
719 tp_chan_iface_group_add_members_async (priv->group_iface,
723 "Failed to add member");
725 g_array_free (handles, TRUE);
729 empathy_tp_group_remove_members (EmpathyTpGroup *group,
731 const gchar *message)
733 EmpathyTpGroupPriv *priv = GET_PRIV (group);
736 g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
737 g_return_if_fail (contacts != NULL);
739 handles = tp_group_get_handles (contacts);
740 tp_chan_iface_group_remove_members_async (priv->group_iface,
744 "Failed to remove members");
746 g_array_free (handles, TRUE);
750 empathy_tp_group_remove_member (EmpathyTpGroup *group,
751 EmpathyContact *contact,
752 const gchar *message)
754 EmpathyTpGroupPriv *priv = GET_PRIV (group);
758 g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
759 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
761 handle = empathy_contact_get_handle (contact);
762 handles = g_array_new (FALSE, FALSE, sizeof (guint));
763 g_array_append_val (handles, handle);
765 tp_chan_iface_group_remove_members_async (priv->group_iface,
769 "Failed to remove member");
771 g_array_free (handles, TRUE);
775 empathy_tp_group_get_members (EmpathyTpGroup *group)
777 EmpathyTpGroupPriv *priv = GET_PRIV (group);
779 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
781 g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
783 return g_list_copy (priv->members);
787 empathy_tp_group_get_local_pendings (EmpathyTpGroup *group)
789 EmpathyTpGroupPriv *priv = GET_PRIV (group);
790 GList *pendings = NULL, *l;
792 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
794 for (l = priv->local_pendings; l; l = l->next) {
795 EmpathyPendingInfo *info;
796 EmpathyPendingInfo *new_info;
799 new_info = empathy_pending_info_new (info->member,
802 pendings = g_list_prepend (pendings, new_info);
809 empathy_tp_group_get_remote_pendings (EmpathyTpGroup *group)
811 EmpathyTpGroupPriv *priv = GET_PRIV (group);
813 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
815 g_list_foreach (priv->remote_pendings, (GFunc) g_object_ref, NULL);
817 return g_list_copy (priv->remote_pendings);
821 empathy_tp_group_get_name (EmpathyTpGroup *group)
823 EmpathyTpGroupPriv *priv;
825 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
827 priv = GET_PRIV (group);
829 /* Lazy initialisation */
830 if (!priv->group_name && priv->tp_chan->handle != 0) {
831 priv->group_name = empathy_inspect_channel (priv->account, priv->tp_chan);
834 return priv->group_name;
838 empathy_tp_group_get_self_contact (EmpathyTpGroup *group)
840 EmpathyTpGroupPriv *priv = GET_PRIV (group);
842 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
844 return tp_group_get_contact (group, priv->self_handle);
848 empathy_tp_group_get_object_path (EmpathyTpGroup *group)
850 EmpathyTpGroupPriv *priv;
852 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
854 priv = GET_PRIV (group);
856 return dbus_g_proxy_get_path (DBUS_G_PROXY (priv->tp_chan));
860 empathy_tp_group_get_channel (EmpathyTpGroup *group)
862 EmpathyTpGroupPriv *priv;
864 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
866 priv = GET_PRIV (group);
868 return priv->tp_chan;
872 empathy_tp_group_is_member (EmpathyTpGroup *group,
873 EmpathyContact *contact)
875 EmpathyTpGroupPriv *priv = GET_PRIV (group);
877 g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
878 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
880 return g_list_find (priv->members, contact) != NULL;