| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the QtDBus module of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 16 | ** GNU Lesser General Public License Usage | 
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional | 
|---|
| 25 | ** rights.  These rights are described in the Nokia Qt LGPL Exception | 
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include <string.h> | 
|---|
| 43 |  | 
|---|
| 44 | #include <QtCore/qcoreapplication.h> | 
|---|
| 45 | #include <QtCore/qvariant.h> | 
|---|
| 46 | #include <QtCore/qmetaobject.h> | 
|---|
| 47 |  | 
|---|
| 48 | #include "qdbusutil_p.h" | 
|---|
| 49 | #include "qdbusconnection_p.h" | 
|---|
| 50 | #include "qdbusmetatype_p.h" | 
|---|
| 51 | #include "qdbusabstractadaptor_p.h" // for QCLASSINFO_DBUS_* | 
|---|
| 52 |  | 
|---|
| 53 | #ifndef QT_NO_DBUS | 
|---|
| 54 |  | 
|---|
| 55 | QT_BEGIN_NAMESPACE | 
|---|
| 56 |  | 
|---|
| 57 | bool qDBusCheckAsyncTag(const char *tag) | 
|---|
| 58 | { | 
|---|
| 59 | static const char noReplyTag[] = "Q_NOREPLY"; | 
|---|
| 60 | if (!tag || !*tag) | 
|---|
| 61 | return false; | 
|---|
| 62 |  | 
|---|
| 63 | const char *p = strstr(tag, noReplyTag); | 
|---|
| 64 | if (p != NULL && | 
|---|
| 65 | (p == tag || *(p-1) == ' ') && | 
|---|
| 66 | (p[sizeof noReplyTag - 1] == '\0' || p[sizeof noReplyTag - 1] == ' ')) | 
|---|
| 67 | return true; | 
|---|
| 68 |  | 
|---|
| 69 | return false; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | int qDBusNameToTypeId(const char *name) | 
|---|
| 73 | { | 
|---|
| 74 | int id = static_cast<int>( QVariant::nameToType(name) ); | 
|---|
| 75 | if (id == QVariant::UserType) | 
|---|
| 76 | id = QMetaType::type(name); | 
|---|
| 77 | return id; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | QString qDBusInterfaceFromMetaObject(const QMetaObject *mo) | 
|---|
| 81 | { | 
|---|
| 82 | QString interface; | 
|---|
| 83 |  | 
|---|
| 84 | int idx = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTERFACE); | 
|---|
| 85 | if (idx >= mo->classInfoOffset()) { | 
|---|
| 86 | interface = QLatin1String(mo->classInfo(idx).value()); | 
|---|
| 87 | } else { | 
|---|
| 88 | interface = QLatin1String(mo->className()); | 
|---|
| 89 | interface.replace(QLatin1String("::"), QLatin1String(".")); | 
|---|
| 90 |  | 
|---|
| 91 | if (interface.startsWith(QLatin1String("QDBus"))) { | 
|---|
| 92 | interface.prepend(QLatin1String("com.trolltech.QtDBus.")); | 
|---|
| 93 | } else if (interface.startsWith(QLatin1Char('Q')) && | 
|---|
| 94 | interface.length() >= 2 && interface.at(1).isUpper()) { | 
|---|
| 95 | // assume it's Qt | 
|---|
| 96 | interface.prepend(QLatin1String("com.trolltech.Qt.")); | 
|---|
| 97 | } else if (!QCoreApplication::instance()|| | 
|---|
| 98 | QCoreApplication::instance()->applicationName().isEmpty()) { | 
|---|
| 99 | interface.prepend(QLatin1String("local.")); | 
|---|
| 100 | } else { | 
|---|
| 101 | interface.prepend(QLatin1Char('.')).prepend(QCoreApplication::instance()->applicationName()); | 
|---|
| 102 | QStringList domainName = | 
|---|
| 103 | QCoreApplication::instance()->organizationDomain().split(QLatin1Char('.'), | 
|---|
| 104 | QString::SkipEmptyParts); | 
|---|
| 105 | if (domainName.isEmpty()) | 
|---|
| 106 | interface.prepend(QLatin1String("local.")); | 
|---|
| 107 | else | 
|---|
| 108 | for (int i = 0; i < domainName.count(); ++i) | 
|---|
| 109 | interface.prepend(QLatin1Char('.')).prepend(domainName.at(i)); | 
|---|
| 110 | } | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | return interface; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | bool qDBusInterfaceInObject(QObject *obj, const QString &interface_name) | 
|---|
| 117 | { | 
|---|
| 118 | const QMetaObject *mo = obj->metaObject(); | 
|---|
| 119 | for ( ; mo != &QObject::staticMetaObject; mo = mo->superClass()) | 
|---|
| 120 | if (interface_name == qDBusInterfaceFromMetaObject(mo)) | 
|---|
| 121 | return true; | 
|---|
| 122 | return false; | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | // calculates the metatypes for the method | 
|---|
| 126 | // the slot must have the parameters in the following form: | 
|---|
| 127 | //  - zero or more value or const-ref parameters of any kind | 
|---|
| 128 | //  - zero or one const ref of QDBusMessage | 
|---|
| 129 | //  - zero or more non-const ref parameters | 
|---|
| 130 | // No parameter may be a template. | 
|---|
| 131 | // this function returns -1 if the parameters don't match the above form | 
|---|
| 132 | // this function returns the number of *input* parameters, including the QDBusMessage one if any | 
|---|
| 133 | // this function does not check the return type, so metaTypes[0] is always 0 and always present | 
|---|
| 134 | // metaTypes.count() >= retval + 1 in all cases | 
|---|
| 135 | // | 
|---|
| 136 | // sig must be the normalised signature for the method | 
|---|
| 137 | int qDBusParametersForMethod(const QMetaMethod &mm, QList<int>& metaTypes) | 
|---|
| 138 | { | 
|---|
| 139 | QDBusMetaTypeId::init(); | 
|---|
| 140 |  | 
|---|
| 141 | QList<QByteArray> parameterTypes = mm.parameterTypes(); | 
|---|
| 142 | metaTypes.clear(); | 
|---|
| 143 |  | 
|---|
| 144 | metaTypes.append(0);        // return type | 
|---|
| 145 | int inputCount = 0; | 
|---|
| 146 | bool seenMessage = false; | 
|---|
| 147 | QList<QByteArray>::ConstIterator it = parameterTypes.constBegin(); | 
|---|
| 148 | QList<QByteArray>::ConstIterator end = parameterTypes.constEnd(); | 
|---|
| 149 | for ( ; it != end; ++it) { | 
|---|
| 150 | const QByteArray &type = *it; | 
|---|
| 151 | if (type.endsWith('*')) { | 
|---|
| 152 | //qWarning("Could not parse the method '%s'", mm.signature()); | 
|---|
| 153 | // pointer? | 
|---|
| 154 | return -1; | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | if (type.endsWith('&')) { | 
|---|
| 158 | QByteArray basictype = type; | 
|---|
| 159 | basictype.truncate(type.length() - 1); | 
|---|
| 160 |  | 
|---|
| 161 | int id = qDBusNameToTypeId(basictype); | 
|---|
| 162 | if (id == 0) { | 
|---|
| 163 | //qWarning("Could not parse the method '%s'", mm.signature()); | 
|---|
| 164 | // invalid type in method parameter list | 
|---|
| 165 | return -1; | 
|---|
| 166 | } else if (QDBusMetaType::typeToSignature(id) == 0) | 
|---|
| 167 | return -1; | 
|---|
| 168 |  | 
|---|
| 169 | metaTypes.append( id ); | 
|---|
| 170 | seenMessage = true; // it cannot appear anymore anyways | 
|---|
| 171 | continue; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | if (seenMessage) {      // && !type.endsWith('&') | 
|---|
| 175 | //qWarning("Could not parse the method '%s'", mm.signature()); | 
|---|
| 176 | // non-output parameters after message or after output params | 
|---|
| 177 | return -1;          // not allowed | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | int id = qDBusNameToTypeId(type); | 
|---|
| 181 | if (id == 0) { | 
|---|
| 182 | //qWarning("Could not parse the method '%s'", mm.signature()); | 
|---|
| 183 | // invalid type in method parameter list | 
|---|
| 184 | return -1; | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | if (id == QDBusMetaTypeId::message) | 
|---|
| 188 | seenMessage = true; | 
|---|
| 189 | else if (QDBusMetaType::typeToSignature(id) == 0) | 
|---|
| 190 | return -1; | 
|---|
| 191 |  | 
|---|
| 192 | metaTypes.append(id); | 
|---|
| 193 | ++inputCount; | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | return inputCount; | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 199 | QT_END_NAMESPACE | 
|---|
| 200 |  | 
|---|
| 201 | #endif // QT_NO_DBUS | 
|---|