Merge remote-tracking branch 'origin/api_changes'
[profile/ivi/qtbase.git] / src / dbus / qdbusmisc.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDBus module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <string.h>
43
44 #ifndef QT_BOOTSTRAPPED
45 #include <QtCore/qcoreapplication.h>
46 #include <QtCore/qvariant.h>
47 #include <QtCore/qmetaobject.h>
48
49 #include "qdbusutil_p.h"
50 #include "qdbusconnection_p.h"
51 #include "qdbusabstractadaptor_p.h" // for QCLASSINFO_DBUS_*
52 #endif
53 #include <QtCore/qvector.h>
54 #include "qdbusmetatype_p.h"
55
56 #ifndef QT_NO_DBUS
57
58 QT_BEGIN_NAMESPACE
59
60 bool qDBusCheckAsyncTag(const char *tag)
61 {
62     static const char noReplyTag[] = "Q_NOREPLY";
63     if (!tag || !*tag)
64         return false;
65
66     const char *p = strstr(tag, noReplyTag);
67     if (p != NULL &&
68         (p == tag || *(p-1) == ' ') &&
69         (p[sizeof noReplyTag - 1] == '\0' || p[sizeof noReplyTag - 1] == ' '))
70         return true;
71
72     return false;
73 }
74
75 #ifndef QT_BOOTSTRAPPED
76
77 QString qDBusInterfaceFromMetaObject(const QMetaObject *mo)
78 {
79     QString interface;
80
81     int idx = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTERFACE);
82     if (idx >= mo->classInfoOffset()) {
83         interface = QLatin1String(mo->classInfo(idx).value());
84     } else {
85         interface = QLatin1String(mo->className());
86         interface.replace(QLatin1String("::"), QLatin1String("."));
87
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()) {
92             // assume it's Qt
93             interface.prepend(QLatin1String("org.qtproject.Qt."));
94         } else if (!QCoreApplication::instance()||
95                    QCoreApplication::instance()->applicationName().isEmpty()) {
96             interface.prepend(QLatin1String("local."));
97          } else {
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."));
104             else
105                 for (int i = 0; i < domainName.count(); ++i)
106                     interface.prepend(QLatin1Char('.')).prepend(domainName.at(i));
107          }
108      }
109
110     return interface;
111 }
112
113 bool qDBusInterfaceInObject(QObject *obj, const QString &interface_name)
114 {
115     const QMetaObject *mo = obj->metaObject();
116     for ( ; mo != &QObject::staticMetaObject; mo = mo->superClass())
117         if (interface_name == qDBusInterfaceFromMetaObject(mo))
118             return true;
119     return false;
120 }
121
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
132 //
133 // sig must be the normalised signature for the method
134 int qDBusParametersForMethod(const QMetaMethod &mm, QVector<int> &metaTypes)
135 {
136     return qDBusParametersForMethod(mm.parameterTypes(), metaTypes);
137 }
138
139 #endif // QT_BOOTSTRAPPED
140
141 int qDBusParametersForMethod(const QList<QByteArray> &parameterTypes, QVector<int>& metaTypes)
142 {
143     QDBusMetaTypeId::init();
144     metaTypes.clear();
145
146     metaTypes.append(0);        // return type
147     int inputCount = 0;
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());
155             // pointer?
156             return -1;
157         }
158
159         if (type.endsWith('&')) {
160             QByteArray basictype = type;
161             basictype.truncate(type.length() - 1);
162
163             int id = QMetaType::type(basictype);
164             if (id == 0) {
165                 //qWarning("Could not parse the method '%s'", mm.methodSignature().constData());
166                 // invalid type in method parameter list
167                 return -1;
168             } else if (QDBusMetaType::typeToSignature(id) == 0)
169                 return -1;
170
171             metaTypes.append( id );
172             seenMessage = true; // it cannot appear anymore anyways
173             continue;
174         }
175
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
180         }
181
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
186             return -1;
187         }
188
189         if (id == QDBusMetaTypeId::message)
190             seenMessage = true;
191         else if (QDBusMetaType::typeToSignature(id) == 0)
192             return -1;
193
194         metaTypes.append(id);
195         ++inputCount;
196     }
197
198     return inputCount;
199 }
200
201 QT_END_NAMESPACE
202
203 #endif // QT_NO_DBUS