Strip any trailing spaces from the filename before trying to open it

On Windows, trailing spaces in a filename are silently ignored, so we
need to strip it before trying to open a file with it. Otherwise it ends
up being stripped later and in a case like " ." it will end up causing
Qt to think that a folder exists when it does not.

[ChangeLog][Platform Specific Changes][Windows][QtWidgets][QFileDialog]
Handled the case of having trailing spaces in a filename correctly so if
the filename ends up being empty that the parent path is used instead.

Change-Id: I6500cc3a44746bf4a65e73bcfb63265a0a97c8a3
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Andy Shaw 2013-04-11 10:23:01 +02:00 committed by The Qt Project
parent 54d43c6480
commit ecd3027d38
2 changed files with 39 additions and 2 deletions

View File

@ -401,9 +401,18 @@ QFileSystemModelPrivate::QFileSystemNode *QFileSystemModelPrivate::node(const QS
for (int i = 0; i < pathElements.count(); ++i) { for (int i = 0; i < pathElements.count(); ++i) {
QString element = pathElements.at(i); QString element = pathElements.at(i);
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
// On Windows, "filename......." and "filename" are equivalent Task #133928 // On Windows, "filename " and "filename" are equivalent and
while (element.endsWith(QLatin1Char('.'))) // "filename . " and "filename" are equivalent
// "filename......." and "filename" are equivalent Task #133928
// whereas "filename .txt" is still "filename .txt"
// If after stripping the characters there is nothing left then we
// just return the parent directory as it is assumed that the path
// is referring to the parent
while (element.endsWith(QLatin1Char('.')) || element.endsWith(QLatin1Char(' ')))
element.chop(1); element.chop(1);
// Only filenames that can't possibly exist will be end up being empty
if (element.isEmpty())
return parent;
#endif #endif
bool alreadyExisted = parent->children.contains(element); bool alreadyExisted = parent->children.contains(element);

View File

@ -148,6 +148,7 @@ private slots:
void enableChooseButton(); void enableChooseButton();
void hooks(); void hooks();
void widgetlessNativeDialog(); void widgetlessNativeDialog();
void trailingDotsAndSpaces();
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
#ifdef QT_BUILD_INTERNAL #ifdef QT_BUILD_INTERNAL
void tildeExpansion_data(); void tildeExpansion_data();
@ -1413,6 +1414,33 @@ void tst_QFiledialog::widgetlessNativeDialog()
QVERIFY(!button); QVERIFY(!button);
} }
void tst_QFiledialog::trailingDotsAndSpaces()
{
#ifndef Q_OS_WIN
QSKIP("This is only tested on Windows");
#endif
QNonNativeFileDialog fd;
fd.setViewMode(QFileDialog::List);
fd.setFileMode(QFileDialog::ExistingFile);
fd.setOptions(QFileDialog::DontUseNativeDialog);
fd.show();
QLineEdit *lineEdit = fd.findChild<QLineEdit *>("fileNameEdit");
QVERIFY(lineEdit);
QListView *list = fd.findChild<QListView *>("listView");
QVERIFY(list);
QTest::qWait(1000);
int currentChildrenCount = list->model()->rowCount(list->rootIndex());
QTest::keyClick(lineEdit, Qt::Key_Space);
QTest::keyClick(lineEdit, Qt::Key_Period);
QTest::qWait(1000);
QVERIFY(currentChildrenCount == list->model()->rowCount(list->rootIndex()));
lineEdit->clear();
QTest::keyClick(lineEdit, Qt::Key_Period);
QTest::keyClick(lineEdit, Qt::Key_Space);
QTest::qWait(1000);
QVERIFY(currentChildrenCount == list->model()->rowCount(list->rootIndex()));
}
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
#ifdef QT_BUILD_INTERNAL #ifdef QT_BUILD_INTERNAL
void tst_QFiledialog::tildeExpansion_data() void tst_QFiledialog::tildeExpansion_data()