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>
|
|
|
|
|
|
|
|
extern const char* traceSettings;
|
|
|
|
|
|
|
|
|
|
|
|
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-05-25 17:06:53 +00:00
|
|
|
bool NESTED_SETTINGS::LoadFromFile( const std::string& 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",
|
|
|
|
m_filename, m_parent->GetFilename(), m_path );
|
|
|
|
}
|
2020-04-13 14:48:43 +00:00
|
|
|
}
|
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-05-30 16:56:16 +00:00
|
|
|
bool NESTED_SETTINGS::SaveToFile( const std::string& 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
|
|
|
|
|
|
|
|
|
|
|
void NESTED_SETTINGS::SetParent( JSON_SETTINGS* aParent )
|
|
|
|
{
|
|
|
|
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
|
|
|
|
LoadFromFile();
|
|
|
|
}
|
2020-05-31 21:42:04 +00:00
|
|
|
}
|