1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtDBus module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <string.h>
41
42#ifndef QT_BOOTSTRAPPED
43#include <QtCore/qcoreapplication.h>
44#include <QtCore/qvariant.h>
45#include <QtCore/qmetaobject.h>
46
47#include "qdbusutil_p.h"
48#include "qdbusconnection_p.h"
49#include "qdbusabstractadaptor_p.h" // for QCLASSINFO_DBUS_*
50#endif
51#include <QtCore/qvector.h>
52#include "qdbusmetatype_p.h"
53
54#ifndef QT_NO_DBUS
55
56QT_BEGIN_NAMESPACE
57
58bool qDBusCheckAsyncTag(const char *tag)
59{
60 static const char noReplyTag[] = "Q_NOREPLY";
61 if (!tag || !*tag)
62 return false;
63
64 const char *p = strstr(tag, noReplyTag);
65 if (p != NULL &&
66 (p == tag || *(p-1) == ' ') &&
67 (p[sizeof noReplyTag - 1] == '\0' || p[sizeof noReplyTag - 1] == ' '))
68 return true;
69
70 return false;
71}
72
73#ifndef QT_BOOTSTRAPPED
74
75QString qDBusInterfaceFromMetaObject(const QMetaObject *mo)
76{
77 QString interface;
78
79 int idx = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTERFACE);
80 if (idx >= mo->classInfoOffset()) {
81 interface = QLatin1String(mo->classInfo(idx).value());
82 } else {
83 interface = QLatin1String(mo->className());
84 interface.replace(QLatin1String("::"), QLatin1String("."));
85
86 if (interface.startsWith(QLatin1String("QDBus"))) {
87 interface.prepend(QLatin1String("org.qtproject.QtDBus."));
88 } else if (interface.startsWith(QLatin1Char('Q')) &&
89 interface.length() >= 2 && interface.at(1).isUpper()) {
90 // assume it's Qt
91 interface.prepend(QLatin1String("org.qtproject.Qt."));
92 } else if (!QCoreApplication::instance()||
93 QCoreApplication::instance()->applicationName().isEmpty()) {
94 interface.prepend(QLatin1String("local."));
95 } else {
96 interface.prepend(QLatin1Char('.')).prepend(QCoreApplication::instance()->applicationName());
97 const QString organizationDomain = QCoreApplication::instance()->organizationDomain();
98 const auto domainName = organizationDomain.splitRef(QLatin1Char('.'), QString::SkipEmptyParts);
99 if (domainName.isEmpty()) {
100 interface.prepend(QLatin1String("local."));
101 } else {
102 QString composedDomain;
103 // + 1 for additional dot, e.g. organizationDomain equals "example.com",
104 // then composedDomain will be equal "com.example."
105 composedDomain.reserve(organizationDomain.size() + 1);
106 for (auto it = domainName.rbegin(), end = domainName.rend(); it != end; ++it)
107 composedDomain += *it + QLatin1Char('.');
108
109 interface.prepend(composedDomain);
110 }
111 }
112 }
113
114 return interface;
115}
116
117bool qDBusInterfaceInObject(QObject *obj, const QString &interface_name)
118{
119 const QMetaObject *mo = obj->metaObject();
120 for ( ; mo != &QObject::staticMetaObject; mo = mo->superClass())
121 if (interface_name == qDBusInterfaceFromMetaObject(mo))
122 return true;
123 return false;
124}
125
126// calculates the metatypes for the method
127// the slot must have the parameters in the following form:
128// - zero or more value or const-ref parameters of any kind
129// - zero or one const ref of QDBusMessage
130// - zero or more non-const ref parameters
131// No parameter may be a template.
132// this function returns -1 if the parameters don't match the above form
133// this function returns the number of *input* parameters, including the QDBusMessage one if any
134// this function does not check the return type, so metaTypes[0] is always 0 and always present
135// metaTypes.count() >= retval + 1 in all cases
136//
137// sig must be the normalised signature for the method
138int qDBusParametersForMethod(const QMetaMethod &mm, QVector<int> &metaTypes, QString &errorMsg)
139{
140 return qDBusParametersForMethod(mm.parameterTypes(), metaTypes, errorMsg);
141}
142
143#endif // QT_BOOTSTRAPPED
144
145int qDBusParametersForMethod(const QList<QByteArray> &parameterTypes, QVector<int>& metaTypes, QString &errorMsg)
146{
147 QDBusMetaTypeId::init();
148 metaTypes.clear();
149
150 metaTypes.append(0); // return type
151 int inputCount = 0;
152 bool seenMessage = false;
153 QList<QByteArray>::ConstIterator it = parameterTypes.constBegin();
154 QList<QByteArray>::ConstIterator end = parameterTypes.constEnd();
155 for ( ; it != end; ++it) {
156 const QByteArray &type = *it;
157 if (type.endsWith('*')) {
158 errorMsg = QLatin1String("Pointers are not supported: ") + QLatin1String(type);
159 return -1;
160 }
161
162 if (type.endsWith('&')) {
163 QByteArray basictype = type;
164 basictype.truncate(type.length() - 1);
165
166 int id = QMetaType::type(basictype);
167 if (id == 0) {
168 errorMsg = QLatin1String("Unregistered output type in parameter list: ") + QLatin1String(type);
169 return -1;
170 } else if (QDBusMetaType::typeToSignature(id) == 0)
171 return -1;
172
173 metaTypes.append( id );
174 seenMessage = true; // it cannot appear anymore anyways
175 continue;
176 }
177
178 if (seenMessage) { // && !type.endsWith('&')
179 errorMsg = QLatin1String("Invalid method, non-output parameters after message or after output parameters: ") + QLatin1String(type);
180 return -1; // not allowed
181 }
182
183 int id = QMetaType::type(type);
184#ifdef QT_BOOTSTRAPPED
185 // in bootstrap mode QDBusMessage isn't included, thus we need to resolve it manually here
186 if (type == "QDBusMessage") {
187 id = QDBusMetaTypeId::message();
188 }
189#endif
190
191 if (id == QMetaType::UnknownType) {
192 errorMsg = QLatin1String("Unregistered input type in parameter list: ") + QLatin1String(type);
193 return -1;
194 }
195
196 if (id == QDBusMetaTypeId::message())
197 seenMessage = true;
198 else if (QDBusMetaType::typeToSignature(id) == 0) {
199 errorMsg = QLatin1String("Type not registered with QtDBus in parameter list: ") + QLatin1String(type);
200 return -1;
201 }
202
203 metaTypes.append(id);
204 ++inputCount;
205 }
206
207 return inputCount;
208}
209
210QT_END_NAMESPACE
211
212#endif // QT_NO_DBUS
213