From 207225d620a3f1c78e270ae1a8d433364d34831a Mon Sep 17 00:00:00 2001 From: Tony Motakis Date: Thu, 25 Oct 2012 03:47:14 +0200 Subject: [PATCH] HasCompilations() implementation local to the LibraryModel Instead of relying on the backend to provide us with the information of whether there are compilations in the whole of the library, we instead look into the query we are currently working with for compilations. This way we can be as granular as we want in the future. This also means we now have to add the Various artists node at the time we do the query with RunQuery() instead at BeginReset(). --- src/library/librarybackend.cpp | 12 ------------ src/library/librarybackend.h | 2 -- src/library/librarymodel.cpp | 22 +++++++++++++++------- src/library/librarymodel.h | 2 ++ tests/mock_librarybackend.h | 1 - 5 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/library/librarybackend.cpp b/src/library/librarybackend.cpp index ca098396b..df236fa8e 100644 --- a/src/library/librarybackend.cpp +++ b/src/library/librarybackend.cpp @@ -628,18 +628,6 @@ SongList LibraryBackend::GetSongsByUrl(const QUrl& url) { return songlist; } -bool LibraryBackend::HasCompilations(const QueryOptions& opt) { - LibraryQuery query(opt); - query.SetColumnSpec("%songs_table.ROWID"); - query.AddCompilationRequirement(true); - query.SetLimit(1); - - QMutexLocker l(db_->Mutex()); - if (!ExecQuery(&query)) return false; - - return query.Next(); -} - LibraryBackend::AlbumList LibraryBackend::GetCompilationAlbums(const QueryOptions& opt) { return GetAlbums(QString(), true, opt); } diff --git a/src/library/librarybackend.h b/src/library/librarybackend.h index 8c3104adf..3db6b4288 100644 --- a/src/library/librarybackend.h +++ b/src/library/librarybackend.h @@ -74,7 +74,6 @@ public: virtual SongList GetSongs( const QString& artist, const QString& album, const QueryOptions& opt = QueryOptions()) = 0; - virtual bool HasCompilations(const QueryOptions& opt = QueryOptions()) = 0; virtual SongList GetCompilationSongs(const QString& album, const QueryOptions& opt = QueryOptions()) = 0; virtual AlbumList GetAllAlbums(const QueryOptions& opt = QueryOptions()) = 0; @@ -132,7 +131,6 @@ class LibraryBackend : public LibraryBackendInterface { SongList GetSongsByAlbum(const QString& album, const QueryOptions& opt = QueryOptions()); SongList GetSongs(const QString& artist, const QString& album, const QueryOptions& opt = QueryOptions()); - bool HasCompilations(const QueryOptions& opt = QueryOptions()); SongList GetCompilationSongs(const QString& album, const QueryOptions& opt = QueryOptions()); AlbumList GetAllAlbums(const QueryOptions& opt = QueryOptions()); diff --git a/src/library/librarymodel.cpp b/src/library/librarymodel.cpp index 452a36cb1..166fb150a 100644 --- a/src/library/librarymodel.cpp +++ b/src/library/librarymodel.cpp @@ -554,6 +554,17 @@ QVariant LibraryModel::data(const LibraryItem* item, int role) const { return QVariant(); } +bool LibraryModel::HasCompilations(const LibraryQuery query) { + LibraryQuery q = query; + q.AddCompilationRequirement(true); + q.SetLimit(1); + + QMutexLocker l(backend_->db()->Mutex()); + if (!backend_->ExecQuery(&q)) return false; + + return q.Next(); +} + SqlRowList LibraryModel::RunQuery(LibraryItem* parent, bool signal) { // Information about what we want the children to be int child_level = parent == root_ ? 0 : parent->container_level + 1; @@ -573,6 +584,10 @@ SqlRowList LibraryModel::RunQuery(LibraryItem* parent, bool signal) { // Top-level artists is special - we don't want compilation albums appearing if (child_level == 0 && IsArtistGroupBy(child_type)) { + // Various artists? + if (show_various_artists_ && HasCompilations(q)) + CreateCompilationArtistNode(signal, parent); + q.AddCompilationRequirement(false); } @@ -661,13 +676,6 @@ void LibraryModel::BeginReset() { root_ = new LibraryItem(this); root_->lazy_loaded = false; - if (show_various_artists_) { - // Various artists? - if (IsArtistGroupBy(group_by_[0]) && - backend_->HasCompilations(query_options_)) - CreateCompilationArtistNode(false, root_); - } - // Smart playlists? if (show_smart_playlists_ && query_options_.filter().isEmpty()) CreateSmartPlaylists(); diff --git a/src/library/librarymodel.h b/src/library/librarymodel.h index 1c197711d..cf2a83365 100644 --- a/src/library/librarymodel.h +++ b/src/library/librarymodel.h @@ -189,6 +189,8 @@ class LibraryModel : public SimpleTreeModel { SqlRowList RunQuery(LibraryItem* parent, bool signal); void PostQuery(LibraryItem* parent, SqlRowList rows, bool signal); + bool HasCompilations(const LibraryQuery query); + void BeginReset(); // Functions for working with queries and creating items. diff --git a/tests/mock_librarybackend.h b/tests/mock_librarybackend.h index 6adf0ce09..d355ac345 100644 --- a/tests/mock_librarybackend.h +++ b/tests/mock_librarybackend.h @@ -41,7 +41,6 @@ class MockLibraryBackend : public LibraryBackendInterface { MOCK_METHOD1(GetAllArtistsWithAlbums, QStringList(const QueryOptions&)); MOCK_METHOD3(GetSongs, SongList(const QString&, const QString&, const QueryOptions&)); - MOCK_METHOD1(HasCompilations, bool(const QueryOptions&)); MOCK_METHOD2(GetCompilationSongs, SongList(const QString&, const QueryOptions&)); MOCK_METHOD1(GetAllAlbums, AlbumList(const QueryOptions&));