1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Copyright (C) 2016 Intel Corporation. |
5 | ** Contact: https://www.qt.io/licensing/ |
6 | ** |
7 | ** This file is part of the QtDBus module of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:LGPL$ |
10 | ** Commercial License Usage |
11 | ** Licensees holding valid commercial Qt licenses may use this file in |
12 | ** accordance with the commercial license agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and The Qt Company. For licensing terms |
15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
16 | ** information use the contact form at https://www.qt.io/contact-us. |
17 | ** |
18 | ** GNU Lesser General Public License Usage |
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
20 | ** General Public License version 3 as published by the Free Software |
21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
22 | ** packaging of this file. Please review the following information to |
23 | ** ensure the GNU Lesser General Public License version 3 requirements |
24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
25 | ** |
26 | ** GNU General Public License Usage |
27 | ** Alternatively, this file may be used under the terms of the GNU |
28 | ** General Public License version 2.0 or (at your option) the GNU General |
29 | ** Public license version 3 or any later version approved by the KDE Free |
30 | ** Qt Foundation. The licenses are as published by the Free Software |
31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
32 | ** included in the packaging of this file. Please review the following |
33 | ** information to ensure the GNU General Public License requirements will |
34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
36 | ** |
37 | ** $QT_END_LICENSE$ |
38 | ** |
39 | ****************************************************************************/ |
40 | |
41 | // |
42 | // W A R N I N G |
43 | // ------------- |
44 | // |
45 | // This file is not part of the Qt API. It exists for the convenience |
46 | // of the QLibrary class. This header file may change from |
47 | // version to version without notice, or even be removed. |
48 | // |
49 | // We mean it. |
50 | // |
51 | |
52 | #ifndef QDBUSUTIL_P_H |
53 | #define QDBUSUTIL_P_H |
54 | |
55 | #include <QtDBus/private/qtdbusglobal_p.h> |
56 | #include <QtDBus/qdbuserror.h> |
57 | #include <QtCore/qstring.h> |
58 | #include <QtCore/qvariant.h> |
59 | |
60 | #include "qdbus_symbols_p.h" |
61 | |
62 | #ifndef QT_NO_DBUS |
63 | |
64 | QT_BEGIN_NAMESPACE |
65 | |
66 | #define Q_DBUS_NO_EXPORT // force findclasslist.pl looking into this namespace |
67 | namespace Q_DBUS_NO_EXPORT QDBusUtil |
68 | { |
69 | Q_DBUS_EXPORT bool isValidInterfaceName(const QString &ifaceName); |
70 | |
71 | Q_DBUS_EXPORT bool isValidUniqueConnectionName(const QStringRef &busName); |
72 | inline bool isValidUniqueConnectionName(const QString &busName) { return isValidUniqueConnectionName(busName: QStringRef(&busName)); } |
73 | |
74 | Q_DBUS_EXPORT bool isValidBusName(const QString &busName); |
75 | |
76 | Q_DBUS_EXPORT bool isValidMemberName(const QStringRef &memberName); |
77 | inline bool isValidMemberName(const QString &memberName) { return isValidMemberName(memberName: QStringRef(&memberName)); } |
78 | |
79 | Q_DBUS_EXPORT bool isValidErrorName(const QString &errorName); |
80 | |
81 | Q_DBUS_EXPORT bool isValidPartOfObjectPath(const QStringRef &path); |
82 | inline bool isValidPartOfObjectPath(const QString &path) { return isValidPartOfObjectPath(path: QStringRef(&path)); } |
83 | |
84 | Q_DBUS_EXPORT bool isValidObjectPath(const QString &path); |
85 | |
86 | Q_DBUS_EXPORT bool isValidFixedType(int c); |
87 | |
88 | Q_DBUS_EXPORT bool isValidBasicType(int c); |
89 | |
90 | Q_DBUS_EXPORT bool isValidSignature(const QString &signature); |
91 | |
92 | Q_DBUS_EXPORT bool isValidSingleSignature(const QString &signature); |
93 | |
94 | Q_DBUS_EXPORT QString argumentToString(const QVariant &variant); |
95 | |
96 | enum AllowEmptyFlag { |
97 | EmptyAllowed, |
98 | EmptyNotAllowed |
99 | }; |
100 | |
101 | inline bool checkInterfaceName(const QString &name, AllowEmptyFlag empty, QDBusError *error) |
102 | { |
103 | if (name.isEmpty()) { |
104 | if (empty == EmptyAllowed) return true; |
105 | *error = QDBusError(QDBusError::InvalidInterface, QLatin1String("Interface name cannot be empty" )); |
106 | return false; |
107 | } |
108 | if (isValidInterfaceName(ifaceName: name)) return true; |
109 | *error = QDBusError(QDBusError::InvalidInterface, QLatin1String("Invalid interface class: %1" ).arg(args: name)); |
110 | return false; |
111 | } |
112 | |
113 | inline bool checkBusName(const QString &name, AllowEmptyFlag empty, QDBusError *error) |
114 | { |
115 | if (name.isEmpty()) { |
116 | if (empty == EmptyAllowed) return true; |
117 | *error = QDBusError(QDBusError::InvalidService, QLatin1String("Service name cannot be empty" )); |
118 | return false; |
119 | } |
120 | if (isValidBusName(busName: name)) return true; |
121 | *error = QDBusError(QDBusError::InvalidService, QLatin1String("Invalid service name: %1" ).arg(args: name)); |
122 | return false; |
123 | } |
124 | |
125 | inline bool checkObjectPath(const QString &path, AllowEmptyFlag empty, QDBusError *error) |
126 | { |
127 | if (path.isEmpty()) { |
128 | if (empty == EmptyAllowed) return true; |
129 | *error = QDBusError(QDBusError::InvalidObjectPath, QLatin1String("Object path cannot be empty" )); |
130 | return false; |
131 | } |
132 | if (isValidObjectPath(path)) return true; |
133 | *error = QDBusError(QDBusError::InvalidObjectPath, QLatin1String("Invalid object path: %1" ).arg(args: path)); |
134 | return false; |
135 | } |
136 | |
137 | inline bool checkMemberName(const QString &name, AllowEmptyFlag empty, QDBusError *error, const char *nameType = nullptr) |
138 | { |
139 | if (!nameType) nameType = "member" ; |
140 | if (name.isEmpty()) { |
141 | if (empty == EmptyAllowed) return true; |
142 | *error = QDBusError(QDBusError::InvalidMember, QLatin1String(nameType) + QLatin1String(" name cannot be empty" )); |
143 | return false; |
144 | } |
145 | if (isValidMemberName(memberName: name)) return true; |
146 | *error = QDBusError(QDBusError::InvalidMember, QLatin1String("Invalid %1 name: %2" ) |
147 | .arg(args: QLatin1String(nameType), args: name)); |
148 | return false; |
149 | } |
150 | |
151 | inline bool checkErrorName(const QString &name, AllowEmptyFlag empty, QDBusError *error) |
152 | { |
153 | if (name.isEmpty()) { |
154 | if (empty == EmptyAllowed) return true; |
155 | *error = QDBusError(QDBusError::InvalidInterface, QLatin1String("Error name cannot be empty" )); |
156 | return false; |
157 | } |
158 | if (isValidErrorName(errorName: name)) return true; |
159 | *error = QDBusError(QDBusError::InvalidInterface, QLatin1String("Invalid error name: %1" ).arg(args: name)); |
160 | return false; |
161 | } |
162 | |
163 | inline QString dbusService() |
164 | { return QStringLiteral(DBUS_SERVICE_DBUS); } |
165 | inline QString dbusPath() |
166 | { return QStringLiteral(DBUS_PATH_DBUS); } |
167 | inline QString dbusPathLocal() |
168 | { return QStringLiteral(DBUS_PATH_LOCAL); } |
169 | inline QString dbusInterface() |
170 | { |
171 | // it's the same string, but just be sure |
172 | Q_ASSERT(dbusService() == QLatin1String(DBUS_INTERFACE_DBUS)); |
173 | return dbusService(); |
174 | } |
175 | inline QString dbusInterfaceProperties() |
176 | { return QStringLiteral(DBUS_INTERFACE_PROPERTIES); } |
177 | inline QString dbusInterfaceIntrospectable() |
178 | { return QStringLiteral(DBUS_INTERFACE_INTROSPECTABLE); } |
179 | inline QString nameOwnerChanged() |
180 | { return QStringLiteral("NameOwnerChanged" ); } |
181 | inline QString disconnectedErrorMessage() |
182 | { return QStringLiteral("Not connected to D-Bus server" ); } |
183 | } |
184 | |
185 | QT_END_NAMESPACE |
186 | |
187 | #endif // QT_NO_DBUS |
188 | #endif |
189 | |