KDBusAddons

kdedmodule.cpp
1/*
2 This file is part of the KDE libraries
3
4 SPDX-FileCopyrightText: 2001 Waldo Bastian <bastian@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "kdedmodule.h"
10#include "kdbusaddons_debug.h"
11
12#include <QDBusConnection>
13#include <QDBusMessage>
14#include <QDBusObjectPath>
15
16class KDEDModulePrivate
17{
18public:
19 QString moduleName;
20};
21
23 : QObject(parent)
24 , d(new KDEDModulePrivate)
25{
26}
27
28KDEDModule::~KDEDModule()
29{
30}
31
33{
34 d->moduleName = name;
35 QDBusObjectPath realPath(QLatin1String("/modules/") + d->moduleName);
36
37 if (realPath.path().isEmpty()) {
38 qCWarning(KDBUSADDONS_LOG) << "The kded module name" << name << "is invalid!";
39 return;
40 }
41
43
44 if (this->metaObject()->indexOfClassInfo("D-Bus Interface") != -1) {
45 // 1. There are kded modules that don't have a D-Bus interface.
46 // 2. qt 4.4.3 crashes when trying to emit signals on class without
47 // Q_CLASSINFO("D-Bus Interface", "<your interface>") but
48 // ExportSignal set.
49 // We try to solve that for now with just registering Properties and
50 // Adaptors. But we should investigate where the sense is in registering
51 // the module at all. Just for autoload? Is there a better solution?
53 } else {
54 // Full functional module. Register everything.
58 qCDebug(KDBUSADDONS_LOG) << "Registration of kded module" << d->moduleName << "without D-Bus interface.";
59 }
60
61 if (!QDBusConnection::sessionBus().registerObject(realPath.path(), this, regOptions)) {
62 // Happens for khotkeys but the module works. Need some time to investigate.
63 qCDebug(KDBUSADDONS_LOG) << "registerObject() returned false for" << d->moduleName;
64 } else {
65 // qCDebug(KDBUSADDONS_LOG) << "registerObject() successful for" << d->moduleName;
66 // Fix deadlock with Qt 5.6: this has to be delayed until the dbus thread is unlocked
67 auto registeredSignal = [this, realPath]() {
68 moduleRegistered(realPath);
69 };
70 QMetaObject::invokeMethod(this, registeredSignal, Qt::QueuedConnection);
71 }
72}
73
74QString KDEDModule::moduleName() const
75{
76 return d->moduleName;
77}
78
79static const char s_modules_path[] = "/modules/";
80
82{
83 if (message.type() != QDBusMessage::MethodCallMessage) {
84 return QString();
85 }
86
87 QString obj = message.path();
88 if (!obj.startsWith(QLatin1String(s_modules_path))) {
89 return QString();
90 }
91
92 // Remove the <s_modules_path> part
93 obj = obj.mid(strlen(s_modules_path));
94
95 // Remove the part after the modules name
96 const int index = obj.indexOf(QLatin1Char('/'));
97 if (index != -1) {
98 obj = obj.left(index);
99 }
100
101 return obj;
102}
103
104#include "moc_kdedmodule.cpp"
static QString moduleForMessage(const QDBusMessage &message)
Returns the module being called by this D-Bus message.
void setModuleName(const QString &name)
Sets the name of the module, and uses it to register the module to D-Bus.
KDEDModule(QObject *parent=nullptr)
Constructor.
void moduleRegistered(const QDBusObjectPath &path)
Emitted after the module is registered successfully with D-Bus.
typedef RegisterOptions
QDBusConnection sessionBus()
QString path() const const
MessageType type() const const
QString path() const const
bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)
virtual const QMetaObject * metaObject() const const
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString left(qsizetype n) const const
QString mid(qsizetype position, qsizetype n) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QueuedConnection
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:12 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.