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

View File

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

View File

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