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 <thiago.macieira@intel.com>
This commit is contained in:
David Faure 2012-01-26 01:57:59 +01:00 committed by Qt by Nokia
parent e650dd3b6d
commit 27d9fef14a
3 changed files with 52 additions and 38 deletions

View File

@ -114,9 +114,6 @@
\list \list
\o When creating an QString to contain a URL from a QByteArray or a \o When creating an QString to contain a URL from a QByteArray or a
char*, always use QString::fromUtf8(). 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 \endlist
\sa QUrlInfo \sa QUrlInfo
@ -318,6 +315,7 @@ public:
QString userInfo(QUrl::FormattingOptions options = QUrl::None) const; QString userInfo(QUrl::FormattingOptions options = QUrl::None) const;
void setEncodedAuthority(const QByteArray &authority); void setEncodedAuthority(const QByteArray &authority);
void setEncodedUserInfo(const QUrlParseData *parseData); void setEncodedUserInfo(const QUrlParseData *parseData);
void setEncodedUrl(const QByteArray&, QUrl::ParsingMode);
QByteArray mergePaths(const QByteArray &relativePath) const; QByteArray mergePaths(const QByteArray &relativePath) const;
@ -4185,11 +4183,7 @@ QString QUrlPrivate::createErrorString()
\snippet doc/src/snippets/code/src_corelib_io_qurl.cpp 0 \snippet doc/src/snippets/code/src_corelib_io_qurl.cpp 0
To construct a URL from an encoded string, call fromEncoded(): \sa setUrl(), TolerantMode
\snippet doc/src/snippets/code/src_corelib_io_qurl.cpp 1
\sa setUrl(), setEncodedUrl(), fromEncoded(), TolerantMode
*/ */
QUrl::QUrl(const QString &url, ParsingMode parsingMode) : d(0) QUrl::QUrl(const QString &url, ParsingMode parsingMode) : d(0)
{ {
@ -4294,7 +4288,7 @@ void QUrl::setUrl(const QString &url, ParsingMode parsingMode)
{ {
detach(); detach();
setEncodedUrl(url.toUtf8(), parsingMode); d->setEncodedUrl(url.toUtf8(), parsingMode);
if (isValid() || parsingMode == StrictMode) if (isValid() || parsingMode == StrictMode)
return; return;
@ -4327,7 +4321,7 @@ void QUrl::setUrl(const QString &url, ParsingMode parsingMode)
} else { } else {
encodedUrl = toPercentEncodingHelper(tmp, ABNF_reserved); encodedUrl = toPercentEncodingHelper(tmp, ABNF_reserved);
} }
setEncodedUrl(encodedUrl, StrictMode); d->setEncodedUrl(encodedUrl, StrictMode);
} }
inline static bool isHex(char c) 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. Constructs a URL by parsing the contents of \a encodedUrl.
\a encodedUrl is assumed to be a URL string in percent encoded \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. 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() \sa setUrl()
*/ */
void QUrl::setEncodedUrl(const QByteArray &encodedUrl, ParsingMode parsingMode)
void QUrlPrivate::setEncodedUrl(const QByteArray &encodedUrl, QUrl::ParsingMode parsingMode)
{ {
QByteArray tmp = encodedUrl; QByteArray tmp = encodedUrl;
if (!d) d = new QUrlPrivate; clear();
else d->clear(); if ((parsingMode = parsingMode) == QUrl::TolerantMode) {
if ((d->parsingMode = parsingMode) == TolerantMode) {
// Replace stray % with %25 // Replace stray % with %25
QByteArray copy = tmp; QByteArray copy = tmp;
for (int i = 0, j = 0; i < copy.size(); ++i, ++j) { 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. 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 QString QUrl::toString(FormattingOptions options) const
{ {
@ -5733,6 +5731,22 @@ QString QUrl::toString(FormattingOptions options) const
return url; return url;
} }
/*!
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; Returns the encoded representation of the URL if it's valid;
otherwise an empty QByteArray is returned. The output can be otherwise an empty QByteArray is returned. The output can be
@ -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 Parses \a input and returns the corresponding QUrl. \a input is
assumed to be in encoded form, containing only ASCII characters. assumed to be in encoded form, containing only ASCII characters.
The URL is parsed using \a parsingMode. The URL is parsed using \a parsingMode.
Use QUrl(QString::fromUtf8(input), parsingMode) instead.
\sa toEncoded(), setUrl() \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 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; QByteArray u;
in >> u; in >> u;
url = QUrl::fromEncoded(u); url = QUrl(QString::fromUtf8(u));
return in; return in;
} }
#endif // QT_NO_DATASTREAM #endif // QT_NO_DATASTREAM
@ -6319,8 +6332,8 @@ QUrl QUrl::fromUserInput(const QString &userInput)
if (QDir::isAbsolutePath(trimmedString)) if (QDir::isAbsolutePath(trimmedString))
return QUrl::fromLocalFile(trimmedString); return QUrl::fromLocalFile(trimmedString);
QUrl url = QUrl::fromEncoded(trimmedString.toUtf8(), QUrl::TolerantMode); QUrl url(trimmedString, QUrl::TolerantMode);
QUrl urlPrepended = QUrl::fromEncoded("http://" + trimmedString.toUtf8(), QUrl::TolerantMode); QUrl urlPrepended(QString::fromLatin1("http://") + trimmedString, QUrl::TolerantMode);
// Check the most common case of a valid url with scheme and host // 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 // We check if the port would be valid by adding the scheme to handle the case host:port

View File

@ -100,7 +100,8 @@ public:
inline void swap(QUrl &other) { qSwap(d, other.d); } inline void swap(QUrl &other) { qSwap(d, other.d); }
void setUrl(const QString &url, ParsingMode mode = TolerantMode); 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; bool isValid() const;
@ -185,10 +186,7 @@ public:
QString toLocalFile() const; QString toLocalFile() const;
bool isLocalFile() const; bool isLocalFile() const;
QString toString(FormattingOptions options = None) const;
QByteArray toEncoded(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); static QUrl fromUserInput(const QString &userInput);
@ -212,6 +210,13 @@ public:
QString errorString() const; 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: private:
QUrlPrivate *d; QUrlPrivate *d;
public: public:

View File

@ -524,13 +524,11 @@ void tst_QUrl::setUrl()
} }
{ {
QUrl notPretty; QUrl notPretty("http://ferret.lmh.ox.ac.uk/%7Ekdecvs/");
notPretty.setEncodedUrl("http://ferret.lmh.ox.ac.uk/%7Ekdecvs/");
QVERIFY(notPretty.isValid()); QVERIFY(notPretty.isValid());
QCOMPARE(notPretty.toString(), QString::fromLatin1("http://ferret.lmh.ox.ac.uk/~kdecvs/")); QCOMPARE(notPretty.toString(), QString::fromLatin1("http://ferret.lmh.ox.ac.uk/~kdecvs/"));
QUrl notPretty2; QUrl notPretty2("file:/home/test/directory%20with%20spaces");
notPretty2.setEncodedUrl("file:/home/test/directory%20with%20spaces");
QVERIFY(notPretty2.isValid()); QVERIFY(notPretty2.isValid());
QCOMPARE(notPretty2.toString(), QString::fromLatin1("file:///home/test/directory with spaces")); 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"); charles.setPath("/home/charles/foo%20moo");
QCOMPARE(charles.path(), QString::fromLatin1("/home/charles/foo%20moo")); QCOMPARE(charles.path(), QString::fromLatin1("/home/charles/foo%20moo"));
QUrl charles2; QUrl charles2("file:/home/charles/foo%20moo");
charles2.setEncodedUrl("file:/home/charles/foo%20moo");
QCOMPARE(charles2.path(), QString::fromLatin1("/home/charles/foo moo")); QCOMPARE(charles2.path(), QString::fromLatin1("/home/charles/foo moo"));
} }
@ -622,8 +619,7 @@ void tst_QUrl::setUrl()
} }
{ {
QUrl url; QUrl url("data:text/javascript,d5%20%3D%20'five\\u0027s'%3B");
url.setEncodedUrl("data:text/javascript,d5%20%3D%20'five\\u0027s'%3B");
QVERIFY(url.isValid()); QVERIFY(url.isValid());
QCOMPARE(url.scheme(), QString("data")); QCOMPARE(url.scheme(), QString("data"));
QCOMPARE(url.host(), QString()); QCOMPARE(url.host(), QString());