1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
From cd0810f82d9fce629fa27e2a1d330fbcc8d2edf6 Mon Sep 17 00:00:00 2001
From: Akseli Lahtinen <akselmo@akselmo.dev>
Date: Thu, 17 Apr 2025 08:43:26 +0000
Subject: [PATCH] KFileWidget: Do not override filename with folder name if
edited
If the filename input has been modified, do not override it with
the name of the folder when selecting a folder.
BUG: 502794
FIXED-IN: 6.14
---
autotests/kfilewidgettest.cpp | 85 +++++++++++++++++++++++++++++++++
src/filewidgets/kdiroperator.h | 1 +
src/filewidgets/kfilewidget.cpp | 4 ++
3 files changed, 90 insertions(+)
diff --git a/autotests/kfilewidgettest.cpp b/autotests/kfilewidgettest.cpp
index cd04b57c4..e0ff3945d 100644
--- a/autotests/kfilewidgettest.cpp
+++ b/autotests/kfilewidgettest.cpp
@@ -87,6 +87,8 @@ private Q_SLOTS:
void testTokenizeForSave_data();
void testTokenizeForSave();
void testThumbnailPreviewSetting();
+ void testReplaceLocationEditFilename_data();
+ void testReplaceLocationEditFilename();
};
void KFileWidgetTest::initTestCase()
@@ -932,6 +934,89 @@ void KFileWidgetTest::testThumbnailPreviewSetting()
fwPreviewFalse.cancelButton()->click();
}
+struct LocationTestItem {
+ bool dir;
+ QString name;
+};
+
+void KFileWidgetTest::testReplaceLocationEditFilename_data()
+{
+ QTest::addColumn<LocationTestItem>("initialItem");
+ QTest::addColumn<LocationTestItem>("selectedItem");
+ QTest::addColumn<QString>("lineEditTextResult");
+ QTest::addColumn<bool>("overrideModifiedText");
+
+ QTest::newRow("replace-dir-with-dir") << LocationTestItem(true, "folder1") << LocationTestItem(true, "folder2") << "" << false;
+ QTest::newRow("replace-dir-with-file") << LocationTestItem(true, "folder1") << LocationTestItem(false, "file1") << "file1" << true;
+ QTest::newRow("replace-file-with-file") << LocationTestItem(false, "file1") << LocationTestItem(false, "file2") << "file2" << true;
+ QTest::newRow("replace-file-with-dir") << LocationTestItem(false, "file1") << LocationTestItem(true, "folder1") << "file1" << false;
+}
+
+// BUG: 502794
+// Test that we don't override file names with folder names
+void KFileWidgetTest::testReplaceLocationEditFilename()
+{
+ QFETCH(LocationTestItem, initialItem);
+ QFETCH(LocationTestItem, selectedItem);
+ QFETCH(QString, lineEditTextResult);
+ QFETCH(bool, overrideModifiedText);
+
+ // Setup - Create folders/files in temp dir
+ QTemporaryDir tempDir;
+ const QString tempDirPath = tempDir.path();
+ QUrl tempDirUrl = QUrl::fromLocalFile(tempDirPath);
+ QUrl replacedUrl = QUrl::fromLocalFile(tempDirPath + QLatin1Char('/') + initialItem.name);
+ QUrl selectedUrl = QUrl::fromLocalFile(tempDirPath + QLatin1Char('/') + selectedItem.name);
+
+ auto createTestItem = [tempDirUrl](LocationTestItem item, const QUrl &url) {
+ if (item.dir) {
+ QDir(tempDirUrl.toLocalFile()).mkdir(url.toLocalFile());
+ QVERIFY(QDir(url.toLocalFile()).exists());
+ } else {
+ QFile file(url.toLocalFile());
+ if (!file.open(QIODevice::WriteOnly)) {
+ qFatal("Couldn't create %s", qPrintable(url.toLocalFile()));
+ }
+ file.write(QByteArray("Test file"));
+ file.close();
+ QVERIFY(file.exists());
+ }
+ };
+
+ createTestItem(initialItem, replacedUrl);
+ createTestItem(selectedItem, selectedUrl);
+
+ // Open the filewidget in tempdir
+ KFileWidget fw(tempDirUrl);
+ fw.setOperationMode(KFileWidget::Saving);
+
+ // Highlight the item, then another
+ auto highlightItem = [&fw](QUrl url) {
+ KFileItem fileItem(url);
+ QSignalSpy fileHighlightedSpy(fw.dirOperator(), &KDirOperator::fileHighlighted);
+ fw.dirOperator()->highlightFile(fileItem);
+ fileHighlightedSpy.wait(500);
+ QVERIFY(fileHighlightedSpy.count());
+ };
+
+ highlightItem(replacedUrl);
+ highlightItem(selectedUrl);
+
+ // Compare that we have the wanted result when selecting items
+ QCOMPARE(fw.locationEdit()->lineEdit()->text(), lineEditTextResult);
+
+ // Make sure we don't overwrite any text user has modified in some cases
+ const QString modifiedText("New Filename.txt");
+ fw.locationEdit()->setEditText(modifiedText);
+ highlightItem(selectedUrl);
+
+ if (overrideModifiedText) {
+ QCOMPARE(fw.locationEdit()->lineEdit()->text(), lineEditTextResult);
+ } else {
+ QCOMPARE(fw.locationEdit()->lineEdit()->text(), modifiedText);
+ }
+}
+
QTEST_MAIN(KFileWidgetTest)
#include "kfilewidgettest.moc"
diff --git a/src/filewidgets/kdiroperator.h b/src/filewidgets/kdiroperator.h
index 6013a39cc..818432141 100644
--- a/src/filewidgets/kdiroperator.h
+++ b/src/filewidgets/kdiroperator.h
@@ -1042,6 +1042,7 @@ private:
KIOFILEWIDGETS_NO_EXPORT void setViewInternal(QAbstractItemView *view);
friend class KDirOperatorPrivate;
+ friend class KFileWidgetTest; // For testing
std::unique_ptr<KDirOperatorPrivate> d;
};
diff --git a/src/filewidgets/kfilewidget.cpp b/src/filewidgets/kfilewidget.cpp
index 064efc3b3..d5afb9de3 100644
--- a/src/filewidgets/kfilewidget.cpp
+++ b/src/filewidgets/kfilewidget.cpp
@@ -900,6 +900,10 @@ void KFileWidgetPrivate::fileHighlighted(const KFileItem &i, bool isKeyNavigatio
return;
}
+ if (!i.isNull() && i.isDir() && !(m_ops->mode() & KFile::Directory)) {
+ return;
+ }
+
const bool modified = m_locationEdit->lineEdit()->isModified();
if (!(m_ops->mode() & KFile::Files)) {
--
GitLab
|