1
0
Fork 0
mirror of https://github.com/HenkKalkwater/harbour-sailfin.git synced 2025-09-05 18:22:46 +00:00

Added settings, logout and improved error states

This commit is contained in:
Chris Josten 2020-09-26 23:29:45 +02:00
parent edb514bf2d
commit 67c8621d6f
11 changed files with 264 additions and 42 deletions

View file

@ -33,7 +33,7 @@ void FallbackCredentialsManager::get(const QString &server, const QString &user)
}
void FallbackCredentialsManager::remove(const QString &server, const QString &user) {
m_settings.remove(urlToGroupName(server) + "/" + user);
m_settings.remove(urlToGroupName(server) + "/users/" + user);
}
void FallbackCredentialsManager::listServers() const {

View file

@ -132,12 +132,7 @@ void ApiClient::setupConnection() {
}
rep->deleteLater();
});
connect(rep, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),
this, [rep, this](QNetworkReply::NetworkError error) {
qDebug() << "Error from URL: " << rep->url();
emit this->networkError(error);
rep->deleteLater();
});
setDefaultErrorHandler(rep);
}
void ApiClient::getBrandingConfiguration() {
@ -162,11 +157,7 @@ void ApiClient::getBrandingConfiguration() {
}
rep->deleteLater();
});
connect(rep, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),
this, [rep, this](QNetworkReply::NetworkError error) {
emit this->networkError(error);
rep->deleteLater();
});
setDefaultErrorHandler(rep);
}
void ApiClient::authenticate(QString username, QString password, bool storeCredentials) {
@ -195,8 +186,17 @@ void ApiClient::authenticate(QString username, QString password, bool storeCrede
}
rep->deleteLater();
});
connect(rep, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),
this, &ApiClient::defaultNetworkErrorHandler);
setDefaultErrorHandler(rep);
}
void ApiClient::deleteSession() {
QNetworkReply *rep = post("/Sessions/Logout");
connect(rep, &QNetworkReply::finished, this, [rep, this] {
m_credManager->remove(m_baseUrl, m_userId);
this->setAuthenticated(false);
emit this->setupRequired();
rep->deleteLater();
});
}
void ApiClient::fetchItem(const QString &id) {
@ -209,6 +209,11 @@ void ApiClient::fetchItem(const QString &id) {
}
rep->deleteLater();
});
connect(rep, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),
this, [id, rep, this](QNetworkReply::NetworkError error) {
emit this->itemFetchFailed(id, error);
rep->deleteLater();
});
}
void ApiClient::postCapabilities() {
@ -221,8 +226,7 @@ void ApiClient::postCapabilities() {
capabilities["IconUrl"] = "https://chris.netsoj.nl/static/img/logo.png";
capabilities["DeviceProfile"] = m_deviceProfile;
QNetworkReply *rep = post("/Sessions/Capabilities/Full", QJsonDocument(capabilities));
connect(rep, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),
this, &ApiClient::defaultNetworkErrorHandler);
setDefaultErrorHandler(rep);
}
void ApiClient::generateDeviceProfile() {
@ -244,6 +248,7 @@ void ApiClient::defaultNetworkErrorHandler(QNetworkReply::NetworkError error) {
QObject *signalSender = sender();
QNetworkReply *rep = dynamic_cast<QNetworkReply *>(signalSender);
if (rep != nullptr && statusCode(rep) == 401) {
this->setAuthenticated(false);
emit this->authenticationError(ApiError::INVALID_PASSWORD);
} else {
emit this->networkError(error);

View file

@ -106,6 +106,7 @@ signals:
void userIdChanged(QString userId);
void itemFetched(const QString &itemId, const QJsonObject &result);
void itemFetchFailed(const QString &itemId, const QNetworkReply::NetworkError error);
public slots:
/**
@ -120,6 +121,12 @@ public slots:
*/
void setupConnection();
void authenticate(QString username, QString password, bool storeCredentials = false);
/**
* @brief Logs the user out and clears the session.
*/
void deleteSession();
void fetchItem(const QString &id);
/**
@ -157,6 +164,7 @@ protected:
* is a big mess and should be safely contained in it's own file.
*/
void generateDeviceProfile();
QString &token() { return m_token; }
private:
@ -206,6 +214,18 @@ private:
static inline int statusCode(QNetworkReply *rep) {
return rep->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
}
/**
* @brief Sets the error handler of a reply to this classes default error handler
* @param rep The reply to set the error handler on.
*
* Motivation for this helper is because I forget the correct signature each time, with all the
* funky casts.
*/
void setDefaultErrorHandler(QNetworkReply *rep) {
connect(rep, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),
this, &ApiClient::defaultNetworkErrorHandler);
}
};
} // NS Jellyfin