From 74d2dba46041448c70dbd3049ae2a8277770baf6 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 13 Oct 2011 19:56:28 +0200 Subject: Port to the new QUrl API The use of any broken-down components of the query now needs QUrlQuery. The QUrl constructor and toString() are now rehabilitated and the preferred forms. Use toEncoded() and fromEncoded() now only when we need to store data in a QByteArray or the data comes from a QByteArray anyway. Change to toString() or the constructor if the data was in a QString. Change-Id: I9d761a628bef9c70185a48e927a61779a1642342 Reviewed-by: Lars Knoll --- src/corelib/io/qurlquery.cpp | 2 +- src/gui/text/qtextimagehandler.cpp | 4 ++-- src/gui/text/qtextodfwriter.cpp | 2 +- src/network/access/qhttpnetworkrequest.cpp | 15 +++++++-------- src/network/access/qhttpthreaddelegate.cpp | 16 ++++++++-------- src/network/access/qnetworkaccessdebugpipebackend.cpp | 3 ++- src/network/access/qnetworkreplydataimpl.cpp | 2 +- src/network/access/qnetworkreplyhttpimpl.cpp | 2 +- src/testlib/qtest.h | 2 +- src/widgets/widgets/qtextbrowser.cpp | 2 +- 10 files changed, 25 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/corelib/io/qurlquery.cpp b/src/corelib/io/qurlquery.cpp index 042f9a278b..b1c4b7d02c 100644 --- a/src/corelib/io/qurlquery.cpp +++ b/src/corelib/io/qurlquery.cpp @@ -337,7 +337,7 @@ QUrlQuery::QUrlQuery(const QUrl &url) // use internals to avoid unnecessary recoding // ### FIXME: actually do it if (url.hasQuery()) - d = new QUrlQueryPrivate(QString::fromUtf8(url.encodedQuery())); + d = new QUrlQueryPrivate(url.query()); } /*! diff --git a/src/gui/text/qtextimagehandler.cpp b/src/gui/text/qtextimagehandler.cpp index abd283e459..6804dba95c 100644 --- a/src/gui/text/qtextimagehandler.cpp +++ b/src/gui/text/qtextimagehandler.cpp @@ -59,7 +59,7 @@ static QPixmap getPixmap(QTextDocument *doc, const QTextImageFormat &format) QString name = format.name(); if (name.startsWith(QLatin1String(":/"))) // auto-detect resources name.prepend(QLatin1String("qrc")); - QUrl url = QUrl::fromEncoded(name.toUtf8()); + QUrl url = QUrl(name); const QVariant data = doc->resource(QTextDocument::ImageResource, url); if (data.type() == QVariant::Pixmap || data.type() == QVariant::Image) { pm = qvariant_cast(data); @@ -134,7 +134,7 @@ static QImage getImage(QTextDocument *doc, const QTextImageFormat &format) QString name = format.name(); if (name.startsWith(QLatin1String(":/"))) // auto-detect resources name.prepend(QLatin1String("qrc")); - QUrl url = QUrl::fromEncoded(name.toUtf8()); + QUrl url = QUrl(name); const QVariant data = doc->resource(QTextDocument::ImageResource, url); if (data.type() == QVariant::Image) { image = qvariant_cast(data); diff --git a/src/gui/text/qtextodfwriter.cpp b/src/gui/text/qtextodfwriter.cpp index 80adeb602c..c03805b95f 100644 --- a/src/gui/text/qtextodfwriter.cpp +++ b/src/gui/text/qtextodfwriter.cpp @@ -366,7 +366,7 @@ void QTextOdfWriter::writeInlineCharacter(QXmlStreamWriter &writer, const QTextF QString name = imageFormat.name(); if (name.startsWith(QLatin1String(":/"))) // auto-detect resources name.prepend(QLatin1String("qrc")); - QUrl url = QUrl::fromEncoded(name.toUtf8()); + QUrl url = QUrl(name); const QVariant data = m_document->resource(QTextDocument::ImageResource, url); if (data.type() == QVariant::Image) { image = qvariant_cast(data); diff --git a/src/network/access/qhttpnetworkrequest.cpp b/src/network/access/qhttpnetworkrequest.cpp index f32251935b..1325f105cf 100644 --- a/src/network/access/qhttpnetworkrequest.cpp +++ b/src/network/access/qhttpnetworkrequest.cpp @@ -116,19 +116,18 @@ QByteArray QHttpNetworkRequestPrivate::methodName() const QByteArray QHttpNetworkRequestPrivate::uri(bool throughProxy) const { - QUrl::FormattingOptions format(QUrl::RemoveFragment); + QUrl::FormattingOptions format(QUrl::RemoveFragment | QUrl::RemoveUserInfo | QUrl::FullyEncoded); // for POST, query data is send as content if (operation == QHttpNetworkRequest::Post && !uploadByteDevice) format |= QUrl::RemoveQuery; // for requests through proxy, the Request-URI contains full url - if (throughProxy) - format |= QUrl::RemoveUserInfo; - else + if (!throughProxy) format |= QUrl::RemoveScheme | QUrl::RemoveAuthority; - QByteArray uri = url.toEncoded(format); - if (uri.isEmpty() || (throughProxy && url.path().isEmpty())) - uri += '/'; + QUrl copy = url; + if (copy.path().isEmpty()) + copy.setPath(QStringLiteral("/")); + QByteArray uri = copy.toEncoded(format); return uri; } @@ -163,7 +162,7 @@ QByteArray QHttpNetworkRequestPrivate::header(const QHttpNetworkRequest &request ba += "Content-Type: application/octet-stream\r\n"; } if (!request.d->uploadByteDevice && request.d->url.hasQuery()) { - QByteArray query = request.d->url.encodedQuery(); + QByteArray query = request.d->url.query(QUrl::FullyEncoded).toLatin1(); ba += "Content-Length: "; ba += QByteArray::number(query.size()); ba += "\r\n\r\n"; diff --git a/src/network/access/qhttpthreaddelegate.cpp b/src/network/access/qhttpthreaddelegate.cpp index c8b4c51e23..634340bb54 100644 --- a/src/network/access/qhttpthreaddelegate.cpp +++ b/src/network/access/qhttpthreaddelegate.cpp @@ -105,12 +105,12 @@ static QNetworkReply::NetworkError statusCodeFromHttp(int httpStatusCode, const static QByteArray makeCacheKey(QUrl &url, QNetworkProxy *proxy) { - QByteArray result; + QString result; QUrl copy = url; bool isEncrypted = copy.scheme().toLower() == QLatin1String("https"); copy.setPort(copy.port(isEncrypted ? 443 : 80)); - result = copy.toEncoded(QUrl::RemoveUserInfo | QUrl::RemovePath | - QUrl::RemoveQuery | QUrl::RemoveFragment); + result = copy.toString(QUrl::RemoveUserInfo | QUrl::RemovePath | + QUrl::RemoveQuery | QUrl::RemoveFragment | QUrl::FullyEncoded); #ifndef QT_NO_NETWORKPROXY if (proxy && proxy->type() != QNetworkProxy::NoProxy) { @@ -134,15 +134,15 @@ static QByteArray makeCacheKey(QUrl &url, QNetworkProxy *proxy) key.setUserName(proxy->user()); key.setHost(proxy->hostName()); key.setPort(proxy->port()); - key.setEncodedQuery(result); - result = key.toEncoded(); + key.setQuery(result); + result = key.toString(QUrl::FullyEncoded); } } #else Q_UNUSED(proxy) #endif - return "http-connection:" + result; + return "http-connection:" + result.toLatin1(); } class QNetworkAccessCachedHttpConnection: public QHttpNetworkConnection, @@ -386,7 +386,7 @@ void QHttpThreadDelegate::finishedSlot() // it's an error reply QString msg = QLatin1String(QT_TRANSLATE_NOOP("QNetworkReply", "Error downloading %1 - server replied: %2")); - msg = msg.arg(QString::fromAscii(httpRequest.url().toEncoded()), httpReply->reasonPhrase()); + msg = msg.arg(httpRequest.url().toString(), httpReply->reasonPhrase()); emit error(statusCodeFromHttp(httpReply->statusCode(), httpRequest.url()), msg); } @@ -406,7 +406,7 @@ void QHttpThreadDelegate::synchronousFinishedSlot() // it's an error reply QString msg = QLatin1String(QT_TRANSLATE_NOOP("QNetworkReply", "Error downloading %1 - server replied: %2")); - incomingErrorDetail = msg.arg(QString::fromAscii(httpRequest.url().toEncoded()), httpReply->reasonPhrase()); + incomingErrorDetail = msg.arg(httpRequest.url().toString(), httpReply->reasonPhrase()); incomingErrorCode = statusCodeFromHttp(httpReply->statusCode(), httpRequest.url()); } diff --git a/src/network/access/qnetworkaccessdebugpipebackend.cpp b/src/network/access/qnetworkaccessdebugpipebackend.cpp index 5a4cd7b20d..3fb882b5e4 100644 --- a/src/network/access/qnetworkaccessdebugpipebackend.cpp +++ b/src/network/access/qnetworkaccessdebugpipebackend.cpp @@ -42,6 +42,7 @@ #include "qnetworkaccessdebugpipebackend_p.h" #include "QtCore/qdatastream.h" #include +#include #include "private/qnoncontiguousbytedevice_p.h" QT_BEGIN_NAMESPACE @@ -99,7 +100,7 @@ void QNetworkAccessDebugPipeBackend::open() // socket bytes written -> we can push more from upstream to socket connect(&socket, SIGNAL(bytesWritten(qint64)), SLOT(socketBytesWritten(qint64))); - bareProtocol = url().queryItemValue(QLatin1String("bare")) == QLatin1String("1"); + bareProtocol = QUrlQuery(url()).queryItemValue(QLatin1String("bare")) == QLatin1String("1"); if (operation() == QNetworkAccessManager::PutOperation) { uploadByteDevice = createUploadByteDevice(); diff --git a/src/network/access/qnetworkreplydataimpl.cpp b/src/network/access/qnetworkreplydataimpl.cpp index ab2c97b653..7a8d4ee3e0 100644 --- a/src/network/access/qnetworkreplydataimpl.cpp +++ b/src/network/access/qnetworkreplydataimpl.cpp @@ -88,7 +88,7 @@ QNetworkReplyDataImpl::QNetworkReplyDataImpl(QObject *parent, const QNetworkRequ } else { // something wrong with this URI const QString msg = QCoreApplication::translate("QNetworkAccessDataBackend", - "Invalid URI: %1").arg(QString::fromLatin1(url.toEncoded())); + "Invalid URI: %1").arg(url.toString()); setError(QNetworkReply::ProtocolFailure, msg); QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection, Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ProtocolFailure)); diff --git a/src/network/access/qnetworkreplyhttpimpl.cpp b/src/network/access/qnetworkreplyhttpimpl.cpp index a914ee3f04..e019ade314 100644 --- a/src/network/access/qnetworkreplyhttpimpl.cpp +++ b/src/network/access/qnetworkreplyhttpimpl.cpp @@ -1039,7 +1039,7 @@ void QNetworkReplyHttpImplPrivate::checkForRedirect(const int statusCode) // The response to a 303 MUST NOT be cached, while the response to // all of the others is cacheable if the headers indicate it to be QByteArray header = q->rawHeader("location"); - QUrl url = QUrl::fromEncoded(header); + QUrl url = QUrl(QString::fromUtf8(header)); if (!url.isValid()) url = QUrl(QLatin1String(header)); // FIXME? diff --git a/src/testlib/qtest.h b/src/testlib/qtest.h index 392e223e39..90705b3d40 100644 --- a/src/testlib/qtest.h +++ b/src/testlib/qtest.h @@ -142,7 +142,7 @@ template<> inline char *toString(const QRectF &s) template<> inline char *toString(const QUrl &uri) { - return qstrdup(uri.toEncoded().constData()); + return qstrdup(uri.toEncoded(QUrl::DecodeUnambiguousDelimiters).constData()); } template<> inline char *toString(const QVariant &v) diff --git a/src/widgets/widgets/qtextbrowser.cpp b/src/widgets/widgets/qtextbrowser.cpp index 050730ec2a..261b96f8af 100644 --- a/src/widgets/widgets/qtextbrowser.cpp +++ b/src/widgets/widgets/qtextbrowser.cpp @@ -139,7 +139,7 @@ public: // re-imlemented from QTextEditPrivate virtual QUrl resolveUrl(const QUrl &url) const; inline QUrl resolveUrl(const QString &url) const - { return resolveUrl(QUrl::fromEncoded(url.toUtf8())); } + { return resolveUrl(QUrl(url)); } #ifdef QT_KEYPAD_NAVIGATION void keypadMove(bool next); -- cgit v1.2.3