From 78b47415a9415e5714d379a6a8069811611f713d Mon Sep 17 00:00:00 2001
From: Akira Ohgaki <akiraohgaki@gmail.com>
Date: Mon, 24 Oct 2016 06:36:40 +0900
Subject: [PATCH] Add utility script

---
 src/qml.qrc                |  1 +
 src/qml/main.qml           | 48 ++++----------------------------------
 src/qml/scripts/Utility.js | 43 ++++++++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+), 44 deletions(-)
 create mode 100644 src/qml/scripts/Utility.js

diff --git a/src/qml.qrc b/src/qml.qrc
index 69145a8..4512a10 100644
--- a/src/qml.qrc
+++ b/src/qml.qrc
@@ -1,5 +1,6 @@
 <RCC>
     <qresource prefix="/">
         <file>qml/main.qml</file>
+        <file>qml/scripts/Utility.js</file>
     </qresource>
 </RCC>
diff --git a/src/qml/main.qml b/src/qml/main.qml
index 5977c6b..3b1d8ea 100644
--- a/src/qml/main.qml
+++ b/src/qml/main.qml
@@ -3,6 +3,8 @@ import QtQuick.Window 2.0
 import QtQuick.Controls 1.2
 import QtQuick.Dialogs 1.2
 
+import 'scripts/Utility.js' as Utility
+
 Window {
     id: root
     title: Qt.application.name
@@ -132,8 +134,8 @@ Window {
             progressDialog.primaryLabel.text = 'Downloading... ';
             progressDialog.informativeLabel.text = metadata.filename;
             progressDialog.progressBar.value = received / total;
-            progressDialog.progressLabel.text = convertByteToHumanReadable(received)
-                    + ' / ' + convertByteToHumanReadable(total)
+            progressDialog.progressLabel.text = Utility.convertByteToHumanReadable(received)
+                    + ' / ' + Utility.convertByteToHumanReadable(total)
         });
 
         if (xdgUrlHandler.isValid()) {
@@ -150,46 +152,4 @@ Window {
             errorDialog.open();
         }
     }
-
-    function convertByteToHumanReadable(bytes) {
-        bytes = parseFloat(bytes);
-        var kb = 1024;
-        var mb = 1024 * kb;
-        var gb = 1024 * mb;
-        var tb = 1024 * gb;
-        var pb = 1024 * tb;
-        var eb = 1024 * pb;
-        var zb = 1024 * eb;
-        var yb = 1024 * zb;
-
-        var text = '';
-        if (bytes < kb) {
-            text = bytes.toFixed(0) + ' B';
-        }
-        else if (bytes < mb) {
-            text = (bytes / kb).toFixed(2) + ' KB';
-        }
-        else if (bytes < gb) {
-            text = (bytes / mb).toFixed(2) + ' MB';
-        }
-        else if (bytes < tb) {
-            text = (bytes / gb).toFixed(2) + ' GB';
-        }
-        else if (bytes < pb) {
-            text = (bytes / tb).toFixed(2) + ' TB';
-        }
-        else if (bytes < eb) {
-            text = (bytes / pb).toFixed(2) + ' PB';
-        }
-        else if (bytes < zb) {
-            text = (bytes / eb).toFixed(2) + ' EB';
-        }
-        else if (bytes < yb) {
-            text = (bytes / zb).toFixed(2) + ' ZB';
-        }
-        else if (bytes >= yb) {
-            text = (bytes / yb).toFixed(2) + ' YB';
-        }
-        return text;
-    }
 }
diff --git a/src/qml/scripts/Utility.js b/src/qml/scripts/Utility.js
new file mode 100644
index 0000000..d60a332
--- /dev/null
+++ b/src/qml/scripts/Utility.js
@@ -0,0 +1,43 @@
+.pragma library
+
+function convertByteToHumanReadable(bytes) {
+    bytes = parseFloat(bytes);
+    var kb = 1024;
+    var mb = 1024 * kb;
+    var gb = 1024 * mb;
+    var tb = 1024 * gb;
+    var pb = 1024 * tb;
+    var eb = 1024 * pb;
+    var zb = 1024 * eb;
+    var yb = 1024 * zb;
+
+    var text = '';
+    if (bytes < kb) {
+        text = bytes.toFixed(0) + ' B';
+    }
+    else if (bytes < mb) {
+        text = (bytes / kb).toFixed(2) + ' KB';
+    }
+    else if (bytes < gb) {
+        text = (bytes / mb).toFixed(2) + ' MB';
+    }
+    else if (bytes < tb) {
+        text = (bytes / gb).toFixed(2) + ' GB';
+    }
+    else if (bytes < pb) {
+        text = (bytes / tb).toFixed(2) + ' TB';
+    }
+    else if (bytes < eb) {
+        text = (bytes / pb).toFixed(2) + ' PB';
+    }
+    else if (bytes < zb) {
+        text = (bytes / eb).toFixed(2) + ' EB';
+    }
+    else if (bytes < yb) {
+        text = (bytes / zb).toFixed(2) + ' ZB';
+    }
+    else if (bytes >= yb) {
+        text = (bytes / yb).toFixed(2) + ' YB';
+    }
+    return text;
+}
-- 
GitLab