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

110 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/>.
*/
#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/settings.h"
2023-04-22 19:13:42 +02:00
#include "engine/enginebase.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),
2021-07-11 07:40:57 +02:00
app_(app),
enabled_(false) {
2019-04-18 15:03:01 +02: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() {
Settings 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;
const MoodbarLoader::Result result = app_->moodbar_loader()->Load(song.url(), song.has_cue(), &data, &pipeline);
2019-04-18 15:03:01 +02:00
switch (result) {
2023-02-18 14:09:27 +01:00
case MoodbarLoader::Result::CannotLoad:
2019-04-18 15:03:01 +02:00
emit CurrentMoodbarDataChanged(QByteArray());
break;
2023-02-18 14:09:27 +01:00
case MoodbarLoader::Result::Loaded:
2019-04-18 15:03:01 +02:00
emit CurrentMoodbarDataChanged(data);
break;
2023-02-18 14:09:27 +01:00
case MoodbarLoader::Result::WillLoadAsync:
2019-04-18 15:03:01 +02:00
// 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());
2021-01-29 18:47:50 +01:00
QObject::connect(pipeline, &MoodbarPipeline::Finished, this, [this, pipeline, song]() { AsyncLoadComplete(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?
2021-07-11 09:49:38 +02:00
switch (app_->player()->GetState()) {
2023-04-22 19:13:42 +02:00
case EngineBase::State::Error:
case EngineBase::State::Empty:
case EngineBase::State::Idle:
2019-04-18 15:03:01 +02:00
return;
default:
break;
}
emit CurrentMoodbarDataChanged(pipeline->data());
}