1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtDBus module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 #ifndef QT_BOOTSTRAPPED
45 #include <QtCore/qcoreapplication.h>
46 #include <QtCore/qvariant.h>
47 #include <QtCore/qmetaobject.h>
49 #include "qdbusutil_p.h"
50 #include "qdbusconnection_p.h"
51 #include "qdbusabstractadaptor_p.h" // for QCLASSINFO_DBUS_*
53 #include <QtCore/qvector.h>
54 #include "qdbusmetatype_p.h"
60 bool qDBusCheckAsyncTag(const char *tag)
62 static const char noReplyTag[] = "Q_NOREPLY";
66 const char *p = strstr(tag, noReplyTag);
68 (p == tag || *(p-1) == ' ') &&
69 (p[sizeof noReplyTag - 1] == '\0' || p[sizeof noReplyTag - 1] == ' '))
75 #ifndef QT_BOOTSTRAPPED
77 QString qDBusInterfaceFromMetaObject(const QMetaObject *mo)
81 int idx = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTERFACE);
82 if (idx >= mo->classInfoOffset()) {
83 interface = QLatin1String(mo->classInfo(idx).value());
85 interface = QLatin1String(mo->className());
86 interface.replace(QLatin1String("::"), QLatin1String("."));
88 if (interface.startsWith(QLatin1String("QDBus"))) {
89 interface.prepend(QLatin1String("org.qtproject.QtDBus."));
90 } else if (interface.startsWith(QLatin1Char('Q')) &&
91 interface.length() >= 2 && interface.at(1).isUpper()) {
93 interface.prepend(QLatin1String("org.qtproject.Qt."));
94 } else if (!QCoreApplication::instance()||
95 QCoreApplication::instance()->applicationName().isEmpty()) {
96 interface.prepend(QLatin1String("local."));
98 interface.prepend(QLatin1Char('.')).prepend(QCoreApplication::instance()->applicationName());
99 QStringList domainName =
100 QCoreApplication::instance()->organizationDomain().split(QLatin1Char('.'),
101 QString::SkipEmptyParts);
102 if (domainName.isEmpty())
103 interface.prepend(QLatin1String("local."));
105 for (int i = 0; i < domainName.count(); ++i)
106 interface.prepend(QLatin1Char('.')).prepend(domainName.at(i));
113 bool qDBusInterfaceInObject(QObject *obj, const QString &interface_name)
115 const QMetaObject *mo = obj->metaObject();
116 for ( ; mo != &QObject::staticMetaObject; mo = mo->superClass())
117 if (interface_name == qDBusInterfaceFromMetaObject(mo))
122 // calculates the metatypes for the method
123 // the slot must have the parameters in the following form:
124 // - zero or more value or const-ref parameters of any kind
125 // - zero or one const ref of QDBusMessage
126 // - zero or more non-const ref parameters
127 // No parameter may be a template.
128 // this function returns -1 if the parameters don't match the above form
129 // this function returns the number of *input* parameters, including the QDBusMessage one if any
130 // this function does not check the return type, so metaTypes[0] is always 0 and always present
131 // metaTypes.count() >= retval + 1 in all cases
133 // sig must be the normalised signature for the method
134 int qDBusParametersForMethod(const QMetaMethod &mm, QVector<int> &metaTypes)
136 return qDBusParametersForMethod(mm.parameterTypes(), metaTypes);
139 #endif // QT_BOOTSTRAPPED
141 int qDBusParametersForMethod(const QList<QByteArray> ¶meterTypes, QVector<int>& metaTypes)
143 QDBusMetaTypeId::init();
146 metaTypes.append(0); // return type
148 bool seenMessage = false;
149 QList<QByteArray>::ConstIterator it = parameterTypes.constBegin();
150 QList<QByteArray>::ConstIterator end = parameterTypes.constEnd();
151 for ( ; it != end; ++it) {
152 const QByteArray &type = *it;
153 if (type.endsWith('*')) {
154 //qWarning("Could not parse the method '%s'", mm.methodSignature().constData());
159 if (type.endsWith('&')) {
160 QByteArray basictype = type;
161 basictype.truncate(type.length() - 1);
163 int id = QMetaType::type(basictype);
165 //qWarning("Could not parse the method '%s'", mm.methodSignature().constData());
166 // invalid type in method parameter list
168 } else if (QDBusMetaType::typeToSignature(id) == 0)
171 metaTypes.append( id );
172 seenMessage = true; // it cannot appear anymore anyways
176 if (seenMessage) { // && !type.endsWith('&')
177 //qWarning("Could not parse the method '%s'", mm.methodSignature().constData());
178 // non-output parameters after message or after output params
179 return -1; // not allowed
182 int id = QMetaType::type(type);
183 if (id == QMetaType::UnknownType) {
184 //qWarning("Could not parse the method '%s'", mm.methodSignature().constData());
185 // invalid type in method parameter list
189 if (id == QDBusMetaTypeId::message)
191 else if (QDBusMetaType::typeToSignature(id) == 0)
194 metaTypes.append(id);