* dbus/dbus-sysdeps.c: Make tcp socket connection error somewhat more
[platform/upstream/dbus.git] / qt / qdbusutil.cpp
1 /* -*- C++ -*-
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 "qdbusutil.h"
25
26 #include <dbus/dbus.h>
27
28 #include <QtCore/qstringlist.h>
29 #include <QtCore/qregexp.h>
30
31 #include "qdbustype_p.h"
32
33 /*!
34     \namespace QDBusUtil
35     The QDBusUtil namespace contains a few functions that are of general use when dealing with D-Bus
36     strings.
37 */
38 namespace QDBusUtil
39 {
40     /*!
41         \fn QDBusUtil::isValidInterfaceName(const QString &ifaceName)
42         Returns true if this is \a ifaceName is a valid interface name.
43
44         Valid interface names must:
45         \list
46           \o not be empty
47           \o not exceed 255 characters in length
48           \o be composed of dot-separated string components that contain only ASCII letters, digits
49              and the underscore ("_") character
50           \o contain at least two such components
51         \endlist
52     */
53     bool isValidInterfaceName(const QString& ifaceName)
54     {
55         if (ifaceName.isEmpty() || ifaceName.length() > DBUS_MAXIMUM_NAME_LENGTH)
56             return false;
57
58         QStringList parts = ifaceName.split(QLatin1Char('.'));
59         if (parts.count() < 2)
60             return false;           // at least two parts
61
62         foreach (QString part, parts)
63             if (!isValidMemberName(part))
64                 return false;
65
66         return true;
67     }
68
69     /*!
70         \fn QDBusUtil::isValidUniqueConnectionName(const QString &connName)
71         Returns true if \a connName is a valid unique connection name.
72
73         Unique connection names start with a colon (":") and are followed by a list of dot-separated
74         components composed of ASCII letters, digits, the hypen or the underscore ("_") character.
75     */
76     bool isValidUniqueConnectionName(const QString &connName)
77     {
78         if (connName.isEmpty() || connName.length() > DBUS_MAXIMUM_NAME_LENGTH ||
79             !connName.startsWith(QLatin1Char(':')))
80             return false;
81
82         QStringList parts = connName.mid(1).split(QLatin1Char('.'));
83         if (parts.count() < 1)
84             return false;
85
86         QRegExp regex(QLatin1String("[a-zA-Z0-9_-]+"));
87         foreach (QString part, parts)
88             if (!regex.exactMatch(part))
89                 return false;
90
91         return true;
92     }
93
94     /*!
95         \fn QDBusUtil::isValidBusName(const QString &busName)
96         Returns true if \a busName is a valid bus name.
97
98         A valid bus name is either a valid unique connection name or follows the rules:
99         \list
100           \o is not empty
101           \o does not exceed 255 characters in length
102           \o be composed of dot-separated string components that contain only ASCII letters, digits,
103              hyphens or underscores ("_"), but don't start with a digit
104           \o contains at least two such elements
105         \endlist
106
107         \sa isValidUniqueConnectionName()
108     */
109     bool isValidBusName(const QString &busName)
110     {
111         if (busName.isEmpty() || busName.length() > DBUS_MAXIMUM_NAME_LENGTH)
112             return false;
113
114         if (busName.startsWith(QLatin1Char(':')))
115             return isValidUniqueConnectionName(busName);
116
117         QStringList parts = busName.split(QLatin1Char('.'));
118         if (parts.count() < 1)
119             return false;
120
121         QRegExp regex(QLatin1String("[a-zA-Z_-][a-zA-Z0-9_-]*"));
122         foreach (QString part, parts)
123             if (!regex.exactMatch(part))
124                 return false;
125
126         return true;
127     }
128
129     /*!
130         \fn QDBusUtil::isValidMemberName(const QString &memberName)
131         Returns true if \a memberName is a valid member name. A valid member name does not exceed
132         255 characters in length, is not empty, is composed only of ASCII letters, digits and
133         underscores, but does not start with a digit.
134     */
135     bool isValidMemberName(const QString &memberName)
136     {
137         if (memberName.isEmpty() || memberName.length() > DBUS_MAXIMUM_NAME_LENGTH)
138             return false;
139
140         QRegExp regex(QLatin1String("[a-zA-Z_][a-zA-Z0-9_]+"));
141         return regex.exactMatch(memberName);
142     }
143
144     /*!
145         \fn QDBusUtil::isValidErrorName(const QString &errorName)
146         Returns true if \a errorName is a valid error name. Valid error names are valid interface
147         names and vice-versa, so this function is actually an alias for isValidInterfaceName.
148     */
149     bool isValidErrorName(const QString &errorName)
150     {
151         return isValidInterfaceName(errorName);
152     }
153
154     /*!
155         \fn QDBusUtil::isValidObjectPath(const QString &path)
156         Returns true if \a path is valid object path.
157
158         Valid object paths follow the rules:
159         \list
160           \o start with the slash character ("/")
161           \o do not end in a slash, unless the path is just the initial slash
162           \o do not contain any two slashes in sequence
163           \o contain slash-separated parts, each of which is composed of ASCII letters, digits and
164              underscores ("_")
165         \endlist
166     */
167     bool isValidObjectPath(const QString &path)
168     {
169         if (path == QLatin1String("/"))
170             return true;
171
172         if (!path.startsWith(QLatin1Char('/')) || path.indexOf(QLatin1String("//")) != -1 ||
173             path.endsWith(QLatin1Char('/')))
174             return false;
175
176         QStringList parts = path.split(QLatin1Char('/'));
177         Q_ASSERT(parts.count() >= 1);
178         parts.removeFirst();    // it starts with /, so we get an empty first part
179
180         QRegExp regex(QLatin1String("[a-zA-Z0-9_]+"));
181         foreach (QString part, parts)
182             if (!regex.exactMatch(part))
183                 return false;
184
185         return true;
186     }
187
188     /*!
189         \fn QDBusUtil::isValidSignature(const QString &signature)
190         Returns true if \a signature is a valid D-Bus type signature for one or more types.
191         This function returns true if it can all of \a signature into valid, individual types and no
192         characters remain in \a signature.
193
194         \sa isValidSingleSignature()
195     */
196     bool isValidSignature(const QString &signature)
197     {
198         return dbus_signature_validate(signature.toUtf8(), 0);
199     }
200
201     /*!
202         \fn QDBusUtil::isValidSingleSignature(const QString &signature)
203         Returns true if \a signature is a valid D-Bus type signature for exactly one full type. This
204         function tries to convert the type signature into a D-Bus type and, if it succeeds and no
205         characters remain in the signature, it returns true.
206     */
207     bool isValidSingleSignature(const QString &signature)
208     {
209         return dbus_signature_validate_single(signature.toUtf8(), 0);
210     }
211
212     /*!
213         \fn QDBusUtil::signatureToType(const QString &signature)
214         Returns the Qt meta type id for the given D-Bus signature for exactly one full type, given
215         by \a signature.
216
217         \sa isValidSingleSignature(), typeToSignature(), QVariant::type(), QVariant::userType()
218     */
219     QVariant::Type signatureToType(const QString &signature)
220     {
221         return QVariant::Type( QDBusType::qvariantType(signature.toLatin1().constData()) );
222     }
223
224     /*!
225         \fn QDBusUtil::typeToSignature(QVariant::Type type)
226         Returns the D-Bus signature equivalent to the supplied meta type id \a type.
227
228         \sa isValidSingleSignature(), signatureToType(), QVariant::type(), QVariant::userType()
229     */
230     const char *typeToSignature(QVariant::Type type)
231     {
232         return QDBusType::dbusSignature( type );
233     }
234
235 } // namespace QDBusUtil