Add Subsonic support (#180)

This commit is contained in:
Jonas Kvinge
2019-06-17 23:54:24 +02:00
committed by GitHub
parent a9da8811fc
commit 7b54cef23b
44 changed files with 2656 additions and 43 deletions

View File

@@ -0,0 +1,207 @@
/*
* Strawberry Music Player
* Copyright 2019, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#include "config.h"
#include <QObject>
#include <QByteArray>
#include <QPair>
#include <QList>
#include <QString>
#include <QUrl>
#include <QUrlQuery>
#include <QSslConfiguration>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonParseError>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include "core/logging.h"
#include "core/network.h"
#include "subsonicservice.h"
#include "subsonicbaserequest.h"
SubsonicBaseRequest::SubsonicBaseRequest(SubsonicService *service, NetworkAccessManager *network, QObject *parent) :
QObject(parent),
service_(service),
network_(network)
{}
SubsonicBaseRequest::~SubsonicBaseRequest() {
while (!replies_.isEmpty()) {
QNetworkReply *reply = replies_.takeFirst();
disconnect(reply, 0, nullptr, 0);
if (reply->isRunning()) reply->abort();
reply->deleteLater();
}
}
QUrl SubsonicBaseRequest::CreateUrl(const QString &ressource_name, const QList<Param> &params_provided) {
ParamList params = ParamList() << params_provided
<< Param("c", client_name())
<< Param("v", api_version())
<< Param("f", "json")
<< Param("u", username())
<< Param("p", QString("enc:" + password().toUtf8().toHex()));
QUrlQuery url_query;
for (const Param& param : params) {
EncodedParam encoded_param(QUrl::toPercentEncoding(param.first), QUrl::toPercentEncoding(param.second));
url_query.addQueryItem(encoded_param.first, encoded_param.second);
}
QUrl url;
url.setScheme("https");
url.setHost(hostname());
if (port() > 0 && port() != 443)
url.setPort(port());
url.setPath(QString("/rest/") + ressource_name);
url.setQuery(url_query);
return url;
}
QNetworkReply *SubsonicBaseRequest::CreateGetRequest(const QString &ressource_name, const QList<Param> &params_provided) {
QUrl url = CreateUrl(ressource_name, params_provided);
QNetworkRequest req(url);
if (!verify_certificate()) {
QSslConfiguration sslconfig = QSslConfiguration::defaultConfiguration();
sslconfig.setPeerVerifyMode(QSslSocket::VerifyNone);
req.setSslConfiguration(sslconfig);
}
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QNetworkReply *reply = network_->get(req);
replies_ << reply;
//qLog(Debug) << "Subsonic: Sending request" << url;
return reply;
}
QByteArray SubsonicBaseRequest::GetReplyData(QNetworkReply *reply, QString &error) {
if (replies_.contains(reply)) {
replies_.removeAll(reply);
reply->deleteLater();
}
QByteArray data;
if (reply->error() == QNetworkReply::NoError) {
data = reply->readAll();
}
else {
if (reply->error() < 200) {
// This is a network error, there is nothing more to do.
error = Error(QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
}
else {
// See if there is Json data containing "error" - then use that instead.
data = reply->readAll();
QJsonParseError parse_error;
QJsonDocument json_doc = QJsonDocument::fromJson(data, &parse_error);
QString failure_reason;
if (parse_error.error == QJsonParseError::NoError && !json_doc.isNull() && !json_doc.isEmpty() && json_doc.isObject()) {
QJsonObject json_obj = json_doc.object();
if (!json_obj.isEmpty() && json_obj.contains("error")) {
QJsonValue json_error = json_obj["error"];
if (json_error.isObject()) {
json_obj = json_error.toObject();
if (!json_obj.isEmpty() && json_obj.contains("code") && json_obj.contains("message")) {
int code = json_obj["code"].toInt();
QString message = json_obj["message"].toString();
failure_reason = QString("%1 (%2)").arg(message).arg(code);
}
}
}
}
if (failure_reason.isEmpty()) {
failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
}
error = Error(failure_reason);
}
return QByteArray();
}
return data;
}
QJsonObject SubsonicBaseRequest::ExtractJsonObj(QByteArray &data, QString &error) {
QJsonParseError json_error;
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
if (json_error.error != QJsonParseError::NoError) {
error = Error("Reply from server missing Json data.", data);
return QJsonObject();
}
if (json_doc.isNull() || json_doc.isEmpty()) {
error = Error("Received empty Json document.", data);
return QJsonObject();
}
if (!json_doc.isObject()) {
error = Error("Json document is not an object.", json_doc);
return QJsonObject();
}
QJsonObject json_obj = json_doc.object();
if (json_obj.isEmpty()) {
error = Error("Received empty Json object.", json_doc);
return QJsonObject();
}
if (!json_obj.contains("subsonic-response")) {
error = Error("Json reply is missing subsonic-response.", json_obj);
return QJsonObject();
}
QJsonValue json_response = json_obj["subsonic-response"];
if (!json_response.isObject()) {
error = Error("Json response is not an object.", json_response);
return QJsonObject();
}
json_obj = json_response.toObject();
return json_obj;
}
QString SubsonicBaseRequest::Error(QString error, QVariant debug) {
qLog(Error) << "Subsonic:" << error;
if (debug.isValid()) qLog(Debug) << debug;
return error;
}

View File

@@ -0,0 +1,86 @@
/*
* Strawberry Music Player
* Copyright 2019, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#ifndef SUBSONICBASEREQUEST_H
#define SUBSONICBASEREQUEST_H
#include "config.h"
#include <QtGlobal>
#include <QObject>
#include <QList>
#include <QPair>
#include <QString>
#include <QUrl>
#include <QNetworkReply>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include "core/song.h"
#include "internet/internetservices.h"
#include "internet/internetservice.h"
#include "internet/internetsearch.h"
#include "subsonicservice.h"
class Application;
class NetworkAccessManager;
class SubsonicUrlHandler;
class CollectionBackend;
class CollectionModel;
class SubsonicBaseRequest : public QObject {
Q_OBJECT
public:
SubsonicBaseRequest(SubsonicService *service, NetworkAccessManager *network, QObject *parent);
~SubsonicBaseRequest();
typedef QPair<QString, QString> Param;
typedef QList<Param> ParamList;
typedef QPair<QByteArray, QByteArray> EncodedParam;
typedef QList<EncodedParam> EncodedParamList;
QUrl CreateUrl(const QString &ressource_name, const QList<Param> &params_provided);
QNetworkReply *CreateGetRequest(const QString &ressource_name, const QList<Param> &params_provided);
QByteArray GetReplyData(QNetworkReply *reply, QString &error);
QJsonObject ExtractJsonObj(QByteArray &data, QString &error);
virtual QString Error(QString error, QVariant debug = QVariant());
QString client_name() { return service_->client_name(); }
QString api_version() { return service_->api_version(); }
QString hostname() { return service_->hostname(); }
int port() { return service_->port(); }
QString username() { return service_->username(); }
QString password() { return service_->password(); }
bool verify_certificate() { return service_->verify_certificate(); }
bool cache_album_covers() { return service_->cache_album_covers(); }
private:
SubsonicService *service_;
NetworkAccessManager *network_;
QList<QNetworkReply*> replies_;
};
#endif // SUBSONICBASEREQUEST_H

View File

@@ -0,0 +1,759 @@
/*
* Strawberry Music Player
* Copyright 2019, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#include "config.h"
#include <assert.h>
#include <QObject>
#include <QByteArray>
#include <QDir>
#include <QString>
#include <QUrl>
#include <QImage>
#include <QNetworkReply>
#include <QSslConfiguration>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QMimeDatabase>
#include "core/closure.h"
#include "core/logging.h"
#include "core/network.h"
#include "core/song.h"
#include "core/timeconstants.h"
#include "organise/organiseformat.h"
#include "subsonicservice.h"
#include "subsonicurlhandler.h"
#include "subsonicrequest.h"
const int SubsonicRequest::kMaxConcurrentAlbumsRequests = 3;
const int SubsonicRequest::kMaxConcurrentAlbumSongsRequests = 3;
const int SubsonicRequest::kMaxConcurrentAlbumCoverRequests = 1;
SubsonicRequest::SubsonicRequest(SubsonicService *service, SubsonicUrlHandler *url_handler, NetworkAccessManager *network, QObject *parent)
: SubsonicBaseRequest(service, network, parent),
service_(service),
url_handler_(url_handler),
network_(network),
finished_(false),
albums_requests_active_(0),
album_songs_requests_active_(0),
album_songs_requested_(0),
album_songs_received_(0),
album_covers_requests_active_(),
album_covers_requested_(0),
album_covers_received_(0),
no_results_(false)
{}
SubsonicRequest::~SubsonicRequest() {
while (!album_cover_replies_.isEmpty()) {
QNetworkReply *reply = album_cover_replies_.takeFirst();
disconnect(reply, 0, nullptr, 0);
if (reply->isRunning()) reply->abort();
reply->deleteLater();
}
}
void SubsonicRequest::Reset() {
finished_ = false;
albums_requests_queue_.clear();
album_songs_requests_queue_.clear();
album_cover_requests_queue_.clear();
album_songs_requests_pending_.clear();
album_covers_requests_sent_.clear();
albums_requests_active_ = 0;
album_songs_requests_active_ = 0;
album_songs_requested_ = 0;
album_songs_received_ = 0;
album_covers_requests_active_ = 0;
album_covers_requested_ = 0;
album_covers_received_ = 0;
songs_.clear();
errors_.clear();
no_results_ = false;
album_cover_replies_.clear();
}
void SubsonicRequest::GetAlbums() {
emit UpdateStatus(tr("Retrieving albums..."));
emit UpdateProgress(0);
AddAlbumsRequest();
}
void SubsonicRequest::AddAlbumsRequest(const int offset, const int size) {
Request request;
request.size = size;
request.offset = offset;
albums_requests_queue_.enqueue(request);
if (albums_requests_active_ < kMaxConcurrentAlbumsRequests) FlushAlbumsRequests();
}
void SubsonicRequest::FlushAlbumsRequests() {
while (!albums_requests_queue_.isEmpty() && albums_requests_active_ < kMaxConcurrentAlbumsRequests) {
Request request = albums_requests_queue_.dequeue();
++albums_requests_active_;
ParamList params = ParamList() << Param("type", "alphabeticalByName");
if (request.size > 0) params << Param("size", QString::number(request.size));
if (request.offset > 0) params << Param("offset", QString::number(request.offset));
QNetworkReply *reply;
reply = CreateGetRequest(QString("getAlbumList2"), params);
NewClosure(reply, SIGNAL(finished()), this, SLOT(AlbumsReplyReceived(QNetworkReply*, int)), reply, request.offset);
}
}
void SubsonicRequest::AlbumsReplyReceived(QNetworkReply *reply, const int offset_requested) {
--albums_requests_active_;
QString error;
QByteArray data = GetReplyData(reply, error);
if (finished_) return;
if (data.isEmpty()) {
AlbumsFinishCheck(offset_requested);
return;
}
QJsonObject json_obj = ExtractJsonObj(data, error);
if (json_obj.isEmpty()) {
AlbumsFinishCheck(offset_requested);
return;
}
if (json_obj.contains("error")) {
QJsonValue json_error = json_obj["error"];
if (!json_error.isObject()) {
Error("Json error is not an object.", json_obj);
AlbumsFinishCheck(offset_requested);
return;
}
json_obj = json_error.toObject();
if (!json_obj.isEmpty() && json_obj.contains("code") && json_obj.contains("message")) {
int code = json_obj["code"].toInt();
QString message = json_obj["message"].toString();
Error(QString("%1 (%2)").arg(message).arg(code));
AlbumsFinishCheck(offset_requested);
}
else {
Error("Json error object missing code or message.", json_obj);
AlbumsFinishCheck(offset_requested);
return;
}
return;
}
if (!json_obj.contains("albumList") && !json_obj.contains("albumList2")) {
error = Error("Json reply is missing albumList.", json_obj);
AlbumsFinishCheck(offset_requested);
return;
}
QJsonValue json_albumlist;
if (json_obj.contains("albumList")) json_albumlist = json_obj["albumList"];
else if (json_obj.contains("albumList2")) json_albumlist = json_obj["albumList2"];
if (!json_albumlist.isObject()) {
error = Error("Json album list is not an object.", json_albumlist);
AlbumsFinishCheck(offset_requested);
}
json_obj = json_albumlist.toObject();
if (json_obj.isEmpty()) {
if (offset_requested == 0) no_results_ = true;
AlbumsFinishCheck(offset_requested);
return;
}
if (!json_obj.contains("album")) {
error = Error("Json album list does not contain album array.", json_obj);
AlbumsFinishCheck(offset_requested);
}
QJsonValue json_album = json_obj["album"];
if (json_album.isNull()) {
if (offset_requested == 0) no_results_ = true;
AlbumsFinishCheck(offset_requested);
return;
}
if (!json_album.isArray()) {
error = Error("Json album is not an array.", json_album);
AlbumsFinishCheck(offset_requested);
}
QJsonArray json_albums = json_album.toArray();
if (json_albums.isEmpty()) {
if (offset_requested == 0) no_results_ = true;
AlbumsFinishCheck(offset_requested);
return;
}
int albums_received = 0;
for (const QJsonValue &value : json_albums) {
++albums_received;
if (!value.isObject()) {
Error("Invalid Json reply, album is not an object.", value);
continue;
}
QJsonObject json_obj = value.toObject();
if (!json_obj.contains("id") || !json_obj.contains("artist")) {
Error("Invalid Json reply, album object is missing ID or artist.", json_obj);
continue;
}
if (!json_obj.contains("album") && !json_obj.contains("name")) {
Error("Invalid Json reply, album object is missing album or name.", json_obj);
continue;
}
int album_id = json_obj["id"].toString().toInt();
QString artist = json_obj["artist"].toString();
QString album;
if (json_obj.contains("album")) album = json_obj["album"].toString();
else if (json_obj.contains("name")) album = json_obj["name"].toString();
if (album_songs_requests_pending_.contains(album_id)) continue;
Request request;
request.album_id = album_id;
request.album_artist = artist;
album_songs_requests_pending_.insert(album_id, request);
}
AlbumsFinishCheck(offset_requested, albums_received);
}
void SubsonicRequest::AlbumsFinishCheck(const int offset, const int albums_received) {
if (finished_) return;
if (albums_received > 0) {
int offset_next = offset + albums_received;
if (offset_next > 0) {
AddAlbumsRequest(offset_next);
}
}
if (!albums_requests_queue_.isEmpty() && albums_requests_active_ < kMaxConcurrentAlbumsRequests) FlushAlbumsRequests();
if (albums_requests_queue_.isEmpty() && albums_requests_active_ <= 0) { // Albums list is finished, get songs for all albums.
QHash<int, Request> ::iterator i;
for (i = album_songs_requests_pending_.begin() ; i != album_songs_requests_pending_.end() ; ++i) {
Request request = i.value();
AddAlbumSongsRequest(request.artist_id, request.album_id, request.album_artist);
}
album_songs_requests_pending_.clear();
if (album_songs_requested_ > 0) {
if (album_songs_requested_ == 1) emit UpdateStatus(tr("Retrieving songs for %1 album...").arg(album_songs_requested_));
else emit UpdateStatus(tr("Retrieving songs for %1 albums...").arg(album_songs_requested_));
emit ProgressSetMaximum(album_songs_requested_);
emit UpdateProgress(0);
}
}
FinishCheck();
}
void SubsonicRequest::AddAlbumSongsRequest(const int artist_id, const int album_id, const QString &album_artist, const int offset) {
Request request;
request.artist_id = artist_id;
request.album_id = album_id;
request.album_artist = album_artist;
request.offset = offset;
album_songs_requests_queue_.enqueue(request);
++album_songs_requested_;
if (album_songs_requests_active_ < kMaxConcurrentAlbumSongsRequests) FlushAlbumSongsRequests();
}
void SubsonicRequest::FlushAlbumSongsRequests() {
while (!album_songs_requests_queue_.isEmpty() && album_songs_requests_active_ < kMaxConcurrentAlbumSongsRequests) {
Request request = album_songs_requests_queue_.dequeue();
++album_songs_requests_active_;
ParamList params = ParamList() << Param("id", QString::number(request.album_id));
QNetworkReply *reply = CreateGetRequest(QString("getAlbum"), params);
NewClosure(reply, SIGNAL(finished()), this, SLOT(AlbumSongsReplyReceived(QNetworkReply*, int, int, QString)), reply, request.artist_id, request.album_id, request.album_artist);
}
}
void SubsonicRequest::AlbumSongsReplyReceived(QNetworkReply *reply, const int artist_id, const int album_id, const QString album_artist) {
--album_songs_requests_active_;
++album_songs_received_;
emit UpdateProgress(album_songs_received_);
QString error;
QByteArray data = GetReplyData(reply, error);
if (finished_) return;
if (data.isEmpty()) {
SongsFinishCheck();
return;
}
QJsonObject json_obj = ExtractJsonObj(data, error);
if (json_obj.isEmpty()) {
SongsFinishCheck();
return;
}
if (json_obj.contains("error")) {
QJsonValue json_error = json_obj["error"];
if (!json_error.isObject()) {
Error("Json error is not an object.", json_obj);
SongsFinishCheck();
return;
}
json_obj = json_error.toObject();
if (!json_obj.isEmpty() && json_obj.contains("code") && json_obj.contains("message")) {
int code = json_obj["code"].toInt();
QString message = json_obj["message"].toString();
Error(QString("%1 (%2)").arg(message).arg(code));
SongsFinishCheck();
}
else {
Error("Json error object missing code or message.", json_obj);
SongsFinishCheck();
}
return;
}
if (!json_obj.contains("album")) {
error = Error("Json reply is missing albumList.", json_obj);
SongsFinishCheck();
return;
}
QJsonValue json_album = json_obj["album"];
if (!json_album.isObject()) {
error = Error("Json album is not an object.", json_album);
SongsFinishCheck();
return;
}
QJsonObject json_album_obj = json_album.toObject();
if (!json_album_obj.contains("song")) {
error = Error("Json album object does not contain song array.", json_obj);
SongsFinishCheck();
return;
}
QJsonValue json_song = json_album_obj["song"];
if (!json_song.isArray()) {
error = Error("Json song is not an array.", json_album_obj);
SongsFinishCheck();
return;
}
QJsonArray json_array = json_song.toArray();
bool compilation = false;
bool multidisc = false;
SongList songs;
int songs_received = 0;
for (const QJsonValue &value : json_array) {
if (!value.isObject()) {
Error("Invalid Json reply, track is not a object.", value);
continue;
}
QJsonObject json_obj = value.toObject();
++songs_received;
Song song;
ParseSong(song, json_obj, artist_id, album_id, album_artist);
if (!song.is_valid()) continue;
if (song.disc() >= 2) multidisc = true;
if (song.is_compilation()) compilation = true;
songs << song;
}
for (Song &song : songs) {
if (compilation) song.set_compilation_detected(true);
if (multidisc) {
QString album_full(QString("%1 - (Disc %2)").arg(song.album()).arg(song.disc()));
song.set_album(album_full);
}
songs_ << song;
}
SongsFinishCheck();
}
void SubsonicRequest::SongsFinishCheck() {
if (finished_) return;
if (!album_songs_requests_queue_.isEmpty() && album_songs_requests_active_ < kMaxConcurrentAlbumSongsRequests) FlushAlbumSongsRequests();
if (
cache_album_covers() &&
album_songs_requests_queue_.isEmpty() &&
album_songs_requests_active_ <= 0 &&
album_cover_requests_queue_.isEmpty() &&
album_covers_received_ <= 0 &&
album_covers_requests_sent_.isEmpty() &&
album_songs_received_ >= album_songs_requested_
) {
GetAlbumCovers();
}
FinishCheck();
}
int SubsonicRequest::ParseSong(Song &song, const QJsonObject &json_obj, const int artist_id_requested, const int album_id_requested, const QString &album_artist) {
if (
!json_obj.contains("id") ||
!json_obj.contains("title") ||
!json_obj.contains("album") ||
!json_obj.contains("artist") ||
!json_obj.contains("size") ||
!json_obj.contains("contentType") ||
!json_obj.contains("suffix") ||
!json_obj.contains("duration") ||
!json_obj.contains("bitRate") ||
!json_obj.contains("albumId") ||
!json_obj.contains("artistId") ||
!json_obj.contains("type")
) {
Error("Invalid Json reply, song is missing one or more values.", json_obj);
return -1;
}
int song_id = json_obj["id"].toString().toInt();
int album_id = json_obj["albumId"].toString().toInt();
int artist_id = json_obj["artistId"].toString().toInt();
QString title = json_obj["title"].toString();
title.remove(Song::kTitleRemoveMisc);
QString album = json_obj["album"].toString();
QString artist = json_obj["artist"].toString();
int size = json_obj["size"].toInt();
QString mimetype = json_obj["contentType"].toString();
quint64 duration = json_obj["duration"].toInt() * kNsecPerSec;
int bitrate = json_obj["bitRate"].toInt();
int year = 0;
if (json_obj.contains("year")) year = json_obj["year"].toInt();
int disc = 0;
if (json_obj.contains("disc")) disc = json_obj["disc"].toString().toInt();
int track = 0;
if (json_obj.contains("track")) track = json_obj["track"].toInt();
QString genre;
if (json_obj.contains("genre")) genre = json_obj["genre"].toString();
int cover_art_id = -1;
if (json_obj.contains("coverArt")) cover_art_id = json_obj["coverArt"].toString().toInt();
QUrl url;
url.setScheme(url_handler_->scheme());
url.setPath(QString::number(song_id));
QUrl cover_url;
if (cover_art_id != -1) {
const ParamList params = ParamList() << Param("id", QString::number(cover_art_id));
cover_url = CreateUrl("getCoverArt", params);
}
Song::FileType filetype(Song::FileType_Stream);
QMimeDatabase mimedb;
for (QString suffix : mimedb.mimeTypeForName(mimetype.toUtf8()).suffixes()) {
filetype = Song::FiletypeByExtension(suffix);
if (filetype != Song::FileType_Unknown) break;
}
if (filetype == Song::FileType_Unknown) {
qLog(Debug) << "Subsonic: Unknown mimetype" << mimetype;
filetype = Song::FileType_Stream;
}
song.set_source(Song::Source_Subsonic);
song.set_song_id(song_id);
song.set_album_id(album_id);
song.set_artist_id(artist_id);
if (album_artist != artist) song.set_albumartist(album_artist);
song.set_album(album);
song.set_artist(artist);
song.set_title(title);
if (track > 0) song.set_track(track);
if (disc > 0) song.set_disc(disc);
if (year > 0) song.set_year(year);
song.set_url(url);
song.set_length_nanosec(duration);
if (cover_url.isValid()) song.set_art_automatic(cover_url.toEncoded());
song.set_genre(genre);
song.set_directory_id(0);
song.set_filetype(filetype);
song.set_filesize(size);
song.set_mtime(0);
song.set_ctime(0);
song.set_bitrate(bitrate);
song.set_valid(true);
return song_id;
}
void SubsonicRequest::GetAlbumCovers() {
for (Song &song : songs_) {
if (!song.art_automatic().isEmpty()) AddAlbumCoverRequest(song);
}
FlushAlbumCoverRequests();
if (album_covers_requested_ == 1) emit UpdateStatus(tr("Retrieving album cover for %1 album...").arg(album_covers_requested_));
else emit UpdateStatus(tr("Retrieving album covers for %1 albums...").arg(album_covers_requested_));
emit ProgressSetMaximum(album_covers_requested_);
emit UpdateProgress(0);
}
void SubsonicRequest::AddAlbumCoverRequest(Song &song) {
QUrl url(song.art_automatic());
if (!url.isValid()) return;
if (album_covers_requests_sent_.contains(song.album_id())) {
album_covers_requests_sent_.insertMulti(song.album_id(), &song);
return;
}
album_covers_requests_sent_.insertMulti(song.album_id(), &song);
++album_covers_requested_;
AlbumCoverRequest request;
request.album_id = song.album_id();
request.url = url;
request.filename = AlbumCoverFileName(song);
album_cover_requests_queue_.enqueue(request);
}
QString SubsonicRequest::AlbumCoverFileName(const Song &song) {
QString artist = song.effective_albumartist();
QString album = song.effective_album();
QString title = song.title();
artist.remove('/');
album.remove('/');
title.remove('/');
QString filename = artist + "-" + album + ".jpg";
filename = filename.toLower();
filename.replace(' ', '-');
filename.replace("--", "-");
filename.replace(230, "ae");
filename.replace(198, "AE");
filename.replace(246, 'o');
filename.replace(248, 'o');
filename.replace(214, 'O');
filename.replace(216, 'O');
filename.replace(228, 'a');
filename.replace(229, 'a');
filename.replace(196, 'A');
filename.replace(197, 'A');
filename.remove(OrganiseFormat::kValidFatCharacters);
return filename;
}
void SubsonicRequest::FlushAlbumCoverRequests() {
while (!album_cover_requests_queue_.isEmpty() && album_covers_requests_active_ < kMaxConcurrentAlbumCoverRequests) {
AlbumCoverRequest request = album_cover_requests_queue_.dequeue();
++album_covers_requests_active_;
QNetworkRequest req(request.url);
if (!verify_certificate()) {
QSslConfiguration sslconfig = QSslConfiguration::defaultConfiguration();
sslconfig.setPeerVerifyMode(QSslSocket::VerifyNone);
req.setSslConfiguration(sslconfig);
}
QNetworkReply *reply = network_->get(req);
album_cover_replies_ << reply;
NewClosure(reply, SIGNAL(finished()), this, SLOT(AlbumCoverReceived(QNetworkReply*, const int, const QUrl&, const QString&)), reply, request.album_id, request.url, request.filename);
}
}
void SubsonicRequest::AlbumCoverReceived(QNetworkReply *reply, const int album_id, const QUrl &url, const QString &filename) {
if (album_cover_replies_.contains(reply)) {
album_cover_replies_.removeAll(reply);
reply->deleteLater();
}
else {
AlbumCoverFinishCheck();
return;
}
--album_covers_requests_active_;
++album_covers_received_;
if (finished_) return;
emit UpdateProgress(album_covers_received_);
if (!album_covers_requests_sent_.contains(album_id)) {
AlbumCoverFinishCheck();
return;
}
QString error;
if (reply->error() != QNetworkReply::NoError) {
error = Error(QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
album_covers_requests_sent_.remove(album_id);
AlbumCoverFinishCheck();
return;
}
QByteArray data = reply->readAll();
if (data.isEmpty()) {
error = Error(QString("Received empty image data for %1").arg(url.toString()));
album_covers_requests_sent_.remove(album_id);
AlbumCoverFinishCheck();
return;
}
QImage image;
if (image.loadFromData(data)) {
QDir dir;
if (dir.mkpath(service_->CoverCacheDir())) {
QString filepath(service_->CoverCacheDir() + "/" + filename);
if (image.save(filepath, "JPG")) {
while (album_covers_requests_sent_.contains(album_id)) {
Song *song = album_covers_requests_sent_.take(album_id);
song->set_art_automatic(filepath);
}
}
}
}
else {
album_covers_requests_sent_.remove(album_id);
error = Error(QString("Error decoding image data from %1").arg(url.toString()));
}
AlbumCoverFinishCheck();
}
void SubsonicRequest::AlbumCoverFinishCheck() {
if (!album_cover_requests_queue_.isEmpty() && album_covers_requests_active_ < kMaxConcurrentAlbumCoverRequests)
FlushAlbumCoverRequests();
FinishCheck();
}
void SubsonicRequest::FinishCheck() {
if (
!finished_ &&
albums_requests_queue_.isEmpty() &&
album_songs_requests_queue_.isEmpty() &&
album_cover_requests_queue_.isEmpty() &&
album_songs_requests_pending_.isEmpty() &&
album_covers_requests_sent_.isEmpty() &&
albums_requests_active_ <= 0 &&
album_songs_requests_active_ <= 0 &&
album_songs_received_ >= album_songs_requested_ &&
album_covers_requests_active_ <= 0 &&
album_covers_received_ >= album_covers_requested_
) {
finished_ = true;
if (songs_.isEmpty()) {
if (no_results_) emit Results(songs_);
else if (errors_.isEmpty()) emit ErrorSignal(tr("Unknown error"));
else emit ErrorSignal(errors_);
}
else {
emit Results(songs_);
}
}
}
QString SubsonicRequest::Error(QString error, QVariant debug) {
qLog(Error) << "Subsonic:" << error;
if (debug.isValid()) qLog(Debug) << debug;
if (!error.isEmpty()) {
errors_ += error;
errors_ += "<br />";
}
FinishCheck();
return error;
}
void SubsonicRequest::Warn(QString error, QVariant debug) {
qLog(Error) << "Subsonic:" << error;
if (debug.isValid()) qLog(Debug) << debug;
}

View File

@@ -0,0 +1,149 @@
/*
* Strawberry Music Player
* Copyright 2019, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#ifndef SUBSONICREQUEST_H
#define SUBSONICREQUEST_H
#include "config.h"
#include <QtGlobal>
#include <QObject>
#include <QPair>
#include <QList>
#include <QHash>
#include <QMap>
#include <QMultiMap>
#include <QQueue>
#include <QString>
#include <QUrl>
#include <QNetworkReply>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include "core/song.h"
#include "subsonicbaserequest.h"
class NetworkAccessManager;
class SubsonicService;
class SubsonicUrlHandler;
class SubsonicRequest : public SubsonicBaseRequest {
Q_OBJECT
public:
SubsonicRequest(SubsonicService *service, SubsonicUrlHandler *url_handler, NetworkAccessManager *network, QObject *parent);
~SubsonicRequest();
void ReloadSettings();
void GetAlbums();
void Reset();
signals:
void Results(SongList songs);
void ErrorSignal(QString message);
void ErrorSignal(int id, QString message);
void UpdateStatus(QString text);
void ProgressSetMaximum(int max);
void UpdateProgress(int max);
private slots:
void AlbumsReplyReceived(QNetworkReply *reply, const int offset_requested);
void AlbumSongsReplyReceived(QNetworkReply *reply, const int artist_id, const int album_id, const QString album_artist);
void AlbumCoverReceived(QNetworkReply *reply, const int album_id, const QUrl &url, const QString &filename);
private:
typedef QPair<QString, QString> Param;
typedef QList<Param> ParamList;
struct Request {
int artist_id = 0;
int album_id = 0;
int song_id = 0;
int offset = 0;
int size = 0;
QString album_artist;
};
struct AlbumCoverRequest {
int artist_id = 0;
int album_id = 0;
QUrl url;
QString filename;
};
void AddAlbumsRequest(const int offset = 0, const int size = 0);
void FlushAlbumsRequests();
void AlbumsFinishCheck(const int offset = 0, const int albums_received = 0);
void SongsFinishCheck();
void AddAlbumSongsRequest(const int artist_id, const int album_id, const QString &album_artist, const int offset = 0);
QString AlbumCoverFileName(const Song &song);
void FlushAlbumSongsRequests();
int ParseSong(Song &song, const QJsonObject &json_obj, const int artist_id_requested = 0, const int album_id_requested = 0, const QString &album_artist = QString());
void GetAlbumCovers();
void AddAlbumCoverRequest(Song &song);
void FlushAlbumCoverRequests();
void AlbumCoverFinishCheck();
void FinishCheck();
void Warn(QString error, QVariant debug = QVariant());
QString Error(QString error, QVariant debug = QVariant());
static const int kMaxConcurrentAlbumsRequests;
static const int kMaxConcurrentArtistAlbumsRequests;
static const int kMaxConcurrentAlbumSongsRequests;
static const int kMaxConcurrentAlbumCoverRequests;
SubsonicService *service_;
SubsonicUrlHandler *url_handler_;
NetworkAccessManager *network_;
bool finished_;
QQueue<Request> albums_requests_queue_;
QQueue<Request> album_songs_requests_queue_;
QQueue<AlbumCoverRequest> album_cover_requests_queue_;
QHash<int, Request> album_songs_requests_pending_;
QMultiMap<int, Song*> album_covers_requests_sent_;
int albums_requests_active_;
int album_songs_requests_active_;
int album_songs_requested_;
int album_songs_received_;
int album_covers_requests_active_;
int album_covers_requested_;
int album_covers_received_;
SongList songs_;
QString errors_;
bool no_results_;
QList<QNetworkReply*> album_cover_replies_;
};
#endif // SUBSONICREQUEST_H

View File

@@ -0,0 +1,350 @@
/*
* Strawberry Music Player
* Copyright 2019, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#include "config.h"
#include <stdbool.h>
#include <memory>
#include <QObject>
#include <QStandardPaths>
#include <QByteArray>
#include <QPair>
#include <QList>
#include <QString>
#include <QUrl>
#include <QUrlQuery>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonParseError>
#include <QJsonDocument>
#include <QJsonObject>
#include <QSettings>
#include <QSortFilterProxyModel>
#include "core/application.h"
#include "core/player.h"
#include "core/closure.h"
#include "core/logging.h"
#include "core/network.h"
#include "core/database.h"
#include "core/song.h"
#include "internet/internetsearch.h"
#include "collection/collectionbackend.h"
#include "collection/collectionmodel.h"
#include "subsonicservice.h"
#include "subsonicurlhandler.h"
#include "subsonicrequest.h"
#include "settings/subsonicsettingspage.h"
using std::shared_ptr;
const Song::Source SubsonicService::kSource = Song::Source_Subsonic;
const char *SubsonicService::kClientName = "Strawberry";
const char *SubsonicService::kApiVersion = "1.16.1";
const char *SubsonicService::kSongsTable = "subsonic_songs";
const char *SubsonicService::kSongsFtsTable = "subsonic_songs_fts";
SubsonicService::SubsonicService(Application *app, QObject *parent)
: InternetService(Song::Source_Subsonic, "Subsonic", "subsonic", app, parent),
app_(app),
network_(new NetworkAccessManager(this)),
url_handler_(new SubsonicUrlHandler(app, this)),
collection_backend_(nullptr),
collection_model_(nullptr),
collection_sort_model_(new QSortFilterProxyModel(this)),
verify_certificate_(false),
cache_album_covers_(true)
{
app->player()->RegisterUrlHandler(url_handler_);
// Backend
collection_backend_ = new CollectionBackend();
collection_backend_->moveToThread(app_->database()->thread());
collection_backend_->Init(app_->database(), kSongsTable, QString(), QString(), kSongsFtsTable);
// Model
collection_model_ = new CollectionModel(collection_backend_, app_, this);
collection_sort_model_->setSourceModel(collection_model_);
collection_sort_model_->setSortRole(CollectionModel::Role_SortText);
collection_sort_model_->setDynamicSortFilter(true);
collection_sort_model_->setSortLocaleAware(true);
collection_sort_model_->sort(0);
ReloadSettings();
}
SubsonicService::~SubsonicService() {}
void SubsonicService::ShowConfig() {
app_->OpenSettingsDialogAtPage(SettingsDialog::Page_Subsonic);
}
void SubsonicService::ReloadSettings() {
QSettings s;
s.beginGroup(SubsonicSettingsPage::kSettingsGroup);
hostname_ = s.value("hostname").toString();
port_ = s.value("port", 443).toInt();
username_ = s.value("username").toString();
QByteArray password = s.value("password").toByteArray();
if (password.isEmpty()) password_.clear();
else password_ = QString::fromUtf8(QByteArray::fromBase64(password));
verify_certificate_ = s.value("verifycertificate", false).toBool();
cache_album_covers_ = s.value("cachealbumcovers", true).toBool();
s.endGroup();
}
QString SubsonicService::CoverCacheDir() {
return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/subsonicalbumcovers";
}
void SubsonicService::SendPing() {
SendPing(hostname_, port_, username_, password_);
}
void SubsonicService::SendPing(const QString &hostname, const int port, const QString &username, const QString &password) {
const ParamList params = ParamList() << Param("c", kClientName)
<< Param("v", kApiVersion)
<< Param("f", "json")
<< Param("u", username)
<< Param("p", QString("enc:" + password.toUtf8().toHex()));
QUrlQuery url_query;
for (const Param &param : params) {
EncodedParam encoded_param(QUrl::toPercentEncoding(param.first), QUrl::toPercentEncoding(param.second));
url_query.addQueryItem(encoded_param.first, encoded_param.second);
}
QUrl url;
url.setScheme("https");
url.setHost(hostname);
url.setPort(port);
url.setPath("/rest/ping.view");
url.setQuery(url_query);
QNetworkRequest req(url);
if (!verify_certificate_) {
QSslConfiguration sslconfig = QSslConfiguration::defaultConfiguration();
sslconfig.setPeerVerifyMode(QSslSocket::VerifyNone);
req.setSslConfiguration(sslconfig);
}
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QNetworkReply *reply = network_->get(req);
NewClosure(reply, SIGNAL(finished()), this, SLOT(HandlePingReply(QNetworkReply*)), reply);
//qLog(Debug) << "Subsonic: Sending request" << url << query;
}
void SubsonicService::HandlePingReply(QNetworkReply *reply) {
reply->deleteLater();
if (reply->error() != QNetworkReply::NoError) {
if (reply->error() < 200) {
// This is a network error, there is nothing more to do.
PingError(QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
return;
}
else {
// See if there is Json data containing "error" - then use that instead.
QByteArray data = reply->readAll();
QJsonParseError parse_error;
QJsonDocument json_doc = QJsonDocument::fromJson(data, &parse_error);
QString failure_reason;
if (parse_error.error == QJsonParseError::NoError && !json_doc.isNull() && !json_doc.isEmpty() && json_doc.isObject()) {
QJsonObject json_obj = json_doc.object();
if (!json_obj.isEmpty() && json_obj.contains("error")) {
QJsonValue json_error = json_obj["error"];
if (json_error.isObject()) {
json_obj = json_error.toObject();
if (!json_obj.isEmpty() && json_obj.contains("code") && json_obj.contains("message")) {
int code = json_obj["code"].toInt();
QString message = json_obj["message"].toString();
failure_reason = QString("%1 (%2)").arg(message).arg(code);
}
}
}
}
if (failure_reason.isEmpty()) {
failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
}
PingError(failure_reason);
return;
}
}
QByteArray data(reply->readAll());
QJsonParseError json_error;
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
if (json_error.error != QJsonParseError::NoError) {
PingError("Ping reply from server missing Json data.");
return;
}
if (json_doc.isNull() || json_doc.isEmpty()) {
PingError("Ping reply from server has empty Json document.");
return;
}
if (!json_doc.isObject()) {
PingError("Ping reply from server has Json document that is not an object.", json_doc);
return;
}
QJsonObject json_obj = json_doc.object();
if (json_obj.isEmpty()) {
PingError("Ping reply from server has empty Json object.", json_doc);
return;
}
if (!json_obj.contains("subsonic-response")) {
PingError("Ping reply from server is missing subsonic-response", json_obj);
return;
}
QJsonValue json_response = json_obj["subsonic-response"];
if (!json_response.isObject()) {
PingError("Ping reply from server subsonic-response is not an object", json_response);
return;
}
json_obj = json_response.toObject();
if (json_obj.contains("error")) {
QJsonValue json_error = json_obj["error"];
if (!json_error.isObject()) {
PingError("Authentication error reply from server is not an object", json_response);
return;
}
json_obj = json_error.toObject();
if (!json_obj.contains("code") || !json_obj.contains("message")) {
PingError("Authentication error reply from server is missing status or message", json_obj);
return;
}
//int status = json_obj["code"].toInt();
QString message = json_obj["message"].toString();
emit TestComplete(false, message);
emit TestFailure(message);
return;
}
if (!json_obj.contains("status")) {
PingError("Ping reply from server is missing status", json_obj);
return;
}
QString status = json_obj["status"].toString().toLower();
QString message = json_obj["message"].toString();
if (status == "failed") {
emit TestComplete(false, message);
emit TestFailure(message);
return;
}
else if (status == "ok") {
emit TestComplete(true);
emit TestSuccess();
return;
}
else {
PingError("Ping reply status from server is unknown", json_obj);
return;
}
}
void SubsonicService::CheckConfiguration() {
if (hostname_.isEmpty()) {
emit TestComplete(false, "Missing Subsonic hostname.");
return;
}
if (username_.isEmpty()) {
emit TestComplete(false, "Missing Subsonic username.");
return;
}
if (password_.isEmpty()) {
emit TestComplete(false, "Missing Subsonic password.");
return;
}
}
void SubsonicService::ResetSongsRequest() {
if (songs_request_.get()) {
disconnect(songs_request_.get(), 0, nullptr, 0);
disconnect(this, 0, songs_request_.get(), 0);
songs_request_.reset();
}
}
void SubsonicService::GetSongs() {
ResetSongsRequest();
songs_request_.reset(new SubsonicRequest(this, url_handler_, network_, this));
connect(songs_request_.get(), SIGNAL(ErrorSignal(QString)), SLOT(SongsErrorReceived(QString)));
connect(songs_request_.get(), SIGNAL(Results(SongList)), SLOT(SongsResultsReceived(SongList)));
connect(songs_request_.get(), SIGNAL(UpdateStatus(QString)), SIGNAL(SongsUpdateStatus(QString)));
connect(songs_request_.get(), SIGNAL(ProgressSetMaximum(int)), SIGNAL(SongsProgressSetMaximum(int)));
connect(songs_request_.get(), SIGNAL(UpdateProgress(int)), SIGNAL(SongsUpdateProgress(int)));
songs_request_->GetAlbums();
}
void SubsonicService::SongsResultsReceived(SongList songs) {
emit SongsResults(songs);
}
void SubsonicService::SongsErrorReceived(QString error) {
emit SongsError(error);
}
QString SubsonicService::PingError(QString error, QVariant debug) {
qLog(Error) << "Subsonic:" << error;
if (debug.isValid()) qLog(Debug) << debug;
emit TestFailure(error);
emit TestComplete(false, error);
return error;
}

View File

@@ -0,0 +1,130 @@
/*
* Strawberry Music Player
* Copyright 2019, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#ifndef SUBSONICSERVICE_H
#define SUBSONICSERVICE_H
#include "config.h"
#include <memory>
#include <stdbool.h>
#include <QtGlobal>
#include <QObject>
#include <QPair>
#include <QList>
#include <QString>
#include <QUrl>
#include <QNetworkReply>
#include <QTimer>
#include "core/song.h"
#include "internet/internetservice.h"
#include "internet/internetsearch.h"
#include "settings/subsonicsettingspage.h"
class QSortFilterProxyModel;
class Application;
class NetworkAccessManager;
class SubsonicUrlHandler;
class SubsonicRequest;
class CollectionBackend;
class CollectionModel;
using std::shared_ptr;
class SubsonicService : public InternetService {
Q_OBJECT
public:
SubsonicService(Application *app, QObject *parent);
~SubsonicService();
static const Song::Source kSource;
void ReloadSettings();
QString CoverCacheDir();
QString client_name() { return kClientName; }
QString api_version() { return kApiVersion; }
QString hostname() { return hostname_; }
int port() { return port_; }
QString username() { return username_; }
QString password() { return password_; }
bool verify_certificate() { return verify_certificate_; }
bool cache_album_covers() { return cache_album_covers_; }
CollectionBackend *collection_backend() { return collection_backend_; }
CollectionModel *collection_model() { return collection_model_; }
QSortFilterProxyModel *collection_sort_model() { return collection_sort_model_; }
CollectionBackend *songs_collection_backend() { return collection_backend_; }
CollectionModel *songs_collection_model() { return collection_model_; }
QSortFilterProxyModel *songs_collection_sort_model() { return collection_sort_model_; }
void CheckConfiguration();
signals:
public slots:
void ShowConfig();
void SendPing();
void SendPing(const QString &hostname, const int port, const QString &username, const QString &password);
void GetSongs();
void ResetSongsRequest();
private slots:
void HandlePingReply(QNetworkReply *reply);
void SongsResultsReceived(SongList songs);
void SongsErrorReceived(QString error);
private:
typedef QPair<QString, QString> Param;
typedef QList<Param> ParamList;
typedef QPair<QByteArray, QByteArray> EncodedParam;
typedef QList<EncodedParam> EncodedParamList;
QString PingError(QString error, QVariant debug = QVariant());
static const char *kClientName;
static const char *kApiVersion;
static const char *kSongsTable;
static const char *kSongsFtsTable;
Application *app_;
NetworkAccessManager *network_;
SubsonicUrlHandler *url_handler_;
CollectionBackend *collection_backend_;
CollectionModel *collection_model_;
QSortFilterProxyModel *collection_sort_model_;
std::shared_ptr<SubsonicRequest> songs_request_;
QString hostname_;
int port_;
QString username_;
QString password_;
bool verify_certificate_;
bool cache_album_covers_;
};
#endif // SUBSONICSERVICE_H

View File

@@ -0,0 +1,57 @@
/*
* Strawberry Music Player
* Copyright 2019, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#include <QObject>
#include <QString>
#include <QUrl>
#include <QUrlQuery>
#include "subsonicservice.h"
#include "subsonicurlhandler.h"
class Application;
SubsonicUrlHandler::SubsonicUrlHandler(Application *app, SubsonicService *service) : UrlHandler(service), service_(service) {}
UrlHandler::LoadResult SubsonicUrlHandler::StartLoading(const QUrl &url) {
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;
for (const Param& param : params) {
EncodedParam encoded_param(QUrl::toPercentEncoding(param.first), QUrl::toPercentEncoding(param.second));
url_query.addQueryItem(encoded_param.first, encoded_param.second);
}
QUrl media_url;
media_url.setScheme("https");
media_url.setHost(service_->hostname());
if (service_->port() > 0 && service_->port() != 443)
media_url.setPort(service_->port());
media_url.setPath("/rest/stream");
media_url.setQuery(url_query);
return LoadResult(url, LoadResult::TrackAvailable, media_url);
}

View File

@@ -0,0 +1,57 @@
/*
* Strawberry Music Player
* Copyright 2019, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#ifndef SUBSONICURLHANDLER_H
#define SUBSONICURLHANDLER_H
#include <QObject>
#include <QPair>
#include <QList>
#include <QByteArray>
#include <QString>
#include <QUrl>
#include "core/urlhandler.h"
#include "core/song.h"
#include "subsonic/subsonicservice.h"
class Application;
class SubsonicService;
class SubsonicUrlHandler : public UrlHandler {
Q_OBJECT
public:
SubsonicUrlHandler(Application *app, SubsonicService *service);
QString scheme() const { return service_->url_scheme(); }
LoadResult StartLoading(const QUrl &url);
private:
typedef QPair<QString, QString> Param;
typedef QList<Param> ParamList;
typedef QPair<QByteArray, QByteArray> EncodedParam;
typedef QList<EncodedParam> EncodedParamList;
SubsonicService *service_;
};
#endif // SUBSONICURLHANDLER_H