From fb4875c681a2d8bc3c039113b17da4d0897b45d4 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Mon, 13 Nov 2017 15:15:10 +0100 Subject: [PATCH] Library Editor: refactored code for creating backup files --- eeschema/libedit.cpp | 48 ++++++++------------------------------- eeschema/libeditframe.cpp | 22 ++++++++++++++++++ eeschema/libeditframe.h | 3 +++ 3 files changed, 34 insertions(+), 39 deletions(-) diff --git a/eeschema/libedit.cpp b/eeschema/libedit.cpp index 1c553dc78b..b9f1126cff 100644 --- a/eeschema/libedit.cpp +++ b/eeschema/libedit.cpp @@ -513,56 +513,26 @@ bool LIB_EDIT_FRAME::saveLibrary( const wxString& aLibrary, bool aNewFile ) ClearMsgPanel(); - wxFileName libFileName = fn; - wxFileName backupFileName = fn; - // Copy .lib file to .bak. - if( libFileName.FileExists() ) - { - backupFileName.SetExt( "bak" ); + if( !backupFile( fn, "bak" ) ) + return false; - if( backupFileName.FileExists() ) - wxRemoveFile( backupFileName.GetFullPath() ); - - if( !wxCopyFile( libFileName.GetFullPath(), backupFileName.GetFullPath() ) ) - { - libFileName.MakeAbsolute(); - msg.Printf( _( "Failed to rename old symbol library to file '%s'" ), - backupFileName.GetFullPath() ); - DisplayError( this, msg ); - return false; - } - } - - wxFileName docFileName = libFileName; + wxFileName docFileName = fn; docFileName.SetExt( DOC_EXT ); - // Copy .dcm file to .bck. // handle - if( docFileName.FileExists() ) - { - backupFileName.SetExt( "bck" ); + // Copy .dcm file to .bck. + if( !backupFile( docFileName, "bck" ) ) + return false; - if( backupFileName.FileExists() ) - wxRemoveFile( backupFileName.GetFullPath() ); - - if( !wxCopyFile( docFileName.GetFullPath(), backupFileName.GetFullPath() ) ) - { - msg.Printf( _( "Failed to save old library document to file '%s'" ), - backupFileName.GetFullPath() ); - DisplayError( this, msg ); - return false; - } - } - - if( !m_libMgr->SaveLibrary( aLibrary, libFileName.GetFullPath() ) ) + if( !m_libMgr->SaveLibrary( aLibrary, fn.GetFullPath() ) ) { msg.Printf( _( "Failed to save changes to symbol library file '%s'" ), - libFileName.GetFullPath() ); + fn.GetFullPath() ); DisplayErrorMessage( this, _( "Error saving library" ), msg ); return false; } - msg.Printf( _( "Symbol library file '%s' saved" ), libFileName.GetFullPath() ); + msg.Printf( _( "Symbol library file '%s' saved" ), fn.GetFullPath() ); wxString msg1; msg1.Printf( _( "Symbol library documentation file '%s' saved" ), docFileName.GetFullPath() ); AppendMsgPanel( msg, msg1, BLUE ); diff --git a/eeschema/libeditframe.cpp b/eeschema/libeditframe.cpp index 96169f4e08..299c9d2975 100644 --- a/eeschema/libeditframe.cpp +++ b/eeschema/libeditframe.cpp @@ -1552,6 +1552,28 @@ SYMBOL_LIB_TABLE* LIB_EDIT_FRAME::SelectSymLibTable() } +bool LIB_EDIT_FRAME::backupFile( const wxFileName& aOriginalFile, const wxString& aBackupExt ) +{ + if( aOriginalFile.FileExists() ) + { + wxFileName backupFileName( aOriginalFile ); + backupFileName.SetExt( "bck" ); + + if( backupFileName.FileExists() ) + wxRemoveFile( backupFileName.GetFullPath() ); + + if( !wxCopyFile( aOriginalFile.GetFullPath(), backupFileName.GetFullPath() ) ) + { + DisplayError( this, _( "Failed to save backup document to file " ) + + backupFileName.GetFullPath() ); + return false; + } + } + + return true; +} + + void LIB_EDIT_FRAME::storeCurrentPart() { if( m_my_part && !GetCurLib().IsEmpty() && GetScreen()->IsModify() ) diff --git a/eeschema/libeditframe.h b/eeschema/libeditframe.h index 789e590ea4..49d5801a25 100644 --- a/eeschema/libeditframe.h +++ b/eeschema/libeditframe.h @@ -698,6 +698,9 @@ public: ///> Helper screen used when no part is loaded SCH_SCREEN* m_dummyScreen; + ///> Creates a backup copy of a file with requested extension + bool backupFile( const wxFileName& aOriginalFile, const wxString& aBackupExt ); + // TODO // TODO move to tree pane? LIB_PART* getTargetPart() const;