* qt/: Update to Subversion r548032.
[platform/upstream/dbus.git] / qt / src / qdbusmisc.cpp
1 /* qdbusmisc.cpp Miscellaneous routines that didn't fit anywhere else
2  *
3  * Copyright (C) 2006 Trolltech AS. All rights reserved.
4  *    Author: Thiago Macieira <thiago.macieira@trolltech.com>
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  */
23
24 #include <string.h>
25
26 #include <QtCore/qvariant.h>
27 #include <QtCore/qmetaobject.h>
28
29 #include "qdbusconnection_p.h"
30 #include "qdbustypehelper_p.h"
31
32 bool qDBusCheckAsyncTag(const char *tag)
33 {
34     if (!tag || !*tag)
35         return false;
36
37     const char *p = strstr(tag, "async");
38     if (p != NULL &&
39         (p == tag || *(p-1) == ' ') &&
40         (p[5] == '\0' || p[5] == ' '))
41         return true;
42
43     p = strstr(tag, "Q_ASYNC");
44     if (p != NULL &&
45         (p == tag || *(p-1) == ' ') &&
46         (p[7] == '\0' || p[7] == ' '))
47         return true;
48
49     return false;
50 }
51
52 int qDBusNameToTypeId(const char *name)
53 {
54     int id = static_cast<int>( QVariant::nameToType(name) );
55     if (id == QVariant::UserType)
56         id = QMetaType::type(name);
57
58     switch (id) {
59     case QVariant::Bool:
60     case QVariant::Int:
61     case QVariant::UInt:
62     case QVariant::Char:
63     case QMetaType::Short:
64     case QMetaType::UShort:
65     case QMetaType::UChar:
66     case QVariant::LongLong:
67     case QVariant::ULongLong:
68     case QVariant::Double:
69     case QVariant::String:
70     case QVariant::Date:
71     case QVariant::Time:
72     case QVariant::DateTime:
73     case QVariant::Map:
74     case QVariant::StringList:
75     case QVariant::ByteArray:
76     case QVariant::List:
77         return id;
78
79     default:
80         if (id == QDBusConnectionPrivate::registerMessageMetaType() ||
81             id == QDBusTypeHelper<QVariant>::id() ||
82             id == QDBusTypeHelper<bool>::listId() ||
83             id == QDBusTypeHelper<short>::listId() ||
84             id == QDBusTypeHelper<ushort>::listId() ||
85             id == QDBusTypeHelper<int>::listId() ||
86             id == QDBusTypeHelper<qlonglong>::listId() ||
87             id == QDBusTypeHelper<qulonglong>::listId() ||
88             id == QDBusTypeHelper<double>::listId())
89             return id;
90
91         return 0;               // invalid
92     }
93 }
94
95 // calculates the metatypes for the method
96 // the slot must have the parameters in the following form:
97 //  - zero or more value or const-ref parameters of any kind
98 //  - zero or one const ref of QDBusMessage
99 //  - zero or more non-const ref parameters
100 // No parameter may be a template.
101 // this function returns -1 if the parameters don't match the above form
102 // this function returns the number of *input* parameters, including the QDBusMessage one if any
103 // this function does not check the return type, so metaTypes[0] is always 0 and always present
104 // metaTypes.count() >= retval + 1 in all cases
105 //
106 // sig must be the normalised signature for the method
107 int qDBusParametersForMethod(const QMetaMethod &mm, QList<int>& metaTypes)
108 {
109     QList<QByteArray> parameterTypes = mm.parameterTypes();
110     metaTypes.clear();
111
112     metaTypes.append(0);        // return type
113     int inputCount = 0;
114     bool seenMessage = false;
115     foreach (QByteArray type, parameterTypes) {
116         if (type.endsWith('*')) {
117             //qWarning("Could not parse the method '%s'", mm.signature());
118             // pointer?
119             return -1;
120         }
121
122         if (type.endsWith('&')) {
123             type.truncate(type.length() - 1);
124             int id = qDBusNameToTypeId(type);
125             if (id == 0) {
126                 //qWarning("Could not parse the method '%s'", mm.signature());
127                 // invalid type in method parameter list
128                 return -1;
129             }
130
131             metaTypes.append( id );
132             seenMessage = true; // it cannot appear anymore anyways
133             continue;
134         }
135
136         if (seenMessage) {      // && !type.endsWith('&')
137             //qWarning("Could not parse the method '%s'", mm.signature());
138             // non-output parameters after message or after output params
139             return -1;          // not allowed
140         }
141
142         int id = qDBusNameToTypeId(type);
143         if (id == 0) {
144             //qWarning("Could not parse the method '%s'", mm.signature());
145             // invalid type in method parameter list
146             return -1;
147         }
148         metaTypes.append(id);
149         ++inputCount;
150
151         if (id == QDBusConnectionPrivate::registerMessageMetaType())
152             seenMessage = true;
153     }
154
155     return inputCount;
156 }