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 
24 
25 NESTED_SETTINGS::NESTED_SETTINGS( const std::string& aName, int aVersion, JSON_SETTINGS* aParent,
26  const std::string& aPath ) :
27  JSON_SETTINGS( aName, SETTINGS_LOC::NESTED, aVersion ),
28  m_parent( aParent ), m_path( aPath )
29 {
30  SetParent( aParent );
31 }
32 
33 
35 {
36  if( m_parent )
38 }
39 
40 
41 bool NESTED_SETTINGS::LoadFromFile( const wxString& aDirectory )
42 {
43  clear();
44  bool success = false;
45 
46  if( m_parent )
47  {
48  nlohmann::json::json_pointer ptr = PointerFromString( m_path );
49 
50  if( m_parent->contains( ptr ) )
51  {
52  try
53  {
54  update( ( *m_parent )[ptr] );
55 
56  wxLogTrace( traceSettings, "Loaded NESTED_SETTINGS %s", GetFilename() );
57 
58  success = true;
59  }
60  catch( ... )
61  {
62  wxLogTrace( traceSettings, "NESTED_SETTINGS %s: Could not load from %s at %s",
64  }
65  }
66  }
67 
68  if( success )
69  {
70  int filever = -1;
71 
72  try
73  {
74  filever = at( PointerFromString( "meta.version" ) ).get<int>();
75  }
76  catch( ... )
77  {
78  wxLogTrace( traceSettings, "%s: nested settings version could not be read!",
79  m_filename );
80  success = false;
81  }
82 
83  if( filever >= 0 && filever < m_schemaVersion )
84  {
85  wxLogTrace( traceSettings, "%s: attempting migration from version %d to %d",
86  m_filename, filever, m_schemaVersion );
87 
88  if( !Migrate() )
89  {
90  wxLogTrace( traceSettings, "%s: migration failed!", GetFullFilename() );
91  success = false;
92  }
93  }
94  else if( filever > m_schemaVersion )
95  {
96  wxLogTrace( traceSettings,
97  "%s: warning: nested settings version %d is newer than latest (%d)",
98  m_filename, filever, m_schemaVersion );
99  }
100  else if( filever >= 0 )
101  {
102  wxLogTrace( traceSettings, "%s: schema version %d is current", m_filename, filever );
103  }
104  }
105 
106  Load();
107 
108  return success;
109 }
110 
111 
112 bool NESTED_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
113 {
114  if( !m_parent )
115  return false;
116 
117  bool modified = Store();
118 
119  try
120  {
121  nlohmann::json patch =
122  nlohmann::json::diff( *this, ( *m_parent )[PointerFromString( m_path )] );
123  modified |= !patch.empty();
124  }
125  catch( ... )
126  {
127  modified = true;
128  }
129 
130  if( !modified && !aForce )
131  return false;
132 
133  try
134  {
135  ( *m_parent )[PointerFromString( m_path ) ].update( *this );
136 
137  wxLogTrace( traceSettings, "Stored NESTED_SETTINGS %s with schema %d",
139  }
140  catch( ... )
141  {
142  wxLogTrace( traceSettings, "NESTED_SETTINGS %s: Could not store to %s at %s",
144  }
145 
146  return modified;
147 }
148 
149 
150 void NESTED_SETTINGS::SetParent( JSON_SETTINGS* aParent, bool aLoadFromFile )
151 {
152  m_parent = aParent;
153 
154  if( m_parent )
155  {
156  m_parent->AddNestedSettings( this );
157 
158  // In case we were created after the parent's ctor
159  if( aLoadFromFile )
160  LoadFromFile();
161  }
162 }
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:64
void clear(int aType=UNDEFINED_TYPE)
Definition: multivector.h:212
SETTINGS_LOC
Definition: json_settings.h:44
wxString GetFullFilename() const
bool LoadFromFile(const wxString &aDirectory="") override
Loads the JSON document from the parent and then calls Load()
nlohmann::json json
Definition: gerbview.cpp:40
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.
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)
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.
static nlohmann::json::json_pointer PointerFromString(std::string aPath)
Builds a JSON pointer based on a given string.
int m_schemaVersion
Version of this settings schema.
virtual void Load()
Updates the parameters of this object based on the current JSON document contents.