0a5da604f2575b23e6a6619eed28736fdce0f1df
[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 #include <QtCore/qcoreapplication.h>
45 #include <QtCore/qvariant.h>
46 #include <QtCore/qmetaobject.h>
47
48 #include "qdbusutil_p.h"
49 #include "qdbusconnection_p.h"
50 #include "qdbusmetatype_p.h"
51 #include "qdbusabstractadaptor_p.h" // for QCLASSINFO_DBUS_*
52
53 #ifndef QT_NO_DBUS
54
55 QT_BEGIN_NAMESPACE
56
57 bool qDBusCheckAsyncTag(const char *tag)
58 {
59     static const char noReplyTag[] = "Q_NOREPLY";
60     if (!tag || !*tag)
61         return false;
62
63     const char *p = strstr(tag, noReplyTag);
64     if (p != NULL &&
65         (p == tag || *(p-1) == ' ') &&
66         (p[sizeof noReplyTag - 1] == '\0' || p[sizeof noReplyTag - 1] == ' '))
67         return true;
68
69     return false;
70 }
71
72 QString qDBusInterfaceFromMetaObject(const QMetaObject *mo)
73 {
74     QString interface;
75
76     int idx = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTERFACE);
77     if (idx >= mo->classInfoOffset()) {
78         interface = QLatin1String(mo->classInfo(idx).value());
79     } else {
80         interface = QLatin1String(mo->className());
81         interface.replace(QLatin1String("::"), QLatin1String("."));
82
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()) {
87             // assume it's Qt
88             interface.prepend(QLatin1String("com.trolltech.Qt."));
89         } else if (!QCoreApplication::instance()||
90                    QCoreApplication::instance()->applicationName().isEmpty()) {
91             interface.prepend(QLatin1String("local."));
92          } else {
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."));
99             else
100                 for (int i = 0; i < domainName.count(); ++i)
101                     interface.prepend(QLatin1Char('.')).prepend(domainName.at(i));
102          }
103      }
104
105     return interface;
106 }
107
108 bool qDBusInterfaceInObject(QObject *obj, const QString &interface_name)
109 {
110     const QMetaObject *mo = obj->metaObject();
111     for ( ; mo != &QObject::staticMetaObject; mo = mo->superClass())
112         if (interface_name == qDBusInterfaceFromMetaObject(mo))
113             return true;
114     return false;
115 }
116
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
127 //
128 // sig must be the normalised signature for the method
129 int qDBusParametersForMethod(const QMetaMethod &mm, QList<int>& metaTypes)
130 {
131     QDBusMetaTypeId::init();
132
133     QList<QByteArray> parameterTypes = mm.parameterTypes();
134     metaTypes.clear();
135
136     metaTypes.append(0);        // return type
137     int inputCount = 0;
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());
145             // pointer?
146             return -1;
147         }
148
149         if (type.endsWith('&')) {
150             QByteArray basictype = type;
151             basictype.truncate(type.length() - 1);
152
153             int id = QMetaType::type(basictype);
154             if (id == 0) {
155                 //qWarning("Could not parse the method '%s'", mm.methodSignature().constData());
156                 // invalid type in method parameter list
157                 return -1;
158             } else if (QDBusMetaType::typeToSignature(id) == 0)
159                 return -1;
160
161             metaTypes.append( id );
162             seenMessage = true; // it cannot appear anymore anyways
163             continue;
164         }
165
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
170         }
171
172         int id = QMetaType::type(type);
173         if (id == 0) {
174             //qWarning("Could not parse the method '%s'", mm.methodSignature().constData());
175             // invalid type in method parameter list
176             return -1;
177         }
178
179         if (id == QDBusMetaTypeId::message)
180             seenMessage = true;
181         else if (QDBusMetaType::typeToSignature(id) == 0)
182             return -1;
183
184         metaTypes.append(id);
185         ++inputCount;
186     }
187
188     return inputCount;
189 }
190
191 QT_END_NAMESPACE
192
193 #endif // QT_NO_DBUS