/* * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2020 Jon Evans * 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 . */ #include #include extern const char* traceSettings; NESTED_SETTINGS::NESTED_SETTINGS( const std::string& aName, int aVersion, JSON_SETTINGS* aParent, const std::string& aPath ) : JSON_SETTINGS( aName, SETTINGS_LOC::NESTED, aVersion ), m_parent( aParent ), m_path( aPath ) { SetParent( aParent ); } NESTED_SETTINGS::~NESTED_SETTINGS() { if( m_parent ) m_parent->ReleaseNestedSettings( this ); } bool NESTED_SETTINGS::LoadFromFile( const wxString& aDirectory ) { clear(); bool success = false; if( m_parent ) { nlohmann::json::json_pointer ptr = PointerFromString( m_path ); if( m_parent->contains( ptr ) ) { 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 ); } } } Load(); return success; } bool NESTED_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce ) { if( !m_parent ) return false; 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; 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 ); } return modified; } void NESTED_SETTINGS::SetParent( JSON_SETTINGS* aParent ) { m_parent = aParent; if( m_parent ) { m_parent->AddNestedSettings( this ); // In case we were created after the parent's ctor LoadFromFile(); } }