Refactor Tidal, Spotify, Qobuz, Subsonic and cover providers
Use common HTTP, Json and OAuthenticator class
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2025, 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
|
||||
@@ -19,20 +19,11 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QByteArray>
|
||||
#include <QPair>
|
||||
#include <QList>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
#include <QUrlQuery>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
#include <QSslError>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonValue>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include "includes/shared_ptr.h"
|
||||
#include "core/logging.h"
|
||||
@@ -43,150 +34,103 @@
|
||||
using namespace Qt::Literals::StringLiterals;
|
||||
|
||||
TidalBaseRequest::TidalBaseRequest(TidalService *service, const SharedPtr<NetworkAccessManager> network, QObject *parent)
|
||||
: QObject(parent),
|
||||
: JsonBaseRequest(network, parent),
|
||||
service_(service),
|
||||
network_(network) {}
|
||||
|
||||
QString TidalBaseRequest::service_name() const {
|
||||
|
||||
return service_->name();
|
||||
|
||||
}
|
||||
|
||||
bool TidalBaseRequest::authentication_required() const {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
bool TidalBaseRequest::authenticated() const {
|
||||
|
||||
return service_->authenticated();
|
||||
|
||||
}
|
||||
|
||||
bool TidalBaseRequest::use_authorization_header() const {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
QByteArray TidalBaseRequest::authorization_header() const {
|
||||
|
||||
return service_->authorization_header();
|
||||
|
||||
}
|
||||
|
||||
QNetworkReply *TidalBaseRequest::CreateRequest(const QString &ressource_name, const ParamList ¶ms_provided) {
|
||||
|
||||
const ParamList params = ParamList() << params_provided
|
||||
<< Param(u"countryCode"_s, country_code());
|
||||
|
||||
QUrlQuery url_query;
|
||||
for (const Param ¶m : params) {
|
||||
url_query.addQueryItem(QString::fromLatin1(QUrl::toPercentEncoding(param.first)), QString::fromLatin1(QUrl::toPercentEncoding(param.second)));
|
||||
}
|
||||
|
||||
QUrl url(QLatin1String(TidalService::kApiUrl) + QLatin1Char('/') + ressource_name);
|
||||
url.setQuery(url_query);
|
||||
QNetworkRequest req(url);
|
||||
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
req.setHeader(QNetworkRequest::ContentTypeHeader, u"application/x-www-form-urlencoded"_s);
|
||||
if (!token_type().isEmpty() && !access_token().isEmpty()) {
|
||||
req.setRawHeader("Authorization", token_type().toUtf8() + " " + access_token().toUtf8());
|
||||
}
|
||||
|
||||
QNetworkReply *reply = network_->get(req);
|
||||
QObject::connect(reply, &QNetworkReply::sslErrors, this, &TidalBaseRequest::HandleSSLErrors);
|
||||
|
||||
//qLog(Debug) << "Tidal: Sending request" << url;
|
||||
|
||||
return reply;
|
||||
<< Param(u"countryCode"_s, service_->country_code());
|
||||
return CreateGetRequest(QUrl(QLatin1String(TidalService::kApiUrl) + QLatin1Char('/') + ressource_name), params);
|
||||
|
||||
}
|
||||
|
||||
void TidalBaseRequest::HandleSSLErrors(const QList<QSslError> &ssl_errors) {
|
||||
JsonBaseRequest::JsonObjectResult TidalBaseRequest::ParseJsonObject(QNetworkReply *reply) {
|
||||
|
||||
for (const QSslError &ssl_error : ssl_errors) {
|
||||
Error(ssl_error.errorString());
|
||||
if (reply->error() != QNetworkReply::NoError && reply->error() < 200) {
|
||||
return ReplyDataResult(ErrorCode::NetworkError, QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
QByteArray TidalBaseRequest::GetReplyData(QNetworkReply *reply) {
|
||||
|
||||
QByteArray data;
|
||||
|
||||
if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200) {
|
||||
data = reply->readAll();
|
||||
JsonObjectResult result(ErrorCode::Success);
|
||||
result.network_error = reply->error();
|
||||
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).isValid()) {
|
||||
result.http_status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
}
|
||||
else {
|
||||
if (reply->error() != QNetworkReply::NoError && reply->error() < 200) {
|
||||
// This is a network error, there is nothing more to do.
|
||||
Error(QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
|
||||
|
||||
const QByteArray data = reply->readAll();
|
||||
bool clear_session = false;
|
||||
if (!data.isEmpty()) {
|
||||
QJsonParseError json_parse_error;
|
||||
const QJsonDocument json_document = QJsonDocument::fromJson(data, &json_parse_error);
|
||||
if (json_parse_error.error == QJsonParseError::NoError) {
|
||||
const QJsonObject json_object = json_document.object();
|
||||
if (json_object.contains("status"_L1) && json_object.contains("subStatus"_L1) && json_object.contains("userMessage"_L1)) {
|
||||
const int status = json_object["status"_L1].toInt();
|
||||
const int sub_status = json_object["subStatus"_L1].toInt();
|
||||
const QString user_message = json_object["userMessage"_L1].toString();
|
||||
result.error_code = ErrorCode::APIError;
|
||||
result.api_error = status;
|
||||
result.error_message = QStringLiteral("%1 (%2) (%3)").arg(user_message).arg(status).arg(sub_status);
|
||||
if (status == 401 && sub_status == 6001) {
|
||||
clear_session = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
result.json_object = json_document.object();
|
||||
}
|
||||
}
|
||||
else {
|
||||
// See if there is Json data containing "status" and "userMessage" - then use that instead.
|
||||
data = reply->readAll();
|
||||
QString error;
|
||||
QJsonParseError json_error;
|
||||
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
|
||||
int status = 0;
|
||||
int sub_status = 0;
|
||||
if (json_error.error == QJsonParseError::NoError && !json_doc.isEmpty() && json_doc.isObject()) {
|
||||
QJsonObject json_obj = json_doc.object();
|
||||
if (!json_obj.isEmpty() && json_obj.contains("status"_L1) && json_obj.contains("userMessage"_L1)) {
|
||||
status = json_obj["status"_L1].toInt();
|
||||
sub_status = json_obj["subStatus"_L1].toInt();
|
||||
QString user_message = json_obj["userMessage"_L1].toString();
|
||||
error = QStringLiteral("%1 (%2) (%3)").arg(user_message).arg(status).arg(sub_status);
|
||||
}
|
||||
}
|
||||
if (error.isEmpty()) {
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
error = QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error());
|
||||
}
|
||||
else {
|
||||
error = QStringLiteral("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt());
|
||||
}
|
||||
}
|
||||
if (status == 401 && sub_status == 6001) { // User does not have a valid session
|
||||
service_->Logout();
|
||||
}
|
||||
Error(error);
|
||||
result.error_code = ErrorCode::ParseError;
|
||||
result.error_message = json_parse_error.errorString();
|
||||
}
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
QJsonObject TidalBaseRequest::ExtractJsonObj(const QByteArray &data) {
|
||||
|
||||
QJsonParseError json_error;
|
||||
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
|
||||
|
||||
if (json_error.error != QJsonParseError::NoError) {
|
||||
Error(u"Reply from server missing Json data."_s, data);
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
if (json_doc.isEmpty()) {
|
||||
Error(u"Received empty Json document."_s, data);
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
if (!json_doc.isObject()) {
|
||||
Error(u"Json document is not an object."_s, json_doc);
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
QJsonObject json_obj = json_doc.object();
|
||||
if (json_obj.isEmpty()) {
|
||||
Error(u"Received empty Json object."_s, json_doc);
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
return json_obj;
|
||||
|
||||
}
|
||||
|
||||
QJsonValue TidalBaseRequest::ExtractItems(const QByteArray &data) {
|
||||
|
||||
QJsonObject json_obj = ExtractJsonObj(data);
|
||||
if (json_obj.isEmpty()) return QJsonValue();
|
||||
return ExtractItems(json_obj);
|
||||
|
||||
}
|
||||
|
||||
QJsonValue TidalBaseRequest::ExtractItems(const QJsonObject &json_obj) {
|
||||
|
||||
if (!json_obj.contains("items"_L1)) {
|
||||
Error(u"Json reply is missing items."_s, json_obj);
|
||||
return QJsonArray();
|
||||
}
|
||||
QJsonValue json_items = json_obj["items"_L1];
|
||||
return json_items;
|
||||
|
||||
}
|
||||
|
||||
QString TidalBaseRequest::ErrorsToHTML(const QStringList &errors) {
|
||||
|
||||
QString error_html;
|
||||
for (const QString &error : errors) {
|
||||
error_html += error + "<br />"_L1;
|
||||
}
|
||||
return error_html;
|
||||
if (result.error_code != ErrorCode::APIError) {
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
result.error_code = ErrorCode::NetworkError;
|
||||
result.error_message = QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error());
|
||||
}
|
||||
else if (result.http_status_code != 200) {
|
||||
result.error_code = ErrorCode::HttpError;
|
||||
result.error_message = QStringLiteral("Received HTTP code %1").arg(result.http_status_code);
|
||||
}
|
||||
}
|
||||
|
||||
if (reply->error() == QNetworkReply::AuthenticationRequiredError || clear_session) {
|
||||
service_->ClearSession();
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2025, 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
|
||||
@@ -22,27 +22,18 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QSet>
|
||||
#include <QList>
|
||||
#include <QPair>
|
||||
#include <QVariant>
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QUrl>
|
||||
#include <QSslError>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
|
||||
#include "includes/shared_ptr.h"
|
||||
#include "core/jsonbaserequest.h"
|
||||
#include "tidalservice.h"
|
||||
|
||||
class QNetworkReply;
|
||||
class NetworkAccessManager;
|
||||
class TidalService;
|
||||
|
||||
class TidalBaseRequest : public QObject {
|
||||
class TidalBaseRequest : public JsonBaseRequest {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
@@ -60,31 +51,14 @@ class TidalBaseRequest : public QObject {
|
||||
};
|
||||
|
||||
protected:
|
||||
using Param = QPair<QString, QString>;
|
||||
using ParamList = QList<Param>;
|
||||
QString service_name() const override;
|
||||
bool authentication_required() const override;
|
||||
bool authenticated() const override;
|
||||
bool use_authorization_header() const override;
|
||||
QByteArray authorization_header() const override;
|
||||
|
||||
QNetworkReply *CreateRequest(const QString &ressource_name, const ParamList ¶ms_provided);
|
||||
QByteArray GetReplyData(QNetworkReply *reply);
|
||||
QJsonObject ExtractJsonObj(const QByteArray &data);
|
||||
QJsonValue ExtractItems(const QByteArray &data);
|
||||
QJsonValue ExtractItems(const QJsonObject &json_obj);
|
||||
|
||||
virtual void Error(const QString &error, const QVariant &debug = QVariant()) = 0;
|
||||
static QString ErrorsToHTML(const QStringList &errors);
|
||||
|
||||
QString client_id() const { return service_->client_id(); }
|
||||
quint64 user_id() const { return service_->user_id(); }
|
||||
QString country_code() const { return service_->country_code(); }
|
||||
QString quality() const { return service_->quality(); }
|
||||
int artistssearchlimit() const { return service_->artistssearchlimit(); }
|
||||
int albumssearchlimit() const { return service_->albumssearchlimit(); }
|
||||
int songssearchlimit() const { return service_->songssearchlimit(); }
|
||||
QString token_type() const { return service_->token_type(); }
|
||||
QString access_token() const { return service_->access_token(); }
|
||||
bool authenticated() const { return service_->authenticated(); }
|
||||
|
||||
private Q_SLOTS:
|
||||
void HandleSSLErrors(const QList<QSslError> &ssl_errors);
|
||||
JsonObjectResult ParseJsonObject(QNetworkReply *reply);
|
||||
|
||||
private:
|
||||
TidalService *service_;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2025, 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
|
||||
@@ -19,9 +19,6 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QMap>
|
||||
#include <QMultiMap>
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
@@ -46,17 +43,6 @@ TidalFavoriteRequest::TidalFavoriteRequest(TidalService *service, const SharedPt
|
||||
service_(service),
|
||||
network_(network) {}
|
||||
|
||||
TidalFavoriteRequest::~TidalFavoriteRequest() {
|
||||
|
||||
while (!replies_.isEmpty()) {
|
||||
QNetworkReply *reply = replies_.takeFirst();
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
reply->abort();
|
||||
reply->deleteLater();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
QString TidalFavoriteRequest::FavoriteText(const FavoriteType type) {
|
||||
|
||||
switch (type) {
|
||||
@@ -135,7 +121,7 @@ void TidalFavoriteRequest::AddFavorites(const FavoriteType type, const SongList
|
||||
|
||||
void TidalFavoriteRequest::AddFavoritesRequest(const FavoriteType type, const QStringList &id_list, const SongList &songs) {
|
||||
|
||||
const ParamList params = ParamList() << Param(u"countryCode"_s, country_code())
|
||||
const ParamList params = ParamList() << Param(u"countryCode"_s, service_->country_code())
|
||||
<< Param(FavoriteMethod(type), id_list.join(u','));
|
||||
|
||||
QUrlQuery url_query;
|
||||
@@ -143,15 +129,15 @@ void TidalFavoriteRequest::AddFavoritesRequest(const FavoriteType type, const QS
|
||||
url_query.addQueryItem(QString::fromLatin1(QUrl::toPercentEncoding(param.first)), QString::fromLatin1(QUrl::toPercentEncoding(param.second)));
|
||||
}
|
||||
|
||||
QUrl url(QLatin1String(TidalService::kApiUrl) + QLatin1Char('/') + "users/"_L1 + QString::number(service_->user_id()) + "/favorites/"_L1 + FavoriteText(type));
|
||||
QNetworkRequest req(url);
|
||||
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
req.setHeader(QNetworkRequest::ContentTypeHeader, u"application/x-www-form-urlencoded"_s);
|
||||
if (!token_type().isEmpty() && !access_token().isEmpty()) {
|
||||
req.setRawHeader("Authorization", token_type().toUtf8() + " " + access_token().toUtf8());
|
||||
const QUrl url(QLatin1String(TidalService::kApiUrl) + QLatin1Char('/') + "users/"_L1 + QString::number(service_->user_id()) + "/favorites/"_L1 + FavoriteText(type));
|
||||
QNetworkRequest network_request(url);
|
||||
network_request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
network_request.setHeader(QNetworkRequest::ContentTypeHeader, u"application/x-www-form-urlencoded"_s);
|
||||
if (authenticated()) {
|
||||
network_request.setRawHeader("Authorization", authorization_header());
|
||||
}
|
||||
QByteArray query = url_query.toString(QUrl::FullyEncoded).toUtf8();
|
||||
QNetworkReply *reply = network_->post(req, query);
|
||||
const QByteArray query = url_query.toString(QUrl::FullyEncoded).toUtf8();
|
||||
QNetworkReply *reply = network_->post(network_request, query);
|
||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, type, songs]() { AddFavoritesReply(reply, type, songs); });
|
||||
replies_ << reply;
|
||||
|
||||
@@ -170,9 +156,9 @@ void TidalFavoriteRequest::AddFavoritesReply(QNetworkReply *reply, const Favorit
|
||||
return;
|
||||
}
|
||||
|
||||
GetReplyData(reply);
|
||||
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
const JsonObjectResult json_object_result = ParseJsonObject(reply);
|
||||
if (!json_object_result.success()) {
|
||||
Error(json_object_result.error_message);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -246,7 +232,7 @@ void TidalFavoriteRequest::RemoveFavorites(const FavoriteType type, const SongLi
|
||||
|
||||
void TidalFavoriteRequest::RemoveFavoritesRequest(const FavoriteType type, const QString &id, const SongList &songs) {
|
||||
|
||||
const ParamList params = ParamList() << Param(u"countryCode"_s, country_code());
|
||||
const ParamList params = ParamList() << Param(u"countryCode"_s, service_->country_code());
|
||||
|
||||
QUrlQuery url_query;
|
||||
for (const Param ¶m : params) {
|
||||
@@ -255,13 +241,13 @@ void TidalFavoriteRequest::RemoveFavoritesRequest(const FavoriteType type, const
|
||||
|
||||
QUrl url(QLatin1String(TidalService::kApiUrl) + "/users/"_L1 + QString::number(service_->user_id()) + "/favorites/"_L1 + FavoriteText(type) + "/"_L1 + id);
|
||||
url.setQuery(url_query);
|
||||
QNetworkRequest req(url);
|
||||
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
req.setHeader(QNetworkRequest::ContentTypeHeader, u"application/x-www-form-urlencoded"_s);
|
||||
if (!token_type().isEmpty() && !access_token().isEmpty()) {
|
||||
req.setRawHeader("Authorization", token_type().toUtf8() + " " + access_token().toUtf8());
|
||||
QNetworkRequest network_request(url);
|
||||
network_request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
network_request.setHeader(QNetworkRequest::ContentTypeHeader, u"application/x-www-form-urlencoded"_s);
|
||||
if (authenticated()) {
|
||||
network_request.setRawHeader("Authorization", authorization_header());
|
||||
}
|
||||
QNetworkReply *reply = network_->deleteResource(req);
|
||||
QNetworkReply *reply = network_->deleteResource(network_request);
|
||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, type, songs]() { RemoveFavoritesReply(reply, type, songs); });
|
||||
replies_ << reply;
|
||||
|
||||
@@ -280,8 +266,9 @@ void TidalFavoriteRequest::RemoveFavoritesReply(QNetworkReply *reply, const Favo
|
||||
return;
|
||||
}
|
||||
|
||||
GetReplyData(reply);
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
const JsonObjectResult json_object_result = ParseJsonObject(reply);
|
||||
if (!json_object_result.success()) {
|
||||
Error(json_object_result.error_message);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2025, 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
|
||||
@@ -41,7 +41,6 @@ class TidalFavoriteRequest : public TidalBaseRequest {
|
||||
|
||||
public:
|
||||
explicit TidalFavoriteRequest(TidalService *service, const SharedPtr<NetworkAccessManager> network, QObject *parent = nullptr);
|
||||
~TidalFavoriteRequest() override;
|
||||
|
||||
private:
|
||||
enum class FavoriteType {
|
||||
@@ -85,7 +84,6 @@ class TidalFavoriteRequest : public TidalBaseRequest {
|
||||
|
||||
TidalService *service_;
|
||||
const SharedPtr<NetworkAccessManager> network_;
|
||||
QList <QNetworkReply*> replies_;
|
||||
};
|
||||
|
||||
#endif // TIDALFAVORITEREQUEST_H
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2025, 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
|
||||
@@ -21,7 +21,6 @@
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include <QObject>
|
||||
#include <QList>
|
||||
#include <QByteArray>
|
||||
#include <QByteArrayList>
|
||||
@@ -35,6 +34,7 @@
|
||||
#include <QJsonArray>
|
||||
#include <QJsonValue>
|
||||
#include <QTimer>
|
||||
#include <QScopeGuard>
|
||||
|
||||
#include "includes/shared_ptr.h"
|
||||
#include "core/logging.h"
|
||||
@@ -107,24 +107,6 @@ TidalRequest::TidalRequest(TidalService *service, TidalUrlHandler *url_handler,
|
||||
|
||||
}
|
||||
|
||||
TidalRequest::~TidalRequest() {
|
||||
|
||||
while (!replies_.isEmpty()) {
|
||||
QNetworkReply *reply = replies_.takeFirst();
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
if (reply->isRunning()) reply->abort();
|
||||
reply->deleteLater();
|
||||
}
|
||||
|
||||
while (!album_cover_replies_.isEmpty()) {
|
||||
QNetworkReply *reply = album_cover_replies_.takeFirst();
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
if (reply->isRunning()) reply->abort();
|
||||
reply->deleteLater();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void TidalRequest::Process() {
|
||||
|
||||
switch (query_type_) {
|
||||
@@ -227,7 +209,7 @@ void TidalRequest::FlushArtistsRequests() {
|
||||
|
||||
while (!artists_requests_queue_.isEmpty() && artists_requests_active_ < kMaxConcurrentArtistsRequests) {
|
||||
|
||||
Request request = artists_requests_queue_.dequeue();
|
||||
const Request request = artists_requests_queue_.dequeue();
|
||||
|
||||
ParamList parameters;
|
||||
if (query_type_ == Type::SearchArtists) parameters << Param(u"query"_s, search_text_);
|
||||
@@ -241,7 +223,6 @@ void TidalRequest::FlushArtistsRequests() {
|
||||
reply = CreateRequest(u"search/artists"_s, parameters);
|
||||
}
|
||||
if (!reply) continue;
|
||||
replies_ << reply;
|
||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, request]() { ArtistsReplyReceived(reply, request.limit, request.offset); });
|
||||
|
||||
++artists_requests_active_;
|
||||
@@ -275,7 +256,7 @@ void TidalRequest::FlushAlbumsRequests() {
|
||||
|
||||
while (!albums_requests_queue_.isEmpty() && albums_requests_active_ < kMaxConcurrentAlbumsRequests) {
|
||||
|
||||
Request request = albums_requests_queue_.dequeue();
|
||||
const Request request = albums_requests_queue_.dequeue();
|
||||
|
||||
ParamList parameters;
|
||||
if (query_type_ == Type::SearchAlbums) parameters << Param(u"query"_s, search_text_);
|
||||
@@ -289,7 +270,6 @@ void TidalRequest::FlushAlbumsRequests() {
|
||||
reply = CreateRequest(u"search/albums"_s, parameters);
|
||||
}
|
||||
if (!reply) continue;
|
||||
replies_ << reply;
|
||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, request]() { AlbumsReplyReceived(reply, request.limit, request.offset); });
|
||||
|
||||
++albums_requests_active_;
|
||||
@@ -323,7 +303,7 @@ void TidalRequest::FlushSongsRequests() {
|
||||
|
||||
while (!songs_requests_queue_.isEmpty() && songs_requests_active_ < kMaxConcurrentSongsRequests) {
|
||||
|
||||
Request request = songs_requests_queue_.dequeue();
|
||||
const Request request = songs_requests_queue_.dequeue();
|
||||
|
||||
ParamList parameters;
|
||||
if (query_type_ == Type::SearchSongs) parameters << Param(u"query"_s, search_text_);
|
||||
@@ -337,7 +317,6 @@ void TidalRequest::FlushSongsRequests() {
|
||||
reply = CreateRequest(u"search/tracks"_s, parameters);
|
||||
}
|
||||
if (!reply) continue;
|
||||
replies_ << reply;
|
||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, request]() { SongsReplyReceived(reply, request.limit, request.offset); });
|
||||
|
||||
++songs_requests_active_;
|
||||
@@ -395,48 +374,44 @@ void TidalRequest::ArtistsReplyReceived(QNetworkReply *reply, const int limit_re
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
reply->deleteLater();
|
||||
|
||||
QByteArray data = GetReplyData(reply);
|
||||
const JsonObjectResult json_object_result = ParseJsonObject(reply);
|
||||
|
||||
--artists_requests_active_;
|
||||
++artists_requests_received_;
|
||||
|
||||
if (finished_) return;
|
||||
|
||||
if (data.isEmpty()) {
|
||||
ArtistsFinishCheck();
|
||||
return;
|
||||
}
|
||||
int offset = 0;
|
||||
int artists_received = 0;
|
||||
const QScopeGuard finish_check = qScopeGuard([this, limit_requested, &offset, &artists_received]() { ArtistsFinishCheck(limit_requested, offset, artists_received); });
|
||||
|
||||
QJsonObject json_obj = ExtractJsonObj(data);
|
||||
if (json_obj.isEmpty()) {
|
||||
ArtistsFinishCheck();
|
||||
if (!json_object_result.success()) {
|
||||
Error(json_object_result.error_message);
|
||||
return;
|
||||
}
|
||||
const QJsonObject &json_object = json_object_result.json_object;
|
||||
|
||||
if (!json_obj.contains("limit"_L1) ||
|
||||
!json_obj.contains("offset"_L1) ||
|
||||
!json_obj.contains("totalNumberOfItems"_L1) ||
|
||||
!json_obj.contains("items"_L1)) {
|
||||
Error(u"Json object missing values."_s, json_obj);
|
||||
ArtistsFinishCheck();
|
||||
if (!json_object.contains("limit"_L1) ||
|
||||
!json_object.contains("offset"_L1) ||
|
||||
!json_object.contains("totalNumberOfItems"_L1) ||
|
||||
!json_object.contains("items"_L1)) {
|
||||
Error(u"Json object missing values."_s, json_object);
|
||||
return;
|
||||
}
|
||||
//int limit = json_obj["limit"].toInt();
|
||||
int offset = json_obj["offset"_L1].toInt();
|
||||
int artists_total = json_obj["totalNumberOfItems"_L1].toInt();
|
||||
//int limit = json_object["limit"].toInt();
|
||||
offset = json_object["offset"_L1].toInt();
|
||||
const int artists_total = json_object["totalNumberOfItems"_L1].toInt();
|
||||
|
||||
if (offset_requested == 0) {
|
||||
artists_total_ = artists_total;
|
||||
}
|
||||
else if (artists_total != artists_total_) {
|
||||
Error(QStringLiteral("totalNumberOfItems returned does not match previous totalNumberOfItems! %1 != %2").arg(artists_total).arg(artists_total_));
|
||||
ArtistsFinishCheck();
|
||||
return;
|
||||
}
|
||||
|
||||
if (offset != offset_requested) {
|
||||
Error(QStringLiteral("Offset returned does not match offset requested! %1 != %2").arg(offset).arg(offset_requested));
|
||||
ArtistsFinishCheck();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -444,19 +419,17 @@ void TidalRequest::ArtistsReplyReceived(QNetworkReply *reply, const int limit_re
|
||||
Q_EMIT UpdateProgress(query_id_, GetProgress(artists_received_, artists_total_));
|
||||
}
|
||||
|
||||
QJsonValue value_items = ExtractItems(json_obj);
|
||||
if (!value_items.isArray()) {
|
||||
ArtistsFinishCheck();
|
||||
const JsonArrayResult json_array_result = GetJsonArray(json_object, u"items"_s);
|
||||
if (!json_array_result.success()) {
|
||||
Error(json_array_result.error_message);
|
||||
return;
|
||||
}
|
||||
|
||||
const QJsonArray array_items = value_items.toArray();
|
||||
const QJsonArray &array_items = json_array_result.json_array;
|
||||
if (array_items.isEmpty()) { // Empty array means no results
|
||||
ArtistsFinishCheck();
|
||||
return;
|
||||
}
|
||||
|
||||
int artists_received = 0;
|
||||
for (const QJsonValue &value_item : array_items) {
|
||||
|
||||
++artists_received;
|
||||
@@ -465,30 +438,30 @@ void TidalRequest::ArtistsReplyReceived(QNetworkReply *reply, const int limit_re
|
||||
Error(u"Invalid Json reply, item in array is not a object."_s);
|
||||
continue;
|
||||
}
|
||||
QJsonObject obj_item = value_item.toObject();
|
||||
QJsonObject object_item = value_item.toObject();
|
||||
|
||||
if (obj_item.contains("item"_L1)) {
|
||||
QJsonValue json_item = obj_item["item"_L1];
|
||||
if (object_item.contains("item"_L1)) {
|
||||
const QJsonValue json_item = object_item["item"_L1];
|
||||
if (!json_item.isObject()) {
|
||||
Error(u"Invalid Json reply, item in array is not a object."_s, json_item);
|
||||
continue;
|
||||
}
|
||||
obj_item = json_item.toObject();
|
||||
object_item = json_item.toObject();
|
||||
}
|
||||
|
||||
if (!obj_item.contains("id"_L1) || !obj_item.contains("name"_L1)) {
|
||||
Error(u"Invalid Json reply, item missing id or album."_s, obj_item);
|
||||
if (!object_item.contains("id"_L1) || !object_item.contains("name"_L1)) {
|
||||
Error(u"Invalid Json reply, item missing id or album."_s, object_item);
|
||||
continue;
|
||||
}
|
||||
|
||||
Artist artist;
|
||||
if (obj_item["id"_L1].isString()) {
|
||||
artist.artist_id = obj_item["id"_L1].toString();
|
||||
if (object_item["id"_L1].isString()) {
|
||||
artist.artist_id = object_item["id"_L1].toString();
|
||||
}
|
||||
else {
|
||||
artist.artist_id = QString::number(obj_item["id"_L1].toInt());
|
||||
artist.artist_id = QString::number(object_item["id"_L1].toInt());
|
||||
}
|
||||
artist.artist = obj_item["name"_L1].toString();
|
||||
artist.artist = object_item["name"_L1].toString();
|
||||
|
||||
if (artist_albums_requests_pending_.contains(artist.artist_id)) continue;
|
||||
|
||||
@@ -501,8 +474,6 @@ void TidalRequest::ArtistsReplyReceived(QNetworkReply *reply, const int limit_re
|
||||
|
||||
if (offset_requested != 0) Q_EMIT UpdateProgress(query_id_, GetProgress(artists_received_, artists_total_));
|
||||
|
||||
ArtistsFinishCheck(limit_requested, offset, artists_received);
|
||||
|
||||
}
|
||||
|
||||
void TidalRequest::ArtistsFinishCheck(const int limit, const int offset, const int artists_received) {
|
||||
@@ -569,7 +540,6 @@ void TidalRequest::FlushArtistAlbumsRequests() {
|
||||
if (request.offset > 0) parameters << Param(u"offset"_s, QString::number(request.offset));
|
||||
QNetworkReply *reply = CreateRequest(QStringLiteral("artists/%1/albums").arg(request.artist.artist_id), parameters);
|
||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, request]() { ArtistAlbumsReplyReceived(reply, request.artist, request.offset); });
|
||||
replies_ << reply;
|
||||
|
||||
++artist_albums_requests_active_;
|
||||
|
||||
@@ -593,52 +563,54 @@ void TidalRequest::AlbumsReceived(QNetworkReply *reply, const Artist &artist_req
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
reply->deleteLater();
|
||||
|
||||
QByteArray data = GetReplyData(reply);
|
||||
const JsonObjectResult json_object_result = ParseJsonObject(reply);
|
||||
|
||||
if (finished_) return;
|
||||
|
||||
if (data.isEmpty()) {
|
||||
AlbumsFinishCheck(artist_requested);
|
||||
int offset = 0;
|
||||
int albums_total = 0;
|
||||
int albums_received = 0;
|
||||
const QScopeGuard finish_check = qScopeGuard([this, artist_requested, limit_requested, &offset, &albums_total, &albums_received]() { AlbumsFinishCheck(artist_requested, limit_requested, offset, albums_total, albums_received); });
|
||||
|
||||
if (!json_object_result.success()) {
|
||||
Error(json_object_result.error_message);
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject json_obj = ExtractJsonObj(data);
|
||||
if (json_obj.isEmpty()) {
|
||||
AlbumsFinishCheck(artist_requested);
|
||||
const QJsonObject &json_object = json_object_result.json_object;
|
||||
if (json_object.isEmpty()) {
|
||||
Error(json_object_result.error_message);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!json_obj.contains("limit"_L1) ||
|
||||
!json_obj.contains("offset"_L1) ||
|
||||
!json_obj.contains("totalNumberOfItems"_L1) ||
|
||||
!json_obj.contains("items"_L1)) {
|
||||
Error(u"Json object missing values."_s, json_obj);
|
||||
AlbumsFinishCheck(artist_requested);
|
||||
if (!json_object.contains("limit"_L1) ||
|
||||
!json_object.contains("offset"_L1) ||
|
||||
!json_object.contains("totalNumberOfItems"_L1) ||
|
||||
!json_object.contains("items"_L1)) {
|
||||
Error(u"Json object missing values."_s, json_object);
|
||||
return;
|
||||
}
|
||||
|
||||
//int limit = json_obj["limit"].toInt();
|
||||
int offset = json_obj["offset"_L1].toInt();
|
||||
int albums_total = json_obj["totalNumberOfItems"_L1].toInt();
|
||||
offset = json_object["offset"_L1].toInt();
|
||||
albums_total = json_object["totalNumberOfItems"_L1].toInt();
|
||||
|
||||
if (offset != offset_requested) {
|
||||
Error(QStringLiteral("Offset returned does not match offset requested! %1 != %2").arg(offset).arg(offset_requested));
|
||||
AlbumsFinishCheck(artist_requested);
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonValue value_items = ExtractItems(json_obj);
|
||||
if (!value_items.isArray()) {
|
||||
AlbumsFinishCheck(artist_requested);
|
||||
const JsonArrayResult json_array_result = GetJsonArray(json_object, u"items"_s);
|
||||
if (!json_array_result.success()) {
|
||||
Error(json_array_result.error_message);
|
||||
return;
|
||||
}
|
||||
const QJsonArray array_items = value_items.toArray();
|
||||
|
||||
const QJsonArray &array_items = json_array_result.json_array;
|
||||
if (array_items.isEmpty()) {
|
||||
AlbumsFinishCheck(artist_requested);
|
||||
return;
|
||||
}
|
||||
|
||||
int albums_received = 0;
|
||||
for (const QJsonValue &value_item : array_items) {
|
||||
|
||||
++albums_received;
|
||||
@@ -647,79 +619,79 @@ void TidalRequest::AlbumsReceived(QNetworkReply *reply, const Artist &artist_req
|
||||
Error(u"Invalid Json reply, item in array is not a object."_s);
|
||||
continue;
|
||||
}
|
||||
QJsonObject obj_item = value_item.toObject();
|
||||
QJsonObject object_item = value_item.toObject();
|
||||
|
||||
if (obj_item.contains("item"_L1)) {
|
||||
QJsonValue json_item = obj_item["item"_L1];
|
||||
if (object_item.contains("item"_L1)) {
|
||||
const QJsonValue json_item = object_item["item"_L1];
|
||||
if (!json_item.isObject()) {
|
||||
Error(u"Invalid Json reply, item in array is not a object."_s, json_item);
|
||||
continue;
|
||||
}
|
||||
obj_item = json_item.toObject();
|
||||
object_item = json_item.toObject();
|
||||
}
|
||||
|
||||
Album album;
|
||||
if (obj_item.contains("type"_L1)) { // This was an albums request or search
|
||||
if (!obj_item.contains("id"_L1) || !obj_item.contains("title"_L1)) {
|
||||
Error(u"Invalid Json reply, item is missing ID or title."_s, obj_item);
|
||||
if (object_item.contains("type"_L1)) { // This was an albums request or search
|
||||
if (!object_item.contains("id"_L1) || !object_item.contains("title"_L1)) {
|
||||
Error(u"Invalid Json reply, item is missing ID or title."_s, object_item);
|
||||
continue;
|
||||
}
|
||||
if (obj_item["id"_L1].isString()) {
|
||||
album.album_id = obj_item["id"_L1].toString();
|
||||
if (object_item["id"_L1].isString()) {
|
||||
album.album_id = object_item["id"_L1].toString();
|
||||
}
|
||||
else {
|
||||
album.album_id = QString::number(obj_item["id"_L1].toInt());
|
||||
album.album_id = QString::number(object_item["id"_L1].toInt());
|
||||
}
|
||||
album.album = obj_item["title"_L1].toString();
|
||||
if (service_->album_explicit() && obj_item.contains("explicit"_L1)) {
|
||||
album.album_explicit = obj_item["explicit"_L1].toVariant().toBool();
|
||||
album.album = object_item["title"_L1].toString();
|
||||
if (service_->album_explicit() && object_item.contains("explicit"_L1)) {
|
||||
album.album_explicit = object_item["explicit"_L1].toVariant().toBool();
|
||||
if (album.album_explicit && !album.album.isEmpty()) {
|
||||
album.album.append(" (Explicit)"_L1);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (obj_item.contains("album"_L1)) { // This was a tracks request or search
|
||||
QJsonValue value_album = obj_item["album"_L1];
|
||||
else if (object_item.contains("album"_L1)) { // This was a tracks request or search
|
||||
const QJsonValue value_album = object_item["album"_L1];
|
||||
if (!value_album.isObject()) {
|
||||
Error(u"Invalid Json reply, item album is not a object."_s, value_album);
|
||||
continue;
|
||||
}
|
||||
QJsonObject obj_album = value_album.toObject();
|
||||
if (!obj_album.contains("id"_L1) || !obj_album.contains("title"_L1)) {
|
||||
Error(u"Invalid Json reply, item album is missing ID or title."_s, obj_album);
|
||||
const QJsonObject object_album = value_album.toObject();
|
||||
if (!object_album.contains("id"_L1) || !object_album.contains("title"_L1)) {
|
||||
Error(u"Invalid Json reply, item album is missing ID or title."_s, object_album);
|
||||
continue;
|
||||
}
|
||||
if (obj_album["id"_L1].isString()) {
|
||||
album.album_id = obj_album["id"_L1].toString();
|
||||
if (object_album["id"_L1].isString()) {
|
||||
album.album_id = object_album["id"_L1].toString();
|
||||
}
|
||||
else {
|
||||
album.album_id = QString::number(obj_album["id"_L1].toInt());
|
||||
album.album_id = QString::number(object_album["id"_L1].toInt());
|
||||
}
|
||||
album.album = obj_album["title"_L1].toString();
|
||||
if (service_->album_explicit() && obj_album.contains("explicit"_L1)) {
|
||||
album.album_explicit = obj_album["explicit"_L1].toVariant().toBool();
|
||||
album.album = object_album["title"_L1].toString();
|
||||
if (service_->album_explicit() && object_album.contains("explicit"_L1)) {
|
||||
album.album_explicit = object_album["explicit"_L1].toVariant().toBool();
|
||||
if (album.album_explicit && !album.album.isEmpty()) {
|
||||
album.album.append(" (Explicit)"_L1);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
Error(u"Invalid Json reply, item missing type or album."_s, obj_item);
|
||||
Error(u"Invalid Json reply, item missing type or album."_s, object_item);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (album_songs_requests_pending_.contains(album.album_id)) continue;
|
||||
|
||||
if (!obj_item.contains("artist"_L1) || !obj_item.contains("title"_L1) || !obj_item.contains("audioQuality"_L1)) {
|
||||
Error(u"Invalid Json reply, item missing artist, title or audioQuality."_s, obj_item);
|
||||
if (!object_item.contains("artist"_L1) || !object_item.contains("title"_L1) || !object_item.contains("audioQuality"_L1)) {
|
||||
Error(u"Invalid Json reply, item missing artist, title or audioQuality."_s, object_item);
|
||||
continue;
|
||||
}
|
||||
QJsonValue value_artist = obj_item["artist"_L1];
|
||||
const QJsonValue value_artist = object_item["artist"_L1];
|
||||
if (!value_artist.isObject()) {
|
||||
Error(u"Invalid Json reply, item artist is not a object."_s, value_artist);
|
||||
continue;
|
||||
}
|
||||
QJsonObject obj_artist = value_artist.toObject();
|
||||
const QJsonObject obj_artist = value_artist.toObject();
|
||||
if (!obj_artist.contains("id"_L1) || !obj_artist.contains("name"_L1)) {
|
||||
Error(u"Invalid Json reply, item artist missing id or name."_s, obj_artist);
|
||||
continue;
|
||||
@@ -751,8 +723,6 @@ void TidalRequest::AlbumsReceived(QNetworkReply *reply, const Artist &artist_req
|
||||
Q_EMIT UpdateProgress(query_id_, GetProgress(albums_received_, albums_total_));
|
||||
}
|
||||
|
||||
AlbumsFinishCheck(artist_requested, limit_requested, offset, albums_total, albums_received);
|
||||
|
||||
}
|
||||
|
||||
void TidalRequest::AlbumsFinishCheck(const Artist &artist, const int limit, const int offset, const int albums_total, const int albums_received) {
|
||||
@@ -843,7 +813,6 @@ void TidalRequest::FlushAlbumSongsRequests() {
|
||||
ParamList parameters;
|
||||
if (request.offset > 0) parameters << Param(u"offset"_s, QString::number(request.offset));
|
||||
QNetworkReply *reply = CreateRequest(QStringLiteral("albums/%1/tracks").arg(request.album.album_id), parameters);
|
||||
replies_ << reply;
|
||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, request]() { AlbumSongsReplyReceived(reply, request.artist, request.album, request.offset); });
|
||||
|
||||
++album_songs_requests_active_;
|
||||
@@ -870,76 +839,75 @@ void TidalRequest::SongsReceived(QNetworkReply *reply, const Artist &artist, con
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
reply->deleteLater();
|
||||
|
||||
QByteArray data = GetReplyData(reply);
|
||||
const JsonObjectResult json_object_result = ParseJsonObject(reply);
|
||||
|
||||
if (finished_) return;
|
||||
|
||||
if (data.isEmpty()) {
|
||||
SongsFinishCheck(artist, album, limit_requested, offset_requested);
|
||||
int songs_total = 0;
|
||||
int songs_received = 0;
|
||||
const QScopeGuard finish_check = qScopeGuard([this, artist, album, limit_requested, offset_requested, &songs_total, &songs_received]() { SongsFinishCheck(artist, album, limit_requested, offset_requested, songs_total, songs_received); });
|
||||
|
||||
if (!json_object_result.success()) {
|
||||
Error(json_object_result.error_message);
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject json_obj = ExtractJsonObj(data);
|
||||
if (json_obj.isEmpty()) {
|
||||
SongsFinishCheck(artist, album, limit_requested, offset_requested);
|
||||
const QJsonObject &json_object = json_object_result.json_object;
|
||||
if (json_object.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!json_obj.contains("limit"_L1) ||
|
||||
!json_obj.contains("offset"_L1) ||
|
||||
!json_obj.contains("totalNumberOfItems"_L1) ||
|
||||
!json_obj.contains("items"_L1)) {
|
||||
Error(u"Json object missing values."_s, json_obj);
|
||||
SongsFinishCheck(artist, album, limit_requested, offset_requested);
|
||||
if (!json_object.contains("limit"_L1) ||
|
||||
!json_object.contains("offset"_L1) ||
|
||||
!json_object.contains("totalNumberOfItems"_L1) ||
|
||||
!json_object.contains("items"_L1)) {
|
||||
Error(u"Json object missing values."_s, json_object);
|
||||
return;
|
||||
}
|
||||
|
||||
//int limit = json_obj["limit"].toInt();
|
||||
int offset = json_obj["offset"_L1].toInt();
|
||||
int songs_total = json_obj["totalNumberOfItems"_L1].toInt();
|
||||
const int offset = json_object["offset"_L1].toInt();
|
||||
songs_total = json_object["totalNumberOfItems"_L1].toInt();
|
||||
|
||||
if (offset != offset_requested) {
|
||||
Error(QStringLiteral("Offset returned does not match offset requested! %1 != %2").arg(offset).arg(offset_requested));
|
||||
SongsFinishCheck(artist, album, limit_requested, offset_requested, songs_total, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonValue json_value = ExtractItems(json_obj);
|
||||
if (!json_value.isArray()) {
|
||||
SongsFinishCheck(artist, album, limit_requested, offset_requested, songs_total, 0);
|
||||
const JsonArrayResult json_array_result = GetJsonArray(json_object, u"items"_s);
|
||||
if (!json_array_result.success()) {
|
||||
Error(json_array_result.error_message);
|
||||
return;
|
||||
}
|
||||
|
||||
const QJsonArray array_items = json_value.toArray();
|
||||
const QJsonArray &array_items = json_array_result.json_array;
|
||||
if (array_items.isEmpty()) {
|
||||
SongsFinishCheck(artist, album, limit_requested, offset_requested, songs_total, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
bool compilation = false;
|
||||
bool multidisc = false;
|
||||
SongList songs;
|
||||
int songs_received = 0;
|
||||
for (const QJsonValue &value_item : array_items) {
|
||||
|
||||
if (!value_item.isObject()) {
|
||||
Error(u"Invalid Json reply, track is not a object."_s);
|
||||
continue;
|
||||
}
|
||||
QJsonObject obj_item = value_item.toObject();
|
||||
QJsonObject object_item = value_item.toObject();
|
||||
|
||||
if (obj_item.contains("item"_L1)) {
|
||||
QJsonValue item = obj_item["item"_L1];
|
||||
if (object_item.contains("item"_L1)) {
|
||||
const QJsonValue item = object_item["item"_L1];
|
||||
if (!item.isObject()) {
|
||||
Error(u"Invalid Json reply, item is not a object."_s, item);
|
||||
continue;
|
||||
}
|
||||
obj_item = item.toObject();
|
||||
object_item = item.toObject();
|
||||
}
|
||||
|
||||
++songs_received;
|
||||
Song song(Song::Source::Tidal);
|
||||
ParseSong(song, obj_item, artist, album);
|
||||
ParseSong(song, object_item, artist, album);
|
||||
if (!song.is_valid()) continue;
|
||||
if (song.disc() >= 2) multidisc = true;
|
||||
if (song.is_compilation()) compilation = true;
|
||||
@@ -957,8 +925,6 @@ void TidalRequest::SongsReceived(QNetworkReply *reply, const Artist &artist, con
|
||||
Q_EMIT UpdateProgress(query_id_, GetProgress(songs_received_, songs_total_));
|
||||
}
|
||||
|
||||
SongsFinishCheck(artist, album, limit_requested, offset_requested, songs_total, songs_received);
|
||||
|
||||
}
|
||||
|
||||
void TidalRequest::SongsFinishCheck(const Artist &artist, const Album &album, const int limit, const int offset, const int songs_total, const int songs_received) {
|
||||
@@ -1017,10 +983,10 @@ void TidalRequest::ParseSong(Song &song, const QJsonObject &json_obj, const Arti
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonValue value_artist = json_obj["artist"_L1];
|
||||
QJsonValue value_album = json_obj["album"_L1];
|
||||
QJsonValue json_duration = json_obj["duration"_L1];
|
||||
//QJsonArray array_artists = json_obj["artists"].toArray();
|
||||
const QJsonValue value_artist = json_obj["artist"_L1];
|
||||
const QJsonValue value_album = json_obj["album"_L1];
|
||||
const QJsonValue json_duration = json_obj["duration"_L1];
|
||||
//const QJsonArray array_artists = json_obj["artists"].toArray();
|
||||
|
||||
QString song_id;
|
||||
if (json_obj["id"_L1].isString()) {
|
||||
@@ -1031,52 +997,52 @@ void TidalRequest::ParseSong(Song &song, const QJsonObject &json_obj, const Arti
|
||||
}
|
||||
|
||||
QString title = json_obj["title"_L1].toString();
|
||||
//QString urlstr = json_obj["url"].toString();
|
||||
int track = json_obj["trackNumber"_L1].toInt();
|
||||
int disc = json_obj["volumeNumber"_L1].toInt();
|
||||
bool allow_streaming = json_obj["allowStreaming"_L1].toBool();
|
||||
bool stream_ready = json_obj["streamReady"_L1].toBool();
|
||||
QString copyright = json_obj["copyright"_L1].toString();
|
||||
//const QString urlstr = json_obj["url"].toString();
|
||||
const int track = json_obj["trackNumber"_L1].toInt();
|
||||
const int disc = json_obj["volumeNumber"_L1].toInt();
|
||||
const bool allow_streaming = json_obj["allowStreaming"_L1].toBool();
|
||||
const bool stream_ready = json_obj["streamReady"_L1].toBool();
|
||||
const QString copyright = json_obj["copyright"_L1].toString();
|
||||
|
||||
if (!value_artist.isObject()) {
|
||||
Error(u"Invalid Json reply, track artist is not a object."_s, value_artist);
|
||||
return;
|
||||
}
|
||||
QJsonObject obj_artist = value_artist.toObject();
|
||||
if (!obj_artist.contains("id"_L1) || !obj_artist.contains("name"_L1)) {
|
||||
Error(u"Invalid Json reply, track artist is missing id or name."_s, obj_artist);
|
||||
const QJsonObject object_artist = value_artist.toObject();
|
||||
if (!object_artist.contains("id"_L1) || !object_artist.contains("name"_L1)) {
|
||||
Error(u"Invalid Json reply, track artist is missing id or name."_s, object_artist);
|
||||
return;
|
||||
}
|
||||
QString artist_id;
|
||||
if (obj_artist["id"_L1].isString()) {
|
||||
artist_id = obj_artist["id"_L1].toString();
|
||||
if (object_artist["id"_L1].isString()) {
|
||||
artist_id = object_artist["id"_L1].toString();
|
||||
}
|
||||
else {
|
||||
artist_id = QString::number(obj_artist["id"_L1].toInt());
|
||||
artist_id = QString::number(object_artist["id"_L1].toInt());
|
||||
}
|
||||
QString artist = obj_artist["name"_L1].toString();
|
||||
QString artist = object_artist["name"_L1].toString();
|
||||
|
||||
if (!value_album.isObject()) {
|
||||
Error(u"Invalid Json reply, track album is not a object."_s, value_album);
|
||||
return;
|
||||
}
|
||||
QJsonObject obj_album = value_album.toObject();
|
||||
if (!obj_album.contains("id"_L1) || !obj_album.contains("title"_L1)) {
|
||||
Error(u"Invalid Json reply, track album is missing ID or title."_s, obj_album);
|
||||
const QJsonObject object_album = value_album.toObject();
|
||||
if (!object_album.contains("id"_L1) || !object_album.contains("title"_L1)) {
|
||||
Error(u"Invalid Json reply, track album is missing ID or title."_s, object_album);
|
||||
return;
|
||||
}
|
||||
QString album_id;
|
||||
if (obj_album["id"_L1].isString()) {
|
||||
album_id = obj_album["id"_L1].toString();
|
||||
if (object_album["id"_L1].isString()) {
|
||||
album_id = object_album["id"_L1].toString();
|
||||
}
|
||||
else {
|
||||
album_id = QString::number(obj_album["id"_L1].toInt());
|
||||
album_id = QString::number(object_album["id"_L1].toInt());
|
||||
}
|
||||
if (!album.album_id.isEmpty() && album.album_id != album_id) {
|
||||
Error(u"Invalid Json reply, track album id is wrong."_s, obj_album);
|
||||
Error(u"Invalid Json reply, track album id is wrong."_s, object_album);
|
||||
return;
|
||||
}
|
||||
QString album_title = obj_album["title"_L1].toString();
|
||||
QString album_title = object_album["title"_L1].toString();
|
||||
if (album.album_explicit) album_title.append(" (Explicit)"_L1);
|
||||
|
||||
if (!allow_streaming) {
|
||||
@@ -1104,8 +1070,8 @@ void TidalRequest::ParseSong(Song &song, const QJsonObject &json_obj, const Arti
|
||||
}
|
||||
|
||||
QUrl cover_url;
|
||||
if (obj_album.contains("cover"_L1)) {
|
||||
const QString cover = obj_album["cover"_L1].toString().replace(u'-', u'/');
|
||||
if (object_album.contains("cover"_L1)) {
|
||||
const QString cover = object_album["cover"_L1].toString().replace(u'-', u'/');
|
||||
if (!cover.isEmpty()) {
|
||||
cover_url.setUrl(QStringLiteral("%1/images/%2/%3.jpg").arg(QLatin1String(kResourcesUrl), cover, coversize_));
|
||||
}
|
||||
@@ -1207,11 +1173,7 @@ void TidalRequest::FlushAlbumCoverRequests() {
|
||||
while (!album_cover_requests_queue_.isEmpty() && album_covers_requests_active_ < kMaxConcurrentAlbumCoverRequests) {
|
||||
|
||||
AlbumCoverRequest request = album_cover_requests_queue_.dequeue();
|
||||
|
||||
QNetworkRequest req(request.url);
|
||||
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
QNetworkReply *reply = network_->get(req);
|
||||
album_cover_replies_ << reply;
|
||||
QNetworkReply *reply = CreateGetRequest(request.url);
|
||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, request]() { AlbumCoverReceived(reply, request.album_id, request.url, request.filename); });
|
||||
|
||||
++album_covers_requests_active_;
|
||||
@@ -1222,8 +1184,8 @@ void TidalRequest::FlushAlbumCoverRequests() {
|
||||
|
||||
void TidalRequest::AlbumCoverReceived(QNetworkReply *reply, const QString &album_id, const QUrl &url, const QString &filename) {
|
||||
|
||||
if (album_cover_replies_.contains(reply)) {
|
||||
album_cover_replies_.removeAll(reply);
|
||||
if (replies_.contains(reply)) {
|
||||
replies_.removeAll(reply);
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
reply->deleteLater();
|
||||
}
|
||||
@@ -1237,24 +1199,23 @@ void TidalRequest::AlbumCoverReceived(QNetworkReply *reply, const QString &album
|
||||
|
||||
if (finished_) return;
|
||||
|
||||
const QScopeGuard finish_check = qScopeGuard([this]() { AlbumCoverFinishCheck(); });
|
||||
|
||||
Q_EMIT UpdateProgress(query_id_, GetProgress(album_covers_requests_received_, album_covers_requests_total_));
|
||||
|
||||
if (!album_covers_requests_sent_.contains(album_id)) {
|
||||
AlbumCoverFinishCheck();
|
||||
return;
|
||||
}
|
||||
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
Error(QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
|
||||
album_covers_requests_sent_.remove(album_id);
|
||||
AlbumCoverFinishCheck();
|
||||
return;
|
||||
}
|
||||
|
||||
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
|
||||
Error(QStringLiteral("Received HTTP code %1 for %2.").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()).arg(url.toString()));
|
||||
if (album_covers_requests_sent_.contains(album_id)) album_covers_requests_sent_.remove(album_id);
|
||||
AlbumCoverFinishCheck();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1265,7 +1226,6 @@ void TidalRequest::AlbumCoverReceived(QNetworkReply *reply, const QString &album
|
||||
if (!ImageUtils::SupportedImageMimeTypes().contains(mimetype, Qt::CaseInsensitive) && !ImageUtils::SupportedImageFormats().contains(mimetype, Qt::CaseInsensitive)) {
|
||||
Error(QStringLiteral("Unsupported mimetype for image reader %1 for %2").arg(mimetype, url.toString()));
|
||||
if (album_covers_requests_sent_.contains(album_id)) album_covers_requests_sent_.remove(album_id);
|
||||
AlbumCoverFinishCheck();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1273,7 +1233,6 @@ void TidalRequest::AlbumCoverReceived(QNetworkReply *reply, const QString &album
|
||||
if (data.isEmpty()) {
|
||||
Error(QStringLiteral("Received empty image data for %1").arg(url.toString()));
|
||||
if (album_covers_requests_sent_.contains(album_id)) album_covers_requests_sent_.remove(album_id);
|
||||
AlbumCoverFinishCheck();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1303,8 +1262,6 @@ void TidalRequest::AlbumCoverReceived(QNetworkReply *reply, const QString &album
|
||||
Error(QStringLiteral("Error decoding image data from %1").arg(url.toString()));
|
||||
}
|
||||
|
||||
AlbumCoverFinishCheck();
|
||||
|
||||
}
|
||||
|
||||
void TidalRequest::AlbumCoverFinishCheck() {
|
||||
@@ -1332,13 +1289,13 @@ void TidalRequest::FinishCheck() {
|
||||
artist_albums_requests_active_ <= 0 &&
|
||||
album_songs_requests_active_ <= 0 &&
|
||||
album_covers_requests_active_ <= 0
|
||||
) {
|
||||
) {
|
||||
if (timer_flush_requests_->isActive()) {
|
||||
timer_flush_requests_->stop();
|
||||
}
|
||||
finished_ = true;
|
||||
if (songs_.isEmpty()) {
|
||||
if (errors_.isEmpty()) {
|
||||
if (error_.isEmpty()) {
|
||||
if (IsSearch()) {
|
||||
Q_EMIT Results(query_id_, SongMap(), tr("No match."));
|
||||
}
|
||||
@@ -1347,7 +1304,7 @@ void TidalRequest::FinishCheck() {
|
||||
}
|
||||
}
|
||||
else {
|
||||
Q_EMIT Results(query_id_, SongMap(), ErrorsToHTML(errors_));
|
||||
Q_EMIT Results(query_id_, SongMap(), error_);
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -1363,22 +1320,20 @@ int TidalRequest::GetProgress(const int count, const int total) {
|
||||
|
||||
}
|
||||
|
||||
void TidalRequest::Error(const QString &error, const QVariant &debug) {
|
||||
void TidalRequest::Error(const QString &error_message, const QVariant &debug_output) {
|
||||
|
||||
if (!error.isEmpty()) {
|
||||
errors_ << error;
|
||||
qLog(Error) << "Tidal:" << error;
|
||||
qLog(Error) << "Tidal:" << error_message;
|
||||
if (debug_output.isValid()) {
|
||||
qLog(Debug) << debug_output;
|
||||
}
|
||||
|
||||
if (debug.isValid()) qLog(Debug) << debug;
|
||||
|
||||
FinishCheck();
|
||||
error_ = error_message;
|
||||
|
||||
}
|
||||
|
||||
void TidalRequest::Warn(const QString &error, const QVariant &debug) {
|
||||
void TidalRequest::Warn(const QString &error_message, const QVariant &debug) {
|
||||
|
||||
qLog(Error) << "Tidal:" << error;
|
||||
qLog(Warning) << "Tidal:" << error_message;
|
||||
if (debug.isValid()) qLog(Debug) << debug;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2025, 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
|
||||
@@ -22,20 +22,15 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QPair>
|
||||
#include <QSet>
|
||||
#include <QList>
|
||||
#include <QHash>
|
||||
#include <QMap>
|
||||
#include <QMultiMap>
|
||||
#include <QQueue>
|
||||
#include <QVariant>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QUrl>
|
||||
#include <QJsonObject>
|
||||
#include <QScopedPointer>
|
||||
|
||||
#include "includes/shared_ptr.h"
|
||||
#include "core/song.h"
|
||||
@@ -53,7 +48,6 @@ class TidalRequest : public TidalBaseRequest {
|
||||
|
||||
public:
|
||||
explicit TidalRequest(TidalService *service, TidalUrlHandler *url_handler, const SharedPtr<NetworkAccessManager> network, const Type query_type, QObject *parent);
|
||||
~TidalRequest() override;
|
||||
|
||||
void ReloadSettings();
|
||||
|
||||
@@ -164,8 +158,8 @@ class TidalRequest : public TidalBaseRequest {
|
||||
int GetProgress(const int count, const int total);
|
||||
|
||||
void FinishCheck();
|
||||
static void Warn(const QString &error, const QVariant &debug = QVariant());
|
||||
void Error(const QString &error, const QVariant &debug = QVariant()) override;
|
||||
static void Warn(const QString &error_message, const QVariant &debug_output = QVariant());
|
||||
void Error(const QString &error_message, const QVariant &debug_output = QVariant()) override;
|
||||
|
||||
TidalService *service_;
|
||||
TidalUrlHandler *url_handler_;
|
||||
@@ -180,6 +174,7 @@ class TidalRequest : public TidalBaseRequest {
|
||||
QString search_text_;
|
||||
|
||||
bool finished_;
|
||||
QString error_;
|
||||
|
||||
QQueue<Request> artists_requests_queue_;
|
||||
QQueue<Request> albums_requests_queue_;
|
||||
@@ -228,9 +223,8 @@ class TidalRequest : public TidalBaseRequest {
|
||||
int album_covers_requests_received_;
|
||||
|
||||
SongMap songs_;
|
||||
QStringList errors_;
|
||||
QList<QNetworkReply*> replies_;
|
||||
QList<QNetworkReply*> album_cover_replies_;
|
||||
};
|
||||
|
||||
using TidalRequestPtr = QScopedPointer<TidalRequest, QScopedPointerDeleteLater>;
|
||||
|
||||
#endif // TIDALREQUEST_H
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2025, 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
|
||||
@@ -19,41 +19,22 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <utility>
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
|
||||
#include <QObject>
|
||||
#include <QDesktopServices>
|
||||
#include <QCryptographicHash>
|
||||
#include <QByteArray>
|
||||
#include <QList>
|
||||
#include <QString>
|
||||
#include <QChar>
|
||||
#include <QUrl>
|
||||
#include <QUrlQuery>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
#include <QSslError>
|
||||
#include <QTimer>
|
||||
#include <QJsonValue>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QSettings>
|
||||
#include <QSortFilterProxyModel>
|
||||
|
||||
#include "includes/shared_ptr.h"
|
||||
#include "core/logging.h"
|
||||
#include "core/networkaccessmanager.h"
|
||||
#include "core/settings.h"
|
||||
#include "core/database.h"
|
||||
#include "core/song.h"
|
||||
#include "core/settings.h"
|
||||
#include "core/taskmanager.h"
|
||||
#include "core/database.h"
|
||||
#include "core/networkaccessmanager.h"
|
||||
#include "core/urlhandlers.h"
|
||||
#include "utilities/randutils.h"
|
||||
#include "constants/timeconstants.h"
|
||||
#include "core/oauthenticator.h"
|
||||
#include "constants/tidalsettings.h"
|
||||
#include "streaming/streamingsearchview.h"
|
||||
#include "collection/collectionbackend.h"
|
||||
@@ -65,11 +46,11 @@
|
||||
#include "tidalrequest.h"
|
||||
#include "tidalfavoriterequest.h"
|
||||
#include "tidalstreamurlrequest.h"
|
||||
#include "settings/tidalsettingspage.h"
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
using namespace Qt::Literals::StringLiterals;
|
||||
using std::make_shared;
|
||||
using namespace TidalSettings;
|
||||
|
||||
const Song::Source TidalService::kSource = Song::Source::Tidal;
|
||||
|
||||
@@ -81,20 +62,12 @@ namespace {
|
||||
constexpr char kOAuthUrl[] = "https://login.tidal.com/authorize";
|
||||
constexpr char kOAuthAccessTokenUrl[] = "https://login.tidal.com/oauth2/token";
|
||||
constexpr char kOAuthRedirectUrl[] = "tidal://login/auth";
|
||||
constexpr char kOAuthScope[] = "r_usr w_usr";
|
||||
|
||||
constexpr char kArtistsSongsTable[] = "tidal_artists_songs";
|
||||
constexpr char kAlbumsSongsTable[] = "tidal_albums_songs";
|
||||
constexpr char kSongsTable[] = "tidal_songs";
|
||||
|
||||
constexpr char kUserId[] = "user_id";
|
||||
constexpr char kCountryCode[] = "country_code";
|
||||
constexpr char kTokenType[] = "token_type";
|
||||
constexpr char kAccessToken[] = "access_token";
|
||||
constexpr char kRefreshToken[] = "refresh_token";
|
||||
constexpr char kSessionId[] = "session_id";
|
||||
constexpr char kExpiresIn[] = "expires_in";
|
||||
constexpr char kLoginTime[] = "login_time";
|
||||
|
||||
} // namespace
|
||||
|
||||
TidalService::TidalService(const SharedPtr<TaskManager> task_manager,
|
||||
@@ -106,6 +79,7 @@ TidalService::TidalService(const SharedPtr<TaskManager> task_manager,
|
||||
: StreamingService(Song::Source::Tidal, u"Tidal"_s, u"tidal"_s, QLatin1String(TidalSettings::kSettingsGroup), parent),
|
||||
network_(network),
|
||||
url_handler_(new TidalUrlHandler(task_manager, this)),
|
||||
oauth_(new OAuthenticator(network, this)),
|
||||
artists_collection_backend_(nullptr),
|
||||
albums_collection_backend_(nullptr),
|
||||
songs_collection_backend_(nullptr),
|
||||
@@ -113,10 +87,8 @@ TidalService::TidalService(const SharedPtr<TaskManager> task_manager,
|
||||
albums_collection_model_(nullptr),
|
||||
songs_collection_model_(nullptr),
|
||||
timer_search_delay_(new QTimer(this)),
|
||||
timer_refresh_login_(new QTimer(this)),
|
||||
favorite_request_(new TidalFavoriteRequest(this, network_, this)),
|
||||
enabled_(false),
|
||||
user_id_(0),
|
||||
artistssearchlimit_(1),
|
||||
albumssearchlimit_(1),
|
||||
songssearchlimit_(1),
|
||||
@@ -124,8 +96,6 @@ TidalService::TidalService(const SharedPtr<TaskManager> task_manager,
|
||||
download_album_covers_(true),
|
||||
stream_url_method_(TidalSettings::StreamUrlMethod::StreamUrl),
|
||||
album_explicit_(false),
|
||||
expires_in_(0),
|
||||
login_time_(0),
|
||||
pending_search_id_(0),
|
||||
next_pending_search_id_(1),
|
||||
pending_search_type_(SearchType::Artists),
|
||||
@@ -134,6 +104,16 @@ TidalService::TidalService(const SharedPtr<TaskManager> task_manager,
|
||||
|
||||
url_handlers->Register(url_handler_);
|
||||
|
||||
oauth_->set_settings_group(QLatin1String(kSettingsGroup));
|
||||
oauth_->set_type(OAuthenticator::Type::Authorization_Code);
|
||||
oauth_->set_authorize_url(QUrl(QLatin1String(kOAuthUrl)));
|
||||
oauth_->set_redirect_url(QUrl(QLatin1String(kOAuthRedirectUrl)));
|
||||
oauth_->set_access_token_url(QUrl(QLatin1String(kOAuthAccessTokenUrl)));
|
||||
oauth_->set_scope(QLatin1String(kOAuthScope));
|
||||
oauth_->set_use_local_redirect_server(false);
|
||||
oauth_->set_random_port(false);
|
||||
QObject::connect(oauth_, &OAuthenticator::AuthenticationFinished, this, &TidalService::OAuthFinished);
|
||||
|
||||
// Backends
|
||||
|
||||
artists_collection_backend_ = make_shared<CollectionBackend>();
|
||||
@@ -158,9 +138,6 @@ TidalService::TidalService(const SharedPtr<TaskManager> task_manager,
|
||||
timer_search_delay_->setSingleShot(true);
|
||||
QObject::connect(timer_search_delay_, &QTimer::timeout, this, &TidalService::StartSearch);
|
||||
|
||||
timer_refresh_login_->setSingleShot(true);
|
||||
QObject::connect(timer_refresh_login_, &QTimer::timeout, this, &TidalService::RequestNewAccessToken);
|
||||
|
||||
QObject::connect(this, &TidalService::AddArtists, favorite_request_, &TidalFavoriteRequest::AddArtists);
|
||||
QObject::connect(this, &TidalService::AddAlbums, favorite_request_, &TidalFavoriteRequest::AddAlbums);
|
||||
QObject::connect(this, &TidalService::AddSongs, favorite_request_, QOverload<const SongList&>::of(&TidalFavoriteRequest::AddSongs));
|
||||
@@ -179,23 +156,40 @@ TidalService::TidalService(const SharedPtr<TaskManager> task_manager,
|
||||
QObject::connect(favorite_request_, &TidalFavoriteRequest::SongsRemoved, &*songs_collection_backend_, &CollectionBackend::DeleteSongs);
|
||||
|
||||
TidalService::ReloadSettings();
|
||||
LoadSession();
|
||||
oauth_->LoadSession();
|
||||
|
||||
}
|
||||
|
||||
TidalService::~TidalService() {
|
||||
|
||||
while (!replies_.isEmpty()) {
|
||||
QNetworkReply *reply = replies_.takeFirst();
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
reply->abort();
|
||||
reply->deleteLater();
|
||||
while (!stream_url_requests_.isEmpty()) {
|
||||
TidalStreamURLRequestPtr stream_url_request = stream_url_requests_.take(stream_url_requests_.firstKey());
|
||||
QObject::disconnect(&*stream_url_request, nullptr, this, nullptr);
|
||||
}
|
||||
|
||||
while (!stream_url_requests_.isEmpty()) {
|
||||
SharedPtr<TidalStreamURLRequest> stream_url_req = stream_url_requests_.take(stream_url_requests_.firstKey());
|
||||
QObject::disconnect(&*stream_url_req, nullptr, this, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
bool TidalService::authenticated() const {
|
||||
|
||||
return oauth_->authenticated();
|
||||
|
||||
}
|
||||
|
||||
QByteArray TidalService::authorization_header() const {
|
||||
|
||||
return oauth_->authorization_header();
|
||||
|
||||
}
|
||||
|
||||
QString TidalService::country_code() const {
|
||||
|
||||
return oauth_->country_code();
|
||||
|
||||
}
|
||||
|
||||
quint64 TidalService::user_id() const {
|
||||
|
||||
return oauth_->user_id();
|
||||
|
||||
}
|
||||
|
||||
@@ -223,41 +217,10 @@ void TidalService::ExitReceived() {
|
||||
|
||||
}
|
||||
|
||||
void TidalService::LoadSession() {
|
||||
|
||||
Settings s;
|
||||
s.beginGroup(TidalSettings::kSettingsGroup);
|
||||
user_id_ = s.value(kUserId).toInt();
|
||||
country_code_ = s.value(kCountryCode, u"US"_s).toString();
|
||||
token_type_ = s.value(kTokenType).toString();
|
||||
access_token_ = s.value(kAccessToken).toString();
|
||||
refresh_token_ = s.value(kRefreshToken).toString();
|
||||
expires_in_ = s.value(kExpiresIn).toLongLong();
|
||||
login_time_ = s.value(kLoginTime).toLongLong();
|
||||
s.endGroup();
|
||||
|
||||
if (token_type_.isEmpty()) {
|
||||
token_type_ = "Bearer"_L1;
|
||||
}
|
||||
|
||||
if (!refresh_token_.isEmpty()) {
|
||||
qint64 time = static_cast<qint64>(expires_in_) - (QDateTime::currentSecsSinceEpoch() - static_cast<qint64>(login_time_));
|
||||
if (time <= 0) {
|
||||
timer_refresh_login_->setInterval(200ms);
|
||||
}
|
||||
else {
|
||||
timer_refresh_login_->setInterval(static_cast<int>(time * kMsecPerSec));
|
||||
}
|
||||
timer_refresh_login_->start();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void TidalService::ReloadSettings() {
|
||||
|
||||
Settings s;
|
||||
s.beginGroup(TidalSettings::kSettingsGroup);
|
||||
|
||||
enabled_ = s.value(TidalSettings::kEnabled, false).toBool();
|
||||
client_id_ = s.value(TidalSettings::kClientId).toString();
|
||||
quality_ = s.value(TidalSettings::kQuality, u"LOSSLESS"_s).toString();
|
||||
@@ -270,272 +233,45 @@ void TidalService::ReloadSettings() {
|
||||
download_album_covers_ = s.value(TidalSettings::kDownloadAlbumCovers, true).toBool();
|
||||
stream_url_method_ = static_cast<TidalSettings::StreamUrlMethod>(s.value(TidalSettings::kStreamUrl, static_cast<int>(TidalSettings::StreamUrlMethod::StreamUrl)).toInt());
|
||||
album_explicit_ = s.value(TidalSettings::kAlbumExplicit).toBool();
|
||||
|
||||
s.endGroup();
|
||||
|
||||
oauth_->set_client_id(client_id_);
|
||||
timer_search_delay_->setInterval(static_cast<int>(search_delay));
|
||||
|
||||
}
|
||||
|
||||
void TidalService::StartAuthorization(const QString &client_id) {
|
||||
|
||||
client_id_ = client_id;
|
||||
code_verifier_ = Utilities::CryptographicRandomString(44);
|
||||
code_challenge_ = QString::fromLatin1(QCryptographicHash::hash(code_verifier_.toUtf8(), QCryptographicHash::Sha256).toBase64(QByteArray::Base64UrlEncoding));
|
||||
oauth_->set_client_id(client_id);
|
||||
oauth_->Authenticate();
|
||||
|
||||
if (code_challenge_.lastIndexOf(u'=') == code_challenge_.length() - 1) {
|
||||
code_challenge_.chop(1);
|
||||
}
|
||||
|
||||
void TidalService::OAuthFinished(const bool success, const QString &error) {
|
||||
|
||||
if (success) {
|
||||
qLog(Debug) << "Tidal: Login successful" << "user id" << user_id();
|
||||
Q_EMIT LoginFinished(true);
|
||||
Q_EMIT LoginSuccess();
|
||||
}
|
||||
|
||||
const ParamList params = ParamList() << Param(u"response_type"_s, u"code"_s)
|
||||
<< Param(u"code_challenge"_s, code_challenge_)
|
||||
<< Param(u"code_challenge_method"_s, u"S256"_s)
|
||||
<< Param(u"redirect_uri"_s, QLatin1String(kOAuthRedirectUrl))
|
||||
<< Param(u"client_id"_s, client_id_)
|
||||
<< Param(u"scope"_s, u"r_usr w_usr"_s);
|
||||
|
||||
QUrlQuery url_query;
|
||||
for (const Param ¶m : params) {
|
||||
url_query.addQueryItem(QString::fromLatin1(QUrl::toPercentEncoding(param.first)), QString::fromLatin1(QUrl::toPercentEncoding(param.second)));
|
||||
else {
|
||||
Q_EMIT LoginFailure(error);
|
||||
Q_EMIT LoginFinished(false);
|
||||
}
|
||||
|
||||
QUrl url = QUrl(QString::fromLatin1(kOAuthUrl));
|
||||
url.setQuery(url_query);
|
||||
QDesktopServices::openUrl(url);
|
||||
|
||||
}
|
||||
|
||||
void TidalService::AuthorizationUrlReceived(const QUrl &url) {
|
||||
|
||||
qLog(Debug) << "Tidal: Authorization URL Received" << url;
|
||||
|
||||
QUrlQuery url_query(url);
|
||||
|
||||
if (url_query.hasQueryItem(u"token_type"_s) && url_query.hasQueryItem(u"expires_in"_s) && url_query.hasQueryItem(u"access_token"_s)) {
|
||||
|
||||
access_token_ = url_query.queryItemValue(u"access_token"_s);
|
||||
if (url_query.hasQueryItem(u"refresh_token"_s)) {
|
||||
refresh_token_ = url_query.queryItemValue(u"refresh_token"_s);
|
||||
}
|
||||
expires_in_ = url_query.queryItemValue(u"expires_in"_s).toInt();
|
||||
login_time_ = QDateTime::currentSecsSinceEpoch();
|
||||
|
||||
Settings s;
|
||||
s.beginGroup(TidalSettings::kSettingsGroup);
|
||||
s.setValue(kTokenType, token_type_);
|
||||
s.setValue(kAccessToken, access_token_);
|
||||
s.setValue(kRefreshToken, refresh_token_);
|
||||
s.setValue(kExpiresIn, expires_in_);
|
||||
s.setValue(kLoginTime, login_time_);
|
||||
s.remove(kSessionId);
|
||||
s.endGroup();
|
||||
|
||||
Q_EMIT LoginComplete(true);
|
||||
Q_EMIT LoginSuccess();
|
||||
}
|
||||
|
||||
else if (url_query.hasQueryItem(u"code"_s) && url_query.hasQueryItem(u"state"_s)) {
|
||||
|
||||
QString code = url_query.queryItemValue(u"code"_s);
|
||||
|
||||
RequestAccessToken(code);
|
||||
|
||||
}
|
||||
|
||||
else {
|
||||
LoginError(tr("Reply from Tidal is missing query items."));
|
||||
return;
|
||||
}
|
||||
oauth_->ExternalAuthorizationUrlReceived(url);
|
||||
|
||||
}
|
||||
|
||||
void TidalService::RequestAccessToken(const QString &code) {
|
||||
void TidalService::ClearSession() {
|
||||
|
||||
timer_refresh_login_->stop();
|
||||
|
||||
ParamList params = ParamList() << Param(u"client_id"_s, client_id_);
|
||||
|
||||
if (!code.isEmpty()) {
|
||||
params << Param(u"grant_type"_s, u"authorization_code"_s);
|
||||
params << Param(u"code"_s, code);
|
||||
params << Param(u"code_verifier"_s, code_verifier_);
|
||||
params << Param(u"redirect_uri"_s, QLatin1String(kOAuthRedirectUrl));
|
||||
params << Param(u"scope"_s, u"r_usr w_usr"_s);
|
||||
}
|
||||
else if (!refresh_token_.isEmpty() && enabled_) {
|
||||
params << Param(u"grant_type"_s, u"refresh_token"_s);
|
||||
params << Param(u"refresh_token"_s, refresh_token_);
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
|
||||
QUrlQuery url_query;
|
||||
for (const Param ¶m : std::as_const(params)) {
|
||||
url_query.addQueryItem(QString::fromLatin1(QUrl::toPercentEncoding(param.first)), QString::fromLatin1(QUrl::toPercentEncoding(param.second)));
|
||||
}
|
||||
|
||||
QUrl url(QString::fromLatin1(kOAuthAccessTokenUrl));
|
||||
QNetworkRequest req(url);
|
||||
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
const QByteArray query = url_query.toString(QUrl::FullyEncoded).toUtf8();
|
||||
|
||||
login_errors_.clear();
|
||||
QNetworkReply *reply = network_->post(req, query);
|
||||
replies_ << reply;
|
||||
QObject::connect(reply, &QNetworkReply::sslErrors, this, &TidalService::HandleLoginSSLErrors);
|
||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply]() { AccessTokenRequestFinished(reply); });
|
||||
|
||||
}
|
||||
|
||||
void TidalService::HandleLoginSSLErrors(const QList<QSslError> &ssl_errors) {
|
||||
|
||||
for (const QSslError &ssl_error : ssl_errors) {
|
||||
login_errors_ += ssl_error.errorString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void TidalService::AccessTokenRequestFinished(QNetworkReply *reply) {
|
||||
|
||||
if (!replies_.contains(reply)) return;
|
||||
replies_.removeAll(reply);
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
reply->deleteLater();
|
||||
|
||||
if (reply->error() != QNetworkReply::NoError || reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
|
||||
if (reply->error() != QNetworkReply::NoError && reply->error() < 200) {
|
||||
// This is a network error, there is nothing more to do.
|
||||
LoginError(QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
|
||||
return;
|
||||
}
|
||||
else {
|
||||
// See if there is Json data containing "status" and "userMessage" then use that instead.
|
||||
QByteArray data(reply->readAll());
|
||||
QJsonParseError json_error;
|
||||
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
|
||||
if (json_error.error == QJsonParseError::NoError && !json_doc.isEmpty() && json_doc.isObject()) {
|
||||
QJsonObject json_obj = json_doc.object();
|
||||
if (!json_obj.isEmpty() && json_obj.contains("status"_L1) && json_obj.contains("userMessage"_L1)) {
|
||||
int status = json_obj["status"_L1].toInt();
|
||||
int sub_status = json_obj["subStatus"_L1].toInt();
|
||||
QString user_message = json_obj["userMessage"_L1].toString();
|
||||
login_errors_ << QStringLiteral("Authentication failure: %1 (%2) (%3)").arg(user_message).arg(status).arg(sub_status);
|
||||
}
|
||||
}
|
||||
if (login_errors_.isEmpty()) {
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
login_errors_ << QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error());
|
||||
}
|
||||
else {
|
||||
login_errors_ << QStringLiteral("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt());
|
||||
}
|
||||
}
|
||||
LoginError();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const QByteArray data = reply->readAll();
|
||||
QJsonParseError json_error;
|
||||
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
|
||||
|
||||
if (json_error.error != QJsonParseError::NoError) {
|
||||
LoginError(u"Authentication reply from server missing Json data."_s);
|
||||
return;
|
||||
}
|
||||
|
||||
if (json_doc.isEmpty()) {
|
||||
LoginError(u"Authentication reply from server has empty Json document."_s);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!json_doc.isObject()) {
|
||||
LoginError(u"Authentication reply from server has Json document that is not an object."_s, json_doc);
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject json_obj = json_doc.object();
|
||||
if (json_obj.isEmpty()) {
|
||||
LoginError(u"Authentication reply from server has empty Json object."_s, json_doc);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!json_obj.contains("token_type"_L1) || !json_obj.contains("access_token"_L1) || !json_obj.contains("expires_in"_L1)) {
|
||||
LoginError(u"Authentication reply from server is missing token_type, access_token or expires_in"_s, json_obj);
|
||||
return;
|
||||
}
|
||||
|
||||
token_type_ = json_obj["token_type"_L1].toString();
|
||||
access_token_ = json_obj["access_token"_L1].toString();
|
||||
refresh_token_ = json_obj["refresh_token"_L1].toString();
|
||||
expires_in_ = json_obj["expires_in"_L1].toInt();
|
||||
login_time_ = QDateTime::currentSecsSinceEpoch();
|
||||
|
||||
if (json_obj.contains("user"_L1) && json_obj["user"_L1].isObject()) {
|
||||
QJsonObject obj_user = json_obj["user"_L1].toObject();
|
||||
if (obj_user.contains("countryCode"_L1) && obj_user.contains("userId"_L1)) {
|
||||
country_code_ = obj_user["countryCode"_L1].toString();
|
||||
user_id_ = obj_user["userId"_L1].toInt();
|
||||
}
|
||||
}
|
||||
|
||||
Settings s;
|
||||
s.beginGroup(TidalSettings::kSettingsGroup);
|
||||
s.setValue(kTokenType, token_type_);
|
||||
s.setValue(kAccessToken, access_token_);
|
||||
s.setValue(kRefreshToken, refresh_token_);
|
||||
s.setValue(kExpiresIn, expires_in_);
|
||||
s.setValue(kLoginTime, login_time_);
|
||||
s.setValue(kCountryCode, country_code_);
|
||||
s.setValue(kUserId, user_id_);
|
||||
s.remove(kSessionId);
|
||||
s.endGroup();
|
||||
|
||||
if (expires_in_ > 0) {
|
||||
timer_refresh_login_->setInterval(static_cast<int>(expires_in_ * kMsecPerSec));
|
||||
timer_refresh_login_->start();
|
||||
}
|
||||
|
||||
qLog(Debug) << "Tidal: Login successful" << "user id" << user_id_;
|
||||
|
||||
Q_EMIT LoginComplete(true);
|
||||
Q_EMIT LoginSuccess();
|
||||
|
||||
}
|
||||
|
||||
void TidalService::Logout() {
|
||||
|
||||
user_id_ = 0;
|
||||
country_code_.clear();
|
||||
access_token_.clear();
|
||||
refresh_token_.clear();
|
||||
expires_in_ = 0;
|
||||
login_time_ = 0;
|
||||
|
||||
Settings s;
|
||||
s.beginGroup(TidalSettings::kSettingsGroup);
|
||||
s.remove(kUserId);
|
||||
s.remove(kCountryCode);
|
||||
s.remove(kTokenType);
|
||||
s.remove(kAccessToken);
|
||||
s.remove(kRefreshToken);
|
||||
s.remove(kSessionId);
|
||||
s.remove(kExpiresIn);
|
||||
s.remove(kLoginTime);
|
||||
s.endGroup();
|
||||
|
||||
timer_refresh_login_->stop();
|
||||
|
||||
}
|
||||
|
||||
void TidalService::ResetArtistsRequest() {
|
||||
|
||||
if (artists_request_) {
|
||||
QObject::disconnect(&*artists_request_, nullptr, this, nullptr);
|
||||
QObject::disconnect(this, nullptr, &*artists_request_, nullptr);
|
||||
artists_request_.reset();
|
||||
}
|
||||
oauth_->ClearSession();
|
||||
|
||||
}
|
||||
|
||||
@@ -547,8 +283,7 @@ void TidalService::GetArtists() {
|
||||
return;
|
||||
}
|
||||
|
||||
ResetArtistsRequest();
|
||||
artists_request_.reset(new TidalRequest(this, url_handler_, network_, TidalBaseRequest::Type::FavouriteArtists, this), [](TidalRequest *request) { request->deleteLater(); });
|
||||
artists_request_.reset(new TidalRequest(this, url_handler_, network_, TidalBaseRequest::Type::FavouriteArtists, this));
|
||||
QObject::connect(&*artists_request_, &TidalRequest::Results, this, &TidalService::ArtistsResultsReceived);
|
||||
QObject::connect(&*artists_request_, &TidalRequest::UpdateStatus, this, &TidalService::ArtistsUpdateStatusReceived);
|
||||
QObject::connect(&*artists_request_, &TidalRequest::UpdateProgress, this, &TidalService::ArtistsUpdateProgressReceived);
|
||||
@@ -557,6 +292,13 @@ void TidalService::GetArtists() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
void TidalService::ResetArtistsRequest() {
|
||||
|
||||
artists_request_.reset();
|
||||
|
||||
}
|
||||
|
||||
void TidalService::ArtistsResultsReceived(const int id, const SongMap &songs, const QString &error) {
|
||||
|
||||
Q_UNUSED(id);
|
||||
@@ -575,16 +317,6 @@ void TidalService::ArtistsUpdateProgressReceived(const int id, const int progres
|
||||
Q_EMIT ArtistsUpdateProgress(progress);
|
||||
}
|
||||
|
||||
void TidalService::ResetAlbumsRequest() {
|
||||
|
||||
if (albums_request_) {
|
||||
QObject::disconnect(&*albums_request_, nullptr, this, nullptr);
|
||||
QObject::disconnect(this, nullptr, &*albums_request_, nullptr);
|
||||
albums_request_.reset();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void TidalService::GetAlbums() {
|
||||
|
||||
if (!authenticated()) {
|
||||
@@ -593,8 +325,7 @@ void TidalService::GetAlbums() {
|
||||
return;
|
||||
}
|
||||
|
||||
ResetAlbumsRequest();
|
||||
albums_request_.reset(new TidalRequest(this, url_handler_, network_, TidalBaseRequest::Type::FavouriteAlbums, this), [](TidalRequest *request) { request->deleteLater(); });
|
||||
albums_request_.reset(new TidalRequest(this, url_handler_, network_, TidalBaseRequest::Type::FavouriteAlbums, this));
|
||||
QObject::connect(&*albums_request_, &TidalRequest::Results, this, &TidalService::AlbumsResultsReceived);
|
||||
QObject::connect(&*albums_request_, &TidalRequest::UpdateStatus, this, &TidalService::AlbumsUpdateStatusReceived);
|
||||
QObject::connect(&*albums_request_, &TidalRequest::UpdateProgress, this, &TidalService::AlbumsUpdateProgressReceived);
|
||||
@@ -603,6 +334,12 @@ void TidalService::GetAlbums() {
|
||||
|
||||
}
|
||||
|
||||
void TidalService::ResetAlbumsRequest() {
|
||||
|
||||
albums_request_.reset();
|
||||
|
||||
}
|
||||
|
||||
void TidalService::AlbumsResultsReceived(const int id, const SongMap &songs, const QString &error) {
|
||||
|
||||
Q_UNUSED(id);
|
||||
@@ -621,16 +358,6 @@ void TidalService::AlbumsUpdateProgressReceived(const int id, const int progress
|
||||
Q_EMIT AlbumsUpdateProgress(progress);
|
||||
}
|
||||
|
||||
void TidalService::ResetSongsRequest() {
|
||||
|
||||
if (songs_request_) {
|
||||
QObject::disconnect(&*songs_request_, nullptr, this, nullptr);
|
||||
QObject::disconnect(this, nullptr, &*songs_request_, nullptr);
|
||||
songs_request_.reset();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void TidalService::GetSongs() {
|
||||
|
||||
if (!authenticated()) {
|
||||
@@ -639,8 +366,7 @@ void TidalService::GetSongs() {
|
||||
return;
|
||||
}
|
||||
|
||||
ResetSongsRequest();
|
||||
songs_request_.reset(new TidalRequest(this, url_handler_, network_, TidalBaseRequest::Type::FavouriteSongs, this), [](TidalRequest *request) { request->deleteLater(); });
|
||||
songs_request_.reset(new TidalRequest(this, url_handler_, network_, TidalBaseRequest::Type::FavouriteSongs, this));
|
||||
QObject::connect(&*songs_request_, &TidalRequest::Results, this, &TidalService::SongsResultsReceived);
|
||||
QObject::connect(&*songs_request_, &TidalRequest::UpdateStatus, this, &TidalService::SongsUpdateStatusReceived);
|
||||
QObject::connect(&*songs_request_, &TidalRequest::UpdateProgress, this, &TidalService::SongsUpdateProgressReceived);
|
||||
@@ -649,6 +375,12 @@ void TidalService::GetSongs() {
|
||||
|
||||
}
|
||||
|
||||
void TidalService::ResetSongsRequest() {
|
||||
|
||||
songs_request_.reset();
|
||||
|
||||
}
|
||||
|
||||
void TidalService::SongsResultsReceived(const int id, const SongMap &songs, const QString &error) {
|
||||
|
||||
Q_UNUSED(id);
|
||||
@@ -721,8 +453,7 @@ void TidalService::SendSearch() {
|
||||
return;
|
||||
}
|
||||
|
||||
search_request_.reset(new TidalRequest(this, url_handler_, network_, query_type, this), [](TidalRequest *request) { request->deleteLater(); });
|
||||
|
||||
search_request_.reset(new TidalRequest(this, url_handler_, network_, query_type, this));
|
||||
QObject::connect(&*search_request_, &TidalRequest::Results, this, &TidalService::SearchResultsReceived);
|
||||
QObject::connect(&*search_request_, &TidalRequest::UpdateStatus, this, &TidalService::SearchUpdateStatus);
|
||||
QObject::connect(&*search_request_, &TidalRequest::UpdateProgress, this, &TidalService::SearchUpdateProgress);
|
||||
@@ -748,14 +479,11 @@ uint TidalService::GetStreamURL(const QUrl &url, QString &error) {
|
||||
|
||||
uint id = 0;
|
||||
while (id == 0) id = ++next_stream_url_request_id_;
|
||||
SharedPtr<TidalStreamURLRequest> stream_url_req;
|
||||
stream_url_req.reset(new TidalStreamURLRequest(this, network_, url, id), [](TidalStreamURLRequest *request) { request->deleteLater(); });
|
||||
stream_url_requests_.insert(id, stream_url_req);
|
||||
|
||||
QObject::connect(&*stream_url_req, &TidalStreamURLRequest::StreamURLFailure, this, &TidalService::HandleStreamURLFailure);
|
||||
QObject::connect(&*stream_url_req, &TidalStreamURLRequest::StreamURLSuccess, this, &TidalService::HandleStreamURLSuccess);
|
||||
|
||||
stream_url_req->Process();
|
||||
TidalStreamURLRequestPtr stream_url_request = TidalStreamURLRequestPtr(new TidalStreamURLRequest(this, network_, url, id), &QObject::deleteLater);
|
||||
stream_url_requests_.insert(id, stream_url_request);
|
||||
QObject::connect(&*stream_url_request, &TidalStreamURLRequest::StreamURLFailure, this, &TidalService::HandleStreamURLFailure);
|
||||
QObject::connect(&*stream_url_request, &TidalStreamURLRequest::StreamURLSuccess, this, &TidalService::HandleStreamURLSuccess);
|
||||
stream_url_request->Process();
|
||||
|
||||
return id;
|
||||
|
||||
@@ -778,21 +506,3 @@ void TidalService::HandleStreamURLSuccess(const uint id, const QUrl &media_url,
|
||||
Q_EMIT StreamURLSuccess(id, media_url, stream_url, filetype, samplerate, bit_depth, duration);
|
||||
|
||||
}
|
||||
|
||||
void TidalService::LoginError(const QString &error, const QVariant &debug) {
|
||||
|
||||
if (!error.isEmpty()) login_errors_ << error;
|
||||
|
||||
QString error_html;
|
||||
for (const QString &e : std::as_const(login_errors_)) {
|
||||
qLog(Error) << "Tidal:" << e;
|
||||
error_html += e + "<br />"_L1;
|
||||
}
|
||||
if (debug.isValid()) qLog(Debug) << debug;
|
||||
|
||||
Q_EMIT LoginFailure(error_html);
|
||||
Q_EMIT LoginComplete(false, error_html);
|
||||
|
||||
login_errors_.clear();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2025, 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
|
||||
@@ -22,27 +22,20 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QPair>
|
||||
#include <QSet>
|
||||
#include <QList>
|
||||
#include <QMap>
|
||||
#include <QVariant>
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QUrl>
|
||||
#include <QDateTime>
|
||||
#include <QSslError>
|
||||
#include <QScopedPointer>
|
||||
#include <QSharedPointer>
|
||||
|
||||
#include "includes/shared_ptr.h"
|
||||
#include "core/song.h"
|
||||
#include "streaming/streamingservice.h"
|
||||
#include "streaming/streamingsearchview.h"
|
||||
#include "constants/tidalsettings.h"
|
||||
#include "collection/collectionmodel.h"
|
||||
|
||||
class QNetworkReply;
|
||||
class QTimer;
|
||||
|
||||
class TaskManager;
|
||||
@@ -57,6 +50,10 @@ class TidalStreamURLRequest;
|
||||
class CollectionBackend;
|
||||
class CollectionModel;
|
||||
class CollectionFilter;
|
||||
class OAuthenticator;
|
||||
|
||||
using TidalRequestPtr = QScopedPointer<TidalRequest, QScopedPointerDeleteLater>;
|
||||
using TidalStreamURLRequestPtr = QSharedPointer<TidalStreamURLRequest>;
|
||||
|
||||
class TidalService : public StreamingService {
|
||||
Q_OBJECT
|
||||
@@ -78,13 +75,11 @@ class TidalService : public StreamingService {
|
||||
void Exit() override;
|
||||
void ReloadSettings() override;
|
||||
|
||||
void Logout();
|
||||
void ClearSession();
|
||||
int Search(const QString &text, const SearchType type) override;
|
||||
void CancelSearch() override;
|
||||
|
||||
QString client_id() const { return client_id_; }
|
||||
quint64 user_id() const { return user_id_; }
|
||||
QString country_code() const { return country_code_; }
|
||||
QString quality() const { return quality_; }
|
||||
int artistssearchlimit() const { return artistssearchlimit_; }
|
||||
int albumssearchlimit() const { return albumssearchlimit_; }
|
||||
@@ -95,10 +90,10 @@ class TidalService : public StreamingService {
|
||||
TidalSettings::StreamUrlMethod stream_url_method() const { return stream_url_method_; }
|
||||
bool album_explicit() const { return album_explicit_; }
|
||||
|
||||
QString token_type() const { return token_type_; }
|
||||
QString access_token() const { return access_token_; }
|
||||
|
||||
bool authenticated() const override { return !token_type_.isEmpty() && !access_token_.isEmpty(); }
|
||||
bool authenticated() const override;
|
||||
QByteArray authorization_header() const;
|
||||
QString country_code() const;
|
||||
quint64 user_id() const;
|
||||
|
||||
uint GetStreamURL(const QUrl &url, QString &error);
|
||||
|
||||
@@ -116,19 +111,17 @@ class TidalService : public StreamingService {
|
||||
|
||||
public Q_SLOTS:
|
||||
void StartAuthorization(const QString &client_id);
|
||||
void AuthorizationUrlReceived(const QUrl &url);
|
||||
void GetArtists() override;
|
||||
void GetAlbums() override;
|
||||
void GetSongs() override;
|
||||
void ResetArtistsRequest() override;
|
||||
void ResetAlbumsRequest() override;
|
||||
void ResetSongsRequest() override;
|
||||
void AuthorizationUrlReceived(const QUrl &url);
|
||||
|
||||
private Q_SLOTS:
|
||||
void ExitReceived();
|
||||
void RequestNewAccessToken() { RequestAccessToken(); }
|
||||
void HandleLoginSSLErrors(const QList<QSslError> &ssl_errors);
|
||||
void AccessTokenRequestFinished(QNetworkReply *reply);
|
||||
void OAuthFinished(const bool success, const QString &error);
|
||||
void StartSearch();
|
||||
void ArtistsResultsReceived(const int id, const SongMap &songs, const QString &error);
|
||||
void AlbumsResultsReceived(const int id, const SongMap &songs, const QString &error);
|
||||
@@ -144,16 +137,12 @@ class TidalService : public StreamingService {
|
||||
void HandleStreamURLSuccess(const uint id, const QUrl &media_url, const QUrl &stream_url, const Song::FileType filetype, const int samplerate, const int bit_depth, const qint64 duration);
|
||||
|
||||
private:
|
||||
using Param = QPair<QString, QString>;
|
||||
using ParamList = QList<Param>;
|
||||
|
||||
void LoadSession();
|
||||
void RequestAccessToken(const QString &code = QString());
|
||||
void SendSearch();
|
||||
void LoginError(const QString &error = QString(), const QVariant &debug = QVariant());
|
||||
|
||||
private:
|
||||
const SharedPtr<NetworkAccessManager> network_;
|
||||
TidalUrlHandler *url_handler_;
|
||||
OAuthenticator *oauth_;
|
||||
|
||||
SharedPtr<CollectionBackend> artists_collection_backend_;
|
||||
SharedPtr<CollectionBackend> albums_collection_backend_;
|
||||
@@ -164,18 +153,15 @@ class TidalService : public StreamingService {
|
||||
CollectionModel *songs_collection_model_;
|
||||
|
||||
QTimer *timer_search_delay_;
|
||||
QTimer *timer_refresh_login_;
|
||||
|
||||
SharedPtr<TidalRequest> artists_request_;
|
||||
SharedPtr<TidalRequest> albums_request_;
|
||||
SharedPtr<TidalRequest> songs_request_;
|
||||
SharedPtr<TidalRequest> search_request_;
|
||||
TidalRequestPtr artists_request_;
|
||||
TidalRequestPtr albums_request_;
|
||||
TidalRequestPtr songs_request_;
|
||||
TidalRequestPtr search_request_;
|
||||
TidalFavoriteRequest *favorite_request_;
|
||||
|
||||
bool enabled_;
|
||||
QString client_id_;
|
||||
quint64 user_id_;
|
||||
QString country_code_;
|
||||
QString quality_;
|
||||
int artistssearchlimit_;
|
||||
int albumssearchlimit_;
|
||||
@@ -186,12 +172,6 @@ class TidalService : public StreamingService {
|
||||
TidalSettings::StreamUrlMethod stream_url_method_;
|
||||
bool album_explicit_;
|
||||
|
||||
QString token_type_;
|
||||
QString access_token_;
|
||||
QString refresh_token_;
|
||||
quint64 expires_in_;
|
||||
quint64 login_time_;
|
||||
|
||||
int pending_search_id_;
|
||||
int next_pending_search_id_;
|
||||
QString pending_search_text_;
|
||||
@@ -200,16 +180,10 @@ class TidalService : public StreamingService {
|
||||
int search_id_;
|
||||
QString search_text_;
|
||||
|
||||
QString code_verifier_;
|
||||
QString code_challenge_;
|
||||
|
||||
uint next_stream_url_request_id_;
|
||||
QMap<uint, SharedPtr<TidalStreamURLRequest>> stream_url_requests_;
|
||||
|
||||
QStringList login_errors_;
|
||||
QMap<uint, TidalStreamURLRequestPtr> stream_url_requests_;
|
||||
|
||||
QList<QObject*> wait_for_exit_;
|
||||
QList<QNetworkReply*> replies_;
|
||||
};
|
||||
|
||||
using TidalServicePtr = SharedPtr<TidalService>;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2025, 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
|
||||
@@ -62,6 +62,29 @@ TidalStreamURLRequest::~TidalStreamURLRequest() {
|
||||
|
||||
}
|
||||
|
||||
bool TidalStreamURLRequest::oauth() const {
|
||||
|
||||
return service_->oauth();
|
||||
|
||||
}
|
||||
TidalSettings::StreamUrlMethod TidalStreamURLRequest::stream_url_method() const {
|
||||
|
||||
return service_->stream_url_method();
|
||||
|
||||
}
|
||||
|
||||
QUrl TidalStreamURLRequest::media_url() const {
|
||||
|
||||
return media_url_;
|
||||
|
||||
}
|
||||
|
||||
int TidalStreamURLRequest::song_id() const {
|
||||
|
||||
return song_id_;
|
||||
|
||||
}
|
||||
|
||||
void TidalStreamURLRequest::Process() {
|
||||
|
||||
if (!authenticated()) {
|
||||
@@ -96,12 +119,12 @@ void TidalStreamURLRequest::GetStreamURL() {
|
||||
|
||||
switch (stream_url_method()) {
|
||||
case TidalSettings::StreamUrlMethod::StreamUrl:
|
||||
params << Param(u"soundQuality"_s, quality());
|
||||
params << Param(u"soundQuality"_s, service_->quality());
|
||||
reply_ = CreateRequest(QStringLiteral("tracks/%1/streamUrl").arg(song_id_), params);
|
||||
QObject::connect(reply_, &QNetworkReply::finished, this, &TidalStreamURLRequest::StreamURLReceived);
|
||||
break;
|
||||
case TidalSettings::StreamUrlMethod::UrlPostPaywall:
|
||||
params << Param(u"audioquality"_s, quality());
|
||||
params << Param(u"audioquality"_s, service_->quality());
|
||||
params << Param(u"playbackmode"_s, u"STREAM"_s);
|
||||
params << Param(u"assetpresentation"_s, u"FULL"_s);
|
||||
params << Param(u"urlusagemode"_s, u"STREAM"_s);
|
||||
@@ -109,7 +132,7 @@ void TidalStreamURLRequest::GetStreamURL() {
|
||||
QObject::connect(reply_, &QNetworkReply::finished, this, &TidalStreamURLRequest::StreamURLReceived);
|
||||
break;
|
||||
case TidalSettings::StreamUrlMethod::PlaybackInfoPostPaywall:
|
||||
params << Param(u"audioquality"_s, quality());
|
||||
params << Param(u"audioquality"_s, service_->quality());
|
||||
params << Param(u"playbackmode"_s, u"STREAM"_s);
|
||||
params << Param(u"assetpresentation"_s, u"FULL"_s);
|
||||
reply_ = CreateRequest(QStringLiteral("tracks/%1/playbackinfopostpaywall").arg(song_id_), params);
|
||||
@@ -123,39 +146,36 @@ void TidalStreamURLRequest::StreamURLReceived() {
|
||||
|
||||
if (!reply_) return;
|
||||
|
||||
QByteArray data = GetReplyData(reply_);
|
||||
Q_ASSERT(replies_.contains(reply_));
|
||||
replies_.removeAll(reply_);
|
||||
|
||||
const JsonObjectResult json_object_result = ParseJsonObject(reply_);
|
||||
|
||||
QObject::disconnect(reply_, nullptr, this, nullptr);
|
||||
reply_->deleteLater();
|
||||
reply_ = nullptr;
|
||||
|
||||
if (data.isEmpty()) {
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, errors_.constFirst());
|
||||
if (!json_object_result.success()) {
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, json_object_result.error_message);
|
||||
return;
|
||||
}
|
||||
const QJsonObject &json_object = json_object_result.json_object;
|
||||
|
||||
QJsonObject json_obj = ExtractJsonObj(data);
|
||||
if (json_obj.isEmpty()) {
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, errors_.constFirst());
|
||||
if (!json_object.contains("trackId"_L1)) {
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, u"Invalid Json reply, stream missing trackId."_s);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!json_obj.contains("trackId"_L1)) {
|
||||
Error(u"Invalid Json reply, stream missing trackId."_s, json_obj);
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, errors_.constFirst());
|
||||
return;
|
||||
}
|
||||
int track_id = json_obj["trackId"_L1].toInt();
|
||||
const int track_id = json_object["trackId"_L1].toInt();
|
||||
if (track_id != song_id_) {
|
||||
qLog(Debug) << "Tidal returned track ID" << track_id << "for" << media_url_;
|
||||
}
|
||||
|
||||
Song::FileType filetype(Song::FileType::Stream);
|
||||
|
||||
if (json_obj.contains("codec"_L1) || json_obj.contains("codecs"_L1)) {
|
||||
if (json_object.contains("codec"_L1) || json_object.contains("codecs"_L1)) {
|
||||
QString codec;
|
||||
if (json_obj.contains("codec"_L1)) codec = json_obj["codec"_L1].toString().toLower();
|
||||
if (json_obj.contains("codecs"_L1)) codec = json_obj["codecs"_L1].toString().toLower();
|
||||
if (json_object.contains("codec"_L1)) codec = json_object["codec"_L1].toString().toLower();
|
||||
if (json_object.contains("codecs"_L1)) codec = json_object["codecs"_L1].toString().toLower();
|
||||
filetype = Song::FiletypeByExtension(codec);
|
||||
if (filetype == Song::FileType::Unknown) {
|
||||
qLog(Debug) << "Tidal: Unknown codec" << codec;
|
||||
@@ -165,10 +185,10 @@ void TidalStreamURLRequest::StreamURLReceived() {
|
||||
|
||||
QList<QUrl> urls;
|
||||
|
||||
if (json_obj.contains("manifest"_L1)) {
|
||||
if (json_object.contains("manifest"_L1)) {
|
||||
|
||||
QString manifest(json_obj["manifest"_L1].toString());
|
||||
QByteArray data_manifest = QByteArray::fromBase64(manifest.toUtf8());
|
||||
const QString manifest(json_object["manifest"_L1].toString());
|
||||
const QByteArray data_manifest = QByteArray::fromBase64(manifest.toUtf8());
|
||||
|
||||
QXmlStreamReader xml_reader(data_manifest);
|
||||
if (xml_reader.readNextStartElement()) {
|
||||
@@ -180,29 +200,28 @@ void TidalStreamURLRequest::StreamURLReceived() {
|
||||
|
||||
else {
|
||||
|
||||
json_obj = ExtractJsonObj(data_manifest);
|
||||
if (json_obj.isEmpty()) {
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, errors_.constFirst());
|
||||
const JsonObjectResult json_object_result_manifest = GetJsonObject(data_manifest);
|
||||
if (!json_object_result_manifest.success()) {
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, json_object_result_manifest.error_message);
|
||||
return;
|
||||
}
|
||||
const QJsonObject &object_manifest = json_object_result_manifest.json_object;
|
||||
|
||||
if (json_obj.contains("encryptionType"_L1) && json_obj.contains("keyId"_L1)) {
|
||||
QString encryption_type = json_obj["encryptionType"_L1].toString();
|
||||
QString key_id = json_obj["keyId"_L1].toString();
|
||||
if (object_manifest.contains("encryptionType"_L1) && object_manifest.contains("keyId"_L1)) {
|
||||
QString encryption_type = object_manifest["encryptionType"_L1].toString();
|
||||
QString key_id = object_manifest["keyId"_L1].toString();
|
||||
if (!encryption_type.isEmpty() && !key_id.isEmpty()) {
|
||||
Error(tr("Received URL with %1 encrypted stream from Tidal. Strawberry does not currently support encrypted streams.").arg(encryption_type));
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, errors_.constFirst());
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, tr("Received URL with %1 encrypted stream from Tidal. Strawberry does not currently support encrypted streams.").arg(encryption_type));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!json_obj.contains("mimeType"_L1)) {
|
||||
Error(u"Invalid Json reply, stream url reply manifest is missing mimeType."_s, json_obj);
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, errors_.constFirst());
|
||||
if (!object_manifest.contains("mimeType"_L1)) {
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, u"Invalid Json reply, stream url reply manifest is missing mimeType."_s);
|
||||
return;
|
||||
}
|
||||
|
||||
QString mimetype = json_obj["mimeType"_L1].toString();
|
||||
const QString mimetype = object_manifest["mimeType"_L1].toString();
|
||||
QMimeDatabase mimedb;
|
||||
const QStringList suffixes = mimedb.mimeTypeForName(mimetype).suffixes();
|
||||
for (const QString &suffix : suffixes) {
|
||||
@@ -217,11 +236,10 @@ void TidalStreamURLRequest::StreamURLReceived() {
|
||||
|
||||
}
|
||||
|
||||
if (json_obj.contains("urls"_L1)) {
|
||||
QJsonValue json_urls = json_obj["urls"_L1];
|
||||
if (json_object.contains("urls"_L1)) {
|
||||
const QJsonValue json_urls = json_object["urls"_L1];
|
||||
if (!json_urls.isArray()) {
|
||||
Error(u"Invalid Json reply, urls is not an array."_s, json_urls);
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, errors_.constFirst());
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, u"Invalid Json reply, urls is not an array."_s);
|
||||
return;
|
||||
}
|
||||
const QJsonArray json_array_urls = json_urls.toArray();
|
||||
@@ -230,8 +248,8 @@ void TidalStreamURLRequest::StreamURLReceived() {
|
||||
urls << QUrl(value.toString());
|
||||
}
|
||||
}
|
||||
else if (json_obj.contains("url"_L1)) {
|
||||
QUrl new_url(json_obj["url"_L1].toString());
|
||||
else if (json_object.contains("url"_L1)) {
|
||||
const QUrl new_url(json_object["url"_L1].toString());
|
||||
urls << new_url;
|
||||
if (filetype == Song::FileType::Stream) {
|
||||
// Guess filetype by filename extension in URL.
|
||||
@@ -240,42 +258,28 @@ void TidalStreamURLRequest::StreamURLReceived() {
|
||||
}
|
||||
}
|
||||
|
||||
if (json_obj.contains("encryptionKey"_L1)) {
|
||||
QString encryption_key = json_obj["encryptionKey"_L1].toString();
|
||||
if (json_object.contains("encryptionKey"_L1)) {
|
||||
const QString encryption_key = json_object["encryptionKey"_L1].toString();
|
||||
if (!encryption_key.isEmpty()) {
|
||||
Error(tr("Received URL with encrypted stream from Tidal. Strawberry does not currently support encrypted streams."));
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, errors_.constFirst());
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, tr("Received URL with encrypted stream from Tidal. Strawberry does not currently support encrypted streams."));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (json_obj.contains("securityType"_L1) && json_obj.contains("securityToken"_L1)) {
|
||||
QString security_type = json_obj["securityType"_L1].toString();
|
||||
QString security_token = json_obj["securityToken"_L1].toString();
|
||||
if (json_object.contains("securityType"_L1) && json_object.contains("securityToken"_L1)) {
|
||||
const QString security_type = json_object["securityType"_L1].toString();
|
||||
const QString security_token = json_object["securityToken"_L1].toString();
|
||||
if (!security_type.isEmpty() && !security_token.isEmpty()) {
|
||||
Error(tr("Received URL with encrypted stream from Tidal. Strawberry does not currently support encrypted streams."));
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, errors_.constFirst());
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, tr("Received URL with encrypted stream from Tidal. Strawberry does not currently support encrypted streams."));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (urls.isEmpty()) {
|
||||
Error(u"Missing stream urls."_s, json_obj);
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, errors_.constFirst());
|
||||
Q_EMIT StreamURLFailure(id_, media_url_, u"Missing stream urls."_s);
|
||||
return;
|
||||
}
|
||||
|
||||
Q_EMIT StreamURLSuccess(id_, media_url_, urls.first(), filetype);
|
||||
|
||||
}
|
||||
|
||||
void TidalStreamURLRequest::Error(const QString &error, const QVariant &debug) {
|
||||
|
||||
qLog(Error) << "Tidal:" << error;
|
||||
if (debug.isValid()) qLog(Debug) << debug;
|
||||
|
||||
if (!error.isEmpty()) {
|
||||
errors_ << error;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2025, 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
|
||||
@@ -22,21 +22,18 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QVariant>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QUrl>
|
||||
#include <QSharedPointer>
|
||||
|
||||
#include "includes/shared_ptr.h"
|
||||
#include "core/song.h"
|
||||
#include "tidalservice.h"
|
||||
#include "tidalbaserequest.h"
|
||||
#include "settings/tidalsettingspage.h"
|
||||
|
||||
class QNetworkReply;
|
||||
class NetworkAccessManager;
|
||||
class TidalService;
|
||||
|
||||
class TidalStreamURLRequest : public TidalBaseRequest {
|
||||
Q_OBJECT
|
||||
@@ -49,10 +46,10 @@ class TidalStreamURLRequest : public TidalBaseRequest {
|
||||
void Process();
|
||||
void Cancel();
|
||||
|
||||
bool oauth() const { return service_->oauth(); }
|
||||
TidalSettings::StreamUrlMethod stream_url_method() const { return service_->stream_url_method(); }
|
||||
QUrl media_url() const { return media_url_; }
|
||||
int song_id() const { return song_id_; }
|
||||
bool oauth() const;
|
||||
TidalSettings::StreamUrlMethod stream_url_method() const;
|
||||
QUrl media_url() const;
|
||||
int song_id() const;
|
||||
|
||||
Q_SIGNALS:
|
||||
void StreamURLFailure(const uint id, const QUrl &media_url, const QString &error);
|
||||
@@ -62,15 +59,13 @@ class TidalStreamURLRequest : public TidalBaseRequest {
|
||||
void StreamURLReceived();
|
||||
|
||||
private:
|
||||
void Error(const QString &error, const QVariant &debug = QVariant()) override;
|
||||
|
||||
TidalService *service_;
|
||||
QNetworkReply *reply_;
|
||||
QUrl media_url_;
|
||||
uint id_;
|
||||
int song_id_;
|
||||
bool need_login_;
|
||||
QStringList errors_;
|
||||
};
|
||||
|
||||
using TidalStreamURLRequestPtr = QSharedPointer<TidalStreamURLRequest>;
|
||||
|
||||
#endif // TIDALSTREAMURLREQUEST_H
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2025, 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
|
||||
@@ -19,7 +19,6 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
|
||||
@@ -39,18 +38,24 @@ TidalUrlHandler::TidalUrlHandler(const SharedPtr<TaskManager> task_manager, Tida
|
||||
|
||||
}
|
||||
|
||||
QString TidalUrlHandler::scheme() const {
|
||||
|
||||
return service_->url_scheme();
|
||||
|
||||
}
|
||||
|
||||
UrlHandler::LoadResult TidalUrlHandler::StartLoading(const QUrl &url) {
|
||||
|
||||
Request req;
|
||||
req.task_id = task_manager_->StartTask(QStringLiteral("Loading %1 stream...").arg(url.scheme()));
|
||||
Request request;
|
||||
request.task_id = task_manager_->StartTask(QStringLiteral("Loading %1 stream...").arg(url.scheme()));
|
||||
QString error;
|
||||
req.id = service_->GetStreamURL(url, error);
|
||||
if (req.id == 0) {
|
||||
CancelTask(req.task_id);
|
||||
request.id = service_->GetStreamURL(url, error);
|
||||
if (request.id == 0) {
|
||||
CancelTask(request.task_id);
|
||||
return LoadResult(url, LoadResult::Type::Error, error);
|
||||
}
|
||||
|
||||
requests_.insert(req.id, req);
|
||||
requests_.insert(request.id, request);
|
||||
|
||||
LoadResult ret(url);
|
||||
ret.type_ = LoadResult::Type::WillLoadAsynchronously;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2018-2025, 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
|
||||
@@ -22,8 +22,6 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QMap>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
@@ -31,9 +29,9 @@
|
||||
#include "includes/shared_ptr.h"
|
||||
#include "core/urlhandler.h"
|
||||
#include "core/song.h"
|
||||
#include "tidal/tidalservice.h"
|
||||
|
||||
class TaskManager;
|
||||
class TidalService;
|
||||
|
||||
class TidalUrlHandler : public UrlHandler {
|
||||
Q_OBJECT
|
||||
@@ -41,7 +39,7 @@ class TidalUrlHandler : public UrlHandler {
|
||||
public:
|
||||
explicit TidalUrlHandler(const SharedPtr<TaskManager> task_manager, TidalService *service);
|
||||
|
||||
QString scheme() const override { return service_->url_scheme(); }
|
||||
QString scheme() const override;
|
||||
LoadResult StartLoading(const QUrl &url) override;
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user