]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-tp-file.c
tp-file: ready property has to be read only
[empathy.git] / libempathy / empathy-tp-file.c
index de07d496b9f1d337889286b5c7728815f96927e7..6796f5a0bf82e044ba12799de1c15d7f1f1c63e9 100644 (file)
 #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-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_send_file()
+ * @see_also: #EmpathyTpFile, #EmpathyContact, empathy_dispatcher_send_file()
  * @include: libempthy/empathy-tp-file.h
  *
  * The #EmpathyTpFile object represents a Telepathy file channel.
@@ -83,32 +82,36 @@ typedef struct {
   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
-free_copy_data_if_closed (CopyData *copy)
+copy_data_unref (CopyData *copy)
 {
-  gint i;
+  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;
+      /* 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);
+      if (copy->in != NULL)
+        g_object_unref (copy->in);
 
-  if (copy->out != NULL)
-    g_object_unref (copy->out);
+      if (copy->out != NULL)
+        g_object_unref (copy->out);
 
-  for (i = 0; i < N_BUFFERS; i++)
-    g_free (copy->buff[i]);
+      for (i = 0; i < N_BUFFERS; i++)
+        g_free (copy->buff[i]);
 
-  g_object_unref (copy->cancellable);
-  g_free (copy);
+      g_object_unref (copy->cancellable);
+      g_free (copy);
+    }
 }
 
 static void
@@ -130,7 +133,7 @@ io_error (CopyData *copy,
   if (copy->out != NULL)
     g_output_stream_close (copy->out, NULL, NULL);
 
-  free_copy_data_if_closed (copy);
+  copy_data_unref (copy);
 }
 
 static void
@@ -141,7 +144,7 @@ close_done (GObject *source_object,
   CopyData *copy = user_data;
 
   g_object_unref (source_object);
-  free_copy_data_if_closed (copy);
+  copy_data_unref (copy);
 }
 
 static void
@@ -258,6 +261,7 @@ copy_stream (GInputStream *in,
   copy = g_new0 (CopyData, 1);
   copy->in = g_object_ref (in);
   copy->out = g_object_ref (out);
+  copy->ref_count = 1;
 
   if (cancellable != NULL)
     copy->cancellable = g_object_ref (cancellable);
@@ -273,26 +277,29 @@ copy_stream (GInputStream *in,
 /* EmpathyTpFile object */
 
 struct _EmpathyTpFilePriv {
-  EmpathyContactFactory *factory;
-  gchar *id;
+  EmpathyTpContactFactory *factory;
   MissionControl *mc;
   TpChannel *channel;
+  gboolean ready;
 
   EmpathyContact *contact;
   GInputStream *in_stream;
   GOutputStream *out_stream;
-  gboolean incoming;
+
+  /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
+  TpFileTransferState state;
+  gchar *content_type;
   gchar *filename;
-  EmpFileTransferState state;
-  EmpFileTransferStateChangeReason state_change_reason;
   guint64 size;
-  guint64 transferred_bytes;
-  time_t start_time;
-  gchar *unix_socket_path;
+  TpFileHashType content_hash_type;
   gchar *content_hash;
-  EmpFileHashType content_hash_type;
-  gchar *content_type;
   gchar *description;
+  guint64 transferred_bytes;
+
+  gboolean incoming;
+  TpFileTransferStateChangeReason state_change_reason;
+  time_t start_time;
+  GValue *socket_address;
   GCancellable *cancellable;
 };
 
@@ -301,13 +308,13 @@ enum {
   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,
-  PROP_IN_STREAM,
 };
 
 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
@@ -324,30 +331,36 @@ empathy_tp_file_init (EmpathyTpFile *tp_file)
 }
 
 static void
-tp_file_destroy_cb (TpChannel *file_channel,
-                    EmpathyTpFile *tp_file)
+tp_file_invalidated_cb (TpProxy       *proxy,
+                       guint          domain,
+                       gint           code,
+                       gchar         *message,
+                       EmpathyTpFile *tp_file)
 {
-  DEBUG ("Channel Closed or CM crashed");
+  DEBUG ("Channel invalidated: %s", message);
 
-  g_object_unref (tp_file->priv->channel);
-  tp_file->priv->channel = NULL;
+  if (tp_file->priv->state != TP_FILE_TRANSFER_STATE_COMPLETED &&
+      tp_file->priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
+    {
+      /* 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");
+    }
 }
 
 static void
 tp_file_finalize (GObject *object)
 {
-  EmpathyTpFile *tp_file;
-
-  tp_file = EMPATHY_TP_FILE (object);
+  EmpathyTpFile *tp_file = EMPATHY_TP_FILE (object);
 
   if (tp_file->priv->channel)
     {
-      DEBUG ("Closing channel..");
       g_signal_handlers_disconnect_by_func (tp_file->priv->channel,
-          tp_file_destroy_cb, object);
-      tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL,
-          NULL, NULL);
+          tp_file_invalidated_cb, object);
       g_object_unref (tp_file->priv->channel);
+      tp_file->priv->channel = NULL;
     }
 
   if (tp_file->priv->factory)
@@ -359,9 +372,9 @@ tp_file_finalize (GObject *object)
       g_object_unref (tp_file->priv->mc);
     }
 
-  g_free (tp_file->priv->id);
   g_free (tp_file->priv->filename);
-  g_free (tp_file->priv->unix_socket_path);
+  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);
@@ -382,119 +395,84 @@ tp_file_finalize (GObject *object)
 }
 
 static void
-tp_file_closed_cb (TpChannel *file_channel,
-                   EmpathyTpFile *tp_file,
-                   GObject *weak_object)
-{
-  /* The channel is closed, do just like if the proxy was destroyed */
-  g_signal_handlers_disconnect_by_func (tp_file->priv->channel,
-      tp_file_destroy_cb,
-      tp_file);
-  tp_file_destroy_cb (file_channel, tp_file);
-}
-
-static gint
-_get_local_socket (EmpathyTpFile *tp_file)
+tp_file_start_transfer (EmpathyTpFile *tp_file)
 {
   gint fd;
-  size_t path_len;
   struct sockaddr_un addr;
-
-  if (G_STR_EMPTY (tp_file->priv->unix_socket_path))
-    return -1;
+  GArray *array;
 
   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 (tp_file->priv->unix_socket_path);
-  strncpy (addr.sun_path, tp_file->priv->unix_socket_path, path_len);
-
-  if (connect (fd, (struct sockaddr*) &addr,
-      sizeof (addr)) < 0)
     {
-      close (fd);
-      return -1;
+      DEBUG ("Failed to create socket, closing channel");
+      empathy_tp_file_cancel (tp_file);
+      return;
     }
 
-  return fd;
-}
-
-static void
-send_tp_file (EmpathyTpFile *tp_file)
-{
-  gint socket_fd;
-  GOutputStream *socket_stream;
+  array = g_value_get_boxed (tp_file->priv->socket_address);
 
-  DEBUG ("Sending file content: filename=%s",
-      tp_file->priv->filename);
-
-  g_return_if_fail (tp_file->priv->in_stream);
+  memset (&addr, 0, sizeof (addr));
+  addr.sun_family = AF_UNIX;
+  strncpy (addr.sun_path, array->data, array->len);
 
-  socket_fd = _get_local_socket (tp_file);
-  if (socket_fd < 0)
+  if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
     {
-      DEBUG ("failed to get local socket fd");
+      DEBUG ("Failed to connect socket, closing channel");
+      empathy_tp_file_cancel (tp_file);
+      close (fd);
       return;
     }
-  DEBUG ("got local socket fd");
-  socket_stream = g_unix_output_stream_new (socket_fd, TRUE);
-
-  tp_file->priv->cancellable = g_cancellable_new ();
 
-  copy_stream (tp_file->priv->in_stream, socket_stream,
-      tp_file->priv->cancellable);
-
-  g_object_unref (socket_stream);
-}
-
-static void
-receive_tp_file (EmpathyTpFile *tp_file)
-{
-  GInputStream *socket_stream;
-  gint socket_fd;
-
-  socket_fd = _get_local_socket (tp_file);
-
-  if (socket_fd < 0)
-    return;
-
-  socket_stream = g_unix_input_stream_new (socket_fd, TRUE);
+  DEBUG ("Start the transfer");
 
+  tp_file->priv->start_time = empathy_time_get_current ();
   tp_file->priv->cancellable = g_cancellable_new ();
+  if (tp_file->priv->incoming)
+    {
+      GInputStream *socket_stream;
 
-  copy_stream (socket_stream, tp_file->priv->out_stream,
-      tp_file->priv->cancellable);
+      socket_stream = g_unix_input_stream_new (fd, TRUE);
+      copy_stream (socket_stream, tp_file->priv->out_stream,
+          tp_file->priv->cancellable);
+      g_object_unref (socket_stream);
+    }
+  else
+    {
+      GOutputStream *socket_stream;
 
-  g_object_unref (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_object_unref (socket_stream);
+    }
 }
 
 static void
-tp_file_state_changed_cb (DBusGProxy *tp_file_iface,
-                          EmpFileTransferState state,
-                          EmpFileTransferStateChangeReason reason,
-                          EmpathyTpFile *tp_file)
+tp_file_state_changed_cb (TpChannel *channel,
+                          guint state,
+                          guint reason,
+                          gpointer user_data,
+                          GObject *weak_object)
 {
-  DEBUG ("File transfer state changed: filename=%s, "
-      "old state=%u, state=%u, reason=%u",
-      tp_file->priv->filename, tp_file->priv->state, state, reason);
+  EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
 
-  if (state == EMP_FILE_TRANSFER_STATE_OPEN)
-    tp_file->priv->start_time = empathy_time_get_current ();
+  if (state == tp_file->priv->state)
+    return;
 
-  DEBUG ("state = %u, incoming = %s, in_stream = %s, out_stream = %s",
-      state, tp_file->priv->incoming ? "yes" : "no",
+  DEBUG ("File transfer state changed:\n"
+      "\tfilename = %s, 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");
 
-  if (state == EMP_FILE_TRANSFER_STATE_OPEN && !tp_file->priv->incoming &&
-      tp_file->priv->in_stream)
-    send_tp_file (tp_file);
-  else if (state == EMP_FILE_TRANSFER_STATE_OPEN && tp_file->priv->incoming &&
-      tp_file->priv->out_stream)
-    receive_tp_file (tp_file);
+  /* 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 &&
+      tp_file->priv->socket_address != NULL)
+    tp_file_start_transfer (tp_file);
 
   tp_file->priv->state = state;
   tp_file->priv->state_change_reason = reason;
@@ -503,92 +481,168 @@ tp_file_state_changed_cb (DBusGProxy *tp_file_iface,
 }
 
 static void
-tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
+tp_file_transferred_bytes_changed_cb (TpChannel *channel,
                                       guint64 count,
-                                      EmpathyTpFile *tp_file,
+                                      gpointer user_data,
                                       GObject *weak_object)
 {
+  EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
+
   if (tp_file->priv->transferred_bytes == count)
     return;
 
   tp_file->priv->transferred_bytes = count;
-
   g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
 }
 
-static GObject *
-tp_file_constructor (GType type,
-                     guint n_props,
-                     GObjectConstructParam *props)
+static void
+tp_file_check_if_ready (EmpathyTpFile *tp_file)
 {
-  GObject *file_obj;
-  EmpathyTpFile *tp_file;
-  TpHandle handle;
-  GHashTable *properties;
-  McAccount *account;
-
-  file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
-      n_props, props);
-
-  tp_file = EMPATHY_TP_FILE (file_obj);
-
-  tp_file->priv->factory = empathy_contact_factory_new ();
-  tp_file->priv->mc = empathy_mission_control_new ();
+  if (tp_file->priv->ready || tp_file->priv->contact == NULL ||
+      tp_file->priv->state == 0)
+    return;
 
-  tp_cli_channel_connect_to_closed (tp_file->priv->channel,
-      (tp_cli_channel_signal_callback_closed) tp_file_closed_cb,
-      tp_file,
-      NULL, NULL, NULL);
+  tp_file->priv->ready = TRUE;
+  g_object_notify (G_OBJECT (tp_file), "ready");
+}
 
-  emp_cli_channel_type_file_connect_to_file_transfer_state_changed (
-      TP_PROXY (tp_file->priv->channel),
-      (emp_cli_channel_type_file_signal_callback_file_transfer_state_changed)
-          tp_file_state_changed_cb,
-      tp_file,
-      NULL, NULL, NULL);
+static void
+tp_file_got_contact_cb (EmpathyTpContactFactory *factory,
+                        EmpathyContact *contact,
+                        const GError *error,
+                        gpointer user_data,
+                        GObject *weak_object)
+{
+  EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
 
-  emp_cli_channel_type_file_connect_to_transferred_bytes_changed (
-      TP_PROXY (tp_file->priv->channel),
-      (emp_cli_channel_type_file_signal_callback_transferred_bytes_changed)
-          tp_file_transferred_bytes_changed_cb,
-      tp_file,
-      NULL, NULL, NULL);
+  if (error)
+    {
+      DEBUG ("Error: %s", error->message);
+      empathy_tp_file_close (tp_file);
+      return;
+    }
 
-  account = empathy_channel_get_account (tp_file->priv->channel);
+  tp_file->priv->contact = g_object_ref (contact);
+  tp_file_check_if_ready (tp_file);
+}
 
-  handle = tp_channel_get_handle (tp_file->priv->channel, NULL);
-  tp_file->priv->contact = empathy_contact_factory_get_from_handle (
-      tp_file->priv->factory, account, (guint) handle);
+static void
+tp_file_get_all_cb (TpProxy *proxy,
+                    GHashTable *properties,
+                    const GError *error,
+                    gpointer user_data,
+                    GObject *file_obj)
+{
+  EmpathyTpFile *tp_file = EMPATHY_TP_FILE (file_obj);
 
-  tp_cli_dbus_properties_run_get_all (tp_file->priv->channel,
-      -1, EMP_IFACE_CHANNEL_TYPE_FILE, &properties, NULL, NULL);
+  if (error)
+    {
+      DEBUG ("Error: %s", error->message);
+      tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL,
+          NULL);
+      return;
+    }
 
   tp_file->priv->size = g_value_get_uint64 (
       g_hash_table_lookup (properties, "Size"));
+  g_object_notify (file_obj, "size");
 
   tp_file->priv->state = g_value_get_uint (
       g_hash_table_lookup (properties, "State"));
-
-  tp_file->priv->state_change_reason =
-      EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
+  g_object_notify (file_obj, "state");
 
   tp_file->priv->transferred_bytes = g_value_get_uint64 (
       g_hash_table_lookup (properties, "TransferredBytes"));
+  g_object_notify (file_obj, "transferred-bytes");
 
   tp_file->priv->filename = g_value_dup_string (
       g_hash_table_lookup (properties, "Filename"));
+  g_object_notify (file_obj, "filename");
 
   tp_file->priv->content_hash = g_value_dup_string (
       g_hash_table_lookup (properties, "ContentHash"));
+  g_object_notify (file_obj, "content-hash");
+
+  tp_file->priv->content_hash_type = g_value_get_uint (
+      g_hash_table_lookup (properties, "ContentHashType"));
+  g_object_notify (file_obj, "content-hash-type");
+
+  tp_file->priv->content_type = g_value_dup_string (
+      g_hash_table_lookup (properties, "ContentType"));
+  g_object_notify (file_obj, "content-type");
 
   tp_file->priv->description = g_value_dup_string (
       g_hash_table_lookup (properties, "Description"));
 
-  if (tp_file->priv->state == EMP_FILE_TRANSFER_STATE_LOCAL_PENDING)
-    tp_file->priv->incoming = TRUE;
+  tp_file_check_if_ready (tp_file);
+}
+
+static void
+tp_file_get_requested_cb (TpProxy *proxy,
+                          const GValue *requested,
+                          const GError *error,
+                          gpointer user_data,
+                          GObject *weak_object)
+{
+  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;
+    }
+
+  tp_file->priv->incoming = !g_value_get_boolean (requested);
+  g_object_notify (G_OBJECT (tp_file), "incoming");
 
-  g_hash_table_destroy (properties);
-  g_object_unref (account);
+  tp_file_check_if_ready (tp_file);
+}
+
+static GObject *
+tp_file_constructor (GType type,
+                     guint n_props,
+                     GObjectConstructParam *props)
+{
+  GObject *file_obj;
+  EmpathyTpFile *tp_file;
+  TpHandle handle;
+  TpConnection *connection;
+
+  file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
+      n_props, props);
+
+  tp_file = EMPATHY_TP_FILE (file_obj);
+
+  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;
+
+  g_signal_connect (tp_file->priv->channel, "invalidated",
+    G_CALLBACK (tp_file_invalidated_cb), tp_file);
+
+  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);
+
+  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);
+
+  tp_cli_dbus_properties_call_get (tp_file->priv->channel, -1,
+      TP_IFACE_CHANNEL, "Requested",
+      tp_file_get_requested_cb, NULL, NULL, file_obj);
+
+  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);
+
+  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);
 
   return file_obj;
 }
@@ -608,6 +662,33 @@ tp_file_get_property (GObject *object,
       case PROP_CHANNEL:
         g_value_set_object (value, tp_file->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);
+        break;
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
         break;
@@ -621,11 +702,10 @@ tp_file_channel_set_dbus_property (gpointer proxy,
 {
         DEBUG ("Setting %s property", property);
         tp_cli_dbus_properties_call_set (TP_PROXY (proxy), -1,
-            EMP_IFACE_CHANNEL_TYPE_FILE, property, value,
+            TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, property, value,
             NULL, NULL, NULL, NULL);
 }
 
-
 static void
 tp_file_set_property (GObject *object,
                       guint param_id,
@@ -667,49 +747,56 @@ tp_file_set_property (GObject *object,
         g_free (tp_file->priv->content_hash);
         tp_file->priv->content_hash = g_value_dup_string (value);
         break;
-      case PROP_IN_STREAM:
-        if (tp_file->priv->in_stream)
-          g_object_unref (tp_file->priv->in_stream);
-        tp_file->priv->in_stream = g_object_ref (g_value_get_object (value));
-        break;
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
         break;
     };
 }
 
+static GHashTable *ft_table = NULL;
+
+static void
+tp_file_weak_notify_cb (gpointer channel,
+                        GObject *tp_file)
+{
+  g_hash_table_remove (ft_table, channel);
+}
+
 /**
  * empathy_tp_file_new:
  * @channel: a Telepathy channel
  *
- * Creates a new #EmpathyTpFile wrapping @channel.
+ * Creates a new #EmpathyTpFile wrapping @channel, or return a new ref to an
+ * existing #EmpathyTpFile for that channel.
  *
  * Returns: a new #EmpathyTpFile
  */
 EmpathyTpFile *
 empathy_tp_file_new (TpChannel *channel)
 {
+  EmpathyTpFile *tp_file;
+
   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
 
-  return g_object_new (EMPATHY_TYPE_TP_FILE,
+  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);
-}
 
-/**
- * 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)
-{
-  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), 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->priv->id;
+  return tp_file;
 }
 
 /**
@@ -729,77 +816,122 @@ empathy_tp_file_get_channel (EmpathyTpFile *tp_file)
 }
 
 static void
-tp_file_method_cb (TpProxy *proxy,
+tp_file_method_cb (TpChannel *channel,
                    const GValue *address,
                    const GError *error,
                    gpointer user_data,
                    GObject *weak_object)
 {
-  EmpathyTpFile *tp_file = (EmpathyTpFile *) user_data;
+  EmpathyTpFile *tp_file = (EmpathyTpFile *) weak_object;
+  GArray *array;
 
   if (error)
     {
       DEBUG ("Error: %s", error->message);
+      empathy_tp_file_cancel (tp_file);
       return;
     }
 
-  if (tp_file->priv->unix_socket_path)
-    g_free (tp_file->priv->unix_socket_path);
+  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;
 
-  tp_file->priv->unix_socket_path = g_value_dup_string (address);
+      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));
 
-  DEBUG ("Got unix socket path: %s", tp_file->priv->unix_socket_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
  *
  * Accepts a file transfer that's in the "local pending" state (i.e.
- * EMP_FILE_TRANSFER_STATE_LOCAL_PENDING).
+ * TP_FILE_TRANSFER_STATE_LOCAL_PENDING).
  */
 void
 empathy_tp_file_accept (EmpathyTpFile *tp_file,
-                        guint64 offset)
+                        guint64 offset,
+                        GFile *gfile,
+                        GError **error)
 {
   GValue nothing = { 0 };
 
   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
+  g_return_if_fail (G_IS_FILE (gfile));
+
+  tp_file->priv->out_stream = G_OUTPUT_STREAM (g_file_replace (gfile, NULL,
+       FALSE, 0, NULL, error));
+  if (error && *error)
+    return;
 
-  g_return_if_fail (tp_file->priv->out_stream != NULL);
+  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);
 
   g_value_init (&nothing, G_TYPE_STRING);
-  g_value_set_string (&nothing, "");
+  g_value_set_static_string (&nothing, "");
 
-  emp_cli_channel_type_file_call_accept_file (TP_PROXY (tp_file->priv->channel),
+  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, tp_file, NULL, NULL);
+      &nothing, offset, tp_file_method_cb, NULL, NULL, G_OBJECT (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
  *
  * Offers a file transfer that's in the "not offered" state (i.e.
- * EMP_FILE_TRANSFER_STATE_NOT_OFFERED).
+ * TP_FILE_TRANSFER_STATE_NOT_OFFERED).
  */
 void
-empathy_tp_file_offer (EmpathyTpFile *tp_file)
+empathy_tp_file_offer (EmpathyTpFile *tp_file, GFile *gfile, GError **error)
 {
   GValue nothing = { 0 };
 
   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
 
+  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_string (&nothing, "");
+  g_value_set_static_string (&nothing, "");
 
-  emp_cli_channel_type_file_call_offer_file (
-      TP_PROXY (tp_file->priv->channel), -1,
-      TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
-      &nothing, tp_file_method_cb, tp_file, NULL, NULL);
+  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 *
@@ -809,20 +941,6 @@ empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
   return tp_file->priv->contact;
 }
 
-GInputStream *
-empathy_tp_file_get_input_stream (EmpathyTpFile *tp_file)
-{
-  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
-  return tp_file->priv->in_stream;
-}
-
-GOutputStream *
-empathy_tp_file_get_output_stream (EmpathyTpFile *tp_file)
-{
-  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
-  return tp_file->priv->out_stream;
-}
-
 const gchar *
 empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
 {
@@ -831,27 +949,23 @@ empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
 }
 
 gboolean
-empathy_tp_file_get_incoming (EmpathyTpFile *tp_file)
+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;
 }
 
-EmpFileTransferState
-empathy_tp_file_get_state (EmpathyTpFile *tp_file)
+TpFileTransferState
+empathy_tp_file_get_state (EmpathyTpFile *tp_file,
+                           TpFileTransferStateChangeReason *reason)
 {
   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
-      EMP_FILE_TRANSFER_STATE_NONE);
-  return tp_file->priv->state;
-}
+      TP_FILE_TRANSFER_STATE_NONE);
 
-EmpFileTransferStateChangeReason
-empathy_tp_file_get_state_change_reason (EmpathyTpFile *tp_file)
-{
-  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
-      EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE);
+  if (reason != NULL)
+    *reason = tp_file->priv->state_change_reason;
 
-  return tp_file->priv->state_change_reason;
+  return tp_file->priv->state;
 }
 
 guint64
@@ -894,79 +1008,38 @@ empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
   return (gint) remaining_time;
 }
 
-void
-empathy_tp_file_cancel (EmpathyTpFile *tp_file)
+const gchar *
+empathy_tp_file_get_content_type (EmpathyTpFile *tp_file)
 {
-  g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
-
-  tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL, NULL);
-
-  g_cancellable_cancel (tp_file->priv->cancellable);
+  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
+  return tp_file->priv->content_type;
 }
 
 void
-empathy_tp_file_set_input_stream (EmpathyTpFile *tp_file,
-                                  GInputStream *in_stream)
+empathy_tp_file_cancel (EmpathyTpFile *tp_file)
 {
   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
-  g_return_if_fail (G_IS_INPUT_STREAM (in_stream));
 
-  if (tp_file->priv->in_stream == in_stream)
-    return;
-
-  if (tp_file->priv->incoming)
-    g_warning ("Setting an input stream for incoming file "
-         "transfers is useless");
+  DEBUG ("Closing channel..");
+  tp_cli_channel_call_close (tp_file->priv->channel, -1,
+    NULL, NULL, NULL, NULL);
 
-  if (tp_file->priv->in_stream)
-    g_object_unref (tp_file->priv->in_stream);
-
-  if (in_stream)
-    g_object_ref (in_stream);
-
-  tp_file->priv->in_stream = in_stream;
-
-  g_object_notify (G_OBJECT (tp_file), "in-stream");
+  if (tp_file->priv->cancellable != NULL)
+    g_cancellable_cancel (tp_file->priv->cancellable);
 }
 
 void
-empathy_tp_file_set_output_stream (EmpathyTpFile *tp_file,
-                                   GOutputStream *out_stream)
+empathy_tp_file_close (EmpathyTpFile *tp_file)
 {
-  g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
-  g_return_if_fail (G_IS_OUTPUT_STREAM (out_stream));
-
-  if (tp_file->priv->out_stream == out_stream)
-    return;
-
-  if (!tp_file->priv->incoming)
-    g_warning ("Setting an output stream for outgoing file "
-         "transfers is useless");
-
-  if (tp_file->priv->out_stream)
-    g_object_unref (tp_file->priv->out_stream);
-
-  if (out_stream)
-    g_object_ref (out_stream);
-
-  tp_file->priv->out_stream = out_stream;
+  empathy_tp_file_cancel (tp_file);
 }
 
-void
-empathy_tp_file_set_filename (EmpathyTpFile *tp_file,
-                              const gchar *filename)
+gboolean
+empathy_tp_file_is_ready (EmpathyTpFile *tp_file)
 {
-  g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
-  g_return_if_fail (filename != NULL);
-
-  if (tp_file->priv->filename && strcmp (filename,
-      tp_file->priv->filename) == 0)
-    return;
-
-  g_free (tp_file->priv->filename);
-  tp_file->priv->filename = g_strdup (filename);
+  g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
 
-  g_object_notify (G_OBJECT (tp_file), "filename");
+  return tp_file->priv->ready;
 }
 
 static void
@@ -1009,6 +1082,14 @@ empathy_tp_file_class_init (EmpathyTpFileClass *klass)
           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",
@@ -1063,13 +1144,6 @@ empathy_tp_file_class_init (EmpathyTpFileClass *klass)
           0,
           G_PARAM_READWRITE));
 
-  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));
-
   g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
 }
+