Improve FileManager::saveBuffer()

This commit is contained in:
Don HO 2020-10-14 03:01:49 +02:00
parent ae2479efd3
commit 469fa62c0e
No known key found for this signature in database
GPG Key ID: 6C429F1D8D84F46E
4 changed files with 32 additions and 48 deletions

View File

@ -5604,9 +5604,9 @@ bool Notepad_plus::dumpFiles(const TCHAR * outdir, const TCHAR * fileprefix)
const TCHAR * unitext = (docbuf->getUnicodeMode() != uni8Bit)?TEXT("_utf8"):TEXT(""); const TCHAR * unitext = (docbuf->getUnicodeMode() != uni8Bit)?TEXT("_utf8"):TEXT("");
wsprintf(savePath, TEXT("%s\\%s%03d%s.dump"), outdir, fileprefix, i, unitext); 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; return somethingsaved || !somedirty;

View File

@ -561,8 +561,7 @@ bool Notepad_plus::doSave(BufferID id, const TCHAR * filename, bool isCopy)
_pluginsManager.notify(&scnN); _pluginsManager.notify(&scnN);
} }
generic_string error_msg; SavingStatus res = MainFileManager.saveBuffer(id, filename, isCopy);
bool res = MainFileManager.saveBuffer(id, filename, isCopy, &error_msg);
if (!isCopy) if (!isCopy)
{ {
@ -570,7 +569,15 @@ bool Notepad_plus::doSave(BufferID id, const TCHAR * filename, bool isCopy)
_pluginsManager.notify(&scnN); _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 // try to open Notepad++ in admin mode
if (!_isAdministrator) 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. { // Open the 2nd Notepad++ instance in Admin mode, then close the 1st instance.
int openInAdminModeRes = _nativeLangSpeaker.messageBox("OpenInAdminMode", int openInAdminModeRes = _nativeLangSpeaker.messageBox("OpenInAdminMode",
_pPublicInterface->getHSelf(), _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"), TEXT("Save failed"),
MB_YESNO); 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(); _pFuncList->reload();
} }
return res; return res;
} }

View File

@ -968,7 +968,7 @@ bool FileManager::deleteBufferBackup(BufferID id)
std::mutex save_mutex; 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<std::mutex> lock(save_mutex); std::lock_guard<std::mutex> lock(save_mutex);
@ -1005,7 +1005,12 @@ bool FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool isCopy, g
int encoding = buffer->getEncoding(); int encoding = buffer->getEncoding();
FILE *fp = UnicodeConvertor.fopen(fullpath, TEXT("wbc")); 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 _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) if (items_written != 1)
{ {
_pscratchTilla->execute(SCI_SETDOCPOINTER, 0, _scratchDocDefault); _pscratchTilla->execute(SCI_SETDOCPOINTER, 0, _scratchDocDefault);
return SavingStatus::SaveWrittingFailed;
if (error_msg != NULL)
*error_msg = TEXT("Failed to save file.\nNot enough space on disk to save file?");
return false;
} }
if (isHiddenOrSys) if (isHiddenOrSys)
@ -1061,19 +1062,7 @@ bool FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool isCopy, g
if (isCopy) // Save As command if (isCopy) // Save As command
{ {
_pscratchTilla->execute(SCI_SETDOCPOINTER, 0, _scratchDocDefault); _pscratchTilla->execute(SCI_SETDOCPOINTER, 0, _scratchDocDefault);
return SavingStatus::SaveOK; //all done
/* 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
} }
buffer->setFileName(fullpath, language); buffer->setFileName(fullpath, language);
@ -1091,10 +1080,8 @@ bool FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool isCopy, g
::DeleteFile(backupFilePath.c_str()); ::DeleteFile(backupFilePath.c_str());
} }
return true; return SavingStatus::SaveOK;
} }
return false;
} }
size_t FileManager::nextUntitledNewNumber() const size_t FileManager::nextUntitledNewNumber() const

View File

@ -65,7 +65,12 @@ enum BufferStatusInfo
//const int userLangNameMax = 16; //const int userLangNameMax = 16;
const TCHAR UNTITLED_STR[] = TEXT("new "); const TCHAR UNTITLED_STR[] = TEXT("new ");
enum SavingStatus
{
SaveOK = 0,
SaveOpenFailed = 1,
SaveWrittingFailed = 2
};
//File manager class maintains all buffers //File manager class maintains all buffers
class FileManager final class FileManager final
@ -101,7 +106,7 @@ public:
void setLoadedBufferEncodingAndEol(Buffer* buf, const Utf8_16_Read& UnicodeConvertor, int encoding, EolType bkformat); void setLoadedBufferEncodingAndEol(Buffer* buf, const Utf8_16_Read& UnicodeConvertor, int encoding, EolType bkformat);
bool reloadBuffer(BufferID id); bool reloadBuffer(BufferID id);
bool reloadBufferDeferred(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 backupCurrentBuffer();
bool deleteBufferBackup(BufferID id); bool deleteBufferBackup(BufferID id);
bool deleteFile(BufferID id); bool deleteFile(BufferID id);