2
* This file is part of the Polkit-qt project
3
* Copyright (C) 2009 Daniel Nicoletti <dantti85-pk@yahoo.com.br>
4
* Copyright (C) 2009 Dario Freddi <drf@kde.org>
5
* Copyright (C) 2009 Jaroslav Reznik <jreznik@redhat.com>
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Library General Public
9
* License as published by the Free Software Foundation; either
10
* version 2 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Library General Public License for more details.
17
* You should have received a copy of the GNU Library General Public License
18
* along with this library; see the file COPYING.LIB. If not, write to
19
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20
* Boston, MA 02110-1301, USA.
24
#include "authority.h"
27
#include <QtCore/QCoreApplication>
29
using namespace PolkitQt;
42
Authority::Result pkResult;
46
bool computePkResult();
49
bool initiallyChecked;
52
bool selfBlockedVisible;
53
bool selfBlockedEnabled;
54
QString selfBlockedText;
55
QString selfBlockedWhatsThis;
56
QString selfBlockedToolTip;
57
QIcon selfBlockedIcon;
69
QString authWhatsThis;
81
Action::Private::Private(Action *p)
85
initiallyChecked = false;
87
// Set the default values
88
selfBlockedVisible = true;
89
selfBlockedEnabled = false;
101
Action::Action(const QString &actionId, QObject *parent)
103
, d(new Private(this))
105
// this must be called AFTER the values initialization
106
setPolkitAction(actionId);
108
// track the config changes to update the action
109
connect(Authority::instance(), SIGNAL(configChanged()),
110
this, SLOT(configChanged()));
111
// for now we call config changed..
112
connect(Authority::instance(), SIGNAL(consoleKitDBChanged()),
113
this, SLOT(configChanged()));
121
bool Action::activate()
123
switch (d->pkResult) {
125
case Authority::Challenge:
126
// just emit the 'activated' signal
133
/* If PolicyKit says no... and we got here.. it means
134
* that the user set the property "no-enabled" to
137
* Hence, they probably have a good reason for doing
138
* this so do let the 'activate' signal propagate..
148
void Action::setChecked(bool checked)
150
// We store this as initiallyChecked
151
// to be able to undo changes in case the auth fails
152
d->initiallyChecked = checked;
153
QAction::setChecked(checked);
156
void Action::Private::updateAction()
158
if (Authority::instance()->hasError()) {
164
case Authority::Unknown:
166
qobject_cast<QAction *>(parent)->setVisible(noVisible);
167
qobject_cast<QAction *>(parent)->setEnabled(noEnabled);
168
qobject_cast<QAction *>(parent)->setText(noText);
169
if (!noWhatsThis.isNull()) {
170
qobject_cast<QAction *>(parent)->setWhatsThis(noWhatsThis);
172
if (!noToolTip.isNull()) {
173
qobject_cast<QAction *>(parent)->setToolTip(noToolTip);
175
qobject_cast<QAction *>(parent)->setIcon(noIcon);
178
case Authority::Challenge:
179
qobject_cast<QAction *>(parent)->setVisible(authVisible);
180
qobject_cast<QAction *>(parent)->setEnabled(authEnabled);
181
qobject_cast<QAction *>(parent)->setText(authText);
182
if (!authWhatsThis.isNull()) {
183
qobject_cast<QAction *>(parent)->setWhatsThis(authWhatsThis);
185
if (!authToolTip.isNull()) {
186
qobject_cast<QAction *>(parent)->setToolTip(authToolTip);
188
qobject_cast<QAction *>(parent)->setIcon(authIcon);
191
qobject_cast<QAction *>(parent)->setVisible(yesVisible);
192
qobject_cast<QAction *>(parent)->setEnabled(yesEnabled);
193
qobject_cast<QAction *>(parent)->setText(yesText);
194
if (!yesWhatsThis.isNull()) {
195
qobject_cast<QAction *>(parent)->setWhatsThis(yesWhatsThis);
197
if (!yesToolTip.isNull()) {
198
qobject_cast<QAction *>(parent)->setToolTip(yesToolTip);
200
qobject_cast<QAction *>(parent)->setIcon(yesIcon);
201
if (parent->isCheckable()) {
202
qobject_cast<QAction *>(parent)->setChecked(!initiallyChecked);
206
emit parent->dataChanged();
209
void Action::Private::configChanged()
212
result_changed = computePkResult();
213
if (result_changed) {
218
bool Action::Private::computePkResult()
220
Authority::Result old_result;
221
UnixProcessSubject *subject;
223
subject = new UnixProcessSubject(parent->targetPID());
225
old_result = pkResult;
226
pkResult = Authority::Unknown;
228
pkResult = Authority::instance()->checkAuthorizationSync(actionId, subject, Authority::None);
231
return old_result != pkResult;
234
qint64 Action::targetPID() const
236
if (d->targetPID != 0)
239
return QCoreApplication::applicationPid();
242
void Action::setTargetPID(qint64 pid)
246
d->computePkResult();
250
bool Action::isAllowed() const
252
return d->pkResult == Authority::Yes;
255
bool Action::is(const QString &other) const
257
return d->actionId == other;
260
void Action::revoke()
262
/*TODO: implement it? no negative authorizations available, no authorization db*/
265
void Action::setText(const QString &text, States states)
268
d->selfBlockedText = text;
272
} else if (states & Auth) {
274
} else if (states & No) {
276
} else if (states & SelfBlocked) {
277
d->selfBlockedText = text;
278
} else if (states & Yes) {
285
QString Action::text(Action::State state) const
295
return d->selfBlockedText;
297
return QAction::text();
303
void Action::setToolTip(const QString &toolTip, States states)
306
d->selfBlockedToolTip = toolTip;
307
d->noToolTip = toolTip;
308
d->authToolTip = toolTip;
309
d->yesToolTip = toolTip;
310
} else if (states & Auth) {
311
d->authToolTip = toolTip;
312
} else if (states & No) {
313
d->noToolTip = toolTip;
314
} else if (states & SelfBlocked) {
315
d->selfBlockedToolTip = toolTip;
316
} else if (states & Yes) {
317
d->yesToolTip = toolTip;
323
QString Action::toolTip(Action::State state) const
327
return d->yesToolTip;
331
return d->authToolTip;
333
return d->selfBlockedToolTip;
335
return QAction::toolTip();
341
void Action::setWhatsThis(const QString &whatsThis, States states)
344
d->selfBlockedWhatsThis = whatsThis;
345
d->noWhatsThis = whatsThis;
346
d->authWhatsThis = whatsThis;
347
d->yesWhatsThis = whatsThis;
348
} else if (states & Auth) {
349
d->authWhatsThis = whatsThis;
350
} else if (states & No) {
351
d->noWhatsThis = whatsThis;
352
} else if (states & SelfBlocked) {
353
d->selfBlockedWhatsThis = whatsThis;
354
} else if (states & Yes) {
355
d->yesWhatsThis = whatsThis;
361
QString Action::whatsThis(Action::State state) const
365
return d->yesWhatsThis;
367
return d->noWhatsThis;
369
return d->authWhatsThis;
371
return d->selfBlockedWhatsThis;
373
return QAction::whatsThis();
379
void Action::setIcon(const QIcon &icon, States states)
382
d->selfBlockedIcon = icon;
386
} else if (states & Auth) {
388
} else if (states & No) {
390
} else if (states & SelfBlocked) {
391
d->selfBlockedIcon = icon;
392
} else if (states & Yes) {
399
QIcon Action::icon(Action::State state) const
409
return d->selfBlockedIcon;
411
return QAction::icon();
417
void Action::setEnabled(bool enabled, States states)
420
d->selfBlockedEnabled = enabled;
421
d->noEnabled = enabled;
422
d->authEnabled = enabled;
423
d->yesEnabled = enabled;
424
} else if (states & Auth) {
425
d->authEnabled = enabled;
426
} else if (states & No) {
427
d->noEnabled = enabled;
428
} else if (states & SelfBlocked) {
429
d->selfBlockedEnabled = enabled;
430
} else if (states & Yes) {
431
d->yesEnabled = enabled;
437
bool Action::isEnabled(Action::State state) const
441
return d->yesEnabled;
445
return d->authEnabled;
447
return d->selfBlockedEnabled;
449
return QAction::isEnabled();
455
void Action::setVisible(bool visible, States states)
458
d->selfBlockedVisible = visible;
459
d->noVisible = visible;
460
d->authVisible = visible;
461
d->yesVisible = visible;
462
} else if (states & Auth) {
463
d->authVisible = visible;
464
} else if (states & No) {
465
d->noVisible = visible;
466
} else if (states & SelfBlocked) {
467
d->selfBlockedVisible = visible;
468
} else if (states & Yes) {
469
d->yesVisible = visible;
475
bool Action::isVisible(Action::State state) const
479
return d->yesVisible;
483
return d->authVisible;
485
return d->selfBlockedVisible;
487
return QAction::isVisible();
493
void Action::setPolkitAction(const QString &actionId)
496
d->actionId = actionId;
498
d->computePkResult();
502
//--------------------------------------------------
504
QString Action::actionId() const
509
#include "moc_action.cpp"