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 #include <QtCore/qcoreapplication.h>
45 #include <QtCore/qvariant.h>
46 #include <QtCore/qmetaobject.h>
48 #include "qdbusutil_p.h"
49 #include "qdbusconnection_p.h"
50 #include "qdbusmetatype_p.h"
51 #include "qdbusabstractadaptor_p.h" // for QCLASSINFO_DBUS_*
57 bool qDBusCheckAsyncTag(const char *tag)
59 static const char noReplyTag[] = "Q_NOREPLY";
63 const char *p = strstr(tag, noReplyTag);
65 (p == tag || *(p-1) == ' ') &&
66 (p[sizeof noReplyTag - 1] == '\0' || p[sizeof noReplyTag - 1] == ' '))
72 QString qDBusInterfaceFromMetaObject(const QMetaObject *mo)
76 int idx = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTERFACE);
77 if (idx >= mo->classInfoOffset()) {
78 interface = QLatin1String(mo->classInfo(idx).value());
80 interface = QLatin1String(mo->className());
81 interface.replace(QLatin1String("::"), QLatin1String("."));
83 if (interface.startsWith(QLatin1String("QDBus"))) {
84 interface.prepend(QLatin1String("com.trolltech.QtDBus."));
85 } else if (interface.startsWith(QLatin1Char('Q')) &&
86 interface.length() >= 2 && interface.at(1).isUpper()) {
88 interface.prepend(QLatin1String("com.trolltech.Qt."));
89 } else if (!QCoreApplication::instance()||
90 QCoreApplication::instance()->applicationName().isEmpty()) {
91 interface.prepend(QLatin1String("local."));
93 interface.prepend(QLatin1Char('.')).prepend(QCoreApplication::instance()->applicationName());
94 QStringList domainName =
95 QCoreApplication::instance()->organizationDomain().split(QLatin1Char('.'),
96 QString::SkipEmptyParts);
97 if (domainName.isEmpty())
98 interface.prepend(QLatin1String("local."));
100 for (int i = 0; i < domainName.count(); ++i)
101 interface.prepend(QLatin1Char('.')).prepend(domainName.at(i));
108 bool qDBusInterfaceInObject(QObject *obj, const QString &interface_name)
110 const QMetaObject *mo = obj->metaObject();
111 for ( ; mo != &QObject::staticMetaObject; mo = mo->superClass())
112 if (interface_name == qDBusInterfaceFromMetaObject(mo))
117 // calculates the metatypes for the method
118 // the slot must have the parameters in the following form:
119 // - zero or more value or const-ref parameters of any kind
120 // - zero or one const ref of QDBusMessage
121 // - zero or more non-const ref parameters
122 // No parameter may be a template.
123 // this function returns -1 if the parameters don't match the above form
124 // this function returns the number of *input* parameters, including the QDBusMessage one if any
125 // this function does not check the return type, so metaTypes[0] is always 0 and always present
126 // metaTypes.count() >= retval + 1 in all cases
128 // sig must be the normalised signature for the method
129 int qDBusParametersForMethod(const QMetaMethod &mm, QList<int>& metaTypes)
131 QDBusMetaTypeId::init();
133 QList<QByteArray> parameterTypes = mm.parameterTypes();
136 metaTypes.append(0); // return type
138 bool seenMessage = false;
139 QList<QByteArray>::ConstIterator it = parameterTypes.constBegin();
140 QList<QByteArray>::ConstIterator end = parameterTypes.constEnd();
141 for ( ; it != end; ++it) {
142 const QByteArray &type = *it;
143 if (type.endsWith('*')) {
144 //qWarning("Could not parse the method '%s'", mm.methodSignature().constData());
149 if (type.endsWith('&')) {
150 QByteArray basictype = type;
151 basictype.truncate(type.length() - 1);
153 int id = QMetaType::type(basictype);
155 //qWarning("Could not parse the method '%s'", mm.methodSignature().constData());
156 // invalid type in method parameter list
158 } else if (QDBusMetaType::typeToSignature(id) == 0)
161 metaTypes.append( id );
162 seenMessage = true; // it cannot appear anymore anyways
166 if (seenMessage) { // && !type.endsWith('&')
167 //qWarning("Could not parse the method '%s'", mm.methodSignature().constData());
168 // non-output parameters after message or after output params
169 return -1; // not allowed
172 int id = QMetaType::type(type);
174 //qWarning("Could not parse the method '%s'", mm.methodSignature().constData());
175 // invalid type in method parameter list
179 if (id == QDBusMetaTypeId::message)
181 else if (QDBusMetaType::typeToSignature(id) == 0)
184 metaTypes.append(id);