mirror of
https://github.com/clementine-player/Clementine
synced 2025-01-31 03:27:40 +01:00
Simplify Subsonic URL handling
This commit is contained in:
parent
38ce86529e
commit
d05202265e
@ -21,13 +21,11 @@ const char* SubsonicService::kApiClientName = "Clementine";
|
||||
SubsonicService::SubsonicService(Application* app, InternetModel *parent)
|
||||
: InternetService(kServiceName, app, parent, parent),
|
||||
network_(new QNetworkAccessManager(this)),
|
||||
http_url_handler_(new SubsonicUrlHandler(this, this)),
|
||||
https_url_handler_(new SubsonicHttpsUrlHandler(this, this)),
|
||||
url_handler_(new SubsonicUrlHandler(this, this)),
|
||||
login_state_(LoginState_OtherError),
|
||||
item_lookup_()
|
||||
{
|
||||
app_->player()->RegisterUrlHandler(http_url_handler_);
|
||||
app_->player()->RegisterUrlHandler(https_url_handler_);
|
||||
app_->player()->RegisterUrlHandler(url_handler_);
|
||||
connect(this, SIGNAL(LoginStateChanged(SubsonicService::LoginState)),
|
||||
SLOT(onLoginStateChanged(SubsonicService::LoginState)));
|
||||
}
|
||||
@ -112,11 +110,6 @@ void SubsonicService::GetMusicDirectory(const QString &id)
|
||||
Send(url, SLOT(onGetMusicDirectoryFinished()));
|
||||
}
|
||||
|
||||
QModelIndex SubsonicService::GetCurrentIndex()
|
||||
{
|
||||
return context_item_;
|
||||
}
|
||||
|
||||
QUrl SubsonicService::BuildRequestUrl(const QString &view)
|
||||
{
|
||||
QUrl url(server_ + "rest/" + view + ".view");
|
||||
@ -127,6 +120,11 @@ QUrl SubsonicService::BuildRequestUrl(const QString &view)
|
||||
return url;
|
||||
}
|
||||
|
||||
QModelIndex SubsonicService::GetCurrentIndex()
|
||||
{
|
||||
return context_item_;
|
||||
}
|
||||
|
||||
void SubsonicService::Send(const QUrl &url, const char *slot)
|
||||
{
|
||||
QNetworkRequest request(url);
|
||||
@ -193,9 +191,7 @@ void SubsonicService::ReadTrack(QXmlStreamReader *reader, QStandardItem *parent)
|
||||
qint64 length = reader->attributes().value("duration").toString().toInt();
|
||||
length *= 1000000000;
|
||||
song.set_length_nanosec(length);
|
||||
QUrl url = BuildRequestUrl("stream");
|
||||
url.setScheme(url.scheme() == "https" ? "subsonics" : "subsonic");
|
||||
url.addQueryItem("id", id);
|
||||
QUrl url = QUrl(QString("subsonic://%1").arg(id));
|
||||
song.set_url(url);
|
||||
song.set_filesize(reader->attributes().value("size").toString().toInt());
|
||||
|
||||
|
@ -67,6 +67,8 @@ class SubsonicService : public InternetService
|
||||
void GetIndexes();
|
||||
void GetMusicDirectory(const QString &id);
|
||||
|
||||
QUrl BuildRequestUrl(const QString &view);
|
||||
|
||||
static const char* kServiceName;
|
||||
static const char* kSettingsGroup;
|
||||
static const char* kApiVersion;
|
||||
@ -79,7 +81,6 @@ class SubsonicService : public InternetService
|
||||
QModelIndex GetCurrentIndex();
|
||||
|
||||
private:
|
||||
QUrl BuildRequestUrl(const QString &view);
|
||||
// Convenience function to reduce QNetworkRequest/QNetworkReply/connect boilerplate
|
||||
void Send(const QUrl &url, const char *slot);
|
||||
|
||||
@ -91,8 +92,7 @@ class SubsonicService : public InternetService
|
||||
QModelIndex context_item_;
|
||||
QStandardItem* root_;
|
||||
QNetworkAccessManager* network_;
|
||||
SubsonicUrlHandler* http_url_handler_;
|
||||
SubsonicHttpsUrlHandler* https_url_handler_;
|
||||
SubsonicUrlHandler* url_handler_;
|
||||
|
||||
// Configuration
|
||||
QString server_;
|
||||
|
@ -10,7 +10,7 @@ UrlHandler::LoadResult SubsonicUrlHandler::StartLoading(const QUrl& url) {
|
||||
if (service_->login_state() != SubsonicService::LoginState_Loggedin)
|
||||
return LoadResult();
|
||||
|
||||
QUrl newurl(url);
|
||||
newurl.setScheme(realscheme());
|
||||
QUrl newurl = service_->BuildRequestUrl("stream");
|
||||
newurl.addQueryItem("id", url.host());
|
||||
return LoadResult(url, LoadResult::TrackAvailable, newurl);
|
||||
}
|
||||
|
@ -5,10 +5,7 @@
|
||||
|
||||
class SubsonicService;
|
||||
|
||||
// Subsonic URL handler.
|
||||
// For now, at least, Subsonic URLs are just HTTP URLs but with the scheme
|
||||
// replaced with "subsonic" (or "subsonics" for HTTPS). This is a hook to
|
||||
// allow magic to be implemented later.
|
||||
// Subsonic URL handler: subsonic://id
|
||||
class SubsonicUrlHandler : public UrlHandler {
|
||||
Q_OBJECT
|
||||
public:
|
||||
@ -19,23 +16,8 @@ class SubsonicUrlHandler : public UrlHandler {
|
||||
LoadResult StartLoading(const QUrl& url);
|
||||
//LoadResult LoadNext(const QUrl& url);
|
||||
|
||||
protected:
|
||||
virtual QString realscheme() const { return "http"; }
|
||||
|
||||
private:
|
||||
SubsonicService* service_;
|
||||
};
|
||||
|
||||
class SubsonicHttpsUrlHandler : public SubsonicUrlHandler {
|
||||
Q_OBJECT
|
||||
public:
|
||||
SubsonicHttpsUrlHandler(SubsonicService* service, QObject* parent)
|
||||
: SubsonicUrlHandler(service, parent) {}
|
||||
|
||||
QString scheme() const { return "subsonics"; }
|
||||
|
||||
protected:
|
||||
QString realscheme() const { return "https"; }
|
||||
};
|
||||
|
||||
#endif // SUBSONICURLHANDLER_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user