diff options
author | Michele Calgaro <michele.calgaro@yahoo.it> | 2021-12-06 00:02:47 +0900 |
---|---|---|
committer | Michele Calgaro <michele.calgaro@yahoo.it> | 2021-12-08 19:24:33 +0900 |
commit | 7d1585c071206dd8460ed624eff764de5464dde7 (patch) | |
tree | 6e3a6fa7a9e1eb1d5dd510a6d4ae0e39bd915cb1 /examples/PkExample.cpp | |
parent | 26fc60d30352bb3bbf00f8a392bbc695ff8cf29f (diff) | |
download | polkit-tqt-7d1585c071206dd8460ed624eff764de5464dde7.tar.gz polkit-tqt-7d1585c071206dd8460ed624eff764de5464dde7.zip |
Converted first part of examples code.
Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
Diffstat (limited to 'examples/PkExample.cpp')
-rw-r--r-- | examples/PkExample.cpp | 520 |
1 files changed, 323 insertions, 197 deletions
diff --git a/examples/PkExample.cpp b/examples/PkExample.cpp index 7366b4eaa..da2405ef5 100644 --- a/examples/PkExample.cpp +++ b/examples/PkExample.cpp @@ -21,220 +21,346 @@ #include "PkExample.h" -#include "gui/polkitqt1-gui-actionbutton.h" -#include "gui/polkitqt1-gui-actionbuttons.h" -#include "core/polkitqt1-authority.h" -#include <TQtCore/TQDebug> +#include "gui/polkit-tqt-gui-actionbutton.h" +#include "gui/polkit-tqt-gui-actionbuttons.h" +#include "core/polkit-tqt-authority.h" -#include <TQtDBus/TQDBusMessage> -#include <TQtDBus/TQDBusConnection> +#include <tqdbusconnection.h> +#include <tqdbuserror.h> +#include <tqdbusmessage.h> +#include <tqdbusproxy.h> + +#include <tqvariant.h> +#include <tqlabel.h> +#include <tqpushbutton.h> +#include <tqcombobox.h> +#include <tqheader.h> +#include <tqlistview.h> +#include <tqlayout.h> +#include <tqtooltip.h> +#include <tqwhatsthis.h> +#include <tqaction.h> +#include <tqmenubar.h> +#include <tqpopupmenu.h> +#include <tqtoolbar.h> using namespace PolkitTQt; using namespace PolkitTQt::Gui; -PkExample::PkExample(TQMainWindow *parent) - : TQMainWindow(parent) + +PkExample::PkExample() : TQMainWindow(0, "Polkit tqt example application", WDestructiveClose) { - setupUi(this); - - ActionButton *bt; - - // Here we create an ActionButton that is a subclass of Action - // always pass a TQButton pointer and action id - // You can change the action id later if you want - bt = new ActionButton(kickPB, "org.qt.policykit.examples.kick", this); - // Here we are setting the text and icon to all four states - // an action might have - bt->setText("Kick!"); - bt->setIcon(TQPixmap(":/Icons/custom-no.png")); - // By using set{Yes|No|Auth}Enabled you can set the states - // when the button is enabled - bt->setEnabled(true, Action::No); - // As ActionButton is also an Action we cast it to add to menu - menuActions->addAction(qobject_cast<Action *>(bt)); - toolBar->addAction(qobject_cast<Action *>(bt)); - // this signal is emitted when the user click on the action, - // it will only happen if it was inserted in a TQMenu or a TQToolBar - connect(bt, SIGNAL(triggered(bool)), this, SLOT(activateAction())); - // This signal was propagated from the TQButton just for - // convenience in this case we don't have any benefit but the code - // look cleaner - connect(bt, SIGNAL(clicked(TQButton *, bool)), bt, SLOT(activate())); - // this is the Action activated signal, it is always emmited whenever - // someone click and get authorized to do the action - connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated())); - - bt = new ActionButton(cryPB, "org.qt.policykit.examples.cry", this); - bt->setText("Cry!"); - bt->setIcon(TQPixmap(":/Icons/custom-yes.png")); - menuActions->addAction(qobject_cast<Action *>(bt)); - toolBar->addAction(qobject_cast<Action *>(bt)); - connect(bt, SIGNAL(triggered(bool)), this, SLOT(activateAction())); - connect(bt, SIGNAL(clicked(TQButton *, bool)), bt, SLOT(activate())); - connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated())); - - bt = new ActionButton(bleedPB, "org.qt.policykit.examples.bleed", this); - bt->setText("Bleed!"); - bt->setIcon(TQPixmap(":/Icons/action-locked-default.png")); - menuActions->addAction(qobject_cast<Action *>(bt)); - toolBar->addAction(qobject_cast<Action *>(bt)); - connect(bt, SIGNAL(triggered(bool)), this, SLOT(activateAction())); - connect(bt, SIGNAL(clicked(TQButton *, bool)), bt, SLOT(activate())); - connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated())); - - // This action is more customized - bt = new ActionButton(playPB, "org.qt.policykit.examples.play", this); - bt->setText("Play!"); - bt->setVisible(true, Action::No | Action::Auth | Action::Yes); - bt->setEnabled(true, Action::No | Action::Auth | Action::Yes); - // here we set the behavior of PolKitResul = No - bt->setText("Can't play!", Action::No); - bt->setIcon(TQPixmap(":/Icons/custom-no.png"), Action::No); - bt->setToolTip("If your admin wasn't annoying, you could do this", Action::No); - // here we set the behavior of PolKitResul = Auth - bt->setText("Play?", Action::Auth); - bt->setIcon(TQPixmap(":/Icons/action-locked-default.png"), Action::Auth); - bt->setToolTip("Only card carrying tweakers can do this!", Action::Auth); - // here we set the behavior of PolKitResul = Yes - bt->setText("Play!", Action::Yes); - bt->setIcon(TQPixmap(":/Icons/custom-yes.png"), Action::Yes); - bt->setToolTip("Go ahead, play!", Action::Yes); - - menuActions->addAction(qobject_cast<Action *>(bt)); - toolBar->addAction(qobject_cast<Action *>(bt)); - connect(bt, SIGNAL(triggered(bool)), this, SLOT(activateAction())); - connect(bt, SIGNAL(clicked(TQButton *, bool)), bt, SLOT(activate())); - connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated())); - - bt = new ActionButton(deletePB, "org.qt.policykit.examples.delete", this); - bt->setText("Delete!"); - bt->setIcon(TQPixmap(":/Icons/custom-no.png"), Action::No); - bt->setIcon(TQPixmap(":/Icons/action-locked-default.png"), Action::Auth); - bt->setIcon(TQPixmap(":/Icons/custom-yes.png"), Action::Yes); - menuActions->addAction(qobject_cast<Action *>(bt)); - toolBar->addAction(qobject_cast<Action *>(bt)); - connect(bt, SIGNAL(triggered(bool)), this, SLOT(activateAction())); - connect(bt, SIGNAL(clicked(TQButton *, bool)), bt, SLOT(activate())); - connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated())); - - bt = new ActionButton(listenPB, "org.qt.policykit.examples.listen", this); - bt->setText("Listen!"); - bt->setIcon(TQPixmap(":/Icons/custom-no.png"), Action::No); - bt->setIcon(TQPixmap(":/Icons/action-locked-default.png"), Action::Auth); - bt->setIcon(TQPixmap(":/Icons/custom-yes.png"), Action::Yes); - menuActions->addAction(qobject_cast<Action *>(bt)); - toolBar->addAction(qobject_cast<Action *>(bt)); - connect(bt, SIGNAL(triggered(bool)), this, SLOT(activateAction())); - connect(bt, SIGNAL(clicked(TQButton *, bool)), bt, SLOT(activate())); - connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated())); - - bt = new ActionButton(setPB, "org.qt.policykit.examples.set", this); - bt->setText("Set!"); - bt->setIcon(TQPixmap(":/Icons/custom-no.png"), Action::No); - bt->setIcon(TQPixmap(":/Icons/action-locked-default.png"), Action::Auth); - bt->setIcon(TQPixmap(":/Icons/custom-yes.png"), Action::Yes); - menuActions->addAction(qobject_cast<Action *>(bt)); - toolBar->addAction(qobject_cast<Action *>(bt)); - connect(bt, SIGNAL(triggered(bool)), this, SLOT(activateAction())); - connect(bt, SIGNAL(clicked(TQButton *, bool)), bt, SLOT(activate())); - connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated())); - - bt = new ActionButton(shoutPB, "org.qt.policykit.examples.shout", this); - bt->setIcon(TQPixmap(":/Icons/custom-no.png"), Action::No); - bt->setIcon(TQPixmap(":/Icons/action-locked-default.png"), Action::Auth); - bt->setIcon(TQPixmap(":/Icons/custom-yes.png"), Action::Yes); - bt->setText("Can't shout!", Action::No); - bt->setText("Shout?", Action::Auth); - bt->setText("Shout!", Action::Yes); - menuActions->addAction(qobject_cast<Action *>(bt)); - toolBar->addAction(qobject_cast<Action *>(bt)); - connect(bt, SIGNAL(triggered(bool)), this, SLOT(activateAction())); - connect(bt, SIGNAL(clicked(TQButton *, bool)), bt, SLOT(activate())); - connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated())); - - // test configChanged + TQWidget *privateLayoutWidget = new TQWidget(this, "qt_central_widget"); + setCentralWidget(privateLayoutWidget); + + grid = new TQGridLayout(0, 9, 1, 0, 6, "grid"); + grid->setColSpacing(0, 700); + grid->setColSpacing(1, 200); + + kickL = new TQLabel(privateLayoutWidget, "kickL"); + kickPB = new TQPushButton(privateLayoutWidget, "kickPB"); + grid->addWidget(kickL, 0, 0); + grid->addWidget(kickPB, 0, 1); + + cryL = new TQLabel(privateLayoutWidget, "cryL"); + cryPB = new TQPushButton(privateLayoutWidget, "cryPB"); + grid->addWidget(cryL, 1, 0); + grid->addWidget(cryPB, 1, 1); + + bleedL = new TQLabel(privateLayoutWidget, "bleedL"); + bleedPB = new TQPushButton(privateLayoutWidget, "bleedPB"); + grid->addWidget(bleedL, 2, 0); + grid->addWidget(bleedPB, 2, 1); + + playL = new TQLabel(privateLayoutWidget, "playL"); + playPB = new TQPushButton(privateLayoutWidget, "playPB"); + grid->addWidget(playL, 3, 0); + grid->addWidget(playPB, 3, 1); + + listenL = new TQLabel(privateLayoutWidget, "listenL"); + listenPB = new TQPushButton(privateLayoutWidget, "listenPB"); + grid->addWidget(listenL, 4, 0); + grid->addWidget(listenPB, 4, 1); + + deleteL = new TQLabel(privateLayoutWidget, "deleteL"); + deletePB = new TQPushButton(privateLayoutWidget, "deletePB"); + grid->addWidget(deleteL, 5, 0); + grid->addWidget(deletePB, 5, 1); + + deleteL_2 = new TQLabel(privateLayoutWidget, "deleteL_2"); + setCB = new TQComboBox(FALSE, privateLayoutWidget, "setCB"); + grid->addMultiCellWidget(deleteL_2, 6, 7, 0, 0); + grid->addWidget(setCB, 6, 1); + + setPB = new TQPushButton(privateLayoutWidget, "setPB"); + deleteL_3 = new TQLabel(privateLayoutWidget, "deleteL_3"); + grid->addWidget(setPB, 7, 1); + grid->addWidget(deleteL_3, 8, 0); + + shoutPB = new TQPushButton(privateLayoutWidget, "shoutPB"); + grid->addWidget(shoutPB, 8, 1); + + vLayout = new TQVBoxLayout(privateLayoutWidget, 11, 6, "vLayout"); + vLayout->addLayout(grid); + + actionList = new TQListView(privateLayoutWidget); + actionList->addColumn("Action"); + actionList->addColumn("Result"); + actionList->setSorting(-1); + + vLayout->addWidget(actionList); + + // menubar + menubar = new TQMenuBar(this, "menubar"); + menubar->setGeometry(TQRect(0, 0, 800, 22)); + menuActions = new TQPopupMenu(this); + menubar->insertItem(TQString(""), menuActions, 2); + + // toolbars + toolBar = new TQToolBar(TQString(""), this, DockTop); + + // statusbar + statusBar(); + + languageChange(); + resize(TQSize(900, 700).expandedTo(minimumSizeHint())); + clearWState(WState_Polished); + + // Setup actions + ActionButton *bt; + + // Here we create an ActionButton that is a subclass of Action + // always pass a TQButton pointer and action id + // You can change the action id later if you want + bt = new ActionButton(kickPB, "org.tqt.policykit.examples.kick", this); + // Here we are setting the text and icon to all four states + // an action might have + bt->setText("Kick!"); + bt->setIconSet(TQPixmap("/usr/share/apps/polkit-tqt/icons/custom-no.png")); + // By using set{Yes|No|Auth}Enabled you can set the states + // when the button is enabled + bt->setEnabled(true, Action::No); + // As ActionButton is also an Action we cast it to add to menu + ::tqt_cast<TQAction*>(bt)->addTo(menuActions); + ::tqt_cast<TQAction*>(bt)->addTo(toolBar); + // this signal is emitted when the user click on the action, + // it will only happen if it was inserted in a TQMenu or a TQToolBar + connect(bt, SIGNAL(activated()), this, SLOT(activateAction())); + // This signal was propagated from the TQButton just for + // convenience in this case we don't have any benefit but the code + // look cleaner + connect(bt, SIGNAL(clicked(TQButton*, bool)), bt, SLOT(activate())); + // this is the Action activated signal, it is always emmited whenever + // someone click and get authorized to do the action + connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated())); + + bt = new ActionButton(cryPB, "org.tqt.policykit.examples.cry", this); + bt->setText("Cry!"); + bt->setIconSet(TQPixmap("/usr/share/apps/polkit-tqt/icons/custom-yes.png")); + ::tqt_cast<TQAction*>(bt)->addTo(menuActions); + ::tqt_cast<TQAction*>(bt)->addTo(toolBar); + connect(bt, SIGNAL(activated()), this, SLOT(activateAction())); + connect(bt, SIGNAL(clicked(TQButton*, bool)), bt, SLOT(activate())); + connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated())); + + bt = new ActionButton(bleedPB, "org.tqt.policykit.examples.bleed", this); + bt->setText("Bleed!"); + bt->setIconSet(TQPixmap("/usr/share/apps/polkit-tqt/icons/action-locked-default.png")); + ::tqt_cast<TQAction*>(bt)->addTo(menuActions); + ::tqt_cast<TQAction*>(bt)->addTo(toolBar); + connect(bt, SIGNAL(activated()), this, SLOT(activateAction())); + connect(bt, SIGNAL(clicked(TQButton*, bool)), bt, SLOT(activate())); + connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated())); + + // This action is more customized + bt = new ActionButton(playPB, "org.tqt.policykit.examples.play", this); + bt->setText("Play!"); + bt->setVisible(true, (Action::States)(Action::No | Action::Auth | Action::Yes)); + bt->setEnabled(true, (Action::States)(Action::No | Action::Auth | Action::Yes)); + // here we set the behavior of PolKitResul = No + bt->setText("Can't play!", Action::No); + bt->setIconSet(TQPixmap("/usr/share/apps/polkit-tqt/icons/custom-no.png"), Action::No); + bt->setToolTip("If your admin wasn't annoying, you could do this", Action::No); + // here we set the behavior of PolKitResul = Auth + bt->setText("Play?", Action::Auth); + bt->setIconSet(TQPixmap("/usr/share/apps/polkit-tqt/icons/action-locked-default.png"), Action::Auth); + bt->setToolTip("Only card carrying tweakers can do this!", Action::Auth); + // here we set the behavior of PolKitResul = Yes + bt->setText("Play!", Action::Yes); + bt->setIconSet(TQPixmap("/usr/share/apps/polkit-tqt/icons/custom-yes.png"), Action::Yes); + bt->setToolTip("Go ahead, play!", Action::Yes); + ::tqt_cast<TQAction*>(bt)->addTo(menuActions); + ::tqt_cast<TQAction*>(bt)->addTo(toolBar); + connect(bt, SIGNAL(activated()), this, SLOT(activateAction())); + connect(bt, SIGNAL(clicked(TQButton*, bool)), bt, SLOT(activate())); + connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated())); + + bt = new ActionButton(listenPB, "org.tqt.policykit.examples.listen", this); + bt->setText("Listen!"); + bt->setIconSet(TQPixmap("/usr/share/apps/polkit-tqt/icons/custom-no.png"), Action::No); + bt->setIconSet(TQPixmap("/usr/share/apps/polkit-tqt/icons/action-locked-default.png"), Action::Auth); + bt->setIconSet(TQPixmap("/usr/share/apps/polkit-tqt/icons/custom-yes.png"), Action::Yes); + ::tqt_cast<TQAction*>(bt)->addTo(menuActions); + ::tqt_cast<TQAction*>(bt)->addTo(toolBar); + connect(bt, SIGNAL(activated()), this, SLOT(activateAction())); + connect(bt, SIGNAL(clicked(TQButton*, bool)), bt, SLOT(activate())); + connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated())); + + bt = new ActionButton(deletePB, "org.tqt.policykit.examples.delete", this); + bt->setText("Delete!"); + bt->setIconSet(TQPixmap("/usr/share/apps/polkit-tqt/icons/custom-no.png"), Action::No); + bt->setIconSet(TQPixmap("/usr/share/apps/polkit-tqt/icons/action-locked-default.png"), Action::Auth); + bt->setIconSet(TQPixmap("/usr/share/apps/polkit-tqt/icons/custom-yes.png"), Action::Yes); + ::tqt_cast<TQAction*>(bt)->addTo(menuActions); + ::tqt_cast<TQAction*>(bt)->addTo(toolBar); + connect(bt, SIGNAL(activated()), this, SLOT(activateAction())); + connect(bt, SIGNAL(clicked(TQButton*, bool)), bt, SLOT(activate())); + connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated())); + + bt = new ActionButton(setPB, "org.tqt.policykit.examples.set", this); + bt->setText("Set!"); + bt->setIconSet(TQPixmap("/usr/share/apps/polkit-tqt/icons/custom-no.png"), Action::No); + bt->setIconSet(TQPixmap("/usr/share/apps/polkit-tqt/icons/action-locked-default.png"), Action::Auth); + bt->setIconSet(TQPixmap("/usr/share/apps/polkit-tqt/icons/custom-yes.png"), Action::Yes); + ::tqt_cast<TQAction*>(bt)->addTo(menuActions); + ::tqt_cast<TQAction*>(bt)->addTo(toolBar); + connect(bt, SIGNAL(activated()), this, SLOT(activateAction())); + connect(bt, SIGNAL(clicked(TQButton*, bool)), bt, SLOT(activate())); + connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated())); + + bt = new ActionButton(shoutPB, "org.tqt.policykit.examples.shout", this); + bt->setIconSet(TQPixmap("/usr/share/apps/polkit-tqt/icons/custom-no.png"), Action::No); + bt->setIconSet(TQPixmap("/usr/share/apps/polkit-tqt/icons/action-locked-default.png"), Action::Auth); + bt->setIconSet(TQPixmap("/usr/share/apps/polkit-tqt/icons/custom-yes.png"), Action::Yes); + bt->setText("Can't shout!", Action::No); + bt->setText("Shout?", Action::Auth); + bt->setText("Shout!", Action::Yes); + ::tqt_cast<TQAction*>(bt)->addTo(menuActions); + ::tqt_cast<TQAction*>(bt)->addTo(toolBar); + connect(bt, SIGNAL(activated()), this, SLOT(activateAction())); + connect(bt, SIGNAL(clicked(TQButton*, bool)), bt, SLOT(activate())); + connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated())); } -PkExample::~PkExample() +void PkExample::languageChange() { + setCaption(tr("PolicyKit-tqt example")); + kickL->setText(tr("Making the helper <b>Kick</b> is not allowed. Action cannot be activated.")); + kickPB->setText(tr("Kick!")); + cryL->setText(tr("Making the helper <b>Cry</b> is always allowed. Action can be activated without authentication.")); + cryPB->setText(tr("Cry!")); + bleedL->setText(tr("Making the helper <b>Bleed</b> requires the user to authenticate. This is a one-shot authorization.")); + bleedPB->setText(tr("Bleed!")); + playL->setText(tr("Making the helper <b>Play</b> requires a system administrator to authenticate. This is a one-shot authorization.")); + playPB->setText(tr("Play!")); + listenL->setText(tr("Making the helper <b>Listen</b> requires the user to authenticate. The authorization will be kept for a brief period.")); + listenPB->setText(tr("Listen!")); + deleteL->setText(tr("Making the helper <b>Delete</b> requires a system administrator to authenticate. The authorization will be kept for a brief period.")); + deletePB->setText(tr("Delete!")); + deleteL_2->setText(tr("<b>Set</b> is special action. It sets the permissions for the <b>Shout</b> action by actually using the helper application to change the implicit authorization.")); + setCB->clear(); + setCB->insertItem(tr("no")); + setCB->insertItem(tr("yes")); + setCB->insertItem(tr("auth_self")); + setCB->insertItem(tr("auth_admin")); + setCB->insertItem(tr("auth_self_keep")); + setCB->insertItem(tr("auth_admin_keep")); + setPB->setText(tr("Set!")); + deleteL_3->setText(tr("The implicit authorization for the <b>Shout</b> action is set by the <b>Set</b> action. You can watch how different options can change behaviour of this action.")); + shoutPB->setText(tr("Shout!")); + toolBar->setCaption(tr("toolBar")); + if (menubar->findItem(2)) + { + menubar->findItem(2)->setText(tr("&Actions")); + } } void PkExample::activateAction() { - // Here we cast the sender() to an Action and call activate() - // on it. - // Be careful in doing the same for ActionButton won't work as expected - // as action->activate() is calling Action::activate() and - // not ActionButton::activate which are different. - // You can cast then to ActionButton but be carefull - // an Action casted to ActionButton may crash you app - Action *action = qobject_cast<Action *>(sender()); - // calling activate with winId() makes the auth dialog - // be correct parented with your application. - action->activate(); + // Here we cast the sender() to an Action and call activate() on it. + // Be careful in doing the same for ActionButton won't work as expected + // as action->activate() is calling Action::activate() and + // not ActionButton::activate which are different. + // You can cast then to ActionButton but be carefull + // an Action casted to ActionButton may crash you app + Action *action = ::tqt_cast<Action*>(sender()); + // calling activate with winId() makes the auth dialog + // be correct parented with your application. + action->activate(); } void PkExample::actionActivated() { - // This slot is called whenever an action is allowed - // here you will do your dirt job by calling a helper application - // that might erase your hardrive ;) - Action *action = qobject_cast<Action *>(sender()); - - // this is our Special Action that after allowed will call the helper - if (action->is("org.qt.policykit.examples.set")) { - tqDebug() << "toggled for: org.qt.policykit.examples.set"; - - TQDBusMessage message; - message = TQDBusMessage::createMethodCall("org.qt.policykit.examples", - "/", - "org.qt.policykit.examples", - TQLatin1String("set")); - TQList<TQVariant> argumentList; - argumentList << qVariantFromValue(setCB->currentText()); - message.setArguments(argumentList); - // notice the systemBus here.. - TQDBusMessage reply = TQDBusConnection::systemBus().call(message); - if (reply.type() == TQDBusMessage::ReplyMessage - && reply.arguments().size() == 1) { - // the reply can be anything, here we receive a bool - TQListWidgetItem *item; - if (reply.arguments().first().toBool()) - item = new TQListWidgetItem(TQPixmap(":/Icons/custom-yes.png"), - TQString("Implicit authorization for shout has been set to %0") - .arg(setCB->currentText())); - else - item = new TQListWidgetItem(TQPixmap(":/Icons/custom-no.png"), - TQString("Can't change the implicit authorization. Denied.")); - actionList->addItem(item); - tqDebug() << reply.arguments().first().toString(); - } else if (reply.type() == TQDBusMessage::MethodCallMessage) { - tqWarning() << "Message did not receive a reply (timeout by message bus)"; - } - return; - } + // This slot is called whenever an action is allowed + // here you will do your dirt job by calling a helper application + // that might erase your hardrive ;) + Action *action = ::tqt_cast<Action*>(sender()); - // As debug message says we are pretending to be the mechanism for the - // following action, here you will actually call your DBus helper that - // will run as root (setuid is not needed, see DBus docs). - // In the helper application you will issue checkAuthorizationSync, - // passing the action id and the caller pid (which DBus will tell you). - tqDebug() << "pretending to be the mechanism for action:" << action->actionId(); - - Authority::Result result; - UnixProcessSubject subject(static_cast<uint>(TQCoreApplication::applicationPid())); - - result = Authority::instance()->checkAuthorizationSync(action->actionId(), subject, - Authority::AllowUserInteraction); - if (result == Authority::Yes) { - // in the helper you will do the action - tqDebug() << "caller is authorized to do:" << action->actionId(); - TQListWidgetItem *item = new TQListWidgetItem(TQPixmap(":/Icons/custom-yes.png"), action->actionId()); - actionList->addItem(item); - } else { - // OR return false to notify the caller that the action is not authorized. - tqDebug() << "caller is NOT authorized to do:" << action->actionId(); - TQListWidgetItem *item = new TQListWidgetItem(TQIcon(":/Icons/custom-no.png"), action->actionId()); - actionList->addItem(item); + // this is our Special Action that after allowed will call the helper + if (action->is("org.tqt.policykit.examples.set")) + { + tqDebug("toggled for: org.tqt.policykit.examples.set"); + + TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus); + TQT_DBusProxy exampleProxy("org.tqt.policykit.examples", "/", "org.tqt.policykit.examples", dbusConn); + TQT_DBusError error; + TQValueList<TQT_DBusData> params; + params << TQT_DBusData::fromString(setCB->currentText()); + TQT_DBusMessage reply = exampleProxy.sendWithReply("set", params, &error); + if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) + { + // the reply can be anything, here we receive a bool + TQListViewItem *item; + if (reply[0].toBool()) + { + item = new TQListViewItem(actionList); + item->setText(0, TQString("Implicit authorization for shout has been set to ") + + setCB->currentText()); + item->setPixmap(1, TQPixmap("/usr/share/apps/polkit-tqt/icons/custom-yes.png")); + } + else + { + item = new TQListViewItem(actionList); + item->setText(0, TQString("Can't change the implicit authorization. Denied.")); + item->setPixmap(1, TQPixmap("/usr/share/apps/icons/custom-no.png")); + } + actionList->insertItem(item); + } + else if (reply.type() == TQT_DBusMessage::MethodCallMessage) + { + tqDebug("Message did not receive a reply (timeout by message bus)"); } + return; + } + + // As debug message says we are pretending to be the mechanism for the + // following action, here you will actually call your DBus helper that + // will run as root (setuid is not needed, see DBus docs). + // In the helper application you will issue checkAuthorizationSync, + // passing the action id and the caller pid (which DBus will tell you). + tqDebug("Pretending to be the mechanism for action " + action->actionId()); + + Authority::Result result; + UnixProcessSubject subject(static_cast<uint>(getpid())); + result = Authority::instance()->checkAuthorizationSync(action->actionId(), subject, + Authority::AllowUserInteraction); + TQListViewItem *item; + item = new TQListViewItem(actionList); + item->setText(0, action->actionId()); + if (result == Authority::Yes) + { + // in the helper you will do the action + tqDebug("caller is authorized to do: " + action->actionId()); + item->setPixmap(1, TQPixmap("/usr/share/apps/polkit-tqt/icons/custom-yes.png")); + } + else + { + // or return false to notify the caller that the action is not authorized. + tqDebug("caller is NOT authorized to do: " + action->actionId()); + item->setPixmap(1, TQPixmap("/usr/share/apps/polkit-tqt/icons/custom-no.png")); + } + actionList->insertItem(item); } + +#include "PkExample.moc" + |