[empathy: 57/148] Add back speed and remaining time



commit 53073da65b919bd68a26e5e0e276ec5ba15f51ea
Author: Cosimo Cecchi <cosimo cecchi collabora co uk>
Date:   Tue May 5 17:22:14 2009 +0200

    Add back speed and remaining time
---
 libempathy/empathy-ft-handler.c |   66 +++++++++++++++++++++++++++++----------
 src/empathy-ft-manager.c        |   49 ++++++++++++++++++++++++----
 2 files changed, 91 insertions(+), 24 deletions(-)

diff --git a/libempathy/empathy-ft-handler.c b/libempathy/empathy-ft-handler.c
index e5c18cb..6746f81 100644
--- a/libempathy/empathy-ft-handler.c
+++ b/libempathy/empathy-ft-handler.c
@@ -29,6 +29,7 @@
 #include "empathy-contact-factory.h"
 #include "empathy-dispatcher.h"
 #include "empathy-marshal.h"
+#include "empathy-time.h"
 #include "empathy-utils.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_FT
@@ -96,6 +97,11 @@ typedef struct {
   TpFileHashType content_hash_type;
   TpFileTransferState current_state;
 
+  /* time and speed */
+  gdouble speed;
+  guint remaining_time;
+  time_t last_update_time;
+
   gboolean is_completed;
 } EmpathyFTHandlerPriv;
 
@@ -183,6 +189,12 @@ do_dispose (GObject *object)
     g_object_unref (priv->cancellable);
     priv->cancellable = NULL;
   }
+
+  if (priv->request != NULL)
+    {
+      g_hash_table_unref (priv->request);
+      priv->request = NULL;
+    }
   
   G_OBJECT_CLASS (empathy_ft_handler_parent_class)->dispose (object);
 }
@@ -206,12 +218,6 @@ do_finalize (GObject *object)
   g_free (priv->content_hash);
   priv->content_hash = NULL;
 
-  if (priv->request != NULL)
-    {
-      g_hash_table_destroy (priv->request);
-      priv->request = NULL;
-    }
-
   G_OBJECT_CLASS (empathy_ft_handler_parent_class)->finalize (object);
 }
 
@@ -272,9 +278,9 @@ empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass)
   signals[TRANSFER_PROGRESS] =
     g_signal_new ("transfer-progress", G_TYPE_FROM_CLASS (klass),
         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
-        _empathy_marshal_VOID__UINT64_UINT64,
+        _empathy_marshal_VOID__UINT64_UINT64_UINT_DOUBLE,
         G_TYPE_NONE,
-        2, G_TYPE_UINT64, G_TYPE_UINT64);
+        4, G_TYPE_UINT64, G_TYPE_UINT64, G_TYPE_UINT, G_TYPE_DOUBLE);
 
   signals[HASHING_STARTED] =
     g_signal_new ("hashing-started", G_TYPE_FROM_CLASS (klass),
@@ -379,6 +385,33 @@ ft_transfer_operation_callback (EmpathyTpFile *tp_file,
 }
 
 static void
+update_remaining_time_and_speed (EmpathyFTHandler *handler,
+                                 guint64 transferred_bytes)
+{
+  EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
+  time_t elapsed_time, current_time;
+  guint64 transferred, last_transferred_bytes;
+  gdouble speed;
+  gint remaining_time;
+
+  last_transferred_bytes = priv->transferred_bytes;
+  priv->transferred_bytes = transferred_bytes;
+
+  current_time = empathy_time_get_current ();
+  elapsed_time = current_time - priv->last_update_time;
+
+  if (elapsed_time >= 1)
+    {
+      transferred = transferred_bytes - last_transferred_bytes;
+      speed = (gdouble) transferred / (gdouble) elapsed_time;
+      remaining_time = (priv->total_bytes - transferred) / speed;
+      priv->speed = speed;
+      priv->remaining_time = remaining_time;
+      priv->last_update_time = current_time;
+    }
+}
+
+static void
 ft_transfer_progress_callback (EmpathyTpFile *tp_file,
                                guint64 transferred_bytes,
                                gpointer user_data)
@@ -386,16 +419,19 @@ ft_transfer_progress_callback (EmpathyTpFile *tp_file,
   EmpathyFTHandler *handler = user_data;
   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
 
-  if (transferred_bytes == 0) {
+  if (transferred_bytes == 0)
+    {
+      priv->last_update_time = empathy_time_get_current ();
       g_signal_emit (handler, signals[TRANSFER_STARTED], 0, tp_file);
-  }
-    
+    }
 
   if (priv->transferred_bytes != transferred_bytes)
     {
-      priv->transferred_bytes = transferred_bytes;
+      update_remaining_time_and_speed (handler, transferred_bytes);
+
       g_signal_emit (handler, signals[TRANSFER_PROGRESS], 0,
-          transferred_bytes, priv->total_bytes);
+          transferred_bytes, priv->total_bytes, priv->remaining_time,
+          priv->speed);
     }
 }
 
@@ -410,10 +446,6 @@ ft_handler_create_channel_cb (EmpathyDispatchOperation *operation,
 
   DEBUG ("Dispatcher create channel CB");
 
-  /* we can destroy now the request */
-  g_hash_table_destroy (priv->request);
-  priv->request = NULL;
-
   if (my_error == NULL)
     {
       g_cancellable_set_error_if_cancelled (priv->cancellable, &my_error);
diff --git a/src/empathy-ft-manager.c b/src/empathy-ft-manager.c
index b379175..91c1ef0 100644
--- a/src/empathy-ft-manager.c
+++ b/src/empathy-ft-manager.c
@@ -98,9 +98,8 @@ G_DEFINE_TYPE (EmpathyFTManager, empathy_ft_manager, G_TYPE_OBJECT);
 
 static EmpathyFTManager *manager_singleton = NULL;
 
-#if 0
 static gchar *
-ft_manager_format_interval (gint interval)
+ft_manager_format_interval (guint interval)
 {
   gint hours, mins, secs;
 
@@ -117,7 +116,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)
@@ -255,19 +253,27 @@ remove_finished_transfer_foreach (gpointer key,
 static char *
 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)
     {
@@ -389,6 +395,30 @@ 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_handler_transfer_error_cb (EmpathyFTHandler *handler,
                               GError *error,
                               EmpathyFTManager *manager)
@@ -458,6 +488,8 @@ 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;
@@ -471,13 +503,16 @@ ft_handler_transfer_progress_cb (EmpathyFTHandler *handler,
 
   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);
 }
 
@@ -499,7 +534,7 @@ 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
@@ -544,7 +579,7 @@ ft_handler_hashing_progress_cb (EmpathyFTHandler *handler,
   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);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]