| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// Copyright (C) 2016 Intel Corporation. |
| 3 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 4 |
|
| 5 |
#include <QCoreApplication> |
| 6 |
#include <QDBusServer> |
| 7 |
#include <QDBusContext> |
| 8 |
#include <QDBusConnection> |
| 9 |
#include <QDBusVariant> |
| 10 |
#include <QDBusServer> |
| 11 |
|
| 12 |
#include "../myobject.h" |
| 13 |
|
| 14 |
static const char serviceName[] = "org.qtproject.autotests.qmyserver"; |
| 15 |
static const char objectPath[] = "/org/qtproject/qmyserver"; |
| 16 |
//static const char *interfaceName = serviceName; |
| 17 |
|
| 18 |
const char *slotSpy; |
| 19 |
QString valueSpy; |
| 20 |
|
| 21 |
Q_DECLARE_METATYPE(QDBusConnection::RegisterOptions) |
| 22 |
|
| 23 |
class MyServer : public QDBusServer, protected QDBusContext |
| 24 |
{ |
| 25 |
Q_OBJECT |
| 26 |
Q_CLASSINFO("D-Bus Interface", "org.qtproject.autotests.qmyserver") |
| 27 |
|
| 28 |
public: |
| 29 |
MyServer(QObject* parent = nullptr) |
| 30 |
: QDBusServer(parent), |
| 31 |
m_conn("none"), |
| 32 |
obj(NULL) |
| 33 |
{ |
| 34 |
connect(this, SIGNAL(newConnection(QDBusConnection)), SLOT(handleConnection(QDBusConnection))); |
| 35 |
} |
| 36 |
|
| 37 |
~MyServer() |
| 38 |
{ |
| 39 |
if (obj) |
| 40 |
obj->deleteLater(); |
| 41 |
} |
| 42 |
|
| 43 |
public slots: |
| 44 |
QString address() const |
| 45 |
{ |
| 46 |
if (!QDBusServer::isConnected()) |
| 47 |
sendErrorReply(QDBusServer::lastError().name(), QDBusServer::lastError().message()); |
| 48 |
return QDBusServer::address(); |
| 49 |
} |
| 50 |
|
| 51 |
void waitForConnected() |
| 52 |
{ |
| 53 |
if (callPendingReply.type() != QDBusMessage::InvalidMessage) { |
| 54 |
sendErrorReply(QDBusError::NotSupported, "One call already pending!"); |
| 55 |
return; |
| 56 |
} |
| 57 |
if (m_conn.isConnected()) |
| 58 |
return; |
| 59 |
// not connected, we'll reply later |
| 60 |
setDelayedReply(true); |
| 61 |
callPendingReply = message(); |
| 62 |
} |
| 63 |
|
| 64 |
Q_NOREPLY void requestSync(const QString &seq) |
| 65 |
{ |
| 66 |
emit syncReceived(seq); |
| 67 |
} |
| 68 |
|
| 69 |
void emitSignal(const QString& interface, const QString& name, const QDBusVariant& parameter) |
| 70 |
{ |
| 71 |
if (interface.endsWith('2')) |
| 72 |
obj->if2->emitSignal(name, parameter.variant()); |
| 73 |
else if (interface.endsWith('3')) |
| 74 |
obj->if3->emitSignal(name, parameter.variant()); |
| 75 |
else if (interface.endsWith('4')) |
| 76 |
obj->if4->emitSignal(name, parameter.variant()); |
| 77 |
else |
| 78 |
obj->emitSignal(name, parameter.variant()); |
| 79 |
} |
| 80 |
|
| 81 |
void emitSignal2(const QString& interface, const QString& name) |
| 82 |
{ |
| 83 |
if (interface.endsWith('2')) |
| 84 |
obj->if2->emitSignal(name, QVariant()); |
| 85 |
else if (interface.endsWith('3')) |
| 86 |
obj->if3->emitSignal(name, QVariant()); |
| 87 |
else if (interface.endsWith('4')) |
| 88 |
obj->if4->emitSignal(name, QVariant()); |
| 89 |
else |
| 90 |
obj->emitSignal(name, QVariant()); |
| 91 |
} |
| 92 |
|
| 93 |
void newMyObject(int nInterfaces = 4) |
| 94 |
{ |
| 95 |
if (obj) |
| 96 |
obj->deleteLater(); |
| 97 |
obj = new MyObject(nInterfaces); |
| 98 |
} |
| 99 |
|
| 100 |
void registerMyObject(const QString & path, int options) |
| 101 |
{ |
| 102 |
m_conn.registerObject(path, obj, (QDBusConnection::RegisterOptions)options); |
| 103 |
} |
| 104 |
|
| 105 |
QString slotSpyServer() |
| 106 |
{ |
| 107 |
return QLatin1String(slotSpy); |
| 108 |
} |
| 109 |
|
| 110 |
QString valueSpyServer() |
| 111 |
{ |
| 112 |
return valueSpy; |
| 113 |
} |
| 114 |
|
| 115 |
void clearValueSpy() |
| 116 |
{ |
| 117 |
valueSpy.clear(); |
| 118 |
} |
| 119 |
|
| 120 |
void quit() |
| 121 |
{ |
| 122 |
qApp->quit(); |
| 123 |
} |
| 124 |
|
| 125 |
signals: |
| 126 |
Q_SCRIPTABLE void syncReceived(const QString &sequence); |
| 127 |
|
| 128 |
private slots: |
| 129 |
void handleConnection(QDBusConnection con) |
| 130 |
{ |
| 131 |
m_conn = con; |
| 132 |
con.registerObject(objectPath, this, QDBusConnection::ExportScriptableSignals); |
| 133 |
|
| 134 |
if (callPendingReply.type() != QDBusMessage::InvalidMessage) { |
| 135 |
QDBusConnection::sessionBus().send(callPendingReply.createReply()); |
| 136 |
callPendingReply = QDBusMessage(); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
private: |
| 141 |
QDBusConnection m_conn; |
| 142 |
QDBusMessage callPendingReply; |
| 143 |
MyObject* obj; |
| 144 |
}; |
| 145 |
|
| 146 |
int main(int argc, char *argv[]) |
| 147 |
{ |
| 148 |
QCoreApplication app(argc, argv); |
| 149 |
|
| 150 |
QDBusConnection con = QDBusConnection::sessionBus(); |
| 151 |
if (!con.isConnected()) |
| 152 |
exit(1); |
| 153 |
|
| 154 |
if (!con.registerService(serviceName)) |
| 155 |
exit(2); |
| 156 |
|
| 157 |
MyServer server; |
| 158 |
con.registerObject(objectPath, &server, QDBusConnection::ExportAllSlots); |
| 159 |
|
| 160 |
printf("ready.\n"); |
| 161 |
fflush(stdout); |
| 162 |
|
| 163 |
return app.exec(); |
| 164 |
} |
| 165 |
|
| 166 |
#include "qmyserver.moc" |