소스 검색

Use the same convention as on the server to format strings

 * use power of 10 SI units
 * use "B" instead of "bytes"
 * use at least 2 significant digit, but no more than one digit after
   the period
Olivier Goffart 12 년 전
부모
커밋
02b3033ca3
2개의 변경된 파일38개의 추가작업 그리고 26개의 파일을 삭제
  1. 18 21
      src/mirall/utility.cpp
  2. 20 5
      test/testutility.h

+ 18 - 21
src/mirall/utility.cpp

@@ -103,33 +103,30 @@ void Utility::setupFavLink(const QString &folder)
 
 QString Utility::octetsToString( qint64 octets )
 {
-    static const qint64 kb = 1024;
-    static const qint64 mb = 1024 * kb;
-    static const qint64 gb = 1024 * mb;
-    static const qint64 tb = 1024 * gb;
+    static const qint64 kb = 1000;
+    static const qint64 mb = 1000 * kb;
+    static const qint64 gb = 1000 * mb;
+    static const qint64 tb = 1000 * gb;
 
+    QString s;
+    qreal value = octets;
     if (octets >= tb) {
-        if (octets < 10*tb) {
-            return compactFormatDouble(qreal(octets)/qreal(tb), 1, QLatin1String("TB"));
-        }
-        return QString::number(qRound64(qreal(octets)/qreal(tb))) + QLatin1String(" TB");
+        s = QCoreApplication::translate("Utility", "%L1 TB");
+        value /= tb;
     } else if (octets >= gb) {
-        if (octets < 10*gb) {
-            return compactFormatDouble(qreal(octets)/qreal(gb), 1, QLatin1String("GB"));
-        }
-        return QString::number(qRound64(qreal(octets)/qreal(gb))) + QLatin1String(" GB");
+        s = QCoreApplication::translate("Utility", "%L1 GB");
+        value /= gb;
     } else if (octets >= mb) {
-        if (octets < 10*mb) {
-            return compactFormatDouble(qreal(octets)/qreal(mb), 1, QLatin1String("MB"));
-        }
-        return QString::number(qRound64(qreal(octets)/qreal(mb))) + QLatin1String(" MB");
+        s = QCoreApplication::translate("Utility", "%L1 MB");
+        value /= mb;
     } else if (octets >= kb) {
-        return QString::number(qRound64(qreal(octets)/qreal(kb))) + QLatin1String(" KB");
-    } else if (octets == 1){
-        return QLatin1String("1 byte");
-    } else {
-        return QString::number(octets) + QLatin1String(" bytes");
+        s = QCoreApplication::translate("Utility", "%L1 kB");
+        value /= kb;
+    } else  {
+        s = QCoreApplication::translate("Utility", "%L1 B");
     }
+
+    return (value > 9.95)  ? s.arg(qRound(value)) : s.arg(value, 0, 'g', 2);
 }
 
 // Qtified version of get_platforms() in csync_owncloud.c

+ 20 - 5
test/testutility.h

@@ -26,12 +26,27 @@ private slots:
     }
     void testOctetsToString()
     {
-        QVERIFY(octetsToString(1) == "1 byte");
-        QVERIFY(octetsToString(2) == "2 bytes");
-        QVERIFY(octetsToString(1024) == "1 KB");
+        QCOMPARE(octetsToString(999) , QString("999 B"));
+        QCOMPARE(octetsToString(1000) , QString("1 kB"));
+        QCOMPARE(octetsToString(1010) , QString("1 kB"));
+        QCOMPARE(octetsToString(1110) , QString("1.1 kB"));
+
+        QCOMPARE(octetsToString(9110) , QString("9.1 kB"));
+        QCOMPARE(octetsToString(9910) , QString("9.9 kB"));
+        QCOMPARE(octetsToString(9999) , QString("10 kB"));
+
+        QCOMPARE(octetsToString(123456) , QString("123 kB"));
+        QCOMPARE(octetsToString(1234567) , QString("1.2 MB"));
+        QCOMPARE(octetsToString(12345678) , QString("12 MB"));
+        QCOMPARE(octetsToString(123456789) , QString("123 MB"));
+        QCOMPARE(octetsToString(1000LL*1000*1000 * 5) , QString("5 GB"));
+
+        QVERIFY(octetsToString(1) == "1 B");
+        QVERIFY(octetsToString(2) == "2 B");
+        QVERIFY(octetsToString(1024) == "1 kB");
         QVERIFY(octetsToString(1024*1024) == "1 MB");
-        QVERIFY(octetsToString(1024LL*1024*1024) == "1 GB");
-        QVERIFY(octetsToString(1024LL*1024*1024*1024) == "1 TB");
+        QVERIFY(octetsToString(1024LL*1024*1024) == "1.1 GB");
+        QVERIFY(octetsToString(1024LL*1024*1024*1024) == "1.1 TB");
     }
 
     void testLaunchOnStartup()