[NEW_FEATURE] Automatic Backup System (in progress).

git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository/trunk@1206 f5eea248-9336-0410-98b8-ebc06183d4e3
This commit is contained in:
Don Ho 2014-03-31 01:01:54 +00:00
parent 97c7395059
commit 33189832ba
11 changed files with 200 additions and 202 deletions

View File

@ -3201,6 +3201,10 @@ void Notepad_plus::docGotoAnotherEditView(FileTransferMode mode)
bool Notepad_plus::activateBuffer(BufferID id, int whichOne)
{
//scnN.nmhdr.code = NPPN_DOCSWITCHINGOFF; //superseeded by NPPN_BUFFERACTIVATED
// Before switching off, synchronize backup file
MainFileManager->backupCurrentBuffer();
Buffer * pBuf = MainFileManager->getBufferByID(id);
bool reload = pBuf->getNeedReload();
if (reload)
@ -4275,7 +4279,7 @@ void Notepad_plus::getCurrentOpenedFiles(Session & session)
generic_string languageName = getLangFromMenu(buf);
const TCHAR *langName = languageName.c_str();
sessionFileInfo sfi(buf->getFullPathName(), langName, buf->getEncoding(), buf->getPosition(&_mainEditView));
sessionFileInfo sfi(buf->getFullPathName(), langName, buf->getEncoding(), buf->getPosition(&_mainEditView), buf->getBackupFileName().c_str(), int(buf->getLastModifiedTimestamp()));
//_mainEditView.activateBuffer(buf->getID());
_invisibleEditView.execute(SCI_SETDOCPOINTER, 0, buf->getDocument());
@ -4319,7 +4323,7 @@ void Notepad_plus::getCurrentOpenedFiles(Session & session)
generic_string languageName = getLangFromMenu( buf );
const TCHAR *langName = languageName.c_str();
sessionFileInfo sfi(buf->getFullPathName(), langName, buf->getEncoding(), buf->getPosition(&_subEditView));
sessionFileInfo sfi(buf->getFullPathName(), langName, buf->getEncoding(), buf->getPosition(&_subEditView), buf->getBackupFileName().c_str(), int(buf->getLastModifiedTimestamp()));
_invisibleEditView.execute(SCI_SETDOCPOINTER, 0, buf->getDocument());
int maxLine = _invisibleEditView.execute(SCI_GETLINECOUNT);
@ -4503,8 +4507,8 @@ void Notepad_plus::notifyBufferChanged(Buffer * buffer, int mask)
if (doCloseOrNot(buffer->getFullPathName()) == IDNO)
{
//close in both views, doing current view last since that has to remain opened
doClose(buffer->getID(), otherView());
doClose(buffer->getID(), currentView());
doClose(buffer->getID(), otherView(), true);
doClose(buffer->getID(), currentView(), true);
}
break;
}
@ -5790,7 +5794,7 @@ DWORD WINAPI Notepad_plus::backupDocument(void *param)
//static int i = 0;
while (notepad_plus)
{
::Sleep(3000);
::Sleep(7000);
//printInt(i++);
MainFileManager->backupCurrentBuffer();

View File

@ -223,7 +223,7 @@ public:
BufferID doOpen(const TCHAR *fileName, bool isRecursive = false, bool isReadOnly = false, int encoding = -1);
bool doReload(BufferID id, bool alert = true);
bool doSave(BufferID, const TCHAR * filename, bool isSaveCopy = false);
void doClose(BufferID, int whichOne);
void doClose(BufferID, int whichOne, bool doDeleteBackup = false);
//bool doDelete(const TCHAR *fileName) const {return ::DeleteFile(fileName) != 0;};
void fileOpen();
@ -235,7 +235,7 @@ public:
};
bool fileClose(BufferID id = BUFFER_INVALID, int curView = -1); //use curView to override view to close from
bool fileCloseAll();
bool fileCloseAll(bool doDeleteBackup);
bool fileCloseAllButCurrent();
bool fileCloseAllGiven(const std::vector<int> &krvecBufferIndexes);
bool fileCloseAllToLeft();

View File

@ -224,7 +224,7 @@ void Notepad_plus_Window::init(HINSTANCE hInst, HWND parent, const TCHAR *cmdLin
}
// Lauch backup task
//_notepad_plus_plus_core.launchDocumentBackupTask();
_notepad_plus_plus_core.launchDocumentBackupTask();
}
bool Notepad_plus_Window::isDlgsMsg(MSG *msg, bool unicodeSupported) const

View File

@ -1407,7 +1407,7 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lPa
//Causing them to show on restart even though they are loaded by session
_lastRecentFileList.setLock(true); //only lock when the session is remembered
}
bool allClosed = fileCloseAll(); //try closing files before doing anything else
bool allClosed = fileCloseAll(false); //try closing files before doing anything else
if (nppgui._rememberLastSession)
{

View File

@ -117,7 +117,7 @@ void Notepad_plus::command(int id)
break;
case IDM_FILE_CLOSEALL:
fileCloseAll();
fileCloseAll(true);
checkDocState();
break;

View File

@ -333,13 +333,16 @@ bool Notepad_plus::doSave(BufferID id, const TCHAR * filename, bool isCopy)
return res;
}
void Notepad_plus::doClose(BufferID id, int whichOne)
void Notepad_plus::doClose(BufferID id, int whichOne, bool doDeleteBackup)
{
DocTabView *tabToClose = (whichOne == MAIN_VIEW)?&_mainDocTab:&_subDocTab;
int i = tabToClose->getIndexByBuffer(id);
if (i == -1)
return;
if (doDeleteBackup)
MainFileManager->deleteCurrentBufferBackup();
Buffer * buf = MainFileManager->getBufferByID(id);
// Notify plugins that current file is about to be closed
@ -575,11 +578,11 @@ bool Notepad_plus::fileClose(BufferID id, int curView)
viewToClose = curView;
//first check amount of documents, we dont want the view to hide if we closed a secondary doc with primary being empty
//int nrDocs = _pDocTab->nbItem();
doClose(bufferID, viewToClose);
doClose(bufferID, viewToClose, true);
return true;
}
bool Notepad_plus::fileCloseAll()
bool Notepad_plus::fileCloseAll(bool doDeleteBackup)
{
//closes all documents, makes the current view the only one visible
@ -643,14 +646,14 @@ bool Notepad_plus::fileCloseAll()
//Set active tab to the last one closed.
activateBuffer(_pNonDocTab->getBufferByIndex(0), otherView());
for(int i = _pNonDocTab->nbItem() - 1; i >= 0; i--) { //close all from right to left
doClose(_pNonDocTab->getBufferByIndex(i), otherView());
doClose(_pNonDocTab->getBufferByIndex(i), otherView(), doDeleteBackup);
}
//hideView(otherView());
}
activateBuffer(_pDocTab->getBufferByIndex(0), currentView());
for(int i = _pDocTab->nbItem() - 1; i >= 0; i--) { //close all from right to left
doClose(_pDocTab->getBufferByIndex(i), currentView());
doClose(_pDocTab->getBufferByIndex(i), currentView(), doDeleteBackup);
}
return true;
}
@ -697,7 +700,7 @@ bool Notepad_plus::fileCloseAllGiven(const std::vector<int> &krvecBufferIndexes)
// Now we close.
for(std::vector<int>::const_iterator itIndex = krvecBufferIndexes.begin(); itIndex != itIndexesEnd; ++itIndex) {
doClose(_pDocTab->getBufferByIndex(*itIndex), currentView());
doClose(_pDocTab->getBufferByIndex(*itIndex), currentView(), true);
}
return true;
@ -794,7 +797,7 @@ bool Notepad_plus::fileCloseAllButCurrent()
//Set active tab to the last one closed.
activateBuffer(_pNonDocTab->getBufferByIndex(0), otherView());
for(int i = _pNonDocTab->nbItem() - 1; i >= 0; i--) { //close all from right to left
doClose(_pNonDocTab->getBufferByIndex(i), otherView());
doClose(_pNonDocTab->getBufferByIndex(i), otherView(), true);
}
//hideView(otherView());
}
@ -804,7 +807,7 @@ bool Notepad_plus::fileCloseAllButCurrent()
if (i == active) { //dont close active index
continue;
}
doClose(_pDocTab->getBufferByIndex(i), currentView());
doClose(_pDocTab->getBufferByIndex(i), currentView(), true);
}
return true;
}
@ -1019,8 +1022,8 @@ bool Notepad_plus::fileDelete(BufferID id)
MB_OK);
return false;
}
doClose(bufferID, MAIN_VIEW);
doClose(bufferID, SUB_VIEW);
doClose(bufferID, MAIN_VIEW, true);
doClose(bufferID, SUB_VIEW, true);
return true;
}
return false;

View File

@ -109,12 +109,12 @@ BOOL Notepad_plus::notify(SCNotification *notification)
}
bool isDirty = notification->nmhdr.code == SCN_SAVEPOINTLEFT;
buf->setDirty(isDirty);
/*
if (notification->nmhdr.code == SCN_SAVEPOINTREACHED)
{
MainFileManager->backupCurrentBuffer();
}
*/
break;
}
@ -234,7 +234,7 @@ BOOL Notepad_plus::notify(SCNotification *notification)
int open = 1;
if (isFromPrimary || isFromSecondary)
open = notifyDocTab->nbItem();
doClose(bufferToClose, iView);
doClose(bufferToClose, iView, true);
//if (open == 1 && canHideView(iView))
// hideView(iView);
break;
@ -277,12 +277,18 @@ BOOL Notepad_plus::notify(SCNotification *notification)
_statusBar.setText((_pEditView->execute(SCI_GETOVERTYPE))?TEXT("OVR"):TEXT("INS"), STATUSBAR_TYPING_MODE);
}
}
else if (notification->nmhdr.hwndFrom == _mainDocTab.getHSelf())
else if (notification->nmhdr.hwndFrom == _mainDocTab.getHSelf() && _activeView == SUB_VIEW)
{
// Before switching off, synchronize backup file
MainFileManager->backupCurrentBuffer();
// Switch off
switchEditViewTo(MAIN_VIEW);
}
else if (notification->nmhdr.hwndFrom == _subDocTab.getHSelf())
else if (notification->nmhdr.hwndFrom == _subDocTab.getHSelf() && _activeView == MAIN_VIEW)
{
// Before switching off, synchronize backup file
MainFileManager->backupCurrentBuffer();
// Switch off
switchEditViewTo(SUB_VIEW);
}

View File

@ -1628,7 +1628,6 @@ bool NppParameters::getSessionFromXmlTree(TiXmlDocument *pSessionDoc, Session *p
if (!sessionRoot)
return false;
TiXmlElement *actView = sessionRoot->ToElement();
size_t index;
const TCHAR *str = actView->Attribute(TEXT("activeView"), (int *)&index);
@ -1637,17 +1636,24 @@ bool NppParameters::getSessionFromXmlTree(TiXmlDocument *pSessionDoc, Session *p
(*ptrSession)._activeView = index;
}
TiXmlNode *mainviewRoot = sessionRoot->FirstChildElement(TEXT("mainView"));
if (mainviewRoot)
const size_t nbView = 2;
TiXmlNode *viewRoots[nbView];
viewRoots[0] = sessionRoot->FirstChildElement(TEXT("mainView"));
viewRoots[1] = sessionRoot->FirstChildElement(TEXT("subView"));
for (size_t k = 0; k < nbView; ++k)
{
TiXmlElement *actIndex = mainviewRoot->ToElement();
if (viewRoots[k])
{
TiXmlElement *actIndex = viewRoots[k]->ToElement();
str = actIndex->Attribute(TEXT("activeIndex"), (int *)&index);
if (str)
{
if (k == 0)
(*ptrSession)._activeMainIndex = index;
else // k == 1
(*ptrSession)._activeSubIndex = index;
}
for (TiXmlNode *childNode = mainviewRoot->FirstChildElement(TEXT("File"));
for (TiXmlNode *childNode = viewRoots[k]->FirstChildElement(TEXT("File"));
childNode ;
childNode = childNode->NextSibling(TEXT("File")) )
{
@ -1666,7 +1672,12 @@ bool NppParameters::getSessionFromXmlTree(TiXmlDocument *pSessionDoc, Session *p
langName = (childNode->ToElement())->Attribute(TEXT("lang"));
int encoding = -1;
const TCHAR *encStr = (childNode->ToElement())->Attribute(TEXT("encoding"), &encoding);
sessionFileInfo sfi(fileName, langName, encStr?encoding:-1, position);
const TCHAR *backupFilePath = (childNode->ToElement())->Attribute(TEXT("backupFilePath"));
int fileModifiedTimestamp = 0;
(childNode->ToElement())->Attribute(TEXT("originalFileLastModifTimestamp"), &fileModifiedTimestamp);
sessionFileInfo sfi(fileName, langName, encStr?encoding:-1, position, backupFilePath, fileModifiedTimestamp);
for (TiXmlNode *markNode = childNode->FirstChildElement(TEXT("Mark"));
markNode ;
@ -1691,71 +1702,14 @@ bool NppParameters::getSessionFromXmlTree(TiXmlDocument *pSessionDoc, Session *p
sfi._foldStates.push_back(lineNumber);
}
}
if (k == 0)
(*ptrSession)._mainViewFiles.push_back(sfi);
}
}
}
TiXmlNode *subviewRoot = sessionRoot->FirstChildElement(TEXT("subView"));
if (subviewRoot)
{
TiXmlElement *actIndex = subviewRoot->ToElement();
str = actIndex->Attribute(TEXT("activeIndex"), (int *)&index);
if (str)
{
(*ptrSession)._activeSubIndex = index;
}
for (TiXmlNode *childNode = subviewRoot->FirstChildElement(TEXT("File"));
childNode ;
childNode = childNode->NextSibling(TEXT("File")) )
{
const TCHAR *fileName = (childNode->ToElement())->Attribute(TEXT("filename"));
if (fileName)
{
Position position;
(childNode->ToElement())->Attribute(TEXT("firstVisibleLine"), &position._firstVisibleLine);
(childNode->ToElement())->Attribute(TEXT("xOffset"), &position._xOffset);
(childNode->ToElement())->Attribute(TEXT("startPos"), &position._startPos);
(childNode->ToElement())->Attribute(TEXT("endPos"), &position._endPos);
(childNode->ToElement())->Attribute(TEXT("selMode"), &position._selMode);
(childNode->ToElement())->Attribute(TEXT("scrollWidth"), &position._scrollWidth);
const TCHAR *langName;
langName = (childNode->ToElement())->Attribute(TEXT("lang"));
int encoding = -1;
(childNode->ToElement())->Attribute(TEXT("encoding"), &encoding);
sessionFileInfo sfi(fileName, langName, encoding, position);
for (TiXmlNode *markNode = childNode->FirstChildElement(TEXT("Mark"));
markNode ;
markNode = markNode->NextSibling(TEXT("Mark")))
{
int lineNumber;
const TCHAR *lineNumberStr = (markNode->ToElement())->Attribute(TEXT("line"), &lineNumber);
if (lineNumberStr)
{
sfi.marks.push_back(lineNumber);
}
}
for (TiXmlNode *foldNode = childNode->FirstChildElement(TEXT("Fold"));
foldNode ;
foldNode = foldNode->NextSibling(TEXT("Fold")))
{
int lineNumber;
const TCHAR *lineNumberStr = (foldNode->ToElement())->Attribute(TEXT("line"), &lineNumber);
if (lineNumberStr)
{
sfi._foldStates.push_back(lineNumber);
}
}
else // k == 1
(*ptrSession)._subViewFiles.push_back(sfi);
}
}
}
}
return true;
}
@ -2443,66 +2397,55 @@ void NppParameters::writeSession(const Session & session, const TCHAR *fileName)
TiXmlNode *sessionNode = root->InsertEndChild(TiXmlElement(TEXT("Session")));
(sessionNode->ToElement())->SetAttribute(TEXT("activeView"), (int)session._activeView);
TiXmlNode *mainViewNode = sessionNode->InsertEndChild(TiXmlElement(TEXT("mainView")));
(mainViewNode->ToElement())->SetAttribute(TEXT("activeIndex"), (int)session._activeMainIndex);
for (size_t i = 0, len = session._mainViewFiles.size(); i < len ; ++i)
{
TiXmlNode *fileNameNode = mainViewNode->InsertEndChild(TiXmlElement(TEXT("File")));
struct ViewElem {
TiXmlNode *viewNode;
vector<sessionFileInfo> *viewFiles;
size_t activeIndex;
};
const int nbElem = 2;
ViewElem viewElems[nbElem];
viewElems[0].viewNode = sessionNode->InsertEndChild(TiXmlElement(TEXT("mainView")));
viewElems[1].viewNode = sessionNode->InsertEndChild(TiXmlElement(TEXT("subView")));
viewElems[0].viewFiles = (vector<sessionFileInfo> *)(&(session._mainViewFiles));
viewElems[1].viewFiles = (vector<sessionFileInfo> *)(&(session._subViewFiles));
viewElems[0].activeIndex = session._activeMainIndex;
viewElems[1].activeIndex = session._activeSubIndex;
(fileNameNode->ToElement())->SetAttribute(TEXT("firstVisibleLine"), session._mainViewFiles[i]._firstVisibleLine);
(fileNameNode->ToElement())->SetAttribute(TEXT("xOffset"), session._mainViewFiles[i]._xOffset);
(fileNameNode->ToElement())->SetAttribute(TEXT("scrollWidth"), session._mainViewFiles[i]._scrollWidth);
(fileNameNode->ToElement())->SetAttribute(TEXT("startPos"), session._mainViewFiles[i]._startPos);
(fileNameNode->ToElement())->SetAttribute(TEXT("endPos"), session._mainViewFiles[i]._endPos);
(fileNameNode->ToElement())->SetAttribute(TEXT("selMode"), session._mainViewFiles[i]._selMode);
(fileNameNode->ToElement())->SetAttribute(TEXT("lang"), session._mainViewFiles[i]._langName.c_str());
(fileNameNode->ToElement())->SetAttribute(TEXT("encoding"), session._mainViewFiles[i]._encoding);
(fileNameNode->ToElement())->SetAttribute(TEXT("filename"), session._mainViewFiles[i]._fileName.c_str());
for (size_t j = 0, len = session._mainViewFiles[i].marks.size() ; j < len ; ++j)
for (size_t k = 0; k < nbElem ; ++k)
{
size_t markLine = session._mainViewFiles[i].marks[j];
(viewElems[k].viewNode->ToElement())->SetAttribute(TEXT("activeIndex"), (int)viewElems[k].activeIndex);
vector<sessionFileInfo> & viewSessionFiles = *(viewElems[k].viewFiles);
for (size_t i = 0, len = viewElems[k].viewFiles->size(); i < len ; ++i)
{
TiXmlNode *fileNameNode = viewElems[k].viewNode->InsertEndChild(TiXmlElement(TEXT("File")));
(fileNameNode->ToElement())->SetAttribute(TEXT("firstVisibleLine"), viewSessionFiles[i]._firstVisibleLine);
(fileNameNode->ToElement())->SetAttribute(TEXT("xOffset"), viewSessionFiles[i]._xOffset);
(fileNameNode->ToElement())->SetAttribute(TEXT("scrollWidth"), viewSessionFiles[i]._scrollWidth);
(fileNameNode->ToElement())->SetAttribute(TEXT("startPos"), viewSessionFiles[i]._startPos);
(fileNameNode->ToElement())->SetAttribute(TEXT("endPos"), viewSessionFiles[i]._endPos);
(fileNameNode->ToElement())->SetAttribute(TEXT("selMode"), viewSessionFiles[i]._selMode);
(fileNameNode->ToElement())->SetAttribute(TEXT("lang"), viewSessionFiles[i]._langName.c_str());
(fileNameNode->ToElement())->SetAttribute(TEXT("encoding"), viewSessionFiles[i]._encoding);
(fileNameNode->ToElement())->SetAttribute(TEXT("filename"), viewSessionFiles[i]._fileName.c_str());
(fileNameNode->ToElement())->SetAttribute(TEXT("backupFilePath"), viewSessionFiles[i]._backupFilePath.c_str());
(fileNameNode->ToElement())->SetAttribute(TEXT("originalFileLastModifTimestamp"), int(viewSessionFiles[i]._originalFileLastModifTimestamp));
for (size_t j = 0, len = viewSessionFiles[i].marks.size() ; j < len ; ++j)
{
size_t markLine = viewSessionFiles[i].marks[j];
TiXmlNode *markNode = fileNameNode->InsertEndChild(TiXmlElement(TEXT("Mark")));
markNode->ToElement()->SetAttribute(TEXT("line"), markLine);
}
for (size_t j = 0, len = session._mainViewFiles[i]._foldStates.size() ; j < len ; ++j)
for (size_t j = 0, len = viewSessionFiles[i]._foldStates.size() ; j < len ; ++j)
{
size_t foldLine = session._mainViewFiles[i]._foldStates[j];
size_t foldLine = viewSessionFiles[i]._foldStates[j];
TiXmlNode *foldNode = fileNameNode->InsertEndChild(TiXmlElement(TEXT("Fold")));
foldNode->ToElement()->SetAttribute(TEXT("line"), foldLine);
}
}
TiXmlNode *subViewNode = sessionNode->InsertEndChild(TiXmlElement(TEXT("subView")));
(subViewNode->ToElement())->SetAttribute(TEXT("activeIndex"), (int)session._activeSubIndex);
for (size_t i = 0, len = session._subViewFiles.size(); i < len ; ++i)
{
TiXmlNode *fileNameNode = subViewNode->InsertEndChild(TiXmlElement(TEXT("File")));
(fileNameNode->ToElement())->SetAttribute(TEXT("firstVisibleLine"), session._subViewFiles[i]._firstVisibleLine);
(fileNameNode->ToElement())->SetAttribute(TEXT("xOffset"), session._subViewFiles[i]._xOffset);
(fileNameNode->ToElement())->SetAttribute(TEXT("scrollWidth"), session._subViewFiles[i]._scrollWidth);
(fileNameNode->ToElement())->SetAttribute(TEXT("startPos"), session._subViewFiles[i]._startPos);
(fileNameNode->ToElement())->SetAttribute(TEXT("endPos"), session._subViewFiles[i]._endPos);
(fileNameNode->ToElement())->SetAttribute(TEXT("selMode"), session._subViewFiles[i]._selMode);
(fileNameNode->ToElement())->SetAttribute(TEXT("lang"), session._subViewFiles[i]._langName.c_str());
(fileNameNode->ToElement())->SetAttribute(TEXT("encoding"), session._subViewFiles[i]._encoding);
(fileNameNode->ToElement())->SetAttribute(TEXT("filename"), session._subViewFiles[i]._fileName.c_str());
for (size_t j = 0, len = session._subViewFiles[i].marks.size(); j < len; ++j)
{
size_t markLine = session._subViewFiles[i].marks[j];
TiXmlNode *markNode = fileNameNode->InsertEndChild(TiXmlElement(TEXT("Mark")));
markNode->ToElement()->SetAttribute(TEXT("line"), markLine);
}
for (size_t j = 0, len = session._subViewFiles[i]._foldStates.size() ; j < len ; ++j)
{
size_t foldLine = session._subViewFiles[i]._foldStates[j];
TiXmlNode *foldNode = fileNameNode->InsertEndChild(TiXmlElement(TEXT("Fold")));
foldNode->ToElement()->SetAttribute(TEXT("line"), foldLine);
}
}
}
_pXmlSessionDoc->SaveFile();

View File

@ -145,9 +145,11 @@ struct Position
};
struct sessionFileInfo : public Position {
sessionFileInfo(const TCHAR *fn, const TCHAR *ln, int encoding, Position pos) : _encoding(encoding), Position(pos) {
sessionFileInfo(const TCHAR *fn, const TCHAR *ln, int encoding, Position pos, const TCHAR *backupFilePath, int originalFileLastModifTimestamp) :
_encoding(encoding), Position(pos), _originalFileLastModifTimestamp(originalFileLastModifTimestamp) {
if (fn) _fileName = fn;
if (ln) _langName = ln;
if (backupFilePath) _backupFilePath = backupFilePath;
};
sessionFileInfo(generic_string fn) : _fileName(fn), _encoding(-1){};
@ -157,6 +159,9 @@ struct sessionFileInfo : public Position {
vector<size_t> marks;
vector<size_t> _foldStates;
int _encoding;
generic_string _backupFilePath;
time_t _originalFileLastModifTimestamp;
};
struct Session {

View File

@ -648,25 +648,36 @@ bool FileManager::backupCurrentBuffer()
Utf8_16_Write UnicodeConvertor;
UnicodeConvertor.setEncoding(mode);
int encoding = buffer->getEncoding();
generic_string backupFilePath = NppParameters::getInstance()->getUserPath();
if (buffer->getBackupFileName() == TEXT(""))
{
// Create file name
if (buffer->isUntitled())
generic_string backupFilePath = buffer->getBackupFileName();
if (backupFilePath == TEXT(""))
{
// Create file
backupFilePath = NppParameters::getInstance()->getUserPath();
backupFilePath += TEXT("\\backup\\");
// if "backup" folder doesn't exist, create it.
if (!PathFileExists(backupFilePath.c_str()))
{
::CreateDirectory(backupFilePath.c_str(), NULL);
}
else
{
}
backupFilePath += buffer->getFileName();
const int temBufLen = 32;
TCHAR tmpbuf[temBufLen];
time_t ltime = time(0);
struct tm *today;
today = localtime(&ltime);
generic_strftime(tmpbuf, temBufLen, TEXT("%Y-%m-%d_%H%M%S"), today);
backupFilePath += TEXT("@");
backupFilePath += tmpbuf;
// Set created file name in buffer
//backupFilePath +=
buffer->setBackupFileName(backupFilePath);
}
@ -745,11 +756,34 @@ bool FileManager::backupCurrentBuffer()
if (backupFilePath != TEXT(""))
{
// delete backup file
generic_string file2Delete = buffer->getBackupFileName();
buffer->setBackupFileName(TEXT(""));
result = (::DeleteFile(file2Delete.c_str()) != 0);
}
//printStr(TEXT("backup deleted in backupCurrentBuffer"));
result = true; // no backup file to delete
}
//printStr(TEXT("backup sync"));
::ReleaseMutex(mutex);
return result;
}
bool FileManager::deleteCurrentBufferBackup()
{
HANDLE mutex = ::CreateMutex(NULL, false, TEXT("nppBackupSystem"));
::WaitForSingleObject(mutex, INFINITE);
Buffer * buffer = _pNotepadPlus->getCurrentBuffer();
bool result = true;
generic_string backupFilePath = buffer->getBackupFileName();
if (backupFilePath != TEXT(""))
{
// delete backup file
generic_string file2Delete = buffer->getBackupFileName();
buffer->setBackupFileName(TEXT(""));
result = (::DeleteFile(file2Delete.c_str()) != 0);
}
//printStr(TEXT("backup deleted in deleteCurrentBufferBackup"));
::ReleaseMutex(mutex);
return result;
}

View File

@ -96,6 +96,7 @@ public:
bool reloadBufferDeferred(BufferID id);
bool saveBuffer(BufferID id, const TCHAR * filename, bool isCopy = false, generic_string * error_msg = NULL);
bool backupCurrentBuffer();
bool deleteCurrentBufferBackup();
bool deleteFile(BufferID id);
bool moveFile(BufferID id, const TCHAR * newFilename);
bool createEmptyFile(const TCHAR * path);
@ -325,6 +326,8 @@ public :
bool isModified() const {return _isModified;};
void setModifiedStatus(bool isModified) {_isModified = isModified;};
generic_string getBackupFileName() const {return _backupFileName;};
void setBackupFileName(generic_string fileName) {_backupFileName = fileName;};
time_t getLastModifiedTimestamp() const {return _timeStamp;};
private :
FileManager * _pManager;