AcitivityWidget: Moved timespan-in-words method to utility.

Also added a second parameter, fixed plural translation and added
a less-than-a-minute-ago term.
This commit is contained in:
Klaas Freitag 2015-11-16 18:08:25 +01:00
parent c781155b60
commit e38bc6eab8
4 changed files with 42 additions and 22 deletions

View file

@ -96,7 +96,7 @@ QVariant ActivityListModel::data(const QModelIndex &index, int role) const
return a._accName; return a._accName;
break; break;
case ActivityItemDelegate::PointInTimeRole: case ActivityItemDelegate::PointInTimeRole:
return timeSpanFromNow(a._dateTime); return Utility::timeAgoInWords(a._dateTime);
break; break;
case ActivityItemDelegate::AccountConnectedRole: case ActivityItemDelegate::AccountConnectedRole:
return (ast && ast->isConnected()); 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 int ActivityListModel::rowCount(const QModelIndex&) const
{ {
return _finalList.count(); return _finalList.count();

View file

@ -116,7 +116,6 @@ private slots:
private: private:
void startFetchJob(AccountState* s); void startFetchJob(AccountState* s);
void combineActivityLists(); void combineActivityLists();
QString timeSpanFromNow(const QDateTime& dt) const;
QMap<AccountState*, ActivityList> _activityLists; QMap<AccountState*, ActivityList> _activityLists;
ActivityList _finalList; ActivityList _finalList;

View file

@ -26,6 +26,7 @@
#include <QUrl> #include <QUrl>
#include <QDebug> #include <QDebug>
#include <QProcess> #include <QProcess>
#include <QObject>
#include <QThread> #include <QThread>
#include <QDateTime> #include <QDateTime>
#include <QSysInfo> #include <QSysInfo>
@ -430,6 +431,36 @@ QByteArray Utility::versionOfInstalledBinary( const QString& command )
return re; 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"; static const char STOPWATCH_END_TAG[] = "_STOPWATCH_END";
void Utility::StopWatch::start() void Utility::StopWatch::start()

View file

@ -107,6 +107,16 @@ namespace Utility
OWNCLOUDSYNC_EXPORT QString fileNameForGuiUse(const QString& fName); 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 { class OWNCLOUDSYNC_EXPORT StopWatch {
private: private:
QHash<QString, quint64> _lapTimes; QHash<QString, quint64> _lapTimes;