2020-09-27 18:38:33 +00:00
|
|
|
/*
|
|
|
|
Sailfin: a Jellyfin client written using Qt
|
2021-02-17 18:42:10 +00:00
|
|
|
Copyright (C) 2021 Chris Josten
|
2020-09-27 18:38:33 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library 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
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2021-02-17 18:42:10 +00:00
|
|
|
#include "JellyfinQt/apiclient.h"
|
2020-09-15 14:53:13 +00:00
|
|
|
|
2021-09-08 19:44:42 +00:00
|
|
|
#include <QSharedPointer>
|
|
|
|
|
|
|
|
#include "JellyfinQt/dto/clientcapabilitiesdto.h"
|
2021-03-28 02:00:00 +00:00
|
|
|
#include "JellyfinQt/support/jsonconv.h"
|
2021-09-03 01:47:25 +00:00
|
|
|
#include "JellyfinQt/viewmodel/settings.h"
|
2021-03-28 02:00:00 +00:00
|
|
|
#include "JellyfinQt/websocket.h"
|
|
|
|
|
|
|
|
|
2021-09-08 19:44:42 +00:00
|
|
|
|
2020-09-25 12:46:39 +00:00
|
|
|
namespace Jellyfin {
|
2020-10-08 01:00:08 +00:00
|
|
|
|
2021-09-03 01:47:25 +00:00
|
|
|
class ApiClientPrivate {
|
|
|
|
public:
|
|
|
|
ApiClientPrivate(ApiClient *parent);
|
|
|
|
virtual ~ApiClientPrivate() {}
|
|
|
|
|
|
|
|
// Exposed properties
|
|
|
|
ViewModel::Settings *settings;
|
|
|
|
WebSocket *webSocket;
|
|
|
|
EventBus *eventbus;
|
|
|
|
CredentialsManager *credManager;
|
|
|
|
|
|
|
|
// Authentication-related variables
|
|
|
|
QString token;
|
|
|
|
QString baseUrl;
|
|
|
|
QString deviceName;
|
|
|
|
QString deviceId;
|
|
|
|
QString userId;
|
|
|
|
|
|
|
|
bool online = true;
|
2021-09-08 19:44:42 +00:00
|
|
|
QSharedPointer<DTO::DeviceProfile> deviceProfile;
|
|
|
|
QSharedPointer<DTO::ClientCapabilitiesDto> clientCapabilities;
|
2021-09-08 21:20:12 +00:00
|
|
|
QList<DTO::GeneralCommandType> supportedCommands;
|
2021-09-03 01:47:25 +00:00
|
|
|
|
|
|
|
bool authenticated = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Retrieves the device ID or generates a random one.
|
|
|
|
*/
|
|
|
|
QUuid retrieveDeviceId() const;
|
|
|
|
|
2021-09-08 21:20:12 +00:00
|
|
|
bool componentBeingParsed = false;
|
|
|
|
|
2021-09-03 01:47:25 +00:00
|
|
|
};
|
|
|
|
|
2020-09-25 12:46:39 +00:00
|
|
|
ApiClient::ApiClient(QObject *parent)
|
2020-10-02 10:20:54 +00:00
|
|
|
: QObject(parent),
|
2021-09-03 01:47:25 +00:00
|
|
|
d_ptr(new ApiClientPrivate(this)) {
|
|
|
|
Q_D(ApiClient);
|
|
|
|
|
|
|
|
d->deviceId = Support::toString(d->retrieveDeviceId());
|
|
|
|
d->deviceName = QHostInfo::localHostName();
|
|
|
|
d->credManager = CredentialsManager::newInstance(this);
|
|
|
|
connect(d->credManager, &CredentialsManager::serversListed, this, &ApiClient::credManagerServersListed);
|
|
|
|
connect(d->credManager, &CredentialsManager::usersListed, this, &ApiClient::credManagerUsersListed);
|
|
|
|
connect(d->credManager, &CredentialsManager::tokenRetrieved, this, &ApiClient::credManagerTokenRetrieved);
|
2021-09-08 19:44:42 +00:00
|
|
|
connect(d->settings, &ViewModel::Settings::maxStreamingBitRateChanged, this, [d](qint32 newBitrate){
|
|
|
|
d->deviceProfile->setMaxStreamingBitrate(newBitrate);
|
|
|
|
});
|
2020-09-15 14:53:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 01:47:25 +00:00
|
|
|
ApiClient::~ApiClient() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ApiClient::authenticated() const {
|
|
|
|
Q_D(const ApiClient);
|
|
|
|
return d->authenticated;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString &ApiClient::baseUrl() const {
|
|
|
|
Q_D(const ApiClient);
|
|
|
|
return d->baseUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::setBaseUrl(const QString &url) {
|
|
|
|
Q_D(ApiClient);
|
|
|
|
d->baseUrl = url;
|
|
|
|
if (d->baseUrl.endsWith("/")) {
|
|
|
|
d->baseUrl.chop(1);
|
|
|
|
}
|
|
|
|
emit this->baseUrlChanged(d->baseUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString &ApiClient::userId() const {
|
|
|
|
Q_D(const ApiClient);
|
|
|
|
return d->userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::setUserId(const QString &userId) {
|
|
|
|
Q_D(ApiClient);
|
|
|
|
d->userId = userId;
|
|
|
|
emit userIdChanged(userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString &ApiClient::deviceId() const {
|
|
|
|
Q_D(const ApiClient);
|
|
|
|
return d->deviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventBus *ApiClient::eventbus() const {
|
|
|
|
Q_D(const ApiClient);
|
|
|
|
return d->eventbus;
|
|
|
|
}
|
|
|
|
WebSocket *ApiClient::websocket() const {
|
|
|
|
Q_D(const ApiClient);
|
|
|
|
return d->webSocket;
|
|
|
|
}
|
|
|
|
|
2020-10-08 01:00:08 +00:00
|
|
|
QString ApiClient::version() const {
|
|
|
|
return QString(SAILFIN_VERSION);
|
|
|
|
}
|
|
|
|
|
2021-09-03 01:47:25 +00:00
|
|
|
bool ApiClient::online() const {
|
|
|
|
Q_D(const ApiClient);
|
|
|
|
return d->online;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString &ApiClient::token() const {
|
|
|
|
Q_D(const ApiClient);
|
|
|
|
return d->token;
|
|
|
|
}
|
|
|
|
|
|
|
|
ViewModel::Settings *ApiClient::settings() const {
|
|
|
|
Q_D(const ApiClient);
|
|
|
|
return d->settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantList ApiClient::supportedCommands() const {
|
|
|
|
Q_D(const ApiClient);
|
2021-09-08 21:20:12 +00:00
|
|
|
QVariantList result;
|
|
|
|
result.reserve(d->supportedCommands.size());
|
|
|
|
for(auto it = d->supportedCommands.begin(); it != d->supportedCommands.end(); it++) {
|
|
|
|
result.append(QVariant::fromValue<DTO::GeneralCommandType>(*it));
|
|
|
|
}
|
|
|
|
return result;
|
2021-09-03 01:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::setSupportedCommands(QVariantList newSupportedCommands) {
|
|
|
|
Q_D(ApiClient);
|
2021-09-08 21:20:12 +00:00
|
|
|
d->supportedCommands.clear();
|
|
|
|
d->supportedCommands.reserve(newSupportedCommands.size());
|
|
|
|
for (int i = 0; i < newSupportedCommands.size(); i++) {
|
|
|
|
if (newSupportedCommands[i].canConvert<DTO::GeneralCommandType>()) {
|
|
|
|
d->supportedCommands.append(newSupportedCommands[i].value<DTO::GeneralCommandType>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qDebug() << "Supported commands changed: " << d->supportedCommands;
|
2021-09-03 01:47:25 +00:00
|
|
|
emit supportedCommandsChanged();
|
2021-09-08 21:20:12 +00:00
|
|
|
|
|
|
|
if (!d->componentBeingParsed) {
|
|
|
|
this->generateDeviceProfile();
|
|
|
|
}
|
2021-09-03 01:47:25 +00:00
|
|
|
}
|
2021-09-08 19:44:42 +00:00
|
|
|
QSharedPointer<DTO::DeviceProfile> ApiClient::deviceProfile() const {
|
2021-09-03 01:47:25 +00:00
|
|
|
Q_D(const ApiClient);
|
|
|
|
return d->deviceProfile;
|
|
|
|
}
|
2021-09-08 19:44:42 +00:00
|
|
|
|
|
|
|
const QJsonObject ApiClient::deviceProfileJson() const {
|
|
|
|
Q_D(const ApiClient);
|
|
|
|
return d->deviceProfile->toJson();
|
|
|
|
}
|
|
|
|
const QJsonObject ApiClient::clientCapabilities() const {
|
2021-09-03 01:47:25 +00:00
|
|
|
Q_D(const ApiClient);
|
2021-09-08 19:44:42 +00:00
|
|
|
return d->clientCapabilities->toJson();
|
2021-09-03 01:47:25 +00:00
|
|
|
}
|
2021-09-08 21:20:12 +00:00
|
|
|
|
|
|
|
// QQMLParserStatus implementation
|
|
|
|
void ApiClient::classBegin() {
|
|
|
|
Q_D(ApiClient);
|
|
|
|
d->componentBeingParsed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::componentComplete() {
|
|
|
|
Q_D(ApiClient);
|
|
|
|
d->componentBeingParsed = false;
|
|
|
|
|
|
|
|
// Generate the device profile after all properties have been parsed.
|
|
|
|
generateDeviceProfile();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-15 14:53:13 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// BASE HTTP METHODS //
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2021-09-03 01:47:25 +00:00
|
|
|
void ApiClient::addBaseRequestHeaders(QNetworkRequest &request, const QString &path, const QUrlQuery ¶ms) const {
|
|
|
|
Q_D(const ApiClient);
|
2020-09-25 12:46:39 +00:00
|
|
|
addTokenHeader(request);
|
|
|
|
request.setRawHeader("Accept", "application/json;"); // profile=\"CamelCase\"");
|
2020-10-08 01:00:08 +00:00
|
|
|
request.setHeader(QNetworkRequest::UserAgentHeader, QString("Sailfin/%1").arg(version()));
|
2021-09-03 01:47:25 +00:00
|
|
|
QString url = d->baseUrl + path;
|
2021-02-20 22:20:39 +00:00
|
|
|
if (!params.isEmpty()) url += "?" + params.toString(QUrl::EncodeReserved);
|
2020-09-25 12:46:39 +00:00
|
|
|
request.setUrl(url);
|
|
|
|
}
|
|
|
|
|
2021-09-03 01:47:25 +00:00
|
|
|
void ApiClient::addTokenHeader(QNetworkRequest &request) const {
|
|
|
|
Q_D(const ApiClient);
|
2020-09-26 02:13:21 +00:00
|
|
|
QString authentication = "MediaBrowser ";
|
2020-09-15 14:53:13 +00:00
|
|
|
authentication += "Client=\"Sailfin\"";
|
2021-09-03 01:47:25 +00:00
|
|
|
authentication += ", Device=\"" + d->deviceName + "\"";
|
|
|
|
authentication += ", DeviceId=\"" + d->deviceId + "\"";
|
2020-10-08 01:00:08 +00:00
|
|
|
authentication += ", Version=\"" + version() + "\"";
|
2021-09-03 01:47:25 +00:00
|
|
|
if (d->authenticated) {
|
2022-07-20 21:13:42 +00:00
|
|
|
authentication += ", Token=\"" + d->token + "\"";
|
2020-09-15 14:53:13 +00:00
|
|
|
}
|
|
|
|
request.setRawHeader("X-Emby-Authorization", authentication.toUtf8());
|
|
|
|
}
|
|
|
|
|
2020-09-25 12:46:39 +00:00
|
|
|
QNetworkReply *ApiClient::get(const QString &path, const QUrlQuery ¶ms) {
|
2020-09-15 14:53:13 +00:00
|
|
|
QNetworkRequest req;
|
|
|
|
addBaseRequestHeaders(req, path, params);
|
2020-09-25 12:46:39 +00:00
|
|
|
qDebug() << "GET " << req.url();
|
2020-09-15 14:53:13 +00:00
|
|
|
return m_naManager.get(req);
|
|
|
|
}
|
2020-09-25 12:46:39 +00:00
|
|
|
QNetworkReply *ApiClient::post(const QString &path, const QJsonDocument &data, const QUrlQuery ¶ms) {
|
2020-09-15 14:53:13 +00:00
|
|
|
|
|
|
|
QNetworkRequest req;
|
|
|
|
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
2020-09-25 12:46:39 +00:00
|
|
|
addBaseRequestHeaders(req, path, params);
|
|
|
|
qDebug() << "POST " << req.url();
|
2020-09-15 14:53:13 +00:00
|
|
|
if (data.isEmpty())
|
|
|
|
return m_naManager.post(req, QByteArray());
|
|
|
|
else {
|
|
|
|
return m_naManager.post(req, data.toJson(QJsonDocument::Compact));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 01:47:25 +00:00
|
|
|
QNetworkReply *ApiClient::post(const QString &path, const QByteArray &data, const QUrlQuery ¶ms) {
|
|
|
|
QNetworkRequest req;
|
|
|
|
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
|
|
addBaseRequestHeaders(req, path, params);
|
|
|
|
qDebug() << "POST " << req.url();
|
2021-10-25 14:11:10 +00:00
|
|
|
qDebug() << " DATA: " << data;
|
2021-09-03 01:47:25 +00:00
|
|
|
if (data.isEmpty())
|
|
|
|
return m_naManager.post(req, QByteArray());
|
|
|
|
else {
|
|
|
|
return m_naManager.post(req, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-15 14:53:13 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Nice to have methods //
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-09-25 12:46:39 +00:00
|
|
|
void ApiClient::restoreSavedSession(){
|
2021-09-03 01:47:25 +00:00
|
|
|
Q_D(ApiClient);
|
|
|
|
d->credManager->listServers();
|
2020-09-15 14:53:13 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 13:06:17 +00:00
|
|
|
void ApiClient::credManagerServersListed(QStringList servers) {
|
2021-09-03 01:47:25 +00:00
|
|
|
Q_D(ApiClient);
|
2021-07-31 13:06:17 +00:00
|
|
|
qDebug() << "Servers listed: " << servers;
|
|
|
|
if (servers.size() == 0) {
|
|
|
|
emit this->setupRequired();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//FIXME: support multiple servers
|
|
|
|
QString server = servers[0];
|
2021-09-03 01:47:25 +00:00
|
|
|
d->baseUrl = server;
|
2021-07-31 13:06:17 +00:00
|
|
|
qDebug() << "Chosen server: " << server;
|
2021-09-03 01:47:25 +00:00
|
|
|
d->credManager->listUsers(server);
|
2021-07-31 13:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::credManagerUsersListed(const QString &server, QStringList users) {
|
2021-09-03 01:47:25 +00:00
|
|
|
Q_D(ApiClient);
|
2021-07-31 13:06:17 +00:00
|
|
|
if (users.size() == 0) {
|
|
|
|
emit this->setupRequired();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//FIXME: support multiple users
|
|
|
|
QString user = users[0];
|
|
|
|
qDebug() << "Chosen user: " << user;
|
|
|
|
|
2021-09-03 01:47:25 +00:00
|
|
|
d->credManager->get(server, user);
|
2021-07-31 13:06:17 +00:00
|
|
|
}
|
|
|
|
void ApiClient::credManagerTokenRetrieved(const QString &server, const QString &user, const QString &token) {
|
2021-09-03 01:47:25 +00:00
|
|
|
Q_D(ApiClient);
|
|
|
|
d->token = token;
|
2021-08-11 21:35:33 +00:00
|
|
|
qDebug() << "Token retreived, logged in as user " << user;
|
|
|
|
this->setUserId(user);
|
|
|
|
this->setAuthenticated(true);
|
|
|
|
this->postCapabilities();
|
2021-07-31 13:06:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 12:46:39 +00:00
|
|
|
void ApiClient::setupConnection() {
|
2021-09-03 01:47:25 +00:00
|
|
|
Q_D(ApiClient);
|
2020-09-15 14:53:13 +00:00
|
|
|
// First detect redirects:
|
|
|
|
// Note that this is done without calling JellyfinApiClient::get since that automatically includes the base_url,
|
|
|
|
// which is something we want to avoid here.
|
2021-09-03 01:47:25 +00:00
|
|
|
QNetworkRequest req = QNetworkRequest(d->baseUrl);
|
2021-03-24 19:04:03 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
|
|
|
|
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
|
|
|
#else
|
2020-09-28 12:23:10 +00:00
|
|
|
req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
|
2021-03-24 19:04:03 +00:00
|
|
|
#endif
|
2020-09-28 12:23:10 +00:00
|
|
|
QNetworkReply *rep = m_naManager.get(req);
|
2020-09-15 14:53:13 +00:00
|
|
|
connect(rep, &QNetworkReply::finished, this, [rep, this](){
|
|
|
|
int status = statusCode(rep);
|
|
|
|
qDebug() << status;
|
|
|
|
|
2020-09-28 12:23:10 +00:00
|
|
|
QString newUrl = rep->url().toString();
|
|
|
|
// If the server wants to redirect us to their web interface, we have to chop the last part of the url off.
|
|
|
|
if (newUrl.endsWith("/web/index.html")) {
|
|
|
|
newUrl.chop(QString("/web/index.html").size());
|
|
|
|
this->setBaseUrl(newUrl);
|
2020-09-15 14:53:13 +00:00
|
|
|
}
|
2020-09-28 12:23:10 +00:00
|
|
|
this->getBrandingConfiguration();
|
2020-09-15 14:53:13 +00:00
|
|
|
rep->deleteLater();
|
|
|
|
});
|
2020-09-28 12:23:10 +00:00
|
|
|
connect(rep, &QNetworkReply::redirected, this, [req] (const QUrl &url) {
|
|
|
|
qDebug() << "Redirect from " << req.url() << " to " << url;
|
|
|
|
});
|
2020-09-26 21:29:45 +00:00
|
|
|
setDefaultErrorHandler(rep);
|
2020-09-15 14:53:13 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 12:46:39 +00:00
|
|
|
void ApiClient::getBrandingConfiguration() {
|
2020-09-15 14:53:13 +00:00
|
|
|
QNetworkReply *rep = get("/Branding/Configuration");
|
|
|
|
connect(rep, &QNetworkReply::finished, this, [rep, this]() {
|
|
|
|
qDebug() << "RESPONSE: " << statusCode(rep);
|
|
|
|
switch(statusCode(rep)) {
|
|
|
|
case 200:
|
|
|
|
QJsonDocument response = QJsonDocument::fromJson(rep->readAll());
|
|
|
|
if (response.isNull() || !response.isObject()) {
|
|
|
|
emit this->connectionFailed(ApiError::JSON_ERROR);
|
|
|
|
} else {
|
|
|
|
QJsonObject obj = response.object();
|
|
|
|
if (obj.contains("LoginDisclaimer")) {
|
|
|
|
qDebug() << "Login disclaimer: " << obj["LoginDisclaimer"];
|
|
|
|
emit this->connectionSuccess(obj["LoginDisclaimer"].toString());
|
|
|
|
} else {
|
|
|
|
emit this->connectionSuccess("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rep->deleteLater();
|
|
|
|
});
|
2020-09-26 21:29:45 +00:00
|
|
|
setDefaultErrorHandler(rep);
|
2020-09-15 14:53:13 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 12:46:39 +00:00
|
|
|
void ApiClient::authenticate(QString username, QString password, bool storeCredentials) {
|
2021-09-03 01:47:25 +00:00
|
|
|
Q_D(ApiClient);
|
2020-09-15 14:53:13 +00:00
|
|
|
QJsonObject requestData;
|
|
|
|
|
|
|
|
requestData["Username"] = username;
|
|
|
|
requestData["Pw"] = password;
|
2021-03-19 19:57:04 +00:00
|
|
|
QNetworkReply *rep = post("/Users/authenticatebyname", QJsonDocument(requestData));
|
2020-09-15 14:53:13 +00:00
|
|
|
connect(rep, &QNetworkReply::finished, this, [rep, username, storeCredentials, this]() {
|
2021-09-03 01:47:25 +00:00
|
|
|
Q_D(ApiClient);
|
2020-09-15 14:53:13 +00:00
|
|
|
int status = rep->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
qDebug() << "Got reply with status code " << status;
|
|
|
|
if (status >= 200 && status < 300) {
|
|
|
|
QJsonObject authInfo = QJsonDocument::fromJson(rep->readAll()).object();
|
2021-09-03 01:47:25 +00:00
|
|
|
d->token = authInfo["AccessToken"].toString();
|
2020-09-15 14:53:13 +00:00
|
|
|
|
2020-09-25 12:46:39 +00:00
|
|
|
// Fool this class's addRequestheaders to add the token, without
|
|
|
|
// notifying QML that we're authenticated, to prevent other requests going first.
|
2021-09-03 01:47:25 +00:00
|
|
|
d->authenticated = true;
|
2020-09-15 14:53:13 +00:00
|
|
|
this->setUserId(authInfo["User"].toObject()["Id"].toString());
|
2020-09-25 12:46:39 +00:00
|
|
|
this->postCapabilities();
|
|
|
|
this->setAuthenticated(true);
|
2020-09-15 14:53:13 +00:00
|
|
|
|
|
|
|
if (storeCredentials) {
|
2021-09-03 01:47:25 +00:00
|
|
|
d->credManager->store(d->baseUrl, d->userId, d->token);
|
2020-09-15 14:53:13 +00:00
|
|
|
}
|
2021-03-19 19:57:04 +00:00
|
|
|
} else if(status >= 400 && status < 500) {
|
|
|
|
if (status == 401) {
|
|
|
|
emit authenticationError(ApiError::INVALID_PASSWORD);
|
|
|
|
} else {
|
|
|
|
emit authenticationError(ApiError::UNEXPECTED_STATUS);
|
|
|
|
}
|
2020-09-15 14:53:13 +00:00
|
|
|
}
|
|
|
|
rep->deleteLater();
|
|
|
|
});
|
2020-09-26 21:29:45 +00:00
|
|
|
setDefaultErrorHandler(rep);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::deleteSession() {
|
|
|
|
QNetworkReply *rep = post("/Sessions/Logout");
|
|
|
|
connect(rep, &QNetworkReply::finished, this, [rep, this] {
|
2021-09-03 01:47:25 +00:00
|
|
|
Q_D(ApiClient);
|
|
|
|
d->credManager->remove(d->baseUrl, d->userId);
|
2020-09-26 21:29:45 +00:00
|
|
|
this->setAuthenticated(false);
|
|
|
|
emit this->setupRequired();
|
|
|
|
rep->deleteLater();
|
|
|
|
});
|
2020-09-15 14:53:13 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 12:46:39 +00:00
|
|
|
void ApiClient::postCapabilities() {
|
2021-09-03 01:47:25 +00:00
|
|
|
Q_D(const ApiClient);
|
2021-09-08 19:44:42 +00:00
|
|
|
QNetworkReply *rep = post("/Sessions/Capabilities/Full", QJsonDocument(d->clientCapabilities->toJson()));
|
2020-09-26 21:29:45 +00:00
|
|
|
setDefaultErrorHandler(rep);
|
2020-09-25 12:46:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-10 15:28:13 +00:00
|
|
|
QString ApiClient::downloadUrl(const QString &itemId) const {
|
2021-09-03 01:47:25 +00:00
|
|
|
Q_D(const ApiClient);
|
|
|
|
return d->baseUrl + "/Items/" + itemId + "/Download?api_key=" + d->token;
|
2020-10-10 15:28:13 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 12:46:39 +00:00
|
|
|
void ApiClient::generateDeviceProfile() {
|
2021-09-03 01:47:25 +00:00
|
|
|
Q_D(ApiClient);
|
2021-09-08 19:44:42 +00:00
|
|
|
QSharedPointer<DTO::DeviceProfile> deviceProfile = QSharedPointer<DTO::DeviceProfile>::create(Model::DeviceProfile::generateProfile());
|
|
|
|
deviceProfile->setName(d->deviceName);
|
|
|
|
deviceProfile->setJellyfinId(d->deviceId);
|
|
|
|
deviceProfile->setFriendlyName(QSysInfo::prettyProductName());
|
|
|
|
deviceProfile->setMaxStreamingBitrate(d->settings->maxStreamingBitRate());
|
|
|
|
d->deviceProfile = deviceProfile;
|
|
|
|
|
2021-10-25 14:11:10 +00:00
|
|
|
QSharedPointer<DTO::ClientCapabilitiesDto> clientCapabilities = QSharedPointer<DTO::ClientCapabilitiesDto>::create(true, // supports mediaControl
|
|
|
|
false, // supports content uploading
|
|
|
|
true, // supports persistent identifier
|
|
|
|
false, // supports sync
|
|
|
|
deviceProfile);
|
2021-09-08 19:44:42 +00:00
|
|
|
clientCapabilities->setPlayableMediaTypes({"Audio", "Video", "Photo"});
|
2021-09-08 21:20:12 +00:00
|
|
|
clientCapabilities->setSupportedCommands(d->supportedCommands);
|
2021-09-08 19:44:42 +00:00
|
|
|
clientCapabilities->setAppStoreUrl("https://chris.netsoj.nl/projects/harbour-sailfin");
|
|
|
|
clientCapabilities->setIconUrl("https://chris.netsoj.nl/static/img/logo.png");
|
|
|
|
|
|
|
|
d->clientCapabilities = clientCapabilities;
|
2021-02-13 20:42:57 +00:00
|
|
|
emit deviceProfileChanged();
|
2020-09-25 12:46:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::defaultNetworkErrorHandler(QNetworkReply::NetworkError error) {
|
2020-09-15 14:53:13 +00:00
|
|
|
QObject *signalSender = sender();
|
|
|
|
QNetworkReply *rep = dynamic_cast<QNetworkReply *>(signalSender);
|
|
|
|
if (rep != nullptr && statusCode(rep) == 401) {
|
2020-09-26 21:29:45 +00:00
|
|
|
this->setAuthenticated(false);
|
2021-03-24 19:04:03 +00:00
|
|
|
emit this->setupRequired();
|
2020-09-15 14:53:13 +00:00
|
|
|
emit this->authenticationError(ApiError::INVALID_PASSWORD);
|
2021-03-24 19:04:03 +00:00
|
|
|
rep->deleteLater();
|
2020-09-15 14:53:13 +00:00
|
|
|
} else {
|
|
|
|
emit this->networkError(error);
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 10:20:54 +00:00
|
|
|
|
2021-02-20 22:20:39 +00:00
|
|
|
void ApiClient::onUserDataChanged(const QString &itemId, UserData *userData) {
|
2021-02-17 18:42:10 +00:00
|
|
|
emit userDataChanged(itemId, userData);
|
2020-10-09 00:33:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 10:20:54 +00:00
|
|
|
void ApiClient::setAuthenticated(bool authenticated) {
|
2021-09-03 01:47:25 +00:00
|
|
|
Q_D(ApiClient);
|
|
|
|
d->authenticated = authenticated;
|
2020-10-02 10:20:54 +00:00
|
|
|
emit authenticatedChanged(authenticated);
|
|
|
|
}
|
2021-03-24 19:04:03 +00:00
|
|
|
|
2021-09-03 01:47:25 +00:00
|
|
|
QUuid ApiClientPrivate::retrieveDeviceId() const {
|
2021-03-24 19:04:03 +00:00
|
|
|
// This should probably not block the main thread, but on the other side,
|
|
|
|
// the file is not too big.
|
|
|
|
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
|
|
|
|
|
|
|
QDir folder(path);
|
|
|
|
static QString fileName = QStringLiteral("deviceid");
|
|
|
|
if (!path.isEmpty() && !folder.exists(fileName)) {
|
|
|
|
folder.mkpath(".");
|
|
|
|
QFile uuidFile(folder.absoluteFilePath(fileName));
|
|
|
|
if (uuidFile.open(QIODevice::WriteOnly)) {
|
|
|
|
QUuid uuid = QUuid::createUuid();
|
|
|
|
uuidFile.write(uuid.toByteArray());
|
|
|
|
uuidFile.close();
|
|
|
|
return uuid;
|
|
|
|
} else {
|
|
|
|
qDebug() << "Could not persist device id";
|
|
|
|
return QUuid::createUuid();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QFile uuidFile(folder.absoluteFilePath(fileName));
|
|
|
|
if (uuidFile.open(QIODevice::ReadOnly)) {
|
|
|
|
QUuid uuid(uuidFile.readAll());
|
|
|
|
uuidFile.close();
|
|
|
|
if (uuid.isNull()) {
|
|
|
|
qDebug() << "UUID file contains junk. Recreating";
|
|
|
|
uuidFile.remove();
|
|
|
|
uuidFile.open(QIODevice::WriteOnly);
|
|
|
|
uuid = QUuid::createUuid();
|
|
|
|
uuidFile.write(uuid.toByteArray());
|
|
|
|
uuidFile.close();
|
|
|
|
}
|
|
|
|
return uuid;
|
|
|
|
} else {
|
|
|
|
qDebug() << "Could not open device id file. Generating a random one.";
|
|
|
|
return QUuid::createUuid();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 01:47:25 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// PRIVATE //
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
ApiClientPrivate::ApiClientPrivate(ApiClient *apiClient)
|
|
|
|
: settings(new ViewModel::Settings(apiClient)),
|
|
|
|
webSocket(new WebSocket(apiClient)),
|
|
|
|
eventbus(new EventBus(apiClient)) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-09-25 12:46:39 +00:00
|
|
|
}
|