Skip to content

Commit 1a01ccd

Browse files
committed
service/polkit: use bindable properties
1 parent a3834ed commit 1a01ccd

File tree

8 files changed

+172
-149
lines changed

8 files changed

+172
-149
lines changed

src/services/polkit/agentimpl.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ PolkitAgentImpl::~PolkitAgentImpl() {
3636
delete req;
3737
}
3838

39-
if (this->activeFlow) {
40-
this->activeFlow->cancelAuthenticationRequest();
41-
this->activeFlow->deleteLater();
39+
auto* flow = this->bActiveFlow.value();
40+
if (flow) {
41+
flow->cancelAuthenticationRequest();
42+
flow->deleteLater();
4243
}
4344

44-
if (this->isRegistered) qs_polkit_agent_unregister(this->listener.get());
45+
if (this->bIsRegistered.value()) qs_polkit_agent_unregister(this->listener.get());
4546
}
4647

4748
PolkitAgentImpl* PolkitAgentImpl::tryGetOrCreate(PolkitAgent* agent) {
@@ -70,7 +71,6 @@ PolkitAgentImpl* PolkitAgentImpl::tryTakeover(PolkitAgent* agent) {
7071
}
7172

7273
instance->qmlAgent = agent;
73-
emit agent->isRegisteredChanged();
7474

7575
return instance;
7676
}
@@ -82,13 +82,12 @@ void PolkitAgentImpl::onEndOfQmlAgent(PolkitAgent* agent) {
8282
}
8383
}
8484

85+
QBindable<AuthFlow*> PolkitAgentImpl::activeFlow() { return &this->bActiveFlow; }
86+
QBindable<bool> PolkitAgentImpl::isRegistered() { return &this->bIsRegistered; }
87+
8588
void PolkitAgentImpl::registerComplete(bool success) {
86-
if (success) {
87-
this->isRegistered = true;
88-
emit this->qmlAgent->isRegisteredChanged();
89-
} else {
90-
qCWarning(logPolkit) << "failed to register listener on path" << this->qmlAgent->path();
91-
}
89+
if (success) this->bIsRegistered = true;
90+
else qCWarning(logPolkit) << "failed to register listener on path" << this->qmlAgent->path();
9291
}
9392

9493
void PolkitAgentImpl::initiateAuthentication(AuthRequest* request) {
@@ -104,8 +103,9 @@ void PolkitAgentImpl::initiateAuthentication(AuthRequest* request) {
104103
void PolkitAgentImpl::cancelAuthentication(AuthRequest* request) {
105104
qCDebug(logPolkit) << "cancelling authentication request from agent";
106105

107-
if (this->activeFlow && this->activeFlow->authRequest() == request) {
108-
this->activeFlow->cancelFromAgent();
106+
auto* flow = this->bActiveFlow.value();
107+
if (flow && flow->authRequest() == request) {
108+
flow->cancelFromAgent();
109109
} else if (auto it = std::ranges::find(this->queuedRequests, request);
110110
it != this->queuedRequests.end())
111111
{
@@ -139,11 +139,11 @@ void PolkitAgentImpl::activateAuthenticationRequest() {
139139
return;
140140
}
141141

142-
this->activeFlow = new AuthFlow(req, std::move(identities));
142+
this->bActiveFlow = new AuthFlow(req, std::move(identities));
143143

144144
QObject::connect(
145-
this->activeFlow,
146-
&AuthFlow::completedChanged,
145+
this->bActiveFlow.value(),
146+
&AuthFlow::isCompletedChanged,
147147
this,
148148
&PolkitAgentImpl::finishAuthenticationRequest
149149
);
@@ -154,19 +154,17 @@ void PolkitAgentImpl::activateAuthenticationRequest() {
154154
}
155155

156156
void PolkitAgentImpl::finishAuthenticationRequest() {
157-
if (!this->activeFlow) return;
157+
if (!this->bActiveFlow.value()) return;
158158

159159
qCDebug(logPolkit) << "finishing authentication request for action"
160-
<< this->activeFlow->actionId();
160+
<< this->bActiveFlow.value()->actionId();
161161

162-
this->activeFlow->deleteLater();
163-
this->activeFlow = nullptr;
164-
emit this->qmlAgent->flowChanged();
162+
this->bActiveFlow.value()->deleteLater();
165163

166164
if (!this->queuedRequests.empty()) {
167165
this->activateAuthenticationRequest();
168166
} else {
169-
emit this->qmlAgent->isActiveChanged();
167+
this->bActiveFlow = nullptr;
170168
}
171169
}
172170
} // namespace qs::service::polkit

src/services/polkit/agentimpl.hpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <deque>
44

55
#include <qobject.h>
6+
#include <qproperty.h>
67

78
#include "flow.hpp"
89
#include "gobjectref.hpp"
@@ -25,10 +26,17 @@ class PolkitAgentImpl
2526
static PolkitAgentImpl* tryTakeover(PolkitAgent* agent);
2627
static void onEndOfQmlAgent(PolkitAgent* agent);
2728

29+
[[nodiscard]] QBindable<AuthFlow*> activeFlow();
30+
[[nodiscard]] QBindable<bool> isRegistered();
31+
2832
void initiateAuthentication(AuthRequest* request) override;
2933
void cancelAuthentication(AuthRequest* request) override;
3034
void registerComplete(bool success) override;
3135

36+
signals:
37+
void activeFlowChanged();
38+
void isRegisteredChanged();
39+
3240
private:
3341
PolkitAgentImpl(PolkitAgent* agent);
3442

@@ -40,13 +48,21 @@ class PolkitAgentImpl
4048
void finishAuthenticationRequest();
4149

4250
GObjectRef<QsPolkitAgent> listener;
43-
bool isRegistered = false;
44-
4551
PolkitAgent* qmlAgent = nullptr;
4652

47-
AuthFlow* activeFlow = nullptr;
4853
std::deque<AuthRequest*> queuedRequests;
4954

50-
friend class PolkitAgent;
55+
Q_OBJECT_BINDABLE_PROPERTY(
56+
PolkitAgentImpl,
57+
AuthFlow*,
58+
bActiveFlow,
59+
&PolkitAgentImpl::activeFlowChanged
60+
);
61+
Q_OBJECT_BINDABLE_PROPERTY(
62+
PolkitAgentImpl,
63+
bool,
64+
bIsRegistered,
65+
&PolkitAgentImpl::isRegisteredChanged
66+
);
5167
};
5268
} // namespace qs::service::polkit

src/services/polkit/flow.cpp

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ AuthFlow::AuthFlow(AuthRequest* request, QList<Identity*>&& identities, QObject*
2121
: QObject(parent)
2222
, mRequest(request)
2323
, mIdentities(std::move(identities))
24-
, mSelectedIdentity(this->mIdentities.isEmpty() ? nullptr : this->mIdentities.first()) {
24+
, bSelectedIdentity(this->mIdentities.isEmpty() ? nullptr : this->mIdentities.first()) {
2525
// We reject auth requests with no identities before a flow is created.
2626
// This should never happen.
27-
if (!this->mSelectedIdentity)
27+
if (!this->bSelectedIdentity.value())
2828
qCFatal(logPolkitState) << "AuthFlow created with no valid identities!";
2929

3030
for (auto* identity: this->mIdentities) {
@@ -42,38 +42,35 @@ const QString& AuthFlow::actionId() const { return this->mRequest->actionId; }
4242
const QString& AuthFlow::cookie() const { return this->mRequest->cookie; }
4343
const QList<Identity*>& AuthFlow::identities() const { return this->mIdentities; }
4444

45-
Identity* AuthFlow::selectedIdentity() const { return this->mSelectedIdentity; }
45+
QBindable<Identity*> AuthFlow::selectedIdentity() { return &this->bSelectedIdentity; }
4646

4747
void AuthFlow::setSelectedIdentity(Identity* identity) {
48-
if (this->mSelectedIdentity == identity) return;
48+
if (this->bSelectedIdentity.value() == identity) return;
4949
if (!identity) {
5050
qmlWarning(this) << "Cannot set selected identity to null.";
5151
return;
5252
}
53-
for (auto* id: this->mIdentities) {
54-
if (id == identity) {
55-
this->mSelectedIdentity = id;
56-
emit this->selectedIdentityChanged();
57-
return;
58-
}
59-
}
53+
this->bSelectedIdentity = identity;
54+
this->currentSession->cancel();
55+
this->setupSession();
6056
}
6157

62-
bool AuthFlow::isResponseRequired() const { return this->mIsResponseRequired; }
63-
const QString& AuthFlow::inputPrompt() const { return this->mResponseMessage; }
64-
bool AuthFlow::responseVisible() const { return this->mResponseVisible; }
65-
const QString& AuthFlow::supplementaryMessage() const { return this->mExtraMessage; }
66-
bool AuthFlow::supplementaryIsError() const { return this->mExtraIsError; }
67-
bool AuthFlow::isCompleted() const { return this->mIsCompleted; }
68-
bool AuthFlow::isSuccessful() const { return this->mIsSuccessful; }
58+
QBindable<bool> AuthFlow::isResponseRequired() { return &this->bIsResponseRequired; }
59+
QBindable<QString> AuthFlow::inputPrompt() { return &this->bInputPrompt; }
60+
QBindable<bool> AuthFlow::responseVisible() { return &this->bResponseVisible; }
61+
QBindable<QString> AuthFlow::supplementaryMessage() { return &this->bSupplementaryMessage; }
62+
QBindable<bool> AuthFlow::supplementaryIsError() { return &this->bSupplementaryIsError; }
63+
QBindable<bool> AuthFlow::isCompleted() { return &this->bIsCompleted; }
64+
QBindable<bool> AuthFlow::isSuccessful() { return &this->bIsSuccessful; }
65+
QBindable<bool> AuthFlow::isCancelled() { return &this->bIsCancelled; }
6966
AuthRequest* AuthFlow::authRequest() const { return this->mRequest; }
7067

7168
void AuthFlow::cancelFromAgent() {
7269
if (!this->currentSession) return;
7370

7471
qCDebug(logPolkitState) << "cancelling authentication request from agent";
7572

76-
this->mIsCancelled = true;
73+
this->bIsCancelled = true;
7774
this->currentSession->cancel();
7875

7976
emit this->authenticationRequestCancelled();
@@ -88,18 +85,17 @@ void AuthFlow::submit(const QString& value) {
8885

8986
this->currentSession->respond(value);
9087

91-
this->mIsResponseRequired = false;
92-
this->mResponseMessage.clear();
93-
this->mResponseVisible = false;
94-
emit this->responseRequestChanged();
88+
this->bIsResponseRequired = false;
89+
this->bInputPrompt = QString {};
90+
this->bResponseVisible = false;
9591
}
9692

9793
void AuthFlow::cancelAuthenticationRequest() {
9894
if (!this->currentSession) return;
9995

10096
qCDebug(logPolkitState) << "cancelling authentication request by user request";
10197

102-
this->mIsCancelled = true;
98+
this->bIsCancelled = true;
10399
this->currentSession->cancel();
104100

105101
this->mRequest->cancel("Authentication request cancelled by user.");
@@ -108,10 +104,14 @@ void AuthFlow::cancelAuthenticationRequest() {
108104
void AuthFlow::setupSession() {
109105
delete this->currentSession;
110106

111-
qCDebug(logPolkitState) << "setting up session for identity" << this->mSelectedIdentity->name();
107+
qCDebug(logPolkitState) << "setting up session for identity"
108+
<< this->bSelectedIdentity.value()->name();
112109

113-
this->currentSession =
114-
new Session(this->mSelectedIdentity->polkitIdentity.get(), this->mRequest->cookie, this);
110+
this->currentSession = new Session(
111+
this->bSelectedIdentity.value()->polkitIdentity.get(),
112+
this->mRequest->cookie,
113+
this
114+
);
115115
QObject::connect(this->currentSession, &Session::request, this, &AuthFlow::request);
116116
QObject::connect(this->currentSession, &Session::completed, this, &AuthFlow::completed);
117117
QObject::connect(this->currentSession, &Session::showError, this, &AuthFlow::showError);
@@ -120,39 +120,32 @@ void AuthFlow::setupSession() {
120120
}
121121

122122
void AuthFlow::clearState() {
123-
this->mIsResponseRequired = false;
124-
this->mResponseMessage.clear();
125-
this->mResponseVisible = false;
126-
this->mExtraMessage.clear();
127-
this->mExtraIsError = false;
128-
129-
emit this->responseRequestChanged();
130-
emit this->supplementaryChanged();
123+
this->bIsResponseRequired = false;
124+
this->bInputPrompt = QString {};
125+
this->bResponseVisible = false;
126+
this->bSupplementaryMessage = QString {};
127+
this->bSupplementaryIsError = false;
131128
}
132129

133130
void AuthFlow::request(const QString& message, bool echo) {
134-
this->mIsResponseRequired = true;
135-
this->mResponseMessage = message;
136-
this->mResponseVisible = echo;
137-
emit this->responseRequestChanged();
131+
this->bIsResponseRequired = true;
132+
this->bInputPrompt = message;
133+
this->bResponseVisible = echo;
138134
}
139135

140136
void AuthFlow::completed(bool gainedAuthorization) {
141137
qCDebug(logPolkitState) << "authentication session completed, gainedAuthorization ="
142-
<< gainedAuthorization << ", isCancelled =" << this->mIsCancelled;
138+
<< gainedAuthorization << ", isCancelled =" << this->bIsCancelled.value();
143139

144140
if (gainedAuthorization) {
145-
this->mIsCompleted = true;
146-
this->mIsSuccessful = true;
141+
this->bIsCompleted = true;
142+
this->bIsSuccessful = true;
147143
this->mRequest->complete();
148144

149-
emit this->completedChanged();
150145
emit this->authenticationSucceeded();
151-
} else if (this->mIsCancelled) {
152-
this->mIsCompleted = true;
153-
this->mIsSuccessful = false;
154-
155-
emit this->completedChanged();
146+
} else if (this->bIsCancelled.value()) {
147+
this->bIsCompleted = true;
148+
this->bIsSuccessful = false;
156149
} else {
157150
emit this->authenticationFailed();
158151

@@ -162,14 +155,12 @@ void AuthFlow::completed(bool gainedAuthorization) {
162155
}
163156

164157
void AuthFlow::showError(const QString& message) {
165-
this->mExtraMessage = message;
166-
this->mExtraIsError = true;
167-
emit this->supplementaryChanged();
158+
this->bSupplementaryMessage = message;
159+
this->bSupplementaryIsError = true;
168160
}
169161

170162
void AuthFlow::showInfo(const QString& message) {
171-
this->mExtraMessage = message;
172-
this->mExtraIsError = false;
173-
emit this->supplementaryChanged();
163+
this->bSupplementaryMessage = message;
164+
this->bSupplementaryIsError = false;
174165
}
175166
} // namespace qs::service::polkit

0 commit comments

Comments
 (0)