| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Copyright (C) 2016 Intel Corporation. |
| 5 | ** Contact: https://www.qt.io/licensing/ |
| 6 | ** |
| 7 | ** This file is part of the test suite of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 22 | ** included in the packaging of this file. Please review the following |
| 23 | ** information to ensure the GNU General Public License requirements will |
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 25 | ** |
| 26 | ** $QT_END_LICENSE$ |
| 27 | ** |
| 28 | ****************************************************************************/ |
| 29 | #include <QtCore/QtCore> |
| 30 | #include <QtDBus/QtDBus> |
| 31 | |
| 32 | #include "../myobject.h" |
| 33 | |
| 34 | static const char serviceName[] = "org.qtproject.autotests.qmyserver" ; |
| 35 | static const char objectPath[] = "/org/qtproject/qmyserver" ; |
| 36 | //static const char *interfaceName = serviceName; |
| 37 | |
| 38 | const char *slotSpy; |
| 39 | QString valueSpy; |
| 40 | |
| 41 | Q_DECLARE_METATYPE(QDBusConnection::RegisterOptions) |
| 42 | |
| 43 | class MyServer : public QDBusServer, protected QDBusContext |
| 44 | { |
| 45 | Q_OBJECT |
| 46 | Q_CLASSINFO("D-Bus Interface" , "org.qtproject.autotests.qmyserver" ) |
| 47 | |
| 48 | public: |
| 49 | MyServer(QObject* parent = 0) |
| 50 | : QDBusServer(parent), |
| 51 | m_conn("none" ), |
| 52 | obj(NULL) |
| 53 | { |
| 54 | connect(asender: this, SIGNAL(newConnection(QDBusConnection)), SLOT(handleConnection(QDBusConnection))); |
| 55 | } |
| 56 | |
| 57 | ~MyServer() |
| 58 | { |
| 59 | if (obj) |
| 60 | obj->deleteLater(); |
| 61 | } |
| 62 | |
| 63 | public slots: |
| 64 | QString address() const |
| 65 | { |
| 66 | if (!QDBusServer::isConnected()) |
| 67 | sendErrorReply(name: QDBusServer::lastError().name(), msg: QDBusServer::lastError().message()); |
| 68 | return QDBusServer::address(); |
| 69 | } |
| 70 | |
| 71 | void waitForConnected() |
| 72 | { |
| 73 | if (callPendingReply.type() != QDBusMessage::InvalidMessage) { |
| 74 | sendErrorReply(type: QDBusError::NotSupported, msg: "One call already pending!" ); |
| 75 | return; |
| 76 | } |
| 77 | if (m_conn.isConnected()) |
| 78 | return; |
| 79 | // not connected, we'll reply later |
| 80 | setDelayedReply(true); |
| 81 | callPendingReply = message(); |
| 82 | } |
| 83 | |
| 84 | Q_NOREPLY void requestSync(const QString &seq) |
| 85 | { |
| 86 | emit syncReceived(sequence: seq); |
| 87 | } |
| 88 | |
| 89 | void emitSignal(const QString& interface, const QString& name, const QDBusVariant& parameter) |
| 90 | { |
| 91 | if (interface.endsWith(c: '2')) |
| 92 | obj->if2->emitSignal(name, parameter.variant()); |
| 93 | else if (interface.endsWith(c: '3')) |
| 94 | obj->if3->emitSignal(name, value: parameter.variant()); |
| 95 | else if (interface.endsWith(c: '4')) |
| 96 | obj->if4->emitSignal(name, value: parameter.variant()); |
| 97 | else |
| 98 | obj->emitSignal(name, value: parameter.variant()); |
| 99 | } |
| 100 | |
| 101 | void emitSignal2(const QString& interface, const QString& name) |
| 102 | { |
| 103 | if (interface.endsWith(c: '2')) |
| 104 | obj->if2->emitSignal(name, QVariant()); |
| 105 | else if (interface.endsWith(c: '3')) |
| 106 | obj->if3->emitSignal(name, value: QVariant()); |
| 107 | else if (interface.endsWith(c: '4')) |
| 108 | obj->if4->emitSignal(name, value: QVariant()); |
| 109 | else |
| 110 | obj->emitSignal(name, value: QVariant()); |
| 111 | } |
| 112 | |
| 113 | void newMyObject(int nInterfaces = 4) |
| 114 | { |
| 115 | if (obj) |
| 116 | obj->deleteLater(); |
| 117 | obj = new MyObject(nInterfaces); |
| 118 | } |
| 119 | |
| 120 | void registerMyObject(const QString & path, int options) |
| 121 | { |
| 122 | m_conn.registerObject(path, object: obj, options: (QDBusConnection::RegisterOptions)options); |
| 123 | } |
| 124 | |
| 125 | QString slotSpyServer() |
| 126 | { |
| 127 | return QLatin1String(slotSpy); |
| 128 | } |
| 129 | |
| 130 | QString valueSpyServer() |
| 131 | { |
| 132 | return valueSpy; |
| 133 | } |
| 134 | |
| 135 | void clearValueSpy() |
| 136 | { |
| 137 | valueSpy.clear(); |
| 138 | } |
| 139 | |
| 140 | void quit() |
| 141 | { |
| 142 | qApp->quit(); |
| 143 | } |
| 144 | |
| 145 | signals: |
| 146 | Q_SCRIPTABLE void syncReceived(const QString &sequence); |
| 147 | |
| 148 | private slots: |
| 149 | void handleConnection(QDBusConnection con) |
| 150 | { |
| 151 | m_conn = con; |
| 152 | con.registerObject(path: objectPath, object: this, options: QDBusConnection::ExportScriptableSignals); |
| 153 | |
| 154 | if (callPendingReply.type() != QDBusMessage::InvalidMessage) { |
| 155 | QDBusConnection::sessionBus().send(message: callPendingReply.createReply()); |
| 156 | callPendingReply = QDBusMessage(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | private: |
| 161 | QDBusConnection m_conn; |
| 162 | QDBusMessage callPendingReply; |
| 163 | MyObject* obj; |
| 164 | }; |
| 165 | |
| 166 | int main(int argc, char *argv[]) |
| 167 | { |
| 168 | QCoreApplication app(argc, argv); |
| 169 | |
| 170 | QDBusConnection con = QDBusConnection::sessionBus(); |
| 171 | if (!con.isConnected()) |
| 172 | exit(status: 1); |
| 173 | |
| 174 | if (!con.registerService(serviceName)) |
| 175 | exit(status: 2); |
| 176 | |
| 177 | MyServer server; |
| 178 | con.registerObject(path: objectPath, object: &server, options: QDBusConnection::ExportAllSlots); |
| 179 | |
| 180 | printf(format: "ready.\n" ); |
| 181 | fflush(stdout); |
| 182 | |
| 183 | return app.exec(); |
| 184 | } |
| 185 | |
| 186 | #include "qmyserver.moc" |
| 187 | |