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>
|
2023-12-19 19:29:17 +00:00
|
|
|
* Copyright (C) 2020, 2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-01-13 01:44:19 +00:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2021-06-04 03:52:50 +00:00
|
|
|
#include <settings/json_settings_internals.h>
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <settings/nested_settings.h>
|
2022-08-23 18:38:16 +00:00
|
|
|
#include <locale_io.h>
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2023-12-19 19:29:17 +00:00
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
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
|
|
|
{
|
2021-06-04 03:52:50 +00:00
|
|
|
m_internals->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
|
|
|
{
|
2021-06-04 03:52:50 +00:00
|
|
|
nlohmann::json::json_pointer ptr = m_internals->PointerFromString( m_path );
|
2020-05-25 17:06:53 +00:00
|
|
|
|
2021-06-04 03:52:50 +00:00
|
|
|
if( m_parent->m_internals->contains( ptr ) )
|
2020-04-13 14:48:43 +00:00
|
|
|
{
|
2020-06-20 14:36:16 +00:00
|
|
|
try
|
|
|
|
{
|
2021-06-06 12:38:48 +00:00
|
|
|
m_internals->update( m_parent->m_internals->at( ptr ) );
|
2020-06-20 14:36:16 +00:00
|
|
|
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "Loaded NESTED_SETTINGS %s" ), GetFilename() );
|
2020-06-20 14:36:16 +00:00
|
|
|
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
2023-12-19 19:29:17 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "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;
|
|
|
|
|
2021-02-28 19:04:28 +00:00
|
|
|
try
|
2020-09-11 01:28:49 +00:00
|
|
|
{
|
2021-06-04 03:52:50 +00:00
|
|
|
filever = m_internals->Get<int>( "meta.version" );
|
2020-09-11 01:28:49 +00:00
|
|
|
}
|
2021-02-28 19:04:28 +00:00
|
|
|
catch( ... )
|
2020-09-11 01:28:49 +00:00
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "%s: nested settings version could not be read!" ),
|
2021-02-28 19:04:28 +00:00
|
|
|
m_filename );
|
2020-09-11 01:28:49 +00:00
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( filever >= 0 && filever < m_schemaVersion )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "%s: attempting migration from version %d to %d" ),
|
2020-09-11 01:28:49 +00:00
|
|
|
m_filename, filever, m_schemaVersion );
|
|
|
|
|
|
|
|
if( !Migrate() )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "%s: migration failed!" ), GetFullFilename() );
|
2020-09-11 01:28:49 +00:00
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( filever > m_schemaVersion )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceSettings,
|
2022-02-03 20:57:26 +00:00
|
|
|
wxT( "%s: warning: nested settings version %d is newer than latest (%d)" ),
|
2020-09-11 01:28:49 +00:00
|
|
|
m_filename, filever, m_schemaVersion );
|
|
|
|
}
|
2021-02-28 19:04:28 +00:00
|
|
|
else if( filever >= 0 )
|
|
|
|
{
|
2022-02-03 20:57:26 +00:00
|
|
|
wxLogTrace( traceSettings, wxT( "%s: schema version %d is current" ),
|
|
|
|
m_filename, filever );
|
2021-02-28 19:04:28 +00:00
|
|
|
}
|
2020-09-11 01:28:49 +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-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;
|
|
|
|
|
2022-08-23 18:38:16 +00:00
|
|
|
LOCALE_IO dummy;
|
|
|
|
|
2020-05-30 16:56:16 +00:00
|
|
|
try
|
|
|
|
{
|
2021-06-14 22:15:08 +00:00
|
|
|
bool modified = Store();
|
2021-06-04 03:52:50 +00:00
|
|
|
|
2021-06-14 22:15:08 +00:00
|
|
|
auto jsonObjectInParent = m_parent->GetJson( m_path );
|
2020-05-30 16:56:16 +00:00
|
|
|
|
2021-06-14 22:15:08 +00:00
|
|
|
if( !jsonObjectInParent )
|
|
|
|
modified = true;
|
|
|
|
else if( !nlohmann::json::diff( *m_internals, jsonObjectInParent.value() ).empty() )
|
|
|
|
modified = true;
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2021-06-14 22:15:08 +00:00
|
|
|
if( modified || aForce )
|
|
|
|
{
|
|
|
|
( *m_parent->m_internals )[m_path].update( *m_internals );
|
|
|
|
|
2023-01-17 12:42:30 +00:00
|
|
|
wxLogTrace( traceSettings, wxS( "Stored NESTED_SETTINGS %s with schema %d" ),
|
2021-06-14 22:15:08 +00:00
|
|
|
GetFilename(),
|
|
|
|
m_schemaVersion );
|
|
|
|
}
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2021-06-14 22:15:08 +00:00
|
|
|
return modified;
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
2023-01-17 12:42:30 +00:00
|
|
|
wxLogTrace( traceSettings, wxS( "NESTED_SETTINGS %s: Could not store to %s at %s" ),
|
2021-06-14 22:15:08 +00:00
|
|
|
m_filename,
|
|
|
|
m_parent->GetFilename(),
|
|
|
|
m_path );
|
2020-05-30 16:56:16 +00:00
|
|
|
|
2021-06-14 22:15:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
}
|