3 * Copyright (C) 2006 Trolltech AS. All rights reserved.
4 * Author: Thiago Macieira <thiago.macieira@trolltech.com>
6 * Licensed under the Academic Free License version 2.1
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.
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.
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.
24 #include "qdbusutil.h"
26 #include <dbus/dbus.h>
28 #include <QtCore/qstringlist.h>
29 #include <QtCore/qregexp.h>
31 #include "qdbustype_p.h"
35 The QDBusUtil namespace contains a few functions that are of general use when dealing with D-Bus
41 \fn QDBusUtil::isValidInterfaceName(const QString &ifaceName)
42 Returns true if this is \a ifaceName is a valid interface name.
44 Valid interface names must:
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
53 bool isValidInterfaceName(const QString& ifaceName)
55 if (ifaceName.isEmpty() || ifaceName.length() > DBUS_MAXIMUM_NAME_LENGTH)
58 QStringList parts = ifaceName.split(QLatin1Char('.'));
59 if (parts.count() < 2)
60 return false; // at least two parts
62 foreach (QString part, parts)
63 if (!isValidMemberName(part))
70 \fn QDBusUtil::isValidUniqueConnectionName(const QString &connName)
71 Returns true if \a connName is a valid unique connection name.
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.
76 bool isValidUniqueConnectionName(const QString &connName)
78 if (connName.isEmpty() || connName.length() > DBUS_MAXIMUM_NAME_LENGTH ||
79 !connName.startsWith(QLatin1Char(':')))
82 QStringList parts = connName.mid(1).split(QLatin1Char('.'));
83 if (parts.count() < 1)
86 QRegExp regex(QLatin1String("[a-zA-Z0-9_-]+"));
87 foreach (QString part, parts)
88 if (!regex.exactMatch(part))
95 \fn QDBusUtil::isValidBusName(const QString &busName)
96 Returns true if \a busName is a valid bus name.
98 A valid bus name is either a valid unique connection name or follows the rules:
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
107 \sa isValidUniqueConnectionName()
109 bool isValidBusName(const QString &busName)
111 if (busName.isEmpty() || busName.length() > DBUS_MAXIMUM_NAME_LENGTH)
114 if (busName.startsWith(QLatin1Char(':')))
115 return isValidUniqueConnectionName(busName);
117 QStringList parts = busName.split(QLatin1Char('.'));
118 if (parts.count() < 1)
121 QRegExp regex(QLatin1String("[a-zA-Z_-][a-zA-Z0-9_-]*"));
122 foreach (QString part, parts)
123 if (!regex.exactMatch(part))
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.
135 bool isValidMemberName(const QString &memberName)
137 if (memberName.isEmpty() || memberName.length() > DBUS_MAXIMUM_NAME_LENGTH)
140 QRegExp regex(QLatin1String("[a-zA-Z_][a-zA-Z0-9_]+"));
141 return regex.exactMatch(memberName);
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.
149 bool isValidErrorName(const QString &errorName)
151 return isValidInterfaceName(errorName);
155 \fn QDBusUtil::isValidObjectPath(const QString &path)
156 Returns true if \a path is valid object path.
158 Valid object paths follow the rules:
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
167 bool isValidObjectPath(const QString &path)
169 if (path == QLatin1String("/"))
172 if (!path.startsWith(QLatin1Char('/')) || path.indexOf(QLatin1String("//")) != -1 ||
173 path.endsWith(QLatin1Char('/')))
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
180 QRegExp regex(QLatin1String("[a-zA-Z0-9_]+"));
181 foreach (QString part, parts)
182 if (!regex.exactMatch(part))
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.
194 \sa isValidSingleSignature()
196 bool isValidSignature(const QString &signature)
198 return dbus_signature_validate(signature.toUtf8(), 0);
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.
207 bool isValidSingleSignature(const QString &signature)
209 return dbus_signature_validate_single(signature.toUtf8(), 0);
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
217 \sa isValidSingleSignature(), typeToSignature(), QVariant::type(), QVariant::userType()
219 QVariant::Type signatureToType(const QString &signature)
221 return QVariant::Type( QDBusType::qvariantType(signature.toLatin1().constData()) );
225 \fn QDBusUtil::typeToSignature(QVariant::Type type)
226 Returns the D-Bus signature equivalent to the supplied meta type id \a type.
228 \sa isValidSingleSignature(), signatureToType(), QVariant::type(), QVariant::userType()
230 const char *typeToSignature(QVariant::Type type)
232 return QDBusType::dbusSignature( type );
235 } // namespace QDBusUtil