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 | |
16 | class KDEDModulePrivate |
17 | { |
18 | public: |
19 | QString moduleName; |
20 | }; |
21 | |
22 | KDEDModule::KDEDModule(QObject *parent) |
23 | : QObject(parent) |
24 | , d(new KDEDModulePrivate) |
25 | { |
26 | } |
27 | |
28 | KDEDModule::~KDEDModule() |
29 | { |
30 | } |
31 | |
32 | void KDEDModule::setModuleName(const QString &name) |
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 | |
42 | QDBusConnection::RegisterOptions regOptions; |
43 | |
44 | if (this->metaObject()->indexOfClassInfo(name: "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? |
52 | regOptions = QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors; |
53 | } else { |
54 | // Full functional module. Register everything. |
55 | regOptions = QDBusConnection::ExportScriptableSlots // |
56 | | QDBusConnection::ExportScriptableProperties // |
57 | | QDBusConnection::ExportAdaptors; |
58 | qCDebug(KDBUSADDONS_LOG) << "Registration of kded module" << d->moduleName << "without D-Bus interface." ; |
59 | } |
60 | |
61 | if (!QDBusConnection::sessionBus().registerObject(path: realPath.path(), object: this, options: 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(path: realPath); |
69 | }; |
70 | QMetaObject::invokeMethod(object: this, function&: registeredSignal, type: Qt::QueuedConnection); |
71 | } |
72 | } |
73 | |
74 | QString KDEDModule::moduleName() const |
75 | { |
76 | return d->moduleName; |
77 | } |
78 | |
79 | static const char s_modules_path[] = "/modules/" ; |
80 | |
81 | QString KDEDModule::moduleForMessage(const QDBusMessage &message) |
82 | { |
83 | if (message.type() != QDBusMessage::MethodCallMessage) { |
84 | return QString(); |
85 | } |
86 | |
87 | QString obj = message.path(); |
88 | if (!obj.startsWith(s: QLatin1String(s_modules_path))) { |
89 | return QString(); |
90 | } |
91 | |
92 | // Remove the <s_modules_path> part |
93 | obj = obj.mid(position: strlen(s: s_modules_path)); |
94 | |
95 | // Remove the part after the modules name |
96 | const int index = obj.indexOf(c: QLatin1Char('/')); |
97 | if (index != -1) { |
98 | obj = obj.left(n: index); |
99 | } |
100 | |
101 | return obj; |
102 | } |
103 | |
104 | #include "moc_kdedmodule.cpp" |
105 | |