]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-tp-file.c
Merge branch 'sasl'
[empathy.git] / libempathy / empathy-tp-file.c
index f003b0c70639c42d6ec89c91c9e9c348110995cb..25a8c40bc095c254bca7f8c2d4d03fec35399768 100644 (file)
@@ -1,6 +1,5 @@
-/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
- * Copyright (C) 2007-2008 Collabora Ltd.
+ * 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
  *
  * 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 <arpa/inet.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/un.h>
+#include <netinet/in.h>
 
 #include <glib/gi18n-lib.h>
 
 #include <gio/gunixinputstream.h>
 #include <gio/gunixoutputstream.h>
 
+#include <telepathy-glib/gtypes.h>
 #include <telepathy-glib/proxy-subclass.h>
 #include <telepathy-glib/util.h>
+#include <telepathy-glib/interfaces.h>
 
 #include "empathy-tp-file.h"
-#include "empathy-tp-contact-factory.h"
 #include "empathy-marshal.h"
 #include "empathy-time.h"
 #include "empathy-utils.h"
 
-#include "extensions/extensions.h"
-
 #define DEBUG_FLAG EMPATHY_DEBUG_FT
 #include "empathy-debug.h"
 
 /**
  * SECTION:empathy-tp-file
- * @short_description: File channel
- * @see_also: #EmpathyTpFile, #EmpathyContact, empathy_dispatcher_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.
  */
 
-/* Functions to copy the content of a GInputStream to a GOutputStream */
-
-#define N_BUFFERS 2
-#define BUFFER_SIZE 4096
-#define STALLED_TIMEOUT 5
+/* EmpathyTpFile object */
 
 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 */
-  gint ref_count;
-} CopyData;
-
-static void schedule_next (CopyData *copy);
-
-static void
-copy_data_unref (CopyData *copy)
-{
-  if (--copy->ref_count == 0)
-    {
-      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;
+  TpChannel *channel;
 
-      if (copy->in != NULL)
-        g_object_unref (copy->in);
+  GInputStream *in_stream;
+  GOutputStream *out_stream;
 
-      if (copy->out != NULL)
-        g_object_unref (copy->out);
+  /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
+  TpFileTransferState state;
+  TpFileTransferStateChangeReason state_change_reason;
+  TpSocketAddressType socket_address_type;
+  TpSocketAccessControl socket_access_control;
 
-      for (i = 0; i < N_BUFFERS; i++)
-        g_free (copy->buff[i]);
+  /* transfer properties */
+  gboolean incoming;
+  time_t start_time;
+  GArray *socket_address;
+  guint port;
+  guint64 offset;
 
-      g_object_unref (copy->cancellable);
-      g_free (copy);
-    }
-}
+  /* GCancellable we're passed when offering/accepting the transfer */
+  GCancellable *cancellable;
 
-static void
-io_error (CopyData *copy,
-          GError *error)
-{
-  g_cancellable_cancel (copy->cancellable);
+  /* callbacks for the operation */
+  EmpathyTpFileProgressCallback progress_callback;
+  gpointer progress_user_data;
+  EmpathyTpFileOperationCallback op_callback;
+  gpointer op_user_data;
 
-  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);
+  gboolean is_closing;
+  gboolean is_closed;
 
-  if (copy->in != NULL)
-    g_input_stream_close (copy->in, NULL, NULL);
+  gboolean dispose_run;
+} EmpathyTpFilePriv;
 
-  if (copy->out != NULL)
-    g_output_stream_close (copy->out, NULL, NULL);
+enum {
+  PROP_0,
+  PROP_CHANNEL,
+  PROP_INCOMING
+};
 
-  copy_data_unref (copy);
-}
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpFile)
 
-static void
-close_done (GObject *source_object,
-            GAsyncResult *res,
-            gpointer user_data)
-{
-  CopyData *copy = user_data;
+G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
 
-  g_object_unref (source_object);
-  copy_data_unref (copy);
-}
+/* private functions */
 
 static void
-write_done_cb (GObject *source_object,
-               GAsyncResult *res,
-               gpointer user_data)
+tp_file_get_state_cb (TpProxy *proxy,
+    const GValue *value,
+    const GError *error,
+    gpointer user_data,
+    GObject *weak_object)
 {
-  CopyData *copy = user_data;
-  gssize count_write;
-  GError *error = NULL;
+  EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
 
-  count_write = g_output_stream_write_finish (copy->out, res, &error);
-
-  if (count_write <= 0)
+  if (error != NULL)
     {
-      io_error (copy, error);
-      g_error_free (error);
+      /* set a default value for the state */
+      priv->state = TP_FILE_TRANSFER_STATE_NONE;
       return;
     }
 
-  copy->is_full[copy->curr_write] = FALSE;
-  copy->curr_write = (copy->curr_write + 1) % N_BUFFERS;
-  copy->is_writing = FALSE;
-
-  schedule_next (copy);
+  priv->state = g_value_get_uint (value);
 }
 
 static void
-read_done_cb (GObject *source_object,
-              GAsyncResult *res,
-              gpointer user_data)
+tp_file_get_available_socket_types_cb (TpProxy *proxy,
+    const GValue *value,
+    const GError *error,
+    gpointer user_data,
+    GObject *weak_object)
 {
-  CopyData *copy = user_data;
-  gssize count_read;
-  GError *error = NULL;
-
-  count_read = g_input_stream_read_finish (copy->in, res, &error);
+  EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
+  GHashTable *socket_types;
+  GArray *access_controls;
 
-  if (count_read == 0)
+  if (error != NULL ||
+      !G_VALUE_HOLDS (value, TP_HASH_TYPE_SUPPORTED_SOCKET_MAP))
     {
-      g_input_stream_close_async (copy->in, 0, copy->cancellable,
-          close_done, copy);
-      copy->in = NULL;
+      /* set a default value */
+      priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_UNIX;
+      priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST;
+      goto out;
     }
-  else if (count_read < 0)
+
+  socket_types = g_value_get_boxed (value);
+
+  /* here UNIX is preferred to IPV4 */
+  if ((access_controls = g_hash_table_lookup (socket_types,
+      GUINT_TO_POINTER (TP_SOCKET_ADDRESS_TYPE_UNIX))) != NULL)
     {
-      io_error (copy, error);
-      g_error_free (error);
-      return;
+      priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_UNIX;
+      priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST;
+      goto out;
     }
 
-  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;
+  if ((access_controls = g_hash_table_lookup (socket_types,
+      GUINT_TO_POINTER (TP_SOCKET_ADDRESS_TYPE_IPV4))) != NULL)
+    {
+      priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_IPV4;
+
+      /* TODO: we should prefer PORT over LOCALHOST when the CM will
+       * support it.
+       */
 
-  schedule_next (copy);
+      priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST;
+    }
+
+out:
+  DEBUG ("Socket address type: %u, access control %u",
+      priv->socket_address_type, priv->socket_access_control);
 }
 
 static void
-schedule_next (CopyData *copy)
+tp_file_invalidated_cb (TpProxy       *proxy,
+    guint          domain,
+    gint           code,
+    gchar         *message,
+    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);
-    }
+  EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
+
+  DEBUG ("Channel invalidated: %s", message);
 
-  if (!copy->is_writing &&
-      copy->is_full[copy->curr_write])
+  if (priv->state != TP_FILE_TRANSFER_STATE_COMPLETED &&
+      priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
     {
-      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);
-        }
+      /* 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;
     }
 }
 
 static void
-copy_stream (GInputStream *in,
-             GOutputStream *out,
-             GCancellable *cancellable)
+ft_operation_close_clean (EmpathyTpFile *tp_file)
 {
-  CopyData *copy;
-  gint i;
-
-  g_return_if_fail (in != NULL);
-  g_return_if_fail (out != NULL);
+  EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
 
-  copy = g_new0 (CopyData, 1);
-  copy->in = g_object_ref (in);
-  copy->out = g_object_ref (out);
-  copy->ref_count = 1;
+  if (priv->is_closed)
+    return;
 
-  if (cancellable != NULL)
-    copy->cancellable = g_object_ref (cancellable);
-  else
-    copy->cancellable = g_cancellable_new ();
+  DEBUG ("FT operation close clean");
 
-  for (i = 0; i < N_BUFFERS; i++)
-    copy->buff[i] = g_malloc (BUFFER_SIZE);
+  priv->is_closed = TRUE;
 
-  schedule_next (copy);
+  if (priv->op_callback != NULL)
+    priv->op_callback (tp_file, NULL, priv->op_user_data);
 }
 
-/* EmpathyTpFile object */
-
-struct _EmpathyTpFilePriv {
-  EmpathyTpContactFactory *factory;
-  MissionControl *mc;
-  TpChannel *channel;
-  gboolean ready;
-
-  EmpathyContact *contact;
-  GInputStream *in_stream;
-  GOutputStream *out_stream;
-
-  /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
-  TpFileTransferState state;
-  gchar *content_type;
-  gchar *filename;
-  guint64 size;
-  TpFileHashType content_hash_type;
-  gchar *content_hash;
-  gchar *description;
-  guint64 transferred_bytes;
+static void
+ft_operation_close_with_error (EmpathyTpFile *tp_file,
+    GError *error)
+{
+  EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
 
-  gboolean incoming;
-  TpFileTransferStateChangeReason state_change_reason;
-  time_t last_update_time;
-  guint64 last_update_transferred_bytes;
-  gdouble speed;
-  gint remaining_time;
-  guint stalled_id;
-  GValue *socket_address;
-  GCancellable *cancellable;
-};
+  if (priv->is_closed)
+    return;
 
-enum {
-  PROP_0,
-  PROP_CHANNEL,
-  PROP_STATE,
-  PROP_INCOMING,
-  PROP_READY,
-  PROP_FILENAME,
-  PROP_SIZE,
-  PROP_CONTENT_TYPE,
-  PROP_TRANSFERRED_BYTES,
-  PROP_CONTENT_HASH_TYPE,
-  PROP_CONTENT_HASH,
-};
+  DEBUG ("FT operation close with error %s", error->message);
 
-enum {
-       REFRESH,
-       LAST_SIGNAL
-};
+  priv->is_closed = TRUE;
 
-static guint signals[LAST_SIGNAL];
+  /* close the channel if it's not cancelled already */
+  if (priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
+    empathy_tp_file_cancel (tp_file);
 
-G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
+  if (priv->op_callback != NULL)
+    priv->op_callback (tp_file, error, priv->op_user_data);
+}
 
 static void
-empathy_tp_file_init (EmpathyTpFile *tp_file)
+splice_stream_ready_cb (GObject *source,
+    GAsyncResult *res,
+    gpointer user_data)
 {
+  EmpathyTpFile *tp_file;
   EmpathyTpFilePriv *priv;
+  GError *error = NULL;
 
-  priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
-      EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
+  tp_file = user_data;
+  priv = GET_PRIV (tp_file);
 
-  tp_file->priv = priv;
-}
+  g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error);
 
-static void
-tp_file_invalidated_cb (TpProxy       *proxy,
-                       guint          domain,
-                       gint           code,
-                       gchar         *message,
-                       EmpathyTpFile *tp_file)
-{
-  DEBUG ("Channel invalidated: %s", message);
+  DEBUG ("Splice stream ready cb, error %p", error);
 
-  if (tp_file->priv->state != TP_FILE_TRANSFER_STATE_COMPLETED &&
-      tp_file->priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
+  if (error != NULL && !priv->is_closing)
     {
-      /* The channel is not in a finished state, an error occured */
-      tp_file->priv->state = TP_FILE_TRANSFER_STATE_CANCELLED;
-      tp_file->priv->state_change_reason =
-          TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR;
-      g_object_notify (G_OBJECT (tp_file), "state");
+      ft_operation_close_with_error (tp_file, error);
+      g_clear_error (&error);
+      return;
     }
 }
 
 static void
-tp_file_finalize (GObject *object)
+tp_file_start_transfer (EmpathyTpFile *tp_file)
 {
-  EmpathyTpFile *tp_file = EMPATHY_TP_FILE (object);
+  gint fd, domain, res = 0;
+  GError *error = NULL;
+  struct sockaddr *my_addr = NULL;
+  size_t my_size = 0;
+  EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
 
-  if (tp_file->priv->channel)
+  if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_UNIX)
     {
-      g_signal_handlers_disconnect_by_func (tp_file->priv->channel,
-          tp_file_invalidated_cb, object);
-      g_object_unref (tp_file->priv->channel);
-      tp_file->priv->channel = NULL;
+      domain = AF_UNIX;
     }
-
-  if (tp_file->priv->factory)
+  else if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_IPV4)
     {
-      g_object_unref (tp_file->priv->factory);
+      domain = AF_INET;
     }
-  if (tp_file->priv->mc)
+  else
     {
-      g_object_unref (tp_file->priv->mc);
-    }
+      error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
+          EMPATHY_FT_ERROR_NOT_SUPPORTED, _("Socket type not supported"));
 
-  g_free (tp_file->priv->filename);
-  if (tp_file->priv->socket_address != NULL)
-    tp_g_value_slice_free (tp_file->priv->socket_address);
-  g_free (tp_file->priv->description);
-  g_free (tp_file->priv->content_hash);
-  g_free (tp_file->priv->content_type);
+      DEBUG ("Socket not supported, closing channel");
 
-  if (tp_file->priv->in_stream)
-    g_object_unref (tp_file->priv->in_stream);
+      ft_operation_close_with_error (tp_file, error);
+      g_clear_error (&error);
 
-  if (tp_file->priv->out_stream)
-    g_object_unref (tp_file->priv->out_stream);
+      return;
+    }
 
-  if (tp_file->priv->contact)
-    g_object_unref (tp_file->priv->contact);
+  fd = socket (domain, SOCK_STREAM, 0);
 
-  if (tp_file->priv->cancellable)
-    g_object_unref (tp_file->priv->cancellable);
+  if (fd < 0)
+    {
+      int code = errno;
 
-  if (tp_file->priv->stalled_id != 0)
-    g_source_remove (tp_file->priv->stalled_id);
+      error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
+          EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
 
-  G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
-}
+      DEBUG ("Failed to create socket, closing channel");
 
-static gboolean
-tp_file_stalled_cb (EmpathyTpFile *tp_file)
-{
-  /* We didn't get transferred bytes update for a while, the transfer is
-   * stalled. */
+      ft_operation_close_with_error (tp_file, error);
+      g_clear_error (&error);
 
-  tp_file->priv->speed = 0;
-  tp_file->priv->remaining_time = -1;
-  g_signal_emit (tp_file, signals[REFRESH], 0);
+      return;
+    }
 
-  return FALSE;
-}
+  if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_UNIX)
+    {
+      struct sockaddr_un addr;
 
-static void
-tp_file_start_transfer (EmpathyTpFile *tp_file)
-{
-  gint fd;
-  struct sockaddr_un addr;
-  GArray *array;
+      memset (&addr, 0, sizeof (addr));
+      addr.sun_family = domain;
+      strncpy (addr.sun_path, priv->socket_address->data,
+          priv->socket_address->len);
 
-  fd = socket (PF_UNIX, SOCK_STREAM, 0);
-  if (fd < 0)
-    {
-      DEBUG ("Failed to create socket, closing channel");
-      empathy_tp_file_cancel (tp_file);
-      return;
+      my_addr = (struct sockaddr *) &addr;
+      my_size = sizeof (addr);
     }
+  else if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_IPV4)
+    {
+      struct sockaddr_in addr;
+
+      memset (&addr, 0, sizeof (addr));
+      addr.sin_family = domain;
+      inet_pton (AF_INET, priv->socket_address->data, &addr.sin_addr);
+      addr.sin_port = htons (priv->port);
 
-  array = g_value_get_boxed (tp_file->priv->socket_address);
+      my_addr = (struct sockaddr *) &addr;
+      my_size = sizeof (addr);
+    }
 
-  memset (&addr, 0, sizeof (addr));
-  addr.sun_family = AF_UNIX;
-  strncpy (addr.sun_path, array->data, array->len);
+  res = connect (fd, my_addr, my_size);
 
-  if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
+  if (res < 0)
     {
+      int code = errno;
+
+      error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
+          EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
+
       DEBUG ("Failed to connect socket, closing channel");
-      empathy_tp_file_cancel (tp_file);
+
+      ft_operation_close_with_error (tp_file, error);
       close (fd);
+      g_clear_error (&error);
+
       return;
     }
 
   DEBUG ("Start the transfer");
 
-  tp_file->priv->last_update_time = empathy_time_get_current ();
-  tp_file->priv->last_update_transferred_bytes = tp_file->priv->transferred_bytes;
-  tp_file->priv->stalled_id = g_timeout_add_seconds (STALLED_TIMEOUT,
-    (GSourceFunc) tp_file_stalled_cb, tp_file);
+  priv->start_time = empathy_time_get_current ();
 
-  tp_file->priv->cancellable = g_cancellable_new ();
-  if (tp_file->priv->incoming)
+  /* notify we're starting a transfer */
+  if (priv->progress_callback != NULL)
+    priv->progress_callback (tp_file, 0, priv->progress_user_data);
+
+  if (priv->incoming)
     {
       GInputStream *socket_stream;
 
       socket_stream = g_unix_input_stream_new (fd, TRUE);
-      copy_stream (socket_stream, tp_file->priv->out_stream,
-          tp_file->priv->cancellable);
+
+      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);
+
       g_object_unref (socket_stream);
     }
   else
@@ -473,281 +377,386 @@ tp_file_start_transfer (EmpathyTpFile *tp_file)
       GOutputStream *socket_stream;
 
       socket_stream = g_unix_output_stream_new (fd, TRUE);
-      copy_stream (tp_file->priv->in_stream, socket_stream,
-          tp_file->priv->cancellable);
+
+      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);
+
       g_object_unref (socket_stream);
     }
 }
 
+static GError *
+error_from_state_change_reason (TpFileTransferStateChangeReason reason)
+{
+  const char *string;
+  GError *retval = NULL;
+
+  string = NULL;
+
+  switch (reason)
+    {
+      case TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE:
+        string = _("No reason was specified");
+        break;
+      case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REQUESTED:
+        string = _("The change in state was requested");
+        break;
+      case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_STOPPED:
+        string = _("You canceled the file transfer");
+        break;
+      case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_STOPPED:
+        string = _("The other participant canceled the file transfer");
+        break;
+      case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR:
+        string = _("Error while trying to transfer the file");
+        break;
+      case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_ERROR:
+        string = _("The other participant is unable to transfer the file");
+        break;
+      default:
+        string = _("Unknown reason");
+        break;
+    }
+
+  retval = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
+      EMPATHY_FT_ERROR_TP_ERROR, string);
+
+  return retval;
+}
+
 static void
-tp_file_state_changed_cb (TpChannel *channel,
-                          guint state,
-                          guint reason,
-                          gpointer user_data,
-                          GObject *weak_object)
+tp_file_state_changed_cb (TpChannel *proxy,
+    guint state,
+    guint reason,
+    gpointer user_data,
+    GObject *weak_object)
 {
-  EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
+  EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
+  GError *error = NULL;
 
-  if (state == tp_file->priv->state)
+  if (state == priv->state)
     return;
 
   DEBUG ("File transfer state changed:\n"
-      "\tfilename = %s, old state = %u, state = %u, reason = %u\n"
+      "old state = %u, state = %u, reason = %u\n"
       "\tincoming = %s, in_stream = %s, out_stream = %s",
-      tp_file->priv->filename, tp_file->priv->state, state, reason,
-      tp_file->priv->incoming ? "yes" : "no",
-      tp_file->priv->in_stream ? "present" : "not present",
-      tp_file->priv->out_stream ? "present" : "not present");
+      priv->state, state, reason,
+      priv->incoming ? "yes" : "no",
+      priv->in_stream ? "present" : "not present",
+      priv->out_stream ? "present" : "not present");
+
+  priv->state = state;
+  priv->state_change_reason = reason;
 
   /* 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. */
+   * data transfer but are just an observer for the channel.
+   */
   if (state == TP_FILE_TRANSFER_STATE_OPEN &&
-      tp_file->priv->socket_address != NULL)
-    tp_file_start_transfer (tp_file);
+      priv->socket_address != NULL)
+    tp_file_start_transfer (EMPATHY_TP_FILE (weak_object));
+
+  if (state == TP_FILE_TRANSFER_STATE_COMPLETED)
+    ft_operation_close_clean (EMPATHY_TP_FILE (weak_object));
+
+  if (state == TP_FILE_TRANSFER_STATE_CANCELLED)
+    {
+      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);
+    }
+}
 
-  tp_file->priv->state = state;
-  tp_file->priv->state_change_reason = reason;
+static void
+tp_file_transferred_bytes_changed_cb (TpChannel *proxy,
+    guint64 count,
+    gpointer user_data,
+    GObject *weak_object)
+{
+  EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
+
+  /* don't notify for 0 bytes count */
+  if (count == 0)
+    return;
 
-  g_object_notify (G_OBJECT (tp_file), "state");
+  /* notify clients */
+  if (priv->progress_callback != NULL)
+    priv->progress_callback (EMPATHY_TP_FILE (weak_object),
+        count, priv->progress_user_data);
 }
 
 static void
-tp_file_transferred_bytes_changed_cb (TpChannel *channel,
-                                      guint64 count,
-                                      gpointer user_data,
-                                      GObject *weak_object)
+ft_operation_provide_or_accept_file_cb (TpChannel *proxy,
+    const GValue *address,
+    const GError *error,
+    gpointer user_data,
+    GObject *weak_object)
 {
   EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
-  time_t curr_time, elapsed_time;
-  guint64 transferred_bytes;
+  GError *myerr = NULL;
+  EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
 
-  /* If we didn't progress since last update, return */
-  if (tp_file->priv->transferred_bytes == count)
-    return;
+  g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr);
+
+  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);
+    }
 
-  /* Update the transferred bytes count */
-  tp_file->priv->transferred_bytes = count;
-  g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
-
-  /* We got a progress, reset the stalled timeout */
-  if (tp_file->priv->stalled_id != 0)
-    g_source_remove (tp_file->priv->stalled_id);
-  tp_file->priv->stalled_id = g_timeout_add_seconds (STALLED_TIMEOUT,
-    (GSourceFunc) tp_file_stalled_cb, tp_file);
-
-  /* Calculate the transfer speed and remaining time estimation. We recalculate
-   * that each second to get more dynamic values that react faster to network
-   * changes. This is better than calculating the average from the begining of
-   * the transfer, I think. */
-  curr_time = empathy_time_get_current ();
-  elapsed_time = curr_time - tp_file->priv->last_update_time;
-  if (elapsed_time >= 1)
+  if (myerr != NULL)
     {
-      transferred_bytes = count - tp_file->priv->last_update_transferred_bytes;
-      tp_file->priv->speed = (gdouble) transferred_bytes / (gdouble) elapsed_time;
-      tp_file->priv->remaining_time = (tp_file->priv->size - count) /
-        tp_file->priv->speed;
-      tp_file->priv->last_update_transferred_bytes = count;
-      tp_file->priv->last_update_time = curr_time;
-
-      g_signal_emit (tp_file, signals[REFRESH], 0);
+      DEBUG ("Error: %s", myerr->message);
+      ft_operation_close_with_error (tp_file, myerr);
+      g_clear_error (&myerr);
+      return;
+    }
+
+  if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY)
+    {
+      priv->socket_address = 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->socket_address = g_array_sized_new (TRUE, FALSE, sizeof (gchar),
+          strlen (path));
+      g_array_insert_vals (priv->socket_address, 0, path, strlen (path));
+    }
+  else if (G_VALUE_TYPE (address) == TP_STRUCT_TYPE_SOCKET_ADDRESS_IPV4)
+    {
+      GValueArray *val_array;
+      GValue *v;
+      const char *addr;
+
+      val_array = g_value_get_boxed (address);
+
+      /* IPV4 address */
+      v = g_value_array_get_nth (val_array, 0);
+      addr = g_value_get_string (v);
+      priv->socket_address = g_array_sized_new (TRUE, FALSE, sizeof (gchar),
+          strlen (addr));
+      g_array_insert_vals (priv->socket_address, 0, addr, strlen (addr));
+
+      /* port number */
+      v = g_value_array_get_nth (val_array, 1);
+      priv->port = g_value_get_uint (v);
     }
+
+  DEBUG ("Got socket address: %s, port (not zero if IPV4): %d",
+      priv->socket_address->data, priv->port);
+
+  /* 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_check_if_ready (EmpathyTpFile *tp_file)
+initialize_empty_ac_variant (TpSocketAccessControl ac,
+    GValue *val)
 {
-  if (tp_file->priv->ready || tp_file->priv->contact == NULL ||
-      tp_file->priv->state == 0)
-    return;
-
-  tp_file->priv->ready = TRUE;
-  g_object_notify (G_OBJECT (tp_file), "ready");
+  /* TODO: we will add more types here once we support PORT access control. */
+  if (ac == TP_SOCKET_ACCESS_CONTROL_LOCALHOST)
+    {
+      g_value_init (val, G_TYPE_STRING);
+      g_value_set_static_string (val, "");
+    }
 }
 
 static void
-tp_file_got_contact_cb (EmpathyTpContactFactory *factory,
-                        EmpathyContact *contact,
-                        const GError *error,
-                        gpointer user_data,
-                        GObject *weak_object)
+file_read_async_cb (GObject *source,
+    GAsyncResult *res,
+    gpointer user_data)
 {
-  EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
+  GValue nothing = { 0 };
+  EmpathyTpFile *tp_file = user_data;
+  EmpathyTpFilePriv *priv;
+  GFileInputStream *in_stream;
+  GError *error = NULL;
 
-  if (error)
+  priv = GET_PRIV (tp_file);
+
+  in_stream = g_file_read_finish (G_FILE (source), res, &error);
+
+  if (error != NULL && !priv->is_closing)
     {
-      DEBUG ("Error: %s", error->message);
-      empathy_tp_file_close (tp_file);
+      ft_operation_close_with_error (tp_file, error);
+      g_clear_error (&error);
       return;
     }
 
-  tp_file->priv->contact = g_object_ref (contact);
-  tp_file_check_if_ready (tp_file);
+  priv->in_stream = G_INPUT_STREAM (in_stream);
+
+  /* we don't impose specific interface/port requirements even
+   * if we're not using UNIX sockets.
+   */
+  initialize_empty_ac_variant (priv->socket_access_control, &nothing);
+
+  tp_cli_channel_type_file_transfer_call_provide_file (
+      priv->channel, -1,
+      priv->socket_address_type, priv->socket_access_control,
+      &nothing, ft_operation_provide_or_accept_file_cb,
+      NULL, NULL, G_OBJECT (tp_file));
 }
 
 static void
-tp_file_get_all_cb (TpProxy *proxy,
-                    GHashTable *properties,
-                    const GError *error,
-                    gpointer user_data,
-                    GObject *file_obj)
+file_replace_async_cb (GObject *source,
+    GAsyncResult *res,
+    gpointer user_data)
 {
-  EmpathyTpFile *tp_file = EMPATHY_TP_FILE (file_obj);
+  GValue nothing = { 0 };
+  EmpathyTpFile *tp_file = user_data;
+  EmpathyTpFilePriv *priv;
+  GError *error = NULL;
+  GFileOutputStream *out_stream;
 
-  if (error)
+  priv = GET_PRIV (tp_file);
+
+  out_stream = g_file_replace_finish (G_FILE (source), res, &error);
+
+  if (error != NULL)
     {
-      DEBUG ("Error: %s", error->message);
-      tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL,
-          NULL);
+      ft_operation_close_with_error (tp_file, error);
+      g_clear_error (&error);
+
       return;
     }
 
-  tp_file->priv->size = g_value_get_uint64 (
-      g_hash_table_lookup (properties, "Size"));
-  g_object_notify (file_obj, "size");
+  priv->out_stream = G_OUTPUT_STREAM (out_stream);
 
-  tp_file->priv->state = g_value_get_uint (
-      g_hash_table_lookup (properties, "State"));
-  g_object_notify (file_obj, "state");
+  /* we don't impose specific interface/port requirements even
+   * if we're not using UNIX sockets.
+   */
+  initialize_empty_ac_variant (priv->socket_access_control, &nothing);
 
-  tp_file->priv->transferred_bytes = g_value_get_uint64 (
-      g_hash_table_lookup (properties, "TransferredBytes"));
-  g_object_notify (file_obj, "transferred-bytes");
+  tp_cli_channel_type_file_transfer_call_accept_file (priv->channel,
+      -1, priv->socket_address_type, priv->socket_access_control,
+      &nothing, priv->offset,
+      ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
+}
 
-  tp_file->priv->filename = g_value_dup_string (
-      g_hash_table_lookup (properties, "Filename"));
-  g_object_notify (file_obj, "filename");
+static void
+channel_closed_cb (TpChannel *proxy,
+    const GError *error,
+    gpointer user_data,
+    GObject *weak_object)
+{
+  EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
+  EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
+  gboolean cancel = GPOINTER_TO_INT (user_data);
 
-  tp_file->priv->content_hash = g_value_dup_string (
-      g_hash_table_lookup (properties, "ContentHash"));
-  g_object_notify (file_obj, "content-hash");
+  DEBUG ("Channel is closed, should cancel %s", cancel ? "True" : "False");
 
-  tp_file->priv->content_hash_type = g_value_get_uint (
-      g_hash_table_lookup (properties, "ContentHashType"));
-  g_object_notify (file_obj, "content-hash-type");
+  if (priv->cancellable != NULL &&
+      !g_cancellable_is_cancelled (priv->cancellable) && cancel)
+    g_cancellable_cancel (priv->cancellable);
+}
+
+static void
+close_channel_internal (EmpathyTpFile *tp_file,
+    gboolean cancel)
+{
+  EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
 
-  tp_file->priv->content_type = g_value_dup_string (
-      g_hash_table_lookup (properties, "ContentType"));
-  g_object_notify (file_obj, "content-type");
+  DEBUG ("Closing channel, should cancel %s", cancel ?
+         "True" : "False");
 
-  tp_file->priv->description = g_value_dup_string (
-      g_hash_table_lookup (properties, "Description"));
+  priv->is_closing = TRUE;
 
-  tp_file_check_if_ready (tp_file);
+  tp_cli_channel_call_close (priv->channel, -1,
+    channel_closed_cb, GINT_TO_POINTER (cancel), NULL, G_OBJECT (tp_file));
 }
 
+/* GObject methods */
+
 static void
-tp_file_get_requested_cb (TpProxy *proxy,
-                          const GValue *requested,
-                          const GError *error,
-                          gpointer user_data,
-                          GObject *weak_object)
+empathy_tp_file_init (EmpathyTpFile *tp_file)
 {
-  EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
-
-  if (error)
-    {
-      DEBUG ("Error: %s", error->message);
-      tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL,
-          NULL);
-      return;
-    }
+  EmpathyTpFilePriv *priv;
 
-  tp_file->priv->incoming = !g_value_get_boolean (requested);
-  g_object_notify (G_OBJECT (tp_file), "incoming");
+  priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
+      EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
 
-  tp_file_check_if_ready (tp_file);
+  tp_file->priv = priv;
 }
 
-static GObject *
-tp_file_constructor (GType type,
-                     guint n_props,
-                     GObjectConstructParam *props)
+static void
+do_dispose (GObject *object)
 {
-  GObject *file_obj;
-  EmpathyTpFile *tp_file;
-  TpHandle handle;
-  TpConnection *connection;
+  EmpathyTpFilePriv *priv = GET_PRIV (object);
 
-  file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
-      n_props, props);
+  if (priv->dispose_run)
+    return;
 
-  tp_file = EMPATHY_TP_FILE (file_obj);
+  priv->dispose_run = TRUE;
 
-  connection = tp_channel_borrow_connection (tp_file->priv->channel);
-  tp_file->priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
-  tp_file->priv->mc = empathy_mission_control_dup_singleton ();
-  tp_file->priv->state_change_reason =
-      TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
+  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;
+    }
 
-  g_signal_connect (tp_file->priv->channel, "invalidated",
-    G_CALLBACK (tp_file_invalidated_cb), tp_file);
+  if (priv->in_stream != NULL)
+    g_object_unref (priv->in_stream);
 
-  tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
-      tp_file->priv->channel, tp_file_state_changed_cb, NULL, NULL,
-      G_OBJECT (tp_file), NULL);
+  if (priv->out_stream != NULL)
+    g_object_unref (priv->out_stream);
 
-  tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
-      tp_file->priv->channel, tp_file_transferred_bytes_changed_cb,
-      NULL, NULL, G_OBJECT (tp_file), NULL);
+  if (priv->cancellable != NULL)
+    g_object_unref (priv->cancellable);
 
-  tp_cli_dbus_properties_call_get (tp_file->priv->channel, -1,
-      TP_IFACE_CHANNEL, "Requested",
-      tp_file_get_requested_cb, NULL, NULL, file_obj);
+  G_OBJECT_CLASS (empathy_tp_file_parent_class)->dispose (object);
+}
 
-  tp_cli_dbus_properties_call_get_all (tp_file->priv->channel, -1,
-      TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
-      tp_file_get_all_cb, NULL, NULL, file_obj);
+static void
+do_finalize (GObject *object)
+{
+  EmpathyTpFilePriv *priv = GET_PRIV (object);
 
-  handle = tp_channel_get_handle (tp_file->priv->channel, NULL);
-  empathy_tp_contact_factory_get_from_handle (tp_file->priv->factory,
-      handle, tp_file_got_contact_cb, NULL, NULL, file_obj);
+  DEBUG ("%p", object);
 
-  return file_obj;
+  if (priv->socket_address != NULL)
+    {
+      g_array_free (priv->socket_address, TRUE);
+      priv->socket_address = NULL;
+    }
+
+  G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
 }
 
 static void
-tp_file_get_property (GObject *object,
-                      guint param_id,
-                      GValue *value,
-                      GParamSpec *pspec)
+do_get_property (GObject *object,
+    guint param_id,
+    GValue *value,
+    GParamSpec *pspec)
 {
-  EmpathyTpFile *tp_file;
-
-  tp_file = EMPATHY_TP_FILE (object);
+  EmpathyTpFilePriv *priv = GET_PRIV (object);
 
   switch (param_id)
     {
       case PROP_CHANNEL:
-        g_value_set_object (value, tp_file->priv->channel);
+        g_value_set_object (value, priv->channel);
         break;
       case PROP_INCOMING:
-        g_value_set_boolean (value, tp_file->priv->incoming);
-        break;
-      case PROP_STATE:
-        g_value_set_uint (value, tp_file->priv->state);
-        break;
-      case PROP_CONTENT_TYPE:
-        g_value_set_string (value, tp_file->priv->content_type);
-        break;
-      case PROP_FILENAME:
-        g_value_set_string (value, tp_file->priv->filename);
-        break;
-      case PROP_SIZE:
-        g_value_set_uint64 (value, tp_file->priv->size);
-        break;
-      case PROP_CONTENT_HASH_TYPE:
-        g_value_set_uint (value, tp_file->priv->content_hash_type);
-        break;
-      case PROP_CONTENT_HASH:
-        g_value_set_string (value, tp_file->priv->content_hash);
-        break;
-      case PROP_TRANSFERRED_BYTES:
-        g_value_set_uint64 (value, tp_file->priv->transferred_bytes);
-        break;
-      case PROP_READY:
-        g_value_set_boolean (value, tp_file->priv->ready);
+        g_value_set_boolean (value, priv->incoming);
         break;
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -756,56 +765,16 @@ tp_file_get_property (GObject *object,
 }
 
 static void
-tp_file_channel_set_dbus_property (gpointer proxy,
-                                   const gchar *property,
-                                   const GValue *value)
+do_set_property (GObject *object,
+    guint param_id,
+    const GValue *value,
+    GParamSpec *pspec)
 {
-        DEBUG ("Setting %s property", property);
-        tp_cli_dbus_properties_call_set (TP_PROXY (proxy), -1,
-            TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, property, value,
-            NULL, NULL, NULL, NULL);
-}
-
-static void
-tp_file_set_property (GObject *object,
-                      guint param_id,
-                      const GValue *value,
-                      GParamSpec *pspec)
-{
-  EmpathyTpFile *tp_file = (EmpathyTpFile *) object;
+  EmpathyTpFilePriv *priv = GET_PRIV (object);
   switch (param_id)
     {
       case PROP_CHANNEL:
-        tp_file->priv->channel = g_object_ref (g_value_get_object (value));
-        break;
-      case PROP_STATE:
-        tp_file->priv->state = g_value_get_uint (value);
-        break;
-      case PROP_INCOMING:
-        tp_file->priv->incoming = g_value_get_boolean (value);
-        break;
-      case PROP_FILENAME:
-        g_free (tp_file->priv->filename);
-        tp_file->priv->filename = g_value_dup_string (value);
-        tp_file_channel_set_dbus_property (tp_file->priv->channel,
-            "Filename", value);
-        break;
-      case PROP_SIZE:
-        tp_file->priv->size = g_value_get_uint64 (value);
-        tp_file_channel_set_dbus_property (tp_file->priv->channel,
-            "Size", value);
-        break;
-      case PROP_CONTENT_TYPE:
-        tp_file_channel_set_dbus_property (tp_file->priv->channel,
-            "ContentType", value);
-        g_free (tp_file->priv->content_type);
-        tp_file->priv->content_type = g_value_dup_string (value);
-        break;
-      case PROP_CONTENT_HASH:
-        tp_file_channel_set_dbus_property (tp_file->priv->channel,
-            "ContentHash", value);
-        g_free (tp_file->priv->content_hash);
-        tp_file->priv->content_hash = g_value_dup_string (value);
+        priv->channel = g_object_ref (g_value_get_object (value));
         break;
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -813,23 +782,94 @@ tp_file_set_property (GObject *object,
     };
 }
 
-static GHashTable *ft_table = NULL;
+static void
+do_constructed (GObject *object)
+{
+  EmpathyTpFile *tp_file;
+  EmpathyTpFilePriv *priv;
+
+  tp_file = EMPATHY_TP_FILE (object);
+  priv = GET_PRIV (tp_file);
+
+  g_signal_connect (priv->channel, "invalidated",
+    G_CALLBACK (tp_file_invalidated_cb), tp_file);
+
+  priv->incoming = !tp_channel_get_requested (priv->channel);
+
+  tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
+      priv->channel, tp_file_state_changed_cb, NULL, NULL, object, NULL);
+
+  tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
+      priv->channel, tp_file_transferred_bytes_changed_cb,
+      NULL, NULL, object, NULL);
+
+  tp_cli_dbus_properties_call_get (priv->channel,
+      -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "State", tp_file_get_state_cb,
+      NULL, NULL, object);
+
+  tp_cli_dbus_properties_call_get (priv->channel,
+      -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "AvailableSocketTypes",
+      tp_file_get_available_socket_types_cb, NULL, NULL, object);
+
+  priv->state_change_reason =
+      TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
+}
 
 static void
-tp_file_weak_notify_cb (gpointer channel,
-                        GObject *tp_file)
+empathy_tp_file_class_init (EmpathyTpFileClass *klass)
 {
-  g_hash_table_remove (ft_table, channel);
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  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;
+
+  /* Construct-only properties */
+
+  /**
+   * 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));
+
+  /**
+   * 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_READABLE));
+
+  g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
 }
 
+/* public methods */
+
 /**
  * empathy_tp_file_new:
- * @channel: a Telepathy channel
+ * @channel: a #TpChannel
+ * @incoming: whether the file transfer is incoming or not
  *
- * Creates a new #EmpathyTpFile wrapping @channel, or return a new ref to an
- * existing #EmpathyTpFile for that channel.
+ * Creates a new #EmpathyTpFile wrapping @channel.
+ * The returned #EmpathyTpFile should be unrefed
+ * with g_object_unref() when finished with.
  *
- * Returns: a new #EmpathyTpFile
+ * Return value: a new #EmpathyTpFile
  */
 EmpathyTpFile *
 empathy_tp_file_new (TpChannel *channel)
@@ -838,392 +878,149 @@ empathy_tp_file_new (TpChannel *channel)
 
   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
 
-  if (ft_table != NULL)
-    {
-      tp_file = g_hash_table_lookup (ft_table, channel);
-      if (tp_file != NULL) {
-        return g_object_ref (tp_file);
-      }
-    }
-  else
-    ft_table = g_hash_table_new_full (empathy_proxy_hash,
-      empathy_proxy_equal, (GDestroyNotify) g_object_unref, NULL);
-
   tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
       "channel", channel,
       NULL);
 
-  g_hash_table_insert (ft_table, g_object_ref (channel), tp_file);
-  g_object_weak_ref (G_OBJECT (tp_file), tp_file_weak_notify_cb, channel);
-
   return tp_file;
 }
 
-/**
- * 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)
-{
-  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
-
-  return tp_file->priv->channel;
-}
-
-static void
-tp_file_method_cb (TpChannel *channel,
-                   const GValue *address,
-                   const GError *error,
-                   gpointer user_data,
-                   GObject *weak_object)
-{
-  EmpathyTpFile *tp_file = (EmpathyTpFile *) weak_object;
-  GArray *array;
-
-  if (error)
-    {
-      DEBUG ("Error: %s", error->message);
-      empathy_tp_file_cancel (tp_file);
-      return;
-    }
-
-  if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY)
-    {
-      tp_file->priv->socket_address = tp_g_value_slice_dup (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);
-      array = g_array_sized_new (TRUE, FALSE, sizeof (gchar), strlen (path));
-      g_array_insert_vals (array, 0, path, strlen (path));
-
-      tp_file->priv->socket_address = tp_g_value_slice_new (
-          DBUS_TYPE_G_UCHAR_ARRAY);
-      g_value_set_boxed (tp_file->priv->socket_address, array);
-
-      g_array_free (array, TRUE);
-    }
-  else
-    {
-      DEBUG ("Wrong address type: %s", G_VALUE_TYPE_NAME (address));
-      empathy_tp_file_cancel (tp_file);
-      return;
-    }
-
-  array = g_value_get_boxed (tp_file->priv->socket_address);
-  DEBUG ("Got unix socket path: %s", array->data);
-
-  if (tp_file->priv->state == TP_FILE_TRANSFER_STATE_OPEN)
-    tp_file_start_transfer (tp_file);
-}
-
 /**
  * empathy_tp_file_accept:
- * @tp_file: an #EmpathyTpFile
- * @offset: position where to start the transfer
- * @gfile: a #GFile where to write transfered data
- * @error: a #GError set if there is an error when opening @gfile
+ * @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 a file transfer that's in the "local pending" state (i.e.
- * TP_FILE_TRANSFER_STATE_LOCAL_PENDING).
+ * 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,
-                        GError **error)
+    guint64 offset,
+    GFile *gfile,
+    GCancellable *cancellable,
+    EmpathyTpFileProgressCallback progress_callback,
+    gpointer progress_user_data,
+    EmpathyTpFileOperationCallback op_callback,
+    gpointer op_user_data)
 {
-  GValue nothing = { 0 };
+  EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
 
   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));
 
-  tp_file->priv->out_stream = G_OUTPUT_STREAM (g_file_replace (gfile, NULL,
-       FALSE, 0, NULL, error));
-  if (error && *error)
-    return;
-
-  g_free (tp_file->priv->filename);
-  tp_file->priv->filename = g_file_get_basename (gfile);
-  g_object_notify (G_OBJECT (tp_file), "filename");
-
-  DEBUG ("Accepting file: filename=%s", tp_file->priv->filename);
+  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;
 
-  g_value_init (&nothing, G_TYPE_STRING);
-  g_value_set_static_string (&nothing, "");
-
-  tp_cli_channel_type_file_transfer_call_accept_file (tp_file->priv->channel,
-      -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
-      &nothing, offset, tp_file_method_cb, NULL, NULL, G_OBJECT (tp_file));
+  g_file_replace_async (gfile, NULL, FALSE, G_FILE_CREATE_NONE,
+      G_PRIORITY_DEFAULT, cancellable, file_replace_async_cb, tp_file);
 }
 
+
 /**
  * empathy_tp_file_offer:
- * @tp_file: an #EmpathyTpFile
- * @gfile: a #GFile where to read the data to transfer
- * @error: a #GError set if there is an error when opening @gfile
+ * @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 a file transfer that's in the "not offered" state (i.e.
- * TP_FILE_TRANSFER_STATE_NOT_OFFERED).
+ * 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, GError **error)
+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)
 {
-  GValue nothing = { 0 };
+  EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
 
   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));
 
-  tp_file->priv->in_stream = G_INPUT_STREAM (g_file_read (gfile, NULL, error));
-  if (error && *error)
-       return;
-
-  g_value_init (&nothing, G_TYPE_STRING);
-  g_value_set_static_string (&nothing, "");
-
-  tp_cli_channel_type_file_transfer_call_provide_file (tp_file->priv->channel,
-      -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
-      &nothing, tp_file_method_cb, NULL, NULL, G_OBJECT (tp_file));
-}
-
-EmpathyContact *
-empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
-{
-  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
-  return tp_file->priv->contact;
-}
-
-const gchar *
-empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
-{
-  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
-  return tp_file->priv->filename;
-}
-
-gboolean
-empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
-{
-  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
-  return tp_file->priv->incoming;
-}
-
-TpFileTransferState
-empathy_tp_file_get_state (EmpathyTpFile *tp_file,
-                           TpFileTransferStateChangeReason *reason)
-{
-  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
-      TP_FILE_TRANSFER_STATE_NONE);
-
-  if (reason != NULL)
-    *reason = tp_file->priv->state_change_reason;
-
-  return tp_file->priv->state;
-}
-
-guint64
-empathy_tp_file_get_size (EmpathyTpFile *tp_file)
-{
-  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
-      EMPATHY_TP_FILE_UNKNOWN_SIZE);
-  return tp_file->priv->size;
-}
+  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;
 
-guint64
-empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file)
-{
-  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), 0);
-  return tp_file->priv->transferred_bytes;
+  g_file_read_async (gfile, G_PRIORITY_DEFAULT, cancellable,
+      file_read_async_cb, tp_file);
 }
 
 /**
- * empathy_tp_file_get_remaining_time:
- * @tp_file: a #EmpathyTpFile
+ * empathy_tp_file_is_incoming:
+ * @tp_file: an #EmpathyTpFile
  *
- * Get the current remaining time estimation, in seconds.
+ * Returns whether @tp_file is incoming.
  *
- * Returns: The time remaining.
- **/
-gint
-empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
+ * Return value: %TRUE if the @tp_file is incoming, otherwise %FALSE
+ */
+gboolean
+empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
 {
-  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), -1);
+  EmpathyTpFilePriv *priv;
 
-  if (tp_file->priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE)
-    return -1;
+  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
 
-  if (tp_file->priv->transferred_bytes == tp_file->priv->size)
-    return 0;
+  priv = GET_PRIV (tp_file);
 
-  return tp_file->priv->remaining_time;
+  return priv->incoming;
 }
 
 /**
- * empathy_tp_file_get_speed:
- * @tp_file: a #EmpathyTpFile
- *
- * Get the current speed of the transfer, in bytes per seconds.
+ * empathy_tp_file_cancel:
+ * @tp_file: an #EmpathyTpFile
  *
- * Returns: The current speed.
- **/
-gdouble
-empathy_tp_file_get_speed (EmpathyTpFile *tp_file)
-{
-  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), 0);
-
-  if (tp_file->priv->transferred_bytes == tp_file->priv->size)
-    return 0;
-
-  return tp_file->priv->speed;
-}
-
-const gchar *
-empathy_tp_file_get_content_type (EmpathyTpFile *tp_file)
-{
-  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
-  return tp_file->priv->content_type;
-}
-
+ * 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)
 {
   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
 
-  DEBUG ("Closing channel..");
-  tp_cli_channel_call_close (tp_file->priv->channel, -1,
-    NULL, NULL, NULL, NULL);
-
-  if (tp_file->priv->cancellable != NULL)
-    g_cancellable_cancel (tp_file->priv->cancellable);
+  close_channel_internal (tp_file, TRUE);
 }
 
+/**
+ * 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)
 {
-  empathy_tp_file_cancel (tp_file);
-}
-
-gboolean
-empathy_tp_file_is_ready (EmpathyTpFile *tp_file)
-{
-  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
-
-  return tp_file->priv->ready;
-}
-
-static void
-empathy_tp_file_class_init (EmpathyTpFileClass *klass)
-{
-  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_CHANNEL,
-      g_param_spec_object ("channel",
-          "telepathy channel",
-          "The file transfer channel",
-          TP_TYPE_CHANNEL,
-          G_PARAM_READWRITE |
-          G_PARAM_CONSTRUCT_ONLY));
-
-  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));
-
-  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));
-
-  g_object_class_install_property (object_class,
-      PROP_READY,
-      g_param_spec_boolean ("ready",
-          "ready",
-          "Whether the object is ready",
-          FALSE,
-          G_PARAM_READABLE));
-
-  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));
-
-  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));
-
-  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));
-
-  g_object_class_install_property (object_class,
-      PROP_CONTENT_HASH_TYPE,
-      g_param_spec_uint ("content-hash-type",
-          "file transfer hash type",
-          "The type of the file transfer hash",
-          0,
-          G_MAXUINT,
-          0,
-          G_PARAM_READWRITE));
-
-  g_object_class_install_property (object_class,
-      PROP_CONTENT_HASH,
-      g_param_spec_string ("content-hash",
-          "file transfer hash",
-          "The hash of the transfer's contents",
-          "",
-          G_PARAM_READWRITE));
-
-  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));
-
-  signals[REFRESH] = g_signal_new ("refresh", G_TYPE_FROM_CLASS (klass),
-      G_SIGNAL_RUN_LAST, 0, NULL, NULL,
-      g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
+  g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
 
-  g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
+  close_channel_internal (tp_file, FALSE);
 }
-