diff --git a/src/gui/activitywidget.cpp b/src/gui/activitywidget.cpp index 731c0262c..460161314 100644 --- a/src/gui/activitywidget.cpp +++ b/src/gui/activitywidget.cpp @@ -96,7 +96,7 @@ QVariant ActivityListModel::data(const QModelIndex &index, int role) const return a._accName; break; case ActivityItemDelegate::PointInTimeRole: - return timeSpanFromNow(a._dateTime); + return Utility::timeAgoInWords(a._dateTime); break; case ActivityItemDelegate::AccountConnectedRole: return (ast && ast->isConnected()); @@ -109,26 +109,6 @@ QVariant ActivityListModel::data(const QModelIndex &index, int role) const } -QString ActivityListModel::timeSpanFromNow(const QDateTime& dt) const -{ - QDateTime now = QDateTime::currentDateTime(); - - if( dt.daysTo(now)>0 ) { - return tr("%1 day(s) ago").arg(dt.daysTo(now)); - } else { - qint64 secs = dt.secsTo(now); - - if( floor(secs / 3600.0) > 0 ) { - int hours = floor(secs/3600.0); - return( tr("%1 hour(s) ago").arg(hours)); - } else { - int minutes = qRound(secs/60.0); - return( tr("%1 minute(s) ago").arg(minutes)); - } - } - return tr("Some time ago"); -} - int ActivityListModel::rowCount(const QModelIndex&) const { return _finalList.count(); diff --git a/src/gui/activitywidget.h b/src/gui/activitywidget.h index 7949a74e3..df5e73c65 100644 --- a/src/gui/activitywidget.h +++ b/src/gui/activitywidget.h @@ -116,7 +116,6 @@ private slots: private: void startFetchJob(AccountState* s); void combineActivityLists(); - QString timeSpanFromNow(const QDateTime& dt) const; QMap _activityLists; ActivityList _finalList; diff --git a/src/libsync/utility.cpp b/src/libsync/utility.cpp index dbe25ae38..914e9f86e 100644 --- a/src/libsync/utility.cpp +++ b/src/libsync/utility.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -430,6 +431,36 @@ QByteArray Utility::versionOfInstalledBinary( const QString& command ) return re; } +QString Utility::timeAgoInWords(const QDateTime& dt, const QDateTime& from) +{ + QDateTime now = QDateTime::currentDateTime(); + + if( from.isValid() ) { + now = from; + } + + if( dt.daysTo(now)>0 ) { + int dtn = dt.daysTo(now); + return QObject::tr("%1 day(s) ago", "", dtn).arg(dtn); + } else { + qint64 secs = dt.secsTo(now); + + if( floor(secs / 3600.0) > 0 ) { + int hours = floor(secs/3600.0); + return( QObject::tr("%1 hour(s) ago", "", hours).arg(hours)); + } else { + int minutes = qRound(secs/60.0); + if( minutes == 0 ) { + return QObject::tr("Less than a minute ago"); + } + return( QObject::tr("%1 minute(s) ago", "", minutes).arg(minutes)); + } + } + return QObject::tr("Some time ago"); +} + +/* --------------------------------------------------------------------------- */ + static const char STOPWATCH_END_TAG[] = "_STOPWATCH_END"; void Utility::StopWatch::start() diff --git a/src/libsync/utility.h b/src/libsync/utility.h index 2691e2a98..7aa31e357 100644 --- a/src/libsync/utility.h +++ b/src/libsync/utility.h @@ -107,6 +107,16 @@ namespace Utility OWNCLOUDSYNC_EXPORT QString fileNameForGuiUse(const QString& fName); + /** + * @brief timeAgoInWords - human readable time span + * + * Use this to get a string that describes the timespan between the first and + * the second timestamp in a human readable and understandable form. + * + * If the second parameter is ommitted, the current time is used. + */ + OWNCLOUDSYNC_EXPORT QString timeAgoInWords(const QDateTime& dt, const QDateTime& from = QDateTime() ); + class OWNCLOUDSYNC_EXPORT StopWatch { private: QHash _lapTimes;