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 
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  clear();
45  bool success = false;
46 
47  if( m_parent )
48  {
49  nlohmann::json::json_pointer ptr = PointerFromString( m_path );
50 
51  if( m_parent->contains( ptr ) )
52  {
53  try
54  {
55  update( ( *m_parent )[ptr] );
56 
57  wxLogTrace( traceSettings, "Loaded NESTED_SETTINGS %s with schema %d",
59 
60  success = true;
61  }
62  catch( ... )
63  {
64  wxLogTrace( traceSettings, "NESTED_SETTINGS %s: Could not load from %s at %s",
66  }
67  }
68  }
69 
70  if( success )
71  {
72  int filever = -1;
73 
74  if( count( PointerFromString( "meta.version" ) ) )
75  {
76  try
77  {
78  filever = at( PointerFromString( "meta.version" ) ).get<int>();
79  }
80  catch( ... )
81  {
82  wxLogTrace( traceSettings, "%s: nested settings version could not be read!",
83  m_filename );
84  success = false;
85  }
86  }
87  else
88  {
89  success = false;
90  }
91 
92  if( filever >= 0 && filever < m_schemaVersion )
93  {
94  wxLogTrace( traceSettings, "%s: attempting migration from version %d to %d",
95  m_filename, filever, m_schemaVersion );
96 
97  if( !Migrate() )
98  {
99  wxLogTrace( traceSettings, "%s: migration failed!", GetFullFilename() );
100  success = false;
101  }
102  }
103  else if( filever > m_schemaVersion )
104  {
105  wxLogTrace( traceSettings,
106  "%s: warning: nested settings version %d is newer than latest (%d)",
107  m_filename, filever, m_schemaVersion );
108  }
109  }
110 
111  Load();
112 
113  return success;
114 }
115 
116 
117 bool NESTED_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
118 {
119  if( !m_parent )
120  return false;
121 
122  bool modified = Store();
123 
124  try
125  {
126  nlohmann::json patch =
127  nlohmann::json::diff( *this, ( *m_parent )[PointerFromString( m_path )] );
128  modified |= !patch.empty();
129  }
130  catch( ... )
131  {
132  modified = true;
133  }
134 
135  if( !modified && !aForce )
136  return false;
137 
138  try
139  {
140  ( *m_parent )[PointerFromString( m_path ) ].update( *this );
141 
142  wxLogTrace( traceSettings, "Stored NESTED_SETTINGS %s with schema %d",
144  }
145  catch( ... )
146  {
147  wxLogTrace( traceSettings, "NESTED_SETTINGS %s: Could not store to %s at %s",
149  }
150 
151  return modified;
152 }
153 
154 
156 {
157  m_parent = aParent;
158 
159  if( m_parent )
160  {
161  m_parent->AddNestedSettings( this );
162 
163  // In case we were created after the parent's ctor
164  LoadFromFile();
165  }
166 }
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:207
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:39
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.
void SetParent(JSON_SETTINGS *aParent)