From 469fa62c0ebc438c8003c735e6a801c6d098df7c Mon Sep 17 00:00:00 2001 From: Don HO Date: Wed, 14 Oct 2020 03:01:49 +0200 Subject: [PATCH] Improve FileManager::saveBuffer() --- PowerEditor/src/Notepad_plus.cpp | 4 +-- PowerEditor/src/NppIO.cpp | 34 ++++++++------------ PowerEditor/src/ScitillaComponent/Buffer.cpp | 33 ++++++------------- PowerEditor/src/ScitillaComponent/Buffer.h | 9 ++++-- 4 files changed, 32 insertions(+), 48 deletions(-) diff --git a/PowerEditor/src/Notepad_plus.cpp b/PowerEditor/src/Notepad_plus.cpp index dd95d8e57..0fb0feb90 100644 --- a/PowerEditor/src/Notepad_plus.cpp +++ b/PowerEditor/src/Notepad_plus.cpp @@ -5604,9 +5604,9 @@ bool Notepad_plus::dumpFiles(const TCHAR * outdir, const TCHAR * fileprefix) const TCHAR * unitext = (docbuf->getUnicodeMode() != uni8Bit)?TEXT("_utf8"):TEXT(""); wsprintf(savePath, TEXT("%s\\%s%03d%s.dump"), outdir, fileprefix, i, unitext); - bool res = MainFileManager.saveBuffer(docbuf->getID(), savePath); + SavingStatus res = MainFileManager.saveBuffer(docbuf->getID(), savePath); - somethingsaved |= res; + somethingsaved |= (res == SavingStatus::SaveOK); } return somethingsaved || !somedirty; diff --git a/PowerEditor/src/NppIO.cpp b/PowerEditor/src/NppIO.cpp index 169792285..de4747b25 100644 --- a/PowerEditor/src/NppIO.cpp +++ b/PowerEditor/src/NppIO.cpp @@ -561,8 +561,7 @@ bool Notepad_plus::doSave(BufferID id, const TCHAR * filename, bool isCopy) _pluginsManager.notify(&scnN); } - generic_string error_msg; - bool res = MainFileManager.saveBuffer(id, filename, isCopy, &error_msg); + SavingStatus res = MainFileManager.saveBuffer(id, filename, isCopy); if (!isCopy) { @@ -570,7 +569,15 @@ bool Notepad_plus::doSave(BufferID id, const TCHAR * filename, bool isCopy) _pluginsManager.notify(&scnN); } - if (!res) + if (res == SavingStatus::SaveWrittingFailed) + { + _nativeLangSpeaker.messageBox("NotEnoughRoom4Saving", + _pPublicInterface->getHSelf(), + TEXT("Failed to save file.\nIt seems there's not enough space on disk to save file."), + TEXT("Save failed"), + MB_OK); + } + else if (res == SavingStatus::SaveOpenFailed) { // try to open Notepad++ in admin mode if (!_isAdministrator) @@ -580,7 +587,7 @@ bool Notepad_plus::doSave(BufferID id, const TCHAR * filename, bool isCopy) { // Open the 2nd Notepad++ instance in Admin mode, then close the 1st instance. int openInAdminModeRes = _nativeLangSpeaker.messageBox("OpenInAdminMode", _pPublicInterface->getHSelf(), - TEXT("The file cannot be saved and it may be protected.\rDo you want to launch Notepad++ in Administrator mode?"), + TEXT("This file cannot be saved and it may be protected.\rDo you want to launch Notepad++ in Administrator mode?"), TEXT("Save failed"), MB_YESNO); @@ -652,28 +659,13 @@ bool Notepad_plus::doSave(BufferID id, const TCHAR * filename, bool isCopy) } } - else - { - - if (error_msg.empty()) - { - _nativeLangSpeaker.messageBox("FileLockedWarning", - _pPublicInterface->getHSelf(), - TEXT("Please check if this file is opened in another program."), - TEXT("Save failed"), - MB_OK); - } - else - { - ::MessageBox(_pPublicInterface->getHSelf(), error_msg.c_str(), TEXT("Save failed"), MB_OK); - } - } } - if (res && _pFuncList && (!_pFuncList->isClosed()) && _pFuncList->isVisible()) + if (res == SavingStatus::SaveOK && _pFuncList && (!_pFuncList->isClosed()) && _pFuncList->isVisible()) { _pFuncList->reload(); } + return res; } diff --git a/PowerEditor/src/ScitillaComponent/Buffer.cpp b/PowerEditor/src/ScitillaComponent/Buffer.cpp index daf2c9f53..32185c969 100644 --- a/PowerEditor/src/ScitillaComponent/Buffer.cpp +++ b/PowerEditor/src/ScitillaComponent/Buffer.cpp @@ -968,7 +968,7 @@ bool FileManager::deleteBufferBackup(BufferID id) std::mutex save_mutex; -bool FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool isCopy, generic_string * error_msg) +SavingStatus FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool isCopy) { std::lock_guard lock(save_mutex); @@ -1005,7 +1005,12 @@ bool FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool isCopy, g int encoding = buffer->getEncoding(); FILE *fp = UnicodeConvertor.fopen(fullpath, TEXT("wbc")); - if (fp) + + if (!fp) + { + return SavingStatus::SaveOpenFailed; + } + else { _pscratchTilla->execute(SCI_SETDOCPOINTER, 0, buffer->_doc); //generate new document @@ -1048,11 +1053,7 @@ bool FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool isCopy, g if (items_written != 1) { _pscratchTilla->execute(SCI_SETDOCPOINTER, 0, _scratchDocDefault); - - if (error_msg != NULL) - *error_msg = TEXT("Failed to save file.\nNot enough space on disk to save file?"); - - return false; + return SavingStatus::SaveWrittingFailed; } if (isHiddenOrSys) @@ -1061,19 +1062,7 @@ bool FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool isCopy, g if (isCopy) // Save As command { _pscratchTilla->execute(SCI_SETDOCPOINTER, 0, _scratchDocDefault); - - /* for saveAs it's not necessary since this action is for the "current" directory, so we let manage in SAVEPOINTREACHED event - generic_string backupFilePath = buffer->getBackupFileName(); - if (not backupFilePath.empty()) - { - // delete backup file - generic_string file2Delete = buffer->getBackupFileName(); - buffer->setBackupFileName(generic_string()); - ::DeleteFile(file2Delete.c_str()); - } - */ - - return true; //all done + return SavingStatus::SaveOK; //all done } buffer->setFileName(fullpath, language); @@ -1091,10 +1080,8 @@ bool FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool isCopy, g ::DeleteFile(backupFilePath.c_str()); } - return true; + return SavingStatus::SaveOK; } - - return false; } size_t FileManager::nextUntitledNewNumber() const diff --git a/PowerEditor/src/ScitillaComponent/Buffer.h b/PowerEditor/src/ScitillaComponent/Buffer.h index f5cc6186b..3696e813c 100644 --- a/PowerEditor/src/ScitillaComponent/Buffer.h +++ b/PowerEditor/src/ScitillaComponent/Buffer.h @@ -65,7 +65,12 @@ enum BufferStatusInfo //const int userLangNameMax = 16; const TCHAR UNTITLED_STR[] = TEXT("new "); - +enum SavingStatus +{ + SaveOK = 0, + SaveOpenFailed = 1, + SaveWrittingFailed = 2 +}; //File manager class maintains all buffers class FileManager final @@ -101,7 +106,7 @@ public: void setLoadedBufferEncodingAndEol(Buffer* buf, const Utf8_16_Read& UnicodeConvertor, int encoding, EolType bkformat); bool reloadBuffer(BufferID id); bool reloadBufferDeferred(BufferID id); - bool saveBuffer(BufferID id, const TCHAR* filename, bool isCopy = false, generic_string * error_msg = NULL); + SavingStatus saveBuffer(BufferID id, const TCHAR* filename, bool isCopy = false); bool backupCurrentBuffer(); bool deleteBufferBackup(BufferID id); bool deleteFile(BufferID id);