37#include <wx/aui/framemanager.h>
40#include <wx/fileconf.h>
41#include <wx/filename.h>
44#include <wx/stdstream.h>
45#include <wx/wfstream.h>
50 std::replace( aPath.begin(), aPath.end(),
'.',
'/' );
51 aPath.insert( 0,
"/" );
53 nlohmann::json::json_pointer p;
57 p = nlohmann::json::json_pointer( aPath );
61 wxASSERT_MSG(
false, wxT(
"Invalid pointer path in PointerFromString!" ) );
69 int aSchemaVersion,
bool aCreateIfMissing,
bool aCreateIfDefault,
86 m_internals = std::make_unique<JSON_SETTINGS_INTERNALS>();
156 wxLogTrace(
traceSettings, wxT(
"param '%s' load err" ), param->GetJsonPath().c_str() );
169 bool migrated =
false;
170 bool legacy_migrated =
false;
174 auto migrateFromLegacy =
175 [&] ( wxFileName& aPath )
179 bool backed_up =
false;
182 if( aPath.IsDirWritable() )
184 temp.AssignTempFileName( aPath.GetFullPath() );
186 if( !wxCopyFile( aPath.GetFullPath(), temp.GetFullPath() ) )
188 wxLogTrace(
traceSettings, wxT(
"%s: could not create temp file for migration" ),
200 wxConfigBase::DontCreateOnDemand();
201 auto cfg = std::make_unique<wxFileConfig>( wxT(
"" ), wxT(
"" ), aPath.GetFullPath() );
208 wxLogTrace(
traceSettings, wxT(
"%s: migrated; not all settings were found in legacy file" ),
221 if( !wxCopyFile( temp.GetFullPath(), aPath.GetFullPath() ) )
223 wxLogTrace(
traceSettings, wxT(
"migrate; copy temp file %s to %s failed" ),
224 temp.GetFullPath(), aPath.GetFullPath() );
227 if( !wxRemoveFile( temp.GetFullPath() ) )
229 wxLogTrace(
traceSettings, wxT(
"migrate; failed to remove temp file %s" ),
230 temp.GetFullPath() );
235 legacy_migrated =
true;
240 if( aDirectory.empty() )
257 migrateFromLegacy(
path );
265 migrateFromLegacy(
path );
274 if( !
path.IsFileWritable() )
277 bool parseError =
false;
279 [&]( nlohmann::json& aDoc )
281 wxFFileInputStream fp(
path.GetFullPath(), wxT(
"rt" ) );
282 wxStdInputStream fstream( fp );
286 wxLogTrace(
traceSettings, wxT(
"%s exists but can't be opened for read" ),
294 migrated, parseError );
301 wxLogTrace(
traceSettings, wxT(
"Attempting migration in case file is in legacy format" ) );
302 migrateFromLegacy(
path );
311 settings->LoadFromFile();
321 bool doWrite =
m_writeFile && !wxGetEnv( wxT(
"KICAD_INHIBIT_SETTINGS_WRITES" ),
nullptr );
326 if( doWrite && ( legacy_migrated || migrated ) )
330 bool savedMigrated =
SaveToFile( aDirectory,
true );
332 if( savedMigrated && legacy_migrated && !wxRemoveFile(
path.GetFullPath() ) )
334 wxLogTrace(
traceSettings, wxT(
"Warning: could not remove legacy file %s" ),
path.GetFullPath() );
336 else if( !savedMigrated )
338 wxLogTrace(
traceSettings, wxT(
"Migrated save of %s failed; leaving legacy file intact" ),
355 nlohmann::json& doc = *
static_cast<nlohmann::json*
>(
m_internals.get() );
388 wxLogTrace(
traceSettings, wxT(
"%s: warning: file version %d is newer than latest (%d)" ),
393 catch( nlohmann::json::parse_error& error )
400 error.id,
static_cast<int>( error.byte ) );
413 bool migrated =
false;
414 bool parseError =
false;
417 [&]( nlohmann::json& aDoc )
422 migrated, parseError );
440 param->Store(
this );
442 catch(
const std::exception& e )
444 wxLogTrace(
traceSettings, wxT(
"param '%s' store err: %s" ), param->GetJsonPath().c_str(), e.what() );
461 std::map<std::string, nlohmann::json> histories;
463 for(
const std::string& candidate : { std::string(
"system.file_history" ) } )
466 histories[candidate] =
GetJson( candidate ).value();
486 if( aDirectory.empty() )
493 wxString dir( aDirectory );
500 wxT(
"File for %s doesn't exist and m_createIfMissing == false; not saving" ),
507 if( !modified && !aForce &&
path.FileExists() )
514 wxLogTrace(
traceSettings, wxT(
"%s contents still default and m_createIfDefault == false; not saving" ),
532 if( !aForce &&
path.FileExists() )
534 std::ifstream existing(
path.GetFullPath().fn_str(), std::ios::in | std::ios::binary );
538 std::string current( ( std::istreambuf_iterator<char>( existing ) ),
539 std::istreambuf_iterator<char>() );
543 if( !existing.bad() && current == payload )
545 wxLogTrace(
traceSettings, wxT(
"%s on-disk contents match payload, skipping write" ),
557 if( !
path.DirExists() && !
path.Mkdir() )
559 wxLogTrace(
traceSettings, wxT(
"Warning: could not create path %s, can't save %s" ),
563 else if( (
path.FileExists() && !
path.IsFileWritable() )
564 || ( !
path.FileExists() && !
path.IsDirWritable() ) )
576 catch( nlohmann::json::exception& error )
584 wxLogTrace(
traceSettings, wxT(
"Error: could not save %s." ) );
602 bool modified =
false;
606 wxCHECK2( settings,
continue );
608 modified |= settings->SaveToFile();
631 if( param->ClearUnknownKeys() )
635 toSave[p] = nlohmann::json::object();
641 return toSave.dump( 2 ) +
'\n';
651 std::stringstream buffer;
652 buffer << std::setw( 2 ) << *
m_internals << std::endl;
662 wxFFileInputStream fp( aPath, wxT(
"rt" ) );
663 wxStdInputStream fstream( fp );
667 *
static_cast<nlohmann::json*
>(
m_internals.get() ) = nlohmann::json::parse( fstream,
nullptr,
676 catch( nlohmann::json::parse_error& error )
678 wxLogTrace(
traceSettings, wxT(
"Json parse error reading %s (id %d, byte %d)" ), aPath, error.id,
679 static_cast<int>( error.byte ) );
692 nlohmann::json::json_pointer ptr =
m_internals->PointerFromString( aPath );
698 return std::optional<nlohmann::json>{
m_internals->at( ptr ) };
705 return std::optional<nlohmann::json>{};
709template<
typename ValueType>
712 if( std::optional<nlohmann::json> ret =
GetJson( aPath ) )
716 return ret->get<ValueType>();
764template<
typename ValueType>
767 m_internals->SetFromString( aPath, std::move( aVal ) );
777 unsigned int aValue );
779 unsigned long long aValue );
781 const char* aValue );
783 std::string aValue );
785 nlohmann::json aValue );
799 wxAuiPaneInfo aValue );
805 std::function<
bool()> aMigrator )
807 wxASSERT( aNewSchemaVersion > aOldSchemaVersion );
809 m_migrators[aOldSchemaVersion] = std::make_pair( aNewSchemaVersion, aMigrator );
815 int filever =
m_internals->Get<
int>(
"meta.version" );
823 wxLogTrace(
traceSettings, wxT(
"Migrator missing for %s version %d!" ),
typeid( *this ).name(),
828 std::pair<int, std::function<bool()>> pair =
m_migrators.at( filever );
832 wxLogTrace(
traceSettings, wxT(
"Migrated %s from %d to %d" ),
typeid( *this ).name(), filever,
834 filever = pair.first;
839 wxLogTrace(
traceSettings, wxT(
"Migration failed for %s from %d to %d" ),
typeid( *this ).name(),
840 filever, pair.first );
851 wxLogTrace(
traceSettings, wxT(
"MigrateFromLegacy() not implemented for %s" ),
typeid( *this ).name() );
860 if( aObj.contains( ptr ) && aObj.at( ptr ).is_string() )
862 aTarget = aObj.at( ptr ).get<wxString>();
874 if( aObj.contains( ptr ) && aObj.at( ptr ).is_boolean() )
876 aTarget = aObj.at( ptr ).get<
bool>();
888 if( aObj.contains( ptr ) && aObj.at( ptr ).is_number_integer() )
890 aTarget = aObj.at( ptr ).get<
int>();
902 if( aObj.contains( ptr ) && aObj.at( ptr ).is_number_unsigned() )
904 aTarget = aObj.at( ptr ).get<
unsigned int>();
912template<
typename ValueType>
917 if( aConfig->Read( aKey, &val ) )
921 ( *m_internals )[aDest] = val;
925 wxASSERT_MSG(
false, wxT(
"Could not write value in fromLegacy!" ) );
952 if( aConfig->Read( aKey, &str ) )
956 ( *m_internals )[aDest] = str.ToUTF8();
960 wxASSERT_MSG(
false, wxT(
"Could not write value in fromLegacyString!" ) );
975 if( aConfig->Read( aKey, &str ) )
982 nlohmann::json js = nlohmann::json::array( { color.
r, color.
g, color.
b, color.
a } );
983 ( *m_internals )[aDest] = std::move( js );
987 wxASSERT_MSG(
false, wxT(
"Could not write value in fromLegacyColor!" ) );
1013 return aPtr == aSettings;
1018 wxLogTrace(
traceSettings, wxT(
"Flush and release %s" ), ( *it )->GetFilename() );
1036 if( std::optional<nlohmann::json> opt_json =
GetJson( aPath ) )
1037 return wxString( opt_json->get<std::string>().c_str(), wxConvUTF8 );
1039 return std::nullopt;
1046 ( *m_internals )[aPath] = aVal.ToUTF8();
1050template<
typename ResultType>
1052 ResultType aDefault )
1054 ResultType ret = std::move( aDefault );
1058 if( aJson.contains( aKey ) )
1059 ret = aJson.at( aKey ).get<ResultType>();
1071 std::string aDefault );
static nlohmann::json::json_pointer PointerFromString(std::string aPath)
Builds a JSON pointer based on a given string.
bool fromLegacyString(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy wxConfig string value to a given JSON pointer value.
bool fromLegacy(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy wxConfig value to a given JSON pointer value.
virtual wxString getFileExt() const
const nlohmann::json & originalJson() const
virtual std::map< std::string, nlohmann::json > GetFileHistories()
bool flushToStore()
Flushes nested settings and parameters into the JSON store.
void Set(const std::string &aPath, ValueType aVal)
Stores a value into the JSON document Will throw an exception if ValueType isn't something that the l...
std::optional< nlohmann::json > GetJson(const std::string &aPath) const
Fetches a JSON object that is a subset of this JSON_SETTINGS object, using a path of the form "key1....
wxString m_filename
The filename (not including path) of this settings file (inicode)
SETTINGS_MANAGER * m_manager
A pointer to the settings manager managing this file (may be null)
bool Contains(const std::string &aPath) const
bool m_fileSynced
True once the store has been synchronized with an on-disk file (loaded or saved)
bool LoadFromRawFile(const wxString &aPath)
bool m_isFutureFormat
Set to true if this settings is loaded from a file with a newer schema version than is known.
std::vector< NESTED_SETTINGS * > m_nested_settings
Nested settings files that live inside this one, if any.
bool m_modified
True if the JSON data store has been written to since the last file write.
virtual bool LoadFromFile(const wxString &aDirectory="")
Loads the backing file from disk and then calls Load()
bool m_createIfDefault
Whether or not the backing store file should be created if all parameters are still at their default ...
bool m_writeFile
Whether or not the backing store file should be written.
static bool SetIfPresent(const nlohmann::json &aObj, const std::string &aPath, wxString &aTarget)
Sets the given string if the given key/path is present.
virtual void Load()
Updates the parameters of this object based on the current JSON document contents.
bool parseAndMigrate(const std::function< bool(nlohmann::json &)> &aParse, bool &aMigrated, bool &aParseError)
Parses a document into the store through aParse, then checks the schema version and migrates.
void ResetToDefaults()
Resets all parameters to default values.
bool fromLegacyColor(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy COLOR4D stored in a wxConfig string to a given JSON pointer value.
wxString GetFullFilename() const
bool m_createIfMissing
Whether or not the backing store file should be created it if doesn't exist.
bool Migrate()
Migrates the schema of this settings from the version in the file to the latest version.
std::optional< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
bool m_lastSaveFailed
True if the most recent SaveToFile() had changes to write and could not write them.
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
virtual bool MigrateFromLegacy(wxConfigBase *aLegacyConfig)
Migrates from wxConfig to JSON-based configuration.
const std::string FormatAsString()
void registerMigration(int aOldSchemaVersion, int aNewSchemaVersion, std::function< bool(void)> aMigrator)
Registers a migration from one schema version to another.
nlohmann::json & At(const std::string &aPath)
Wrappers for the underlying JSON API so that most consumers don't need json.hpp All of these function...
JSON_SETTINGS_INTERNALS * Internals()
static ResultType fetchOrDefault(const nlohmann::json &aJson, const std::string &aKey, ResultType aDefault=ResultType())
Helper to retrieve a value from a JSON object (dictionary) as a certain result type.
void ReleaseNestedSettings(NESTED_SETTINGS *aSettings)
Saves and frees a nested settings object, if it exists within this one.
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
bool m_deleteLegacyAfterMigration
Whether or not to delete legacy file after migration.
std::unique_ptr< JSON_SETTINGS_INTERNALS > m_internals
friend class NESTED_SETTINGS
SETTINGS_LOC m_location
The location of this settings file (.
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
Calls Store() and then writes the contents of the JSON document to a file.
virtual wxString getLegacyFileExt() const
bool m_resetParamsIfMissing
Whether or not to set parameters to their default value if missing from JSON on Load()
std::string formatFileContents()
Overlays the current store on the original file contents.
bool LoadFromBuffer(const std::string &aContents)
Loads the document from file contents already in memory, so the caller can bound the read of the file...
std::map< int, std::pair< int, std::function< bool()> > > m_migrators
A map of starting schema version to a pair of <ending version, migrator function>
int m_schemaVersion
Version of this settings schema.
wxString m_legacy_filename
The filename of the wxConfig legacy file (if different from m_filename)
void AddNestedSettings(NESTED_SETTINGS *aSettings)
Transfers ownership of a given NESTED_SETTINGS to this object.
virtual bool Store()
Stores the current parameters into the JSON document represented by this object Note: this doesn't do...
wxString GetFilename() const
A color representation with 4 components: red, green, blue, alpha.
bool SetFromWxString(const wxString &aColorString)
Set color values by parsing a string using wxColour::Set().
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
void SetParent(JSON_SETTINGS *aParent, bool aLoadFromFile=true)
std::vector< FAB_LAYER_COLOR > dummy
Common grid settings, available to every frame.