diff --git a/src/libs/utils/filepath.cpp b/src/libs/utils/filepath.cpp index dcf1ff6d07e..57cafaba175 100644 --- a/src/libs/utils/filepath.cpp +++ b/src/libs/utils/filepath.cpp @@ -200,8 +200,8 @@ QString FilePath::toString() const return path(); if (isRelativePath()) - return scheme() + "://" + encodedHost() + "/./" + path(); - return scheme() + "://" + encodedHost() + path(); + return scheme() + "://" + encodedHost() + "/./" + pathView(); + return scheme() + "://" + encodedHost() + pathView(); } QString FilePath::toFSPathString() const @@ -210,8 +210,8 @@ QString FilePath::toFSPathString() const return path(); if (isRelativePath()) - return specialPath(SpecialPathComponent::RootPath) + "/" + scheme() + "/" + encodedHost() + "/./" + path(); - return specialPath(SpecialPathComponent::RootPath) + "/" + scheme() + "/" + encodedHost() + path(); + return specialRootPath() + '/' + scheme() + '/' + encodedHost() + "/./" + pathView(); + return specialRootPath() + '/' + scheme() + '/' + encodedHost() + pathView(); } QUrl FilePath::toUrl() const @@ -710,25 +710,28 @@ FilePath FilePath::absoluteFilePath() const return FilePath::currentWorkingPath().resolvePath(*this); } -QString FilePath::specialPath(SpecialPathComponent component) +const QString &FilePath::specialRootName() { - switch (component) { - case SpecialPathComponent::RootName: - return QLatin1String("__qtc_devices__"); - case SpecialPathComponent::RootPath: - return (QDir::rootPath() + "__qtc_devices__"); - case SpecialPathComponent::DeviceRootName: - return QLatin1String("device"); - case SpecialPathComponent::DeviceRootPath: - return QDir::rootPath() + "__qtc_devices__/device"; - } - - QTC_ASSERT(false, return {}); + static const QString rootName = "__qtc_devices__"; + return rootName; } -FilePath FilePath::specialFilePath(SpecialPathComponent component) +const QString &FilePath::specialRootPath() { - return FilePath::fromString(specialPath(component)); + static const QString rootPath = QDir::rootPath() + u"__qtc_devices__"; + return rootPath; +} + +const QString &FilePath::specialDeviceRootName() +{ + static const QString deviceRootName = "device"; + return deviceRootName; +} + +const QString &FilePath::specialDeviceRootPath() +{ + static const QString deviceRootPath = QDir::rootPath() + u"__qtc_devices__/device"; + return deviceRootPath; } FilePath FilePath::normalizedPathName() const diff --git a/src/libs/utils/filepath.h b/src/libs/utils/filepath.h index c91403b5f74..cf9cf2cf1b3 100644 --- a/src/libs/utils/filepath.h +++ b/src/libs/utils/filepath.h @@ -223,15 +223,10 @@ public: [[nodiscard]] static FilePath fromFileInfo(const QFileInfo &info); // Support for FSEngine. Do not use unless needed. - enum class SpecialPathComponent { - RootName, - RootPath, - DeviceRootName, - DeviceRootPath, - }; - - [[nodiscard]] static QString specialPath(SpecialPathComponent component); - [[nodiscard]] static FilePath specialFilePath(SpecialPathComponent component); + [[nodiscard]] static const QString &specialRootName(); + [[nodiscard]] static const QString &specialRootPath(); + [[nodiscard]] static const QString &specialDeviceRootName(); + [[nodiscard]] static const QString &specialDeviceRootPath(); [[nodiscard]] bool ensureReachable(const FilePath &other) const; diff --git a/src/libs/utils/fsengine/fileiconprovider.cpp b/src/libs/utils/fsengine/fileiconprovider.cpp index 12ca91e2a2b..33762b0e36b 100644 --- a/src/libs/utils/fsengine/fileiconprovider.cpp +++ b/src/libs/utils/fsengine/fileiconprovider.cpp @@ -174,8 +174,7 @@ QIcon FileIconProviderImplementation::icon(const QFileInfo &fi) const return unknownFileIcon(); // Check if its one of the virtual devices directories - if (filePath.path().startsWith( - FilePath::specialPath(FilePath::SpecialPathComponent::RootPath))) { + if (filePath.path().startsWith(FilePath::specialRootPath())) { // If the filepath does not need a device, it is a virtual device directory if (!filePath.needsDevice()) return dirIcon(); @@ -221,7 +220,7 @@ QIcon FileIconProviderImplementation::icon(const FilePath &filePath) const return unknownFileIcon(); // Check if its one of the virtual devices directories - if (filePath.path().startsWith(FilePath::specialPath(FilePath::SpecialPathComponent::RootPath))) { + if (filePath.path().startsWith(FilePath::specialRootPath())) { // If the filepath does not need a device, it is a virtual device directory if (!filePath.needsDevice()) return dirIcon(); diff --git a/src/libs/utils/fsengine/fileiteratordevicesappender.h b/src/libs/utils/fsengine/fileiteratordevicesappender.h index fbbd57ded88..b270bed150d 100644 --- a/src/libs/utils/fsengine/fileiteratordevicesappender.h +++ b/src/libs/utils/fsengine/fileiteratordevicesappender.h @@ -74,7 +74,7 @@ public: QString currentFileName() const override { if (m_status == State::Ended) - return FilePath::specialPath(FilePath::SpecialPathComponent::RootPath); + return FilePath::specialRootPath(); setPath(); checkStatus(); @@ -83,7 +83,7 @@ public: QFileInfo currentFileInfo() const override { if (m_status == State::Ended) - return QFileInfo(FilePath::specialPath(FilePath::SpecialPathComponent::RootPath)); + return QFileInfo(FilePath::specialRootPath()); setPath(); checkStatus(); return m_baseIterator->currentFileInfo(); diff --git a/src/libs/utils/fsengine/fsenginehandler.cpp b/src/libs/utils/fsengine/fsenginehandler.cpp index 36cd0950ad6..6bada882790 100644 --- a/src/libs/utils/fsengine/fsenginehandler.cpp +++ b/src/libs/utils/fsengine/fsenginehandler.cpp @@ -11,19 +11,15 @@ #include "../algorithm.h" -namespace Utils { - -namespace Internal { +namespace Utils::Internal { QAbstractFileEngine *FSEngineHandler::create(const QString &fileName) const { if (fileName.startsWith(':')) return nullptr; - static const QString rootPath = - FilePath::specialPath(FilePath::SpecialPathComponent::RootPath); - static const FilePath rootFilePath = - FilePath::specialFilePath(FilePath::SpecialPathComponent::RootPath); + static const QString rootPath = FilePath::specialRootPath(); + static const FilePath rootFilePath = FilePath::fromString(rootPath); const QString fixedFileName = QDir::cleanPath(fileName); @@ -60,6 +56,4 @@ QAbstractFileEngine *FSEngineHandler::create(const QString &fileName) const return nullptr; } -} // namespace Internal - -} // namespace Utils +} // Utils::Internal diff --git a/src/plugins/coreplugin/foldernavigationwidget.cpp b/src/plugins/coreplugin/foldernavigationwidget.cpp index 64f96a6be3f..55c70541d49 100644 --- a/src/plugins/coreplugin/foldernavigationwidget.cpp +++ b/src/plugins/coreplugin/foldernavigationwidget.cpp @@ -138,10 +138,8 @@ bool FolderSortProxyModel::filterAcceptsRow(int source_row, const QModelIndex &s if (static_cast(sourceModel())->rootPath().isEmpty()) { QModelIndex sourceIndex = sourceModel()->index(source_row, 0, source_parent); while (sourceIndex.isValid()) { - if (sourceIndex.data().toString() - == FilePath::specialPath(FilePath::SpecialPathComponent::RootName)) { + if (sourceIndex.data().toString() == FilePath::specialRootName()) return false; - } sourceIndex = sourceIndex.parent(); } diff --git a/tests/auto/utils/fsengine/tst_fsengine.cpp b/tests/auto/utils/fsengine/tst_fsengine.cpp index e9a21353f99..c3f289791de 100644 --- a/tests/auto/utils/fsengine/tst_fsengine.cpp +++ b/tests/auto/utils/fsengine/tst_fsengine.cpp @@ -76,26 +76,24 @@ void tst_fsengine::testFilePathFromToString() QCOMPARE(p.path(), u"/test.txt"); QString asString = p.toFSPathString(); - QCOMPARE(asString, - FilePath::specialPath(FilePath::SpecialPathComponent::DeviceRootPath) - + "/test/test.txt"); + QCOMPARE(asString, FilePath::specialDeviceRootPath() + "/test/test.txt"); FilePath p2 = FilePath::fromString(asString); - QCOMPARE(p.scheme(), u"device"); - QCOMPARE(p.host(), u"test"); - QCOMPARE(p.path(), u"/test.txt"); + QCOMPARE(p2.scheme(), u"device"); + QCOMPARE(p2.host(), u"test"); + QCOMPARE(p2.path(), u"/test.txt"); } void tst_fsengine::testRootPathContainsFakeDir() { const QStringList rootList = QDir::root().entryList(); - QVERIFY(rootList.contains(FilePath::specialPath(FilePath::SpecialPathComponent::RootName))); + QVERIFY(rootList.contains(FilePath::specialRootName())); - QDir schemes(FilePath::specialPath(FilePath::SpecialPathComponent::RootPath)); + QDir schemes(FilePath::specialRootPath()); const QStringList schemeList = schemes.entryList(); QVERIFY(schemeList.contains("device")); - QDir deviceRoot(FilePath::specialPath(FilePath::SpecialPathComponent::DeviceRootPath) + "/test" + startWithSlash(QDir::rootPath())); + QDir deviceRoot(FilePath::specialDeviceRootPath() + "/test" + startWithSlash(QDir::rootPath())); const QStringList deviceRootList = deviceRoot.entryList(); QVERIFY(!deviceRootList.isEmpty()); } @@ -127,12 +125,10 @@ void tst_fsengine::testCreateDir() QString tst_fsengine::makeTestPath(QString path, bool asUrl) { - if (asUrl) { + if (asUrl) return QString("device://test%1/tst_fsengine/%2").arg(tempFolder, path); - } - return QString(FilePath::specialPath(FilePath::SpecialPathComponent::DeviceRootPath) - + "/test%1/tst_fsengine/%2") + return QString(FilePath::specialDeviceRootPath() + "/test%1/tst_fsengine/%2") .arg(startWithSlash(tempFolder), path); } @@ -193,8 +189,7 @@ void tst_fsengine::testRead() tmp.write(data); tmp.flush(); - QFile file(FilePath::specialPath(FilePath::SpecialPathComponent::DeviceRootPath) + "/test" - + tmp.fileName()); + QFile file(FilePath::specialDeviceRootPath() + "/test" + tmp.fileName()); QVERIFY(file.open(QIODevice::ReadOnly)); QCOMPARE(file.readAll(), data); } @@ -205,8 +200,7 @@ void tst_fsengine::testWrite() const QString path = dir.path() + "/testWrite.txt"; const QByteArray data = "Hello World!"; { - QFile file(FilePath::specialPath(FilePath::SpecialPathComponent::DeviceRootPath) + "/test" - + path); + QFile file(FilePath::specialDeviceRootPath() + "/test" + path); QVERIFY(file.open(QIODevice::WriteOnly)); QCOMPARE(file.write(data), qint64(data.size())); }