| 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 <string.h>
|
|---|
| 43 |
|
|---|
| 44 | #include <QtCore/qvariant.h>
|
|---|
| 45 | #include <QtCore/qmetaobject.h>
|
|---|
| 46 |
|
|---|
| 47 | #include "qdbusutil_p.h"
|
|---|
| 48 | #include "qdbusconnection_p.h"
|
|---|
| 49 | #include "qdbusmetatype_p.h"
|
|---|
| 50 |
|
|---|
| 51 | QT_BEGIN_NAMESPACE
|
|---|
| 52 |
|
|---|
| 53 | bool qDBusCheckAsyncTag(const char *tag)
|
|---|
| 54 | {
|
|---|
| 55 | static const char noReplyTag[] = "Q_NOREPLY";
|
|---|
| 56 | if (!tag || !*tag)
|
|---|
| 57 | return false;
|
|---|
| 58 |
|
|---|
| 59 | const char *p = strstr(tag, noReplyTag);
|
|---|
| 60 | if (p != NULL &&
|
|---|
| 61 | (p == tag || *(p-1) == ' ') &&
|
|---|
| 62 | (p[sizeof noReplyTag - 1] == '\0' || p[sizeof noReplyTag - 1] == ' '))
|
|---|
| 63 | return true;
|
|---|
| 64 |
|
|---|
| 65 | return false;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | int qDBusNameToTypeId(const char *name)
|
|---|
| 69 | {
|
|---|
| 70 | int id = static_cast<int>( QVariant::nameToType(name) );
|
|---|
| 71 | if (id == QVariant::UserType)
|
|---|
| 72 | id = QMetaType::type(name);
|
|---|
| 73 | return id;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | // calculates the metatypes for the method
|
|---|
| 77 | // the slot must have the parameters in the following form:
|
|---|
| 78 | // - zero or more value or const-ref parameters of any kind
|
|---|
| 79 | // - zero or one const ref of QDBusMessage
|
|---|
| 80 | // - zero or more non-const ref parameters
|
|---|
| 81 | // No parameter may be a template.
|
|---|
| 82 | // this function returns -1 if the parameters don't match the above form
|
|---|
| 83 | // this function returns the number of *input* parameters, including the QDBusMessage one if any
|
|---|
| 84 | // this function does not check the return type, so metaTypes[0] is always 0 and always present
|
|---|
| 85 | // metaTypes.count() >= retval + 1 in all cases
|
|---|
| 86 | //
|
|---|
| 87 | // sig must be the normalised signature for the method
|
|---|
| 88 | int qDBusParametersForMethod(const QMetaMethod &mm, QList<int>& metaTypes)
|
|---|
| 89 | {
|
|---|
| 90 | QDBusMetaTypeId::init();
|
|---|
| 91 |
|
|---|
| 92 | QList<QByteArray> parameterTypes = mm.parameterTypes();
|
|---|
| 93 | metaTypes.clear();
|
|---|
| 94 |
|
|---|
| 95 | metaTypes.append(0); // return type
|
|---|
| 96 | int inputCount = 0;
|
|---|
| 97 | bool seenMessage = false;
|
|---|
| 98 | QList<QByteArray>::ConstIterator it = parameterTypes.constBegin();
|
|---|
| 99 | QList<QByteArray>::ConstIterator end = parameterTypes.constEnd();
|
|---|
| 100 | for ( ; it != end; ++it) {
|
|---|
| 101 | const QByteArray &type = *it;
|
|---|
| 102 | if (type.endsWith('*')) {
|
|---|
| 103 | //qWarning("Could not parse the method '%s'", mm.signature());
|
|---|
| 104 | // pointer?
|
|---|
| 105 | return -1;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | if (type.endsWith('&')) {
|
|---|
| 109 | QByteArray basictype = type;
|
|---|
| 110 | basictype.truncate(type.length() - 1);
|
|---|
| 111 |
|
|---|
| 112 | int id = qDBusNameToTypeId(basictype);
|
|---|
| 113 | if (id == 0) {
|
|---|
| 114 | //qWarning("Could not parse the method '%s'", mm.signature());
|
|---|
| 115 | // invalid type in method parameter list
|
|---|
| 116 | return -1;
|
|---|
| 117 | } else if (QDBusMetaType::typeToSignature(id) == 0)
|
|---|
| 118 | return -1;
|
|---|
| 119 |
|
|---|
| 120 | metaTypes.append( id );
|
|---|
| 121 | seenMessage = true; // it cannot appear anymore anyways
|
|---|
| 122 | continue;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | if (seenMessage) { // && !type.endsWith('&')
|
|---|
| 126 | //qWarning("Could not parse the method '%s'", mm.signature());
|
|---|
| 127 | // non-output parameters after message or after output params
|
|---|
| 128 | return -1; // not allowed
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | int id = qDBusNameToTypeId(type);
|
|---|
| 132 | if (id == 0) {
|
|---|
| 133 | //qWarning("Could not parse the method '%s'", mm.signature());
|
|---|
| 134 | // invalid type in method parameter list
|
|---|
| 135 | return -1;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | if (id == QDBusMetaTypeId::message)
|
|---|
| 139 | seenMessage = true;
|
|---|
| 140 | else if (QDBusMetaType::typeToSignature(id) == 0)
|
|---|
| 141 | return -1;
|
|---|
| 142 |
|
|---|
| 143 | metaTypes.append(id);
|
|---|
| 144 | ++inputCount;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | return inputCount;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | QT_END_NAMESPACE
|
|---|