2020-01-13 01:44:19 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 Jon Evans <jon@craftyjon.com>
|
2021-08-13 09:15:12 +00:00
|
|
|
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-01-13 01:44:19 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-12-03 01:41:57 +00:00
|
|
|
#include "settings/json_settings.h"
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <regex>
|
|
|
|
#include <wx/debug.h>
|
2021-03-20 15:35:37 +00:00
|
|
|
#include <wx/dir.h>
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <wx/filename.h>
|
2021-04-02 23:29:47 +00:00
|
|
|
#include <wx/snglinst.h>
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <wx/stdpaths.h>
|
|
|
|
#include <wx/utils.h>
|
|
|
|
|
|
|
|
#include <build_version.h>
|
|
|
|
#include <confirm.h>
|
|
|
|
#include <dialogs/dialog_migrate_settings.h>
|
|
|
|
#include <gestfich.h>
|
2021-01-28 00:07:38 +00:00
|
|
|
#include <kiplatform/environment.h>
|
2020-08-09 18:49:26 +00:00
|
|
|
#include <kiway.h>
|
2021-04-02 23:29:47 +00:00
|
|
|
#include <lockfile.h>
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <macros.h>
|
2021-07-11 06:24:30 +00:00
|
|
|
#include <pgm_base.h>
|
2021-01-28 02:06:13 +00:00
|
|
|
#include <paths.h>
|
2020-05-25 17:06:53 +00:00
|
|
|
#include <project.h>
|
2020-07-03 13:40:31 +00:00
|
|
|
#include <project/project_archiver.h>
|
2020-05-31 21:42:04 +00:00
|
|
|
#include <project/project_file.h>
|
|
|
|
#include <project/project_local_settings.h>
|
2020-05-25 17:06:53 +00:00
|
|
|
#include <settings/color_settings.h>
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <settings/common_settings.h>
|
2021-06-04 03:52:50 +00:00
|
|
|
#include <settings/json_settings_internals.h>
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <settings/settings_manager.h>
|
2020-05-25 20:41:24 +00:00
|
|
|
#include <wildcards_and_files_ext.h>
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
|
2020-06-20 14:36:16 +00:00
|
|
|
SETTINGS_MANAGER::SETTINGS_MANAGER( bool aHeadless ) :
|
|
|
|
m_headless( aHeadless ),
|
2020-08-09 18:49:26 +00:00
|
|
|
m_kiway( nullptr ),
|
2020-04-08 21:34:18 +00:00
|
|
|
m_common_settings( nullptr ),
|
2020-09-25 00:54:31 +00:00
|
|
|
m_migration_source(),
|
|
|
|
m_migrateLibraryTables( true )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
|
|
|
// Check if the settings directory already exists, and if not, perform a migration if possible
|
|
|
|
if( !MigrateIfNeeded() )
|
|
|
|
{
|
|
|
|
m_ok = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_ok = true;
|
|
|
|
|
|
|
|
// create the common settings shared by all applications. Not loaded immediately
|
2021-08-04 15:44:12 +00:00
|
|
|
m_common_settings = RegisterSettings( new COMMON_SETTINGS, false );
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SETTINGS_MANAGER::~SETTINGS_MANAGER()
|
|
|
|
{
|
|
|
|
m_settings.clear();
|
|
|
|
m_color_settings.clear();
|
2020-05-25 17:38:09 +00:00
|
|
|
m_projects.clear();
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-04 15:44:12 +00:00
|
|
|
JSON_SETTINGS* SETTINGS_MANAGER::registerSettings( JSON_SETTINGS* aSettings, bool aLoadNow )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
|
|
|
std::unique_ptr<JSON_SETTINGS> ptr( aSettings );
|
|
|
|
|
2020-05-06 01:16:05 +00:00
|
|
|
ptr->SetManager( this );
|
|
|
|
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Registered new settings object <%s>" ), ptr->GetFullFilename() );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
if( aLoadNow )
|
|
|
|
ptr->LoadFromFile( GetPathForSettingsFile( ptr.get() ) );
|
|
|
|
|
|
|
|
m_settings.push_back( std::move( ptr ) );
|
|
|
|
return m_settings.back().get();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SETTINGS_MANAGER::Load()
|
|
|
|
{
|
|
|
|
// TODO(JE) We should check for dirty settings here and write them if so, because
|
|
|
|
// Load() could be called late in the application lifecycle
|
|
|
|
|
|
|
|
for( auto&& settings : m_settings )
|
|
|
|
settings->LoadFromFile( GetPathForSettingsFile( settings.get() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SETTINGS_MANAGER::Load( JSON_SETTINGS* aSettings )
|
|
|
|
{
|
|
|
|
auto it = std::find_if( m_settings.begin(), m_settings.end(),
|
2020-04-08 21:34:18 +00:00
|
|
|
[&aSettings]( const std::unique_ptr<JSON_SETTINGS>& aPtr )
|
|
|
|
{
|
|
|
|
return aPtr.get() == aSettings;
|
2020-01-13 01:44:19 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
if( it != m_settings.end() )
|
|
|
|
( *it )->LoadFromFile( GetPathForSettingsFile( it->get() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SETTINGS_MANAGER::Save()
|
|
|
|
{
|
|
|
|
for( auto&& settings : m_settings )
|
|
|
|
{
|
|
|
|
// Never automatically save color settings, caller should use SaveColorSettings
|
|
|
|
if( dynamic_cast<COLOR_SETTINGS*>( settings.get() ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
settings->SaveToFile( GetPathForSettingsFile( settings.get() ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SETTINGS_MANAGER::Save( JSON_SETTINGS* aSettings )
|
|
|
|
{
|
|
|
|
auto it = std::find_if( m_settings.begin(), m_settings.end(),
|
2020-04-08 21:34:18 +00:00
|
|
|
[&aSettings]( const std::unique_ptr<JSON_SETTINGS>& aPtr )
|
|
|
|
{
|
|
|
|
return aPtr.get() == aSettings;
|
2020-01-13 01:44:19 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
if( it != m_settings.end() )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Saving %s" ), ( *it )->GetFullFilename() );
|
2020-01-13 01:44:19 +00:00
|
|
|
( *it )->SaveToFile( GetPathForSettingsFile( it->get() ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
void SETTINGS_MANAGER::FlushAndRelease( JSON_SETTINGS* aSettings, bool aSave )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
|
|
|
auto it = std::find_if( m_settings.begin(), m_settings.end(),
|
2020-04-08 21:34:18 +00:00
|
|
|
[&aSettings]( const std::unique_ptr<JSON_SETTINGS>& aPtr )
|
|
|
|
{
|
|
|
|
return aPtr.get() == aSettings;
|
|
|
|
} );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
if( it != m_settings.end() )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Flush and release %s" ), ( *it )->GetFullFilename() );
|
2020-05-31 21:42:04 +00:00
|
|
|
|
|
|
|
if( aSave )
|
|
|
|
( *it )->SaveToFile( GetPathForSettingsFile( it->get() ) );
|
|
|
|
|
2021-03-19 16:12:41 +00:00
|
|
|
size_t typeHash = typeid( *it->get() ).hash_code();
|
|
|
|
|
|
|
|
if( m_app_settings_cache.count( typeHash ) )
|
|
|
|
m_app_settings_cache.erase( typeHash );
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
m_settings.erase( it );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-03 16:46:58 +00:00
|
|
|
COLOR_SETTINGS* SETTINGS_MANAGER::GetColorSettings( const wxString& aName )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2020-04-08 21:34:18 +00:00
|
|
|
if( m_color_settings.count( aName ) )
|
|
|
|
return m_color_settings.at( aName );
|
|
|
|
|
|
|
|
if( !aName.empty() )
|
2021-08-02 01:17:54 +00:00
|
|
|
{
|
|
|
|
COLOR_SETTINGS* ret = loadColorSettingsByName( aName );
|
2020-02-03 16:46:58 +00:00
|
|
|
|
2021-08-02 01:17:54 +00:00
|
|
|
if( !ret )
|
|
|
|
{
|
|
|
|
ret = registerColorSettings( aName );
|
|
|
|
*ret = *m_color_settings.at( "_builtin_default" );
|
|
|
|
ret->SetFilename( wxT( "user" ) );
|
|
|
|
ret->SetReadOnly( false );
|
|
|
|
}
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2021-08-02 01:17:54 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This had better work
|
|
|
|
return m_color_settings.at( "_builtin_default" );
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-03 16:46:58 +00:00
|
|
|
COLOR_SETTINGS* SETTINGS_MANAGER::loadColorSettingsByName( const wxString& aName )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Attempting to load color theme %s" ), aName );
|
2020-02-03 16:46:58 +00:00
|
|
|
|
|
|
|
wxFileName fn( GetColorSettingsPath(), aName, "json" );
|
|
|
|
|
|
|
|
if( !fn.IsOk() || !fn.Exists() )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Theme file %s.json not found, falling back to user" ), aName );
|
2020-02-03 16:46:58 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-08-04 15:44:12 +00:00
|
|
|
COLOR_SETTINGS* settings = RegisterSettings( new COLOR_SETTINGS( aName ) );
|
2020-02-03 16:46:58 +00:00
|
|
|
|
2021-08-04 15:44:12 +00:00
|
|
|
if( settings->GetFilename() != aName.ToStdString() )
|
2021-08-04 12:51:38 +00:00
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Warning: stored filename is actually %s, " ),
|
2021-08-04 15:44:12 +00:00
|
|
|
settings->GetFilename() );
|
2021-08-04 12:51:38 +00:00
|
|
|
}
|
2020-02-03 16:46:58 +00:00
|
|
|
|
2021-08-04 15:44:12 +00:00
|
|
|
m_color_settings[aName] = settings;
|
2020-02-03 16:46:58 +00:00
|
|
|
|
2021-08-04 15:44:12 +00:00
|
|
|
return settings;
|
2020-02-03 16:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-11 06:24:30 +00:00
|
|
|
class JSON_DIR_TRAVERSER : public wxDirTraverser
|
2020-02-03 16:46:58 +00:00
|
|
|
{
|
|
|
|
private:
|
2021-07-11 06:24:30 +00:00
|
|
|
std::function<void( const wxFileName& )> m_action;
|
2020-02-03 16:46:58 +00:00
|
|
|
|
|
|
|
public:
|
2021-07-11 06:24:30 +00:00
|
|
|
explicit JSON_DIR_TRAVERSER( std::function<void( const wxFileName& )> aAction )
|
2020-02-03 16:46:58 +00:00
|
|
|
: m_action( std::move( aAction ) )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
wxDirTraverseResult OnFile( const wxString& aFilePath ) override
|
|
|
|
{
|
|
|
|
wxFileName file( aFilePath );
|
|
|
|
|
2021-07-11 06:24:30 +00:00
|
|
|
if( file.GetExt() == "json" )
|
|
|
|
m_action( file );
|
2020-02-03 16:46:58 +00:00
|
|
|
|
|
|
|
return wxDIR_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxDirTraverseResult OnDir( const wxString& dirPath ) override
|
|
|
|
{
|
2021-07-11 06:24:30 +00:00
|
|
|
return wxDIR_CONTINUE;
|
2020-02-03 16:46:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-11-10 03:11:59 +00:00
|
|
|
COLOR_SETTINGS* SETTINGS_MANAGER::registerColorSettings( const wxString& aName, bool aAbsolutePath )
|
2020-02-03 16:46:58 +00:00
|
|
|
{
|
2021-08-02 01:17:54 +00:00
|
|
|
if( !m_color_settings.count( aName ) )
|
|
|
|
{
|
2021-11-10 03:11:59 +00:00
|
|
|
COLOR_SETTINGS* colorSettings = RegisterSettings( new COLOR_SETTINGS( aName,
|
|
|
|
aAbsolutePath ) );
|
2021-08-04 15:44:12 +00:00
|
|
|
m_color_settings[aName] = colorSettings;
|
2021-08-02 01:17:54 +00:00
|
|
|
}
|
2020-08-07 16:31:17 +00:00
|
|
|
|
2021-08-02 01:17:54 +00:00
|
|
|
return m_color_settings.at( aName );
|
2020-02-03 16:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-02 01:17:54 +00:00
|
|
|
COLOR_SETTINGS* SETTINGS_MANAGER::AddNewColorSettings( const wxString& aName )
|
2020-02-03 16:46:58 +00:00
|
|
|
{
|
2021-08-02 01:17:54 +00:00
|
|
|
if( aName.EndsWith( wxT( ".json" ) ) )
|
|
|
|
return registerColorSettings( aName.BeforeLast( '.' ) );
|
|
|
|
else
|
|
|
|
return registerColorSettings( aName );
|
2020-02-03 16:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-15 01:57:36 +00:00
|
|
|
COLOR_SETTINGS* SETTINGS_MANAGER::GetMigratedColorSettings()
|
|
|
|
{
|
|
|
|
if( !m_color_settings.count( "user" ) )
|
|
|
|
{
|
2021-08-02 01:17:54 +00:00
|
|
|
COLOR_SETTINGS* settings = registerColorSettings( wxT( "user" ) );
|
|
|
|
settings->SetName( wxT( "User" ) );
|
|
|
|
Save( settings );
|
2020-10-15 01:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_color_settings.at( "user" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-03 16:46:58 +00:00
|
|
|
void SETTINGS_MANAGER::loadAllColorSettings()
|
|
|
|
{
|
2020-10-15 01:57:36 +00:00
|
|
|
// Create the built-in color settings
|
|
|
|
for( COLOR_SETTINGS* settings : COLOR_SETTINGS::CreateBuiltinColorSettings() )
|
2021-08-04 15:44:12 +00:00
|
|
|
m_color_settings[settings->GetFilename()] = RegisterSettings( settings, false );
|
2020-02-03 16:46:58 +00:00
|
|
|
|
2021-07-11 06:24:30 +00:00
|
|
|
wxFileName third_party_path;
|
|
|
|
const ENV_VAR_MAP& env = Pgm().GetLocalEnvVariables();
|
|
|
|
auto it = env.find( "KICAD6_3RD_PARTY" );
|
|
|
|
|
|
|
|
if( it != env.end() && !it->second.GetValue().IsEmpty() )
|
|
|
|
third_party_path.SetPath( it->second.GetValue() );
|
|
|
|
else
|
|
|
|
third_party_path.SetPath( PATHS::GetDefault3rdPartyPath() );
|
|
|
|
|
|
|
|
third_party_path.AppendDir( "colors" );
|
|
|
|
|
|
|
|
wxDir third_party_colors_dir( third_party_path.GetFullPath() );
|
|
|
|
wxString color_settings_path = GetColorSettingsPath();
|
|
|
|
|
2020-02-03 16:46:58 +00:00
|
|
|
// Search for and load any other settings
|
2021-07-11 06:24:30 +00:00
|
|
|
JSON_DIR_TRAVERSER loader( [&]( const wxFileName& aFilename )
|
|
|
|
{
|
|
|
|
registerColorSettings( aFilename.GetName() );
|
|
|
|
} );
|
2020-02-03 16:46:58 +00:00
|
|
|
|
2021-11-10 03:11:59 +00:00
|
|
|
JSON_DIR_TRAVERSER thirdPartyLoader(
|
|
|
|
[&]( const wxFileName& aFilename )
|
|
|
|
{
|
|
|
|
COLOR_SETTINGS* settings = registerColorSettings( aFilename.GetFullPath(), true );
|
|
|
|
settings->SetReadOnly( true );
|
|
|
|
} );
|
|
|
|
|
2021-07-11 06:24:30 +00:00
|
|
|
wxDir colors_dir( color_settings_path );
|
2020-02-03 16:46:58 +00:00
|
|
|
|
|
|
|
if( colors_dir.IsOpened() )
|
2021-07-11 06:24:30 +00:00
|
|
|
{
|
|
|
|
if( third_party_colors_dir.IsOpened() )
|
2021-11-10 03:11:59 +00:00
|
|
|
third_party_colors_dir.Traverse( thirdPartyLoader );
|
2021-07-11 06:24:30 +00:00
|
|
|
|
2020-02-03 16:46:58 +00:00
|
|
|
colors_dir.Traverse( loader );
|
2021-07-11 06:24:30 +00:00
|
|
|
}
|
2020-02-03 16:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-08 21:34:18 +00:00
|
|
|
void SETTINGS_MANAGER::ReloadColorSettings()
|
|
|
|
{
|
|
|
|
m_color_settings.clear();
|
|
|
|
loadAllColorSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
void SETTINGS_MANAGER::SaveColorSettings( COLOR_SETTINGS* aSettings, const std::string& aNamespace )
|
|
|
|
{
|
|
|
|
// The passed settings should already be managed
|
|
|
|
wxASSERT( std::find_if( m_color_settings.begin(), m_color_settings.end(),
|
2020-04-08 21:34:18 +00:00
|
|
|
[aSettings] ( const std::pair<wxString, COLOR_SETTINGS*>& el )
|
|
|
|
{
|
|
|
|
return el.second->GetFilename() == aSettings->GetFilename();
|
|
|
|
}
|
|
|
|
) != m_color_settings.end() );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2021-06-04 03:52:50 +00:00
|
|
|
if( aSettings->IsReadOnly() )
|
|
|
|
return;
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2020-05-30 16:56:16 +00:00
|
|
|
if( !aSettings->Store() )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Color scheme %s not modified; skipping save" ),
|
2021-06-16 16:28:16 +00:00
|
|
|
aNamespace );
|
2020-05-30 16:56:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2021-06-04 03:52:50 +00:00
|
|
|
wxASSERT( aSettings->Contains( aNamespace ) );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Saving color scheme %s, preserving %s" ),
|
2021-06-16 16:28:16 +00:00
|
|
|
aSettings->GetFilename(),
|
2021-06-04 03:52:50 +00:00
|
|
|
aNamespace );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2021-06-16 16:28:16 +00:00
|
|
|
OPT<nlohmann::json> backup = aSettings->GetJson( aNamespace );
|
2020-08-02 09:23:21 +00:00
|
|
|
wxString path = GetColorSettingsPath();
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
aSettings->LoadFromFile( path );
|
|
|
|
|
2021-06-16 16:28:16 +00:00
|
|
|
if( backup )
|
|
|
|
( *aSettings->Internals() )[aNamespace].update( *backup );
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
aSettings->Load();
|
|
|
|
|
2020-05-30 16:56:16 +00:00
|
|
|
aSettings->SaveToFile( path, true );
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-02 09:23:21 +00:00
|
|
|
wxString SETTINGS_MANAGER::GetPathForSettingsFile( JSON_SETTINGS* aSettings )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
|
|
|
wxASSERT( aSettings );
|
|
|
|
|
|
|
|
switch( aSettings->GetLocation() )
|
|
|
|
{
|
|
|
|
case SETTINGS_LOC::USER:
|
|
|
|
return GetUserSettingsPath();
|
|
|
|
|
|
|
|
case SETTINGS_LOC::PROJECT:
|
2022-03-10 14:08:20 +00:00
|
|
|
// TODO: MDI support
|
2020-08-05 16:34:22 +00:00
|
|
|
return Prj().GetProjectPath();
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
case SETTINGS_LOC::COLORS:
|
|
|
|
return GetColorSettingsPath();
|
|
|
|
|
2020-05-25 17:06:53 +00:00
|
|
|
case SETTINGS_LOC::NONE:
|
|
|
|
return "";
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
default:
|
2022-02-03 20:57:26 +00:00
|
|
|
wxASSERT_MSG( false, wxT( "Unknown settings location!" ) );
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class MIGRATION_TRAVERSER : public wxDirTraverser
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
wxString m_src;
|
|
|
|
wxString m_dest;
|
|
|
|
wxString m_errors;
|
2020-09-25 00:54:31 +00:00
|
|
|
bool m_migrateTables;
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
public:
|
2020-09-25 00:54:31 +00:00
|
|
|
MIGRATION_TRAVERSER( const wxString& aSrcDir, const wxString& aDestDir, bool aMigrateTables ) :
|
|
|
|
m_src( aSrcDir ),
|
|
|
|
m_dest( aDestDir ),
|
|
|
|
m_migrateTables( aMigrateTables )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString GetErrors() { return m_errors; }
|
|
|
|
|
2020-09-25 00:54:31 +00:00
|
|
|
wxDirTraverseResult OnFile( const wxString& aSrcFilePath ) override
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
|
|
|
wxFileName file( aSrcFilePath );
|
2020-09-25 00:54:31 +00:00
|
|
|
|
|
|
|
if( !m_migrateTables && ( file.GetName() == wxT( "sym-lib-table" ) ||
|
|
|
|
file.GetName() == wxT( "fp-lib-table" ) ) )
|
|
|
|
{
|
|
|
|
return wxDIR_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2021-11-23 22:49:40 +00:00
|
|
|
// Skip migrating PCM installed packages as packages themselves are not moved
|
|
|
|
if( file.GetFullName() == wxT( "installed_packages.json" ) )
|
|
|
|
return wxDIR_CONTINUE;
|
|
|
|
|
2021-03-29 22:14:24 +00:00
|
|
|
// Don't migrate hotkeys config files; we don't have a reasonable migration handler for them
|
|
|
|
// and so there is no way to resolve conflicts at the moment
|
|
|
|
if( file.GetExt() == wxT( "hotkeys" ) )
|
|
|
|
return wxDIR_CONTINUE;
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
wxString path = file.GetPath();
|
|
|
|
|
|
|
|
path.Replace( m_src, m_dest, false );
|
|
|
|
file.SetPath( path );
|
|
|
|
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Copying %s to %s" ), aSrcFilePath, file.GetFullPath() );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
// For now, just copy everything
|
2020-10-02 18:36:06 +00:00
|
|
|
KiCopyFile( aSrcFilePath, file.GetFullPath(), m_errors );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
return wxDIR_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2020-09-25 00:54:31 +00:00
|
|
|
wxDirTraverseResult OnDir( const wxString& dirPath ) override
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
|
|
|
wxFileName dir( dirPath );
|
|
|
|
|
|
|
|
// Whitelist of directories to migrate
|
|
|
|
if( dir.GetName() == "colors" ||
|
|
|
|
dir.GetName() == "3d" )
|
|
|
|
{
|
|
|
|
|
|
|
|
wxString path = dir.GetPath();
|
|
|
|
|
|
|
|
path.Replace( m_src, m_dest, false );
|
|
|
|
dir.SetPath( path );
|
|
|
|
|
|
|
|
wxMkdir( dir.GetFullPath() );
|
|
|
|
|
|
|
|
return wxDIR_CONTINUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return wxDIR_IGNORE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool SETTINGS_MANAGER::MigrateIfNeeded()
|
|
|
|
{
|
2020-09-22 22:57:19 +00:00
|
|
|
if( m_headless )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Settings migration not checked; running headless" ) );
|
2020-09-22 22:57:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
wxFileName path( GetUserSettingsPath(), "" );
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Using settings path %s" ), path.GetFullPath() );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
if( path.DirExists() )
|
|
|
|
{
|
|
|
|
wxFileName common = path;
|
|
|
|
common.SetName( "kicad_common" );
|
|
|
|
common.SetExt( "json" );
|
|
|
|
|
|
|
|
if( common.Exists() )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Path exists and has a kicad_common, continuing!" ) );
|
2020-01-13 01:44:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we have an empty path, let's figure out what to put in it
|
|
|
|
DIALOG_MIGRATE_SETTINGS dlg( this );
|
|
|
|
|
|
|
|
if( dlg.ShowModal() != wxID_OK )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Migration dialog canceled; exiting" ) );
|
2020-01-13 01:44:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !path.DirExists() )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Path didn't exist; creating it" ) );
|
2020-01-13 01:44:19 +00:00
|
|
|
path.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_migration_source.IsEmpty() )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "No migration source given; starting with defaults" ) );
|
2020-01-13 01:44:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Migrating from path %s" ), m_migration_source );
|
2021-12-03 01:41:57 +00:00
|
|
|
|
2020-09-25 00:54:31 +00:00
|
|
|
MIGRATION_TRAVERSER traverser( m_migration_source, path.GetFullPath(), m_migrateLibraryTables );
|
2020-01-13 01:44:19 +00:00
|
|
|
wxDir source_dir( m_migration_source );
|
|
|
|
|
|
|
|
source_dir.Traverse( traverser );
|
|
|
|
|
|
|
|
if( !traverser.GetErrors().empty() )
|
|
|
|
DisplayErrorMessage( nullptr, traverser.GetErrors() );
|
|
|
|
|
2021-11-17 01:48:11 +00:00
|
|
|
// Remove any library configuration if we didn't choose to import
|
|
|
|
if( !m_migrateLibraryTables )
|
|
|
|
{
|
|
|
|
COMMON_SETTINGS common;
|
2021-11-20 20:32:28 +00:00
|
|
|
wxString commonPath = GetPathForSettingsFile( &common );
|
|
|
|
common.LoadFromFile( commonPath );
|
2021-11-17 01:48:11 +00:00
|
|
|
|
|
|
|
const std::vector<wxString> libKeys = {
|
|
|
|
wxT( "KICAD6_SYMBOL_DIR" ),
|
|
|
|
wxT( "KICAD6_3DMODEL_DIR" ),
|
|
|
|
wxT( "KICAD6_FOOTPRINT_DIR" ),
|
2021-11-17 19:45:08 +00:00
|
|
|
wxT( "KICAD6_TEMPLATE_DIR" ), // Stores the default library table to be copied
|
2021-11-17 01:48:11 +00:00
|
|
|
|
|
|
|
// Deprecated keys
|
|
|
|
wxT( "KICAD_PTEMPLATES" ),
|
|
|
|
wxT( "KISYS3DMOD" ),
|
|
|
|
wxT( "KISYSMOD" ),
|
|
|
|
wxT( "KICAD_SYMBOL_DIR" ),
|
|
|
|
};
|
2021-11-17 19:45:08 +00:00
|
|
|
|
2021-11-17 01:48:11 +00:00
|
|
|
for( const wxString& key : libKeys )
|
|
|
|
common.m_Env.vars.erase( key );
|
|
|
|
|
2021-11-20 20:32:28 +00:00
|
|
|
common.SaveToFile( commonPath );
|
2021-11-17 01:48:11 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SETTINGS_MANAGER::GetPreviousVersionPaths( std::vector<wxString>* aPaths )
|
|
|
|
{
|
|
|
|
wxASSERT( aPaths );
|
|
|
|
|
|
|
|
aPaths->clear();
|
|
|
|
|
|
|
|
wxDir dir;
|
|
|
|
std::vector<wxFileName> base_paths;
|
|
|
|
|
|
|
|
base_paths.emplace_back( wxFileName( calculateUserSettingsPath( false ), "" ) );
|
|
|
|
|
|
|
|
// If the env override is set, also check the default paths
|
|
|
|
if( wxGetEnv( wxT( "KICAD_CONFIG_HOME" ), nullptr ) )
|
|
|
|
base_paths.emplace_back( wxFileName( calculateUserSettingsPath( false, false ), "" ) );
|
|
|
|
|
2021-02-14 20:03:22 +00:00
|
|
|
#ifdef __WXGTK__
|
|
|
|
// When running inside FlatPak, KIPLATFORM::ENV::GetUserConfigPath() will return a sandboxed
|
|
|
|
// path. In case the user wants to move from non-FlatPak KiCad to FlatPak KiCad, let's add our
|
|
|
|
// best guess as to the non-FlatPak config path. Unfortunately FlatPak also hides the host
|
|
|
|
// XDG_CONFIG_HOME, so if the user customizes their config path, they will have to browse
|
|
|
|
// for it.
|
|
|
|
{
|
|
|
|
wxFileName wxGtkPath;
|
|
|
|
wxGtkPath.AssignDir( "~/.config/kicad" );
|
|
|
|
wxGtkPath.MakeAbsolute();
|
|
|
|
base_paths.emplace_back( wxGtkPath.GetPath() );
|
|
|
|
|
|
|
|
// We also want to pick up regular flatpak if we are nightly
|
|
|
|
wxGtkPath.AssignDir( "~/.var/app/org.kicad.KiCad/config/kicad" );
|
|
|
|
wxGtkPath.MakeAbsolute();
|
|
|
|
base_paths.emplace_back( wxGtkPath.GetPath() );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
wxString subdir;
|
|
|
|
std::string mine = GetSettingsVersion();
|
|
|
|
|
|
|
|
auto check_dir = [&] ( const wxString& aSubDir )
|
|
|
|
{
|
|
|
|
// Only older versions are valid for migration
|
|
|
|
if( compareVersions( aSubDir.ToStdString(), mine ) <= 0 )
|
|
|
|
{
|
|
|
|
wxString sub_path = dir.GetNameWithSep() + aSubDir;
|
|
|
|
|
|
|
|
if( IsSettingsPathValid( sub_path ) )
|
|
|
|
{
|
|
|
|
aPaths->push_back( sub_path );
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "GetPreviousVersionName: %s is valid" ), sub_path );
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-14 20:03:22 +00:00
|
|
|
std::set<wxString> checkedPaths;
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
for( auto base_path : base_paths )
|
|
|
|
{
|
2021-02-14 20:03:22 +00:00
|
|
|
if( checkedPaths.count( base_path.GetFullPath() ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
checkedPaths.insert( base_path.GetFullPath() );
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
if( !dir.Open( base_path.GetFullPath() ) )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "GetPreviousVersionName: could not open base path %s" ),
|
2021-02-14 20:03:22 +00:00
|
|
|
base_path.GetFullPath() );
|
2020-01-13 01:44:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "GetPreviousVersionName: checking base path %s" ),
|
2021-02-14 20:03:22 +00:00
|
|
|
base_path.GetFullPath() );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
if( dir.GetFirst( &subdir, wxEmptyString, wxDIR_DIRS ) )
|
|
|
|
{
|
|
|
|
if( subdir != mine )
|
|
|
|
check_dir( subdir );
|
|
|
|
|
|
|
|
while( dir.GetNext( &subdir ) )
|
|
|
|
{
|
|
|
|
if( subdir != mine )
|
|
|
|
check_dir( subdir );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't find one yet, check for legacy settings without a version directory
|
|
|
|
if( IsSettingsPathValid( dir.GetNameWithSep() ) )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings,
|
2022-02-03 20:57:26 +00:00
|
|
|
wxT( "GetPreviousVersionName: root path %s is valid" ), dir.GetName() );
|
2020-01-13 01:44:19 +00:00
|
|
|
aPaths->push_back( dir.GetName() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return aPaths->size() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SETTINGS_MANAGER::IsSettingsPathValid( const wxString& aPath )
|
|
|
|
{
|
|
|
|
wxFileName test( aPath, "kicad_common" );
|
2021-02-14 20:03:22 +00:00
|
|
|
|
|
|
|
if( test.Exists() )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
test.SetExt( "json" );
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
return test.Exists();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-02 09:23:21 +00:00
|
|
|
wxString SETTINGS_MANAGER::GetColorSettingsPath()
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
|
|
|
wxFileName path;
|
|
|
|
|
|
|
|
path.AssignDir( GetUserSettingsPath() );
|
|
|
|
path.AppendDir( "colors" );
|
|
|
|
|
2020-10-25 20:56:22 +00:00
|
|
|
if( !path.DirExists() )
|
|
|
|
{
|
|
|
|
if( !wxMkdir( path.GetPath() ) )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings,
|
2022-02-03 20:57:26 +00:00
|
|
|
wxT( "GetColorSettingsPath(): Path %s missing and could not be created!" ),
|
2020-10-25 20:56:22 +00:00
|
|
|
path.GetPath() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-02 09:23:21 +00:00
|
|
|
return path.GetPath();
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-02 09:23:21 +00:00
|
|
|
wxString SETTINGS_MANAGER::GetUserSettingsPath()
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2020-08-02 09:23:21 +00:00
|
|
|
static wxString user_settings_path;
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
if( user_settings_path.empty() )
|
|
|
|
user_settings_path = calculateUserSettingsPath();
|
|
|
|
|
|
|
|
return user_settings_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-02 09:23:21 +00:00
|
|
|
wxString SETTINGS_MANAGER::calculateUserSettingsPath( bool aIncludeVer, bool aUseEnv )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
|
|
|
wxFileName cfgpath;
|
|
|
|
|
|
|
|
// http://docs.wxwidgets.org/3.0/classwx_standard_paths.html#a7c7cf595d94d29147360d031647476b0
|
|
|
|
|
|
|
|
wxString envstr;
|
|
|
|
if( aUseEnv && wxGetEnv( wxT( "KICAD_CONFIG_HOME" ), &envstr ) && !envstr.IsEmpty() )
|
|
|
|
{
|
|
|
|
// Override the assignment above with KICAD_CONFIG_HOME
|
|
|
|
cfgpath.AssignDir( envstr );
|
|
|
|
}
|
2021-01-28 00:07:38 +00:00
|
|
|
else
|
|
|
|
{
|
2021-01-28 00:11:39 +00:00
|
|
|
cfgpath.AssignDir( KIPLATFORM::ENV::GetUserConfigPath() );
|
2021-01-28 00:07:38 +00:00
|
|
|
|
|
|
|
cfgpath.AppendDir( TO_STR( KICAD_CONFIG_DIR ) );
|
|
|
|
}
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
if( aIncludeVer )
|
|
|
|
cfgpath.AppendDir( GetSettingsVersion() );
|
|
|
|
|
2020-08-07 16:31:17 +00:00
|
|
|
return cfgpath.GetPath();
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string SETTINGS_MANAGER::GetSettingsVersion()
|
|
|
|
{
|
2020-03-07 18:43:24 +00:00
|
|
|
// CMake computes the major.minor string for us.
|
|
|
|
return GetMajorMinorVersion().ToStdString();
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int SETTINGS_MANAGER::compareVersions( const std::string& aFirst, const std::string& aSecond )
|
|
|
|
{
|
|
|
|
int a_maj = 0;
|
|
|
|
int a_min = 0;
|
|
|
|
int b_maj = 0;
|
|
|
|
int b_min = 0;
|
|
|
|
|
|
|
|
if( !extractVersion( aFirst, &a_maj, &a_min ) || !extractVersion( aSecond, &b_maj, &b_min ) )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "compareSettingsVersions: bad input (%s, %s)" ), aFirst, aSecond );
|
2020-01-13 01:44:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( a_maj < b_maj )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if( a_maj > b_maj )
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( a_min < b_min )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if( a_min > b_min )
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SETTINGS_MANAGER::extractVersion( const std::string& aVersionString, int* aMajor, int* aMinor )
|
|
|
|
{
|
|
|
|
std::regex re_version( "(\\d+)\\.(\\d+)" );
|
|
|
|
std::smatch match;
|
|
|
|
|
|
|
|
if( std::regex_match( aVersionString, match, re_version ) )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
*aMajor = std::stoi( match[1].str() );
|
|
|
|
*aMinor = std::stoi( match[2].str() );
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-25 17:06:53 +00:00
|
|
|
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
bool SETTINGS_MANAGER::LoadProject( const wxString& aFullPath, bool aSetActive )
|
2020-05-25 17:06:53 +00:00
|
|
|
{
|
2020-05-25 20:41:24 +00:00
|
|
|
// Normalize path to new format even if migrating from a legacy file
|
|
|
|
wxFileName path( aFullPath );
|
|
|
|
|
|
|
|
if( path.GetExt() == LegacyProjectFileExtension )
|
|
|
|
path.SetExt( ProjectFileExtension );
|
|
|
|
|
|
|
|
wxString fullPath = path.GetFullPath();
|
|
|
|
|
2020-06-20 14:36:16 +00:00
|
|
|
// If already loaded, we are all set. This might be called more than once over a project's
|
|
|
|
// lifetime in case the project is first loaded by the KiCad manager and then eeschema or
|
|
|
|
// pcbnew try to load it again when they are launched.
|
|
|
|
if( m_projects.count( fullPath ) )
|
|
|
|
return true;
|
2020-05-25 17:38:09 +00:00
|
|
|
|
2021-04-02 23:29:47 +00:00
|
|
|
bool readOnly = false;
|
|
|
|
std::unique_ptr<wxSingleInstanceChecker> lockFile = ::LockFile( fullPath );
|
|
|
|
|
|
|
|
if( !lockFile )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Project %s is locked; opening read-only" ), fullPath );
|
2021-04-02 23:29:47 +00:00
|
|
|
readOnly = true;
|
|
|
|
}
|
|
|
|
|
2020-05-25 17:38:09 +00:00
|
|
|
// No MDI yet
|
2020-08-27 00:46:20 +00:00
|
|
|
if( aSetActive && !m_projects.empty() )
|
|
|
|
{
|
2020-10-06 02:13:06 +00:00
|
|
|
PROJECT* oldProject = m_projects.begin()->second;
|
2021-02-22 02:31:45 +00:00
|
|
|
unloadProjectFile( oldProject, false );
|
2020-08-27 00:46:20 +00:00
|
|
|
m_projects.erase( m_projects.begin() );
|
2021-08-20 00:50:41 +00:00
|
|
|
|
|
|
|
auto it = std::find_if( m_projects_list.begin(), m_projects_list.end(),
|
|
|
|
[&]( const std::unique_ptr<PROJECT>& ptr )
|
|
|
|
{
|
|
|
|
return ptr.get() == oldProject;
|
|
|
|
} );
|
|
|
|
|
|
|
|
wxASSERT( it != m_projects_list.end() );
|
|
|
|
m_projects_list.erase( it );
|
2020-08-27 00:46:20 +00:00
|
|
|
}
|
2020-05-25 17:38:09 +00:00
|
|
|
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Load project %s" ), fullPath );
|
2020-05-25 17:38:09 +00:00
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
std::unique_ptr<PROJECT> project = std::make_unique<PROJECT>();
|
|
|
|
project->setProjectFullName( fullPath );
|
|
|
|
|
|
|
|
bool success = loadProjectFile( *project );
|
|
|
|
|
2020-10-23 00:30:50 +00:00
|
|
|
if( success )
|
2021-04-02 23:29:47 +00:00
|
|
|
{
|
|
|
|
project->SetReadOnly( readOnly || project->GetProjectFile().IsReadOnly() );
|
|
|
|
|
|
|
|
if( lockFile )
|
|
|
|
m_project_lock.reset( lockFile.release() );
|
|
|
|
}
|
2020-10-23 00:30:50 +00:00
|
|
|
|
2020-10-06 02:13:06 +00:00
|
|
|
m_projects_list.push_back( std::move( project ) );
|
|
|
|
m_projects[fullPath] = m_projects_list.back().get();
|
2020-05-25 17:38:09 +00:00
|
|
|
|
2020-08-02 09:23:21 +00:00
|
|
|
wxString fn( path.GetName() );
|
2020-05-31 21:42:04 +00:00
|
|
|
|
2020-11-30 22:59:18 +00:00
|
|
|
PROJECT_LOCAL_SETTINGS* settings = new PROJECT_LOCAL_SETTINGS( m_projects[fullPath], fn );
|
|
|
|
|
|
|
|
if( aSetActive )
|
2021-08-04 15:44:12 +00:00
|
|
|
settings = RegisterSettings( settings );
|
2022-01-02 17:40:25 +00:00
|
|
|
else
|
|
|
|
settings->LoadFromFile( path.GetPath() );
|
2020-05-31 21:42:04 +00:00
|
|
|
|
|
|
|
m_projects[fullPath]->setLocalSettings( settings );
|
|
|
|
|
2020-11-30 22:59:18 +00:00
|
|
|
if( aSetActive && m_kiway )
|
2020-08-09 18:49:26 +00:00
|
|
|
m_kiway->ProjectChanged();
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
return success;
|
2020-05-25 17:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
bool SETTINGS_MANAGER::UnloadProject( PROJECT* aProject, bool aSave )
|
2020-05-25 17:38:09 +00:00
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
if( !aProject || !m_projects.count( aProject->GetProjectFullName() ) )
|
2020-05-25 17:38:09 +00:00
|
|
|
return false;
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
if( !unloadProjectFile( aProject, aSave ) )
|
2020-05-25 17:38:09 +00:00
|
|
|
return false;
|
|
|
|
|
2020-10-06 02:13:06 +00:00
|
|
|
wxString projectPath = aProject->GetProjectFullName();
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Unload project %s" ), projectPath );
|
2020-05-25 17:38:09 +00:00
|
|
|
|
2020-10-06 02:13:06 +00:00
|
|
|
PROJECT* toRemove = m_projects.at( projectPath );
|
|
|
|
auto it = std::find_if( m_projects_list.begin(), m_projects_list.end(),
|
|
|
|
[&]( const std::unique_ptr<PROJECT>& ptr )
|
|
|
|
{
|
|
|
|
return ptr.get() == toRemove;
|
|
|
|
} );
|
|
|
|
|
|
|
|
wxASSERT( it != m_projects_list.end() );
|
|
|
|
m_projects_list.erase( it );
|
|
|
|
|
|
|
|
m_projects.erase( projectPath );
|
2020-05-25 17:38:09 +00:00
|
|
|
|
2020-08-26 22:37:34 +00:00
|
|
|
// Immediately reload a null project; this is required until the rest of the application
|
|
|
|
// is refactored to not assume that Prj() always works
|
2020-08-30 16:27:20 +00:00
|
|
|
if( m_projects.empty() )
|
|
|
|
LoadProject( "" );
|
2020-08-26 22:37:34 +00:00
|
|
|
|
2020-08-09 18:49:26 +00:00
|
|
|
// Remove the reference in the environment to the previous project
|
|
|
|
wxSetEnv( PROJECT_VAR_NAME, "" );
|
|
|
|
|
2021-04-02 23:29:47 +00:00
|
|
|
// Release lock on the file, in case we had one
|
|
|
|
m_project_lock = nullptr;
|
|
|
|
|
2020-08-09 18:49:26 +00:00
|
|
|
if( m_kiway )
|
|
|
|
m_kiway->ProjectChanged();
|
|
|
|
|
2020-05-25 17:38:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PROJECT& SETTINGS_MANAGER::Prj() const
|
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
// No MDI yet: First project in the list is the active project
|
2022-02-03 20:57:26 +00:00
|
|
|
wxASSERT_MSG( m_projects_list.size(), wxT( "no project in list" ) );
|
2021-08-20 00:50:41 +00:00
|
|
|
return *m_projects_list.begin()->get();
|
2020-05-25 17:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-09 15:13:42 +00:00
|
|
|
bool SETTINGS_MANAGER::IsProjectOpen() const
|
|
|
|
{
|
|
|
|
return !m_projects.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
PROJECT* SETTINGS_MANAGER::GetProject( const wxString& aFullPath ) const
|
2020-05-25 20:41:24 +00:00
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
if( m_projects.count( aFullPath ) )
|
2020-10-06 02:13:06 +00:00
|
|
|
return m_projects.at( aFullPath );
|
2020-05-25 20:41:24 +00:00
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-08 20:00:29 +00:00
|
|
|
std::vector<wxString> SETTINGS_MANAGER::GetOpenProjects() const
|
|
|
|
{
|
|
|
|
std::vector<wxString> ret;
|
|
|
|
|
2020-10-06 02:13:06 +00:00
|
|
|
for( const std::pair<const wxString, PROJECT*>& pair : m_projects )
|
2020-08-08 20:00:29 +00:00
|
|
|
ret.emplace_back( pair.first );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-05 17:45:51 +00:00
|
|
|
bool SETTINGS_MANAGER::SaveProject( const wxString& aFullPath, PROJECT* aProject )
|
2020-05-31 21:42:04 +00:00
|
|
|
{
|
2022-03-05 17:45:51 +00:00
|
|
|
if( !aProject )
|
|
|
|
aProject = &Prj();
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
wxString path = aFullPath;
|
|
|
|
|
|
|
|
if( path.empty() )
|
2022-03-05 17:45:51 +00:00
|
|
|
path = aProject->GetProjectFullName();
|
2020-05-31 21:42:04 +00:00
|
|
|
|
2020-08-30 20:04:39 +00:00
|
|
|
// TODO: refactor for MDI
|
2022-03-05 17:45:51 +00:00
|
|
|
if( aProject->IsReadOnly() )
|
2020-08-30 20:04:39 +00:00
|
|
|
return false;
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
if( !m_project_files.count( path ) )
|
2020-05-25 20:41:24 +00:00
|
|
|
return false;
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
PROJECT_FILE* project = m_project_files.at( path );
|
2022-03-10 14:08:20 +00:00
|
|
|
wxString projectPath = aProject->GetProjectPath();
|
2020-05-31 21:42:04 +00:00
|
|
|
|
|
|
|
project->SaveToFile( projectPath );
|
2022-03-05 17:45:51 +00:00
|
|
|
aProject->GetLocalSettings().SaveToFile( projectPath );
|
2020-05-25 20:41:24 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-05 17:45:51 +00:00
|
|
|
void SETTINGS_MANAGER::SaveProjectAs( const wxString& aFullPath, PROJECT* aProject )
|
2020-10-06 02:13:06 +00:00
|
|
|
{
|
2022-03-05 17:45:51 +00:00
|
|
|
if( !aProject )
|
|
|
|
aProject = &Prj();
|
|
|
|
|
|
|
|
wxString oldName = aProject->GetProjectFullName();
|
2020-10-06 02:13:06 +00:00
|
|
|
|
2021-04-21 00:59:51 +00:00
|
|
|
if( aFullPath.IsSameAs( oldName ) )
|
|
|
|
{
|
2022-03-05 17:45:51 +00:00
|
|
|
SaveProject( aFullPath, aProject );
|
2021-04-21 00:59:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-06 02:13:06 +00:00
|
|
|
// Changing this will cause UnloadProject to not save over the "old" project when loading below
|
2022-03-05 17:45:51 +00:00
|
|
|
aProject->setProjectFullName( aFullPath );
|
2020-10-06 02:13:06 +00:00
|
|
|
|
|
|
|
wxFileName fn( aFullPath );
|
|
|
|
|
|
|
|
PROJECT_FILE* project = m_project_files.at( oldName );
|
2021-08-21 16:09:13 +00:00
|
|
|
|
|
|
|
// Ensure read-only flags are copied; this allows doing a "Save As" on a standalong board/sch
|
|
|
|
// without creating project files if the checkbox is turned off
|
2022-03-05 17:45:51 +00:00
|
|
|
project->SetReadOnly( aProject->IsReadOnly() );
|
|
|
|
aProject->GetLocalSettings().SetReadOnly( aProject->IsReadOnly() );
|
2021-08-21 16:09:13 +00:00
|
|
|
|
2020-10-06 02:13:06 +00:00
|
|
|
project->SetFilename( fn.GetName() );
|
|
|
|
project->SaveToFile( fn.GetPath() );
|
|
|
|
|
2022-03-05 17:45:51 +00:00
|
|
|
aProject->GetLocalSettings().SetFilename( fn.GetName() );
|
|
|
|
aProject->GetLocalSettings().SaveToFile( fn.GetPath() );
|
2020-10-06 02:13:06 +00:00
|
|
|
|
|
|
|
m_project_files[fn.GetFullPath()] = project;
|
|
|
|
m_project_files.erase( oldName );
|
|
|
|
|
|
|
|
m_projects[fn.GetFullPath()] = m_projects[oldName];
|
|
|
|
m_projects.erase( oldName );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-05 17:45:51 +00:00
|
|
|
void SETTINGS_MANAGER::SaveProjectCopy( const wxString& aFullPath, PROJECT* aProject )
|
2021-04-13 16:41:23 +00:00
|
|
|
{
|
2022-03-05 17:45:51 +00:00
|
|
|
if( !aProject )
|
|
|
|
aProject = &Prj();
|
|
|
|
|
|
|
|
PROJECT_FILE* project = m_project_files.at( aProject->GetProjectFullName() );
|
2021-04-13 16:41:23 +00:00
|
|
|
wxString oldName = project->GetFilename();
|
|
|
|
wxFileName fn( aFullPath );
|
|
|
|
|
2021-04-21 00:59:51 +00:00
|
|
|
bool readOnly = project->IsReadOnly();
|
|
|
|
project->SetReadOnly( false );
|
|
|
|
|
2021-04-13 16:41:23 +00:00
|
|
|
project->SetFilename( fn.GetName() );
|
|
|
|
project->SaveToFile( fn.GetPath() );
|
|
|
|
project->SetFilename( oldName );
|
|
|
|
|
2022-03-05 17:45:51 +00:00
|
|
|
PROJECT_LOCAL_SETTINGS& localSettings = aProject->GetLocalSettings();
|
|
|
|
|
|
|
|
localSettings.SetFilename( fn.GetName() );
|
|
|
|
localSettings.SaveToFile( fn.GetPath() );
|
|
|
|
localSettings.SetFilename( oldName );
|
2021-04-21 00:59:51 +00:00
|
|
|
|
|
|
|
project->SetReadOnly( readOnly );
|
2021-04-13 16:41:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-25 17:38:09 +00:00
|
|
|
bool SETTINGS_MANAGER::loadProjectFile( PROJECT& aProject )
|
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
wxFileName fullFn( aProject.GetProjectFullName() );
|
2020-08-02 09:23:21 +00:00
|
|
|
wxString fn( fullFn.GetName() );
|
2020-05-25 17:06:53 +00:00
|
|
|
|
2021-08-04 15:44:12 +00:00
|
|
|
PROJECT_FILE* file = RegisterSettings( new PROJECT_FILE( fn ), false );
|
2020-05-25 17:06:53 +00:00
|
|
|
|
|
|
|
m_project_files[aProject.GetProjectFullName()] = file;
|
|
|
|
|
2020-05-25 20:41:24 +00:00
|
|
|
aProject.setProjectFile( file );
|
2020-05-31 21:42:04 +00:00
|
|
|
file->SetProject( &aProject );
|
2020-05-25 20:41:24 +00:00
|
|
|
|
2020-08-02 09:23:21 +00:00
|
|
|
wxString path( fullFn.GetPath() );
|
2020-07-05 16:25:04 +00:00
|
|
|
|
|
|
|
return file->LoadFromFile( path );
|
2020-05-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
bool SETTINGS_MANAGER::unloadProjectFile( PROJECT* aProject, bool aSave )
|
2020-05-25 17:06:53 +00:00
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
if( !aProject )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
wxString name = aProject->GetProjectFullName();
|
2020-05-25 17:06:53 +00:00
|
|
|
|
|
|
|
if( !m_project_files.count( name ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
PROJECT_FILE* file = m_project_files[name];
|
|
|
|
|
|
|
|
auto it = std::find_if( m_settings.begin(), m_settings.end(),
|
|
|
|
[&file]( const std::unique_ptr<JSON_SETTINGS>& aPtr )
|
|
|
|
{
|
|
|
|
return aPtr.get() == file;
|
|
|
|
} );
|
|
|
|
|
|
|
|
if( it != m_settings.end() )
|
|
|
|
{
|
2020-08-02 09:23:21 +00:00
|
|
|
wxString projectPath = GetPathForSettingsFile( it->get() );
|
2020-05-31 21:42:04 +00:00
|
|
|
|
|
|
|
FlushAndRelease( &aProject->GetLocalSettings(), aSave );
|
|
|
|
|
|
|
|
if( aSave )
|
|
|
|
( *it )->SaveToFile( projectPath );
|
|
|
|
|
2020-05-25 17:06:53 +00:00
|
|
|
m_settings.erase( it );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_project_files.erase( name );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-07-03 13:40:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
wxString SETTINGS_MANAGER::GetProjectBackupsPath() const
|
|
|
|
{
|
|
|
|
return Prj().GetProjectPath() + Prj().GetProjectName() + PROJECT_BACKUPS_DIR_SUFFIX;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString SETTINGS_MANAGER::backupDateTimeFormat = wxT( "%Y-%m-%d_%H%M%S" );
|
|
|
|
|
|
|
|
|
|
|
|
bool SETTINGS_MANAGER::BackupProject( REPORTER& aReporter ) const
|
|
|
|
{
|
|
|
|
wxDateTime timestamp = wxDateTime::Now();
|
|
|
|
|
|
|
|
wxString fileName = wxString::Format( wxT( "%s-%s" ), Prj().GetProjectName(),
|
|
|
|
timestamp.Format( backupDateTimeFormat ) );
|
|
|
|
|
|
|
|
wxFileName target;
|
|
|
|
target.SetPath( GetProjectBackupsPath() );
|
|
|
|
target.SetName( fileName );
|
|
|
|
target.SetExt( ArchiveFileExtension );
|
|
|
|
|
|
|
|
wxDir dir( target.GetPath() );
|
|
|
|
|
|
|
|
if( !target.DirExists() && !wxMkdir( target.GetPath() ) )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Could not create project backup path %s" ), target.GetPath() );
|
2020-07-03 13:40:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !target.IsDirWritable() )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Backup directory %s is not writable" ), target.GetPath() );
|
2020-07-03 13:40:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Backing up project to %s" ), target.GetPath() );
|
2020-07-03 13:40:31 +00:00
|
|
|
|
|
|
|
PROJECT_ARCHIVER archiver;
|
|
|
|
|
|
|
|
return archiver.Archive( Prj().GetProjectPath(), target.GetFullPath(), aReporter );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class VECTOR_INSERT_TRAVERSER : public wxDirTraverser
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
VECTOR_INSERT_TRAVERSER( std::vector<wxString>& aVec,
|
|
|
|
std::function<bool( const wxString& )> aCond ) :
|
|
|
|
m_files( aVec ),
|
|
|
|
m_condition( aCond )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
wxDirTraverseResult OnFile( const wxString& aFile ) override
|
|
|
|
{
|
|
|
|
if( m_condition( aFile ) )
|
|
|
|
m_files.emplace_back( aFile );
|
|
|
|
|
|
|
|
return wxDIR_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxDirTraverseResult OnDir( const wxString& aDirName ) override
|
|
|
|
{
|
|
|
|
return wxDIR_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<wxString>& m_files;
|
|
|
|
|
|
|
|
std::function<bool( const wxString& )> m_condition;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool SETTINGS_MANAGER::TriggerBackupIfNeeded( REPORTER& aReporter ) const
|
|
|
|
{
|
|
|
|
COMMON_SETTINGS::AUTO_BACKUP settings = GetCommonSettings()->m_Backup;
|
|
|
|
|
|
|
|
if( !settings.enabled )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
wxString prefix = Prj().GetProjectName() + '-';
|
|
|
|
|
|
|
|
auto modTime =
|
|
|
|
[&prefix]( const wxString& aFile )
|
|
|
|
{
|
|
|
|
wxDateTime dt;
|
|
|
|
wxString fn( wxFileName( aFile ).GetName() );
|
|
|
|
fn.Replace( prefix, "" );
|
|
|
|
dt.ParseFormat( fn, backupDateTimeFormat );
|
|
|
|
return dt;
|
|
|
|
};
|
|
|
|
|
2021-02-04 00:17:10 +00:00
|
|
|
wxFileName projectPath( Prj().GetProjectPath() );
|
|
|
|
|
2021-11-08 23:42:09 +00:00
|
|
|
// Skip backup if project path isn't valid or writable
|
2021-02-04 00:17:10 +00:00
|
|
|
if( !projectPath.IsOk() || !projectPath.Exists() || !projectPath.IsDirWritable() )
|
2020-07-03 13:40:31 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
wxString backupPath = GetProjectBackupsPath();
|
|
|
|
|
|
|
|
if( !wxDirExists( backupPath ) )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Backup path %s doesn't exist, creating it" ), backupPath );
|
2020-07-03 13:40:31 +00:00
|
|
|
|
|
|
|
if( !wxMkdir( backupPath ) )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Could not create backups path! Skipping backup" ) );
|
2020-07-03 13:40:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wxDir dir( backupPath );
|
|
|
|
|
|
|
|
if( !dir.IsOpened() )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Could not open project backups path %s" ), dir.GetName() );
|
2020-07-03 13:40:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<wxString> files;
|
|
|
|
|
|
|
|
VECTOR_INSERT_TRAVERSER traverser( files,
|
|
|
|
[&modTime]( const wxString& aFile )
|
|
|
|
{
|
|
|
|
return modTime( aFile ).IsValid();
|
|
|
|
} );
|
|
|
|
|
|
|
|
dir.Traverse( traverser, wxT( "*.zip" ) );
|
|
|
|
|
|
|
|
// Sort newest-first
|
|
|
|
std::sort( files.begin(), files.end(),
|
|
|
|
[&]( const wxString& aFirst, const wxString& aSecond ) -> bool
|
|
|
|
{
|
|
|
|
wxDateTime first = modTime( aFirst );
|
|
|
|
wxDateTime second = modTime( aSecond );
|
|
|
|
|
|
|
|
return first.GetTicks() > second.GetTicks();
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Do we even need to back up?
|
|
|
|
if( !files.empty() )
|
|
|
|
{
|
|
|
|
wxDateTime lastTime = modTime( files[0] );
|
|
|
|
|
|
|
|
if( lastTime.IsValid() )
|
|
|
|
{
|
|
|
|
wxTimeSpan delta = wxDateTime::Now() - modTime( files[0] );
|
|
|
|
|
|
|
|
if( delta.IsShorterThan( wxTimeSpan::Seconds( settings.min_interval ) ) )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we know a backup is needed, apply the retention policy
|
|
|
|
|
|
|
|
// Step 1: if we're over the total file limit, remove the oldest
|
|
|
|
if( !files.empty() && settings.limit_total_files > 0 )
|
|
|
|
{
|
|
|
|
while( files.size() > static_cast<size_t>( settings.limit_total_files ) )
|
|
|
|
{
|
|
|
|
wxRemoveFile( files.back() );
|
|
|
|
files.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2: Stay under the total size limit
|
|
|
|
if( settings.limit_total_size > 0 )
|
|
|
|
{
|
|
|
|
wxULongLong totalSize = 0;
|
|
|
|
|
|
|
|
for( const wxString& file : files )
|
|
|
|
totalSize += wxFileName::GetSize( file );
|
|
|
|
|
|
|
|
while( !files.empty() && totalSize > static_cast<wxULongLong>( settings.limit_total_size ) )
|
|
|
|
{
|
|
|
|
totalSize -= wxFileName::GetSize( files.back() );
|
|
|
|
wxRemoveFile( files.back() );
|
|
|
|
files.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 3: Stay under the daily limit
|
|
|
|
if( settings.limit_daily_files > 0 && files.size() > 1 )
|
|
|
|
{
|
|
|
|
wxDateTime day = modTime( files[0] );
|
|
|
|
int num = 1;
|
|
|
|
|
|
|
|
wxASSERT( day.IsValid() );
|
|
|
|
|
|
|
|
std::vector<wxString> filesToDelete;
|
|
|
|
|
|
|
|
for( size_t i = 1; i < files.size(); i++ )
|
|
|
|
{
|
|
|
|
wxDateTime dt = modTime( files[i] );
|
|
|
|
|
|
|
|
if( dt.IsSameDate( day ) )
|
|
|
|
{
|
|
|
|
num++;
|
|
|
|
|
|
|
|
if( num > settings.limit_daily_files )
|
|
|
|
filesToDelete.emplace_back( files[i] );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
day = dt;
|
|
|
|
num = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( const wxString& file : filesToDelete )
|
|
|
|
wxRemoveFile( file );
|
|
|
|
}
|
|
|
|
|
|
|
|
return BackupProject( aReporter );
|
|
|
|
}
|