2021-02-12 21:11:20 +01:00
|
|
|
/* This file is part of Clementine.
|
2021-03-07 22:59:32 +01:00
|
|
|
Copyright 2021, Fabio Bas <ctrlaltca@gmail.com>
|
2021-02-12 21:11:20 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "radiobrowserservice.h"
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/network.h"
|
|
|
|
#include "core/player.h"
|
|
|
|
#include "core/taskmanager.h"
|
|
|
|
#include "core/utilities.h"
|
|
|
|
#include "globalsearch/globalsearch.h"
|
|
|
|
#include "globalsearch/radiobrowsersearchprovider.h"
|
2021-03-13 11:28:00 +01:00
|
|
|
#include "internet/internetradio/savedradio.h"
|
2021-03-12 13:00:26 +01:00
|
|
|
#include "radiobrowserurlhandler.h"
|
2021-02-12 21:11:20 +01:00
|
|
|
#include "ui/iconloader.h"
|
|
|
|
|
2021-03-13 11:28:00 +01:00
|
|
|
Q_DECLARE_METATYPE(QStandardItem*)
|
|
|
|
|
2021-03-15 22:47:19 +01:00
|
|
|
namespace {
|
|
|
|
struct Branch {
|
|
|
|
QString name;
|
2021-04-03 16:29:41 +02:00
|
|
|
QString list_url;
|
|
|
|
QString items_url;
|
2021-03-15 22:47:19 +01:00
|
|
|
RadioBrowserService::Type type;
|
|
|
|
};
|
|
|
|
|
|
|
|
QList<Branch> BranchList = {
|
2021-03-07 22:59:32 +01:00
|
|
|
{"By Country", "%1/json/countries",
|
2021-03-15 22:47:19 +01:00
|
|
|
"%1/json/stations/bycountryexact/%2?hidebroken=true",
|
|
|
|
RadioBrowserService::Type_Category},
|
2021-03-07 22:59:32 +01:00
|
|
|
{"By Language", "%1/json/languages",
|
2021-03-15 22:47:19 +01:00
|
|
|
"%1/json/stations/bylanguageexact/%2?hidebroken=true",
|
|
|
|
RadioBrowserService::Type_Category},
|
2021-03-07 22:59:32 +01:00
|
|
|
{"By Tag", "%1/json/tags", "%1/json/stations/bytagexact/%2?hidebroken=true",
|
2021-03-15 22:47:19 +01:00
|
|
|
RadioBrowserService::Type_Category},
|
2021-03-07 22:59:32 +01:00
|
|
|
{"By Codec", "%1/json/codecs",
|
2021-03-15 22:47:19 +01:00
|
|
|
"%1/json/stations/bycodecexact/%2?hidebroken=true",
|
|
|
|
RadioBrowserService::Type_Category},
|
2021-03-07 22:59:32 +01:00
|
|
|
{"Top 100 Clicked", "",
|
|
|
|
"%1/json/stations/search?order=clickcount&reverse=true&limit=100",
|
2021-03-15 22:47:19 +01:00
|
|
|
RadioBrowserService::Type_Top100},
|
2021-03-07 22:59:32 +01:00
|
|
|
{"Top 100 Voted", "",
|
2021-03-15 22:47:19 +01:00
|
|
|
"%1/json/stations/search?order=votes&reverse=true&limit=100",
|
|
|
|
RadioBrowserService::Type_Top100},
|
2021-03-07 22:59:32 +01:00
|
|
|
{"Top 100 Trending", "",
|
|
|
|
"%1/json/stations/search?order=clicktrend&reverse=true&limit=100",
|
2021-03-15 22:47:19 +01:00
|
|
|
RadioBrowserService::Type_Top100}};
|
|
|
|
|
|
|
|
QString SearchUrl = "%1/json/stations/byname/%2?limit=%3";
|
2021-03-27 23:17:14 +01:00
|
|
|
QString StationDetailsUrl = "%1/json/stations/byuuid?uuids=%2";
|
2021-03-15 22:47:19 +01:00
|
|
|
QString PlayClickUrl = "%1/json/url/%2";
|
|
|
|
|
|
|
|
void ReadStation(const QJsonObject& item, RadioBrowserService::StreamList* ret,
|
|
|
|
RadioBrowserUrlHandler* url_handler_) {
|
|
|
|
RadioBrowserService::Stream stream;
|
|
|
|
stream.name_ = item["name"].toString();
|
|
|
|
QUrl url(item["stationuuid"].toString());
|
|
|
|
url.setScheme(url_handler_->scheme());
|
|
|
|
stream.url_ = url;
|
|
|
|
QUrl favicon(item["favicon"].toString());
|
|
|
|
stream.favicon_ = favicon;
|
|
|
|
ret->append(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PopulateCategory(QStandardItem* parentItem, QStringList& elements) {
|
|
|
|
if (parentItem->hasChildren())
|
|
|
|
parentItem->removeRows(0, parentItem->rowCount());
|
|
|
|
|
|
|
|
for (auto element : elements) {
|
|
|
|
QStandardItem* item = new QStandardItem(
|
|
|
|
IconLoader::Load("icon_radio", IconLoader::Lastfm), QString());
|
|
|
|
item->setText(element);
|
|
|
|
item->setData(RadioBrowserService::Type_CategoryItem,
|
|
|
|
InternetModel::Role_Type);
|
|
|
|
item->setData(true, InternetModel::Role_CanLazyLoad);
|
|
|
|
parentItem->appendRow(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PopulateStreams(QStandardItem* parentItem,
|
|
|
|
RadioBrowserService::StreamList& streams) {
|
|
|
|
if (parentItem->hasChildren())
|
|
|
|
parentItem->removeRows(0, parentItem->rowCount());
|
|
|
|
|
|
|
|
for (auto stream : streams) {
|
|
|
|
QStandardItem* item = new QStandardItem(
|
|
|
|
IconLoader::Load("icon_radio", IconLoader::Lastfm), QString());
|
|
|
|
item->setText(stream.name_);
|
|
|
|
item->setData(
|
|
|
|
QVariant::fromValue(stream.ToSong(RadioBrowserService::kServiceName)),
|
|
|
|
InternetModel::Role_SongMetadata);
|
|
|
|
item->setData(InternetModel::PlayBehaviour_SingleItem,
|
|
|
|
InternetModel::Role_PlayBehaviour);
|
|
|
|
item->setData(InternetModel::Type_Track, InternetModel::Role_Type);
|
|
|
|
|
|
|
|
parentItem->appendRow(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool operator<(const RadioBrowserService::Stream& a,
|
|
|
|
const RadioBrowserService::Stream& b) {
|
|
|
|
return a.name_.compare(b.name_, Qt::CaseInsensitive) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* RadioBrowserService::kServiceName = "Radio-Browser.info";
|
|
|
|
const char* RadioBrowserService::kSettingsGroup = "RadioBrowser";
|
|
|
|
const char* RadioBrowserService::kSchemeName = "radiobrowser";
|
|
|
|
const char* RadioBrowserService::defaultServer =
|
|
|
|
"http://all.api.radio-browser.info";
|
2021-03-07 22:59:32 +01:00
|
|
|
|
|
|
|
RadioBrowserService::RadioBrowserService(Application* app,
|
|
|
|
InternetModel* parent)
|
|
|
|
: InternetService(kServiceName, app, parent, parent),
|
2021-02-12 21:11:20 +01:00
|
|
|
root_(nullptr),
|
2021-03-13 11:28:00 +01:00
|
|
|
station_menu_(nullptr),
|
|
|
|
add_to_saved_radio_action_(nullptr),
|
2021-02-12 21:11:20 +01:00
|
|
|
network_(new NetworkAccessManager(this)),
|
2021-03-12 13:00:26 +01:00
|
|
|
url_handler_(new RadioBrowserUrlHandler(app, this, this)),
|
2021-03-07 22:59:32 +01:00
|
|
|
homepage_url_(QUrl("https://www.radio-browser.info")),
|
|
|
|
icon_(IconLoader::Load("radiobrowser", IconLoader::Provider)) {
|
2021-03-12 13:00:26 +01:00
|
|
|
app_->player()->RegisterUrlHandler(url_handler_);
|
2021-02-12 21:11:20 +01:00
|
|
|
app_->global_search()->AddProvider(
|
2021-03-07 22:59:32 +01:00
|
|
|
new RadioBrowserSearchProvider(app_, this, this));
|
2021-03-28 15:23:34 +02:00
|
|
|
connect(app_->player(), SIGNAL(SongChangeRequestProcessed(QUrl, bool)), this,
|
|
|
|
SLOT(SongChangeRequestProcessed(QUrl, bool)));
|
2021-02-12 21:11:20 +01:00
|
|
|
}
|
|
|
|
|
2021-03-07 22:59:32 +01:00
|
|
|
QStandardItem* RadioBrowserService::CreateRootItem() {
|
2021-03-15 22:47:19 +01:00
|
|
|
root_ = new QStandardItem(icon_, RadioBrowserService::kServiceName);
|
2021-02-12 21:11:20 +01:00
|
|
|
root_->setData(true, InternetModel::Role_CanLazyLoad);
|
|
|
|
return root_;
|
|
|
|
}
|
|
|
|
|
2021-03-07 22:59:32 +01:00
|
|
|
void RadioBrowserService::LazyPopulate(QStandardItem* item) {
|
2021-02-12 21:11:20 +01:00
|
|
|
switch (item->data(InternetModel::Role_Type).toInt()) {
|
|
|
|
case InternetModel::Type_Service:
|
2021-03-07 22:59:32 +01:00
|
|
|
RefreshRootItem();
|
|
|
|
break;
|
|
|
|
case RadioBrowserService::Type_Category:
|
|
|
|
RefreshCategory(item);
|
|
|
|
break;
|
|
|
|
case RadioBrowserService::Type_CategoryItem:
|
|
|
|
RefreshCategoryItem(item);
|
|
|
|
break;
|
|
|
|
case RadioBrowserService::Type_Top100:
|
|
|
|
RefreshTop100(item);
|
2021-02-12 21:11:20 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 22:59:32 +01:00
|
|
|
void RadioBrowserService::RefreshRootItem() {
|
2021-03-11 18:56:26 +01:00
|
|
|
if (!EnsureServerConfig()) {
|
|
|
|
ShowConfig();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-07 22:59:32 +01:00
|
|
|
if (root_->hasChildren()) root_->removeRows(0, root_->rowCount());
|
2021-03-15 22:47:19 +01:00
|
|
|
for (auto branch : BranchList) {
|
2021-03-07 22:59:32 +01:00
|
|
|
QStandardItem* item = new QStandardItem(
|
|
|
|
IconLoader::Load("icon_radio", IconLoader::Lastfm), QString());
|
|
|
|
item->setText(branch.name);
|
|
|
|
item->setData(branch.type, InternetModel::Role_Type);
|
2021-04-03 16:29:41 +02:00
|
|
|
item->setData(branch.list_url, RadioBrowserService::Role_ListUrl);
|
|
|
|
item->setData(branch.items_url, RadioBrowserService::Role_ItemsUrl);
|
2021-03-07 22:59:32 +01:00
|
|
|
item->setData(true, InternetModel::Role_CanLazyLoad);
|
|
|
|
root_->appendRow(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RadioBrowserService::RefreshCategory(QStandardItem* item) {
|
2021-03-13 11:28:00 +01:00
|
|
|
if (!EnsureServerConfig()) {
|
|
|
|
ShowConfig();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-03 16:29:41 +02:00
|
|
|
QString determined_url = item->data(RadioBrowserService::Role_ListUrl)
|
|
|
|
.toString()
|
|
|
|
.arg(main_server_url_);
|
|
|
|
QUrl url(determined_url);
|
2021-03-07 22:59:32 +01:00
|
|
|
|
|
|
|
QNetworkReply* reply = network_->get(QNetworkRequest(url));
|
|
|
|
int task_id = app_->task_manager()->StartTask(tr("Getting channels"));
|
|
|
|
|
2021-03-15 22:47:19 +01:00
|
|
|
connect(reply, &QNetworkReply::finished, [=] {
|
|
|
|
app_->task_manager()->SetTaskFinished(task_id);
|
|
|
|
reply->deleteLater();
|
|
|
|
QJsonDocument document = ParseJsonReply(reply);
|
|
|
|
if (document.isNull()) {
|
|
|
|
LastRequestFailed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList list;
|
|
|
|
QJsonArray contents = document.array();
|
|
|
|
qLog(Debug) << "RadioBrowser station list found:" << contents.size();
|
|
|
|
for (const QJsonValue& c : contents) {
|
|
|
|
QJsonObject item = c.toObject();
|
|
|
|
list << item["name"].toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
PopulateCategory(item, list);
|
|
|
|
});
|
2021-03-07 22:59:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RadioBrowserService::RefreshCategoryItem(QStandardItem* item) {
|
2021-03-13 11:28:00 +01:00
|
|
|
if (!EnsureServerConfig()) {
|
|
|
|
ShowConfig();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-07 22:59:32 +01:00
|
|
|
QStandardItem* parent = item->parent();
|
2021-04-03 16:29:41 +02:00
|
|
|
QString determined_url = parent->data(RadioBrowserService::Role_ItemsUrl)
|
|
|
|
.toString()
|
|
|
|
.arg(main_server_url_, item->text());
|
|
|
|
QUrl url(determined_url);
|
2021-03-07 22:59:32 +01:00
|
|
|
|
|
|
|
QNetworkReply* reply = network_->get(QNetworkRequest(url));
|
|
|
|
int task_id = app_->task_manager()->StartTask(tr("Getting channels"));
|
|
|
|
|
2021-03-12 13:00:26 +01:00
|
|
|
connect(reply, &QNetworkReply::finished,
|
|
|
|
[=] { this->RefreshStreamsFinished(reply, task_id, item); });
|
2021-03-07 22:59:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RadioBrowserService::RefreshTop100(QStandardItem* item) {
|
2021-03-13 11:28:00 +01:00
|
|
|
if (!EnsureServerConfig()) {
|
|
|
|
ShowConfig();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-03 16:29:41 +02:00
|
|
|
QString determined_url = item->data(RadioBrowserService::Role_ItemsUrl)
|
|
|
|
.toString()
|
|
|
|
.arg(main_server_url_);
|
|
|
|
QUrl url(determined_url);
|
2021-03-07 22:59:32 +01:00
|
|
|
|
|
|
|
QNetworkReply* reply = network_->get(QNetworkRequest(url));
|
|
|
|
int task_id = app_->task_manager()->StartTask(tr("Getting channels"));
|
|
|
|
|
2021-03-12 13:00:26 +01:00
|
|
|
connect(reply, &QNetworkReply::finished,
|
|
|
|
[=] { this->RefreshStreamsFinished(reply, task_id, item); });
|
2021-03-07 22:59:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RadioBrowserService::ShowContextMenu(const QPoint& global_pos) {
|
|
|
|
if (!model()->current_index().isValid()) return;
|
|
|
|
QStandardItem* item = model()->itemFromIndex(model()->current_index());
|
|
|
|
if (!item) return;
|
|
|
|
|
2021-03-13 11:28:00 +01:00
|
|
|
switch (item->data(InternetModel::Role_Type).toInt()) {
|
|
|
|
case InternetModel::Type_Service:
|
|
|
|
GetContextMenu(item)->popup(global_pos);
|
|
|
|
break;
|
|
|
|
case InternetModel::Type_Track:
|
|
|
|
GetStationMenu(item)->popup(global_pos);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QMenu* RadioBrowserService::GetContextMenu(QStandardItem* item) {
|
|
|
|
Q_UNUSED(item);
|
2021-02-12 21:11:20 +01:00
|
|
|
if (!context_menu_) {
|
2021-03-15 22:47:19 +01:00
|
|
|
context_menu_.reset(new QMenu);
|
2021-02-12 21:11:20 +01:00
|
|
|
context_menu_->addAction(IconLoader::Load("download", IconLoader::Base),
|
|
|
|
tr("Open %1 in browser").arg(homepage_url_.host()),
|
|
|
|
this, SLOT(Homepage()));
|
|
|
|
|
|
|
|
context_menu_->addAction(IconLoader::Load("view-refresh", IconLoader::Base),
|
|
|
|
tr("Refresh channels"), this,
|
2021-03-11 18:56:26 +01:00
|
|
|
SLOT(ReloadSettings()));
|
|
|
|
context_menu_->addAction(IconLoader::Load("configure", IconLoader::Base),
|
|
|
|
tr("Configure..."), this, SLOT(ShowConfig()));
|
2021-02-12 21:11:20 +01:00
|
|
|
}
|
2021-03-15 22:47:19 +01:00
|
|
|
return context_menu_.get();
|
2021-03-13 11:28:00 +01:00
|
|
|
}
|
2021-02-12 21:11:20 +01:00
|
|
|
|
2021-03-13 11:28:00 +01:00
|
|
|
QMenu* RadioBrowserService::GetStationMenu(QStandardItem* item) {
|
|
|
|
if (!station_menu_) {
|
2021-03-15 22:47:19 +01:00
|
|
|
station_menu_.reset(new QMenu);
|
2021-03-13 11:28:00 +01:00
|
|
|
station_menu_->addActions(GetPlaylistActions());
|
2021-03-15 22:47:19 +01:00
|
|
|
add_to_saved_radio_action_.reset(
|
2021-03-13 11:28:00 +01:00
|
|
|
new QAction(IconLoader::Load("document-open-remote", IconLoader::Base),
|
2021-03-15 22:47:19 +01:00
|
|
|
tr("Add to your radio streams"), station_menu_.get()));
|
|
|
|
connect(add_to_saved_radio_action_.get(), &QAction::triggered, this,
|
2021-03-13 11:28:00 +01:00
|
|
|
&RadioBrowserService::AddToSavedRadio);
|
2021-03-15 22:47:19 +01:00
|
|
|
station_menu_->addAction(add_to_saved_radio_action_.get());
|
2021-03-13 11:28:00 +01:00
|
|
|
}
|
|
|
|
add_to_saved_radio_action_->setData(QVariant::fromValue(item));
|
2021-03-15 22:47:19 +01:00
|
|
|
return station_menu_.get();
|
2021-03-07 22:59:32 +01:00
|
|
|
}
|
2021-02-12 21:11:20 +01:00
|
|
|
|
2021-03-07 22:59:32 +01:00
|
|
|
void RadioBrowserService::RefreshStreamsFinished(QNetworkReply* reply,
|
2021-03-11 18:56:26 +01:00
|
|
|
int task_id,
|
|
|
|
QStandardItem* item) {
|
2021-03-07 22:59:32 +01:00
|
|
|
app_->task_manager()->SetTaskFinished(task_id);
|
|
|
|
reply->deleteLater();
|
|
|
|
QJsonDocument document = ParseJsonReply(reply);
|
2021-03-13 11:28:00 +01:00
|
|
|
if (document.isNull()) {
|
|
|
|
LastRequestFailed();
|
|
|
|
return;
|
|
|
|
}
|
2021-02-12 21:11:20 +01:00
|
|
|
|
2021-03-07 22:59:32 +01:00
|
|
|
StreamList list;
|
2021-02-12 21:11:20 +01:00
|
|
|
QJsonArray contents = document.array();
|
|
|
|
qLog(Debug) << "RadioBrowser station list found:" << contents.size();
|
|
|
|
for (const QJsonValue& c : contents) {
|
2021-03-12 13:00:26 +01:00
|
|
|
QJsonObject station = c.toObject();
|
2021-03-15 22:47:19 +01:00
|
|
|
ReadStation(station, &list, url_handler_);
|
2021-02-12 21:11:20 +01:00
|
|
|
}
|
|
|
|
|
2021-03-07 22:59:32 +01:00
|
|
|
PopulateStreams(item, list);
|
2021-02-12 21:11:20 +01:00
|
|
|
}
|
|
|
|
|
2021-03-07 22:59:32 +01:00
|
|
|
Song RadioBrowserService::Stream::ToSong(const QString& prefix) const {
|
2021-02-12 21:11:20 +01:00
|
|
|
QString song_title = name_.trimmed();
|
|
|
|
if (!song_title.startsWith(prefix)) {
|
|
|
|
song_title = prefix + " " + song_title;
|
|
|
|
}
|
|
|
|
|
|
|
|
Song ret;
|
|
|
|
ret.set_valid(true);
|
|
|
|
ret.set_title(song_title);
|
|
|
|
ret.set_artist(name_);
|
|
|
|
ret.set_url(url_);
|
2021-03-11 16:59:02 +01:00
|
|
|
ret.set_art_automatic(favicon_.toString());
|
2021-02-12 21:11:20 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-07 22:59:32 +01:00
|
|
|
void RadioBrowserService::Homepage() {
|
2021-02-12 21:11:20 +01:00
|
|
|
QDesktopServices::openUrl(homepage_url_);
|
|
|
|
}
|
|
|
|
|
2021-03-07 22:59:32 +01:00
|
|
|
PlaylistItem::Options RadioBrowserService::playlistitem_options() const {
|
|
|
|
return PlaylistItem::PauseDisabled | PlaylistItem::SeekDisabled;
|
2021-02-12 21:11:20 +01:00
|
|
|
}
|
|
|
|
|
2021-03-11 18:56:26 +01:00
|
|
|
void RadioBrowserService::ReloadSettings() {
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
|
2021-03-13 11:28:00 +01:00
|
|
|
main_server_url_ =
|
2021-03-15 22:47:19 +01:00
|
|
|
s.value("server", QVariant(RadioBrowserService::defaultServer))
|
|
|
|
.toString();
|
2021-03-11 18:56:26 +01:00
|
|
|
|
|
|
|
if (root_ && root_->hasChildren()) {
|
|
|
|
root_->removeRows(0, root_->rowCount());
|
|
|
|
root_->setData(true, InternetModel::Role_CanLazyLoad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RadioBrowserService::EnsureServerConfig() {
|
2021-03-15 22:47:19 +01:00
|
|
|
return !main_server_url_.isEmpty();
|
2021-03-11 18:56:26 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 11:28:00 +01:00
|
|
|
void RadioBrowserService::LastRequestFailed() { ShowConfig(); }
|
|
|
|
|
2021-03-11 18:56:26 +01:00
|
|
|
void RadioBrowserService::ShowConfig() {
|
|
|
|
app_->OpenSettingsDialogAtPage(SettingsDialog::Page_RadioBrowser);
|
|
|
|
}
|
2021-03-07 22:59:32 +01:00
|
|
|
|
|
|
|
void RadioBrowserService::Search(int search_id, const QString& query,
|
2021-03-11 18:56:26 +01:00
|
|
|
const int limit) {
|
2021-04-03 16:29:41 +02:00
|
|
|
QString determined_url = SearchUrl.arg(main_server_url_, query).arg(limit);
|
|
|
|
QUrl url(determined_url);
|
2021-03-07 22:59:32 +01:00
|
|
|
|
|
|
|
QNetworkReply* reply = network_->get(QNetworkRequest(url));
|
|
|
|
int task_id = app_->task_manager()->StartTask(tr("Getting channels"));
|
|
|
|
|
2021-03-15 22:47:19 +01:00
|
|
|
connect(reply, &QNetworkReply::finished, [=] {
|
|
|
|
app_->task_manager()->SetTaskFinished(task_id);
|
|
|
|
reply->deleteLater();
|
|
|
|
QJsonDocument document = ParseJsonReply(reply);
|
|
|
|
|
|
|
|
StreamList list;
|
|
|
|
QJsonArray contents = document.array();
|
|
|
|
qLog(Debug) << "RadioBrowser station list found:" << contents.size();
|
|
|
|
for (const QJsonValue& c : contents) {
|
|
|
|
QJsonObject item = c.toObject();
|
|
|
|
ReadStation(item, &list, url_handler_);
|
|
|
|
}
|
|
|
|
|
|
|
|
emit SearchFinished(search_id, list);
|
|
|
|
});
|
2021-03-07 22:59:32 +01:00
|
|
|
}
|
|
|
|
|
2021-03-12 13:00:26 +01:00
|
|
|
void RadioBrowserService::ResolveStationUrl(const QUrl& original_url) {
|
2021-04-03 16:29:41 +02:00
|
|
|
QString determined_url =
|
2021-03-27 23:17:14 +01:00
|
|
|
StationDetailsUrl.arg(main_server_url_, original_url.path());
|
2021-04-03 16:29:41 +02:00
|
|
|
QUrl url(determined_url);
|
2021-03-07 22:59:32 +01:00
|
|
|
|
|
|
|
QNetworkReply* reply = network_->get(QNetworkRequest(url));
|
2021-03-12 13:00:26 +01:00
|
|
|
int task_id = app_->task_manager()->StartTask(tr("Getting station"));
|
|
|
|
|
|
|
|
connect(reply, &QNetworkReply::finished, [=] {
|
2021-03-15 22:47:19 +01:00
|
|
|
app_->task_manager()->SetTaskFinished(task_id);
|
|
|
|
reply->deleteLater();
|
|
|
|
QJsonDocument document = ParseJsonReply(reply);
|
2021-03-12 13:00:26 +01:00
|
|
|
|
2021-03-27 23:17:14 +01:00
|
|
|
StreamList list;
|
|
|
|
QJsonArray contents = document.array();
|
2021-03-28 15:23:34 +02:00
|
|
|
QJsonObject item = contents.first().toObject();
|
|
|
|
if (item.isEmpty()) {
|
2021-04-03 16:29:41 +02:00
|
|
|
emit StationUrlResolveFailed(original_url);
|
2021-03-28 15:23:34 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-03-27 23:17:14 +01:00
|
|
|
|
|
|
|
Song ret;
|
|
|
|
ret.set_valid(true);
|
|
|
|
ret.set_title(item["name"].toString());
|
2021-05-28 18:31:24 +02:00
|
|
|
QUrl url(item["url_resolved"].toString());
|
2021-05-29 10:21:58 +02:00
|
|
|
if (url.isEmpty()) url.setUrl(item["url"].toString());
|
2021-03-27 23:17:14 +01:00
|
|
|
ret.set_url(url);
|
|
|
|
ret.set_art_automatic(item["favicon"].toString());
|
|
|
|
|
|
|
|
emit StreamMetadataFound(original_url, ret);
|
2021-03-15 22:47:19 +01:00
|
|
|
});
|
2021-03-12 13:00:26 +01:00
|
|
|
}
|
2021-03-13 11:28:00 +01:00
|
|
|
|
2021-03-28 15:23:34 +02:00
|
|
|
void RadioBrowserService::SongChangeRequestProcessed(const QUrl& url,
|
|
|
|
bool valid) {
|
|
|
|
if (!valid || url.scheme() != url_handler_->scheme()) return;
|
|
|
|
|
|
|
|
// inform the server that the station is playing
|
2021-04-03 16:29:41 +02:00
|
|
|
QString determined_url = PlayClickUrl.arg(main_server_url_, url.path());
|
|
|
|
QUrl playUrl(determined_url);
|
2021-03-28 15:23:34 +02:00
|
|
|
QNetworkReply* playReply = network_->get(QNetworkRequest(playUrl));
|
|
|
|
connect(playReply, &QNetworkReply::finished,
|
|
|
|
[=] { playReply->deleteLater(); });
|
|
|
|
}
|
|
|
|
|
2021-03-13 11:28:00 +01:00
|
|
|
void RadioBrowserService::AddToSavedRadio(bool checked) {
|
|
|
|
Q_UNUSED(checked);
|
|
|
|
QAction* action = qobject_cast<QAction*>(sender());
|
|
|
|
if (!action) return;
|
|
|
|
|
|
|
|
QStandardItem* item = action->data().value<QStandardItem*>();
|
|
|
|
if (!item) return;
|
|
|
|
|
|
|
|
Song station = item->data(InternetModel::Role_SongMetadata).value<Song>();
|
|
|
|
InternetModel::Service<SavedRadio>()->Add(station.url(), station.artist(),
|
|
|
|
QUrl(station.art_automatic()));
|
2021-03-18 00:57:12 +01:00
|
|
|
}
|