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 QtCore 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#ifndef QURL_H
42#define QURL_H
43
44#include <QtCore/qbytearray.h>
45#include <QtCore/qobjectdefs.h>
46#include <QtCore/qstring.h>
47#include <QtCore/qlist.h>
48#include <QtCore/qpair.h>
49#include <QtCore/qglobal.h>
50
51#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
52Q_FORWARD_DECLARE_CF_TYPE(CFURL);
53Q_FORWARD_DECLARE_OBJC_CLASS(NSURL);
54#endif
55
56QT_BEGIN_NAMESPACE
57
58
59class QUrlQuery;
60class QUrlPrivate;
61class QDataStream;
62
63template <typename E1, typename E2>
64class QUrlTwoFlags
65{
66 int i;
67 typedef int QUrlTwoFlags:: *Zero;
68public:
69 Q_DECL_CONSTEXPR inline QUrlTwoFlags(E1 f) : i(f) {}
70 Q_DECL_CONSTEXPR inline QUrlTwoFlags(E2 f) : i(f) {}
71 Q_DECL_CONSTEXPR inline QUrlTwoFlags(QFlag f) : i(f) {}
72 Q_DECL_CONSTEXPR inline QUrlTwoFlags(QFlags<E1> f) : i(f.operator typename QFlags<E1>::Int()) {}
73 Q_DECL_CONSTEXPR inline QUrlTwoFlags(QFlags<E2> f) : i(f.operator typename QFlags<E2>::Int()) {}
74 Q_DECL_CONSTEXPR inline QUrlTwoFlags(Zero = 0) : i(0) {}
75
76 inline QUrlTwoFlags &operator&=(int mask) { i &= mask; return *this; }
77 inline QUrlTwoFlags &operator&=(uint mask) { i &= mask; return *this; }
78 inline QUrlTwoFlags &operator|=(QUrlTwoFlags f) { i |= f.i; return *this; }
79 inline QUrlTwoFlags &operator|=(E1 f) { i |= f; return *this; }
80 inline QUrlTwoFlags &operator|=(E2 f) { i |= f; return *this; }
81 inline QUrlTwoFlags &operator^=(QUrlTwoFlags f) { i ^= f.i; return *this; }
82 inline QUrlTwoFlags &operator^=(E1 f) { i ^= f; return *this; }
83 inline QUrlTwoFlags &operator^=(E2 f) { i ^= f; return *this; }
84
85 Q_DECL_CONSTEXPR inline operator QFlags<E1>() const { return QFlag(i); }
86 Q_DECL_CONSTEXPR inline operator QFlags<E2>() const { return QFlag(i); }
87 Q_DECL_CONSTEXPR inline operator int() const { return i; }
88 Q_DECL_CONSTEXPR inline bool operator!() const { return !i; }
89
90 Q_DECL_CONSTEXPR inline QUrlTwoFlags operator|(QUrlTwoFlags f) const
91 { return QUrlTwoFlags(QFlag(i | f.i)); }
92 Q_DECL_CONSTEXPR inline QUrlTwoFlags operator|(E1 f) const
93 { return QUrlTwoFlags(QFlag(i | f)); }
94 Q_DECL_CONSTEXPR inline QUrlTwoFlags operator|(E2 f) const
95 { return QUrlTwoFlags(QFlag(i | f)); }
96 Q_DECL_CONSTEXPR inline QUrlTwoFlags operator^(QUrlTwoFlags f) const
97 { return QUrlTwoFlags(QFlag(i ^ f.i)); }
98 Q_DECL_CONSTEXPR inline QUrlTwoFlags operator^(E1 f) const
99 { return QUrlTwoFlags(QFlag(i ^ f)); }
100 Q_DECL_CONSTEXPR inline QUrlTwoFlags operator^(E2 f) const
101 { return QUrlTwoFlags(QFlag(i ^ f)); }
102 Q_DECL_CONSTEXPR inline QUrlTwoFlags operator&(int mask) const
103 { return QUrlTwoFlags(QFlag(i & mask)); }
104 Q_DECL_CONSTEXPR inline QUrlTwoFlags operator&(uint mask) const
105 { return QUrlTwoFlags(QFlag(i & mask)); }
106 Q_DECL_CONSTEXPR inline QUrlTwoFlags operator&(E1 f) const
107 { return QUrlTwoFlags(QFlag(i & f)); }
108 Q_DECL_CONSTEXPR inline QUrlTwoFlags operator&(E2 f) const
109 { return QUrlTwoFlags(QFlag(i & f)); }
110 Q_DECL_CONSTEXPR inline QUrlTwoFlags operator~() const
111 { return QUrlTwoFlags(QFlag(~i)); }
112
113 Q_DECL_CONSTEXPR inline bool testFlag(E1 f) const { return (i & f) == f && (f != 0 || i == int(f)); }
114 Q_DECL_CONSTEXPR inline bool testFlag(E2 f) const { return (i & f) == f && (f != 0 || i == int(f)); }
115};
116
117template<typename E1, typename E2>
118class QTypeInfo<QUrlTwoFlags<E1, E2> > : public QTypeInfoMerger<QUrlTwoFlags<E1, E2>, E1, E2> {};
119
120class QUrl;
121// qHash is a friend, but we can't use default arguments for friends (ยง8.3.6.4)
122Q_CORE_EXPORT uint qHash(const QUrl &url, uint seed = 0) noexcept;
123
124class Q_CORE_EXPORT QUrl
125{
126public:
127 enum ParsingMode {
128 TolerantMode,
129 StrictMode,
130 DecodedMode
131 };
132
133 // encoding / toString values
134 enum UrlFormattingOption {
135 None = 0x0,
136 RemoveScheme = 0x1,
137 RemovePassword = 0x2,
138 RemoveUserInfo = RemovePassword | 0x4,
139 RemovePort = 0x8,
140 RemoveAuthority = RemoveUserInfo | RemovePort | 0x10,
141 RemovePath = 0x20,
142 RemoveQuery = 0x40,
143 RemoveFragment = 0x80,
144 // 0x100 was a private code in Qt 4, keep unused for a while
145 PreferLocalFile = 0x200,
146 StripTrailingSlash = 0x400,
147 RemoveFilename = 0x800,
148 NormalizePathSegments = 0x1000
149 };
150
151 enum ComponentFormattingOption {
152 PrettyDecoded = 0x000000,
153 EncodeSpaces = 0x100000,
154 EncodeUnicode = 0x200000,
155 EncodeDelimiters = 0x400000 | 0x800000,
156 EncodeReserved = 0x1000000,
157 DecodeReserved = 0x2000000,
158 // 0x4000000 used to indicate full-decode mode
159
160 FullyEncoded = EncodeSpaces | EncodeUnicode | EncodeDelimiters | EncodeReserved,
161 FullyDecoded = FullyEncoded | DecodeReserved | 0x4000000
162 };
163 Q_DECLARE_FLAGS(ComponentFormattingOptions, ComponentFormattingOption)
164#ifdef Q_QDOC
165private:
166 // We need to let qdoc think that FormattingOptions is a normal QFlags, but
167 // it needs to be a QUrlTwoFlags for compiling default arguments of somme functions.
168 template<typename T> struct QFlags : QUrlTwoFlags<T, ComponentFormattingOption>
169 { using QUrlTwoFlags<T, ComponentFormattingOption>::QUrlTwoFlags; };
170public:
171 Q_DECLARE_FLAGS(FormattingOptions, UrlFormattingOption)
172#else
173 typedef QUrlTwoFlags<UrlFormattingOption, ComponentFormattingOption> FormattingOptions;
174#endif
175
176 QUrl();
177 QUrl(const QUrl &copy);
178 QUrl &operator =(const QUrl &copy);
179#ifdef QT_NO_URL_CAST_FROM_STRING
180 explicit QUrl(const QString &url, ParsingMode mode = TolerantMode);
181#else
182 QUrl(const QString &url, ParsingMode mode = TolerantMode);
183 QUrl &operator=(const QString &url);
184#endif
185 QUrl(QUrl &&other) noexcept : d(other.d)
186 { other.d = nullptr; }
187 inline QUrl &operator=(QUrl &&other) noexcept
188 { qSwap(d, other.d); return *this; }
189 ~QUrl();
190
191 inline void swap(QUrl &other) noexcept { qSwap(d, other.d); }
192
193 void setUrl(const QString &url, ParsingMode mode = TolerantMode);
194 QString url(FormattingOptions options = FormattingOptions(PrettyDecoded)) const;
195 QString toString(FormattingOptions options = FormattingOptions(PrettyDecoded)) const;
196 QString toDisplayString(FormattingOptions options = FormattingOptions(PrettyDecoded)) const;
197 Q_REQUIRED_RESULT QUrl adjusted(FormattingOptions options) const;
198
199 QByteArray toEncoded(FormattingOptions options = FullyEncoded) const;
200 static QUrl fromEncoded(const QByteArray &url, ParsingMode mode = TolerantMode);
201
202 enum UserInputResolutionOption {
203 DefaultResolution,
204 AssumeLocalFile
205 };
206 Q_DECLARE_FLAGS(UserInputResolutionOptions, UserInputResolutionOption)
207
208 static QUrl fromUserInput(const QString &userInput);
209 // ### Qt6 merge with fromUserInput(QString), by adding = QString()
210 static QUrl fromUserInput(const QString &userInput, const QString &workingDirectory,
211 UserInputResolutionOptions options = DefaultResolution);
212
213 bool isValid() const;
214 QString errorString() const;
215
216 bool isEmpty() const;
217 void clear();
218
219 void setScheme(const QString &scheme);
220 QString scheme() const;
221
222 void setAuthority(const QString &authority, ParsingMode mode = TolerantMode);
223 QString authority(ComponentFormattingOptions options = PrettyDecoded) const;
224
225 void setUserInfo(const QString &userInfo, ParsingMode mode = TolerantMode);
226 QString userInfo(ComponentFormattingOptions options = PrettyDecoded) const;
227
228 void setUserName(const QString &userName, ParsingMode mode = DecodedMode);
229 QString userName(ComponentFormattingOptions options = FullyDecoded) const;
230
231 void setPassword(const QString &password, ParsingMode mode = DecodedMode);
232 QString password(ComponentFormattingOptions = FullyDecoded) const;
233
234 void setHost(const QString &host, ParsingMode mode = DecodedMode);
235 QString host(ComponentFormattingOptions = FullyDecoded) const;
236#if QT_CONFIG(topleveldomain)
237 QString topLevelDomain(ComponentFormattingOptions options = FullyDecoded) const;
238#endif
239
240 void setPort(int port);
241 int port(int defaultPort = -1) const;
242
243 void setPath(const QString &path, ParsingMode mode = DecodedMode);
244 QString path(ComponentFormattingOptions options = FullyDecoded) const;
245 QString fileName(ComponentFormattingOptions options = FullyDecoded) const;
246
247 bool hasQuery() const;
248 void setQuery(const QString &query, ParsingMode mode = TolerantMode);
249 void setQuery(const QUrlQuery &query);
250 QString query(ComponentFormattingOptions = PrettyDecoded) const;
251
252 bool hasFragment() const;
253 QString fragment(ComponentFormattingOptions options = PrettyDecoded) const;
254 void setFragment(const QString &fragment, ParsingMode mode = TolerantMode);
255
256 Q_REQUIRED_RESULT QUrl resolved(const QUrl &relative) const;
257
258 bool isRelative() const;
259 bool isParentOf(const QUrl &url) const;
260
261 bool isLocalFile() const;
262 static QUrl fromLocalFile(const QString &localfile);
263 QString toLocalFile() const;
264
265 void detach();
266 bool isDetached() const;
267
268 bool operator <(const QUrl &url) const;
269 bool operator ==(const QUrl &url) const;
270 bool operator !=(const QUrl &url) const;
271
272 bool matches(const QUrl &url, FormattingOptions options) const;
273
274 static QString fromPercentEncoding(const QByteArray &);
275 static QByteArray toPercentEncoding(const QString &,
276 const QByteArray &exclude = QByteArray(),
277 const QByteArray &include = QByteArray());
278#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
279 static QUrl fromCFURL(CFURLRef url);
280 CFURLRef toCFURL() const Q_DECL_CF_RETURNS_RETAINED;
281 static QUrl fromNSURL(const NSURL *url);
282 NSURL *toNSURL() const Q_DECL_NS_RETURNS_AUTORELEASED;
283#endif
284
285#if QT_DEPRECATED_SINCE(5,0)
286 QT_DEPRECATED static QString fromPunycode(const QByteArray &punycode)
287 { return fromAce(punycode); }
288 QT_DEPRECATED static QByteArray toPunycode(const QString &string)
289 { return toAce(string); }
290
291 QT_DEPRECATED inline void setQueryItems(const QList<QPair<QString, QString> > &qry);
292 QT_DEPRECATED inline void addQueryItem(const QString &key, const QString &value);
293 QT_DEPRECATED inline QList<QPair<QString, QString> > queryItems() const;
294 QT_DEPRECATED inline bool hasQueryItem(const QString &key) const;
295 QT_DEPRECATED inline QString queryItemValue(const QString &key) const;
296 QT_DEPRECATED inline QStringList allQueryItemValues(const QString &key) const;
297 QT_DEPRECATED inline void removeQueryItem(const QString &key);
298 QT_DEPRECATED inline void removeAllQueryItems(const QString &key);
299
300 QT_DEPRECATED inline void setEncodedQueryItems(const QList<QPair<QByteArray, QByteArray> > &query);
301 QT_DEPRECATED inline void addEncodedQueryItem(const QByteArray &key, const QByteArray &value);
302 QT_DEPRECATED inline QList<QPair<QByteArray, QByteArray> > encodedQueryItems() const;
303 QT_DEPRECATED inline bool hasEncodedQueryItem(const QByteArray &key) const;
304 QT_DEPRECATED inline QByteArray encodedQueryItemValue(const QByteArray &key) const;
305 QT_DEPRECATED inline QList<QByteArray> allEncodedQueryItemValues(const QByteArray &key) const;
306 QT_DEPRECATED inline void removeEncodedQueryItem(const QByteArray &key);
307 QT_DEPRECATED inline void removeAllEncodedQueryItems(const QByteArray &key);
308
309 QT_DEPRECATED void setEncodedUrl(const QByteArray &u, ParsingMode mode = TolerantMode)
310 { setUrl(fromEncodedComponent_helper(u), mode); }
311
312 QT_DEPRECATED QByteArray encodedUserName() const
313 { return userName(FullyEncoded).toLatin1(); }
314 QT_DEPRECATED void setEncodedUserName(const QByteArray &value)
315 { setUserName(fromEncodedComponent_helper(value)); }
316
317 QT_DEPRECATED QByteArray encodedPassword() const
318 { return password(FullyEncoded).toLatin1(); }
319 QT_DEPRECATED void setEncodedPassword(const QByteArray &value)
320 { setPassword(fromEncodedComponent_helper(value)); }
321
322 QT_DEPRECATED QByteArray encodedHost() const
323 { return host(FullyEncoded).toLatin1(); }
324 QT_DEPRECATED void setEncodedHost(const QByteArray &value)
325 { setHost(fromEncodedComponent_helper(value)); }
326
327 QT_DEPRECATED QByteArray encodedPath() const
328 { return path(FullyEncoded).toLatin1(); }
329 QT_DEPRECATED void setEncodedPath(const QByteArray &value)
330 { setPath(fromEncodedComponent_helper(value)); }
331
332 QT_DEPRECATED QByteArray encodedQuery() const
333 { return toLatin1_helper(query(FullyEncoded)); }
334 QT_DEPRECATED void setEncodedQuery(const QByteArray &value)
335 { setQuery(fromEncodedComponent_helper(value)); }
336
337 QT_DEPRECATED QByteArray encodedFragment() const
338 { return toLatin1_helper(fragment(FullyEncoded)); }
339 QT_DEPRECATED void setEncodedFragment(const QByteArray &value)
340 { setFragment(fromEncodedComponent_helper(value)); }
341
342private:
343 // helper function for the encodedQuery and encodedFragment functions
344 static QByteArray toLatin1_helper(const QString &string)
345 {
346 if (string.isEmpty())
347 return string.isNull() ? QByteArray() : QByteArray("");
348 return string.toLatin1();
349 }
350#endif
351private:
352 static QString fromEncodedComponent_helper(const QByteArray &ba);
353
354public:
355 static QString fromAce(const QByteArray &);
356 static QByteArray toAce(const QString &);
357 static QStringList idnWhitelist();
358 static QStringList toStringList(const QList<QUrl> &uris, FormattingOptions options = FormattingOptions(PrettyDecoded));
359 static QList<QUrl> fromStringList(const QStringList &uris, ParsingMode mode = TolerantMode);
360
361 static void setIdnWhitelist(const QStringList &);
362 friend Q_CORE_EXPORT uint qHash(const QUrl &url, uint seed) noexcept;
363
364private:
365 QUrlPrivate *d;
366 friend class QUrlQuery;
367
368public:
369 typedef QUrlPrivate * DataPtr;
370 inline DataPtr &data_ptr() { return d; }
371};
372
373Q_DECLARE_SHARED(QUrl)
374Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::ComponentFormattingOptions)
375//Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::FormattingOptions)
376
377#ifndef Q_QDOC
378Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::UrlFormattingOption f2)
379{ return QUrl::FormattingOptions(f1) | f2; }
380Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::FormattingOptions f2)
381{ return f2 | f1; }
382Q_DECL_CONSTEXPR inline QIncompatibleFlag operator|(QUrl::UrlFormattingOption f1, int f2)
383{ return QIncompatibleFlag(int(f1) | f2); }
384
385// add operators for OR'ing the two types of flags
386inline QUrl::FormattingOptions &operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOptions f)
387{ i |= QUrl::UrlFormattingOption(int(f)); return i; }
388Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOption f)
389{ return i | QUrl::UrlFormattingOption(int(f)); }
390Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOptions f)
391{ return i | QUrl::UrlFormattingOption(int(f)); }
392Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOption f, QUrl::UrlFormattingOption i)
393{ return i | QUrl::UrlFormattingOption(int(f)); }
394Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOptions f, QUrl::UrlFormattingOption i)
395{ return i | QUrl::UrlFormattingOption(int(f)); }
396Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::FormattingOptions i, QUrl::ComponentFormattingOptions f)
397{ return i | QUrl::UrlFormattingOption(int(f)); }
398Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOption f, QUrl::FormattingOptions i)
399{ return i | QUrl::UrlFormattingOption(int(f)); }
400Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOptions f, QUrl::FormattingOptions i)
401{ return i | QUrl::UrlFormattingOption(int(f)); }
402
403//inline QUrl::UrlFormattingOption &operator=(const QUrl::UrlFormattingOption &i, QUrl::ComponentFormattingOptions f)
404//{ i = int(f); f; }
405#endif // Q_QDOC
406
407#ifndef QT_NO_DATASTREAM
408Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QUrl &);
409Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QUrl &);
410#endif
411
412#ifndef QT_NO_DEBUG_STREAM
413Q_CORE_EXPORT QDebug operator<<(QDebug, const QUrl &);
414#endif
415
416QT_END_NAMESPACE
417
418#if QT_DEPRECATED_SINCE(5,0)
419# include <QtCore/qurlquery.h>
420#endif
421
422#endif // QURL_H
423