Skip to content

Commit

Permalink
Improve FileManager::saveBuffer()
Browse files Browse the repository at this point in the history
  • Loading branch information
donho committed Oct 14, 2020
1 parent ae2479e commit 469fa62
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 48 deletions.
4 changes: 2 additions & 2 deletions PowerEditor/src/Notepad_plus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 13 additions & 21 deletions PowerEditor/src/NppIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,16 +561,23 @@ 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)
{
scnN.nmhdr.code = NPPN_FILESAVED;
_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)
Expand All @@ -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);

Expand Down Expand Up @@ -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;
}

Expand Down
33 changes: 10 additions & 23 deletions PowerEditor/src/ScitillaComponent/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> lock(save_mutex);

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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
Expand Down
9 changes: 7 additions & 2 deletions PowerEditor/src/ScitillaComponent/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -101,7 +106,7 @@ class FileManager final
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);
Expand Down

0 comments on commit 469fa62

Please sign in to comment.