strawberry-audio-player-win.../src/playlist/songloaderinserter.cpp

216 lines
6.5 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>
*
* 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"
#include <QtConcurrent>
#include <QtAlgorithms>
#include <QList>
#include <QUrl>
2018-02-27 18:06:05 +01:00
#include "core/logging.h"
#include "core/songloader.h"
#include "core/taskmanager.h"
#include "playlist.h"
#include "songloaderinserter.h"
2018-02-27 18:06:05 +01:00
SongLoaderInserter::SongLoaderInserter(TaskManager *task_manager, CollectionBackendInterface *collection, const Player *player)
: task_manager_(task_manager),
destination_(nullptr),
row_(-1),
play_now_(true),
enqueue_(false),
2019-04-08 18:46:11 +02:00
enqueue_next_(false),
2018-02-27 18:06:05 +01:00
collection_(collection),
2021-02-02 21:08:58 +01:00
player_(player) {}
2018-02-27 18:06:05 +01:00
SongLoaderInserter::~SongLoaderInserter() { qDeleteAll(pending_); }
void SongLoaderInserter::Load(Playlist *destination, int row, bool play_now, bool enqueue, bool enqueue_next, const QList<QUrl> &urls) {
2018-02-27 18:06:05 +01:00
destination_ = destination;
row_ = row;
play_now_ = play_now;
enqueue_ = enqueue;
enqueue_next_ = enqueue_next;
2018-02-27 18:06:05 +01:00
2021-01-26 16:48:04 +01:00
QObject::connect(destination, &Playlist::destroyed, this, &SongLoaderInserter::DestinationDestroyed);
QObject::connect(this, &SongLoaderInserter::PreloadFinished, this, &SongLoaderInserter::InsertSongs);
QObject::connect(this, &SongLoaderInserter::EffectiveLoadFinished, destination, &Playlist::UpdateItems);
2018-02-27 18:06:05 +01:00
for (const QUrl &url : urls) {
2018-02-27 18:06:05 +01:00
SongLoader *loader = new SongLoader(collection_, player_, this);
SongLoader::Result ret = loader->Load(url);
if (ret == SongLoader::BlockingLoadRequired) {
pending_.append(loader);
continue;
}
2019-04-20 15:28:16 +02:00
if (ret == SongLoader::Success) {
2018-02-27 18:06:05 +01:00
songs_ << loader->songs();
2019-04-20 15:28:16 +02:00
}
else {
for (const QString &error : loader->errors()) {
emit Error(error);
}
}
2018-02-27 18:06:05 +01:00
delete loader;
}
if (pending_.isEmpty()) {
InsertSongs();
deleteLater();
}
else {
2020-11-14 02:13:22 +01:00
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
(void)QtConcurrent::run(&SongLoaderInserter::AsyncLoad, this);
#else
(void)QtConcurrent::run(this, &SongLoaderInserter::AsyncLoad);
#endif
2018-02-27 18:06:05 +01:00
}
}
// Load audio CD tracks:
// First, we add tracks (without metadata) into the playlist
// In the meantime, MusicBrainz will be queried to get songs' metadata.
// AudioCDTagsLoaded will be called next, and playlist's items will be updated.
void SongLoaderInserter::LoadAudioCD(Playlist *destination, int row, bool play_now, bool enqueue, bool enqueue_next) {
2018-02-27 18:06:05 +01:00
destination_ = destination;
row_ = row;
play_now_ = play_now;
enqueue_ = enqueue;
enqueue_next_ = enqueue_next;
2018-02-27 18:06:05 +01:00
SongLoader *loader = new SongLoader(collection_, player_, this);
QObject::connect(loader, &SongLoader::AudioCDTracksLoadFinished, this, [this, loader]() { AudioCDTracksLoadFinished(loader); });
2021-01-26 16:48:04 +01:00
QObject::connect(loader, &SongLoader::LoadAudioCDFinished, this, &SongLoaderInserter::AudioCDTagsLoaded);
2018-02-27 18:06:05 +01:00
qLog(Info) << "Loading audio CD...";
SongLoader::Result ret = loader->LoadAudioCD();
if (ret == SongLoader::Error) {
2019-04-20 15:28:16 +02:00
if (loader->errors().isEmpty())
emit Error(tr("Error while loading audio CD."));
else {
for (const QString &error : loader->errors()) {
emit Error(error);
}
}
2018-02-27 18:06:05 +01:00
delete loader;
}
// Songs will be loaded later: see AudioCDTracksLoadFinished and AudioCDTagsLoaded slots
2018-02-27 18:06:05 +01:00
}
void SongLoaderInserter::DestinationDestroyed() { destination_ = nullptr; }
void SongLoaderInserter::AudioCDTracksLoadFinished(SongLoader *loader) {
2018-02-27 18:06:05 +01:00
songs_ = loader->songs();
if (songs_.isEmpty()) {
for (const QString &error : loader->errors()) {
emit Error(error);
}
}
else {
InsertSongs();
}
2018-02-27 18:06:05 +01:00
}
void SongLoaderInserter::AudioCDTagsLoaded(const bool success) {
2018-02-27 18:06:05 +01:00
SongLoader *loader = qobject_cast<SongLoader*>(sender());
if (!loader || !destination_) return;
if (success)
destination_->UpdateItems(loader->songs());
else
qLog(Error) << "Error while getting audio CD metadata from MusicBrainz";
deleteLater();
}
void SongLoaderInserter::InsertSongs() {
// Insert songs (that haven't been completely loaded) to allow user to see and play them while not loaded completely
2018-02-27 18:06:05 +01:00
if (destination_) {
destination_->InsertSongsOrCollectionItems(songs_, row_, play_now_, enqueue_, enqueue_next_);
2018-02-27 18:06:05 +01:00
}
2018-02-27 18:06:05 +01:00
}
void SongLoaderInserter::AsyncLoad() {
// First, quick load raw songs.
int async_progress = 0;
int async_load_id = task_manager_->StartTask(tr("Loading tracks"));
2018-03-10 13:02:56 +01:00
task_manager_->SetTaskProgress(async_load_id, async_progress, pending_.count());
2019-04-20 15:28:16 +02:00
bool first_loaded = false;
2018-02-27 18:06:05 +01:00
for (int i = 0; i < pending_.count(); ++i) {
SongLoader *loader = pending_[i];
2019-04-20 15:28:16 +02:00
SongLoader::Result res = loader->LoadFilenamesBlocking();
2018-02-27 18:06:05 +01:00
task_manager_->SetTaskProgress(async_load_id, ++async_progress);
2019-04-20 15:28:16 +02:00
if (res == SongLoader::Error) {
for (const QString &error : loader->errors()) {
emit Error(error);
}
continue;
}
if (!first_loaded) {
// Load everything from the first song.
// It'll start playing as soon as we emit PreloadFinished, so it needs to have the duration set to show properly in the UI.
2018-02-27 18:06:05 +01:00
loader->LoadMetadataBlocking();
2019-04-20 15:28:16 +02:00
first_loaded = true;
2018-02-27 18:06:05 +01:00
}
2019-04-20 15:28:16 +02:00
2018-02-27 18:06:05 +01:00
songs_ << loader->songs();
2019-04-20 15:28:16 +02:00
2018-02-27 18:06:05 +01:00
}
task_manager_->SetTaskFinished(async_load_id);
emit PreloadFinished();
// Songs are inserted in playlist, now load them completely.
async_progress = 0;
async_load_id = task_manager_->StartTask(tr("Loading tracks info"));
task_manager_->SetTaskProgress(async_load_id, async_progress, songs_.count());
SongList songs;
for (int i = 0; i < pending_.count(); ++i) {
SongLoader *loader = pending_[i];
if (i != 0) {
// We already did this earlier for the first song.
loader->LoadMetadataBlocking();
}
songs << loader->songs();
task_manager_->SetTaskProgress(async_load_id, songs.count());
}
task_manager_->SetTaskFinished(async_load_id);
// Replace the partially-loaded items by the new ones, fully loaded.
emit EffectiveLoadFinished(songs);
deleteLater();
}