Add QUrl formatting option "PreferLocalFile".

For the readonly case (e.g. progress dialogs), where local file paths
look much nicer to end users than file:/// URLs.

Change-Id: I899fed27cfb73ebf565389cfd489d2d2fcbeac7c
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
David Faure 2012-03-02 15:41:51 +01:00 committed by Qt by Nokia
parent e1b2755083
commit a6a7cabcd3
3 changed files with 35 additions and 16 deletions

View File

@ -170,6 +170,8 @@
\value RemoveQuery The query part of the URL (following a '?' character)
is removed.
\value RemoveFragment
\value PreferLocalFile If the URL is a local file according to isLocalFile()
and contains no query or fragment, a local file path is returned.
\value StripTrailingSlash The trailing slash is removed if one is present.
Note that the case folding rules in \l{RFC 3491}{Nameprep}, which QUrl
@ -331,6 +333,7 @@ public:
void clear();
QByteArray toEncoded(QUrl::FormattingOptions options = QUrl::None) const;
bool isLocalFile() const;
QAtomicInt ref;
@ -3953,6 +3956,9 @@ QByteArray QUrlPrivate::toEncoded(QUrl::FormattingOptions options) const
if (options==0x100) // private - see qHash(QUrl)
return normalized();
if ((options & QUrl::PreferLocalFile) && isLocalFile() && !hasQuery && !hasFragment)
return encodedPath;
QByteArray url;
if (!(options & QUrl::RemoveScheme) && !scheme.isEmpty()) {
@ -5695,9 +5701,12 @@ QString QUrl::toString(FormattingOptions options) const
QString url;
const QString ourPath = path();
if ((options & QUrl::PreferLocalFile) && isLocalFile() && !d->hasQuery && !d->hasFragment)
return ourPath;
if (!(options & QUrl::RemoveScheme) && !d->scheme.isEmpty())
url += d->scheme + QLatin1Char(':');
QString ourPath = path();
if ((options & QUrl::RemoveAuthority) != QUrl::RemoveAuthority) {
bool doFileScheme = d->scheme == QLatin1String("file") && ourPath.startsWith(QLatin1Char('/'));
QString tmp = d->authority(options);
@ -5733,6 +5742,7 @@ QString QUrl::toString(FormattingOptions options) const
}
/*!
\since 5.0
Returns a string representation of the URL.
The output can be customized by passing flags with \a options.
@ -5748,13 +5758,16 @@ QString QUrl::url(FormattingOptions options) const
}
/*!
\since 5.0
Returns a human-displayable string representation of the URL.
The output can be customized by passing flags with \a options.
The option RemovePassword is always enabled, since passwords
should never be shown back to users.
The resulting QString can be passed back to a QUrl later on,
but any password that was present initially will be lost.
With the default options, the resulting QString can be passed back
to a QUrl later on, but any password that was present initially will
be lost.
\sa FormattingOptions, toEncoded(), toString()
*/
@ -6171,6 +6184,13 @@ QString QUrl::toLocalFile() const
return tmp;
}
bool QUrlPrivate::isLocalFile() const
{
if (scheme.compare(QLatin1String("file"), Qt::CaseInsensitive) != 0)
return false; // not file
return true;
}
/*!
\since 4.7
Returns true if this URL is pointing to a local file path. A URL is a
@ -6186,10 +6206,7 @@ bool QUrl::isLocalFile() const
{
if (!d) return false;
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse();
if (d->scheme.compare(QLatin1String("file"), Qt::CaseInsensitive) != 0)
return false; // not file
return true;
return d->isLocalFile();
}
/*!

View File

@ -76,6 +76,7 @@ public:
RemoveQuery = 0x40,
RemoveFragment = 0x80,
// 0x100: private: normalized
PreferLocalFile = 0x200,
StripTrailingSlash = 0x10000
};

View File

@ -262,9 +262,16 @@ void tst_QUrl::hashInPath()
QCOMPARE(withHashInPath.path(), QString::fromLatin1("hi#mum.txt"));
QCOMPARE(withHashInPath.toEncoded(), QByteArray("hi%23mum.txt"));
QCOMPARE(withHashInPath.toString(), QString("hi%23mum.txt"));
QCOMPARE(withHashInPath.toDisplayString(QUrl::PreferLocalFile), QString("hi%23mum.txt"));
QUrl fromHashInPath = QUrl::fromEncoded(withHashInPath.toEncoded());
QVERIFY(withHashInPath == fromHashInPath);
const QUrl localWithHash = QUrl::fromLocalFile("/hi#mum.txt");
QCOMPARE(localWithHash.path(), QString::fromLatin1("/hi#mum.txt"));
QCOMPARE(localWithHash.toEncoded(), QByteArray("file:///hi%23mum.txt"));
QCOMPARE(localWithHash.toString(), QString("file:///hi%23mum.txt"));
QCOMPARE(localWithHash.toDisplayString(QUrl::PreferLocalFile), QString("/hi#mum.txt"));
}
void tst_QUrl::unc()
@ -352,6 +359,7 @@ void tst_QUrl::setUrl()
QCOMPARE(url.port(), -1);
QCOMPARE(url.toString(), QString::fromLatin1("file:///"));
QCOMPARE(url.toDisplayString(), QString::fromLatin1("file:///"));
QCOMPARE(url.toDisplayString(QUrl::PreferLocalFile), QString::fromLatin1("/"));
}
{
@ -367,6 +375,7 @@ void tst_QUrl::setUrl()
QCOMPARE(url.port(), 80);
QCOMPARE(url.toString(), QString::fromLatin1("http://www.foo.bar:80"));
QCOMPARE(url.toDisplayString(), QString::fromLatin1("http://www.foo.bar:80"));
QCOMPARE(url.toDisplayString(QUrl::PreferLocalFile), QString::fromLatin1("http://www.foo.bar:80"));
QUrl url2("//www1.foo.bar");
QCOMPARE(url.resolved(url2).toString(), QString::fromLatin1("http://www1.foo.bar"));
@ -451,15 +460,7 @@ void tst_QUrl::setUrl()
QUrl url = u1;
QVERIFY(url.isValid());
QCOMPARE(url.toString(), QString::fromLatin1("file:///home/dfaure/my#myref"));
QCOMPARE(url.fragment(), QString::fromLatin1("myref"));
}
{
QString u1 = "file:/home/dfaure/my#myref";
QUrl url = u1;
QVERIFY(url.isValid());
QCOMPARE(url.toString(), QString::fromLatin1("file:///home/dfaure/my#myref"));
QCOMPARE(url.toString(QUrl::PreferLocalFile), QString::fromLatin1("file:///home/dfaure/my#myref"));
QCOMPARE(url.fragment(), QString::fromLatin1("myref"));
}