Rewrite collection model and search

Fixes #392
This commit is contained in:
Jonas Kvinge
2021-06-27 22:54:08 +02:00
parent ea1e4541c0
commit e477449cd4
52 changed files with 2321 additions and 2637 deletions

View File

@@ -49,7 +49,6 @@
using std::make_shared;
const char *SCollection::kSongsTable = "songs";
const char *SCollection::kFtsTable = "songs_fts";
const char *SCollection::kDirsTable = "directories";
const char *SCollection::kSubdirsTable = "subdirectories";
@@ -70,7 +69,7 @@ SCollection::SCollection(Application *app, QObject *parent)
backend()->moveToThread(app->database()->thread());
qLog(Debug) << &*backend_ << "moved to thread" << app->database()->thread();
backend_->Init(app->database(), app->task_manager(), Song::Source::Collection, QLatin1String(kSongsTable), QLatin1String(kFtsTable), QLatin1String(kDirsTable), QLatin1String(kSubdirsTable));
backend_->Init(app->database(), app->task_manager(), Song::Source::Collection, QLatin1String(kSongsTable), QLatin1String(kDirsTable), QLatin1String(kSubdirsTable));
model_ = new CollectionModel(backend_, app_, this);

View File

@@ -76,16 +76,13 @@ CollectionBackend::~CollectionBackend() {
}
void CollectionBackend::Init(SharedPtr<Database> db, SharedPtr<TaskManager> task_manager, const Song::Source source, const QString &songs_table, const QString &fts_table, const QString &dirs_table, const QString &subdirs_table) {
void CollectionBackend::Init(SharedPtr<Database> db, SharedPtr<TaskManager> task_manager, const Song::Source source, const QString &songs_table, const QString &dirs_table, const QString &subdirs_table) {
db_ = db;
task_manager_ = task_manager;
source_ = source;
songs_table_ = songs_table;
dirs_table_ = dirs_table;
subdirs_table_ = subdirs_table;
fts_table_ = fts_table;
}
void CollectionBackend::Close() {
@@ -123,6 +120,35 @@ void CollectionBackend::ReportErrors(const CollectionQuery &query) {
}
void CollectionBackend::GetAllSongsAsync(const int id) {
metaObject()->invokeMethod(this, "GetAllSongs", Qt::QueuedConnection, Q_ARG(int, id));
}
void CollectionBackend::GetAllSongs(const int id) {
QMutexLocker l(db_->Mutex());
QSqlDatabase db(db_->Connect());
SqlQuery q(db);
q.setForwardOnly(true);
q.prepare(QStringLiteral("SELECT %1 FROM %2").arg(Song::kRowIdColumnSpec, songs_table_));
if (!q.exec()) {
db_->ReportErrors(q);
emit GotSongs(SongList(), id);
return;
}
SongList songs;
while (q.next()) {
Song song(source_);
song.InitFromQuery(q, true);
songs << song;
}
emit GotSongs(songs, id);
}
void CollectionBackend::LoadDirectoriesAsync() {
QMetaObject::invokeMethod(this, &CollectionBackend::LoadDirectories, Qt::QueuedConnection);
}
@@ -596,7 +622,7 @@ void CollectionBackend::AddOrUpdateSongs(const SongList &songs) {
ScopedTransaction transaction(&db);
SongList added_songs;
SongList deleted_songs;
SongList changed_songs;
for (const Song &song : songs) {
@@ -633,19 +659,7 @@ void CollectionBackend::AddOrUpdateSongs(const SongList &songs) {
}
}
{
SqlQuery q(db);
q.prepare(QStringLiteral("UPDATE %1 SET %2 WHERE ROWID = :id").arg(fts_table_, Song::kFtsUpdateSpec));
song.BindToFtsQuery(&q);
q.BindValue(QStringLiteral(":id"), song.id());
if (!q.Exec()) {
db_->ReportErrors(q);
return;
}
}
deleted_songs << old_song;
added_songs << song;
changed_songs << song;
continue;
@@ -672,19 +686,7 @@ void CollectionBackend::AddOrUpdateSongs(const SongList &songs) {
}
}
{
SqlQuery q(db);
q.prepare(QStringLiteral("UPDATE %1 SET %2 WHERE ROWID = :id").arg(fts_table_, Song::kFtsUpdateSpec));
new_song.BindToFtsQuery(&q);
q.BindValue(QStringLiteral(":id"), new_song.id());
if (!q.Exec()) {
db_->ReportErrors(q);
return;
}
}
deleted_songs << old_song;
added_songs << new_song;
changed_songs << new_song;
continue;
}
@@ -707,17 +709,6 @@ void CollectionBackend::AddOrUpdateSongs(const SongList &songs) {
if (id == -1) return;
{ // Add to the FTS index
SqlQuery q(db);
q.prepare(QStringLiteral("INSERT INTO %1 (ROWID, %2) VALUES (:id, %3)").arg(fts_table_, Song::kFtsColumnSpec, Song::kFtsBindSpec));
q.BindValue(QStringLiteral(":id"), id);
song.BindToFtsQuery(&q);
if (!q.Exec()) {
db_->ReportErrors(q);
return;
}
}
Song song_copy(song);
song_copy.set_id(id);
added_songs << song_copy;
@@ -726,8 +717,8 @@ void CollectionBackend::AddOrUpdateSongs(const SongList &songs) {
transaction.Commit();
if (!deleted_songs.isEmpty()) emit SongsDeleted(deleted_songs);
if (!added_songs.isEmpty()) emit SongsDiscovered(added_songs);
if (!added_songs.isEmpty()) emit SongsAdded(added_songs);
if (!changed_songs.isEmpty()) emit SongsChanged(changed_songs);
UpdateTotalSongCountAsync();
UpdateTotalArtistCountAsync();
@@ -748,11 +739,12 @@ void CollectionBackend::UpdateSongsBySongID(const SongMap &new_songs) {
ScopedTransaction transaction(&db);
SongList added_songs;
SongList changed_songs;
SongList deleted_songs;
SongMap old_songs;
{
CollectionQuery query(db, songs_table_, fts_table_);
CollectionQuery query(db, songs_table_);
if (!ExecCollectionQuery(&query, old_songs)) {
ReportErrors(query);
return;
@@ -778,21 +770,10 @@ void CollectionBackend::UpdateSongsBySongID(const SongMap &new_songs) {
return;
}
}
{
SqlQuery q(db);
q.prepare(QStringLiteral("UPDATE %1 SET %2 WHERE ROWID = :id").arg(fts_table_, Song::kFtsUpdateSpec));
new_song.BindToFtsQuery(&q);
q.BindValue(QStringLiteral(":id"), old_song.id());
if (!q.Exec()) {
db_->ReportErrors(q);
return;
}
}
deleted_songs << old_song;
Song new_song_copy(new_song);
new_song_copy.set_id(old_song.id());
added_songs << new_song_copy;
changed_songs << new_song_copy;
}
@@ -813,17 +794,6 @@ void CollectionBackend::UpdateSongsBySongID(const SongMap &new_songs) {
if (id == -1) return;
{ // Add to the FTS index
SqlQuery q(db);
q.prepare(QStringLiteral("INSERT INTO %1 (ROWID, %2) VALUES (:id, %3)").arg(fts_table_, Song::kFtsColumnSpec, Song::kFtsBindSpec));
q.BindValue(QStringLiteral(":id"), id);
new_song.BindToFtsQuery(&q);
if (!q.Exec()) {
db_->ReportErrors(q);
return;
}
}
Song new_song_copy(new_song);
new_song_copy.set_id(id);
added_songs << new_song_copy;
@@ -843,15 +813,6 @@ void CollectionBackend::UpdateSongsBySongID(const SongMap &new_songs) {
return;
}
}
{
SqlQuery q(db);
q.prepare(QStringLiteral("DELETE FROM %1 WHERE ROWID = :id").arg(fts_table_));
q.BindValue(QStringLiteral(":id"), old_song.id());
if (!q.Exec()) {
db_->ReportErrors(q);
return;
}
}
deleted_songs << old_song;
}
}
@@ -859,7 +820,8 @@ void CollectionBackend::UpdateSongsBySongID(const SongMap &new_songs) {
transaction.Commit();
if (!deleted_songs.isEmpty()) emit SongsDeleted(deleted_songs);
if (!added_songs.isEmpty()) emit SongsDiscovered(added_songs);
if (!added_songs.isEmpty()) emit SongsAdded(added_songs);
if (!changed_songs.isEmpty()) emit SongsChanged(changed_songs);
UpdateTotalSongCountAsync();
UpdateTotalArtistCountAsync();
@@ -872,11 +834,10 @@ void CollectionBackend::UpdateMTimesOnly(const SongList &songs) {
QMutexLocker l(db_->Mutex());
QSqlDatabase db(db_->Connect());
SqlQuery q(db);
q.prepare(QStringLiteral("UPDATE %1 SET mtime = :mtime WHERE ROWID = :id").arg(songs_table_));
ScopedTransaction transaction(&db);
for (const Song &song : songs) {
SqlQuery q(db);
q.prepare(QStringLiteral("UPDATE %1 SET mtime = :mtime WHERE ROWID = :id").arg(songs_table_));
q.BindValue(QStringLiteral(":mtime"), song.mtime());
q.BindValue(QStringLiteral(":id"), song.id());
if (!q.Exec()) {
@@ -893,25 +854,17 @@ void CollectionBackend::DeleteSongs(const SongList &songs) {
QMutexLocker l(db_->Mutex());
QSqlDatabase db(db_->Connect());
SqlQuery remove(db);
remove.prepare(QStringLiteral("DELETE FROM %1 WHERE ROWID = :id").arg(songs_table_));
SqlQuery remove_fts(db);
remove_fts.prepare(QStringLiteral("DELETE FROM %1 WHERE ROWID = :id").arg(fts_table_));
ScopedTransaction transaction(&db);
for (const Song &song : songs) {
remove.BindValue(QStringLiteral(":id"), song.id());
if (!remove.Exec()) {
db_->ReportErrors(remove);
return;
}
remove_fts.BindValue(QStringLiteral(":id"), song.id());
if (!remove_fts.Exec()) {
db_->ReportErrors(remove_fts);
SqlQuery q(db);
q.prepare(QStringLiteral("DELETE FROM %1 WHERE ROWID = :id").arg(songs_table_));
q.BindValue(QStringLiteral(":id"), song.id());
if (!q.Exec()) {
db_->ReportErrors(q);
return;
}
}
transaction.Commit();
emit SongsDeleted(songs);
@@ -944,7 +897,7 @@ void CollectionBackend::MarkSongsUnavailable(const SongList &songs, const bool u
emit SongsDeleted(songs);
}
else {
emit SongsDiscovered(songs);
emit SongsAdded(songs);
}
UpdateTotalSongCountAsync();
@@ -958,7 +911,7 @@ QStringList CollectionBackend::GetAll(const QString &column, const CollectionFil
QMutexLocker l(db_->Mutex());
QSqlDatabase db(db_->Connect());
CollectionQuery query(db, songs_table_, fts_table_, filter_options);
CollectionQuery query(db, songs_table_, filter_options);
query.SetColumnSpec(QStringLiteral("DISTINCT ") + column);
query.AddCompilationRequirement(false);
@@ -986,13 +939,13 @@ QStringList CollectionBackend::GetAllArtistsWithAlbums(const CollectionFilterOpt
QSqlDatabase db(db_->Connect());
// Albums with 'albumartist' field set:
CollectionQuery query(db, songs_table_, fts_table_, opt);
CollectionQuery query(db, songs_table_, opt);
query.SetColumnSpec(QStringLiteral("DISTINCT albumartist"));
query.AddCompilationRequirement(false);
query.AddWhere(QStringLiteral("album"), QLatin1String(""), QStringLiteral("!="));
// Albums with no 'albumartist' (extract 'artist'):
CollectionQuery query2(db, songs_table_, fts_table_, opt);
CollectionQuery query2(db, songs_table_, opt);
query2.SetColumnSpec(QStringLiteral("DISTINCT artist"));
query2.AddCompilationRequirement(false);
query2.AddWhere(QStringLiteral("album"), QLatin1String(""), QStringLiteral("!="));
@@ -1033,7 +986,7 @@ SongList CollectionBackend::GetArtistSongs(const QString &effective_albumartist,
QSqlDatabase db(db_->Connect());
QMutexLocker l(db_->Mutex());
CollectionQuery query(db, songs_table_, fts_table_, opt);
CollectionQuery query(db, songs_table_, opt);
query.AddCompilationRequirement(false);
query.AddWhere(QStringLiteral("effective_albumartist"), effective_albumartist);
@@ -1051,7 +1004,7 @@ SongList CollectionBackend::GetAlbumSongs(const QString &effective_albumartist,
QSqlDatabase db(db_->Connect());
QMutexLocker l(db_->Mutex());
CollectionQuery query(db, songs_table_, fts_table_, opt);
CollectionQuery query(db, songs_table_, opt);
query.AddCompilationRequirement(false);
query.AddWhere(QStringLiteral("effective_albumartist"), effective_albumartist);
query.AddWhere(QStringLiteral("album"), album);
@@ -1070,7 +1023,7 @@ SongList CollectionBackend::GetSongsByAlbum(const QString &album, const Collecti
QSqlDatabase db(db_->Connect());
QMutexLocker l(db_->Mutex());
CollectionQuery query(db, songs_table_, fts_table_, opt);
CollectionQuery query(db, songs_table_, opt);
query.AddCompilationRequirement(false);
query.AddWhere(QStringLiteral("album"), album);
@@ -1207,11 +1160,10 @@ Song CollectionBackend::GetSongByUrl(const QUrl &url, const qint64 beginning) {
SqlQuery q(db);
q.prepare(QStringLiteral("SELECT %1 FROM %2 WHERE (url = :url1 OR url = :url2 OR url = :url3 OR url = :url4) AND beginning = :beginning AND unavailable = 0").arg(Song::kRowIdColumnSpec, songs_table_));
q.BindValue(QStringLiteral(":url1"), url);
q.BindValue(QStringLiteral(":url2"), url.toString());
q.BindValue(QStringLiteral(":url3"), url.toString(QUrl::FullyEncoded));
q.BindValue(QStringLiteral(":url4"), url.toEncoded());
q.BindValue(QStringLiteral(":url1"), url.toString(QUrl::FullyDecoded));
q.BindValue(QStringLiteral(":url2"), url.toString(QUrl::FullyEncoded));
q.BindValue(QStringLiteral(":url3"), url.toEncoded(QUrl::FullyDecoded));
q.BindValue(QStringLiteral(":url4"), url.toEncoded(QUrl::FullyEncoded));
q.BindValue(QStringLiteral(":beginning"), beginning);
if (!q.Exec()) {
@@ -1237,11 +1189,10 @@ Song CollectionBackend::GetSongByUrlAndTrack(const QUrl &url, const int track) {
SqlQuery q(db);
q.prepare(QStringLiteral("SELECT %1 FROM %2 WHERE (url = :url1 OR url = :url2 OR url = :url3 OR url = :url4) AND track = :track AND unavailable = 0").arg(Song::kRowIdColumnSpec, songs_table_));
q.BindValue(QStringLiteral(":url1"), url);
q.BindValue(QStringLiteral(":url2"), url.toString());
q.BindValue(QStringLiteral(":url3"), url.toString(QUrl::FullyEncoded));
q.BindValue(QStringLiteral(":url4"), url.toEncoded());
q.BindValue(QStringLiteral(":url1"), url.toString(QUrl::FullyDecoded));
q.BindValue(QStringLiteral(":url2"), url.toString(QUrl::FullyEncoded));
q.BindValue(QStringLiteral(":url3"), url.toEncoded(QUrl::FullyDecoded));
q.BindValue(QStringLiteral(":url4"), url.toEncoded(QUrl::FullyEncoded));
q.BindValue(QStringLiteral(":track"), track);
if (!q.Exec()) {
@@ -1267,23 +1218,21 @@ SongList CollectionBackend::GetSongsByUrl(const QUrl &url, const bool unavailabl
SqlQuery q(db);
q.prepare(QStringLiteral("SELECT %1 FROM %2 WHERE (url = :url1 OR url = :url2 OR url = :url3 OR url = :url4) AND unavailable = :unavailable").arg(Song::kRowIdColumnSpec, songs_table_));
q.BindValue(QStringLiteral(":url1"), url);
q.BindValue(QStringLiteral(":url2"), url.toString());
q.BindValue(QStringLiteral(":url3"), url.toString(QUrl::FullyEncoded));
q.BindValue(QStringLiteral(":url4"), url.toEncoded());
q.BindValue(QStringLiteral(":url1"), url.toString(QUrl::FullyDecoded));
q.BindValue(QStringLiteral(":url2"), url.toString(QUrl::FullyEncoded));
q.BindValue(QStringLiteral(":url3"), url.toEncoded(QUrl::FullyDecoded));
q.BindValue(QStringLiteral(":url4"), url.toEncoded(QUrl::FullyEncoded));
q.BindValue(QStringLiteral(":unavailable"), (unavailable ? 1 : 0));
SongList songs;
if (q.Exec()) {
while (q.next()) {
Song song(source_);
song.InitFromQuery(q, true);
songs << song;
}
}
else {
if (!q.Exec()) {
db_->ReportErrors(q);
return SongList();
}
while (q.next()) {
Song song(source_);
song.InitFromQuery(q, true);
songs << song;
}
return songs;
@@ -1377,7 +1326,7 @@ SongList CollectionBackend::GetCompilationSongs(const QString &album, const Coll
QMutexLocker l(db_->Mutex());
QSqlDatabase db(db_->Connect());
CollectionQuery query(db, songs_table_, fts_table_, opt);
CollectionQuery query(db, songs_table_, opt);
query.SetColumnSpec(QStringLiteral("%songs_table.ROWID, ") + Song::kColumnSpec);
query.AddCompilationRequirement(true);
query.AddWhere(QStringLiteral("album"), album);
@@ -1434,8 +1383,7 @@ void CollectionBackend::CompilationsNeedUpdating() {
}
// Now mark the songs that we think are in compilations
SongList deleted_songs;
SongList added_songs;
SongList changed_songs;
ScopedTransaction transaction(&db);
@@ -1448,12 +1396,12 @@ void CollectionBackend::CompilationsNeedUpdating() {
for (const QUrl &url : info.urls) {
if (info.artists.count() > 1) { // This directory+album is a compilation.
if (info.has_not_compilation_detected > 0) { // Run updates if any of the songs is not marked as compilations.
UpdateCompilations(db, deleted_songs, added_songs, url, true);
UpdateCompilations(db, changed_songs, url, true);
}
}
else {
if (info.has_compilation_detected > 0) {
UpdateCompilations(db, deleted_songs, added_songs, url, false);
UpdateCompilations(db, changed_songs, url, false);
}
}
}
@@ -1461,29 +1409,27 @@ void CollectionBackend::CompilationsNeedUpdating() {
transaction.Commit();
if (!deleted_songs.isEmpty()) {
emit SongsDeleted(deleted_songs);
emit SongsDiscovered(added_songs);
if (!changed_songs.isEmpty()) {
emit SongsChanged(changed_songs);
}
}
bool CollectionBackend::UpdateCompilations(const QSqlDatabase &db, SongList &deleted_songs, SongList &added_songs, const QUrl &url, const bool compilation_detected) {
bool CollectionBackend::UpdateCompilations(const QSqlDatabase &db, SongList &changed_songs, const QUrl &url, const bool compilation_detected) {
{ // Get song, so we can tell the model its updated
SqlQuery q(db);
q.prepare(QStringLiteral("SELECT %1 FROM %2 WHERE (url = :url1 OR url = :url2 OR url = :url3 OR url = :url4) AND unavailable = 0").arg(Song::kRowIdColumnSpec, songs_table_));
q.BindValue(QStringLiteral(":url1"), url);
q.BindValue(QStringLiteral(":url2"), url.toString());
q.BindValue(QStringLiteral(":url3"), url.toString(QUrl::FullyEncoded));
q.BindValue(QStringLiteral(":url4"), url.toEncoded());
q.BindValue(QStringLiteral(":url1"), url.toString(QUrl::FullyDecoded));
q.BindValue(QStringLiteral(":url2"), url.toString(QUrl::FullyEncoded));
q.BindValue(QStringLiteral(":url3"), url.toEncoded(QUrl::FullyDecoded));
q.BindValue(QStringLiteral(":url4"), url.toEncoded(QUrl::FullyEncoded));
if (q.Exec()) {
while (q.next()) {
Song song(source_);
song.InitFromQuery(q, true);
deleted_songs << song;
song.set_compilation_detected(compilation_detected);
added_songs << song;
changed_songs << song;
}
}
else {
@@ -1496,10 +1442,10 @@ bool CollectionBackend::UpdateCompilations(const QSqlDatabase &db, SongList &del
SqlQuery q(db);
q.prepare(QStringLiteral("UPDATE %1 SET compilation_detected = :compilation_detected, compilation_effective = ((compilation OR :compilation_detected OR compilation_on) AND NOT compilation_off) + 0 WHERE (url = :url1 OR url = :url2 OR url = :url3 OR url = :url4) AND unavailable = 0").arg(songs_table_));
q.BindValue(QStringLiteral(":compilation_detected"), static_cast<int>(compilation_detected));
q.BindValue(QStringLiteral(":url1"), url);
q.BindValue(QStringLiteral(":url2"), url.toString());
q.BindValue(QStringLiteral(":url3"), url.toString(QUrl::FullyEncoded));
q.BindValue(QStringLiteral(":url4"), url.toEncoded());
q.BindValue(QStringLiteral(":url1"), url.toString(QUrl::FullyDecoded));
q.BindValue(QStringLiteral(":url2"), url.toString(QUrl::FullyEncoded));
q.BindValue(QStringLiteral(":url3"), url.toEncoded(QUrl::FullyDecoded));
q.BindValue(QStringLiteral(":url4"), url.toEncoded(QUrl::FullyEncoded));
if (!q.Exec()) {
db_->ReportErrors(q);
return false;
@@ -1514,7 +1460,7 @@ CollectionBackend::AlbumList CollectionBackend::GetAlbums(const QString &artist,
QMutexLocker l(db_->Mutex());
QSqlDatabase db(db_->Connect());
CollectionQuery query(db, songs_table_, fts_table_, opt);
CollectionQuery query(db, songs_table_, opt);
query.SetColumnSpec(QStringLiteral("url, filetype, cue_path, effective_albumartist, album, compilation_effective, art_embedded, art_automatic, art_manual, art_unset"));
query.SetOrderBy(QStringLiteral("effective_albumartist, album, url"));
@@ -1605,7 +1551,7 @@ CollectionBackend::Album CollectionBackend::GetAlbumArt(const QString &effective
ret.album = album;
ret.album_artist = effective_albumartist;
CollectionQuery query(db, songs_table_, fts_table_);
CollectionQuery query(db, songs_table_);
query.SetColumnSpec(QStringLiteral("url, art_embedded, art_automatic, art_manual, art_unset"));
if (!effective_albumartist.isEmpty()) {
query.AddWhere(QStringLiteral("effective_albumartist"), effective_albumartist);
@@ -1640,54 +1586,37 @@ void CollectionBackend::UpdateEmbeddedAlbumArt(const QString &effective_albumart
QMutexLocker l(db_->Mutex());
QSqlDatabase db(db_->Connect());
// Get the songs before they're updated
CollectionQuery query(db, songs_table_, fts_table_);
query.SetColumnSpec(Song::kRowIdColumnSpec);
query.AddWhere(QStringLiteral("effective_albumartist"), effective_albumartist);
query.AddWhere(QStringLiteral("album"), album);
if (!query.Exec()) {
ReportErrors(query);
return;
{
SqlQuery q(db);
q.prepare(QStringLiteral("UPDATE %1 SET art_embedded = :art_embedded, art_unset = 0 WHERE effective_albumartist = :effective_albumartist AND album = :album AND unavailable = 0").arg(songs_table_));
q.BindValue(QStringLiteral(":art_embedded"), art_embedded ? 1 : 0);
q.BindValue(QStringLiteral(":effective_albumartist"), effective_albumartist);
q.BindValue(QStringLiteral(":album"), album);
if (!q.Exec()) {
db_->ReportErrors(q);
return;
}
}
SongList deleted_songs;
while (query.Next()) {
Song song(source_);
song.InitFromQuery(query, true);
deleted_songs << song;
SongList songs;
{
CollectionQuery q(db, songs_table_);
q.SetColumnSpec(Song::kRowIdColumnSpec);
q.AddWhere(QStringLiteral("effective_albumartist"), effective_albumartist);
q.AddWhere(QStringLiteral("album"), album);
if (!q.Exec()) {
ReportErrors(q);
return;
}
while (q.Next()) {
Song song(source_);
song.InitFromQuery(q, true);
songs << song;
}
}
// Update the songs
QString sql = QStringLiteral("UPDATE %1 SET art_embedded = :art_embedded, art_unset = 0 WHERE effective_albumartist = :effective_albumartist AND album = :album AND unavailable = 0").arg(songs_table_);
SqlQuery q(db);
q.prepare(sql);
q.BindValue(QStringLiteral(":art_embedded"), art_embedded ? 1 : 0);
q.BindValue(QStringLiteral(":effective_albumartist"), effective_albumartist);
q.BindValue(QStringLiteral(":album"), album);
if (!q.Exec()) {
db_->ReportErrors(q);
return;
}
// Now get the updated songs
if (!query.Exec()) {
ReportErrors(query);
return;
}
SongList added_songs;
while (query.Next()) {
Song song(source_);
song.InitFromQuery(query, true);
added_songs << song;
}
if (!added_songs.isEmpty() || !deleted_songs.isEmpty()) {
emit SongsDeleted(deleted_songs);
emit SongsDiscovered(added_songs);
if (!songs.isEmpty()) {
emit SongsChanged(songs);
}
}
@@ -1703,49 +1632,37 @@ void CollectionBackend::UpdateManualAlbumArt(const QString &effective_albumartis
QMutexLocker l(db_->Mutex());
QSqlDatabase db(db_->Connect());
CollectionQuery query(db, songs_table_, fts_table_);
query.SetColumnSpec(Song::kRowIdColumnSpec);
query.AddWhere(QStringLiteral("effective_albumartist"), effective_albumartist);
query.AddWhere(QStringLiteral("album"), album);
if (!query.Exec()) {
ReportErrors(query);
return;
{
SqlQuery q(db);
q.prepare(QStringLiteral("UPDATE %1 SET art_manual = :art_manual, art_unset = 0 WHERE effective_albumartist = :effective_albumartist AND album = :album AND unavailable = 0").arg(songs_table_));
q.BindValue(QStringLiteral(":art_manual"), art_manual.isValid() ? art_manual.toString(QUrl::FullyEncoded) : QLatin1String(""));
q.BindValue(QStringLiteral(":effective_albumartist"), effective_albumartist);
q.BindValue(QStringLiteral(":album"), album);
if (!q.Exec()) {
db_->ReportErrors(q);
return;
}
}
SongList deleted_songs;
while (query.Next()) {
Song song(source_);
song.InitFromQuery(query, true);
deleted_songs << song;
SongList songs;
{
CollectionQuery q(db, songs_table_);
q.SetColumnSpec(Song::kRowIdColumnSpec);
q.AddWhere(QStringLiteral("effective_albumartist"), effective_albumartist);
q.AddWhere(QStringLiteral("album"), album);
if (!q.Exec()) {
ReportErrors(q);
return;
}
while (q.Next()) {
Song song(source_);
song.InitFromQuery(q, true);
songs << song;
}
}
SqlQuery q(db);
q.prepare(QStringLiteral("UPDATE %1 SET art_manual = :art_manual, art_unset = 0 WHERE effective_albumartist = :effective_albumartist AND album = :album AND unavailable = 0").arg(songs_table_));
q.BindValue(QStringLiteral(":art_manual"), art_manual.isValid() ? art_manual.toString(QUrl::FullyEncoded) : QLatin1String(""));
q.BindValue(QStringLiteral(":effective_albumartist"), effective_albumartist);
q.BindValue(QStringLiteral(":album"), album);
if (!q.Exec()) {
db_->ReportErrors(q);
return;
}
if (!query.Exec()) {
ReportErrors(query);
return;
}
SongList added_songs;
while (query.Next()) {
Song song(source_);
song.InitFromQuery(query, true);
added_songs << song;
}
if (!added_songs.isEmpty() || !deleted_songs.isEmpty()) {
emit SongsDeleted(deleted_songs);
emit SongsDiscovered(added_songs);
if (!songs.isEmpty()) {
emit SongsChanged(songs);
}
}
@@ -1761,48 +1678,36 @@ void CollectionBackend::UnsetAlbumArt(const QString &effective_albumartist, cons
QMutexLocker l(db_->Mutex());
QSqlDatabase db(db_->Connect());
CollectionQuery query(db, songs_table_, fts_table_);
query.SetColumnSpec(Song::kRowIdColumnSpec);
query.AddWhere(QStringLiteral("effective_albumartist"), effective_albumartist);
query.AddWhere(QStringLiteral("album"), album);
if (!query.Exec()) {
ReportErrors(query);
return;
{
SqlQuery q(db);
q.prepare(QStringLiteral("UPDATE %1 SET art_unset = 1, art_manual = '', art_automatic = '', art_embedded = '' WHERE effective_albumartist = :effective_albumartist AND album = :album AND unavailable = 0").arg(songs_table_));
q.BindValue(QStringLiteral(":effective_albumartist"), effective_albumartist);
q.BindValue(QStringLiteral(":album"), album);
if (!q.Exec()) {
db_->ReportErrors(q);
return;
}
}
SongList deleted_songs;
while (query.Next()) {
Song song(source_);
song.InitFromQuery(query, true);
deleted_songs << song;
SongList songs;
{
CollectionQuery q(db, songs_table_);
q.SetColumnSpec(Song::kRowIdColumnSpec);
q.AddWhere(QStringLiteral("effective_albumartist"), effective_albumartist);
q.AddWhere(QStringLiteral("album"), album);
if (!q.Exec()) {
ReportErrors(q);
return;
}
while (q.Next()) {
Song song(source_);
song.InitFromQuery(q, true);
songs << song;
}
}
SqlQuery q(db);
q.prepare(QStringLiteral("UPDATE %1 SET art_unset = 1, art_manual = '', art_automatic = '', art_embedded = '' WHERE effective_albumartist = :effective_albumartist AND album = :album AND unavailable = 0").arg(songs_table_));
q.BindValue(QStringLiteral(":effective_albumartist"), effective_albumartist);
q.BindValue(QStringLiteral(":album"), album);
if (!q.Exec()) {
db_->ReportErrors(q);
return;
}
if (!query.Exec()) {
ReportErrors(query);
return;
}
SongList added_songs;
while (query.Next()) {
Song song(source_);
song.InitFromQuery(query, true);
added_songs << song;
}
if (!added_songs.isEmpty() || !deleted_songs.isEmpty()) {
emit SongsDeleted(deleted_songs);
emit SongsDiscovered(added_songs);
if (!songs.isEmpty()) {
emit SongsChanged(songs);
}
}
@@ -1818,49 +1723,37 @@ void CollectionBackend::ClearAlbumArt(const QString &effective_albumartist, cons
QMutexLocker l(db_->Mutex());
QSqlDatabase db(db_->Connect());
CollectionQuery query(db, songs_table_, fts_table_);
query.SetColumnSpec(Song::kRowIdColumnSpec);
query.AddWhere(QStringLiteral("effective_albumartist"), effective_albumartist);
query.AddWhere(QStringLiteral("album"), album);
if (!query.Exec()) {
ReportErrors(query);
return;
{
SqlQuery q(db);
q.prepare(QStringLiteral("UPDATE %1 SET art_embedded = 0, art_automatic = '', art_manual = '', art_unset = :art_unset WHERE effective_albumartist = :effective_albumartist AND album = :album AND unavailable = 0").arg(songs_table_));
q.BindValue(QStringLiteral(":art_unset"), art_unset ? 1 : 0);
q.BindValue(QStringLiteral(":effective_albumartist"), effective_albumartist);
q.BindValue(QStringLiteral(":album"), album);
if (!q.Exec()) {
db_->ReportErrors(q);
return;
}
}
SongList deleted_songs;
while (query.Next()) {
Song song(source_);
song.InitFromQuery(query, true);
deleted_songs << song;
SongList songs;
{
CollectionQuery q(db, songs_table_);
q.SetColumnSpec(Song::kRowIdColumnSpec);
q.AddWhere(QStringLiteral("effective_albumartist"), effective_albumartist);
q.AddWhere(QStringLiteral("album"), album);
if (!q.Exec()) {
ReportErrors(q);
return;
}
while (q.Next()) {
Song song(source_);
song.InitFromQuery(q, true);
songs << song;
}
}
SqlQuery q(db);
q.prepare(QStringLiteral("UPDATE %1 SET art_embedded = 0, art_automatic = '', art_manual = '', art_unset = :art_unset WHERE effective_albumartist = :effective_albumartist AND album = :album AND unavailable = 0").arg(songs_table_));
q.BindValue(QStringLiteral(":art_unset"), art_unset ? 1 : 0);
q.BindValue(QStringLiteral(":effective_albumartist"), effective_albumartist);
q.BindValue(QStringLiteral(":album"), album);
if (!q.Exec()) {
db_->ReportErrors(q);
return;
}
if (!query.Exec()) {
ReportErrors(query);
return;
}
SongList added_songs;
while (query.Next()) {
Song song(source_);
song.InitFromQuery(query, true);
added_songs << song;
}
if (!added_songs.isEmpty() || !deleted_songs.isEmpty()) {
emit SongsDeleted(deleted_songs);
emit SongsDiscovered(added_songs);
if (!songs.isEmpty()) {
emit SongsChanged(songs);
}
}
@@ -1869,25 +1762,9 @@ void CollectionBackend::ForceCompilation(const QString &album, const QStringList
QMutexLocker l(db_->Mutex());
QSqlDatabase db(db_->Connect());
SongList deleted_songs, added_songs;
SongList songs;
for (const QString &artist : artists) {
// Get the songs before they're updated
CollectionQuery query(db, songs_table_, fts_table_);
query.SetColumnSpec(Song::kRowIdColumnSpec);
query.AddWhere(QStringLiteral("album"), album);
if (!artist.isEmpty()) query.AddWhere(QStringLiteral("artist"), artist);
if (!query.Exec()) {
ReportErrors(query);
return;
}
while (query.Next()) {
Song song(source_);
song.InitFromQuery(query, true);
deleted_songs << song;
}
// Update the songs
QString sql(QStringLiteral("UPDATE %1 SET compilation_on = :compilation_on, compilation_off = :compilation_off, compilation_effective = ((compilation OR compilation_detected OR :compilation_on) AND NOT :compilation_off) + 0 WHERE album = :album AND unavailable = 0").arg(songs_table_));
@@ -1905,7 +1782,13 @@ void CollectionBackend::ForceCompilation(const QString &album, const QStringList
return;
}
// Now get the updated songs
// Get the updated songs
CollectionQuery query(db, songs_table_);
query.SetColumnSpec(Song::kRowIdColumnSpec);
query.AddWhere(QStringLiteral("album"), album);
if (!artist.isEmpty()) query.AddWhere(QStringLiteral("artist"), artist);
if (!query.Exec()) {
ReportErrors(query);
return;
@@ -1914,13 +1797,12 @@ void CollectionBackend::ForceCompilation(const QString &album, const QStringList
while (query.Next()) {
Song song(source_);
song.InitFromQuery(query, true);
added_songs << song;
songs << song;
}
}
if (!added_songs.isEmpty() || !deleted_songs.isEmpty()) {
emit SongsDeleted(deleted_songs);
emit SongsDiscovered(added_songs);
if (!songs.isEmpty()) {
emit SongsChanged(songs);
}
}
@@ -2035,15 +1917,6 @@ void CollectionBackend::DeleteAll() {
}
}
{
SqlQuery q(db);
q.prepare(QStringLiteral("DELETE FROM ") + fts_table_);
if (!q.Exec()) {
db_->ReportErrors(q);
return;
}
}
t.Commit();
}

View File

@@ -80,12 +80,13 @@ class CollectionBackendInterface : public QObject {
using AlbumList = QList<Album>;
virtual QString songs_table() const = 0;
virtual QString fts_table() const = 0;
virtual Song::Source source() const = 0;
virtual SharedPtr<Database> db() const = 0;
virtual void GetAllSongsAsync(const int id = 0) = 0;
// Get a list of directories in the collection. Emits DirectoriesDiscovered.
virtual void LoadDirectoriesAsync() = 0;
@@ -145,7 +146,8 @@ class CollectionBackend : public CollectionBackendInterface {
~CollectionBackend();
void Init(SharedPtr<Database> db, SharedPtr<TaskManager> task_manager, const Song::Source source, const QString &songs_table, const QString &fts_table, const QString &dirs_table = QString(), const QString &subdirs_table = QString());
void Init(SharedPtr<Database> db, SharedPtr<TaskManager> task_manager, const Song::Source source, const QString &songs_table, const QString &dirs_table = QString(), const QString &subdirs_table = QString());
void Close();
void ExitAsync();
@@ -157,10 +159,11 @@ class CollectionBackend : public CollectionBackendInterface {
SharedPtr<Database> db() const override { return db_; }
QString songs_table() const override { return songs_table_; }
QString fts_table() const override { return fts_table_; }
QString dirs_table() const { return dirs_table_; }
QString subdirs_table() const { return subdirs_table_; }
void GetAllSongsAsync(const int id = 0) override;
// Get a list of directories in the collection. Emits DirectoriesDiscovered.
void LoadDirectoriesAsync() override;
@@ -235,6 +238,7 @@ class CollectionBackend : public CollectionBackendInterface {
public slots:
void Exit();
void GetAllSongs(const int id);
void LoadDirectories();
void UpdateTotalSongCount();
void UpdateTotalArtistCount();
@@ -275,8 +279,10 @@ class CollectionBackend : public CollectionBackendInterface {
void DirectoryAdded(const CollectionDirectory &dir, const CollectionSubdirectoryList &subdir);
void DirectoryDeleted(const CollectionDirectory &dir);
void SongsDiscovered(const SongList &songs);
void GotSongs(const SongList &songs, const int id);
void SongsAdded(const SongList &songs);
void SongsDeleted(const SongList &songs);
void SongsChanged(const SongList &songs);
void SongsStatisticsChanged(const SongList &songs, const bool save_tags = false);
void DatabaseReset();
@@ -301,7 +307,7 @@ class CollectionBackend : public CollectionBackendInterface {
int has_not_compilation_detected;
};
bool UpdateCompilations(const QSqlDatabase &db, SongList &deleted_songs, SongList &added_songs, const QUrl &url, const bool compilation_detected);
bool UpdateCompilations(const QSqlDatabase &db, SongList &changed_songs, const QUrl &url, const bool compilation_detected);
AlbumList GetAlbums(const QString &artist, const QString &album_artist, const bool compilation_required = false, const CollectionFilterOptions &opt = CollectionFilterOptions());
AlbumList GetAlbums(const QString &artist, const bool compilation_required, const CollectionFilterOptions &opt = CollectionFilterOptions());
CollectionSubdirectoryList SubdirsInDirectory(const int id, QSqlDatabase &db);
@@ -319,7 +325,6 @@ class CollectionBackend : public CollectionBackendInterface {
QString songs_table_;
QString dirs_table_;
QString subdirs_table_;
QString fts_table_;
QThread *original_thread_;
};

View File

@@ -0,0 +1,335 @@
/*
* Strawberry Music Player
* Copyright 2021-2024, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include <QSortFilterProxyModel>
#include <QVariant>
#include <QString>
#include <QStringList>
#include "core/logging.h"
#include "utilities/timeconstants.h"
#include "utilities/searchparserutils.h"
#include "collectionfilter.h"
#include "collectionmodel.h"
#include "collectionitem.h"
const QStringList CollectionFilter::Operators = QStringList() << QStringLiteral(":")
<< QStringLiteral("=")
<< QStringLiteral("==")
<< QStringLiteral("<>")
<< QStringLiteral("<")
<< QStringLiteral("<=")
<< QStringLiteral(">")
<< QStringLiteral(">=");
CollectionFilter::CollectionFilter(QObject *parent) : QSortFilterProxyModel(parent) {}
bool CollectionFilter::filterAcceptsRow(const int source_row, const QModelIndex &source_parent) const {
CollectionModel *model = qobject_cast<CollectionModel*>(sourceModel());
if (!model) return false;
const QModelIndex idx = sourceModel()->index(source_row, 0, source_parent);
if (!idx.isValid()) return false;
CollectionItem *item = model->IndexToItem(idx);
if (!item) return false;
if (item->type == CollectionItem::Type::LoadingIndicator) return true;
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
QString filter_text = filterRegularExpression().pattern().remove(QLatin1Char('\\'));
#else
QString filter_text = filterRegExp().pattern();
#endif
if (filter_text.isEmpty()) return true;
filter_text = filter_text.replace(QRegularExpression(QStringLiteral("\\s*:\\s*")), QStringLiteral(":"))
.replace(QRegularExpression(QStringLiteral("\\s*=\\s*")), QStringLiteral("="))
.replace(QRegularExpression(QStringLiteral("\\s*==\\s*")), QStringLiteral("=="))
.replace(QRegularExpression(QStringLiteral("\\s*<>\\s*")), QStringLiteral("<>"))
.replace(QRegularExpression(QStringLiteral("\\s*<\\s*")), QStringLiteral("<"))
.replace(QRegularExpression(QStringLiteral("\\s*>\\s*")), QStringLiteral(">"))
.replace(QRegularExpression(QStringLiteral("\\s*<=\\s*")), QStringLiteral("<="))
.replace(QRegularExpression(QStringLiteral("\\s*>=\\s*")), QStringLiteral(">="));
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
const QStringList tokens = filter_text.split(QRegularExpression(QStringLiteral("\\s+")), Qt::SkipEmptyParts);
#else
const QStringList tokens = filter_text.split(QRegularExpression(QStringLiteral("\\s+")), QString::SkipEmptyParts);
#endif
filter_text.clear();
FilterList filters;
static QRegularExpression operator_regex(QStringLiteral("(=|<[>=]?|>=?|!=)"));
for (int i = 0; i < tokens.count(); ++i) {
const QString &token = tokens[i];
if (token.contains(QLatin1Char(':'))) {
QString field = token.section(QLatin1Char(':'), 0, 0).remove(QLatin1Char(':')).trimmed();
QString value = token.section(QLatin1Char(':'), 1, -1).remove(QLatin1Char(':')).trimmed();
if (field.isEmpty() || value.isEmpty()) continue;
if (Song::kTextSearchColumns.contains(field, Qt::CaseInsensitive) && value.count(QLatin1Char('"')) <= 2) {
bool quotation_mark_start = false;
bool quotation_mark_end = false;
if (value.left(1) == QLatin1Char('"')) {
value.remove(0, 1);
quotation_mark_start = true;
if (value.length() >= 1 && value.count(QLatin1Char('"')) == 1) {
value = value.section(QLatin1Char(QLatin1Char('"')), 0, 0).remove(QLatin1Char('"')).trimmed();
quotation_mark_end = true;
}
}
for (int y = i + 1; y < tokens.count() && !quotation_mark_end; ++y) {
QString next_value = tokens[y];
if (!quotation_mark_start && ContainsOperators(next_value)) {
break;
}
if (quotation_mark_start && next_value.contains(QLatin1Char('"'))) {
next_value = next_value.section(QLatin1Char(QLatin1Char('"')), 0, 0).remove(QLatin1Char('"')).trimmed();
quotation_mark_end = true;
}
value.append(QLatin1Char(' ') + next_value);
i = y;
}
if (!field.isEmpty() && !value.isEmpty()) {
filters.insert(field, Filter(field, value));
}
continue;
}
}
else if (token.contains(operator_regex)) {
QRegularExpressionMatch re_match = operator_regex.match(token);
if (re_match.hasMatch()) {
const QString foperator = re_match.captured(0);
const QString field = token.section(foperator, 0, 0).remove(foperator).trimmed();
const QString value = token.section(foperator, 1, -1).remove(foperator).trimmed();
if (value.isEmpty()) continue;
if (Song::kNumericalSearchColumns.contains(field, Qt::CaseInsensitive)) {
if (Song::kIntSearchColumns.contains(field, Qt::CaseInsensitive)) {
bool ok = false;
const int value_int = value.toInt(&ok);
if (ok) {
filters.insert(field, Filter(field, value_int, foperator));
continue;
}
}
else if (Song::kUIntSearchColumns.contains(field, Qt::CaseInsensitive)) {
bool ok = false;
const uint value_uint = value.toUInt(&ok);
if (ok) {
filters.insert(field, Filter(field, value_uint, foperator));
continue;
}
}
else if (field.compare(QStringLiteral("length"), Qt::CaseInsensitive) == 0) {
filters.insert(field, Filter(field, static_cast<qint64>(Utilities::ParseSearchTime(value)) * kNsecPerSec, foperator));
continue;
}
else if (field.compare(QStringLiteral("rating"), Qt::CaseInsensitive) == 0) {
filters.insert(field, Filter(field, Utilities::ParseSearchRating(value), foperator));
}
}
}
}
if (!filter_text.isEmpty()) filter_text.append(QLatin1Char(' '));
filter_text += token;
}
if (filter_text.isEmpty() && filters.isEmpty()) return true;
return ItemMatchesFilters(item, filters, filter_text);
}
bool CollectionFilter::ItemMatchesFilters(CollectionItem *item, const FilterList &filters, const QString &filter_text) {
if (item->type == CollectionItem::Type::Song &&
item->metadata.is_valid() &&
ItemMetadataMatchesFilters(item->metadata, filters, filter_text)) {
return true;
}
for (CollectionItem *child : std::as_const(item->children)) {
if (ItemMatchesFilters(child, filters, filter_text)) return true;
}
return false;
}
bool CollectionFilter::ItemMetadataMatchesFilters(const Song &metadata, const FilterList &filters, const QString &filter_text) {
for (FilterList::const_iterator it = filters.begin() ; it != filters.end() ; ++it) {
const QString &field = it.key();
const Filter &filter = it.value();
const QVariant &value = filter.value;
const QString &foperator = filter.foperator;
if (field.isEmpty() || !value.isValid()) {
continue;
}
const QVariant data = DataFromField(field, metadata);
if (
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
value.metaType() != data.metaType()
#else
value.type() != data.type()
#endif
|| !FieldValueMatchesData(value, data, foperator)) {
return false;
}
}
return filter_text.isEmpty() || ItemMetadataMatchesFilterText(metadata, filter_text);
}
bool CollectionFilter::ItemMetadataMatchesFilterText(const Song &metadata, const QString &filter_text) {
return metadata.effective_albumartist().contains(filter_text, Qt::CaseInsensitive) ||
metadata.artist().contains(filter_text, Qt::CaseInsensitive) ||
metadata.album().contains(filter_text, Qt::CaseInsensitive) ||
metadata.title().contains(filter_text, Qt::CaseInsensitive) ||
metadata.composer().contains(filter_text, Qt::CaseInsensitive) ||
metadata.performer().contains(filter_text, Qt::CaseInsensitive) ||
metadata.grouping().contains(filter_text, Qt::CaseInsensitive) ||
metadata.genre().contains(filter_text, Qt::CaseInsensitive) ||
metadata.comment().contains(filter_text, Qt::CaseInsensitive);
}
QVariant CollectionFilter::DataFromField(const QString &field, const Song &metadata) {
if (field == QStringLiteral("albumartist")) return metadata.effective_albumartist();
if (field == QStringLiteral("artist")) return metadata.artist();
if (field == QStringLiteral("album")) return metadata.album();
if (field == QStringLiteral("title")) return metadata.title();
if (field == QStringLiteral("composer")) return metadata.composer();
if (field == QStringLiteral("performer")) return metadata.performer();
if (field == QStringLiteral("grouping")) return metadata.grouping();
if (field == QStringLiteral("genre")) return metadata.genre();
if (field == QStringLiteral("comment")) return metadata.comment();
if (field == QStringLiteral("track")) return metadata.track();
if (field == QStringLiteral("year")) return metadata.year();
if (field == QStringLiteral("length")) return metadata.length_nanosec();
if (field == QStringLiteral("samplerate")) return metadata.samplerate();
if (field == QStringLiteral("bitdepth")) return metadata.bitdepth();
if (field == QStringLiteral("bitrate")) return metadata.bitrate();
if (field == QStringLiteral("rating")) return metadata.rating();
if (field == QStringLiteral("playcount")) return metadata.playcount();
if (field == QStringLiteral("skipcount")) return metadata.skipcount();
return QVariant();
}
bool CollectionFilter::FieldValueMatchesData(const QVariant &value, const QVariant &data, const QString &foperator) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
switch (value.metaType().id()) {
#else
switch (value.userType()) {
#endif
case QMetaType::QString:{
const QString str_value = value.toString();
const QString str_data = data.toString();
return str_data.contains(str_value, Qt::CaseInsensitive);
}
case QMetaType::Int:{
return FieldIntValueMatchesData(value.toInt(), foperator, data.toInt());
}
case QMetaType::UInt:{
return FieldUIntValueMatchesData(value.toUInt(), foperator, data.toUInt());
}
case QMetaType::LongLong:{
return FieldLongLongValueMatchesData(value.toLongLong(), foperator, data.toLongLong());
}
case QMetaType::Float:{
return FieldFloatValueMatchesData(value.toFloat(), foperator, data.toFloat());
}
default:{
return false;
}
}
return false;
}
template<typename T>
bool CollectionFilter::FieldNumericalValueMatchesData(const T value, const QString &foperator, const T data) {
if (foperator == QStringLiteral("=") || foperator == QStringLiteral("==")) {
return data == value;
}
else if (foperator == QStringLiteral("!=") || foperator == QStringLiteral("<>")) {
return data != value;
}
else if (foperator == QStringLiteral("<")) {
return data < value;
}
else if (foperator == QStringLiteral(">")) {
return data > value;
}
else if (foperator == QStringLiteral(">=")) {
return data >= value;
}
else if (foperator == QStringLiteral("<=")) {
return data <= value;
}
return false;
}
bool CollectionFilter::FieldIntValueMatchesData(const int value, const QString &foperator, const int data) {
return FieldNumericalValueMatchesData(value, foperator, data);
}
bool CollectionFilter::FieldUIntValueMatchesData(const uint value, const QString &foperator, const uint data) {
return FieldNumericalValueMatchesData(value, foperator, data);
}
bool CollectionFilter::FieldLongLongValueMatchesData(const qint64 value, const QString &foperator, const qint64 data) {
return FieldNumericalValueMatchesData(value, foperator, data);
}
bool CollectionFilter::FieldFloatValueMatchesData(const float value, const QString &foperator, const float data) {
return FieldNumericalValueMatchesData(value, foperator, data);
}
bool CollectionFilter::ContainsOperators(const QString &token) {
for (const QString &foperator : Operators) {
if (token.contains(foperator, Qt::CaseInsensitive)) return true;
}
return false;
}

View File

@@ -0,0 +1,69 @@
/*
* Strawberry Music Player
* Copyright 2021-2024, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef COLLECTIONFILTER_H
#define COLLECTIONFILTER_H
#include "config.h"
#include <QtGlobal>
#include <QObject>
#include <QSortFilterProxyModel>
#include <QVariant>
#include <QString>
#include <QStringList>
#include "core/song.h"
class CollectionItem;
class CollectionFilter : public QSortFilterProxyModel {
Q_OBJECT
public:
explicit CollectionFilter(QObject *parent = nullptr);
protected:
bool filterAcceptsRow(const int source_row, const QModelIndex &source_parent) const override;
private:
static const QStringList Operators;
struct Filter {
public:
Filter(const QString &_field = QString(), const QVariant &_value = QVariant(), const QString &_foperator = QString()) : field(_field), value(_value), foperator(_foperator) {}
QString field;
QVariant value;
QString foperator;
};
using FilterList = QMap<QString, Filter>;
static bool ItemMatchesFilters(CollectionItem *item, const FilterList &filters, const QString &filter_text);
static bool ItemMetadataMatchesFilters(const Song &metadata, const FilterList &filters, const QString &filter_text);
static bool ItemMetadataMatchesFilterText(const Song &metadata, const QString &filter_text);
static QVariant DataFromField(const QString &field, const Song &metadata);
static bool FieldValueMatchesData(const QVariant &value, const QVariant &data, const QString &foperator);
template<typename T>
static bool FieldNumericalValueMatchesData(const T value, const QString &foperator, const T data);
static bool FieldIntValueMatchesData(const int value, const QString &foperator, const int data);
static bool FieldUIntValueMatchesData(const uint value, const QString &foperator, const uint data);
static bool FieldLongLongValueMatchesData(const qint64 value, const QString &foperator, const qint64 data);
static bool FieldFloatValueMatchesData(const float value, const QString &foperator, const float data);
static bool ContainsOperators(const QString &token);
};
#endif // COLLECTIONFILTER_H

View File

@@ -50,6 +50,8 @@
#include "core/settings.h"
#include "collectionfilteroptions.h"
#include "collectionmodel.h"
#include "collectionfilter.h"
#include "collectionquery.h"
#include "savedgroupingmanager.h"
#include "collectionfilterwidget.h"
#include "groupbydialog.h"
@@ -62,6 +64,7 @@ CollectionFilterWidget::CollectionFilterWidget(QWidget *parent)
: QWidget(parent),
ui_(new Ui_CollectionFilterWidget),
model_(nullptr),
filter_(nullptr),
group_by_dialog_(new GroupByDialog(this)),
groupings_manager_(nullptr),
filter_age_menu_(nullptr),
@@ -74,8 +77,8 @@ CollectionFilterWidget::CollectionFilterWidget(QWidget *parent)
ui_->setupUi(this);
QString available_fields = Song::kFtsColumns.join(QStringLiteral(", ")).replace(QRegularExpression(QStringLiteral("\\bfts")), QLatin1String(""));
available_fields += QStringLiteral(", ") + Song::kNumericalColumns.join(QStringLiteral(", "));
QString available_fields = Song::kTextSearchColumns.join(QStringLiteral(", "));
available_fields += QStringLiteral(", ") + Song::kNumericalSearchColumns.join(QStringLiteral(", "));
ui_->search_field->setToolTip(
QStringLiteral("<html><head/><body><p>") +
@@ -125,12 +128,12 @@ CollectionFilterWidget::CollectionFilterWidget(QWidget *parent)
filter_age_menu_ = new QMenu(tr("Show"), this);
filter_age_menu_->addActions(filter_age_group->actions());
filter_ages_[ui_->filter_age_all] = -1;
filter_ages_[ui_->filter_age_today] = 60 * 60 * 24;
filter_ages_[ui_->filter_age_week] = 60 * 60 * 24 * 7;
filter_ages_[ui_->filter_age_month] = 60 * 60 * 24 * 30;
filter_ages_[ui_->filter_age_three_months] = 60 * 60 * 24 * 30 * 3;
filter_ages_[ui_->filter_age_year] = 60 * 60 * 24 * 365;
filter_max_ages_[ui_->filter_age_all] = -1;
filter_max_ages_[ui_->filter_age_today] = 60 * 60 * 24;
filter_max_ages_[ui_->filter_age_week] = 60 * 60 * 24 * 7;
filter_max_ages_[ui_->filter_age_month] = 60 * 60 * 24 * 30;
filter_max_ages_[ui_->filter_age_three_months] = 60 * 60 * 24 * 30 * 3;
filter_max_ages_[ui_->filter_age_year] = 60 * 60 * 24 * 365;
group_by_menu_ = new QMenu(tr("Group by"), this);
@@ -156,29 +159,30 @@ CollectionFilterWidget::CollectionFilterWidget(QWidget *parent)
CollectionFilterWidget::~CollectionFilterWidget() { delete ui_; }
void CollectionFilterWidget::Init(CollectionModel *model) {
void CollectionFilterWidget::Init(CollectionModel *model, CollectionFilter *filter) {
if (model_) {
QObject::disconnect(model_, nullptr, this, nullptr);
QObject::disconnect(model_, nullptr, group_by_dialog_, nullptr);
QObject::disconnect(group_by_dialog_, nullptr, model_, nullptr);
const QList<QAction*> filter_ages = filter_ages_.keys();
for (QAction *action : filter_ages) {
const QList<QAction*> actions = filter_max_ages_.keys();
for (QAction *action : actions) {
QObject::disconnect(action, &QAction::triggered, model_, nullptr);
}
}
model_ = model;
filter_ = filter;
// Connect signals
QObject::connect(model_, &CollectionModel::GroupingChanged, group_by_dialog_, &GroupByDialog::CollectionGroupingChanged);
QObject::connect(model_, &CollectionModel::GroupingChanged, this, &CollectionFilterWidget::GroupingChanged);
QObject::connect(group_by_dialog_, &GroupByDialog::Accepted, model_, &CollectionModel::SetGroupBy);
const QList<QAction*> filter_ages = filter_ages_.keys();
for (QAction *action : filter_ages) {
int age = filter_ages_[action];
QObject::connect(action, &QAction::triggered, this, [this, age]() { model_->SetFilterAge(age); } );
const QList<QAction*> actions = filter_max_ages_.keys();
for (QAction *action : actions) {
int filter_max_age = filter_max_ages_[action];
QObject::connect(action, &QAction::triggered, this, [this, filter_max_age]() { model_->SetFilterMaxAge(filter_max_age); } );
}
// Load settings
@@ -217,6 +221,10 @@ void CollectionFilterWidget::SetSettingsPrefix(const QString &prefix) {
}
void CollectionFilterWidget::setFilter(CollectionFilter *filter) {
filter_ = filter;
}
void CollectionFilterWidget::ReloadSettings() {
Settings s;
@@ -518,9 +526,6 @@ void CollectionFilterWidget::keyReleaseEvent(QKeyEvent *e) {
void CollectionFilterWidget::FilterTextChanged(const QString &text) {
// Searching with one or two characters can be very expensive on the database even with FTS,
// so if there are a large number of songs in the database introduce a small delay before actually filtering the model,
// so if the user is typing the first few characters of something it will be quicker.
const bool delay = (delay_behaviour_ == DelayBehaviour::AlwaysDelayed) || (delay_behaviour_ == DelayBehaviour::DelayedOnLargeLibraries && !text.isEmpty() && text.length() < 3 && model_->total_song_count() >= 100000);
if (delay) {
@@ -535,9 +540,8 @@ void CollectionFilterWidget::FilterTextChanged(const QString &text) {
void CollectionFilterWidget::FilterDelayTimeout() {
emit Filter(ui_->search_field->text());
if (filter_applies_to_model_) {
model_->SetFilterText(ui_->search_field->text());
filter_->setFilterFixedString(ui_->search_field->text());
}
}

View File

@@ -41,6 +41,7 @@ class QKeyEvent;
class GroupByDialog;
class SavedGroupingManager;
class CollectionFilter;
class Ui_CollectionFilterWidget;
class CollectionFilterWidget : public QWidget {
@@ -58,7 +59,9 @@ class CollectionFilterWidget : public QWidget {
AlwaysDelayed,
};
void Init(CollectionModel *model);
void Init(CollectionModel *model, CollectionFilter *filter);
void setFilter(CollectionFilter *filter);
static QActionGroup *CreateGroupByActions(const QString &saved_groupings_settings_group, QObject *parent);
@@ -94,7 +97,6 @@ class CollectionFilterWidget : public QWidget {
void UpPressed();
void DownPressed();
void ReturnPressed();
void Filter(const QString &text);
protected:
void keyReleaseEvent(QKeyEvent *e) override;
@@ -115,6 +117,7 @@ class CollectionFilterWidget : public QWidget {
private:
Ui_CollectionFilterWidget *ui_;
CollectionModel *model_;
CollectionFilter *filter_;
GroupByDialog *group_by_dialog_;
SavedGroupingManager *groupings_manager_;
@@ -123,7 +126,7 @@ class CollectionFilterWidget : public QWidget {
QMenu *group_by_menu_;
QMenu *collection_menu_;
QActionGroup *group_by_group_;
QHash<QAction*, int> filter_ages_;
QHash<QAction*, int> filter_max_ages_;
QTimer *filter_delay_;

View File

@@ -29,24 +29,27 @@
class CollectionItem : public SimpleTreeItem<CollectionItem> {
public:
enum Type {
Type_Root,
Type_Divider,
Type_Container,
Type_Song,
Type_LoadingIndicator,
enum class Type {
Root,
Divider,
Container,
Song,
LoadingIndicator,
};
explicit CollectionItem(SimpleTreeModel<CollectionItem> *_model)
: SimpleTreeItem<CollectionItem>(Type_Root, _model),
: SimpleTreeItem<CollectionItem>(_model),
type(Type::Root),
container_level(-1),
compilation_artist_node_(nullptr) {}
explicit CollectionItem(Type _type, CollectionItem *_parent = nullptr)
: SimpleTreeItem<CollectionItem>(_type, _parent),
explicit CollectionItem(const Type _type, CollectionItem *_parent = nullptr)
: SimpleTreeItem<CollectionItem>(_parent),
type(_type),
container_level(-1),
compilation_artist_node_(nullptr) {}
Type type;
int container_level;
Song metadata;
CollectionItem *compilation_artist_node_;
@@ -55,4 +58,6 @@ class CollectionItem : public SimpleTreeItem<CollectionItem> {
Q_DISABLE_COPY(CollectionItem)
};
Q_DECLARE_METATYPE(CollectionItem::Type)
#endif // COLLECTIONITEM_H

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,5 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2018-2024, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
@@ -44,6 +42,7 @@
#include <QIcon>
#include <QPixmap>
#include <QNetworkDiskCache>
#include <QQueue>
#include "core/shared_ptr.h"
#include "core/simpletreemodel.h"
@@ -51,15 +50,17 @@
#include "core/sqlrow.h"
#include "covermanager/albumcoverloaderoptions.h"
#include "covermanager/albumcoverloaderresult.h"
#include "collectionmodelupdate.h"
#include "collectionfilteroptions.h"
#include "collectionqueryoptions.h"
#include "collectionitem.h"
class QTimer;
class Settings;
class Application;
class CollectionBackend;
class CollectionDirectoryModel;
class CollectionFilter;
class CollectionModel : public SimpleTreeModel<CollectionItem> {
Q_OBJECT
@@ -69,13 +70,12 @@ class CollectionModel : public SimpleTreeModel<CollectionItem> {
~CollectionModel() override;
static const int kPrettyCoverSize;
static const char *kPixmapDiskCacheDir;
enum Role {
Role_Type = Qt::UserRole + 1,
Role_ContainerType,
Role_SortText,
Role_Key,
Role_ContainerKey,
Role_Artist,
Role_IsDivider,
Role_Editable,
@@ -125,147 +125,123 @@ class CollectionModel : public SimpleTreeModel<CollectionItem> {
bool operator!=(const Grouping other) const { return !(*this == other); }
};
struct QueryResult {
QueryResult() : create_va(false) {}
struct Options {
Options() : group_by(GroupBy::AlbumArtist, GroupBy::AlbumDisc, GroupBy::None),
show_dividers(true),
show_pretty_covers(true),
show_various_artists(true),
sort_skips_articles(true),
separate_albums_by_grouping(false) {}
SqlRowList rows;
bool create_va;
Grouping group_by;
bool show_dividers;
bool show_pretty_covers;
bool show_various_artists;
bool sort_skips_articles;
bool separate_albums_by_grouping;
CollectionFilterOptions filter_options;
};
SharedPtr<CollectionBackend> backend() const { return backend_; }
CollectionFilter *filter() const { return filter_; }
void Init();
void Reset();
void ReloadSettings();
CollectionDirectoryModel *directory_model() const { return dir_model_; }
// Call before Init()
void set_show_various_artists(const bool show_various_artists) { show_various_artists_ = show_various_artists; }
// Get information about the collection
void GetChildSongs(CollectionItem *item, QList<QUrl> *urls, SongList *songs, QSet<int> *song_ids) const;
SongList GetChildSongs(const QModelIndex &idx) const;
SongList GetChildSongs(const QModelIndexList &indexes) const;
// Might be accurate
int total_song_count() const { return total_song_count_; }
int total_artist_count() const { return total_artist_count_; }
int total_album_count() const { return total_album_count_; }
// QAbstractItemModel
QVariant data(const QModelIndex &idx, const int role = Qt::DisplayRole) const override;
Qt::ItemFlags flags(const QModelIndex &idx) const override;
QStringList mimeTypes() const override;
QMimeData *mimeData(const QModelIndexList &indexes) const override;
bool canFetchMore(const QModelIndex &parent) const override;
// Whether or not to use album cover art, if it exists, in the collection view
void set_pretty_covers(const bool use_pretty_covers);
bool use_pretty_covers() const { return use_pretty_covers_; }
// Whether or not to show letters heading in the collection view
void set_show_dividers(const bool show_dividers);
// Whether to skip articles such as “The” when sorting artist names
void set_sort_skips_articles(const bool sort_skips_articles);
// Reload settings.
void ReloadSettings();
// Utility functions for manipulating text
static QString TextOrUnknown(const QString &text);
static QString PrettyYearAlbum(const int year, const QString &album);
static QString PrettyAlbumDisc(const QString &album, const int disc);
static QString PrettyYearAlbumDisc(const int year, const QString &album, const int disc);
static QString PrettyDisc(const int disc);
static QString SortText(QString text);
static QString SortTextForNumber(const int number);
static QString SortTextForArtist(QString artist, const bool skip_articles);
static QString SortTextForSong(const Song &song);
static QString SortTextForYear(const int year);
static QString SortTextForBitrate(const int bitrate);
quint64 icon_cache_disk_size() { return sIconCache->cacheSize(); }
const CollectionModel::Grouping GetGroupBy() const { return options_current_.group_by; }
void SetGroupBy(const CollectionModel::Grouping g, const std::optional<bool> separate_albums_by_grouping = std::optional<bool>());
static bool IsArtistGroupBy(const GroupBy group_by) {
return group_by == CollectionModel::GroupBy::Artist || group_by == CollectionModel::GroupBy::AlbumArtist;
}
static bool IsAlbumGroupBy(const GroupBy group_by) { return group_by == GroupBy::Album || group_by == GroupBy::YearAlbum || group_by == GroupBy::AlbumDisc || group_by == GroupBy::YearAlbumDisc || group_by == GroupBy::OriginalYearAlbum || group_by == GroupBy::OriginalYearAlbumDisc; }
void set_use_lazy_loading(const bool value) { use_lazy_loading_ = value; }
QMap<QString, CollectionItem*> container_nodes(const int i) { return container_nodes_[i]; }
QList<CollectionItem*> song_nodes() const { return song_nodes_.values(); }
int divider_nodes_count() const { return divider_nodes_.count(); }
// QAbstractItemModel
QVariant data(const QModelIndex &idx, const int role = Qt::DisplayRole) const override;
Qt::ItemFlags flags(const QModelIndex &idx) const override;
QStringList mimeTypes() const override;
QMimeData *mimeData(const QModelIndexList &indexes) const override;
// Utility functions for manipulating text
static QString DisplayText(const GroupBy group_by, const Song &song);
static QString TextOrUnknown(const QString &text);
static QString PrettyYearAlbum(const int year, const QString &album);
static QString PrettyAlbumDisc(const QString &album, const int disc);
static QString PrettyYearAlbumDisc(const int year, const QString &album, const int disc);
static QString PrettyDisc(const int disc);
static QString PrettyFormat(const Song &song);
QString SortText(const GroupBy group_by, const int container_level, const Song &song, const bool sort_skips_articles);
static QString SortText(QString text);
static QString SortTextForNumber(const int number);
static QString SortTextForArtist(QString artist, const bool skip_articles);
static QString SortTextForSong(const Song &song);
static QString SortTextForYear(const int year);
static QString SortTextForBitrate(const int bitrate);
static bool IsSongTitleDataChanged(const Song &song1, const Song &song2);
QString ContainerKey(const GroupBy group_by, const Song &song) const;
// Get information about the collection
void GetChildSongs(CollectionItem *item, QList<QUrl> *urls, SongList *songs, QSet<int> *song_ids) const;
SongList GetChildSongs(const QModelIndex &idx) const;
SongList GetChildSongs(const QModelIndexList &indexes) const;
void ExpandAll(CollectionItem *item = nullptr) const;
const CollectionModel::Grouping GetGroupBy() const { return group_by_; }
void SetGroupBy(const CollectionModel::Grouping g, const std::optional<bool> separate_albums_by_grouping = std::optional<bool>());
static QString ContainerKey(const GroupBy group_by, const bool separate_albums_by_grouping, const Song &song);
signals:
void TotalSongCountUpdated(const int count);
void TotalArtistCountUpdated(const int count);
void TotalAlbumCountUpdated(const int count);
void GroupingChanged(const CollectionModel::Grouping g, const bool separate_albums_by_grouping);
void SongsAdded(const SongList &songs);
void SongsRemoved(const SongList &songs);
public slots:
void SetFilterMode(CollectionFilterOptions::FilterMode filter_mode);
void SetFilterAge(const int filter_age);
void SetFilterText(const QString &filter_text);
void SetFilterMode(const CollectionFilterOptions::FilterMode filter_mode);
void SetFilterMaxAge(const int filter_max_age);
void Init(const bool async = true);
void Reset();
void ResetAsync();
void SongsDiscovered(const SongList &songs);
protected:
void LazyPopulate(CollectionItem *item) override { LazyPopulate(item, true); }
void LazyPopulate(CollectionItem *parent, const bool signal);
private slots:
// From CollectionBackend
void SongsDeleted(const SongList &songs);
void SongsSlightlyChanged(const SongList &songs);
void TotalSongCountUpdatedSlot(const int count);
void TotalArtistCountUpdatedSlot(const int count);
void TotalAlbumCountUpdatedSlot(const int count);
static void ClearDiskCache();
// Called after ResetAsync
void ResetAsyncQueryFinished();
void AlbumCoverLoaded(const quint64 id, const AlbumCoverLoaderResult &result);
void AddReAddOrUpdate(const SongList &songs);
void RemoveSongs(const SongList &songs);
private:
// Provides some optimizations for loading the list of items in the root.
// This gets called a lot when filtering the playlist, so it's nice to be able to do it in a background thread.
CollectionQueryOptions PrepareQuery(CollectionItem *parent);
QueryResult RunQuery(const CollectionFilterOptions &filter_options = CollectionFilterOptions(), const CollectionQueryOptions &query_options = CollectionQueryOptions());
void PostQuery(CollectionItem *parent, const QueryResult &result, const bool signal);
bool HasCompilations(const QSqlDatabase &db, const CollectionFilterOptions &filter_options, const CollectionQueryOptions &query_options);
void Clear();
void BeginReset();
void EndReset();
// Functions for working with queries and creating items.
// When the model is reset or when a node is lazy-loaded the Collection constructs a database query to populate the items.
// Filters are added for each parent item, restricting the songs returned to a particular album or artist for example.
static void SetQueryColumnSpec(const GroupBy group_by, const bool separate_albums_by_grouping, CollectionQueryOptions *query_options);
static void AddQueryWhere(const GroupBy group_by, const bool separate_albums_by_grouping, CollectionItem *item, CollectionQueryOptions *query_options);
QVariant data(const CollectionItem *item, const int role) const;
// Items can be created either from a query that's been run to populate a node, or by a spontaneous SongsDiscovered emission from the backend.
CollectionItem *ItemFromQuery(const GroupBy group_by, const bool separate_albums_by_grouping, const bool signal, const bool create_divider, CollectionItem *parent, const SqlRow &row, const int container_level);
CollectionItem *ItemFromSong(const GroupBy group_by, const bool separate_albums_by_grouping, const bool signal, const bool create_divider, CollectionItem *parent, const Song &s, const int container_level);
void ScheduleUpdate(const CollectionModelUpdate::Type type, const SongList &songs);
void ScheduleAddSongs(const SongList &songs);
void ScheduleUpdateSongs(const SongList &songs);
void ScheduleRemoveSongs(const SongList &songs);
// The "Various Artists" node is an annoying special case.
CollectionItem *CreateCompilationArtistNode(const bool signal, CollectionItem *parent);
void AddReAddOrUpdateSongsInternal(const SongList &songs);
void AddSongsInternal(const SongList &songs);
void UpdateSongsInternal(const SongList &songs);
void RemoveSongsInternal(const SongList &songs);
// Helpers for ItemFromQuery and ItemFromSong
CollectionItem *InitItem(const GroupBy group_by, const bool signal, CollectionItem *parent, const int container_level);
void FinishItem(const GroupBy group_by, const bool signal, const bool create_divider, CollectionItem *parent, CollectionItem *item);
void CreateDividerItem(const QString &divider_key, const QString &display_text, CollectionItem *parent);
CollectionItem *CreateContainerItem(const GroupBy group_by, const int container_level, const QString &container_key, const Song &song, CollectionItem *parent);
void CreateSongItem(const Song &song, CollectionItem *parent);
void SetSongItemData(CollectionItem *item, const Song &song);
CollectionItem *CreateCompilationArtistNode(CollectionItem *parent);
static QString DividerKey(const GroupBy group_by, CollectionItem *item);
void LoadSongsFromSqlAsync();
SongList LoadSongsFromSql(const CollectionFilterOptions &filter_options = CollectionFilterOptions());
static QString DividerKey(const GroupBy group_by, const Song &song, const QString &sort_text);
static QString DividerDisplayText(const GroupBy group_by, const QString &key);
// Helpers
@@ -273,24 +249,50 @@ class CollectionModel : public SimpleTreeModel<CollectionItem> {
QString AlbumIconPixmapCacheKey(const QModelIndex &idx) const;
QUrl AlbumIconPixmapDiskCacheKey(const QString &cache_key) const;
QVariant AlbumIcon(const QModelIndex &idx);
QVariant data(const CollectionItem *item, const int role) const;
void ClearItemPixmapCache(CollectionItem *item);
bool CompareItems(const CollectionItem *a, const CollectionItem *b) const;
static qint64 MaximumCacheSize(Settings *s, const char *size_id, const char *size_unit_id, const qint64 cache_size_default);
private slots:
void Reload();
void ScheduleReset();
void ProcessUpdate();
void LoadSongsFromSqlAsyncFinished();
void AlbumCoverLoaded(const quint64 id, const AlbumCoverLoaderResult &result);
// From CollectionBackend
void TotalSongCountUpdatedSlot(const int count);
void TotalArtistCountUpdatedSlot(const int count);
void TotalAlbumCountUpdatedSlot(const int count);
static void ClearDiskCache();
void RowsInserted(const QModelIndex &parent, const int first, const int last);
void RowsRemoved(const QModelIndex &parent, const int first, const int last);
private:
static QNetworkDiskCache *sIconCache;
SharedPtr<CollectionBackend> backend_;
Application *app_;
CollectionDirectoryModel *dir_model_;
bool show_various_artists_;
bool sort_skips_articles_;
CollectionFilter *filter_;
QTimer *timer_reload_;
QTimer *timer_update_;
QPixmap pixmap_no_cover_;
QIcon icon_artist_;
Options options_current_;
Options options_active_;
bool use_disk_cache_;
AlbumCoverLoaderOptions::Types cover_types_;
int total_song_count_;
int total_artist_count_;
int total_album_count_;
CollectionFilterOptions filter_options_;
Grouping group_by_;
bool separate_albums_by_grouping_;
QQueue<CollectionModelUpdate> updates_;
// Keyed on database ID
QMap<int, CollectionItem*> song_nodes_;
@@ -301,22 +303,6 @@ class CollectionModel : public SimpleTreeModel<CollectionItem> {
// Keyed on a letter, a year, a century, etc.
QMap<QString, CollectionItem*> divider_nodes_;
QIcon artist_icon_;
QIcon album_icon_;
// Used as a generic icon to show when no cover art is found, fixed to the same size as the artwork (32x32)
QPixmap no_cover_icon_;
static QNetworkDiskCache *sIconCache;
int init_task_id_;
bool use_pretty_covers_;
bool show_dividers_;
bool use_disk_cache_;
bool use_lazy_loading_;
AlbumCoverLoaderOptions::Types cover_types_;
using ItemAndCacheKey = QPair<CollectionItem*, QString>;
QMap<quint64, ItemAndCacheKey> pending_art_;
QSet<QString> pending_cache_keys_;

View File

@@ -0,0 +1,23 @@
/*
* Strawberry Music Player
* Copyright 2023, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "collectionmodelupdate.h"
CollectionModelUpdate::CollectionModelUpdate(const Type &_type, const SongList &_songs)
: type(_type), songs(_songs) {}

View File

@@ -0,0 +1,38 @@
/*
* Strawberry Music Player
* Copyright 2023, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef COLLECTIONMODELUPDATE_H
#define COLLECTIONMODELUPDATE_H
#include "core/song.h"
class CollectionModelUpdate {
public:
enum class Type {
AddReAddOrUpdate,
Add,
Update,
Remove,
};
explicit CollectionModelUpdate(const Type &_type, const SongList &_songs);
Type type;
SongList songs;
};
#endif // COLLECTIONMODELUPDATE_H

View File

@@ -38,78 +38,13 @@
#include "collectionfilteroptions.h"
#include "utilities/searchparserutils.h"
CollectionQuery::CollectionQuery(const QSqlDatabase &db, const QString &songs_table, const QString &fts_table, const CollectionFilterOptions &filter_options)
CollectionQuery::CollectionQuery(const QSqlDatabase &db, const QString &songs_table, const CollectionFilterOptions &filter_options)
: SqlQuery(db),
songs_table_(songs_table),
fts_table_(fts_table),
include_unavailable_(false),
join_with_fts_(false),
duplicates_only_(false),
limit_(-1) {
if (!filter_options.filter_text().isEmpty()) {
// We need to munge the filter text a little bit to get it to work as expected with sqlite's FTS5:
// 1) Append * to all tokens.
// 2) Prefix "fts" to column names.
// 3) Remove colons which don't correspond to column names.
// Split on whitespace
QString filter_text = filter_options.filter_text().replace(QRegularExpression(QStringLiteral(":\\s+")), QStringLiteral(":"));
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
QStringList tokens(filter_text.split(QRegularExpression(QStringLiteral("\\s+")), Qt::SkipEmptyParts));
#else
QStringList tokens(filter_text.split(QRegularExpression(QStringLiteral("\\s+")), QString::SkipEmptyParts));
#endif
QString query;
for (QString token : tokens) {
token.remove(QLatin1Char('('))
.remove(QLatin1Char(')'))
.remove(QLatin1Char('"'))
.replace(QLatin1Char('-'), QLatin1Char(' '));
if (token.contains(QLatin1Char(':'))) {
const QString columntoken = token.section(QLatin1Char(':'), 0, 0);
QString subtoken = token.section(QLatin1Char(':'), 1, -1).replace(QLatin1String(":"), QLatin1String(" ")).trimmed();
if (subtoken.isEmpty()) continue;
if (Song::kFtsColumns.contains(QLatin1String("fts") + columntoken, Qt::CaseInsensitive)) {
if (!query.isEmpty()) query.append(QLatin1String(" "));
query += QStringLiteral("fts") + columntoken + QStringLiteral(":\"") + subtoken + QStringLiteral("\"*");
}
else if (Song::kNumericalColumns.contains(columntoken, Qt::CaseInsensitive)) {
QString comparator = RemoveSqlOperator(subtoken);
if (columntoken.compare(QLatin1String("rating"), Qt::CaseInsensitive) == 0) {
AddWhereRating(subtoken, comparator);
}
else if (columntoken.compare(QLatin1String("length"), Qt::CaseInsensitive) == 0) {
// Time is saved in nanoseconds, so add 9 0's
QString parsedTime = QString::number(Utilities::ParseSearchTime(subtoken)) + QStringLiteral("000000000");
AddWhere(columntoken, parsedTime, comparator);
}
else {
AddWhere(columntoken, subtoken, comparator);
}
}
// Not a valid filter, remove
else {
token = token.replace(QLatin1String(":"), QLatin1String(" ")).trimmed();
if (!token.isEmpty()) {
if (!query.isEmpty()) query.append(QLatin1Char(' '));
query += QLatin1Char('\"') + token + QStringLiteral("\"*");
}
}
}
else {
if (!query.isEmpty()) query.append(QLatin1Char(' '));
query += QLatin1Char('\"') + token + QStringLiteral("\"*");
}
}
if (!query.isEmpty()) {
where_clauses_ << QStringLiteral("fts.%fts_table_noprefix MATCH ?");
bound_values_ << query;
join_with_fts_ = true;
}
}
if (filter_options.max_age() != -1) {
qint64 cutoff = QDateTime::currentDateTime().toSecsSinceEpoch() - filter_options.max_age();
@@ -117,12 +52,6 @@ CollectionQuery::CollectionQuery(const QSqlDatabase &db, const QString &songs_ta
bound_values_ << cutoff;
}
// TODO: Currently you cannot use any FilterMode other than All and FTS at the same time.
// Joining songs, duplicated_songs and songs_fts all together takes a huge amount of time.
// The query takes about 20 seconds on my machine then. Why?
// Untagged mode could work with additional filtering but I'm disabling it just to be consistent
// this way filtering is available only in the All mode.
// Remember though that when you fix the Duplicates + FTS cooperation, enable the filtering in both Duplicates and Untagged modes.
duplicates_only_ = filter_options.filter_mode() == CollectionFilterOptions::FilterMode::Duplicates;
if (filter_options.filter_mode() == CollectionFilterOptions::FilterMode::Untagged) {
@@ -131,28 +60,10 @@ CollectionQuery::CollectionQuery(const QSqlDatabase &db, const QString &songs_ta
}
QString CollectionQuery::RemoveSqlOperator(QString &token) {
QString op = QStringLiteral("=");
static QRegularExpression rxOp(QStringLiteral("^(=|<[>=]?|>=?|!=)"));
QRegularExpressionMatch match = rxOp.match(token);
if (match.hasMatch()) {
op = match.captured(0);
}
token.remove(rxOp);
if (op == QStringLiteral("!=")) {
op = QStringLiteral("<>");
}
return op;
}
void CollectionQuery::AddWhere(const QString &column, const QVariant &value, const QString &op) {
// Ignore 'literal' for IN
if (op.compare(QLatin1String("IN"), Qt::CaseInsensitive) == 0) {
if (op.compare(QStringLiteral("IN"), Qt::CaseInsensitive) == 0) {
QStringList values = value.toStringList();
QStringList final_values;
final_values.reserve(values.count());
@@ -161,7 +72,7 @@ void CollectionQuery::AddWhere(const QString &column, const QVariant &value, con
bound_values_ << single_value;
}
where_clauses_ << QStringLiteral("%1 IN (%2)").arg(column, final_values.join(QStringLiteral(",")));
where_clauses_ << QStringLiteral("%1 IN (%2)").arg(column, final_values.join(QLatin1Char(',')));
}
else {
// Do integers inline - sqlite seems to get confused when you pass integers to bound parameters
@@ -190,49 +101,8 @@ void CollectionQuery::AddWhere(const QString &column, const QVariant &value, con
}
void CollectionQuery::AddWhereArtist(const QVariant &value) {
where_clauses_ << QStringLiteral("((artist = ? AND albumartist = '') OR albumartist = ?)");
bound_values_ << value;
bound_values_ << value;
}
void CollectionQuery::AddWhereRating(const QVariant &value, const QString &op) {
float parsed_rating = Utilities::ParseSearchRating(value.toString());
// You can't query the database for a float, due to float precision errors,
// So we have to use a certain tolerance, so that the searched value is definetly included.
const float tolerance = 0.001F;
if (op == QStringLiteral("<")) {
AddWhere(QStringLiteral("rating"), parsed_rating-tolerance, QStringLiteral("<"));
}
else if (op == QStringLiteral(">")) {
AddWhere(QStringLiteral("rating"), parsed_rating+tolerance, QStringLiteral(">"));
}
else if (op == QStringLiteral("<=")) {
AddWhere(QStringLiteral("rating"), parsed_rating+tolerance, QStringLiteral("<="));
}
else if (op == QStringLiteral(">=")) {
AddWhere(QStringLiteral("rating"), parsed_rating-tolerance, QStringLiteral(">="));
}
else if (op == QStringLiteral("<>")) {
where_clauses_ << QStringLiteral("(rating<? OR rating>?)");
bound_values_ << parsed_rating - tolerance;
bound_values_ << parsed_rating + tolerance;
}
else /* (op == "=") */ {
AddWhere(QStringLiteral("rating"), parsed_rating+tolerance, QStringLiteral("<"));
AddWhere(QStringLiteral("rating"), parsed_rating-tolerance, QStringLiteral(">"));
}
}
void CollectionQuery::AddCompilationRequirement(const bool compilation) {
// The unary + is added to prevent sqlite from using the index idx_comp_artist.
// When joining with fts, sqlite 3.8 has a tendency to use this index and thereby nesting the tables in an order which gives very poor performance
where_clauses_ << QStringLiteral("+compilation_effective = %1").arg(compilation ? 1 : 0);
}
@@ -248,14 +118,7 @@ QString CollectionQuery::GetInnerQuery() const {
bool CollectionQuery::Exec() {
QString sql;
if (join_with_fts_) {
sql = QStringLiteral("SELECT %1 FROM %2 INNER JOIN %3 AS fts ON %2.ROWID = fts.ROWID").arg(column_spec_, songs_table_, fts_table_);
}
else {
sql = QStringLiteral("SELECT %1 FROM %2 %3").arg(column_spec_, songs_table_, GetInnerQuery());
}
QString sql = QStringLiteral("SELECT %1 FROM %2 %3").arg(column_spec_, songs_table_, GetInnerQuery());
QStringList where_clauses(where_clauses_);
if (!include_unavailable_) {
@@ -268,9 +131,7 @@ bool CollectionQuery::Exec() {
if (limit_ != -1) sql += QStringLiteral(" LIMIT ") + QString::number(limit_);
sql.replace(QLatin1String("%songs_table"), songs_table_);
sql.replace(QLatin1String("%fts_table_noprefix"), fts_table_.section(QLatin1Char('.'), -1, -1));
sql.replace(QLatin1String("%fts_table"), fts_table_);
sql.replace(QStringLiteral("%songs_table"), songs_table_);
if (!QSqlQuery::prepare(sql)) return false;

View File

@@ -36,7 +36,7 @@
class CollectionQuery : public SqlQuery {
public:
explicit CollectionQuery(const QSqlDatabase &db, const QString &songs_table, const QString &fts_table, const CollectionFilterOptions &filter_options = CollectionFilterOptions());
explicit CollectionQuery(const QSqlDatabase &db, const QString &songs_table, const CollectionFilterOptions &filter_options = CollectionFilterOptions());
QVariant Value(const int column) const;
QVariant value(const int column) const { return Value(column); }
@@ -51,7 +51,6 @@ class CollectionQuery : public SqlQuery {
QStringList where_clauses() const { return where_clauses_; }
QVariantList bound_values() const { return bound_values_; }
bool include_unavailable() const { return include_unavailable_; }
bool join_with_fts() const { return join_with_fts_; }
bool duplicates_only() const { return duplicates_only_; }
int limit() const { return limit_; }
@@ -63,14 +62,9 @@ class CollectionQuery : public SqlQuery {
void SetWhereClauses(const QStringList &where_clauses) { where_clauses_ = where_clauses; }
// Removes = < > <= >= <> from the beginning of the input string and returns the operator
// If the input String has no operator, returns "="
QString RemoveSqlOperator(QString &token);
// Adds a fragment of WHERE clause. When executed, this Query will connect all the fragments with AND operator.
// Please note that IN operator expects a QStringList as value.
void AddWhere(const QString &column, const QVariant &value, const QString &op = QStringLiteral("="));
void AddWhereArtist(const QVariant &value);
void AddWhereRating(const QVariant &value, const QString &op = QStringLiteral("="));
void SetBoundValues(const QVariantList &bound_values) { bound_values_ = bound_values; }
void SetDuplicatesOnly(const bool duplicates_only) { duplicates_only_ = duplicates_only; }
@@ -83,7 +77,6 @@ class CollectionQuery : public SqlQuery {
QSqlDatabase db_;
QString songs_table_;
QString fts_table_;
QString column_spec_;
QString order_by_;
@@ -91,7 +84,6 @@ class CollectionQuery : public SqlQuery {
QVariantList bound_values_;
bool include_unavailable_;
bool join_with_fts_;
bool duplicates_only_;
int limit_;
};

View File

@@ -120,9 +120,14 @@ CollectionView::~CollectionView() = default;
void CollectionView::SaveFocus() {
QModelIndex current = currentIndex();
QVariant type = model()->data(current, CollectionModel::Role_Type);
if (!type.isValid() || (type.toInt() != CollectionItem::Type_Song && type.toInt() != CollectionItem::Type_Container && type.toInt() != CollectionItem::Type_Divider)) {
const QModelIndex current = currentIndex();
const QVariant role_type = model()->data(current, CollectionModel::Role_Type);
if (!role_type.isValid()) {
return;
}
const CollectionItem::Type item_type = role_type.value<CollectionItem::Type>();
if (item_type != CollectionItem::Type::Song && item_type != CollectionItem::Type::Container && item_type != CollectionItem::Type::Divider) {
return;
}
@@ -130,8 +135,8 @@ void CollectionView::SaveFocus() {
last_selected_song_ = Song();
last_selected_container_ = QString();
switch (type.toInt()) {
case CollectionItem::Type_Song:{
switch (item_type) {
case CollectionItem::Type::Song:{
QModelIndex index = qobject_cast<QSortFilterProxyModel*>(model())->mapToSource(current);
SongList songs = app_->collection_model()->GetChildSongs(index);
if (!songs.isEmpty()) {
@@ -140,8 +145,8 @@ void CollectionView::SaveFocus() {
break;
}
case CollectionItem::Type_Container:
case CollectionItem::Type_Divider:{
case CollectionItem::Type::Container:
case CollectionItem::Type::Divider:{
QString text = model()->data(current, CollectionModel::Role_SortText).toString();
last_selected_container_ = text;
break;
@@ -157,9 +162,14 @@ void CollectionView::SaveFocus() {
void CollectionView::SaveContainerPath(const QModelIndex &child) {
QModelIndex current = model()->parent(child);
QVariant type = model()->data(current, CollectionModel::Role_Type);
if (!type.isValid() || (type.toInt() != CollectionItem::Type_Container && type.toInt() != CollectionItem::Type_Divider)) {
const QModelIndex current = model()->parent(child);
const QVariant role_type = model()->data(current, CollectionModel::Role_Type);
if (!role_type.isValid()) {
return;
}
const CollectionItem::Type item_type = role_type.value<CollectionItem::Type>();
if (item_type != CollectionItem::Type::Container && item_type != CollectionItem::Type::Divider) {
return;
}
@@ -183,12 +193,17 @@ bool CollectionView::RestoreLevelFocus(const QModelIndex &parent) {
if (model()->canFetchMore(parent)) {
model()->fetchMore(parent);
}
int rows = model()->rowCount(parent);
const int rows = model()->rowCount(parent);
for (int i = 0; i < rows; i++) {
QModelIndex current = model()->index(i, 0, parent);
QVariant type = model()->data(current, CollectionModel::Role_Type);
switch (type.toInt()) {
case CollectionItem::Type_Song:
const QVariant role_type = model()->data(current, CollectionModel::Role_Type);
if (!role_type.isValid()) return false;
const CollectionItem::Type item_type = role_type.value<CollectionItem::Type>();
switch (item_type) {
case CollectionItem::Type::Root:
case CollectionItem::Type::LoadingIndicator:
break;
case CollectionItem::Type::Song:
if (!last_selected_song_.url().isEmpty()) {
QModelIndex index = qobject_cast<QSortFilterProxyModel*>(model())->mapToSource(current);
const SongList songs = app_->collection_model()->GetChildSongs(index);
@@ -199,8 +214,8 @@ bool CollectionView::RestoreLevelFocus(const QModelIndex &parent) {
}
break;
case CollectionItem::Type_Container:
case CollectionItem::Type_Divider:{
case CollectionItem::Type::Container:
case CollectionItem::Type::Divider:{
QString text = model()->data(current, CollectionModel::Role_SortText).toString();
if (!last_selected_container_.isEmpty() && last_selected_container_ == text) {
expand(current);
@@ -228,18 +243,9 @@ bool CollectionView::RestoreLevelFocus(const QModelIndex &parent) {
void CollectionView::ReloadSettings() {
Settings settings;
settings.beginGroup(CollectionSettingsPage::kSettingsGroup);
SetAutoOpen(settings.value("auto_open", false).toBool());
if (app_) {
app_->collection_model()->set_pretty_covers(settings.value("pretty_covers", true).toBool());
app_->collection_model()->set_show_dividers(settings.value("show_dividers", true).toBool());
app_->collection_model()->set_sort_skips_articles(settings.value("sort_skips_articles", true).toBool());
}
delete_files_ = settings.value("delete_files", false).toBool();
settings.endGroup();
}
@@ -573,15 +579,20 @@ void CollectionView::OpenInNewPlaylist() {
void CollectionView::SearchForThis() {
QModelIndex current = currentIndex();
QVariant type = model()->data(current, CollectionModel::Role_Type);
if (!type.isValid() || (type.toInt() != CollectionItem::Type_Song && type.toInt() != CollectionItem::Type_Container && type.toInt() != CollectionItem::Type_Divider)) {
const QVariant role_type = model()->data(current, CollectionModel::Role_Type);
if (!role_type.isValid()) {
return;
}
const CollectionItem::Type item_type = role_type.value<CollectionItem::Type>();
if (item_type != CollectionItem::Type::Song && item_type != CollectionItem::Type::Container && item_type != CollectionItem::Type::Divider) {
return;
}
QString search;
QModelIndex index = qobject_cast<QSortFilterProxyModel*>(model())->mapToSource(current);
switch (type.toInt()) {
case CollectionItem::Type_Song:{
switch (item_type) {
case CollectionItem::Type::Song:{
SongList songs = app_->collection_model()->GetChildSongs(index);
if (!songs.isEmpty()) {
last_selected_song_ = songs.last();
@@ -590,11 +601,11 @@ void CollectionView::SearchForThis() {
break;
}
case CollectionItem::Type_Divider:{
case CollectionItem::Type::Divider:{
break;
}
case CollectionItem::Type_Container:{
case CollectionItem::Type::Container:{
CollectionItem *item = app_->collection_model()->IndexToItem(index);
int container_level = item->container_level;
@@ -750,8 +761,11 @@ void CollectionView::FilterReturnPressed() {
if (!currentIndex().isValid()) {
// Pick the first thing that isn't a divider
for (int row = 0; row < model()->rowCount(); ++row) {
QModelIndex idx(model()->index(row, 0));
if (idx.data(CollectionModel::Role_Type) != CollectionItem::Type_Divider) {
const QModelIndex idx = model()->index(row, 0);
const QVariant role_type = idx.data(CollectionModel::Role_Type);
if (!role_type.isValid()) continue;
const CollectionItem::Type item_type = role_type.value<CollectionItem::Type>();
if (item_type != CollectionItem::Type::Divider) {
setCurrentIndex(idx);
break;
}