bool isBakWrite:是否进行保护写,即先写swap文件,再写源文件。这样可以避免突然断电导致源文件被清空 isBakWrite 是否写保护swp文件,默认true。只有新文件时不需要,因为新文件不存在覆盖写的问题 isStatic 是否静默:不弹出对话框,在外部批量查找替换文件夹时使用,避免弹窗中断。默认false isClearSwpFile:是否回收swp交换文件,在外部批量查找替换文件夹时使用,替换后直接删除swp文件。默认false
| 5348 | //isStatic 是否静默:不弹出对话框,在外部批量查找替换文件夹时使用,避免弹窗中断。默认false |
| 5349 | //isClearSwpFile:是否回收swp交换文件,在外部批量查找替换文件夹时使用,替换后直接删除swp文件。默认false |
| 5350 | bool CCNotePad::saveFile(QString fileName, ScintillaEditView* pEdit, bool isBakWrite, bool isStatic, bool isClearSwpFile) |
| 5351 | { |
| 5352 | QFile srcfile(fileName); |
| 5353 | |
| 5354 | //如果文件存在,说明是旧文件,检测是否能写,不能写则失败。 |
| 5355 | //反之文件不存,是保存为新文件 |
| 5356 | bool isNewFile = false; |
| 5357 | |
| 5358 | if (srcfile.exists()) |
| 5359 | { |
| 5360 | //linux也不是拥有者,可写权限就行 |
| 5361 | QFlags<QFileDevice::Permission> power = QFile::permissions(fileName); |
| 5362 | |
| 5363 | if (!power.testFlag(QFile::WriteUser)) |
| 5364 | { |
| 5365 | //文件不能写 |
| 5366 | QApplication::beep(); |
| 5367 | if (!isStatic) |
| 5368 | { |
| 5369 | QMessageBox::warning(this, tr("Error"), tr("Save File %1 failed. Can not write auth, Please save as new file").arg(fileName)); |
| 5370 | } |
| 5371 | ui.statusBar->showMessage(tr("Save File %1 failed. Can not write auth, Please save as new file").arg(fileName)); |
| 5372 | return false; |
| 5373 | } |
| 5374 | } |
| 5375 | else |
| 5376 | { |
| 5377 | isNewFile = true; |
| 5378 | } |
| 5379 | |
| 5380 | auto saveWork = [this, &pEdit,isStatic](QFile& file, QString &fileName, bool isSwapFile=false)->bool{ |
| 5381 | |
| 5382 | if (!file.open(QIODevice::ReadWrite | QIODevice::Truncate)) |
| 5383 | { |
| 5384 | QApplication::beep(); |
| 5385 | if (!isStatic) |
| 5386 | { |
| 5387 | #ifdef Q_OS_WIN |
| 5388 | //打开失败,这里一般是权限问题导致。如果是windows,在外面申请权限后继续处理 |
| 5389 | if (QFileDevice::OpenError == file.error()) |
| 5390 | { |
| 5391 | //先把当前文件的内容,保存到临时的目录中。 |
| 5392 | QString tempDir = getGlboalTempSaveDir(); |
| 5393 | QFileInfo fi(fileName); |
| 5394 | QString saveTempFile = QString("%1\\%2").arg(tempDir).arg(fi.fileName()); |
| 5395 | saveFile(saveTempFile, pEdit, false, true, false); |
| 5396 | |
| 5397 | //后面新打开的文件,再去读取该文件。 |
| 5398 | this->runAsAdmin(fileName); |
| 5399 | |
| 5400 | return false; |
| 5401 | } |
| 5402 | #endif |
| 5403 | if (isSwapFile) |
| 5404 | { |
| 5405 | //如果是交换文件写失败,询问是否继续直接写文件 |
| 5406 | return (QMessageBox::Yes == QMessageBox::question(this, tr("Error"), tr("Save Swap File %1 failed. Write the target file directly ?").arg(fileName))); |
| 5407 |
nothing calls this directly
no test coverage detected