From 848239ce4b137d71d90d7581b1945c3c206c1c9b Mon Sep 17 00:00:00 2001 From: Vaxry Date: Wed, 16 Oct 2024 15:05:32 +0100 Subject: [PATCH] qml/core: minor improvements --- qml/main.qml | 10 ++++++++++ src/QMLIntegration.cpp | 4 ++++ src/QMLIntegration.hpp | 2 ++ src/core/Agent.cpp | 10 ---------- src/core/Agent.hpp | 3 +-- src/core/PolkitListener.cpp | 12 +++++++++--- 6 files changed, 26 insertions(+), 15 deletions(-) diff --git a/qml/main.qml b/qml/main.qml index 9fcd82e..f7eea40 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -13,6 +13,9 @@ ApplicationWindow { minimumWidth: windowWidth minimumHeight: windowHeight visible: true + onClosing: { + hpa.setResult("fail"); + } FontMetrics { id: fontMetrics @@ -71,6 +74,13 @@ ApplicationWindow { onFocusField: () => { passwordField.focus = true; } + onBlockInput: (block) => { + passwordField.readOnly = block; + if (!block) { + passwordField.focus = true; + passwordField.selectAll(); + } + } } } diff --git a/src/QMLIntegration.cpp b/src/QMLIntegration.cpp index eff2e5a..66dbaa4 100644 --- a/src/QMLIntegration.cpp +++ b/src/QMLIntegration.cpp @@ -27,3 +27,7 @@ void CQMLIntegration::setError(QString str) { void CQMLIntegration::focus() { emit focusField(); } + +void CQMLIntegration::setInputBlocked(bool blocked) { + emit blockInput(blocked); +} diff --git a/src/QMLIntegration.hpp b/src/QMLIntegration.hpp index c58fba4..eae432f 100644 --- a/src/QMLIntegration.hpp +++ b/src/QMLIntegration.hpp @@ -19,6 +19,7 @@ class CQMLIntegration : public QObject { void setError(QString str); void focus(); + void setInputBlocked(bool blocked); QString result = "fail", errorText = ""; @@ -33,4 +34,5 @@ class CQMLIntegration : public QObject { signals: void setErrorString(QString err); void focusField(); + void blockInput(bool block); }; diff --git a/src/core/Agent.cpp b/src/core/Agent.cpp index 1a95555..531c0f5 100644 --- a/src/core/Agent.cpp +++ b/src/core/Agent.cpp @@ -73,13 +73,10 @@ void CAgent::initAuthPrompt() { } bool CAgent::resultReady() { - std::lock_guard lg(lastAuthResult.resultMutex); - return !lastAuthResult.used; } void CAgent::submitResultThreadSafe(const std::string& result) { - std::lock_guard lg(lastAuthResult.resultMutex); lastAuthResult.used = false; lastAuthResult.result = result; @@ -92,10 +89,3 @@ void CAgent::submitResultThreadSafe(const std::string& result) { else listener.cancelPending(); } - -void CAgent::setAuthError(const QString& err) { - if (!authState.qmlIntegration) - return; - - authState.qmlIntegration->setErrorString(err); -} diff --git a/src/core/Agent.hpp b/src/core/Agent.hpp index dfe5a40..29aa782 100644 --- a/src/core/Agent.hpp +++ b/src/core/Agent.hpp @@ -26,7 +26,6 @@ class CAgent { void resetAuthState(); bool start(); void initAuthPrompt(); - void setAuthError(const QString& err); private: struct { @@ -36,7 +35,6 @@ class CAgent { } authState; struct { - std::mutex resultMutex; std::string result; bool used = true; } lastAuthResult; @@ -48,6 +46,7 @@ class CAgent { bool resultReady(); friend class CQMLIntegration; + friend class CPolkitListener; }; inline std::unique_ptr g_pAgent; \ No newline at end of file diff --git a/src/core/PolkitListener.cpp b/src/core/PolkitListener.cpp index 8fd9613..52092ca 100644 --- a/src/core/PolkitListener.cpp +++ b/src/core/PolkitListener.cpp @@ -2,6 +2,7 @@ #include #include "PolkitListener.hpp" +#include "../QMLIntegration.hpp" #include "Agent.hpp" #include @@ -81,8 +82,8 @@ void CPolkitListener::completed(bool gainedAuthorization) { session.gainedAuth = gainedAuthorization; - if (!gainedAuthorization) - g_pAgent->setAuthError("Authentication failed"); + if (!gainedAuthorization && g_pAgent->authState.qmlIntegration) + g_pAgent->authState.qmlIntegration->setError("Authentication failed"); finishAuth(); } @@ -90,7 +91,8 @@ void CPolkitListener::completed(bool gainedAuthorization) { void CPolkitListener::showError(const QString& text) { std::print("> PKS showError: {}\n", text.toStdString()); - g_pAgent->setAuthError(text); + if (g_pAgent->authState.qmlIntegration) + g_pAgent->authState.qmlIntegration->setError(text); } void CPolkitListener::showInfo(const QString& text) { @@ -105,6 +107,8 @@ void CPolkitListener::finishAuth() { if (!session.gainedAuth && !session.cancelled) { std::print("> finishAuth: Did not gain auth. Reattempting.\n"); + if (g_pAgent->authState.qmlIntegration) + g_pAgent->authState.qmlIntegration->blockInput(false); session.session->deleteLater(); reattempt(); return; @@ -128,6 +132,8 @@ void CPolkitListener::submitPassword(const QString& pass) { return; session.session->setResponse(pass); + if (g_pAgent->authState.qmlIntegration) + g_pAgent->authState.qmlIntegration->blockInput(true); } void CPolkitListener::cancelPending() {