summaryrefslogtreecommitdiffstats
path: root/src/corelib/io/qurl.cpp
diff options
context:
space:
mode:
authorDavid Faure <faure@kde.org>2013-07-06 09:52:49 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-07-08 15:20:42 +0200
commit602c911820fd8b0832dba64b37b3110580f381ae (patch)
tree872eaccaff606a1858117d27d162863df5786357 /src/corelib/io/qurl.cpp
parent67ec78aac1cc0d490d8531cb2fa0c044c58da4ee (diff)
QUrl: add "QUrl adjusted(options)" convenience method.
Change-Id: I5eea3e0dc7b56b88a56d813207b04661b8f05a55 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/io/qurl.cpp')
-rw-r--r--src/corelib/io/qurl.cpp63
1 files changed, 55 insertions, 8 deletions
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('?');
@@ -3188,6 +3189,52 @@ QString QUrl::toDisplayString(FormattingOptions options) const
}
/*!
+ \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
customized by passing flags with \a options.