1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). |
4 | ** Contact: http://www.qt-project.org/legal |
5 | ** |
6 | ** This file is part of the QtCore module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and Digia. For licensing terms and |
14 | ** conditions see http://qt.digia.com/licensing. For further information |
15 | ** use the contact form at http://qt.digia.com/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 2.1 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
24 | ** |
25 | ** In addition, as a special exception, Digia gives you certain additional |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
28 | ** |
29 | ** GNU General Public License Usage |
30 | ** Alternatively, this file may be used under the terms of the GNU |
31 | ** General Public License version 3.0 as published by the Free Software |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the |
33 | ** packaging of this file. Please review the following information to |
34 | ** ensure the GNU General Public License version 3.0 requirements will be |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. |
36 | ** |
37 | ** |
38 | ** $QT_END_LICENSE$ |
39 | ** |
40 | ****************************************************************************/ |
41 | |
42 | #ifndef QURL_H |
43 | #define QURL_H |
44 | |
45 | #include <QtCore/qbytearray.h> |
46 | #include <QtCore/qobjectdefs.h> |
47 | #include <QtCore/qpair.h> |
48 | #include <QtCore/qstring.h> |
49 | #include <QtCore/qhash.h> |
50 | |
51 | QT_BEGIN_HEADER |
52 | |
53 | QT_BEGIN_NAMESPACE |
54 | |
55 | QT_MODULE(Core) |
56 | |
57 | class QUrlPrivate; |
58 | class QDataStream; |
59 | class QMutexLocker; |
60 | |
61 | class Q_CORE_EXPORT QUrl |
62 | { |
63 | public: |
64 | enum ParsingMode { |
65 | TolerantMode, |
66 | StrictMode |
67 | }; |
68 | |
69 | // encoding / toString values |
70 | enum FormattingOption { |
71 | None = 0x0, |
72 | RemoveScheme = 0x1, |
73 | RemovePassword = 0x2, |
74 | RemoveUserInfo = RemovePassword | 0x4, |
75 | RemovePort = 0x8, |
76 | RemoveAuthority = RemoveUserInfo | RemovePort | 0x10, |
77 | RemovePath = 0x20, |
78 | RemoveQuery = 0x40, |
79 | RemoveFragment = 0x80, |
80 | // 0x100: private: normalized |
81 | |
82 | StripTrailingSlash = 0x10000 |
83 | }; |
84 | Q_DECLARE_FLAGS(FormattingOptions, FormattingOption) |
85 | |
86 | QUrl(); |
87 | #ifdef QT_NO_URL_CAST_FROM_STRING |
88 | explicit |
89 | #endif |
90 | QUrl(const QString &url); |
91 | QUrl(const QString &url, ParsingMode mode); |
92 | // ### Qt 5: merge the two constructors, with mode = TolerantMode |
93 | QUrl(const QUrl ©); |
94 | QUrl &operator =(const QUrl ©); |
95 | #ifndef QT_NO_URL_CAST_FROM_STRING |
96 | QUrl &operator =(const QString &url); |
97 | #endif |
98 | #ifdef Q_COMPILER_RVALUE_REFS |
99 | inline QUrl &operator=(QUrl &&other) |
100 | { qSwap(d, other.d); return *this; } |
101 | #endif |
102 | ~QUrl(); |
103 | |
104 | inline void swap(QUrl &other) { qSwap(d, other.d); } |
105 | |
106 | void setUrl(const QString &url); |
107 | void setUrl(const QString &url, ParsingMode mode); |
108 | // ### Qt 5: merge the two setUrl() functions, with mode = TolerantMode |
109 | void setEncodedUrl(const QByteArray &url); |
110 | void setEncodedUrl(const QByteArray &url, ParsingMode mode); |
111 | // ### Qt 5: merge the two setEncodedUrl() functions, with mode = TolerantMode |
112 | |
113 | bool isValid() const; |
114 | |
115 | bool isEmpty() const; |
116 | |
117 | void clear(); |
118 | |
119 | void setScheme(const QString &scheme); |
120 | QString scheme() const; |
121 | |
122 | void setAuthority(const QString &authority); |
123 | QString authority() const; |
124 | |
125 | void setUserInfo(const QString &userInfo); |
126 | QString userInfo() const; |
127 | |
128 | void setUserName(const QString &userName); |
129 | QString userName() const; |
130 | void setEncodedUserName(const QByteArray &userName); |
131 | QByteArray encodedUserName() const; |
132 | |
133 | void setPassword(const QString &password); |
134 | QString password() const; |
135 | void setEncodedPassword(const QByteArray &password); |
136 | QByteArray encodedPassword() const; |
137 | |
138 | void setHost(const QString &host); |
139 | QString host() const; |
140 | void setEncodedHost(const QByteArray &host); |
141 | QByteArray encodedHost() const; |
142 | |
143 | void setPort(int port); |
144 | int port() const; |
145 | int port(int defaultPort) const; |
146 | // ### Qt 5: merge the two port() functions, with defaultPort = -1 |
147 | |
148 | void setPath(const QString &path); |
149 | QString path() const; |
150 | void setEncodedPath(const QByteArray &path); |
151 | QByteArray encodedPath() const; |
152 | |
153 | bool hasQuery() const; |
154 | |
155 | void setEncodedQuery(const QByteArray &query); |
156 | QByteArray encodedQuery() const; |
157 | |
158 | void setQueryDelimiters(char valueDelimiter, char pairDelimiter); |
159 | char queryValueDelimiter() const; |
160 | char queryPairDelimiter() const; |
161 | |
162 | void setQueryItems(const QList<QPair<QString, QString> > &query); |
163 | void addQueryItem(const QString &key, const QString &value); |
164 | QList<QPair<QString, QString> > queryItems() const; |
165 | bool hasQueryItem(const QString &key) const; |
166 | QString queryItemValue(const QString &key) const; |
167 | QStringList allQueryItemValues(const QString &key) const; |
168 | void removeQueryItem(const QString &key); |
169 | void removeAllQueryItems(const QString &key); |
170 | |
171 | void setEncodedQueryItems(const QList<QPair<QByteArray, QByteArray> > &query); |
172 | void addEncodedQueryItem(const QByteArray &key, const QByteArray &value); |
173 | QList<QPair<QByteArray, QByteArray> > encodedQueryItems() const; |
174 | bool hasEncodedQueryItem(const QByteArray &key) const; |
175 | QByteArray encodedQueryItemValue(const QByteArray &key) const; |
176 | QList<QByteArray> allEncodedQueryItemValues(const QByteArray &key) const; |
177 | void removeEncodedQueryItem(const QByteArray &key); |
178 | void removeAllEncodedQueryItems(const QByteArray &key); |
179 | |
180 | void setFragment(const QString &fragment); |
181 | QString fragment() const; |
182 | void setEncodedFragment(const QByteArray &fragment); |
183 | QByteArray encodedFragment() const; |
184 | bool hasFragment() const; |
185 | #ifndef QT_BOOTSTRAPPED |
186 | QString topLevelDomain() const; |
187 | #endif |
188 | |
189 | QUrl resolved(const QUrl &relative) const; |
190 | |
191 | bool isRelative() const; |
192 | bool isParentOf(const QUrl &url) const; |
193 | |
194 | static QUrl fromLocalFile(const QString &localfile); |
195 | QString toLocalFile() const; |
196 | bool isLocalFile() const; |
197 | |
198 | QString toString(FormattingOptions options = None) const; |
199 | |
200 | QByteArray toEncoded(FormattingOptions options = None) const; |
201 | static QUrl fromEncoded(const QByteArray &url); |
202 | static QUrl fromEncoded(const QByteArray &url, ParsingMode mode); |
203 | // ### Qt 5: merge the two fromEncoded() functions, with mode = TolerantMode |
204 | |
205 | static QUrl fromUserInput(const QString &userInput); |
206 | |
207 | void detach(); |
208 | bool isDetached() const; |
209 | |
210 | bool operator <(const QUrl &url) const; |
211 | bool operator ==(const QUrl &url) const; |
212 | bool operator !=(const QUrl &url) const; |
213 | |
214 | static QString fromPercentEncoding(const QByteArray &); |
215 | static QByteArray toPercentEncoding(const QString &, |
216 | const QByteArray &exclude = QByteArray(), |
217 | const QByteArray &include = QByteArray()); |
218 | static QString fromPunycode(const QByteArray &); |
219 | static QByteArray toPunycode(const QString &); |
220 | static QString fromAce(const QByteArray &); |
221 | static QByteArray toAce(const QString &); |
222 | static QStringList idnWhitelist(); |
223 | static void setIdnWhitelist(const QStringList &); |
224 | |
225 | #if defined QT3_SUPPORT |
226 | inline QT3_SUPPORT QString protocol() const { return scheme(); } |
227 | inline QT3_SUPPORT void setProtocol(const QString &s) { setScheme(s); } |
228 | inline QT3_SUPPORT void setUser(const QString &s) { setUserName(s); } |
229 | inline QT3_SUPPORT QString user() const { return userName(); } |
230 | inline QT3_SUPPORT bool hasUser() const { return !userName().isEmpty(); } |
231 | inline QT3_SUPPORT bool hasPassword() const { return !password().isEmpty(); } |
232 | inline QT3_SUPPORT bool hasHost() const { return !host().isEmpty(); } |
233 | inline QT3_SUPPORT bool hasPort() const { return port() != -1; } |
234 | inline QT3_SUPPORT bool hasPath() const { return !path().isEmpty(); } |
235 | inline QT3_SUPPORT void setQuery(const QString &txt) |
236 | { |
237 | setEncodedQuery(txt.toLatin1()); |
238 | } |
239 | inline QT3_SUPPORT QString query() const |
240 | { |
241 | return QString::fromLatin1(encodedQuery().constData()); |
242 | } |
243 | inline QT3_SUPPORT QString ref() const { return fragment(); } |
244 | inline QT3_SUPPORT void setRef(const QString &txt) { setFragment(txt); } |
245 | inline QT3_SUPPORT bool hasRef() const { return !fragment().isEmpty(); } |
246 | inline QT3_SUPPORT void addPath(const QString &p) { setPath(path() + QLatin1Char('/') + p); } |
247 | QT3_SUPPORT void setFileName(const QString &txt); |
248 | QT3_SUPPORT QString fileName() const; |
249 | QT3_SUPPORT QString dirPath() const; |
250 | static inline QT3_SUPPORT void decode(QString &url) |
251 | { |
252 | url = QUrl::fromPercentEncoding(url.toLatin1()); |
253 | } |
254 | static inline QT3_SUPPORT void encode(QString &url) |
255 | { |
256 | url = QString::fromLatin1(QUrl::toPercentEncoding(url).constData()); |
257 | } |
258 | inline QT3_SUPPORT operator QString() const { return toString(); } |
259 | inline QT3_SUPPORT bool cdUp() |
260 | { |
261 | *this = resolved(QUrl(QLatin1String(".." ))); |
262 | return true; |
263 | } |
264 | static inline QT3_SUPPORT bool isRelativeUrl(const QString &url) |
265 | { |
266 | return QUrl(url).isRelative(); |
267 | } |
268 | #endif |
269 | |
270 | QString errorString() const; |
271 | |
272 | protected: |
273 | #if defined (QT3_SUPPORT) |
274 | inline QT3_SUPPORT void reset() { clear(); } |
275 | #endif |
276 | |
277 | private: |
278 | void detach(QMutexLocker &locker); |
279 | QUrlPrivate *d; |
280 | public: |
281 | typedef QUrlPrivate * DataPtr; |
282 | inline DataPtr &data_ptr() { return d; } |
283 | }; |
284 | |
285 | inline uint qHash(const QUrl &url) |
286 | { |
287 | return qHash(url.toEncoded(QUrl::FormattingOption(0x100))); |
288 | } |
289 | |
290 | Q_DECLARE_TYPEINFO(QUrl, Q_MOVABLE_TYPE); |
291 | Q_DECLARE_SHARED(QUrl) |
292 | Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::FormattingOptions) |
293 | |
294 | #ifndef QT_NO_DATASTREAM |
295 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QUrl &); |
296 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QUrl &); |
297 | #endif |
298 | |
299 | #ifndef QT_NO_DEBUG_STREAM |
300 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QUrl &); |
301 | #endif |
302 | |
303 | QT_END_NAMESPACE |
304 | |
305 | QT_END_HEADER |
306 | |
307 | #endif // QURL_H |
308 | |