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 | int MyObject::callCount = 0; |
39 | QVariantList MyObject::callArgs; |
40 | |
41 | class MyServer : public QDBusServer, protected QDBusContext |
42 | { |
43 | Q_OBJECT |
44 | Q_CLASSINFO("D-Bus Interface" , "org.qtproject.autotests.qmyserver" ) |
45 | |
46 | public: |
47 | MyServer(QObject* parent = 0) |
48 | : QDBusServer(parent), |
49 | m_conn("none" ) |
50 | { |
51 | connect(asender: this, SIGNAL(newConnection(QDBusConnection)), SLOT(handleConnection(QDBusConnection))); |
52 | } |
53 | |
54 | public slots: |
55 | QString address() const |
56 | { |
57 | if (!QDBusServer::isConnected()) |
58 | sendErrorReply(name: QDBusServer::lastError().name(), msg: QDBusServer::lastError().message()); |
59 | return QDBusServer::address(); |
60 | } |
61 | |
62 | void waitForConnected() |
63 | { |
64 | if (callPendingReply.type() != QDBusMessage::InvalidMessage) { |
65 | sendErrorReply(type: QDBusError::NotSupported, msg: "One call already pending!" ); |
66 | return; |
67 | } |
68 | if (m_conn.isConnected()) |
69 | return; |
70 | // not connected, we'll reply later |
71 | setDelayedReply(true); |
72 | callPendingReply = message(); |
73 | } |
74 | |
75 | void emitSignal(const QString &interface, const QString &name, const QString &arg) |
76 | { |
77 | QDBusMessage msg = QDBusMessage::createSignal(path: "/" , interface, name); |
78 | msg << arg; |
79 | m_conn.send(message: msg); |
80 | } |
81 | |
82 | void reset() |
83 | { |
84 | MyObject::callCount = 0; |
85 | obj.m_complexProp.clear(); |
86 | } |
87 | |
88 | int callCount() |
89 | { |
90 | return MyObject::callCount; |
91 | } |
92 | |
93 | QVariantList callArgs() |
94 | { |
95 | return MyObject::callArgs; |
96 | } |
97 | |
98 | void setProp1(int val) |
99 | { |
100 | obj.m_prop1 = val; |
101 | } |
102 | |
103 | int prop1() |
104 | { |
105 | return obj.m_prop1; |
106 | } |
107 | |
108 | void setComplexProp(QList<int> val) |
109 | { |
110 | obj.m_complexProp = val; |
111 | } |
112 | |
113 | QList<int> complexProp() |
114 | { |
115 | return obj.m_complexProp; |
116 | } |
117 | |
118 | bool interactiveAuthorization() |
119 | { |
120 | if (message().isInteractiveAuthorizationAllowed()) |
121 | return true; |
122 | |
123 | sendErrorReply(QStringLiteral("org.freedesktop.DBus.Error.InteractiveAuthorizationRequired" ), |
124 | QStringLiteral("Interactive authentication required." )); |
125 | return false; |
126 | } |
127 | |
128 | void quit() |
129 | { |
130 | qApp->quit(); |
131 | } |
132 | |
133 | private slots: |
134 | void handleConnection(const QDBusConnection& con) |
135 | { |
136 | m_conn = con; |
137 | m_conn.registerObject(path: "/" , object: &obj, options: QDBusConnection::ExportAllProperties |
138 | | QDBusConnection::ExportAllSlots |
139 | | QDBusConnection::ExportAllInvokables); |
140 | if (callPendingReply.type() != QDBusMessage::InvalidMessage) { |
141 | QDBusConnection::sessionBus().send(message: callPendingReply.createReply()); |
142 | callPendingReply = QDBusMessage(); |
143 | } |
144 | } |
145 | |
146 | private: |
147 | QDBusConnection m_conn; |
148 | QDBusMessage callPendingReply; |
149 | MyObject obj; |
150 | }; |
151 | |
152 | int main(int argc, char *argv[]) |
153 | { |
154 | QCoreApplication app(argc, argv); |
155 | |
156 | QDBusConnection con = QDBusConnection::sessionBus(); |
157 | if (!con.isConnected()) |
158 | exit(status: 1); |
159 | |
160 | if (!con.registerService(serviceName)) |
161 | exit(status: 2); |
162 | |
163 | MyServer server; |
164 | con.registerObject(path: objectPath, object: &server, options: QDBusConnection::ExportAllSlots); |
165 | |
166 | printf(format: "ready.\n" ); |
167 | fflush(stdout); |
168 | |
169 | return app.exec(); |
170 | } |
171 | |
172 | #include "qmyserver.moc" |
173 | |