1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5//
6// W A R N I N G
7// -------------
8//
9// This file is not part of the Qt API. It exists for the convenience
10// of the QLibrary class. This header file may change from
11// version to version without notice, or even be removed.
12//
13// We mean it.
14//
15
16#ifndef QDBUSUTIL_P_H
17#define QDBUSUTIL_P_H
18
19#include <QtDBus/private/qtdbusglobal_p.h>
20#include <QtDBus/qdbuserror.h>
21#include <QtCore/qstring.h>
22#include <QtCore/qvariant.h>
23
24#include "qdbus_symbols_p.h"
25
26#ifndef QT_NO_DBUS
27
28QT_BEGIN_NAMESPACE
29
30#define Q_DBUS_NO_EXPORT // force syncqt looking into this namespace
31namespace Q_DBUS_NO_EXPORT QDBusUtil
32{
33 Q_DBUS_EXPORT bool isValidInterfaceName(const QString &ifaceName);
34
35 Q_DBUS_EXPORT bool isValidUniqueConnectionName(QStringView busName);
36
37 Q_DBUS_EXPORT bool isValidBusName(const QString &busName);
38
39 Q_DBUS_EXPORT bool isValidMemberName(QStringView memberName);
40
41 Q_DBUS_EXPORT bool isValidErrorName(const QString &errorName);
42
43 Q_DBUS_EXPORT bool isValidPartOfObjectPath(QStringView path);
44
45 Q_DBUS_EXPORT bool isValidObjectPath(const QString &path);
46
47 Q_DBUS_EXPORT bool isValidFixedType(int c);
48
49 Q_DBUS_EXPORT bool isValidBasicType(int c);
50
51 Q_DBUS_EXPORT bool isValidSignature(const QString &signature);
52
53 Q_DBUS_EXPORT bool isValidSingleSignature(const QString &signature);
54
55 Q_DBUS_EXPORT QString argumentToString(const QVariant &variant);
56
57 enum AllowEmptyFlag {
58 EmptyAllowed,
59 EmptyNotAllowed
60 };
61
62 inline bool checkInterfaceName(const QString &name, AllowEmptyFlag empty, QDBusError *error)
63 {
64 if (name.isEmpty()) {
65 if (empty == EmptyAllowed) return true;
66 *error = QDBusError(QDBusError::InvalidInterface, QLatin1StringView("Interface name cannot be empty"));
67 return false;
68 }
69 if (isValidInterfaceName(ifaceName: name)) return true;
70 *error = QDBusError(QDBusError::InvalidInterface, QLatin1StringView("Invalid interface class: %1").arg(args: name));
71 return false;
72 }
73
74 inline bool checkBusName(const QString &name, AllowEmptyFlag empty, QDBusError *error)
75 {
76 if (name.isEmpty()) {
77 if (empty == EmptyAllowed) return true;
78 *error = QDBusError(QDBusError::InvalidService, QLatin1StringView("Service name cannot be empty"));
79 return false;
80 }
81 if (isValidBusName(busName: name)) return true;
82 *error = QDBusError(QDBusError::InvalidService, QLatin1StringView("Invalid service name: %1").arg(args: name));
83 return false;
84 }
85
86 inline bool checkObjectPath(const QString &path, AllowEmptyFlag empty, QDBusError *error)
87 {
88 if (path.isEmpty()) {
89 if (empty == EmptyAllowed) return true;
90 *error = QDBusError(QDBusError::InvalidObjectPath, QLatin1StringView("Object path cannot be empty"));
91 return false;
92 }
93 if (isValidObjectPath(path)) return true;
94 *error = QDBusError(QDBusError::InvalidObjectPath, QLatin1StringView("Invalid object path: %1").arg(args: path));
95 return false;
96 }
97
98 inline bool checkMemberName(const QString &name, AllowEmptyFlag empty, QDBusError *error, const char *nameType = nullptr)
99 {
100 if (!nameType) nameType = "member";
101 if (name.isEmpty()) {
102 if (empty == EmptyAllowed) return true;
103 *error = QDBusError(QDBusError::InvalidMember, QLatin1StringView(nameType) + QLatin1StringView(" name cannot be empty"));
104 return false;
105 }
106 if (isValidMemberName(memberName: name)) return true;
107 *error = QDBusError(QDBusError::InvalidMember, QLatin1StringView("Invalid %1 name: %2")
108 .arg(args: QLatin1StringView(nameType), args: name));
109 return false;
110 }
111
112 inline bool checkErrorName(const QString &name, AllowEmptyFlag empty, QDBusError *error)
113 {
114 if (name.isEmpty()) {
115 if (empty == EmptyAllowed) return true;
116 *error = QDBusError(QDBusError::InvalidInterface, QLatin1StringView("Error name cannot be empty"));
117 return false;
118 }
119 if (isValidErrorName(errorName: name)) return true;
120 *error = QDBusError(QDBusError::InvalidInterface, QLatin1StringView("Invalid error name: %1").arg(args: name));
121 return false;
122 }
123
124 inline QString dbusService()
125 { return QStringLiteral(DBUS_SERVICE_DBUS); }
126 inline QString dbusPath()
127 { return QStringLiteral(DBUS_PATH_DBUS); }
128 inline QString dbusPathLocal()
129 { return QStringLiteral(DBUS_PATH_LOCAL); }
130 inline QString dbusInterface()
131 {
132 // it's the same string, but just be sure
133 Q_ASSERT(dbusService() == QLatin1StringView(DBUS_INTERFACE_DBUS));
134 return dbusService();
135 }
136 inline QString dbusInterfaceProperties()
137 { return QStringLiteral(DBUS_INTERFACE_PROPERTIES); }
138 inline QString dbusInterfaceIntrospectable()
139 { return QStringLiteral(DBUS_INTERFACE_INTROSPECTABLE); }
140 inline QString nameOwnerChanged()
141 { return QStringLiteral("NameOwnerChanged"); }
142 inline QString disconnectedErrorMessage()
143 { return QStringLiteral("Not connected to D-Bus server"); }
144}
145
146QT_END_NAMESPACE
147
148#endif // QT_NO_DBUS
149#endif
150

source code of qtbase/src/dbus/qdbusutil_p.h