You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
polkit-tqt/examples/PkExampleHelper.cpp

111 lines
4.5 KiB

// This is an example not a library
/***************************************************************************
* Copyright (C) 2008 Daniel Nicoletti <dantti85-pk@yahoo.com.br> *
* Copyright (C) 2009 Radek Novacek <rnovacek@redhat.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
***************************************************************************/
#include "PkExampleHelper.h"
#include "examplesadaptor.h"
#include "polkittqt-authority.h"
#include <TQtDBus/TQDBusConnection>
#include <TQtCore/TQTimer>
#include <TQtCore/TQDebug>
#include <TQtXml/TQDomDocument>
#define MINUTE 60000
using namespace PolkitTQt;
PkExampleHelper::PkExampleHelper(int &argc, char **argv)
: TQCoreApplication(argc, argv)
{
tqDebug() << "Creating Helper";
(void) new ExamplesAdaptor(this);
// Register the DBus service
if (!TQDBusConnection::systemBus().registerService("org.tqt.policykit.examples")) {
tqDebug() << TQDBusConnection::systemBus().lastError().message();;
TQTimer::singleShot(0, this, SLOT(quit()));
return;
}
if (!TQDBusConnection::systemBus().registerObject("/", this)) {
tqDebug() << "unable to register service interface to dbus";
TQTimer::singleShot(0, this, SLOT(quit()));
return;
}
// Normally you will set a timeout so your application can
// free some resources of the poor client machine ;)
TQTimer::singleShot(MINUTE, this, SLOT(quit()));
}
PkExampleHelper::~PkExampleHelper()
{
tqDebug() << "Destroying Helper";
}
bool PkExampleHelper::set(const TQString &action)
{
tqDebug() << "PkExampleHelper::set" << action;
// message().service() is the service name of the caller
// We can check if the caller is authorized to the following action
Authority::Result result;
SystemBusNameSubject subject(message().service());
result = Authority::instance()->checkAuthorizationSync("org.tqt.policykit.examples.set",
subject , Authority::AllowUserInteraction);
if (result == Authority::Yes) {
tqDebug() << message().service() << TQString("Implicit authorization set to") << action;
// Caller is authorized so we can perform the action
return setValue(action);
} else {
tqDebug() << message().service() << TQString("Can't set the implicit authorization");
// Caller is not authorized so the action can't be performed
return false;
}
}
bool PkExampleHelper::setValue(const TQString &action)
{
// This action must be authorized first. It will set the implicit
// authorization for the Shout action by editing the .policy file
TQDomDocument doc = TQDomDocument("policy");
TQFile file("/usr/share/polkit-1/actions/org.tqt.policykit.examples.policy");
if (!file.open(TQIODevice::ReadOnly))
return false;
doc.setContent(&file);
file.close();
TQDomElement el = doc.firstChildElement("policyconfig").
firstChildElement("action");
while (!el.isNull() && el.attribute("id", TQString()) != "org.tqt.policykit.examples.shout") {
el = el.nextSiblingElement("action");
}
el = el.firstChildElement("defaults");
el = el.firstChildElement("allow_active");
if (el.isNull())
return false;
el.firstChild().toText().setData(action);
if (!file.open(TQIODevice::WriteOnly))
return false;
TQTextStream stream(&file);
doc.save(stream, 2);
file.close();
return true;
}