]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-tp-group.c
Make use of tp-glib debug system.
[empathy.git] / libempathy / empathy-tp-group.c
index 45a5e784de4be661d5756fe256af90cdec46bb6f..62212aaadccd666f13af5387c6ca920f94da3477 100644 (file)
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
  * Copyright (C) 2006 Xavier Claessens <xclaesse@gmail.com>
+ * Copyright (C) 2007-2008 Collabora Ltd.
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or (at your option) any later version.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful,
+ * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public
- * License along with this program; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors: Xavier Claessens <xclaesse@gmail.com>
  */
 
 #include <config.h>
 
-#include <dbus/dbus-glib.h>
-#include <libtelepathy/tp-chan.h>
-#include <libtelepathy/tp-chan-iface-group-gen.h>
-#include <libtelepathy/tp-constants.h>
-#include <libtelepathy/tp-conn.h>
+#include <libmissioncontrol/mc-account.h>
+
+#include <telepathy-glib/channel.h>
+#include <telepathy-glib/util.h>
+#include <telepathy-glib/interfaces.h>
 
-#include "empathy-debug.h"
 #include "empathy-tp-group.h"
+#include "empathy-contact-factory.h"
+#include "empathy-utils.h"
 #include "empathy-marshal.h"
 
+#define DEBUG_FLAG EMPATHY_DEBUG_TP
+#include "empathy-debug.h"
+
 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
                       EMPATHY_TYPE_TP_GROUP, EmpathyTpGroupPriv))
 
-#define DEBUG_DOMAIN "TpGroup"
-
 struct _EmpathyTpGroupPriv {
-       DBusGProxy *group_iface;
-       TpConn     *tp_conn;
-       TpChan     *tp_chan;
-       gchar      *group_name;
+       TpChannel             *channel;
+       gboolean               ready;
+
+       EmpathyContactFactory *factory;
+       McAccount             *account;
+       gchar                 *group_name;
+       guint                  self_handle;
+       GList                 *members;
+       GList                 *local_pendings;
+       GList                 *remote_pendings;
 };
 
 static void empathy_tp_group_class_init (EmpathyTpGroupClass *klass);
 static void empathy_tp_group_init       (EmpathyTpGroup      *group);
-static void tp_group_finalize           (GObject             *object);
-static void tp_group_destroy_cb         (DBusGProxy          *proxy,
-                                        EmpathyTpGroup      *group);
-static void tp_group_members_changed_cb (DBusGProxy          *group_iface,
-                                        gchar               *message,
-                                        GArray              *added,
-                                        GArray              *removed,
-                                        GArray              *local_pending,
-                                        GArray              *remote_pending,
-                                        guint                actor,
-                                        guint                reason,
-                                        EmpathyTpGroup      *group);
 
 enum {
-       MEMBERS_ADDED,
-       MEMBERS_REMOVED,
+       MEMBER_ADDED,
+       MEMBER_REMOVED,
        LOCAL_PENDING,
        REMOTE_PENDING,
+       DESTROY,
        LAST_SIGNAL
 };
 
+enum {
+       PROP_0,
+       PROP_CHANNEL,
+       PROP_READY
+};
+
 static guint signals[LAST_SIGNAL];
 
 G_DEFINE_TYPE (EmpathyTpGroup, empathy_tp_group, G_TYPE_OBJECT);
 
-static void
-empathy_tp_group_class_init (EmpathyTpGroupClass *klass)
+static EmpathyContact *
+tp_group_get_contact (EmpathyTpGroup *group,
+                     guint           handle)
 {
-       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
+       EmpathyContact     *contact = NULL;
+       
+       if (handle != 0) {
+               contact = empathy_contact_factory_get_from_handle (priv->factory,
+                                                                  priv->account,
+                                                                  handle);
+       }
 
-       object_class->finalize = tp_group_finalize;
+       if (contact && handle == priv->self_handle) {
+               empathy_contact_set_is_user (contact, TRUE);
+       }
 
-       signals[MEMBERS_ADDED] =
-               g_signal_new ("members-added",
-                             G_TYPE_FROM_CLASS (klass),
-                             G_SIGNAL_RUN_LAST,
-                             0,
-                             NULL, NULL,
-                             empathy_marshal_VOID__POINTER_UINT_UINT_STRING,
-                             G_TYPE_NONE,
-                             4, G_TYPE_POINTER, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING);
+       return contact;
+}
 
-       signals[MEMBERS_REMOVED] =
-               g_signal_new ("members-removed",
-                             G_TYPE_FROM_CLASS (klass),
-                             G_SIGNAL_RUN_LAST,
-                             0,
-                             NULL, NULL,
-                             empathy_marshal_VOID__POINTER_UINT_UINT_STRING,
-                             G_TYPE_NONE,
-                             4, G_TYPE_POINTER, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING);
+static GList *
+tp_group_get_contacts (EmpathyTpGroup *group,
+                      const GArray   *handles)
+{
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
+       GList              *contacts,  *l;
 
-       signals[LOCAL_PENDING] =
-               g_signal_new ("local-pending",
-                             G_TYPE_FROM_CLASS (klass),
-                             G_SIGNAL_RUN_LAST,
-                             0,
-                             NULL, NULL,
-                             empathy_marshal_VOID__POINTER_UINT_UINT_STRING,
-                             G_TYPE_NONE,
-                             4, G_TYPE_POINTER, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING);
+       if (!handles) {
+               return NULL;
+       }
 
-       signals[REMOTE_PENDING] =
-               g_signal_new ("remote-pending",
-                             G_TYPE_FROM_CLASS (klass),
-                             G_SIGNAL_RUN_LAST,
-                             0,
-                             NULL, NULL,
-                             empathy_marshal_VOID__POINTER_UINT_UINT_STRING,
-                             G_TYPE_NONE,
-                             4, G_TYPE_POINTER, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING);
+       contacts = empathy_contact_factory_get_from_handles (priv->factory,
+                                                            priv->account,
+                                                            handles);
 
-       g_type_class_add_private (object_class, sizeof (EmpathyTpGroupPriv));
-}
+       /* FIXME: Only useful if the group has a different self handle than
+        * the connection, otherwise the contact factory already set that
+        * property. That can be known using group flags. */
+       for (l = contacts; l; l = l->next) {
+               if (empathy_contact_get_handle (l->data) == priv->self_handle) {
+                       empathy_contact_set_is_user (l->data, TRUE);
+               }
+       }
 
-static void
-empathy_tp_group_init (EmpathyTpGroup *group)
-{
+       return contacts;
 }
 
-static void
-tp_group_finalize (GObject *object)
+EmpathyPendingInfo *
+empathy_pending_info_new (EmpathyContact *member,
+                         EmpathyContact *actor,
+                         const gchar    *message)
 {
-       EmpathyTpGroupPriv *priv;
+       EmpathyPendingInfo *info;
 
-       priv = GET_PRIV (object);
+       info = g_slice_new0 (EmpathyPendingInfo);
 
-       if (priv->group_iface) {
-               g_signal_handlers_disconnect_by_func (priv->group_iface,
-                                                     tp_group_destroy_cb,
-                                                     object);
-               dbus_g_proxy_disconnect_signal (priv->group_iface, "MembersChanged",
-                                               G_CALLBACK (tp_group_members_changed_cb),
-                                               object);
-               g_object_unref (priv->group_iface);
+       if (member) {
+               info->member = g_object_ref (member);
        }
-
-       if (priv->tp_conn) {
-               g_object_unref (priv->tp_conn);
+       if (actor) {
+               info->actor = g_object_ref (actor);
+       }
+       if (message) {
+               info->message = g_strdup (message);
        }
 
-       if (priv->tp_chan) {
-               g_object_unref (priv->tp_chan);
+       return info;
+}
+
+void
+empathy_pending_info_free (EmpathyPendingInfo *info)
+{
+       if (!info) {
+               return;
        }
 
-       g_free (priv->group_name);
+       if (info->member) {
+               g_object_unref (info->member);
+       }
+       if (info->actor) {
+               g_object_unref (info->actor);
+       }
+       g_free (info->message);
 
-       G_OBJECT_CLASS (empathy_tp_group_parent_class)->finalize (object);
+       g_slice_free (EmpathyPendingInfo, info);
 }
 
-EmpathyTpGroup *
-empathy_tp_group_new (TpChan *tp_chan,
-                     TpConn *tp_conn)
+static gint
+tp_group_local_pending_find (gconstpointer a,
+                            gconstpointer b)
 {
-       EmpathyTpGroup     *group;
-       EmpathyTpGroupPriv *priv;
-       DBusGProxy         *group_iface;
+       const EmpathyPendingInfo *info = a;
 
-       g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
+       return (info->member != b);
+}
 
-       group_iface = tp_chan_get_interface (tp_chan,
-                                            TELEPATHY_CHAN_IFACE_GROUP_QUARK);
-       g_return_val_if_fail (group_iface != NULL, NULL);
+static void
+tp_group_remove_from_pendings (EmpathyTpGroup *group,
+                              EmpathyContact *contact)
+{
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
+       GList              *l;
+
+       /* local pending */
+       l = g_list_find_custom (priv->local_pendings,
+                               contact,
+                               tp_group_local_pending_find);
+       if (l) {
+               empathy_pending_info_free (l->data);
+               priv->local_pendings = g_list_delete_link (priv->local_pendings, l);
+       }
 
-       group = g_object_new (EMPATHY_TYPE_TP_GROUP, NULL);
-       priv = GET_PRIV (group);
+       /* remote pending */
+       l = g_list_find (priv->remote_pendings, contact);
+       if (l) {
+               g_object_unref (l->data);
+               priv->remote_pendings = g_list_delete_link (priv->remote_pendings, l);
+       }
+}
 
-       priv->tp_conn = g_object_ref (tp_conn);
-       priv->tp_chan = g_object_ref (tp_chan);
-       priv->group_iface = g_object_ref (group_iface);
+static void
+tp_group_update_members (EmpathyTpGroup *group,
+                        const gchar    *message,
+                        const GArray   *added,
+                        const GArray   *removed,
+                        const GArray   *local_pending,
+                        const GArray   *remote_pending,
+                        guint           actor,
+                        guint           reason)
+{
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
+       EmpathyContact     *actor_contact = NULL;
+       GList              *contacts, *l, *ll;
+
+       actor_contact = tp_group_get_contact (group, actor);
+
+       DEBUG ("Members changed for list %s:\n"
+               "  added-len=%d, current-len=%d\n"
+               "  removed-len=%d\n"
+               "  local-pending-len=%d, current-len=%d\n"
+               "  remote-pending-len=%d, current-len=%d",
+               priv->group_name, added ? added->len : 0,
+               g_list_length (priv->members), removed ? removed->len : 0,
+               local_pending ? local_pending->len : 0,
+               g_list_length (priv->local_pendings),
+               remote_pending ? remote_pending->len : 0,
+               g_list_length (priv->remote_pendings));
+
+       /* Contacts added */
+       contacts = tp_group_get_contacts (group, added);
+       for (l = contacts; l; l = l->next) {
+               tp_group_remove_from_pendings (group, l->data);
+
+               /* If the contact is not yet member, add it and emit signal */
+               if (!g_list_find (priv->members, l->data)) {
+                       priv->members = g_list_prepend (priv->members,
+                                                       g_object_ref (l->data));
+                       g_signal_emit (group, signals[MEMBER_ADDED], 0,
+                                      l->data, actor_contact, reason, message);
+               }
+               g_object_unref (l->data);
+       }
+       g_list_free (contacts);
+
+       /* Contacts removed */
+       contacts = tp_group_get_contacts (group, removed);
+       for (l = contacts; l; l = l->next) {
+               tp_group_remove_from_pendings (group, l->data);
+
+               /* If the contact is member, remove it and emit signal */
+               if ((ll = g_list_find (priv->members, l->data))) {
+                       g_object_unref (ll->data);
+                       priv->members = g_list_delete_link (priv->members, ll);
+                       g_signal_emit (group, signals[MEMBER_REMOVED], 0,
+                                      l->data, actor_contact, reason, message);
+               }
+               g_object_unref (l->data);
+       }
+       g_list_free (contacts);
+
+       /* Contacts local pending */
+       contacts = tp_group_get_contacts (group, local_pending);
+       for (l = contacts; l; l = l->next) {
+               /* If the contact is not yet local-pending, add it and emit signal */
+               if (!g_list_find_custom (priv->local_pendings, l->data,
+                                        tp_group_local_pending_find)) {
+                       EmpathyPendingInfo *info;
+
+                       info = empathy_pending_info_new (l->data,
+                                                        actor_contact,
+                                                        message);
+
+                       priv->local_pendings = g_list_prepend (priv->local_pendings, info);
+                       g_signal_emit (group, signals[LOCAL_PENDING], 0,
+                                      l->data, actor_contact, reason, message);
+               }
+               g_object_unref (l->data);
+       }
+       g_list_free (contacts);
+
+       /* Contacts remote pending */
+       contacts = tp_group_get_contacts (group, remote_pending);
+       for (l = contacts; l; l = l->next) {
+               /* If the contact is not yet remote-pending, add it and emit signal */
+               if (!g_list_find (priv->remote_pendings, l->data)) {
+                       priv->remote_pendings = g_list_prepend (priv->remote_pendings,
+                                                               g_object_ref (l->data));
+                       g_signal_emit (group, signals[REMOTE_PENDING], 0,
+                                      l->data, actor_contact, reason, message);
+               }
+               g_object_unref (l->data);
+       }
+       g_list_free (contacts);
 
-       dbus_g_proxy_connect_signal (priv->group_iface, "MembersChanged",
-                                    G_CALLBACK (tp_group_members_changed_cb),
-                                    group, NULL);
-       g_signal_connect (group_iface, "destroy",
-                         G_CALLBACK (tp_group_destroy_cb),
-                         group);
+       if (actor_contact) {
+               g_object_unref (actor_contact);
+       }
 
-       return group;
+       DEBUG ("Members changed done for list %s:\n"
+               "  members-len=%d\n"
+               "  local-pendings-len=%d\n"
+               "  remote-pendings-len=%d",
+               priv->group_name, g_list_length (priv->members),
+               g_list_length (priv->local_pendings),
+               g_list_length (priv->remote_pendings));
 }
 
-void
-empathy_tp_group_add_members (EmpathyTpGroup *group,
-                             GArray         *handles,
-                             const gchar    *message)
+static void
+tp_group_members_changed_cb (TpChannel    *channel,
+                            const gchar  *message,
+                            const GArray *added,
+                            const GArray *removed,
+                            const GArray *local_pending,
+                            const GArray *remote_pending,
+                            guint         actor,
+                            guint         reason,
+                            gpointer      user_data,
+                            GObject      *group)
 {
-       EmpathyTpGroupPriv *priv;
-       GError             *error = NULL;
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
 
-       g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
-       g_return_if_fail (handles != NULL);
+       if (priv->ready) {
+               tp_group_update_members (EMPATHY_TP_GROUP (group), message,
+                                        added, removed,
+                                        local_pending, remote_pending,
+                                        actor, reason);
+       }
+}
 
-       priv = GET_PRIV (group);
+static void
+tp_group_get_members_cb (TpChannel    *channel,
+                        const GArray *handles,
+                        const GError *error,
+                        gpointer      user_data,
+                        GObject      *group)
+{
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
 
-       if (!tp_chan_iface_group_add_members (priv->group_iface,
-                                             handles,
-                                             message,
-                                             &error)) {
-               empathy_debug (DEBUG_DOMAIN,
-                             "Failed to add members: %s",
-                             error ? error->message : "No error given");
-               g_clear_error (&error);
+       if (error) {
+               DEBUG ("Failed to get members: %s", error->message);
+               return;
        }
+
+       tp_group_update_members (EMPATHY_TP_GROUP (group),
+                                NULL,    /* message */
+                                handles, /* added */
+                                NULL,    /* removed */
+                                NULL,    /* local_pending */
+                                NULL,    /* remote_pending */
+                                0,       /* actor */
+                                0);      /* reason */
+
+       DEBUG ("Ready");
+       priv->ready = TRUE;
+       g_object_notify (group, "ready");
 }
 
-void
-empathy_tp_group_add_member (EmpathyTpGroup *group,
-                            guint           handle,
-                            const gchar    *message)
+static void
+tp_group_get_local_pending_cb (TpChannel        *channel,
+                              const GPtrArray  *array,
+                              const GError     *error,
+                              gpointer          user_data,
+                              GObject          *group)
 {
        GArray *handles;
+       guint   i = 0;
+       
+       if (error) {
+               DEBUG ("Failed to get local pendings: %s", error->message);
+               return;
+       }
 
-       handles = g_array_new (FALSE, FALSE, sizeof (guint));
-       g_array_append_val (handles, handle);
+       handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
+       g_array_append_val (handles, i);
+       for (i = 0; array->len > i; i++) {
+               GValueArray *pending_struct;
+               const gchar *message;
+               guint        member_handle;
+               guint        actor_handle;
+               guint        reason;
 
-       empathy_tp_group_add_members (group, handles, message);
+               pending_struct = g_ptr_array_index (array, i);
+               member_handle = g_value_get_uint (g_value_array_get_nth (pending_struct, 0));
+               actor_handle = g_value_get_uint (g_value_array_get_nth (pending_struct, 1));
+               reason = g_value_get_uint (g_value_array_get_nth (pending_struct, 2));
+               message = g_value_get_string (g_value_array_get_nth (pending_struct, 3));
 
+               g_array_index (handles, guint, 0) = member_handle;
+
+               tp_group_update_members (EMPATHY_TP_GROUP (group),
+                                        message,      /* message */
+                                        NULL,         /* added */
+                                        NULL,         /* removed */
+                                        handles,      /* local_pending */
+                                        NULL,         /* remote_pending */
+                                        actor_handle, /* actor */
+                                        reason);      /* reason */
+       }
        g_array_free (handles, TRUE);
 }
 
-void
-empathy_tp_group_remove_members (EmpathyTpGroup *group,
-                                GArray         *handles,
-                                const gchar    *message)
+static void
+tp_group_get_remote_pending_cb (TpChannel    *channel,
+                               const GArray *handles,
+                               const GError *error,
+                               gpointer      user_data,
+                               GObject      *group)
 {
-       EmpathyTpGroupPriv *priv;
-       GError             *error = NULL;
+       if (error) {
+               DEBUG ("Failed to get remote pendings: %s", error->message);
+               return;
+       }
 
-       g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
+       tp_group_update_members (EMPATHY_TP_GROUP (group),
+                                NULL,    /* message */
+                                NULL,    /* added */
+                                NULL,    /* removed */
+                                NULL,    /* local_pending */
+                                handles, /* remote_pending */
+                                0,       /* actor */
+                                0);      /* reason */
+}
 
-       priv = GET_PRIV (group);
+static void
+tp_group_inspect_handles_cb (TpConnection  *connection,
+                            const gchar  **names,
+                            const GError  *error,
+                            gpointer       user_data,
+                            GObject       *group)
+{
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
 
-       if (!tp_chan_iface_group_remove_members (priv->group_iface,
-                                                handles,
-                                                message,
-                                                &error)) {
-               empathy_debug (DEBUG_DOMAIN, 
-                             "Failed to remove members: %s",
-                             error ? error->message : "No error given");
-               g_clear_error (&error);
+       if (error) {
+               DEBUG ("Failed to inspect channel handle: %s", error->message);
+               return;
        }
+
+       priv->group_name = g_strdup (*names);
 }
 
-void
-empathy_tp_group_remove_member (EmpathyTpGroup *group,
-                               guint           handle,
-                               const gchar    *message)
+static void
+tp_group_invalidated_cb (TpProxy        *proxy,
+                        guint           domain,
+                        gint            code,
+                        gchar          *message,
+                        EmpathyTpGroup *group)
 {
-       GArray *handles;
+       DEBUG ("Channel invalidated: %s", message);
+       g_signal_emit (group, signals[DESTROY], 0);
+}
 
-       g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
+static void
+tp_group_get_self_handle_cb (TpChannel    *proxy,
+                            guint         handle,
+                            const GError *error,
+                            gpointer      user_data,
+                            GObject      *group)
+{
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
+       TpConnection       *connection;
+       guint               channel_handle;
+       guint               channel_handle_type;
+       GArray             *handles;
+
+       if (error) {
+               DEBUG ("Failed to get self handle: %s", error->message);
+               return;
+       }
 
-       handles = g_array_new (FALSE, FALSE, sizeof (guint));
-       g_array_append_val (handles, handle);
+       priv->self_handle = handle;
+       tp_cli_channel_interface_group_connect_to_members_changed (priv->channel,
+                                                                  tp_group_members_changed_cb,
+                                                                  NULL, NULL,
+                                                                  group, NULL);
+
+       /* GetMembers is called last, so it will be the last to get the reply,
+        * so we'll be ready once that call return. */
+       g_object_get (priv->channel,
+                     "connection", &connection,
+                     "handle-type", &channel_handle_type,
+                     "handle", &channel_handle,
+                     NULL);
+       handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
+       g_array_prepend_val (handles, channel_handle);
+       tp_cli_connection_call_inspect_handles (connection, -1,
+                                               channel_handle_type,
+                                               handles,
+                                               tp_group_inspect_handles_cb,
+                                               NULL, NULL,
+                                               group);
+       g_array_free (handles, TRUE);
 
-       empathy_tp_group_remove_members (group, handles, message);
+       tp_cli_channel_interface_group_call_get_local_pending_members_with_info
+                                                       (priv->channel, -1,
+                                                        tp_group_get_local_pending_cb,
+                                                        NULL, NULL, 
+                                                        group);
+       tp_cli_channel_interface_group_call_get_remote_pending_members
+                                                       (priv->channel, -1,
+                                                        tp_group_get_remote_pending_cb,
+                                                        NULL, NULL, 
+                                                        group);
+       tp_cli_channel_interface_group_call_get_members (priv->channel, -1,
+                                                        tp_group_get_members_cb,
+                                                        NULL, NULL, 
+                                                        group);
+}
 
-       g_array_free (handles, TRUE);
+static void
+tp_group_factory_ready_cb (EmpathyTpGroup *group)
+{
+       EmpathyTpGroupPriv      *priv = GET_PRIV (group);
+       EmpathyTpContactFactory *tp_factory;
+
+       tp_factory = empathy_contact_factory_get_tp_factory (priv->factory, priv->account);
+       g_signal_handlers_disconnect_by_func (tp_factory, tp_group_factory_ready_cb, group);
+       tp_cli_channel_interface_group_call_get_self_handle (priv->channel, -1,
+                                                            tp_group_get_self_handle_cb,
+                                                            NULL, NULL,
+                                                            G_OBJECT (group));
 }
 
-GArray *
-empathy_tp_group_get_members (EmpathyTpGroup *group)
+static void
+tp_group_channel_ready_cb (EmpathyTpGroup *group)
 {
-       EmpathyTpGroupPriv *priv;
-       GArray             *members;
-       GError             *error = NULL;
+       EmpathyTpGroupPriv      *priv = GET_PRIV (group);
+       EmpathyTpContactFactory *tp_factory;
+
+       tp_factory = empathy_contact_factory_get_tp_factory (priv->factory,
+                                                            priv->account);
+       if (empathy_tp_contact_factory_is_ready (tp_factory)) {
+               tp_group_factory_ready_cb (group);
+       } else {
+               g_signal_connect_swapped (tp_factory, "notify::ready",
+                                         G_CALLBACK (tp_group_factory_ready_cb),
+                                         group);
+       }
+}
 
-       g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
+static void
+tp_group_finalize (GObject *object)
+{
+       EmpathyTpGroupPriv      *priv = GET_PRIV (object);
+       EmpathyTpContactFactory *tp_factory;
 
-       priv = GET_PRIV (group);
+       DEBUG ("finalize: %p", object);
 
-       if (!tp_chan_iface_group_get_members (priv->group_iface,
-                                             &members,
-                                             &error)) {
-               empathy_debug (DEBUG_DOMAIN, 
-                             "Couldn't get members: %s",
-                             error ? error->message : "No error given");
-               g_clear_error (&error);
-               return NULL;
+       tp_factory = empathy_contact_factory_get_tp_factory (priv->factory, priv->account);
+       g_signal_handlers_disconnect_by_func (tp_factory, tp_group_factory_ready_cb, object);
+
+       if (priv->channel) {
+               g_signal_handlers_disconnect_by_func (priv->channel,
+                                                     tp_group_invalidated_cb,
+                                                     object);
+               g_object_unref (priv->channel);
+       }
+       if (priv->account) {
+               g_object_unref (priv->account);
        }
+       if (priv->factory) {
+               g_object_unref (priv->factory);
+       }
+       g_free (priv->group_name);
+
+       g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
+       g_list_free (priv->members);
+
+       g_list_foreach (priv->local_pendings, (GFunc) empathy_pending_info_free, NULL);
+       g_list_free (priv->local_pendings);
+
+       g_list_foreach (priv->remote_pendings, (GFunc) g_object_unref, NULL);
+       g_list_free (priv->remote_pendings);
 
-       return members;
+       G_OBJECT_CLASS (empathy_tp_group_parent_class)->finalize (object);
 }
 
-void
-empathy_tp_group_get_all_members (EmpathyTpGroup  *group,
-                                 GArray         **members,
-                                 GArray         **local_pending,
-                                 GArray         **remote_pending)
+static void
+tp_group_constructed (GObject *group)
 {
-       EmpathyTpGroupPriv *priv;
-       GError             *error = NULL;
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
+       gboolean            channel_ready;
 
-       g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
+       priv->factory = empathy_contact_factory_new ();
+       priv->account = empathy_channel_get_account (priv->channel);
 
-       priv = GET_PRIV (group);
+       g_signal_connect (priv->channel, "invalidated",
+                         G_CALLBACK (tp_group_invalidated_cb),
+                         group);
 
-       if (!tp_chan_iface_group_get_all_members (priv->group_iface,
-                                                 members,
-                                                 local_pending,
-                                                 remote_pending,
-                                                 &error)) {
-               empathy_debug (DEBUG_DOMAIN,
-                             "Couldn't get all members: %s",
-                             error ? error->message : "No error given");
-               g_clear_error (&error);
+       g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
+       if (channel_ready) {
+               tp_group_channel_ready_cb (EMPATHY_TP_GROUP (group));
+       } else {
+               g_signal_connect_swapped (priv->channel, "notify::channel-ready",
+                                         G_CALLBACK (tp_group_channel_ready_cb),
+                                         group);
        }
 }
 
-GList *
-empathy_tp_group_get_local_pending_members_with_info (EmpathyTpGroup *group)
+static void
+tp_group_get_property (GObject    *object,
+                      guint       param_id,
+                      GValue     *value,
+                      GParamSpec *pspec)
 {
-       EmpathyTpGroupPriv *priv;
-       GPtrArray          *array;
-       guint               i;
-       GList              *infos = NULL;
-       GError             *error = NULL;
-
-       g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
+       EmpathyTpGroupPriv *priv = GET_PRIV (object);
+
+       switch (param_id) {
+       case PROP_CHANNEL:
+               g_value_set_object (value, priv->channel);
+               break;
+       case PROP_READY:
+               g_value_set_boolean (value, priv->ready);
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+               break;
+       };
+}
 
-       priv = GET_PRIV (group);
+static void
+tp_group_set_property (GObject      *object,
+                      guint         param_id,
+                      const GValue *value,
+                      GParamSpec   *pspec)
+{
+       EmpathyTpGroupPriv *priv = GET_PRIV (object);
+
+       switch (param_id) {
+       case PROP_CHANNEL:
+               priv->channel = g_object_ref (g_value_get_object (value));
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+               break;
+       };
+}
 
-       if (!tp_chan_iface_group_get_local_pending_members_with_info (priv->group_iface,
-                                                                     &array,
-                                                                     &error)) {
-               empathy_debug (DEBUG_DOMAIN, 
-                             "GetLocalPendingMembersWithInfo failed: %s",
-                             error ? error->message : "No error given");
-               g_clear_error (&error);
+static void
+empathy_tp_group_class_init (EmpathyTpGroupClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-               return NULL;
-       }
+       object_class->finalize = tp_group_finalize;
+       object_class->constructed = tp_group_constructed;
+       object_class->get_property = tp_group_get_property;
+       object_class->set_property = tp_group_set_property;
+
+       g_object_class_install_property (object_class,
+                                        PROP_CHANNEL,
+                                        g_param_spec_object ("channel",
+                                                             "telepathy channel",
+                                                             "The channel for the group",
+                                                             TP_TYPE_CHANNEL,
+                                                             G_PARAM_READWRITE |
+                                                             G_PARAM_CONSTRUCT_ONLY));
+       g_object_class_install_property (object_class,
+                                        PROP_READY,
+                                        g_param_spec_boolean ("ready",
+                                                              "Is the object ready",
+                                                              "This object can't be used until this becomes true",
+                                                              FALSE,
+                                                              G_PARAM_READABLE));
+
+       signals[MEMBER_ADDED] =
+               g_signal_new ("member-added",
+                             G_TYPE_FROM_CLASS (klass),
+                             G_SIGNAL_RUN_LAST,
+                             0,
+                             NULL, NULL,
+                             _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
+                             G_TYPE_NONE,
+                             4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
 
-       if (!array) {
-               /* This happens with butterfly because
-                * GetLocalPendingMembersWithInfo is not 
-                * implemented */
-               return NULL;
-       }
+       signals[MEMBER_REMOVED] =
+               g_signal_new ("member-removed",
+                             G_TYPE_FROM_CLASS (klass),
+                             G_SIGNAL_RUN_LAST,
+                             0,
+                             NULL, NULL,
+                             _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
+                             G_TYPE_NONE,
+                             4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
 
-       for (i = 0; array->len > i; i++) {
-               GValueArray        *pending_struct;
-               EmpathyTpGroupInfo *info;
-               const gchar        *message;
+       signals[LOCAL_PENDING] =
+               g_signal_new ("local-pending",
+                             G_TYPE_FROM_CLASS (klass),
+                             G_SIGNAL_RUN_LAST,
+                             0,
+                             NULL, NULL,
+                             _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
+                             G_TYPE_NONE,
+                             4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
 
-               info = g_slice_new (EmpathyTpGroupInfo);
+       signals[REMOTE_PENDING] =
+               g_signal_new ("remote-pending",
+                             G_TYPE_FROM_CLASS (klass),
+                             G_SIGNAL_RUN_LAST,
+                             0,
+                             NULL, NULL,
+                             _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
+                             G_TYPE_NONE,
+                             4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
 
-               pending_struct = g_ptr_array_index (array, i);
-               info->member = g_value_get_uint (g_value_array_get_nth (pending_struct, 0));
-               info->actor = g_value_get_uint (g_value_array_get_nth (pending_struct, 1));
-               info->reason = g_value_get_uint (g_value_array_get_nth (pending_struct, 2));
-               message = g_value_get_string (g_value_array_get_nth (pending_struct, 3));
-               info->message = g_strdup (message);
-               g_value_array_free (pending_struct);
+       signals[DESTROY] =
+               g_signal_new ("destroy",
+                             G_TYPE_FROM_CLASS (klass),
+                             G_SIGNAL_RUN_LAST,
+                             0,
+                             NULL, NULL,
+                             g_cclosure_marshal_VOID__VOID,
+                             G_TYPE_NONE,
+                             0);
 
-               infos = g_list_prepend (infos, info);
-       }
-       g_ptr_array_free (array, TRUE);
+       g_type_class_add_private (object_class, sizeof (EmpathyTpGroupPriv));
+}
 
-       return infos;
+static void
+empathy_tp_group_init (EmpathyTpGroup *group)
+{
 }
 
-void
-empathy_tp_group_info_list_free (GList *infos)
+EmpathyTpGroup *
+empathy_tp_group_new (TpChannel *channel)
 {
-       GList *l;
+       g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
 
-       for (l = infos; l; l = l->next) {
-               EmpathyTpGroupInfo *info;
+       return g_object_new (EMPATHY_TYPE_TP_GROUP, 
+                            "channel", channel,
+                            NULL);
+}
 
-               info = l->data;
+static void
+tp_group_async_cb (TpChannel    *channel,
+                  const GError *error,
+                  gpointer      user_data,
+                  GObject      *weak_object)
+{
+       const gchar *msg = user_data;
 
-               g_free (info->message);
-               g_slice_free (EmpathyTpGroupInfo, info);
+       if (error) {
+               DEBUG ("%s: %s", msg, error->message);
        }
-       g_list_free (infos);
 }
 
-
-static void
-tp_group_destroy_cb (DBusGProxy     *proxy,
-                    EmpathyTpGroup *group)
+void
+empathy_tp_group_close (EmpathyTpGroup *group)
 {
-       EmpathyTpGroupPriv *priv;
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
 
-       priv = GET_PRIV (group);
+       g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
+       g_return_if_fail (priv->ready);
 
-       g_object_unref (priv->group_iface);
-       g_object_unref (priv->tp_conn);
-       g_object_unref (priv->tp_chan);
-       priv->group_iface = NULL;
-       priv->tp_chan = NULL;
-       priv->tp_conn = NULL;
+       tp_cli_channel_call_close (priv->channel, -1,
+                                  tp_group_async_cb,
+                                  "Failed to close", NULL,
+                                  G_OBJECT (group));
 }
 
-static void
-tp_group_members_changed_cb (DBusGProxy     *group_iface,
-                            gchar          *message,
-                            GArray         *added,
-                            GArray         *removed,
-                            GArray         *local_pending,
-                            GArray         *remote_pending,
-                            guint           actor,
-                            guint           reason,
-                            EmpathyTpGroup *group)
+static GArray *
+tp_group_get_handles (GList *contacts)
 {
-       EmpathyTpGroupPriv *priv;
+       GArray *handles;
+       guint   length;
+       GList  *l;
 
-       priv = GET_PRIV (group);
+       length = g_list_length (contacts);
+       handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
 
-       /* emit signals */
-       if (added->len > 0) {
-               g_signal_emit (group, signals[MEMBERS_ADDED], 0, 
-                              added, actor, reason, message);
-       }
-       if (removed->len > 0) {
-               g_signal_emit (group, signals[MEMBERS_REMOVED], 0, 
-                              removed, actor, reason, message);
-       }
-       if (local_pending->len > 0) {
-               g_signal_emit (group, signals[LOCAL_PENDING], 0,
-                              local_pending, actor, reason, message);
-       }
-       if (remote_pending->len > 0) {
-               g_signal_emit (group, signals[REMOTE_PENDING], 0,
-                              remote_pending, actor, reason, message);
+       for (l = contacts; l; l = l->next) {
+               guint handle;
+
+               handle = empathy_contact_get_handle (l->data);
+               g_array_append_val (handles, handle);
        }
+
+       return handles;
 }
 
-const gchar *
-empathy_tp_group_get_name (EmpathyTpGroup *group)
+void
+empathy_tp_group_add_members (EmpathyTpGroup *group,
+                             GList          *contacts,
+                             const gchar    *message)
 {
-       TelepathyHandleType  handle_type;
-       guint                channel_handle;
-       GArray              *group_handles;
-       gchar              **group_names;
-       GError              *error = NULL;
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
+       GArray             *handles;
 
-       EmpathyTpGroupPriv *priv;
+       g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
+       g_return_if_fail (contacts != NULL);
+       g_return_if_fail (priv->ready);
+
+       handles = tp_group_get_handles (contacts);
+       tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
+                                                        handles,
+                                                        message,
+                                                        tp_group_async_cb,
+                                                        "Failed to add members", NULL,
+                                                        G_OBJECT (group));
+       g_array_free (handles, TRUE);
+}
 
-       g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
+void
+empathy_tp_group_remove_members (EmpathyTpGroup *group,
+                                GList          *contacts,
+                                const gchar    *message)
+{
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
+       GArray             *handles;
 
-       priv = GET_PRIV (group);
+       g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
+       g_return_if_fail (contacts != NULL);
+       g_return_if_fail (priv->ready);
+
+       handles = tp_group_get_handles (contacts);
+       tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
+                                                           handles,
+                                                           message,
+                                                           tp_group_async_cb,
+                                                           "Failed to remove members", NULL,
+                                                           G_OBJECT (group));
+       g_array_free (handles, TRUE);
+}
 
-       /* Lazy initialisation */
-       if (priv->group_name) {
-               return priv->group_name;
-       }
+void
+empathy_tp_group_add_member (EmpathyTpGroup *group,
+                            EmpathyContact *contact,
+                            const gchar    *message)
+{
+       GList *contacts;
 
-       if (!tp_chan_get_handle (DBUS_G_PROXY (priv->tp_chan),
-                                &handle_type,
-                                &channel_handle,
-                                &error)) {
-               empathy_debug (DEBUG_DOMAIN, 
-                             "Couldn't retreive channel handle for group: %s",
-                             error ? error->message : "No error given");
-               g_clear_error (&error);
-               return NULL;
-       }
+       contacts = g_list_prepend (NULL, contact);
+       empathy_tp_group_add_members (group, contacts, message);
+       g_list_free (contacts);
+}
 
-       group_handles = g_array_new (FALSE, FALSE, sizeof (guint));
-       g_array_append_val (group_handles, channel_handle);
-       if (!tp_conn_inspect_handles (DBUS_G_PROXY (priv->tp_conn),
-                                     handle_type,
-                                     group_handles,
-                                     &group_names,
-                                     &error)) {
-               empathy_debug (DEBUG_DOMAIN, 
-                             "Couldn't get group name: %s",
-                             error ? error->message : "No error given");
-               g_clear_error (&error);
-               g_array_free (group_handles, TRUE);
-               return NULL;
-       }
+void
+empathy_tp_group_remove_member (EmpathyTpGroup *group,
+                               EmpathyContact *contact,
+                               const gchar    *message)
+{
+       GList *contacts;
 
-       priv->group_name = *group_names;
-       g_array_free (group_handles, TRUE);
-       g_free (group_names);
+       contacts = g_list_prepend (NULL, contact);
+       empathy_tp_group_remove_members (group, contacts, message);
+       g_list_free (contacts);
+}
 
-       return priv->group_name;
+GList *
+empathy_tp_group_get_members (EmpathyTpGroup *group)
+{
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
+
+       g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
+
+       g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
+
+       return g_list_copy (priv->members);
 }
 
-guint 
-empathy_tp_group_get_self_handle (EmpathyTpGroup *group)
+GList *
+empathy_tp_group_get_local_pendings (EmpathyTpGroup *group)
 {
-       EmpathyTpGroupPriv *priv;
-       guint               handle;
-       GError             *error = NULL;
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
+       GList              *pendings = NULL, *l;
 
-       g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), );
+       g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
 
-       priv = GET_PRIV (group);
+       for (l = priv->local_pendings; l; l = l->next) {
+               EmpathyPendingInfo *info;
+               EmpathyPendingInfo *new_info;
 
-       if (!tp_chan_iface_group_get_self_handle (priv->group_iface, &handle, &error)) {
-               empathy_debug (DEBUG_DOMAIN, 
-                             "Failed to get self handle: %s",
-                             error ? error->message : "No error given");
-               g_clear_error (&error);
-               return 0;
+               info = l->data;
+               new_info = empathy_pending_info_new (info->member,
+                                                    info->actor,
+                                                    info->message);
+               pendings = g_list_prepend (pendings, new_info);
        }
 
-       return handle;
+       return pendings;
+}
+
+GList *
+empathy_tp_group_get_remote_pendings (EmpathyTpGroup *group)
+{
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
+
+       g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
+
+       g_list_foreach (priv->remote_pendings, (GFunc) g_object_ref, NULL);
+
+       return g_list_copy (priv->remote_pendings);
 }
 
 const gchar *
-empathy_tp_group_get_object_path (EmpathyTpGroup *group)
+empathy_tp_group_get_name (EmpathyTpGroup *group)
 {
-       EmpathyTpGroupPriv *priv;
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
 
        g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
+       g_return_val_if_fail (priv->ready, NULL);
 
-       priv = GET_PRIV (group);
+       return priv->group_name;
+}
+
+EmpathyContact *
+empathy_tp_group_get_self_contact (EmpathyTpGroup *group)
+{
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
 
-       return dbus_g_proxy_get_path (DBUS_G_PROXY (priv->tp_chan));
+       g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
+       g_return_val_if_fail (priv->ready, NULL);
+
+       return tp_group_get_contact (group, priv->self_handle);
 }
 
 gboolean
 empathy_tp_group_is_member (EmpathyTpGroup *group,
-                           guint           handle)
+                           EmpathyContact *contact)
 {
-       GArray   *members;
-       guint     i;
-       gboolean  found = FALSE;
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
 
-       members = empathy_tp_group_get_members (group);
-       for (i = 0; i < members->len; i++) {
-               if (g_array_index (members, guint, i) == handle) {
-                       found = TRUE;
+       g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
+       g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
+
+       return g_list_find (priv->members, contact) != NULL;
+}
+
+gboolean
+empathy_tp_group_is_ready (EmpathyTpGroup *group)
+{
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
+
+       g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
+
+       return priv->ready;
+}
+
+EmpathyPendingInfo *
+empathy_tp_group_get_invitation (EmpathyTpGroup  *group,
+                                EmpathyContact **remote_contact)
+{
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
+       EmpathyContact     *contact = NULL;
+       EmpathyPendingInfo *invitation = NULL;
+       GList              *l;
+
+       g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
+       g_return_val_if_fail (priv->ready, NULL);
+
+       for (l = priv->local_pendings; l; l = l->next) {
+               EmpathyPendingInfo *info = l->data;
+
+               if (empathy_contact_is_user (info->member)) {
+                       invitation = info;
                        break;
                }
        }
-       g_array_free (members, TRUE);
-       
-       return found;
+
+       if (invitation && priv->members && !priv->members->next) {
+               contact = priv->members->data;
+       }
+       if (!invitation && priv->remote_pendings && !priv->remote_pendings->next) {
+               contact = priv->remote_pendings->data;
+       }
+
+       if (remote_contact && contact) {
+               *remote_contact = g_object_ref (contact);
+       }
+
+       return invitation;
 }