]> 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 f6f8004cce00a988246ad13fab83e351371a625f..62212aaadccd666f13af5387c6ca920f94da3477 100644 (file)
@@ -1,54 +1,52 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
  * Copyright (C) 2006 Xavier Claessens <xclaesse@gmail.com>
- * Copyright (C) 2007 Collabora Ltd.
+ * 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-gen.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-tp-group.h"
 #include "empathy-contact-factory.h"
-#include "empathy-debug.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 {
+       TpChannel             *channel;
+       gboolean               ready;
+
        EmpathyContactFactory *factory;
        McAccount             *account;
-       DBusGProxy            *group_iface;
-       TpChan                *tp_chan;
        gchar                 *group_name;
        guint                  self_handle;
-
        GList                 *members;
        GList                 *local_pendings;
        GList                 *remote_pendings;
@@ -66,6 +64,12 @@ enum {
        LAST_SIGNAL
 };
 
+enum {
+       PROP_0,
+       PROP_CHANNEL,
+       PROP_READY
+};
+
 static guint signals[LAST_SIGNAL];
 
 G_DEFINE_TYPE (EmpathyTpGroup, empathy_tp_group, G_TYPE_OBJECT);
@@ -83,7 +87,7 @@ tp_group_get_contact (EmpathyTpGroup *group,
                                                                   handle);
        }
 
-       if (contact && empathy_contact_get_handle (contact) == priv->self_handle) {
+       if (contact && handle == priv->self_handle) {
                empathy_contact_set_is_user (contact, TRUE);
        }
 
@@ -92,7 +96,7 @@ tp_group_get_contact (EmpathyTpGroup *group,
 
 static GList *
 tp_group_get_contacts (EmpathyTpGroup *group,
-                      GArray         *handles)
+                      const GArray   *handles)
 {
        EmpathyTpGroupPriv *priv = GET_PRIV (group);
        GList              *contacts,  *l;
@@ -191,15 +195,14 @@ tp_group_remove_from_pendings (EmpathyTpGroup *group,
 }
 
 static void
-tp_group_members_changed_cb (DBusGProxy     *group_iface,
-                            const gchar    *message,
-                            GArray         *added,
-                            GArray         *removed,
-                            GArray         *local_pending,
-                            GArray         *remote_pending,
-                            guint           actor,
-                            guint           reason,
-                            EmpathyTpGroup *group)
+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;
@@ -207,18 +210,17 @@ tp_group_members_changed_cb (DBusGProxy     *group_iface,
 
        actor_contact = tp_group_get_contact (group, actor);
 
-       empathy_debug (DEBUG_DOMAIN, "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",
-                      empathy_tp_group_get_name (group),
-                      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));
+       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);
@@ -290,117 +292,88 @@ tp_group_members_changed_cb (DBusGProxy     *group_iface,
                g_object_unref (actor_contact);
        }
 
-       empathy_debug (DEBUG_DOMAIN, "Members changed done for list %s:\n"
-                                    "  members-len=%d\n"
-                                    "  local-pendings-len=%d\n"
-                                    "  remote-pendings-len=%d",
-                      empathy_tp_group_get_name (group),
-                      g_list_length (priv->members),
-                      g_list_length (priv->local_pendings),
-                      g_list_length (priv->remote_pendings));
-}
-
-static void
-tp_group_destroy_cb (TpChan         *tp_chan,
-                    EmpathyTpGroup *group)
-{
-       EmpathyTpGroupPriv *priv = GET_PRIV (group);
-
-       empathy_debug (DEBUG_DOMAIN, "Account disconnected or CM crashed");
-
-       g_object_unref (priv->tp_chan);
-       priv->group_iface = NULL;
-       priv->tp_chan = NULL;
-
-       g_signal_emit (group, signals[DESTROY], 0);
+       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));
 }
 
-static void tp_group_closed_cb (DBusGProxy     *proxy,
-                               EmpathyTpGroup *group);
-
 static void
-tp_group_disconnect (EmpathyTpGroup *group)
+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 = GET_PRIV (group);
 
-       if (priv->group_iface) {
-               dbus_g_proxy_disconnect_signal (priv->group_iface, "MembersChanged",
-                                               G_CALLBACK (tp_group_members_changed_cb),
-                                               group);
-       }
-       if (priv->tp_chan) {
-               g_signal_handlers_disconnect_by_func (priv->tp_chan,
-                                                     tp_group_destroy_cb,
-                                                     group);
-               dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->tp_chan), "Closed",
-                                               G_CALLBACK (tp_group_closed_cb),
-                                               group);
+       if (priv->ready) {
+               tp_group_update_members (EMPATHY_TP_GROUP (group), message,
+                                        added, removed,
+                                        local_pending, remote_pending,
+                                        actor, reason);
        }
 }
 
 static void
-tp_group_closed_cb (DBusGProxy     *proxy,
-                   EmpathyTpGroup *group)
-{
-       tp_group_disconnect (group);
-       tp_group_destroy_cb (TELEPATHY_CHAN (proxy), group);
-}
-
-static void
-tp_group_get_members_cb (DBusGProxy *proxy,
-                        GArray     *handles,
-                        GError     *error,
-                        gpointer    user_data)
+tp_group_get_members_cb (TpChannel    *channel,
+                        const GArray *handles,
+                        const GError *error,
+                        gpointer      user_data,
+                        GObject      *group)
 {
-       EmpathyTpGroup     *group = user_data;
        EmpathyTpGroupPriv *priv = GET_PRIV (group);
 
        if (error) {
-               empathy_debug (DEBUG_DOMAIN, "Failed to get members: %s",
-                              error->message);
-               g_object_unref (group);
+               DEBUG ("Failed to get members: %s", error->message);
                return;
        }
 
-       tp_group_members_changed_cb (priv->group_iface,
-                                    NULL,    /* message */
-                                    handles, /* added */
-                                    NULL,    /* removed */
-                                    NULL,    /* local_pending */
-                                    NULL,    /* remote_pending */
-                                    0,       /* actor */
-                                    0,       /* reason */
-                                    group);
-
-       g_array_free (handles, TRUE);
-       g_object_unref (group);
+       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");
 }
 
 static void
-tp_group_get_local_pending_cb (DBusGProxy *proxy,
-                              GPtrArray  *array,
-                              GError     *error,
-                              gpointer    user_data)
+tp_group_get_local_pending_cb (TpChannel        *channel,
+                              const GPtrArray  *array,
+                              const GError     *error,
+                              gpointer          user_data,
+                              GObject          *group)
 {
-       EmpathyTpGroup     *group = user_data;
-       EmpathyTpGroupPriv *priv = GET_PRIV (group);
-       GArray             *handles;
-       guint               i;
+       GArray *handles;
+       guint   i = 0;
        
        if (error) {
-               empathy_debug (DEBUG_DOMAIN, "Failed to get local pendings: %s",
-                              error->message);
-               g_object_unref (group);
+               DEBUG ("Failed to get local pendings: %s", error->message);
                return;
        }
 
        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;
+               GValueArray *pending_struct;
+               const gchar *message;
+               guint        member_handle;
+               guint        actor_handle;
+               guint        reason;
 
                pending_struct = g_ptr_array_index (array, i);
                member_handle = g_value_get_uint (g_value_array_get_nth (pending_struct, 0));
@@ -408,65 +381,174 @@ tp_group_get_local_pending_cb (DBusGProxy *proxy,
                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_insert_val (handles, 0, member_handle);
-               tp_group_members_changed_cb (priv->group_iface,
-                                            message,      /* message */
-                                            NULL,         /* added */
-                                            NULL,         /* removed */
-                                            handles,      /* local_pending */
-                                            NULL,         /* remote_pending */
-                                            actor_handle, /* actor */
-                                            reason,       /* reason */
-                                            group);
+               g_array_index (handles, guint, 0) = member_handle;
 
-               g_value_array_free (pending_struct);
+               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_ptr_array_free (array, TRUE);
        g_array_free (handles, TRUE);
-       g_object_unref (group);
 }
 
 static void
-tp_group_get_remote_pending_cb (DBusGProxy *proxy,
-                               GArray     *handles,
-                               GError     *error,
-                               gpointer    user_data)
+tp_group_get_remote_pending_cb (TpChannel    *channel,
+                               const GArray *handles,
+                               const GError *error,
+                               gpointer      user_data,
+                               GObject      *group)
+{
+       if (error) {
+               DEBUG ("Failed to get remote pendings: %s", error->message);
+               return;
+       }
+
+       tp_group_update_members (EMPATHY_TP_GROUP (group),
+                                NULL,    /* message */
+                                NULL,    /* added */
+                                NULL,    /* removed */
+                                NULL,    /* local_pending */
+                                handles, /* remote_pending */
+                                0,       /* actor */
+                                0);      /* reason */
+}
+
+static void
+tp_group_inspect_handles_cb (TpConnection  *connection,
+                            const gchar  **names,
+                            const GError  *error,
+                            gpointer       user_data,
+                            GObject       *group)
 {
-       EmpathyTpGroup     *group = user_data;
        EmpathyTpGroupPriv *priv = GET_PRIV (group);
-       
+
        if (error) {
-               empathy_debug (DEBUG_DOMAIN, "Failed to get remote pendings: %s",
-                              error->message);
-               g_object_unref (group);
+               DEBUG ("Failed to inspect channel handle: %s", error->message);
                return;
        }
 
-       tp_group_members_changed_cb (priv->group_iface,
-                                    NULL,    /* message */
-                                    NULL,    /* added */
-                                    NULL,    /* removed */
-                                    NULL,    /* local_pending */
-                                    handles, /* remote_pending */
-                                    0,       /* actor */
-                                    0,       /* reason */
-                                    group);
+       priv->group_name = g_strdup (*names);
+}
 
+static void
+tp_group_invalidated_cb (TpProxy        *proxy,
+                        guint           domain,
+                        gint            code,
+                        gchar          *message,
+                        EmpathyTpGroup *group)
+{
+       DEBUG ("Channel invalidated: %s", message);
+       g_signal_emit (group, signals[DESTROY], 0);
+}
+
+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;
+       }
+
+       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);
-       g_object_unref (group);
+
+       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);
+}
+
+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));
+}
+
+static void
+tp_group_channel_ready_cb (EmpathyTpGroup *group)
+{
+       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);
+       }
 }
 
 static void
 tp_group_finalize (GObject *object)
 {
-       EmpathyTpGroupPriv *priv = GET_PRIV (object);
+       EmpathyTpGroupPriv      *priv = GET_PRIV (object);
+       EmpathyTpContactFactory *tp_factory;
 
-       empathy_debug (DEBUG_DOMAIN, "finalize: %p");
+       DEBUG ("finalize: %p", object);
 
-       tp_group_disconnect (EMPATHY_TP_GROUP (object));
+       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->tp_chan) {
-               g_object_unref (priv->tp_chan);
+       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);
@@ -488,12 +570,93 @@ tp_group_finalize (GObject *object)
        G_OBJECT_CLASS (empathy_tp_group_parent_class)->finalize (object);
 }
 
+static void
+tp_group_constructed (GObject *group)
+{
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
+       gboolean            channel_ready;
+
+       priv->factory = empathy_contact_factory_new ();
+       priv->account = empathy_channel_get_account (priv->channel);
+
+       g_signal_connect (priv->channel, "invalidated",
+                         G_CALLBACK (tp_group_invalidated_cb),
+                         group);
+
+       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);
+       }
+}
+
+static void
+tp_group_get_property (GObject    *object,
+                      guint       param_id,
+                      GValue     *value,
+                      GParamSpec *pspec)
+{
+       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;
+       };
+}
+
+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;
+       };
+}
+
 static void
 empathy_tp_group_class_init (EmpathyTpGroupClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
        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",
@@ -501,7 +664,7 @@ empathy_tp_group_class_init (EmpathyTpGroupClass *klass)
                              G_SIGNAL_RUN_LAST,
                              0,
                              NULL, NULL,
-                             empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
+                             _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
                              G_TYPE_NONE,
                              4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
 
@@ -511,7 +674,7 @@ empathy_tp_group_class_init (EmpathyTpGroupClass *klass)
                              G_SIGNAL_RUN_LAST,
                              0,
                              NULL, NULL,
-                             empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
+                             _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
                              G_TYPE_NONE,
                              4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
 
@@ -521,7 +684,7 @@ empathy_tp_group_class_init (EmpathyTpGroupClass *klass)
                              G_SIGNAL_RUN_LAST,
                              0,
                              NULL, NULL,
-                             empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
+                             _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
                              G_TYPE_NONE,
                              4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
 
@@ -531,7 +694,7 @@ empathy_tp_group_class_init (EmpathyTpGroupClass *klass)
                              G_SIGNAL_RUN_LAST,
                              0,
                              NULL, NULL,
-                             empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
+                             _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
                              G_TYPE_NONE,
                              4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
 
@@ -554,79 +717,40 @@ empathy_tp_group_init (EmpathyTpGroup *group)
 }
 
 EmpathyTpGroup *
-empathy_tp_group_new (McAccount *account,
-                     TpChan    *tp_chan)
+empathy_tp_group_new (TpChannel *channel)
 {
-       EmpathyTpGroup     *group;
-       EmpathyTpGroupPriv *priv;
-       DBusGProxy         *group_iface;
-       GError             *error;
-
-       g_return_val_if_fail (TELEPATHY_IS_CHAN (tp_chan), NULL);
-       g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
-
-       group_iface = tp_chan_get_interface (tp_chan,
-                                            TELEPATHY_CHAN_IFACE_GROUP_QUARK);
-       g_return_val_if_fail (group_iface != NULL, NULL);
-
-       group = g_object_new (EMPATHY_TYPE_TP_GROUP, NULL);
-       priv = GET_PRIV (group);
-
-       priv->account = g_object_ref (account);
-       priv->tp_chan = g_object_ref (tp_chan);
-       priv->group_iface = group_iface;
-       priv->factory = empathy_contact_factory_new ();
+       g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
 
-       if (!tp_chan_iface_group_get_self_handle (priv->group_iface,
-                                                 &priv->self_handle,
-                                                 &error)) {
-               empathy_debug (DEBUG_DOMAIN, 
-                             "Failed to get self handle: %s",
-                             error ? error->message : "No error given");
-               g_clear_error (&error);
-       }
-
-       dbus_g_proxy_connect_signal (priv->group_iface, "MembersChanged",
-                                    G_CALLBACK (tp_group_members_changed_cb),
-                                    group, NULL);
-       dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_chan), "Closed",
-                                    G_CALLBACK (tp_group_closed_cb),
-                                    group, NULL);
-       g_signal_connect (priv->tp_chan, "destroy",
-                         G_CALLBACK (tp_group_destroy_cb),
-                         group);
-
-       tp_chan_iface_group_get_members_async (priv->group_iface,
-                                              tp_group_get_members_cb,
-                                              g_object_ref (group));
-       tp_chan_iface_group_get_local_pending_members_with_info_async (priv->group_iface,
-                                                                      tp_group_get_local_pending_cb,
-                                                                      g_object_ref (group));
-       tp_chan_iface_group_get_remote_pending_members_async (priv->group_iface,
-                                                             tp_group_get_remote_pending_cb,
-                                                             g_object_ref (group));
-
-       return group;
+       return g_object_new (EMPATHY_TYPE_TP_GROUP, 
+                            "channel", channel,
+                            NULL);
 }
 
 static void
-tp_group_async_cb (DBusGProxy *proxy, GError *error, gpointer user_data)
+tp_group_async_cb (TpChannel    *channel,
+                  const GError *error,
+                  gpointer      user_data,
+                  GObject      *weak_object)
 {
        const gchar *msg = user_data;
 
        if (error) {
-               empathy_debug (DEBUG_DOMAIN, "%s: %s", msg, error->message);
+               DEBUG ("%s: %s", msg, error->message);
        }
 }
 
 void
-empathy_tp_group_close  (EmpathyTpGroup *group)
+empathy_tp_group_close (EmpathyTpGroup *group)
 {
        EmpathyTpGroupPriv *priv = GET_PRIV (group);
 
-       tp_chan_close_async (DBUS_G_PROXY (priv->tp_chan),
-                            tp_group_async_cb,
-                            "Failed to close");
+       g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
+       g_return_if_fail (priv->ready);
+
+       tp_cli_channel_call_close (priv->channel, -1,
+                                  tp_group_async_cb,
+                                  "Failed to close", NULL,
+                                  G_OBJECT (group));
 }
 
 static GArray *
@@ -659,36 +783,15 @@ empathy_tp_group_add_members (EmpathyTpGroup *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_chan_iface_group_add_members_async (priv->group_iface,
-                                              handles,
-                                              message,
-                                              tp_group_async_cb,
-                                              "Failed to add members");
-
-       g_array_free (handles, TRUE);
-}
-
-void
-empathy_tp_group_add_member (EmpathyTpGroup *group,
-                            EmpathyContact *contact,
-                            const gchar    *message)
-{
-       EmpathyTpGroupPriv *priv = GET_PRIV (group);
-       GArray             *handles;
-       guint               handle;
-
-       handle = empathy_contact_get_handle (contact);
-       handles = g_array_new (FALSE, FALSE, sizeof (guint));
-       g_array_append_val (handles, handle);
-
-       tp_chan_iface_group_add_members_async (priv->group_iface,
-                                              handles,
-                                              message,
-                                              tp_group_async_cb,
-                                              "Failed to add member");
-
+       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);
 }
 
@@ -702,37 +805,40 @@ empathy_tp_group_remove_members (EmpathyTpGroup *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_chan_iface_group_remove_members_async (priv->group_iface,
-                                                 handles,
-                                                 message,
-                                                 tp_group_async_cb,
-                                                 "Failed to remove members");
-
+       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);
 }
 
+void
+empathy_tp_group_add_member (EmpathyTpGroup *group,
+                            EmpathyContact *contact,
+                            const gchar    *message)
+{
+       GList *contacts;
+
+       contacts = g_list_prepend (NULL, contact);
+       empathy_tp_group_add_members (group, contacts, message);
+       g_list_free (contacts);
+}
+
 void
 empathy_tp_group_remove_member (EmpathyTpGroup *group,
                                EmpathyContact *contact,
                                const gchar    *message)
 {
-       EmpathyTpGroupPriv *priv = GET_PRIV (group);
-       GArray             *handles;
-       guint               handle;
-
-       handle = empathy_contact_get_handle (contact);
-       handles = g_array_new (FALSE, FALSE, sizeof (guint));
-       g_array_append_val (handles, handle);
-
-       tp_chan_iface_group_remove_members_async (priv->group_iface,
-                                                 handles,
-                                                 message,
-                                                 tp_group_async_cb,
-                                                 "Failed to remove member");
+       GList *contacts;
 
-       g_array_free (handles, TRUE);
+       contacts = g_list_prepend (NULL, contact);
+       empathy_tp_group_remove_members (group, contacts, message);
+       g_list_free (contacts);
 }
 
 GList *
@@ -740,6 +846,8 @@ 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);
@@ -751,6 +859,8 @@ empathy_tp_group_get_local_pendings (EmpathyTpGroup *group)
        EmpathyTpGroupPriv *priv = GET_PRIV (group);
        GList              *pendings = NULL, *l;
 
+       g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
+
        for (l = priv->local_pendings; l; l = l->next) {
                EmpathyPendingInfo *info;
                EmpathyPendingInfo *new_info;
@@ -770,6 +880,8 @@ 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);
@@ -778,16 +890,10 @@ empathy_tp_group_get_remote_pendings (EmpathyTpGroup *group)
 const gchar *
 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);
-
-       priv = GET_PRIV (group);
-
-       /* Lazy initialisation */
-       if (!priv->group_name && priv->tp_chan->handle != 0) {
-               priv->group_name = empathy_inspect_channel (priv->account, priv->tp_chan);
-       }
+       g_return_val_if_fail (priv->ready, NULL);
 
        return priv->group_name;
 }
@@ -798,28 +904,65 @@ empathy_tp_group_get_self_contact (EmpathyTpGroup *group)
        EmpathyTpGroupPriv *priv = GET_PRIV (group);
 
        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);
 }
 
-const gchar *
-empathy_tp_group_get_object_path (EmpathyTpGroup *group)
+gboolean
+empathy_tp_group_is_member (EmpathyTpGroup *group,
+                           EmpathyContact *contact)
 {
-       EmpathyTpGroupPriv *priv;
-
-       g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
+       EmpathyTpGroupPriv *priv = GET_PRIV (group);
 
-       priv = GET_PRIV (group);
+       g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
+       g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
 
-       return dbus_g_proxy_get_path (DBUS_G_PROXY (priv->tp_chan));
+       return g_list_find (priv->members, contact) != NULL;
 }
 
 gboolean
-empathy_tp_group_is_member (EmpathyTpGroup *group,
-                           EmpathyContact *contact)
+empathy_tp_group_is_ready (EmpathyTpGroup *group)
 {
        EmpathyTpGroupPriv *priv = GET_PRIV (group);
 
-       return g_list_find (priv->members, contact) != NULL;
+       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;
+               }
+       }
+
+       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;
 }