Library Editor: refactored code for creating backup files

This commit is contained in:
Maciej Suminski 2017-11-13 15:15:10 +01:00
parent f6f1dff9d0
commit fb4875c681
3 changed files with 34 additions and 39 deletions

View File

@ -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 );

View File

@ -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() )

View File

@ -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;