| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the QtDBus module of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include <QtCore/qcoreapplication.h> | 
|---|
| 43 | #include <QtCore/qmetaobject.h> | 
|---|
| 44 | #include <QtCore/qstringlist.h> | 
|---|
| 45 |  | 
|---|
| 46 | #include "qdbusinterface_p.h"   // for ANNOTATION_NO_WAIT | 
|---|
| 47 | #include "qdbusabstractadaptor_p.h" // for QCLASSINFO_DBUS_* | 
|---|
| 48 | #include "qdbusconnection_p.h"  // for the flags | 
|---|
| 49 | #include "qdbusmetatype_p.h" | 
|---|
| 50 | #include "qdbusmetatype.h" | 
|---|
| 51 | #include "qdbusutil_p.h" | 
|---|
| 52 |  | 
|---|
| 53 | QT_BEGIN_NAMESPACE | 
|---|
| 54 |  | 
|---|
| 55 | extern QDBUS_EXPORT QString qDBusGenerateMetaObjectXml(QString interface, const QMetaObject *mo, | 
|---|
| 56 | const QMetaObject *base, int flags); | 
|---|
| 57 |  | 
|---|
| 58 | static inline QString typeNameToXml(const char *typeName) | 
|---|
| 59 | { | 
|---|
| 60 | // ### copied from qtextdocument.cpp | 
|---|
| 61 | // ### move this into QtCore at some point | 
|---|
| 62 | QString plain = QLatin1String(typeName); | 
|---|
| 63 | QString rich; | 
|---|
| 64 | rich.reserve(int(plain.length() * 1.1)); | 
|---|
| 65 | for (int i = 0; i < plain.length(); ++i) { | 
|---|
| 66 | if (plain.at(i) == QLatin1Char('<')) | 
|---|
| 67 | rich += QLatin1String("<"); | 
|---|
| 68 | else if (plain.at(i) == QLatin1Char('>')) | 
|---|
| 69 | rich += QLatin1String(">"); | 
|---|
| 70 | else if (plain.at(i) == QLatin1Char('&')) | 
|---|
| 71 | rich += QLatin1String("&"); | 
|---|
| 72 | else | 
|---|
| 73 | rich += plain.at(i); | 
|---|
| 74 | } | 
|---|
| 75 | return rich; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | // implement the D-Bus org.freedesktop.DBus.Introspectable interface | 
|---|
| 79 | // we do that by analysing the metaObject of all the adaptor interfaces | 
|---|
| 80 |  | 
|---|
| 81 | static QString generateInterfaceXml(const QMetaObject *mo, int flags, int methodOffset, int propOffset) | 
|---|
| 82 | { | 
|---|
| 83 | QString retval; | 
|---|
| 84 |  | 
|---|
| 85 | // start with properties: | 
|---|
| 86 | if (flags & (QDBusConnection::ExportScriptableProperties | | 
|---|
| 87 | QDBusConnection::ExportNonScriptableProperties)) { | 
|---|
| 88 | for (int i = propOffset; i < mo->propertyCount(); ++i) { | 
|---|
| 89 | static const char *accessvalues[] = {0, "read", "write", "readwrite"}; | 
|---|
| 90 |  | 
|---|
| 91 | QMetaProperty mp = mo->property(i); | 
|---|
| 92 |  | 
|---|
| 93 | if (!((mp.isScriptable() && (flags & QDBusConnection::ExportScriptableProperties)) || | 
|---|
| 94 | (!mp.isScriptable() && (flags & QDBusConnection::ExportNonScriptableProperties)))) | 
|---|
| 95 | continue; | 
|---|
| 96 |  | 
|---|
| 97 | int access = 0; | 
|---|
| 98 | if (mp.isReadable()) | 
|---|
| 99 | access |= 1; | 
|---|
| 100 | if (mp.isWritable()) | 
|---|
| 101 | access |= 2; | 
|---|
| 102 |  | 
|---|
| 103 | int typeId = qDBusNameToTypeId(mp.typeName()); | 
|---|
| 104 | if (!typeId) | 
|---|
| 105 | continue; | 
|---|
| 106 | const char *signature = QDBusMetaType::typeToSignature(typeId); | 
|---|
| 107 | if (!signature) | 
|---|
| 108 | continue; | 
|---|
| 109 |  | 
|---|
| 110 | retval += QString::fromLatin1("    <property name=\"%1\" type=\"%2\" access=\"%3\"") | 
|---|
| 111 | .arg(QLatin1String(mp.name())) | 
|---|
| 112 | .arg(QLatin1String(signature)) | 
|---|
| 113 | .arg(QLatin1String(accessvalues[access])); | 
|---|
| 114 |  | 
|---|
| 115 | if (QDBusMetaType::signatureToType(signature) == QVariant::Invalid) { | 
|---|
| 116 | const char *typeName = QVariant::typeToName(QVariant::Type(typeId)); | 
|---|
| 117 | retval += QString::fromLatin1(">\n      <annotation name=\"com.trolltech.QtDBus.QtTypeName\" value=\"%3\"/>\n    </property>\n") | 
|---|
| 118 | .arg(typeNameToXml(typeName)); | 
|---|
| 119 | } else { | 
|---|
| 120 | retval += QLatin1String("/>\n"); | 
|---|
| 121 | } | 
|---|
| 122 | } | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | // now add methods: | 
|---|
| 126 | for (int i = methodOffset; i < mo->methodCount(); ++i) { | 
|---|
| 127 | QMetaMethod mm = mo->method(i); | 
|---|
| 128 | QByteArray signature = mm.signature(); | 
|---|
| 129 | int paren = signature.indexOf('('); | 
|---|
| 130 |  | 
|---|
| 131 | bool isSignal; | 
|---|
| 132 | if (mm.methodType() == QMetaMethod::Signal) | 
|---|
| 133 | // adding a signal | 
|---|
| 134 | isSignal = true; | 
|---|
| 135 | else if (mm.methodType() == QMetaMethod::Slot && mm.access() == QMetaMethod::Public) | 
|---|
| 136 | isSignal = false; | 
|---|
| 137 | else | 
|---|
| 138 | continue;           // neither signal nor public slot | 
|---|
| 139 |  | 
|---|
| 140 | if (isSignal && !(flags & (QDBusConnection::ExportScriptableSignals | | 
|---|
| 141 | QDBusConnection::ExportNonScriptableSignals))) | 
|---|
| 142 | continue;           // we're not exporting any signals | 
|---|
| 143 | if (!isSignal && !(flags & (QDBusConnection::ExportScriptableSlots | | 
|---|
| 144 | QDBusConnection::ExportNonScriptableSlots))) | 
|---|
| 145 | continue;           // we're not exporting any slots | 
|---|
| 146 |  | 
|---|
| 147 | QString xml = QString::fromLatin1("    <%1 name=\"%2\">\n") | 
|---|
| 148 | .arg(isSignal ? QLatin1String("signal") : QLatin1String("method")) | 
|---|
| 149 | .arg(QLatin1String(signature.left(paren))); | 
|---|
| 150 |  | 
|---|
| 151 | // check the return type first | 
|---|
| 152 | int typeId = qDBusNameToTypeId(mm.typeName()); | 
|---|
| 153 | if (typeId) { | 
|---|
| 154 | const char *typeName = QDBusMetaType::typeToSignature(typeId); | 
|---|
| 155 | if (typeName) { | 
|---|
| 156 | xml += QString::fromLatin1("      <arg type=\"%1\" direction=\"out\"/>\n") | 
|---|
| 157 | .arg(typeNameToXml(typeName)); | 
|---|
| 158 |  | 
|---|
| 159 | // do we need to describe this argument? | 
|---|
| 160 | if (QDBusMetaType::signatureToType(typeName) == QVariant::Invalid) | 
|---|
| 161 | xml += QString::fromLatin1("      <annotation name=\"com.trolltech.QtDBus.QtTypeName.Out0\" value=\"%1\"/>\n") | 
|---|
| 162 | .arg(typeNameToXml(mm.typeName())); | 
|---|
| 163 | } else | 
|---|
| 164 | continue; | 
|---|
| 165 | } | 
|---|
| 166 | else if (*mm.typeName()) | 
|---|
| 167 | continue;           // wasn't a valid type | 
|---|
| 168 |  | 
|---|
| 169 | QList<QByteArray> names = mm.parameterNames(); | 
|---|
| 170 | QList<int> types; | 
|---|
| 171 | int inputCount = qDBusParametersForMethod(mm, types); | 
|---|
| 172 | if (inputCount == -1) | 
|---|
| 173 | continue;           // invalid form | 
|---|
| 174 | if (isSignal && inputCount + 1 != types.count()) | 
|---|
| 175 | continue;           // signal with output arguments? | 
|---|
| 176 | if (isSignal && types.at(inputCount) == QDBusMetaTypeId::message) | 
|---|
| 177 | continue;           // signal with QDBusMessage argument? | 
|---|
| 178 | if (isSignal && mm.attributes() & QMetaMethod::Cloned) | 
|---|
| 179 | continue;           // cloned signal? | 
|---|
| 180 |  | 
|---|
| 181 | int j; | 
|---|
| 182 | bool isScriptable = mm.attributes() & QMetaMethod::Scriptable; | 
|---|
| 183 | for (j = 1; j < types.count(); ++j) { | 
|---|
| 184 | // input parameter for a slot or output for a signal | 
|---|
| 185 | if (types.at(j) == QDBusMetaTypeId::message) { | 
|---|
| 186 | isScriptable = true; | 
|---|
| 187 | continue; | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | QString name; | 
|---|
| 191 | if (!names.at(j - 1).isEmpty()) | 
|---|
| 192 | name = QString::fromLatin1("name=\"%1\" ").arg(QLatin1String(names.at(j - 1))); | 
|---|
| 193 |  | 
|---|
| 194 | bool isOutput = isSignal || j > inputCount; | 
|---|
| 195 |  | 
|---|
| 196 | const char *signature = QDBusMetaType::typeToSignature(types.at(j)); | 
|---|
| 197 | xml += QString::fromLatin1("      <arg %1type=\"%2\" direction=\"%3\"/>\n") | 
|---|
| 198 | .arg(name) | 
|---|
| 199 | .arg(QLatin1String(signature)) | 
|---|
| 200 | .arg(isOutput ? QLatin1String("out") : QLatin1String("in")); | 
|---|
| 201 |  | 
|---|
| 202 | // do we need to describe this argument? | 
|---|
| 203 | if (QDBusMetaType::signatureToType(signature) == QVariant::Invalid) { | 
|---|
| 204 | const char *typeName = QVariant::typeToName( QVariant::Type(types.at(j)) ); | 
|---|
| 205 | xml += QString::fromLatin1("      <annotation name=\"com.trolltech.QtDBus.QtTypeName.%1%2\" value=\"%3\"/>\n") | 
|---|
| 206 | .arg(isOutput ? QLatin1String("Out") : QLatin1String("In")) | 
|---|
| 207 | .arg(isOutput ? j - inputCount : j - 1) | 
|---|
| 208 | .arg(typeNameToXml(typeName)); | 
|---|
| 209 | } | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | int wantedMask; | 
|---|
| 213 | if (isScriptable) | 
|---|
| 214 | wantedMask = isSignal ? QDBusConnection::ExportScriptableSignals | 
|---|
| 215 | : QDBusConnection::ExportScriptableSlots; | 
|---|
| 216 | else | 
|---|
| 217 | wantedMask = isSignal ? QDBusConnection::ExportNonScriptableSignals | 
|---|
| 218 | : QDBusConnection::ExportNonScriptableSlots; | 
|---|
| 219 | if ((flags & wantedMask) != wantedMask) | 
|---|
| 220 | continue; | 
|---|
| 221 |  | 
|---|
| 222 | if (qDBusCheckAsyncTag(mm.tag())) | 
|---|
| 223 | // add the no-reply annotation | 
|---|
| 224 | xml += QLatin1String("      <annotation name=\"" ANNOTATION_NO_WAIT "\"" | 
|---|
| 225 | " value=\"true\"/>\n"); | 
|---|
| 226 |  | 
|---|
| 227 | retval += xml; | 
|---|
| 228 | retval += QString::fromLatin1("    </%1>\n") | 
|---|
| 229 | .arg(isSignal ? QLatin1String("signal") : QLatin1String("method")); | 
|---|
| 230 | } | 
|---|
| 231 |  | 
|---|
| 232 | return retval; | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 | QString qDBusInterfaceFromMetaObject(const QMetaObject *mo) | 
|---|
| 236 | { | 
|---|
| 237 | QString interface; | 
|---|
| 238 |  | 
|---|
| 239 | int idx = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTERFACE); | 
|---|
| 240 | if (idx >= mo->classInfoOffset()) { | 
|---|
| 241 | interface = QLatin1String(mo->classInfo(idx).value()); | 
|---|
| 242 | } else { | 
|---|
| 243 | interface = QLatin1String(mo->className()); | 
|---|
| 244 | interface.replace(QLatin1String("::"), QLatin1String(".")); | 
|---|
| 245 |  | 
|---|
| 246 | if (interface.startsWith(QLatin1String("QDBus"))) { | 
|---|
| 247 | interface.prepend(QLatin1String("com.trolltech.QtDBus.")); | 
|---|
| 248 | } else if (interface.startsWith(QLatin1Char('Q')) && | 
|---|
| 249 | interface.length() >= 2 && interface.at(1).isUpper()) { | 
|---|
| 250 | // assume it's Qt | 
|---|
| 251 | interface.prepend(QLatin1String("com.trolltech.Qt.")); | 
|---|
| 252 | } else if (!QCoreApplication::instance()|| | 
|---|
| 253 | QCoreApplication::instance()->applicationName().isEmpty()) { | 
|---|
| 254 | interface.prepend(QLatin1String("local.")); | 
|---|
| 255 | } else { | 
|---|
| 256 | interface.prepend(QLatin1Char('.')).prepend(QCoreApplication::instance()->applicationName()); | 
|---|
| 257 | QStringList domainName = | 
|---|
| 258 | QCoreApplication::instance()->organizationDomain().split(QLatin1Char('.'), | 
|---|
| 259 | QString::SkipEmptyParts); | 
|---|
| 260 | if (domainName.isEmpty()) | 
|---|
| 261 | interface.prepend(QLatin1String("local.")); | 
|---|
| 262 | else | 
|---|
| 263 | for (int i = 0; i < domainName.count(); ++i) | 
|---|
| 264 | interface.prepend(QLatin1Char('.')).prepend(domainName.at(i)); | 
|---|
| 265 | } | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | return interface; | 
|---|
| 269 | } | 
|---|
| 270 |  | 
|---|
| 271 | QString qDBusGenerateMetaObjectXml(QString interface, const QMetaObject *mo, | 
|---|
| 272 | const QMetaObject *base, int flags) | 
|---|
| 273 | { | 
|---|
| 274 | if (interface.isEmpty()) | 
|---|
| 275 | // generate the interface name from the meta object | 
|---|
| 276 | interface = qDBusInterfaceFromMetaObject(mo); | 
|---|
| 277 |  | 
|---|
| 278 | QString xml; | 
|---|
| 279 | int idx = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTROSPECTION); | 
|---|
| 280 | if (idx >= mo->classInfoOffset()) | 
|---|
| 281 | return QString::fromUtf8(mo->classInfo(idx).value()); | 
|---|
| 282 | else | 
|---|
| 283 | xml = generateInterfaceXml(mo, flags, base->methodCount(), base->propertyCount()); | 
|---|
| 284 |  | 
|---|
| 285 | if (xml.isEmpty()) | 
|---|
| 286 | return QString();       // don't add an empty interface | 
|---|
| 287 | return QString::fromLatin1("  <interface name=\"%1\">\n%2  </interface>\n") | 
|---|
| 288 | .arg(interface, xml); | 
|---|
| 289 | } | 
|---|
| 290 | #if 0 | 
|---|
| 291 | QString qDBusGenerateMetaObjectXml(QString interface, const QMetaObject *mo, const QMetaObject *base, | 
|---|
| 292 | int flags) | 
|---|
| 293 | { | 
|---|
| 294 | if (interface.isEmpty()) { | 
|---|
| 295 | // generate the interface name from the meta object | 
|---|
| 296 | int idx = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTERFACE); | 
|---|
| 297 | if (idx >= mo->classInfoOffset()) { | 
|---|
| 298 | interface = QLatin1String(mo->classInfo(idx).value()); | 
|---|
| 299 | } else { | 
|---|
| 300 | interface = QLatin1String(mo->className()); | 
|---|
| 301 | interface.replace(QLatin1String("::"), QLatin1String(".")); | 
|---|
| 302 |  | 
|---|
| 303 | if (interface.startsWith(QLatin1String("QDBus"))) { | 
|---|
| 304 | interface.prepend(QLatin1String("com.trolltech.QtDBus.")); | 
|---|
| 305 | } else if (interface.startsWith(QLatin1Char('Q')) && | 
|---|
| 306 | interface.length() >= 2 && interface.at(1).isUpper()) { | 
|---|
| 307 | // assume it's Qt | 
|---|
| 308 | interface.prepend(QLatin1String("com.trolltech.Qt.")); | 
|---|
| 309 | } else if (!QCoreApplication::instance()|| | 
|---|
| 310 | QCoreApplication::instance()->applicationName().isEmpty()) { | 
|---|
| 311 | interface.prepend(QLatin1String("local.")); | 
|---|
| 312 | } else { | 
|---|
| 313 | interface.prepend(QLatin1Char('.')).prepend(QCoreApplication::instance()->applicationName()); | 
|---|
| 314 | QStringList domainName = | 
|---|
| 315 | QCoreApplication::instance()->organizationDomain().split(QLatin1Char('.'), | 
|---|
| 316 | QString::SkipEmptyParts); | 
|---|
| 317 | if (domainName.isEmpty()) | 
|---|
| 318 | interface.prepend(QLatin1String("local.")); | 
|---|
| 319 | else | 
|---|
| 320 | for (int i = 0; i < domainName.count(); ++i) | 
|---|
| 321 | interface.prepend(QLatin1Char('.')).prepend(domainName.at(i)); | 
|---|
| 322 | } | 
|---|
| 323 | } | 
|---|
| 324 | } | 
|---|
| 325 |  | 
|---|
| 326 | QString xml; | 
|---|
| 327 | int idx = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTROSPECTION); | 
|---|
| 328 | if (idx >= mo->classInfoOffset()) | 
|---|
| 329 | return QString::fromUtf8(mo->classInfo(idx).value()); | 
|---|
| 330 | else | 
|---|
| 331 | xml = generateInterfaceXml(mo, flags, base->methodCount(), base->propertyCount()); | 
|---|
| 332 |  | 
|---|
| 333 | if (xml.isEmpty()) | 
|---|
| 334 | return QString();       // don't add an empty interface | 
|---|
| 335 | return QString::fromLatin1("  <interface name=\"%1\">\n%2  </interface>\n") | 
|---|
| 336 | .arg(interface, xml); | 
|---|
| 337 | } | 
|---|
| 338 |  | 
|---|
| 339 | #endif | 
|---|
| 340 |  | 
|---|
| 341 | QT_END_NAMESPACE | 
|---|