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 <QDBusObjectPath>
14 #include <QDBusMessage>
15 
16 class KDEDModulePrivate
17 {
18 public:
19  QString moduleName;
20 };
21 
23  : QObject(parent), d(new KDEDModulePrivate)
24 {
25 }
26 
27 KDEDModule::~KDEDModule()
28 {
29  Q_EMIT moduleDeleted(this);
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  QMetaObject::invokeMethod(this, "moduleRegistered", Qt::QueuedConnection, Q_ARG(QDBusObjectPath, realPath));
68  }
69 
70 }
71 
72 QString KDEDModule::moduleName() const
73 {
74  return d->moduleName;
75 }
76 
77 static const char s_modules_path[] = "/modules/";
78 
80 {
81  if (message.type() != QDBusMessage::MethodCallMessage) {
82  return QString();
83  }
84 
85  QString obj = message.path();
86  if (!obj.startsWith(QLatin1String(s_modules_path))) {
87  return QString();
88  }
89 
90  // Remove the <s_modules_path> part
91  obj = obj.mid(strlen(s_modules_path));
92 
93  // Remove the part after the modules name
94  const int index = obj.indexOf(QLatin1Char('/'));
95  if (index != -1) {
96  obj = obj.left(index);
97  }
98 
99  return obj;
100 }
101 
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
virtual const QMetaObject * metaObject() const const
QDBusMessage::MessageType type() const const
QDBusConnection sessionBus()
void setModuleName(const QString &name)
Sets the name of the module, and uses it to register the module to D-Bus.
Definition: kdedmodule.cpp:32
void moduleDeleted(KDEDModule *)
Emitted when the module is being deleted.
bool isEmpty() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
KDEDModule(QObject *parent=nullptr)
Constructor.
Definition: kdedmodule.cpp:22
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)
static QString moduleForMessage(const QDBusMessage &message)
Returns the module being called by this D-Bus message.
Definition: kdedmodule.cpp:79
QString mid(int position, int n) const const
typedef RegisterOptions
QString left(int n) const const
QString path() const const
QueuedConnection
QString path() const const
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Mar 2 2021 00:58:24 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.