summaryrefslogtreecommitdiffstats
path: root/examples/PkExampleHelper.cpp
diff options
context:
space:
mode:
authorMichele Calgaro <[email protected]>2021-12-06 00:02:47 +0900
committerMichele Calgaro <[email protected]>2021-12-08 19:24:33 +0900
commit7d1585c071206dd8460ed624eff764de5464dde7 (patch)
tree6e3a6fa7a9e1eb1d5dd510a6d4ae0e39bd915cb1 /examples/PkExampleHelper.cpp
parent26fc60d30352bb3bbf00f8a392bbc695ff8cf29f (diff)
downloadpolkit-tqt-7d1585c071206dd8460ed624eff764de5464dde7.tar.gz
polkit-tqt-7d1585c071206dd8460ed624eff764de5464dde7.zip
Converted first part of examples code.
Signed-off-by: Michele Calgaro <[email protected]>
Diffstat (limited to 'examples/PkExampleHelper.cpp')
-rw-r--r--examples/PkExampleHelper.cpp179
1 files changed, 111 insertions, 68 deletions
diff --git a/examples/PkExampleHelper.cpp b/examples/PkExampleHelper.cpp
index 45b9efed4..a6d169207 100644
--- a/examples/PkExampleHelper.cpp
+++ b/examples/PkExampleHelper.cpp
@@ -20,91 +20,134 @@
***************************************************************************/
#include "PkExampleHelper.h"
-#include "examplesadaptor.h"
-#include "polkittqt-authority.h"
+#include "polkit-tqt-authority.h"
-#include <TQtDBus/TQDBusConnection>
-#include <TQtCore/TQTimer>
-#include <TQtCore/TQDebug>
-#include <TQtXml/TQDomDocument>
-
-#define MINUTE 60000
+#include <tqdbusdatalist.h>
+#include <tqdbuserror.h>
+#include <tqdbusmessage.h>
+#include <tqdom.h>
+#include <tqfile.h>
+#include <tqtimer.h>
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(int &argc, char **argv) : TQApplication(argc, argv, false)
+{
+ tqDebug("Creating Helper");
+ // Register the DBus service
+ m_connection = TQT_DBusConnection::systemBus();
+ if (!m_connection.registerObject("/", this))
+ {
+ tqDebug("unable to register 'org.tqt.policykit.examples' service interface to dbus");
+ tqDebug(m_connection.lastError().message());
+ return;
+ }
+ if (!m_connection.requestName("org.tqt.policykit.examples"))
+ {
+ tqDebug("unable to acquire 'org.tqt.policykit.examples' service interface to dbus");
+ tqDebug(m_connection.lastError().message());
+ return;
+ }
+ // Exit if not used for 10 minutes
+ TQTimer::singleShot(600000, this, TQT_SLOT(quit()));
+ tqDebug("Register successful");
}
PkExampleHelper::~PkExampleHelper()
{
- tqDebug() << "Destroying Helper";
+ tqDebug("Destroying Helper");
+ m_connection.unregisterObject("org.tqt.policykit.examples");
}
-bool PkExampleHelper::set(const TQString &action)
+bool PkExampleHelper::handleMethodCall(const TQT_DBusMessage& message)
{
- 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());
+ if (message.interface() != "org.tqt.policykit.examples")
+ {
+ return false;
+ }
- 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;
+ if (message.member() == "set")
+ {
+ // check parameters
+ if (message.count() != 1 || message[0].type() != TQT_DBusData::String)
+ {
+ // method signature not what we expected
+ TQT_DBusError error = TQT_DBusError::stdInvalidArgs(
+ "Expected one argument of type string");
+ TQT_DBusMessage reply = TQT_DBusMessage::methodError(message, error);
+ m_connection.send(reply);
+ return true;
}
+
+ bool res = set(message[0].toString());
+ TQT_DBusMessage reply = TQT_DBusMessage::methodReply(message);
+ reply << TQT_DBusData::fromBool(res);
+ m_connection.send(reply);
+ return true;
+ }
+
+ TQT_DBusMessage reply = TQT_DBusMessage::methodReply(message);
+ reply << TQT_DBusData::fromString("Bad request");
+ m_connection.send(reply);
+ return true;
+}
+
+bool PkExampleHelper::set(const TQString &action)
+{
+ // We can check if the caller is authorized to the following action
+ SystemBusNameSubject subject(m_connection.uniqueName());
+ Authority::Result result;
+ result = Authority::instance()->checkAuthorizationSync("org.tqt.policykit.examples.set",
+ subject , Authority::AllowUserInteraction);
+ if (result == Authority::Yes)
+ {
+ // Caller is authorized so we can perform the action
+ tqDebug(TQString("Implicit authorization set to ") + action);
+ return setValue(action);
+ }
+ else
+ {
+ // Caller is not authorized so the action can't be performed
+ tqDebug(TQString("Can't set the implicit authorization to ") + action);
+ 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;
+ // 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(IO_ReadOnly))
+ {
+ return false;
+ }
+ doc.setContent(&file);
+ file.close();
+ TQDomElement el = doc.namedItem("policyconfig").namedItem("action").toElement();
+ while (!el.isNull() && el.attribute("id") != "org.tqt.policykit.examples.shout")
+ {
+ el = el.nextSibling().toElement();
+ }
+ el = el.namedItem("defaults").toElement();
+ el = el.namedItem("allow_active").toElement();
+ if (el.isNull())
+ {
+ return false;
+ }
+ el.firstChild().toText().setData(action);
+ if (!file.open(IO_WriteOnly))
+ {
+ return false;
+ }
+ TQTextStream stream(&file);
+ doc.save(stream, 2);
+ file.close();
+ return true;
}
+
+#include "PkExampleHelper.moc"
+