1 | // Copyright (C) 2022 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QPERMISSIONS_H |
5 | #define QPERMISSIONS_H |
6 | |
7 | #if 0 |
8 | #pragma qt_class(QPermissions) |
9 | #endif |
10 | |
11 | #include <QtCore/qglobal.h> |
12 | #include <QtCore/qtmetamacros.h> |
13 | #include <QtCore/qvariant.h> |
14 | |
15 | #include <QtCore/qshareddata_impl.h> |
16 | #include <QtCore/qtypeinfo.h> |
17 | #include <QtCore/qmetatype.h> |
18 | |
19 | #include <optional> |
20 | |
21 | #if !defined(Q_QDOC) |
22 | QT_REQUIRE_CONFIG(permissions); |
23 | #endif |
24 | |
25 | QT_BEGIN_NAMESPACE |
26 | |
27 | #ifndef QT_NO_DEBUG_STREAM |
28 | class QDebug; |
29 | #endif |
30 | |
31 | struct QMetaObject; |
32 | class QCoreApplication; |
33 | |
34 | class QPermission |
35 | { |
36 | template <typename T, typename Enable = void> |
37 | static constexpr inline bool is_permission_v = false; |
38 | |
39 | template <typename T> |
40 | using if_permission = std::enable_if_t<is_permission_v<T>, bool>; |
41 | public: |
42 | explicit QPermission() = default; |
43 | |
44 | template <typename T, if_permission<T> = true> |
45 | QPermission(const T &t) : m_data(QVariant::fromValue(t)) {} |
46 | |
47 | Qt::PermissionStatus status() const { return m_status; } |
48 | |
49 | QMetaType type() const { return m_data.metaType(); } |
50 | |
51 | template <typename T, if_permission<T> = true> |
52 | std::optional<T> value() const |
53 | { |
54 | if (auto p = data(id: QMetaType::fromType<T>())) |
55 | return *static_cast<const T *>(p); |
56 | return std::nullopt; |
57 | } |
58 | |
59 | #ifndef QT_NO_DEBUG_STREAM |
60 | friend Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QPermission &); |
61 | #endif |
62 | |
63 | private: |
64 | Q_CORE_EXPORT const void *data(QMetaType id) const; |
65 | |
66 | Qt::PermissionStatus m_status = Qt::PermissionStatus::Undetermined; |
67 | QVariant m_data; |
68 | |
69 | friend class QCoreApplication; |
70 | }; |
71 | |
72 | template <typename T> |
73 | constexpr bool QPermission::is_permission_v<T, typename T::QtPermissionHelper> = true; |
74 | |
75 | #define QT_PERMISSION(ClassName) \ |
76 | using QtPermissionHelper = void; \ |
77 | friend class QPermission; \ |
78 | union U { \ |
79 | U() : d(nullptr) {} \ |
80 | U(ShortData _data) : data(_data) {} \ |
81 | U(ClassName##Private *_d) : d(_d) {} \ |
82 | ShortData data; \ |
83 | ClassName##Private *d; \ |
84 | } u; \ |
85 | public: \ |
86 | Q_CORE_EXPORT ClassName(); \ |
87 | Q_CORE_EXPORT ClassName(const ClassName &other) noexcept; \ |
88 | ClassName(ClassName &&other) noexcept \ |
89 | : u{other.u} { other.u.d = nullptr; } \ |
90 | Q_CORE_EXPORT ~ClassName(); \ |
91 | Q_CORE_EXPORT ClassName &operator=(const ClassName &other) noexcept; \ |
92 | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(ClassName) \ |
93 | void swap(ClassName &other) noexcept { std::swap(u, other.u); } \ |
94 | private: \ |
95 | /*end*/ |
96 | |
97 | class QLocationPermissionPrivate; |
98 | class QLocationPermission |
99 | { |
100 | Q_GADGET_EXPORT(Q_CORE_EXPORT) |
101 | public: |
102 | enum Accuracy : quint8 { |
103 | Approximate, |
104 | Precise, |
105 | }; |
106 | Q_ENUM(Accuracy) |
107 | |
108 | Q_CORE_EXPORT void setAccuracy(Accuracy accuracy); |
109 | Q_CORE_EXPORT Accuracy accuracy() const; |
110 | |
111 | enum Availability : quint8 { |
112 | WhenInUse, |
113 | Always, |
114 | }; |
115 | Q_ENUM(Availability) |
116 | |
117 | Q_CORE_EXPORT void setAvailability(Availability availability); |
118 | Q_CORE_EXPORT Availability availability() const; |
119 | |
120 | private: |
121 | struct ShortData { |
122 | Accuracy accuracy; |
123 | Availability availability; |
124 | char reserved[sizeof(void*) - sizeof(accuracy) - sizeof(availability)]; |
125 | }; |
126 | QT_PERMISSION(QLocationPermission) |
127 | }; |
128 | Q_DECLARE_SHARED(QLocationPermission) |
129 | |
130 | class QCalendarPermissionPrivate; |
131 | class QCalendarPermission |
132 | { |
133 | Q_GADGET_EXPORT(Q_CORE_EXPORT) |
134 | public: |
135 | enum AccessMode : quint8 { |
136 | ReadOnly, |
137 | ReadWrite, |
138 | }; |
139 | Q_ENUM(AccessMode) |
140 | |
141 | Q_CORE_EXPORT void setAccessMode(AccessMode mode); |
142 | Q_CORE_EXPORT AccessMode accessMode() const; |
143 | |
144 | private: |
145 | struct ShortData { |
146 | AccessMode mode; |
147 | char reserved[sizeof(void*) - sizeof(mode)]; |
148 | }; |
149 | QT_PERMISSION(QCalendarPermission) |
150 | }; |
151 | Q_DECLARE_SHARED(QCalendarPermission) |
152 | |
153 | class QContactsPermissionPrivate; |
154 | class QContactsPermission |
155 | { |
156 | Q_GADGET_EXPORT(Q_CORE_EXPORT) |
157 | public: |
158 | enum AccessMode : quint8 { |
159 | ReadOnly, |
160 | ReadWrite, |
161 | }; |
162 | Q_ENUM(AccessMode) |
163 | |
164 | Q_CORE_EXPORT void setAccessMode(AccessMode mode); |
165 | Q_CORE_EXPORT AccessMode accessMode() const; |
166 | |
167 | private: |
168 | struct ShortData { |
169 | AccessMode mode; |
170 | char reserved[sizeof(void*) - sizeof(mode)]; |
171 | }; |
172 | QT_PERMISSION(QContactsPermission) |
173 | }; |
174 | Q_DECLARE_SHARED(QContactsPermission) |
175 | |
176 | class QBluetoothPermissionPrivate; |
177 | class QBluetoothPermission |
178 | { |
179 | Q_GADGET_EXPORT(Q_CORE_EXPORT) |
180 | public: |
181 | enum CommunicationMode : quint8 { |
182 | Access = 0x01, |
183 | Advertise = 0x02, |
184 | Default = Access | Advertise, |
185 | }; |
186 | Q_DECLARE_FLAGS(CommunicationModes, CommunicationMode) |
187 | Q_FLAG(CommunicationModes) |
188 | |
189 | Q_CORE_EXPORT void setCommunicationModes(CommunicationModes modes); |
190 | Q_CORE_EXPORT CommunicationModes communicationModes() const; |
191 | |
192 | private: |
193 | struct ShortData { |
194 | CommunicationMode mode; |
195 | char reserved[sizeof(void*) - sizeof(mode)]; |
196 | }; |
197 | QT_PERMISSION(QBluetoothPermission) |
198 | }; |
199 | Q_DECLARE_OPERATORS_FOR_FLAGS(QBluetoothPermission::CommunicationModes) |
200 | Q_DECLARE_SHARED(QBluetoothPermission) |
201 | |
202 | #define Q_DECLARE_MINIMAL_PERMISSION(ClassName) \ |
203 | class ClassName##Private; \ |
204 | class ClassName \ |
205 | { \ |
206 | struct ShortData { char reserved[sizeof(void*)]; }; \ |
207 | QT_PERMISSION(ClassName) \ |
208 | }; \ |
209 | Q_DECLARE_SHARED(ClassName) |
210 | |
211 | Q_DECLARE_MINIMAL_PERMISSION(QCameraPermission) |
212 | Q_DECLARE_MINIMAL_PERMISSION(QMicrophonePermission) |
213 | |
214 | #undef QT_PERMISSION |
215 | #undef Q_DECLARE_MINIMAL_PERMISSION |
216 | |
217 | QT_END_NAMESPACE |
218 | |
219 | #endif // QPERMISSIONS_H |
220 | |