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>
|
2023-05-20 17:47:12 +00:00
|
|
|
* Copyright (C) 2020-2023 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/>.
|
|
|
|
*/
|
|
|
|
|
2020-03-03 00:18:52 +00:00
|
|
|
#include <algorithm>
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <utility>
|
2020-08-01 14:57:44 +00:00
|
|
|
#include <sstream>
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2020-10-24 01:38:50 +00:00
|
|
|
#include <locale_io.h>
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <gal/color4d.h>
|
|
|
|
#include <settings/json_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/nested_settings.h>
|
|
|
|
#include <settings/parameters.h>
|
2023-03-27 20:48:37 +00:00
|
|
|
#include <settings/bom_settings.h>
|
2023-08-30 15:21:56 +00:00
|
|
|
#include <settings/grid_settings.h>
|
2023-05-20 17:47:12 +00:00
|
|
|
#include <settings/aui_settings.h>
|
|
|
|
#include <wx/aui/framemanager.h>
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <wx/config.h>
|
|
|
|
#include <wx/debug.h>
|
2020-10-24 01:38:50 +00:00
|
|
|
#include <wx/fileconf.h>
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <wx/filename.h>
|
2023-05-20 17:47:12 +00:00
|
|
|
#include <wx/gdicmn.h>
|
2021-06-01 05:17:57 +00:00
|
|
|
#include <wx/log.h>
|
2021-10-08 20:33:07 +00:00
|
|
|
#include <wx/stdstream.h>
|
2020-12-12 15:44:31 +00:00
|
|
|
#include <wx/wfstream.h>
|
2020-12-12 15:00:49 +00:00
|
|
|
|
2021-06-04 03:52:50 +00:00
|
|
|
|
|
|
|
nlohmann::json::json_pointer JSON_SETTINGS_INTERNALS::PointerFromString( std::string aPath )
|
|
|
|
{
|
|
|
|
std::replace( aPath.begin(), aPath.end(), '.', '/' );
|
|
|
|
aPath.insert( 0, "/" );
|
|
|
|
|
|
|
|
nlohmann::json::json_pointer p;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
p = nlohmann::json::json_pointer( aPath );
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
wxASSERT_MSG( false, wxT( "Invalid pointer path in PointerFromString!" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-02 09:23:21 +00:00
|
|
|
JSON_SETTINGS::JSON_SETTINGS( const wxString& aFilename, SETTINGS_LOC aLocation,
|
2020-05-30 16:56:16 +00:00
|
|
|
int aSchemaVersion, bool aCreateIfMissing, bool aCreateIfDefault,
|
|
|
|
bool aWriteFile ) :
|
2020-05-08 18:10:47 +00:00
|
|
|
m_filename( aFilename ),
|
|
|
|
m_legacy_filename( "" ),
|
|
|
|
m_location( aLocation ),
|
|
|
|
m_createIfMissing( aCreateIfMissing ),
|
2020-05-30 16:56:16 +00:00
|
|
|
m_createIfDefault( aCreateIfDefault ),
|
2020-05-08 18:10:47 +00:00
|
|
|
m_writeFile( aWriteFile ),
|
2020-05-25 20:41:24 +00:00
|
|
|
m_deleteLegacyAfterMigration( true ),
|
2020-05-31 21:42:04 +00:00
|
|
|
m_resetParamsIfMissing( true ),
|
2020-05-08 18:10:47 +00:00
|
|
|
m_schemaVersion( aSchemaVersion ),
|
|
|
|
m_manager( nullptr )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2021-06-04 03:52:50 +00:00
|
|
|
m_internals = std::make_unique<JSON_SETTINGS_INTERNALS>();
|
|
|
|
|
2020-09-20 22:50:09 +00:00
|
|
|
try
|
|
|
|
{
|
2021-06-04 03:52:50 +00:00
|
|
|
m_internals->SetFromString( "meta.filename", GetFullFilename() );
|
2020-09-20 22:50:09 +00:00
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Error: Could not create filename field for %s" ),
|
2020-09-20 22:50:09 +00:00
|
|
|
GetFullFilename() );
|
|
|
|
}
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
m_params.emplace_back(
|
|
|
|
new PARAM<int>( "meta.version", &m_schemaVersion, m_schemaVersion, true ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JSON_SETTINGS::~JSON_SETTINGS()
|
|
|
|
{
|
2024-01-31 12:18:01 +00:00
|
|
|
for( PARAM_BASE* param: m_params )
|
2020-01-13 01:44:19 +00:00
|
|
|
delete param;
|
|
|
|
|
|
|
|
m_params.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
wxString JSON_SETTINGS::GetFullFilename() const
|
|
|
|
{
|
2023-11-01 03:38:44 +00:00
|
|
|
if( m_filename.BeforeLast( '.' ) == getFileExt() )
|
|
|
|
return m_filename;
|
|
|
|
|
2020-08-02 09:23:21 +00:00
|
|
|
return wxString( m_filename + "." + getFileExt() );
|
2020-05-31 21:42:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-04 03:52:50 +00:00
|
|
|
nlohmann::json& JSON_SETTINGS::At( const std::string& aPath )
|
|
|
|
{
|
|
|
|
return m_internals->At( aPath );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool JSON_SETTINGS::Contains( const std::string& aPath ) const
|
|
|
|
{
|
|
|
|
return m_internals->contains( JSON_SETTINGS_INTERNALS::PointerFromString( aPath ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JSON_SETTINGS_INTERNALS* JSON_SETTINGS::Internals()
|
|
|
|
{
|
|
|
|
return m_internals.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
void JSON_SETTINGS::Load()
|
|
|
|
{
|
2024-01-31 12:18:01 +00:00
|
|
|
for( PARAM_BASE* param : m_params )
|
2020-05-15 12:55:28 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
param->Load( this, m_resetParamsIfMissing );
|
2020-05-15 12:55:28 +00:00
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
2020-09-20 22:50:09 +00:00
|
|
|
// Skip unreadable parameters in file
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "param '%s' load err" ), param->GetJsonPath().c_str() );
|
2020-05-15 12:55:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-02 09:23:21 +00:00
|
|
|
bool JSON_SETTINGS::LoadFromFile( const wxString& aDirectory )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
|
|
|
// First, load all params to default values
|
2021-06-04 03:52:50 +00:00
|
|
|
m_internals->clear();
|
2020-01-13 01:44:19 +00:00
|
|
|
Load();
|
|
|
|
|
2020-05-25 17:06:53 +00:00
|
|
|
bool success = true;
|
|
|
|
bool migrated = false;
|
|
|
|
bool legacy_migrated = false;
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
LOCALE_IO locale;
|
|
|
|
|
2024-01-31 12:18:01 +00:00
|
|
|
auto migrateFromLegacy =
|
|
|
|
[&] ( wxFileName& aPath )
|
|
|
|
{
|
|
|
|
// Backup and restore during migration so that the original can be mutated if
|
|
|
|
// convenient
|
|
|
|
bool backed_up = false;
|
|
|
|
wxFileName temp;
|
2020-05-26 02:27:27 +00:00
|
|
|
|
2024-01-31 12:18:01 +00:00
|
|
|
if( aPath.IsDirWritable() )
|
|
|
|
{
|
|
|
|
temp.AssignTempFileName( aPath.GetFullPath() );
|
2020-07-17 02:56:02 +00:00
|
|
|
|
2024-01-31 12:18:01 +00:00
|
|
|
if( !wxCopyFile( aPath.GetFullPath(), temp.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings,
|
|
|
|
wxT( "%s: could not create temp file for migration" ),
|
|
|
|
GetFullFilename() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
backed_up = true;
|
|
|
|
}
|
|
|
|
}
|
2020-05-26 02:27:27 +00:00
|
|
|
|
2024-01-31 12:18:01 +00:00
|
|
|
// Silence popups if legacy file is read-only
|
|
|
|
wxLogNull doNotLog;
|
2021-02-04 00:11:02 +00:00
|
|
|
|
2024-01-31 12:18:01 +00:00
|
|
|
wxConfigBase::DontCreateOnDemand();
|
|
|
|
auto cfg = std::make_unique<wxFileConfig>( wxT( "" ), wxT( "" ),
|
|
|
|
aPath.GetFullPath() );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2024-01-31 12:18:01 +00:00
|
|
|
// If migrate fails or is not implemented, fall back to built-in defaults that
|
|
|
|
// were already loaded above
|
|
|
|
if( !MigrateFromLegacy( cfg.get() ) )
|
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
wxLogTrace( traceSettings,
|
|
|
|
wxT( "%s: migrated; not all settings were found in legacy file" ),
|
|
|
|
GetFullFilename() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
success = true;
|
|
|
|
wxLogTrace( traceSettings, wxT( "%s: migrated from legacy format" ),
|
|
|
|
GetFullFilename() );
|
|
|
|
}
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2024-01-31 12:18:01 +00:00
|
|
|
if( backed_up )
|
|
|
|
{
|
|
|
|
cfg.reset();
|
2020-08-03 06:44:34 +00:00
|
|
|
|
2024-01-31 12:18:01 +00:00
|
|
|
if( !wxCopyFile( temp.GetFullPath(), aPath.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings,
|
|
|
|
wxT( "migrate; copy temp file %s to %s failed" ),
|
|
|
|
temp.GetFullPath(),
|
|
|
|
aPath.GetFullPath() );
|
|
|
|
}
|
2020-08-03 06:44:34 +00:00
|
|
|
|
2024-01-31 12:18:01 +00:00
|
|
|
if( !wxRemoveFile( temp.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings,
|
|
|
|
wxT( "migrate; failed to remove temp file %s" ),
|
|
|
|
temp.GetFullPath() );
|
|
|
|
}
|
|
|
|
}
|
2020-05-26 02:27:27 +00:00
|
|
|
|
2024-01-31 12:18:01 +00:00
|
|
|
// Either way, we want to clean up the old file afterwards
|
|
|
|
legacy_migrated = true;
|
|
|
|
};
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2020-05-25 17:06:53 +00:00
|
|
|
wxFileName path;
|
|
|
|
|
|
|
|
if( aDirectory.empty() )
|
|
|
|
{
|
|
|
|
path.Assign( m_filename );
|
2020-05-25 20:41:24 +00:00
|
|
|
path.SetExt( getFileExt() );
|
2020-05-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-02 09:23:21 +00:00
|
|
|
wxString dir( aDirectory );
|
2020-08-01 13:24:57 +00:00
|
|
|
path.Assign( dir, m_filename, getFileExt() );
|
2020-05-25 17:06:53 +00:00
|
|
|
}
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
if( !path.Exists() )
|
|
|
|
{
|
|
|
|
// Case 1: legacy migration, no .json extension yet
|
2020-05-25 17:06:53 +00:00
|
|
|
path.SetExt( getLegacyFileExt() );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
if( path.Exists() )
|
|
|
|
{
|
|
|
|
migrateFromLegacy( path );
|
|
|
|
}
|
|
|
|
// Case 2: legacy filename is different from new one
|
|
|
|
else if( !m_legacy_filename.empty() )
|
|
|
|
{
|
|
|
|
path.SetName( m_legacy_filename );
|
|
|
|
|
|
|
|
if( path.Exists() )
|
|
|
|
migrateFromLegacy( path );
|
|
|
|
}
|
2020-05-25 17:06:53 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
}
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-23 00:30:50 +00:00
|
|
|
if( !path.IsFileWritable() )
|
|
|
|
m_writeFile = false;
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
try
|
|
|
|
{
|
2021-10-08 20:33:07 +00:00
|
|
|
wxFFileInputStream fp( path.GetFullPath(), wxT( "rt" ) );
|
|
|
|
wxStdInputStream fstream( fp );
|
2020-05-06 01:16:05 +00:00
|
|
|
|
2021-10-08 20:33:07 +00:00
|
|
|
if( fp.IsOk() )
|
2020-05-06 01:16:05 +00:00
|
|
|
{
|
2021-06-04 03:52:50 +00:00
|
|
|
*static_cast<nlohmann::json*>( m_internals.get() ) =
|
2021-10-08 20:33:07 +00:00
|
|
|
nlohmann::json::parse( fstream, nullptr,
|
2021-06-04 03:52:50 +00:00
|
|
|
/* allow_exceptions = */ true,
|
|
|
|
/* ignore_comments = */ true );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2023-02-17 04:15:44 +00:00
|
|
|
// Save whatever we loaded, before doing any migration etc
|
|
|
|
m_internals->m_original = *static_cast<nlohmann::json*>( m_internals.get() );
|
|
|
|
|
2020-10-23 00:30:50 +00:00
|
|
|
// If parse succeeds, check if schema migration is required
|
|
|
|
int filever = -1;
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2020-10-23 00:30:50 +00:00
|
|
|
try
|
2020-05-06 01:16:05 +00:00
|
|
|
{
|
2021-06-04 03:52:50 +00:00
|
|
|
filever = m_internals->Get<int>( "meta.version" );
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
2020-10-23 00:30:50 +00:00
|
|
|
catch( ... )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "%s: file version could not be read!" ),
|
2020-10-23 00:30:50 +00:00
|
|
|
GetFullFilename() );
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( filever >= 0 && filever < m_schemaVersion )
|
|
|
|
{
|
2023-12-19 19:29:17 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "%s: attempting migration from version "
|
2024-01-31 12:18:01 +00:00
|
|
|
"%d to %d" ),
|
2020-10-23 00:30:50 +00:00
|
|
|
GetFullFilename(), filever, m_schemaVersion );
|
|
|
|
|
|
|
|
if( Migrate() )
|
|
|
|
{
|
|
|
|
migrated = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-12-19 19:29:17 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "%s: migration failed!" ),
|
|
|
|
GetFullFilename() );
|
2020-10-23 00:30:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( filever > m_schemaVersion )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings,
|
2022-02-03 20:57:26 +00:00
|
|
|
wxT( "%s: warning: file version %d is newer than latest (%d)" ),
|
2020-10-23 00:30:50 +00:00
|
|
|
GetFullFilename(), filever, m_schemaVersion );
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-23 00:30:50 +00:00
|
|
|
else
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "%s exists but can't be opened for read" ),
|
2020-10-23 00:30:50 +00:00
|
|
|
GetFullFilename() );
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( nlohmann::json::parse_error& error )
|
|
|
|
{
|
2022-11-09 04:27:15 +00:00
|
|
|
success = false;
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Json parse error reading %s: %s" ),
|
2020-08-23 13:01:52 +00:00
|
|
|
path.GetFullPath(), error.what() );
|
2023-12-19 19:29:17 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Attempting migration in case file is in legacy "
|
|
|
|
"format" ) );
|
2020-01-13 01:44:19 +00:00
|
|
|
migrateFromLegacy( path );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have new data in the JSON structure, load the params again
|
|
|
|
Load();
|
|
|
|
|
|
|
|
// And finally load any nested settings
|
2024-01-31 12:18:01 +00:00
|
|
|
for( NESTED_SETTINGS* settings : m_nested_settings )
|
2020-01-13 01:44:19 +00:00
|
|
|
settings->LoadFromFile();
|
|
|
|
|
2023-12-19 19:29:17 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Loaded <%s> with schema %d" ), GetFullFilename(),
|
|
|
|
m_schemaVersion );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
// If we migrated, clean up the legacy file (with no extension)
|
2022-11-09 04:27:15 +00:00
|
|
|
if( m_writeFile && ( legacy_migrated || migrated ) )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2020-05-25 20:41:24 +00:00
|
|
|
if( legacy_migrated && m_deleteLegacyAfterMigration && !wxRemoveFile( path.GetFullPath() ) )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Warning: could not remove legacy file %s" ),
|
2020-08-23 13:01:52 +00:00
|
|
|
path.GetFullPath() );
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// And write-out immediately so that we don't lose data if the program later crashes.
|
2021-02-22 02:31:45 +00:00
|
|
|
if( m_deleteLegacyAfterMigration )
|
|
|
|
SaveToFile( aDirectory, true );
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
2020-05-25 17:06:53 +00:00
|
|
|
|
|
|
|
return success;
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-30 16:56:16 +00:00
|
|
|
bool JSON_SETTINGS::Store()
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2020-05-30 16:56:16 +00:00
|
|
|
bool modified = false;
|
|
|
|
|
2024-01-31 12:18:01 +00:00
|
|
|
for( PARAM_BASE* param : m_params )
|
2020-05-30 16:56:16 +00:00
|
|
|
{
|
|
|
|
modified |= !param->MatchesFile( this );
|
2020-01-13 01:44:19 +00:00
|
|
|
param->Store( this );
|
2020-05-30 16:56:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSON_SETTINGS::ResetToDefaults()
|
|
|
|
{
|
2024-01-31 12:18:01 +00:00
|
|
|
for( PARAM_BASE* param : m_params )
|
2020-01-13 01:44:19 +00:00
|
|
|
param->SetDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-02 09:23:21 +00:00
|
|
|
bool JSON_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
|
|
|
if( !m_writeFile )
|
2020-05-30 16:56:16 +00:00
|
|
|
return false;
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2020-05-25 17:38:09 +00:00
|
|
|
// Default PROJECT won't have a filename set
|
2020-08-02 09:23:21 +00:00
|
|
|
if( m_filename.IsEmpty() )
|
2020-05-25 17:38:09 +00:00
|
|
|
return false;
|
|
|
|
|
2020-05-25 17:06:53 +00:00
|
|
|
wxFileName path;
|
|
|
|
|
|
|
|
if( aDirectory.empty() )
|
|
|
|
{
|
|
|
|
path.Assign( m_filename );
|
2020-05-25 20:41:24 +00:00
|
|
|
path.SetExt( getFileExt() );
|
2020-05-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-02 09:23:21 +00:00
|
|
|
wxString dir( aDirectory );
|
2020-07-05 16:25:04 +00:00
|
|
|
path.Assign( dir, m_filename, getFileExt() );
|
2020-05-25 17:06:53 +00:00
|
|
|
}
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
if( !m_createIfMissing && !path.FileExists() )
|
2020-05-30 16:56:16 +00:00
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings,
|
2023-12-19 19:29:17 +00:00
|
|
|
wxT( "File for %s doesn't exist and m_createIfMissing == false; not saving" ),
|
|
|
|
GetFullFilename() );
|
2020-05-30 16:56:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-05 10:09:05 +00:00
|
|
|
// Ensure the path exists, and create it if not.
|
|
|
|
if( !path.DirExists() && !path.Mkdir() )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Warning: could not create path %s, can't save %s" ),
|
2020-09-05 10:09:05 +00:00
|
|
|
path.GetPath(), GetFullFilename() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-20 23:39:59 +00:00
|
|
|
if( ( path.FileExists() && !path.IsFileWritable() ) ||
|
|
|
|
( !path.FileExists() && !path.IsDirWritable() ) )
|
2020-07-17 02:56:02 +00:00
|
|
|
{
|
2023-12-19 19:29:17 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "File for %s is read-only; not saving" ),
|
|
|
|
GetFullFilename() );
|
2020-07-17 02:56:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-30 16:56:16 +00:00
|
|
|
bool modified = false;
|
|
|
|
|
2024-01-31 12:18:01 +00:00
|
|
|
for( NESTED_SETTINGS* settings : m_nested_settings )
|
2020-05-30 16:56:16 +00:00
|
|
|
modified |= settings->SaveToFile();
|
|
|
|
|
|
|
|
modified |= Store();
|
|
|
|
|
|
|
|
if( !modified && !aForce && path.FileExists() )
|
|
|
|
{
|
2023-12-19 19:29:17 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "%s contents not modified, skipping save" ),
|
|
|
|
GetFullFilename() );
|
2020-05-30 16:56:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if( !modified && !aForce && !m_createIfDefault )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings,
|
2023-12-19 19:29:17 +00:00
|
|
|
wxT( "%s contents still default and m_createIfDefault == false; not saving" ),
|
2020-05-31 21:42:04 +00:00
|
|
|
GetFullFilename() );
|
2020-05-30 16:56:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Saving %s" ), GetFullFilename() );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
LOCALE_IO dummy;
|
2020-08-01 13:24:57 +00:00
|
|
|
bool success = true;
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2023-02-17 04:15:44 +00:00
|
|
|
nlohmann::json toSave = m_internals->m_original;
|
|
|
|
|
2024-01-31 14:09:00 +00:00
|
|
|
|
|
|
|
for( PARAM_BASE* param : m_params )
|
|
|
|
{
|
|
|
|
if( PARAM_WXSTRING_MAP* stringMap = dynamic_cast<PARAM_WXSTRING_MAP*>( param ) )
|
|
|
|
{
|
|
|
|
if( stringMap->ClearUnknownKeys() )
|
|
|
|
toSave[ stringMap->GetJsonPath() ] = nlohmann::json( {} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-17 04:15:44 +00:00
|
|
|
toSave.update( m_internals->begin(), m_internals->end(), /* merge_objects = */ true );
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
try
|
|
|
|
{
|
2020-08-01 13:24:57 +00:00
|
|
|
std::stringstream buffer;
|
2023-02-17 04:15:44 +00:00
|
|
|
buffer << std::setw( 2 ) << toSave << std::endl;
|
2020-08-01 13:24:57 +00:00
|
|
|
|
2020-12-12 15:44:31 +00:00
|
|
|
wxFFileOutputStream fileStream( path.GetFullPath(), "wb" );
|
2020-08-23 13:01:52 +00:00
|
|
|
|
2020-12-12 15:44:31 +00:00
|
|
|
if( !fileStream.IsOk()
|
|
|
|
|| !fileStream.WriteAll( buffer.str().c_str(), buffer.str().size() ) )
|
2020-07-31 20:00:31 +00:00
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Warning: could not save %s" ), GetFullFilename() );
|
2020-08-01 13:24:57 +00:00
|
|
|
success = false;
|
2020-07-31 20:00:31 +00:00
|
|
|
}
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
2020-08-23 13:01:52 +00:00
|
|
|
catch( nlohmann::json::exception& error )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Catch error: could not save %s. Json error %s" ),
|
2020-08-23 13:01:52 +00:00
|
|
|
GetFullFilename(), error.what() );
|
|
|
|
success = false;
|
|
|
|
}
|
2020-01-13 01:44:19 +00:00
|
|
|
catch( ... )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Error: could not save %s." ) );
|
2020-08-01 13:24:57 +00:00
|
|
|
success = false;
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
2020-05-30 16:56:16 +00:00
|
|
|
|
2020-08-01 13:24:57 +00:00
|
|
|
return success;
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-06 19:30:53 +00:00
|
|
|
const std::string JSON_SETTINGS::FormatAsString()
|
2022-10-23 22:21:10 +00:00
|
|
|
{
|
2023-08-06 19:30:53 +00:00
|
|
|
Store();
|
|
|
|
|
2022-10-23 22:21:10 +00:00
|
|
|
LOCALE_IO dummy;
|
|
|
|
|
|
|
|
std::stringstream buffer;
|
|
|
|
buffer << std::setw( 2 ) << *m_internals << std::endl;
|
|
|
|
|
|
|
|
return buffer.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool JSON_SETTINGS::LoadFromRawFile( const wxString& aPath )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
wxFFileInputStream fp( aPath, wxT( "rt" ) );
|
|
|
|
wxStdInputStream fstream( fp );
|
|
|
|
|
|
|
|
if( fp.IsOk() )
|
|
|
|
{
|
|
|
|
*static_cast<nlohmann::json*>( m_internals.get() ) =
|
|
|
|
nlohmann::json::parse( fstream, nullptr,
|
|
|
|
/* allow_exceptions = */ true,
|
|
|
|
/* ignore_comments = */ true );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( nlohmann::json::parse_error& error )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings, wxT( "Json parse error reading %s: %s" ), aPath, error.what() );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have new data in the JSON structure, load the params again
|
|
|
|
Load();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-25 22:50:47 +00:00
|
|
|
std::optional<nlohmann::json> JSON_SETTINGS::GetJson( const std::string& aPath ) const
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2021-06-04 03:52:50 +00:00
|
|
|
nlohmann::json::json_pointer ptr = m_internals->PointerFromString( aPath );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2021-06-04 03:52:50 +00:00
|
|
|
if( m_internals->contains( ptr ) )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2020-03-07 21:06:33 +00:00
|
|
|
try
|
|
|
|
{
|
2022-08-25 22:50:47 +00:00
|
|
|
return std::optional<nlohmann::json>{ m_internals->at( ptr ) };
|
2020-03-07 21:06:33 +00:00
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
}
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 22:50:47 +00:00
|
|
|
return std::optional<nlohmann::json>{};
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-04 03:52:50 +00:00
|
|
|
template<typename ValueType>
|
2022-08-25 22:50:47 +00:00
|
|
|
std::optional<ValueType> JSON_SETTINGS::Get( const std::string& aPath ) const
|
2021-06-04 03:52:50 +00:00
|
|
|
{
|
2022-08-25 22:50:47 +00:00
|
|
|
if( std::optional<nlohmann::json> ret = GetJson( aPath ) )
|
2021-06-04 03:52:50 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return ret->get<ValueType>();
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 22:50:47 +00:00
|
|
|
return std::nullopt;
|
2021-06-04 03:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Instantiate all required templates here to allow reducing scope of json.hpp
|
2024-03-14 02:49:01 +00:00
|
|
|
template KICOMMON_API std::optional<bool> JSON_SETTINGS::Get<bool>( const std::string& aPath ) const;
|
|
|
|
template KICOMMON_API std::optional<double>
|
|
|
|
JSON_SETTINGS::Get<double>( const std::string& aPath ) const;
|
|
|
|
template KICOMMON_API std::optional<float>
|
|
|
|
JSON_SETTINGS::Get<float>( const std::string& aPath ) const;
|
|
|
|
template KICOMMON_API std::optional<int> JSON_SETTINGS::Get<int>( const std::string& aPath ) const;
|
|
|
|
template KICOMMON_API std::optional<unsigned int>
|
|
|
|
JSON_SETTINGS::Get<unsigned int>( const std::string& aPath ) const;
|
|
|
|
template KICOMMON_API std::optional<unsigned long long>
|
|
|
|
JSON_SETTINGS::Get<unsigned long long>( const std::string& aPath ) const;
|
|
|
|
template KICOMMON_API std::optional<std::string>
|
|
|
|
JSON_SETTINGS::Get<std::string>( const std::string& aPath ) const;
|
|
|
|
template KICOMMON_API std::optional<nlohmann::json>
|
|
|
|
JSON_SETTINGS::Get<nlohmann::json>( const std::string& aPath ) const;
|
|
|
|
template KICOMMON_API std::optional<KIGFX::COLOR4D>
|
|
|
|
JSON_SETTINGS::Get<KIGFX::COLOR4D>( const std::string& aPath ) const;
|
|
|
|
template KICOMMON_API std::optional<BOM_FIELD>
|
|
|
|
JSON_SETTINGS::Get<BOM_FIELD>( const std::string& aPath ) const;
|
|
|
|
template KICOMMON_API std::optional<BOM_PRESET>
|
|
|
|
JSON_SETTINGS::Get<BOM_PRESET>( const std::string& aPath ) const;
|
|
|
|
template KICOMMON_API std::optional<BOM_FMT_PRESET>
|
|
|
|
JSON_SETTINGS::Get<BOM_FMT_PRESET>( const std::string& aPath ) const;
|
|
|
|
template KICOMMON_API std::optional<GRID> JSON_SETTINGS::Get<GRID>( const std::string& aPath ) const;
|
|
|
|
template KICOMMON_API std::optional<wxPoint>
|
|
|
|
JSON_SETTINGS::Get<wxPoint>( const std::string& aPath ) const;
|
|
|
|
template KICOMMON_API std::optional<wxSize>
|
|
|
|
JSON_SETTINGS::Get<wxSize>( const std::string& aPath ) const;
|
|
|
|
template KICOMMON_API std::optional<wxRect>
|
|
|
|
JSON_SETTINGS::Get<wxRect>( const std::string& aPath ) const;
|
|
|
|
template KICOMMON_API std::optional<wxAuiPaneInfo>
|
|
|
|
JSON_SETTINGS::Get<wxAuiPaneInfo>( const std::string& aPath ) const;
|
2021-06-04 03:52:50 +00:00
|
|
|
|
|
|
|
template<typename ValueType>
|
|
|
|
void JSON_SETTINGS::Set( const std::string& aPath, ValueType aVal )
|
|
|
|
{
|
2023-12-18 17:01:27 +00:00
|
|
|
m_internals->SetFromString( aPath, std::move( aVal ) );
|
2021-06-04 03:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Instantiate all required templates here to allow reducing scope of json.hpp
|
2024-03-14 02:49:01 +00:00
|
|
|
template KICOMMON_API void JSON_SETTINGS::Set<bool>( const std::string& aPath, bool aValue );
|
|
|
|
template KICOMMON_API void JSON_SETTINGS::Set<double>( const std::string& aPath, double aValue );
|
|
|
|
template KICOMMON_API void JSON_SETTINGS::Set<float>( const std::string& aPath, float aValue );
|
|
|
|
template KICOMMON_API void JSON_SETTINGS::Set<int>( const std::string& aPath, int aValue );
|
|
|
|
template KICOMMON_API void JSON_SETTINGS::Set<unsigned int>( const std::string& aPath,
|
|
|
|
unsigned int aValue );
|
|
|
|
template KICOMMON_API void JSON_SETTINGS::Set<unsigned long long>( const std::string& aPath,
|
|
|
|
unsigned long long aValue );
|
|
|
|
template KICOMMON_API void JSON_SETTINGS::Set<const char*>( const std::string& aPath,
|
|
|
|
const char* aValue );
|
|
|
|
template KICOMMON_API void JSON_SETTINGS::Set<std::string>( const std::string& aPath,
|
|
|
|
std::string aValue );
|
|
|
|
template KICOMMON_API void JSON_SETTINGS::Set<nlohmann::json>( const std::string& aPath,
|
|
|
|
nlohmann::json aValue );
|
|
|
|
template KICOMMON_API void JSON_SETTINGS::Set<KIGFX::COLOR4D>( const std::string& aPath,
|
|
|
|
KIGFX::COLOR4D aValue );
|
|
|
|
template KICOMMON_API void JSON_SETTINGS::Set<BOM_FIELD>( const std::string& aPath,
|
|
|
|
BOM_FIELD aValue );
|
|
|
|
template KICOMMON_API void JSON_SETTINGS::Set<BOM_PRESET>( const std::string& aPath,
|
|
|
|
BOM_PRESET aValue );
|
|
|
|
template KICOMMON_API void JSON_SETTINGS::Set<BOM_FMT_PRESET>( const std::string& aPath,
|
|
|
|
BOM_FMT_PRESET aValue );
|
|
|
|
template KICOMMON_API void JSON_SETTINGS::Set<GRID>( const std::string& aPath, GRID aValue );
|
|
|
|
template KICOMMON_API void JSON_SETTINGS::Set<wxPoint>( const std::string& aPath, wxPoint aValue );
|
|
|
|
template KICOMMON_API void JSON_SETTINGS::Set<wxSize>( const std::string& aPath, wxSize aValue );
|
|
|
|
template KICOMMON_API void JSON_SETTINGS::Set<wxRect>( const std::string& aPath, wxRect aValue );
|
|
|
|
template KICOMMON_API void JSON_SETTINGS::Set<wxAuiPaneInfo>( const std::string& aPath,
|
|
|
|
wxAuiPaneInfo aValue );
|
2021-06-04 03:52:50 +00:00
|
|
|
|
|
|
|
|
2020-10-06 03:21:57 +00:00
|
|
|
void JSON_SETTINGS::registerMigration( int aOldSchemaVersion, int aNewSchemaVersion,
|
|
|
|
std::function<bool()> aMigrator )
|
|
|
|
{
|
|
|
|
wxASSERT( aNewSchemaVersion > aOldSchemaVersion );
|
|
|
|
wxASSERT( aNewSchemaVersion <= m_schemaVersion );
|
|
|
|
m_migrators[aOldSchemaVersion] = std::make_pair( aNewSchemaVersion, aMigrator );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
bool JSON_SETTINGS::Migrate()
|
|
|
|
{
|
2021-06-04 03:52:50 +00:00
|
|
|
int filever = m_internals->Get<int>( "meta.version" );
|
2020-10-06 03:21:57 +00:00
|
|
|
|
|
|
|
while( filever < m_schemaVersion )
|
|
|
|
{
|
2024-05-05 18:08:55 +00:00
|
|
|
wxASSERT( m_migrators.count( filever ) > 0 );
|
|
|
|
|
2020-10-06 03:21:57 +00:00
|
|
|
if( !m_migrators.count( filever ) )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Migrator missing for %s version %d!" ),
|
2020-10-06 03:21:57 +00:00
|
|
|
typeid( *this ).name(), filever );
|
2024-05-05 18:08:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<int, std::function<bool()>> pair = m_migrators.at( filever );
|
|
|
|
|
|
|
|
if( pair.second() )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings, wxT( "Migrated %s from %d to %d" ), typeid( *this ).name(),
|
|
|
|
filever, pair.first );
|
|
|
|
filever = pair.first;
|
|
|
|
m_internals->At( "meta.version" ) = filever;
|
2020-10-06 03:21:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-05-05 18:08:55 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Migration failed for %s from %d to %d" ),
|
|
|
|
typeid( *this ).name(), filever, pair.first );
|
|
|
|
return false;
|
2020-10-06 03:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool JSON_SETTINGS::MigrateFromLegacy( wxConfigBase* aLegacyConfig )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings,
|
2022-02-03 20:57:26 +00:00
|
|
|
wxT( "MigrateFromLegacy() not implemented for %s" ), typeid( *this ).name() );
|
2020-01-13 01:44:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-05 10:09:05 +00:00
|
|
|
bool JSON_SETTINGS::SetIfPresent( const nlohmann::json& aObj, const std::string& aPath,
|
2020-08-24 02:01:14 +00:00
|
|
|
wxString& aTarget )
|
|
|
|
{
|
2021-06-04 03:52:50 +00:00
|
|
|
nlohmann::json::json_pointer ptr = JSON_SETTINGS_INTERNALS::PointerFromString( aPath );
|
2020-08-24 02:01:14 +00:00
|
|
|
|
|
|
|
if( aObj.contains( ptr ) && aObj.at( ptr ).is_string() )
|
|
|
|
{
|
|
|
|
aTarget = aObj.at( ptr ).get<wxString>();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-05 10:09:05 +00:00
|
|
|
bool JSON_SETTINGS::SetIfPresent( const nlohmann::json& aObj, const std::string& aPath,
|
2020-08-24 02:01:14 +00:00
|
|
|
bool& aTarget )
|
|
|
|
{
|
2021-06-04 03:52:50 +00:00
|
|
|
nlohmann::json::json_pointer ptr = JSON_SETTINGS_INTERNALS::PointerFromString( aPath );
|
2020-08-24 02:01:14 +00:00
|
|
|
|
|
|
|
if( aObj.contains( ptr ) && aObj.at( ptr ).is_boolean() )
|
|
|
|
{
|
|
|
|
aTarget = aObj.at( ptr ).get<bool>();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-05 10:09:05 +00:00
|
|
|
bool JSON_SETTINGS::SetIfPresent( const nlohmann::json& aObj, const std::string& aPath,
|
2020-08-24 02:01:14 +00:00
|
|
|
int& aTarget )
|
|
|
|
{
|
2021-06-04 03:52:50 +00:00
|
|
|
nlohmann::json::json_pointer ptr = JSON_SETTINGS_INTERNALS::PointerFromString( aPath );
|
2020-08-24 02:01:14 +00:00
|
|
|
|
|
|
|
if( aObj.contains( ptr ) && aObj.at( ptr ).is_number_integer() )
|
|
|
|
{
|
|
|
|
aTarget = aObj.at( ptr ).get<int>();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-05 10:09:05 +00:00
|
|
|
bool JSON_SETTINGS::SetIfPresent( const nlohmann::json& aObj, const std::string& aPath,
|
2020-08-24 02:01:14 +00:00
|
|
|
unsigned int& aTarget )
|
|
|
|
{
|
2021-06-04 03:52:50 +00:00
|
|
|
nlohmann::json::json_pointer ptr = JSON_SETTINGS_INTERNALS::PointerFromString( aPath );
|
2020-08-24 02:01:14 +00:00
|
|
|
|
|
|
|
if( aObj.contains( ptr ) && aObj.at( ptr ).is_number_unsigned() )
|
|
|
|
{
|
|
|
|
aTarget = aObj.at( ptr ).get<unsigned int>();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
template<typename ValueType>
|
|
|
|
bool JSON_SETTINGS::fromLegacy( wxConfigBase* aConfig, const std::string& aKey,
|
|
|
|
const std::string& aDest )
|
|
|
|
{
|
|
|
|
ValueType val;
|
|
|
|
|
|
|
|
if( aConfig->Read( aKey, &val ) )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-06-04 03:52:50 +00:00
|
|
|
( *m_internals )[aDest] = val;
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
wxASSERT_MSG( false, wxT( "Could not write value in fromLegacy!" ) );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Explicitly declare these because we only support a few types anyway, and it means we can keep
|
|
|
|
// wxConfig detail out of the header file
|
2024-03-14 02:49:01 +00:00
|
|
|
template KICOMMON_API bool JSON_SETTINGS::fromLegacy<int>( wxConfigBase*, const std::string&,
|
2020-01-13 01:44:19 +00:00
|
|
|
const std::string& );
|
|
|
|
|
2024-03-14 02:49:01 +00:00
|
|
|
template KICOMMON_API bool JSON_SETTINGS::fromLegacy<double>( wxConfigBase*, const std::string&,
|
2022-08-14 11:03:18 +00:00
|
|
|
const std::string& );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2024-03-14 02:49:01 +00:00
|
|
|
template KICOMMON_API bool JSON_SETTINGS::fromLegacy<bool>( wxConfigBase*, const std::string&,
|
2020-01-13 01:44:19 +00:00
|
|
|
const std::string& );
|
|
|
|
|
|
|
|
|
|
|
|
bool JSON_SETTINGS::fromLegacyString( wxConfigBase* aConfig, const std::string& aKey,
|
|
|
|
const std::string& aDest )
|
|
|
|
{
|
|
|
|
wxString str;
|
|
|
|
|
|
|
|
if( aConfig->Read( aKey, &str ) )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-06-04 03:52:50 +00:00
|
|
|
( *m_internals )[aDest] = str.ToUTF8();
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
wxASSERT_MSG( false, wxT( "Could not write value in fromLegacyString!" ) );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool JSON_SETTINGS::fromLegacyColor( wxConfigBase* aConfig, const std::string& aKey,
|
|
|
|
const std::string& aDest )
|
|
|
|
{
|
|
|
|
wxString str;
|
|
|
|
|
|
|
|
if( aConfig->Read( aKey, &str ) )
|
|
|
|
{
|
|
|
|
KIGFX::COLOR4D color;
|
|
|
|
color.SetFromWxString( str );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
nlohmann::json js = nlohmann::json::array( { color.r, color.g, color.b, color.a } );
|
2023-12-18 17:01:27 +00:00
|
|
|
( *m_internals )[aDest] = std::move( js );
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
wxASSERT_MSG( false, wxT( "Could not write value in fromLegacyColor!" ) );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSON_SETTINGS::AddNestedSettings( NESTED_SETTINGS* aSettings )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "AddNestedSettings %s" ), aSettings->GetFilename() );
|
2020-01-13 01:44:19 +00:00
|
|
|
m_nested_settings.push_back( aSettings );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-28 03:53:00 +00:00
|
|
|
void JSON_SETTINGS::ReleaseNestedSettings( NESTED_SETTINGS* aSettings )
|
|
|
|
{
|
2022-08-14 11:03:18 +00:00
|
|
|
if( !aSettings || !m_manager )
|
2020-05-31 21:42:04 +00:00
|
|
|
return;
|
|
|
|
|
2020-02-28 03:53:00 +00:00
|
|
|
auto it = std::find_if( m_nested_settings.begin(), m_nested_settings.end(),
|
2024-01-31 12:18:01 +00:00
|
|
|
[&aSettings]( const JSON_SETTINGS* aPtr )
|
|
|
|
{
|
|
|
|
return aPtr == aSettings;
|
2020-02-28 03:53:00 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
if( it != m_nested_settings.end() )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Flush and release %s" ), ( *it )->GetFilename() );
|
2020-02-28 03:53:00 +00:00
|
|
|
( *it )->SaveToFile();
|
|
|
|
m_nested_settings.erase( it );
|
|
|
|
}
|
2020-05-31 21:42:04 +00:00
|
|
|
|
|
|
|
aSettings->SetParent( nullptr );
|
2020-02-28 03:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
// Specializations to allow conversion between wxString and std::string via JSON_SETTINGS API
|
2022-08-25 22:50:47 +00:00
|
|
|
template<> std::optional<wxString> JSON_SETTINGS::Get( const std::string& aPath ) const
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2022-08-25 22:50:47 +00:00
|
|
|
if( std::optional<nlohmann::json> opt_json = GetJson( aPath ) )
|
2020-03-07 21:06:33 +00:00
|
|
|
return wxString( opt_json->get<std::string>().c_str(), wxConvUTF8 );
|
|
|
|
|
2022-08-25 22:50:47 +00:00
|
|
|
return std::nullopt;
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
template<> void JSON_SETTINGS::Set<wxString>( const std::string& aPath, wxString aVal )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2021-06-04 03:52:50 +00:00
|
|
|
( *m_internals )[aPath] = aVal.ToUTF8();
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-10 02:24:31 +00:00
|
|
|
template<typename ResultType>
|
|
|
|
ResultType JSON_SETTINGS::fetchOrDefault( const nlohmann::json& aJson, const std::string& aKey,
|
|
|
|
ResultType aDefault )
|
|
|
|
{
|
2023-12-18 17:19:15 +00:00
|
|
|
ResultType ret = std::move( aDefault );
|
2022-11-10 02:24:31 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if( aJson.contains( aKey ) )
|
|
|
|
ret = aJson.at( aKey ).get<ResultType>();
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-14 02:49:01 +00:00
|
|
|
template KICOMMON_API std::string JSON_SETTINGS::fetchOrDefault( const nlohmann::json& aJson,
|
2022-11-10 02:24:31 +00:00
|
|
|
const std::string& aKey, std::string aDefault );
|
|
|
|
|
2023-12-19 19:29:17 +00:00
|
|
|
|
2024-03-14 02:49:01 +00:00
|
|
|
template KICOMMON_API bool JSON_SETTINGS::fetchOrDefault( const nlohmann::json& aJson,
|
|
|
|
const std::string& aKey,
|
2022-11-10 02:24:31 +00:00
|
|
|
bool aDefault );
|