~ubuntu-branches/ubuntu/trusty/polkit-qt-1/trusty

« back to all changes in this revision

Viewing changes to gui/action.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Thomas
  • Date: 2009-12-13 09:08:07 UTC
  • Revision ID: james.westby@ubuntu.com-20091213090807-ibl3mdtee5964009
Tags: upstream-0.95.0~svn1057107
ImportĀ upstreamĀ versionĀ 0.95.0~svn1057107

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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>
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
21
 */
 
22
 
 
23
#include "action.h"
 
24
#include "authority.h"
 
25
#include "subject.h"
 
26
 
 
27
#include <QtCore/QCoreApplication>
 
28
 
 
29
using namespace PolkitQt;
 
30
 
 
31
/**
 
32
  * \internal
 
33
  */
 
34
class Action::Private
 
35
{
 
36
public:
 
37
    Private(Action *p);
 
38
 
 
39
    Action *parent;
 
40
 
 
41
    QString       actionId;
 
42
    Authority::Result  pkResult;
 
43
    qint64        targetPID;
 
44
 
 
45
    void                 updateAction();
 
46
    bool                 computePkResult();
 
47
    void                 configChanged();
 
48
 
 
49
    bool    initiallyChecked;
 
50
 
 
51
    // states data
 
52
    bool    selfBlockedVisible;
 
53
    bool    selfBlockedEnabled;
 
54
    QString selfBlockedText;
 
55
    QString selfBlockedWhatsThis;
 
56
    QString selfBlockedToolTip;
 
57
    QIcon   selfBlockedIcon;
 
58
 
 
59
    bool    noVisible;
 
60
    bool    noEnabled;
 
61
    QString noText;
 
62
    QString noWhatsThis;
 
63
    QString noToolTip;
 
64
    QIcon   noIcon;
 
65
 
 
66
    bool    authVisible;
 
67
    bool    authEnabled;
 
68
    QString authText;
 
69
    QString authWhatsThis;
 
70
    QString authToolTip;
 
71
    QIcon   authIcon;
 
72
 
 
73
    bool    yesVisible;
 
74
    bool    yesEnabled;
 
75
    QString yesText;
 
76
    QString yesWhatsThis;
 
77
    QString yesToolTip;
 
78
    QIcon   yesIcon;
 
79
};
 
80
 
 
81
Action::Private::Private(Action *p)
 
82
        : parent(p)
 
83
        , targetPID(getpid())
 
84
{
 
85
    initiallyChecked = false;
 
86
 
 
87
    // Set the default values
 
88
    selfBlockedVisible = true;
 
89
    selfBlockedEnabled = false;
 
90
 
 
91
    noVisible     = true;
 
92
    noEnabled     = false;
 
93
 
 
94
    authVisible   = true;
 
95
    authEnabled   = true;
 
96
 
 
97
    yesVisible    = true;
 
98
    yesEnabled    = true;
 
99
}
 
100
 
 
101
Action::Action(const QString &actionId, QObject *parent)
 
102
        : QAction(parent)
 
103
        , d(new Private(this))
 
104
{
 
105
    // this must be called AFTER the values initialization
 
106
    setPolkitAction(actionId);
 
107
 
 
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()));
 
114
}
 
115
 
 
116
Action::~Action()
 
117
{
 
118
    delete d;
 
119
}
 
120
 
 
121
bool Action::activate()
 
122
{
 
123
    switch (d->pkResult) {
 
124
    case Authority::Yes:
 
125
    case Authority::Challenge:
 
126
        // just emit the 'activated' signal
 
127
        emit activated();
 
128
        return true;
 
129
        break;
 
130
    default:
 
131
    case Authority::No:
 
132
        if (d->noEnabled) {
 
133
            /* If PolicyKit says no... and we got here.. it means
 
134
             * that the user set the property "no-enabled" to
 
135
             * TRUE..
 
136
             *
 
137
             * Hence, they probably have a good reason for doing
 
138
             * this so do let the 'activate' signal propagate..
 
139
             */
 
140
            emit activated();
 
141
            return true;
 
142
        }
 
143
        break;
 
144
    }
 
145
    return false;
 
146
}
 
147
 
 
148
void Action::setChecked(bool checked)
 
149
{
 
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);
 
154
}
 
155
 
 
156
void Action::Private::updateAction()
 
157
{
 
158
    if (Authority::instance()->hasError()) {
 
159
        return;
 
160
    }
 
161
 
 
162
    switch (pkResult) {
 
163
    default:
 
164
    case Authority::Unknown:
 
165
    case Authority::No:
 
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);
 
171
        }
 
172
        if (!noToolTip.isNull()) {
 
173
            qobject_cast<QAction *>(parent)->setToolTip(noToolTip);
 
174
        }
 
175
        qobject_cast<QAction *>(parent)->setIcon(noIcon);
 
176
        break;
 
177
 
 
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);
 
184
        }
 
185
        if (!authToolTip.isNull()) {
 
186
            qobject_cast<QAction *>(parent)->setToolTip(authToolTip);
 
187
        }
 
188
        qobject_cast<QAction *>(parent)->setIcon(authIcon);
 
189
        break;
 
190
    case Authority::Yes:
 
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);
 
196
        }
 
197
        if (!yesToolTip.isNull()) {
 
198
            qobject_cast<QAction *>(parent)->setToolTip(yesToolTip);
 
199
        }
 
200
        qobject_cast<QAction *>(parent)->setIcon(yesIcon);
 
201
        if (parent->isCheckable()) {
 
202
            qobject_cast<QAction *>(parent)->setChecked(!initiallyChecked);
 
203
        }
 
204
        break;
 
205
    }
 
206
    emit parent->dataChanged();
 
207
}
 
208
 
 
209
void Action::Private::configChanged()
 
210
{
 
211
    bool result_changed;
 
212
    result_changed = computePkResult();
 
213
    if (result_changed) {
 
214
        updateAction();
 
215
    }
 
216
}
 
217
 
 
218
bool Action::Private::computePkResult()
 
219
{
 
220
    Authority::Result old_result;
 
221
    UnixProcessSubject *subject;
 
222
  
 
223
    subject = new UnixProcessSubject(parent->targetPID());
 
224
    
 
225
    old_result = pkResult;
 
226
    pkResult = Authority::Unknown;
 
227
 
 
228
    pkResult = Authority::instance()->checkAuthorizationSync(actionId, subject, Authority::None);
 
229
 
 
230
    delete subject;
 
231
    return old_result != pkResult;
 
232
}
 
233
 
 
234
qint64 Action::targetPID() const
 
235
{
 
236
    if (d->targetPID != 0)
 
237
        return d->targetPID;
 
238
    else
 
239
        return QCoreApplication::applicationPid();
 
240
}
 
241
 
 
242
void Action::setTargetPID(qint64 pid)
 
243
{
 
244
    d->targetPID = pid;
 
245
 
 
246
    d->computePkResult();
 
247
    d->updateAction();
 
248
}
 
249
 
 
250
bool Action::isAllowed() const
 
251
{
 
252
    return d->pkResult == Authority::Yes;
 
253
}
 
254
 
 
255
bool Action::is(const QString &other) const
 
256
{
 
257
    return d->actionId == other;
 
258
}
 
259
 
 
260
void Action::revoke()
 
261
{
 
262
/*TODO: implement it? no negative authorizations available, no authorization db*/
 
263
}
 
264
 
 
265
void Action::setText(const QString &text, States states)
 
266
{
 
267
    if (states & All) {
 
268
        d->selfBlockedText = text;
 
269
        d->noText = text;
 
270
        d->authText = text;
 
271
        d->yesText = text;
 
272
    } else if (states & Auth) {
 
273
        d->authText = text;
 
274
    } else if (states & No) {
 
275
        d->noText = text;
 
276
    } else if (states & SelfBlocked) {
 
277
        d->selfBlockedText = text;
 
278
    } else if (states & Yes) {
 
279
        d->yesText = text;
 
280
    }
 
281
 
 
282
    d->updateAction();
 
283
}
 
284
 
 
285
QString Action::text(Action::State state) const
 
286
{
 
287
    switch (state) {
 
288
    case Yes:
 
289
        return d->yesText;
 
290
    case No:
 
291
        return d->noText;
 
292
    case Auth:
 
293
        return d->authText;
 
294
    case SelfBlocked:
 
295
        return d->selfBlockedText;
 
296
    case None:
 
297
        return QAction::text();
 
298
    default:
 
299
        return QString();
 
300
    }
 
301
}
 
302
 
 
303
void Action::setToolTip(const QString &toolTip, States states)
 
304
{
 
305
    if (states & All) {
 
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;
 
318
    }
 
319
 
 
320
    d->updateAction();
 
321
}
 
322
 
 
323
QString Action::toolTip(Action::State state) const
 
324
{
 
325
    switch (state) {
 
326
    case Yes:
 
327
        return d->yesToolTip;
 
328
    case No:
 
329
        return d->noToolTip;
 
330
    case Auth:
 
331
        return d->authToolTip;
 
332
    case SelfBlocked:
 
333
        return d->selfBlockedToolTip;
 
334
    case None:
 
335
        return QAction::toolTip();
 
336
    default:
 
337
        return QString();
 
338
    }
 
339
}
 
340
 
 
341
void Action::setWhatsThis(const QString &whatsThis, States states)
 
342
{
 
343
    if (states & All) {
 
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;
 
356
    }
 
357
 
 
358
    d->updateAction();
 
359
}
 
360
 
 
361
QString Action::whatsThis(Action::State state) const
 
362
{
 
363
    switch (state) {
 
364
    case Yes:
 
365
        return d->yesWhatsThis;
 
366
    case No:
 
367
        return d->noWhatsThis;
 
368
    case Auth:
 
369
        return d->authWhatsThis;
 
370
    case SelfBlocked:
 
371
        return d->selfBlockedWhatsThis;
 
372
    case None:
 
373
        return QAction::whatsThis();
 
374
    default:
 
375
        return QString();
 
376
    }
 
377
}
 
378
 
 
379
void Action::setIcon(const QIcon &icon, States states)
 
380
{
 
381
    if (states & All) {
 
382
        d->selfBlockedIcon = icon;
 
383
        d->noIcon = icon;
 
384
        d->authIcon = icon;
 
385
        d->yesIcon = icon;
 
386
    } else if (states & Auth) {
 
387
        d->authIcon = icon;
 
388
    } else if (states & No) {
 
389
        d->noIcon = icon;
 
390
    } else if (states & SelfBlocked) {
 
391
        d->selfBlockedIcon = icon;
 
392
    } else if (states & Yes) {
 
393
        d->yesIcon = icon;
 
394
    }
 
395
 
 
396
    d->updateAction();
 
397
}
 
398
 
 
399
QIcon Action::icon(Action::State state) const
 
400
{
 
401
    switch (state) {
 
402
    case Yes:
 
403
        return d->yesIcon;
 
404
    case No:
 
405
        return d->noIcon;
 
406
    case Auth:
 
407
        return d->authIcon;
 
408
    case SelfBlocked:
 
409
        return d->selfBlockedIcon;
 
410
    case None:
 
411
        return QAction::icon();
 
412
    default:
 
413
        return QIcon();
 
414
    }
 
415
}
 
416
 
 
417
void Action::setEnabled(bool enabled, States states)
 
418
{
 
419
    if (states & All) {
 
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;
 
432
    }
 
433
 
 
434
    d->updateAction();
 
435
}
 
436
 
 
437
bool Action::isEnabled(Action::State state) const
 
438
{
 
439
    switch (state) {
 
440
    case Yes:
 
441
        return d->yesEnabled;
 
442
    case No:
 
443
        return d->noEnabled;
 
444
    case Auth:
 
445
        return d->authEnabled;
 
446
    case SelfBlocked:
 
447
        return d->selfBlockedEnabled;
 
448
    case None:
 
449
        return QAction::isEnabled();
 
450
    default:
 
451
        return false;
 
452
    }
 
453
}
 
454
 
 
455
void Action::setVisible(bool visible, States states)
 
456
{
 
457
    if (states & All) {
 
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;
 
470
    }
 
471
 
 
472
    d->updateAction();
 
473
}
 
474
 
 
475
bool Action::isVisible(Action::State state) const
 
476
{
 
477
    switch (state) {
 
478
    case Yes:
 
479
        return d->yesVisible;
 
480
    case No:
 
481
        return d->noVisible;
 
482
    case Auth:
 
483
        return d->authVisible;
 
484
    case SelfBlocked:
 
485
        return d->selfBlockedVisible;
 
486
    case None:
 
487
        return QAction::isVisible();
 
488
    default:
 
489
        return false;
 
490
    }
 
491
}
 
492
 
 
493
void Action::setPolkitAction(const QString &actionId)
 
494
{
 
495
    //TODO:
 
496
    d->actionId = actionId; 
 
497
 
 
498
    d->computePkResult();
 
499
    d->updateAction();
 
500
}
 
501
 
 
502
//--------------------------------------------------
 
503
 
 
504
QString Action::actionId() const
 
505
{
 
506
    return d->actionId;
 
507
}
 
508
 
 
509
#include "moc_action.cpp"