| 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 | #include "../interface.h" |
| 32 | |
| 33 | static const char serviceName[] = "org.qtproject.autotests.qpinger" ; |
| 34 | static const char objectPath[] = "/org/qtproject/qpinger" ; |
| 35 | //static const char *interfaceName = serviceName; |
| 36 | |
| 37 | class PingerServer : public QDBusServer, protected QDBusContext |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | Q_CLASSINFO("D-Bus Interface" , "org.qtproject.autotests.qpinger" ) |
| 41 | public: |
| 42 | PingerServer(QObject* parent = 0) |
| 43 | : QDBusServer(parent), |
| 44 | m_conn("none" ) |
| 45 | { |
| 46 | connect(asender: this, SIGNAL(newConnection(QDBusConnection)), SLOT(handleConnection(QDBusConnection))); |
| 47 | reset(); |
| 48 | } |
| 49 | |
| 50 | public slots: |
| 51 | QString address() const |
| 52 | { |
| 53 | if (!QDBusServer::isConnected()) |
| 54 | sendErrorReply(name: QDBusServer::lastError().name(), msg: QDBusServer::lastError().message()); |
| 55 | return QDBusServer::address(); |
| 56 | } |
| 57 | |
| 58 | void waitForConnected() |
| 59 | { |
| 60 | if (callPendingReply.type() != QDBusMessage::InvalidMessage) { |
| 61 | sendErrorReply(type: QDBusError::NotSupported, msg: "One call already pending!" ); |
| 62 | return; |
| 63 | } |
| 64 | if (m_conn.isConnected()) |
| 65 | return; |
| 66 | // not connected, we'll reply later |
| 67 | setDelayedReply(true); |
| 68 | callPendingReply = message(); |
| 69 | } |
| 70 | |
| 71 | void reset() |
| 72 | { |
| 73 | targetObj.m_stringProp = "This is a test" ; |
| 74 | targetObj.m_variantProp = QDBusVariant(QVariant(42)); |
| 75 | targetObj.m_complexProp = RegisteredType("This is a test" ); |
| 76 | } |
| 77 | |
| 78 | void voidSignal() |
| 79 | { |
| 80 | emit targetObj.voidSignal(); |
| 81 | } |
| 82 | |
| 83 | void stringSignal(const QString& value) |
| 84 | { |
| 85 | emit targetObj.stringSignal(value); |
| 86 | } |
| 87 | |
| 88 | void complexSignal(const QString& value) |
| 89 | { |
| 90 | RegisteredType reg(value); |
| 91 | emit targetObj.complexSignal(reg); |
| 92 | } |
| 93 | |
| 94 | void quit() |
| 95 | { |
| 96 | qApp->quit(); |
| 97 | } |
| 98 | |
| 99 | private slots: |
| 100 | void handleConnection(const QDBusConnection& con) |
| 101 | { |
| 102 | m_conn = con; |
| 103 | m_conn.registerObject(path: "/" , object: &targetObj, options: QDBusConnection::ExportScriptableContents); |
| 104 | if (callPendingReply.type() != QDBusMessage::InvalidMessage) { |
| 105 | QDBusConnection::sessionBus().send(message: callPendingReply.createReply()); |
| 106 | callPendingReply = QDBusMessage(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | Interface targetObj; |
| 112 | QDBusConnection m_conn; |
| 113 | QDBusMessage callPendingReply; |
| 114 | }; |
| 115 | |
| 116 | int main(int argc, char *argv[]) |
| 117 | { |
| 118 | QCoreApplication app(argc, argv); |
| 119 | |
| 120 | // register the meta types |
| 121 | qDBusRegisterMetaType<RegisteredType>(); |
| 122 | qRegisterMetaType<UnregisteredType>(); |
| 123 | |
| 124 | QDBusConnection con = QDBusConnection::sessionBus(); |
| 125 | if (!con.isConnected()) |
| 126 | exit(status: 1); |
| 127 | |
| 128 | if (!con.registerService(serviceName)) |
| 129 | exit(status: 2); |
| 130 | |
| 131 | PingerServer server; |
| 132 | con.registerObject(path: objectPath, object: &server, options: QDBusConnection::ExportAllSlots); |
| 133 | |
| 134 | printf(format: "ready.\n" ); |
| 135 | fflush(stdout); |
| 136 | |
| 137 | return app.exec(); |
| 138 | } |
| 139 | |
| 140 | #include "qpinger.moc" |
| 141 | |