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(QStringView busName); |
72 | |
73 | Q_DBUS_EXPORT bool isValidBusName(const QString &busName); |
74 | |
75 | Q_DBUS_EXPORT bool isValidMemberName(QStringView memberName); |
76 | |
77 | Q_DBUS_EXPORT bool isValidErrorName(const QString &errorName); |
78 | |
79 | Q_DBUS_EXPORT bool isValidPartOfObjectPath(QStringView path); |
80 | |
81 | Q_DBUS_EXPORT bool isValidObjectPath(const QString &path); |
82 | |
83 | Q_DBUS_EXPORT bool isValidFixedType(int c); |
84 | |
85 | Q_DBUS_EXPORT bool isValidBasicType(int c); |
86 | |
87 | Q_DBUS_EXPORT bool isValidSignature(const QString &signature); |
88 | |
89 | Q_DBUS_EXPORT bool isValidSingleSignature(const QString &signature); |
90 | |
91 | Q_DBUS_EXPORT QString argumentToString(const QVariant &variant); |
92 | |
93 | enum AllowEmptyFlag { |
94 | EmptyAllowed, |
95 | EmptyNotAllowed |
96 | }; |
97 | |
98 | inline bool checkInterfaceName(const QString &name, AllowEmptyFlag empty, QDBusError *error) |
99 | { |
100 | if (name.isEmpty()) { |
101 | if (empty == EmptyAllowed) return true; |
102 | *error = QDBusError(QDBusError::InvalidInterface, QLatin1String("Interface name cannot be empty" )); |
103 | return false; |
104 | } |
105 | if (isValidInterfaceName(name)) return true; |
106 | *error = QDBusError(QDBusError::InvalidInterface, QLatin1String("Invalid interface class: %1" ).arg(name)); |
107 | return false; |
108 | } |
109 | |
110 | inline bool checkBusName(const QString &name, AllowEmptyFlag empty, QDBusError *error) |
111 | { |
112 | if (name.isEmpty()) { |
113 | if (empty == EmptyAllowed) return true; |
114 | *error = QDBusError(QDBusError::InvalidService, QLatin1String("Service name cannot be empty" )); |
115 | return false; |
116 | } |
117 | if (isValidBusName(name)) return true; |
118 | *error = QDBusError(QDBusError::InvalidService, QLatin1String("Invalid service name: %1" ).arg(name)); |
119 | return false; |
120 | } |
121 | |
122 | inline bool checkObjectPath(const QString &path, AllowEmptyFlag empty, QDBusError *error) |
123 | { |
124 | if (path.isEmpty()) { |
125 | if (empty == EmptyAllowed) return true; |
126 | *error = QDBusError(QDBusError::InvalidObjectPath, QLatin1String("Object path cannot be empty" )); |
127 | return false; |
128 | } |
129 | if (isValidObjectPath(path)) return true; |
130 | *error = QDBusError(QDBusError::InvalidObjectPath, QLatin1String("Invalid object path: %1" ).arg(path)); |
131 | return false; |
132 | } |
133 | |
134 | inline bool checkMemberName(const QString &name, AllowEmptyFlag empty, QDBusError *error, const char *nameType = nullptr) |
135 | { |
136 | if (!nameType) nameType = "member" ; |
137 | if (name.isEmpty()) { |
138 | if (empty == EmptyAllowed) return true; |
139 | *error = QDBusError(QDBusError::InvalidMember, QLatin1String(nameType) + QLatin1String(" name cannot be empty" )); |
140 | return false; |
141 | } |
142 | if (isValidMemberName(name)) return true; |
143 | *error = QDBusError(QDBusError::InvalidMember, QLatin1String("Invalid %1 name: %2" ) |
144 | .arg(QLatin1String(nameType), name)); |
145 | return false; |
146 | } |
147 | |
148 | inline bool checkErrorName(const QString &name, AllowEmptyFlag empty, QDBusError *error) |
149 | { |
150 | if (name.isEmpty()) { |
151 | if (empty == EmptyAllowed) return true; |
152 | *error = QDBusError(QDBusError::InvalidInterface, QLatin1String("Error name cannot be empty" )); |
153 | return false; |
154 | } |
155 | if (isValidErrorName(name)) return true; |
156 | *error = QDBusError(QDBusError::InvalidInterface, QLatin1String("Invalid error name: %1" ).arg(name)); |
157 | return false; |
158 | } |
159 | |
160 | inline QString dbusService() |
161 | { return QStringLiteral(DBUS_SERVICE_DBUS); } |
162 | inline QString dbusPath() |
163 | { return QStringLiteral(DBUS_PATH_DBUS); } |
164 | inline QString dbusPathLocal() |
165 | { return QStringLiteral(DBUS_PATH_LOCAL); } |
166 | inline QString dbusInterface() |
167 | { |
168 | // it's the same string, but just be sure |
169 | Q_ASSERT(dbusService() == QLatin1String(DBUS_INTERFACE_DBUS)); |
170 | return dbusService(); |
171 | } |
172 | inline QString dbusInterfaceProperties() |
173 | { return QStringLiteral(DBUS_INTERFACE_PROPERTIES); } |
174 | inline QString dbusInterfaceIntrospectable() |
175 | { return QStringLiteral(DBUS_INTERFACE_INTROSPECTABLE); } |
176 | inline QString nameOwnerChanged() |
177 | { return QStringLiteral("NameOwnerChanged" ); } |
178 | inline QString disconnectedErrorMessage() |
179 | { return QStringLiteral("Not connected to D-Bus server" ); } |
180 | } |
181 | |
182 | QT_END_NAMESPACE |
183 | |
184 | #endif // QT_NO_DBUS |
185 | #endif |
186 | |