2010-12-03 14:53:43 +01:00
|
|
|
#include "backgroundstreams.h"
|
|
|
|
|
2011-06-19 16:42:00 +02:00
|
|
|
#include <QAction>
|
2010-12-03 14:53:43 +01:00
|
|
|
#include <QSettings>
|
|
|
|
#include <QtDebug>
|
|
|
|
|
2011-06-19 16:42:00 +02:00
|
|
|
#include "core/logging.h"
|
2010-12-03 14:53:43 +01:00
|
|
|
#include "engines/enginebase.h"
|
|
|
|
|
|
|
|
const char* BackgroundStreams::kSettingsGroup = "BackgroundStreams";
|
|
|
|
const char* BackgroundStreams::kHypnotoadUrl = "hypnotoad:///";
|
|
|
|
const char* BackgroundStreams::kRainUrl = "http://data.clementine-player.org/rainymood";
|
2012-01-10 17:00:17 +01:00
|
|
|
const char* BackgroundStreams::kEnterpriseUrl = "enterprise:///";
|
2010-12-03 14:53:43 +01:00
|
|
|
|
|
|
|
BackgroundStreams::BackgroundStreams(EngineBase* engine, QObject* parent)
|
|
|
|
: QObject(parent),
|
|
|
|
engine_(engine) {
|
|
|
|
}
|
|
|
|
|
|
|
|
BackgroundStreams::~BackgroundStreams() {
|
|
|
|
SaveStreams();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundStreams::LoadStreams() {
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
|
|
|
|
int version = s.value("version", 0).toInt();
|
2012-01-10 17:00:17 +01:00
|
|
|
if (version < 1) {
|
2010-12-04 16:11:50 +01:00
|
|
|
AddStream(QT_TR_NOOP("Hypnotoad"), QUrl(kHypnotoadUrl));
|
|
|
|
AddStream(QT_TR_NOOP("Rain"), QUrl(kRainUrl));
|
2012-01-10 17:00:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (version < kVersion) {
|
|
|
|
s.setValue("version", kVersion);
|
|
|
|
AddStream(QT_TR_NOOP("Make it so!"), QUrl(kEnterpriseUrl));
|
2010-12-03 14:53:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int size = s.beginReadArray("streams");
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
s.setArrayIndex(i);
|
|
|
|
AddStream(s.value("name").toString(),
|
|
|
|
s.value("url").toUrl(),
|
|
|
|
s.value("volume").toInt(),
|
|
|
|
s.value("enabled").toBool());
|
|
|
|
}
|
2012-01-10 17:00:17 +01:00
|
|
|
|
|
|
|
SaveStreams();
|
2010-12-03 14:53:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundStreams::SaveStreams() {
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
|
|
|
|
QList<Stream*> values = streams_.values();
|
|
|
|
s.beginWriteArray("streams");
|
|
|
|
for (int i = 0; i < values.size(); ++i) {
|
|
|
|
s.setArrayIndex(i);
|
|
|
|
Stream* stream = values[i];
|
|
|
|
s.setValue("name", stream->name);
|
|
|
|
s.setValue("url", stream->url);
|
|
|
|
s.setValue("volume", stream->volume);
|
|
|
|
s.setValue("enabled", stream->id != -1);
|
|
|
|
}
|
|
|
|
s.endArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundStreams::AddStream(const QString& name,
|
|
|
|
const QUrl& url,
|
|
|
|
int volume,
|
|
|
|
bool enabled) {
|
|
|
|
Stream* s = new Stream;
|
|
|
|
s->name = name;
|
|
|
|
s->url = url;
|
|
|
|
s->volume = volume;
|
|
|
|
s->id = -1;
|
|
|
|
streams_[name] = s;
|
|
|
|
if (enabled) {
|
|
|
|
PlayStream(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundStreams::EnableStream(const QString& name, bool enable) {
|
2011-06-19 16:42:00 +02:00
|
|
|
if (!streams_.contains(name)) {
|
|
|
|
qLog(Warning) << "Tried to toggle a stream" << name << "which didn't exist";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-03 14:53:43 +01:00
|
|
|
Stream* stream = streams_[name];
|
|
|
|
if (enable == (stream->id != -1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (enable) {
|
|
|
|
PlayStream(stream);
|
|
|
|
} else {
|
|
|
|
StopStream(stream);
|
|
|
|
}
|
2012-01-21 00:24:43 +01:00
|
|
|
SaveStreams();
|
2010-12-03 14:53:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundStreams::SetStreamVolume(const QString& name, int volume) {
|
|
|
|
Stream* stream = streams_[name];
|
|
|
|
stream->volume = volume;
|
|
|
|
if (stream->id != -1) {
|
|
|
|
engine_->SetBackgroundStreamVolume(stream->id, stream->volume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundStreams::PlayStream(Stream* stream) {
|
|
|
|
stream->id = engine_->AddBackgroundStream(stream->url);
|
|
|
|
engine_->SetBackgroundStreamVolume(stream->id, stream->volume);
|
|
|
|
emit StreamStarted(stream->name);
|
2011-06-19 16:42:00 +02:00
|
|
|
|
|
|
|
if (stream->action) {
|
|
|
|
stream->action->setChecked(true);
|
|
|
|
}
|
2010-12-03 14:53:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundStreams::StopStream(Stream* stream) {
|
|
|
|
engine_->StopBackgroundStream(stream->id);
|
|
|
|
stream->id = -1;
|
|
|
|
emit StreamStopped(stream->name);
|
2011-06-19 16:42:00 +02:00
|
|
|
|
|
|
|
if (stream->action) {
|
|
|
|
stream->action->setChecked(false);
|
|
|
|
}
|
2010-12-03 14:53:43 +01:00
|
|
|
}
|
|
|
|
|
2011-06-19 16:42:00 +02:00
|
|
|
int BackgroundStreams::GetStreamVolume(const QString& name) const {
|
2010-12-03 14:53:43 +01:00
|
|
|
return streams_[name]->volume;
|
|
|
|
}
|
|
|
|
|
2011-06-19 16:42:00 +02:00
|
|
|
bool BackgroundStreams::IsPlaying(const QString& name) const {
|
2010-12-03 14:53:43 +01:00
|
|
|
return streams_[name]->id != -1;
|
|
|
|
}
|
|
|
|
|
2011-06-19 16:42:00 +02:00
|
|
|
void BackgroundStreams::AddAction(const QString& name, QAction* action) {
|
|
|
|
if (!streams_.contains(name)) {
|
|
|
|
qLog(Error) << "Tried to add action for stream" << name << "which doesn't exist";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream* stream = streams_[name];
|
|
|
|
if (stream->action) {
|
|
|
|
qLog(Error) << "Tried to add multiple actions for stream" << name;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->action = action;
|
|
|
|
action->setChecked(IsPlaying(name));
|
|
|
|
connect(action, SIGNAL(toggled(bool)), SLOT(StreamActionToggled(bool)));
|
|
|
|
connect(action, SIGNAL(destroyed()), SLOT(StreamActionDestroyed()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundStreams::StreamActionDestroyed() {
|
|
|
|
QAction* action = static_cast<QAction*>(sender());
|
|
|
|
if (!action) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (Stream* stream, streams_.values()) {
|
|
|
|
if (stream->action == action) {
|
|
|
|
stream->action = NULL;
|
|
|
|
}
|
2010-12-03 14:53:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-19 16:42:00 +02:00
|
|
|
void BackgroundStreams::StreamActionToggled(bool checked) {
|
|
|
|
QAction* action = static_cast<QAction*>(sender());
|
|
|
|
if (!action) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (Stream* stream, streams_.values()) {
|
|
|
|
if (stream->action == action) {
|
|
|
|
EnableStream(stream->name, checked);
|
|
|
|
}
|
2010-12-03 14:53:43 +01:00
|
|
|
}
|
|
|
|
}
|