Fix toDisplayString(QUrl::PreferLocalFile) on Win

When using QUrl::PreferLocalFile we do want to strip the leading slash,
as toLocalFile() would do as well.

Behavior change by means of an example:
  QUrl url(QUrl::fromLocalFile("C:/file.txt")
  url.toLocalFile() --> "C:/file.txt"

Before:
  url.toDisplayString(QUrl::PreferLocalFile) --> "/C:/file.txt"

After:
  url.toDisplayString(QUrl::PreferLocalFile) --> "C:/file.txt"

Task-number: QTBUG-41729
Change-Id: I7d425541f6077ebcf3fcf46feeb7e0f03a0d7fe2
Reviewed-by: Dominik Haumann <dhaumann@kde.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Kevin Funk 2016-01-14 23:33:28 +01:00 committed by Dominik Haumann
parent b530ca770f
commit 8dad3bf212
2 changed files with 55 additions and 23 deletions

View File

@ -556,6 +556,7 @@ public:
inline bool hasFragment() const { return sectionIsPresent & Fragment; }
inline bool isLocalFile() const { return flags & IsLocalFile; }
QString toLocalFile(QUrl::FormattingOptions options) const;
QString mergePaths(const QString &relativePath) const;
@ -1460,6 +1461,33 @@ inline void QUrlPrivate::parse(const QString &url, QUrl::ParsingMode parsingMode
validateComponent(Fragment, url, hash + 1, len);
}
QString QUrlPrivate::toLocalFile(QUrl::FormattingOptions options) const
{
QString tmp;
QString ourPath;
appendPath(ourPath, options, QUrlPrivate::Path);
// magic for shared drive on windows
if (!host.isEmpty()) {
tmp = QStringLiteral("//") + host;
#ifdef Q_OS_WIN // QTBUG-42346, WebDAV is visible as local file on Windows only.
if (scheme == webDavScheme())
tmp += webDavSslTag();
#endif
if (!ourPath.isEmpty() && !ourPath.startsWith(QLatin1Char('/')))
tmp += QLatin1Char('/');
tmp += ourPath;
} else {
tmp = ourPath;
#ifdef Q_OS_WIN
// magic for drives on windows
if (ourPath.length() > 2 && ourPath.at(0) == QLatin1Char('/') && ourPath.at(2) == QLatin1Char(':'))
tmp.remove(0, 1);
#endif
}
return tmp;
}
/*
From http://www.ietf.org/rfc/rfc3986.txt, 5.2.3: Merge paths
@ -3257,7 +3285,7 @@ QString QUrl::toString(FormattingOptions options) const
&& (!d->hasQuery() || options.testFlag(QUrl::RemoveQuery))
&& (!d->hasFragment() || options.testFlag(QUrl::RemoveFragment))
&& isLocalFile()) {
return path(options);
return d->toLocalFile(options);
}
QString url;
@ -3820,28 +3848,7 @@ QString QUrl::toLocalFile() const
if (!isLocalFile())
return QString();
QString tmp;
QString ourPath = path(QUrl::FullyDecoded);
// magic for shared drive on windows
if (!d->host.isEmpty()) {
tmp = QStringLiteral("//") + host();
#ifdef Q_OS_WIN // QTBUG-42346, WebDAV is visible as local file on Windows only.
if (scheme() == webDavScheme())
tmp += webDavSslTag();
#endif
if (!ourPath.isEmpty() && !ourPath.startsWith(QLatin1Char('/')))
tmp += QLatin1Char('/');
tmp += ourPath;
} else {
tmp = ourPath;
#ifdef Q_OS_WIN
// magic for drives on windows
if (ourPath.length() > 2 && ourPath.at(0) == QLatin1Char('/') && ourPath.at(2) == QLatin1Char(':'))
tmp.remove(0, 1);
#endif
}
return tmp;
return d->toLocalFile(QUrl::FullyDecoded);
}
/*!

View File

@ -69,6 +69,8 @@ private slots:
void resolving();
void toString_data();
void toString();
void toString_PreferLocalFile_data();
void toString_PreferLocalFile();
void toString_constructed_data();
void toString_constructed();
void toAndFromStringList_data();
@ -1050,6 +1052,29 @@ void tst_QUrl::toString()
QCOMPARE(url.adjusted(opt).toString(), string);
}
void tst_QUrl::toString_PreferLocalFile_data()
{
QTest::addColumn<QUrl>("url");
QTest::addColumn<QString>("string");
#ifdef Q_OS_WIN
QTest::newRow("win-drive") << QUrl(QString::fromLatin1("file:///c:/windows/regedit.exe"))
<< QString::fromLatin1("c:/windows/regedit.exe");
QTest::newRow("win-share") << QUrl(QString::fromLatin1("//Anarki/homes"))
<< QString::fromLatin1("//anarki/homes");
#else
QTest::newRow("unix-path") << QUrl(QString::fromLatin1("file:///tmp"))
<< QString::fromLatin1("/tmp");
#endif
}
void tst_QUrl::toString_PreferLocalFile()
{
QFETCH(QUrl, url);
QFETCH(QString, string);
QCOMPARE(url.toString(QUrl::PreferLocalFile), string);
}
void tst_QUrl::toAndFromStringList_data()
{