1
0
mirror of https://github.com/clementine-player/Clementine synced 2025-01-31 11:35:24 +01:00

Use smart pointers for menus; move static vars and most private methods to an anonymous namespace

This commit is contained in:
Fabio Bas 2021-03-15 22:47:19 +01:00 committed by John Maguire
parent 3ba2c811f4
commit 89e9b20df1
4 changed files with 153 additions and 187 deletions

View File

@ -40,6 +40,87 @@
Q_DECLARE_METATYPE(QStandardItem*)
namespace {
struct Branch {
QString name;
QString listUrl;
QString itemsUrl;
RadioBrowserService::Type type;
};
QList<Branch> BranchList = {
{"By Country", "%1/json/countries",
"%1/json/stations/bycountryexact/%2?hidebroken=true",
RadioBrowserService::Type_Category},
{"By Language", "%1/json/languages",
"%1/json/stations/bylanguageexact/%2?hidebroken=true",
RadioBrowserService::Type_Category},
{"By Tag", "%1/json/tags", "%1/json/stations/bytagexact/%2?hidebroken=true",
RadioBrowserService::Type_Category},
{"By Codec", "%1/json/codecs",
"%1/json/stations/bycodecexact/%2?hidebroken=true",
RadioBrowserService::Type_Category},
{"Top 100 Clicked", "",
"%1/json/stations/search?order=clickcount&reverse=true&limit=100",
RadioBrowserService::Type_Top100},
{"Top 100 Voted", "",
"%1/json/stations/search?order=votes&reverse=true&limit=100",
RadioBrowserService::Type_Top100},
{"Top 100 Trending", "",
"%1/json/stations/search?order=clicktrend&reverse=true&limit=100",
RadioBrowserService::Type_Top100}};
QString SearchUrl = "%1/json/stations/byname/%2?limit=%3";
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;
@ -48,29 +129,8 @@ bool operator<(const RadioBrowserService::Stream& a,
const char* RadioBrowserService::kServiceName = "Radio-Browser.info";
const char* RadioBrowserService::kSettingsGroup = "RadioBrowser";
const char* RadioBrowserService::kSchemeName = "radiobrowser";
const QString RadioBrowserService::defaultServer =
QStringLiteral("http://all.api.radio-browser.info");
QString RadioBrowserService::SearchUrl = "%1/json/stations/byname/%2?limit=%3";
QString RadioBrowserService::PlayClickUrl = "%1/json/url/%2";
QList<RadioBrowserService::Branch> RadioBrowserService::BranchList = {
{"By Country", "%1/json/countries",
"%1/json/stations/bycountryexact/%2?hidebroken=true", Type_Category},
{"By Language", "%1/json/languages",
"%1/json/stations/bylanguageexact/%2?hidebroken=true", Type_Category},
{"By Tag", "%1/json/tags", "%1/json/stations/bytagexact/%2?hidebroken=true",
Type_Category},
{"By Codec", "%1/json/codecs",
"%1/json/stations/bycodecexact/%2?hidebroken=true", Type_Category},
{"Top 100 Clicked", "",
"%1/json/stations/search?order=clickcount&reverse=true&limit=100",
Type_Top100},
{"Top 100 Voted", "",
"%1/json/stations/search?order=votes&reverse=true&limit=100", Type_Top100},
{"Top 100 Trending", "",
"%1/json/stations/search?order=clicktrend&reverse=true&limit=100",
Type_Top100}};
const char* RadioBrowserService::defaultServer =
"http://all.api.radio-browser.info";
RadioBrowserService::RadioBrowserService(Application* app,
InternetModel* parent)
@ -80,8 +140,6 @@ RadioBrowserService::RadioBrowserService(Application* app,
station_menu_(nullptr),
add_to_saved_radio_action_(nullptr),
network_(new NetworkAccessManager(this)),
name_(kServiceName),
url_scheme_(kSchemeName),
url_handler_(new RadioBrowserUrlHandler(app, this, this)),
homepage_url_(QUrl("https://www.radio-browser.info")),
icon_(IconLoader::Load("radiobrowser", IconLoader::Provider)) {
@ -90,10 +148,8 @@ RadioBrowserService::RadioBrowserService(Application* app,
new RadioBrowserSearchProvider(app_, this, this));
}
RadioBrowserService::~RadioBrowserService() { delete context_menu_; }
QStandardItem* RadioBrowserService::CreateRootItem() {
root_ = new QStandardItem(icon_, name_);
root_ = new QStandardItem(icon_, RadioBrowserService::kServiceName);
root_->setData(true, InternetModel::Role_CanLazyLoad);
return root_;
}
@ -124,7 +180,7 @@ void RadioBrowserService::RefreshRootItem() {
}
if (root_->hasChildren()) root_->removeRows(0, root_->rowCount());
for (auto branch : RadioBrowserService::BranchList) {
for (auto branch : BranchList) {
QStandardItem* item = new QStandardItem(
IconLoader::Load("icon_radio", IconLoader::Lastfm), QString());
item->setText(branch.name);
@ -150,8 +206,25 @@ void RadioBrowserService::RefreshCategory(QStandardItem* item) {
QNetworkReply* reply = network_->get(QNetworkRequest(url));
int task_id = app_->task_manager()->StartTask(tr("Getting channels"));
connect(reply, &QNetworkReply::finished,
[=] { this->RefreshCategoryFinished(reply, task_id, item); });
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);
});
}
void RadioBrowserService::RefreshCategoryItem(QStandardItem* item) {
@ -211,7 +284,7 @@ void RadioBrowserService::ShowContextMenu(const QPoint& global_pos) {
QMenu* RadioBrowserService::GetContextMenu(QStandardItem* item) {
Q_UNUSED(item);
if (!context_menu_) {
context_menu_ = new QMenu;
context_menu_.reset(new QMenu);
context_menu_->addAction(IconLoader::Load("download", IconLoader::Base),
tr("Open %1 in browser").arg(homepage_url_.host()),
this, SLOT(Homepage()));
@ -222,44 +295,22 @@ QMenu* RadioBrowserService::GetContextMenu(QStandardItem* item) {
context_menu_->addAction(IconLoader::Load("configure", IconLoader::Base),
tr("Configure..."), this, SLOT(ShowConfig()));
}
return context_menu_;
return context_menu_.get();
}
QMenu* RadioBrowserService::GetStationMenu(QStandardItem* item) {
if (!station_menu_) {
station_menu_ = new QMenu;
station_menu_.reset(new QMenu);
station_menu_->addActions(GetPlaylistActions());
add_to_saved_radio_action_ =
add_to_saved_radio_action_.reset(
new QAction(IconLoader::Load("document-open-remote", IconLoader::Base),
tr("Add to your radio streams"), station_menu_);
connect(add_to_saved_radio_action_, &QAction::triggered, this,
tr("Add to your radio streams"), station_menu_.get()));
connect(add_to_saved_radio_action_.get(), &QAction::triggered, this,
&RadioBrowserService::AddToSavedRadio);
station_menu_->addAction(add_to_saved_radio_action_);
station_menu_->addAction(add_to_saved_radio_action_.get());
}
add_to_saved_radio_action_->setData(QVariant::fromValue(item));
return station_menu_;
}
void RadioBrowserService::RefreshCategoryFinished(QNetworkReply* reply,
int task_id,
QStandardItem* item) {
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);
return station_menu_.get();
}
void RadioBrowserService::RefreshStreamsFinished(QNetworkReply* reply,
@ -278,23 +329,12 @@ void RadioBrowserService::RefreshStreamsFinished(QNetworkReply* reply,
qLog(Debug) << "RadioBrowser station list found:" << contents.size();
for (const QJsonValue& c : contents) {
QJsonObject station = c.toObject();
ReadStation(station, &list);
ReadStation(station, &list, url_handler_);
}
PopulateStreams(item, list);
}
void RadioBrowserService::ReadStation(QJsonObject& item, StreamList* ret) {
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);
}
Song RadioBrowserService::Stream::ToSong(const QString& prefix) const {
QString song_title = name_.trimmed();
if (!song_title.startsWith(prefix)) {
@ -318,47 +358,13 @@ PlaylistItem::Options RadioBrowserService::playlistitem_options() const {
return PlaylistItem::PauseDisabled | PlaylistItem::SeekDisabled;
}
void RadioBrowserService::PopulateCategory(QStandardItem* parentItem,
QStringList& elements) {
if (parentItem->hasChildren())
parentItem->removeRows(0, parentItem->rowCount());
for (const QString& 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 RadioBrowserService::PopulateStreams(QStandardItem* parentItem,
StreamList& streams) {
if (parentItem->hasChildren())
parentItem->removeRows(0, parentItem->rowCount());
for (const Stream& stream : streams) {
QStandardItem* item = new QStandardItem(
IconLoader::Load("icon_radio", IconLoader::Lastfm), QString());
item->setText(stream.name_);
item->setData(QVariant::fromValue(stream.ToSong(name_)),
InternetModel::Role_SongMetadata);
item->setData(InternetModel::PlayBehaviour_SingleItem,
InternetModel::Role_PlayBehaviour);
item->setData(InternetModel::Type_Track, InternetModel::Role_Type);
parentItem->appendRow(item);
}
}
void RadioBrowserService::ReloadSettings() {
QSettings s;
s.beginGroup(kSettingsGroup);
main_server_url_ =
s.value("server", RadioBrowserService::defaultServer).toString();
s.value("server", QVariant(RadioBrowserService::defaultServer))
.toString();
if (root_ && root_->hasChildren()) {
root_->removeRows(0, root_->rowCount());
@ -367,10 +373,7 @@ void RadioBrowserService::ReloadSettings() {
}
bool RadioBrowserService::EnsureServerConfig() {
if (main_server_url_.isEmpty())
return false;
else
return true;
return !main_server_url_.isEmpty();
}
void RadioBrowserService::LastRequestFailed() { ShowConfig(); }
@ -381,59 +384,48 @@ void RadioBrowserService::ShowConfig() {
void RadioBrowserService::Search(int search_id, const QString& query,
const int limit) {
QString determinedUrl =
RadioBrowserService::SearchUrl.arg(main_server_url_, query).arg(limit);
QString determinedUrl = SearchUrl.arg(main_server_url_, query).arg(limit);
QUrl url(determinedUrl);
QNetworkReply* reply = network_->get(QNetworkRequest(url));
int task_id = app_->task_manager()->StartTask(tr("Getting channels"));
connect(reply, &QNetworkReply::finished,
[=] { this->SearchFinishedInternal(reply, task_id, search_id); });
}
connect(reply, &QNetworkReply::finished, [=] {
app_->task_manager()->SetTaskFinished(task_id);
reply->deleteLater();
QJsonDocument document = ParseJsonReply(reply);
void RadioBrowserService::SearchFinishedInternal(QNetworkReply* reply,
int task_id, int search_id) {
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_);
}
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);
}
emit SearchFinished(search_id, list);
emit SearchFinished(search_id, list);
});
}
void RadioBrowserService::ResolveStationUrl(const QUrl& original_url) {
QString determinedUrl = RadioBrowserService::PlayClickUrl.arg(
main_server_url_, original_url.path());
QString determinedUrl =
PlayClickUrl.arg(main_server_url_, original_url.path());
QUrl url(determinedUrl);
QNetworkReply* reply = network_->get(QNetworkRequest(url));
int task_id = app_->task_manager()->StartTask(tr("Getting station"));
connect(reply, &QNetworkReply::finished, [=] {
this->ResolveStationUrlFinished(reply, task_id, original_url);
app_->task_manager()->SetTaskFinished(task_id);
reply->deleteLater();
QJsonDocument document = ParseJsonReply(reply);
QJsonObject item = document.object();
QUrl url(item["url"].toString());
emit StationUrlResolved(original_url, url);
});
}
void RadioBrowserService::ResolveStationUrlFinished(QNetworkReply* reply,
int task_id,
const QUrl& original_url) {
app_->task_manager()->SetTaskFinished(task_id);
reply->deleteLater();
QJsonDocument document = ParseJsonReply(reply);
QJsonObject item = document.object();
QUrl url(item["url"].toString());
emit StationUrlResolved(original_url, url);
}
void RadioBrowserService::AddToSavedRadio(bool checked) {
Q_UNUSED(checked);
QAction* action = qobject_cast<QAction*>(sender());

View File

@ -19,6 +19,7 @@
#define INTERNET_RADIOBROWSER_RADIOBROWSERSERVICE_H_
#include <QJsonObject>
#include <QMenu>
#include "core/cachedlist.h"
#include "internet/core/internetmodel.h"
@ -28,14 +29,13 @@ class RadioBrowserUrlHandler;
class QNetworkAccessManager;
class QNetworkReply;
class QMenu;
class RadioBrowserService : public InternetService {
Q_OBJECT
public:
RadioBrowserService(Application* app, InternetModel* parent);
~RadioBrowserService();
~RadioBrowserService(){};
enum ItemType {
Type_Stream = 2000,
@ -60,26 +60,15 @@ class RadioBrowserService : public InternetService {
Song ToSong(const QString& prefix) const;
};
struct Branch {
QString name;
QString listUrl;
QString itemsUrl;
Type type;
};
static QList<Branch> BranchList;
static QString SearchUrl;
static QString PlayClickUrl;
typedef QList<Stream> StreamList;
static const char* kServiceName;
static const char* kSettingsGroup;
static const char* kSchemeName;
static const QString defaultServer;
static const char* defaultServer;
const QString& url_scheme() const { return url_scheme_; }
const QIcon& icon() const { return icon_; }
QString url_scheme() const { return kSchemeName; }
QIcon icon() const { return icon_; }
QNetworkAccessManager* network() const { return network_; }
QStandardItem* CreateRootItem() override;
@ -104,38 +93,26 @@ class RadioBrowserService : public InternetService {
void RefreshCategory(QStandardItem* item);
void RefreshCategoryItem(QStandardItem* item);
void RefreshTop100(QStandardItem* item);
void RefreshCategoryFinished(QNetworkReply* reply, int task_id,
QStandardItem* item);
void RefreshStreamsFinished(QNetworkReply* reply, int task_id,
QStandardItem* item);
void ResolveStationUrlFinished(QNetworkReply* reply, int task_id,
const QUrl& original_url);
void SearchFinishedInternal(QNetworkReply* reply, int task_id, int search_id);
void Homepage();
void ShowConfig() override;
void AddToSavedRadio(bool checked);
private:
void ReadStation(QJsonObject& value, StreamList* ret);
void PopulateCategory(QStandardItem* parentItem, QStringList& elements);
void PopulateStreams(QStandardItem* parentItem, StreamList& streams);
bool EnsureServerConfig();
QMenu* GetContextMenu(QStandardItem* item);
QMenu* GetStationMenu(QStandardItem* item);
bool EnsureServerConfig();
void LastRequestFailed();
private:
QStandardItem* root_;
QMenu* context_menu_;
QMenu* station_menu_;
QAction* add_to_saved_radio_action_;
std::unique_ptr<QMenu> context_menu_;
std::unique_ptr<QMenu> station_menu_;
std::unique_ptr<QAction> add_to_saved_radio_action_;
QNetworkAccessManager* network_;
const QString name_;
const QString url_scheme_;
RadioBrowserUrlHandler* url_handler_;
QString main_server_url_;
const QUrl homepage_url_;

View File

@ -22,7 +22,6 @@
#include "core/logging.h"
#include "internet/core/internetmodel.h"
#include "ui/iconloader.h"
#include "ui_radiobrowsersettingspage.h"
RadioBrowserSettingsPage::RadioBrowserSettingsPage(SettingsDialog* dialog)
: SettingsPage(dialog),
@ -37,14 +36,13 @@ RadioBrowserSettingsPage::RadioBrowserSettingsPage(SettingsDialog* dialog)
SLOT(RestoreDefaultServer()));
}
RadioBrowserSettingsPage::~RadioBrowserSettingsPage() { delete ui_; }
void RadioBrowserSettingsPage::Load() {
QSettings s;
s.beginGroup(RadioBrowserService::kSettingsGroup);
ui_->server->setText(
s.value("server", RadioBrowserService::defaultServer).toString());
s.value("server", QVariant(RadioBrowserService::defaultServer))
.toString());
}
void RadioBrowserSettingsPage::Save() {

View File

@ -20,15 +20,14 @@
#include "radiobrowserservice.h"
#include "ui/settingspage.h"
class Ui_RadioBrowserSettingsPage;
#include "ui_radiobrowsersettingspage.h"
class RadioBrowserSettingsPage : public SettingsPage {
Q_OBJECT
public:
explicit RadioBrowserSettingsPage(SettingsDialog* dialog);
~RadioBrowserSettingsPage();
~RadioBrowserSettingsPage(){};
void Load();
void Save();
@ -38,7 +37,7 @@ class RadioBrowserSettingsPage : public SettingsPage {
void RestoreDefaultServer();
private:
Ui_RadioBrowserSettingsPage* ui_;
std::unique_ptr<Ui_RadioBrowserSettingsPage> ui_;
RadioBrowserService* service_;
};