From 400426ed9f90399c769d91c6dda8bb640af310aa Mon Sep 17 00:00:00 2001 From: Christian Stenger Date: Thu, 5 Aug 2021 07:40:41 +0200 Subject: [PATCH] Utils: Handle resolving paths and validation more appropriate Manually editing the path inside a PathChooser will trigger some validation and may end up in constructing some invalid paths but still marking them as valid. Beside this it will make the cursor jump to different locations while typing which ends up in creating even more incorrect paths. So, avoid creating invalid paths and mark the resulting path as invalid if it is. Change-Id: I27f55ae03745474fc6b91c44cdbc307c581fb2f8 Reviewed-by: hjk --- src/libs/utils/pathchooser.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/libs/utils/pathchooser.cpp b/src/libs/utils/pathchooser.cpp index d596a27712a..d4109175cbb 100644 --- a/src/libs/utils/pathchooser.cpp +++ b/src/libs/utils/pathchooser.cpp @@ -231,8 +231,19 @@ FilePath PathChooserPrivate::expandedPath(const QString &input) const case PathChooser::ExistingDirectory: case PathChooser::File: case PathChooser::SaveFile: - if (!m_baseDirectory.isEmpty()) - return m_baseDirectory.resolvePath(path.path()).absoluteFilePath(); + if (!m_baseDirectory.isEmpty()) { + Utils::FilePath fp = m_baseDirectory.resolvePath(path.path()).absoluteFilePath(); + // FIXME bad hotfix for manually editing PathChooser (invalid paths, jumping cursor) + // examples: have an absolute path and try to change the device letter by typing the new + // letter and removing the original afterwards ends up in + // D:\\dev\\project\\cD:\\dev\\build-project (before trying to remove the original) + // as 'cD:\\dev\\build-project' is considered is handled as being relative + // input = "cD:\\dev\build-project"; // prepended 'c' to change the device letter + // m_baseDirectory = "D:\\dev\\project" + if (!fp.needsDevice() && HostOsInfo::isWindowsHost() && fp.toString().count(':') > 1) + return path; + return fp; + } break; } return path; @@ -588,6 +599,12 @@ bool PathChooser::validatePath(FancyLineEdit *edit, QString *errorMessage) const *errorMessage = tr("The path \"%1\" is not a directory.").arg(filePath.toUserOutput()); return false; } + if (HostOsInfo::isWindowsHost() && !filePath.startsWithDriveLetter() + && !filePath.startsWith("\\\\") && !filePath.startsWith("//")) { + if (errorMessage) + *errorMessage = tr("Invalid path \"%1\".").arg(filePath.toUserOutput()); + return false; + } break; case PathChooser::Command: if (filePath.exists() && !filePath.isExecutableFile()) {