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 <jon@craftyjon.com>
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 
26 NESTED_SETTINGS::NESTED_SETTINGS( const std::string& aName, int aVersion, JSON_SETTINGS* aParent,
27  const std::string& aPath ) :
28  JSON_SETTINGS( aName, SETTINGS_LOC::NESTED, aVersion ),
29  m_parent( aParent ), m_path( aPath )
30 {
31  SetParent( aParent );
32 }
33 
34 
36 {
37  if( m_parent )
39 }
40 
41 
42 bool NESTED_SETTINGS::LoadFromFile( const wxString& aDirectory )
43 {
44  m_internals->clear();
45  bool success = false;
46 
47  if( m_parent )
48  {
49  nlohmann::json::json_pointer ptr = m_internals->PointerFromString( m_path );
50 
51  if( m_parent->m_internals->contains( ptr ) )
52  {
53  try
54  {
55  m_internals->update( m_parent->m_internals->at( ptr ) );
56 
57  wxLogTrace( traceSettings, "Loaded NESTED_SETTINGS %s", GetFilename() );
58 
59  success = true;
60  }
61  catch( ... )
62  {
63  wxLogTrace( traceSettings, "NESTED_SETTINGS %s: Could not load from %s at %s",
65  }
66  }
67  }
68 
69  if( success )
70  {
71  int filever = -1;
72 
73  try
74  {
75  filever = m_internals->Get<int>( "meta.version" );
76  }
77  catch( ... )
78  {
79  wxLogTrace( traceSettings, "%s: nested settings version could not be read!",
80  m_filename );
81  success = false;
82  }
83 
84  if( filever >= 0 && filever < m_schemaVersion )
85  {
86  wxLogTrace( traceSettings, "%s: attempting migration from version %d to %d",
87  m_filename, filever, m_schemaVersion );
88 
89  if( !Migrate() )
90  {
91  wxLogTrace( traceSettings, "%s: migration failed!", GetFullFilename() );
92  success = false;
93  }
94  }
95  else if( filever > m_schemaVersion )
96  {
97  wxLogTrace( traceSettings,
98  "%s: warning: nested settings version %d is newer than latest (%d)",
99  m_filename, filever, m_schemaVersion );
100  }
101  else if( filever >= 0 )
102  {
103  wxLogTrace( traceSettings, "%s: schema version %d is current", m_filename, filever );
104  }
105  }
106 
107  Load();
108 
109  return success;
110 }
111 
112 
113 bool NESTED_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
114 {
115  if( !m_parent )
116  return false;
117 
118  try
119  {
120  bool modified = Store();
121 
122  auto jsonObjectInParent = m_parent->GetJson( m_path );
123 
124  if( !jsonObjectInParent )
125  modified = true;
126  else if( !nlohmann::json::diff( *m_internals, jsonObjectInParent.value() ).empty() )
127  modified = true;
128 
129  if( modified || aForce )
130  {
131  ( *m_parent->m_internals )[m_path].update( *m_internals );
132 
133  wxLogTrace( traceSettings, "Stored NESTED_SETTINGS %s with schema %d",
134  GetFilename(),
135  m_schemaVersion );
136  }
137 
138  return modified;
139  }
140  catch( ... )
141  {
142  wxLogTrace( traceSettings, "NESTED_SETTINGS %s: Could not store to %s at %s",
143  m_filename,
145  m_path );
146 
147  return false;
148  }
149 }
150 
151 
152 void NESTED_SETTINGS::SetParent( JSON_SETTINGS* aParent, bool aLoadFromFile )
153 {
154  m_parent = aParent;
155 
156  if( m_parent )
157  {
158  m_parent->AddNestedSettings( this );
159 
160  // In case we were created after the parent's ctor
161  if( aLoadFromFile )
162  LoadFromFile();
163  }
164 }
void SetParent(JSON_SETTINGS *aParent, bool aLoadFromFile=true)
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:71
SETTINGS_LOC
Definition: json_settings.h:46
OPT< 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 GetFullFilename() const
bool LoadFromFile(const wxString &aDirectory="") override
Loads the JSON document from the parent and then calls Load()
bool SaveToFile(const wxString &aDirectory="", bool aForce=false) override
Calls Store() and then saves the JSON document contents into the parent JSON_SETTINGS.
void AddNestedSettings(NESTED_SETTINGS *aSettings)
Transfers ownership of a given NESTED_SETTINGS to this object.
wxString m_filename
The filename (not including path) of this settings file (inicode)
bool Migrate()
Migrates the schema of this settings from the version in the file to the latest version.
std::unique_ptr< JSON_SETTINGS_INTERNALS > m_internals
JSON_SETTINGS * m_parent
A pointer to the parent object to load and store from.
virtual ~NESTED_SETTINGS()
Not stored in a file, but inside another JSON_SETTINGS.
NESTED_SETTINGS(const std::string &aName, int aSchemaVersion, JSON_SETTINGS *aParent, const std::string &aPath)
static bool empty(const wxTextEntryBase *aCtrl)
std::string m_path
The path (in pointer format) of where to store this document in the parent.
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.
void ReleaseNestedSettings(NESTED_SETTINGS *aSettings)
Saves and frees a nested settings object, if it exists within this one.
int m_schemaVersion
Version of this settings schema.
virtual void Load()
Updates the parameters of this object based on the current JSON document contents.