KDBusAddons

kdedmodule.cpp
1 /*
2  This file is part of the KDE libraries
3 
4  SPDX-FileCopyrightText: 2001 Waldo Bastian <[email protected]>
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 
23  : QObject(parent)
24  , d(new KDEDModulePrivate)
25 {
26 }
27 
28 KDEDModule::~KDEDModule()
29 {
30  Q_EMIT moduleDeleted(this);
31 }
32 
34 {
35  d->moduleName = name;
36  QDBusObjectPath realPath(QLatin1String("/modules/") + d->moduleName);
37 
38  if (realPath.path().isEmpty()) {
39  qCWarning(KDBUSADDONS_LOG) << "The kded module name" << name << "is invalid!";
40  return;
41  }
42 
44 
45  if (this->metaObject()->indexOfClassInfo("D-Bus Interface") != -1) {
46  // 1. There are kded modules that don't have a D-Bus interface.
47  // 2. qt 4.4.3 crashes when trying to emit signals on class without
48  // Q_CLASSINFO("D-Bus Interface", "<your interface>") but
49  // ExportSignal set.
50  // We try to solve that for now with just registering Properties and
51  // Adaptors. But we should investigate where the sense is in registering
52  // the module at all. Just for autoload? Is there a better solution?
54  } else {
55  // Full functional module. Register everything.
59  qCDebug(KDBUSADDONS_LOG) << "Registration of kded module" << d->moduleName << "without D-Bus interface.";
60  }
61 
62  if (!QDBusConnection::sessionBus().registerObject(realPath.path(), this, regOptions)) {
63  // Happens for khotkeys but the module works. Need some time to investigate.
64  qCDebug(KDBUSADDONS_LOG) << "registerObject() returned false for" << d->moduleName;
65  } else {
66  // qCDebug(KDBUSADDONS_LOG) << "registerObject() successful for" << d->moduleName;
67  // Fix deadlock with Qt 5.6: this has to be delayed until the dbus thread is unlocked
68  auto registeredSignal = [this, realPath]() {
69  moduleRegistered(realPath);
70  };
71  QMetaObject::invokeMethod(this, registeredSignal, Qt::QueuedConnection);
72  }
73 }
74 
75 QString KDEDModule::moduleName() const
76 {
77  return d->moduleName;
78 }
79 
80 static const char s_modules_path[] = "/modules/";
81 
83 {
85  return QString();
86  }
87 
88  QString obj = message.path();
89  if (!obj.startsWith(QLatin1String(s_modules_path))) {
90  return QString();
91  }
92 
93  // Remove the <s_modules_path> part
94  obj = obj.mid(strlen(s_modules_path));
95 
96  // Remove the part after the modules name
97  const int index = obj.indexOf(QLatin1Char('/'));
98  if (index != -1) {
99  obj = obj.left(index);
100  }
101 
102  return obj;
103 }
104 
105 #include "moc_kdedmodule.cpp"
KDEDModule(QObject *parent=nullptr)
Constructor.
Definition: kdedmodule.cpp:22
Q_EMITQ_EMIT
void moduleRegistered(const QDBusObjectPath &path)
Emitted after the module is registered successfully with D-Bus.
static QString moduleForMessage(const QDBusMessage &message)
Returns the module being called by this D-Bus message.
Definition: kdedmodule.cpp:82
QDBusConnection sessionBus()
bool isEmpty() const const
QueuedConnection
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QString path() const const
virtual const QMetaObject * metaObject() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QString left(int n) const const
typedef RegisterOptions
void setModuleName(const QString &name)
Sets the name of the module, and uses it to register the module to D-Bus.
Definition: kdedmodule.cpp:33
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
QString mid(int position, int n) const const
void moduleDeleted(KDEDModule *)
Emitted when the module is being deleted.
QString message
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Fri Dec 1 2023 03:50:41 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.