diff options
Diffstat (limited to 'gui/polkittqt1-gui-action.cpp')
-rw-r--r-- | gui/polkittqt1-gui-action.cpp | 515 |
1 files changed, 515 insertions, 0 deletions
diff --git a/gui/polkittqt1-gui-action.cpp b/gui/polkittqt1-gui-action.cpp new file mode 100644 index 000000000..31f01cfb7 --- /dev/null +++ b/gui/polkittqt1-gui-action.cpp @@ -0,0 +1,515 @@ +/* + * This file is part of the Polkit-tqt project + * Copyright (C) 2009 Daniel Nicoletti <dantti85-pk@yahoo.com.br> + * Copyright (C) 2009 Dario Freddi <drf@kde.org> + * Copyright (C) 2009 Jaroslav Reznik <jreznik@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "polkittqt1-gui-action.h" +#include "polkittqt1-authority.h" +#include "polkittqt1-subject.h" + +#include <TQtCore/TQCoreApplication> + +namespace PolkitTQt1 +{ + +namespace Gui +{ + +/** + * \internal + */ +class Action::Private +{ +public: + Private(Action *p); + + Action *parent; + + TQString actionId; + Authority::Result pkResult; + tqint64 targetPID; + + void updateAction(); + bool computePkResult(); + void configChanged(); + + bool initiallyChecked; + + // states data + bool selfBlockedVisible; + bool selfBlockedEnabled; + TQString selfBlockedText; + TQString selfBlockedWhatsThis; + TQString selfBlockedToolTip; + TQIcon selfBlockedIcon; + + bool noVisible; + bool noEnabled; + TQString noText; + TQString noWhatsThis; + TQString noToolTip; + TQIcon noIcon; + + bool authVisible; + bool authEnabled; + TQString authText; + TQString authWhatsThis; + TQString authToolTip; + TQIcon authIcon; + + bool yesVisible; + bool yesEnabled; + TQString yesText; + TQString yesWhatsThis; + TQString yesToolTip; + TQIcon yesIcon; +}; + +Action::Private::Private(Action *p) + : parent(p) + , targetPID(getpid()) +{ + initiallyChecked = false; + + // Set the default values + selfBlockedVisible = true; + selfBlockedEnabled = false; + + noVisible = true; + noEnabled = false; + + authVisible = true; + authEnabled = true; + + yesVisible = true; + yesEnabled = true; +} + +Action::Action(const TQString &actionId, TQObject *parent) + : TQAction(parent) + , d(new Private(this)) +{ + // this must be called AFTER the values initialization + setPolkitAction(actionId); + + // track the config changes to update the action + connect(Authority::instance(), SIGNAL(configChanged()), + this, SLOT(configChanged())); + // for now we call config changed.. + connect(Authority::instance(), SIGNAL(consoleKitDBChanged()), + this, SLOT(configChanged())); +} + +Action::~Action() +{ + delete d; +} + +bool Action::activate() +{ + switch (d->pkResult) { + case Authority::Yes: + case Authority::Challenge: + // just Q_EMIT the 'activated' signal + TQ_EMIT authorized(); + return true; + break; + default: + case Authority::No: + if (d->noEnabled) { + /* If PolicyKit says no... and we got here.. it means + * that the user set the property "no-enabled" to + * TRUE.. + * + * Hence, they probably have a good reason for doing + * this so do let the 'activate' signal propagate.. + */ + TQ_EMIT authorized(); + return true; + } + break; + } + return false; +} + +void Action::setChecked(bool checked) +{ + // We store this as initiallyChecked + // to be able to undo changes in case the auth fails + d->initiallyChecked = checked; + TQAction::setChecked(checked); +} + +void Action::Private::updateAction() +{ + if (Authority::instance()->hasError()) { + return; + } + + switch (pkResult) { + default: + case Authority::Unknown: + case Authority::No: + qobject_cast<TQAction *>(parent)->setVisible(noVisible); + qobject_cast<TQAction *>(parent)->setEnabled(noEnabled); + qobject_cast<TQAction *>(parent)->setText(noText); + if (!noWhatsThis.isNull()) { + qobject_cast<TQAction *>(parent)->setWhatsThis(noWhatsThis); + } + if (!noToolTip.isNull()) { + qobject_cast<TQAction *>(parent)->setToolTip(noToolTip); + } + qobject_cast<TQAction *>(parent)->setIcon(noIcon); + break; + + case Authority::Challenge: + qobject_cast<TQAction *>(parent)->setVisible(authVisible); + qobject_cast<TQAction *>(parent)->setEnabled(authEnabled); + qobject_cast<TQAction *>(parent)->setText(authText); + if (!authWhatsThis.isNull()) { + qobject_cast<TQAction *>(parent)->setWhatsThis(authWhatsThis); + } + if (!authToolTip.isNull()) { + qobject_cast<TQAction *>(parent)->setToolTip(authToolTip); + } + qobject_cast<TQAction *>(parent)->setIcon(authIcon); + break; + case Authority::Yes: + qobject_cast<TQAction *>(parent)->setVisible(yesVisible); + qobject_cast<TQAction *>(parent)->setEnabled(yesEnabled); + qobject_cast<TQAction *>(parent)->setText(yesText); + if (!yesWhatsThis.isNull()) { + qobject_cast<TQAction *>(parent)->setWhatsThis(yesWhatsThis); + } + if (!yesToolTip.isNull()) { + qobject_cast<TQAction *>(parent)->setToolTip(yesToolTip); + } + qobject_cast<TQAction *>(parent)->setIcon(yesIcon); + if (parent->isCheckable()) { + qobject_cast<TQAction *>(parent)->setChecked(!initiallyChecked); + } + break; + } + TQ_EMIT parent->dataChanged(); +} + +void Action::Private::configChanged() +{ + bool result_changed; + result_changed = computePkResult(); + if (result_changed) { + updateAction(); + } +} + +bool Action::Private::computePkResult() +{ + Authority::Result old_result; + UnixProcessSubject subject(parent->targetPID()); + + old_result = pkResult; + pkResult = Authority::Unknown; + + pkResult = Authority::instance()->checkAuthorizationSync(actionId, subject, Authority::None); + + return old_result != pkResult; +} + +tqint64 Action::targetPID() const +{ + if (d->targetPID != 0) { + return d->targetPID; + } else { + return TQCoreApplication::applicationPid(); + } +} + +void Action::setTargetPID(tqint64 pid) +{ + d->targetPID = pid; + + d->computePkResult(); + d->updateAction(); +} + +bool Action::isAllowed() const +{ + return d->pkResult == Authority::Yes; +} + +bool Action::is(const TQString &other) const +{ + return d->actionId == other; +} + +void Action::revoke() +{ + /*TODO: implement it? no negative authorizations available, no authorization db*/ +} + +void Action::setText(const TQString &text, States states) +{ + if (states & All) { + d->selfBlockedText = text; + d->noText = text; + d->authText = text; + d->yesText = text; + } else if (states & Auth) { + d->authText = text; + } else if (states & No) { + d->noText = text; + } else if (states & SelfBlocked) { + d->selfBlockedText = text; + } else if (states & Yes) { + d->yesText = text; + } + + d->updateAction(); +} + +TQString Action::text(Action::State state) const +{ + switch (state) { + case Yes: + return d->yesText; + case No: + return d->noText; + case Auth: + return d->authText; + case SelfBlocked: + return d->selfBlockedText; + case None: + return TQAction::text(); + default: + return TQString(); + } +} + +void Action::setToolTip(const TQString &toolTip, States states) +{ + if (states & All) { + d->selfBlockedToolTip = toolTip; + d->noToolTip = toolTip; + d->authToolTip = toolTip; + d->yesToolTip = toolTip; + } else if (states & Auth) { + d->authToolTip = toolTip; + } else if (states & No) { + d->noToolTip = toolTip; + } else if (states & SelfBlocked) { + d->selfBlockedToolTip = toolTip; + } else if (states & Yes) { + d->yesToolTip = toolTip; + } + + d->updateAction(); +} + +TQString Action::toolTip(Action::State state) const +{ + switch (state) { + case Yes: + return d->yesToolTip; + case No: + return d->noToolTip; + case Auth: + return d->authToolTip; + case SelfBlocked: + return d->selfBlockedToolTip; + case None: + return TQAction::toolTip(); + default: + return TQString(); + } +} + +void Action::setWhatsThis(const TQString &whatsThis, States states) +{ + if (states & All) { + d->selfBlockedWhatsThis = whatsThis; + d->noWhatsThis = whatsThis; + d->authWhatsThis = whatsThis; + d->yesWhatsThis = whatsThis; + } else if (states & Auth) { + d->authWhatsThis = whatsThis; + } else if (states & No) { + d->noWhatsThis = whatsThis; + } else if (states & SelfBlocked) { + d->selfBlockedWhatsThis = whatsThis; + } else if (states & Yes) { + d->yesWhatsThis = whatsThis; + } + + d->updateAction(); +} + +TQString Action::whatsThis(Action::State state) const +{ + switch (state) { + case Yes: + return d->yesWhatsThis; + case No: + return d->noWhatsThis; + case Auth: + return d->authWhatsThis; + case SelfBlocked: + return d->selfBlockedWhatsThis; + case None: + return TQAction::whatsThis(); + default: + return TQString(); + } +} + +void Action::setIcon(const TQIcon &icon, States states) +{ + if (states & All) { + d->selfBlockedIcon = icon; + d->noIcon = icon; + d->authIcon = icon; + d->yesIcon = icon; + } else if (states & Auth) { + d->authIcon = icon; + } else if (states & No) { + d->noIcon = icon; + } else if (states & SelfBlocked) { + d->selfBlockedIcon = icon; + } else if (states & Yes) { + d->yesIcon = icon; + } + + d->updateAction(); +} + +TQIcon Action::icon(Action::State state) const +{ + switch (state) { + case Yes: + return d->yesIcon; + case No: + return d->noIcon; + case Auth: + return d->authIcon; + case SelfBlocked: + return d->selfBlockedIcon; + case None: + return TQAction::icon(); + default: + return TQIcon(); + } +} + +void Action::setEnabled(bool enabled, States states) +{ + if (states & All) { + d->selfBlockedEnabled = enabled; + d->noEnabled = enabled; + d->authEnabled = enabled; + d->yesEnabled = enabled; + } else if (states & Auth) { + d->authEnabled = enabled; + } else if (states & No) { + d->noEnabled = enabled; + } else if (states & SelfBlocked) { + d->selfBlockedEnabled = enabled; + } else if (states & Yes) { + d->yesEnabled = enabled; + } + + d->updateAction(); +} + +bool Action::isEnabled(Action::State state) const +{ + switch (state) { + case Yes: + return d->yesEnabled; + case No: + return d->noEnabled; + case Auth: + return d->authEnabled; + case SelfBlocked: + return d->selfBlockedEnabled; + case None: + return TQAction::isEnabled(); + default: + return false; + } +} + +void Action::setVisible(bool visible, States states) +{ + if (states & All) { + d->selfBlockedVisible = visible; + d->noVisible = visible; + d->authVisible = visible; + d->yesVisible = visible; + } else if (states & Auth) { + d->authVisible = visible; + } else if (states & No) { + d->noVisible = visible; + } else if (states & SelfBlocked) { + d->selfBlockedVisible = visible; + } else if (states & Yes) { + d->yesVisible = visible; + } + + d->updateAction(); +} + +bool Action::isVisible(Action::State state) const +{ + switch (state) { + case Yes: + return d->yesVisible; + case No: + return d->noVisible; + case Auth: + return d->authVisible; + case SelfBlocked: + return d->selfBlockedVisible; + case None: + return TQAction::isVisible(); + default: + return false; + } +} + +void Action::setPolkitAction(const TQString &actionId) +{ + //TODO: + d->actionId = actionId; + + d->computePkResult(); + d->updateAction(); +} + +//-------------------------------------------------- + +TQString Action::actionId() const +{ + return d->actionId; +} + +} + +} + +#include "polkittqt1-gui-action.moc" |