| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #ifndef MYOBJECT_H |
| 30 | #define MYOBJECT_H |
| 31 | |
| 32 | #include <QtCore/QObject> |
| 33 | #include <QtDBus/QtDBus> |
| 34 | |
| 35 | extern const char *slotSpy; |
| 36 | extern QString valueSpy; |
| 37 | |
| 38 | class QDBusSignalSpy: public QObject |
| 39 | { |
| 40 | Q_OBJECT |
| 41 | |
| 42 | public slots: |
| 43 | void slot(const QDBusMessage &msg) |
| 44 | { |
| 45 | ++count; |
| 46 | interface = msg.interface(); |
| 47 | name = msg.member(); |
| 48 | signature = msg.signature(); |
| 49 | path = msg.path(); |
| 50 | value.clear(); |
| 51 | if (msg.arguments().count()) |
| 52 | value = msg.arguments().at(i: 0); |
| 53 | } |
| 54 | |
| 55 | public: |
| 56 | QDBusSignalSpy() : count(0) { } |
| 57 | |
| 58 | int count; |
| 59 | QString interface; |
| 60 | QString name; |
| 61 | QString signature; |
| 62 | QString path; |
| 63 | QVariant value; |
| 64 | }; |
| 65 | |
| 66 | class Interface1: public QDBusAbstractAdaptor |
| 67 | { |
| 68 | Q_OBJECT |
| 69 | Q_CLASSINFO("D-Bus Interface" , "local.Interface1" ) |
| 70 | public: |
| 71 | Interface1(QObject *parent) : QDBusAbstractAdaptor(parent) |
| 72 | { } |
| 73 | }; |
| 74 | |
| 75 | class Interface2: public QDBusAbstractAdaptor |
| 76 | { |
| 77 | Q_OBJECT |
| 78 | Q_CLASSINFO("D-Bus Interface" , "local.Interface2" ) |
| 79 | Q_PROPERTY(QString prop1 READ prop1) |
| 80 | Q_PROPERTY(QString prop2 READ prop2 WRITE setProp2 SCRIPTABLE true) |
| 81 | Q_PROPERTY(QUrl nonDBusProperty READ nonDBusProperty) |
| 82 | public: |
| 83 | Interface2(QObject *parent) : QDBusAbstractAdaptor(parent) |
| 84 | { setAutoRelaySignals(true); } |
| 85 | |
| 86 | QString prop1() const |
| 87 | { return QLatin1String("QString Interface2::prop1() const" ); } |
| 88 | |
| 89 | QString prop2() const |
| 90 | { return QLatin1String("QString Interface2::prop2() const" ); } |
| 91 | |
| 92 | void setProp2(const QString &value) |
| 93 | { |
| 94 | slotSpy = "void Interface2::setProp2(const QString &)" ; |
| 95 | valueSpy = value; |
| 96 | } |
| 97 | |
| 98 | QUrl nonDBusProperty() const |
| 99 | { return QUrl(); } |
| 100 | |
| 101 | void emitSignal(const QString &, const QVariant &) |
| 102 | { emit signal(); } |
| 103 | |
| 104 | public slots: |
| 105 | void method() |
| 106 | { |
| 107 | slotSpy = "void Interface2::method()" ; |
| 108 | } |
| 109 | |
| 110 | Q_SCRIPTABLE void scriptableMethod() |
| 111 | { |
| 112 | slotSpy = "void Interface2::scriptableMethod()" ; |
| 113 | } |
| 114 | |
| 115 | signals: |
| 116 | void signal(); |
| 117 | }; |
| 118 | |
| 119 | class Interface3: public QDBusAbstractAdaptor |
| 120 | { |
| 121 | Q_OBJECT |
| 122 | Q_CLASSINFO("D-Bus Interface" , "local.Interface3" ) |
| 123 | Q_PROPERTY(QString prop1 READ prop1) |
| 124 | Q_PROPERTY(QString prop2 READ prop2 WRITE setProp2) |
| 125 | Q_PROPERTY(QString interface3prop READ interface3prop) |
| 126 | public: |
| 127 | Interface3(QObject *parent) : QDBusAbstractAdaptor(parent) |
| 128 | { setAutoRelaySignals(true); } |
| 129 | |
| 130 | QString prop1() const |
| 131 | { return QLatin1String("QString Interface3::prop1() const" ); } |
| 132 | |
| 133 | QString prop2() const |
| 134 | { return QLatin1String("QString Interface3::prop2() const" ); } |
| 135 | |
| 136 | void setProp2(const QString &value) |
| 137 | { |
| 138 | slotSpy = "void Interface3::setProp2(const QString &)" ; |
| 139 | valueSpy = value; |
| 140 | } |
| 141 | |
| 142 | QString interface3prop() const |
| 143 | { return QLatin1String("QString Interface3::interface3prop() const" ); } |
| 144 | |
| 145 | void emitSignal(const QString &name, const QVariant &value) |
| 146 | { |
| 147 | if (name == "signalVoid" ) |
| 148 | emit signalVoid(); |
| 149 | else if (name == "signalInt" ) |
| 150 | emit signalInt(value.toInt()); |
| 151 | else if (name == "signalString" ) |
| 152 | emit signalString(value.toString()); |
| 153 | } |
| 154 | |
| 155 | public slots: |
| 156 | void methodVoid() { slotSpy = "void Interface3::methodVoid()" ; } |
| 157 | void methodInt(int) { slotSpy = "void Interface3::methodInt(int)" ; } |
| 158 | void methodString(QString) { slotSpy = "void Interface3::methodString(QString)" ; } |
| 159 | |
| 160 | int methodStringString(const QString &s, QString &out) |
| 161 | { |
| 162 | slotSpy = "int Interface3::methodStringString(const QString &, QString &)" ; |
| 163 | out = s; |
| 164 | return 42; |
| 165 | } |
| 166 | |
| 167 | signals: |
| 168 | void signalVoid(); |
| 169 | void signalInt(int); |
| 170 | void signalString(const QString &); |
| 171 | }; |
| 172 | |
| 173 | class Interface4: public QDBusAbstractAdaptor |
| 174 | { |
| 175 | Q_OBJECT |
| 176 | Q_CLASSINFO("D-Bus Interface" , "local.Interface4" ) |
| 177 | Q_PROPERTY(QString prop1 READ prop1) |
| 178 | Q_PROPERTY(QString prop2 READ prop2 WRITE setProp2) |
| 179 | Q_PROPERTY(QString interface4prop READ interface4prop) |
| 180 | public: |
| 181 | Interface4(QObject *parent) : QDBusAbstractAdaptor(parent) |
| 182 | { setAutoRelaySignals(true); } |
| 183 | |
| 184 | QString prop1() const |
| 185 | { return QLatin1String("QString Interface4::prop1() const" ); } |
| 186 | |
| 187 | QString prop2() const |
| 188 | { return QLatin1String("QString Interface4::prop2() const" ); } |
| 189 | |
| 190 | QString interface4prop() const |
| 191 | { return QLatin1String("QString Interface4::interface4prop() const" ); } |
| 192 | |
| 193 | void setProp2(const QString &value) |
| 194 | { |
| 195 | slotSpy = "void Interface4::setProp2(const QString &)" ; |
| 196 | valueSpy = value; |
| 197 | } |
| 198 | |
| 199 | void emitSignal(const QString &, const QVariant &value) |
| 200 | { |
| 201 | switch (value.type()) |
| 202 | { |
| 203 | case QVariant::Invalid: |
| 204 | emit signal(); |
| 205 | break; |
| 206 | case QVariant::Int: |
| 207 | emit signal(value.toInt()); |
| 208 | break; |
| 209 | case QVariant::String: |
| 210 | emit signal(value.toString()); |
| 211 | break; |
| 212 | default: |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | public slots: |
| 218 | void method() { slotSpy = "void Interface4::method()" ; } |
| 219 | void method(int) { slotSpy = "void Interface4::method(int)" ; } |
| 220 | void method(QString) { slotSpy = "void Interface4::method(QString)" ; } |
| 221 | |
| 222 | signals: |
| 223 | void signal(); |
| 224 | void signal(int); |
| 225 | void signal(const QString &); |
| 226 | }; |
| 227 | |
| 228 | class MyObject: public QObject |
| 229 | { |
| 230 | Q_OBJECT |
| 231 | Q_CLASSINFO("D-Bus Interface" , "local.MyObject" ) |
| 232 | public: |
| 233 | Interface1 *if1; |
| 234 | Interface2 *if2; |
| 235 | Interface3 *if3; |
| 236 | Interface4 *if4; |
| 237 | |
| 238 | MyObject(int n = 4) |
| 239 | : if1(0), if2(0), if3(0), if4(0) |
| 240 | { |
| 241 | switch (n) |
| 242 | { |
| 243 | case 4: |
| 244 | if4 = new Interface4(this); |
| 245 | Q_FALLTHROUGH(); |
| 246 | case 3: |
| 247 | if3 = new Interface3(this); |
| 248 | Q_FALLTHROUGH(); |
| 249 | case 2: |
| 250 | if2 = new Interface2(this); |
| 251 | Q_FALLTHROUGH(); |
| 252 | case 1: |
| 253 | if1 = new Interface1(this); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void emitSignal(const QString &name, const QVariant &value) |
| 258 | { |
| 259 | if (name == "scriptableSignalVoid" ) |
| 260 | emit scriptableSignalVoid(); |
| 261 | else if (name == "scriptableSignalInt" ) |
| 262 | emit scriptableSignalInt(value.toInt()); |
| 263 | else if (name == "scriptableSignalString" ) |
| 264 | emit scriptableSignalString(value.toString()); |
| 265 | else if (name == "nonScriptableSignalVoid" ) |
| 266 | emit nonScriptableSignalVoid(); |
| 267 | } |
| 268 | |
| 269 | signals: |
| 270 | Q_SCRIPTABLE void scriptableSignalVoid(); |
| 271 | Q_SCRIPTABLE void scriptableSignalInt(int); |
| 272 | Q_SCRIPTABLE void scriptableSignalString(QString); |
| 273 | void nonScriptableSignalVoid(); |
| 274 | }; |
| 275 | |
| 276 | #endif // MYOBJECT_H |
| 277 | |