kdecore: unregister the helper service and object from KAuthorization destructor

I should (ab)use the object name for more things

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-12-12 08:19:15 +02:00
parent f9171a653f
commit 90c57ada83
5 changed files with 25 additions and 7 deletions

View file

@ -492,7 +492,6 @@ void KDebugConfig::destroyDevices()
}
}
QIODevice* KDebugConfig::areaDevice(const QtMsgType type, const char* const funcinfo, const int area)
{
const KDebugAreaCache kdebugareacache = KDebugConfig::areaCache(area);

View file

@ -121,17 +121,28 @@ void KAuthorizationAdaptor::delayedStop()
}
KAuthorization::KAuthorization(QObject *parent)
KAuthorization::KAuthorization(const char* const helper, QObject *parent)
: QObject(parent ? parent : qApp)
{
Q_ASSERT(helper);
setObjectName(QString::fromLatin1(helper));
new KAuthorizationAdaptor(this);
}
KAuthorization::~KAuthorization()
{
const QString helper = objectName();
if (!helper.isEmpty()) {
QDBusConnection::systemBus().unregisterService(helper);
QDBusConnection::systemBus().unregisterObject(QString::fromLatin1("/KAuthorization"));
}
}
bool KAuthorization::isAuthorized(const QString &helper)
{
kDebug() << "Checking if" << helper << "is authorized";
QDBusInterface kauthorizationinterface(
helper, QString::fromLatin1("/"), QString::fromLatin1("org.kde.kauthorization"),
helper, QString::fromLatin1("/KAuthorization"), QString::fromLatin1("org.kde.kauthorization"),
QDBusConnection::systemBus()
);
QDBusReply<void> reply = kauthorizationinterface.call(QString::fromLatin1("stop"));
@ -155,7 +166,7 @@ int KAuthorization::execute(const QString &helper, const QString &method, const
}
QDBusInterface kauthorizationinterface(
helper, QString::fromLatin1("/"), QString::fromLatin1("org.kde.kauthorization"),
helper, QString::fromLatin1("/KAuthorization"), QString::fromLatin1("org.kde.kauthorization"),
QDBusConnection::systemBus()
);
QDBusReply<int> reply = kauthorizationinterface.call(QString::fromLatin1("execute"), method, arguments);
@ -208,7 +219,7 @@ void KAuthorization::helperMain(const char* const helper, KAuthorization *object
qApp->exit(1);
return;
}
if (!QDBusConnection::systemBus().registerObject(QString::fromLatin1("/"), object)) {
if (!QDBusConnection::systemBus().registerObject(QString::fromLatin1("/KAuthorization"), object)) {
qApp->exit(2);
return;
}

View file

@ -84,7 +84,8 @@ public:
AuthorizationError = -4
};
KAuthorization(QObject *parent = nullptr);
KAuthorization(const char* const helper, QObject *parent = nullptr);
~KAuthorization();
/*!
@brief Returns @p true if the current user is allowed to execute @p helper methods,
@ -112,7 +113,7 @@ private:
#define K_AUTH_MAIN(HELPER, CLASS) \
int main(int argc, char **argv) { \
QCoreApplication app(argc, argv); \
KAuthorization::helperMain(HELPER, new CLASS()); \
KAuthorization::helperMain(HELPER, new CLASS(HELPER, qApp)); \
return app.exec(); \
}

View file

@ -23,6 +23,11 @@
#include <kdebug.h>
KPowerManagerHelper::KPowerManagerHelper(const char* const helper, QObject *parent)
: KAuthorization(helper, parent)
{
}
int KPowerManagerHelper::setgovernor(const QVariantMap &parameters)
{
if (!parameters.contains("governor")) {

View file

@ -24,6 +24,8 @@
class KPowerManagerHelper : public KAuthorization
{
Q_OBJECT
public:
KPowerManagerHelper(const char* const helper, QObject *parent = nullptr);
public slots:
int setgovernor(const QVariantMap &parameters);
};