Skip to content

Commit 59850bf

Browse files
lichaofan2008deepin-bot[bot]
authored andcommitted
fix: Adjust the logic for saving files when closing the window.
调整关闭窗口时保存文件的逻辑。 在【保留关闭前的页签】勾选后,普通情况下不提醒保存文件,当本地文件被重命名或删除时提醒保存文件,以确保文件不丢失。 Bug: https://pms.uniontech.com/bug-view-315439.html
1 parent 1ac3c8f commit 59850bf

2 files changed

Lines changed: 17 additions & 34 deletions

File tree

src/widgets/window.cpp

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,29 +2606,21 @@ bool Window::closeAllFiles()
26062606
return true;
26072607
}
26082608

2609-
bool Window::saveAllFiles()
2609+
bool Window::saveAllFloatingFiles()
26102610
{
2611-
qInfo() << "begin saveAllFiles()";
2611+
qInfo() << "begin saveAllFloatingFiles()";
26122612
QMap<QString, EditWrapper *> wrappers = m_wrappers;
2613-
for (int i = wrappers.count() - 1; i > 0; i--) {
2614-
const QString &filePath = m_tabbar->fileAt(i);
2613+
for (int i = wrappers.count() - 1; i >= 0; i--) {
2614+
const QString &filePath = m_tabbar->truePathAt(i);
26152615
// 避免异常情况重入时当前已无标签页的情况
26162616
if (filePath.isEmpty()) {
26172617
return false;
26182618
}
2619-
EditWrapper *wrapper = m_wrappers.value(filePath);
2620-
if (!wrapper) {
2621-
return false;
2622-
}
2623-
bool isDraftFile = wrapper->isDraftFile();
2624-
bool isModified = wrapper->isModified();
2625-
bool bIsBackupFile = false;
26262619

2627-
if (wrapper->isTemFile()) {
2628-
bIsBackupFile = true;
2629-
}
2630-
2631-
if (isModified) {
2620+
QFileInfo finfo(filePath);
2621+
if (!finfo.exists()) {
2622+
qWarning() << "File not exist:" << filePath;
2623+
m_tabbar->setCurrentIndex(i);
26322624
DDialog *dialog = createDialog(tr("Do you want to save this file?"), "");
26332625
int res = dialog->exec();
26342626

@@ -2644,24 +2636,11 @@ bool Window::saveAllFiles()
26442636

26452637
//保存
26462638
if (res == 2) {
2647-
m_tabbar->setCurrentIndex(i);
2648-
QFileInfo fileInfo(filePath);
2649-
QString newFilePath;
2650-
if (isDraftFile) {
2651-
wrapper->saveDraftFile(newFilePath);
2652-
} else if(bIsBackupFile) {
2653-
if (!wrapper->saveFile()) {
2654-
saveAsFile();
2655-
}
2656-
} else {
2657-
if (!wrapper->saveFile()) {
2658-
saveAsFile();
2659-
}
2660-
}
2639+
saveAsFile();
26612640
}
26622641
}
26632642
}
2664-
qInfo() << "end saveAllFiles()";
2643+
qInfo() << "end saveAllFloatingFiles()";
26652644
return true;
26662645
}
26672646

@@ -3459,19 +3438,23 @@ void Window::closeEvent(QCloseEvent *e)
34593438
return;
34603439
}
34613440
} else {
3441+
// 是否记录上次打开的文件
34623442
bool save_tab_before_close = m_settings->settings->option("advance.startup.save_tab_before_close")->value().toBool();
34633443
if (!save_tab_before_close) {
3444+
// 不记录,关闭所有标签页,没保存的文件提示保存
34643445
if (!closeAllFiles()) {
34653446
e->ignore();
34663447
return;
34673448
}
34683449
} else {
3450+
// 记录,没保存的文件不提示保存,除非磁盘上文件已删除
34693451
// 单个窗口时,没有记录单独关闭窗口,需要记录窗口信息。
34703452
for (auto itr = m_wrappers.begin(); itr != m_wrappers.end(); ++itr) {
34713453
QString filePath = itr.value()->textEditor()->getFilePath();
34723454
Utils::recordCloseFile(filePath);
34733455
}
3474-
if (!saveAllFiles()) {
3456+
// 检查是否存在磁盘上已删除的文件,若存在则提示保存
3457+
if (!saveAllFloatingFiles()) {
34753458
backupFile();
34763459
e->ignore();
34773460
return;

src/widgets/window.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ class Window : public DMainWindow
124124
void backupFile();
125125
// 关闭当前窗口所有文件
126126
bool closeAllFiles();
127-
// 保存窗口所有文件
128-
bool saveAllFiles();
127+
// 保存窗口中所有漂浮文件(本地文件已被删除)
128+
bool saveAllFloatingFiles();
129129
// 恢复备份文件标签页
130130
void addTemFileTab(const QString &qstrPath, const QString &qstrName, const QString &qstrTruePath, const QString &lastModifiedTime, bool bIsTemFile = false);
131131

0 commit comments

Comments
 (0)