From: David Faure Date: Thu, 26 Jan 2012 00:57:59 +0000 (+0100) Subject: QUrl: deprecate setEncodedUrl() and fromEncoded(), add url(). X-Git-Tag: qt-v5.0.0-alpha1~1512 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=27d9fef14acdc33126004f4de3047d3344448a7d;p=profile%2Fivi%2Fqtbase.git QUrl: deprecate setEncodedUrl() and fromEncoded(), add url(). setEncodedUrl() isn't necessary anymore now that setUrl can handle encoded (and partially encoded) urls. url() is added for symmetry with setUrl(). Change-Id: I4e671482a5635a86797421ca50882db9cd60d852 Reviewed-by: Thiago Macieira --- diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index 5e76168..1ef2cb9 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -114,9 +114,6 @@ \list \o When creating an QString to contain a URL from a QByteArray or a char*, always use QString::fromUtf8(). - \o Favor the use of QUrl::fromEncoded() and QUrl::toEncoded() instead of - QUrl(string) and QUrl::toString() when converting a QUrl to or from - a string. \endlist \sa QUrlInfo @@ -318,6 +315,7 @@ public: QString userInfo(QUrl::FormattingOptions options = QUrl::None) const; void setEncodedAuthority(const QByteArray &authority); void setEncodedUserInfo(const QUrlParseData *parseData); + void setEncodedUrl(const QByteArray&, QUrl::ParsingMode); QByteArray mergePaths(const QByteArray &relativePath) const; @@ -4185,11 +4183,7 @@ QString QUrlPrivate::createErrorString() \snippet doc/src/snippets/code/src_corelib_io_qurl.cpp 0 - To construct a URL from an encoded string, call fromEncoded(): - - \snippet doc/src/snippets/code/src_corelib_io_qurl.cpp 1 - - \sa setUrl(), setEncodedUrl(), fromEncoded(), TolerantMode + \sa setUrl(), TolerantMode */ QUrl::QUrl(const QString &url, ParsingMode parsingMode) : d(0) { @@ -4294,7 +4288,7 @@ void QUrl::setUrl(const QString &url, ParsingMode parsingMode) { detach(); - setEncodedUrl(url.toUtf8(), parsingMode); + d->setEncodedUrl(url.toUtf8(), parsingMode); if (isValid() || parsingMode == StrictMode) return; @@ -4327,7 +4321,7 @@ void QUrl::setUrl(const QString &url, ParsingMode parsingMode) } else { encodedUrl = toPercentEncodingHelper(tmp, ABNF_reserved); } - setEncodedUrl(encodedUrl, StrictMode); + d->setEncodedUrl(encodedUrl, StrictMode); } inline static bool isHex(char c) @@ -4342,6 +4336,7 @@ static inline char toHex(quint8 c) } /*! + \fn void QUrl::setEncodedUrl(const QByteArray &encodedUrl, ParsingMode parsingMode) Constructs a URL by parsing the contents of \a encodedUrl. \a encodedUrl is assumed to be a URL string in percent encoded @@ -4349,16 +4344,17 @@ static inline char toHex(quint8 c) The parsing mode \a parsingMode is used for parsing \a encodedUrl. - Use isValid() to determine if a valid URL was constructed. + \obsolete Use setUrl(QString::fromUtf8(encodedUrl), parsingMode) \sa setUrl() */ -void QUrl::setEncodedUrl(const QByteArray &encodedUrl, ParsingMode parsingMode) + + +void QUrlPrivate::setEncodedUrl(const QByteArray &encodedUrl, QUrl::ParsingMode parsingMode) { QByteArray tmp = encodedUrl; - if (!d) d = new QUrlPrivate; - else d->clear(); - if ((d->parsingMode = parsingMode) == TolerantMode) { + clear(); + if ((parsingMode = parsingMode) == QUrl::TolerantMode) { // Replace stray % with %25 QByteArray copy = tmp; for (int i = 0, j = 0; i < copy.size(); ++i, ++j) { @@ -4409,7 +4405,7 @@ void QUrl::setEncodedUrl(const QByteArray &encodedUrl, ParsingMode parsingMode) } } - d->encodedOriginal = tmp; + encodedOriginal = tmp; } /*! @@ -5687,7 +5683,9 @@ static QString toPrettyPercentEncoding(const QString &input, bool forFragment) The resulting QString can be passed back to a QUrl later on. - \sa FormattingOptions, toEncoded() + Synonym for url(options). + + \sa FormattingOptions, toEncoded(), url() */ QString QUrl::toString(FormattingOptions options) const { @@ -5734,6 +5732,22 @@ QString QUrl::toString(FormattingOptions options) const } /*! + Returns the human-displayable string representation of the + URL. The output can be customized by passing flags with \a + options. + + The resulting QString can be passed back to a QUrl later on. + + Synonym for toString(options). + + \sa FormattingOptions, toEncoded(), toString() +*/ +QString QUrl::url(FormattingOptions options) const +{ + return toString(options); +} + +/*! Returns the encoded representation of the URL if it's valid; otherwise an empty QByteArray is returned. The output can be customized by passing flags with \a options. @@ -5749,19 +5763,18 @@ QByteArray QUrl::toEncoded(FormattingOptions options) const } /*! + \fn QUrl QUrl::fromEncoded(const QByteArray &input, ParsingMode parsingMode) + \obsolete + Parses \a input and returns the corresponding QUrl. \a input is assumed to be in encoded form, containing only ASCII characters. The URL is parsed using \a parsingMode. + Use QUrl(QString::fromUtf8(input), parsingMode) instead. + \sa toEncoded(), setUrl() */ -QUrl QUrl::fromEncoded(const QByteArray &input, ParsingMode parsingMode) -{ - QUrl tmp; - tmp.setEncodedUrl(input, parsingMode); - return tmp; -} /*! Returns a decoded copy of \a input. \a input is first decoded from @@ -6215,7 +6228,7 @@ QDataStream &operator>>(QDataStream &in, QUrl &url) { QByteArray u; in >> u; - url = QUrl::fromEncoded(u); + url = QUrl(QString::fromUtf8(u)); return in; } #endif // QT_NO_DATASTREAM @@ -6319,8 +6332,8 @@ QUrl QUrl::fromUserInput(const QString &userInput) if (QDir::isAbsolutePath(trimmedString)) return QUrl::fromLocalFile(trimmedString); - QUrl url = QUrl::fromEncoded(trimmedString.toUtf8(), QUrl::TolerantMode); - QUrl urlPrepended = QUrl::fromEncoded("http://" + trimmedString.toUtf8(), QUrl::TolerantMode); + QUrl url(trimmedString, QUrl::TolerantMode); + QUrl urlPrepended(QString::fromLatin1("http://") + trimmedString, QUrl::TolerantMode); // Check the most common case of a valid url with scheme and host // We check if the port would be valid by adding the scheme to handle the case host:port diff --git a/src/corelib/io/qurl.h b/src/corelib/io/qurl.h index cc56a90..c1ca3d9 100644 --- a/src/corelib/io/qurl.h +++ b/src/corelib/io/qurl.h @@ -100,7 +100,8 @@ public: inline void swap(QUrl &other) { qSwap(d, other.d); } void setUrl(const QString &url, ParsingMode mode = TolerantMode); - void setEncodedUrl(const QByteArray &url, ParsingMode mode = TolerantMode); + QString url(FormattingOptions options = None) const; + QString toString(FormattingOptions options = None) const; bool isValid() const; @@ -185,10 +186,7 @@ public: QString toLocalFile() const; bool isLocalFile() const; - QString toString(FormattingOptions options = None) const; - QByteArray toEncoded(FormattingOptions options = None) const; - static QUrl fromEncoded(const QByteArray &url, ParsingMode mode = TolerantMode); static QUrl fromUserInput(const QString &userInput); @@ -212,6 +210,13 @@ public: QString errorString() const; +#if QT_DEPRECATED_SINCE(5,0) + QT_DEPRECATED void setEncodedUrl(const QByteArray &url, ParsingMode mode = TolerantMode) + { setUrl(QString::fromUtf8(url), mode); } + QT_DEPRECATED static QUrl fromEncoded(const QByteArray &url, ParsingMode mode = TolerantMode) + { return QUrl(QString::fromUtf8(url), mode); } +#endif + private: QUrlPrivate *d; public: diff --git a/tests/auto/corelib/io/qurl/tst_qurl.cpp b/tests/auto/corelib/io/qurl/tst_qurl.cpp index 053a1d2..82f16f1 100644 --- a/tests/auto/corelib/io/qurl/tst_qurl.cpp +++ b/tests/auto/corelib/io/qurl/tst_qurl.cpp @@ -524,13 +524,11 @@ void tst_QUrl::setUrl() } { - QUrl notPretty; - notPretty.setEncodedUrl("http://ferret.lmh.ox.ac.uk/%7Ekdecvs/"); + QUrl notPretty("http://ferret.lmh.ox.ac.uk/%7Ekdecvs/"); QVERIFY(notPretty.isValid()); QCOMPARE(notPretty.toString(), QString::fromLatin1("http://ferret.lmh.ox.ac.uk/~kdecvs/")); - QUrl notPretty2; - notPretty2.setEncodedUrl("file:/home/test/directory%20with%20spaces"); + QUrl notPretty2("file:/home/test/directory%20with%20spaces"); QVERIFY(notPretty2.isValid()); QCOMPARE(notPretty2.toString(), QString::fromLatin1("file:///home/test/directory with spaces")); @@ -557,8 +555,7 @@ void tst_QUrl::setUrl() charles.setPath("/home/charles/foo%20moo"); QCOMPARE(charles.path(), QString::fromLatin1("/home/charles/foo%20moo")); - QUrl charles2; - charles2.setEncodedUrl("file:/home/charles/foo%20moo"); + QUrl charles2("file:/home/charles/foo%20moo"); QCOMPARE(charles2.path(), QString::fromLatin1("/home/charles/foo moo")); } @@ -622,8 +619,7 @@ void tst_QUrl::setUrl() } { - QUrl url; - url.setEncodedUrl("data:text/javascript,d5%20%3D%20'five\\u0027s'%3B"); + QUrl url("data:text/javascript,d5%20%3D%20'five\\u0027s'%3B"); QVERIFY(url.isValid()); QCOMPARE(url.scheme(), QString("data")); QCOMPARE(url.host(), QString());