146 lines
4.1 KiB
C
Raw Normal View History

2018-08-09 18:10:03 +02:00
/*
* 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 TIDALSERVICE_H
#define TIDALSERVICE_H
#include "config.h"
#include <QtGlobal>
#include <QObject>
#include <QHash>
#include <QString>
2018-09-20 22:13:30 +02:00
#include <QUrl>
2018-08-09 18:10:03 +02:00
#include <QNetworkReply>
#include <QTimer>
#include <QDateTime>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include "core/song.h"
#include "internet/internetmodel.h"
#include "internet/internetservice.h"
#include "settings/tidalsettingspage.h"
class NetworkAccessManager;
2018-09-20 22:13:30 +02:00
class TidalUrlHandler;
2018-08-09 18:10:03 +02:00
class TidalService : public InternetService {
Q_OBJECT
public:
TidalService(Application *app, InternetModel *parent);
~TidalService();
2018-09-08 12:38:02 +02:00
static const Song::Source kSource;
2018-09-20 22:13:30 +02:00
static const int kLoginAttempts;
2018-08-09 18:10:03 +02:00
void ReloadSettings();
void Logout();
int Search(const QString &query, TidalSettingsPage::SearchBy searchby);
void CancelSearch();
2018-08-09 18:10:03 +02:00
const bool login_sent() { return login_sent_; }
2018-09-12 22:51:10 +02:00
const bool authenticated() { return (!session_id_.isEmpty() && !country_code_.isEmpty()); }
2018-08-09 18:10:03 +02:00
2018-09-20 22:13:30 +02:00
void GetStreamURL(const QUrl &url);
2018-08-09 18:10:03 +02:00
signals:
2018-09-20 22:13:30 +02:00
void Login();
void Login(const QString &username, const QString &password);
2018-08-09 18:10:03 +02:00
void LoginSuccess();
void LoginFailure(QString failure_reason);
void SearchResults(int id, SongList songs);
void SearchError(int id, QString message);
void UpdateStatus(QString text);
void ProgressSetMaximum(int max);
void UpdateProgress(int max);
2018-09-20 22:13:30 +02:00
void GetStreamURLFinished(QNetworkReply *reply, const QUrl url);
void StreamURLFinished(const QUrl url, const Song::FileType);
2018-08-09 18:10:03 +02:00
public slots:
void ShowConfig();
2018-09-20 22:13:30 +02:00
void SendLogin(const QString &username, const QString &password);
2018-08-09 18:10:03 +02:00
private slots:
2018-09-20 22:13:30 +02:00
void SendLogin();
void HandleAuthReply(QNetworkReply *reply);
2018-08-09 18:10:03 +02:00
void StartSearch();
void SearchFinished(QNetworkReply *reply, int search_id);
2018-08-09 18:10:03 +02:00
void GetAlbumFinished(QNetworkReply *reply, int search_id, int album_id);
2018-09-20 22:13:30 +02:00
void GetStreamURLFinished(QNetworkReply *reply, const int song_id, const QUrl original_url);
2018-08-09 18:10:03 +02:00
private:
void ClearSearch();
2018-08-09 18:10:03 +02:00
void LoadSessionID();
QNetworkReply *CreateRequest(const QString &ressource_name, const QList<QPair<QString, QString>> &params);
2018-09-10 21:23:50 +02:00
QJsonObject ExtractJsonObj(QNetworkReply *reply, bool sendlogin = false);
QJsonArray ExtractItems(QNetworkReply *reply, bool sendlogin = false);
void SendSearch();
void GetAlbum(const int album_id);
Song ParseSong(const int album_id_requested, const QJsonValue &value);
void CheckFinish();
void Error(QString error, QString debug = QString());
2018-08-09 18:10:03 +02:00
static const char *kApiUrl;
static const char *kAuthUrl;
static const char *kResourcesUrl;
static const char *kApiToken;
NetworkAccessManager *network_;
2018-09-20 22:13:30 +02:00
TidalUrlHandler *url_handler_;
QTimer *timer_searchdelay_;
2018-08-09 18:10:03 +02:00
QString username_;
QString password_;
QString quality_;
int searchdelay_;
int albumssearchlimit_;
int songssearchlimit_;
bool fetchalbums_;
QString coversize_;
2018-08-09 18:10:03 +02:00
QString session_id_;
quint64 user_id_;
QString country_code_;
int pending_search_id_;
int next_pending_search_id_;
QString pending_search_text_;
2018-08-09 18:10:03 +02:00
TidalSettingsPage::SearchBy pending_searchby_;
int search_id_;
QString search_text_;
QHash<int, int> requests_album_;
2018-09-20 22:13:30 +02:00
QHash<int, QUrl> requests_song_;
int albums_requested_;
int albums_received_;
int songs_requested_;
int songs_received_;
SongList songs_;
QString search_error_;
bool login_sent_;
int login_attempts_;
2018-09-20 22:13:30 +02:00
QUrl stream_request_url_;
2018-08-09 18:10:03 +02:00
};
#endif // TIDALSERVICE_H