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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <memory>
|
|
|
|
|
2018-08-09 18:10:03 +02:00
|
|
|
#include <QObject>
|
2019-05-27 21:10:37 +02:00
|
|
|
#include <QStandardPaths>
|
2019-06-09 19:29:25 +02:00
|
|
|
#include <QDesktopServices>
|
2019-06-10 02:29:57 +02:00
|
|
|
#include <QCryptographicHash>
|
2018-08-09 18:10:03 +02:00
|
|
|
#include <QByteArray>
|
|
|
|
#include <QPair>
|
2019-05-27 21:10:37 +02:00
|
|
|
#include <QList>
|
2018-08-09 18:10:03 +02:00
|
|
|
#include <QString>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QUrlQuery>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QJsonParseError>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QSettings>
|
2019-05-27 21:10:37 +02:00
|
|
|
#include <QSortFilterProxyModel>
|
2018-08-09 18:10:03 +02:00
|
|
|
|
|
|
|
#include "core/application.h"
|
2018-09-20 22:13:30 +02:00
|
|
|
#include "core/player.h"
|
2018-08-09 18:10:03 +02:00
|
|
|
#include "core/closure.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/network.h"
|
2019-05-27 21:10:37 +02:00
|
|
|
#include "core/database.h"
|
2018-08-09 18:10:03 +02:00
|
|
|
#include "core/song.h"
|
2019-06-10 02:29:57 +02:00
|
|
|
#include "core/utilities.h"
|
2018-10-17 21:18:39 +02:00
|
|
|
#include "internet/internetsearch.h"
|
2019-05-27 21:10:37 +02:00
|
|
|
#include "collection/collectionbackend.h"
|
|
|
|
#include "collection/collectionmodel.h"
|
2018-08-09 18:10:03 +02:00
|
|
|
#include "tidalservice.h"
|
2018-09-20 22:13:30 +02:00
|
|
|
#include "tidalurlhandler.h"
|
2019-05-27 21:10:37 +02:00
|
|
|
#include "tidalrequest.h"
|
2019-06-07 20:23:05 +02:00
|
|
|
#include "tidalfavoriterequest.h"
|
2019-05-27 21:10:37 +02:00
|
|
|
#include "tidalstreamurlrequest.h"
|
2018-08-09 18:10:03 +02:00
|
|
|
#include "settings/tidalsettingspage.h"
|
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
using std::shared_ptr;
|
|
|
|
|
2018-09-08 12:38:02 +02:00
|
|
|
const Song::Source TidalService::kSource = Song::Source_Tidal;
|
2019-06-09 19:29:25 +02:00
|
|
|
const char *TidalService::kClientIdB64 = "dTVxUE5OWUliRDBTMG8zNk1yQWlGWjU2SzZxTUNyQ21ZUHpadVRuVg==";
|
2018-09-22 15:37:42 +02:00
|
|
|
const char *TidalService::kApiTokenB64 = "UDVYYmVvNUxGdkVTZUR5Ng==";
|
2019-06-09 19:29:25 +02:00
|
|
|
const char *TidalService::kOAuthUrl = "https://login.tidal.com/authorize";
|
|
|
|
const char *TidalService::kOAuthAccessTokenUrl = "https://login.tidal.com/oauth2/token";
|
|
|
|
const char *TidalService::kOAuthRedirectUrl = "tidal://login/auth";
|
|
|
|
const char *TidalService::kAuthUrl = "https://api.tidalhifi.com/v1/login/username";
|
2019-05-27 21:10:37 +02:00
|
|
|
const int TidalService::kLoginAttempts = 2;
|
2018-10-13 00:18:38 +02:00
|
|
|
const int TidalService::kTimeResetLoginAttempts = 60000;
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
const char *TidalService::kArtistsSongsTable = "tidal_artists_songs";
|
|
|
|
const char *TidalService::kAlbumsSongsTable = "tidal_albums_songs";
|
|
|
|
const char *TidalService::kSongsTable = "tidal_songs";
|
|
|
|
|
|
|
|
const char *TidalService::kArtistsSongsFtsTable = "tidal_artists_songs_fts";
|
|
|
|
const char *TidalService::kAlbumsSongsFtsTable = "tidal_albums_songs_fts";
|
|
|
|
const char *TidalService::kSongsFtsTable = "tidal_songs_fts";
|
|
|
|
|
2018-10-23 23:25:02 +02:00
|
|
|
TidalService::TidalService(Application *app, QObject *parent)
|
|
|
|
: InternetService(Song::Source_Tidal, "Tidal", "tidal", app, parent),
|
2019-03-22 23:20:43 +01:00
|
|
|
app_(app),
|
2018-08-09 18:10:03 +02:00
|
|
|
network_(new NetworkAccessManager(this)),
|
2018-09-20 22:13:30 +02:00
|
|
|
url_handler_(new TidalUrlHandler(app, this)),
|
2019-05-31 02:32:01 +02:00
|
|
|
artists_collection_backend_(nullptr),
|
|
|
|
albums_collection_backend_(nullptr),
|
|
|
|
songs_collection_backend_(nullptr),
|
|
|
|
artists_collection_model_(nullptr),
|
|
|
|
albums_collection_model_(nullptr),
|
|
|
|
songs_collection_model_(nullptr),
|
2019-05-27 21:10:37 +02:00
|
|
|
artists_collection_sort_model_(new QSortFilterProxyModel(this)),
|
|
|
|
albums_collection_sort_model_(new QSortFilterProxyModel(this)),
|
|
|
|
songs_collection_sort_model_(new QSortFilterProxyModel(this)),
|
2019-02-12 21:58:03 +01:00
|
|
|
timer_search_delay_(new QTimer(this)),
|
2018-10-13 00:18:38 +02:00
|
|
|
timer_login_attempt_(new QTimer(this)),
|
2019-06-07 20:23:05 +02:00
|
|
|
favorite_request_(new TidalFavoriteRequest(this, network_, this)),
|
2019-06-09 19:29:25 +02:00
|
|
|
user_id_(0),
|
2019-02-12 21:58:03 +01:00
|
|
|
search_delay_(1500),
|
|
|
|
artistssearchlimit_(1),
|
2018-09-06 17:39:26 +02:00
|
|
|
albumssearchlimit_(1),
|
|
|
|
songssearchlimit_(1),
|
2019-05-27 21:10:37 +02:00
|
|
|
fetchalbums_(true),
|
|
|
|
cache_album_covers_(true),
|
2018-08-09 18:10:03 +02:00
|
|
|
pending_search_id_(0),
|
|
|
|
next_pending_search_id_(1),
|
2018-09-22 15:37:42 +02:00
|
|
|
search_id_(0),
|
|
|
|
login_sent_(false),
|
|
|
|
login_attempts_(0)
|
2018-09-06 17:39:26 +02:00
|
|
|
{
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
app->player()->RegisterUrlHandler(url_handler_);
|
|
|
|
|
|
|
|
// Backends
|
|
|
|
|
2019-05-31 02:32:01 +02:00
|
|
|
artists_collection_backend_ = new CollectionBackend();
|
2019-05-27 21:10:37 +02:00
|
|
|
artists_collection_backend_->moveToThread(app_->database()->thread());
|
|
|
|
artists_collection_backend_->Init(app_->database(), kArtistsSongsTable, QString(), QString(), kArtistsSongsFtsTable);
|
|
|
|
|
2019-05-31 02:32:01 +02:00
|
|
|
albums_collection_backend_ = new CollectionBackend();
|
2019-05-27 21:10:37 +02:00
|
|
|
albums_collection_backend_->moveToThread(app_->database()->thread());
|
|
|
|
albums_collection_backend_->Init(app_->database(), kAlbumsSongsTable, QString(), QString(), kAlbumsSongsFtsTable);
|
|
|
|
|
2019-05-31 02:32:01 +02:00
|
|
|
songs_collection_backend_ = new CollectionBackend();
|
2019-05-27 21:10:37 +02:00
|
|
|
songs_collection_backend_->moveToThread(app_->database()->thread());
|
|
|
|
songs_collection_backend_->Init(app_->database(), kSongsTable, QString(), QString(), kSongsFtsTable);
|
|
|
|
|
2019-05-31 02:32:01 +02:00
|
|
|
artists_collection_model_ = new CollectionModel(artists_collection_backend_, app_, this);
|
|
|
|
albums_collection_model_ = new CollectionModel(albums_collection_backend_, app_, this);
|
|
|
|
songs_collection_model_ = new CollectionModel(songs_collection_backend_, app_, this);
|
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
artists_collection_sort_model_->setSourceModel(artists_collection_model_);
|
|
|
|
artists_collection_sort_model_->setSortRole(CollectionModel::Role_SortText);
|
|
|
|
artists_collection_sort_model_->setDynamicSortFilter(true);
|
|
|
|
artists_collection_sort_model_->setSortLocaleAware(true);
|
|
|
|
artists_collection_sort_model_->sort(0);
|
|
|
|
|
|
|
|
albums_collection_sort_model_->setSourceModel(albums_collection_model_);
|
|
|
|
albums_collection_sort_model_->setSortRole(CollectionModel::Role_SortText);
|
|
|
|
albums_collection_sort_model_->setDynamicSortFilter(true);
|
|
|
|
albums_collection_sort_model_->setSortLocaleAware(true);
|
|
|
|
albums_collection_sort_model_->sort(0);
|
|
|
|
|
|
|
|
songs_collection_sort_model_->setSourceModel(songs_collection_model_);
|
|
|
|
songs_collection_sort_model_->setSortRole(CollectionModel::Role_SortText);
|
|
|
|
songs_collection_sort_model_->setDynamicSortFilter(true);
|
|
|
|
songs_collection_sort_model_->setSortLocaleAware(true);
|
|
|
|
songs_collection_sort_model_->sort(0);
|
|
|
|
|
|
|
|
// Search
|
|
|
|
|
2019-02-12 21:58:03 +01:00
|
|
|
timer_search_delay_->setSingleShot(true);
|
|
|
|
connect(timer_search_delay_, SIGNAL(timeout()), SLOT(StartSearch()));
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2018-10-13 00:18:38 +02:00
|
|
|
timer_login_attempt_->setSingleShot(true);
|
|
|
|
connect(timer_login_attempt_, SIGNAL(timeout()), SLOT(ResetLoginAttempts()));
|
|
|
|
|
2018-09-20 22:13:30 +02:00
|
|
|
connect(this, SIGNAL(Login()), SLOT(SendLogin()));
|
2019-05-20 19:14:33 +02:00
|
|
|
connect(this, SIGNAL(Login(QString, QString, QString)), SLOT(SendLogin(QString, QString, QString)));
|
2018-09-20 22:13:30 +02:00
|
|
|
|
2019-06-07 20:23:05 +02:00
|
|
|
connect(this, SIGNAL(AddArtists(const SongList&)), favorite_request_, SLOT(AddArtists(const SongList&)));
|
|
|
|
connect(this, SIGNAL(AddAlbums(const SongList&)), favorite_request_, SLOT(AddAlbums(const SongList&)));
|
|
|
|
connect(this, SIGNAL(AddSongs(const SongList&)), favorite_request_, SLOT(AddSongs(const SongList&)));
|
|
|
|
|
|
|
|
connect(this, SIGNAL(RemoveArtists(const SongList&)), favorite_request_, SLOT(RemoveArtists(const SongList&)));
|
|
|
|
connect(this, SIGNAL(RemoveAlbums(const SongList&)), favorite_request_, SLOT(RemoveAlbums(const SongList&)));
|
|
|
|
connect(this, SIGNAL(RemoveSongs(const SongList&)), favorite_request_, SLOT(RemoveSongs(const SongList&)));
|
|
|
|
|
|
|
|
connect(favorite_request_, SIGNAL(ArtistsAdded(const SongList&)), artists_collection_backend_, SLOT(AddOrUpdateSongs(const SongList&)));
|
|
|
|
connect(favorite_request_, SIGNAL(AlbumsAdded(const SongList&)), albums_collection_backend_, SLOT(AddOrUpdateSongs(const SongList&)));
|
|
|
|
connect(favorite_request_, SIGNAL(SongsAdded(const SongList&)), songs_collection_backend_, SLOT(AddOrUpdateSongs(const SongList&)));
|
|
|
|
|
|
|
|
connect(favorite_request_, SIGNAL(ArtistsRemoved(const SongList&)), artists_collection_backend_, SLOT(DeleteSongs(const SongList&)));
|
|
|
|
connect(favorite_request_, SIGNAL(AlbumsRemoved(const SongList&)), albums_collection_backend_, SLOT(DeleteSongs(const SongList&)));
|
|
|
|
connect(favorite_request_, SIGNAL(SongsRemoved(const SongList&)), songs_collection_backend_, SLOT(DeleteSongs(const SongList&)));
|
|
|
|
|
2018-08-09 18:10:03 +02:00
|
|
|
ReloadSettings();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-07 20:23:05 +02:00
|
|
|
TidalService::~TidalService() {
|
|
|
|
|
|
|
|
while (!stream_url_requests_.isEmpty()) {
|
|
|
|
TidalStreamURLRequest *stream_url_req = stream_url_requests_.takeFirst();
|
|
|
|
disconnect(stream_url_req, 0, nullptr, 0);
|
2019-06-09 19:29:25 +02:00
|
|
|
stream_url_req->deleteLater();
|
2019-06-07 20:23:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-08-09 18:10:03 +02:00
|
|
|
|
|
|
|
void TidalService::ShowConfig() {
|
|
|
|
app_->OpenSettingsDialogAtPage(SettingsDialog::Page_Tidal);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TidalService::ReloadSettings() {
|
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(TidalSettingsPage::kSettingsGroup);
|
2019-06-09 19:29:25 +02:00
|
|
|
|
2019-06-14 23:02:57 +02:00
|
|
|
//oauth_ = s.value("oauth", false).toBool();
|
|
|
|
oauth_ = false;
|
2019-06-09 19:29:25 +02:00
|
|
|
client_id_ = s.value("client_id").toString();
|
|
|
|
if (client_id_.isEmpty()) client_id_ = QString::fromUtf8(QByteArray::fromBase64(kClientIdB64));
|
|
|
|
api_token_ = s.value("api_token").toString();
|
|
|
|
if (api_token_.isEmpty()) api_token_ = QString::fromUtf8(QByteArray::fromBase64(kApiTokenB64));
|
|
|
|
|
2018-08-09 18:10:03 +02:00
|
|
|
username_ = s.value("username").toString();
|
2018-09-24 18:40:05 +02:00
|
|
|
QByteArray password = s.value("password").toByteArray();
|
|
|
|
if (password.isEmpty()) password_.clear();
|
|
|
|
else password_ = QString::fromUtf8(QByteArray::fromBase64(password));
|
2019-06-09 19:29:25 +02:00
|
|
|
|
2019-05-13 23:49:09 +02:00
|
|
|
quality_ = s.value("quality", "LOSSLESS").toString();
|
2019-02-12 21:58:03 +01:00
|
|
|
search_delay_ = s.value("searchdelay", 1500).toInt();
|
|
|
|
artistssearchlimit_ = s.value("artistssearchlimit", 5).toInt();
|
2018-09-21 18:53:27 +02:00
|
|
|
albumssearchlimit_ = s.value("albumssearchlimit", 100).toInt();
|
|
|
|
songssearchlimit_ = s.value("songssearchlimit", 100).toInt();
|
2018-09-06 17:39:26 +02:00
|
|
|
fetchalbums_ = s.value("fetchalbums", false).toBool();
|
|
|
|
coversize_ = s.value("coversize", "320x320").toString();
|
2019-05-27 21:10:37 +02:00
|
|
|
cache_album_covers_ = s.value("cachealbumcovers", true).toBool();
|
2019-06-09 19:29:25 +02:00
|
|
|
stream_url_method_ = static_cast<TidalSettingsPage::StreamUrlMethod>(s.value("streamurl").toInt());
|
|
|
|
|
2019-06-20 02:14:44 +02:00
|
|
|
user_id_ = s.value("user_id").toInt();
|
|
|
|
country_code_ = s.value("country_code", "US").toString();
|
2019-06-09 19:29:25 +02:00
|
|
|
access_token_ = s.value("access_token").toString();
|
|
|
|
refresh_token_ = s.value("refresh_token").toString();
|
|
|
|
session_id_ = s.value("session_id").toString();
|
|
|
|
expiry_time_ = s.value("expiry_time").toDateTime();
|
|
|
|
|
2018-08-09 18:10:03 +02:00
|
|
|
s.endGroup();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
QString TidalService::CoverCacheDir() {
|
|
|
|
return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/tidalalbumcovers";
|
|
|
|
}
|
|
|
|
|
2019-06-09 19:29:25 +02:00
|
|
|
void TidalService::StartAuthorisation() {
|
|
|
|
|
|
|
|
login_sent_ = true;
|
|
|
|
++login_attempts_;
|
|
|
|
if (timer_login_attempt_->isActive()) timer_login_attempt_->stop();
|
|
|
|
timer_login_attempt_->setInterval(kTimeResetLoginAttempts);
|
|
|
|
timer_login_attempt_->start();
|
|
|
|
|
2019-06-10 02:29:57 +02:00
|
|
|
code_verifier_ = Utilities::CryptographicRandomString(44);
|
|
|
|
code_challenge_ = QString(QCryptographicHash::hash(code_verifier_.toUtf8(), QCryptographicHash::Sha256).toBase64(QByteArray::Base64UrlEncoding));
|
|
|
|
|
|
|
|
if (code_challenge_.lastIndexOf(QChar('=')) == code_challenge_.length() - 1) {
|
|
|
|
code_challenge_.chop(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ParamList params = ParamList() << Param("response_type", "code")
|
|
|
|
<< Param("code_challenge", code_challenge_)
|
|
|
|
<< Param("code_challenge_method", "S256")
|
|
|
|
<< Param("redirect_uri", kOAuthRedirectUrl)
|
|
|
|
<< Param("client_id", client_id_)
|
|
|
|
<< Param("scope", "r_usr w_usr");
|
2019-06-09 19:29:25 +02:00
|
|
|
|
|
|
|
QUrlQuery url_query;
|
|
|
|
for (const Param ¶m : params) {
|
|
|
|
EncodedParam encoded_param(QUrl::toPercentEncoding(param.first), QUrl::toPercentEncoding(param.second));
|
|
|
|
url_query.addQueryItem(encoded_param.first, encoded_param.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl url = QUrl(kOAuthUrl);
|
|
|
|
url.setQuery(url_query);
|
|
|
|
QDesktopServices::openUrl(url);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TidalService::AuthorisationUrlReceived(const QUrl &url) {
|
|
|
|
|
|
|
|
qLog(Debug) << "Tidal: Authorisation URL Received" << url;
|
|
|
|
|
|
|
|
QUrlQuery url_query(url);
|
|
|
|
|
|
|
|
if (url_query.hasQueryItem("token_type") && url_query.hasQueryItem("expires_in") && url_query.hasQueryItem("access_token")) {
|
|
|
|
|
|
|
|
access_token_ = url_query.queryItemValue("access_token").toUtf8();
|
|
|
|
int expires_in = url_query.queryItemValue("expires_in").toInt();
|
|
|
|
expiry_time_ = QDateTime::currentDateTime().addSecs(expires_in - 120);
|
|
|
|
session_id_.clear();
|
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(TidalSettingsPage::kSettingsGroup);
|
|
|
|
s.setValue("access_token", access_token_);
|
|
|
|
s.setValue("expiry_time", expiry_time_);
|
|
|
|
s.remove("refresh_token");
|
|
|
|
s.remove("session_id");
|
|
|
|
s.endGroup();
|
|
|
|
|
|
|
|
login_attempts_ = 0;
|
|
|
|
if (timer_login_attempt_->isActive()) timer_login_attempt_->stop();
|
|
|
|
|
|
|
|
emit LoginComplete(true);
|
|
|
|
emit LoginSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (url_query.hasQueryItem("code") && url_query.hasQueryItem("state")) {
|
|
|
|
|
|
|
|
QString code = url_query.queryItemValue("code");
|
|
|
|
QString state = url_query.queryItemValue("state");
|
|
|
|
|
|
|
|
const ParamList params = ParamList() << Param("code", code)
|
2019-06-10 02:29:57 +02:00
|
|
|
<< Param("client_id", client_id_)
|
|
|
|
<< Param("grant_type", "authorization_code")
|
|
|
|
<< Param("redirect_uri", kOAuthRedirectUrl)
|
|
|
|
<< Param("scope", "r_usr w_usr")
|
|
|
|
<< Param("code_verifier", code_verifier_);
|
2019-06-09 19:29:25 +02:00
|
|
|
|
|
|
|
QUrlQuery url_query;
|
|
|
|
for (const Param ¶m : params) {
|
|
|
|
EncodedParam encoded_param(QUrl::toPercentEncoding(param.first), QUrl::toPercentEncoding(param.second));
|
|
|
|
url_query.addQueryItem(encoded_param.first, encoded_param.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl url(kOAuthAccessTokenUrl);
|
|
|
|
QNetworkRequest request = QNetworkRequest(url);
|
|
|
|
QByteArray query = url_query.toString(QUrl::FullyEncoded).toUtf8();
|
|
|
|
QNetworkReply *reply = network_->post(request, query);
|
|
|
|
NewClosure(reply, SIGNAL(finished()), this, SLOT(AccessTokenRequestFinished(QNetworkReply*)), reply);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
LoginError(tr("Reply from Tidal is missing query items."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TidalService::AccessTokenRequestFinished(QNetworkReply *reply) {
|
|
|
|
|
|
|
|
reply->deleteLater();
|
|
|
|
|
|
|
|
login_sent_ = false;
|
|
|
|
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
if (reply->error() < 200) {
|
|
|
|
// This is a network error, there is nothing more to do.
|
|
|
|
LoginError(QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// See if there is Json data containing "redirectUri" then use that instead.
|
|
|
|
QByteArray data(reply->readAll());
|
|
|
|
QJsonParseError json_error;
|
|
|
|
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
|
|
|
|
QString failure_reason;
|
|
|
|
if (json_error.error == QJsonParseError::NoError && !json_doc.isNull() && !json_doc.isEmpty() && json_doc.isObject()) {
|
|
|
|
QJsonObject json_obj = json_doc.object();
|
2019-06-10 02:29:57 +02:00
|
|
|
if (!json_obj.isEmpty() && json_obj.contains("status") && json_obj.contains("userMessage")) {
|
|
|
|
int status = json_obj["status"].toInt();
|
|
|
|
int sub_status = json_obj["subStatus"].toInt();
|
|
|
|
QString user_message = json_obj["userMessage"].toString();
|
|
|
|
failure_reason = QString("Authentication failure: %1 (%2) (%3)").arg(user_message).arg(status).arg(sub_status);
|
2019-06-09 19:29:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (failure_reason.isEmpty()) {
|
|
|
|
failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
|
|
|
|
}
|
|
|
|
LoginError(failure_reason);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 23:39:16 +02:00
|
|
|
int http_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
if (http_code != 200) {
|
|
|
|
LoginError(QString("Received HTTP code %1").arg(http_code));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-09 19:29:25 +02:00
|
|
|
QByteArray data(reply->readAll());
|
|
|
|
QJsonParseError json_error;
|
|
|
|
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
|
|
|
|
|
|
|
|
if (json_error.error != QJsonParseError::NoError) {
|
|
|
|
LoginError("Authentication reply from server missing Json data.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_doc.isNull() || json_doc.isEmpty()) {
|
|
|
|
LoginError("Authentication reply from server has empty Json document.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json_doc.isObject()) {
|
|
|
|
LoginError("Authentication reply from server has Json document that is not an object.", json_doc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject json_obj = json_doc.object();
|
|
|
|
if (json_obj.isEmpty()) {
|
|
|
|
LoginError("Authentication reply from server has empty Json object.", json_doc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json_obj.contains("access_token") ||
|
|
|
|
!json_obj.contains("refresh_token") ||
|
|
|
|
!json_obj.contains("expires_in") ||
|
|
|
|
!json_obj.contains("user")
|
|
|
|
) {
|
|
|
|
LoginError("Authentication reply from server is missing access_token, refresh_token, expires_in or user", json_obj);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
access_token_ = json_obj["access_token"].toString();
|
|
|
|
refresh_token_ = json_obj["refresh_token"].toString();
|
|
|
|
int expires_in = json_obj["expires_in"].toInt();
|
|
|
|
expiry_time_ = QDateTime::currentDateTime().addSecs(expires_in - 120);
|
|
|
|
|
|
|
|
QJsonValue json_user = json_obj["user"];
|
|
|
|
if (!json_user.isObject()) {
|
|
|
|
LoginError("Authentication reply from server has Json user that is not an object.", json_doc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QJsonObject json_obj_user = json_user.toObject();
|
|
|
|
if (json_obj_user.isEmpty()) {
|
|
|
|
LoginError("Authentication reply from server has empty Json user object.", json_doc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
country_code_ = json_obj_user["countryCode"].toString();
|
|
|
|
user_id_ = json_obj_user["userId"].toInt();
|
|
|
|
session_id_.clear();
|
2018-08-09 18:10:03 +02:00
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(TidalSettingsPage::kSettingsGroup);
|
2019-06-09 19:29:25 +02:00
|
|
|
s.setValue("access_token", access_token_);
|
|
|
|
s.setValue("refresh_token", refresh_token_);
|
|
|
|
s.setValue("expiry_time", expiry_time_);
|
|
|
|
s.setValue("country_code", country_code_);
|
|
|
|
s.setValue("user_id", user_id_);
|
|
|
|
s.remove("session_id");
|
2018-08-09 18:10:03 +02:00
|
|
|
s.endGroup();
|
|
|
|
|
2019-06-09 19:29:25 +02:00
|
|
|
qLog(Debug) << "Tidal: Login successful" << "user id" << user_id_ << "access token" << access_token_;
|
|
|
|
|
|
|
|
login_attempts_ = 0;
|
|
|
|
if (timer_login_attempt_->isActive()) timer_login_attempt_->stop();
|
|
|
|
|
|
|
|
emit LoginComplete(true);
|
|
|
|
emit LoginSuccess();
|
|
|
|
|
2018-08-09 18:10:03 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 22:13:30 +02:00
|
|
|
void TidalService::SendLogin() {
|
2019-06-09 19:29:25 +02:00
|
|
|
SendLogin(username_, password_, api_token_);
|
2018-09-10 21:23:50 +02:00
|
|
|
}
|
|
|
|
|
2019-05-13 23:49:09 +02:00
|
|
|
void TidalService::SendLogin(const QString &username, const QString &password, const QString &token) {
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
emit UpdateStatus(tr("Authenticating..."));
|
2018-08-09 18:10:03 +02:00
|
|
|
|
|
|
|
login_sent_ = true;
|
2019-05-27 21:10:37 +02:00
|
|
|
++login_attempts_;
|
2018-10-13 00:18:38 +02:00
|
|
|
if (timer_login_attempt_->isActive()) timer_login_attempt_->stop();
|
|
|
|
timer_login_attempt_->setInterval(kTimeResetLoginAttempts);
|
|
|
|
timer_login_attempt_->start();
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-06-09 19:29:25 +02:00
|
|
|
const ParamList params = ParamList() << Param("token", (token.isEmpty() ? api_token_ : token))
|
|
|
|
<< Param("username", username)
|
|
|
|
<< Param("password", password)
|
|
|
|
<< Param("clientVersion", "2.2.1--7");
|
2018-08-09 18:10:03 +02:00
|
|
|
|
|
|
|
QUrlQuery url_query;
|
2019-05-27 21:10:37 +02:00
|
|
|
for (const Param ¶m : params) {
|
|
|
|
EncodedParam encoded_param(QUrl::toPercentEncoding(param.first), QUrl::toPercentEncoding(param.second));
|
|
|
|
url_query.addQueryItem(encoded_param.first, encoded_param.second);
|
2018-08-09 18:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QUrl url(kAuthUrl);
|
|
|
|
QNetworkRequest req(url);
|
|
|
|
|
2019-05-05 21:20:28 +02:00
|
|
|
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
2019-06-09 19:29:25 +02:00
|
|
|
req.setRawHeader("X-Tidal-Token", (token.isEmpty() ? api_token_.toUtf8() : token.toUtf8()));
|
2019-05-05 21:20:28 +02:00
|
|
|
|
2019-06-09 19:29:25 +02:00
|
|
|
QByteArray query = url_query.toString(QUrl::FullyEncoded).toUtf8();
|
|
|
|
QNetworkReply *reply = network_->post(req, query);
|
2018-09-20 22:13:30 +02:00
|
|
|
NewClosure(reply, SIGNAL(finished()), this, SLOT(HandleAuthReply(QNetworkReply*)), reply);
|
2019-06-09 19:29:25 +02:00
|
|
|
|
|
|
|
//qLog(Debug) << "Tidal: Sending request" << url << query;
|
2018-08-09 18:10:03 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-20 22:13:30 +02:00
|
|
|
void TidalService::HandleAuthReply(QNetworkReply *reply) {
|
2018-08-09 18:10:03 +02:00
|
|
|
|
|
|
|
reply->deleteLater();
|
|
|
|
|
|
|
|
login_sent_ = false;
|
|
|
|
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
if (reply->error() < 200) {
|
|
|
|
// This is a network error, there is nothing more to do.
|
2019-03-22 23:20:43 +01:00
|
|
|
LoginError(QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
|
2018-08-09 18:10:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
2019-03-31 03:41:25 +02:00
|
|
|
// See if there is Json data containing "status" and "userMessage" - then use that instead.
|
2018-08-09 18:10:03 +02:00
|
|
|
QByteArray data(reply->readAll());
|
2019-03-22 23:20:43 +01:00
|
|
|
QJsonParseError json_error;
|
|
|
|
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
|
2018-08-09 18:10:03 +02:00
|
|
|
QString failure_reason;
|
2019-03-22 23:20:43 +01:00
|
|
|
if (json_error.error == QJsonParseError::NoError && !json_doc.isNull() && !json_doc.isEmpty() && json_doc.isObject()) {
|
2018-08-09 18:10:03 +02:00
|
|
|
QJsonObject json_obj = json_doc.object();
|
2019-03-31 03:41:25 +02:00
|
|
|
if (!json_obj.isEmpty() && json_obj.contains("status") && json_obj.contains("userMessage")) {
|
2019-03-23 21:14:46 +01:00
|
|
|
int status = json_obj["status"].toInt();
|
|
|
|
int sub_status = json_obj["subStatus"].toInt();
|
|
|
|
QString user_message = json_obj["userMessage"].toString();
|
|
|
|
failure_reason = QString("Authentication failure: %1 (%2) (%3)").arg(user_message).arg(status).arg(sub_status);
|
2018-08-09 18:10:03 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-23 21:14:46 +01:00
|
|
|
if (failure_reason.isEmpty()) {
|
2018-08-09 18:10:03 +02:00
|
|
|
failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
|
|
|
|
}
|
2019-03-22 23:20:43 +01:00
|
|
|
LoginError(failure_reason);
|
2018-08-09 18:10:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray data(reply->readAll());
|
2019-03-22 23:20:43 +01:00
|
|
|
QJsonParseError json_error;
|
|
|
|
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-03-22 23:20:43 +01:00
|
|
|
if (json_error.error != QJsonParseError::NoError) {
|
|
|
|
LoginError("Authentication reply from server missing Json data.");
|
2018-08-09 18:10:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_doc.isNull() || json_doc.isEmpty()) {
|
2019-03-22 23:20:43 +01:00
|
|
|
LoginError("Authentication reply from server has empty Json document.");
|
2018-08-09 18:10:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json_doc.isObject()) {
|
2019-03-22 23:20:43 +01:00
|
|
|
LoginError("Authentication reply from server has Json document that is not an object.", json_doc);
|
2018-08-09 18:10:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject json_obj = json_doc.object();
|
|
|
|
if (json_obj.isEmpty()) {
|
2019-03-22 23:20:43 +01:00
|
|
|
LoginError("Authentication reply from server has empty Json object.", json_doc);
|
2018-08-09 18:10:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-22 23:20:43 +01:00
|
|
|
if (!json_obj.contains("userId") || !json_obj.contains("sessionId") || !json_obj.contains("countryCode") ) {
|
|
|
|
LoginError("Authentication reply from server is missing userId, sessionId or countryCode", json_obj);
|
2018-08-09 18:10:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
country_code_ = json_obj["countryCode"].toString();
|
|
|
|
session_id_ = json_obj["sessionId"].toString();
|
|
|
|
user_id_ = json_obj["userId"].toInt();
|
2019-06-09 19:29:25 +02:00
|
|
|
access_token_.clear();
|
|
|
|
refresh_token_.clear();
|
|
|
|
expiry_time_ = QDateTime();
|
2018-08-09 18:10:03 +02:00
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(TidalSettingsPage::kSettingsGroup);
|
2019-06-09 19:29:25 +02:00
|
|
|
s.remove("access_token");
|
|
|
|
s.remove("refresh_token");
|
|
|
|
s.remove("expiry_time");
|
2019-06-20 02:14:44 +02:00
|
|
|
s.setValue("user_id", user_id_);
|
|
|
|
s.setValue("session_id", session_id_);
|
|
|
|
s.setValue("country_code", country_code_);
|
2018-08-09 18:10:03 +02:00
|
|
|
s.endGroup();
|
|
|
|
|
|
|
|
qLog(Debug) << "Tidal: Login successful" << "user id" << user_id_ << "session id" << session_id_ << "country code" << country_code_;
|
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
login_attempts_ = 0;
|
|
|
|
if (timer_login_attempt_->isActive()) timer_login_attempt_->stop();
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
emit LoginComplete(true);
|
2018-08-09 18:10:03 +02:00
|
|
|
emit LoginSuccess();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TidalService::Logout() {
|
|
|
|
|
2019-06-09 19:29:25 +02:00
|
|
|
access_token_.clear();
|
2018-08-09 18:10:03 +02:00
|
|
|
session_id_.clear();
|
2019-06-09 19:29:25 +02:00
|
|
|
expiry_time_ = QDateTime();
|
2018-08-09 18:10:03 +02:00
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(TidalSettingsPage::kSettingsGroup);
|
2019-06-20 02:14:44 +02:00
|
|
|
s.remove("user_id");
|
|
|
|
s.remove("country_code");
|
2019-06-09 19:29:25 +02:00
|
|
|
s.remove("access_token");
|
2018-08-09 18:10:03 +02:00
|
|
|
s.remove("session_id");
|
2019-06-09 19:29:25 +02:00
|
|
|
s.remove("expiry_time");
|
2018-08-09 18:10:03 +02:00
|
|
|
s.endGroup();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-13 00:18:38 +02:00
|
|
|
void TidalService::ResetLoginAttempts() {
|
|
|
|
login_attempts_ = 0;
|
|
|
|
}
|
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
void TidalService::TryLogin() {
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
if (authenticated() || login_sent_) return;
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
if (login_attempts_ >= kLoginAttempts) {
|
|
|
|
emit LoginComplete(false, "Maximum number of login attempts reached.");
|
|
|
|
return;
|
|
|
|
}
|
2019-06-09 19:29:25 +02:00
|
|
|
if (api_token_.isEmpty()) {
|
2019-05-27 21:10:37 +02:00
|
|
|
emit LoginComplete(false, "Missing Tidal API token.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (username_.isEmpty()) {
|
|
|
|
emit LoginComplete(false, "Missing Tidal username.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (password_.isEmpty()) {
|
|
|
|
emit LoginComplete(false, "Missing Tidal password.");
|
|
|
|
return;
|
2018-08-09 18:10:03 +02:00
|
|
|
}
|
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
emit Login();
|
2018-08-09 18:10:03 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-30 18:06:48 +02:00
|
|
|
void TidalService::ResetArtistsRequest() {
|
|
|
|
|
|
|
|
if (artists_request_.get()) {
|
|
|
|
disconnect(artists_request_.get(), 0, nullptr, 0);
|
|
|
|
disconnect(this, 0, artists_request_.get(), 0);
|
|
|
|
artists_request_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
void TidalService::GetArtists() {
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-30 18:06:48 +02:00
|
|
|
ResetArtistsRequest();
|
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
artists_request_.reset(new TidalRequest(this, url_handler_, network_, TidalBaseRequest::QueryType_Artists, this));
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-30 18:06:48 +02:00
|
|
|
connect(artists_request_.get(), SIGNAL(ErrorSignal(QString)), SLOT(ArtistsErrorReceived(QString)));
|
|
|
|
connect(artists_request_.get(), SIGNAL(Results(SongList)), SLOT(ArtistsResultsReceived(SongList)));
|
2019-05-27 21:10:37 +02:00
|
|
|
connect(artists_request_.get(), SIGNAL(UpdateStatus(QString)), SIGNAL(ArtistsUpdateStatus(QString)));
|
|
|
|
connect(artists_request_.get(), SIGNAL(ProgressSetMaximum(int)), SIGNAL(ArtistsProgressSetMaximum(int)));
|
|
|
|
connect(artists_request_.get(), SIGNAL(UpdateProgress(int)), SIGNAL(ArtistsUpdateProgress(int)));
|
|
|
|
connect(this, SIGNAL(LoginComplete(bool, QString)), artists_request_.get(), SLOT(LoginComplete(bool, QString)));
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
artists_request_->Process();
|
2018-10-16 21:31:28 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-30 18:06:48 +02:00
|
|
|
void TidalService::ArtistsResultsReceived(SongList songs) {
|
|
|
|
|
|
|
|
emit ArtistsResults(songs);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TidalService::ArtistsErrorReceived(QString error) {
|
|
|
|
|
|
|
|
emit ArtistsError(error);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TidalService::ResetAlbumsRequest() {
|
2018-10-16 21:31:28 +02:00
|
|
|
|
2019-05-30 18:06:48 +02:00
|
|
|
if (albums_request_.get()) {
|
|
|
|
disconnect(albums_request_.get(), 0, nullptr, 0);
|
|
|
|
disconnect(this, 0, albums_request_.get(), 0);
|
|
|
|
albums_request_.reset();
|
|
|
|
}
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
}
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
void TidalService::GetAlbums() {
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-30 18:06:48 +02:00
|
|
|
ResetAlbumsRequest();
|
2019-05-27 21:10:37 +02:00
|
|
|
albums_request_.reset(new TidalRequest(this, url_handler_, network_, TidalBaseRequest::QueryType_Albums, this));
|
2019-05-30 18:06:48 +02:00
|
|
|
connect(albums_request_.get(), SIGNAL(ErrorSignal(QString)), SLOT(AlbumsErrorReceived(QString)));
|
|
|
|
connect(albums_request_.get(), SIGNAL(Results(SongList)), SLOT(AlbumsResultsReceived(SongList)));
|
2019-05-27 21:10:37 +02:00
|
|
|
connect(albums_request_.get(), SIGNAL(UpdateStatus(QString)), SIGNAL(AlbumsUpdateStatus(QString)));
|
|
|
|
connect(albums_request_.get(), SIGNAL(ProgressSetMaximum(int)), SIGNAL(AlbumsProgressSetMaximum(int)));
|
|
|
|
connect(albums_request_.get(), SIGNAL(UpdateProgress(int)), SIGNAL(AlbumsUpdateProgress(int)));
|
|
|
|
connect(this, SIGNAL(LoginComplete(bool, QString)), albums_request_.get(), SLOT(LoginComplete(bool, QString)));
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
albums_request_->Process();
|
2018-08-09 18:10:03 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-30 18:06:48 +02:00
|
|
|
void TidalService::AlbumsResultsReceived(SongList songs) {
|
|
|
|
|
|
|
|
emit AlbumsResults(songs);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TidalService::AlbumsErrorReceived(QString error) {
|
|
|
|
|
|
|
|
emit AlbumsError(error);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TidalService::ResetSongsRequest() {
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-30 18:06:48 +02:00
|
|
|
if (songs_request_.get()) {
|
|
|
|
disconnect(songs_request_.get(), 0, nullptr, 0);
|
|
|
|
disconnect(this, 0, songs_request_.get(), 0);
|
|
|
|
songs_request_.reset();
|
|
|
|
}
|
2019-02-12 21:58:03 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
void TidalService::GetSongs() {
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-30 18:06:48 +02:00
|
|
|
ResetSongsRequest();
|
2019-05-27 21:10:37 +02:00
|
|
|
songs_request_.reset(new TidalRequest(this, url_handler_, network_, TidalBaseRequest::QueryType_Songs, this));
|
2019-05-30 18:06:48 +02:00
|
|
|
connect(songs_request_.get(), SIGNAL(ErrorSignal(QString)), SLOT(SongsErrorReceived(QString)));
|
|
|
|
connect(songs_request_.get(), SIGNAL(Results(SongList)), SLOT(SongsResultsReceived(SongList)));
|
2019-05-27 21:10:37 +02:00
|
|
|
connect(songs_request_.get(), SIGNAL(UpdateStatus(QString)), SIGNAL(SongsUpdateStatus(QString)));
|
|
|
|
connect(songs_request_.get(), SIGNAL(ProgressSetMaximum(int)), SIGNAL(SongsProgressSetMaximum(int)));
|
|
|
|
connect(songs_request_.get(), SIGNAL(UpdateProgress(int)), SIGNAL(SongsUpdateProgress(int)));
|
|
|
|
connect(this, SIGNAL(LoginComplete(bool, QString)), songs_request_.get(), SLOT(LoginComplete(bool, QString)));
|
|
|
|
|
|
|
|
songs_request_->Process();
|
2018-08-09 18:10:03 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-30 18:06:48 +02:00
|
|
|
void TidalService::SongsResultsReceived(SongList songs) {
|
|
|
|
|
|
|
|
emit SongsResults(songs);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TidalService::SongsErrorReceived(QString error) {
|
|
|
|
|
|
|
|
emit SongsError(error);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-12 21:58:03 +01:00
|
|
|
int TidalService::Search(const QString &text, InternetSearch::SearchType type) {
|
2018-08-09 18:10:03 +02:00
|
|
|
|
|
|
|
pending_search_id_ = next_pending_search_id_;
|
2018-09-06 17:39:26 +02:00
|
|
|
pending_search_text_ = text;
|
2019-02-12 21:58:03 +01:00
|
|
|
pending_search_type_ = type;
|
2018-08-09 18:10:03 +02:00
|
|
|
|
|
|
|
next_pending_search_id_++;
|
|
|
|
|
|
|
|
if (text.isEmpty()) {
|
2019-02-12 21:58:03 +01:00
|
|
|
timer_search_delay_->stop();
|
2018-08-09 18:10:03 +02:00
|
|
|
return pending_search_id_;
|
|
|
|
}
|
2019-02-12 21:58:03 +01:00
|
|
|
timer_search_delay_->setInterval(search_delay_);
|
|
|
|
timer_search_delay_->start();
|
2018-08-09 18:10:03 +02:00
|
|
|
|
|
|
|
return pending_search_id_;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TidalService::StartSearch() {
|
|
|
|
|
2019-06-09 19:29:25 +02:00
|
|
|
if ((oauth_ && !authenticated()) || api_token_.isEmpty() || username_.isEmpty() || password_.isEmpty()) {
|
|
|
|
emit SearchError(pending_search_id_, tr("Not authenticated."));
|
2018-08-09 18:10:03 +02:00
|
|
|
next_pending_search_id_ = 1;
|
|
|
|
ShowConfig();
|
|
|
|
return;
|
|
|
|
}
|
2019-05-27 21:10:37 +02:00
|
|
|
|
2018-09-06 17:39:26 +02:00
|
|
|
search_id_ = pending_search_id_;
|
|
|
|
search_text_ = pending_search_text_;
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
SendSearch();
|
2018-08-09 18:10:03 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-06 17:39:26 +02:00
|
|
|
void TidalService::CancelSearch() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void TidalService::SendSearch() {
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
TidalBaseRequest::QueryType type;
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-02-12 21:58:03 +01:00
|
|
|
switch (pending_search_type_) {
|
|
|
|
case InternetSearch::SearchType_Artists:
|
2019-05-27 21:10:37 +02:00
|
|
|
type = TidalBaseRequest::QueryType_SearchArtists;
|
2019-02-12 21:58:03 +01:00
|
|
|
break;
|
|
|
|
case InternetSearch::SearchType_Albums:
|
2019-05-27 21:10:37 +02:00
|
|
|
type = TidalBaseRequest::QueryType_SearchAlbums;
|
2019-02-12 21:58:03 +01:00
|
|
|
break;
|
|
|
|
case InternetSearch::SearchType_Songs:
|
2019-05-27 21:10:37 +02:00
|
|
|
type = TidalBaseRequest::QueryType_SearchSongs;
|
2018-08-09 18:10:03 +02:00
|
|
|
break;
|
|
|
|
default:
|
2019-05-27 21:10:37 +02:00
|
|
|
//Error("Invalid search type.");
|
2019-02-12 21:58:03 +01:00
|
|
|
return;
|
2018-08-09 18:10:03 +02:00
|
|
|
}
|
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
search_request_.reset(new TidalRequest(this, url_handler_, network_, type, this));
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
connect(search_request_.get(), SIGNAL(SearchResults(int, SongList)), SIGNAL(SearchResults(int, SongList)));
|
|
|
|
connect(search_request_.get(), SIGNAL(ErrorSignal(int, QString)), SIGNAL(SearchError(int, QString)));
|
|
|
|
connect(search_request_.get(), SIGNAL(UpdateStatus(QString)), SIGNAL(SearchUpdateStatus(QString)));
|
|
|
|
connect(search_request_.get(), SIGNAL(ProgressSetMaximum(int)), SIGNAL(SearchProgressSetMaximum(int)));
|
|
|
|
connect(search_request_.get(), SIGNAL(UpdateProgress(int)), SIGNAL(SearchUpdateProgress(int)));
|
|
|
|
connect(this, SIGNAL(LoginComplete(bool, QString)), search_request_.get(), SLOT(LoginComplete(bool, QString)));
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
search_request_->Search(search_id_, search_text_);
|
|
|
|
search_request_->Process();
|
2018-08-09 18:10:03 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-20 22:13:30 +02:00
|
|
|
void TidalService::GetStreamURL(const QUrl &url) {
|
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
TidalStreamURLRequest *stream_url_req = new TidalStreamURLRequest(this, network_, url, this);
|
2019-06-07 20:23:05 +02:00
|
|
|
stream_url_requests_ << stream_url_req;
|
2018-08-09 18:10:03 +02:00
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
connect(stream_url_req, SIGNAL(TryLogin()), this, SLOT(TryLogin()));
|
2019-06-07 20:23:05 +02:00
|
|
|
connect(stream_url_req, SIGNAL(StreamURLFinished(QUrl, QUrl, Song::FileType, QString)), this, SLOT(HandleStreamURLFinished(QUrl, QUrl, Song::FileType, QString)));
|
2019-05-27 21:10:37 +02:00
|
|
|
connect(this, SIGNAL(LoginComplete(bool, QString)), stream_url_req, SLOT(LoginComplete(bool, QString)));
|
2018-09-06 22:34:18 +02:00
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
stream_url_req->Process();
|
2018-08-09 18:10:03 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-07 20:23:05 +02:00
|
|
|
void TidalService::HandleStreamURLFinished(const QUrl original_url, const QUrl stream_url, const Song::FileType filetype, QString error) {
|
|
|
|
|
|
|
|
TidalStreamURLRequest *stream_url_req = qobject_cast<TidalStreamURLRequest*>(sender());
|
|
|
|
if (!stream_url_req || !stream_url_requests_.contains(stream_url_req)) return;
|
2019-06-09 19:29:25 +02:00
|
|
|
stream_url_req->deleteLater();
|
2019-06-07 20:23:05 +02:00
|
|
|
stream_url_requests_.removeAll(stream_url_req);
|
|
|
|
|
|
|
|
emit StreamURLFinished(original_url, stream_url, filetype, error);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-22 23:20:43 +01:00
|
|
|
QString TidalService::LoginError(QString error, QVariant debug) {
|
|
|
|
|
2018-08-09 18:10:03 +02:00
|
|
|
qLog(Error) << "Tidal:" << error;
|
2018-10-16 21:31:28 +02:00
|
|
|
if (debug.isValid()) qLog(Debug) << debug;
|
2019-03-22 23:20:43 +01:00
|
|
|
|
2019-05-27 21:10:37 +02:00
|
|
|
emit LoginFailure(error);
|
|
|
|
emit LoginComplete(false, error);
|
2019-03-22 23:20:43 +01:00
|
|
|
|
|
|
|
return error;
|
|
|
|
|
2018-08-09 18:10:03 +02:00
|
|
|
}
|