]> git.0d.be Git - empathy.git/blobdiff - src/empathy-ft-manager.c
Updated Polish translation
[empathy.git] / src / empathy-ft-manager.c
index 26a2428252d6e8853a2049a55ff69f5cc0433acb..e681f8441f1a76be01881779718afa66d3a3be38 100644 (file)
@@ -33,6 +33,7 @@
 
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
+#include <gdk/gdkkeysyms.h>
 
 #define DEBUG_FLAG EMPATHY_DEBUG_FT
 #include <libempathy/empathy-debug.h>
 
 #include "empathy-ft-manager.h"
 
-#include "extensions/extensions.h"
-
-/**
- * SECTION:empathy-ft-manager
- * @short_description: File transfer dialog
- * @see_also: #EmpathyTpFile, empathy_dispatcher_send_file()
- * @include: libempthy-gtk/empathy-ft-manager.h
- *
- * The #EmpathyFTManager object represents the file transfer dialog,
- * it can show multiple file transfers at the same time (added
- * with empathy_ft_manager_add_tp_file()).
- */
-
 enum
 {
   COL_PERCENT,
@@ -68,23 +56,16 @@ enum
   COL_FT_OBJECT
 };
 
-/**
- * EmpathyFTManagerPriv:
- *
- * Private fields of the #EmpathyFTManager class.
- */
 typedef struct {
   GtkTreeModel *model;
   GHashTable *ft_handler_to_row_ref;
-  GHashTable *cancellable_refs;
 
   /* Widgets */
   GtkWidget *window;
   GtkWidget *treeview;
   GtkWidget *open_button;
   GtkWidget *abort_button;
-
-  guint save_geometry_id;
+  GtkWidget *clear_button;
 } EmpathyFTManagerPriv;
 
 enum
@@ -100,9 +81,11 @@ G_DEFINE_TYPE (EmpathyFTManager, empathy_ft_manager, G_TYPE_OBJECT);
 
 static EmpathyFTManager *manager_singleton = NULL;
 
-#if 0
+static void ft_handler_hashing_started_cb (EmpathyFTHandler *handler,
+    EmpathyFTManager *manager);
+
 static gchar *
-ft_manager_format_interval (gint interval)
+ft_manager_format_interval (guint interval)
 {
   gint hours, mins, secs;
 
@@ -119,7 +102,6 @@ ft_manager_format_interval (gint interval)
     /* Translators: time left, when is is less than one hour */
     return g_strdup_printf (_("%02u.%02u"), mins, secs);
 }
-#endif
 
 static void
 ft_manager_update_buttons (EmpathyFTManager *manager)
@@ -127,15 +109,12 @@ ft_manager_update_buttons (EmpathyFTManager *manager)
   GtkTreeSelection *selection;
   GtkTreeModel *model;
   GtkTreeIter iter;
-<<<<<<< HEAD:src/empathy-ft-manager.c
-  EmpathyTpFile *tp_file;
-  TpFileTransferState state;
-=======
   EmpathyFTHandler *handler;
->>>>>>> Use the proper TP interface instead of emp_cli:src/empathy-ft-manager.c
   gboolean open_enabled = FALSE;
   gboolean abort_enabled = FALSE;
+  gboolean clear_enabled = FALSE;
   gboolean is_completed, is_cancelled;
+  GHashTableIter hash_iter;
   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
 
   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
@@ -148,49 +127,32 @@ ft_manager_update_buttons (EmpathyFTManager *manager)
       is_cancelled = empathy_ft_handler_is_cancelled (handler);
 
       /* I can open the file if the transfer is completed and was incoming */
-<<<<<<< HEAD:src/empathy-ft-manager.c
-      open_enabled = (state == TP_FILE_TRANSFER_STATE_COMPLETED &&
-        empathy_tp_file_is_incoming (tp_file));
-
-      /* I can abort if the transfer is not already finished */
-      abort_enabled = (state != TP_FILE_TRANSFER_STATE_CANCELLED &&
-        state != TP_FILE_TRANSFER_STATE_COMPLETED);
-=======
       open_enabled = (is_completed && empathy_ft_handler_is_incoming (handler));
 
       /* I can abort if the transfer is not already finished */
       abort_enabled = (is_cancelled == FALSE && is_completed == FALSE);
->>>>>>> Use the proper TP interface instead of emp_cli:src/empathy-ft-manager.c
 
       g_object_unref (handler);
     }
 
-  gtk_widget_set_sensitive (priv->open_button, open_enabled);
-  gtk_widget_set_sensitive (priv->abort_button, abort_enabled);
-}
+  g_hash_table_iter_init (&hash_iter, priv->ft_handler_to_row_ref);
 
-#if 0
-static const gchar *
-ft_manager_state_change_reason_to_string (TpFileTransferStateChangeReason reason)
-{
-  switch (reason)
+  while (g_hash_table_iter_next (&hash_iter, (gpointer *) &handler, NULL))
     {
-      case TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE:
-        return _("No reason was specified");
-      case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REQUESTED:
-        return _("The change in state was requested");
-      case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_STOPPED:
-        return _("You canceled the file transfer");
-      case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_STOPPED:
-        return _("The other participant canceled the file transfer");
-      case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR:
-        return _("Error while trying to transfer the file");
-      case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_ERROR:
-        return _("The other participant is unable to transfer the file");
+      if (empathy_ft_handler_is_completed (handler) ||
+          empathy_ft_handler_is_cancelled (handler))
+          clear_enabled = TRUE;
+
+      if (clear_enabled)
+        break;
     }
-  return _("Unknown reason");
+
+  gtk_widget_set_sensitive (priv->open_button, open_enabled);
+  gtk_widget_set_sensitive (priv->abort_button, abort_enabled);
+
+  if (clear_enabled)
+    gtk_widget_set_sensitive (priv->clear_button, TRUE);
 }
-#endif
 
 static void
 ft_manager_selection_changed (GtkTreeSelection *selection,
@@ -238,7 +200,6 @@ ft_manager_remove_file_from_model (EmpathyFTManager *manager,
   GtkTreePath *path = NULL;
   GtkTreeIter iter;
   gboolean update_selection;
-  GCancellable *cancellable;
   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
 
   row_ref = ft_manager_get_row_from_handler (manager, handler);
@@ -272,12 +233,6 @@ ft_manager_remove_file_from_model (EmpathyFTManager *manager,
 
   if (update_selection)
     gtk_tree_selection_select_iter (selection, &iter);
-
-  cancellable = g_hash_table_lookup (priv->cancellable_refs, handler);
-
-  if (cancellable != NULL) {
-    g_cancellable_cancel (cancellable);
-  }
 }
 
 static gboolean
@@ -298,26 +253,34 @@ remove_finished_transfer_foreach (gpointer key,
   return FALSE;
 }
 
-static char *
+static gchar *
 ft_manager_format_progress_bytes_and_percentage (guint64 current,
                                                  guint64 total,
+                                                 gdouble speed,
                                                  int *percentage)
 {
   char *total_str, *current_str, *retval;
+  char *speed_str = NULL;
 
   total_str = g_format_size_for_display (total);
   current_str = g_format_size_for_display (current);
 
+  if (speed > 0)
+    speed_str = g_format_size_for_display ((goffset) speed);
+
   /* translators: first %s is the currently processed size, second %s is
    * the total file size */
-  retval = g_strdup_printf (_("%s of %s"), current_str, total_str);
+  retval = speed_str ?
+    g_strdup_printf (_("%s of %s at %s/s"), current_str, total_str, speed_str) :
+    g_strdup_printf (_("%s of %s"), current_str, total_str);
 
   g_free (total_str);
   g_free (current_str);
+  g_free (speed_str);
 
   if (percentage != NULL)
     {
-      if (total != 0) 
+      if (total != 0)
         *percentage = current * 100 / total;
       else
         *percentage = -1;
@@ -326,7 +289,7 @@ ft_manager_format_progress_bytes_and_percentage (guint64 current,
   return retval;
 }
 
-static char *
+static gchar *
 ft_manager_format_contact_info (EmpathyFTHandler *handler)
 {
   gboolean incoming;
@@ -350,6 +313,49 @@ ft_manager_format_contact_info (EmpathyFTHandler *handler)
   return retval;
 }
 
+static gchar *
+ft_manager_format_error_message (EmpathyFTHandler *handler,
+                                 const GError *error)
+{
+  const char *contact_name, *filename;
+  EmpathyContact *contact;
+  char *first_line, *message;
+  gboolean incoming;
+
+  contact_name = NULL;
+  incoming = empathy_ft_handler_is_incoming (handler);
+
+  contact = empathy_ft_handler_get_contact (handler);
+  if (contact)
+    contact_name = empathy_contact_get_name (contact);
+
+  filename = empathy_ft_handler_get_filename (handler);
+
+  if (incoming)
+    /* filename/contact_name here are either both NULL or both valid */
+    if (filename && contact_name)
+      /* translators: first %s is filename, second %s
+       * is the contact name */
+      first_line = g_strdup_printf (_("Error receiving \"%s\" from %s"), filename,
+          contact_name);
+    else
+      first_line = g_strdup (_("Error receiving a file"));
+  else
+    /* translators: first %s is filename, second %s
+     * is the contact name */
+    if (filename && contact_name)
+      first_line = g_strdup_printf (_("Error sending \"%s\" to %s"), filename,
+          contact_name);
+    else
+      first_line = g_strdup (_("Error sending a file"));
+
+  message = g_strdup_printf ("%s\n%s", first_line, error->message);
+
+  g_free (first_line);
+
+  return message;
+}
+
 static void
 ft_manager_update_handler_message (EmpathyFTManager *manager,
                                    GtkTreeRowReference *row_ref,
@@ -391,25 +397,83 @@ ft_manager_update_handler_progress (EmpathyFTManager *manager,
 
 }
 
+static void
+ft_manager_update_handler_time (EmpathyFTManager *manager,
+                                GtkTreeRowReference *row_ref,
+                                guint remaining_time)
+{
+  GtkTreePath *path;
+  GtkTreeIter iter;
+  EmpathyFTManagerPriv *priv = GET_PRIV (manager);
+  char *remaining_str;
+
+  remaining_str = ft_manager_format_interval (remaining_time);
+
+  /* Set new value in the store */
+  path = gtk_tree_row_reference_get_path (row_ref);
+  gtk_tree_model_get_iter (priv->model, &iter, path);
+  gtk_list_store_set (GTK_LIST_STORE (priv->model),
+      &iter,
+      COL_REMAINING, remaining_str,
+      -1);
+
+  gtk_tree_path_free (path);
+  g_free (remaining_str);
+}
+
+static void
+ft_manager_clear_handler_time (EmpathyFTManager *manager,
+                               GtkTreeRowReference *row_ref)
+{
+  GtkTreePath *path;
+  GtkTreeIter iter;
+  EmpathyFTManagerPriv *priv = GET_PRIV (manager);
+
+  /* Set new value in the store */
+  path = gtk_tree_row_reference_get_path (row_ref);
+  gtk_tree_model_get_iter (priv->model, &iter, path);
+  gtk_list_store_set (GTK_LIST_STORE (priv->model),
+      &iter,
+      COL_REMAINING, NULL,
+      -1);
+
+  gtk_tree_path_free (path);
+}
+
 static void
 ft_handler_transfer_error_cb (EmpathyFTHandler *handler,
                               GError *error,
                               EmpathyFTManager *manager)
 {
-  /* TODO: implement */
+  char *message;
+  GtkTreeRowReference *row_ref;
+
+  DEBUG ("Transfer error %s", error->message);
+
+  row_ref = ft_manager_get_row_from_handler (manager, handler);
+  g_return_if_fail (row_ref != NULL);
+
+  message = ft_manager_format_error_message (handler, error);
+
+  ft_manager_update_handler_message (manager, row_ref, message);
+  ft_manager_clear_handler_time (manager, row_ref);
+  ft_manager_update_buttons (manager);
+
+  g_free (message);
 }
 
 static void
-ft_handler_transfer_done_cb (EmpathyFTHandler *handler,
-                             EmpathyTpFile *tp_file,
-                             EmpathyFTManager *manager)
+do_real_transfer_done (EmpathyFTManager *manager,
+                       EmpathyFTHandler *handler)
 {
   const char *contact_name;
   const char *filename;
   char *first_line, *second_line, *message;
+  char *uri;
   gboolean incoming;
   GtkTreeRowReference *row_ref;
-  EmpathyFTManagerPriv *priv = GET_PRIV (manager);
+  GtkRecentManager *recent_manager;
+  GFile *file;
 
   row_ref = ft_manager_get_row_from_handler (manager, handler);
   g_return_if_fail (row_ref != NULL);
@@ -434,9 +498,7 @@ ft_handler_transfer_done_cb (EmpathyFTHandler *handler,
 
   message = g_strdup_printf ("%s\n%s", first_line, second_line);
   ft_manager_update_handler_message (manager, row_ref, message);
-
-  /* remove the cancellable object */
-  g_hash_table_remove (priv->cancellable_refs, handler);
+  ft_manager_clear_handler_time (manager, row_ref);
 
   /* update buttons */
   ft_manager_update_buttons (manager);
@@ -444,31 +506,70 @@ ft_handler_transfer_done_cb (EmpathyFTHandler *handler,
   g_free (message);
   g_free (first_line);
   g_free (second_line);
+
+  recent_manager = gtk_recent_manager_get_default ();
+  file = empathy_ft_handler_get_gfile (handler);
+  uri = g_file_get_uri (file);
+
+  gtk_recent_manager_add_item (recent_manager, uri);
+
+  g_free (uri);
+}
+
+static void
+ft_handler_transfer_done_cb (EmpathyFTHandler *handler,
+                             EmpathyTpFile *tp_file,
+                             EmpathyFTManager *manager)
+{
+  if (empathy_ft_handler_is_incoming (handler) &&
+      empathy_ft_handler_get_use_hash (handler))
+    {
+      DEBUG ("Transfer done, waiting for hashing-started");
+
+      /* connect to the signal and return early */
+      g_signal_connect (handler, "hashing-started",
+          G_CALLBACK (ft_handler_hashing_started_cb), manager);
+
+      return;
+    }
+
+  DEBUG ("Transfer done, no hashing");
+
+  do_real_transfer_done (manager, handler);
 }
 
 static void
 ft_handler_transfer_progress_cb (EmpathyFTHandler *handler,
                                  guint64 current_bytes,
                                  guint64 total_bytes,
+                                 guint remaining_time,
+                                 gdouble speed,
                                  EmpathyFTManager *manager)
 {
   char *first_line, *second_line, *message;
   int percentage;
   GtkTreeRowReference *row_ref;
 
+  DEBUG ("Transfer progress");
+
   row_ref = ft_manager_get_row_from_handler (manager, handler);
   g_return_if_fail (row_ref != NULL);
 
   first_line = ft_manager_format_contact_info (handler);
   second_line = ft_manager_format_progress_bytes_and_percentage
-    (current_bytes, total_bytes, &percentage);
+    (current_bytes, total_bytes, speed, &percentage);
 
   message = g_strdup_printf ("%s\n%s", first_line, second_line);
 
   ft_manager_update_handler_message (manager, row_ref, message);
   ft_manager_update_handler_progress (manager, row_ref, percentage);
 
+  if (remaining_time > 0)
+    ft_manager_update_handler_time (manager, row_ref, remaining_time);
+
   g_free (message);
+  g_free (first_line);
+  g_free (second_line);
 }
 
 static void
@@ -478,6 +579,8 @@ ft_handler_transfer_started_cb (EmpathyFTHandler *handler,
 {
   guint64 transferred_bytes, total_bytes;
 
+  DEBUG ("Transfer started");
+
   g_signal_connect (handler, "transfer-progress",
       G_CALLBACK (ft_handler_transfer_progress_cb), manager);
   g_signal_connect (handler, "transfer-done",
@@ -487,13 +590,38 @@ ft_handler_transfer_started_cb (EmpathyFTHandler *handler,
   total_bytes = empathy_ft_handler_get_total_bytes (handler);
 
   ft_handler_transfer_progress_cb (handler, transferred_bytes, total_bytes,
-      manager);
+      0, -1, manager);
 }
 
 static void
 ft_handler_hashing_done_cb (EmpathyFTHandler *handler,
                             EmpathyFTManager *manager)
 {
+  GtkTreeRowReference *row_ref;
+  char *first_line, *second_line, *message;
+
+  DEBUG ("Hashing done");
+
+  /* update the message */
+  if (empathy_ft_handler_is_incoming (handler))
+    {
+      do_real_transfer_done (manager, handler);
+      return;
+    }
+
+  row_ref = ft_manager_get_row_from_handler (manager, handler);
+  g_return_if_fail (row_ref != NULL);
+
+  first_line = ft_manager_format_contact_info (handler);
+  second_line = g_strdup (_("Waiting for the other participant's response"));
+  message = g_strdup_printf ("%s\n%s", first_line, second_line);
+
+  ft_manager_update_handler_message (manager, row_ref, message);
+
+  g_free (message);
+  g_free (first_line);
+  g_free (second_line);
+
   g_signal_connect (handler, "transfer-started",
       G_CALLBACK (ft_handler_transfer_started_cb), manager);
 }
@@ -510,25 +638,34 @@ ft_handler_hashing_progress_cb (EmpathyFTHandler *handler,
   row_ref = ft_manager_get_row_from_handler (manager, handler);
   g_return_if_fail (row_ref != NULL);
 
-  first_line =  g_strdup_printf (_("Hashing \"%s\""),
-      empathy_ft_handler_get_filename (handler));
+  if (empathy_ft_handler_is_incoming (handler))
+      first_line = g_strdup_printf (_("Checking integrity of \"%s\""),
+          empathy_ft_handler_get_filename (handler));
+  else
+      first_line =  g_strdup_printf (_("Hashing \"%s\""),
+          empathy_ft_handler_get_filename (handler));
+
   second_line = ft_manager_format_progress_bytes_and_percentage
-    (current_bytes, total_bytes, NULL);
+    (current_bytes, total_bytes, -1, NULL);
 
   message = g_strdup_printf ("%s\n%s", first_line, second_line);
 
   ft_manager_update_handler_message (manager, row_ref, message);
 
   g_free (message);
+  g_free (first_line);
+  g_free (second_line);
 }
 
 static void
 ft_handler_hashing_started_cb (EmpathyFTHandler *handler,
                                EmpathyFTManager *manager)
 {
-  char *message;
+  char *message, *first_line, *second_line;
   GtkTreeRowReference *row_ref;
 
+  DEBUG ("Hashing started");
+
   g_signal_connect (handler, "hashing-progress",
      G_CALLBACK (ft_handler_hashing_progress_cb), manager);
   g_signal_connect (handler, "hashing-done",
@@ -537,11 +674,21 @@ ft_handler_hashing_started_cb (EmpathyFTHandler *handler,
   row_ref = ft_manager_get_row_from_handler (manager, handler);
   g_return_if_fail (row_ref != NULL);
 
-  message =  g_strdup_printf (_("Hashing \"%s\""),
-      empathy_ft_handler_get_filename (handler));
+  first_line = ft_manager_format_contact_info (handler);
+
+  if (empathy_ft_handler_is_incoming (handler))
+      second_line = g_strdup_printf (_("Checking integrity of \"%s\""),
+          empathy_ft_handler_get_filename (handler));
+  else
+      second_line = g_strdup_printf (_("Hashing \"%s\""),
+          empathy_ft_handler_get_filename (handler));
+
+  message = g_strdup_printf ("%s\n%s", first_line, second_line);
 
   ft_manager_update_handler_message (manager, row_ref, message);
 
+  g_free (first_line);
+  g_free (second_line);
   g_free (message);
 }
 
@@ -549,51 +696,61 @@ static void
 ft_manager_start_transfer (EmpathyFTManager *manager,
                            EmpathyFTHandler *handler)
 {
-  GCancellable *cancellable;
   EmpathyFTManagerPriv *priv;
+  gboolean is_outgoing;
 
   priv = GET_PRIV (manager);
 
-  cancellable = g_cancellable_new ();
-  g_hash_table_insert (priv->cancellable_refs, g_object_ref (handler),
-      cancellable);
+  is_outgoing = !empathy_ft_handler_is_incoming (handler);
+
+  DEBUG ("Start transfer, is outgoing %s",
+      is_outgoing ? "True" : "False");
 
   /* now connect the signals */
   g_signal_connect (handler, "transfer-error",
       G_CALLBACK (ft_handler_transfer_error_cb), manager);
 
-  if (empathy_ft_handler_is_incoming (handler)) {
+  if (is_outgoing && empathy_ft_handler_get_use_hash (handler)) {
     g_signal_connect (handler, "hashing-started",
         G_CALLBACK (ft_handler_hashing_started_cb), manager);
   } else {
+    /* either incoming or outgoing without hash */
     g_signal_connect (handler, "transfer-started",
         G_CALLBACK (ft_handler_transfer_started_cb), manager);
   }
 
-  empathy_ft_handler_start_transfer (handler, cancellable);
+  empathy_ft_handler_start_transfer (handler);
 }
 
 static void
 ft_manager_add_handler_to_list (EmpathyFTManager *manager,
-                                EmpathyFTHandler *handler)
+                                EmpathyFTHandler *handler,
+                                const GError *error)
 {
   GtkTreeRowReference *row_ref;
   GtkTreeIter iter;
   GtkTreeSelection *selection;
   GtkTreePath *path;
   GIcon *icon;
-  const char *content_type;
-  char *first_line, *second_line, *message;
+  const char *content_type, *second_line;
+  char *first_line, *message;
   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
 
+  icon = NULL;
+
   /* get the icon name from the mime-type of the file. */
   content_type = empathy_ft_handler_get_content_type (handler);
-  icon = g_content_type_get_icon (content_type);
+
+  if (content_type != NULL)
+    icon = g_content_type_get_icon (content_type);
 
   /* append the handler in the store */
   gtk_list_store_insert_with_values (GTK_LIST_STORE (priv->model),
-      &iter, G_MAXINT, COL_FT_OBJECT, handler, COL_ICON, icon, -1);
-  g_object_unref (icon);
+      &iter, G_MAXINT, COL_FT_OBJECT, handler,
+      COL_ICON, icon, -1);
+
+  if (icon != NULL)
+    g_object_unref (icon);
 
   /* insert the new row_ref in the hash table  */
   path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->model), &iter);
@@ -606,19 +763,34 @@ ft_manager_add_handler_to_list (EmpathyFTManager *manager,
   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
   gtk_tree_selection_select_iter (selection, &iter);
 
-  /* hook up the signals and start the transfer */
-  ft_manager_start_transfer (manager, handler);
+  if (error != NULL)
+    {
+      message = ft_manager_format_error_message (handler, error);
+      ft_manager_update_handler_message (manager, row_ref, message);
 
-  /* update the row with the initial values */
-  first_line = ft_manager_format_contact_info (handler);
-  second_line = g_strdup (_("Waiting for the other participant's response"));
-  message = g_strdup_printf ("%s\n%s", first_line, second_line);
+      g_free (message);
+      return;
+    }
 
-  ft_manager_update_handler_message (manager, row_ref, message);
+  /* update the row with the initial values.
+   * the only case where we postpone this is in case we're managing
+   * an outgoing+hashing transfer, as the hashing started signal will
+   * take care of updating the information.
+   */
+  if (empathy_ft_handler_is_incoming (handler) ||
+      !empathy_ft_handler_get_use_hash (handler)) {
+    first_line = ft_manager_format_contact_info (handler);
+    second_line = _("Waiting for the other participant's response");
+    message = g_strdup_printf ("%s\n%s", first_line, second_line);
+
+    ft_manager_update_handler_message (manager, row_ref, message);
+
+    g_free (first_line);
+    g_free (message);
+  }
 
-  g_free (first_line);
-  g_free (second_line);
-  g_free (message);
+  /* hook up the signals and start the transfer */
+  ft_manager_start_transfer (manager, handler);
 }
 
 static void
@@ -633,6 +805,9 @@ ft_manager_clear (EmpathyFTManager *manager)
   /* Remove completed and cancelled transfers */
   g_hash_table_foreach_remove (priv->ft_handler_to_row_ref,
       remove_finished_transfer_foreach, manager);
+
+  /* set the clear button back to insensitive */
+  gtk_widget_set_sensitive (priv->clear_button, FALSE);
 }
 
 static void
@@ -670,7 +845,6 @@ ft_manager_stop (EmpathyFTManager *manager)
   GtkTreeIter iter;
   GtkTreeModel *model;
   EmpathyFTHandler *handler;
-  GCancellable *cancellable;
   EmpathyFTManagerPriv *priv;
 
   priv = GET_PRIV (manager);
@@ -687,42 +861,9 @@ ft_manager_stop (EmpathyFTManager *manager)
       empathy_contact_get_name (empathy_ft_handler_get_contact (handler)),
       empathy_ft_handler_get_filename (handler));
 
-  cancellable = g_hash_table_lookup (priv->cancellable_refs, handler);
-  g_assert (cancellable != NULL);
-
-  g_cancellable_cancel (cancellable);
-}
-
-static gboolean
-ft_manager_save_geometry_timeout_cb (EmpathyFTManager *manager)
-{
-  EmpathyFTManagerPriv *priv = GET_PRIV (manager);
-  gint x, y, w, h;
-
-  gtk_window_get_size (GTK_WINDOW (priv->window), &w, &h);
-  gtk_window_get_position (GTK_WINDOW (priv->window), &x, &y);
-
-  empathy_geometry_save ("ft-manager", x, y, w, h);
-
-  priv->save_geometry_id = 0;
-
-  return FALSE;
-}
-
-static gboolean
-ft_manager_configure_event_cb (GtkWidget *widget,
-                               GdkEventConfigure *event,
-                               EmpathyFTManager *manager)
-{
-  EmpathyFTManagerPriv *priv = GET_PRIV (manager);
-
-  if (priv->save_geometry_id != 0)
-    g_source_remove (priv->save_geometry_id);
-
-  priv->save_geometry_id = g_timeout_add (500,
-      (GSourceFunc) ft_manager_save_geometry_timeout_cb, manager);
+  empathy_ft_handler_cancel_transfer (handler);
 
-  return FALSE;
+  g_object_unref (handler);
 }
 
 static void
@@ -751,6 +892,8 @@ ft_manager_delete_event_cb (GtkWidget *widget,
 {
   EmpathyFTManagerPriv *priv = GET_PRIV (manager);
 
+  DEBUG ("%p", manager);
+
   /* remove all the completed/cancelled/errored transfers */
   ft_manager_clear (manager);
 
@@ -769,14 +912,30 @@ static void
 ft_manager_destroy_cb (GtkWidget *widget,
                        EmpathyFTManager *manager)
 {
+  DEBUG ("%p", manager);
+
   g_object_unref (manager);
 }
 
+static gboolean
+ft_manager_key_press_event_cb (GtkWidget *widget,
+                               GdkEventKey *event,
+                               gpointer user_data)
+{
+  if ((event->state & GDK_CONTROL_MASK && event->keyval == GDK_w)
+      || event->keyval == GDK_Escape)
+    {
+      gtk_widget_destroy (widget);
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
 static void
 ft_manager_build_ui (EmpathyFTManager *manager)
 {
   GtkBuilder *gui;
-  gint x, y, w, h;
   GtkTreeView *view;
   GtkListStore *liststore;
   GtkTreeViewColumn *column;
@@ -789,6 +948,7 @@ ft_manager_build_ui (EmpathyFTManager *manager)
   gui = empathy_builder_get_file (filename,
       "ft_manager_dialog", &priv->window,
       "ft_list", &priv->treeview,
+      "clear_button", &priv->clear_button,
       "open_button", &priv->open_button,
       "abort_button", &priv->abort_button,
       NULL);
@@ -798,27 +958,13 @@ ft_manager_build_ui (EmpathyFTManager *manager)
       "ft_manager_dialog", "destroy", ft_manager_destroy_cb,
       "ft_manager_dialog", "response", ft_manager_response_cb,
       "ft_manager_dialog", "delete-event", ft_manager_delete_event_cb,
-      "ft_manager_dialog", "configure-event", ft_manager_configure_event_cb,
+      "ft_manager_dialog", "key-press-event", ft_manager_key_press_event_cb,
       NULL);
 
-  g_object_unref (gui);
+  empathy_builder_unref_and_keep_widget (gui, priv->window);
 
   /* Window geometry. */
-  empathy_geometry_load ("ft-manager", &x, &y, &w, &h);
-
-  if (x >= 0 && y >= 0)
-    {
-      /* Let the window manager position it if we don't have
-       * good x, y coordinates. */
-      gtk_window_move (GTK_WINDOW (priv->window), x, y);
-    }
-
-  if (w > 0 && h > 0)
-    {
-      /* Use the defaults from the ui file if we don't have
-       * good w, h geometry. */
-      gtk_window_resize (GTK_WINDOW (priv->window), w, h);
-    }
+  empathy_geometry_bind (GTK_WINDOW (priv->window), "ft-manager");
 
   /* Setup the tree view */
   view = GTK_TREE_VIEW (priv->treeview);
@@ -885,6 +1031,11 @@ ft_manager_build_ui (EmpathyFTManager *manager)
   gtk_tree_view_column_pack_start (column, renderer, FALSE);
   gtk_tree_view_column_set_attributes (column, renderer,
       "text", COL_REMAINING, NULL);
+
+  /* clear button should be sensitive only if there are completed/cancelled
+   * handlers in the store.
+   */
+  gtk_widget_set_sensitive (priv->clear_button, FALSE);
 }
 
 /* GObject method overrides */
@@ -894,13 +1045,10 @@ empathy_ft_manager_finalize (GObject *object)
 {
   EmpathyFTManagerPriv *priv = GET_PRIV (object);
 
-  DEBUG ("%p", object);
+  DEBUG ("FT Manager %p", object);
 
   g_hash_table_destroy (priv->ft_handler_to_row_ref);
 
-  if (priv->save_geometry_id != 0)
-    g_source_remove (priv->save_geometry_id);
-
   G_OBJECT_CLASS (empathy_ft_manager_parent_class)->finalize (object);
 }
 
@@ -917,8 +1065,6 @@ empathy_ft_manager_init (EmpathyFTManager *manager)
   priv->ft_handler_to_row_ref = g_hash_table_new_full (g_direct_hash,
       g_direct_equal, (GDestroyNotify) g_object_unref,
       (GDestroyNotify) gtk_tree_row_reference_free);
-  priv->cancellable_refs = g_hash_table_new_full (g_direct_hash, g_direct_equal,
-      (GDestroyNotify) g_object_unref, (GDestroyNotify) g_object_unref);
 
   ft_manager_build_ui (manager);
 }
@@ -932,7 +1078,7 @@ empathy_ft_manager_constructor (GType type,
 
   if (manager_singleton)
     {
-      retval = g_object_ref (manager_singleton);
+      retval = G_OBJECT (manager_singleton);
     }
   else
     {
@@ -959,29 +1105,48 @@ empathy_ft_manager_class_init (EmpathyFTManagerClass *klass)
 
 /* public methods */
 
-/**
- * empathy_ft_manager_dup_singleton:
- *
- * Returns a reference to the #EmpathyFTManager singleton object.
- *
- * Returns: a #EmpathyFTManager
- */
-EmpathyFTManager *
-empathy_ft_manager_dup_singleton (void)
+void
+empathy_ft_manager_add_handler (EmpathyFTHandler *handler)
 {
-  return g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
+  EmpathyFTManager *manager;
+  EmpathyFTManagerPriv *priv;
+
+  DEBUG ("Adding handler");
+
+  g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
+
+  manager = g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
+  priv = GET_PRIV (manager);
+
+  ft_manager_add_handler_to_list (manager, handler, NULL);
+  gtk_window_present (GTK_WINDOW (priv->window));
 }
 
 void
-empathy_ft_manager_add_handler (EmpathyFTManager *manager,
-                                EmpathyFTHandler *handler)
+empathy_ft_manager_display_error (EmpathyFTHandler *handler,
+                                  const GError *error)
 {
-  EmpathyFTManagerPriv *priv = GET_PRIV (manager);
+  EmpathyFTManager *manager;
+  EmpathyFTManagerPriv *priv;
 
-  g_return_if_fail (EMPATHY_IS_FT_MANAGER (manager));
   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
+  g_return_if_fail (error != NULL);
+
+  manager = g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
+  priv = GET_PRIV (manager);
 
-  ft_manager_add_handler_to_list (manager, handler);
+  ft_manager_add_handler_to_list (manager, handler, error);
   gtk_window_present (GTK_WINDOW (priv->window));
 }
 
+void
+empathy_ft_manager_show (void)
+{
+  EmpathyFTManager *manager;
+  EmpathyFTManagerPriv *priv;
+
+  manager = g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL);
+  priv = GET_PRIV (manager);
+
+  gtk_window_present (GTK_WINDOW (priv->window));
+}