From ac20a2c458e4e0245246574c73093405c008fee3 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Sat, 14 Jan 2023 22:52:04 +0800 Subject: [PATCH] - enhancement: Create a temporary copy of the current file when saving files (it's removed after the saving sucessfully finished). --- NEWS.md | 1 + RedPandaIDE/editor.cpp | 25 +- RedPandaIDE/translations/RedPandaIDE_pt_BR.ts | 4 + RedPandaIDE/translations/RedPandaIDE_zh_CN.ts | 356 +++++++++--------- RedPandaIDE/translations/RedPandaIDE_zh_TW.ts | 4 + libs/redpanda_qt_utils/qt_utils/utils.cpp | 28 +- libs/redpanda_qt_utils/qt_utils/utils.h | 4 +- 7 files changed, 228 insertions(+), 194 deletions(-) diff --git a/NEWS.md b/NEWS.md index b5d23b57..ca13a872 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,7 @@ Red Panda C++ Version 2.9 - fix: syntax highlighting for cpp style line comment is not correct. - fix: Save may crash app if the encoding codec is failed to load. - enhancement: support open and save utf-16/utf-32 BOM files. (but gcc can't compile) + - enhancement: Create a temporary copy of the current file when saving files (it's removed after the saving sucessfully finished). Red Panda C++ Version 2.8 diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index 4cffe659..e2c90fce 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -244,13 +244,30 @@ void Editor::saveFile(QString filename) { QByteArray encoding = mFileEncoding; if (mEncodingOption!=ENCODING_AUTO_DETECT || mFileEncoding==ENCODING_ASCII) encoding = mEncodingOption; - + QString backupFilename=filename+".savebak"; + int count=1; + while (fileExists(backupFilename)) { + backupFilename=filename+QString(".savebak%1").arg(count); + count++; + } + if (!fileExists(filename)) { + if (!stringToFile(text(),backupFilename)) { + QMessageBox::critical(pMainWindow,tr("Error"), + tr("Can't generate temporary backup file '%1'.").arg(backupFilename)); + return; + } + } else if (!QFile::copy(filename,backupFilename)) { + QMessageBox::critical(pMainWindow,tr("Error"), + tr("Can't generate temporary backup file '%1'.").arg(backupFilename)); + return; + } this->document()->saveToFile(file,encoding, pSettings->editor().defaultEncoding(), mFileEncoding); if (isVisible() && mParentPageControl) pMainWindow->updateForEncodingInfo(this); emit fileSaved(filename, inProject()); + QFile::remove(backupFilename); } void Editor::convertToEncoding(const QByteArray &encoding) @@ -264,9 +281,9 @@ bool Editor::save(bool force, bool doReparse) { if (this->mIsNew && !force) { return saveAs(); } - while (pMainWindow->parsing()) { - QThread::msleep(200); - } +// while (pMainWindow->parsing()) { +// QThread::msleep(200); +// } //is this file writable; pMainWindow->fileSystemWatcher()->removePath(mFilename); diff --git a/RedPandaIDE/translations/RedPandaIDE_pt_BR.ts b/RedPandaIDE/translations/RedPandaIDE_pt_BR.ts index 2cfdea8d..8bcf636a 100644 --- a/RedPandaIDE/translations/RedPandaIDE_pt_BR.ts +++ b/RedPandaIDE/translations/RedPandaIDE_pt_BR.ts @@ -1034,6 +1034,10 @@ Readonly Apenas leitura + + Can't generate temporary backup file '%1'. + + EditorAutoSaveWidget diff --git a/RedPandaIDE/translations/RedPandaIDE_zh_CN.ts b/RedPandaIDE/translations/RedPandaIDE_zh_CN.ts index e99eebb5..ac7868ca 100644 --- a/RedPandaIDE/translations/RedPandaIDE_zh_CN.ts +++ b/RedPandaIDE/translations/RedPandaIDE_zh_CN.ts @@ -588,7 +588,7 @@ p, li { white-space: pre-wrap; } [说明] - + The compiler process for '%1' failed to start. 无法启动编译器进程'%1'。 @@ -597,27 +597,27 @@ p, li { white-space: pre-wrap; } 无法启动编译进程。 - + The compiler process crashed after starting successfully. 编译进程启动后崩溃。 - + The last waitFor...() function timed out. waitFor()函数等待超时。 - + An error occurred when attempting to write to the compiler process. 在向编译进程输入内容时出错。 - + An error occurred when attempting to read from the compiler process. 在从编译进程读取内容时出错。 - + An unknown error occurred. 发生了未知错误。 @@ -1379,13 +1379,14 @@ Are you really want to continue? 失败 - - - - - - - + + + + + + + + Error 错误 @@ -1394,44 +1395,49 @@ Are you really want to continue? 无法写入文件"%1" - + + Can't generate temporary backup file '%1'. + 无法生成临时备份文件"%1"。 + + + Save As 另存为 - + File %1 already openned! 文件%1已经被打开! - + The text to be copied exceeds count limit! 要复制的内容超过了行数限制! - + The text to be copied exceeds character limit! 要复制的内容超过了字符数限制! - + The text to be cut exceeds count limit! 要剪切的内容超过了行数限制! - + The text to be cut exceeds character limit! 要剪切的内容超过了字符数限制! - + Print Document 打印文档 - - - + + + Ctrl+click for more info Ctrl+单击以获取更多信息 @@ -1440,27 +1446,27 @@ Are you really want to continue? 未找到符号'%1'! - + astyle not found 找不到astyle程序 - + Can't find astyle in "%1". 找不到astyle程序"%1". - + Break point condition 断点条件 - + Enter the condition of the breakpoint: 输入当前断点的生效条件: - + Readonly 只读 @@ -4119,11 +4125,11 @@ Are you really want to continue? - - - - - + + + + + Issues 编译器 @@ -4561,7 +4567,7 @@ Are you really want to continue? - + New Problem Set 新建试题集 @@ -4583,7 +4589,7 @@ Are you really want to continue? - + Save Problem Set 保存试题集 @@ -4591,7 +4597,7 @@ Are you really want to continue? - + Load Problem Set 载入试题集 @@ -4722,14 +4728,14 @@ Are you really want to continue? - + Import FPS Problem Set 导入FPS试题集 - + Export FPS Problem Set 导出FPS试题集 @@ -4971,7 +4977,7 @@ Are you really want to continue? - + Clear all breakpoints 删除所有断点 @@ -5223,7 +5229,7 @@ Are you really want to continue? - + Rename Symbol 重命名符号 @@ -5244,13 +5250,13 @@ Are you really want to continue? - + Export As RTF 导出为RTF - + Export As HTML 导出为HTML @@ -5623,22 +5629,22 @@ Are you really want to continue? - - + + Wrong Compiler Settings 错误的编译器设置 - - + + Compiler is set not to generate executable. 编译器被设置为不生成可执行文件。 - + We need the executabe to run problem case. 我们需要可执行文件来运行试题案例。 @@ -5704,7 +5710,7 @@ Are you really want to continue? - + Please correct this before start debugging 请在调试前改正设置。 @@ -5762,22 +5768,22 @@ Are you really want to continue? 全部复制 - + Go to Line 跳转到行 - + Line - + Template Exists 模板已存在 - + Template %1 already exists. Do you want to overwrite? 模板%1已存在。是否覆盖? @@ -5803,7 +5809,7 @@ Are you really want to continue? - + Problem Set %1 试题集%1 @@ -5877,15 +5883,15 @@ Are you really want to continue? - - + + Bookmark Description 书签描述 - - + + Description: 描述: @@ -6066,7 +6072,7 @@ Are you really want to continue? - + Delete 删除 @@ -6130,17 +6136,17 @@ Are you really want to continue? 中止 - + FPS Problem Set Files (*.fps;*.xml) FPS试题集文件(*.fps;*.xml) - + FPS Problem Set Files (*.fps) FPS试题集文件(*.fps) - + Export Error 导出时出错 @@ -6184,47 +6190,47 @@ Are you really want to continue? - + Do you want to save it? 需要保存吗? - - + + File Changed 文件已发生变化 - + New Project File? 新建项目文件? - + Do you want to add the new file to the project? 您是否要将新建的文件加入项目? - - - - + + + + Save Error 保存失败 - + Change Project Compiler Set 改变项目编译器配置集 - + Change the project's compiler set will lose all custom compiler set options. 改变项目的编译器配置集会导致所有的自定义编译器选项被重置。 - + Do you really want to do that? 你真的想要那么做吗? @@ -6247,78 +6253,78 @@ Are you really want to continue? 无标题%1 - + Modify Watch 修改监视表达式 - + Watch Expression 监视表达式 - + Do you really want to clear all breakpoints in this file? 您真的要清除该文件的所有断点吗? - + New project 新建项目 - + Close %1 and start new project? 关闭'%1'以打开新项目? - + Folder not exist 文件夹不存在 - + Folder '%1' doesn't exist. Create it now? 文件夹'%1'不存在。是否创建? - + Can't create folder 无法创建文件夹 - + Failed to create folder '%1'. 创建文件夹'%1'失败。 - + Save new project as - + Folder %1 is not empty. 文件夹%1不是空的。 - + Do you really want to delete it? 你真的要删除它吗? - + Change working folder 改变工作文件夹 - + File '%1' is not in the current working folder. File '%1' is not in the current working folder 文件'%1'不在当前工作文件夹中。 - + Do you want to change working folder to '%1'? 是否将工作文件夹改设为'%1'? @@ -6327,28 +6333,28 @@ Are you really want to continue? 正在删除试题... - + Can't Commit 无法提交 - + Git needs user info to commit. Git需要用信息进行提交。 - + Choose Input Data File 选择输入数据文件 - - + + All files (*.*) 所有文件 (*.*) - + Choose Expected Output Data File Choose Expected Input Data File 选择期望输出文件 @@ -6360,59 +6366,59 @@ Are you really want to continue? - + Choose Working Folder 选择工作文件夹 - - + + Header Exists 头文件已存在 - - + + Header file "%1" already exists! 头文件"%1"已存在! - + Source Exists 源文件已存在! - + Source file "%1" already exists! 源文件"%1"已存在! - + Can't commit! 无法提交! - + The following files are in conflicting: 下列文件处于冲突状态,请解决后重新添加和提交: - + Commit Message 提交信息 - + Commit Message: 提交信息: - + Commit Failed 提交失败 - + Commit message shouldn't be empty! 提交信息不能为空! @@ -6421,22 +6427,22 @@ Are you really want to continue? 小熊猫Dev-C++项目文件 (*.dev) - + New project fail 新建项目失败 - + Can't assign project template 无法使用模板创建项目 - + Remove file 删除文件 - + Remove the file from disk? 同时从硬盘上删除文件? @@ -6445,27 +6451,27 @@ Are you really want to continue? 无标题 - + New Project File Name 新的项目文件名 - + File Name: 文件名: - + File Already Exists! 文件已存在! - + File '%1' already exists! 文件'%1'已经存在! - + Add to project 添加到项目 @@ -6496,78 +6502,78 @@ Are you really want to continue? 本操作会删除此试题的所有案例。 - + Red Panda C++ project file (*.dev) 小熊猫C++项目文件(*.dev) - + Rename Error 重命名出错 - + Symbol '%1' is defined in system header. 符号'%1'在系统头文件中定义,无法修改。 - + New Name 新名称 - - - - + + + + Replace Error 替换出错 - + Can't open file '%1' for replace! 无法打开文件'%1'进行替换! - + Contents has changed since last search! 内容和上次查找时不一致。 - + Rich Text Format Files (*.rtf) RTF格式文件 (*.rtf) - + HTML Files (*.html) HTML文件 (*.html) - + The current problem set is not empty. 当前的试题集不是空的。 - + Problem %1 试题%1 - - + + Problem Set Files (*.pbs) 试题集文件 (*.pbs) - - + + Load Error 载入失败 - + Problem Case %1 试题案例%1 @@ -6581,13 +6587,13 @@ Are you really want to continue? - - - - - - - + + + + + + + Error 错误 @@ -6614,79 +6620,79 @@ Are you really want to continue? 版本控制 - + File '%1' was changed. 磁盘文件'%1'已被修改。 - + Reload its content from disk? 是否重新读取它的内容? - + File '%1' was removed. 磁盘文件'%1'已被删除。 - + Keep it open? 是否保持它在小熊猫C++中打开的编辑窗口? - + Open 打开 - + Compile Failed 编译失败 - + Run Failed 运行失败 - - - + + + Confirm Convertion 确认转换 - - - + + + The editing file will be saved using %1 encoding. <br />This operation can't be reverted. <br />Are you sure to continue? 当前编辑器中的文件将会使用%1编码保存。<br />这项操作无法被撤回。<br />你确定要继续吗? - + New Watch Expression 新监视表达式 - + Enter Watch Expression (it is recommended to use 'this->' for class members): 输入监视表达式 - + Parsing file %1 of %2: "%3" (%1/%2)正在解析文件"%3" - - + + Done parsing %1 files in %2 seconds 完成%1个文件的解析,用时%2秒 - + (%1 files per second) (每秒%1个文件) @@ -7307,42 +7313,42 @@ Are you really want to continue? - 资源文件: %1 - + Compiling project changes... 正在编译项目修改... - + - Project Filename: %1 - 项目文件名: %1 - + - Compiler Set Name: %1 - 编译器配置: %1 - + Make program '%1' doesn't exists! Make程序“%1”不存在! - + Please check the "program" page of compiler settings. 请检查编译器配置中的“程序”页。 - + Processing makefile: 正在处理makefile... - + - makefile processer: %1 - makefile处理器: %1 - + - Command: %1 %2 - 命令: %1 %2 @@ -9351,7 +9357,7 @@ Are you really want to continue? - + Compiler Set @@ -9360,7 +9366,7 @@ Are you really want to continue? - + Compiler @@ -9372,7 +9378,7 @@ Are you really want to continue? 自动链接 - + @@ -9449,15 +9455,15 @@ Are you really want to continue? 杂项 - - + + Program Runner 程序运行 - + Problem Set 试题集 diff --git a/RedPandaIDE/translations/RedPandaIDE_zh_TW.ts b/RedPandaIDE/translations/RedPandaIDE_zh_TW.ts index 71747afd..c003e925 100644 --- a/RedPandaIDE/translations/RedPandaIDE_zh_TW.ts +++ b/RedPandaIDE/translations/RedPandaIDE_zh_TW.ts @@ -927,6 +927,10 @@ Readonly + + Can't generate temporary backup file '%1'. + + EditorAutoSaveWidget diff --git a/libs/redpanda_qt_utils/qt_utils/utils.cpp b/libs/redpanda_qt_utils/qt_utils/utils.cpp index cee0212c..0ce6faee 100644 --- a/libs/redpanda_qt_utils/qt_utils/utils.cpp +++ b/libs/redpanda_qt_utils/qt_utils/utils.cpp @@ -381,30 +381,32 @@ QByteArray readFileToByteArray(const QString &fileName) return QByteArray(); } -void stringToFile(const QString &str, const QString &fileName) +bool stringToFile(const QString &str, const QString &fileName) { QFile file(fileName); - if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { - QTextStream stream(&file); - stream<= QT_VERSION_CHECK(5,15,0) - <