| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
#include <QString> |
| 5 |
#include <QDBusArgument> |
| 6 |
#include <QDBusMetaType> |
| 7 |
#include <QDBusMessage> |
| 8 |
#include <QDBusContext> |
| 9 |
|
| 10 |
typedef QDBusVariant MyElement; |
| 11 |
typedef QList<MyElement> MyArray; |
| 12 |
typedef QHash<int, MyElement> MyDictionary; |
| 13 |
typedef QDBusVariant MyType; |
| 14 |
typedef QDBusVariant MyValue; |
| 15 |
typedef QDBusVariant Type; |
| 16 |
QDBusArgument argument; |
| 17 |
|
| 18 |
class MyObject: public QObject |
| 19 |
{ |
| 20 |
Q_OBJECT |
| 21 |
|
| 22 |
Q_CLASSINFO("D-Bus Interface", "org.qtproject.QtDBus.MyObject") |
| 23 |
Q_CLASSINFO("D-Bus Introspection", "" |
| 24 |
" <interface name=\"org.qtproject.QtDBus.MyObject\" >\n" |
| 25 |
" <property access=\"readwrite\" type=\"i\" name=\"prop1\" />\n" |
| 26 |
" <property name=\"complexProp\" type=\"ai\" access=\"readwrite\">\n" |
| 27 |
" <annotation name=\"org.qtproject.QtDBus.QtTypeName\" value=\"QList<int>\"/>\n" |
| 28 |
" </property>\n" |
| 29 |
" <signal name=\"somethingHappened\" >\n" |
| 30 |
" <arg direction=\"out\" type=\"s\" />\n" |
| 31 |
" </signal>\n" |
| 32 |
" <method name=\"ping\" >\n" |
| 33 |
" <arg direction=\"in\" type=\"v\" name=\"ping\" />\n" |
| 34 |
" <arg direction=\"out\" type=\"v\" name=\"ping\" />\n" |
| 35 |
" </method>\n" |
| 36 |
" <method name=\"ping_invokable\" >\n" |
| 37 |
" <arg direction=\"in\" type=\"v\" name=\"ping_invokable\" />\n" |
| 38 |
" <arg direction=\"out\" type=\"v\" name=\"ping_invokable\" />\n" |
| 39 |
" </method>\n" |
| 40 |
" <method name=\"ping\" >\n" |
| 41 |
" <arg direction=\"in\" type=\"v\" name=\"ping1\" />\n" |
| 42 |
" <arg direction=\"in\" type=\"v\" name=\"ping2\" />\n" |
| 43 |
" <arg direction=\"out\" type=\"v\" name=\"pong1\" />\n" |
| 44 |
" <arg direction=\"out\" type=\"v\" name=\"pong2\" />\n" |
| 45 |
" </method>\n" |
| 46 |
" <method name=\"ping_invokable\" >\n" |
| 47 |
" <arg direction=\"in\" type=\"v\" name=\"ping1_invokable\" />\n" |
| 48 |
" <arg direction=\"in\" type=\"v\" name=\"ping2_invokable\" />\n" |
| 49 |
" <arg direction=\"out\" type=\"v\" name=\"pong1_invokable\" />\n" |
| 50 |
" <arg direction=\"out\" type=\"v\" name=\"pong2_invokable\" />\n" |
| 51 |
" </method>\n" |
| 52 |
" <method name=\"ping\" >\n" |
| 53 |
" <arg direction=\"in\" type=\"ai\" name=\"ping\" />\n" |
| 54 |
" <arg direction=\"out\" type=\"ai\" name=\"ping\" />\n" |
| 55 |
" <annotation name=\"org.qtproject.QtDBus.QtTypeName.In0\" value=\"QList<int>\"/>\n" |
| 56 |
" <annotation name=\"org.qtproject.QtDBus.QtTypeName.Out0\" value=\"QList<int>\"/>\n" |
| 57 |
" </method>\n" |
| 58 |
" <method name=\"ping_invokable\" >\n" |
| 59 |
" <arg direction=\"in\" type=\"ai\" name=\"ping_invokable\" />\n" |
| 60 |
" <arg direction=\"out\" type=\"ai\" name=\"ping_invokable\" />\n" |
| 61 |
" <annotation name=\"org.qtproject.QtDBus.QtTypeName.In0\" value=\"QList<int>\"/>\n" |
| 62 |
" <annotation name=\"org.qtproject.QtDBus.QtTypeName.Out0\" value=\"QList<int>\"/>\n" |
| 63 |
" </method>\n" |
| 64 |
" </interface>\n" |
| 65 |
"") |
| 66 |
Q_PROPERTY(int prop1 READ prop1 WRITE setProp1) |
| 67 |
Q_PROPERTY(QList<int> complexProp READ complexProp WRITE setComplexProp) |
| 68 |
|
| 69 |
public: |
| 70 |
static int callCount; |
| 71 |
static QVariantList callArgs; |
| 72 |
MyObject() |
| 73 |
{ |
| 74 |
QObject *subObject = new QObject(this); |
| 75 |
subObject->setObjectName("subObject"); |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
struct MyMember |
| 80 |
{ |
| 81 |
int subMember1; |
| 82 |
int subMember2; |
| 83 |
}; |
| 84 |
|
| 85 |
//! [0-0] |
| 86 |
struct MyStructure |
| 87 |
{ |
| 88 |
int count; |
| 89 |
QString name; |
| 90 |
//! [0-0] |
| 91 |
MyMember member1; |
| 92 |
MyMember member2; |
| 93 |
MyMember member3; |
| 94 |
MyMember member4; |
| 95 |
//! [0-1] |
| 96 |
// ... |
| 97 |
}; |
| 98 |
Q_DECLARE_METATYPE(MyStructure) |
| 99 |
|
| 100 |
// Marshall the MyStructure data into a D-Bus argument |
| 101 |
QDBusArgument &operator<<(QDBusArgument &argument, const MyStructure &myStruct) |
| 102 |
{ |
| 103 |
argument.beginStructure(); |
| 104 |
argument << myStruct.count << myStruct.name; |
| 105 |
argument.endStructure(); |
| 106 |
return argument; |
| 107 |
} |
| 108 |
|
| 109 |
// Retrieve the MyStructure data from the D-Bus argument |
| 110 |
const QDBusArgument &operator>>(const QDBusArgument &argument, MyStructure &myStruct) |
| 111 |
{ |
| 112 |
argument.beginStructure(); |
| 113 |
argument >> myStruct.count >> myStruct.name; |
| 114 |
argument.endStructure(); |
| 115 |
return argument; |
| 116 |
} |
| 117 |
//! [0-1] |
| 118 |
|
| 119 |
const QDBusArgument &operator<<(const QDBusArgument &argument, const MyMember &/*member*/) |
| 120 |
{ |
| 121 |
return argument; |
| 122 |
} |
| 123 |
|
| 124 |
const QDBusArgument &operator>>(const QDBusArgument &argument, const MyMember &/*member*/) |
| 125 |
{ |
| 126 |
return argument; |
| 127 |
} |
| 128 |
|
| 129 |
void registerMyStructure() |
| 130 |
{ |
| 131 |
//! [1] |
| 132 |
qDBusRegisterMetaType<MyStructure>(); |
| 133 |
//! [1] |
| 134 |
} |
| 135 |
|
| 136 |
void castType() |
| 137 |
{ |
| 138 |
QVariant argument = MyObject::callArgs.at(0); |
| 139 |
QDBusVariant dv = qdbus_cast<QDBusVariant>(argument); |
| 140 |
//! [2] |
| 141 |
MyType item = qdbus_cast<Type>(argument); |
| 142 |
//! [2] |
| 143 |
} |
| 144 |
|
| 145 |
void argumentItem() |
| 146 |
{ |
| 147 |
//! [3] |
| 148 |
MyType item; |
| 149 |
argument >> item; |
| 150 |
//! [3] |
| 151 |
} |
| 152 |
namespace QDBusSnippets |
| 153 |
{ |
| 154 |
//! [4] |
| 155 |
QDBusArgument &operator<<(QDBusArgument &argument, const MyStructure &myStruct) |
| 156 |
{ |
| 157 |
argument.beginStructure(); |
| 158 |
argument << myStruct.member1 << myStruct.member2; |
| 159 |
argument.endStructure(); |
| 160 |
return argument; |
| 161 |
} |
| 162 |
//! [4] |
| 163 |
|
| 164 |
namespace Alt { |
| 165 |
//! [5] |
| 166 |
QDBusArgument &operator<<(QDBusArgument &argument, const MyStructure &myStruct) |
| 167 |
{ |
| 168 |
argument.beginStructure(); |
| 169 |
argument << myStruct.member1 << myStruct.member2; |
| 170 |
|
| 171 |
argument.beginStructure(); |
| 172 |
argument << myStruct.member3.subMember1 << myStruct.member3.subMember2; |
| 173 |
argument.endStructure(); |
| 174 |
|
| 175 |
argument << myStruct.member4; |
| 176 |
argument.endStructure(); |
| 177 |
return argument; |
| 178 |
} |
| 179 |
//! [5] |
| 180 |
} // namespace |
| 181 |
|
| 182 |
//! [6] |
| 183 |
// Append an array of MyElement types |
| 184 |
QDBusArgument &operator<<(QDBusArgument &argument, const MyArray &myArray) |
| 185 |
{ |
| 186 |
argument.beginArray(qMetaTypeId<MyElement>()); |
| 187 |
for (const auto &element : myArray) |
| 188 |
argument << element; |
| 189 |
argument.endArray(); |
| 190 |
return argument; |
| 191 |
} |
| 192 |
//! [6] |
| 193 |
|
| 194 |
//! [7] |
| 195 |
// Append a dictionary that associates ints to MyValue types |
| 196 |
QDBusArgument &operator<<(QDBusArgument &argument, const MyDictionary &myDict) |
| 197 |
{ |
| 198 |
argument.beginMap(QMetaType::fromType<int>(), QMetaType::fromType<MyValue>()); |
| 199 |
MyDictionary::const_iterator i; |
| 200 |
for (i = myDict.cbegin(); i != myDict.cend(); ++i) { |
| 201 |
argument.beginMapEntry(); |
| 202 |
argument << i.key() << i.value(); |
| 203 |
argument.endMapEntry(); |
| 204 |
} |
| 205 |
argument.endMap(); |
| 206 |
return argument; |
| 207 |
} |
| 208 |
//! [7] |
| 209 |
|
| 210 |
//! [8] |
| 211 |
const QDBusArgument &operator>>(const QDBusArgument &argument, MyStructure &myStruct) |
| 212 |
{ |
| 213 |
argument.beginStructure(); |
| 214 |
argument >> myStruct.member1 >> myStruct.member2 >> myStruct.member3; |
| 215 |
argument.endStructure(); |
| 216 |
return argument; |
| 217 |
} |
| 218 |
//! [8] |
| 219 |
|
| 220 |
//! [9] |
| 221 |
// Extract a MyArray array of MyElement elements |
| 222 |
const QDBusArgument &operator>>(const QDBusArgument &argument, MyArray &myArray) |
| 223 |
{ |
| 224 |
argument.beginArray(); |
| 225 |
myArray.clear(); |
| 226 |
|
| 227 |
while (!argument.atEnd()) { |
| 228 |
MyElement element; |
| 229 |
argument >> element; |
| 230 |
myArray.append(element); |
| 231 |
} |
| 232 |
|
| 233 |
argument.endArray(); |
| 234 |
return argument; |
| 235 |
} |
| 236 |
//! [9] |
| 237 |
|
| 238 |
//! [10] |
| 239 |
// Extract a MyDictionary map that associates integers to MyElement items |
| 240 |
const QDBusArgument &operator>>(const QDBusArgument &argument, MyDictionary &myDict) |
| 241 |
{ |
| 242 |
argument.beginMap(); |
| 243 |
myDict.clear(); |
| 244 |
|
| 245 |
while (!argument.atEnd()) { |
| 246 |
int key; |
| 247 |
MyElement value; |
| 248 |
argument.beginMapEntry(); |
| 249 |
argument >> key >> value; |
| 250 |
argument.endMapEntry(); |
| 251 |
myDict.insert(key, value); |
| 252 |
} |
| 253 |
|
| 254 |
argument.endMap(); |
| 255 |
return argument; |
| 256 |
} |
| 257 |
//! [10] |
| 258 |
} |