strawberry-audio-player-win.../src/subsonic/subsonicurlhandler.cpp

74 lines
2.4 KiB
C++
Raw Normal View History

2019-06-17 23:54:24 +02:00
/*
* Strawberry Music Player
2021-03-20 21:14:47 +01:00
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
2019-06-17 23:54:24 +02:00
*
* Strawberry 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.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
2020-02-12 00:07:05 +01:00
#include "config.h"
2020-02-09 02:29:35 +01:00
#include <QtGlobal>
2019-06-17 23:54:24 +02:00
#include <QString>
#include <QUrl>
#include <QUrlQuery>
2020-02-09 02:29:35 +01:00
#include "core/song.h"
2019-06-17 23:54:24 +02:00
#include "subsonicservice.h"
#include "subsonicurlhandler.h"
class Application;
2019-09-15 20:27:32 +02:00
SubsonicUrlHandler::SubsonicUrlHandler(Application *app, SubsonicService *service) : UrlHandler(service), service_(service) {
Q_UNUSED(app);
}
2019-06-17 23:54:24 +02:00
UrlHandler::LoadResult SubsonicUrlHandler::StartLoading(const QUrl &url) {
2019-06-20 20:49:02 +02:00
if (!server_url().isValid()) {
2021-07-23 21:30:08 +02:00
return LoadResult(url, LoadResult::Error, tr("Subsonic server URL is invalid."));
2019-06-20 20:49:02 +02:00
}
if (username().isEmpty() || password().isEmpty()) {
2021-07-23 21:30:08 +02:00
return LoadResult(url, LoadResult::Error, tr("Missing Subsonic username or password."));
2019-06-20 20:49:02 +02:00
}
2019-06-17 23:54:24 +02:00
ParamList params = ParamList() << Param("c", service_->client_name())
<< Param("v", service_->api_version())
<< Param("f", "json")
<< Param("u", service_->username())
<< Param("p", QString("enc:" + service_->password().toUtf8().toHex()))
<< Param("id", url.path());
QUrlQuery url_query;
2021-06-12 20:53:23 +02:00
for (const Param &param : params) {
url_query.addQueryItem(QUrl::toPercentEncoding(param.first), QUrl::toPercentEncoding(param.second));
2019-06-17 23:54:24 +02:00
}
QUrl stream_url(server_url());
2019-06-18 23:25:10 +02:00
if (!stream_url.path().isEmpty() && stream_url.path().right(1) == "/") {
2019-09-08 19:46:51 +02:00
stream_url.setPath(stream_url.path() + QString("rest/stream.view"));
2019-06-18 23:25:10 +02:00
}
2021-07-23 21:30:08 +02:00
else {
2019-09-08 19:46:51 +02:00
stream_url.setPath(stream_url.path() + QString("/rest/stream.view"));
2021-07-23 21:30:08 +02:00
}
2019-06-18 23:25:10 +02:00
stream_url.setQuery(url_query);
2019-06-17 23:54:24 +02:00
return LoadResult(url, LoadResult::TrackAvailable, stream_url);
2019-06-17 23:54:24 +02:00
}