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>
|
|
|
|
* Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <gal/color4d.h>
|
|
|
|
#include <settings/json_settings.h>
|
|
|
|
#include <settings/nested_settings.h>
|
|
|
|
#include <settings/parameters.h>
|
|
|
|
#include <wx/config.h>
|
|
|
|
#include <wx/debug.h>
|
|
|
|
#include <wx/filename.h>
|
|
|
|
|
|
|
|
extern const char* traceSettings;
|
|
|
|
|
|
|
|
|
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 ) :
|
|
|
|
nlohmann::json(),
|
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
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
( *this )[PointerFromString( "meta.filename" )] = 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()
|
|
|
|
{
|
|
|
|
for( auto param: m_params )
|
|
|
|
delete param;
|
|
|
|
|
|
|
|
m_params.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
wxString JSON_SETTINGS::GetFullFilename() const
|
|
|
|
{
|
2020-08-02 09:23:21 +00:00
|
|
|
return wxString( m_filename + "." + getFileExt() );
|
2020-05-31 21:42:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
void JSON_SETTINGS::Load()
|
|
|
|
{
|
|
|
|
for( auto 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( ... )
|
|
|
|
{
|
|
|
|
// Skip unreadable parameters in file:
|
|
|
|
#ifdef DEBUG
|
|
|
|
wxLogMessage( wxString::Format( "param '%s' load err", param->GetJsonPath().c_str() ) );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
clear();
|
|
|
|
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;
|
|
|
|
|
|
|
|
auto migrateFromLegacy = [&] ( wxFileName& aPath ) {
|
2020-05-26 02:27:27 +00:00
|
|
|
// Backup and restore during migration so that the original can be mutated if convenient
|
2020-07-17 02:56:02 +00:00
|
|
|
bool backed_up = false;
|
2020-05-26 02:27:27 +00:00
|
|
|
wxFileName temp;
|
|
|
|
|
2020-07-17 02:56:02 +00:00
|
|
|
if( aPath.IsDirWritable() )
|
2020-05-26 02:27:27 +00:00
|
|
|
{
|
2020-07-17 02:56:02 +00:00
|
|
|
temp.AssignTempFileName( aPath.GetFullPath() );
|
|
|
|
|
|
|
|
if( !wxCopyFile( aPath.GetFullPath(), temp.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings, "%s: could not create temp file for migration",
|
2020-05-31 21:42:04 +00:00
|
|
|
GetFullFilename() );
|
2020-07-17 02:56:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
backed_up = true;
|
2020-05-26 02:27:27 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
wxConfigBase::DontCreateOnDemand();
|
|
|
|
auto cfg = std::make_unique<wxFileConfig>( wxT( "" ), wxT( "" ), aPath.GetFullPath() );
|
|
|
|
|
|
|
|
// If migrate fails or is not implemented, fall back to built-in defaults that were
|
|
|
|
// already loaded above
|
|
|
|
if( !MigrateFromLegacy( cfg.get() ) )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings,
|
2020-05-31 21:42:04 +00:00
|
|
|
"%s: migrated; not all settings were found in legacy file",
|
|
|
|
GetFullFilename() );
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
wxLogTrace( traceSettings, "%s: migrated from legacy format", GetFullFilename() );
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 02:27:27 +00:00
|
|
|
if( backed_up )
|
|
|
|
{
|
|
|
|
cfg.reset();
|
2020-08-03 06:44:34 +00:00
|
|
|
|
|
|
|
if( !wxCopyFile( temp.GetFullPath(), aPath.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings,
|
|
|
|
"migrate; copy temp file %s to %s failed",
|
|
|
|
temp.GetFullPath(), aPath.GetFullPath() );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !wxRemoveFile( temp.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings,
|
|
|
|
"migrate; failed to remove temp file %s",
|
|
|
|
temp.GetFullPath() );
|
|
|
|
}
|
|
|
|
}
|
2020-05-26 02:27:27 +00:00
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
// Either way, we want to clean up the old file afterwards
|
2020-05-06 01:16:05 +00:00
|
|
|
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
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-07-31 20:00:31 +00:00
|
|
|
FILE* fp = wxFopen( path.GetFullPath(), wxT( "rt" ) );
|
2020-08-01 17:13:36 +00:00
|
|
|
*static_cast<nlohmann::json*>( this ) = nlohmann::json::parse( fp, nullptr,
|
|
|
|
/* allow_exceptions = */ true,
|
|
|
|
/* ignore_comments = */ true );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
// If parse succeeds, check if schema migration is required
|
2020-05-06 01:16:05 +00:00
|
|
|
int filever = -1;
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
try
|
|
|
|
{
|
2020-05-06 01:16:05 +00:00
|
|
|
filever = at( PointerFromString( "meta.version" ) ).get<int>();
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
wxLogTrace(
|
|
|
|
traceSettings, "%s: file version could not be read!", GetFullFilename() );
|
2020-05-25 17:06:53 +00:00
|
|
|
success = false;
|
2020-05-06 01:16:05 +00:00
|
|
|
}
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2020-05-06 01:16:05 +00:00
|
|
|
if( filever >= 0 && filever < m_schemaVersion )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings, "%s: attempting migration from version %d to %d",
|
2020-05-31 21:42:04 +00:00
|
|
|
GetFullFilename(), filever, m_schemaVersion );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2020-05-06 01:16:05 +00:00
|
|
|
if( Migrate() )
|
|
|
|
{
|
|
|
|
migrated = true;
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
2020-05-06 01:16:05 +00:00
|
|
|
else
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
wxLogTrace( traceSettings, "%s: migration failed!", GetFullFilename() );
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 01:16:05 +00:00
|
|
|
else if( filever > m_schemaVersion )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2020-05-06 01:16:05 +00:00
|
|
|
wxLogTrace( traceSettings,
|
2020-05-31 21:42:04 +00:00
|
|
|
"%s: warning: file version %d is newer than latest (%d)", GetFullFilename(),
|
2020-05-06 01:16:05 +00:00
|
|
|
filever, m_schemaVersion );
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( nlohmann::json::parse_error& error )
|
|
|
|
{
|
|
|
|
wxLogTrace(
|
|
|
|
traceSettings, "Parse error reading %s: %s", path.GetFullPath(), error.what() );
|
|
|
|
wxLogTrace( traceSettings, "Attempting migration in case file is in legacy format" );
|
|
|
|
migrateFromLegacy( path );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have new data in the JSON structure, load the params again
|
|
|
|
Load();
|
|
|
|
|
|
|
|
// And finally load any nested settings
|
|
|
|
for( auto settings : m_nested_settings )
|
|
|
|
settings->LoadFromFile();
|
|
|
|
|
2020-08-02 09:23:21 +00:00
|
|
|
wxLogTrace( traceSettings, "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)
|
2020-05-06 01:16:05 +00:00
|
|
|
if( 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
|
|
|
{
|
|
|
|
wxLogTrace(
|
|
|
|
traceSettings, "Warning: could not remove legacy file %s", path.GetFullPath() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// And write-out immediately so that we don't lose data if the program later crashes.
|
|
|
|
SaveToFile( aDirectory );
|
|
|
|
}
|
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;
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
for( auto 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()
|
|
|
|
{
|
|
|
|
for( auto param : m_params )
|
|
|
|
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,
|
|
|
|
"File for %s doesn't exist and m_createIfMissing == false; not saving",
|
2020-05-31 21:42:04 +00:00
|
|
|
GetFullFilename() );
|
2020-05-30 16:56:16 +00:00
|
|
|
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
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings, "File for %s is read-only; not saving", GetFullFilename() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-30 16:56:16 +00:00
|
|
|
bool modified = false;
|
|
|
|
|
|
|
|
for( auto settings : m_nested_settings )
|
|
|
|
modified |= settings->SaveToFile();
|
|
|
|
|
|
|
|
modified |= Store();
|
|
|
|
|
|
|
|
if( !modified && !aForce && path.FileExists() )
|
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
wxLogTrace( traceSettings, "%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,
|
|
|
|
"%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
|
|
|
|
|
|
|
if( !path.DirExists() && !path.Mkdir() )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings, "Warning: could not create path %s, can't save %s",
|
2020-05-31 21:42:04 +00:00
|
|
|
path.GetPath(), GetFullFilename() );
|
2020-05-30 16:56:16 +00:00
|
|
|
return false;
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
wxLogTrace( traceSettings, "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
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-07-31 20:00:31 +00:00
|
|
|
wxFile file( path.GetFullPath(), wxFile::write );
|
|
|
|
|
2020-08-01 13:24:57 +00:00
|
|
|
std::stringstream buffer;
|
|
|
|
buffer << std::setw( 2 ) << *this << std::endl;
|
|
|
|
|
|
|
|
if( !file.IsOpened() || !file.Write( buffer.str().c_str(), buffer.str().size() ) )
|
2020-07-31 20:00:31 +00:00
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings, "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
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
OPT<nlohmann::json> JSON_SETTINGS::GetJson( const std::string& aPath ) const
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
nlohmann::json::json_pointer ptr = PointerFromString( aPath );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2020-03-07 21:06:33 +00:00
|
|
|
if( this->contains( ptr ) )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2020-03-07 21:06:33 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return OPT<nlohmann::json>{ this->at( ptr ) };
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
}
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 21:06:33 +00:00
|
|
|
return OPT<nlohmann::json>{};
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool JSON_SETTINGS::Migrate()
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings, "Migrate() not implemented for %s", typeid( *this ).name() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool JSON_SETTINGS::MigrateFromLegacy( wxConfigBase* aLegacyConfig )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings,
|
|
|
|
"MigrateFromLegacy() not implemented for %s", typeid( *this ).name() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nlohmann::json::json_pointer JSON_SETTINGS::PointerFromString( std::string aPath )
|
|
|
|
{
|
2020-03-03 00:18:52 +00:00
|
|
|
std::replace( aPath.begin(), aPath.end(), '.', '/' );
|
2020-01-13 01:44:19 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
( *this )[PointerFromString( aDest )] = val;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
template bool JSON_SETTINGS::fromLegacy<int>( wxConfigBase*, const std::string&,
|
|
|
|
const std::string& );
|
|
|
|
|
|
|
|
template bool JSON_SETTINGS::fromLegacy<double>( wxConfigBase*, const std::string&,
|
|
|
|
const std::string& );
|
|
|
|
|
|
|
|
template bool JSON_SETTINGS::fromLegacy<bool>( wxConfigBase*, const std::string&,
|
|
|
|
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
|
|
|
|
{
|
|
|
|
( *this )[PointerFromString( aDest )] = str.ToUTF8();
|
|
|
|
}
|
|
|
|
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 } );
|
|
|
|
( *this )[PointerFromString( aDest )] = js;
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
wxASSERT_MSG( false, wxT( "Could not write value in fromLegacyColor!" ) );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSON_SETTINGS::AddNestedSettings( NESTED_SETTINGS* aSettings )
|
|
|
|
{
|
2020-02-28 03:53:00 +00:00
|
|
|
wxLogTrace( traceSettings, "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 )
|
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
if( !aSettings )
|
|
|
|
return;
|
|
|
|
|
2020-02-28 03:53:00 +00:00
|
|
|
auto it = std::find_if( m_nested_settings.begin(), m_nested_settings.end(),
|
|
|
|
[&aSettings]( const JSON_SETTINGS* aPtr ) {
|
|
|
|
return aPtr == aSettings;
|
|
|
|
} );
|
|
|
|
|
|
|
|
if( it != m_nested_settings.end() )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings, "Flush and release %s", ( *it )->GetFilename() );
|
|
|
|
( *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
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
template<> OPT<wxString> JSON_SETTINGS::Get( const std::string& aPath ) const
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
if( OPT<nlohmann::json> opt_json = GetJson( aPath ) )
|
2020-03-07 21:06:33 +00:00
|
|
|
return wxString( opt_json->get<std::string>().c_str(), wxConvUTF8 );
|
|
|
|
|
|
|
|
return 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
|
|
|
{
|
|
|
|
( *this )[PointerFromString( std::move( aPath ) ) ] = aVal.ToUTF8();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Specializations to allow directly reading/writing wxStrings from JSON
|
|
|
|
|
|
|
|
void to_json( nlohmann::json& aJson, const wxString& aString )
|
|
|
|
{
|
|
|
|
aJson = aString.ToUTF8();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void from_json( const nlohmann::json& aJson, wxString& aString )
|
|
|
|
{
|
|
|
|
aString = wxString( aJson.get<std::string>().c_str(), wxConvUTF8 );
|
2020-07-05 16:25:04 +00:00
|
|
|
}
|