QFileDialog: Fix adding default suffix when file path contains dot

Check that a file name, not the full path, contains a dot.

Fixes: QTBUG-59401
Pick-to: 5.15 6.2
Change-Id: I193b2ae457a3ac6a460524dbf200786eb3461cef
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
Alexander Volkov 2021-07-02 15:00:11 +03:00
parent 000f1ee360
commit d36249e975
2 changed files with 24 additions and 3 deletions

View File

@ -1250,8 +1250,9 @@ QStringList QFileDialogPrivate::addDefaultSuffixToFiles(const QStringList &files
QFileInfo info(name);
// if the filename has no suffix, add the default suffix
const QString defaultSuffix = options->defaultSuffix();
if (!defaultSuffix.isEmpty() && !info.isDir() && name.lastIndexOf(QLatin1Char('.')) == -1)
if (!defaultSuffix.isEmpty() && !info.isDir() && !info.fileName().contains(u'.'))
name += QLatin1Char('.') + defaultSuffix;
if (info.isAbsolute()) {
files.append(name);
} else {
@ -1277,8 +1278,12 @@ QList<QUrl> QFileDialogPrivate::addDefaultSuffixToUrls(const QList<QUrl> &urlsTo
QUrl url = urlsToFix.at(i);
// if the filename has no suffix, add the default suffix
const QString defaultSuffix = options->defaultSuffix();
if (!defaultSuffix.isEmpty() && !url.path().endsWith(QLatin1Char('/')) && url.path().lastIndexOf(QLatin1Char('.')) == -1)
url.setPath(url.path() + QLatin1Char('.') + defaultSuffix);
if (!defaultSuffix.isEmpty()) {
const QString urlPath = url.path();
const auto idx = urlPath.lastIndexOf(u'/');
if (idx != (urlPath.size() - 1) && !QStringView{urlPath}.mid(idx + 1).contains(u'.'))
url.setPath(urlPath + u'.' + defaultSuffix);
}
urls.append(url);
}
return urls;

View File

@ -139,6 +139,7 @@ private slots:
void clearLineEdit();
void enableChooseButton();
void selectedFilesWithoutWidgets();
void selectedFileWithDefaultSuffix();
void trailingDotsAndSpaces();
#ifdef Q_OS_UNIX
#ifdef QT_BUILD_INTERNAL
@ -1471,6 +1472,21 @@ void tst_QFiledialog::selectedFilesWithoutWidgets()
QVERIFY(fd.selectedFiles().size() >= 0);
}
void tst_QFiledialog::selectedFileWithDefaultSuffix()
{
// QTBUG-59401: dot in file path should not prevent default suffix from being added
QTemporaryDir tempDir(QDir::tempPath() + "/abcXXXXXX.def");
QVERIFY2(tempDir.isValid(), qPrintable(tempDir.errorString()));
QFileDialog fd;
fd.setDirectory(tempDir.path());
fd.setDefaultSuffix(".txt");
fd.selectFile("xxx");
const auto selectedFiles = fd.selectedFiles();
QCOMPARE(selectedFiles.size(), 1);
QVERIFY(selectedFiles.first().endsWith(".txt"));
}
void tst_QFiledialog::trailingDotsAndSpaces()
{
#ifndef Q_OS_WIN