]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-ft-handler.c
Refuse sending empty or special files (directories, char/block devices, etc.)
[empathy.git] / libempathy / empathy-ft-handler.c
index f9b4a87344a854604acb909776ffaece88a68718..d24467b24e329c87be2143d4e9ef1d612b698493 100644 (file)
  *
  * Author: Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
  */
+
 /* empathy-ft-handler.c */
 
 #include <glib.h>
 #include <glib/gi18n.h>
 #include <telepathy-glib/util.h>
+#include <telepathy-glib/dbus.h>
 
 #include "empathy-ft-handler.h"
 #include "empathy-tp-contact-factory.h"
 #define DEBUG_FLAG EMPATHY_DEBUG_FT
 #include "empathy-debug.h"
 
+/**
+ * SECTION:empathy-ft-handler
+ * @title: EmpathyFTHandler
+ * @short_description: an object representing a File Transfer
+ * @include: libempathy/empathy-ft-handler
+ *
+ * #EmpathyFTHandler is the object which represents a File Transfer with all
+ * its properties.
+ * The creation of an #EmpathyFTHandler is done with
+ * empathy_ft_handler_new_outgoing() or empathy_ft_handler_new_incoming(),
+ * even though clients should not need to call them directly, as
+ * #EmpathyFTFactory does it for them. Remember that for the file transfer
+ * to work with an incoming handler,
+ * empathy_ft_handler_incoming_set_destination() should be called after
+ * empathy_ft_handler_new_incoming(). #EmpathyFTFactory does this
+ * automatically.
+ * It's important to note that, as the creation of the handlers is async, once
+ * an handler is created, it already has all the interesting properties set,
+ * like filename, total bytes, content type and so on, making it useful
+ * to be displayed in an UI.
+ * The transfer API works like a state machine; it has three signals,
+ * ::transfer-started, ::transfer-progress, ::transfer-done, which will be
+ * emitted in the relevant phases.
+ * In addition, if the handler is created with checksumming enabled,
+ * other three signals (::hashing-started, ::hashing-progress, ::hashing-done)
+ * will be emitted before or after the transfer, depending on the direction
+ * (respectively outgoing and incoming) of the handler.
+ * At any time between the call to empathy_ft_handler_start_transfer() and
+ * the last signal, a ::transfer-error can be emitted, indicating that an
+ * error has happened in the operation. The message of the error is localized
+ * to use in an UI.
+ */
+
 G_DEFINE_TYPE (EmpathyFTHandler, empathy_ft_handler, G_TYPE_OBJECT)
 
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyFTHandler)
@@ -45,7 +79,12 @@ enum {
   PROP_TP_FILE = 1,
   PROP_G_FILE,
   PROP_CONTACT,
-  PROP_USE_HASH
+  PROP_CONTENT_TYPE,
+  PROP_DESCRIPTION,
+  PROP_FILENAME,
+  PROP_MODIFICATION_TIME,
+  PROP_TOTAL_BYTES,
+  PROP_TRANSFERRED_BYTES
 };
 
 enum {
@@ -61,7 +100,7 @@ enum {
 
 typedef struct {
   GInputStream *stream;
-  GError *error;
+  GError *error /* comment to make the style checker happy */;
   guchar *buffer;
   GChecksum *checksum;
   gssize total_read;
@@ -84,6 +123,8 @@ typedef struct {
   GCancellable *cancellable;
   gboolean use_hash;
 
+  EmpathyDispatcher *dispatcher;
+
   /* request for the new transfer */
   GHashTable *request;
 
@@ -97,7 +138,6 @@ typedef struct {
   guint64 mtime;
   gchar *content_hash;
   TpFileHashType content_hash_type;
-  TpFileTransferState current_state;
 
   /* time and speed */
   gdouble speed;
@@ -126,15 +166,30 @@ do_get_property (GObject *object,
       case PROP_CONTACT:
         g_value_set_object (value, priv->contact);
         break;
+      case PROP_CONTENT_TYPE:
+        g_value_set_string (value, priv->content_type);
+        break;
+      case PROP_DESCRIPTION:
+        g_value_set_string (value, priv->description);
+        break;
+      case PROP_FILENAME:
+        g_value_set_string (value, priv->filename);
+        break;
+      case PROP_MODIFICATION_TIME:
+        g_value_set_uint64 (value, priv->mtime);
+        break;
+      case PROP_TOTAL_BYTES:
+        g_value_set_uint64 (value, priv->total_bytes);
+        break;
+      case PROP_TRANSFERRED_BYTES:
+        g_value_set_uint64 (value, priv->transferred_bytes);
+        break;
       case PROP_G_FILE:
         g_value_set_object (value, priv->gfile);
         break;
       case PROP_TP_FILE:
         g_value_set_object (value, priv->tpfile);
         break;
-      case PROP_USE_HASH:
-        g_value_set_boolean (value, priv->use_hash);
-        break;
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
     }
@@ -142,7 +197,7 @@ do_get_property (GObject *object,
 
 static void
 do_set_property (GObject *object,
-    guint property_id, 
+    guint property_id,
     const GValue *value,
     GParamSpec *pspec)
 {
@@ -153,15 +208,30 @@ do_set_property (GObject *object,
       case PROP_CONTACT:
         priv->contact = g_value_dup_object (value);
         break;
+      case PROP_CONTENT_TYPE:
+        priv->content_type = g_value_dup_string (value);
+        break;
+      case PROP_DESCRIPTION:
+        priv->description = g_value_dup_string (value);
+        break;
+      case PROP_FILENAME:
+        priv->filename = g_value_dup_string (value);
+        break;
+      case PROP_MODIFICATION_TIME:
+        priv->mtime = g_value_get_uint64 (value);
+        break;
+      case PROP_TOTAL_BYTES:
+        priv->total_bytes = g_value_get_uint64 (value);
+        break;
+      case PROP_TRANSFERRED_BYTES:
+        priv->transferred_bytes = g_value_get_uint64 (value);
+        break;
       case PROP_G_FILE:
         priv->gfile = g_value_dup_object (value);
         break;
       case PROP_TP_FILE:
         priv->tpfile = g_value_dup_object (value);
         break;
-      case PROP_USE_HASH:
-        priv->use_hash = g_value_get_boolean (value);
-        break;
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
     }
@@ -177,23 +247,23 @@ do_dispose (GObject *object)
 
   priv->dispose_run = TRUE;
 
-  if (priv->contact) {
+  if (priv->contact != NULL) {
     g_object_unref (priv->contact);
     priv->contact = NULL;
   }
 
-  if (priv->gfile) {
+  if (priv->gfile != NULL) {
     g_object_unref (priv->gfile);
     priv->gfile = NULL;
   }
 
-  if (priv->tpfile) {
+  if (priv->tpfile != NULL) {
     empathy_tp_file_close (priv->tpfile);
     g_object_unref (priv->tpfile);
     priv->tpfile = NULL;
   }
 
-  if (priv->cancellable) {
+  if (priv->cancellable != NULL) {
     g_object_unref (priv->cancellable);
     priv->cancellable = NULL;
   }
@@ -203,7 +273,13 @@ do_dispose (GObject *object)
       g_hash_table_unref (priv->request);
       priv->request = NULL;
     }
-  
+
+  if (priv->dispatcher != NULL)
+    {
+      g_object_unref (priv->dispatcher);
+      priv->dispatcher = NULL;
+    }
+
   G_OBJECT_CLASS (empathy_ft_handler_parent_class)->dispose (object);
 }
 
@@ -243,30 +319,115 @@ empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
   object_class->finalize = do_finalize;
 
   /* properties */
+
+  /**
+   * EmpathyFTHandler:contact:
+   *
+   * The remote #EmpathyContact for the transfer
+   */
   param_spec = g_param_spec_object ("contact",
     "contact", "The remote contact",
     EMPATHY_TYPE_CONTACT,
     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
   g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
 
+  /**
+   * EmpathyFTHandler:content-type:
+   *
+   * The content type of the file being transferred
+   */
+  param_spec = g_param_spec_string ("content-type",
+    "content-type", "The content type of the file", NULL,
+    G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+  g_object_class_install_property (object_class,
+      PROP_CONTENT_TYPE, param_spec);
+
+  /**
+   * EmpathyFTHandler:description:
+   *
+   * The description of the file being transferred
+   */
+  param_spec = g_param_spec_string ("description",
+    "description", "The description of the file", NULL,
+    G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+  g_object_class_install_property (object_class,
+      PROP_DESCRIPTION, param_spec);
+
+  /**
+   * EmpathyFTHandler:filename:
+   *
+   * The name of the file being transferred
+   */
+  param_spec = g_param_spec_string ("filename",
+    "filename", "The name of the file", NULL,
+    G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+  g_object_class_install_property (object_class,
+      PROP_FILENAME, param_spec);
+
+  /**
+   * EmpathyFTHandler:modification-time:
+   *
+   * The modification time of the file being transferred
+   */
+  param_spec = g_param_spec_uint64 ("modification-time",
+    "modification-time", "The mtime of the file", 0,
+    G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+  g_object_class_install_property (object_class,
+      PROP_MODIFICATION_TIME, param_spec);
+
+  /**
+   * EmpathyFTHandler:total-bytes:
+   *
+   * The size (in bytes) of the file being transferred
+   */
+  param_spec = g_param_spec_uint64 ("total-bytes",
+    "total-bytes", "The size of the file", 0,
+    G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+  g_object_class_install_property (object_class,
+      PROP_TOTAL_BYTES, param_spec);
+
+  /**
+   * EmpathyFTHandler:transferred-bytes:
+   *
+   * The number of the bytes already transferred
+   */
+  param_spec = g_param_spec_uint64 ("transferred-bytes",
+    "transferred-bytes", "The number of bytes already transferred", 0,
+    G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+  g_object_class_install_property (object_class,
+      PROP_TRANSFERRED_BYTES, param_spec);
+
+  /**
+   * EmpathyFTHandler:gfile:
+   *
+   * The #GFile object where the transfer actually happens
+   */
   param_spec = g_param_spec_object ("gfile",
     "gfile", "The GFile we're handling",
     G_TYPE_FILE,
     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
   g_object_class_install_property (object_class, PROP_G_FILE, param_spec);
 
+  /**
+   * EmpathyFTHandler:tp-file:
+   *
+   * The underlying #EmpathyTpFile managing the transfer
+   */
   param_spec = g_param_spec_object ("tp-file",
     "tp-file", "The file's channel wrapper",
     EMPATHY_TYPE_TP_FILE,
     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
   g_object_class_install_property (object_class, PROP_TP_FILE, param_spec);
 
-  param_spec = g_param_spec_boolean ("use-hash",
-    "use-hash", "Whether we should use checksum when sending or receiving",
-    FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
-  g_object_class_install_property (object_class, PROP_USE_HASH, param_spec);
-
   /* signals */
+
+  /**
+   * EmpathyFTHandler::transfer-started
+   * @handler: the object which has received the signal
+   * @tp_file: the #EmpathyTpFile for which the transfer has started
+   *
+   * This signal is emitted when the actual transfer starts.
+   */
   signals[TRANSFER_STARTED] =
     g_signal_new ("transfer-started", G_TYPE_FROM_CLASS (klass),
         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
@@ -274,6 +435,14 @@ empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
         G_TYPE_NONE,
         1, EMPATHY_TYPE_TP_FILE);
 
+  /**
+   * EmpathyFTHandler::transfer-done
+   * @handler: the object which has received the signal
+   * @tp_file: the #EmpathyTpFile for which the transfer has started
+   *
+   * This signal will be emitted when the actual transfer is completed
+   * successfully.
+   */
   signals[TRANSFER_DONE] =
     g_signal_new ("transfer-done", G_TYPE_FROM_CLASS (klass),
         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
@@ -281,6 +450,17 @@ empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
         G_TYPE_NONE,
         1, EMPATHY_TYPE_TP_FILE);
 
+  /**
+   * EmpathyFTHandler::transfer-error
+   * @handler: the object which has received the signal
+   * @error: a #GError
+   *
+   * This signal can be emitted anytime between the call to
+   * empathy_ft_handler_start_transfer() and the last expected signal
+   * (::transfer-done or ::hashing-done), and it's guaranteed to be the last
+   * signal coming from the handler, meaning that no other operation will
+   * take place after this signal.
+   */
   signals[TRANSFER_ERROR] =
     g_signal_new ("transfer-error", G_TYPE_FROM_CLASS (klass),
         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
@@ -288,6 +468,18 @@ empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
         G_TYPE_NONE,
         1, G_TYPE_POINTER);
 
+  /**
+   * EmpathyFTHandler::transfer-progress
+   * @handler: the object which has received the signal
+   * @current_bytes: the bytes currently transferred
+   * @total_bytes: the total bytes of the handler
+   * @remaining_time: the number of seconds remaining for the transfer
+   * to be completed
+   * @speed: the current speed of the transfer (in KB/s)
+   *
+   * This signal is emitted to notify clients of the progress of the
+   * transfer.
+   */
   signals[TRANSFER_PROGRESS] =
     g_signal_new ("transfer-progress", G_TYPE_FROM_CLASS (klass),
         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
@@ -295,12 +487,32 @@ empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
         G_TYPE_NONE,
         4, G_TYPE_UINT64, G_TYPE_UINT64, G_TYPE_UINT, G_TYPE_DOUBLE);
 
+  /**
+   * EmpathyFTHandler::hashing-started
+   * @handler: the object which has received the signal
+   *
+   * This signal is emitted when the hashing operation of the handler
+   * is started. Note that this might happen or not, depending on the CM
+   * and remote contact capabilities. Clients shoud use
+   * empathy_ft_handler_get_use_hash() before calling
+   * empathy_ft_handler_start_transfer() to know whether they should connect
+   * to this signal.
+   */
   signals[HASHING_STARTED] =
     g_signal_new ("hashing-started", G_TYPE_FROM_CLASS (klass),
         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
         g_cclosure_marshal_VOID__VOID,
         G_TYPE_NONE, 0);
 
+  /**
+   * EmpathyFTHandler::hashing-progress
+   * @handler: the object which has received the signal
+   * @current_bytes: the bytes currently hashed
+   * @total_bytes: the total bytes of the handler
+   *
+   * This signal is emitted to notify clients of the progress of the
+   * hashing operation.
+   */
   signals[HASHING_PROGRESS] =
     g_signal_new ("hashing-progress", G_TYPE_FROM_CLASS (klass),
         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
@@ -308,6 +520,13 @@ empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
         G_TYPE_NONE,
         2, G_TYPE_UINT64, G_TYPE_UINT64);
 
+  /**
+   * EmpathyFTHandler::hashing-done
+   * @handler: the object which has received the signal
+   *
+   * This signal is emitted when the hashing operation of the handler
+   * is completed.
+   */
   signals[HASHING_DONE] =
     g_signal_new ("hashing-done", G_TYPE_FROM_CLASS (klass),
         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
@@ -323,6 +542,7 @@ empathy_ft_handler_init (EmpathyFTHandler *self)
 
   self->priv = priv;
   priv->cancellable = g_cancellable_new ();
+  priv->dispatcher = empathy_dispatcher_dup_singleton ();
 }
 
 /* private functions */
@@ -330,34 +550,19 @@ empathy_ft_handler_init (EmpathyFTHandler *self)
 static void
 hash_data_free (HashingData *data)
 {
-  if (data->buffer != NULL)
-    {
-      g_free (data->buffer);
-      data->buffer = NULL;
-    }
+  g_free (data->buffer);
 
   if (data->stream != NULL)
-    {
-      g_object_unref (data->stream);
-      data->stream = NULL;
-    }
+    g_object_unref (data->stream);
 
   if (data->checksum != NULL)
-    {
-      g_checksum_free (data->checksum);
-      data->checksum = NULL;
-    }
+    g_checksum_free (data->checksum);
 
   if (data->error != NULL)
-    {
-      g_error_free (data->error);
-      data->error = NULL;
-    }
+    g_error_free (data->error);
+
   if (data->handler != NULL)
-    {
-      g_object_unref (data->handler);
-      data->handler = NULL;
-    }
+    g_object_unref (data->handler);
 
   g_slice_free (HashingData, data);
 }
@@ -433,7 +638,7 @@ ft_transfer_operation_callback (EmpathyTpFile *tp_file,
     {
       emit_error_signal (handler, error);
     }
-  else 
+  else
     {
       priv->is_completed = TRUE;
       g_signal_emit (handler, signals[TRANSFER_DONE], 0, tp_file);
@@ -467,7 +672,7 @@ update_remaining_time_and_speed (EmpathyFTHandler *handler,
     {
       transferred = transferred_bytes - last_transferred_bytes;
       speed = (gdouble) transferred / (gdouble) elapsed_time;
-      remaining_time = (priv->total_bytes - transferred) / speed;
+      remaining_time = (priv->total_bytes - priv->transferred_bytes) / speed;
       priv->speed = speed;
       priv->remaining_time = remaining_time;
       priv->last_update_time = current_time;
@@ -540,44 +745,16 @@ ft_handler_create_channel_cb (EmpathyDispatchOperation *operation,
 static void
 ft_handler_push_to_dispatcher (EmpathyFTHandler *handler)
 {
-  EmpathyDispatcher *dispatcher;
   TpConnection *connection;
   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
 
   DEBUG ("Pushing request to the dispatcher");
 
-  dispatcher = empathy_dispatcher_dup_singleton ();
   connection = empathy_contact_get_connection (priv->contact);
 
   /* I want to own a reference to the request, and destroy it later */
-  empathy_dispatcher_create_channel (dispatcher, connection,
+  empathy_dispatcher_create_channel (priv->dispatcher, connection,
       g_hash_table_ref (priv->request), ft_handler_create_channel_cb, handler);
-
-  g_object_unref (dispatcher);
-}
-
-static gboolean
-ft_handler_check_if_allowed (EmpathyFTHandler *handler)
-{
-  EmpathyDispatcher *dispatcher;
-  EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
-  TpConnection *connection;
-  GStrv allowed;
-  gboolean res = TRUE;
-
-  dispatcher = empathy_dispatcher_dup_singleton ();
-  connection = empathy_contact_get_connection (priv->contact);
-
-  allowed = empathy_dispatcher_find_channel_class (dispatcher, connection,
-      TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT);
-
-  if (!tp_strv_contains ((const gchar * const *) allowed,
-      TP_IFACE_CHANNEL ".TargetHandle"))
-    res = FALSE;
-
-  g_object_unref (dispatcher);
-
-  return res;
 }
 
 static void
@@ -677,7 +854,7 @@ hash_job_done (gpointer user_data)
       value = tp_g_value_slice_new_string
           (g_checksum_get_string (hash_data->checksum));
       g_hash_table_insert (priv->request,
-          TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHash", value);      
+          TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHash", value);
     }
 
 cleanup:
@@ -753,7 +930,7 @@ again:
   }
 
 out:
-  if (error)
+  if (error != NULL)
     hash_data->error = error;
 
   g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
@@ -778,7 +955,7 @@ do_hash_job_incoming (GIOSchedulerJob *job,
   hash_data->stream =
     G_INPUT_STREAM (g_file_read (priv->gfile, cancellable, &error));
 
-  if (error)
+  if (error != NULL)
     {
       hash_data->error = error;
       g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done,
@@ -816,9 +993,7 @@ ft_handler_read_async_cb (GObject *source,
   hash_data->stream = G_INPUT_STREAM (stream);
   hash_data->total_bytes = priv->total_bytes;
   hash_data->handler = g_object_ref (handler);
-  /* FIXME: should look at the CM capabilities before setting the
-   * checksum type?
-   */
+  /* FIXME: MD5 is the only ContentHashType supported right now */
   hash_data->checksum = g_checksum_new (G_CHECKSUM_MD5);
 
   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHashType */
@@ -833,24 +1008,118 @@ ft_handler_read_async_cb (GObject *source,
 }
 
 static void
-ft_handler_complete_request (EmpathyFTHandler *handler)
-{ 
+callbacks_data_free (gpointer user_data)
+{
+  CallbacksData *data = user_data;
+
+  if (data->handler != NULL)
+    g_object_unref (data->handler);
+
+  g_slice_free (CallbacksData, data);
+}
+
+static void
+set_content_hash_type_from_classes (EmpathyFTHandler *handler,
+    GList *classes)
+{
+  GValueArray *class;
+  GValue *v;
+  GList *l;
+  GArray *possible_values;
+  guint value;
+  GHashTable *fprops;
+  gboolean valid;
+  EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
+
+  possible_values = g_array_new (TRUE, TRUE, sizeof (guint));
+
+  for (l = classes; l != NULL; l = l->next)
+    {
+      class = l->data;
+      v = g_value_array_get_nth (class, 0);
+      fprops = g_value_get_boxed (v);
+
+      value = tp_asv_get_uint32
+        (fprops, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType",
+         &valid);
+
+      if (valid)
+        g_array_append_val (possible_values, value);
+    }
+
+  if (possible_values->len == 0)
+    {
+      /* there are no channel classes with hash support, disable it. */
+      priv->use_hash = FALSE;
+      priv->content_hash_type = TP_FILE_HASH_TYPE_NONE;
+
+      goto out;
+    }
+
+  priv->use_hash = TRUE;
+
+  if (possible_values->len == 1)
+    {
+      priv->content_hash_type = g_array_index (possible_values, guint, 0);
+    }
+  else
+    {
+      /* order the array and pick the first non zero, so that MD5
+       * is the preferred value.
+       */
+      g_array_sort (possible_values, empathy_uint_compare);
+
+      if (g_array_index (possible_values, guint, 0) == 0)
+        priv->content_hash_type = g_array_index (possible_values, guint, 1);
+      else
+        priv->content_hash_type = g_array_index (possible_values, guint, 0);
+    }
+
+out:
+  g_array_free (possible_values, TRUE);
+
+  DEBUG ("Hash enabled %s; setting content hash type as %u",
+         priv->use_hash ? "True" : "False", priv->content_hash_type);
+}
+
+static void
+find_ft_channel_classes_cb (GList *channel_classes,
+    gpointer user_data)
+{
+  CallbacksData *data = user_data;
+  EmpathyFTHandler *handler = data->handler;
   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
   GError *myerr = NULL;
 
-  /* check if FT is allowed before firing up the I/O machinery */
-  if (!ft_handler_check_if_allowed (handler))
+  if (channel_classes == NULL)
     {
       g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK,
           EMPATHY_FT_ERROR_NOT_SUPPORTED,
           _("File transfer not supported by remote contact"));
 
-      emit_error_signal (handler, myerr);
+      if (!g_cancellable_is_cancelled (priv->cancellable))
+        g_cancellable_cancel (priv->cancellable);
+
+      data->callback (handler, myerr, data->user_data);
       g_clear_error (&myerr);
+    }
+  else
+    {
+      /* set whether we support hash and the type of it */
+      set_content_hash_type_from_classes (handler, channel_classes);
 
-      return;
+      /* get back to the caller now */
+      data->callback (handler, NULL, data->user_data);
     }
 
+  callbacks_data_free (data);
+}
+
+static void
+ft_handler_complete_request (EmpathyFTHandler *handler)
+{
+  EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
+
   /* populate the request table with all the known properties */
   ft_handler_populate_outgoing_request (handler);
 
@@ -863,17 +1132,6 @@ ft_handler_complete_request (EmpathyFTHandler *handler)
     ft_handler_push_to_dispatcher (handler);
 }
 
-static void
-callbacks_data_free (gpointer user_data)
-{
-  CallbacksData *data = user_data;
-
-  if (data->handler)
-    g_object_unref (data->handler);
-
-  g_slice_free (CallbacksData, data);
-}
-
 static void
 ft_handler_gfile_ready_cb (GObject *source,
     GAsyncResult *res,
@@ -891,9 +1149,25 @@ ft_handler_gfile_ready_cb (GObject *source,
   if (error != NULL)
     goto out;
 
+  if (g_file_info_get_file_type (info) != G_FILE_TYPE_REGULAR)
+    {
+      error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
+          EMPATHY_FT_ERROR_INVALID_SOURCE_FILE,
+          _("The selected file is not a regular file"));
+      goto out;
+    }
+
+  priv->total_bytes = g_file_info_get_size (info);
+  if (priv->total_bytes == 0)
+    {
+      error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
+          EMPATHY_FT_ERROR_EMPTY_SOURCE_FILE,
+          _("The selected file is empty"));
+      goto out;
+    }
+
   priv->content_type = g_strdup (g_file_info_get_content_type (info));
   priv->filename = g_strdup (g_file_info_get_display_name (info));
-  priv->total_bytes = g_file_info_get_size (info);
   g_file_info_get_modification_time (info, &mtime);
   priv->mtime = mtime.tv_sec;
   priv->transferred_bytes = 0;
@@ -902,24 +1176,28 @@ ft_handler_gfile_ready_cb (GObject *source,
   g_object_unref (info);
 
 out:
-  if (error == NULL)
-    {
-      cb_data->callback (cb_data->handler, NULL, cb_data->user_data);
-    }
-  else
+  if (error != NULL)
     {
       if (!g_cancellable_is_cancelled (priv->cancellable))
         g_cancellable_cancel (priv->cancellable);
 
-      cb_data->callback (NULL, error, cb_data->user_data);
+      cb_data->callback (cb_data->handler, error, cb_data->user_data);
       g_error_free (error);
-      g_object_unref (cb_data->handler);
-    }
 
-  callbacks_data_free (cb_data);
+      callbacks_data_free (cb_data);
+    }
+  else
+    {
+      /* see if FT/hashing are allowed */
+      empathy_dispatcher_find_requestable_channel_classes_async
+          (priv->dispatcher, empathy_contact_get_connection (priv->contact),
+           TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT,
+           find_ft_channel_classes_cb, cb_data,
+           TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType", NULL);
+    }
 }
 
-static void 
+static void
 contact_factory_contact_cb (EmpathyTpContactFactory *factory,
     EmpathyContact *contact,
     const GError *error,
@@ -935,8 +1213,8 @@ contact_factory_contact_cb (EmpathyTpContactFactory *factory,
       if (!g_cancellable_is_cancelled (priv->cancellable))
         g_cancellable_cancel (priv->cancellable);
 
-      cb_data->callback (NULL, (GError *) error, cb_data->user_data);
-      g_object_unref (handler);
+      cb_data->callback (handler, (GError *) error, cb_data->user_data);
+      callbacks_data_free (cb_data);
       return;
     }
 
@@ -963,8 +1241,9 @@ channel_get_all_properties_cb (TpProxy *proxy,
       if (!g_cancellable_is_cancelled (priv->cancellable))
         g_cancellable_cancel (priv->cancellable);
 
-      cb_data->callback (NULL, (GError *) error, cb_data->user_data);
-      g_object_unref (handler);
+      cb_data->callback (handler, (GError *) error, cb_data->user_data);
+
+      callbacks_data_free (cb_data);
       return;
     }
 
@@ -1001,10 +1280,18 @@ channel_get_all_properties_cb (TpProxy *proxy,
 
 /* public methods */
 
+/**
+ * empathy_ft_handler_new_outgoing:
+ * @contact: the #EmpathyContact to send @source to
+ * @source: the #GFile to send
+ * @callback: callback to be called when the handler has been created
+ * @user_data: user data to be passed to @callback
+ *
+ * Triggers the creation of a new #EmpathyFTHandler for an outgoing transfer.
+ */
 void
 empathy_ft_handler_new_outgoing (EmpathyContact *contact,
     GFile *source,
-    gboolean use_hash,
     EmpathyFTHandlerReadyCallback callback,
     gpointer user_data)
 {
@@ -1012,14 +1299,13 @@ empathy_ft_handler_new_outgoing (EmpathyContact *contact,
   CallbacksData *data;
   EmpathyFTHandlerPriv *priv;
 
-  DEBUG ("New handler outgoing, use hash %s",
-         use_hash ? "True" : "False");
+  DEBUG ("New handler outgoing");
 
   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
   g_return_if_fail (G_IS_FILE (source));
 
   handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
-      "contact", contact, "gfile", source, "use-hash", use_hash, NULL);
+      "contact", contact, "gfile", source, NULL);
 
   priv = GET_PRIV (handler);
 
@@ -1033,11 +1319,23 @@ empathy_ft_handler_new_outgoing (EmpathyContact *contact,
       G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
       G_FILE_ATTRIBUTE_STANDARD_SIZE ","
       G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
+      G_FILE_ATTRIBUTE_STANDARD_TYPE ","
       G_FILE_ATTRIBUTE_TIME_MODIFIED,
       G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT,
       NULL, (GAsyncReadyCallback) ft_handler_gfile_ready_cb, data);
 }
 
+/**
+ * empathy_ft_handler_new_incoming:
+ * @tp_file: the #EmpathyTpFile wrapping the incoming channel
+ * @callback: callback to be called when the handler has been created
+ * @user_data: user data to be passed to @callback
+ *
+ * Triggers the creation of a new #EmpathyFTHandler for an incoming transfer.
+ * Note that for the handler to be useful, you will have to set a destination
+ * file with empathy_ft_handler_incoming_set_destination() after the handler
+ * is ready.
+ */
 void
 empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file,
     EmpathyFTHandlerReadyCallback callback,
@@ -1064,6 +1362,13 @@ empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file,
       channel_get_all_properties_cb, data, NULL, G_OBJECT (handler));
 }
 
+/**
+ * empathy_ft_handler_start_transfer:
+ * @handler: an #EmpathyFTHandler
+ *
+ * Starts the transfer machinery. After this call, the transfer and hashing
+ * signals will be emitted by the handler.
+ */
 void
 empathy_ft_handler_start_transfer (EmpathyFTHandler *handler)
 {
@@ -1086,6 +1391,14 @@ empathy_ft_handler_start_transfer (EmpathyFTHandler *handler)
     }
 }
 
+/**
+ * empathy_ft_handler_cancel_transfer:
+ * @handler: an #EmpathyFTHandler
+ *
+ * Cancels an ongoing handler operation. Note that this doesn't destroy
+ * the object, which will keep all the properties, altough it won't be able
+ * to do any more I/O.
+ */
 void
 empathy_ft_handler_cancel_transfer (EmpathyFTHandler *handler)
 {
@@ -1104,21 +1417,46 @@ empathy_ft_handler_cancel_transfer (EmpathyFTHandler *handler)
     empathy_tp_file_cancel (priv->tpfile);
 }
 
+/**
+ * empathy_ft_handler_incoming_set_destination:
+ * @handler: an #EmpathyFTHandler
+ * @destination: the #GFile where the transfer should be saved
+ *
+ * Sets the destination of the incoming handler to be @destination.
+ * Note that calling this method is mandatory before starting the transfer
+ * for incoming handlers.
+ */
 void
 empathy_ft_handler_incoming_set_destination (EmpathyFTHandler *handler,
-    GFile *destination,
-    gboolean use_hash)
+    GFile *destination)
 {
-  DEBUG ("Set incoming destination, use hash %s",
-         use_hash ? "True" : "False");
+  EmpathyFTHandlerPriv *priv;
 
   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
   g_return_if_fail (G_IS_FILE (destination));
 
-  g_object_set (handler, "gfile", destination,
-      "use-hash", use_hash, NULL);
+  priv = GET_PRIV (handler);
+
+  g_object_set (handler, "gfile", destination, NULL);
+
+  /* check if hash is supported. if it isn't, set use_hash to FALSE
+   * anyway, so that clients won't be expecting us to checksum.
+   */
+  if (EMP_STR_EMPTY (priv->content_hash) ||
+      priv->content_hash_type == TP_FILE_HASH_TYPE_NONE)
+    priv->use_hash = FALSE;
+  else
+    priv->use_hash = TRUE;
 }
 
+/**
+ * empathy_ft_handler_get_filename:
+ * @handler: an #EmpathyFTHandler
+ *
+ * Returns the name of the file being transferred.
+ *
+ * Return value: the name of the file being transferred
+ */
 const char *
 empathy_ft_handler_get_filename (EmpathyFTHandler *handler)
 {
@@ -1131,6 +1469,14 @@ empathy_ft_handler_get_filename (EmpathyFTHandler *handler)
   return priv->filename;
 }
 
+/**
+ * empathy_ft_handler_get_content_type:
+ * @handler: an #EmpathyFTHandler
+ *
+ * Returns the content type of the file being transferred.
+ *
+ * Return value: the content type of the file being transferred
+ */
 const char *
 empathy_ft_handler_get_content_type (EmpathyFTHandler *handler)
 {
@@ -1143,6 +1489,14 @@ empathy_ft_handler_get_content_type (EmpathyFTHandler *handler)
   return priv->content_type;
 }
 
+/**
+ * empathy_ft_handler_get_contact:
+ * @handler: an #EmpathyFTHandler
+ *
+ * Returns the remote #EmpathyContact at the other side of the transfer.
+ *
+ * Return value: the remote #EmpathyContact for @handler
+ */
 EmpathyContact *
 empathy_ft_handler_get_contact (EmpathyFTHandler *handler)
 {
@@ -1155,6 +1509,14 @@ empathy_ft_handler_get_contact (EmpathyFTHandler *handler)
   return priv->contact;
 }
 
+/**
+ * empathy_ft_handler_get_gfile:
+ * @handler: an #EmpathyFTHandler
+ *
+ * Returns the #GFile where the transfer is being read/saved.
+ *
+ * Return value: the #GFile where the transfer is being read/saved
+ */
 GFile *
 empathy_ft_handler_get_gfile (EmpathyFTHandler *handler)
 {
@@ -1167,6 +1529,16 @@ empathy_ft_handler_get_gfile (EmpathyFTHandler *handler)
   return priv->gfile;
 }
 
+/**
+ * empathy_ft_handler_get_use_hash:
+ * @handler: an #EmpathyFTHandler
+ *
+ * Returns whether @handler has checksumming enabled. This can depend on
+ * the CM and the remote contact capabilities.
+ *
+ * Return value: %TRUE if the handler has checksumming enabled,
+ * %FALSE otherwise.
+ */
 gboolean
 empathy_ft_handler_get_use_hash (EmpathyFTHandler *handler)
 {
@@ -1179,6 +1551,14 @@ empathy_ft_handler_get_use_hash (EmpathyFTHandler *handler)
   return priv->use_hash;
 }
 
+/**
+ * empathy_ft_handler_is_incoming:
+ * @handler: an #EmpathyFTHandler
+ *
+ * Returns whether @handler is incoming or outgoing.
+ *
+ * Return value: %TRUE if the handler is incoming, %FALSE otherwise.
+ */
 gboolean
 empathy_ft_handler_is_incoming (EmpathyFTHandler *handler)
 {
@@ -1191,9 +1571,17 @@ empathy_ft_handler_is_incoming (EmpathyFTHandler *handler)
   if (priv->tpfile == NULL)
     return FALSE;
 
-  return empathy_tp_file_is_incoming (priv->tpfile);  
+  return empathy_tp_file_is_incoming (priv->tpfile);
 }
 
+/**
+ * empathy_ft_handler_get_transferred_bytes:
+ * @handler: an #EmpathyFTHandler
+ *
+ * Returns the number of bytes already transferred by the handler.
+ *
+ * Return value: the number of bytes already transferred by the handler.
+ */
 guint64
 empathy_ft_handler_get_transferred_bytes (EmpathyFTHandler *handler)
 {
@@ -1206,6 +1594,15 @@ empathy_ft_handler_get_transferred_bytes (EmpathyFTHandler *handler)
   return priv->transferred_bytes;
 }
 
+/**
+ * empathy_ft_handler_get_total_bytes:
+ * @handler: an #EmpathyFTHandler
+ *
+ * Returns the total size of the file being transferred by the handler.
+ *
+ * Return value: a number of bytes indicating the total size of the file being
+ * transferred by the handler.
+ */
 guint64
 empathy_ft_handler_get_total_bytes (EmpathyFTHandler *handler)
 {
@@ -1218,6 +1615,15 @@ empathy_ft_handler_get_total_bytes (EmpathyFTHandler *handler)
   return priv->total_bytes;
 }
 
+/**
+ * empathy_ft_handler_is_completed:
+ * @handler: an #EmpathyFTHandler
+ *
+ * Returns whether the transfer for @handler has been completed succesfully.
+ *
+ * Return value: %TRUE if the handler has been transferred correctly, %FALSE
+ * otherwise
+ */
 gboolean
 empathy_ft_handler_is_completed (EmpathyFTHandler *handler)
 {
@@ -1230,6 +1636,16 @@ empathy_ft_handler_is_completed (EmpathyFTHandler *handler)
   return priv->is_completed;
 }
 
+/**
+ * empathy_ft_handler_is_cancelled:
+ * @handler: an #EmpathyFTHandler
+ *
+ * Returns whether the transfer for @handler has been cancelled or has stopped
+ * due to an error.
+ *
+ * Return value: %TRUE if the transfer for @handler has been cancelled
+ * or has stopped due to an error, %FALSE otherwise.
+ */
 gboolean
 empathy_ft_handler_is_cancelled (EmpathyFTHandler *handler)
 {
@@ -1240,4 +1656,4 @@ empathy_ft_handler_is_cancelled (EmpathyFTHandler *handler)
   priv = GET_PRIV (handler);
 
   return g_cancellable_is_cancelled (priv->cancellable);
-}
\ No newline at end of file
+}