1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Copyright (C) 2012 Intel Corporation.
5
** Contact: http://www.qt-project.org/legal
7
** This file is part of the QtCore module of the Qt Toolkit.
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.
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.
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.
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.
41
****************************************************************************/
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>
62
template <typename E1, typename E2>
66
typedef int QUrlTwoFlags:: *Zero;
68
Q_DECL_CONSTEXPR inline QUrlTwoFlags(E1 f) : i(f) {}
69
Q_DECL_CONSTEXPR inline QUrlTwoFlags(E2 f) : i(f) {}
70
Q_DECL_CONSTEXPR inline QUrlTwoFlags(QFlag f) : i(f) {}
71
Q_DECL_CONSTEXPR inline QUrlTwoFlags(QFlags<E1> f) : i(f.operator int()) {}
72
Q_DECL_CONSTEXPR inline QUrlTwoFlags(QFlags<E2> f) : i(f.operator int()) {}
73
Q_DECL_CONSTEXPR inline QUrlTwoFlags(Zero = 0) : i(0) {}
75
inline QUrlTwoFlags &operator&=(int mask) { i &= mask; return *this; }
76
inline QUrlTwoFlags &operator&=(uint mask) { i &= mask; return *this; }
77
inline QUrlTwoFlags &operator|=(QUrlTwoFlags f) { i |= f.i; return *this; }
78
inline QUrlTwoFlags &operator|=(E1 f) { i |= f; return *this; }
79
inline QUrlTwoFlags &operator|=(E2 f) { i |= f; return *this; }
80
inline QUrlTwoFlags &operator^=(QUrlTwoFlags f) { i ^= f.i; return *this; }
81
inline QUrlTwoFlags &operator^=(E1 f) { i ^= f; return *this; }
82
inline QUrlTwoFlags &operator^=(E2 f) { i ^= f; return *this; }
84
Q_DECL_CONSTEXPR inline operator QFlags<E1>() const { return E1(i); }
85
Q_DECL_CONSTEXPR inline operator QFlags<E2>() const { return E2(i); }
86
Q_DECL_CONSTEXPR inline operator int() const { return i; }
87
Q_DECL_CONSTEXPR inline bool operator!() const { return !i; }
89
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator|(QUrlTwoFlags f) const
90
{ return QUrlTwoFlags(E1(i | f.i)); }
91
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator|(E1 f) const
92
{ return QUrlTwoFlags(E1(i | f)); }
93
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator|(E2 f) const
94
{ return QUrlTwoFlags(E2(i | f)); }
95
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator^(QUrlTwoFlags f) const
96
{ return QUrlTwoFlags(E1(i ^ f.i)); }
97
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator^(E1 f) const
98
{ return QUrlTwoFlags(E1(i ^ f)); }
99
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator^(E2 f) const
100
{ return QUrlTwoFlags(E2(i ^ f)); }
101
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator&(int mask) const
102
{ return QUrlTwoFlags(E1(i & mask)); }
103
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator&(uint mask) const
104
{ return QUrlTwoFlags(E1(i & mask)); }
105
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator&(E1 f) const
106
{ return QUrlTwoFlags(E1(i & f)); }
107
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator&(E2 f) const
108
{ return QUrlTwoFlags(E2(i & f)); }
109
Q_DECL_CONSTEXPR inline QUrlTwoFlags operator~() const
110
{ return QUrlTwoFlags(E1(~i)); }
112
inline bool testFlag(E1 f) const { return (i & f) == f && (f != 0 || i == int(f)); }
113
inline bool testFlag(E2 f) const { return (i & f) == f && (f != 0 || i == int(f)); }
116
template<typename E1, typename E2>
117
class QTypeInfo<QUrlTwoFlags<E1, E2> > : public QTypeInfoMerger<QUrlTwoFlags<E1, E2>, E1, E2> {};
119
class Q_CORE_EXPORT QUrl
128
// encoding / toString values
129
enum UrlFormattingOption {
132
RemovePassword = 0x2,
133
RemoveUserInfo = RemovePassword | 0x4,
135
RemoveAuthority = RemoveUserInfo | RemovePort | 0x10,
138
RemoveFragment = 0x80,
139
// 0x100 was a private code in Qt 4, keep unused for a while
140
PreferLocalFile = 0x200,
141
StripTrailingSlash = 0x400
144
enum ComponentFormattingOption {
145
PrettyDecoded = 0x000000,
146
EncodeSpaces = 0x100000,
147
EncodeUnicode = 0x200000,
148
EncodeDelimiters = 0x400000 | 0x800000,
149
EncodeReserved = 0x1000000,
150
DecodeReserved = 0x2000000,
151
// 0x4000000 used to indicate full-decode mode
153
FullyEncoded = EncodeSpaces | EncodeUnicode | EncodeDelimiters | EncodeReserved,
154
FullyDecoded = FullyEncoded | DecodeReserved | 0x4000000
156
Q_DECLARE_FLAGS(ComponentFormattingOptions, ComponentFormattingOption)
158
Q_DECLARE_FLAGS(FormattingOptions, UrlFormattingOption)
160
typedef QUrlTwoFlags<UrlFormattingOption, ComponentFormattingOption> FormattingOptions;
164
QUrl(const QUrl ©);
165
QUrl &operator =(const QUrl ©);
166
#ifdef QT_NO_URL_CAST_FROM_STRING
167
explicit QUrl(const QString &url, ParsingMode mode = TolerantMode);
169
QUrl(const QString &url, ParsingMode mode = TolerantMode);
170
QUrl &operator=(const QString &url);
172
#ifdef Q_COMPILER_RVALUE_REFS
173
QUrl(QUrl &&other) : d(0)
174
{ qSwap(d, other.d); }
175
inline QUrl &operator=(QUrl &&other)
176
{ qSwap(d, other.d); return *this; }
180
inline void swap(QUrl &other) { qSwap(d, other.d); }
182
void setUrl(const QString &url, ParsingMode mode = TolerantMode);
183
QString url(FormattingOptions options = FormattingOptions(PrettyDecoded)) const;
184
QString toString(FormattingOptions options = FormattingOptions(PrettyDecoded)) const;
185
QString toDisplayString(FormattingOptions options = FormattingOptions(PrettyDecoded)) const;
187
QByteArray toEncoded(FormattingOptions options = FullyEncoded) const;
188
static QUrl fromEncoded(const QByteArray &url, ParsingMode mode = TolerantMode);
190
static QUrl fromUserInput(const QString &userInput);
192
bool isValid() const;
193
QString errorString() const;
195
bool isEmpty() const;
198
void setScheme(const QString &scheme);
199
QString scheme() const;
201
void setAuthority(const QString &authority, ParsingMode mode = TolerantMode);
202
QString authority(ComponentFormattingOptions options = PrettyDecoded) const;
204
void setUserInfo(const QString &userInfo, ParsingMode mode = TolerantMode);
205
QString userInfo(ComponentFormattingOptions options = PrettyDecoded) const;
207
void setUserName(const QString &userName, ParsingMode mode = TolerantMode);
208
QString userName(ComponentFormattingOptions options = PrettyDecoded) const;
210
void setPassword(const QString &password, ParsingMode mode = TolerantMode);
211
QString password(ComponentFormattingOptions = PrettyDecoded) const;
213
void setHost(const QString &host, ParsingMode mode = TolerantMode);
214
QString host(ComponentFormattingOptions = PrettyDecoded) const;
215
QString topLevelDomain(ComponentFormattingOptions options = PrettyDecoded) const;
217
void setPort(int port);
218
int port(int defaultPort = -1) const;
220
void setPath(const QString &path, ParsingMode mode = TolerantMode);
221
QString path(ComponentFormattingOptions options = PrettyDecoded) const;
223
bool hasQuery() const;
224
void setQuery(const QString &query, ParsingMode mode = TolerantMode);
225
void setQuery(const QUrlQuery &query);
226
QString query(ComponentFormattingOptions = PrettyDecoded) const;
228
bool hasFragment() const;
229
QString fragment(ComponentFormattingOptions options = PrettyDecoded) const;
230
void setFragment(const QString &fragment, ParsingMode mode = TolerantMode);
232
QUrl resolved(const QUrl &relative) const;
234
bool isRelative() const;
235
bool isParentOf(const QUrl &url) const;
237
bool isLocalFile() const;
238
static QUrl fromLocalFile(const QString &localfile);
239
QString toLocalFile() const;
242
bool isDetached() const;
244
bool operator <(const QUrl &url) const;
245
bool operator ==(const QUrl &url) const;
246
bool operator !=(const QUrl &url) const;
248
static QString fromPercentEncoding(const QByteArray &);
249
static QByteArray toPercentEncoding(const QString &,
250
const QByteArray &exclude = QByteArray(),
251
const QByteArray &include = QByteArray());
252
#if QT_DEPRECATED_SINCE(5,0)
253
QT_DEPRECATED static QString fromPunycode(const QByteArray &punycode)
254
{ return fromAce(punycode); }
255
QT_DEPRECATED static QByteArray toPunycode(const QString &string)
256
{ return toAce(string); }
258
QT_DEPRECATED inline void setQueryItems(const QList<QPair<QString, QString> > &qry);
259
QT_DEPRECATED inline void addQueryItem(const QString &key, const QString &value);
260
QT_DEPRECATED inline QList<QPair<QString, QString> > queryItems() const;
261
QT_DEPRECATED inline bool hasQueryItem(const QString &key) const;
262
QT_DEPRECATED inline QString queryItemValue(const QString &key) const;
263
QT_DEPRECATED inline QStringList allQueryItemValues(const QString &key) const;
264
QT_DEPRECATED inline void removeQueryItem(const QString &key);
265
QT_DEPRECATED inline void removeAllQueryItems(const QString &key);
267
QT_DEPRECATED inline void setEncodedQueryItems(const QList<QPair<QByteArray, QByteArray> > &query);
268
QT_DEPRECATED inline void addEncodedQueryItem(const QByteArray &key, const QByteArray &value);
269
QT_DEPRECATED inline QList<QPair<QByteArray, QByteArray> > encodedQueryItems() const;
270
QT_DEPRECATED inline bool hasEncodedQueryItem(const QByteArray &key) const;
271
QT_DEPRECATED inline QByteArray encodedQueryItemValue(const QByteArray &key) const;
272
QT_DEPRECATED inline QList<QByteArray> allEncodedQueryItemValues(const QByteArray &key) const;
273
QT_DEPRECATED inline void removeEncodedQueryItem(const QByteArray &key);
274
QT_DEPRECATED inline void removeAllEncodedQueryItems(const QByteArray &key);
276
QT_DEPRECATED void setEncodedUrl(const QByteArray &u, ParsingMode mode = TolerantMode)
277
{ setUrl(fromEncodedComponent_helper(u), mode); }
279
QT_DEPRECATED QByteArray encodedUserName() const
280
{ return userName(FullyEncoded).toLatin1(); }
281
QT_DEPRECATED void setEncodedUserName(const QByteArray &value)
282
{ setUserName(fromEncodedComponent_helper(value)); }
284
QT_DEPRECATED QByteArray encodedPassword() const
285
{ return password(FullyEncoded).toLatin1(); }
286
QT_DEPRECATED void setEncodedPassword(const QByteArray &value)
287
{ setPassword(fromEncodedComponent_helper(value)); }
289
QT_DEPRECATED QByteArray encodedHost() const
290
{ return host(FullyEncoded).toLatin1(); }
291
QT_DEPRECATED void setEncodedHost(const QByteArray &value)
292
{ setHost(fromEncodedComponent_helper(value)); }
294
QT_DEPRECATED QByteArray encodedPath() const
295
{ return path(FullyEncoded).toLatin1(); }
296
QT_DEPRECATED void setEncodedPath(const QByteArray &value)
297
{ setPath(fromEncodedComponent_helper(value)); }
299
QT_DEPRECATED QByteArray encodedQuery() const
300
{ return toLatin1_helper(query(FullyEncoded)); }
301
QT_DEPRECATED void setEncodedQuery(const QByteArray &value)
302
{ setQuery(fromEncodedComponent_helper(value)); }
304
QT_DEPRECATED QByteArray encodedFragment() const
305
{ return toLatin1_helper(fragment(FullyEncoded)); }
306
QT_DEPRECATED void setEncodedFragment(const QByteArray &value)
307
{ setFragment(fromEncodedComponent_helper(value)); }
310
// helper function for the encodedQuery and encodedFragment functions
311
static QByteArray toLatin1_helper(const QString &string)
313
if (string.isEmpty())
314
return string.isNull() ? QByteArray() : QByteArray("");
315
return string.toLatin1();
319
static QString fromEncodedComponent_helper(const QByteArray &ba);
322
static QString fromAce(const QByteArray &);
323
static QByteArray toAce(const QString &);
324
static QStringList idnWhitelist();
325
static void setIdnWhitelist(const QStringList &);
326
friend Q_CORE_EXPORT uint qHash(const QUrl &url, uint seed = 0) Q_DECL_NOTHROW;
330
friend class QUrlQuery;
333
typedef QUrlPrivate * DataPtr;
334
inline DataPtr &data_ptr() { return d; }
337
Q_DECLARE_SHARED(QUrl)
338
Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::ComponentFormattingOptions)
339
//Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::FormattingOptions)
341
Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::UrlFormattingOption f2)
342
{ return QUrl::FormattingOptions(f1) | f2; }
343
Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::FormattingOptions f2)
345
Q_DECL_CONSTEXPR inline QIncompatibleFlag operator|(QUrl::UrlFormattingOption f1, int f2)
346
{ return QIncompatibleFlag(int(f1) | f2); }
348
// add operators for OR'ing the two types of flags
349
inline QUrl::FormattingOptions &operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOptions f)
350
{ i |= QUrl::UrlFormattingOption(int(f)); return i; }
351
Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOption f)
352
{ return i | QUrl::UrlFormattingOption(int(f)); }
353
Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOptions f)
354
{ return i | QUrl::UrlFormattingOption(int(f)); }
355
Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOption f, QUrl::UrlFormattingOption i)
356
{ return i | QUrl::UrlFormattingOption(int(f)); }
357
Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOptions f, QUrl::UrlFormattingOption i)
358
{ return i | QUrl::UrlFormattingOption(int(f)); }
359
Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::FormattingOptions i, QUrl::ComponentFormattingOptions f)
360
{ return i | QUrl::UrlFormattingOption(int(f)); }
361
Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOption f, QUrl::FormattingOptions i)
362
{ return i | QUrl::UrlFormattingOption(int(f)); }
363
Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOptions f, QUrl::FormattingOptions i)
364
{ return i | QUrl::UrlFormattingOption(int(f)); }
366
//inline QUrl::UrlFormattingOption &operator=(const QUrl::UrlFormattingOption &i, QUrl::ComponentFormattingOptions f)
369
#ifndef QT_NO_DATASTREAM
370
Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QUrl &);
371
Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QUrl &);
374
#ifndef QT_NO_DEBUG_STREAM
375
Q_CORE_EXPORT QDebug operator<<(QDebug, const QUrl &);
380
#if QT_DEPRECATED_SINCE(5,0)
381
# include <QtCore/qurlquery.h>