2014-11-01 19:26:05 +01:00
|
|
|
/* This file is part of Clementine.
|
2014-11-01 19:30:40 +01:00
|
|
|
Copyright 2010-2011, 2013, David Sansome <me@davidsansome.com>
|
|
|
|
Copyright 2010, 2012, 2014, John Maguire <john.maguire@gmail.com>
|
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
2014-11-01 19:26:05 +01: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/>.
|
|
|
|
*/
|
|
|
|
|
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:///";
|
2014-02-07 16:34:20 +01:00
|
|
|
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)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QObject(parent), engine_(engine) {}
|
2010-12-03 14:53:43 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
BackgroundStreams::~BackgroundStreams() { SaveStreams(); }
|
2010-12-03 14:53:43 +01:00
|
|
|
|
|
|
|
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);
|
2014-02-07 16:34:20 +01:00
|
|
|
AddStream(s.value("name").toString(), s.value("url").toUrl(),
|
2013-05-04 11:25:43 +02:00
|
|
|
s.value("volume").toInt());
|
2010-12-03 14:53:43 +01:00
|
|
|
}
|
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.endArray();
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void BackgroundStreams::AddStream(const QString& name, const QUrl& url,
|
2013-05-04 11:25:43 +02:00
|
|
|
int volume) {
|
|
|
|
if (streams_.contains(name)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-03 14:53:43 +01:00
|
|
|
Stream* s = new Stream;
|
|
|
|
s->name = name;
|
|
|
|
s->url = url;
|
|
|
|
s->volume = volume;
|
|
|
|
s->id = -1;
|
|
|
|
streams_[name] = 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)) {
|
2014-02-07 16:34:20 +01:00
|
|
|
qLog(Error) << "Tried to add action for stream" << name
|
|
|
|
<< "which doesn't exist";
|
2011-06-19 16:42:00 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (Stream* stream : streams_.values()) {
|
2011-06-19 16:42:00 +02:00
|
|
|
if (stream->action == action) {
|
2014-02-06 16:49:49 +01:00
|
|
|
stream->action = nullptr;
|
2011-06-19 16:42:00 +02:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (Stream* stream : streams_.values()) {
|
2011-06-19 16:42:00 +02:00
|
|
|
if (stream->action == action) {
|
|
|
|
EnableStream(stream->name, checked);
|
|
|
|
}
|
2010-12-03 14:53:43 +01:00
|
|
|
}
|
|
|
|
}
|