KiCad PCB EDA Suite
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 (C) 2020 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 along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <wx/log.h>
22
25#include <locale_io.h>
26
27NESTED_SETTINGS::NESTED_SETTINGS( const std::string& aName, int aVersion, JSON_SETTINGS* aParent,
28 const std::string& aPath ) :
29 JSON_SETTINGS( aName, SETTINGS_LOC::NESTED, aVersion ),
30 m_parent( aParent ), m_path( aPath )
31{
32 SetParent( aParent );
33}
34
35
37{
38 if( m_parent )
40}
41
42
43bool NESTED_SETTINGS::LoadFromFile( const wxString& aDirectory )
44{
45 m_internals->clear();
46 bool success = false;
47
48 if( m_parent )
49 {
50 nlohmann::json::json_pointer ptr = m_internals->PointerFromString( m_path );
51
52 if( m_parent->m_internals->contains( ptr ) )
53 {
54 try
55 {
56 m_internals->update( m_parent->m_internals->at( ptr ) );
57
58 wxLogTrace( traceSettings, wxT( "Loaded NESTED_SETTINGS %s" ), GetFilename() );
59
60 success = true;
61 }
62 catch( ... )
63 {
64 wxLogTrace( traceSettings, wxT( "NESTED_SETTINGS %s: Could not load from %s at %s" ),
66 }
67 }
68 }
69
70 if( success )
71 {
72 int filever = -1;
73
74 try
75 {
76 filever = m_internals->Get<int>( "meta.version" );
77 }
78 catch( ... )
79 {
80 wxLogTrace( traceSettings, wxT( "%s: nested settings version could not be read!" ),
81 m_filename );
82 success = false;
83 }
84
85 if( filever >= 0 && filever < m_schemaVersion )
86 {
87 wxLogTrace( traceSettings, wxT( "%s: attempting migration from version %d to %d" ),
88 m_filename, filever, m_schemaVersion );
89
90 if( !Migrate() )
91 {
92 wxLogTrace( traceSettings, wxT( "%s: migration failed!" ), GetFullFilename() );
93 success = false;
94 }
95 }
96 else if( filever > m_schemaVersion )
97 {
98 wxLogTrace( traceSettings,
99 wxT( "%s: warning: nested settings version %d is newer than latest (%d)" ),
100 m_filename, filever, m_schemaVersion );
101 }
102 else if( filever >= 0 )
103 {
104 wxLogTrace( traceSettings, wxT( "%s: schema version %d is current" ),
105 m_filename, filever );
106 }
107 }
108
109 Load();
110
111 return success;
112}
113
114
115bool NESTED_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
116{
117 if( !m_parent )
118 return false;
119
121
122 try
123 {
124 bool modified = Store();
125
126 auto jsonObjectInParent = m_parent->GetJson( m_path );
127
128 if( !jsonObjectInParent )
129 modified = true;
130 else if( !nlohmann::json::diff( *m_internals, jsonObjectInParent.value() ).empty() )
131 modified = true;
132
133 if( modified || aForce )
134 {
135 ( *m_parent->m_internals )[m_path].update( *m_internals );
136
137 wxLogTrace( traceSettings, "Stored NESTED_SETTINGS %s with schema %d",
138 GetFilename(),
140 }
141
142 return modified;
143 }
144 catch( ... )
145 {
146 wxLogTrace( traceSettings, "NESTED_SETTINGS %s: Could not store to %s at %s",
149 m_path );
150
151 return false;
152 }
153}
154
155
156void NESTED_SETTINGS::SetParent( JSON_SETTINGS* aParent, bool aLoadFromFile )
157{
158 m_parent = aParent;
159
160 if( m_parent )
161 {
163
164 // In case we were created after the parent's ctor
165 if( aLoadFromFile )
166 LoadFromFile();
167 }
168}
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)
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.
void ReleaseNestedSettings(NESTED_SETTINGS *aSettings)
Saves and frees a nested settings object, if it exists within this one.
std::unique_ptr< JSON_SETTINGS_INTERNALS > m_internals
friend class NESTED_SETTINGS
Definition: json_settings.h:62
int m_schemaVersion
Version of this settings schema.
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
Definition: json_settings.h:73
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:41
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.
bool LoadFromFile(const wxString &aDirectory="") override
Loads the JSON document from the parent and then calls Load()
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.
SETTINGS_LOC
Definition: json_settings.h:47
@ NESTED
Not stored in a file, but inside another JSON_SETTINGS.
static LIB_SYMBOL * dummy()
Used to draw a dummy shape when a LIB_SYMBOL is not found in library.
Definition: sch_symbol.cpp:74