From 98e935eed5549e479f6666680aed1711dc42111c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Mon, 10 May 2010 14:12:52 +0200 Subject: Revert "Improve QUrl handling of local file paths" This reverts commit a2f797b52c4274a62a7cf1f0939aca1429afe211. --- src/corelib/io/qurl.cpp | 73 +++++++++++++------------------------------- src/corelib/io/qurl.h | 1 - tests/auto/qurl/tst_qurl.cpp | 11 +------ 3 files changed, 23 insertions(+), 62 deletions(-) diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index 7e5c622..d4b8b5f 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -5976,22 +5976,19 @@ bool QUrl::isDetached() const /*! - Returns a QUrl representation of \a localFile, interpreted as a local - file. This function accepts paths separated by slashes as well as the - native separator for this platform. + Returns a QUrl representation of \a localFile, interpreted as a + local file. - This function also accepts paths with a doubled leading slash (or - backslash) to indicate a remote file, as in - "//servername/path/to/file.txt". Note that only certain platforms can - actually open this file using QFile::open(). - - \sa toLocalFile(), isLocalFile(), QDir::toNativeSeparators + \sa toLocalFile() */ QUrl QUrl::fromLocalFile(const QString &localFile) { QUrl url; url.setScheme(QLatin1String("file")); - QString deslashified = QDir::toNativeSeparators(localFile); + QString deslashified = localFile; + deslashified.replace(QLatin1Char('\\'), QLatin1Char('/')); + + // magic for drives on windows if (deslashified.length() > 1 && deslashified.at(1) == QLatin1Char(':') && deslashified.at(0) != QLatin1Char('/')) { @@ -6010,61 +6007,35 @@ QUrl QUrl::fromLocalFile(const QString &localFile) } /*! - Returns the path of this URL formatted as a local file path. The path - returned will use forward slashes, even if it was originally created - from one with backslashes. + Returns the path of this URL formatted as a local file path. - If this URL contains a non-empty hostname, it will be encoded in the - returned value in the form found on SMB networks (for example, - "//servername/path/to/file.txt"). - - \sa fromLocalFile(), isLocalFile() + \sa fromLocalFile() */ QString QUrl::toLocalFile() const { - // the call to isLocalFile() also ensures that we're parsed - if (!isLocalFile()) - return QString(); + if (!d) return QString(); + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); QString tmp; QString ourPath = path(); + if (d->scheme.isEmpty() || QString::compare(d->scheme, QLatin1String("file"), Qt::CaseInsensitive) == 0) { - // magic for shared drive on windows - if (!d->host.isEmpty()) { - tmp = QLatin1String("//") + d->host + (ourPath.length() > 0 && ourPath.at(0) != QLatin1Char('/') - ? QLatin1Char('/') + ourPath : ourPath); - } else { - tmp = ourPath; - // magic for drives on windows - if (ourPath.length() > 2 && ourPath.at(0) == QLatin1Char('/') && ourPath.at(2) == QLatin1Char(':')) - tmp.remove(0, 1); + // magic for shared drive on windows + if (!d->host.isEmpty()) { + tmp = QLatin1String("//") + d->host + (ourPath.length() > 0 && ourPath.at(0) != QLatin1Char('/') + ? QLatin1Char('/') + ourPath : ourPath); + } else { + tmp = ourPath; + // magic for drives on windows + if (ourPath.length() > 2 && ourPath.at(0) == QLatin1Char('/') && ourPath.at(2) == QLatin1Char(':')) + tmp.remove(0, 1); + } } return tmp; } /*! - \since 4.7 - Returns true if this URL is pointing to a local file path. A URL is a - local file path if the scheme is "file". - - Note that this function considers URLs with hostnames to be local file - paths, even if the eventual file path cannot be opened with - QFile::open(). - - \sa fromLocalFile(), toLocalFile() -*/ -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; -} - -/*! Returns true if this URL is a parent of \a childUrl. \a childUrl is a child of this URL if the two URLs share the same scheme and authority, and this URL's path is a parent of the path of \a childUrl. diff --git a/src/corelib/io/qurl.h b/src/corelib/io/qurl.h index 162aa7c..6f8331a 100644 --- a/src/corelib/io/qurl.h +++ b/src/corelib/io/qurl.h @@ -183,7 +183,6 @@ public: static QUrl fromLocalFile(const QString &localfile); QString toLocalFile() const; - bool isLocalFile() const; QString toString(FormattingOptions options = None) const; diff --git a/tests/auto/qurl/tst_qurl.cpp b/tests/auto/qurl/tst_qurl.cpp index 67bf0c1..fa42adc 100644 --- a/tests/auto/qurl/tst_qurl.cpp +++ b/tests/auto/qurl/tst_qurl.cpp @@ -314,7 +314,6 @@ void tst_QUrl::constructing() QUrl buildUNC; - buildUNC.setScheme(QString::fromLatin1("file")); buildUNC.setHost(QString::fromLatin1("somehost")); buildUNC.setPath(QString::fromLatin1("somepath")); QCOMPARE(buildUNC.toLocalFile(), QString::fromLatin1("//somehost/somepath")); @@ -1758,15 +1757,7 @@ void tst_QUrl::toLocalFile_data() QTest::newRow("data7") << QString::fromLatin1("file://somehost/") << QString::fromLatin1("//somehost/"); QTest::newRow("data8") << QString::fromLatin1("file://somehost") << QString::fromLatin1("//somehost"); QTest::newRow("data9") << QString::fromLatin1("file:////somehost/somedir/somefile") << QString::fromLatin1("//somehost/somedir/somefile"); - QTest::newRow("data10") << QString::fromLatin1("FILE:/a.txt") << QString::fromLatin1("/a.txt"); - - // and some that result in empty (i.e., not local) - QTest::newRow("xdata0") << QString::fromLatin1("/a.txt") << QString(); - QTest::newRow("xdata1") << QString::fromLatin1("//a.txt") << QString(); - QTest::newRow("xdata2") << QString::fromLatin1("///a.txt") << QString(); - QTest::newRow("xdata3") << QString::fromLatin1("foo:/a.txt") << QString(); - QTest::newRow("xdata4") << QString::fromLatin1("foo://a.txt") << QString(); - QTest::newRow("xdata5") << QString::fromLatin1("foo:///a.txt") << QString(); + } void tst_QUrl::toLocalFile() -- cgit v0.12