strawberry-audio-player-win.../src/moodbar/moodbarcontroller.cpp

112 lines
3.1 KiB
C++
Raw Normal View History

2019-04-18 15:03:01 +02:00
/* This file was part of Clementine.
Copyright 2012, 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/>.
*/
2020-02-09 02:29:35 +01:00
#include <memory>
2019-04-18 15:03:01 +02:00
#include <QObject>
#include <QByteArray>
#include <QUrl>
#include "core/application.h"
#include "core/player.h"
2020-02-09 02:29:35 +01:00
#include "core/song.h"
#include "core/closure.h"
2020-02-09 02:29:35 +01:00
#include "engine/engine_fwd.h"
#include "settings/moodbarsettingspage.h"
2019-04-18 15:03:01 +02:00
#include "playlist/playlistmanager.h"
#include "moodbarcontroller.h"
#include "moodbarloader.h"
#include "moodbarpipeline.h"
2021-01-26 16:48:04 +01:00
MoodbarController::MoodbarController(Application *app, QObject *parent)
2019-04-18 15:03:01 +02:00
: QObject(parent),
app_(app),
enabled_(false) {
2019-04-18 15:03:01 +02:00
2021-01-26 16:48:04 +01:00
QObject::connect(app_->playlist_manager(), &PlaylistManager::CurrentSongChanged, this, &MoodbarController::CurrentSongChanged);
QObject::connect(app_->player(), &Player::Stopped, this, &MoodbarController::PlaybackStopped);
2019-04-18 15:03:01 +02:00
ReloadSettings();
2019-04-18 15:03:01 +02:00
}
void MoodbarController::ReloadSettings() {
QSettings s;
s.beginGroup(MoodbarSettingsPage::kSettingsGroup);
enabled_ = s.value("enabled", false).toBool();
s.endGroup();
}
void MoodbarController::CurrentSongChanged(const Song &song) {
if (!enabled_) return;
2019-04-18 15:03:01 +02:00
QByteArray data;
2021-01-26 16:48:04 +01:00
MoodbarPipeline *pipeline = nullptr;
2019-04-18 15:03:01 +02:00
const MoodbarLoader::Result result = app_->moodbar_loader()->Load(song.url(), &data, &pipeline);
switch (result) {
case MoodbarLoader::CannotLoad:
emit CurrentMoodbarDataChanged(QByteArray());
break;
case MoodbarLoader::Loaded:
emit CurrentMoodbarDataChanged(data);
break;
case MoodbarLoader::WillLoadAsync:
// Emit an empty array for now so the GUI reverts to a normal progress
// bar. Our slot will be called when the data is actually loaded.
emit CurrentMoodbarDataChanged(QByteArray());
NewClosure(pipeline, SIGNAL(Finished(bool)), this, SLOT(AsyncLoadComplete(MoodbarPipeline*, QUrl)), pipeline, song.url());
2019-04-18 15:03:01 +02:00
break;
}
}
void MoodbarController::PlaybackStopped() {
if (enabled_) {
emit CurrentMoodbarDataChanged(QByteArray());
}
2019-04-18 15:03:01 +02:00
}
2021-01-26 16:48:04 +01:00
void MoodbarController::AsyncLoadComplete(MoodbarPipeline *pipeline, const QUrl &url) {
2019-04-18 15:03:01 +02:00
// Is this song still playing?
PlaylistItemPtr current_item = app_->player()->GetCurrentItem();
if (current_item && current_item->Url() != url) {
return;
}
// Did we stop the song?
switch(app_->player()->GetState()) {
case Engine::Error:
case Engine::Empty:
case Engine::Idle:
return;
default:
break;
}
emit CurrentMoodbarDataChanged(pipeline->data());
}