[PATCH spice-gtk v5 2/2] Use g_format_size instead of g_format_size_for_display

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

 



g_format_size_for_display is deprecated since glib 0.30. See glib commit
afd1e3697065c1bd23fe9a1cacf43d8744d0bc9b

Add g_format_size to glib-compat
---
 src/channel-main.c |  4 ++--
 src/glib-compat.c  | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/glib-compat.h  |  1 +
 3 files changed, 67 insertions(+), 2 deletions(-)

diff --git a/src/channel-main.c b/src/channel-main.c
index d7b0b76..f5115aa 100644
--- a/src/channel-main.c
+++ b/src/channel-main.c
@@ -1741,8 +1741,8 @@ static void file_xfer_close_cb(GObject      *object,
             gchar *basename = g_file_get_basename(task->file);
             double seconds =
                 (double) g_date_time_difference(now, task->start_time) / G_TIME_SPAN_SECOND;
-            gchar *file_size_str = g_format_size_for_display(task->file_size);
-            gchar *transfer_speed_str = g_format_size_for_display(task->file_size / seconds);
+            gchar *file_size_str = g_format_size(task->file_size);
+            gchar *transfer_speed_str = g_format_size(task->file_size / seconds);
 
             g_warn_if_fail(task->read_bytes == task->file_size);
             SPICE_DEBUG("transferred file %s of %s size in %.1f seconds (%s/s)",
diff --git a/src/glib-compat.c b/src/glib-compat.c
index 49edf73..41a7f52 100644
--- a/src/glib-compat.c
+++ b/src/glib-compat.c
@@ -19,11 +19,75 @@
 #include "config.h"
 
 #include <string.h>
+#include <glib/gi18n.h>
 
 #include "glib-compat.h"
 
 #if !GLIB_CHECK_VERSION(2,30,0)
 G_DEFINE_BOXED_TYPE (GMainContext, spice_main_context, g_main_context_ref, g_main_context_unref)
+
+#define KILOBYTE_FACTOR (G_GOFFSET_CONSTANT (1000))
+#define MEGABYTE_FACTOR (KILOBYTE_FACTOR * KILOBYTE_FACTOR)
+#define GIGABYTE_FACTOR (MEGABYTE_FACTOR * KILOBYTE_FACTOR)
+#define TERABYTE_FACTOR (GIGABYTE_FACTOR * KILOBYTE_FACTOR)
+#define PETABYTE_FACTOR (TERABYTE_FACTOR * KILOBYTE_FACTOR)
+#define EXABYTE_FACTOR  (PETABYTE_FACTOR * KILOBYTE_FACTOR)
+
+/**
+ * g_format_size:
+ * @size: a size in bytes
+ *
+ * Formats a size (for example the size of a file) into a human readable
+ * string.  Sizes are rounded to the nearest size prefix (kB, MB, GB)
+ * and are displayed rounded to the nearest tenth. E.g. the file size
+ * 3292528 bytes will be converted into the string "3.2 MB".
+ *
+ * The prefix units base is 1000 (i.e. 1 kB is 1000 bytes).
+ *
+ * This string should be freed with g_free() when not needed any longer.
+ *
+ * See g_format_size_full() for more options about how the size might be
+ * formatted.
+ *
+ * Returns: a newly-allocated formatted string containing a human readable
+ *     file size
+ *
+ * Since: 2.30
+ */
+gchar *
+g_format_size (guint64 size)
+{
+  GString *string;
+
+  string = g_string_new (NULL);
+
+    if (size < KILOBYTE_FACTOR)
+      {
+        g_string_printf (string,
+                         g_dngettext(GETTEXT_PACKAGE, "%u byte", "%u bytes", (guint) size),
+                         (guint) size);
+      }
+
+    else if (size < MEGABYTE_FACTOR)
+      g_string_printf (string, _("%.1f kB"), (gdouble) size / (gdouble) KILOBYTE_FACTOR);
+
+    else if (size < GIGABYTE_FACTOR)
+      g_string_printf (string, _("%.1f MB"), (gdouble) size / (gdouble) MEGABYTE_FACTOR);
+
+    else if (size < TERABYTE_FACTOR)
+      g_string_printf (string, _("%.1f GB"), (gdouble) size / (gdouble) GIGABYTE_FACTOR);
+    else if (size < PETABYTE_FACTOR)
+      g_string_printf (string, _("%.1f TB"), (gdouble) size / (gdouble) TERABYTE_FACTOR);
+
+    else if (size < EXABYTE_FACTOR)
+      g_string_printf (string, _("%.1f PB"), (gdouble) size / (gdouble) PETABYTE_FACTOR);
+
+    else
+      g_string_printf (string, _("%.1f EB"), (gdouble) size / (gdouble) EXABYTE_FACTOR);
+
+  return g_string_free (string, FALSE);
+}
+
 #endif
 
 
diff --git a/src/glib-compat.h b/src/glib-compat.h
index 5491fe4..512ea55 100644
--- a/src/glib-compat.h
+++ b/src/glib-compat.h
@@ -28,6 +28,7 @@
 #if !GLIB_CHECK_VERSION(2,30,0)
 #define G_TYPE_MAIN_CONTEXT (spice_main_context_get_type ())
 GType spice_main_context_get_type (void) G_GNUC_CONST;
+gchar *g_format_size (guint64 size);
 #endif
 
 #if !GLIB_CHECK_VERSION(2,32,0)
-- 
2.5.0

_______________________________________________
Spice-devel mailing list
Spice-devel@xxxxxxxxxxxxxxxxxxxxx
http://lists.freedesktop.org/mailman/listinfo/spice-devel




[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]     [Monitors]