diff --git a/common/eda_base_frame.cpp b/common/eda_base_frame.cpp index 73ea6830bc..50ec84a24b 100644 --- a/common/eda_base_frame.cpp +++ b/common/eda_base_frame.cpp @@ -582,16 +582,14 @@ bool EDA_BASE_FRAME::IsWritable( const wxFileName& aFileName ) } -void EDA_BASE_FRAME::CheckForAutoSaveFile( const wxFileName& aFileName, - const wxString& aBackupFileExtension ) +void EDA_BASE_FRAME::CheckForAutoSaveFile( const wxFileName& aFileName ) { wxCHECK_RET( aFileName.IsOk(), wxT( "Invalid file name!" ) ); - wxCHECK_RET( !aBackupFileExtension.IsEmpty(), wxT( "Invalid backup file extension!" ) ); wxFileName autoSaveFileName = aFileName; // Check for auto save file. - autoSaveFileName.SetName( AUTOSAVE_PREFIX_FILENAME + aFileName.GetName() ); + autoSaveFileName.SetName( GetAutoSaveFilePrefix() + aFileName.GetName() ); wxLogTrace( traceAutoSave, wxT( "Checking for auto save file " ) + autoSaveFileName.GetFullPath() ); @@ -615,18 +613,14 @@ void EDA_BASE_FRAME::CheckForAutoSaveFile( const wxFileName& aFileName, { // Get the backup file name. wxFileName backupFileName = aFileName; - backupFileName.SetExt( aBackupFileExtension ); + backupFileName.SetExt( aFileName.GetExt() + GetBackupSuffix() ); // If an old backup file exists, delete it. If an old copy of the file exists, rename // it to the backup file name if( aFileName.FileExists() ) { - // Remove the old file backup file. - if( backupFileName.FileExists() ) - wxRemoveFile( backupFileName.GetFullPath() ); - // Rename the old file to the backup file name. - if( !wxRenameFile( aFileName.GetFullPath(), backupFileName.GetFullPath() ) ) + if( !wxRenameFile( aFileName.GetFullPath(), backupFileName.GetFullPath(), true ) ) { msg.Printf( _( "Could not create backup file \"%s\"" ), GetChars( backupFileName.GetFullPath() ) ); diff --git a/eeschema/files-io.cpp b/eeschema/files-io.cpp index ec2dc1beb1..fd564f5bbd 100644 --- a/eeschema/files-io.cpp +++ b/eeschema/files-io.cpp @@ -135,7 +135,7 @@ bool SCH_EDIT_FRAME::SaveEEFile( SCH_SCREEN* aScreen, bool aSaveUnderNewName, { // Delete auto save file. wxFileName autoSaveFileName = schematicFileName; - autoSaveFileName.SetName( AUTOSAVE_PREFIX_FILENAME + schematicFileName.GetName() ); + autoSaveFileName.SetName( GetAutoSaveFilePrefix() + schematicFileName.GetName() ); if( autoSaveFileName.FileExists() ) { @@ -289,6 +289,9 @@ bool SCH_EDIT_FRAME::OpenProjectFiles( const std::vector& aFileSet, in g_RootSheet = NULL; // Force CreateScreens() to build new empty project on load failure. SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) ); + // This will rename the file if there is an autosave and the user want to recover + CheckForAutoSaveFile( fullFileName ); + try { g_RootSheet = pi->Load( fullFileName, &Kiway() ); @@ -745,8 +748,8 @@ bool SCH_EDIT_FRAME::doAutoSave() tmpFileName = fn = screen->GetFileName(); - // Auto save file name is the normal file name prefixed with AUTOSAVE_PREFIX_FILENAME. - fn.SetName( AUTOSAVE_PREFIX_FILENAME + fn.GetName() ); + // Auto save file name is the normal file name prefixed with GetAutoSavePrefix(). + fn.SetName( GetAutoSaveFilePrefix() + fn.GetName() ); screen->SetFileName( fn.GetFullPath() ); diff --git a/eeschema/sch_draw_panel.cpp b/eeschema/sch_draw_panel.cpp index 29f35cd885..3ff6b0ee1e 100644 --- a/eeschema/sch_draw_panel.cpp +++ b/eeschema/sch_draw_panel.cpp @@ -690,7 +690,7 @@ void SCH_DRAW_PANEL::OnKeyEvent( wxKeyEvent& event ) void SCH_DRAW_PANEL::onPaint( wxPaintEvent& aEvent ) { - if( !m_gal->IsInitialized() ) + if( !m_gal->IsInitialized() || !m_gal->IsVisible() ) // The first wxPaintEvent can be fired at startup before the GAL engine is fully initialized // (depending on platforms). Do nothing in this case return; diff --git a/eeschema/sch_edit_frame.cpp b/eeschema/sch_edit_frame.cpp index 4a5485ce4a..e4b47ece92 100644 --- a/eeschema/sch_edit_frame.cpp +++ b/eeschema/sch_edit_frame.cpp @@ -689,8 +689,8 @@ void SCH_EDIT_FRAME::OnCloseWindow( wxCloseEvent& aEvent ) { fn = Prj().AbsolutePath( screen->GetFileName() ); - // Auto save file name is the normal file name prepended with AUTOSAVE_PREFIX_FILENAME. - fn.SetName( AUTOSAVE_PREFIX_FILENAME + fn.GetName() ); + // Auto save file name is the normal file name prepended with GetAutoSaveFilePrefix(). + fn.SetName( GetAutoSaveFilePrefix() + fn.GetName() ); if( fn.FileExists() && fn.IsFileWritable() ) wxRemoveFile( fn.GetFullPath() ); diff --git a/include/eda_base_frame.h b/include/eda_base_frame.h index 8049f8924e..cf0bb6164b 100644 --- a/include/eda_base_frame.h +++ b/include/eda_base_frame.h @@ -60,18 +60,6 @@ #define CREATE_BACKUP_FILE true #define NO_BACKUP_FILE false -/** - * Prefix to create filenames for schematic files or other difile when auto-saved - * to retrieve a crash. - * - * The auto-saved filenames are AUTOSAVE_PREFIX_FILENAME + \ - * where \ is the flename without path of the auto-saved file - * Warning: avoid any special charactoer like / \\ \$ \% which can create issues on Unix - * or Window with filenames or env var expansion. - */ -#define AUTOSAVE_PREFIX_FILENAME wxT( "_saved_" ) - - class EDA_ITEM; class EDA_RECT; class EDA_DRAW_PANEL; @@ -159,6 +147,24 @@ protected: ///> Default style flags used for wxAUI toolbars static constexpr int KICAD_AUI_TB_STYLE = wxAUI_TB_DEFAULT_STYLE | wxAUI_TB_PLAIN_BACKGROUND; + /** + * Function GetBackupSuffix + * @return the suffix to be appended to the file extension on backup + */ + static wxString GetBackupSuffix() + { + return wxT( "-bak" ); + } + + /** + * Function GetAutoSaveFilePrefix + * @return the string to prepend to a file name for automatic save. + */ + static wxString GetAutoSaveFilePrefix() + { + return wxT( "_autosave-" ); + } + /** * Function onAutoSaveTimer * handles the auto save timer event. @@ -386,10 +392,8 @@ public: * is removed. *

* @param aFileName A wxFileName object containing the file name to check. - * @param aBackupFileExtension A wxString object containing the backup file extension - * used to create the backup file name. */ - void CheckForAutoSaveFile( const wxFileName& aFileName, const wxString& aBackupFileExtension ); + void CheckForAutoSaveFile( const wxFileName& aFileName ); /** * Function ShowChangedLanguage diff --git a/pcbnew/files.cpp b/pcbnew/files.cpp index cc9a2e1bba..939fe4830d 100644 --- a/pcbnew/files.cpp +++ b/pcbnew/files.cpp @@ -62,16 +62,6 @@ #define USE_INSTRUMENTATION 0 -static const wxChar backupSuffix[] = wxT( "-bak" ); -static const wxChar autosavePrefix[] = wxT( "_autosave-" ); - - -wxString PCB_EDIT_FRAME::GetAutoSaveFilePrefix() -{ - return wxString( autosavePrefix ); -} - - /** * Function AskLoadBoardFileName * puts up a wxFileDialog asking for a BOARD filename to open. @@ -265,12 +255,12 @@ bool PCB_EDIT_FRAME::Files_io_from_id( int id ) if( id == ID_MENU_RECOVER_BOARD_AUTOSAVE ) { - wxString rec_name = wxString( autosavePrefix ) + fn.GetName(); + wxString rec_name = GetAutoSaveFilePrefix() + fn.GetName(); fn.SetName( rec_name ); } else { - wxString backup_ext = fn.GetExt()+ backupSuffix; + wxString backup_ext = fn.GetExt() + GetBackupSuffix(); fn.SetExt( backup_ext ); } @@ -381,10 +371,6 @@ IO_MGR::PCB_FILE_T plugin_type( const wxString& aFileName, int aCtl ) // both legacy and eagle share a common file extension. pluginType = ( aCtl & KICTL_EAGLE_BRD ) ? IO_MGR::EAGLE : IO_MGR::LEGACY; } - else if( fn.GetExt().CmpNoCase( IO_MGR::GetFileExtension( IO_MGR::LEGACY ) + backupSuffix ) == 0 ) - { - pluginType = IO_MGR::LEGACY; - } else if( fn.GetExt().CmpNoCase( IO_MGR::GetFileExtension( IO_MGR::PCAD ) ) == 0 ) { pluginType = IO_MGR::PCAD; @@ -478,6 +464,9 @@ bool PCB_EDIT_FRAME::OpenProjectFiles( const std::vector& aFileSet, in PLUGIN::RELEASER pi( IO_MGR::PluginFind( pluginType ) ); + // This will rename the file if there is an autosave and the user want to recover + CheckForAutoSaveFile( fullFileName ); + try { PROPERTIES props; @@ -541,11 +530,6 @@ bool PCB_EDIT_FRAME::OpenProjectFiles( const std::vector& aFileSet, in GetScreen()->ClrModify(); - { - wxFileName fn = fullFileName; - CheckForAutoSaveFile( fullFileName, fn.GetExt() ); - } - if( pluginType == IO_MGR::LEGACY && loadedBoard->GetFileFormatVersionAtLoad() < LEGACY_BOARD_FILE_VERSION ) { @@ -618,12 +602,12 @@ bool PCB_EDIT_FRAME::OpenProjectFiles( const std::vector& aFileSet, in } -static wxString create_backup_file( const wxString& aFileName ) +wxString PCB_EDIT_FRAME::createBackupFile( const wxString& aFileName ) { wxFileName fn = aFileName; wxFileName backupFileName = aFileName; - backupFileName.SetExt( fn.GetExt() + backupSuffix ); + backupFileName.SetExt( fn.GetExt() + GetBackupSuffix() ); // If an old backup file exists, delete it. If an old board file exists, // rename it to the backup file name. @@ -674,11 +658,9 @@ bool PCB_EDIT_FRAME::SavePcbFile( const wxString& aFileName, bool aCreateBackupF wxString backupFileName; - // aCreateBackupFile == false is mainly used to write autosave files - // or new files in save as... command if( aCreateBackupFile ) { - backupFileName = create_backup_file( aFileName ); + backupFileName = createBackupFile( aFileName ); } GetBoard()->SynchronizeNetsAndNetClasses(); @@ -729,7 +711,7 @@ bool PCB_EDIT_FRAME::SavePcbFile( const wxString& aFileName, bool aCreateBackupF // Delete auto save file on successful save. wxFileName autoSaveFileName = pcbFileName; - autoSaveFileName.SetName( wxString( autosavePrefix ) + pcbFileName.GetName() ); + autoSaveFileName.SetName( GetAutoSaveFilePrefix() + pcbFileName.GetName() ); if( autoSaveFileName.FileExists() ) wxRemoveFile( autoSaveFileName.GetFullPath() ); @@ -816,7 +798,7 @@ bool PCB_EDIT_FRAME::doAutoSave() wxFileName autoSaveFileName = tmpFileName; // Auto save file name is the board file name prepended with autosaveFilePrefix string. - autoSaveFileName.SetName( wxString( autosavePrefix ) + autoSaveFileName.GetName() ); + autoSaveFileName.SetName( GetAutoSaveFilePrefix() + autoSaveFileName.GetName() ); if( !autoSaveFileName.IsOk() ) return false; diff --git a/pcbnew/pcb_edit_frame.cpp b/pcbnew/pcb_edit_frame.cpp index 97db1e4703..61368a73e9 100644 --- a/pcbnew/pcb_edit_frame.cpp +++ b/pcbnew/pcb_edit_frame.cpp @@ -611,7 +611,7 @@ void PCB_EDIT_FRAME::OnCloseWindow( wxCloseEvent& Event ) // Delete the auto save file if it exists. wxFileName fn = GetBoard()->GetFileName(); - // Auto save file name is the normal file name prefixed with '_autosave'. + // Auto save file name is the normal file name prefixed with 'GetAutoSaveFilePrefix()'. fn.SetName( GetAutoSaveFilePrefix() + fn.GetName() ); // When the auto save feature does not have write access to the board file path, it falls diff --git a/pcbnew/pcb_edit_frame.h b/pcbnew/pcb_edit_frame.h index e5a04898f8..a916893b27 100644 --- a/pcbnew/pcb_edit_frame.h +++ b/pcbnew/pcb_edit_frame.h @@ -113,6 +113,8 @@ protected: void createPopUpBlockMenu( wxMenu* menu ); void createPopUpMenuForMarkers( MARKER_PCB* aMarker, wxMenu* aPopMenu ); + wxString createBackupFile( const wxString& aFileName ); + /** * an helper function to enable some menus only active when the display * is switched to GAL mode and which do nothing in legacy mode @@ -308,12 +310,6 @@ public: */ void UpdateUserInterface(); - /** - * Function GetAutoSaveFilePrefix - * @return the string to prepend to a file name for automatic save. - */ - static wxString GetAutoSaveFilePrefix(); - /** * Execute a remote command send by Eeschema via a socket, * port KICAD_PCB_PORT_SERVICE_NUMBER (currently 4242)