2020-01-13 01:44:19 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 Jon Evans <jon@craftyjon.com>
|
|
|
|
* Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <wx/log.h>
|
|
|
|
|
|
|
|
#include <settings/nested_settings.h>
|
|
|
|
|
|
|
|
|
|
|
|
NESTED_SETTINGS::NESTED_SETTINGS( const std::string& aName, int aVersion, JSON_SETTINGS* aParent,
|
2020-05-30 16:56:16 +00:00
|
|
|
const std::string& aPath ) :
|
|
|
|
JSON_SETTINGS( aName, SETTINGS_LOC::NESTED, aVersion ),
|
2020-01-13 01:44:19 +00:00
|
|
|
m_parent( aParent ), m_path( aPath )
|
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
SetParent( aParent );
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-28 03:53:00 +00:00
|
|
|
NESTED_SETTINGS::~NESTED_SETTINGS()
|
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
if( m_parent )
|
|
|
|
m_parent->ReleaseNestedSettings( this );
|
2020-02-28 03:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-02 09:23:21 +00:00
|
|
|
bool NESTED_SETTINGS::LoadFromFile( const wxString& aDirectory )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
|
|
|
clear();
|
2020-05-25 17:06:53 +00:00
|
|
|
bool success = false;
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2020-04-13 14:48:43 +00:00
|
|
|
if( m_parent )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2020-06-20 14:36:16 +00:00
|
|
|
nlohmann::json::json_pointer ptr = PointerFromString( m_path );
|
2020-05-25 17:06:53 +00:00
|
|
|
|
2020-06-20 14:36:16 +00:00
|
|
|
if( m_parent->contains( ptr ) )
|
2020-04-13 14:48:43 +00:00
|
|
|
{
|
2020-06-20 14:36:16 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
update( ( *m_parent )[ptr] );
|
|
|
|
|
|
|
|
wxLogTrace( traceSettings, "Loaded NESTED_SETTINGS %s with schema %d",
|
|
|
|
GetFilename(), m_schemaVersion );
|
|
|
|
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings, "NESTED_SETTINGS %s: Could not load from %s at %s",
|
2020-09-11 01:28:49 +00:00
|
|
|
m_filename, m_parent->GetFilename(), m_path );
|
2020-06-20 14:36:16 +00:00
|
|
|
}
|
2020-04-13 14:48:43 +00:00
|
|
|
}
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 01:28:49 +00:00
|
|
|
if( success )
|
|
|
|
{
|
|
|
|
int filever = -1;
|
|
|
|
|
2020-09-19 00:41:16 +00:00
|
|
|
if( count( PointerFromString( "meta.version" ) ) )
|
2020-09-11 01:28:49 +00:00
|
|
|
{
|
2020-09-19 00:41:16 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
filever = at( PointerFromString( "meta.version" ) ).get<int>();
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings, "%s: nested settings version could not be read!",
|
|
|
|
m_filename );
|
|
|
|
success = false;
|
|
|
|
}
|
2020-09-11 01:28:49 +00:00
|
|
|
}
|
2020-09-19 00:41:16 +00:00
|
|
|
else
|
2020-09-11 01:28:49 +00:00
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( filever >= 0 && filever < m_schemaVersion )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings, "%s: attempting migration from version %d to %d",
|
|
|
|
m_filename, filever, m_schemaVersion );
|
|
|
|
|
|
|
|
if( !Migrate() )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings, "%s: migration failed!", GetFullFilename() );
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( filever > m_schemaVersion )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings,
|
|
|
|
"%s: warning: nested settings version %d is newer than latest (%d)",
|
|
|
|
m_filename, filever, m_schemaVersion );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
Load();
|
2020-05-25 17:06:53 +00:00
|
|
|
|
|
|
|
return success;
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-02 09:23:21 +00:00
|
|
|
bool NESTED_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
if( !m_parent )
|
|
|
|
return false;
|
|
|
|
|
2020-05-30 16:56:16 +00:00
|
|
|
bool modified = Store();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
nlohmann::json patch =
|
|
|
|
nlohmann::json::diff( *this, ( *m_parent )[PointerFromString( m_path )] );
|
|
|
|
modified |= !patch.empty();
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !modified && !aForce )
|
|
|
|
return false;
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
( *m_parent )[PointerFromString( m_path ) ].update( *this );
|
|
|
|
|
|
|
|
wxLogTrace( traceSettings, "Stored NESTED_SETTINGS %s with schema %d",
|
|
|
|
GetFilename(), m_schemaVersion );
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings, "NESTED_SETTINGS %s: Could not store to %s at %s",
|
|
|
|
m_filename, m_parent->GetFilename(), m_path );
|
|
|
|
}
|
2020-05-30 16:56:16 +00:00
|
|
|
|
|
|
|
return modified;
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
2020-05-31 21:42:04 +00:00
|
|
|
|
|
|
|
|
2021-01-27 02:51:36 +00:00
|
|
|
void NESTED_SETTINGS::SetParent( JSON_SETTINGS* aParent, bool aLoadFromFile )
|
2020-05-31 21:42:04 +00:00
|
|
|
{
|
|
|
|
m_parent = aParent;
|
|
|
|
|
|
|
|
if( m_parent )
|
2020-06-08 02:19:46 +00:00
|
|
|
{
|
2020-05-31 21:42:04 +00:00
|
|
|
m_parent->AddNestedSettings( this );
|
|
|
|
|
2020-06-08 02:19:46 +00:00
|
|
|
// In case we were created after the parent's ctor
|
2021-01-27 02:51:36 +00:00
|
|
|
if( aLoadFromFile )
|
|
|
|
LoadFromFile();
|
2020-06-08 02:19:46 +00:00
|
|
|
}
|
2020-05-31 21:42:04 +00:00
|
|
|
}
|