strawberry-audio-player-win.../src/tidal/tidalservice.h

253 lines
8.6 KiB
C
Raw Normal View History

2020-04-13 19:04:06 +02:00
/*
* Strawberry Music Player
2021-03-20 21:14:47 +01:00
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
2020-04-13 19:04:06 +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 TIDALSERVICE_H
#define TIDALSERVICE_H
#include "config.h"
#include <memory>
#include <QtGlobal>
#include <QObject>
#include <QPair>
#include <QSet>
#include <QList>
#include <QMap>
2020-04-13 19:04:06 +02:00
#include <QVariant>
#include <QByteArray>
#include <QString>
#include <QStringList>
#include <QUrl>
#include <QDateTime>
#include <QSslError>
#include "core/song.h"
#include "internet/internetservice.h"
#include "internet/internetsearchview.h"
#include "settings/tidalsettingspage.h"
class QSortFilterProxyModel;
class QNetworkReply;
class QTimer;
class Application;
class NetworkAccessManager;
class TidalUrlHandler;
class TidalRequest;
class TidalFavoriteRequest;
class TidalStreamURLRequest;
class CollectionBackend;
class CollectionModel;
class TidalService : public InternetService {
Q_OBJECT
public:
2020-09-17 17:07:54 +02:00
explicit TidalService(Application *app, QObject *parent);
2020-06-15 21:55:05 +02:00
~TidalService() override;
2020-04-13 19:04:06 +02:00
static const Song::Source kSource;
2022-10-31 06:12:02 +01:00
static const char kApiUrl[];
static const char kResourcesUrl[];
2020-04-13 19:04:06 +02:00
2020-06-15 21:55:05 +02:00
void Exit() override;
void ReloadSettings() override;
2020-04-13 19:04:06 +02:00
void Logout();
2020-06-15 21:55:05 +02:00
int Search(const QString &text, InternetSearchView::SearchType type) override;
void CancelSearch() override;
2020-04-13 19:04:06 +02:00
2022-10-31 06:12:02 +01:00
int max_login_attempts() const { return kLoginAttempts; }
Application *app() const { return app_; }
bool oauth() const override { return oauth_; }
QString client_id() const { return client_id_; }
QString api_token() const { return api_token_; }
quint64 user_id() const { return user_id_; }
QString country_code() const { return country_code_; }
QString username() const { return username_; }
QString password() const { return password_; }
QString quality() const { return quality_; }
int artistssearchlimit() const { return artistssearchlimit_; }
int albumssearchlimit() const { return albumssearchlimit_; }
int songssearchlimit() const { return songssearchlimit_; }
bool fetchalbums() const { return fetchalbums_; }
QString coversize() const { return coversize_; }
bool download_album_covers() const { return download_album_covers_; }
TidalSettingsPage::StreamUrlMethod stream_url_method() const { return stream_url_method_; }
bool album_explicit() const { return album_explicit_; }
QString access_token() const { return access_token_; }
QString session_id() const { return session_id_; }
bool authenticated() const override { return (!access_token_.isEmpty() || !session_id_.isEmpty()); }
bool login_sent() const { return login_sent_; }
bool login_attempts() const { return login_attempts_; }
2020-04-13 19:04:06 +02:00
uint GetStreamURL(const QUrl &url, QString &error);
2020-04-13 19:04:06 +02:00
2020-06-15 21:55:05 +02:00
CollectionBackend *artists_collection_backend() override { return artists_collection_backend_; }
CollectionBackend *albums_collection_backend() override { return albums_collection_backend_; }
CollectionBackend *songs_collection_backend() override { return songs_collection_backend_; }
2020-04-13 19:04:06 +02:00
2020-06-15 21:55:05 +02:00
CollectionModel *artists_collection_model() override { return artists_collection_model_; }
CollectionModel *albums_collection_model() override { return albums_collection_model_; }
CollectionModel *songs_collection_model() override { return songs_collection_model_; }
2020-04-13 19:04:06 +02:00
2020-06-15 21:55:05 +02:00
QSortFilterProxyModel *artists_collection_sort_model() override { return artists_collection_sort_model_; }
QSortFilterProxyModel *albums_collection_sort_model() override { return albums_collection_sort_model_; }
QSortFilterProxyModel *songs_collection_sort_model() override { return songs_collection_sort_model_; }
2020-04-13 19:04:06 +02:00
public slots:
2020-06-15 21:55:05 +02:00
void ShowConfig() override;
2021-06-20 19:04:08 +02:00
void StartAuthorization(const QString &client_id);
2020-04-13 19:04:06 +02:00
void TryLogin();
2021-01-26 16:48:04 +01:00
void SendLogin();
void SendLoginWithCredentials(const QString &api_token, const QString &username, const QString &password);
2020-06-15 21:55:05 +02:00
void GetArtists() override;
void GetAlbums() override;
void GetSongs() override;
void ResetArtistsRequest() override;
void ResetAlbumsRequest() override;
void ResetSongsRequest() override;
2021-01-26 16:48:04 +01:00
void AuthorizationUrlReceived(const QUrl &url);
2020-04-13 19:04:06 +02:00
private slots:
void ExitReceived();
2021-01-26 16:48:04 +01:00
void RequestNewAccessToken() { RequestAccessToken(); }
2021-06-20 19:04:08 +02:00
void HandleLoginSSLErrors(const QList<QSslError> &ssl_errors);
2020-04-13 19:04:06 +02:00
void AccessTokenRequestFinished(QNetworkReply *reply);
void HandleAuthReply(QNetworkReply *reply);
void ResetLoginAttempts();
void StartSearch();
void ArtistsResultsReceived(const int id, const SongMap &songs, const QString &error);
void AlbumsResultsReceived(const int id, const SongMap &songs, const QString &error);
void SongsResultsReceived(const int id, const SongMap &songs, const QString &error);
void SearchResultsReceived(const int id, const SongMap &songs, const QString &error);
2020-04-13 19:04:06 +02:00
void ArtistsUpdateStatusReceived(const int id, const QString &text);
void AlbumsUpdateStatusReceived(const int id, const QString &text);
void SongsUpdateStatusReceived(const int id, const QString &text);
void ArtistsUpdateProgressReceived(const int id, const int progress);
void AlbumsUpdateProgressReceived(const int id, const int progress);
void SongsUpdateProgressReceived(const int id, const int progress);
void HandleStreamURLFailure(const uint id, const QUrl &original_url, const QString &error);
void HandleStreamURLSuccess(const uint id, const QUrl &original_url, const QUrl &stream_url, const Song::FileType filetype, const int samplerate, const int bit_depth, const qint64 duration);
2020-04-13 19:04:06 +02:00
private:
2022-10-13 22:39:31 +02:00
using Param = QPair<QString, QString>;
using ParamList = QList<Param>;
2020-04-13 19:04:06 +02:00
2020-05-13 21:56:11 +02:00
void LoadSession();
2021-01-26 16:48:04 +01:00
void RequestAccessToken(const QString &code = QString());
2020-04-13 19:04:06 +02:00
void SendSearch();
void LoginError(const QString &error = QString(), const QVariant &debug = QVariant());
2022-10-31 06:12:02 +01:00
static const char kOAuthUrl[];
static const char kOAuthAccessTokenUrl[];
static const char kOAuthRedirectUrl[];
static const char kAuthUrl[];
2020-04-13 19:04:06 +02:00
static const int kLoginAttempts;
static const int kTimeResetLoginAttempts;
2022-10-31 06:12:02 +01:00
static const char kArtistsSongsTable[];
static const char kAlbumsSongsTable[];
static const char kSongsTable[];
2020-04-13 19:04:06 +02:00
2022-10-31 06:12:02 +01:00
static const char kArtistsSongsFtsTable[];
static const char kAlbumsSongsFtsTable[];
static const char kSongsFtsTable[];
2020-04-13 19:04:06 +02:00
Application *app_;
NetworkAccessManager *network_;
TidalUrlHandler *url_handler_;
CollectionBackend *artists_collection_backend_;
CollectionBackend *albums_collection_backend_;
CollectionBackend *songs_collection_backend_;
CollectionModel *artists_collection_model_;
CollectionModel *albums_collection_model_;
CollectionModel *songs_collection_model_;
QSortFilterProxyModel *artists_collection_sort_model_;
QSortFilterProxyModel *albums_collection_sort_model_;
QSortFilterProxyModel *songs_collection_sort_model_;
QTimer *timer_search_delay_;
QTimer *timer_login_attempt_;
2020-05-13 21:56:11 +02:00
QTimer *timer_refresh_login_;
2020-04-13 19:04:06 +02:00
std::shared_ptr<TidalRequest> artists_request_;
std::shared_ptr<TidalRequest> albums_request_;
std::shared_ptr<TidalRequest> songs_request_;
std::shared_ptr<TidalRequest> search_request_;
TidalFavoriteRequest *favorite_request_;
2020-05-13 21:56:11 +02:00
bool enabled_;
2020-04-13 19:04:06 +02:00
bool oauth_;
QString client_id_;
QString api_token_;
quint64 user_id_;
QString country_code_;
QString username_;
QString password_;
QString quality_;
int artistssearchlimit_;
int albumssearchlimit_;
int songssearchlimit_;
bool fetchalbums_;
QString coversize_;
bool download_album_covers_;
TidalSettingsPage::StreamUrlMethod stream_url_method_;
bool album_explicit_;
2020-04-13 19:04:06 +02:00
QString access_token_;
QString refresh_token_;
QString session_id_;
2020-05-13 21:56:11 +02:00
quint64 expires_in_;
quint64 login_time_;
2020-04-13 19:04:06 +02:00
int pending_search_id_;
int next_pending_search_id_;
QString pending_search_text_;
InternetSearchView::SearchType pending_search_type_;
int search_id_;
QString search_text_;
bool login_sent_;
int login_attempts_;
QString code_verifier_;
QString code_challenge_;
uint next_stream_url_request_id_;
QMap<uint, std::shared_ptr<TidalStreamURLRequest>> stream_url_requests_;
2020-04-13 19:04:06 +02:00
QStringList login_errors_;
QList<QObject*> wait_for_exit_;
QList<QNetworkReply*> replies_;
2020-04-13 19:04:06 +02:00
};
#endif // TIDALSERVICE_H