diff --git a/src/library/libraryquery.cpp b/src/library/libraryquery.cpp index 66798c97a3..7f9f696d4a 100644 --- a/src/library/libraryquery.cpp +++ b/src/library/libraryquery.cpp @@ -22,6 +22,7 @@ #include #include "core/song.h" +#include "core/timeconstants.h" QueryOptions::QueryOptions() : max_age_(-1), query_mode_(QueryMode_All) {} @@ -51,6 +52,8 @@ const QMap kFiletypeId = QMap( {"stream", Song::Type_Stream}, {"unknown", Song::Type_Unknown}}); +const QString kAllowedChars = "smhd"; + LibraryQuery::LibraryQuery(const QueryOptions& options) : include_unavailable_(false), join_with_fts_(false), limit_(-1) { if (!options.filter().isEmpty()) { @@ -105,32 +108,16 @@ LibraryQuery::LibraryQuery(const QueryOptions& options) if (ok) { AddWhere(columntoken, doubleVal, op); } + } else if (columntoken == "length") { + qint64 seconds = GetSecondsFromToken(val); + if (seconds > 0) { + qint64 nanoseconds = seconds * kNsecPerSec; + AddWhere(columntoken, nanoseconds, op); + } } else if (columntoken == "filetype") { AddWhere(columntoken, kFiletypeId[val]); } else if (Song::kDateColumns.contains(columntoken)) { - int seconds = 0; - QString tmp = ""; - QString allowedChars = "smhd"; - for (QChar c : val) { - if (c.isDigit()) { - tmp.append(c); - } else if (allowedChars.contains(c)) { - bool ok; - int intVal = tmp.toInt(&ok); - tmp = ""; - if (ok) { - if (c == 's') { - seconds += intVal; - } else if (c == 'm') { - seconds += intVal * 60; - } else if (c == 'h') { - seconds += intVal * 60 * 60; - } else if (c == 'd') { - seconds += intVal * 60 * 60 * 24; - } - } - } - } + int seconds = GetSecondsFromToken(val); if (seconds > 0) { int now = QDateTime::currentDateTime().toTime_t(); QString dt = QString("(%1-%2)").arg(now).arg(columntoken); @@ -181,6 +168,32 @@ LibraryQuery::LibraryQuery(const QueryOptions& options) } } +int LibraryQuery::GetSecondsFromToken(QString& val) const { + int seconds = 0; + QString tmp = ""; + for (QChar c : val) { + if (c.isDigit()) { + tmp.append(c); + } else if (kAllowedChars.contains(c)) { + bool ok = false; + int intVal = tmp.toInt(&ok); + tmp = ""; + if (ok) { + if (c == 's') { + seconds += intVal; + } else if (c == 'm') { + seconds += intVal * 60; + } else if (c == 'h') { + seconds += intVal * 60 * 60; + } else if (c == 'd') { + seconds += intVal * kSecsPerDay; + } + } + } + } + return seconds; +} + QString LibraryQuery::GetInnerQuery() { return duplicates_only_ ? QString( diff --git a/src/library/libraryquery.h b/src/library/libraryquery.h index ead5227b94..2db865772c 100644 --- a/src/library/libraryquery.h +++ b/src/library/libraryquery.h @@ -96,6 +96,7 @@ class LibraryQuery { static const QStringList kNumericCompOperators; private: + int GetSecondsFromToken(QString& val) const; QString GetInnerQuery(); bool include_unavailable_;