qurl.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtCore module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QURL_H
00043 #define QURL_H
00044 
00045 #include <QtCore/qbytearray.h>
00046 #include <QtCore/qobjectdefs.h>
00047 #include <QtCore/qpair.h>
00048 #include <QtCore/qstring.h>
00049 #include <QtCore/qhash.h>
00050 
00051 QT_BEGIN_HEADER
00052 
00053 QT_BEGIN_NAMESPACE
00054 
00055 QT_MODULE(Core)
00056 
00057 class QUrlPrivate;
00058 class QDataStream;
00059 
00060 class Q_CORE_EXPORT QUrl
00061 {
00062 public:
00063     enum ParsingMode {
00064         TolerantMode,
00065         StrictMode
00066     };
00067 
00068     // encoding / toString values
00069     enum FormattingOption {
00070         None = 0x0,
00071         RemoveScheme = 0x1,
00072         RemovePassword = 0x2,
00073         RemoveUserInfo = RemovePassword | 0x4,
00074         RemovePort = 0x8,
00075         RemoveAuthority = RemoveUserInfo | RemovePort | 0x10,
00076         RemovePath = 0x20,
00077         RemoveQuery = 0x40,
00078         RemoveFragment = 0x80,
00079         // 0x100: private: normalized
00080 
00081         StripTrailingSlash = 0x10000
00082     };
00083     Q_DECLARE_FLAGS(FormattingOptions, FormattingOption)
00084 
00085     QUrl();
00086 #ifdef QT_NO_URL_CAST_FROM_STRING
00087     explicit
00088 #endif
00089     QUrl(const QString &url);
00090     QUrl(const QString &url, ParsingMode mode);
00091     // ### Qt 5: merge the two constructors, with mode = TolerantMode
00092     QUrl(const QUrl &copy);
00093     QUrl &operator =(const QUrl &copy);
00094 #ifndef QT_NO_URL_CAST_FROM_STRING
00095     QUrl &operator =(const QString &url);
00096 #endif
00097     ~QUrl();
00098 
00099     void setUrl(const QString &url);
00100     void setUrl(const QString &url, ParsingMode mode);
00101     // ### Qt 5: merge the two setUrl() functions, with mode = TolerantMode
00102     void setEncodedUrl(const QByteArray &url);
00103     void setEncodedUrl(const QByteArray &url, ParsingMode mode);
00104     // ### Qt 5: merge the two setEncodedUrl() functions, with mode = TolerantMode
00105 
00106     bool isValid() const;
00107 
00108     bool isEmpty() const;
00109 
00110     void clear();
00111 
00112     void setScheme(const QString &scheme);
00113     QString scheme() const;
00114 
00115     void setAuthority(const QString &authority);
00116     QString authority() const;
00117 
00118     void setUserInfo(const QString &userInfo);
00119     QString userInfo() const;
00120 
00121     void setUserName(const QString &userName);
00122     QString userName() const;
00123     void setEncodedUserName(const QByteArray &userName);
00124     QByteArray encodedUserName() const;
00125 
00126     void setPassword(const QString &password);
00127     QString password() const;
00128     void setEncodedPassword(const QByteArray &password);
00129     QByteArray encodedPassword() const;
00130 
00131     void setHost(const QString &host);
00132     QString host() const;
00133     void setEncodedHost(const QByteArray &host);
00134     QByteArray encodedHost() const;
00135 
00136     void setPort(int port);
00137     int port() const;
00138     int port(int defaultPort) const;
00139     // ### Qt 5: merge the two port() functions, with defaultPort = -1
00140 
00141     void setPath(const QString &path);
00142     QString path() const;
00143     void setEncodedPath(const QByteArray &path);
00144     QByteArray encodedPath() const;
00145 
00146     bool hasQuery() const;
00147 
00148     void setEncodedQuery(const QByteArray &query);
00149     QByteArray encodedQuery() const;
00150 
00151     void setQueryDelimiters(char valueDelimiter, char pairDelimiter);
00152     char queryValueDelimiter() const;
00153     char queryPairDelimiter() const;
00154 
00155     void setQueryItems(const QList<QPair<QString, QString> > &query);
00156     void addQueryItem(const QString &key, const QString &value);
00157     QList<QPair<QString, QString> > queryItems() const;
00158     bool hasQueryItem(const QString &key) const;
00159     QString queryItemValue(const QString &key) const;
00160     QStringList allQueryItemValues(const QString &key) const;
00161     void removeQueryItem(const QString &key);
00162     void removeAllQueryItems(const QString &key);
00163 
00164     void setEncodedQueryItems(const QList<QPair<QByteArray, QByteArray> > &query);
00165     void addEncodedQueryItem(const QByteArray &key, const QByteArray &value);
00166     QList<QPair<QByteArray, QByteArray> > encodedQueryItems() const;
00167     bool hasEncodedQueryItem(const QByteArray &key) const;
00168     QByteArray encodedQueryItemValue(const QByteArray &key) const;
00169     QList<QByteArray> allEncodedQueryItemValues(const QByteArray &key) const;
00170     void removeEncodedQueryItem(const QByteArray &key);
00171     void removeAllEncodedQueryItems(const QByteArray &key);
00172 
00173     void setFragment(const QString &fragment);
00174     QString fragment() const;
00175     void setEncodedFragment(const QByteArray &fragment);
00176     QByteArray encodedFragment() const;
00177     bool hasFragment() const;
00178 
00179     QUrl resolved(const QUrl &relative) const;
00180 
00181     bool isRelative() const;
00182     bool isParentOf(const QUrl &url) const;
00183 
00184     static QUrl fromLocalFile(const QString &localfile);
00185     QString toLocalFile() const;
00186 
00187     QString toString(FormattingOptions options = None) const;
00188 
00189     QByteArray toEncoded(FormattingOptions options = None) const;
00190     static QUrl fromEncoded(const QByteArray &url);
00191     static QUrl fromEncoded(const QByteArray &url, ParsingMode mode);
00192     // ### Qt 5: merge the two fromEncoded() functions, with mode = TolerantMode
00193 
00194     static QUrl fromUserInput(const QString &userInput);
00195 
00196     void detach();
00197     bool isDetached() const;
00198 
00199     bool operator <(const QUrl &url) const;
00200     bool operator ==(const QUrl &url) const;
00201     bool operator !=(const QUrl &url) const;
00202 
00203     static QString fromPercentEncoding(const QByteArray &);
00204     static QByteArray toPercentEncoding(const QString &,
00205                                         const QByteArray &exclude = QByteArray(),
00206                                         const QByteArray &include = QByteArray());
00207     static QString fromPunycode(const QByteArray &);
00208     static QByteArray toPunycode(const QString &);
00209     static QString fromAce(const QByteArray &);
00210     static QByteArray toAce(const QString &);
00211     static QStringList idnWhitelist();
00212     static void setIdnWhitelist(const QStringList &);
00213 
00214 #if defined QT3_SUPPORT
00215     inline QT3_SUPPORT QString protocol() const { return scheme(); }
00216     inline QT3_SUPPORT void setProtocol(const QString &s) { setScheme(s); }
00217     inline QT3_SUPPORT void setUser(const QString &s) { setUserName(s); }
00218     inline QT3_SUPPORT QString user() const { return userName(); }
00219     inline QT3_SUPPORT bool hasUser() const { return !userName().isEmpty(); }
00220     inline QT3_SUPPORT bool hasPassword() const { return !password().isEmpty(); }
00221     inline QT3_SUPPORT bool hasHost() const { return !host().isEmpty(); }
00222     inline QT3_SUPPORT bool hasPort() const { return port() != -1; }
00223     inline QT3_SUPPORT bool hasPath() const { return !path().isEmpty(); }
00224     inline QT3_SUPPORT void setQuery(const QString &txt)
00225     {
00226         setEncodedQuery(txt.toLatin1());
00227     }
00228     inline QT3_SUPPORT QString query() const
00229     {
00230         return QString::fromLatin1(encodedQuery().constData());
00231     }
00232     inline QT3_SUPPORT QString ref() const { return fragment(); }
00233     inline QT3_SUPPORT void setRef(const QString &txt) { setFragment(txt); }
00234     inline QT3_SUPPORT bool hasRef() const { return !fragment().isEmpty(); }
00235     inline QT3_SUPPORT void addPath(const QString &p) { setPath(path() + QLatin1Char('/') + p); }
00236     QT3_SUPPORT void setFileName(const QString &txt);
00237     QT3_SUPPORT QString fileName() const;
00238     QT3_SUPPORT QString dirPath() const;
00239     static inline QT3_SUPPORT void decode(QString &url)
00240     {
00241         url = QUrl::fromPercentEncoding(url.toLatin1());
00242     }
00243     static inline QT3_SUPPORT void encode(QString &url)
00244     {
00245         url = QString::fromLatin1(QUrl::toPercentEncoding(url).constData());
00246     }
00247     inline QT3_SUPPORT operator QString() const { return toString(); }
00248     inline QT3_SUPPORT bool cdUp()
00249     {
00250         *this = resolved(QUrl(QLatin1String("..")));
00251         return true;
00252     }
00253     static inline QT3_SUPPORT bool isRelativeUrl(const QString &url)
00254     {
00255         return QUrl(url).isRelative();
00256     }
00257 #endif
00258 
00259     QString errorString() const;
00260 
00261 protected:
00262 #if defined (QT3_SUPPORT)
00263     inline QT3_SUPPORT void reset() { clear(); }
00264 #endif
00265 
00266 private:
00267     QUrlPrivate *d;
00268 public:
00269     typedef QUrlPrivate * DataPtr;
00270     inline DataPtr &data_ptr() { return d; }
00271 };
00272 
00273 inline uint qHash(const QUrl &url)
00274 {
00275     return qHash(url.toEncoded(QUrl::FormattingOption(0x100)));
00276 }
00277 
00278 Q_DECLARE_TYPEINFO(QUrl, Q_MOVABLE_TYPE);
00279 Q_DECLARE_SHARED(QUrl)
00280 Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::FormattingOptions)
00281 
00282 #ifndef QT_NO_DATASTREAM
00283 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QUrl &);
00284 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QUrl &);
00285 #endif
00286 
00287 #ifndef QT_NO_DEBUG_STREAM
00288 Q_CORE_EXPORT QDebug operator<<(QDebug, const QUrl &);
00289 #endif
00290 
00291 QT_END_NAMESPACE
00292 
00293 QT_END_HEADER
00294 
00295 #endif // QURL_H