[gwget] Set current and total size in Mb and Gb



commit ec62ba10ddbcb85297fd01af83083f2471232cf0
Author: David Sedeño <david alderia com>
Date:   Sat Nov 7 22:12:56 2009 +0100

    Set current and total size in Mb and Gb

 ChangeLog            |    7 +++++++
 src/gwget_data.c     |   21 ++++++++++++---------
 src/main_window_cb.c |    8 +++++---
 src/utils.c          |   23 +++++++++++++++++++++++
 src/utils.h          |    2 +-
 5 files changed, 48 insertions(+), 13 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 878a8ae..e403d8e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2009-11-07  David Sedeño <david alderia com>
+
+  * src/gwget_data.c, src/main_window_cb.c, src/utils.c:
+    Add function to set the current and total size in Mb and Gb.
+    Thanks Felipe Besoaín <fbesoain gmail com> for initial patch.
+    Fix #596421
+
 2009-10-27  David Sedeño <david alderia com>
 
   * src/gwget_data.c: compare .asp file correctly. 
diff --git a/src/gwget_data.c b/src/gwget_data.c
index 502054d..f727e78 100644
--- a/src/gwget_data.c
+++ b/src/gwget_data.c
@@ -83,6 +83,7 @@ gwget_data_update_statistics (GwgetData *gwgetdata)
 	time_t estimated;
 	gdouble perc;
 	gchar *title;
+	gchar *size;
 		
 	
 	/* Get time and size of the file being retrieved */
@@ -130,22 +131,24 @@ gwget_data_update_statistics (GwgetData *gwgetdata)
 	
 	/* Total Size */
 	if (gwgetdata->state == DL_NOT_STARTED)
-		strcpy (buffer, "");
-	else
-		sprintf (buffer, "%d kB", (guint32)(gwgetdata->total_size + 512) / 1024);
-	
+		size = g_strdup("0");
+	else {
+	  size = get_readable_size (gwgetdata->total_size);
+  }
 	gtk_list_store_set(GTK_LIST_STORE(model),&gwgetdata->file_list,
-						TOTALSIZE_COLUMN,buffer,
+						TOTALSIZE_COLUMN, size,
 						-1);
 	
 	
 	/* Update retrieved information */
 	if (gwgetdata->state == DL_NOT_STARTED || gwgetdata->state == DL_COMPLETED)
-		strcpy (buffer, "");
-	else
-		sprintf (buffer, "%d kB",(guint32) (gwgetdata->cur_size + 512) / 1024);
+		size = g_strdup("0");
+	else {
+	  size = get_readable_size (gwgetdata->cur_size);
+  }
+
 	gtk_list_store_set(GTK_LIST_STORE(model),&gwgetdata->file_list,
-						CURRENTSIZE_COLUMN,buffer,
+						CURRENTSIZE_COLUMN, size,
 						-1);
 	
 	
diff --git a/src/main_window_cb.c b/src/main_window_cb.c
index 0a1af14..eeea988 100644
--- a/src/main_window_cb.c
+++ b/src/main_window_cb.c
@@ -218,15 +218,17 @@ new_download(GwgetData* gwgetdata)
 	int width = 16, height = 16;
 	gdouble perc;
 	
-	gtk_list_store_append (GTK_LIST_STORE(model), &iter); 
-	size = g_strdup_printf ("%d kB", (guint32)(gwgetdata->cur_size + 512) / 1024);
+	gtk_list_store_append (GTK_LIST_STORE(model), &iter);
+
+	size = get_readable_size (gwgetdata->cur_size);
+
 	gtk_list_store_set (GTK_LIST_STORE(model), &iter,URL_COLUMN,gwgetdata->url,
 						CURRENTSIZE_COLUMN, size, 
 						FILENAME_COLUMN, gwgetdata->filename,
 	    				-1);
 	
+  size = get_readable_size (gwgetdata->total_size);
 	
-	size = g_strdup_printf ("%d kB", (guint32)(gwgetdata->total_size + 512) / 1024);
 	gtk_list_store_set (GTK_LIST_STORE(model), &iter, TOTALSIZE_COLUMN, size, -1);
 	
 	perc = gwgetdata->total_size==0?0:((gdouble)gwgetdata->cur_size*100)/(gdouble)gwgetdata->total_size ;
diff --git a/src/utils.c b/src/utils.c
index 66990dc..7e6a19d 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -157,3 +157,26 @@ gboolean check_server_already_exists(gchar *checkurl)
 	return FALSE;
 }
 
+gchar* get_readable_size(guint64 size_in_kb)
+{
+  float n_mb;
+  gchar *size;
+
+  if (size_in_kb == 0)
+    size = g_strdup("0");
+  else
+  {
+    n_mb = (float)((guint32)(size_in_kb  + 512)/1048576);
+
+    if(n_mb >= 1024)
+	    size = g_strdup_printf ("%0.2f Gb", (float)(guint32)(size_in_kb  + 512) / 1073741824);
+    else
+      if (n_mb >= 1)
+          size = g_strdup_printf ("%0.2f Mb", (float)(guint32)(size_in_kb  + 512) / 1048576);
+      else
+          size = g_strdup_printf ("%0.2f Kb", (float)(guint32)(size_in_kb  + 512) / 1024);
+  }
+
+  return size;
+}
+
diff --git a/src/utils.h b/src/utils.h
index e213551..cc8ddd7 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -26,7 +26,7 @@ void run_dialog_information(gchar *title, gchar *msg);
 int check_url(char *str1, char *str2);
 gboolean check_url_already_exists(gchar *checkurl);
 gboolean check_server_already_exists(gchar *checkurl);
-
+gchar* get_readable_size(guint64 size_in_kb);
 
 
 #endif



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