strawberry-audio-player-win.../src/scrobbler/listenbrainzscrobbler.h

125 lines
3.5 KiB
C
Raw Normal View History

/*
* Strawberry Music Player
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef 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>
#include "core/song.h"
#include "scrobblerservice.h"
#include "scrobblercache.h"
class QNetworkReply;
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;
static const char *kName;
static const char *kSettingsGroup;
2020-06-15 21:55:05 +02:00
void ReloadSettings() override;
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; }
QString user_token() const { return user_token_; }
void Authenticate(const bool https = false);
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;
signals:
void AuthenticationComplete(bool success, QString error = QString());
public slots:
2020-06-15 21:55:05 +02:00
void WriteCache() override { cache_->WriteCache(); }
private slots:
void RedirectArrived();
void AuthenticateReplyFinished(QNetworkReply *reply);
2020-05-11 00:51:18 +02:00
void RequestAccessToken(const QUrl &redirect_url = QUrl(), const QString &code = QString());
void UpdateNowPlayingRequestFinished(QNetworkReply *reply);
void ScrobbleRequestFinished(QNetworkReply *reply, QList<quint64>);
private:
QNetworkReply *CreateRequest(const QUrl &url, const QJsonDocument &json_doc);
QByteArray GetReplyData(QNetworkReply *reply);
void AuthError(const QString &error);
2020-06-15 21:55:05 +02:00
void Error(const QString &error, const QVariant &debug = QVariant()) override;
void DoSubmit() override;
2020-04-25 00:07:42 +02:00
void CheckScrobblePrevSong();
2020-05-11 00:51:18 +02:00
static const char *kOAuthAuthorizeUrl;
static const char *kOAuthAccessTokenUrl;
static const char *kOAuthRedirectUrl;
static const char *kApiUrl;
2020-05-11 00:51:18 +02:00
static const char *kClientIDB64;
static const char *kClientSecretB64;
static const char *kCacheFile;
static const int kScrobblesPerRequest;
Application *app_;
NetworkAccessManager *network_;
ScrobblerCache *cache_;
LocalRedirectServer *server_;
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_;
bool submitted_;
Song song_playing_;
2020-04-25 00:07:42 +02:00
bool scrobbled_;
quint64 timestamp_;
2020-05-11 00:51:18 +02:00
QTimer refresh_login_timer_;
QList<QNetworkReply*> replies_;
};
#endif // LISTENBRAINZSCROBBLER_H