1 | // Copyright (C) 2020 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 | #ifndef QURL_H |
6 | #define QURL_H |
7 | |
8 | #include <QtCore/qbytearray.h> |
9 | #include <QtCore/qobjectdefs.h> |
10 | #include <QtCore/qstring.h> |
11 | #include <QtCore/qlist.h> |
12 | #include <QtCore/qglobal.h> |
13 | |
14 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
15 | Q_FORWARD_DECLARE_CF_TYPE(CFURL); |
16 | Q_FORWARD_DECLARE_OBJC_CLASS(NSURL); |
17 | #endif |
18 | |
19 | QT_BEGIN_NAMESPACE |
20 | |
21 | |
22 | class QUrlQuery; |
23 | class QUrlPrivate; |
24 | class QDataStream; |
25 | |
26 | template <typename E1, typename E2> |
27 | class QUrlTwoFlags |
28 | { |
29 | int i; |
30 | public: |
31 | constexpr inline QUrlTwoFlags() : i(0) {} |
32 | constexpr inline QUrlTwoFlags(E1 f) : i(f) {} |
33 | constexpr inline QUrlTwoFlags(E2 f) : i(f) {} |
34 | constexpr inline QUrlTwoFlags(QFlag f) : i(f) {} |
35 | constexpr inline QUrlTwoFlags(QFlags<E1> f) : i(f.operator typename QFlags<E1>::Int()) {} |
36 | constexpr inline QUrlTwoFlags(QFlags<E2> f) : i(f.operator typename QFlags<E2>::Int()) {} |
37 | |
38 | inline QUrlTwoFlags &operator&=(int mask) { i &= mask; return *this; } |
39 | inline QUrlTwoFlags &operator&=(uint mask) { i &= mask; return *this; } |
40 | inline QUrlTwoFlags &operator&=(QFlags<E1> mask) { i &= mask.toInt(); return *this; } |
41 | inline QUrlTwoFlags &operator&=(QFlags<E2> mask) { i &= mask.toInt(); return *this; } |
42 | inline QUrlTwoFlags &operator|=(QUrlTwoFlags f) { i |= f.i; return *this; } |
43 | inline QUrlTwoFlags &operator|=(E1 f) { i |= f; return *this; } |
44 | inline QUrlTwoFlags &operator|=(E2 f) { i |= f; return *this; } |
45 | inline QUrlTwoFlags &operator|=(QFlags<E1> mask) { i |= mask.toInt(); return *this; } |
46 | inline QUrlTwoFlags &operator|=(QFlags<E2> mask) { i |= mask.toInt(); return *this; } |
47 | inline QUrlTwoFlags &operator^=(QUrlTwoFlags f) { i ^= f.i; return *this; } |
48 | inline QUrlTwoFlags &operator^=(E1 f) { i ^= f; return *this; } |
49 | inline QUrlTwoFlags &operator^=(E2 f) { i ^= f; return *this; } |
50 | inline QUrlTwoFlags &operator^=(QFlags<E1> mask) { i ^= mask.toInt(); return *this; } |
51 | inline QUrlTwoFlags &operator^=(QFlags<E2> mask) { i ^= mask.toInt(); return *this; } |
52 | |
53 | constexpr inline operator QFlags<E1>() const { return QFlag(i); } |
54 | constexpr inline operator QFlags<E2>() const { return QFlag(i); } |
55 | constexpr inline operator int() const { return i; } |
56 | constexpr inline bool operator!() const { return !i; } |
57 | |
58 | constexpr inline QUrlTwoFlags operator|(QUrlTwoFlags f) const |
59 | { return QUrlTwoFlags(QFlag(i | f.i)); } |
60 | constexpr inline QUrlTwoFlags operator|(E1 f) const |
61 | { return QUrlTwoFlags(QFlag(i | f)); } |
62 | constexpr inline QUrlTwoFlags operator|(E2 f) const |
63 | { return QUrlTwoFlags(QFlag(i | f)); } |
64 | constexpr inline QUrlTwoFlags operator^(QUrlTwoFlags f) const |
65 | { return QUrlTwoFlags(QFlag(i ^ f.i)); } |
66 | constexpr inline QUrlTwoFlags operator^(E1 f) const |
67 | { return QUrlTwoFlags(QFlag(i ^ f)); } |
68 | constexpr inline QUrlTwoFlags operator^(E2 f) const |
69 | { return QUrlTwoFlags(QFlag(i ^ f)); } |
70 | constexpr inline QUrlTwoFlags operator&(int mask) const |
71 | { return QUrlTwoFlags(QFlag(i & mask)); } |
72 | constexpr inline QUrlTwoFlags operator&(uint mask) const |
73 | { return QUrlTwoFlags(QFlag(i & mask)); } |
74 | constexpr inline QUrlTwoFlags operator&(E1 f) const |
75 | { return QUrlTwoFlags(QFlag(i & f)); } |
76 | constexpr inline QUrlTwoFlags operator&(E2 f) const |
77 | { return QUrlTwoFlags(QFlag(i & f)); } |
78 | constexpr inline QUrlTwoFlags operator~() const |
79 | { return QUrlTwoFlags(QFlag(~i)); } |
80 | |
81 | constexpr inline bool testFlag(E1 f) const { return (i & f) == f && (f != 0 || i == int(f)); } |
82 | constexpr inline bool testFlag(E2 f) const { return (i & f) == f && (f != 0 || i == int(f)); } |
83 | }; |
84 | |
85 | template<typename E1, typename E2> |
86 | class QTypeInfo<QUrlTwoFlags<E1, E2> > : public QTypeInfoMerger<QUrlTwoFlags<E1, E2>, E1, E2> {}; |
87 | |
88 | class QUrl; |
89 | // qHash is a friend, but we can't use default arguments for friends (ยง8.3.6.4) |
90 | Q_CORE_EXPORT size_t qHash(const QUrl &url, size_t seed = 0) noexcept; |
91 | |
92 | class Q_CORE_EXPORT QUrl |
93 | { |
94 | public: |
95 | enum ParsingMode { |
96 | TolerantMode, |
97 | StrictMode, |
98 | DecodedMode |
99 | }; |
100 | |
101 | // encoding / toString values |
102 | enum UrlFormattingOption : unsigned int { |
103 | None = 0x0, |
104 | RemoveScheme = 0x1, |
105 | RemovePassword = 0x2, |
106 | RemoveUserInfo = RemovePassword | 0x4, |
107 | RemovePort = 0x8, |
108 | RemoveAuthority = RemoveUserInfo | RemovePort | 0x10, |
109 | RemovePath = 0x20, |
110 | RemoveQuery = 0x40, |
111 | RemoveFragment = 0x80, |
112 | // 0x100 was a private code in Qt 4, keep unused for a while |
113 | PreferLocalFile = 0x200, |
114 | StripTrailingSlash = 0x400, |
115 | RemoveFilename = 0x800, |
116 | NormalizePathSegments = 0x1000 |
117 | }; |
118 | |
119 | enum ComponentFormattingOption : unsigned int { |
120 | PrettyDecoded = 0x000000, |
121 | EncodeSpaces = 0x100000, |
122 | EncodeUnicode = 0x200000, |
123 | EncodeDelimiters = 0x400000 | 0x800000, |
124 | EncodeReserved = 0x1000000, |
125 | DecodeReserved = 0x2000000, |
126 | // 0x4000000 used to indicate full-decode mode |
127 | |
128 | FullyEncoded = EncodeSpaces | EncodeUnicode | EncodeDelimiters | EncodeReserved, |
129 | FullyDecoded = FullyEncoded | DecodeReserved | 0x4000000 |
130 | }; |
131 | Q_DECLARE_FLAGS(ComponentFormattingOptions, ComponentFormattingOption) |
132 | #ifdef Q_QDOC |
133 | private: |
134 | // We need to let qdoc think that FormattingOptions is a normal QFlags, but |
135 | // it needs to be a QUrlTwoFlags for compiling default arguments of some functions. |
136 | template<typename T> struct QFlags : QUrlTwoFlags<T, ComponentFormattingOption> |
137 | { using QUrlTwoFlags<T, ComponentFormattingOption>::QUrlTwoFlags; }; |
138 | public: |
139 | Q_DECLARE_FLAGS(FormattingOptions, UrlFormattingOption) |
140 | #else |
141 | typedef QUrlTwoFlags<UrlFormattingOption, ComponentFormattingOption> FormattingOptions; |
142 | #endif |
143 | |
144 | QUrl(); |
145 | QUrl(const QUrl ©) noexcept; |
146 | QUrl &operator =(const QUrl ©) noexcept; |
147 | #ifdef QT_NO_URL_CAST_FROM_STRING |
148 | explicit QUrl(const QString &url, ParsingMode mode = TolerantMode); |
149 | #else |
150 | QUrl(const QString &url, ParsingMode mode = TolerantMode); |
151 | QUrl &operator=(const QString &url); |
152 | #endif |
153 | QUrl(QUrl &&other) noexcept : d(other.d) |
154 | { other.d = nullptr; } |
155 | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QUrl) |
156 | ~QUrl(); |
157 | |
158 | void swap(QUrl &other) noexcept { qt_ptr_swap(lhs&: d, rhs&: other.d); } |
159 | |
160 | void setUrl(const QString &url, ParsingMode mode = TolerantMode); |
161 | QString url(FormattingOptions options = FormattingOptions(PrettyDecoded)) const; |
162 | QString toString(FormattingOptions options = FormattingOptions(PrettyDecoded)) const; |
163 | QString toDisplayString(FormattingOptions options = FormattingOptions(PrettyDecoded)) const; |
164 | [[nodiscard]] QUrl adjusted(FormattingOptions options) const; |
165 | |
166 | QByteArray toEncoded(FormattingOptions options = FullyEncoded) const; |
167 | #if QT_CORE_REMOVED_SINCE(6, 7) |
168 | static QUrl fromEncoded(const QByteArray &url, ParsingMode mode = TolerantMode); |
169 | #endif |
170 | static QUrl fromEncoded(QByteArrayView input, ParsingMode mode = TolerantMode); |
171 | |
172 | enum UserInputResolutionOption { |
173 | DefaultResolution, |
174 | AssumeLocalFile |
175 | }; |
176 | Q_DECLARE_FLAGS(UserInputResolutionOptions, UserInputResolutionOption) |
177 | |
178 | static QUrl fromUserInput(const QString &userInput, const QString &workingDirectory = QString(), |
179 | UserInputResolutionOptions options = DefaultResolution); |
180 | |
181 | bool isValid() const; |
182 | QString errorString() const; |
183 | |
184 | bool isEmpty() const; |
185 | void clear(); |
186 | |
187 | void setScheme(const QString &scheme); |
188 | QString scheme() const; |
189 | |
190 | void setAuthority(const QString &authority, ParsingMode mode = TolerantMode); |
191 | QString authority(ComponentFormattingOptions options = PrettyDecoded) const; |
192 | |
193 | void setUserInfo(const QString &userInfo, ParsingMode mode = TolerantMode); |
194 | QString userInfo(ComponentFormattingOptions options = PrettyDecoded) const; |
195 | |
196 | void setUserName(const QString &userName, ParsingMode mode = DecodedMode); |
197 | QString userName(ComponentFormattingOptions options = FullyDecoded) const; |
198 | |
199 | void setPassword(const QString &password, ParsingMode mode = DecodedMode); |
200 | QString password(ComponentFormattingOptions = FullyDecoded) const; |
201 | |
202 | void setHost(const QString &host, ParsingMode mode = DecodedMode); |
203 | QString host(ComponentFormattingOptions = FullyDecoded) const; |
204 | |
205 | void setPort(int port); |
206 | int port(int defaultPort = -1) const; |
207 | |
208 | void setPath(const QString &path, ParsingMode mode = DecodedMode); |
209 | QString path(ComponentFormattingOptions options = FullyDecoded) const; |
210 | QString fileName(ComponentFormattingOptions options = FullyDecoded) const; |
211 | |
212 | bool hasQuery() const; |
213 | void setQuery(const QString &query, ParsingMode mode = TolerantMode); |
214 | void setQuery(const QUrlQuery &query); |
215 | QString query(ComponentFormattingOptions = PrettyDecoded) const; |
216 | |
217 | bool hasFragment() const; |
218 | QString fragment(ComponentFormattingOptions options = PrettyDecoded) const; |
219 | void setFragment(const QString &fragment, ParsingMode mode = TolerantMode); |
220 | |
221 | [[nodiscard]] QUrl resolved(const QUrl &relative) const; |
222 | |
223 | bool isRelative() const; |
224 | bool isParentOf(const QUrl &url) const; |
225 | |
226 | bool isLocalFile() const; |
227 | static QUrl fromLocalFile(const QString &localfile); |
228 | QString toLocalFile() const; |
229 | |
230 | void detach(); |
231 | bool isDetached() const; |
232 | |
233 | bool operator <(const QUrl &url) const; |
234 | bool operator ==(const QUrl &url) const; |
235 | bool operator !=(const QUrl &url) const; |
236 | |
237 | bool matches(const QUrl &url, FormattingOptions options) const; |
238 | |
239 | static QString fromPercentEncoding(const QByteArray &); |
240 | static QByteArray toPercentEncoding(const QString &, |
241 | const QByteArray &exclude = QByteArray(), |
242 | const QByteArray &include = QByteArray()); |
243 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
244 | static QUrl fromCFURL(CFURLRef url); |
245 | CFURLRef toCFURL() const Q_DECL_CF_RETURNS_RETAINED; |
246 | static QUrl fromNSURL(const NSURL *url); |
247 | NSURL *toNSURL() const Q_DECL_NS_RETURNS_AUTORELEASED; |
248 | #endif |
249 | |
250 | enum AceProcessingOption : unsigned int { |
251 | IgnoreIDNWhitelist = 0x1, |
252 | AceTransitionalProcessing = 0x2, |
253 | }; |
254 | Q_DECLARE_FLAGS(AceProcessingOptions, AceProcessingOption) |
255 | |
256 | #if QT_CORE_REMOVED_SINCE(6, 3) |
257 | static QString fromAce(const QByteArray &); |
258 | static QByteArray toAce(const QString &); |
259 | #endif |
260 | static QString fromAce(const QByteArray &domain, AceProcessingOptions options = {}); |
261 | static QByteArray toAce(const QString &domain, AceProcessingOptions options = {}); |
262 | |
263 | static QStringList idnWhitelist(); |
264 | static QStringList toStringList(const QList<QUrl> &uris, FormattingOptions options = FormattingOptions(PrettyDecoded)); |
265 | static QList<QUrl> fromStringList(const QStringList &uris, ParsingMode mode = TolerantMode); |
266 | |
267 | static void setIdnWhitelist(const QStringList &); |
268 | friend Q_CORE_EXPORT size_t qHash(const QUrl &url, size_t seed) noexcept; |
269 | |
270 | private: |
271 | QUrlPrivate *d; |
272 | friend class QUrlQuery; |
273 | |
274 | public: |
275 | typedef QUrlPrivate * DataPtr; |
276 | inline DataPtr &data_ptr() { return d; } |
277 | }; |
278 | |
279 | Q_DECLARE_SHARED(QUrl) |
280 | Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::ComponentFormattingOptions) |
281 | //Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::FormattingOptions) |
282 | Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::AceProcessingOptions) |
283 | |
284 | #ifndef Q_QDOC |
285 | constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::UrlFormattingOption f2) |
286 | { return QUrl::FormattingOptions(f1) | f2; } |
287 | constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::FormattingOptions f2) |
288 | { return f2 | f1; } |
289 | constexpr inline QIncompatibleFlag operator|(QUrl::UrlFormattingOption f1, int f2) |
290 | { return QIncompatibleFlag(uint(f1) | f2); } |
291 | |
292 | // add operators for OR'ing the two types of flags |
293 | inline QUrl::FormattingOptions &operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOptions f) |
294 | { i |= QUrl::UrlFormattingOption(f.toInt()); return i; } |
295 | constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOption f) |
296 | { return i | QUrl::UrlFormattingOption(qToUnderlying(e: f)); } |
297 | constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOptions f) |
298 | { return i | QUrl::UrlFormattingOption(f.toInt()); } |
299 | constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOption f, QUrl::UrlFormattingOption i) |
300 | { return i | QUrl::UrlFormattingOption(qToUnderlying(e: f)); } |
301 | constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOptions f, QUrl::UrlFormattingOption i) |
302 | { return i | QUrl::UrlFormattingOption(f.toInt()); } |
303 | constexpr inline QUrl::FormattingOptions operator|(QUrl::FormattingOptions i, QUrl::ComponentFormattingOptions f) |
304 | { return i | QUrl::UrlFormattingOption(f.toInt()); } |
305 | constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOption f, QUrl::FormattingOptions i) |
306 | { return i | QUrl::UrlFormattingOption(qToUnderlying(e: f)); } |
307 | constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOptions f, QUrl::FormattingOptions i) |
308 | { return i | QUrl::UrlFormattingOption(f.toInt()); } |
309 | |
310 | //inline QUrl::UrlFormattingOption &operator=(const QUrl::UrlFormattingOption &i, QUrl::ComponentFormattingOptions f) |
311 | //{ i = int(f); f; } |
312 | #endif // Q_QDOC |
313 | |
314 | #ifndef QT_NO_DATASTREAM |
315 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QUrl &); |
316 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QUrl &); |
317 | #endif |
318 | |
319 | #ifndef QT_NO_DEBUG_STREAM |
320 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QUrl &); |
321 | #endif |
322 | |
323 | QT_END_NAMESPACE |
324 | |
325 | #endif // QURL_H |
326 | |