From 602c911820fd8b0832dba64b37b3110580f381ae Mon Sep 17 00:00:00 2001 From: David Faure Date: Sat, 6 Jul 2013 09:52:49 +0200 Subject: QUrl: add "QUrl adjusted(options)" convenience method. Change-Id: I5eea3e0dc7b56b88a56d813207b04661b8f05a55 Reviewed-by: Thiago Macieira --- src/corelib/io/qurl.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++------- src/corelib/io/qurl.h | 1 + 2 files changed, 56 insertions(+), 8 deletions(-) (limited to 'src/corelib/io') diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index c5f781ae33..11cc6ed7da 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -799,12 +799,17 @@ inline void QUrlPrivate::appendPassword(QString &appendTo, QUrl::FormattingOptio inline void QUrlPrivate::appendPath(QString &appendTo, QUrl::FormattingOptions options, Section appendingTo) const { + QString thePath = path; + // check if we need to remove trailing slashes + if ((options & QUrl::StripTrailingSlash) && !thePath.isEmpty() && thePath != QLatin1String("/") && thePath.endsWith(QLatin1Char('/'))) + thePath.chop(1); + if (appendingTo != Path && !(options & QUrl::EncodeDelimiters)) { - if (!qt_urlRecode(appendTo, path.constData(), path.constEnd(), options, decodedPathInUrlActions)) - appendTo += path; + if (!qt_urlRecode(appendTo, thePath.constData(), thePath.constEnd(), options, decodedPathInUrlActions)) + appendTo += thePath; } else { - appendToUser(appendTo, path, options, encodedPathActions, decodedPathInIsolationActions); + appendToUser(appendTo, thePath, options, encodedPathActions, decodedPathInIsolationActions); } } @@ -3148,12 +3153,8 @@ QString QUrl::toString(FormattingOptions options) const url += QLatin1String("//"); } - if (!(options & QUrl::RemovePath)) { + if (!(options & QUrl::RemovePath)) d->appendPath(url, options, QUrlPrivate::FullUrl); - // check if we need to remove trailing slashes - if ((options & StripTrailingSlash) && !d->path.isEmpty() && d->path != QLatin1String("/") && url.endsWith(QLatin1Char('/'))) - url.chop(1); - } if (!(options & QUrl::RemoveQuery) && d->hasQuery()) { url += QLatin1Char('?'); @@ -3187,6 +3188,52 @@ QString QUrl::toDisplayString(FormattingOptions options) const return toString(options | RemovePassword); } +/*! + \since 5.2 + + Returns an adjusted version of the URL. + The output can be customized by passing flags with \a options. + + The encoding options from QUrl::ComponentFormattingOption don't make + much sense for this method, nor does QUrl::PreferLocalFile. + + This is always equivalent to QUrl(url.toString(options)). + + \sa FormattingOptions, toEncoded(), toString() +*/ +QUrl QUrl::adjusted(QUrl::FormattingOptions options) const +{ + if (!isValid()) { + // also catches isEmpty() + return QUrl(); + } + QUrl that = *this; + if (options & RemoveScheme) + that.setScheme(QString()); + if ((options & RemoveAuthority) == RemoveAuthority) { + that.setAuthority(QString()); + } else { + if ((options & RemoveUserInfo) == RemoveUserInfo) + that.setUserInfo(QString()); + else if (options & RemovePassword) + that.setPassword(QString()); + if (options & RemovePort) + that.setPort(-1); + } + if (options & RemoveQuery) + that.setQuery(QString()); + if (options & RemoveFragment) + that.setFragment(QString()); + if (options & RemovePath) { + that.setPath(QString()); + } else if (options & StripTrailingSlash) { + QString path; + d->appendPath(path, options, QUrlPrivate::Path); + that.setPath(path); + } + return that; +} + /*! Returns the encoded representation of the URL if it's valid; otherwise an empty QByteArray is returned. The output can be diff --git a/src/corelib/io/qurl.h b/src/corelib/io/qurl.h index de4ce754fb..cb7bcf5409 100644 --- a/src/corelib/io/qurl.h +++ b/src/corelib/io/qurl.h @@ -181,6 +181,7 @@ public: QString url(FormattingOptions options = FormattingOptions(PrettyDecoded)) const; QString toString(FormattingOptions options = FormattingOptions(PrettyDecoded)) const; QString toDisplayString(FormattingOptions options = FormattingOptions(PrettyDecoded)) const; + QUrl adjusted(FormattingOptions options) const; QByteArray toEncoded(FormattingOptions options = FullyEncoded) const; static QUrl fromEncoded(const QByteArray &url, ParsingMode mode = TolerantMode); -- cgit v1.2.3