diff --git a/src/startmanager.cpp b/src/startmanager.cpp index d190aff42..afac4f9c6 100644 --- a/src/startmanager.cpp +++ b/src/startmanager.cpp @@ -147,6 +147,9 @@ void StartManager::autoBackupFile() QFileInfo fileInfo; m_qlistTemFile.clear(); listBackupInfo = Settings::instance()->settings->option("advance.editor.browsing_history_temfile")->value().toStringList(); + if (m_windows.isEmpty()) { + return; + } //记录所有的文件信息 for (int var = 0; var < m_windows.count(); ++var) { diff --git a/src/widgets/window.cpp b/src/widgets/window.cpp index c6a6d2233..b0d6c33fa 100644 --- a/src/widgets/window.cpp +++ b/src/widgets/window.cpp @@ -2593,6 +2593,65 @@ bool Window::closeAllFiles() return true; } +bool Window::saveAllFiles() +{ + qInfo() << "begin saveAllFiles()"; + QMap wrappers = m_wrappers; + for (int i = wrappers.count() - 1; i > 0; i--) { + const QString &filePath = m_tabbar->fileAt(i); + // 避免异常情况重入时当前已无标签页的情况 + if (filePath.isEmpty()) { + return false; + } + EditWrapper *wrapper = m_wrappers.value(filePath); + if (!wrapper) { + return false; + } + bool isDraftFile = wrapper->isDraftFile(); + bool isModified = wrapper->isModified(); + bool bIsBackupFile = false; + + if (wrapper->isTemFile()) { + bIsBackupFile = true; + } + + if (isModified) { + DDialog *dialog = createDialog(tr("Do you want to save this file?"), ""); + int res = dialog->exec(); + + //取消或关闭弹窗不做任务操作 + if (res == 0 || res == -1) { + return false; + } + + //不保存 + if (res == 1) { + continue; + } + + //保存 + if (res == 2) { + m_tabbar->setCurrentIndex(i); + QFileInfo fileInfo(filePath); + QString newFilePath; + if (isDraftFile) { + wrapper->saveDraftFile(newFilePath); + } else if(bIsBackupFile) { + if (!wrapper->saveFile()) { + saveAsFile(); + } + } else { + if (!wrapper->saveFile()) { + saveAsFile(); + } + } + } + } + } + qInfo() << "end saveAllFiles()"; + return true; +} + /** * @brief addTemFileTab 恢复备份文件标签页 * @param qstrPath 打开文件路径 @@ -3394,7 +3453,11 @@ void Window::closeEvent(QCloseEvent *e) QString filePath = itr.value()->textEditor()->getFilePath(); Utils::recordCloseFile(filePath); } - + if (!saveAllFiles()) { + backupFile(); + e->ignore(); + return; + } backupFile(); } } diff --git a/src/widgets/window.h b/src/widgets/window.h index 3de136ad0..3bfd09baa 100644 --- a/src/widgets/window.h +++ b/src/widgets/window.h @@ -118,6 +118,8 @@ class Window : public DMainWindow void backupFile(); // 关闭当前窗口所有文件 bool closeAllFiles(); + // 保存窗口所有文件 + bool saveAllFiles(); // 恢复备份文件标签页 void addTemFileTab(const QString &qstrPath, const QString &qstrName, const QString &qstrTruePath, const QString &lastModifiedTime, bool bIsTemFile = false);