Line data Source code
1 : /****************************************************************************
2 : **
3 : ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 : ** Copyright (C) 2012 Intel Corporation.
5 : ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
15 : ** conditions see http://qt.digia.com/licensing. For further information
16 : ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
21 : ** Foundation and appearing in the file LICENSE.LGPL included in the
22 : ** packaging of this file. Please review the following information to
23 : ** ensure the GNU Lesser General Public License version 2.1 requirements
24 : ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 : **
26 : ** In addition, as a special exception, Digia gives you certain additional
27 : ** rights. These rights are described in the Digia Qt LGPL Exception
28 : ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 : **
30 : ** GNU General Public License Usage
31 : ** Alternatively, this file may be used under the terms of the GNU
32 : ** General Public License version 3.0 as published by the Free Software
33 : ** Foundation and appearing in the file LICENSE.GPL included in the
34 : ** packaging of this file. Please review the following information to
35 : ** ensure the GNU General Public License version 3.0 requirements will be
36 : ** met: http://www.gnu.org/copyleft/gpl.html.
37 : **
38 : **
39 : ** $QT_END_LICENSE$
40 : **
41 : ****************************************************************************/
42 :
43 : #ifndef QURL_H
44 : #define QURL_H
45 :
46 : #include <QtCore/qbytearray.h>
47 : #include <QtCore/qobjectdefs.h>
48 : #include <QtCore/qstring.h>
49 : #include <QtCore/qlist.h>
50 : #include <QtCore/qpair.h>
51 : #include <QtCore/qglobal.h>
52 :
53 : #ifdef Q_OS_MAC
54 : Q_FORWARD_DECLARE_CF_TYPE(CFURL);
55 : # ifdef __OBJC__
56 : Q_FORWARD_DECLARE_OBJC_CLASS(NSURL);
57 : # endif
58 : #endif
59 :
60 : QT_BEGIN_NAMESPACE
61 :
62 :
63 : class QUrlQuery;
64 : class QUrlPrivate;
65 : class QDataStream;
66 :
67 : template <typename E1, typename E2>
68 : class QUrlTwoFlags
69 : {
70 : int i;
71 : typedef int QUrlTwoFlags:: *Zero;
72 : public:
73 : Q_DECL_CONSTEXPR inline QUrlTwoFlags(E1 f) : i(f) {}
74 : Q_DECL_CONSTEXPR inline QUrlTwoFlags(E2 f) : i(f) {}
75 : Q_DECL_CONSTEXPR inline QUrlTwoFlags(QFlag f) : i(f) {}
76 : Q_DECL_CONSTEXPR inline QUrlTwoFlags(QFlags<E1> f) : i(f.operator int()) {}
77 : Q_DECL_CONSTEXPR inline QUrlTwoFlags(QFlags<E2> f) : i(f.operator int()) {}
78 : Q_DECL_CONSTEXPR inline QUrlTwoFlags(Zero = 0) : i(0) {}
79 :
80 : inline QUrlTwoFlags &operator&=(int mask) { i &= mask; return *this; }
81 : inline QUrlTwoFlags &operator&=(uint mask) { i &= mask; return *this; }
82 : inline QUrlTwoFlags &operator|=(QUrlTwoFlags f) { i |= f.i; return *this; }
83 : inline QUrlTwoFlags &operator|=(E1 f) { i |= f; return *this; }
84 : inline QUrlTwoFlags &operator|=(E2 f) { i |= f; return *this; }
85 : inline QUrlTwoFlags &operator^=(QUrlTwoFlags f) { i ^= f.i; return *this; }
86 : inline QUrlTwoFlags &operator^=(E1 f) { i ^= f; return *this; }
87 : inline QUrlTwoFlags &operator^=(E2 f) { i ^= f; return *this; }
88 :
89 : Q_DECL_CONSTEXPR inline operator QFlags<E1>() const { return QFlag(i); }
90 : Q_DECL_CONSTEXPR inline operator QFlags<E2>() const { return QFlag(i); }
91 : Q_DECL_CONSTEXPR inline operator int() const { return i; }
92 : Q_DECL_CONSTEXPR inline bool operator!() const { return !i; }
93 :
94 : Q_DECL_CONSTEXPR inline QUrlTwoFlags operator|(QUrlTwoFlags f) const
95 : { return QUrlTwoFlags(QFlag(i | f.i)); }
96 : Q_DECL_CONSTEXPR inline QUrlTwoFlags operator|(E1 f) const
97 : { return QUrlTwoFlags(QFlag(i | f)); }
98 : Q_DECL_CONSTEXPR inline QUrlTwoFlags operator|(E2 f) const
99 : { return QUrlTwoFlags(QFlag(i | f)); }
100 : Q_DECL_CONSTEXPR inline QUrlTwoFlags operator^(QUrlTwoFlags f) const
101 : { return QUrlTwoFlags(QFlag(i ^ f.i)); }
102 : Q_DECL_CONSTEXPR inline QUrlTwoFlags operator^(E1 f) const
103 : { return QUrlTwoFlags(QFlag(i ^ f)); }
104 : Q_DECL_CONSTEXPR inline QUrlTwoFlags operator^(E2 f) const
105 : { return QUrlTwoFlags(QFlag(i ^ f)); }
106 : Q_DECL_CONSTEXPR inline QUrlTwoFlags operator&(int mask) const
107 : { return QUrlTwoFlags(QFlag(i & mask)); }
108 : Q_DECL_CONSTEXPR inline QUrlTwoFlags operator&(uint mask) const
109 : { return QUrlTwoFlags(QFlag(i & mask)); }
110 : Q_DECL_CONSTEXPR inline QUrlTwoFlags operator&(E1 f) const
111 : { return QUrlTwoFlags(QFlag(i & f)); }
112 : Q_DECL_CONSTEXPR inline QUrlTwoFlags operator&(E2 f) const
113 : { return QUrlTwoFlags(QFlag(i & f)); }
114 : Q_DECL_CONSTEXPR inline QUrlTwoFlags operator~() const
115 : { return QUrlTwoFlags(QFlag(~i)); }
116 :
117 : Q_DECL_CONSTEXPR inline bool testFlag(E1 f) const { return (i & f) == f && (f != 0 || i == int(f)); }
118 : Q_DECL_CONSTEXPR inline bool testFlag(E2 f) const { return (i & f) == f && (f != 0 || i == int(f)); }
119 : };
120 :
121 : template<typename E1, typename E2>
122 : class QTypeInfo<QUrlTwoFlags<E1, E2> > : public QTypeInfoMerger<QUrlTwoFlags<E1, E2>, E1, E2> {};
123 :
124 : class QUrl;
125 : // qHash is a friend, but we can't use default arguments for friends (ยง8.3.6.4)
126 : Q_CORE_EXPORT uint qHash(const QUrl &url, uint seed = 0) Q_DECL_NOTHROW;
127 :
128 : class Q_CORE_EXPORT QUrl
129 : {
130 : public:
131 : enum ParsingMode {
132 : TolerantMode,
133 : StrictMode,
134 : DecodedMode
135 : };
136 :
137 : // encoding / toString values
138 : enum UrlFormattingOption {
139 : None = 0x0,
140 : RemoveScheme = 0x1,
141 : RemovePassword = 0x2,
142 : RemoveUserInfo = RemovePassword | 0x4,
143 : RemovePort = 0x8,
144 : RemoveAuthority = RemoveUserInfo | RemovePort | 0x10,
145 : RemovePath = 0x20,
146 : RemoveQuery = 0x40,
147 : RemoveFragment = 0x80,
148 : // 0x100 was a private code in Qt 4, keep unused for a while
149 : PreferLocalFile = 0x200,
150 : StripTrailingSlash = 0x400,
151 : RemoveFilename = 0x800,
152 : NormalizePathSegments = 0x1000
153 : };
154 :
155 : enum ComponentFormattingOption {
156 : PrettyDecoded = 0x000000,
157 : EncodeSpaces = 0x100000,
158 : EncodeUnicode = 0x200000,
159 : EncodeDelimiters = 0x400000 | 0x800000,
160 : EncodeReserved = 0x1000000,
161 : DecodeReserved = 0x2000000,
162 : // 0x4000000 used to indicate full-decode mode
163 :
164 : FullyEncoded = EncodeSpaces | EncodeUnicode | EncodeDelimiters | EncodeReserved,
165 : FullyDecoded = FullyEncoded | DecodeReserved | 0x4000000
166 : };
167 : Q_DECLARE_FLAGS(ComponentFormattingOptions, ComponentFormattingOption)
168 : #ifdef Q_QDOC
169 : Q_DECLARE_FLAGS(FormattingOptions, UrlFormattingOption)
170 : #else
171 : typedef QUrlTwoFlags<UrlFormattingOption, ComponentFormattingOption> FormattingOptions;
172 : #endif
173 :
174 : QUrl();
175 : QUrl(const QUrl ©);
176 : QUrl &operator =(const QUrl ©);
177 : #ifdef QT_NO_URL_CAST_FROM_STRING
178 : explicit QUrl(const QString &url, ParsingMode mode = TolerantMode);
179 : #else
180 : QUrl(const QString &url, ParsingMode mode = TolerantMode);
181 : QUrl &operator=(const QString &url);
182 : #endif
183 : #ifdef Q_COMPILER_RVALUE_REFS
184 0 : QUrl(QUrl &&other) : d(0)
185 0 : { qSwap(d, other.d); }
186 : inline QUrl &operator=(QUrl &&other)
187 : { qSwap(d, other.d); return *this; }
188 : #endif
189 : ~QUrl();
190 :
191 : inline void swap(QUrl &other) { 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 : 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 : static QUrl fromUserInput(const QString &userInput);
203 :
204 : bool isValid() const;
205 : QString errorString() const;
206 :
207 : bool isEmpty() const;
208 : void clear();
209 :
210 : void setScheme(const QString &scheme);
211 : QString scheme() const;
212 :
213 : void setAuthority(const QString &authority, ParsingMode mode = TolerantMode);
214 : QString authority(ComponentFormattingOptions options = PrettyDecoded) const;
215 :
216 : void setUserInfo(const QString &userInfo, ParsingMode mode = TolerantMode);
217 : QString userInfo(ComponentFormattingOptions options = PrettyDecoded) const;
218 :
219 : void setUserName(const QString &userName, ParsingMode mode = DecodedMode);
220 : QString userName(ComponentFormattingOptions options = FullyDecoded) const;
221 :
222 : void setPassword(const QString &password, ParsingMode mode = DecodedMode);
223 : QString password(ComponentFormattingOptions = FullyDecoded) const;
224 :
225 : void setHost(const QString &host, ParsingMode mode = DecodedMode);
226 : QString host(ComponentFormattingOptions = FullyDecoded) const;
227 : QString topLevelDomain(ComponentFormattingOptions options = FullyDecoded) const;
228 :
229 : void setPort(int port);
230 : int port(int defaultPort = -1) const;
231 :
232 : void setPath(const QString &path, ParsingMode mode = DecodedMode);
233 : QString path(ComponentFormattingOptions options = FullyDecoded) const;
234 : QString fileName(ComponentFormattingOptions options = FullyDecoded) const;
235 :
236 : bool hasQuery() const;
237 : void setQuery(const QString &query, ParsingMode mode = TolerantMode);
238 : void setQuery(const QUrlQuery &query);
239 : QString query(ComponentFormattingOptions = PrettyDecoded) const;
240 :
241 : bool hasFragment() const;
242 : QString fragment(ComponentFormattingOptions options = PrettyDecoded) const;
243 : void setFragment(const QString &fragment, ParsingMode mode = TolerantMode);
244 :
245 : QUrl resolved(const QUrl &relative) const;
246 :
247 : bool isRelative() const;
248 : bool isParentOf(const QUrl &url) const;
249 :
250 : bool isLocalFile() const;
251 : static QUrl fromLocalFile(const QString &localfile);
252 : QString toLocalFile() const;
253 :
254 : void detach();
255 : bool isDetached() const;
256 :
257 : bool operator <(const QUrl &url) const;
258 : bool operator ==(const QUrl &url) const;
259 : bool operator !=(const QUrl &url) const;
260 :
261 : bool matches(const QUrl &url, FormattingOptions options) const;
262 :
263 : static QString fromPercentEncoding(const QByteArray &);
264 : static QByteArray toPercentEncoding(const QString &,
265 : const QByteArray &exclude = QByteArray(),
266 : const QByteArray &include = QByteArray());
267 : #if defined(Q_OS_MAC) || defined(Q_QDOC)
268 : static QUrl fromCFURL(CFURLRef url);
269 : CFURLRef toCFURL() const Q_DECL_CF_RETURNS_RETAINED;
270 : # if defined(__OBJC__) || defined(Q_QDOC)
271 : static QUrl fromNSURL(const NSURL *url);
272 : NSURL *toNSURL() const Q_DECL_NS_RETURNS_AUTORELEASED;
273 : # endif
274 : #endif
275 :
276 : #if QT_DEPRECATED_SINCE(5,0)
277 : QT_DEPRECATED static QString fromPunycode(const QByteArray &punycode)
278 : { return fromAce(punycode); }
279 : QT_DEPRECATED static QByteArray toPunycode(const QString &string)
280 : { return toAce(string); }
281 :
282 : QT_DEPRECATED inline void setQueryItems(const QList<QPair<QString, QString> > &qry);
283 : QT_DEPRECATED inline void addQueryItem(const QString &key, const QString &value);
284 : QT_DEPRECATED inline QList<QPair<QString, QString> > queryItems() const;
285 : QT_DEPRECATED inline bool hasQueryItem(const QString &key) const;
286 : QT_DEPRECATED inline QString queryItemValue(const QString &key) const;
287 : QT_DEPRECATED inline QStringList allQueryItemValues(const QString &key) const;
288 : QT_DEPRECATED inline void removeQueryItem(const QString &key);
289 : QT_DEPRECATED inline void removeAllQueryItems(const QString &key);
290 :
291 : QT_DEPRECATED inline void setEncodedQueryItems(const QList<QPair<QByteArray, QByteArray> > &query);
292 : QT_DEPRECATED inline void addEncodedQueryItem(const QByteArray &key, const QByteArray &value);
293 : QT_DEPRECATED inline QList<QPair<QByteArray, QByteArray> > encodedQueryItems() const;
294 : QT_DEPRECATED inline bool hasEncodedQueryItem(const QByteArray &key) const;
295 : QT_DEPRECATED inline QByteArray encodedQueryItemValue(const QByteArray &key) const;
296 : QT_DEPRECATED inline QList<QByteArray> allEncodedQueryItemValues(const QByteArray &key) const;
297 : QT_DEPRECATED inline void removeEncodedQueryItem(const QByteArray &key);
298 : QT_DEPRECATED inline void removeAllEncodedQueryItems(const QByteArray &key);
299 :
300 : QT_DEPRECATED void setEncodedUrl(const QByteArray &u, ParsingMode mode = TolerantMode)
301 : { setUrl(fromEncodedComponent_helper(u), mode); }
302 :
303 : QT_DEPRECATED QByteArray encodedUserName() const
304 : { return userName(FullyEncoded).toLatin1(); }
305 : QT_DEPRECATED void setEncodedUserName(const QByteArray &value)
306 : { setUserName(fromEncodedComponent_helper(value)); }
307 :
308 : QT_DEPRECATED QByteArray encodedPassword() const
309 : { return password(FullyEncoded).toLatin1(); }
310 : QT_DEPRECATED void setEncodedPassword(const QByteArray &value)
311 : { setPassword(fromEncodedComponent_helper(value)); }
312 :
313 : QT_DEPRECATED QByteArray encodedHost() const
314 : { return host(FullyEncoded).toLatin1(); }
315 : QT_DEPRECATED void setEncodedHost(const QByteArray &value)
316 : { setHost(fromEncodedComponent_helper(value)); }
317 :
318 : QT_DEPRECATED QByteArray encodedPath() const
319 : { return path(FullyEncoded).toLatin1(); }
320 : QT_DEPRECATED void setEncodedPath(const QByteArray &value)
321 : { setPath(fromEncodedComponent_helper(value)); }
322 :
323 : QT_DEPRECATED QByteArray encodedQuery() const
324 : { return toLatin1_helper(query(FullyEncoded)); }
325 : QT_DEPRECATED void setEncodedQuery(const QByteArray &value)
326 : { setQuery(fromEncodedComponent_helper(value)); }
327 :
328 : QT_DEPRECATED QByteArray encodedFragment() const
329 : { return toLatin1_helper(fragment(FullyEncoded)); }
330 : QT_DEPRECATED void setEncodedFragment(const QByteArray &value)
331 : { setFragment(fromEncodedComponent_helper(value)); }
332 :
333 : private:
334 : // helper function for the encodedQuery and encodedFragment functions
335 : static QByteArray toLatin1_helper(const QString &string)
336 : {
337 : if (string.isEmpty())
338 : return string.isNull() ? QByteArray() : QByteArray("");
339 : return string.toLatin1();
340 : }
341 : #endif
342 : private:
343 : static QString fromEncodedComponent_helper(const QByteArray &ba);
344 :
345 : public:
346 : static QString fromAce(const QByteArray &);
347 : static QByteArray toAce(const QString &);
348 : static QStringList idnWhitelist();
349 : static QStringList toStringList(const QList<QUrl> &uris, FormattingOptions options = FormattingOptions(PrettyDecoded));
350 : static QList<QUrl> fromStringList(const QStringList &uris, ParsingMode mode = TolerantMode);
351 :
352 : static void setIdnWhitelist(const QStringList &);
353 : friend Q_CORE_EXPORT uint qHash(const QUrl &url, uint seed) Q_DECL_NOTHROW;
354 :
355 : private:
356 : QUrlPrivate *d;
357 : friend class QUrlQuery;
358 :
359 : public:
360 : typedef QUrlPrivate * DataPtr;
361 : inline DataPtr &data_ptr() { return d; }
362 : };
363 :
364 : Q_DECLARE_SHARED(QUrl)
365 : Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::ComponentFormattingOptions)
366 : //Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::FormattingOptions)
367 :
368 : Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::UrlFormattingOption f2)
369 : { return QUrl::FormattingOptions(f1) | f2; }
370 : Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::FormattingOptions f2)
371 : { return f2 | f1; }
372 : Q_DECL_CONSTEXPR inline QIncompatibleFlag operator|(QUrl::UrlFormattingOption f1, int f2)
373 : { return QIncompatibleFlag(int(f1) | f2); }
374 :
375 : // add operators for OR'ing the two types of flags
376 : inline QUrl::FormattingOptions &operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOptions f)
377 : { i |= QUrl::UrlFormattingOption(int(f)); return i; }
378 : Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOption f)
379 : { return i | QUrl::UrlFormattingOption(int(f)); }
380 : Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOptions f)
381 : { return i | QUrl::UrlFormattingOption(int(f)); }
382 : Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOption f, QUrl::UrlFormattingOption i)
383 : { return i | QUrl::UrlFormattingOption(int(f)); }
384 : Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOptions f, QUrl::UrlFormattingOption i)
385 : { return i | QUrl::UrlFormattingOption(int(f)); }
386 : Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::FormattingOptions i, QUrl::ComponentFormattingOptions f)
387 : { return i | QUrl::UrlFormattingOption(int(f)); }
388 : Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOption f, QUrl::FormattingOptions i)
389 : { return i | QUrl::UrlFormattingOption(int(f)); }
390 : Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOptions f, QUrl::FormattingOptions i)
391 : { return i | QUrl::UrlFormattingOption(int(f)); }
392 :
393 : //inline QUrl::UrlFormattingOption &operator=(const QUrl::UrlFormattingOption &i, QUrl::ComponentFormattingOptions f)
394 : //{ i = int(f); f; }
395 :
396 : #ifndef QT_NO_DATASTREAM
397 : Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QUrl &);
398 : Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QUrl &);
399 : #endif
400 :
401 : #ifndef QT_NO_DEBUG_STREAM
402 : Q_CORE_EXPORT QDebug operator<<(QDebug, const QUrl &);
403 : #endif
404 :
405 : QT_END_NAMESPACE
406 :
407 : #if QT_DEPRECATED_SINCE(5,0)
408 : # include <QtCore/qurlquery.h>
409 : #endif
410 :
411 : #endif // QURL_H
|