]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-tp-file.c
Merge branch 'ft_rework'
[empathy.git] / libempathy / empathy-tp-file.c
index 61f4e26da1ba7c71e7818fc84d5c55b8f834b78f..3a58268680fa77fa69c33cbfa5fdd1a85503a418 100644 (file)
@@ -1,7 +1,5 @@
-/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
- * Copyright (C) 2007 Collabora Ltd.
- *   @author: Xavier Claessens <xclaesse@gmail.com>
+ * Copyright (C) 2007-2009 Collabora Ltd.
  * Copyright (C) 2007 Marco Barisione <marco@barisione.org>
  *
  * This library is free software; you can redistribute it and/or
  * 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: Marco Barisione <marco@barisione.org>
+ *          Jonny Lamb <jonny.lamb@collabora.co.uk>
+ *          Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
  */
 
 #include <config.h>
 
 #include <string.h>
 #include <unistd.h>
+#include <errno.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/un.h>
 
-#include <glib/gi18n.h>
+#include <glib/gi18n-lib.h>
 
 #include <gio/gio.h>
 #include <gio/gunixinputstream.h>
 #include <gio/gunixoutputstream.h>
 
-#include <libtelepathy/tp-conn.h>
-#include <libtelepathy/tp-helpers.h>
-#include <libtelepathy/tp-props-iface.h>
-
 #include <telepathy-glib/proxy-subclass.h>
+#include <telepathy-glib/util.h>
 
 #include "empathy-tp-file.h"
-#include "empathy-contact-factory.h"
 #include "empathy-marshal.h"
 #include "empathy-time.h"
 #include "empathy-utils.h"
 
 /**
  * SECTION:empathy-tp-file
- * @short_description: File channel
- * @see_also: #EmpathyTpFile, #EmpathyContact, empathy_send_file()
- * @include: libempthy/empathy-tp-file.h
- *
- * The #EmpathyTpFile object represents a Telepathy file channel.
- */
-
-/**
- * EMPATHY_TP_FILE_UNKNOWN_SIZE:
+ * @title: EmpathyTpFile
+ * @short_description: Object which represents a Telepathy file channel
+ * @include: libempathy/empathy-tp-file.h
  *
- * Value used for the "size" or "estimated-size" properties when the size of
- * the transferred file is unknown.
+ * #EmpathyTpFile is an object which represents a Telepathy file channel.
+ * Usually, clients do not need to deal with #EmpathyTpFile objects directly,
+ * and are supposed to use #EmpathyFTHandler and #EmpathyFTFactory for
+ * transferring files using libempathy.
  */
 
-static void empathy_tp_file_class_init (EmpathyTpFileClass *klass);
-static void empathy_tp_file_init (EmpathyTpFile *tp_file);
-static void tp_file_finalize (GObject *object);
-static GObject *tp_file_constructor (GType type, guint n_props,
-    GObjectConstructParam *props);
-static void tp_file_get_property (GObject *object, guint param_id,
-    GValue *value, GParamSpec *pspec);
-static void tp_file_set_property (GObject *object, guint param_id,
-    const GValue *value, GParamSpec *pspec);
-static void tp_file_destroy_cb (TpChannel *file_chan, EmpathyTpFile *tp_file);
-static void tp_file_closed_cb (TpChannel *file_chan, EmpathyTpFile *tp_file,
-    GObject *weak_object);
-static void tp_file_state_changed_cb (DBusGProxy *file_iface, guint state,
-    guint reason, EmpathyTpFile *tp_file);
-static void tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
-    guint64 count, EmpathyTpFile *tp_file, GObject *weak_object);
-static void copy_stream (GInputStream *in, GOutputStream *out,
-    GCancellable *cancellable);
-
 /* EmpathyTpFile object */
 
-#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
-           EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv))
-
-typedef struct _EmpathyTpFilePriv  EmpathyTpFilePriv;
-
-struct _EmpathyTpFilePriv {
-  EmpathyContactFactory *factory;
-  McAccount *account;
-  gchar *id;
-  MissionControl *mc;
+typedef struct {
   TpChannel *channel;
+  gboolean ready;
 
-  EmpathyTpFile *cached_empathy_file;
-  EmpathyContact *contact;
   GInputStream *in_stream;
   GOutputStream *out_stream;
+
+  /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
+  TpFileTransferState state;
+  TpFileTransferStateChangeReason state_change_reason;
+
+  /* transfer properties */
   gboolean incoming;
-  gchar *filename;
-  EmpFileTransferState state;
-  EmpFileTransferStateChangeReason state_change_reason;
-  guint64 size;
-  guint64 transferred_bytes;
-  gint64 start_time;
-  gchar *unix_socket_path;
-  gchar *content_md5;
-  gchar *content_type;
-  gchar *description;
+  time_t start_time;
+  GArray *unix_socket_path;
+  guint64 offset;
+
+  /* GCancellable we're passed when offering/accepting the transfer */
   GCancellable *cancellable;
-};
+
+  /* callbacks for the operation */
+  EmpathyTpFileProgressCallback progress_callback;
+  gpointer progress_user_data;
+  EmpathyTpFileOperationCallback op_callback;
+  gpointer op_user_data;
+
+  gboolean is_closed;
+
+  gboolean dispose_run;
+} EmpathyTpFilePriv;
 
 enum {
   PROP_0,
-  PROP_ACCOUNT,
   PROP_CHANNEL,
-  PROP_STATE,
-  PROP_INCOMING,
-  PROP_FILENAME,
-  PROP_SIZE,
-  PROP_CONTENT_TYPE,
-  PROP_TRANSFERRED_BYTES,
-  PROP_CONTENT_MD5,
-  PROP_IN_STREAM,
+  PROP_INCOMING
 };
 
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpFile)
+
 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
 
+/* private functions */
+
 static void
-empathy_tp_file_class_init (EmpathyTpFileClass *klass)
+tp_file_get_state_cb (TpProxy *proxy,
+    const GValue *value,
+    const GError *error,
+    gpointer user_data,
+    GObject *weak_object)
 {
-  GObjectClass *object_class = G_OBJECT_CLASS (klass);
-
-  object_class->finalize = tp_file_finalize;
-  object_class->constructor = tp_file_constructor;
-  object_class->get_property = tp_file_get_property;
-  object_class->set_property = tp_file_set_property;
-
-  /* Construct-only properties */
-  g_object_class_install_property (object_class,
-      PROP_ACCOUNT,
-      g_param_spec_object ("account",
-          "channel Account",
-          "The account associated with the channel",
-          MC_TYPE_ACCOUNT,
-          G_PARAM_READWRITE |
-          G_PARAM_CONSTRUCT_ONLY));
+  EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
 
-  g_object_class_install_property (object_class,
-      PROP_CHANNEL,
-      g_param_spec_object ("channel",
-          "telepathy channel",
-          "The file transfer channel",
-          TP_TYPE_CHANNEL,
-          G_PARAM_READWRITE |
-          G_PARAM_CONSTRUCT_ONLY));
+  if (error != NULL)
+    {
+      /* set a default value for the state */
+      priv->state = TP_FILE_TRANSFER_STATE_NONE;
+      return;
+    }
 
-  g_object_class_install_property (object_class,
-      PROP_STATE,
-      g_param_spec_uint ("state",
-          "state of the transfer",
-          "The file transfer state",
-          0,
-          G_MAXUINT,
-          G_MAXUINT,
-          G_PARAM_READWRITE |
-          G_PARAM_CONSTRUCT));
+  priv->state = g_value_get_uint (value);
+}
 
-  g_object_class_install_property (object_class,
-      PROP_INCOMING,
-      g_param_spec_boolean ("incoming",
-          "incoming",
-          "Whether the transfer is incoming",
-          FALSE,
-          G_PARAM_READWRITE |
-          G_PARAM_CONSTRUCT));
+static void
+tp_file_invalidated_cb (TpProxy       *proxy,
+    guint          domain,
+    gint           code,
+    gchar         *message,
+    EmpathyTpFile *tp_file)
+{
+  EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
 
-  g_object_class_install_property (object_class,
-      PROP_FILENAME,
-      g_param_spec_string ("filename",
-          "name of the transfer",
-          "The file transfer filename",
-          "",
-          G_PARAM_READWRITE));
+  DEBUG ("Channel invalidated: %s", message);
 
-  g_object_class_install_property (object_class,
-      PROP_SIZE,
-      g_param_spec_uint64 ("size",
-          "size of the file",
-          "The file transfer size",
-          0,
-          G_MAXUINT64,
-          G_MAXUINT64,
-          G_PARAM_READWRITE));
+  if (priv->state != TP_FILE_TRANSFER_STATE_COMPLETED &&
+      priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
+    {
+      /* The channel is not in a finished state, an error occured */
+      priv->state = TP_FILE_TRANSFER_STATE_CANCELLED;
+      priv->state_change_reason =
+          TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR;
+    }
+}
 
-  g_object_class_install_property (object_class,
-      PROP_CONTENT_TYPE,
-      g_param_spec_string ("content-type",
-          "file transfer content-type",
-          "The file transfer content-type",
-          "",
-          G_PARAM_READWRITE));
+static void
+ft_operation_close_clean (EmpathyTpFile *tp_file)
+{
+  EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
 
-  g_object_class_install_property (object_class,
-      PROP_CONTENT_MD5,
-      g_param_spec_string ("content-md5",
-          "file transfer md5sum",
-          "The md5 sum of the transfer's contents",
-          "",
-          G_PARAM_READWRITE));
+  if (priv->is_closed)
+    return;
 
-  g_object_class_install_property (object_class,
-      PROP_TRANSFERRED_BYTES,
-      g_param_spec_uint64 ("transferred-bytes",
-          "bytes transferred",
-          "The number of bytes transferred",
-          0,
-          G_MAXUINT64,
-          0,
-          G_PARAM_READWRITE));
+  DEBUG ("FT operation close clean");
 
-  g_object_class_install_property (object_class,
-      PROP_IN_STREAM,
-      g_param_spec_object ("in-stream",
-          "transfer input stream",
-          "The input stream for file transfer",
-          G_TYPE_INPUT_STREAM,
-          G_PARAM_READWRITE));
+  priv->is_closed = TRUE;
 
-  g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
+  if (priv->op_callback != NULL)
+    priv->op_callback (tp_file, NULL, priv->op_user_data);
 }
 
 static void
-empathy_tp_file_init (EmpathyTpFile *tp_file)
+ft_operation_close_with_error (EmpathyTpFile *tp_file,
+    GError *error)
 {
-}
+  EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
 
-static void
-tp_file_finalize (GObject *object)
-{
-  EmpathyTpFilePriv *priv;
-  EmpathyTpFile *tp_file;
+  if (priv->is_closed)
+    return;
 
-  tp_file = EMPATHY_TP_FILE (object);
-  priv = GET_PRIV (tp_file);
+  DEBUG ("FT operation close with error %s", error->message);
 
-  if (priv->channel)
-    {
-      DEBUG ("Closing channel..");
-      g_signal_handlers_disconnect_by_func (priv->channel,
-          tp_file_destroy_cb, object);
-      tp_cli_channel_run_close (priv->channel, -1, NULL, NULL);
-      if (G_IS_OBJECT (priv->channel))
-        g_object_unref (priv->channel);
-    }
+  priv->is_closed = TRUE;
 
-  if (priv->factory)
-    {
-      g_object_unref (priv->factory);
-    }
-  if (priv->account)
-    {
-      g_object_unref (priv->account);
-    }
-  if (priv->mc)
-    {
-      g_object_unref (priv->mc);
-    }
+  /* close the channel if it's not cancelled already */
+  if (priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
+    empathy_tp_file_cancel (tp_file);
 
-  g_free (priv->id);
-  g_free (priv->filename);
-  g_free (priv->unix_socket_path);
-  g_free (priv->description);
-  g_free (priv->content_md5);
-  g_free (priv->content_type);
+  if (priv->op_callback != NULL)
+    priv->op_callback (tp_file, error, priv->op_user_data);
+}
 
-  if (priv->in_stream)
-    g_object_unref (priv->in_stream);
+static void
+splice_stream_ready_cb (GObject *source,
+    GAsyncResult *res,
+    gpointer user_data)
+{
+  EmpathyTpFile *tp_file;
+  GError *error = NULL;
 
-  if (priv->out_stream)
-    g_object_unref (priv->out_stream);
+  tp_file = user_data;
 
-  if (priv->contact)
-    g_object_unref (priv->contact);
+  g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error);
 
-  if (priv->cancellable)
-    g_object_unref (priv->cancellable);
+  DEBUG ("Splice stream ready cb, error %p", error);
 
-  G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
+  if (error != NULL)
+    {
+      ft_operation_close_with_error (tp_file, error);
+      g_clear_error (&error);
+      return;
+    }
 }
 
-static GObject *
-tp_file_constructor (GType type,
-                     guint n_props,
-                     GObjectConstructParam *props)
+static void
+tp_file_start_transfer (EmpathyTpFile *tp_file)
 {
-  GObject *tp_file;
-  EmpathyTpFilePriv *priv;
+  gint fd;
+  struct sockaddr_un addr;
   GError *error = NULL;
-  GHashTable *properties;
-  TpHandle handle;
+  EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
 
-  tp_file = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
-      n_props, props);
-
-  priv = GET_PRIV (tp_file);
-
-  priv->factory = empathy_contact_factory_new ();
-  priv->mc = empathy_mission_control_new ();
+  fd = socket (PF_UNIX, SOCK_STREAM, 0);
+  if (fd < 0)
+    {
+      int code = errno;
 
-  tp_cli_channel_connect_to_closed (priv->channel,
-      (tp_cli_channel_signal_callback_closed) tp_file_closed_cb,
-      tp_file,
-      NULL, NULL, NULL);
+      error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
+          EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
 
-  emp_cli_channel_type_file_connect_to_file_transfer_state_changed (
-      TP_PROXY (priv->channel),
-      (emp_cli_channel_type_file_signal_callback_file_transfer_state_changed)
-          tp_file_state_changed_cb,
-      tp_file,
-      NULL, NULL, NULL);
+      DEBUG ("Failed to create socket, closing channel");
 
-  emp_cli_channel_type_file_connect_to_transferred_bytes_changed (
-      TP_PROXY (priv->channel),
-      (emp_cli_channel_type_file_signal_callback_transferred_bytes_changed)
-          tp_file_transferred_bytes_changed_cb,
-      tp_file,
-      NULL, NULL, NULL);
+      ft_operation_close_with_error (tp_file, error);
+      g_clear_error (&error);
 
+      return;
+    }
 
-  handle = tp_channel_get_handle (priv->channel, NULL);
-  priv->contact = empathy_contact_factory_get_from_handle (priv->factory,
-      priv->account,
-      (guint) handle);
+  memset (&addr, 0, sizeof (addr));
+  addr.sun_family = AF_UNIX;
+  strncpy (addr.sun_path, priv->unix_socket_path->data,
+      priv->unix_socket_path->len);
 
-  if (!tp_cli_dbus_properties_run_get_all (priv->channel,
-      -1, EMP_IFACE_CHANNEL_TYPE_FILE, &properties, &error, NULL))
+  if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
     {
-      DEBUG ("Failed to get properties: %s",
-          error ? error->message : "No error given");
-      g_clear_error (&error);
-      return NULL;
-    }
+      int code = errno;
 
-  priv->size = g_value_get_uint64 (g_hash_table_lookup (properties, "Size"));
+      error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
+          EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
 
-  priv->state = g_value_get_uint (g_hash_table_lookup (properties, "State"));
+      DEBUG ("Failed to connect socket, closing channel");
 
-  /* Invalid reason, so empathy_file_get_state_change_reason() can give
-   * a warning if called for a not closed file transfer. */
-  priv->state_change_reason = -1;
+      ft_operation_close_with_error (tp_file, error);
+      close (fd);
+      g_clear_error (&error);
 
-  priv->transferred_bytes = g_value_get_uint64 (g_hash_table_lookup (
-      properties, "TransferredBytes"));
+      return;
+    }
 
-  priv->filename = g_value_dup_string (g_hash_table_lookup (properties,
-      "Filename"));
+  DEBUG ("Start the transfer");
 
-  priv->content_md5 = g_value_dup_string (g_hash_table_lookup (properties,
-      "ContentMD5"));
+  priv->start_time = empathy_time_get_current ();
 
-  priv->description = g_value_dup_string (g_hash_table_lookup (properties,
-      "Description"));
+  /* notify we're starting a transfer */
+  if (priv->progress_callback != NULL)
+    priv->progress_callback (tp_file, 0, priv->progress_user_data);
 
-  priv->unix_socket_path = g_value_dup_string (g_hash_table_lookup (properties,
-      "SocketPath"));
+  if (priv->incoming)
+    {
+      GInputStream *socket_stream;
 
-  priv->direction = g_value_get_uint (g_hash_table_lookup (properties,
-      "Direction"));
+      socket_stream = g_unix_input_stream_new (fd, TRUE);
 
-  g_hash_table_destroy (properties);
+      g_output_stream_splice_async (priv->out_stream, socket_stream,
+          G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
+          G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
+          G_PRIORITY_DEFAULT, priv->cancellable,
+          splice_stream_ready_cb, tp_file);
 
-  return tp_file;
-}
+      g_object_unref (socket_stream);
+    }
+  else
+    {
+      GOutputStream *socket_stream;
 
-static void
-tp_file_get_property (GObject *object,
-                      guint param_id,
-                      GValue *value,
-                      GParamSpec *pspec)
-{
-  EmpathyTpFilePriv *priv;
-  EmpathyTpFile *tp_file;
+      socket_stream = g_unix_output_stream_new (fd, TRUE);
 
-  priv = GET_PRIV (object);
-  tp_file = EMPATHY_TP_FILE (object);
+      g_output_stream_splice_async (socket_stream, priv->in_stream,
+          G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
+          G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
+          G_PRIORITY_DEFAULT, priv->cancellable,
+          splice_stream_ready_cb, tp_file);
 
-  switch (param_id)
-    {
-      case PROP_ACCOUNT:
-        g_value_set_object (value, priv->account);
-        break;
-      case PROP_CHANNEL:
-        g_value_set_object (value, priv->channel);
-        break;
-      default:
-        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
-        break;
-    };
-}
-
-static void
-tp_file_channel_set_dbus_property (gpointer proxy,
-                                   const gchar *property,
-                                   const GValue *value)
-{
-        DEBUG ("Setting %s property", property);
-        tp_cli_dbus_properties_run_set (TP_PROXY (proxy),
-            -1,
-            EMP_IFACE_CHANNEL_TYPE_FILE,
-            property,
-            value,
-            NULL, NULL);
-        DEBUG ("done");
+      g_object_unref (socket_stream);
+    }
 }
 
-
-static void
-tp_file_set_property (GObject *object,
-                      guint param_id,
-                      const GValue *value,
-                      GParamSpec *pspec)
+static GError *
+error_from_state_change_reason (TpFileTransferStateChangeReason reason)
 {
-  EmpathyTpFilePriv *priv;
+  const char *string;
+  GError *retval = NULL;
 
-  priv = GET_PRIV (object);
+  string = NULL;
 
-  switch (param_id)
+  switch (reason)
     {
-      case PROP_ACCOUNT:
-        priv->account = g_object_ref (g_value_get_object (value));
-        break;
-      case PROP_CHANNEL:
-        priv->channel = g_object_ref (g_value_get_object (value));
-        break;
-      case PROP_STATE:
-        priv->state = g_value_get_uint (value);
-        break;
-      case PROP_INCOMING:
-        priv->incoming = g_value_get_boolean (value);
+      case TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE:
+        string = _("No reason was specified");
         break;
-      case PROP_FILENAME:
-        g_free (priv->filename);
-        priv->filename = g_value_dup_string (value);
-        tp_file_channel_set_dbus_property (priv->channel, "Filename", value);
+      case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REQUESTED:
+        string = _("The change in state was requested");
         break;
-      case PROP_SIZE:
-        priv->size = g_value_get_uint64 (value);
-        tp_file_channel_set_dbus_property (priv->channel, "Size", value);
+      case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_STOPPED:
+        string = _("You canceled the file transfer");
         break;
-      case PROP_CONTENT_TYPE:
-        tp_file_channel_set_dbus_property (priv->channel, "ContentType", value);
-        g_free (priv->content_type);
-        priv->content_type = g_value_dup_string (value);
+      case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_STOPPED:
+        string = _("The other participant canceled the file transfer");
         break;
-      case PROP_CONTENT_MD5:
-        tp_file_channel_set_dbus_property (priv->channel, "ContentMD5", value);
-        g_free (priv->content_md5);
-        priv->content_md5 = g_value_dup_string (value);
+      case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR:
+        string = _("Error while trying to transfer the file");
         break;
-      case PROP_IN_STREAM:
-        if (priv->in_stream)
-          g_object_unref (priv->in_stream);
-        priv->in_stream = g_object_ref (g_value_get_object (value));
+      case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_ERROR:
+        string = _("The other participant is unable to transfer the file");
         break;
       default:
-        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+        string = _("Unknown reason");
         break;
-    };
-}
-
-/**
- * empathy_tp_file_new:
- * @account: the #McAccount for the channel
- * @channel: a Telepathy channel
- *
- * Creates a new #EmpathyTpFile wrapping @channel.
- *
- * Returns: a new #EmpathyTpFile
- */
-EmpathyTpFile *
-empathy_tp_file_new (McAccount *account,
-                  TpChannel *channel)
-{
-  return g_object_new (EMPATHY_TYPE_TP_FILE,
-      "account", account,
-      "channel", channel,
-      NULL);
-}
-
-/**
- * empathy_tp_file_get_id:
- * @tp_file: an #EmpathyTpFile
- *
- * Returns the ID of @tp_file.
- *
- * Returns: the ID
- */
-const gchar *
-empathy_tp_file_get_id (EmpathyTpFile *tp_file)
-{
-  EmpathyTpFilePriv *priv;
-
-  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
-
-  priv = GET_PRIV (tp_file);
-
-  return priv->id;
-}
-
-/**
- * empathy_tp_file_get_channel
- * @tp_file: an #EmpathyTpFile
- *
- * Returns the Telepathy file transfer channel
- *
- * Returns: the #TpChannel
- */
-TpChannel *
-empathy_tp_file_get_channel (EmpathyTpFile *tp_file)
-{
-  EmpathyTpFilePriv *priv;
-
-  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
-
-  priv = GET_PRIV (tp_file);
-
-  return priv->channel;
-}
-
-static void
-tp_file_destroy_cb (TpChannel *file_channel,
-                    EmpathyTpFile *tp_file)
-{
-  EmpathyTpFilePriv *priv;
-
-  priv = GET_PRIV (tp_file);
+    }
 
-  DEBUG ("Channel Closed or CM crashed");
+  retval = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
+      EMPATHY_FT_ERROR_TP_ERROR, string);
 
-  g_object_unref (priv->channel);
-  priv->channel = NULL;
+  return retval;
 }
 
 static void
-tp_file_closed_cb (TpChannel *file_channel,
-                   EmpathyTpFile *tp_file,
-                   GObject *weak_object)
-{
-  EmpathyTpFilePriv *priv;
-
-  priv = GET_PRIV (tp_file);
-
-  /* The channel is closed, do just like if the proxy was destroyed */
-  g_signal_handlers_disconnect_by_func (priv->channel,
-      tp_file_destroy_cb,
-      tp_file);
-  tp_file_destroy_cb (file_channel, tp_file);
-}
-
-static gint64
-get_time_msec (void)
-{
-  GTimeVal tv;
-
-  g_get_current_time (&tv);
-  return ((gint64) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
-}
-
-static gint
-_get_local_socket (EmpathyTpFile *tp_file)
+tp_file_state_changed_cb (TpChannel *proxy,
+    guint state,
+    guint reason,
+    gpointer user_data,
+    GObject *weak_object)
 {
-  gint fd;
-  size_t path_len;
-  struct sockaddr_un addr;
-  EmpathyTpFilePriv *priv;
-  GValue *socket_path;
-
-  priv = GET_PRIV (tp_file);
-
-  /* TODO: This could probably be a little nicer. */
-  tp_cli_dbus_properties_run_get (priv->channel,
-      -1,
-      EMP_IFACE_CHANNEL_TYPE_FILE,
-      "SocketPath",
-      &socket_path,
-      NULL,
-      NULL);
-
-  if (priv->unix_socket_path)
-    g_free (priv->unix_socket_path);
-
-  priv->unix_socket_path = g_value_dup_string (socket_path);
-  g_value_unset (socket_path);
-
-  if (G_STR_EMPTY (priv->unix_socket_path))
-    return -1;
-
-  fd = socket (PF_UNIX, SOCK_STREAM, 0);
-  if (fd < 0)
-    return -1;
-
-  memset (&addr, 0, sizeof (addr));
-  addr.sun_family = AF_UNIX;
-  path_len = strlen (priv->unix_socket_path);
-  strncpy (addr.sun_path, priv->unix_socket_path, path_len);
-
-  if (connect (fd, (struct sockaddr*) &addr,
-      sizeof (addr)) < 0)
-    {
-      close (fd);
-      return -1;
-    }
-
-  return fd;
-}
-
-/**
- * empathy_tp_file_accept:
- * @tp_file: an #EmpathyTpFile
- *
- * Accepts a file transfer that's in the "local pending" state (i.e.
- * EMP_FILE_TRANSFER_STATE_LOCAL_PENDING).
- */
-void
-empathy_tp_file_accept (EmpathyTpFile *tp_file)
-{
-  EmpathyTpFilePriv *priv;
-  GValue *address;
-  GValue nothing = { 0 };
+  EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
   GError *error = NULL;
 
-  g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
+  if (state == priv->state)
+    return;
 
-  priv = GET_PRIV (tp_file);
+  DEBUG ("File transfer state changed:\n"
+      "old state = %u, state = %u, reason = %u\n"
+      "\tincoming = %s, in_stream = %s, out_stream = %s",
+      priv->state, state, reason,
+      priv->incoming ? "yes" : "no",
+      priv->in_stream ? "present" : "not present",
+      priv->out_stream ? "present" : "not present");
 
-  g_return_if_fail (priv->out_stream != NULL);
+  priv->state = state;
+  priv->state_change_reason = reason;
 
-  DEBUG ("Accepting file: filename=%s", priv->filename);
+  /* If the channel is open AND we have the socket path, we can start the
+   * transfer. The socket path could be NULL if we are not doing the actual
+   * data transfer but are just an observer for the channel.
+   */
+  if (state == TP_FILE_TRANSFER_STATE_OPEN &&
+      priv->unix_socket_path != NULL)
+    tp_file_start_transfer (EMPATHY_TP_FILE (weak_object));
 
-  g_value_init (&nothing, G_TYPE_STRING);
-  g_value_set_string (&nothing, "");
+  if (state == TP_FILE_TRANSFER_STATE_COMPLETED)
+    ft_operation_close_clean (EMPATHY_TP_FILE (weak_object));
 
-  if (!emp_cli_channel_type_file_run_accept_file (TP_PROXY (priv->channel),
-      -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
-      &nothing, &address, &error, NULL))
+  if (state == TP_FILE_TRANSFER_STATE_CANCELLED)
     {
-      DEBUG ("Accept error: %s",
-          error ? error->message : "No message given");
+      error = error_from_state_change_reason (priv->state_change_reason);
+      ft_operation_close_with_error (EMPATHY_TP_FILE (weak_object), error);
       g_clear_error (&error);
     }
-
-  if (priv->unix_socket_path)
-    g_free (priv->unix_socket_path);
-
-  priv->unix_socket_path = g_value_dup_string (address);
-  g_value_unset (address);
-
-  DEBUG ("Got unix socket path: %s", priv->unix_socket_path);
 }
 
 static void
-receive_tp_file (EmpathyTpFile *tp_file)
+tp_file_transferred_bytes_changed_cb (TpChannel *proxy,
+    guint64 count,
+    gpointer user_data,
+    GObject *weak_object)
 {
-  EmpathyTpFilePriv *priv;
-  GInputStream *socket_stream;
-  gint socket_fd;
+  EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
 
-  priv = GET_PRIV (tp_file);
-
-  socket_fd = _get_local_socket (tp_file);
-
-  if (socket_fd < 0)
+  /* don't notify for 0 bytes count */
+  if (count == 0)
     return;
 
-  socket_stream = g_unix_input_stream_new (socket_fd, TRUE);
-
-  priv->cancellable = g_cancellable_new ();
-
-  copy_stream (socket_stream, priv->out_stream, priv->cancellable);
-
-  g_object_unref (socket_stream);
+  /* notify clients */
+  if (priv->progress_callback != NULL)
+    priv->progress_callback (EMPATHY_TP_FILE (weak_object),
+        count, priv->progress_user_data);
 }
 
-
 static void
-send_tp_file (EmpathyTpFile *tp_file)
+ft_operation_provide_or_accept_file_cb (TpChannel *proxy,
+    const GValue *address,
+    const GError *error,
+    gpointer user_data,
+    GObject *weak_object)
 {
-  gint socket_fd;
-  GOutputStream *socket_stream;
-  EmpathyTpFilePriv *priv;
+  EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
+  GError *myerr = NULL;
+  EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
 
-  priv = GET_PRIV (tp_file);
-
-  DEBUG ("Sending file content: filename=%s",
-           priv->filename);
+  g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr);
 
-  g_return_if_fail (priv->in_stream);
+  if (error != NULL)
+    {
+      if (myerr != NULL)
+        {
+          /* if we were both cancelled and failed when calling the method,
+          * report the method error.
+          */
+          g_clear_error (&myerr);
+          myerr = g_error_copy (error);
+        }
+    }
 
-  socket_fd = _get_local_socket (tp_file);
-  if (socket_fd < 0)
+  if (myerr != NULL)
     {
-      DEBUG ("failed to get local socket fd");
+      DEBUG ("Error: %s", error->message);
+      ft_operation_close_with_error (tp_file, myerr);
+      g_clear_error (&myerr);
       return;
     }
-  DEBUG ("got local socket fd");
-  socket_stream = g_unix_output_stream_new (socket_fd, TRUE);
 
-  priv->cancellable = g_cancellable_new ();
+  if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY)
+    {
+      priv->unix_socket_path = g_value_dup_boxed (address);
+    }
+  else if (G_VALUE_TYPE (address) == G_TYPE_STRING)
+    {
+      /* Old bugged version of telepathy-salut used to store the address
+       * as a 's' instead of an 'ay' */
+      const gchar *path;
+
+      path = g_value_get_string (address);
+      priv->unix_socket_path = g_array_sized_new (TRUE, FALSE, sizeof (gchar),
+                                                  strlen (path));
+      g_array_insert_vals (priv->unix_socket_path, 0, path, strlen (path));
+    }
 
-  copy_stream (priv->in_stream, socket_stream, priv->cancellable);
+  DEBUG ("Got unix socket path: %s", priv->unix_socket_path->data);
 
-  g_object_unref (socket_stream);
+  /* if the channel is already open, start the transfer now, otherwise,
+   * wait for the state change signal.
+   */
+  if (priv->state == TP_FILE_TRANSFER_STATE_OPEN)
+    tp_file_start_transfer (tp_file);
 }
 
 static void
-tp_file_state_changed_cb (DBusGProxy *tp_file_iface,
-                          EmpFileTransferState state,
-                          EmpFileTransferStateChangeReason reason,
-                          EmpathyTpFile *tp_file)
+file_read_async_cb (GObject *source,
+    GAsyncResult *res,
+    gpointer user_data)
 {
+  GValue nothing = { 0 };
+  EmpathyTpFile *tp_file = user_data;
   EmpathyTpFilePriv *priv;
+  GFileInputStream *in_stream;
+  GError *error = NULL;
 
   priv = GET_PRIV (tp_file);
 
-  DEBUG ("File transfer state changed: filename=%s, "
-      "old state=%u, state=%u, reason=%u",
-      priv->filename, priv->state, state, reason);
+  in_stream = g_file_read_finish (G_FILE (source), res, &error);
 
-  if (state == EMP_FILE_TRANSFER_STATE_OPEN)
-    priv->start_time = get_time_msec ();
-
-  DEBUG ("state = %u, incoming = %s, in_stream = %s, out_stream = %s",
-      state, priv->incoming ? "yes" : "no",
-      priv->in_stream ? "present" : "not present",
-      priv->out_stream ? "present" : "not present");
+  if (error != NULL)
+    {
+      ft_operation_close_with_error (tp_file, error);
+      g_clear_error (&error);
+      return;
+    }
 
-  if (state == EMP_FILE_TRANSFER_STATE_OPEN && !priv->incoming &&
-      priv->in_stream)
-    send_tp_file (tp_file);
-  else if (state == EMP_FILE_TRANSFER_STATE_OPEN && priv->incoming &&
-      priv->out_stream)
-      receive_tp_file (tp_file);
+  priv->in_stream = G_INPUT_STREAM (in_stream);
 
-  priv->state = state;
-  priv->state_change_reason = reason;
+  g_value_init (&nothing, G_TYPE_STRING);
+  g_value_set_static_string (&nothing, "");
 
-  g_object_notify (G_OBJECT (tp_file), "state");
+  tp_cli_channel_type_file_transfer_call_provide_file (
+      priv->channel, -1,
+      TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
+      &nothing, ft_operation_provide_or_accept_file_cb,
+      NULL, NULL, G_OBJECT (tp_file));
 }
 
 static void
-tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
-                                      guint64 count,
-                                      EmpathyTpFile *tp_file,
-                                      GObject *weak_object)
+file_replace_async_cb (GObject *source,
+    GAsyncResult *res,
+    gpointer user_data)
 {
+  GValue nothing = { 0 };
+  EmpathyTpFile *tp_file = user_data;
   EmpathyTpFilePriv *priv;
+  GError *error = NULL;
+  GFileOutputStream *out_stream;
 
   priv = GET_PRIV (tp_file);
 
-  if (priv->transferred_bytes == count)
-    return;
-
-  priv->transferred_bytes = count;
-
-  g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
-}
+  out_stream = g_file_replace_finish (G_FILE (source), res, &error);
 
-EmpathyContact *
-empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
-{
-  EmpathyTpFilePriv *priv;
-
-  priv = GET_PRIV (tp_file);
+  if (error != NULL)
+    {
+      ft_operation_close_with_error (tp_file, error);
+      g_clear_error (&error);
 
-  return priv->contact;
-}
+      return;
+    }
 
-GInputStream *
-empathy_tp_file_get_input_stream (EmpathyTpFile *tp_file)
-{
-  EmpathyTpFilePriv *priv;
+  priv->out_stream = G_OUTPUT_STREAM (out_stream);
 
-  priv = GET_PRIV (tp_file);
+  g_value_init (&nothing, G_TYPE_STRING);
+  g_value_set_static_string (&nothing, "");
 
-  return priv->in_stream;
+  tp_cli_channel_type_file_transfer_call_accept_file (priv->channel,
+      -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
+      &nothing, priv->offset,
+      ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
 }
 
-GOutputStream *
-empathy_tp_file_get_output_stream (EmpathyTpFile *tp_file)
+static void
+channel_closed_cb (TpChannel *proxy,
+    const GError *error,
+    gpointer user_data,
+    GObject *weak_object)
 {
-  EmpathyTpFilePriv *priv;
+  EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
+  EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
+  gboolean cancel = GPOINTER_TO_INT (user_data);
 
-  priv = GET_PRIV (tp_file);
+  DEBUG ("Channel is closed, should cancel %s", cancel ? "True" : "False");
 
-  return priv->out_stream;
+  if (priv->cancellable != NULL &&
+      !g_cancellable_is_cancelled (priv->cancellable) && cancel)
+    g_cancellable_cancel (priv->cancellable);
 }
 
-const gchar *
-empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
+static void
+close_channel_internal (EmpathyTpFile *tp_file,
+    gboolean cancel)
 {
-  EmpathyTpFilePriv *priv;
+  EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
 
-  priv = GET_PRIV (tp_file);
+  DEBUG ("Closing channel, should cancel %s", cancel ?
+         "True" : "False");
 
-  return priv->filename;
+  tp_cli_channel_call_close (priv->channel, -1,
+    channel_closed_cb, GINT_TO_POINTER (cancel), NULL, G_OBJECT (tp_file));
 }
 
-gboolean
-empathy_tp_file_get_incoming (EmpathyTpFile *tp_file)
-{
-  EmpathyTpFilePriv *priv;
-
-  priv = GET_PRIV (tp_file);
-
-  return priv->incoming;
-}
+/* GObject methods */
 
-EmpFileTransferState
-empathy_tp_file_get_state (EmpathyTpFile *tp_file)
+static void
+empathy_tp_file_init (EmpathyTpFile *tp_file)
 {
   EmpathyTpFilePriv *priv;
 
-  priv = GET_PRIV (tp_file);
+  priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
+      EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
 
-  return priv->state;
+  tp_file->priv = priv;
 }
 
-EmpFileTransferStateChangeReason
-empathy_tp_file_get_state_change_reason (EmpathyTpFile *tp_file)
+static void
+do_dispose (GObject *object)
 {
-  EmpathyTpFilePriv *priv;
-
-  priv = GET_PRIV (tp_file);
-
-  g_return_val_if_fail (priv->state_change_reason >= 0,
-      EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE);
+  EmpathyTpFilePriv *priv = GET_PRIV (object);
 
-  return priv->state_change_reason;
-}
+  if (priv->dispose_run)
+    return;
 
-guint64
-empathy_tp_file_get_size (EmpathyTpFile *tp_file)
-{
-  EmpathyTpFilePriv *priv;
+  priv->dispose_run = TRUE;
 
-  priv = GET_PRIV (tp_file);
+  if (priv->channel != NULL)
+    {
+      g_signal_handlers_disconnect_by_func (priv->channel,
+          tp_file_invalidated_cb, object);
+      g_object_unref (priv->channel);
+      priv->channel = NULL;
+    }
 
-  return priv->size;
-}
+  if (priv->in_stream != NULL)
+    g_object_unref (priv->in_stream);
 
-guint64
-empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file)
-{
-  EmpathyTpFilePriv *priv;
+  if (priv->out_stream != NULL)
+    g_object_unref (priv->out_stream);
 
-  priv = GET_PRIV (tp_file);
+  if (priv->cancellable != NULL)
+    g_object_unref (priv->cancellable);
 
-  return priv->transferred_bytes;
+  G_OBJECT_CLASS (empathy_tp_file_parent_class)->dispose (object);
 }
 
-gint
-empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
+static void
+do_finalize (GObject *object)
 {
-  EmpathyTpFilePriv *priv;
-  gint64 curr_time, elapsed_time;
-  gdouble time_per_byte;
-  gdouble remaining_time;
-
-  priv = GET_PRIV (tp_file);
-
-  if (priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE)
-    return -1;
+  EmpathyTpFilePriv *priv = GET_PRIV (object);
 
-  if (priv->transferred_bytes == priv->size)
-    return 0;
+  DEBUG ("%p", object);
 
-  curr_time = get_time_msec ();
-  elapsed_time = curr_time - priv->start_time;
-  time_per_byte = (gdouble) elapsed_time / (gdouble) priv->transferred_bytes;
-  remaining_time = (time_per_byte * (priv->size - priv->transferred_bytes)) / 1000;
+  if (priv->unix_socket_path != NULL)
+    {
+      g_array_free (priv->unix_socket_path, TRUE);
+      priv->unix_socket_path = NULL;
+    }
 
-  return (gint) (remaining_time + 0.5);
+  G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
 }
 
-void
-empathy_tp_file_cancel (EmpathyTpFile *tp_file)
+static void
+do_get_property (GObject *object,
+    guint param_id,
+    GValue *value,
+    GParamSpec *pspec)
 {
-  EmpathyTpFilePriv *priv;
-
-  priv = GET_PRIV (tp_file);
-
-  tp_cli_channel_run_close (priv->channel, -1, NULL, NULL);
+  EmpathyTpFilePriv *priv = GET_PRIV (object);
 
-  g_cancellable_cancel (priv->cancellable);
+  switch (param_id)
+    {
+      case PROP_CHANNEL:
+        g_value_set_object (value, priv->channel);
+        break;
+      case PROP_INCOMING:
+        g_value_set_boolean (value, priv->incoming);
+        break;
+      default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+        break;
+    };
 }
 
-void
-empathy_tp_file_set_input_stream (EmpathyTpFile *tp_file,
-                                  GInputStream *in_stream)
+static void
+do_set_property (GObject *object,
+    guint param_id,
+    const GValue *value,
+    GParamSpec *pspec)
 {
-  EmpathyTpFilePriv *priv;
-
-  priv = GET_PRIV (tp_file);
-
-  if (priv->in_stream == in_stream)
-    return;
-
-  if (priv->incoming)
-    g_warning ("Setting an input stream for incoming file "
-         "transfers is useless");
-
-  if (priv->in_stream)
-    g_object_unref (priv->in_stream);
-
-  if (in_stream)
-    g_object_ref (in_stream);
-
-  priv->in_stream = in_stream;
-
-  g_object_notify (G_OBJECT (tp_file), "in-stream");
+  EmpathyTpFilePriv *priv = GET_PRIV (object);
+  switch (param_id)
+    {
+      case PROP_CHANNEL:
+        priv->channel = g_object_ref (g_value_get_object (value));
+        break;
+      case PROP_INCOMING:
+        priv->incoming = g_value_get_boolean (value);
+        break;
+      default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+        break;
+    };
 }
 
-void
-empathy_tp_file_set_output_stream (EmpathyTpFile *tp_file,
-                                   GOutputStream *out_stream)
+static void
+do_constructed (GObject *object)
 {
+  EmpathyTpFile *tp_file;
   EmpathyTpFilePriv *priv;
 
+  tp_file = EMPATHY_TP_FILE (object);
   priv = GET_PRIV (tp_file);
 
-  if (priv->out_stream == out_stream)
-    return;
+  g_signal_connect (priv->channel, "invalidated",
+    G_CALLBACK (tp_file_invalidated_cb), tp_file);
 
-  if (!priv->incoming)
-    g_warning ("Setting an output stream for outgoing file "
-         "transfers is useless");
+  tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
+      priv->channel, tp_file_state_changed_cb, NULL, NULL, object, NULL);
 
-  if (priv->out_stream)
-    g_object_unref (priv->out_stream);
+  tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
+      priv->channel, tp_file_transferred_bytes_changed_cb,
+      NULL, NULL, object, NULL);
 
-  if (out_stream)
-    g_object_ref (out_stream);
+  tp_cli_dbus_properties_call_get (priv->channel,
+      -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "State", tp_file_get_state_cb,
+      NULL, NULL, object);
 
-  priv->out_stream = out_stream;
+  priv->state_change_reason =
+      TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
 }
 
-void
-empathy_tp_file_set_filename (EmpathyTpFile *tp_file,
-                              const gchar *filename)
+static void
+empathy_tp_file_class_init (EmpathyTpFileClass *klass)
 {
-  EmpathyTpFilePriv *priv;
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-  priv = GET_PRIV (tp_file);
+  object_class->finalize = do_finalize;
+  object_class->dispose = do_dispose;
+  object_class->constructed = do_constructed;
+  object_class->get_property = do_get_property;
+  object_class->set_property = do_set_property;
 
-  g_return_if_fail (filename != NULL);
+  /* Construct-only properties */
 
-  if (priv->filename && strcmp (filename, priv->filename) == 0)
-    return;
+  /**
+   * EmpathyTpFile:channel:
+   *
+   * The #TpChannel requested for the file transfer.
+   */
+  g_object_class_install_property (object_class,
+      PROP_CHANNEL,
+      g_param_spec_object ("channel",
+          "telepathy channel",
+          "The file transfer channel",
+          TP_TYPE_CHANNEL,
+          G_PARAM_READWRITE |
+          G_PARAM_CONSTRUCT_ONLY));
 
-  g_free (priv->filename);
-  priv->filename = g_strdup (filename);
+  /**
+   * EmpathyTpFile:incoming:
+   *
+   * %TRUE if the transfer is incoming, %FALSE if it's outgoing.
+   */
+  g_object_class_install_property (object_class,
+      PROP_INCOMING,
+      g_param_spec_boolean ("incoming",
+          "direction of transfer",
+          "The direction of the file being transferred",
+          FALSE,
+          G_PARAM_READWRITE |
+          G_PARAM_CONSTRUCT_ONLY));
 
-  g_object_notify (G_OBJECT (tp_file), "filename");
+  g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
 }
 
-/* Functions to copy the content of a GInputStream to a GOutputStream */
-
-#define N_BUFFERS 2
-#define BUFFER_SIZE 4096
-
-typedef struct {
-  GInputStream *in;
-  GOutputStream *out;
-  GCancellable  *cancellable;
-  char *buff[N_BUFFERS]; /* the temporary buffers */
-  gsize count[N_BUFFERS]; /* how many bytes are used in the buffers */
-  gboolean is_full[N_BUFFERS]; /* whether the buffers contain data */
-  gint curr_read; /* index of the buffer used for reading */
-  gint curr_write; /* index of the buffer used for writing */
-  gboolean is_reading; /* we are reading */
-  gboolean is_writing; /* we are writing */
-  guint n_closed; /* number of streams that have been closed */
-} CopyData;
-
-static void schedule_next (CopyData *copy);
+/* public methods */
 
-static void
-free_copy_data_if_closed (CopyData *copy)
+/**
+ * empathy_tp_file_new:
+ * @channel: a #TpChannel
+ * @incoming: whether the file transfer is incoming or not
+ *
+ * Creates a new #EmpathyTpFile wrapping @channel, with the direction
+ * specified by @incoming. The returned #EmpathyTpFile should be unrefed
+ * with g_object_unref() when finished with.
+ *
+ * Return value: a new #EmpathyTpFile
+ */
+EmpathyTpFile *
+empathy_tp_file_new (TpChannel *channel,
+    gboolean incoming)
 {
-  gint i;
-
-  /* Free the data only if both the input and output streams have
-   * been closed. */
-  copy->n_closed++;
-  if (copy->n_closed < 2)
-    return;
-
-  if (copy->in != NULL)
-    g_object_unref (copy->in);
+  EmpathyTpFile *tp_file;
 
-  if (copy->out != NULL)
-    g_object_unref (copy->out);
+  g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
 
-  for (i = 0; i < N_BUFFERS; i++)
-    g_free (copy->buff[i]);
+  tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
+      "channel", channel, "incoming", incoming,
+      NULL);
 
-  g_object_unref (copy->cancellable);
-  g_free (copy);
+  return tp_file;
 }
 
-static void
-io_error (CopyData *copy,
-          GError *error)
+/**
+ * empathy_tp_file_accept:
+ * @tp_file: an incoming #EmpathyTpFile
+ * @offset: the offset of @gfile where we should start writing
+ * @gfile: the destination #GFile for the transfer
+ * @cancellable: a #GCancellable
+ * @progress_callback: function to callback with progress information
+ * @progress_user_data: user_data to pass to @progress_callback
+ * @op_callback: function to callback when the transfer ends
+ * @op_user_data: user_data to pass to @op_callback
+ *
+ * Accepts an incoming file transfer, saving the result into @gfile.
+ * The callback @op_callback will be called both when the transfer is
+ * successful and in case of an error. Note that cancelling @cancellable,
+ * closes the socket of the file operation in progress, but doesn't
+ * guarantee that the transfer channel will be closed as well. Thus,
+ * empathy_tp_file_cancel() or empathy_tp_file_close() should be used to
+ * actually cancel an ongoing #EmpathyTpFile.
+ */
+void
+empathy_tp_file_accept (EmpathyTpFile *tp_file,
+    guint64 offset,
+    GFile *gfile,
+    GCancellable *cancellable,
+    EmpathyTpFileProgressCallback progress_callback,
+    gpointer progress_user_data,
+    EmpathyTpFileOperationCallback op_callback,
+    gpointer op_user_data)
 {
-  g_cancellable_cancel (copy->cancellable);
+  EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
 
-  if (error == NULL)
-    g_warning ("I/O error");
-  else if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
-    ; /* Ignore cancellations */
-  else
-    g_warning ("I/O error: %d: %s\n", error->code, error->message);
-
-  if (copy->in != NULL)
-    g_input_stream_close (copy->in, NULL, NULL);
+  g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
+  g_return_if_fail (G_IS_FILE (gfile));
+  g_return_if_fail (G_IS_CANCELLABLE (cancellable));
 
-  if (copy->out != NULL)
-    g_output_stream_close (copy->out, NULL, NULL);
+  priv->cancellable = g_object_ref (cancellable);
+  priv->progress_callback = progress_callback;
+  priv->progress_user_data = progress_user_data;
+  priv->op_callback = op_callback;
+  priv->op_user_data = op_user_data;
+  priv->offset = offset;
 
-  free_copy_data_if_closed (copy);
+  g_file_replace_async (gfile, NULL, FALSE, G_FILE_CREATE_NONE,
+      G_PRIORITY_DEFAULT, cancellable, file_replace_async_cb, tp_file);
 }
 
-static void
-close_done (GObject *source_object,
-            GAsyncResult *res,
-            gpointer user_data)
-{
-  CopyData *copy = user_data;
-
-  g_object_unref (source_object);
-  free_copy_data_if_closed (copy);
-}
 
-static void
-write_done_cb (GObject *source_object,
-               GAsyncResult *res,
-               gpointer user_data)
+/**
+ * empathy_tp_file_offer:
+ * @tp_file: an outgoing #EmpathyTpFile
+ * @gfile: the source #GFile for the transfer
+ * @cancellable: a #GCancellable
+ * @progress_callback: function to callback with progress information
+ * @progress_user_data: user_data to pass to @progress_callback
+ * @op_callback: function to callback when the transfer ends
+ * @op_user_data: user_data to pass to @op_callback
+ *
+ * Offers an outgoing file transfer, reading data from @gfile.
+ * The callback @op_callback will be called both when the transfer is
+ * successful and in case of an error. Note that cancelling @cancellable,
+ * closes the socket of the file operation in progress, but doesn't
+ * guarantee that the transfer channel will be closed as well. Thus,
+ * empathy_tp_file_cancel() or empathy_tp_file_close() should be used to
+ * actually cancel an ongoing #EmpathyTpFile.
+ */
+void
+empathy_tp_file_offer (EmpathyTpFile *tp_file,
+    GFile *gfile,
+    GCancellable *cancellable,
+    EmpathyTpFileProgressCallback progress_callback,
+    gpointer progress_user_data,
+    EmpathyTpFileOperationCallback op_callback,
+    gpointer op_user_data)
 {
-  CopyData *copy = user_data;
-  gssize count_write;
-  GError *error = NULL;
-
-  count_write = g_output_stream_write_finish (copy->out, res, &error);
+  EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
 
-  if (count_write <= 0)
-    {
-      io_error (copy, error);
-      g_error_free (error);
-      return;
-    }
+  g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
+  g_return_if_fail (G_IS_FILE (gfile));
+  g_return_if_fail (G_IS_CANCELLABLE (cancellable));
 
-  copy->is_full[copy->curr_write] = FALSE;
-  copy->curr_write = (copy->curr_write + 1) % N_BUFFERS;
-  copy->is_writing = FALSE;
+  priv->cancellable = g_object_ref (cancellable);
+  priv->progress_callback = progress_callback;
+  priv->progress_user_data = progress_user_data;
+  priv->op_callback = op_callback;
+  priv->op_user_data = op_user_data;
 
-  schedule_next (copy);
+  g_file_read_async (gfile, G_PRIORITY_DEFAULT, cancellable,
+      file_read_async_cb, tp_file);
 }
 
-static void
-read_done_cb (GObject *source_object,
-              GAsyncResult *res,
-              gpointer user_data)
+/**
+ * empathy_tp_file_is_incoming:
+ * @tp_file: an #EmpathyTpFile
+ *
+ * Returns whether @tp_file is incoming.
+ *
+ * Return value: %TRUE if the @tp_file is incoming, otherwise %FALSE
+ */
+gboolean
+empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
 {
-  CopyData *copy = user_data;
-  gssize count_read;
-  GError *error = NULL;
+  EmpathyTpFilePriv *priv;
 
-  count_read = g_input_stream_read_finish (copy->in, res, &error);
+  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
 
-  if (count_read == 0)
-    {
-      g_input_stream_close_async (copy->in, 0, copy->cancellable,
-          close_done, copy);
-      copy->in = NULL;
-    }
-  else if (count_read < 0)
-    {
-      io_error (copy, error);
-      g_error_free (error);
-      return;
-    }
-
-  copy->count[copy->curr_read] = count_read;
-  copy->is_full[copy->curr_read] = TRUE;
-  copy->curr_read = (copy->curr_read + 1) % N_BUFFERS;
-  copy->is_reading = FALSE;
+  priv = GET_PRIV (tp_file);
 
-  schedule_next (copy);
+  return priv->incoming;
 }
 
-static void
-schedule_next (CopyData *copy)
+/**
+ * empathy_tp_file_cancel:
+ * @tp_file: an #EmpathyTpFile
+ *
+ * Cancels an ongoing #EmpathyTpFile, first closing the channel and then
+ * cancelling any I/O operation and closing the socket.
+ */
+void
+empathy_tp_file_cancel (EmpathyTpFile *tp_file)
 {
-  if (copy->in != NULL &&
-      !copy->is_reading &&
-      !copy->is_full[copy->curr_read])
-    {
-      /* We are not reading and the current buffer is empty, so
-       * start an async read. */
-      copy->is_reading = TRUE;
-      g_input_stream_read_async (copy->in,
-          copy->buff[copy->curr_read],
-          BUFFER_SIZE, 0, copy->cancellable,
-          read_done_cb, copy);
-    }
+  g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
 
-  if (!copy->is_writing &&
-      copy->is_full[copy->curr_write])
-    {
-      if (copy->count[copy->curr_write] == 0)
-        {
-          /* The last read on the buffer read 0 bytes, this
-           * means that we got an EOF, so we can close
-           * the output channel. */
-          g_output_stream_close_async (copy->out, 0,
-              copy->cancellable,
-              close_done, copy);
-      copy->out = NULL;
-        }
-      else
-        {
-          /* We are not writing and the current buffer contains
-           * data, so start an async write. */
-          copy->is_writing = TRUE;
-          g_output_stream_write_async (copy->out,
-              copy->buff[copy->curr_write],
-              copy->count[copy->curr_write],
-              0, copy->cancellable,
-              write_done_cb, copy);
-        }
-    }
+  close_channel_internal (tp_file, TRUE);
 }
 
-static void
-copy_stream (GInputStream *in,
-             GOutputStream *out,
-             GCancellable *cancellable)
+/**
+ * empathy_tp_file_close:
+ * @tp_file: an #EmpathyTpFile
+ *
+ * Closes the channel for an ongoing #EmpathyTpFile. It's safe to call this
+ * method after the transfer has ended.
+ */
+void
+empathy_tp_file_close (EmpathyTpFile *tp_file)
 {
-  CopyData *copy;
-  gint i;
-
-  g_return_if_fail (in != NULL);
-  g_return_if_fail (out != NULL);
-
-  copy = g_new0 (CopyData, 1);
-  copy->in = g_object_ref (in);
-  copy->out = g_object_ref (out);
-
-  if (cancellable != NULL)
-    copy->cancellable = g_object_ref (cancellable);
-  else
-    copy->cancellable = g_cancellable_new ();
-
-  for (i = 0; i < N_BUFFERS; i++)
-    copy->buff[i] = g_malloc (BUFFER_SIZE);
+  g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
 
-  schedule_next (copy);
+  close_channel_internal (tp_file, FALSE);
 }