Qt 4.8
qurl.h
Go to the documentation of this file.
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 
52 
54 
55 QT_MODULE(Core)
56 
57 class QUrlPrivate;
58 class QDataStream;
59 class QMutexLocker;
60 
62 {
63 public:
64  enum ParsingMode {
66  StrictMode
67  };
68 
69  // encoding / toString values
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 &copy);
94  QUrl &operator =(const QUrl &copy);
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);
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 
292 Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::FormattingOptions)
293 
294 #ifndef QT_NO_DATASTREAM
297 #endif
298 
299 #ifndef QT_NO_DEBUG_STREAM
301 #endif
302 
304 
306 
307 #endif // QURL_H
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
double d
Definition: qnumeric_p.h:62
static QString fromPercentEncoding(const QByteArray &)
Returns a decoded copy of input.
Definition: qurl.cpp:5992
static QByteArray toPercentEncoding(const QString &, const QByteArray &exclude=QByteArray(), const QByteArray &include=QByteArray())
Returns an encoded copy of input.
Definition: qurl.cpp:6009
Q_CORE_EXPORT QDataStream & operator<<(QDataStream &, const QUrl &)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define QT_MODULE(x)
Definition: qglobal.h:2783
uint qHash(const QUrl &url)
Definition: qurl.h:285
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
#define Q_DECLARE_FLAGS(Flags, Enum)
The Q_DECLARE_FLAGS() macro expands to.
Definition: qglobal.h:2348
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
Q_CORE_EXPORT QTextStream & reset(QTextStream &s)
static void clear(QVariant::Private *d)
Definition: qvariant.cpp:197
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool operator!=(QBool b1, bool b2)
Definition: qglobal.h:2026
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &, QUrl &)
bool operator<(int priority, const QPair< QRunnable *, int > &p)
Definition: qthreadpool.cpp:50
bool isRelative() const
Returns true if the URL is relative; otherwise returns false.
Definition: qurl.cpp:5880
Q_GUI_EXPORT QString errorString(EGLint code=eglGetError())
Definition: qegl.cpp:743
#define Q_DECLARE_SHARED(TYPE)
Definition: qglobal.h:2214
void swap(QUrl &other)
Swaps URL other with this URL.
Definition: qurl.h:104
static QString toString(Register *reg, int type, bool *ok=0)
DataPtr & data_ptr()
Definition: qurl.h:282
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static bool isEmpty(const char *str)
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
The Q_DECLARE_OPERATORS_FOR_FLAGS() macro declares global operator|() functions for Flags...
Definition: qglobal.h:2355
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
unsigned int uint
Definition: qglobal.h:996
QUrlPrivate * DataPtr
Definition: qurl.h:281
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
QUrlPrivate * d
Definition: qurl.h:279
FormattingOption
The formatting options define how the URL is formatted when written out as text.
Definition: qurl.h:70
#define None
void qSwap(T &value1, T &value2)
Definition: qglobal.h:2181
QByteArray toEncoded(FormattingOptions options=None) const
Returns the encoded representation of the URL if it&#39;s valid; otherwise an empty QByteArray is returne...
Definition: qurl.cpp:5949
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
Q_DECLARE_TYPEINFO(QUrl, Q_MOVABLE_TYPE)
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
int key
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
ParsingMode
The parsing mode controls the way QUrl parses strings.
Definition: qurl.h:64
#define QT_END_HEADER
Definition: qglobal.h:137
static QString fileName(const QString &fileUrl)
bool operator==(QBool b1, bool b2)
Definition: qglobal.h:2023
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
The QList class is a template class that provides lists.
Definition: qdatastream.h:62