From 30f69394b7f45f26277d42cc3f925105985a572e Mon Sep 17 00:00:00 2001 From: xiepengfei Date: Wed, 21 Oct 2026 22:33:08 +0800 Subject: [PATCH] fix: prevent file recreation at original path after deletion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Root cause: closeTab() saved to original file path without checking if the file still existed, recreating a deleted file at its original location and conflicting with trash entries showing "locked" state 2. Fix: add fileInfo.exists() check in closeTab() save branch to call saveAsFile() when file is deleted; guard dialog.setDirectory() with fileInfo.exists() in saveAsFileToDisk(), saveBlankFileToDisk(), and saveAsOtherTabFile() to use default save path when file is deleted 3. Impact: only affects files deleted externally while open in editor, normal save behavior unchanged for existing files Log: fix file showing locked in trash after editor save Influence: 1. Test editing a file, deleting it externally, then closing editor 2. Test saving deleted file triggers Save As dialog with default path 3. Test Save As / Save Blank / Save As Other Tab with deleted source file 4. Verify normal save still works for existing files fix: 修复源文件删除后编辑器保存导致回收站显示锁定 1. 根因:closeTab() 保存时未检查文件是否仍存在,直接保存到原始路径 导致已删除文件在原路径重建,与回收站条目冲突显示"锁定"状态 2. 方案:closeTab() 保存分支增加 fileInfo.exists() 检查,文件已删 除时调用 saveAsFile();saveAsFileToDisk()、saveBlankFileToDisk() 和 saveAsOtherTabFile() 在文件不存在时不覆盖默认保存路径,避免 使用原始文件目录 3. 影响:仅影响被外部删除后仍打开的文件,正常保存行为不受影响 Log: 修复源文件删除后编辑器保存导致回收站显示锁定 Influence: 1. 测试编辑文件后外部删除,再关闭编辑器保存 2. 测试保存已删除文件时弹出另存为对话框并使用默认路径 3. 测试另存为/空白文件另存/其他标签页另存时源文件已删除的场景 4. 验证正常文件保存功能不受影响 PMS: BUG-366573 --- src/widgets/window.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/widgets/window.cpp b/src/widgets/window.cpp index 91a9af10..5f219efd 100644 --- a/src/widgets/window.cpp +++ b/src/widgets/window.cpp @@ -1103,7 +1103,14 @@ bool Window::closeTab(const QString &filePath) } } else { qDebug() << "File is not a backup file, saving normally"; - if (wrapper->saveFile()) { + // 源文件可能已被外部删除(如移入回收站),需先检查文件是否存在 + if (!fileInfo.exists()) { + qDebug() << "File no longer exists, using Save As instead"; + if (!saveAsFile()) { + qWarning() << "Save as file cancelled or failed, keep tab open"; + return false; + } + } else if (wrapper->saveFile()) { qDebug() << "File saved, removing wrapper and closing tab"; removeWrapper(filePath, true); m_tabbar->closeCurrentTab(filePath); @@ -1557,7 +1564,10 @@ QString Window::saveAsFileToDisk() dialog.selectFile(match.captured(0) + ".txt"); qDebug() << "Setting dialog file name to draft file name:" << match.captured(0) + ".txt"; } else { - dialog.setDirectory(fileInfo.dir()); + // 源文件已被删除(如移入回收站)时,使用默认保存路径而非原始文件目录 + if (fileInfo.exists()) { + dialog.setDirectory(fileInfo.dir()); + } dialog.selectFile(fileInfo.fileName()); qDebug() << "Setting dialog file name to current file name:" << fileInfo.fileName(); } @@ -1685,7 +1695,10 @@ QString Window::saveBlankFileToDisk() dialog.selectFile(match.captured(0) + ".txt"); qDebug() << "Setting dialog file name to draft file name:" << match.captured(0) + ".txt"; } else { - dialog.setDirectory(fileInfo.dir()); + // 源文件已被删除(如移入回收站)时,使用默认保存路径而非原始文件目录 + if (fileInfo.exists()) { + dialog.setDirectory(fileInfo.dir()); + } dialog.selectFile(fileInfo.fileName()); qDebug() << "Setting dialog file name to current file name:" << fileInfo.fileName(); } @@ -1734,7 +1747,10 @@ bool Window::saveAsOtherTabFile(EditWrapper *wrapper) dialog.selectFile(match.captured(0) + ".txt"); qDebug() << "Setting dialog file name to draft file name:" << match.captured(0) + ".txt"; } else { - dialog.setDirectory(fileInfo.dir()); + // 源文件已被删除(如移入回收站)时,使用默认保存路径而非原始文件目录 + if (fileInfo.exists()) { + dialog.setDirectory(fileInfo.dir()); + } dialog.selectFile(fileInfo.fileName()); qDebug() << "Setting dialog file name to current file name:" << fileInfo.fileName(); }