From 3d40eb690594d99b33fc233fa92907454d7e2278 Mon Sep 17 00:00:00 2001 From: dengzhongyuan365-dev Date: Sat, 14 Nov 2026 01:13:24 +0800 Subject: [PATCH] fix: fix file monitoring lost after recreate save MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. When editing a file in an archive with an external editor using delete+recreate save semantics, slotOpenFileChanged permanently removed the file from monitoring, preventing future change prompts 2. addWatchFile in openFileWatcher.cpp had an inverted condition (!= instead of ==) and wrong key (m_strOpenFile instead of strFile) 3. Replace immediate removal with QTimer::singleShot delayed re-check to restore monitoring when the file reappears after 2 seconds Log: fix file change monitoring lost after delete+recreate save Influence: 1. Test editing files in archives with editors using delete+recreate 2. Test editing files in archives with editors using in-place save 3. Test multiple consecutive edits to the same file in an archive fix: 修复删除重建保存后文件监控丢失 1. 使用外部编辑器以"先删除再创建"方式保存压缩包内文件时, slotOpenFileChanged 首次编辑后永久移除文件监控,后续不再提示 2. openFileWatcher.cpp 的 addWatchFile 条件反转(!= 应为 ==)且 键名错误(m_strOpenFile 应为 strFile),修改状态未正确初始化 3. 将直接移除监控改为 QTimer::singleShot 延迟重检,文件重新 出现时恢复监控 Log: 修复外部编辑器删除重建保存后文件监控丢失 Influence: 1. 测试使用删除+重建方式保存的编辑器编辑压缩包内文件 2. 测试使用原地修改方式保存的编辑器编辑压缩包内文件 3. 测试对压缩包内同一文件连续多次编辑 PMS: BUG-378833 --- src/source/common/openFileWatcher.cpp | 4 ++-- src/source/mainwindow.cpp | 14 +++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/source/common/openFileWatcher.cpp b/src/source/common/openFileWatcher.cpp index 83fd91c6f..c83e106d9 100644 --- a/src/source/common/openFileWatcher.cpp +++ b/src/source/common/openFileWatcher.cpp @@ -44,8 +44,8 @@ void OpenFileWatcher::addWatchFile(const QString &strFile) m_pOpenFileWatcher->addPath(strFile); // 第一次默认文件未修改 - if (m_mapFileHasModified.find(strFile) != m_mapFileHasModified.end()) { - m_mapFileHasModified[m_strOpenFile] = false; + if (m_mapFileHasModified.find(strFile) == m_mapFileHasModified.end()) { + m_mapFileHasModified[strFile] = false; } } diff --git a/src/source/mainwindow.cpp b/src/source/mainwindow.cpp index 49fc09cfa..f1e7b253c 100644 --- a/src/source/mainwindow.cpp +++ b/src/source/mainwindow.cpp @@ -48,6 +48,7 @@ #include #include #include +#include #include #ifdef DTKCORE_CLASS_DConfigFile #include @@ -2949,10 +2950,17 @@ void MainWindow::slotOpenFileChanged(const QString &strPath) SimpleQueryDialog dialog(this); int iResult = dialog.showDialog(strDesText, tr("Cancel", "button"), DDialog::ButtonNormal, tr("Update", "button"), DDialog::ButtonRecommend); if (1 == iResult) { - // 对重命名或删除导致的文件不存在的情况,不给出任何提示,且从文件监控中去掉此文件 + // 文件不存在可能是编辑器"先删除再创建"的保存方式,延迟重检而非直接移除监控 if (!QFile::exists(strPath)) { - mapStatus.remove(strPath); - mapPassword.remove(strPath); + mapStatus[strPath] = false; + QTimer::singleShot(2000, this, [this, strPath]() { + if (QFile::exists(strPath)) { + m_pOpenFileWatcher->addWatchFile(strPath); + } else { + QMap &status = m_pOpenFileWatcher->getFileHasModified(); + status.remove(strPath); + } + }); return; }