21#ifndef _JSON_SETTINGS_H
22#define _JSON_SETTINGS_H
31#include <nlohmann/json_fwd.hpp>
54#define traceSettings wxT( "KICAD_SETTINGS" )
75 JSON_SETTINGS( aFilename, aLocation, aSchemaVersion, true, true, true )
79 bool aCreateIfMissing,
bool aCreateIfDefault,
bool aWriteFile );
90 wxString GetFullFilename()
const;
121 nlohmann::json& At(
const std::string& aPath );
122 bool Contains(
const std::string& aPath )
const;
136 virtual bool Store();
143 virtual bool LoadFromFile(
const wxString& aDirectory =
"" );
152 bool LoadFromBuffer(
const std::string& aContents );
160 virtual bool SaveToFile(
const wxString& aDirectory =
"",
bool aForce =
false );
182 void ResetToDefaults();
190 std::optional<nlohmann::json> GetJson(
const std::string& aPath )
const;
199 template<
typename ValueType>
200 std::optional<ValueType> Get(
const std::string& aPath )
const;
209 template<
typename ValueType>
210 void Set(
const std::string& aPath, ValueType aVal );
212 virtual std::map<std::string, nlohmann::json> GetFileHistories();
231 virtual bool MigrateFromLegacy( wxConfigBase* aLegacyConfig );
260 static bool SetIfPresent(
const nlohmann::json& aObj,
const std::string& aPath, wxString& aTarget );
269 static bool SetIfPresent(
const nlohmann::json& aObj,
const std::string& aPath,
bool& aTarget );
278 static bool SetIfPresent(
const nlohmann::json& aObj,
const std::string& aPath,
int& aTarget );
287 static bool SetIfPresent(
const nlohmann::json& aObj,
const std::string& aPath,
unsigned int& aTarget );
289 const std::string FormatAsString();
291 bool LoadFromRawFile(
const wxString& aPath );
295 const nlohmann::json& originalJson()
const;
305 void registerMigration(
int aOldSchemaVersion,
int aNewSchemaVersion,
306 std::function<
bool(
void)> aMigrator );
315 template<
typename ValueType>
316 bool fromLegacy( wxConfigBase* aConfig,
const std::string& aKey,
const std::string& aDest );
324 bool fromLegacyString( wxConfigBase* aConfig,
const std::string& aKey,
325 const std::string& aDest );
333 bool fromLegacyColor( wxConfigBase* aConfig,
const std::string& aKey,
334 const std::string& aDest );
346 std::string formatFileContents();
350 return wxT(
"json" );
355 return wxEmptyString;
366 template<
typename ResultType>
367 static ResultType fetchOrDefault(
const nlohmann::json& aJson,
const std::string& aKey,
368 ResultType aDefault = ResultType() );
434 bool parseAndMigrate(
const std::function<
bool( nlohmann::json& )>& aParse,
bool& aMigrated,
KICAD_PLUGIN_EXPORT SCENEGRAPH * Load(char const *aFileName)
Read a model file and creates a generic display structure.
virtual wxString getFileExt() const
bool IsFutureFormat() const
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...
JSON_SETTINGS & operator=(const JSON_SETTINGS &)=delete
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 IsFileSynced() const
bool m_fileSynced
True once the store has been synchronized with an on-disk file (loaded or saved)
SETTINGS_LOC GetLocation() const
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.
bool m_createIfDefault
Whether or not the backing store file should be created if all parameters are still at their default ...
JSON_SETTINGS(const JSON_SETTINGS &)=delete
void SetLocation(SETTINGS_LOC aLocation)
bool m_writeFile
Whether or not the backing store file should be written.
virtual const PROJECT * GetOwningProject() const
Project-located settings override this to report the project they belong to so their save path is res...
void SetManager(SETTINGS_MANAGER *aManager)
void SetReadOnly(bool aReadOnly)
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.
bool m_createIfMissing
Whether or not the backing store file should be created it if doesn't exist.
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)
bool ResetsParamsIfMissing() const
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
void SetFilename(const wxString &aFilename)
SETTINGS_LOC m_location
The location of this settings file (.
virtual wxString getLegacyFileExt() const
bool LastSaveFailed() const
SaveToFile() also returns false when it had nothing to write, so its result cannot tell a skipped sav...
bool m_resetParamsIfMissing
Whether or not to set parameters to their default value if missing from JSON on Load()
void SetLegacyFilename(const wxString &aFilename)
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)
wxString GetFilename() const
A color representation with 4 components: red, green, blue, alpha.
NESTED_SETTINGS is a JSON_SETTINGS that lives inside a JSON_SETTINGS.
Container for project specific data.
KICOMMON_API void to_json(nlohmann::json &aJson, const wxString &aString)
@ TOOLBARS
The toolbar directory (e.g. ~/.config/kicad/toolbars/)
@ PROJECT
The settings directory inside a project folder.
@ USER
The main config directory (e.g. ~/.config/kicad/)
@ COLORS
The color scheme directory (e.g. ~/.config/kicad/colors/)
@ NESTED
Not stored in a file, but inside another JSON_SETTINGS.
KICOMMON_API void from_json(const nlohmann::json &aJson, wxString &aString)
The Cairo implementation of the graphics abstraction layer.
Common grid settings, available to every frame.