PolkitQt-1

polkitqt1-gui-action.cpp
1/*
2 This file is part of the Polkit-qt project
3 SPDX-FileCopyrightText: 2009 Daniel Nicoletti <dantti85-pk@yahoo.com.br>
4 SPDX-FileCopyrightText: 2009 Dario Freddi <drf@kde.org>
5 SPDX-FileCopyrightText: 2009 Jaroslav Reznik <jreznik@redhat.com>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
10#include "polkitqt1-gui-action.h"
11#include "polkitqt1-authority.h"
12#include "polkitqt1-subject.h"
13
14#include <QCoreApplication>
15
16namespace PolkitQt1
17{
18
19namespace Gui
20{
21
22/**
23 * \internal
24 */
25class Q_DECL_HIDDEN Action::Private
26{
27public:
28 Private(Action *p);
29
30 Action *parent;
31
32 QString actionId;
33 Authority::Result pkResult;
34 qint64 targetPID;
35
36 void updateAction();
37 bool computePkResult();
38 void configChanged();
39
40 bool initiallyChecked;
41
42 // states data
43 bool selfBlockedVisible;
44 bool selfBlockedEnabled;
45 QString selfBlockedText;
46 QString selfBlockedWhatsThis;
47 QString selfBlockedToolTip;
48 QIcon selfBlockedIcon;
49
50 bool noVisible;
51 bool noEnabled;
52 QString noText;
53 QString noWhatsThis;
54 QString noToolTip;
55 QIcon noIcon;
56
57 bool authVisible;
58 bool authEnabled;
59 QString authText;
60 QString authWhatsThis;
61 QString authToolTip;
62 QIcon authIcon;
63
64 bool yesVisible;
65 bool yesEnabled;
66 QString yesText;
67 QString yesWhatsThis;
68 QString yesToolTip;
69 QIcon yesIcon;
70};
71
72Action::Private::Private(Action *p)
73 : parent(p)
74 , targetPID(getpid())
75{
76 initiallyChecked = false;
77
78 // Set the default values
79 selfBlockedVisible = true;
80 selfBlockedEnabled = false;
81
82 noVisible = true;
83 noEnabled = false;
84
85 authVisible = true;
86 authEnabled = true;
87
88 yesVisible = true;
89 yesEnabled = true;
90}
91
92Action::Action(const QString &actionId, QObject *parent)
93 : QAction(parent)
94 , d(new Private(this))
95{
96 // this must be called AFTER the values initialization
98
99 // track the config changes to update the action
100 connect(Authority::instance(), SIGNAL(configChanged()),
101 this, SLOT(configChanged()));
102 // for now we call config changed..
103 connect(Authority::instance(), SIGNAL(consoleKitDBChanged()),
104 this, SLOT(configChanged()));
105}
106
107Action::~Action()
108{
109 delete d;
110}
111
113{
114 switch (d->pkResult) {
115 case Authority::Yes:
117 // just Q_EMIT the 'activated' signal
119 return true;
120 break;
121 default:
122 case Authority::No:
123 if (d->noEnabled) {
124 /* If PolicyKit says no... and we got here.. it means
125 * that the user set the property "no-enabled" to
126 * TRUE..
127 *
128 * Hence, they probably have a good reason for doing
129 * this so do let the 'activate' signal propagate..
130 */
132 return true;
133 }
134 break;
135 }
136 return false;
137}
138
139void Action::setChecked(bool checked)
140{
141 // We store this as initiallyChecked
142 // to be able to undo changes in case the auth fails
143 d->initiallyChecked = checked;
145}
146
147void Action::Private::updateAction()
148{
149 if (Authority::instance()->hasError()) {
150 return;
151 }
152
153 switch (pkResult) {
154 default:
156 case Authority::No:
157 qobject_cast<QAction *>(parent)->setVisible(noVisible);
158 qobject_cast<QAction *>(parent)->setEnabled(noEnabled);
159 qobject_cast<QAction *>(parent)->setText(noText);
160 if (!noWhatsThis.isNull()) {
161 qobject_cast<QAction *>(parent)->setWhatsThis(noWhatsThis);
162 }
163 if (!noToolTip.isNull()) {
164 qobject_cast<QAction *>(parent)->setToolTip(noToolTip);
165 }
166 qobject_cast<QAction *>(parent)->setIcon(noIcon);
167 break;
168
170 qobject_cast<QAction *>(parent)->setVisible(authVisible);
171 qobject_cast<QAction *>(parent)->setEnabled(authEnabled);
172 qobject_cast<QAction *>(parent)->setText(authText);
173 if (!authWhatsThis.isNull()) {
174 qobject_cast<QAction *>(parent)->setWhatsThis(authWhatsThis);
175 }
176 if (!authToolTip.isNull()) {
177 qobject_cast<QAction *>(parent)->setToolTip(authToolTip);
178 }
179 qobject_cast<QAction *>(parent)->setIcon(authIcon);
180 break;
181 case Authority::Yes:
182 qobject_cast<QAction *>(parent)->setVisible(yesVisible);
183 qobject_cast<QAction *>(parent)->setEnabled(yesEnabled);
184 qobject_cast<QAction *>(parent)->setText(yesText);
185 if (!yesWhatsThis.isNull()) {
186 qobject_cast<QAction *>(parent)->setWhatsThis(yesWhatsThis);
187 }
188 if (!yesToolTip.isNull()) {
189 qobject_cast<QAction *>(parent)->setToolTip(yesToolTip);
190 }
191 qobject_cast<QAction *>(parent)->setIcon(yesIcon);
192 if (parent->isCheckable()) {
193 qobject_cast<QAction *>(parent)->setChecked(!initiallyChecked);
194 }
195 break;
196 }
197 Q_EMIT parent->dataChanged();
198}
199
200void Action::Private::configChanged()
201{
202 bool result_changed;
203 result_changed = computePkResult();
204 if (result_changed) {
205 updateAction();
206 }
207}
208
209bool Action::Private::computePkResult()
210{
211 Authority::Result old_result;
212 UnixProcessSubject subject(parent->targetPID());
213
214 old_result = pkResult;
215 pkResult = Authority::Unknown;
216
218
219 return old_result != pkResult;
220}
221
222qint64 Action::targetPID() const
223{
224 if (d->targetPID != 0) {
225 return d->targetPID;
226 } else {
228 }
229}
230
231void Action::setTargetPID(qint64 pid)
232{
233 d->targetPID = pid;
234
235 d->computePkResult();
236 d->updateAction();
237}
238
240{
241 return d->pkResult == Authority::Yes;
242}
243
244bool Action::is(const QString &other) const
245{
246 return d->actionId == other;
247}
248
250{
251 /*TODO: implement it? no negative authorizations available, no authorization db*/
252}
253
254void Action::setText(const QString &text, States states)
255{
256 if (states & All) {
257 d->selfBlockedText = text;
258 d->noText = text;
259 d->authText = text;
260 d->yesText = text;
261 } else if (states & Auth) {
262 d->authText = text;
263 } else if (states & No) {
264 d->noText = text;
265 } else if (states & SelfBlocked) {
266 d->selfBlockedText = text;
267 } else if (states & Yes) {
268 d->yesText = text;
269 }
270
271 d->updateAction();
272}
273
274QString Action::text(Action::State state) const
275{
276 switch (state) {
277 case Yes:
278 return d->yesText;
279 case No:
280 return d->noText;
281 case Auth:
282 return d->authText;
283 case SelfBlocked:
284 return d->selfBlockedText;
285 case None:
286 return QAction::text();
287 default:
288 return QString();
289 }
290}
291
293{
294 if (states & All) {
295 d->selfBlockedToolTip = toolTip;
296 d->noToolTip = toolTip;
297 d->authToolTip = toolTip;
298 d->yesToolTip = toolTip;
299 } else if (states & Auth) {
300 d->authToolTip = toolTip;
301 } else if (states & No) {
302 d->noToolTip = toolTip;
303 } else if (states & SelfBlocked) {
304 d->selfBlockedToolTip = toolTip;
305 } else if (states & Yes) {
306 d->yesToolTip = toolTip;
307 }
308
309 d->updateAction();
310}
311
312QString Action::toolTip(Action::State state) const
313{
314 switch (state) {
315 case Yes:
316 return d->yesToolTip;
317 case No:
318 return d->noToolTip;
319 case Auth:
320 return d->authToolTip;
321 case SelfBlocked:
322 return d->selfBlockedToolTip;
323 case None:
324 return QAction::toolTip();
325 default:
326 return QString();
327 }
328}
329
331{
332 if (states & All) {
333 d->selfBlockedWhatsThis = whatsThis;
334 d->noWhatsThis = whatsThis;
335 d->authWhatsThis = whatsThis;
336 d->yesWhatsThis = whatsThis;
337 } else if (states & Auth) {
338 d->authWhatsThis = whatsThis;
339 } else if (states & No) {
340 d->noWhatsThis = whatsThis;
341 } else if (states & SelfBlocked) {
342 d->selfBlockedWhatsThis = whatsThis;
343 } else if (states & Yes) {
344 d->yesWhatsThis = whatsThis;
345 }
346
347 d->updateAction();
348}
349
350QString Action::whatsThis(Action::State state) const
351{
352 switch (state) {
353 case Yes:
354 return d->yesWhatsThis;
355 case No:
356 return d->noWhatsThis;
357 case Auth:
358 return d->authWhatsThis;
359 case SelfBlocked:
360 return d->selfBlockedWhatsThis;
361 case None:
362 return QAction::whatsThis();
363 default:
364 return QString();
365 }
366}
367
368void Action::setIcon(const QIcon &icon, States states)
369{
370 if (states & All) {
371 d->selfBlockedIcon = icon;
372 d->noIcon = icon;
373 d->authIcon = icon;
374 d->yesIcon = icon;
375 } else if (states & Auth) {
376 d->authIcon = icon;
377 } else if (states & No) {
378 d->noIcon = icon;
379 } else if (states & SelfBlocked) {
380 d->selfBlockedIcon = icon;
381 } else if (states & Yes) {
382 d->yesIcon = icon;
383 }
384
385 d->updateAction();
386}
387
388QIcon Action::icon(Action::State state) const
389{
390 switch (state) {
391 case Yes:
392 return d->yesIcon;
393 case No:
394 return d->noIcon;
395 case Auth:
396 return d->authIcon;
397 case SelfBlocked:
398 return d->selfBlockedIcon;
399 case None:
400 return QAction::icon();
401 default:
402 return QIcon();
403 }
404}
405
407{
408 if (states & All) {
409 d->selfBlockedEnabled = enabled;
410 d->noEnabled = enabled;
411 d->authEnabled = enabled;
412 d->yesEnabled = enabled;
413 } else if (states & Auth) {
414 d->authEnabled = enabled;
415 } else if (states & No) {
416 d->noEnabled = enabled;
417 } else if (states & SelfBlocked) {
418 d->selfBlockedEnabled = enabled;
419 } else if (states & Yes) {
420 d->yesEnabled = enabled;
421 }
422
423 d->updateAction();
424}
425
426bool Action::isEnabled(Action::State state) const
427{
428 switch (state) {
429 case Yes:
430 return d->yesEnabled;
431 case No:
432 return d->noEnabled;
433 case Auth:
434 return d->authEnabled;
435 case SelfBlocked:
436 return d->selfBlockedEnabled;
437 case None:
438 return QAction::isEnabled();
439 default:
440 return false;
441 }
442}
443
445{
446 if (states & All) {
447 d->selfBlockedVisible = visible;
448 d->noVisible = visible;
449 d->authVisible = visible;
450 d->yesVisible = visible;
451 } else if (states & Auth) {
452 d->authVisible = visible;
453 } else if (states & No) {
454 d->noVisible = visible;
455 } else if (states & SelfBlocked) {
456 d->selfBlockedVisible = visible;
457 } else if (states & Yes) {
458 d->yesVisible = visible;
459 }
460
461 d->updateAction();
462}
463
464bool Action::isVisible(Action::State state) const
465{
466 switch (state) {
467 case Yes:
468 return d->yesVisible;
469 case No:
470 return d->noVisible;
471 case Auth:
472 return d->authVisible;
473 case SelfBlocked:
474 return d->selfBlockedVisible;
475 case None:
476 return QAction::isVisible();
477 default:
478 return false;
479 }
480}
481
483{
484 //TODO:
485 d->actionId = actionId;
486
487 d->computePkResult();
488 d->updateAction();
489}
490
491//--------------------------------------------------
492
494{
495 return d->actionId;
496}
497
498}
499
500}
501
502#include "moc_polkitqt1-gui-action.cpp"
@ Yes
The subject is authorized for the specified action
@ Challenge
The subject is authorized if more information is provided.
@ No
The subject is not authorized for the specified action
Result checkAuthorizationSync(const QString &actionId, const Subject &subject, AuthorizationFlags flags)
Synchronous version of the checkAuthorization method.
static Authority * instance(PolkitAuthority *authority=nullptr)
Returns the instance of Authority.
void setVisible(bool visible, States states=All)
Sets whether the current action is visible or not.
void setWhatsThis(const QString &whatsThis, States states=All)
Sets the whatsthis for the current action.
bool activate()
Use this slot if you want to activate the action.
void revoke()
This method can be used to revoke the authorization obtained for this action.
bool is(const QString &actionId) const
This method compares a PolicyKit action Id with the current one of the object.
void setEnabled(bool enabled, States states=All)
Sets whether the current action is enabled or not.
bool isAllowed() const
This method can be used to check the if the current action can be performed (i.e.
void setToolTip(const QString &toolTip, States states=All)
Sets the tooltip for the current action.
void setTargetPID(qint64 pid)
This function sets the process id of the target that should receive the authorization.
void setPolkitAction(const QString &actionId)
Changes the action being tracked.
void dataChanged()
Emitted when the PolicyKit result (PolKitResult) for the given action or the internal data changes (i...
void setText(const QString &text, States states=All)
Sets the text for the current action.
QString actionId() const
Returns the current action ID.
void authorized()
Emitted when using this class as a proxy for a given action, It's only emitted if the activate() slot...
void setIcon(const QIcon &icon, States states=All)
Sets the icon for the current action.
void setChecked(bool checked)
Defines the checked state.
bool isCheckable() const const
qint64 applicationPid()
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
T qobject_cast(QObject *object)
bool isNull() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 15 2024 11:56:27 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.