Fix a few issues with legacy project loading

- Prevent crash on project tree for "Empty project" item
- Correctly handle loading of legacy projects in tree view
- Fix an issue where legacy projects would not get saved

Fixes https://gitlab.com/kicad/code/kicad/-/issues/4944
This commit is contained in:
Jon Evans 2020-07-20 19:39:59 -04:00
parent 1790c536ef
commit a107f7ce5b
2 changed files with 25 additions and 1 deletions

View File

@ -311,7 +311,8 @@ bool JSON_SETTINGS::SaveToFile( const std::string& aDirectory, bool aForce )
return false;
}
if( !path.IsFileWritable() )
if( ( path.FileExists() && !path.IsFileWritable() ) ||
( !path.FileExists() && !path.IsDirWritable() ) )
{
wxLogTrace( traceSettings, "File for %s is read-only; not saving", GetFullFilename() );
return false;

View File

@ -544,6 +544,7 @@ void TREE_PROJECT_FRAME::ReCreateTreePrj()
return;
wxFileName fn = pro_dir;
bool prjReset = false;
if( !fn.IsOk() )
{
@ -551,10 +552,22 @@ void TREE_PROJECT_FRAME::ReCreateTreePrj()
fn.SetPath( wxStandardPaths::Get().GetDocumentsDir() );
fn.SetName( NAMELESS_PROJECT );
fn.SetExt( ProjectFileExtension );
prjReset = true;
}
bool prjOpened = fn.FileExists();
// We may have opened a legacy project, in which case GetProjectFileName will return the
// name of the migrated (new format) file, which may not have been saved to disk yet.
if( !prjOpened && !prjReset )
{
fn.SetExt( LegacyProjectFileExtension );
prjOpened = fn.FileExists();
// Set the ext back so that in the tree view we see the (not-yet-saved) new file
fn.SetExt( ProjectFileExtension );
}
// root tree:
m_root = m_TreeProject->AddRoot( fn.GetFullName(), TREE_ROOT, TREE_ROOT );
m_TreeProject->SetItemBold( m_root, true );
@ -631,6 +644,16 @@ void TREE_PROJECT_FRAME::OnRight( wxTreeEvent& Event )
for( TREEPROJECT_ITEM* item_data : tree_data )
{
// Check for empty project
if( !item_data )
{
can_switch_to_project = false;
can_edit = false;
can_rename = false;
can_print = false;
continue;
}
int tree_id = item_data->GetType();
wxString full_file_name = item_data->GetFileName();