2018-12-23 18:54:27 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2018-12-23 18:54:27 +01: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 LISTENBRAINZSCROBBLER_H
|
|
|
|
#define LISTENBRAINZSCROBBLER_H
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QList>
|
|
|
|
#include <QVariant>
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QString>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QUrl>
|
2018-12-26 15:05:32 +01:00
|
|
|
#include <QJsonDocument>
|
2020-05-11 00:51:18 +02:00
|
|
|
#include <QTimer>
|
2018-12-23 18:54:27 +01:00
|
|
|
|
|
|
|
#include "core/song.h"
|
|
|
|
#include "scrobblerservice.h"
|
|
|
|
#include "scrobblercache.h"
|
|
|
|
|
2019-11-14 21:07:30 +01:00
|
|
|
class QNetworkReply;
|
|
|
|
|
2018-12-23 18:54:27 +01:00
|
|
|
class Application;
|
|
|
|
class NetworkAccessManager;
|
|
|
|
class LocalRedirectServer;
|
|
|
|
|
|
|
|
class ListenBrainzScrobbler : public ScrobblerService {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ListenBrainzScrobbler(Application *app, QObject *parent = nullptr);
|
2020-06-15 21:55:05 +02:00
|
|
|
~ListenBrainzScrobbler() override;
|
2018-12-23 18:54:27 +01:00
|
|
|
|
|
|
|
static const char *kName;
|
|
|
|
static const char *kSettingsGroup;
|
|
|
|
|
2020-06-15 21:55:05 +02:00
|
|
|
void ReloadSettings() override;
|
2018-12-23 18:54:27 +01:00
|
|
|
void LoadSession();
|
|
|
|
|
2020-06-15 21:55:05 +02:00
|
|
|
bool IsEnabled() const override { return enabled_; }
|
|
|
|
bool IsAuthenticated() const override { return !access_token_.isEmpty() && !user_token_.isEmpty(); }
|
|
|
|
bool IsSubmitted() const override { return submitted_; }
|
|
|
|
void Submitted() override { submitted_ = true; }
|
2018-12-23 18:54:27 +01:00
|
|
|
QString user_token() const { return user_token_; }
|
|
|
|
|
2019-04-15 22:17:40 +02:00
|
|
|
void Authenticate(const bool https = false);
|
2018-12-23 18:54:27 +01:00
|
|
|
void Logout();
|
|
|
|
void ShowConfig();
|
2020-06-15 21:55:05 +02:00
|
|
|
void Submit() override;
|
|
|
|
void UpdateNowPlaying(const Song &song) override;
|
|
|
|
void ClearPlaying() override;
|
|
|
|
void Scrobble(const Song &song) override;
|
2018-12-23 18:54:27 +01:00
|
|
|
|
|
|
|
signals:
|
|
|
|
void AuthenticationComplete(bool success, QString error = QString());
|
|
|
|
|
|
|
|
public slots:
|
2020-06-15 21:55:05 +02:00
|
|
|
void WriteCache() override { cache_->WriteCache(); }
|
2018-12-23 18:54:27 +01:00
|
|
|
|
|
|
|
private slots:
|
2019-11-14 21:07:30 +01:00
|
|
|
void RedirectArrived();
|
2018-12-23 18:54:27 +01:00
|
|
|
void AuthenticateReplyFinished(QNetworkReply *reply);
|
2021-01-26 16:48:04 +01:00
|
|
|
void RequestNewAccessToken() { RequestAccessToken(); }
|
2018-12-23 18:54:27 +01:00
|
|
|
void UpdateNowPlayingRequestFinished(QNetworkReply *reply);
|
2021-06-20 19:04:08 +02:00
|
|
|
void ScrobbleRequestFinished(QNetworkReply *reply, const QList<quint64> &list);
|
2018-12-23 18:54:27 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
QNetworkReply *CreateRequest(const QUrl &url, const QJsonDocument &json_doc);
|
|
|
|
QByteArray GetReplyData(QNetworkReply *reply);
|
|
|
|
|
2019-11-14 21:07:30 +01:00
|
|
|
void AuthError(const QString &error);
|
2020-06-15 21:55:05 +02:00
|
|
|
void Error(const QString &error, const QVariant &debug = QVariant()) override;
|
2021-01-26 16:48:04 +01:00
|
|
|
void RequestAccessToken(const QUrl &redirect_url = QUrl(), const QString &code = QString());
|
2020-06-15 21:55:05 +02:00
|
|
|
void DoSubmit() override;
|
2020-04-25 00:07:42 +02:00
|
|
|
void CheckScrobblePrevSong();
|
2018-12-23 18:54:27 +01:00
|
|
|
|
2020-05-11 00:51:18 +02:00
|
|
|
static const char *kOAuthAuthorizeUrl;
|
|
|
|
static const char *kOAuthAccessTokenUrl;
|
|
|
|
static const char *kOAuthRedirectUrl;
|
2018-12-23 18:54:27 +01:00
|
|
|
static const char *kApiUrl;
|
2020-05-11 00:51:18 +02:00
|
|
|
static const char *kClientIDB64;
|
|
|
|
static const char *kClientSecretB64;
|
2018-12-23 18:54:27 +01:00
|
|
|
static const char *kCacheFile;
|
|
|
|
static const int kScrobblesPerRequest;
|
|
|
|
|
|
|
|
Application *app_;
|
|
|
|
NetworkAccessManager *network_;
|
|
|
|
ScrobblerCache *cache_;
|
2019-11-14 21:07:30 +01:00
|
|
|
LocalRedirectServer *server_;
|
2018-12-23 18:54:27 +01:00
|
|
|
bool enabled_;
|
|
|
|
QString user_token_;
|
|
|
|
QString access_token_;
|
|
|
|
qint64 expires_in_;
|
|
|
|
QString token_type_;
|
|
|
|
QString refresh_token_;
|
2020-05-11 00:51:18 +02:00
|
|
|
quint64 login_time_;
|
2018-12-23 18:54:27 +01:00
|
|
|
bool submitted_;
|
|
|
|
Song song_playing_;
|
2020-04-25 00:07:42 +02:00
|
|
|
bool scrobbled_;
|
2018-12-23 18:54:27 +01:00
|
|
|
quint64 timestamp_;
|
2020-05-11 00:51:18 +02:00
|
|
|
QTimer refresh_login_timer_;
|
2021-01-30 21:50:28 +01:00
|
|
|
QTimer timer_submit_;
|
2018-12-23 18:54:27 +01:00
|
|
|
|
2020-05-12 21:28:42 +02:00
|
|
|
QList<QNetworkReply*> replies_;
|
|
|
|
|
2018-12-23 18:54:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // LISTENBRAINZSCROBBLER_H
|