strawberry-audio-player-win.../src/collection/collection.cpp

237 lines
8.1 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* 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/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
2020-02-08 03:40:30 +01:00
#include <QtGlobal>
#include <QObject>
#include <QThread>
#include <QList>
#include <QSettings>
#include <QtConcurrentRun>
2018-02-27 18:06:05 +01:00
#include "core/application.h"
#include "core/taskmanager.h"
2018-02-27 18:06:05 +01:00
#include "core/database.h"
#include "core/player.h"
#include "core/tagreaderclient.h"
#include "core/thread.h"
#include "core/utilities.h"
2019-04-08 23:00:07 +02:00
#include "core/song.h"
2020-02-08 03:40:30 +01:00
#include "core/logging.h"
#include "collection.h"
#include "collectionwatcher.h"
#include "collectionbackend.h"
#include "collectionmodel.h"
#include "playlist/playlistmanager.h"
2020-08-30 18:09:13 +02:00
#include "scrobbler/lastfmimport.h"
#include "settings/collectionsettingspage.h"
2018-02-27 18:06:05 +01:00
2018-07-01 22:26:46 +02:00
const char *SCollection::kSongsTable = "songs";
const char *SCollection::kFtsTable = "songs_fts";
2018-07-01 22:26:46 +02:00
const char *SCollection::kDirsTable = "directories";
const char *SCollection::kSubdirsTable = "subdirectories";
2018-02-27 18:06:05 +01:00
2018-07-01 22:26:46 +02:00
SCollection::SCollection(Application *app, QObject *parent)
2018-02-27 18:06:05 +01:00
: QObject(parent),
app_(app),
backend_(nullptr),
model_(nullptr),
watcher_(nullptr),
watcher_thread_(nullptr),
original_thread_(nullptr),
io_priority_(Utilities::IoPriority::IOPRIO_CLASS_IDLE),
thread_priority_(QThread::Priority::IdlePriority),
save_playcounts_to_files_(false),
save_ratings_to_files_(false) {
original_thread_ = thread();
2018-02-27 18:06:05 +01:00
2019-04-08 23:00:07 +02:00
backend_ = new CollectionBackend();
2018-02-27 18:06:05 +01:00
backend()->moveToThread(app->database()->thread());
qLog(Debug) << backend_ << "moved to thread" << app->database()->thread();
2018-02-27 18:06:05 +01:00
backend_->Init(app->database(), app->task_manager(), Song::Source_Collection, kSongsTable, kFtsTable, kDirsTable, kSubdirsTable);
2018-02-27 18:06:05 +01:00
model_ = new CollectionModel(backend_, app_, this);
ReloadSettings();
}
2018-07-01 22:26:46 +02:00
SCollection::~SCollection() {
if (watcher_) {
watcher_->Stop();
watcher_->deleteLater();
}
if (watcher_thread_) {
watcher_thread_->exit();
2021-06-12 20:53:23 +02:00
watcher_thread_->wait(5000);
}
2019-07-22 20:53:05 +02:00
backend_->deleteLater();
2018-02-27 18:06:05 +01:00
}
2018-07-01 22:26:46 +02:00
void SCollection::Init() {
2018-10-02 00:38:52 +02:00
2019-04-08 23:00:07 +02:00
watcher_ = new CollectionWatcher(Song::Source_Collection);
2018-02-27 18:06:05 +01:00
watcher_thread_ = new Thread(this);
#ifndef Q_OS_WIN32
if (io_priority_ != Utilities::IoPriority::IOPRIO_CLASS_NONE) {
watcher_thread_->SetIoPriority(io_priority_);
}
#endif
2018-02-27 18:06:05 +01:00
watcher_->moveToThread(watcher_thread_);
qLog(Debug) << watcher_ << "moved to thread" << watcher_thread_ << "with I/O priority" << io_priority_ << "and thread priority" << thread_priority_;
watcher_thread_->start(thread_priority_);
2018-02-27 18:06:05 +01:00
watcher_->set_backend(backend_);
watcher_->set_task_manager(app_->task_manager());
QObject::connect(backend_, &CollectionBackend::Error, this, &SCollection::Error);
2021-01-26 16:48:04 +01:00
QObject::connect(backend_, &CollectionBackend::DirectoryDiscovered, watcher_, &CollectionWatcher::AddDirectory);
QObject::connect(backend_, &CollectionBackend::DirectoryDeleted, watcher_, &CollectionWatcher::RemoveDirectory);
QObject::connect(backend_, &CollectionBackend::SongsRatingChanged, this, &SCollection::SongsRatingChanged);
QObject::connect(backend_, &CollectionBackend::SongsStatisticsChanged, this, &SCollection::SongsPlaycountChanged);
2021-01-26 16:48:04 +01:00
QObject::connect(watcher_, &CollectionWatcher::NewOrUpdatedSongs, backend_, &CollectionBackend::AddOrUpdateSongs);
QObject::connect(watcher_, &CollectionWatcher::SongsMTimeUpdated, backend_, &CollectionBackend::UpdateMTimesOnly);
QObject::connect(watcher_, &CollectionWatcher::SongsDeleted, backend_, &CollectionBackend::DeleteSongs);
QObject::connect(watcher_, &CollectionWatcher::SongsUnavailable, backend_, &CollectionBackend::MarkSongsUnavailable);
QObject::connect(watcher_, &CollectionWatcher::SongsReadded, backend_, &CollectionBackend::MarkSongsUnavailable);
QObject::connect(watcher_, &CollectionWatcher::SubdirsDiscovered, backend_, &CollectionBackend::AddOrUpdateSubdirs);
QObject::connect(watcher_, &CollectionWatcher::SubdirsMTimeUpdated, backend_, &CollectionBackend::AddOrUpdateSubdirs);
QObject::connect(watcher_, &CollectionWatcher::CompilationsNeedUpdating, backend_, &CollectionBackend::CompilationsNeedUpdating);
QObject::connect(watcher_, &CollectionWatcher::UpdateLastSeen, backend_, &CollectionBackend::UpdateLastSeen);
2021-01-26 16:48:04 +01:00
QObject::connect(app_->lastfm_import(), &LastFMImport::UpdateLastPlayed, backend_, &CollectionBackend::UpdateLastPlayed);
QObject::connect(app_->lastfm_import(), &LastFMImport::UpdatePlayCount, backend_, &CollectionBackend::UpdatePlayCount);
2020-08-30 18:09:13 +02:00
2018-02-27 18:06:05 +01:00
// This will start the watcher checking for updates
backend_->LoadDirectoriesAsync();
}
void SCollection::Exit() {
wait_for_exit_ << backend_ << watcher_;
2021-01-26 16:48:04 +01:00
QObject::disconnect(backend_, nullptr, watcher_, nullptr);
QObject::disconnect(watcher_, nullptr, backend_, nullptr);
2021-01-26 16:48:04 +01:00
QObject::connect(backend_, &CollectionBackend::ExitFinished, this, &SCollection::ExitReceived);
QObject::connect(watcher_, &CollectionWatcher::ExitFinished, this, &SCollection::ExitReceived);
backend_->ExitAsync();
watcher_->Abort();
watcher_->ExitAsync();
}
void SCollection::ExitReceived() {
2021-06-20 19:04:08 +02:00
QObject *obj = sender();
2021-01-26 16:48:04 +01:00
QObject::disconnect(obj, nullptr, this, nullptr);
2019-07-24 23:29:09 +02:00
qLog(Debug) << obj << "successfully exited.";
wait_for_exit_.removeAll(obj);
if (wait_for_exit_.isEmpty()) emit ExitFinished();
2018-02-27 18:06:05 +01:00
}
2018-07-01 22:26:46 +02:00
void SCollection::IncrementalScan() { watcher_->IncrementalScanAsync(); }
2018-02-27 18:06:05 +01:00
2018-07-01 22:26:46 +02:00
void SCollection::FullScan() { watcher_->FullScanAsync(); }
2018-02-27 18:06:05 +01:00
2019-06-30 21:06:07 +02:00
void SCollection::AbortScan() { watcher_->Stop(); }
void SCollection::Rescan(const SongList &songs) {
2021-04-26 22:56:14 +02:00
qLog(Debug) << "Rescan" << songs.size() << "songs";
if (!songs.isEmpty()) watcher_->RescanTracksAsync(songs);
2019-06-30 21:06:07 +02:00
}
2018-07-01 22:26:46 +02:00
void SCollection::PauseWatcher() { watcher_->SetRescanPausedAsync(true); }
2018-02-27 18:06:05 +01:00
2018-07-01 22:26:46 +02:00
void SCollection::ResumeWatcher() { watcher_->SetRescanPausedAsync(false); }
void SCollection::ReloadSettings() {
2018-02-27 18:06:05 +01:00
watcher_->ReloadSettingsAsync();
model_->ReloadSettings();
2018-02-27 18:06:05 +01:00
QSettings s;
s.beginGroup(CollectionSettingsPage::kSettingsGroup);
io_priority_ = static_cast<Utilities::IoPriority>(s.value("io_priority", Utilities::IOPRIO_CLASS_IDLE).toInt());
thread_priority_ = static_cast<QThread::Priority>(s.value("thread_priority", QThread::Priority::IdlePriority).toInt());
save_playcounts_to_files_ = s.value("save_playcounts", false).toBool();
save_ratings_to_files_ = s.value("save_ratings", false).toBool();
s.endGroup();
}
void SCollection::SyncPlaycountAndRatingToFilesAsync() {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
(void)QtConcurrent::run(&SCollection::SyncPlaycountAndRatingToFiles, this);
#else
(void)QtConcurrent::run(this, &SCollection::SyncPlaycountAndRatingToFiles);
#endif
}
void SCollection::SyncPlaycountAndRatingToFiles() {
const int task_id = app_->task_manager()->StartTask(tr("Saving playcounts and ratings"));
app_->task_manager()->SetTaskBlocksCollectionScans(task_id);
const SongList songs = backend_->GetAllSongs();
2021-10-30 02:21:29 +02:00
const qint64 nb_songs = songs.size();
int i = 0;
for (const Song &song : songs) {
TagReaderClient::Instance()->UpdateSongPlaycountBlocking(song);
TagReaderClient::Instance()->UpdateSongRatingBlocking(song);
app_->task_manager()->SetTaskProgress(task_id, ++i, nb_songs);
}
app_->task_manager()->SetTaskFinished(task_id);
}
void SCollection::SongsPlaycountChanged(const SongList &songs) {
if (save_playcounts_to_files_) {
app_->tag_reader_client()->UpdateSongsPlaycount(songs);
}
}
void SCollection::SongsRatingChanged(const SongList &songs, const bool save_tags) {
if (save_tags || save_ratings_to_files_) {
app_->tag_reader_client()->UpdateSongsRating(songs);
}
2018-02-27 18:06:05 +01:00
}