KiCad PCB EDA Suite
Loading...
Searching...
No Matches
nested_settings.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2020 Jon Evans <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <wx/log.h>
22
25#include <settings/parameters.h>
26#include <locale_io.h>
27
28
29NESTED_SETTINGS::NESTED_SETTINGS( const std::string& aName, int aVersion, JSON_SETTINGS* aParent,
30 const std::string& aPath, bool aLoadFromFile ) :
31 JSON_SETTINGS( aName, SETTINGS_LOC::NESTED, aVersion ),
32 m_parent( aParent ), m_path( aPath )
33{
34 SetParent( aParent, aLoadFromFile );
35}
36
37
39{
40 if( m_parent )
41 m_parent->ReleaseNestedSettings( this );
42}
43
44
45bool NESTED_SETTINGS::LoadFromFile( const wxString& aDirectory )
46{
47 m_internals->clear();
48 bool success = false;
49 bool migrationFailed = false;
50
51 if( m_parent )
52 {
53 nlohmann::json::json_pointer ptr = m_internals->PointerFromString( m_path );
54
55 if( m_parent->m_internals->contains( ptr ) )
56 {
57 try
58 {
59 m_internals->update( m_parent->m_internals->at( ptr ) );
60
61 wxLogTrace( traceSettings, wxT( "Loaded NESTED_SETTINGS %s" ), GetFilename() );
62
63 success = true;
64 }
65 catch( ... )
66 {
67 wxLogTrace( traceSettings, wxT( "NESTED_SETTINGS %s: Could not load from "
68 "%s at %s" ),
69 m_filename, m_parent->GetFilename(), m_path );
70 }
71 }
72 }
73
74 if( success )
75 {
76 int filever = -1;
77
78 try
79 {
80 filever = m_internals->Get<int>( "meta.version" );
81 }
82 catch( ... )
83 {
84 wxLogTrace( traceSettings, wxT( "%s: nested settings version could not be read!" ),
85 m_filename );
86 success = false;
87 }
88
89 if( filever >= 0 && filever < m_schemaVersion )
90 {
91 wxLogTrace( traceSettings, wxT( "%s: attempting migration from version %d to %d" ),
92 m_filename, filever, m_schemaVersion );
93
94 bool migrated = false;
95
96 try
97 {
98 migrated = Migrate();
99 }
100 catch( ... )
101 {
102 success = false;
103 }
104
105 if( !migrated )
106 {
107 wxLogTrace( traceSettings, wxT( "%s: migration failed!" ), GetFullFilename() );
108 success = false;
109 migrationFailed = true;
110 }
111 }
112 else if( filever > m_schemaVersion )
113 {
114 wxLogTrace( traceSettings,
115 wxT( "%s: warning: nested settings version %d is newer than latest (%d)" ),
116 m_filename, filever, m_schemaVersion );
117 }
118 else if( filever >= 0 )
119 {
120 wxLogTrace( traceSettings, wxT( "%s: schema version %d is current" ),
121 m_filename, filever );
122 }
123 }
124
125 Load();
126
127 // A reread of the file must reach settings nested more than one level down
128 for( NESTED_SETTINGS* settings : m_nested_settings )
129 settings->LoadFromFile();
130
131 // Hand migration and default fill to the parent so they do not read as a change later, unless
132 // the migration failed (as in a load from the base ctor) and a later load must rerun it
133 if( !m_writeImplicitly && !migrationFailed )
134 {
135 SaveToFile( wxEmptyString, true );
136 m_modified = false;
137 }
138
139 return success;
140}
141
142
143bool NESTED_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
144{
145 if( !m_parent )
146 return false;
147
149
150 try
151 {
152 // Diff our still-loaded internals against the parent's copy before Store() materializes
153 // params. Comparing here keeps default-fill of params absent from an older file from
154 // counting as a change and churning the parent on editor close (see #24402).
155 auto jsonObjectInParent = m_parent->GetJson( m_path );
156
157 bool modified = !jsonObjectInParent
158 || !nlohmann::json::diff( *m_internals, jsonObjectInParent.value() ).empty();
159
160 // Store() additionally reports user edits to registered params, not yet reflected above.
161 modified |= Store();
162
163 // The parent merge only adds and updates, so clear subtree-owning params from the baseline
164 // of every ancestor; only the file-backed root holds one
165 for( const PARAM_BASE* param : m_params )
166 {
167 if( !param->ClearUnknownKeys() )
168 continue;
169
170 std::string path = param->GetJsonPath();
171 NESTED_SETTINGS* nested = this;
172
173 while( nested && nested->m_parent )
174 {
175 path = nested->m_path + "." + path;
176
177 nlohmann::json& original = nested->m_parent->m_internals->m_original;
178 nlohmann::json::json_pointer ptr = JSON_SETTINGS_INTERNALS::PointerFromString( path );
179
180 if( original.contains( ptr ) )
181 original[ptr] = nlohmann::json::object();
182
183 nested = dynamic_cast<NESTED_SETTINGS*>( nested->m_parent );
184 }
185 }
186
187 if( modified || aForce )
188 {
189 ( *m_parent->m_internals )[m_path].update( *m_internals );
190
191 wxLogTrace( traceSettings, wxS( "Stored NESTED_SETTINGS %s with schema %d" ),
192 GetFilename(),
194 }
195
196 return modified;
197 }
198 catch( ... )
199 {
200 wxLogTrace( traceSettings, wxS( "NESTED_SETTINGS %s: Could not store to %s at %s" ),
202 m_parent->GetFilename(),
203 m_path );
204
205 return false;
206 }
207}
208
209
210void NESTED_SETTINGS::SetParent( JSON_SETTINGS* aParent, bool aLoadFromFile )
211{
212 m_parent = aParent;
213
214 if( m_parent )
215 {
216 m_writeImplicitly = m_parent->m_writeImplicitly;
217 m_parent->AddNestedSettings( this );
218
219 // In case we were created after the parent's ctor
220 if( aLoadFromFile )
221 LoadFromFile();
222 }
223}
static nlohmann::json::json_pointer PointerFromString(std::string aPath)
Builds a JSON pointer based on a given string.
wxString m_filename
The filename (not including path) of this settings file (inicode)
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_writeImplicitly
Whether migration and default fill may write the file by themselves.
virtual void Load()
Updates the parameters of this object based on the current JSON document contents.
wxString GetFullFilename() const
bool Migrate()
Migrates the schema of this settings from the version in the file to the latest version.
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
std::unique_ptr< JSON_SETTINGS_INTERNALS > m_internals
friend class NESTED_SETTINGS
int m_schemaVersion
Version of this settings schema.
virtual bool Store()
Stores the current parameters into the JSON document represented by this object Note: this doesn't do...
wxString GetFilename() const
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
void SetParent(JSON_SETTINGS *aParent, bool aLoadFromFile=true)
std::string m_path
The path (in pointer format) of where to store this document in the parent.
JSON_SETTINGS * m_parent
A pointer to the parent object to load and store from.
virtual ~NESTED_SETTINGS()
bool SaveToFile(const wxString &aDirectory="", bool aForce=false) override
Calls Store() and then saves the JSON document contents into the parent JSON_SETTINGS.
NESTED_SETTINGS(const std::string &aName, int aSchemaVersion, JSON_SETTINGS *aParent, const std::string &aPath, bool aLoadFromFile=true)
bool LoadFromFile(const wxString &aDirectory="") override
Loads the JSON document from the parent and then calls Load()
SETTINGS_LOC
@ NESTED
Not stored in a file, but inside another JSON_SETTINGS.
#define traceSettings
std::vector< FAB_LAYER_COLOR > dummy
std::string path