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 2022-2024, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2022-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,23 +19,10 @@
|
||||
|
||||
#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 "includes/shared_ptr.h"
|
||||
#include "core/logging.h"
|
||||
#include "core/networkaccessmanager.h"
|
||||
#include "spotifyservice.h"
|
||||
#include "spotifybaserequest.h"
|
||||
@@ -43,143 +30,97 @@
|
||||
using namespace Qt::Literals::StringLiterals;
|
||||
|
||||
SpotifyBaseRequest::SpotifyBaseRequest(SpotifyService *service, const SharedPtr<NetworkAccessManager> network, QObject *parent)
|
||||
: QObject(parent),
|
||||
service_(service),
|
||||
network_(network) {}
|
||||
: JsonBaseRequest(network, parent),
|
||||
service_(service) {}
|
||||
|
||||
QString SpotifyBaseRequest::service_name() const {
|
||||
|
||||
return service_->name();
|
||||
|
||||
}
|
||||
|
||||
bool SpotifyBaseRequest::authentication_required() const {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
bool SpotifyBaseRequest::authenticated() const {
|
||||
|
||||
return service_->authenticated();
|
||||
|
||||
}
|
||||
|
||||
bool SpotifyBaseRequest::use_authorization_header() const {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
QByteArray SpotifyBaseRequest::authorization_header() const {
|
||||
|
||||
return service_->authorization_header();
|
||||
|
||||
}
|
||||
|
||||
QNetworkReply *SpotifyBaseRequest::CreateRequest(const QString &ressource_name, const ParamList ¶ms_provided) {
|
||||
|
||||
QUrlQuery url_query;
|
||||
for (const Param ¶m : params_provided) {
|
||||
url_query.addQueryItem(QString::fromLatin1(QUrl::toPercentEncoding(param.first)), QString::fromLatin1(QUrl::toPercentEncoding(param.second)));
|
||||
}
|
||||
|
||||
QUrl url(QLatin1String(SpotifyService::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 (!access_token().isEmpty()) req.setRawHeader("authorization", "Bearer " + access_token().toUtf8());
|
||||
|
||||
QNetworkReply *reply = network_->get(req);
|
||||
QObject::connect(reply, &QNetworkReply::sslErrors, this, &SpotifyBaseRequest::HandleSSLErrors);
|
||||
|
||||
qLog(Debug) << "Spotify: Sending request" << url;
|
||||
|
||||
return reply;
|
||||
return CreateGetRequest(QUrl(QLatin1String(SpotifyService::kApiUrl) + QLatin1Char('/') + ressource_name), params_provided);
|
||||
|
||||
}
|
||||
|
||||
void SpotifyBaseRequest::HandleSSLErrors(const QList<QSslError> &ssl_errors) {
|
||||
JsonBaseRequest::JsonObjectResult SpotifyBaseRequest::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 SpotifyBaseRequest::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();
|
||||
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("error"_L1) && json_object["error"_L1].isObject()) {
|
||||
const QJsonObject object_error = json_object["error"_L1].toObject();
|
||||
if (object_error.contains("status"_L1) && object_error.contains("message"_L1)) {
|
||||
const int status = object_error["status"_L1].toInt();
|
||||
const QString message = object_error["message"_L1].toString();
|
||||
result.error_code = ErrorCode::APIError;
|
||||
result.error_message = QStringLiteral("%1 (%2)").arg(message).arg(status);
|
||||
}
|
||||
}
|
||||
else {
|
||||
result.json_object = json_document.object();
|
||||
}
|
||||
}
|
||||
else {
|
||||
// See if there is Json data containing "error".
|
||||
data = reply->readAll();
|
||||
QString error;
|
||||
QJsonParseError json_error;
|
||||
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
|
||||
int 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("error"_L1) && json_obj["error"_L1].isObject()) {
|
||||
QJsonObject obj_error = json_obj["error"_L1].toObject();
|
||||
if (!obj_error.isEmpty() && obj_error.contains("status"_L1) && obj_error.contains("message"_L1)) {
|
||||
status = obj_error["status"_L1].toInt();
|
||||
QString user_message = obj_error["message"_L1].toString();
|
||||
error = QStringLiteral("%1 (%2)").arg(user_message).arg(status);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (error.isEmpty()) {
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
error = QStringLiteral("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt());
|
||||
}
|
||||
else {
|
||||
error = QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error());
|
||||
}
|
||||
}
|
||||
Error(error);
|
||||
result.error_code = ErrorCode::ParseError;
|
||||
result.error_message = json_parse_error.errorString();
|
||||
}
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
QJsonObject SpotifyBaseRequest::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 SpotifyBaseRequest::ExtractItems(const QByteArray &data) {
|
||||
|
||||
QJsonObject json_obj = ExtractJsonObj(data);
|
||||
if (json_obj.isEmpty()) return QJsonValue();
|
||||
return ExtractItems(json_obj);
|
||||
|
||||
}
|
||||
|
||||
QJsonValue SpotifyBaseRequest::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 SpotifyBaseRequest::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.http_status_code > 207) {
|
||||
result.error_code = ErrorCode::HttpError;
|
||||
result.error_message = QStringLiteral("Received HTTP code %1").arg(result.http_status_code);
|
||||
}
|
||||
}
|
||||
|
||||
if (reply->error() == QNetworkReply::AuthenticationRequiredError) {
|
||||
service_->ClearSession();
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2022-2024, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2022-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,28 +22,16 @@
|
||||
|
||||
#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 "spotifyservice.h"
|
||||
#include "core/jsonbaserequest.h"
|
||||
|
||||
class QNetworkReply;
|
||||
class NetworkAccessManager;
|
||||
class SpotifyService;
|
||||
|
||||
class SpotifyBaseRequest : public QObject {
|
||||
class SpotifyBaseRequest : public JsonBaseRequest {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
@@ -61,32 +49,17 @@ class SpotifyBaseRequest : 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);
|
||||
JsonObjectResult ParseJsonObject(QNetworkReply *reply);
|
||||
|
||||
virtual void Error(const QString &error, const QVariant &debug = QVariant()) = 0;
|
||||
static QString ErrorsToHTML(const QStringList &errors);
|
||||
|
||||
int artistssearchlimit() const { return service_->artistssearchlimit(); }
|
||||
int albumssearchlimit() const { return service_->albumssearchlimit(); }
|
||||
int songssearchlimit() const { return service_->songssearchlimit(); }
|
||||
|
||||
QString access_token() const { return service_->access_token(); }
|
||||
|
||||
bool authenticated() const { return service_->authenticated(); }
|
||||
|
||||
private Q_SLOTS:
|
||||
void HandleSSLErrors(const QList<QSslError> &ssl_errors);
|
||||
|
||||
private:
|
||||
protected:
|
||||
SpotifyService *service_;
|
||||
const SharedPtr<NetworkAccessManager> network_;
|
||||
};
|
||||
|
||||
#endif // SPOTIFYBASEREQUEST_H
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2022-2024, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2022-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,12 +19,6 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QPair>
|
||||
#include <QList>
|
||||
#include <QMap>
|
||||
#include <QMultiMap>
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
@@ -46,20 +40,7 @@
|
||||
using namespace Qt::Literals::StringLiterals;
|
||||
|
||||
SpotifyFavoriteRequest::SpotifyFavoriteRequest(SpotifyService *service, const SharedPtr<NetworkAccessManager> network, QObject *parent)
|
||||
: SpotifyBaseRequest(service, network, parent),
|
||||
service_(service),
|
||||
network_(network) {}
|
||||
|
||||
SpotifyFavoriteRequest::~SpotifyFavoriteRequest() {
|
||||
|
||||
while (!replies_.isEmpty()) {
|
||||
QNetworkReply *reply = replies_.takeFirst();
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
reply->abort();
|
||||
reply->deleteLater();
|
||||
}
|
||||
|
||||
}
|
||||
: SpotifyBaseRequest(service, network, parent) {}
|
||||
|
||||
QString SpotifyFavoriteRequest::FavoriteText(const FavoriteType type) {
|
||||
|
||||
@@ -121,8 +102,8 @@ void SpotifyFavoriteRequest::AddFavorites(const FavoriteType type, const SongLis
|
||||
|
||||
if (list_ids.isEmpty() || array_ids.isEmpty()) return;
|
||||
|
||||
QByteArray json_data = QJsonDocument(array_ids).toJson();
|
||||
QString ids_list = list_ids.join(u',');
|
||||
const QByteArray json_data = QJsonDocument(array_ids).toJson();
|
||||
const QString ids_list = list_ids.join(u',');
|
||||
|
||||
AddFavoritesRequest(type, ids_list, json_data, songs);
|
||||
|
||||
@@ -137,16 +118,18 @@ void SpotifyFavoriteRequest::AddFavoritesRequest(const FavoriteType type, const
|
||||
url_query.addQueryItem(u"ids"_s, ids_list);
|
||||
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 (!access_token().isEmpty()) req.setRawHeader("authorization", "Bearer " + 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 (service_->authenticated()) {
|
||||
network_request.setRawHeader("Authorization", service_->authorization_header());
|
||||
}
|
||||
QNetworkReply *reply = nullptr;
|
||||
if (type == FavoriteType_Artists) {
|
||||
reply = network_->put(req, "");
|
||||
reply = network_->put(network_request, "");
|
||||
}
|
||||
else {
|
||||
reply = network_->put(req, json_data);
|
||||
reply = network_->put(network_request, json_data);
|
||||
}
|
||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, type, songs]() { AddFavoritesReply(reply, type, songs); });
|
||||
replies_ << reply;
|
||||
@@ -160,8 +143,9 @@ void SpotifyFavoriteRequest::AddFavoritesReply(QNetworkReply *reply, const Favor
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
reply->deleteLater();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -233,8 +217,8 @@ void SpotifyFavoriteRequest::RemoveFavorites(const FavoriteType type, const Song
|
||||
|
||||
if (list_ids.isEmpty() || array_ids.isEmpty()) return;
|
||||
|
||||
QByteArray json_data = QJsonDocument(array_ids).toJson();
|
||||
QString ids_list = list_ids.join(u',');
|
||||
const QByteArray json_data = QJsonDocument(array_ids).toJson();
|
||||
const QString ids_list = list_ids.join(u',');
|
||||
|
||||
RemoveFavoritesRequest(type, ids_list, json_data, songs);
|
||||
|
||||
@@ -251,17 +235,19 @@ void SpotifyFavoriteRequest::RemoveFavoritesRequest(const FavoriteType type, con
|
||||
url_query.addQueryItem(u"ids"_s, ids_list);
|
||||
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 (!access_token().isEmpty()) req.setRawHeader("authorization", "Bearer " + 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 (service_->authenticated()) {
|
||||
network_request.setRawHeader("Authorization", service_->authorization_header());
|
||||
}
|
||||
QNetworkReply *reply = nullptr;
|
||||
if (type == FavoriteType_Artists) {
|
||||
reply = network_->deleteResource(req);
|
||||
reply = network_->deleteResource(network_request);
|
||||
}
|
||||
else {
|
||||
// FIXME
|
||||
reply = network_->deleteResource(req);
|
||||
reply = network_->deleteResource(network_request);
|
||||
}
|
||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, type, songs]() { RemoveFavoritesReply(reply, type, songs); });
|
||||
replies_ << reply;
|
||||
@@ -275,8 +261,9 @@ void SpotifyFavoriteRequest::RemoveFavoritesReply(QNetworkReply *reply, const Fa
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
reply->deleteLater();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -300,10 +287,3 @@ void SpotifyFavoriteRequest::RemoveFavoritesReply(QNetworkReply *reply, const Fa
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SpotifyFavoriteRequest::Error(const QString &error, const QVariant &debug) {
|
||||
|
||||
qLog(Error) << "Spotify:" << error;
|
||||
if (debug.isValid()) qLog(Debug) << debug;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2022-2024, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2022-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
|
||||
@@ -43,7 +43,6 @@ class SpotifyFavoriteRequest : public SpotifyBaseRequest {
|
||||
|
||||
public:
|
||||
explicit SpotifyFavoriteRequest(SpotifyService *service, const SharedPtr<NetworkAccessManager> network, QObject *parent = nullptr);
|
||||
~SpotifyFavoriteRequest() override;
|
||||
|
||||
enum FavoriteType {
|
||||
FavoriteType_Artists,
|
||||
@@ -75,18 +74,12 @@ class SpotifyFavoriteRequest : public SpotifyBaseRequest {
|
||||
void RemoveSongs(const SongMap &songs);
|
||||
|
||||
private:
|
||||
void Error(const QString &error, const QVariant &debug = QVariant()) override;
|
||||
static QString FavoriteText(const FavoriteType type);
|
||||
void AddFavorites(const FavoriteType type, const SongList &songs);
|
||||
void AddFavoritesRequest(const FavoriteType type, const QString &ids_list, const QByteArray &json_data, const SongList &songs);
|
||||
void RemoveFavorites(const FavoriteType type, const SongList &songs);
|
||||
void RemoveFavorites(const FavoriteType type, const QString &id, const SongList &songs);
|
||||
void RemoveFavoritesRequest(const FavoriteType type, const QString &ids_list, const QByteArray &json_data, const SongList &songs);
|
||||
|
||||
SpotifyService *service_;
|
||||
const SharedPtr<NetworkAccessManager> network_;
|
||||
QList <QNetworkReply*> replies_;
|
||||
|
||||
};
|
||||
|
||||
#endif // SPOTIFYFAVORITEREQUEST_H
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2022-2024, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2022-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 <QByteArray>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
@@ -33,13 +32,14 @@
|
||||
#include <QJsonArray>
|
||||
#include <QJsonValue>
|
||||
#include <QTimer>
|
||||
#include <QScopeGuard>
|
||||
|
||||
#include "core/logging.h"
|
||||
#include "core/networkaccessmanager.h"
|
||||
#include "core/song.h"
|
||||
#include "constants/timeconstants.h"
|
||||
#include "utilities/imageutils.h"
|
||||
#include "utilities/coverutils.h"
|
||||
#include "core/logging.h"
|
||||
#include "core/networkaccessmanager.h"
|
||||
#include "core/song.h"
|
||||
#include "spotifyservice.h"
|
||||
#include "spotifybaserequest.h"
|
||||
#include "spotifyrequest.h"
|
||||
@@ -47,18 +47,17 @@
|
||||
using namespace Qt::Literals::StringLiterals;
|
||||
|
||||
namespace {
|
||||
const int kMaxConcurrentArtistsRequests = 1;
|
||||
const int kMaxConcurrentAlbumsRequests = 1;
|
||||
const int kMaxConcurrentSongsRequests = 1;
|
||||
const int kMaxConcurrentArtistAlbumsRequests = 1;
|
||||
const int kMaxConcurrentAlbumSongsRequests = 1;
|
||||
const int kMaxConcurrentAlbumCoverRequests = 10;
|
||||
const int kFlushRequestsDelay = 200;
|
||||
}
|
||||
constexpr int kMaxConcurrentArtistsRequests = 1;
|
||||
constexpr int kMaxConcurrentAlbumsRequests = 1;
|
||||
constexpr int kMaxConcurrentSongsRequests = 1;
|
||||
constexpr int kMaxConcurrentArtistAlbumsRequests = 1;
|
||||
constexpr int kMaxConcurrentAlbumSongsRequests = 1;
|
||||
constexpr int kMaxConcurrentAlbumCoverRequests = 10;
|
||||
constexpr int kFlushRequestsDelay = 200;
|
||||
} // namespace
|
||||
|
||||
SpotifyRequest::SpotifyRequest(SpotifyService *service, const SharedPtr<NetworkAccessManager> network, const Type type, QObject *parent)
|
||||
: SpotifyBaseRequest(service, network, parent),
|
||||
service_(service),
|
||||
network_(network),
|
||||
timer_flush_requests_(new QTimer(this)),
|
||||
type_(type),
|
||||
@@ -107,20 +106,6 @@ SpotifyRequest::~SpotifyRequest() {
|
||||
timer_flush_requests_->stop();
|
||||
}
|
||||
|
||||
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 SpotifyRequest::Process() {
|
||||
@@ -232,7 +217,7 @@ void SpotifyRequest::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 = ParamList() << Param(u"type"_s, u"artist"_s);
|
||||
if (type_ == Type::SearchArtists) {
|
||||
@@ -252,7 +237,6 @@ void SpotifyRequest::FlushArtistsRequests() {
|
||||
reply = CreateRequest(u"search"_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_;
|
||||
@@ -286,7 +270,7 @@ void SpotifyRequest::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 (type_ == Type::SearchAlbums) {
|
||||
@@ -306,7 +290,6 @@ void SpotifyRequest::FlushAlbumsRequests() {
|
||||
reply = CreateRequest(u"search"_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_;
|
||||
@@ -340,7 +323,7 @@ void SpotifyRequest::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 (type_ == Type::SearchSongs) {
|
||||
@@ -361,7 +344,6 @@ void SpotifyRequest::FlushSongsRequests() {
|
||||
reply = CreateRequest(u"search"_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_;
|
||||
@@ -419,57 +401,55 @@ void SpotifyRequest::ArtistsReplyReceived(QNetworkReply *reply, const int limit_
|
||||
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();
|
||||
int offset = 0;
|
||||
int artists_received = 0;
|
||||
const QScopeGuard finish_check = qScopeGuard([this, limit_requested, &offset, &artists_received]() { ArtistsFinishCheck(limit_requested, offset, artists_received); });
|
||||
|
||||
if (!json_object_result.success()) {
|
||||
Error(json_object_result.error_message);
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject json_obj = ExtractJsonObj(data);
|
||||
if (json_obj.isEmpty()) {
|
||||
ArtistsFinishCheck();
|
||||
const QJsonObject &json_object = json_object_result.json_object;
|
||||
if (json_object.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!json_obj.contains("artists"_L1) || !json_obj["artists"_L1].isObject()) {
|
||||
Error(u"Json object missing values."_s, json_obj);
|
||||
ArtistsFinishCheck();
|
||||
if (!json_object.contains("artists"_L1) || !json_object["artists"_L1].isObject()) {
|
||||
Error(u"Json object missing values."_s, json_object);
|
||||
return;
|
||||
}
|
||||
QJsonObject obj_artists = json_obj["artists"_L1].toObject();
|
||||
const QJsonObject obj_artists = json_object["artists"_L1].toObject();
|
||||
|
||||
if (!obj_artists.contains("limit"_L1) ||
|
||||
!obj_artists.contains("total"_L1) ||
|
||||
!obj_artists.contains("items"_L1)) {
|
||||
Error(u"Json object missing values."_s, obj_artists);
|
||||
ArtistsFinishCheck();
|
||||
return;
|
||||
}
|
||||
|
||||
int offset = 0;
|
||||
if (obj_artists.contains("offset"_L1)) {
|
||||
offset = obj_artists["offset"_L1].toInt();
|
||||
}
|
||||
int artists_total = obj_artists["total"_L1].toInt();
|
||||
const int artists_total = obj_artists["total"_L1].toInt();
|
||||
|
||||
if (offset_requested == 0) {
|
||||
artists_total_ = artists_total;
|
||||
}
|
||||
else if (artists_total != artists_total_) {
|
||||
Error(QStringLiteral("Total returned does not match previous total! %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;
|
||||
}
|
||||
|
||||
@@ -477,20 +457,18 @@ void SpotifyRequest::ArtistsReplyReceived(QNetworkReply *reply, const int limit_
|
||||
Q_EMIT UpdateProgress(query_id_, GetProgress(artists_received_, artists_total_));
|
||||
}
|
||||
|
||||
QJsonValue value_items = ExtractItems(obj_artists);
|
||||
if (!value_items.isArray()) {
|
||||
ArtistsFinishCheck();
|
||||
const JsonArrayResult json_array_result = GetJsonArray(obj_artists, 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
|
||||
if (offset_requested == 0) no_results_ = true;
|
||||
ArtistsFinishCheck();
|
||||
return;
|
||||
}
|
||||
|
||||
int artists_received = 0;
|
||||
for (const QJsonValue &value_item : array_items) {
|
||||
|
||||
++artists_received;
|
||||
@@ -499,24 +477,24 @@ void SpotifyRequest::ArtistsReplyReceived(QNetworkReply *reply, const int limit_
|
||||
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)) {
|
||||
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;
|
||||
}
|
||||
|
||||
QString artist_id = obj_item["id"_L1].toString();
|
||||
QString artist = obj_item["name"_L1].toString();
|
||||
const QString artist_id = object_item["id"_L1].toString();
|
||||
const QString artist = object_item["name"_L1].toString();
|
||||
|
||||
if (artist_albums_requests_pending_.contains(artist_id)) continue;
|
||||
|
||||
@@ -530,15 +508,13 @@ void SpotifyRequest::ArtistsReplyReceived(QNetworkReply *reply, const int limit_
|
||||
|
||||
if (offset_requested != 0) Q_EMIT UpdateProgress(query_id_, GetProgress(artists_total_, artists_received_));
|
||||
|
||||
ArtistsFinishCheck(limit_requested, offset, artists_received);
|
||||
|
||||
}
|
||||
|
||||
void SpotifyRequest::ArtistsFinishCheck(const int limit, const int offset, const int artists_received) {
|
||||
|
||||
if (finished_) return;
|
||||
|
||||
if ((limit == 0 || limit > artists_received) && artists_received_ < artists_total_) {
|
||||
if (artists_received > 0 && (limit == 0 || limit > artists_received) && artists_received_ < artists_total_) {
|
||||
int offset_next = offset + artists_received;
|
||||
if (offset_next > 0 && offset_next < artists_total_) {
|
||||
if (type_ == Type::FavouriteArtists) AddArtistsRequest(offset_next);
|
||||
@@ -592,13 +568,12 @@ void SpotifyRequest::FlushArtistAlbumsRequests() {
|
||||
|
||||
while (!artist_albums_requests_queue_.isEmpty() && artist_albums_requests_active_ < kMaxConcurrentArtistAlbumsRequests) {
|
||||
|
||||
ArtistAlbumsRequest request = artist_albums_requests_queue_.dequeue();
|
||||
const ArtistAlbumsRequest request = artist_albums_requests_queue_.dequeue();
|
||||
|
||||
ParamList parameters;
|
||||
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_;
|
||||
|
||||
@@ -622,40 +597,43 @@ void SpotifyRequest::AlbumsReceived(QNetworkReply *reply, const Artist &artist_a
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
reply->deleteLater();
|
||||
|
||||
QByteArray data = GetReplyData(reply);
|
||||
JsonObjectResult json_object_result = ParseJsonObject(reply);
|
||||
|
||||
if (finished_) return;
|
||||
|
||||
if (data.isEmpty()) {
|
||||
AlbumsFinishCheck(artist_artist);
|
||||
int offset = 0;
|
||||
int albums_total = 0;
|
||||
int albums_received = 0;
|
||||
const QScopeGuard finish_check = qScopeGuard([this, artist_artist, limit_requested, &offset, &albums_total, &albums_received]() { AlbumsFinishCheck(artist_artist, 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_artist);
|
||||
QJsonObject json_object = json_object_result.json_object;
|
||||
if (json_object.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (json_obj.contains("albums"_L1) && json_obj["albums"_L1].isObject()) {
|
||||
json_obj = json_obj["albums"_L1].toObject();
|
||||
if (json_object.contains("albums"_L1) && json_object["albums"_L1].isObject()) {
|
||||
json_object = json_object["albums"_L1].toObject();
|
||||
}
|
||||
|
||||
if (json_obj.contains("tracks"_L1) && json_obj["tracks"_L1].isObject()) {
|
||||
json_obj = json_obj["tracks"_L1].toObject();
|
||||
if (json_object.contains("tracks"_L1) && json_object["tracks"_L1].isObject()) {
|
||||
json_object = json_object["tracks"_L1].toObject();
|
||||
}
|
||||
|
||||
if (!json_obj.contains("limit"_L1) ||
|
||||
!json_obj.contains("offset"_L1) ||
|
||||
!json_obj.contains("total"_L1) ||
|
||||
!json_obj.contains("items"_L1)) {
|
||||
Error(u"Json object missing values."_s, json_obj);
|
||||
AlbumsFinishCheck(artist_artist);
|
||||
if (!json_object.contains("limit"_L1) ||
|
||||
!json_object.contains("offset"_L1) ||
|
||||
!json_object.contains("total"_L1) ||
|
||||
!json_object.contains("items"_L1)) {
|
||||
Error(u"Json object missing values."_s, json_object);
|
||||
return;
|
||||
}
|
||||
|
||||
int offset = json_obj["offset"_L1].toInt();
|
||||
int albums_total = json_obj["total"_L1].toInt();
|
||||
offset = json_object["offset"_L1].toInt();
|
||||
albums_total = json_object["total"_L1].toInt();
|
||||
|
||||
if (type_ == Type::FavouriteAlbums || type_ == Type::SearchAlbums) {
|
||||
albums_total_ = albums_total;
|
||||
@@ -663,73 +641,70 @@ void SpotifyRequest::AlbumsReceived(QNetworkReply *reply, const Artist &artist_a
|
||||
|
||||
if (offset != offset_requested) {
|
||||
Error(QStringLiteral("Offset returned does not match offset requested! %1 != %2").arg(offset).arg(offset_requested));
|
||||
AlbumsFinishCheck(artist_artist);
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonValue value_items = ExtractItems(json_obj);
|
||||
if (!value_items.isArray()) {
|
||||
AlbumsFinishCheck(artist_artist);
|
||||
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()) {
|
||||
if ((type_ == Type::FavouriteAlbums || type_ == Type::SearchAlbums || (type_ == Type::SearchSongs && fetchalbums_)) && offset_requested == 0) {
|
||||
no_results_ = true;
|
||||
}
|
||||
AlbumsFinishCheck(artist_artist);
|
||||
return;
|
||||
}
|
||||
|
||||
int albums_received = 0;
|
||||
for (const QJsonValue &value_item : array_items) {
|
||||
albums_received = static_cast<int>(array_items.count());
|
||||
|
||||
++albums_received;
|
||||
for (const QJsonValue &value_item : array_items) {
|
||||
|
||||
if (!value_item.isObject()) {
|
||||
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)) {
|
||||
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("album"_L1)) {
|
||||
QJsonValue json_item = obj_item["album"_L1];
|
||||
if (object_item.contains("album"_L1)) {
|
||||
QJsonValue json_item = object_item["album"_L1];
|
||||
if (!json_item.isObject()) {
|
||||
Error(u"Invalid Json reply, album in array is not a object."_s, json_item);
|
||||
continue;
|
||||
}
|
||||
obj_item = json_item.toObject();
|
||||
object_item = json_item.toObject();
|
||||
}
|
||||
|
||||
Artist artist;
|
||||
Album album;
|
||||
|
||||
if (!obj_item.contains("id"_L1)) {
|
||||
Error(u"Invalid Json reply, item is missing ID."_s, obj_item);
|
||||
if (!object_item.contains("id"_L1)) {
|
||||
Error(u"Invalid Json reply, item is missing ID."_s, object_item);
|
||||
continue;
|
||||
}
|
||||
if (!obj_item.contains("name"_L1)) {
|
||||
Error(u"Invalid Json reply, item is missing name."_s, obj_item);
|
||||
if (!object_item.contains("name"_L1)) {
|
||||
Error(u"Invalid Json reply, item is missing name."_s, object_item);
|
||||
continue;
|
||||
}
|
||||
if (!obj_item.contains("images"_L1)) {
|
||||
Error(u"Invalid Json reply, item is missing images."_s, obj_item);
|
||||
if (!object_item.contains("images"_L1)) {
|
||||
Error(u"Invalid Json reply, item is missing images."_s, object_item);
|
||||
continue;
|
||||
}
|
||||
album.album_id = obj_item["id"_L1].toString();
|
||||
album.album = obj_item["name"_L1].toString();
|
||||
album.album_id = object_item["id"_L1].toString();
|
||||
album.album = object_item["name"_L1].toString();
|
||||
|
||||
if (obj_item.contains("artists"_L1) && obj_item["artists"_L1].isArray()) {
|
||||
const QJsonArray array_artists = obj_item["artists"_L1].toArray();
|
||||
if (object_item.contains("artists"_L1) && object_item["artists"_L1].isArray()) {
|
||||
const QJsonArray array_artists = object_item["artists"_L1].toArray();
|
||||
bool artist_matches = false;
|
||||
for (const QJsonValue &value : array_artists) {
|
||||
if (!value.isObject()) {
|
||||
@@ -747,7 +722,6 @@ void SpotifyRequest::AlbumsReceived(QNetworkReply *reply, const Artist &artist_a
|
||||
}
|
||||
}
|
||||
if (!artist_matches && (type_ == Type::FavouriteArtists || type_ == Type::SearchArtists)) {
|
||||
AlbumsFinishCheck(artist_artist);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -756,8 +730,8 @@ void SpotifyRequest::AlbumsReceived(QNetworkReply *reply, const Artist &artist_a
|
||||
artist = artist_artist;
|
||||
}
|
||||
|
||||
if (obj_item.contains("images"_L1) && obj_item["images"_L1].isArray()) {
|
||||
const QJsonArray array_images = obj_item["images"_L1].toArray();
|
||||
if (object_item.contains("images"_L1) && object_item["images"_L1].isArray()) {
|
||||
const QJsonArray array_images = object_item["images"_L1].toArray();
|
||||
for (const QJsonValue &value : array_images) {
|
||||
if (!value.isObject()) {
|
||||
continue;
|
||||
@@ -773,8 +747,8 @@ void SpotifyRequest::AlbumsReceived(QNetworkReply *reply, const Artist &artist_a
|
||||
}
|
||||
}
|
||||
|
||||
if (obj_item.contains("tracks"_L1) && obj_item["tracks"_L1].isObject()) {
|
||||
QJsonObject obj_tracks = obj_item["tracks"_L1].toObject();
|
||||
if (object_item.contains("tracks"_L1) && object_item["tracks"_L1].isObject()) {
|
||||
QJsonObject obj_tracks = object_item["tracks"_L1].toObject();
|
||||
if (obj_tracks.contains("items"_L1) && obj_tracks["items"_L1].isArray()) {
|
||||
const QJsonArray array_tracks = obj_tracks["items"_L1].toArray();
|
||||
bool compilation = false;
|
||||
@@ -816,15 +790,13 @@ void SpotifyRequest::AlbumsReceived(QNetworkReply *reply, const Artist &artist_a
|
||||
Q_EMIT UpdateProgress(query_id_, GetProgress(albums_received_, albums_total_));
|
||||
}
|
||||
|
||||
AlbumsFinishCheck(artist_artist, limit_requested, offset, albums_total, albums_received);
|
||||
|
||||
}
|
||||
|
||||
void SpotifyRequest::AlbumsFinishCheck(const Artist &artist, const int limit, const int offset, const int albums_total, const int albums_received) {
|
||||
|
||||
if (finished_) return;
|
||||
|
||||
if (limit == 0 || limit > albums_received) {
|
||||
if (albums_received > 0 && (limit == 0 || limit > albums_received)) {
|
||||
int offset_next = offset + albums_received;
|
||||
if (offset_next > 0 && offset_next < albums_total) {
|
||||
switch (type_) {
|
||||
@@ -904,15 +876,12 @@ void SpotifyRequest::AddAlbumSongsRequest(const Artist &artist, const Album &alb
|
||||
void SpotifyRequest::FlushAlbumSongsRequests() {
|
||||
|
||||
while (!album_songs_requests_queue_.isEmpty() && album_songs_requests_active_ < kMaxConcurrentAlbumSongsRequests) {
|
||||
|
||||
AlbumSongsRequest request = album_songs_requests_queue_.dequeue();
|
||||
const AlbumSongsRequest request = album_songs_requests_queue_.dequeue();
|
||||
++album_songs_requests_active_;
|
||||
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); });
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -935,36 +904,38 @@ void SpotifyRequest::SongsReceived(QNetworkReply *reply, const Artist &artist, c
|
||||
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, 0, 0);
|
||||
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, 0, 0);
|
||||
QJsonObject json_object = json_object_result.json_object;
|
||||
if (json_object.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (json_obj.contains("tracks"_L1) && json_obj["tracks"_L1].isObject()) {
|
||||
json_obj = json_obj["tracks"_L1].toObject();
|
||||
if (json_object.contains("tracks"_L1) && json_object["tracks"_L1].isObject()) {
|
||||
json_object = json_object["tracks"_L1].toObject();
|
||||
}
|
||||
|
||||
if (!json_obj.contains("limit"_L1) ||
|
||||
!json_obj.contains("offset"_L1) ||
|
||||
!json_obj.contains("total"_L1) ||
|
||||
!json_obj.contains("items"_L1)) {
|
||||
Error(u"Json object missing values."_s, json_obj);
|
||||
SongsFinishCheck(artist, album, limit_requested, offset_requested, 0, 0);
|
||||
if (!json_object.contains("limit"_L1) ||
|
||||
!json_object.contains("offset"_L1) ||
|
||||
!json_object.contains("total"_L1) ||
|
||||
!json_object.contains("items"_L1)) {
|
||||
Error(u"Json object missing values."_s, json_object);
|
||||
return;
|
||||
}
|
||||
|
||||
int offset = json_obj["offset"_L1].toInt();
|
||||
int songs_total = json_obj["total"_L1].toInt();
|
||||
const int offset = json_object["offset"_L1].toInt();
|
||||
songs_total = json_object["total"_L1].toInt();
|
||||
|
||||
if (type_ == Type::FavouriteSongs || type_ == Type::SearchSongs) {
|
||||
songs_total_ = songs_total;
|
||||
@@ -972,48 +943,45 @@ void SpotifyRequest::SongsReceived(QNetworkReply *reply, const Artist &artist, c
|
||||
|
||||
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()) {
|
||||
if ((type_ == Type::FavouriteSongs || type_ == Type::SearchSongs) && offset_requested == 0) {
|
||||
no_results_ = true;
|
||||
}
|
||||
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) && obj_item["item"_L1].isObject()) {
|
||||
obj_item = obj_item["item"_L1].toObject();
|
||||
if (object_item.contains("item"_L1) && object_item["item"_L1].isObject()) {
|
||||
object_item = object_item["item"_L1].toObject();
|
||||
}
|
||||
|
||||
if (obj_item.contains("track"_L1) && obj_item["track"_L1].isObject()) {
|
||||
obj_item = obj_item["track"_L1].toObject();
|
||||
if (object_item.contains("track"_L1) && object_item["track"_L1].isObject()) {
|
||||
object_item = object_item["track"_L1].toObject();
|
||||
}
|
||||
|
||||
++songs_received;
|
||||
Song song(Song::Source::Spotify);
|
||||
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;
|
||||
@@ -1031,15 +999,13 @@ void SpotifyRequest::SongsReceived(QNetworkReply *reply, const Artist &artist, c
|
||||
Q_EMIT UpdateProgress(query_id_, GetProgress(songs_received_, songs_total_));
|
||||
}
|
||||
|
||||
SongsFinishCheck(artist, album, limit_requested, offset_requested, songs_total, songs_received);
|
||||
|
||||
}
|
||||
|
||||
void SpotifyRequest::SongsFinishCheck(const Artist &artist, const Album &album, const int limit, const int offset, const int songs_total, const int songs_received) {
|
||||
|
||||
if (finished_) return;
|
||||
|
||||
if (limit == 0 || limit > songs_received) {
|
||||
if (songs_received > 0 && (limit == 0 || limit > songs_received)) {
|
||||
int offset_next = offset + songs_received;
|
||||
if (offset_next > 0 && offset_next < songs_total) {
|
||||
switch (type_) {
|
||||
@@ -1240,25 +1206,18 @@ void SpotifyRequest::AddAlbumCoverRequest(const Song &song) {
|
||||
void SpotifyRequest::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;
|
||||
const AlbumCoverRequest request = album_cover_requests_queue_.dequeue();
|
||||
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_;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SpotifyRequest::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();
|
||||
}
|
||||
@@ -1272,24 +1231,23 @@ void SpotifyRequest::AlbumCoverReceived(QNetworkReply *reply, const QString &alb
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1300,7 +1258,6 @@ void SpotifyRequest::AlbumCoverReceived(QNetworkReply *reply, const QString &alb
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1308,7 +1265,6 @@ void SpotifyRequest::AlbumCoverReceived(QNetworkReply *reply, const QString &alb
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1338,8 +1294,6 @@ void SpotifyRequest::AlbumCoverReceived(QNetworkReply *reply, const QString &alb
|
||||
Error(QStringLiteral("Error decoding image data from %1").arg(url.toString()));
|
||||
}
|
||||
|
||||
AlbumCoverFinishCheck();
|
||||
|
||||
}
|
||||
|
||||
void SpotifyRequest::AlbumCoverFinishCheck() {
|
||||
@@ -1373,17 +1327,19 @@ void SpotifyRequest::FinishCheck() {
|
||||
}
|
||||
finished_ = true;
|
||||
if (no_results_ && songs_.isEmpty()) {
|
||||
if (IsSearch())
|
||||
if (IsSearch()) {
|
||||
Q_EMIT Results(query_id_, SongMap(), tr("No match."));
|
||||
else
|
||||
}
|
||||
else {
|
||||
Q_EMIT Results(query_id_, SongMap(), QString());
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (songs_.isEmpty() && errors_.isEmpty()) {
|
||||
if (songs_.isEmpty() && error_.isEmpty()) {
|
||||
Q_EMIT Results(query_id_, songs_, tr("Data missing error"));
|
||||
}
|
||||
else {
|
||||
Q_EMIT Results(query_id_, songs_, ErrorsToHTML(errors_));
|
||||
Q_EMIT Results(query_id_, songs_, error_);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1396,22 +1352,20 @@ int SpotifyRequest::GetProgress(const int count, const int total) {
|
||||
|
||||
}
|
||||
|
||||
void SpotifyRequest::Error(const QString &error, const QVariant &debug) {
|
||||
void SpotifyRequest::Error(const QString &error_message, const QVariant &debug_output) {
|
||||
|
||||
if (!error.isEmpty()) {
|
||||
errors_ << error;
|
||||
qLog(Error) << "Spotify:" << error;
|
||||
qLog(Error) << "Spotify:" << error_message;
|
||||
if (debug_output.isValid()) {
|
||||
qLog(Debug) << debug_output;
|
||||
}
|
||||
|
||||
if (debug.isValid()) qLog(Debug) << debug;
|
||||
|
||||
FinishCheck();
|
||||
error_ = error_message;
|
||||
|
||||
}
|
||||
|
||||
void SpotifyRequest::Warn(const QString &error, const QVariant &debug) {
|
||||
void SpotifyRequest::Warn(const QString &error_message, const QVariant &debug) {
|
||||
|
||||
qLog(Error) << "Spotify:" << error;
|
||||
qLog(Error) << "Spotify:" << error_message;
|
||||
if (debug.isValid()) qLog(Debug) << debug;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2022-2024, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2022-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,11 +22,6 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QPair>
|
||||
#include <QSet>
|
||||
#include <QList>
|
||||
#include <QMap>
|
||||
#include <QMultiMap>
|
||||
#include <QQueue>
|
||||
@@ -36,6 +31,7 @@
|
||||
#include <QUrl>
|
||||
#include <QJsonObject>
|
||||
#include <QTimer>
|
||||
#include <QScopedPointer>
|
||||
|
||||
#include "includes/shared_ptr.h"
|
||||
#include "core/song.h"
|
||||
@@ -138,9 +134,9 @@ class SpotifyRequest : public SpotifyBaseRequest {
|
||||
void AddSongsSearchRequest(const int offset = 0);
|
||||
void FlushSongsRequests();
|
||||
|
||||
void ArtistsFinishCheck(const int limit = 0, const int offset = 0, const int artists_received = 0);
|
||||
void AlbumsFinishCheck(const Artist &artist, const int limit = 0, const int offset = 0, const int albums_total = 0, const int albums_received = 0);
|
||||
void SongsFinishCheck(const Artist &artist, const Album &album, const int limit = 0, const int offset = 0, const int songs_total = 0, const int songs_received = 0);
|
||||
void ArtistsFinishCheck(const int limit, const int offset, const int artists_received);
|
||||
void AlbumsFinishCheck(const Artist &artist, const int limit, const int offset, const int albums_total, const int albums_received);
|
||||
void SongsFinishCheck(const Artist &artist, const Album &album, const int limit, const int offset, const int songs_total, const int songs_received);
|
||||
|
||||
void AddArtistAlbumsRequest(const Artist &artist, const int offset = 0);
|
||||
void FlushArtistAlbumsRequests();
|
||||
@@ -158,11 +154,10 @@ class SpotifyRequest : public SpotifyBaseRequest {
|
||||
|
||||
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;
|
||||
void Error(const QString &error_message, const QVariant &debug_output = QVariant()) override;
|
||||
void Warn(const QString &error_message, const QVariant &debug);
|
||||
|
||||
private:
|
||||
SpotifyService *service_;
|
||||
const SharedPtr<NetworkAccessManager> network_;
|
||||
QTimer *timer_flush_requests_;
|
||||
|
||||
@@ -222,11 +217,10 @@ class SpotifyRequest : public SpotifyBaseRequest {
|
||||
int album_covers_requests_received_;
|
||||
|
||||
SongMap songs_;
|
||||
QStringList errors_;
|
||||
bool no_results_;
|
||||
QList<QNetworkReply*> replies_;
|
||||
QList<QNetworkReply*> album_cover_replies_;
|
||||
|
||||
QString error_;
|
||||
};
|
||||
|
||||
using SpotifyRequestPtr = QScopedPointer<SpotifyRequest, QScopedPointerDeleteLater>;
|
||||
|
||||
#endif // SPOTIFYREQUEST_H
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2022-2024, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2022-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,21 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <utility>
|
||||
#include <memory>
|
||||
#include <chrono>
|
||||
|
||||
#include <QObject>
|
||||
#include <QDesktopServices>
|
||||
#include <QCryptographicHash>
|
||||
#include <QByteArray>
|
||||
#include <QPair>
|
||||
#include <QList>
|
||||
#include <QMap>
|
||||
#include <QString>
|
||||
#include <QChar>
|
||||
#include <QUrl>
|
||||
#include <QUrlQuery>
|
||||
#include <QDateTime>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
#include <QSslError>
|
||||
#include <QTimer>
|
||||
#include <QJsonValue>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QSettings>
|
||||
#include <QMessageBox>
|
||||
|
||||
#include "constants/spotifysettings.h"
|
||||
#include "core/logging.h"
|
||||
#include "core/song.h"
|
||||
#include "core/settings.h"
|
||||
#include "core/taskmanager.h"
|
||||
#include "core/database.h"
|
||||
#include "core/networkaccessmanager.h"
|
||||
#include "core/localredirectserver.h"
|
||||
#include "constants/timeconstants.h"
|
||||
#include "utilities/randutils.h"
|
||||
#include "core/oauthenticator.h"
|
||||
#include "streaming/streamingsearchview.h"
|
||||
#include "collection/collectionbackend.h"
|
||||
#include "collection/collectionmodel.h"
|
||||
@@ -61,7 +41,6 @@
|
||||
#include "spotifybaserequest.h"
|
||||
#include "spotifyrequest.h"
|
||||
#include "spotifyfavoriterequest.h"
|
||||
#include "constants/spotifysettings.h"
|
||||
|
||||
using namespace Qt::Literals::StringLiterals;
|
||||
|
||||
@@ -73,9 +52,9 @@ namespace {
|
||||
constexpr char kOAuthAuthorizeUrl[] = "https://accounts.spotify.com/authorize";
|
||||
constexpr char kOAuthAccessTokenUrl[] = "https://accounts.spotify.com/api/token";
|
||||
constexpr char kOAuthRedirectUrl[] = "http://localhost:63111/";
|
||||
constexpr char kOAuthScope[] = "user-follow-read user-follow-modify user-library-read user-library-modify streaming";
|
||||
constexpr char kClientIDB64[] = "ZTZjY2Y2OTQ5NzY1NGE3NThjOTAxNWViYzdiMWQzMTc=";
|
||||
constexpr char kClientSecretB64[] = "N2ZlMDMxODk1NTBlNDE3ZGI1ZWQ1MzE3ZGZlZmU2MTE=";
|
||||
|
||||
constexpr char kArtistsSongsTable[] = "spotify_artists_songs";
|
||||
constexpr char kAlbumsSongsTable[] = "spotify_albums_songs";
|
||||
constexpr char kSongsTable[] = "spotify_songs";
|
||||
@@ -92,6 +71,7 @@ SpotifyService::SpotifyService(const SharedPtr<TaskManager> task_manager,
|
||||
QObject *parent)
|
||||
: StreamingService(Song::Source::Spotify, u"Spotify"_s, u"spotify"_s, QLatin1String(SpotifySettings::kSettingsGroup), parent),
|
||||
network_(network),
|
||||
oauth_(new OAuthenticator(network, this)),
|
||||
artists_collection_backend_(nullptr),
|
||||
albums_collection_backend_(nullptr),
|
||||
songs_collection_backend_(nullptr),
|
||||
@@ -99,7 +79,6 @@ SpotifyService::SpotifyService(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 SpotifyFavoriteRequest(this, network_, this)),
|
||||
enabled_(false),
|
||||
artistssearchlimit_(1),
|
||||
@@ -107,13 +86,22 @@ SpotifyService::SpotifyService(const SharedPtr<TaskManager> task_manager,
|
||||
songssearchlimit_(1),
|
||||
fetchalbums_(true),
|
||||
download_album_covers_(true),
|
||||
expires_in_(0),
|
||||
login_time_(0),
|
||||
pending_search_id_(0),
|
||||
next_pending_search_id_(1),
|
||||
pending_search_type_(SearchType::Artists),
|
||||
search_id_(0),
|
||||
server_(nullptr) {
|
||||
search_id_(0) {
|
||||
|
||||
oauth_->set_settings_group(QLatin1String(SpotifySettings::kSettingsGroup));
|
||||
oauth_->set_type(OAuthenticator::Type::Authorization_Code);
|
||||
oauth_->set_authorize_url(QUrl(QLatin1String(kOAuthAuthorizeUrl)));
|
||||
oauth_->set_redirect_url(QUrl(QLatin1String(kOAuthRedirectUrl)));
|
||||
oauth_->set_access_token_url(QUrl(QLatin1String(kOAuthAccessTokenUrl)));
|
||||
oauth_->set_client_id(QString::fromLatin1(QByteArray::fromBase64(kClientIDB64)));
|
||||
oauth_->set_client_secret(QString::fromLatin1(QByteArray::fromBase64(kClientSecretB64)));
|
||||
oauth_->set_scope(QLatin1String(kOAuthScope));
|
||||
oauth_->set_use_local_redirect_server(true);
|
||||
oauth_->set_random_port(false);
|
||||
QObject::connect(oauth_, &OAuthenticator::AuthenticationFinished, this, &SpotifyService::OAuthFinished);
|
||||
|
||||
// Backends
|
||||
|
||||
@@ -134,9 +122,6 @@ SpotifyService::SpotifyService(const SharedPtr<TaskManager> task_manager,
|
||||
albums_collection_model_ = new CollectionModel(albums_collection_backend_, albumcover_loader, this);
|
||||
songs_collection_model_ = new CollectionModel(songs_collection_backend_, albumcover_loader, this);
|
||||
|
||||
timer_refresh_login_->setSingleShot(true);
|
||||
QObject::connect(timer_refresh_login_, &QTimer::timeout, this, &SpotifyService::RequestNewAccessToken);
|
||||
|
||||
timer_search_delay_->setSingleShot(true);
|
||||
QObject::connect(timer_search_delay_, &QTimer::timeout, this, &SpotifyService::StartSearch);
|
||||
|
||||
@@ -158,19 +143,12 @@ SpotifyService::SpotifyService(const SharedPtr<TaskManager> task_manager,
|
||||
QObject::connect(favorite_request_, &SpotifyFavoriteRequest::SongsRemoved, &*songs_collection_backend_, &CollectionBackend::DeleteSongs);
|
||||
|
||||
SpotifyService::ReloadSettings();
|
||||
LoadSession();
|
||||
oauth_->LoadSession();
|
||||
|
||||
}
|
||||
|
||||
SpotifyService::~SpotifyService() {
|
||||
|
||||
while (!replies_.isEmpty()) {
|
||||
QNetworkReply *reply = replies_.takeFirst();
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
reply->abort();
|
||||
reply->deleteLater();
|
||||
}
|
||||
|
||||
artists_collection_backend_->deleteLater();
|
||||
albums_collection_backend_->deleteLater();
|
||||
songs_collection_backend_->deleteLater();
|
||||
@@ -201,25 +179,15 @@ void SpotifyService::ExitReceived() {
|
||||
|
||||
}
|
||||
|
||||
void SpotifyService::LoadSession() {
|
||||
bool SpotifyService::authenticated() const {
|
||||
|
||||
refresh_login_timer_.setSingleShot(true);
|
||||
QObject::connect(&refresh_login_timer_, &QTimer::timeout, this, &SpotifyService::RequestNewAccessToken);
|
||||
return oauth_->authenticated();
|
||||
|
||||
Settings s;
|
||||
s.beginGroup(SpotifySettings::kSettingsGroup);
|
||||
access_token_ = s.value(SpotifySettings::kAccessToken).toString();
|
||||
refresh_token_ = s.value(SpotifySettings::kRefreshToken).toString();
|
||||
expires_in_ = s.value(SpotifySettings::kExpiresIn).toLongLong();
|
||||
login_time_ = s.value(SpotifySettings::kLoginTime).toLongLong();
|
||||
s.endGroup();
|
||||
}
|
||||
|
||||
if (!refresh_token_.isEmpty()) {
|
||||
qint64 time = static_cast<qint64>(expires_in_) - (QDateTime::currentSecsSinceEpoch() - static_cast<qint64>(login_time_));
|
||||
if (time < 1) time = 1;
|
||||
refresh_login_timer_.setInterval(static_cast<int>(time * kMsecPerSec));
|
||||
refresh_login_timer_.start();
|
||||
}
|
||||
QByteArray SpotifyService::authorization_header() const {
|
||||
|
||||
return oauth_->authorization_header();
|
||||
|
||||
}
|
||||
|
||||
@@ -245,267 +213,25 @@ void SpotifyService::ReloadSettings() {
|
||||
|
||||
void SpotifyService::Authenticate() {
|
||||
|
||||
QUrl redirect_url(QString::fromLatin1(kOAuthRedirectUrl));
|
||||
|
||||
if (!server_) {
|
||||
server_ = new LocalRedirectServer(this);
|
||||
int port = redirect_url.port();
|
||||
int port_max = port + 10;
|
||||
bool success = false;
|
||||
Q_FOREVER {
|
||||
server_->set_port(port);
|
||||
if (server_->Listen()) {
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
++port;
|
||||
if (port > port_max) break;
|
||||
}
|
||||
if (!success) {
|
||||
LoginError(server_->error());
|
||||
server_->deleteLater();
|
||||
server_ = nullptr;
|
||||
return;
|
||||
}
|
||||
QObject::connect(server_, &LocalRedirectServer::Finished, this, &SpotifyService::RedirectArrived);
|
||||
}
|
||||
|
||||
code_verifier_ = Utilities::CryptographicRandomString(44);
|
||||
code_challenge_ = QString::fromLatin1(QCryptographicHash::hash(code_verifier_.toUtf8(), QCryptographicHash::Sha256).toBase64(QByteArray::Base64UrlEncoding));
|
||||
if (code_challenge_.lastIndexOf(u'=') == code_challenge_.length() - 1) {
|
||||
code_challenge_.chop(1);
|
||||
}
|
||||
|
||||
const ParamList params = ParamList() << Param(u"client_id"_s, QString::fromLatin1(QByteArray::fromBase64(kClientIDB64)))
|
||||
<< Param(u"response_type"_s, u"code"_s)
|
||||
<< Param(u"redirect_uri"_s, redirect_url.toString())
|
||||
<< Param(u"state"_s, code_challenge_)
|
||||
<< Param(u"scope"_s, u"user-follow-read user-follow-modify user-library-read user-library-modify streaming"_s);
|
||||
|
||||
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(QString::fromLatin1(kOAuthAuthorizeUrl));
|
||||
url.setQuery(url_query);
|
||||
|
||||
const bool result = QDesktopServices::openUrl(url);
|
||||
if (!result) {
|
||||
QMessageBox messagebox(QMessageBox::Information, tr("Spotify Authentication"), tr("Please open this URL in your browser") + QStringLiteral(":<br /><a href=\"%1\">%1</a>").arg(url.toString()), QMessageBox::Ok);
|
||||
messagebox.setTextFormat(Qt::RichText);
|
||||
messagebox.exec();
|
||||
}
|
||||
oauth_->Authenticate();
|
||||
|
||||
}
|
||||
|
||||
void SpotifyService::Deauthenticate() {
|
||||
void SpotifyService::ClearSession() {
|
||||
|
||||
access_token_.clear();
|
||||
refresh_token_.clear();
|
||||
expires_in_ = 0;
|
||||
login_time_ = 0;
|
||||
|
||||
Settings s;
|
||||
s.beginGroup(SpotifySettings::kSettingsGroup);
|
||||
s.remove(SpotifySettings::kAccessToken);
|
||||
s.remove(SpotifySettings::kRefreshToken);
|
||||
s.remove(SpotifySettings::kExpiresIn);
|
||||
s.remove(SpotifySettings::kLoginTime);
|
||||
s.endGroup();
|
||||
|
||||
refresh_login_timer_.stop();
|
||||
oauth_->ClearSession();
|
||||
|
||||
}
|
||||
|
||||
void SpotifyService::RedirectArrived() {
|
||||
void SpotifyService::OAuthFinished(const bool success, const QString &error) {
|
||||
|
||||
if (!server_) return;
|
||||
|
||||
if (server_->error().isEmpty()) {
|
||||
QUrl url = server_->request_url();
|
||||
if (url.isValid()) {
|
||||
QUrlQuery url_query(url);
|
||||
if (url_query.hasQueryItem(u"error"_s)) {
|
||||
LoginError(QUrlQuery(url).queryItemValue(u"error"_s));
|
||||
}
|
||||
else if (url_query.hasQueryItem(u"code"_s) && url_query.hasQueryItem(u"state"_s)) {
|
||||
qLog(Debug) << "Spotify: Authorization URL Received" << url;
|
||||
QString code = url_query.queryItemValue(u"code"_s);
|
||||
QUrl redirect_url(QString::fromLatin1(kOAuthRedirectUrl));
|
||||
redirect_url.setPort(server_->url().port());
|
||||
RequestAccessToken(code, redirect_url);
|
||||
}
|
||||
else {
|
||||
LoginError(tr("Redirect missing token code or state!"));
|
||||
}
|
||||
}
|
||||
else {
|
||||
LoginError(tr("Received invalid reply from web browser."));
|
||||
}
|
||||
if (success) {
|
||||
Q_EMIT LoginFinished(true);
|
||||
Q_EMIT LoginSuccess();
|
||||
}
|
||||
else {
|
||||
LoginError(server_->error());
|
||||
}
|
||||
|
||||
server_->close();
|
||||
server_->deleteLater();
|
||||
server_ = nullptr;
|
||||
|
||||
}
|
||||
|
||||
void SpotifyService::RequestAccessToken(const QString &code, const QUrl &redirect_url) {
|
||||
|
||||
refresh_login_timer_.stop();
|
||||
|
||||
ParamList params = ParamList() << Param(u"client_id"_s, QString::fromLatin1(QByteArray::fromBase64(kClientIDB64)))
|
||||
<< Param(u"client_secret"_s, QString::fromLatin1(QByteArray::fromBase64(kClientSecretB64)));
|
||||
|
||||
if (!code.isEmpty() && !redirect_url.isEmpty()) {
|
||||
params << Param(u"grant_type"_s, u"authorization_code"_s);
|
||||
params << Param(u"code"_s, code);
|
||||
params << Param(u"redirect_uri"_s, redirect_url.toString());
|
||||
}
|
||||
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 new_url(QString::fromLatin1(kOAuthAccessTokenUrl));
|
||||
QNetworkRequest req(new_url);
|
||||
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
req.setHeader(QNetworkRequest::ContentTypeHeader, u"application/x-www-form-urlencoded"_s);
|
||||
QString auth_header_data = QString::fromLatin1(QByteArray::fromBase64(kClientIDB64)) + QLatin1Char(':') + QString::fromLatin1(QByteArray::fromBase64(kClientSecretB64));
|
||||
req.setRawHeader("Authorization", "Basic " + auth_header_data.toUtf8().toBase64());
|
||||
|
||||
QByteArray query = url_query.toString(QUrl::FullyEncoded).toUtf8();
|
||||
|
||||
QNetworkReply *reply = network_->post(req, query);
|
||||
replies_ << reply;
|
||||
QObject::connect(reply, &QNetworkReply::sslErrors, this, &SpotifyService::HandleLoginSSLErrors);
|
||||
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply]() { AccessTokenRequestFinished(reply); });
|
||||
|
||||
}
|
||||
|
||||
void SpotifyService::HandleLoginSSLErrors(const QList<QSslError> &ssl_errors) {
|
||||
|
||||
for (const QSslError &ssl_error : ssl_errors) {
|
||||
login_errors_ += ssl_error.errorString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SpotifyService::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 "error" and "error_description" 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("error"_L1) && json_obj.contains("error_description"_L1)) {
|
||||
QString error = json_obj["error"_L1].toString();
|
||||
QString error_description = json_obj["error_description"_L1].toString();
|
||||
login_errors_ << QStringLiteral("Authentication failure: %1 (%2)").arg(error, error_description);
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
QByteArray data = reply->readAll();
|
||||
|
||||
QJsonParseError json_error;
|
||||
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
|
||||
|
||||
if (json_error.error != QJsonParseError::NoError) {
|
||||
LoginError(QStringLiteral("Failed to parse Json data in authentication reply: %1").arg(json_error.errorString()));
|
||||
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("access_token"_L1) || !json_obj.contains("expires_in"_L1)) {
|
||||
LoginError(u"Authentication reply from server is missing access token or expires in."_s, json_obj);
|
||||
return;
|
||||
}
|
||||
|
||||
access_token_ = json_obj["access_token"_L1].toString();
|
||||
if (json_obj.contains("refresh_token"_L1)) {
|
||||
refresh_token_ = json_obj["refresh_token"_L1].toString();
|
||||
}
|
||||
expires_in_ = json_obj["expires_in"_L1].toInt();
|
||||
login_time_ = QDateTime::currentSecsSinceEpoch();
|
||||
|
||||
Settings s;
|
||||
s.beginGroup(SpotifySettings::kSettingsGroup);
|
||||
s.setValue(SpotifySettings::kAccessToken, access_token_);
|
||||
s.setValue(SpotifySettings::kRefreshToken, refresh_token_);
|
||||
s.setValue(SpotifySettings::kExpiresIn, expires_in_);
|
||||
s.setValue(SpotifySettings::kLoginTime, login_time_);
|
||||
s.endGroup();
|
||||
|
||||
if (expires_in_ > 0) {
|
||||
refresh_login_timer_.setInterval(static_cast<int>(expires_in_ * kMsecPerSec));
|
||||
refresh_login_timer_.start();
|
||||
}
|
||||
|
||||
qLog(Debug) << "Spotify: Authentication was successful, login expires in" << expires_in_;
|
||||
|
||||
Q_EMIT LoginComplete(true);
|
||||
Q_EMIT LoginSuccess();
|
||||
|
||||
}
|
||||
|
||||
void SpotifyService::ResetArtistsRequest() {
|
||||
|
||||
if (artists_request_) {
|
||||
QObject::disconnect(&*artists_request_, nullptr, this, nullptr);
|
||||
QObject::disconnect(this, nullptr, &*artists_request_, nullptr);
|
||||
artists_request_.reset();
|
||||
Q_EMIT LoginFailure(error);
|
||||
Q_EMIT LoginFinished(false);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -518,8 +244,7 @@ void SpotifyService::GetArtists() {
|
||||
return;
|
||||
}
|
||||
|
||||
ResetArtistsRequest();
|
||||
artists_request_.reset(new SpotifyRequest(this, network_, SpotifyBaseRequest::Type::FavouriteArtists, this), [](SpotifyRequest *request) { request->deleteLater(); });
|
||||
artists_request_.reset(new SpotifyRequest(this, network_, SpotifyBaseRequest::Type::FavouriteArtists, this));
|
||||
QObject::connect(&*artists_request_, &SpotifyRequest::Results, this, &SpotifyService::ArtistsResultsReceived);
|
||||
QObject::connect(&*artists_request_, &SpotifyRequest::UpdateStatus, this, &SpotifyService::ArtistsUpdateStatusReceived);
|
||||
QObject::connect(&*artists_request_, &SpotifyRequest::ProgressSetMaximum, this, &SpotifyService::ArtistsProgressSetMaximumReceived);
|
||||
@@ -529,6 +254,12 @@ void SpotifyService::GetArtists() {
|
||||
|
||||
}
|
||||
|
||||
void SpotifyService::ResetArtistsRequest() {
|
||||
|
||||
artists_request_.reset();
|
||||
|
||||
}
|
||||
|
||||
void SpotifyService::ArtistsResultsReceived(const int id, const SongMap &songs, const QString &error) {
|
||||
|
||||
Q_UNUSED(id);
|
||||
@@ -552,16 +283,6 @@ void SpotifyService::ArtistsUpdateProgressReceived(const int id, const int progr
|
||||
Q_EMIT ArtistsUpdateProgress(progress);
|
||||
}
|
||||
|
||||
void SpotifyService::ResetAlbumsRequest() {
|
||||
|
||||
if (albums_request_) {
|
||||
QObject::disconnect(&*albums_request_, nullptr, this, nullptr);
|
||||
QObject::disconnect(this, nullptr, &*albums_request_, nullptr);
|
||||
albums_request_.reset();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SpotifyService::GetAlbums() {
|
||||
|
||||
if (!authenticated()) {
|
||||
@@ -570,8 +291,7 @@ void SpotifyService::GetAlbums() {
|
||||
return;
|
||||
}
|
||||
|
||||
ResetAlbumsRequest();
|
||||
albums_request_.reset(new SpotifyRequest(this, network_, SpotifyBaseRequest::Type::FavouriteAlbums, this), [](SpotifyRequest *request) { request->deleteLater(); });
|
||||
albums_request_.reset(new SpotifyRequest(this, network_, SpotifyBaseRequest::Type::FavouriteAlbums, this));
|
||||
QObject::connect(&*albums_request_, &SpotifyRequest::Results, this, &SpotifyService::AlbumsResultsReceived);
|
||||
QObject::connect(&*albums_request_, &SpotifyRequest::UpdateStatus, this, &SpotifyService::AlbumsUpdateStatusReceived);
|
||||
QObject::connect(&*albums_request_, &SpotifyRequest::ProgressSetMaximum, this, &SpotifyService::AlbumsProgressSetMaximumReceived);
|
||||
@@ -581,6 +301,12 @@ void SpotifyService::GetAlbums() {
|
||||
|
||||
}
|
||||
|
||||
void SpotifyService::ResetAlbumsRequest() {
|
||||
|
||||
albums_request_.reset();
|
||||
|
||||
}
|
||||
|
||||
void SpotifyService::AlbumsResultsReceived(const int id, const SongMap &songs, const QString &error) {
|
||||
|
||||
Q_UNUSED(id);
|
||||
@@ -604,16 +330,6 @@ void SpotifyService::AlbumsUpdateProgressReceived(const int id, const int progre
|
||||
Q_EMIT AlbumsUpdateProgress(progress);
|
||||
}
|
||||
|
||||
void SpotifyService::ResetSongsRequest() {
|
||||
|
||||
if (songs_request_) {
|
||||
QObject::disconnect(&*songs_request_, nullptr, this, nullptr);
|
||||
QObject::disconnect(this, nullptr, &*songs_request_, nullptr);
|
||||
songs_request_.reset();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SpotifyService::GetSongs() {
|
||||
|
||||
if (!authenticated()) {
|
||||
@@ -622,8 +338,7 @@ void SpotifyService::GetSongs() {
|
||||
return;
|
||||
}
|
||||
|
||||
ResetSongsRequest();
|
||||
songs_request_.reset(new SpotifyRequest(this, network_, SpotifyBaseRequest::Type::FavouriteSongs, this), [](SpotifyRequest *request) { request->deleteLater(); });
|
||||
songs_request_.reset(new SpotifyRequest(this, network_, SpotifyBaseRequest::Type::FavouriteSongs, this));
|
||||
QObject::connect(&*songs_request_, &SpotifyRequest::Results, this, &SpotifyService::SongsResultsReceived);
|
||||
QObject::connect(&*songs_request_, &SpotifyRequest::UpdateStatus, this, &SpotifyService::SongsUpdateStatusReceived);
|
||||
QObject::connect(&*songs_request_, &SpotifyRequest::ProgressSetMaximum, this, &SpotifyService::SongsProgressSetMaximumReceived);
|
||||
@@ -633,6 +348,12 @@ void SpotifyService::GetSongs() {
|
||||
|
||||
}
|
||||
|
||||
void SpotifyService::ResetSongsRequest() {
|
||||
|
||||
songs_request_.reset();
|
||||
|
||||
}
|
||||
|
||||
void SpotifyService::SongsResultsReceived(const int id, const SongMap &songs, const QString &error) {
|
||||
|
||||
Q_UNUSED(id);
|
||||
@@ -689,8 +410,7 @@ void SpotifyService::StartSearch() {
|
||||
|
||||
}
|
||||
|
||||
void SpotifyService::CancelSearch() {
|
||||
}
|
||||
void SpotifyService::CancelSearch() {}
|
||||
|
||||
void SpotifyService::SendSearch() {
|
||||
|
||||
@@ -711,12 +431,11 @@ void SpotifyService::SendSearch() {
|
||||
return;
|
||||
}
|
||||
|
||||
search_request_.reset(new SpotifyRequest(this, network_, type, this), [](SpotifyRequest *request) { request->deleteLater(); });
|
||||
|
||||
QObject::connect(search_request_.get(), &SpotifyRequest::Results, this, &SpotifyService::SearchResultsReceived);
|
||||
QObject::connect(search_request_.get(), &SpotifyRequest::UpdateStatus, this, &SpotifyService::SearchUpdateStatus);
|
||||
QObject::connect(search_request_.get(), &SpotifyRequest::ProgressSetMaximum, this, &SpotifyService::SearchProgressSetMaximum);
|
||||
QObject::connect(search_request_.get(), &SpotifyRequest::UpdateProgress, this, &SpotifyService::SearchUpdateProgress);
|
||||
search_request_.reset(new SpotifyRequest(this, network_, type, this));
|
||||
QObject::connect(&*search_request_, &SpotifyRequest::Results, this, &SpotifyService::SearchResultsReceived);
|
||||
QObject::connect(&*search_request_, &SpotifyRequest::UpdateStatus, this, &SpotifyService::SearchUpdateStatus);
|
||||
QObject::connect(&*search_request_, &SpotifyRequest::ProgressSetMaximum, this, &SpotifyService::SearchProgressSetMaximum);
|
||||
QObject::connect(&*search_request_, &SpotifyRequest::UpdateProgress, this, &SpotifyService::SearchUpdateProgress);
|
||||
|
||||
search_request_->Search(search_id_, search_text_);
|
||||
search_request_->Process();
|
||||
@@ -729,21 +448,3 @@ void SpotifyService::SearchResultsReceived(const int id, const SongMap &songs, c
|
||||
search_request_.reset();
|
||||
|
||||
}
|
||||
|
||||
void SpotifyService::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) << "Spotify:" << e;
|
||||
error_html += e + "<br />"_L1;
|
||||
}
|
||||
if (debug.isValid()) qLog(Debug) << debug;
|
||||
|
||||
Q_EMIT LoginFailure(error_html);
|
||||
Q_EMIT LoginComplete(false);
|
||||
|
||||
login_errors_.clear();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2022-2024, Jonas Kvinge <jonas@jkvinge.net>
|
||||
* Copyright 2022-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,16 @@
|
||||
|
||||
#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 <QTimer>
|
||||
#include <QScopedPointer>
|
||||
|
||||
#include "includes/shared_ptr.h"
|
||||
#include "core/song.h"
|
||||
#include "streaming/streamingservice.h"
|
||||
#include "streaming/streamingsearchview.h"
|
||||
#include "collection/collectionmodel.h"
|
||||
|
||||
class QNetworkReply;
|
||||
class QTimer;
|
||||
|
||||
class TaskManager;
|
||||
class Database;
|
||||
@@ -54,7 +43,9 @@ class SpotifyStreamURLRequest;
|
||||
class CollectionBackend;
|
||||
class CollectionModel;
|
||||
class CollectionFilter;
|
||||
class LocalRedirectServer;
|
||||
class OAuthenticator;
|
||||
|
||||
using SpotifyRequestPtr = QScopedPointer<SpotifyRequest, QScopedPointerDeleteLater>;
|
||||
|
||||
class SpotifyService : public StreamingService {
|
||||
Q_OBJECT
|
||||
@@ -83,9 +74,8 @@ class SpotifyService : public StreamingService {
|
||||
bool fetchalbums() const { return fetchalbums_; }
|
||||
bool download_album_covers() const { return download_album_covers_; }
|
||||
|
||||
QString access_token() const { return access_token_; }
|
||||
|
||||
bool authenticated() const override { return !access_token_.isEmpty(); }
|
||||
bool authenticated() const override;
|
||||
QByteArray authorization_header() const;
|
||||
|
||||
SharedPtr<CollectionBackend> artists_collection_backend() override { return artists_collection_backend_; }
|
||||
SharedPtr<CollectionBackend> albums_collection_backend() override { return albums_collection_backend_; }
|
||||
@@ -101,7 +91,7 @@ class SpotifyService : public StreamingService {
|
||||
|
||||
public Q_SLOTS:
|
||||
void Authenticate();
|
||||
void Deauthenticate();
|
||||
void ClearSession();
|
||||
void GetArtists() override;
|
||||
void GetAlbums() override;
|
||||
void GetSongs() override;
|
||||
@@ -111,10 +101,7 @@ class SpotifyService : public StreamingService {
|
||||
|
||||
private Q_SLOTS:
|
||||
void ExitReceived();
|
||||
void RedirectArrived();
|
||||
void RequestNewAccessToken() { RequestAccessToken(); }
|
||||
void HandleLoginSSLErrors(const QList<QSslError> &ssl_errors);
|
||||
void AccessTokenRequestFinished(QNetworkReply *reply);
|
||||
void OAuthFinished(const bool success, const QString &error = QString());
|
||||
void StartSearch();
|
||||
void ArtistsResultsReceived(const int id, const SongMap &songs, const QString &error);
|
||||
void AlbumsResultsReceived(const int id, const SongMap &songs, const QString &error);
|
||||
@@ -131,16 +118,13 @@ class SpotifyService : public StreamingService {
|
||||
void SongsUpdateProgressReceived(const int id, const int progress);
|
||||
|
||||
private:
|
||||
using Param = QPair<QString, QString>;
|
||||
using ParamList = QList<Param>;
|
||||
|
||||
void LoadSession();
|
||||
void RequestAccessToken(const QString &code = QString(), const QUrl &redirect_url = QUrl());
|
||||
void SendSearch();
|
||||
void LoginError(const QString &error = QString(), const QVariant &debug = QVariant());
|
||||
|
||||
private:
|
||||
const SharedPtr<NetworkAccessManager> network_;
|
||||
|
||||
OAuthenticator *oauth_;
|
||||
|
||||
SharedPtr<CollectionBackend> artists_collection_backend_;
|
||||
SharedPtr<CollectionBackend> albums_collection_backend_;
|
||||
SharedPtr<CollectionBackend> songs_collection_backend_;
|
||||
@@ -150,12 +134,11 @@ class SpotifyService : public StreamingService {
|
||||
CollectionModel *songs_collection_model_;
|
||||
|
||||
QTimer *timer_search_delay_;
|
||||
QTimer *timer_refresh_login_;
|
||||
|
||||
SharedPtr<SpotifyRequest> artists_request_;
|
||||
SharedPtr<SpotifyRequest> albums_request_;
|
||||
SharedPtr<SpotifyRequest> songs_request_;
|
||||
SharedPtr<SpotifyRequest> search_request_;
|
||||
SpotifyRequestPtr artists_request_;
|
||||
SpotifyRequestPtr albums_request_;
|
||||
SpotifyRequestPtr songs_request_;
|
||||
SpotifyRequestPtr search_request_;
|
||||
SpotifyFavoriteRequest *favorite_request_;
|
||||
|
||||
bool enabled_;
|
||||
@@ -165,11 +148,6 @@ class SpotifyService : public StreamingService {
|
||||
bool fetchalbums_;
|
||||
bool download_album_covers_;
|
||||
|
||||
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_;
|
||||
@@ -178,15 +156,7 @@ class SpotifyService : public StreamingService {
|
||||
int search_id_;
|
||||
QString search_text_;
|
||||
|
||||
QString code_verifier_;
|
||||
QString code_challenge_;
|
||||
|
||||
LocalRedirectServer *server_;
|
||||
QStringList login_errors_;
|
||||
QTimer refresh_login_timer_;
|
||||
|
||||
QList<QObject*> wait_for_exit_;
|
||||
QList<QNetworkReply*> replies_;
|
||||
};
|
||||
|
||||
using SpotifyServicePtr = SharedPtr<SpotifyService>;
|
||||
|
||||
Reference in New Issue
Block a user