From e406fb90a97f1e58cc601f4383840810a0a55f03 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Wed, 16 Nov 2011 05:56:05 +0100 Subject: [PATCH] * Fixed negative time values showing up in GUI. --- .../playlist/playlistitemdelegate.cpp | 6 +- src/libtomahawk/playlist/playlistmodel.cpp | 4 +- src/libtomahawk/utils/tomahawkutils.cpp | 94 ++++++++++--------- src/libtomahawk/utils/tomahawkutils.h | 2 +- 4 files changed, 55 insertions(+), 51 deletions(-) diff --git a/src/libtomahawk/playlist/playlistitemdelegate.cpp b/src/libtomahawk/playlist/playlistitemdelegate.cpp index f38f2d89d..17dfb54f6 100644 --- a/src/libtomahawk/playlist/playlistitemdelegate.cpp +++ b/src/libtomahawk/playlist/playlistitemdelegate.cpp @@ -182,12 +182,12 @@ PlaylistItemDelegate::paintShort( QPainter* painter, const QStyleOptionViewItem& else { upperText = QString( "%1 - %2" ).arg( artist ).arg( track ); - QString playtime = TomahawkUtils::ageToString( QDateTime::fromTime_t( item->query()->playedBy().second ) ); + QString playtime = TomahawkUtils::ageToString( QDateTime::fromTime_t( item->query()->playedBy().second ), true ); if ( source == SourceList::instance()->getLocal() ) - lowerText = QString( "played %1 ago by you" ).arg( playtime ); + lowerText = QString( "played %1 by you" ).arg( playtime ); else - lowerText = QString( "played %1 ago by %2" ).arg( playtime ).arg( source->friendlyName() ); + lowerText = QString( "played %1 by %2" ).arg( playtime ).arg( source->friendlyName() ); if ( useAvatars ) pixmap = source->avatar( Source::FancyStyle ); diff --git a/src/libtomahawk/playlist/playlistmodel.cpp b/src/libtomahawk/playlist/playlistmodel.cpp index 14bf12bae..76829173e 100644 --- a/src/libtomahawk/playlist/playlistmodel.cpp +++ b/src/libtomahawk/playlist/playlistmodel.cpp @@ -101,9 +101,9 @@ PlaylistModel::loadPlaylist( const Tomahawk::playlist_ptr& playlist, bool loadEn setReadOnly( !m_playlist->author()->isLocal() ); setTitle( playlist->title() ); - setDescription( tr( "A playlist by %1, created %2 ago" ) + setDescription( tr( "A playlist by %1, created %2" ) .arg( playlist->author()->isLocal() ? tr( "you" ) : playlist->author()->friendlyName() ) - .arg( TomahawkUtils::ageToString( QDateTime::fromTime_t( playlist->createdOn() ) ) ) ); + .arg( TomahawkUtils::ageToString( QDateTime::fromTime_t( playlist->createdOn() ), true ) ) ); m_isTemporary = false; if ( !loadEntries ) diff --git a/src/libtomahawk/utils/tomahawkutils.cpp b/src/libtomahawk/utils/tomahawkutils.cpp index e3ac02f34..2040f420f 100644 --- a/src/libtomahawk/utils/tomahawkutils.cpp +++ b/src/libtomahawk/utils/tomahawkutils.cpp @@ -212,12 +212,13 @@ timeToString( int seconds ) QString -ageToString( const QDateTime& time ) +ageToString( const QDateTime& time, bool appendAgoString ) { if ( time.toTime_t() == 0 ) return QString(); QDateTime now = QDateTime::currentDateTime(); + QString agoToken = appendAgoString ? QString( " %1" ).arg( QObject::tr( "ago" ) ) : QString(); int mins = time.secsTo( now ) / 60; int hours = mins / 60; @@ -226,53 +227,56 @@ ageToString( const QDateTime& time ) int months = days / 30.42; int years = months / 12; - if ( years ) + if ( mins > 0 ) { - if ( years > 1 ) - return QObject::tr( "%1 years" ).arg( years ); - else - return QObject::tr( "%1 year" ).arg( years ); + if ( years ) + { + if ( years > 1 ) + return QObject::tr( "%1 years%2" ).arg( years ).arg( agoToken ); + else + return QObject::tr( "%1 year%2" ).arg( years ).arg( agoToken ); + } + + if ( months ) + { + if ( months > 1 ) + return QObject::tr( "%1 months%2" ).arg( months ).arg( agoToken ); + else + return QObject::tr( "%1 month%2" ).arg( months ).arg( agoToken ); + } + + if ( weeks ) + { + if ( weeks > 1 ) + return QObject::tr( "%1 weeks%2" ).arg( weeks ).arg( agoToken ); + else + return QObject::tr( "%1 week%2" ).arg( weeks ).arg( agoToken ); + } + + if ( days ) + { + if ( days > 1 ) + return QObject::tr( "%1 days%2" ).arg( days ).arg( agoToken ); + else if ( hours >= 24 ) + return QObject::tr( "%1 day%2" ).arg( days ).arg( agoToken ); + } + + if ( hours ) + { + if ( hours > 1 ) + return QObject::tr( "%1 hours%2" ).arg( hours ).arg( agoToken ); + else + return QObject::tr( "%1 hour%2" ).arg( hours ).arg( agoToken ); + } + + if ( mins ) + { + if ( mins > 1 ) + return QObject::tr( "%1 minutes%2" ).arg( mins ).arg( agoToken ); + } } - if ( months ) - { - if ( months > 1 ) - return QObject::tr( "%1 months" ).arg( months ); - else - return QObject::tr( "%1 month" ).arg( months ); - } - - if ( weeks ) - { - if ( weeks > 1 ) - return QObject::tr( "%1 weeks" ).arg( weeks ); - else - return QObject::tr( "%1 week" ).arg( weeks ); - } - - if ( days ) - { - if ( days > 1 ) - return QObject::tr( "%1 days" ).arg( days ); - else - return QObject::tr( "%1 day" ).arg( days ); - } - - if ( hours ) - { - if ( hours > 1 ) - return QObject::tr( "%1 hours" ).arg( hours ); - else - return QObject::tr( "%1 hour" ).arg( hours ); - } - - if ( mins ) - { - if ( mins > 1 ) - return QObject::tr( "%1 minutes" ).arg( mins ); - } - - return QObject::tr( "1 minute" ); + return QObject::tr( "just now" ); } diff --git a/src/libtomahawk/utils/tomahawkutils.h b/src/libtomahawk/utils/tomahawkutils.h index b772c34f7..67b251187 100644 --- a/src/libtomahawk/utils/tomahawkutils.h +++ b/src/libtomahawk/utils/tomahawkutils.h @@ -85,7 +85,7 @@ namespace TomahawkUtils DLLEXPORT QString sqlEscape( QString sql ); DLLEXPORT QString timeToString( int seconds ); - DLLEXPORT QString ageToString( const QDateTime& time ); + DLLEXPORT QString ageToString( const QDateTime& time, bool appendAgoString = false ); DLLEXPORT QString filesizeToString( unsigned int size ); DLLEXPORT QString extensionToMimetype( const QString& extension );