2020-05-09 01:48:08 +02:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2020-05-09 01:48:08 +02:00
|
|
|
*
|
|
|
|
* Strawberry is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Strawberry is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SPOTIFYCOVERPROVIDER_H
|
|
|
|
#define SPOTIFYCOVERPROVIDER_H
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QPair>
|
|
|
|
#include <QSet>
|
|
|
|
#include <QList>
|
|
|
|
#include <QVariant>
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QString>
|
|
|
|
#include <QSslError>
|
|
|
|
#include <QJsonValue>
|
|
|
|
#include <QJsonObject>
|
2020-05-10 17:10:20 +02:00
|
|
|
#include <QTimer>
|
2020-05-09 01:48:08 +02:00
|
|
|
|
2020-05-10 12:49:11 +02:00
|
|
|
#include "jsoncoverprovider.h"
|
2020-05-09 01:48:08 +02:00
|
|
|
|
|
|
|
class QNetworkReply;
|
|
|
|
class Application;
|
2020-12-09 18:39:37 +01:00
|
|
|
class NetworkAccessManager;
|
2020-05-09 01:48:08 +02:00
|
|
|
class LocalRedirectServer;
|
|
|
|
|
2020-05-10 12:49:11 +02:00
|
|
|
class SpotifyCoverProvider : public JsonCoverProvider {
|
2020-05-09 01:48:08 +02:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit SpotifyCoverProvider(Application *app, QObject *parent = nullptr);
|
2020-06-15 21:55:05 +02:00
|
|
|
~SpotifyCoverProvider() override;
|
2020-05-12 21:28:42 +02:00
|
|
|
|
2020-06-15 21:55:05 +02:00
|
|
|
bool StartSearch(const QString &artist, const QString &album, const QString &title, const int id) override;
|
|
|
|
void CancelSearch(const int id) override;
|
2020-05-09 01:48:08 +02:00
|
|
|
|
2020-06-15 21:55:05 +02:00
|
|
|
void Authenticate() override;
|
|
|
|
void Deauthenticate() override;
|
|
|
|
bool IsAuthenticated() const override { return !access_token_.isEmpty(); }
|
2020-05-09 01:48:08 +02:00
|
|
|
|
|
|
|
private slots:
|
2021-06-20 19:04:08 +02:00
|
|
|
void HandleLoginSSLErrors(const QList<QSslError> &ssl_errors);
|
2020-05-09 01:48:08 +02:00
|
|
|
void RedirectArrived();
|
|
|
|
void AccessTokenRequestFinished(QNetworkReply *reply);
|
|
|
|
void HandleSearchReply(QNetworkReply *reply, const int id, const QString &extract);
|
2021-01-26 16:48:04 +01:00
|
|
|
void RequestNewAccessToken() { RequestAccessToken(); }
|
2020-05-09 01:48:08 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
QByteArray GetReplyData(QNetworkReply *reply);
|
|
|
|
void AuthError(const QString &error = QString(), const QVariant &debug = QVariant());
|
2020-06-15 21:55:05 +02:00
|
|
|
void Error(const QString &error, const QVariant &debug = QVariant()) override;
|
2021-06-20 19:04:08 +02:00
|
|
|
void RequestAccessToken(const QString &code = QString(), const QUrl &redirect_url = QUrl());
|
2020-05-09 01:48:08 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef QPair<QString, QString> Param;
|
|
|
|
typedef QList<Param> ParamList;
|
|
|
|
|
|
|
|
static const char *kSettingsGroup;
|
|
|
|
static const char *kClientIDB64;
|
|
|
|
static const char *kClientSecretB64;
|
|
|
|
static const char *kOAuthAuthorizeUrl;
|
|
|
|
static const char *kOAuthAccessTokenUrl;
|
|
|
|
static const char *kOAuthRedirectUrl;
|
|
|
|
static const char *kApiUrl;
|
|
|
|
static const int kLimit;
|
|
|
|
|
2020-12-09 18:39:37 +01:00
|
|
|
NetworkAccessManager *network_;
|
2020-05-09 01:48:08 +02:00
|
|
|
LocalRedirectServer *server_;
|
|
|
|
QStringList login_errors_;
|
|
|
|
QString code_verifier_;
|
|
|
|
QString code_challenge_;
|
|
|
|
QString access_token_;
|
2020-05-10 17:10:20 +02:00
|
|
|
QString refresh_token_;
|
|
|
|
quint64 expires_in_;
|
|
|
|
quint64 login_time_;
|
|
|
|
QTimer refresh_login_timer_;
|
2020-05-12 21:28:42 +02:00
|
|
|
QList<QNetworkReply*> replies_;
|
2020-05-09 01:48:08 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // SPOTIFYCOVERPROVIDER_H
|