2010-06-15 20:24:08 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-06-15 20:24:08 +02:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-04-16 16:04:15 +02:00
|
|
|
#include <QtConcurrentRun>
|
|
|
|
|
2010-06-15 20:24:08 +02:00
|
|
|
#include "playlist.h"
|
|
|
|
#include "songloaderinserter.h"
|
2011-06-10 01:08:43 +02:00
|
|
|
#include "core/logging.h"
|
2010-06-15 20:24:08 +02:00
|
|
|
#include "core/songloader.h"
|
2010-06-23 15:21:30 +02:00
|
|
|
#include "core/taskmanager.h"
|
2010-06-15 20:24:08 +02:00
|
|
|
|
2014-01-24 13:54:38 +01:00
|
|
|
SongLoaderInserter::SongLoaderInserter(TaskManager* task_manager,
|
|
|
|
LibraryBackendInterface* library,
|
|
|
|
const Player* player)
|
2011-03-06 15:46:01 +01:00
|
|
|
: task_manager_(task_manager),
|
2014-02-06 16:49:49 +01:00
|
|
|
destination_(nullptr),
|
2010-08-31 21:45:33 +02:00
|
|
|
row_(-1),
|
|
|
|
play_now_(true),
|
2011-01-10 23:26:13 +01:00
|
|
|
enqueue_(false),
|
2010-08-31 21:45:33 +02:00
|
|
|
async_load_id_(0),
|
|
|
|
async_progress_(0),
|
2014-01-24 13:54:38 +01:00
|
|
|
library_(library),
|
2014-02-07 16:34:20 +01:00
|
|
|
player_(player) {}
|
2010-06-15 20:24:08 +02:00
|
|
|
|
|
|
|
SongLoaderInserter::~SongLoaderInserter() {
|
|
|
|
qDeleteAll(pending_);
|
2011-04-16 16:04:15 +02:00
|
|
|
qDeleteAll(pending_async_);
|
2010-06-15 20:24:08 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SongLoaderInserter::Load(Playlist* destination, int row, bool play_now,
|
|
|
|
bool enqueue, const QList<QUrl>& urls) {
|
2010-06-15 20:24:08 +02:00
|
|
|
destination_ = destination;
|
|
|
|
row_ = row;
|
|
|
|
play_now_ = play_now;
|
2011-03-10 13:42:35 +01:00
|
|
|
enqueue_ = enqueue;
|
2010-06-15 20:24:08 +02:00
|
|
|
|
2011-03-06 15:46:01 +01:00
|
|
|
connect(destination, SIGNAL(destroyed()), SLOT(DestinationDestroyed()));
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(this, SIGNAL(EffectiveLoadFinished(const SongList&)), destination,
|
|
|
|
SLOT(UpdateItems(const SongList&)));
|
2011-03-06 15:46:01 +01:00
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QUrl& url : urls) {
|
2014-01-24 13:54:38 +01:00
|
|
|
SongLoader* loader = new SongLoader(library_, player_, this);
|
2011-03-21 18:44:36 +01:00
|
|
|
|
|
|
|
// we're connecting this before we're even sure if this is an async load
|
|
|
|
// to avoid race conditions (signal emission before we're listening to it)
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(loader, SIGNAL(LoadFinished(bool)),
|
|
|
|
SLOT(PendingLoadFinished(bool)));
|
2010-06-15 20:24:08 +02:00
|
|
|
SongLoader::Result ret = loader->Load(url);
|
|
|
|
|
|
|
|
if (ret == SongLoader::WillLoadAsync) {
|
|
|
|
pending_.insert(loader);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == SongLoader::Success)
|
|
|
|
songs_ << loader->songs();
|
|
|
|
else
|
|
|
|
emit Error(tr("Error loading %1").arg(url.toString()));
|
|
|
|
delete loader;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pending_.isEmpty())
|
|
|
|
Finished();
|
2010-06-23 15:21:30 +02:00
|
|
|
else {
|
|
|
|
async_progress_ = 0;
|
|
|
|
async_load_id_ = task_manager_->StartTask(tr("Loading tracks"));
|
2014-02-07 16:34:20 +01:00
|
|
|
task_manager_->SetTaskProgress(async_load_id_, async_progress_,
|
|
|
|
pending_.count());
|
2010-06-23 15:21:30 +02:00
|
|
|
}
|
2010-06-15 20:24:08 +02:00
|
|
|
}
|
|
|
|
|
2011-06-10 01:08:43 +02:00
|
|
|
// Load audio CD tracks:
|
|
|
|
// First, we add tracks (without metadata) into the playlist
|
|
|
|
// In the meantine, MusicBrainz will be queried to get songs' metadata.
|
|
|
|
// AudioCDTagsLoaded will be called next, and playlist's items will be updated.
|
2014-02-07 16:34:20 +01:00
|
|
|
void SongLoaderInserter::LoadAudioCD(Playlist* destination, int row,
|
|
|
|
bool play_now, bool enqueue) {
|
2011-06-10 01:08:43 +02:00
|
|
|
destination_ = destination;
|
|
|
|
row_ = row;
|
|
|
|
play_now_ = play_now;
|
|
|
|
enqueue_ = enqueue;
|
|
|
|
|
2014-01-24 13:54:38 +01:00
|
|
|
SongLoader* loader = new SongLoader(library_, player_, this);
|
2011-06-10 01:08:43 +02:00
|
|
|
connect(loader, SIGNAL(LoadFinished(bool)), SLOT(AudioCDTagsLoaded(bool)));
|
|
|
|
qLog(Info) << "Loading audio CD...";
|
|
|
|
SongLoader::Result ret = loader->LoadAudioCD();
|
|
|
|
if (ret == SongLoader::Error) {
|
|
|
|
emit Error(tr("Error while loading audio CD"));
|
|
|
|
delete loader;
|
|
|
|
}
|
|
|
|
songs_ = loader->songs();
|
|
|
|
PartiallyFinished();
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SongLoaderInserter::DestinationDestroyed() { destination_ = nullptr; }
|
2011-11-18 21:55:54 +01:00
|
|
|
|
2011-06-10 01:08:43 +02:00
|
|
|
void SongLoaderInserter::AudioCDTagsLoaded(bool success) {
|
|
|
|
SongLoader* loader = qobject_cast<SongLoader*>(sender());
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!loader || !destination_) return;
|
2011-06-10 01:08:43 +02:00
|
|
|
|
|
|
|
if (success)
|
|
|
|
destination_->UpdateItems(loader->songs());
|
|
|
|
else
|
|
|
|
qLog(Error) << "Error while getting audio CD metadata from MusicBrainz";
|
2011-06-15 01:38:43 +02:00
|
|
|
deleteLater();
|
2011-06-10 01:08:43 +02:00
|
|
|
}
|
2011-03-06 15:46:01 +01:00
|
|
|
|
2010-06-15 20:24:08 +02:00
|
|
|
void SongLoaderInserter::PendingLoadFinished(bool success) {
|
|
|
|
SongLoader* loader = qobject_cast<SongLoader*>(sender());
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!loader || !pending_.contains(loader)) return;
|
2010-06-15 20:24:08 +02:00
|
|
|
pending_.remove(loader);
|
2011-04-16 16:04:15 +02:00
|
|
|
pending_async_.insert(loader);
|
2010-06-15 20:24:08 +02:00
|
|
|
|
|
|
|
if (success)
|
|
|
|
songs_ << loader->songs();
|
|
|
|
else
|
|
|
|
emit Error(tr("Error loading %1").arg(loader->url().toString()));
|
|
|
|
|
2010-06-23 15:21:30 +02:00
|
|
|
task_manager_->SetTaskProgress(async_load_id_, ++async_progress_);
|
2010-06-15 20:24:08 +02:00
|
|
|
if (pending_.isEmpty()) {
|
2010-06-23 15:21:30 +02:00
|
|
|
task_manager_->SetTaskFinished(async_load_id_);
|
2011-04-16 16:04:15 +02:00
|
|
|
async_progress_ = 0;
|
|
|
|
async_load_id_ = task_manager_->StartTask(tr("Loading tracks info"));
|
2014-02-07 16:34:20 +01:00
|
|
|
task_manager_->SetTaskProgress(async_load_id_, async_progress_,
|
|
|
|
pending_async_.count());
|
2011-04-16 16:04:15 +02:00
|
|
|
PartiallyFinished();
|
2011-06-10 01:08:43 +02:00
|
|
|
QtConcurrent::run(this, &SongLoaderInserter::EffectiveLoad);
|
2011-04-16 16:04:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SongLoaderInserter::PartiallyFinished() {
|
|
|
|
// Insert songs (that haven't been completelly loaded) to allow user to see
|
2011-04-16 17:06:13 +02:00
|
|
|
// and play them while not loaded completely
|
2011-04-16 16:04:15 +02:00
|
|
|
if (destination_) {
|
|
|
|
destination_->InsertSongsOrLibraryItems(songs_, row_, play_now_, enqueue_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SongLoaderInserter::EffectiveLoad() {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (SongLoader* loader : pending_async_) {
|
2011-04-16 16:04:15 +02:00
|
|
|
loader->EffectiveSongsLoad();
|
|
|
|
task_manager_->SetTaskProgress(async_load_id_, ++async_progress_);
|
2011-11-18 21:55:54 +01:00
|
|
|
emit EffectiveLoadFinished(loader->songs());
|
2010-06-15 20:24:08 +02:00
|
|
|
}
|
2011-04-16 16:04:15 +02:00
|
|
|
task_manager_->SetTaskFinished(async_load_id_);
|
2011-04-16 17:06:13 +02:00
|
|
|
|
|
|
|
deleteLater();
|
2010-06-15 20:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SongLoaderInserter::Finished() {
|
2011-03-06 15:46:01 +01:00
|
|
|
if (destination_) {
|
|
|
|
destination_->InsertSongsOrLibraryItems(songs_, row_, play_now_, enqueue_);
|
|
|
|
}
|
2010-06-15 20:24:08 +02:00
|
|
|
|
|
|
|
deleteLater();
|
|
|
|
}
|