[Pkg-owncloud-commits] [owncloud-client] 225/470: Add utility function for shorter time duration strings #3403

Sandro Knauß hefee-guest at moszumanska.debian.org
Thu May 12 16:25:06 UTC 2016


This is an automated email from the git hooks/post-receive script.

hefee-guest pushed a commit to branch master
in repository owncloud-client.

commit 41f43feecf36a4ebbdf8c74904f875a94a77e6e5
Author: Christian Kamm <mail at ckamm.de>
Date:   Thu Jan 21 09:53:58 2016 +0100

    Add utility function for shorter time duration strings #3403
---
 src/gui/folderstatusmodel.cpp |  2 +-
 src/gui/owncloudgui.cpp       |  4 ++--
 src/libsync/utility.cpp       | 40 ++++++++++++++++++++++++++++++----------
 src/libsync/utility.h         |  9 ++++++++-
 test/testutility.h            | 38 ++++++++++++++++++++++++++------------
 5 files changed, 67 insertions(+), 26 deletions(-)

diff --git a/src/gui/folderstatusmodel.cpp b/src/gui/folderstatusmodel.cpp
index f36a701..b808cf8 100644
--- a/src/gui/folderstatusmodel.cpp
+++ b/src/gui/folderstatusmodel.cpp
@@ -915,7 +915,7 @@ void FolderStatusModel::slotSetProgress(const ProgressInfo &progress)
         overallSyncString = tr("%1 of %2, file %3 of %4\nTotal time left %5")
             .arg(s1, s2)
             .arg(currentFile).arg(totalFileCount)
-            .arg( Utility::durationToDescriptiveString(progress.totalProgress().estimatedEta) );
+            .arg( Utility::durationToDescriptiveString2(progress.totalProgress().estimatedEta) );
     } else if (totalFileCount > 0) {
         // Don't attempt to estimate the time left if there is no kb to transfer.
         overallSyncString = tr("file %1 of %2") .arg(currentFile).arg(totalFileCount);
diff --git a/src/gui/owncloudgui.cpp b/src/gui/owncloudgui.cpp
index d5005cc..39796bb 100644
--- a/src/gui/owncloudgui.cpp
+++ b/src/gui/owncloudgui.cpp
@@ -641,11 +641,11 @@ void ownCloudGui::slotUpdateProgress(const QString &folder, const ProgressInfo&
         quint64 totalFileCount = qMax(progress.totalFiles(), currentFile);
         _actionStatus->setText( tr("Syncing %1 of %2  (%3 left)")
             .arg( currentFile ).arg( totalFileCount )
-            .arg( Utility::durationToDescriptiveString(progress.totalProgress().estimatedEta) ) );
+            .arg( Utility::durationToDescriptiveString2(progress.totalProgress().estimatedEta) ) );
     } else {
         QString totalSizeStr = Utility::octetsToString( progress.totalSize() );
         _actionStatus->setText( tr("Syncing %1 (%2 left)")
-            .arg( totalSizeStr, Utility::durationToDescriptiveString(progress.totalProgress().estimatedEta) ) );
+            .arg( totalSizeStr, Utility::durationToDescriptiveString2(progress.totalProgress().estimatedEta) ) );
     }
 
 
diff --git a/src/libsync/utility.cpp b/src/libsync/utility.cpp
index 613c7af..bfeafab 100644
--- a/src/libsync/utility.cpp
+++ b/src/libsync/utility.cpp
@@ -309,9 +309,19 @@ qint64 Utility::qDateTimeToTime_t(const QDateTime& t)
     return t.toMSecsSinceEpoch() / 1000;
 }
 
-QString Utility::durationToDescriptiveString(quint64 msecs)
-{
-    struct Period { const char *name; quint64 msec; };
+namespace {
+    struct Period
+    {
+        const char *name;
+        quint64 msec;
+
+        QString description(quint64 value) const
+        {
+            return QCoreApplication::translate(
+                    "Utiliy", name, 0, QCoreApplication::UnicodeUTF8,
+                    value);
+        }
+    };
     Q_DECL_CONSTEXPR Period periods[] = {
         { QT_TRANSLATE_NOOP("Utility", "%Ln year(s)") , 365*24*3600*1000LL },
         { QT_TRANSLATE_NOOP("Utility", "%Ln month(s)") , 30*24*3600*1000LL },
@@ -321,17 +331,16 @@ QString Utility::durationToDescriptiveString(quint64 msecs)
         { QT_TRANSLATE_NOOP("Utility", "%Ln second(s)") , 1000LL },
         { 0, 0 }
     };
+} // anonymous namespace
 
+QString Utility::durationToDescriptiveString2(quint64 msecs)
+{
     int p = 0;
-    while (periods[p].name && msecs < periods[p].msec) {
+    while (periods[p+1].name && msecs < periods[p].msec) {
         p++;
     }
 
-    if (!periods[p].name) {
-        return QCoreApplication::translate("Utility", "0 seconds");
-    }
-
-    auto firstPart = QCoreApplication::translate("Utility", periods[p].name, 0, QCoreApplication::UnicodeUTF8, int(msecs / periods[p].msec));
+    auto firstPart = periods[p].description(int(msecs / periods[p].msec));
 
     if (!periods[p+1].name) {
         return firstPart;
@@ -344,7 +353,18 @@ QString Utility::durationToDescriptiveString(quint64 msecs)
     }
 
     return QCoreApplication::translate("Utility", "%1 %2").arg(firstPart,
-            QCoreApplication::translate("Utility", periods[p+1].name, 0, QCoreApplication::UnicodeUTF8, secondPartNum));
+            periods[p+1].description(secondPartNum));
+}
+
+QString Utility::durationToDescriptiveString1(quint64 msecs)
+{
+    int p = 0;
+    while (periods[p+1].name && msecs < periods[p].msec) {
+        p++;
+    }
+
+    quint64 amount = qRound( double(msecs) / periods[p].msec );
+    return periods[p].description(amount);
 }
 
 QString Utility::fileNameForGuiUse(const QString& fName)
diff --git a/src/libsync/utility.h b/src/libsync/utility.h
index 7aa31e3..032a3e7 100644
--- a/src/libsync/utility.h
+++ b/src/libsync/utility.h
@@ -67,8 +67,15 @@ namespace Utility
      * @brief Convert milliseconds duration to human readable string.
      * @param quint64 msecs the milliseconds to convert to string.
      * @return an HMS representation of the milliseconds value.
+     *
+     * durationToDescriptiveString1 describes the duration in a single
+     * unit, like "5 minutes" or "2 days".
+     *
+     * durationToDescriptiveString2 uses two units where possible, so
+     * "5 minutes 43 seconds" or "1 month 3 days".
      */
-    OWNCLOUDSYNC_EXPORT QString durationToDescriptiveString(quint64 msecs);
+    OWNCLOUDSYNC_EXPORT QString durationToDescriptiveString1(quint64 msecs);
+    OWNCLOUDSYNC_EXPORT QString durationToDescriptiveString2(quint64 msecs);
 
     /**
      * @brief hasDarkSystray - determines whether the systray is dark or light.
diff --git a/test/testutility.h b/test/testutility.h
index bc7cc4d..a4b9572 100644
--- a/test/testutility.h
+++ b/test/testutility.h
@@ -83,21 +83,35 @@ private slots:
 
         QDateTime current = QDateTime::currentDateTime();
 
-        QCOMPARE(durationToDescriptiveString(0), QString("0 seconds") );
-        QCOMPARE(durationToDescriptiveString(5), QString("0 seconds") );
-        QCOMPARE(durationToDescriptiveString(1000), QString("1 second(s)") );
-        QCOMPARE(durationToDescriptiveString(1005), QString("1 second(s)") );
-        QCOMPARE(durationToDescriptiveString(56123), QString("56 second(s)") );
-        QCOMPARE(durationToDescriptiveString(90*sec), QString("1 minute(s) 30 second(s)") );
-        QCOMPARE(durationToDescriptiveString(3*hour), QString("3 hour(s)") );
-        QCOMPARE(durationToDescriptiveString(3*hour + 20*sec), QString("3 hour(s)") );
-        QCOMPARE(durationToDescriptiveString(3*hour + 70*sec), QString("3 hour(s) 1 minute(s)") );
-        QCOMPARE(durationToDescriptiveString(3*hour + 100*sec), QString("3 hour(s) 2 minute(s)") );
-        QCOMPARE(durationToDescriptiveString(current.msecsTo(current.addYears(4).addMonths(5).addDays(2).addSecs(23*60*60))),
+        QCOMPARE(durationToDescriptiveString2(0), QString("0 second(s)") );
+        QCOMPARE(durationToDescriptiveString2(5), QString("0 second(s)") );
+        QCOMPARE(durationToDescriptiveString2(1000), QString("1 second(s)") );
+        QCOMPARE(durationToDescriptiveString2(1005), QString("1 second(s)") );
+        QCOMPARE(durationToDescriptiveString2(56123), QString("56 second(s)") );
+        QCOMPARE(durationToDescriptiveString2(90*sec), QString("1 minute(s) 30 second(s)") );
+        QCOMPARE(durationToDescriptiveString2(3*hour), QString("3 hour(s)") );
+        QCOMPARE(durationToDescriptiveString2(3*hour + 20*sec), QString("3 hour(s)") );
+        QCOMPARE(durationToDescriptiveString2(3*hour + 70*sec), QString("3 hour(s) 1 minute(s)") );
+        QCOMPARE(durationToDescriptiveString2(3*hour + 100*sec), QString("3 hour(s) 2 minute(s)") );
+        QCOMPARE(durationToDescriptiveString2(current.msecsTo(current.addYears(4).addMonths(5).addDays(2).addSecs(23*60*60))),
                  QString("4 year(s) 5 month(s)") );
-        QCOMPARE(durationToDescriptiveString(current.msecsTo(current.addDays(2).addSecs(23*60*60))),
+        QCOMPARE(durationToDescriptiveString2(current.msecsTo(current.addDays(2).addSecs(23*60*60))),
                  QString("2 day(s) 23 hour(s)") );
 
+        QCOMPARE(durationToDescriptiveString1(0), QString("0 second(s)") );
+        QCOMPARE(durationToDescriptiveString1(5), QString("0 second(s)") );
+        QCOMPARE(durationToDescriptiveString1(1000), QString("1 second(s)") );
+        QCOMPARE(durationToDescriptiveString1(1005), QString("1 second(s)") );
+        QCOMPARE(durationToDescriptiveString1(56123), QString("56 second(s)") );
+        QCOMPARE(durationToDescriptiveString1(90*sec), QString("2 minute(s)") );
+        QCOMPARE(durationToDescriptiveString1(3*hour), QString("3 hour(s)") );
+        QCOMPARE(durationToDescriptiveString1(3*hour + 20*sec), QString("3 hour(s)") );
+        QCOMPARE(durationToDescriptiveString1(3*hour + 70*sec), QString("3 hour(s)") );
+        QCOMPARE(durationToDescriptiveString1(3*hour + 100*sec), QString("3 hour(s)") );
+        QCOMPARE(durationToDescriptiveString1(current.msecsTo(current.addYears(4).addMonths(5).addDays(2).addSecs(23*60*60))),
+                 QString("4 year(s)") );
+        QCOMPARE(durationToDescriptiveString1(current.msecsTo(current.addDays(2).addSecs(23*60*60))),
+                 QString("3 day(s)") );
 
     }
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud-client.git



More information about the Pkg-owncloud-commits mailing list