Clean up autosave files when reverting.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/15998
This commit is contained in:
Jeff Young 2023-11-05 12:19:51 +00:00
parent 88a4979fff
commit f83d60a48f
7 changed files with 98 additions and 49 deletions

View File

@ -3,6 +3,7 @@
*
* Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2013 Wayne Stambaugh <stambaughw@gmail.com>
* Copyright (C) 2023 CERN (www.cern.ch)
* Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
@ -1356,11 +1357,25 @@ void EDA_BASE_FRAME::CheckForAutoSaveFile( const wxFileName& aFileName )
}
else
{
wxLogTrace( traceAutoSave,
wxT( "Removing auto save file " ) + autoSaveFileName.GetFullPath() );
DeleteAutoSaveFile( aFileName );
}
}
// Remove the auto save file when using the previous file as is.
wxRemoveFile( autoSaveFileName.GetFullPath() );
void EDA_BASE_FRAME::DeleteAutoSaveFile( const wxFileName& aFileName )
{
if( !Pgm().IsGUI() )
return;
wxCHECK_RET( aFileName.IsOk(), wxT( "Invalid file name!" ) );
wxFileName autoSaveFn = aFileName;
autoSaveFn.SetName( GetAutoSaveFilePrefix() + aFileName.GetName() );
if( autoSaveFn.FileExists() )
{
wxLogTrace( traceAutoSave, wxT( "Removing auto save file " ) + autoSaveFn.GetFullPath() );
wxRemoveFile( autoSaveFn.GetFullPath() );
}
}

View File

@ -3,7 +3,7 @@
*
* Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2013 Wayne Stambaugh <stambaughw@gmail.com>
* Copyright (C) 2013 CERN (www.cern.ch)
* Copyright (C) 2013-2023 CERN (www.cern.ch)
* Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
@ -236,7 +236,15 @@ bool SCH_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, in
autoSaveFn.SetName( getAutoSaveFileName() );
autoSaveFn.ClearExt();
CheckForAutoSaveFile( autoSaveFn );
if( ( aCtl & KICTL_REVERT ) )
{
DeleteAutoSaveFile( fullFileName );
}
else
{
// This will rename the file if there is an autosave and the user wants to recover
CheckForAutoSaveFile( autoSaveFn );
}
SetScreen( nullptr );
@ -1517,6 +1525,15 @@ bool SCH_EDIT_FRAME::updateAutoSaveFile()
}
void removeFile( const wxString& aFilename, wxArrayString& aUnremoved )
{
wxLogTrace( traceAutoSave, wxS( "Removing auto save file " ) + aFilename );
if( wxFileExists( aFilename ) && !wxRemoveFile( aFilename ) )
aUnremoved.Add( aFilename );
};
void SCH_EDIT_FRAME::CheckForAutoSaveFile( const wxFileName& aFileName )
{
if( !Pgm().IsGUI() )
@ -1538,11 +1555,11 @@ void SCH_EDIT_FRAME::CheckForAutoSaveFile( const wxFileName& aFileName )
int response = wxMessageBox( msg, Pgm().App().GetAppDisplayName(), wxYES_NO | wxICON_QUESTION,
this );
wxTextFile autoSaveFile( aFileName.GetFullPath() );
wxTextFile fileList( aFileName.GetFullPath() );
if( !autoSaveFile.Open() )
if( !fileList.Open() )
{
msg.Printf( _( "The file '%s` could not be opened.\n"
msg.Printf( _( "The file '%s' could not be opened.\n"
"Manual recovery of automatically saved files is required." ),
aFileName.GetFullPath() );
@ -1554,8 +1571,7 @@ void SCH_EDIT_FRAME::CheckForAutoSaveFile( const wxFileName& aFileName )
{
wxArrayString unrecoveredFiles;
for( wxString fn = autoSaveFile.GetFirstLine(); !autoSaveFile.Eof();
fn = autoSaveFile.GetNextLine() )
for( wxString fn = fileList.GetFirstLine(); !fileList.Eof(); fn = fileList.GetNextLine() )
{
wxFileName recoveredFn = fn;
wxString tmp = recoveredFn.GetName();
@ -1602,50 +1618,53 @@ void SCH_EDIT_FRAME::CheckForAutoSaveFile( const wxFileName& aFileName )
wxMessageBox( msg, Pgm().App().GetAppDisplayName(), wxOK | wxICON_EXCLAMATION,
this );
}
}
else
{
wxArrayString unremovedFiles;
for( wxString fn = autoSaveFile.GetFirstLine(); !autoSaveFile.Eof();
fn = autoSaveFile.GetNextLine() )
{
wxLogTrace( traceAutoSave, wxS( "Removing auto save file " ) + fn );
if( wxFileExists( fn ) && !wxRemoveFile( fn ) )
unremovedFiles.Add( fn );
}
removeFile( aFileName.GetFullPath(), unremovedFiles );
if( !unremovedFiles.IsEmpty() )
{
msg = _( "The following automatically saved file(s) could not be removed\n" );
msg.Printf( _( "The autosave file '%s' could not be removed.\n"
"Manual removal will be required." ),
unremovedFiles[0] );
for( size_t i = 0; i < unremovedFiles.GetCount(); i++ )
msg += unremovedFiles[i] + wxS( "\n" );
msg += _( "Manual removal will be required for the file(s) above." );
wxMessageBox( msg, Pgm().App().GetAppDisplayName(), wxOK | wxICON_EXCLAMATION,
this );
wxMessageBox( msg, Pgm().App().GetAppDisplayName(), wxOK | wxICON_EXCLAMATION, this );
}
}
// Remove the auto save master file.
wxLogTrace( traceAutoSave, wxS( "Removing auto save file '%s'" ), aFileName.GetFullPath() );
if( !wxRemoveFile( aFileName.GetFullPath() ) )
else
{
msg.Printf( _( "The automatic save master file\n"
"'%s'\n"
"could not be deleted." ), aFileName.GetFullPath() );
DeleteAutoSaveFile( aFileName );
}
}
wxMessageDialog dlg( this, msg, Pgm().App().GetAppDisplayName(),
wxOK | wxICON_EXCLAMATION | wxCENTER );
dlg.SetExtendedMessage(
_( "This file must be manually removed or the auto save feature will be\n"
"shown every time the schematic editor is launched." ) );
void SCH_EDIT_FRAME::DeleteAutoSaveFile( const wxFileName& aFileName )
{
if( !Pgm().IsGUI() )
return;
dlg.ShowModal();
wxCHECK_RET( aFileName.IsOk(), wxS( "Invalid file name!" ) );
if( !aFileName.FileExists() )
return;
wxTextFile fileList( aFileName.GetFullPath() );
wxArrayString unremovedFiles;
for( wxString fn = fileList.GetFirstLine(); !fileList.Eof(); fn = fileList.GetNextLine() )
removeFile( fn, unremovedFiles );
removeFile( aFileName.GetFullPath(), unremovedFiles );
if( !unremovedFiles.IsEmpty() )
{
wxString msg = _( "The following automatically saved file(s) could not be removed\n" );
for( size_t i = 0; i < unremovedFiles.GetCount(); i++ )
msg += unremovedFiles[i] + wxS( "\n" );
msg += _( "Manual removal will be required for the file(s) above." );
wxMessageBox( msg, Pgm().App().GetAppDisplayName(), wxOK | wxICON_EXCLAMATION, this );
}
}

View File

@ -3,6 +3,7 @@
*
* Copyright (C) 2015 Jean-Pierre Charras, jp.charras wanadoo.fr
* Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
* Copyright (C) 2023 CERN (www.cern.ch)
* Copyright (C) 2004-2023 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
@ -809,6 +810,7 @@ public:
*/
virtual void CheckForAutoSaveFile( const wxFileName& aFileName ) override;
virtual void DeleteAutoSaveFile( const wxFileName& aFileName ) override;
/**
* Toggle the show/hide state of the left side schematic navigation panel

View File

@ -1,7 +1,7 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2019 CERN
* Copyright (C) 2019-2023 CERN
* Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
@ -160,7 +160,7 @@ int SCH_EDITOR_CONTROL::Revert( const TOOL_EVENT& aEvent )
screen->SetContentModified( false ); // do not prompt the user for changes
m_frame->ReleaseFile();
m_frame->OpenProjectFiles( std::vector<wxString>( 1, schematic.GetFileName() ) );
m_frame->OpenProjectFiles( std::vector<wxString>( 1, schematic.GetFileName() ), KICTL_REVERT );
return 0;
}

View File

@ -3,6 +3,7 @@
*
* Copyright (C) 2009-2015 Jean-Pierre Charras, jp.charras wanadoo.fr
* Copyright (C) 2011 Wayne Stambaugh <stambaughw@gmail.com>
* Copyright (C) 2023 CERN (www.cern.ch)
* Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
@ -476,6 +477,8 @@ public:
*/
virtual void CheckForAutoSaveFile( const wxFileName& aFileName );
virtual void DeleteAutoSaveFile( const wxFileName& aFileName );
/**
* Update the status bar information.
*

View File

@ -3,6 +3,7 @@
*
* Copyright (C) 2014 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 2017-2023 KiCad Developers, see change_log.txt for contributors.
* Copyright (C) 2023 CERN (www.cern.ch)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -76,6 +77,7 @@ public:
#define KICTL_KICAD_ONLY (1<<1) ///< chosen file is from KiCad according to user
#define KICTL_CREATE (1<<2) ///< caller thinks requested project files may not exist.
#define KICTL_IMPORT_LIB (1<<3) ///< import all footprints into a project library.
#define KICTL_REVERT (1<<4) ///< reverting to a previously-saved (KiCad) file.
/**
* Open a project or set of files given by @a aFileList.

View File

@ -3,6 +3,7 @@
*
* Copyright (C) 2004-2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2011 Wayne Stambaugh <stambaughw@gmail.com>
* Copyright (C) 2023 CERN (www.cern.ch)
* Copyright (C) 2016-2023 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
@ -322,7 +323,7 @@ bool PCB_EDIT_FRAME::Files_io_from_id( int id )
ReleaseFile();
return OpenProjectFiles( std::vector<wxString>( 1, fn.GetFullPath() ) );
return OpenProjectFiles( std::vector<wxString>( 1, fn.GetFullPath() ), KICTL_REVERT );
}
case ID_NEW_BOARD:
@ -622,8 +623,15 @@ bool PCB_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, in
std::placeholders::_1 ) );
}
// This will rename the file if there is an autosave and the user want to recover
CheckForAutoSaveFile( fullFileName );
if( ( aCtl & KICTL_REVERT ) )
{
DeleteAutoSaveFile( fullFileName );
}
else
{
// This will rename the file if there is an autosave and the user wants to recover
CheckForAutoSaveFile( fullFileName );
}
bool failedLoad = false;