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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _JSON_SETTINGS_H
|
|
|
|
#define _JSON_SETTINGS_H
|
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <utility>
|
|
|
|
#include <wx/string.h>
|
|
|
|
|
2020-03-07 21:06:33 +00:00
|
|
|
#include <core/optional.h>
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
class wxConfigBase;
|
|
|
|
class NESTED_SETTINGS;
|
|
|
|
class PARAM_BASE;
|
2020-05-06 01:16:05 +00:00
|
|
|
class SETTINGS_MANAGER;
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
enum class SETTINGS_LOC {
|
|
|
|
USER, ///< The main config directory (e.g. ~/.config/kicad/)
|
|
|
|
PROJECT, ///< The settings directory inside a project folder
|
|
|
|
COLORS, ///< The color scheme directory (e.g. ~/.config/kicad/colors/)
|
|
|
|
NESTED, ///< Not stored in a file, but inside another JSON_SETTINGS
|
2020-05-25 17:06:53 +00:00
|
|
|
NONE, ///< No directory prepended, full path in filename (used for PROJECT_FILE)
|
2020-01-13 01:44:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class JSON_SETTINGS : public nlohmann::json
|
|
|
|
{
|
|
|
|
public:
|
2020-08-02 09:23:21 +00:00
|
|
|
JSON_SETTINGS( const wxString& aFilename, SETTINGS_LOC aLocation, int aSchemaVersion ) :
|
2020-05-30 16:56:16 +00:00
|
|
|
JSON_SETTINGS( aFilename, aLocation, aSchemaVersion, true, true, true ) {}
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2020-08-02 09:23:21 +00:00
|
|
|
JSON_SETTINGS( const wxString& aFilename, SETTINGS_LOC aLocation, int aSchemaVersion,
|
2020-05-30 16:56:16 +00:00
|
|
|
bool aCreateIfMissing, bool aCreateIfDefault, bool aWriteFile );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
virtual ~JSON_SETTINGS();
|
|
|
|
|
2020-08-02 09:23:21 +00:00
|
|
|
wxString GetFilename() const { return m_filename; }
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
wxString GetFullFilename() const;
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
SETTINGS_LOC GetLocation() const { return m_location; }
|
|
|
|
|
2020-08-02 09:23:21 +00:00
|
|
|
void SetLegacyFilename( const wxString& aFilename ) { m_legacy_filename = aFilename; }
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the parameters of this object based on the current JSON document contents
|
|
|
|
*/
|
|
|
|
virtual void Load();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores the current parameters into the JSON document represented by this object
|
|
|
|
* Note: this doesn't do any writing to disk; that's handled by SETTINGS_MANAGER
|
2020-05-30 16:56:16 +00:00
|
|
|
* @return true if any part of the JSON document was updated
|
2020-01-13 01:44:19 +00:00
|
|
|
*/
|
2020-05-30 16:56:16 +00:00
|
|
|
virtual bool Store();
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the backing file from disk and then calls Load()
|
|
|
|
* @param aDirectory is the path to the file
|
2020-05-25 17:06:53 +00:00
|
|
|
* @return true if the file was found on disk and loaded or migrated
|
2020-01-13 01:44:19 +00:00
|
|
|
*/
|
2020-08-02 09:23:21 +00:00
|
|
|
virtual bool LoadFromFile( const wxString& aDirectory = "" );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls Store() and then writes the contents of the JSON document to a file
|
|
|
|
* @param aDirectory is the directory to save to, including trailing separator
|
2020-05-31 21:42:04 +00:00
|
|
|
c * @return true if the file was saved
|
2020-01-13 01:44:19 +00:00
|
|
|
*/
|
2020-08-02 09:23:21 +00:00
|
|
|
virtual bool SaveToFile( const wxString& aDirectory = "", bool aForce = false );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets all parameters to default values. Does NOT write to file or update underlying JSON.
|
|
|
|
*/
|
|
|
|
void ResetToDefaults();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches a JSON object that is a subset of this JSON_SETTINGS object, using a path of the
|
|
|
|
* form "key1.key2.key3" to refer to nested objects.
|
|
|
|
* @param aPath is a string containing one or more keys separated by '.'
|
|
|
|
* @return a JSON object from within this one
|
|
|
|
*/
|
2020-05-31 21:42:04 +00:00
|
|
|
OPT<nlohmann::json> GetJson( const std::string& aPath ) const;
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches a value from within the JSON document.
|
2020-03-07 21:06:33 +00:00
|
|
|
* Will return an empty optional if the value is not found or a mismatching type.
|
2020-01-13 01:44:19 +00:00
|
|
|
* @tparam ValueType is the type to cast to
|
|
|
|
* @param aPath is the path within the document to retrieve
|
|
|
|
* @return a value from within this document
|
|
|
|
*/
|
|
|
|
template<typename ValueType>
|
2020-05-31 21:42:04 +00:00
|
|
|
OPT<ValueType> 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> ret = GetJson( aPath ) )
|
2020-03-09 18:05:34 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return ret->get<ValueType>();
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 21:06:33 +00:00
|
|
|
|
|
|
|
return NULLOPT;
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores a value into the JSON document
|
|
|
|
* Will throw an exception if ValueType isn't something that the library can handle
|
|
|
|
* @tparam ValueType is the type to store
|
|
|
|
* @param aPath is a path to store in the form "key1.key2.key3"
|
|
|
|
* @param aVal is the value to store
|
|
|
|
*/
|
|
|
|
template<typename ValueType>
|
2020-05-31 21:42:04 +00:00
|
|
|
void Set( const std::string& aPath, ValueType aVal )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
( *this )[PointerFromString( aPath ) ] = aVal;
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Migrates the schema of this settings from the version in the file to the latest version
|
|
|
|
*
|
|
|
|
* Schema migration doesn't need to be used every time a setting is added! This is intended
|
|
|
|
* to be more of an "escape hatch" in the event that we decide to move settings around or make
|
|
|
|
* other changes to a settings file format that can't simply be handled by loading a new default
|
|
|
|
*
|
|
|
|
* @return true if migration was successful
|
|
|
|
*/
|
|
|
|
virtual bool Migrate();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Migrates from wxConfig to JSON-based configuration. Should be implemented by any subclasses
|
|
|
|
* of JSON_SETTINGS that map to a legacy (wxConfig-based) config file.
|
|
|
|
* @param aLegacyConfig is a wxConfigBase holding a loaded configuration to migrate
|
|
|
|
* @return true if migration was successful
|
|
|
|
*/
|
|
|
|
virtual bool MigrateFromLegacy( wxConfigBase* aLegacyConfig );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transfers ownership of a given NESTED_SETTINGS to this object.
|
|
|
|
* Can be used to construct a NESTED_SETTINGS without the parent object needing to know about
|
|
|
|
* the implementation of the nested object;
|
|
|
|
*
|
|
|
|
* @param aSettings is the settings object to take ownership of
|
|
|
|
* @param aTarget is a pointer to update to the passed in settings
|
|
|
|
*/
|
|
|
|
void AddNestedSettings( NESTED_SETTINGS* aSettings );
|
2020-02-28 03:53:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves and frees a nested settings object, if it exists within this one
|
|
|
|
* @param aSettings is a pointer to a NESTED_SETTINGS that has already been added to this one
|
|
|
|
*/
|
|
|
|
void ReleaseNestedSettings( NESTED_SETTINGS* aSettings );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2020-05-06 01:16:05 +00:00
|
|
|
void SetManager( SETTINGS_MANAGER* aManager )
|
|
|
|
{
|
|
|
|
m_manager = aManager;
|
|
|
|
}
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
/**
|
|
|
|
* Builds a JSON pointer based on a given string
|
|
|
|
* @param aPath is the path in the form "key1.key2.key3"
|
|
|
|
* @return a JSON pointer that can be used to index into a JSON object
|
|
|
|
*/
|
|
|
|
static nlohmann::json::json_pointer PointerFromString( std::string aPath );
|
|
|
|
|
2020-08-24 02:01:14 +00:00
|
|
|
/**
|
|
|
|
* Sets the given string if the given key/path is present
|
|
|
|
* @param aObj is the source object
|
|
|
|
* @param aTarget is the storage destination
|
|
|
|
* @return True if set, false if not
|
|
|
|
*/
|
2020-08-24 02:10:05 +00:00
|
|
|
static bool SetIfPresent( const nlohmann::json& aObj, const std::string& aPath,
|
|
|
|
wxString& aTarget );
|
2020-08-24 02:01:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the given bool if the given key/path is present
|
|
|
|
* @param aObj is the source object
|
|
|
|
* @param aTarget is the storage destination
|
|
|
|
* @return True if set, false if not
|
|
|
|
*/
|
|
|
|
static bool SetIfPresent( const nlohmann::json& aObj, const std::string& aPath, bool& aTarget );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the given int if the given key/path is present
|
|
|
|
* @param aObj is the source object
|
|
|
|
* @param aTarget is the storage destination
|
|
|
|
* @return True if set, false if not
|
|
|
|
*/
|
|
|
|
static bool SetIfPresent( const nlohmann::json& aObj, const std::string& aPath, int& aTarget );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the given unsigned int if the given key/path is present
|
|
|
|
* @param aObj is the source object
|
|
|
|
* @param aTarget is the storage destination
|
|
|
|
* @return True if set, false if not
|
|
|
|
*/
|
2020-08-24 02:10:05 +00:00
|
|
|
static bool SetIfPresent( const nlohmann::json& aObj, const std::string& aPath,
|
|
|
|
unsigned int& aTarget );
|
2020-01-13 01:44:19 +00:00
|
|
|
protected:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translates a legacy wxConfig value to a given JSON pointer value
|
|
|
|
* @tparam ValueType is the basic type of the value
|
|
|
|
* @param aConfig is the legacy config to read from
|
|
|
|
* @param aKey is the key (within the current path) to read
|
|
|
|
* @param aDest is a string that will form a JSON pointer (key1.key2.key3) to write to
|
|
|
|
*/
|
|
|
|
template<typename ValueType>
|
|
|
|
bool fromLegacy( wxConfigBase* aConfig, const std::string& aKey, const std::string& aDest );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translates a legacy wxConfig string value to a given JSON pointer value
|
|
|
|
* @param aConfig is the legacy config to read from
|
|
|
|
* @param aKey is the key (within the current path) to read
|
|
|
|
* @param aDest is a string that will form a JSON pointer (key1.key2.key3) to write to
|
|
|
|
*/
|
|
|
|
bool fromLegacyString( 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
|
|
|
|
* @param aConfig is the legacy config to read from
|
|
|
|
* @param aKey is the key (within the current path) to read
|
|
|
|
* @param aDest is a string that will form a JSON pointer (key1.key2.key3) to write to
|
|
|
|
*/
|
|
|
|
bool fromLegacyColor( wxConfigBase* aConfig, const std::string& aKey,
|
|
|
|
const std::string& aDest );
|
|
|
|
|
2020-05-25 20:41:24 +00:00
|
|
|
virtual wxString getFileExt() const
|
|
|
|
{
|
|
|
|
return wxT( "json" );
|
|
|
|
}
|
|
|
|
|
2020-05-25 17:06:53 +00:00
|
|
|
virtual wxString getLegacyFileExt() const
|
|
|
|
{
|
|
|
|
return wxEmptyString;
|
|
|
|
}
|
|
|
|
|
2020-08-02 09:23:21 +00:00
|
|
|
/// The filename (not including path) of this settings file (inicode)
|
|
|
|
wxString m_filename;
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
/// The filename of the wxConfig legacy file (if different from m_filename)
|
2020-08-02 09:23:21 +00:00
|
|
|
wxString m_legacy_filename;
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
/// The location of this settings file (@see SETTINGS_LOC)
|
|
|
|
SETTINGS_LOC m_location;
|
|
|
|
|
|
|
|
/// The list of parameters (owned by this object)
|
|
|
|
std::vector<PARAM_BASE*> m_params;
|
|
|
|
|
|
|
|
/// Nested settings files that live inside this one, if any
|
|
|
|
std::vector<NESTED_SETTINGS*> m_nested_settings;
|
|
|
|
|
|
|
|
/// Whether or not the backing store file should be created it if doesn't exist
|
|
|
|
bool m_createIfMissing;
|
|
|
|
|
2020-05-30 16:56:16 +00:00
|
|
|
/**
|
|
|
|
* Whether or not the backing store file should be created if all parameters are still
|
|
|
|
* at their default values. Ignored if m_createIfMissing is false or m_writeFile is false.
|
|
|
|
*/
|
|
|
|
bool m_createIfDefault;
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
/// Whether or not the backing store file should be written
|
|
|
|
bool m_writeFile;
|
|
|
|
|
2020-05-25 20:41:24 +00:00
|
|
|
/// Whether or not to delete legacy file after migration
|
|
|
|
bool m_deleteLegacyAfterMigration;
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
/// Whether or not to set parameters to their default value if missing from JSON on Load()
|
|
|
|
bool m_resetParamsIfMissing;
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
/// Version of this settings schema.
|
|
|
|
int m_schemaVersion;
|
|
|
|
|
2020-05-06 01:16:05 +00:00
|
|
|
/// A pointer to the settings manager managing this file (may be null)
|
|
|
|
SETTINGS_MANAGER* m_manager;
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
/// A list of JSON pointers that are preserved during a read-update-write to disk
|
|
|
|
std::vector<nlohmann::json::json_pointer> m_preserved_paths;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
|
|
template<> void JSON_SETTINGS::Set<wxString>( const std::string& aPath, wxString aVal );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
// Specializations to allow directly reading/writing wxStrings from JSON
|
|
|
|
|
|
|
|
void to_json( nlohmann::json& aJson, const wxString& aString );
|
|
|
|
|
|
|
|
void from_json( const nlohmann::json& aJson, wxString& aString );
|
|
|
|
|
|
|
|
#endif
|