KiCad PCB EDA Suite
Loading...
Searching...
No Matches
json_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 <[email protected]>
5 * Copyright The 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
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <algorithm>
22#include <fstream>
23#include <iomanip>
24#include <utility>
25#include <sstream>
26
27#include <kiplatform/io.h>
28#include <locale_io.h>
29#include <gal/color4d.h>
33#include <settings/parameters.h>
37#include <wx/aui/framemanager.h>
38#include <wx/config.h>
39#include <wx/debug.h>
40#include <wx/fileconf.h>
41#include <wx/filename.h>
42#include <wx/gdicmn.h>
43#include <wx/log.h>
44#include <wx/stdstream.h>
45#include <wx/wfstream.h>
46
47
48nlohmann::json::json_pointer JSON_SETTINGS_INTERNALS::PointerFromString( std::string aPath )
49{
50 std::replace( aPath.begin(), aPath.end(), '.', '/' );
51 aPath.insert( 0, "/" );
52
53 nlohmann::json::json_pointer p;
54
55 try
56 {
57 p = nlohmann::json::json_pointer( aPath );
58 }
59 catch( ... )
60 {
61 wxASSERT_MSG( false, wxT( "Invalid pointer path in PointerFromString!" ) );
62 }
63
64 return p;
65}
66
67
68JSON_SETTINGS::JSON_SETTINGS( const wxString& aFilename, SETTINGS_LOC aLocation,
69 int aSchemaVersion, bool aCreateIfMissing, bool aCreateIfDefault,
70 bool aWriteFile ) :
71 m_filename( aFilename ),
73 m_location( aLocation ),
74 m_createIfMissing( aCreateIfMissing ),
75 m_createIfDefault( aCreateIfDefault ),
76 m_writeFile( aWriteFile ),
77 m_modified( false ),
78 m_fileSynced( false ),
79 m_lastSaveFailed( false ),
82 m_schemaVersion( aSchemaVersion ),
83 m_isFutureFormat( false ),
84 m_manager( nullptr )
85{
86 m_internals = std::make_unique<JSON_SETTINGS_INTERNALS>();
87
88 try
89 {
90 m_internals->SetFromString( "meta.filename", GetFullFilename() );
91 }
92 catch( ... )
93 {
94 wxLogTrace( traceSettings, wxT( "Error: Could not create filename field for %s" ), GetFullFilename() );
95 }
96
97
98 m_params.emplace_back( new PARAM<int>( "meta.version", &m_schemaVersion, m_schemaVersion, true ) );
99}
100
101
103{
104 // ReleaseNestedSettings erases from m_nested_settings, so snapshot first to avoid
105 // iterating an invalidated container
106 std::vector<NESTED_SETTINGS*> nestedToRelease( m_nested_settings );
107
108 for( NESTED_SETTINGS* nested : nestedToRelease )
109 ReleaseNestedSettings( nested );
110
111 for( PARAM_BASE* param: m_params )
112 delete param;
113
114 m_params.clear();
115}
116
117
119{
120 if( m_filename.AfterLast( '.' ) == getFileExt() )
121 return m_filename;
122
123 return wxString( m_filename + "." + getFileExt() );
124}
125
126
127nlohmann::json& JSON_SETTINGS::At( const std::string& aPath )
128{
129 return m_internals->At( aPath );
130}
131
132
133bool JSON_SETTINGS::Contains( const std::string& aPath ) const
134{
135 return m_internals->contains( JSON_SETTINGS_INTERNALS::PointerFromString( aPath ) );
136}
137
138
143
144
146{
147 for( PARAM_BASE* param : m_params )
148 {
149 try
150 {
151 param->Load( *this, m_resetParamsIfMissing );
152 }
153 catch( ... )
154 {
155 // Skip unreadable parameters in file
156 wxLogTrace( traceSettings, wxT( "param '%s' load err" ), param->GetJsonPath().c_str() );
157 }
158 }
159}
160
161
162bool JSON_SETTINGS::LoadFromFile( const wxString& aDirectory )
163{
164 // First, load all params to default values
165 m_internals->clear();
166 Load();
167
168 bool success = true;
169 bool migrated = false;
170 bool legacy_migrated = false;
171
172 LOCALE_IO locale;
173
174 auto migrateFromLegacy =
175 [&] ( wxFileName& aPath )
176 {
177 // Backup and restore during migration so that the original can be mutated if
178 // convenient
179 bool backed_up = false;
180 wxFileName temp;
181
182 if( aPath.IsDirWritable() )
183 {
184 temp.AssignTempFileName( aPath.GetFullPath() );
185
186 if( !wxCopyFile( aPath.GetFullPath(), temp.GetFullPath() ) )
187 {
188 wxLogTrace( traceSettings, wxT( "%s: could not create temp file for migration" ),
189 GetFullFilename() );
190 }
191 else
192 {
193 backed_up = true;
194 }
195 }
196
197 // Silence popups if legacy file is read-only
198 wxLogNull doNotLog;
199
200 wxConfigBase::DontCreateOnDemand();
201 auto cfg = std::make_unique<wxFileConfig>( wxT( "" ), wxT( "" ), aPath.GetFullPath() );
202
203 // If migrate fails or is not implemented, fall back to built-in defaults that
204 // were already loaded above
205 if( !MigrateFromLegacy( cfg.get() ) )
206 {
207 success = false;
208 wxLogTrace( traceSettings, wxT( "%s: migrated; not all settings were found in legacy file" ),
209 GetFullFilename() );
210 }
211 else
212 {
213 success = true;
214 wxLogTrace( traceSettings, wxT( "%s: migrated from legacy format" ), GetFullFilename() );
215 }
216
217 if( backed_up )
218 {
219 cfg.reset();
220
221 if( !wxCopyFile( temp.GetFullPath(), aPath.GetFullPath() ) )
222 {
223 wxLogTrace( traceSettings, wxT( "migrate; copy temp file %s to %s failed" ),
224 temp.GetFullPath(), aPath.GetFullPath() );
225 }
226
227 if( !wxRemoveFile( temp.GetFullPath() ) )
228 {
229 wxLogTrace( traceSettings, wxT( "migrate; failed to remove temp file %s" ),
230 temp.GetFullPath() );
231 }
232 }
233
234 // Either way, we want to clean up the old file afterwards
235 legacy_migrated = true;
236 };
237
238 wxFileName path;
239
240 if( aDirectory.empty() )
241 {
242 path.Assign( m_filename );
243 path.SetExt( getFileExt() );
244 }
245 else
246 {
247 path.Assign( aDirectory, m_filename, getFileExt() );
248 }
249
250 if( !path.Exists() )
251 {
252 // Case 1: legacy migration, no .json extension yet
253 path.SetExt( getLegacyFileExt() );
254
255 if( path.Exists() )
256 {
257 migrateFromLegacy( path );
258 }
259 // Case 2: legacy filename is different from new one
260 else if( !m_legacy_filename.empty() )
261 {
262 path.SetName( m_legacy_filename );
263
264 if( path.Exists() )
265 migrateFromLegacy( path );
266 }
267 else
268 {
269 success = false;
270 }
271 }
272 else
273 {
274 if( !path.IsFileWritable() )
275 m_writeFile = false;
276
277 bool parseError = false;
278 bool parsed = parseAndMigrate(
279 [&]( nlohmann::json& aDoc )
280 {
281 wxFFileInputStream fp( path.GetFullPath(), wxT( "rt" ) );
282 wxStdInputStream fstream( fp );
283
284 if( !fp.IsOk() )
285 {
286 wxLogTrace( traceSettings, wxT( "%s exists but can't be opened for read" ),
287 GetFullFilename() );
288 return false;
289 }
290
291 aDoc = nlohmann::json::parse( fstream, nullptr, ALLOW_EXCEPTIONS, IGNORE_COMMENTS );
292 return true;
293 },
294 migrated, parseError );
295
296 if( !parsed )
297 success = false;
298
299 if( parseError )
300 {
301 wxLogTrace( traceSettings, wxT( "Attempting migration in case file is in legacy format" ) );
302 migrateFromLegacy( path );
303 }
304 }
305
306 // Now that we have new data in the JSON structure, load the params again
307 Load();
308
309 // And finally load any nested settings
310 for( NESTED_SETTINGS* settings : m_nested_settings )
311 settings->LoadFromFile();
312
313 wxLogTrace( traceSettings, wxT( "Loaded <%s> with schema %d" ), GetFullFilename(), m_schemaVersion );
314
315 m_modified = false;
316
317 // A missing backing file leaves the store empty, which must not count as synchronized
318 m_fileSynced = success;
319
320 // Skip writeback when running QA tests
321 bool doWrite = m_writeFile && !wxGetEnv( wxT( "KICAD_INHIBIT_SETTINGS_WRITES" ), nullptr );
322
323 // If we migrated, clean up the legacy file (with no extension). Save the migrated
324 // contents FIRST so that if the save fails we still have the legacy file on disk to
325 // fall back to -- otherwise a crash mid-migration leaves the user with neither copy.
326 if( doWrite && ( legacy_migrated || migrated ) )
327 {
329 {
330 bool savedMigrated = SaveToFile( aDirectory, true );
331
332 if( savedMigrated && legacy_migrated && !wxRemoveFile( path.GetFullPath() ) )
333 {
334 wxLogTrace( traceSettings, wxT( "Warning: could not remove legacy file %s" ), path.GetFullPath() );
335 }
336 else if( !savedMigrated )
337 {
338 wxLogTrace( traceSettings, wxT( "Migrated save of %s failed; leaving legacy file intact" ),
339 GetFullFilename() );
340 }
341 }
342 }
343
344 return success;
345}
346
347
348bool JSON_SETTINGS::parseAndMigrate( const std::function<bool( nlohmann::json& )>& aParse, bool& aMigrated,
349 bool& aParseError )
350{
351 bool success = true;
352
353 try
354 {
355 nlohmann::json& doc = *static_cast<nlohmann::json*>( m_internals.get() );
356
357 if( !aParse( doc ) )
358 return true;
359
360 // Save whatever we loaded, before doing any migration etc
361 m_internals->m_original = doc;
362
363 // If parse succeeds, check if schema migration is required
364 int filever = -1;
365
366 try
367 {
368 filever = m_internals->Get<int>( "meta.version" );
369 }
370 catch( ... )
371 {
372 wxLogTrace( traceSettings, wxT( "%s: file version could not be read!" ), GetFullFilename() );
373 success = false;
374 }
375
376 if( filever >= 0 && filever < m_schemaVersion )
377 {
378 wxLogTrace( traceSettings, wxT( "%s: attempting migration from version %d to %d" ), GetFullFilename(),
379 filever, m_schemaVersion );
380
381 if( Migrate() )
382 aMigrated = true;
383 else
384 wxLogTrace( traceSettings, wxT( "%s: migration failed!" ), GetFullFilename() );
385 }
386 else if( filever > m_schemaVersion )
387 {
388 wxLogTrace( traceSettings, wxT( "%s: warning: file version %d is newer than latest (%d)" ),
389 GetFullFilename(), filever, m_schemaVersion );
390 m_isFutureFormat = true;
391 }
392 }
393 catch( nlohmann::json::parse_error& error )
394 {
395 success = false;
396 aParseError = true;
397
398 // The message text can carry a token of the document, which may be a password
399 wxLogTrace( traceSettings, wxT( "Json parse error reading %s (id %d, byte %d)" ), GetFullFilename(),
400 error.id, static_cast<int>( error.byte ) );
401 }
402
403 return success;
404}
405
406
407bool JSON_SETTINGS::LoadFromBuffer( const std::string& aContents )
408{
409 m_internals->clear();
410 Load();
411
412 // No LOCALE_IO here, this runs on library load threads and a wxLocale would race the GUI thread
413 bool migrated = false;
414 bool parseError = false;
415
416 bool success = parseAndMigrate(
417 [&]( nlohmann::json& aDoc )
418 {
419 aDoc = nlohmann::json::parse( aContents, nullptr, ALLOW_EXCEPTIONS, IGNORE_COMMENTS );
420 return true;
421 },
422 migrated, parseError );
423
424 Load();
425
426 m_modified = false;
427 m_fileSynced = success;
428
429 return success;
430}
431
432
434{
435 for( PARAM_BASE* param : m_params )
436 {
437 try
438 {
439 m_modified |= !param->MatchesFile( *this );
440 param->Store( this );
441 }
442 catch( const std::exception& e )
443 {
444 wxLogTrace( traceSettings, wxT( "param '%s' store err: %s" ), param->GetJsonPath().c_str(), e.what() );
445 }
446 }
447
448 return m_modified;
449}
450
451
453{
454 for( PARAM_BASE* param : m_params )
455 param->SetDefault();
456}
457
458
459std::map<std::string, nlohmann::json> JSON_SETTINGS::GetFileHistories()
460{
461 std::map<std::string, nlohmann::json> histories;
462
463 for( const std::string& candidate : { std::string( "system.file_history" ) } )
464 {
465 if( Contains( candidate ) )
466 histories[candidate] = GetJson( candidate ).value();
467 }
468
469 return histories;
470}
471
472
473bool JSON_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
474{
475 m_lastSaveFailed = false;
476
477 if( !m_writeFile )
478 return false;
479
480 // Default PROJECT won't have a filename set
481 if( m_filename.IsEmpty() )
482 return false;
483
484 wxFileName path;
485
486 if( aDirectory.empty() )
487 {
488 path.Assign( m_filename );
489 path.SetExt( getFileExt() );
490 }
491 else
492 {
493 wxString dir( aDirectory );
494 path.Assign( dir, m_filename, getFileExt() );
495 }
496
497 if( !m_createIfMissing && !path.FileExists() )
498 {
499 wxLogTrace( traceSettings,
500 wxT( "File for %s doesn't exist and m_createIfMissing == false; not saving" ),
501 GetFullFilename() );
502 return false;
503 }
504
505 bool modified = flushToStore();
506
507 if( !modified && !aForce && path.FileExists() )
508 {
509 wxLogTrace( traceSettings, wxT( "%s contents not modified, skipping save" ), GetFullFilename() );
510 return false;
511 }
512 else if( !modified && !aForce && !m_createIfDefault )
513 {
514 wxLogTrace( traceSettings, wxT( "%s contents still default and m_createIfDefault == false; not saving" ),
515 GetFullFilename() );
516 return false;
517 }
518
519 wxLogTrace( traceSettings, wxT( "Saving %s" ), GetFullFilename() );
520
521 bool success = true;
522
523 try
524 {
525 std::string payload = formatFileContents();
526 wxString writeError;
527
528 // Last-chance skip for the case where the dirty heuristic fired but the serialized payload
529 // still equals the on-disk bytes (e.g. key reordering or normalization). Avoids bumping the
530 // project file timestamps for a no-op rewrite; see #24402. A genuine change yields a
531 // differing payload and is always written.
532 if( !aForce && path.FileExists() )
533 {
534 std::ifstream existing( path.GetFullPath().fn_str(), std::ios::in | std::ios::binary );
535
536 if( existing )
537 {
538 std::string current( ( std::istreambuf_iterator<char>( existing ) ),
539 std::istreambuf_iterator<char>() );
540
541 // Only trust an equal comparison from a clean read; on any read error fall through
542 // and write, preferring data safety over avoiding a rewrite.
543 if( !existing.bad() && current == payload )
544 {
545 wxLogTrace( traceSettings, wxT( "%s on-disk contents match payload, skipping write" ),
546 GetFullFilename() );
547
548 m_modified = false;
549
550 return false;
551 }
552 }
553 }
554
555 // The destination is only checked once there is something to write, so that a store with
556 // no changes is skipped rather than reported as a failure to save
557 if( !path.DirExists() && !path.Mkdir() )
558 {
559 wxLogTrace( traceSettings, wxT( "Warning: could not create path %s, can't save %s" ),
560 path.GetPath(), GetFullFilename() );
561 success = false;
562 }
563 else if( ( path.FileExists() && !path.IsFileWritable() )
564 || ( !path.FileExists() && !path.IsDirWritable() ) )
565 {
566 wxLogTrace( traceSettings, wxT( "File for %s is read-only; not saving" ), GetFullFilename() );
567 success = false;
568 }
569 else if( !KIPLATFORM::IO::AtomicWriteFile( path.GetFullPath(), payload.data(), payload.size(),
570 &writeError ) )
571 {
572 wxLogTrace( traceSettings, wxT( "Warning: could not save %s: %s" ), GetFullFilename(), writeError );
573 success = false;
574 }
575 }
576 catch( nlohmann::json::exception& error )
577 {
578 wxLogTrace( traceSettings, wxT( "Catch error: could not save %s. Json error %s" ), GetFullFilename(),
579 error.what() );
580 success = false;
581 }
582 catch( ... )
583 {
584 wxLogTrace( traceSettings, wxT( "Error: could not save %s." ) );
585 success = false;
586 }
587
588 if( success )
589 {
590 m_modified = false;
591 m_fileSynced = true;
592 }
593
594 m_lastSaveFailed = !success;
595
596 return success;
597}
598
599
601{
602 bool modified = false;
603
604 for( NESTED_SETTINGS* settings : m_nested_settings )
605 {
606 wxCHECK2( settings, continue );
607
608 modified |= settings->SaveToFile();
609 }
610
611 modified |= Store();
612
613 return modified;
614}
615
616
617const nlohmann::json& JSON_SETTINGS::originalJson() const
618{
619 return m_internals->m_original;
620}
621
622
624{
626
627 nlohmann::json toSave = m_internals->m_original;
628
629 for( PARAM_BASE* param : m_params )
630 {
631 if( param->ClearUnknownKeys() )
632 {
633 nlohmann::json_pointer p = JSON_SETTINGS_INTERNALS::PointerFromString( param->GetJsonPath() );
634
635 toSave[p] = nlohmann::json::object();
636 }
637 }
638
639 toSave.update( m_internals->begin(), m_internals->end(), /* merge_objects = */ true );
640
641 return toSave.dump( 2 ) + '\n';
642}
643
644
646{
647 Store();
648
650
651 std::stringstream buffer;
652 buffer << std::setw( 2 ) << *m_internals << std::endl;
653
654 return buffer.str();
655}
656
657
658bool JSON_SETTINGS::LoadFromRawFile( const wxString& aPath )
659{
660 try
661 {
662 wxFFileInputStream fp( aPath, wxT( "rt" ) );
663 wxStdInputStream fstream( fp );
664
665 if( fp.IsOk() )
666 {
667 *static_cast<nlohmann::json*>( m_internals.get() ) = nlohmann::json::parse( fstream, nullptr,
670 }
671 else
672 {
673 return false;
674 }
675 }
676 catch( nlohmann::json::parse_error& error )
677 {
678 wxLogTrace( traceSettings, wxT( "Json parse error reading %s (id %d, byte %d)" ), aPath, error.id,
679 static_cast<int>( error.byte ) );
680
681 return false;
682 }
683
684 // Now that we have new data in the JSON structure, load the params again
685 Load();
686 return true;
687}
688
689
690std::optional<nlohmann::json> JSON_SETTINGS::GetJson( const std::string& aPath ) const
691{
692 nlohmann::json::json_pointer ptr = m_internals->PointerFromString( aPath );
693
694 if( m_internals->contains( ptr ) )
695 {
696 try
697 {
698 return std::optional<nlohmann::json>{ m_internals->at( ptr ) };
699 }
700 catch( ... )
701 {
702 }
703 }
704
705 return std::optional<nlohmann::json>{};
706}
707
708
709template<typename ValueType>
710std::optional<ValueType> JSON_SETTINGS::Get( const std::string& aPath ) const
711{
712 if( std::optional<nlohmann::json> ret = GetJson( aPath ) )
713 {
714 try
715 {
716 return ret->get<ValueType>();
717 }
718 catch( ... )
719 {
720 }
721 }
722
723 return std::nullopt;
724}
725
726
727// Instantiate all required templates here to allow reducing scope of json.hpp
728template KICOMMON_API std::optional<bool>
729 JSON_SETTINGS::Get<bool>( const std::string& aPath ) const;
730template KICOMMON_API std::optional<double>
731 JSON_SETTINGS::Get<double>( const std::string& aPath ) const;
732template KICOMMON_API std::optional<float>
733 JSON_SETTINGS::Get<float>( const std::string& aPath ) const;
734template KICOMMON_API std::optional<int> JSON_SETTINGS::Get<int>( const std::string& aPath ) const;
735template KICOMMON_API std::optional<unsigned int>
736 JSON_SETTINGS::Get<unsigned int>( const std::string& aPath ) const;
737template KICOMMON_API std::optional<unsigned long long>
738 JSON_SETTINGS::Get<unsigned long long>( const std::string& aPath ) const;
739template KICOMMON_API std::optional<std::string>
740 JSON_SETTINGS::Get<std::string>( const std::string& aPath ) const;
741template KICOMMON_API std::optional<nlohmann::json>
742 JSON_SETTINGS::Get<nlohmann::json>( const std::string& aPath ) const;
743template KICOMMON_API std::optional<KIGFX::COLOR4D>
744 JSON_SETTINGS::Get<KIGFX::COLOR4D>( const std::string& aPath ) const;
745template KICOMMON_API std::optional<BOM_FIELD>
746 JSON_SETTINGS::Get<BOM_FIELD>( const std::string& aPath ) const;
747template KICOMMON_API std::optional<BOM_PRESET>
748 JSON_SETTINGS::Get<BOM_PRESET>( const std::string& aPath ) const;
749template KICOMMON_API std::optional<BOM_FMT_PRESET>
750 JSON_SETTINGS::Get<BOM_FMT_PRESET>( const std::string& aPath ) const;
751template KICOMMON_API std::optional<GRID>
752 JSON_SETTINGS::Get<GRID>( const std::string& aPath ) const;
753template KICOMMON_API std::optional<wxPoint>
754 JSON_SETTINGS::Get<wxPoint>( const std::string& aPath ) const;
755template KICOMMON_API std::optional<wxSize>
756 JSON_SETTINGS::Get<wxSize>( const std::string& aPath ) const;
757template KICOMMON_API std::optional<wxRect>
758 JSON_SETTINGS::Get<wxRect>( const std::string& aPath ) const;
759template KICOMMON_API std::optional<wxAuiPaneInfo>
760 JSON_SETTINGS::Get<wxAuiPaneInfo>( const std::string& aPath ) const;
761template KICOMMON_API std::optional<KIGFX::CROSS_HAIR_MODE>
762 JSON_SETTINGS::Get<KIGFX::CROSS_HAIR_MODE>( const std::string& aPath ) const;
763
764template<typename ValueType>
765void JSON_SETTINGS::Set( const std::string& aPath, ValueType aVal )
766{
767 m_internals->SetFromString( aPath, std::move( aVal ) );
768}
769
770
771// Instantiate all required templates here to allow reducing scope of json.hpp
772template KICOMMON_API void JSON_SETTINGS::Set<bool>( const std::string& aPath, bool aValue );
773template KICOMMON_API void JSON_SETTINGS::Set<double>( const std::string& aPath, double aValue );
774template KICOMMON_API void JSON_SETTINGS::Set<float>( const std::string& aPath, float aValue );
775template KICOMMON_API void JSON_SETTINGS::Set<int>( const std::string& aPath, int aValue );
776template KICOMMON_API void JSON_SETTINGS::Set<unsigned int>( const std::string& aPath,
777 unsigned int aValue );
778template KICOMMON_API void JSON_SETTINGS::Set<unsigned long long>( const std::string& aPath,
779 unsigned long long aValue );
780template KICOMMON_API void JSON_SETTINGS::Set<const char*>( const std::string& aPath,
781 const char* aValue );
782template KICOMMON_API void JSON_SETTINGS::Set<std::string>( const std::string& aPath,
783 std::string aValue );
784template KICOMMON_API void JSON_SETTINGS::Set<nlohmann::json>( const std::string& aPath,
785 nlohmann::json aValue );
786template KICOMMON_API void JSON_SETTINGS::Set<KIGFX::COLOR4D>( const std::string& aPath,
787 KIGFX::COLOR4D aValue );
788template KICOMMON_API void JSON_SETTINGS::Set<BOM_FIELD>( const std::string& aPath,
789 BOM_FIELD aValue );
790template KICOMMON_API void JSON_SETTINGS::Set<BOM_PRESET>( const std::string& aPath,
791 BOM_PRESET aValue );
792template KICOMMON_API void JSON_SETTINGS::Set<BOM_FMT_PRESET>( const std::string& aPath,
793 BOM_FMT_PRESET aValue );
794template KICOMMON_API void JSON_SETTINGS::Set<GRID>( const std::string& aPath, GRID aValue );
795template KICOMMON_API void JSON_SETTINGS::Set<wxPoint>( const std::string& aPath, wxPoint aValue );
796template KICOMMON_API void JSON_SETTINGS::Set<wxSize>( const std::string& aPath, wxSize aValue );
797template KICOMMON_API void JSON_SETTINGS::Set<wxRect>( const std::string& aPath, wxRect aValue );
798template KICOMMON_API void JSON_SETTINGS::Set<wxAuiPaneInfo>( const std::string& aPath,
799 wxAuiPaneInfo aValue );
800template KICOMMON_API void JSON_SETTINGS::Set<KIGFX::CROSS_HAIR_MODE>( const std::string& aPath,
801 KIGFX::CROSS_HAIR_MODE aValue );
802
803
804void JSON_SETTINGS::registerMigration( int aOldSchemaVersion, int aNewSchemaVersion,
805 std::function<bool()> aMigrator )
806{
807 wxASSERT( aNewSchemaVersion > aOldSchemaVersion );
808 wxASSERT( aNewSchemaVersion <= m_schemaVersion );
809 m_migrators[aOldSchemaVersion] = std::make_pair( aNewSchemaVersion, aMigrator );
810}
811
812
814{
815 int filever = m_internals->Get<int>( "meta.version" );
816
817 while( filever < m_schemaVersion )
818 {
819 wxASSERT( m_migrators.count( filever ) > 0 );
820
821 if( !m_migrators.count( filever ) )
822 {
823 wxLogTrace( traceSettings, wxT( "Migrator missing for %s version %d!" ), typeid( *this ).name(),
824 filever );
825 return false;
826 }
827
828 std::pair<int, std::function<bool()>> pair = m_migrators.at( filever );
829
830 if( pair.second() )
831 {
832 wxLogTrace( traceSettings, wxT( "Migrated %s from %d to %d" ), typeid( *this ).name(), filever,
833 pair.first );
834 filever = pair.first;
835 m_internals->At( "meta.version" ) = filever;
836 }
837 else
838 {
839 wxLogTrace( traceSettings, wxT( "Migration failed for %s from %d to %d" ), typeid( *this ).name(),
840 filever, pair.first );
841 return false;
842 }
843 }
844
845 return true;
846}
847
848
849bool JSON_SETTINGS::MigrateFromLegacy( wxConfigBase* aLegacyConfig )
850{
851 wxLogTrace( traceSettings, wxT( "MigrateFromLegacy() not implemented for %s" ), typeid( *this ).name() );
852 return false;
853}
854
855
856bool JSON_SETTINGS::SetIfPresent( const nlohmann::json& aObj, const std::string& aPath, wxString& aTarget )
857{
858 nlohmann::json::json_pointer ptr = JSON_SETTINGS_INTERNALS::PointerFromString( aPath );
859
860 if( aObj.contains( ptr ) && aObj.at( ptr ).is_string() )
861 {
862 aTarget = aObj.at( ptr ).get<wxString>();
863 return true;
864 }
865
866 return false;
867}
868
869
870bool JSON_SETTINGS::SetIfPresent( const nlohmann::json& aObj, const std::string& aPath, bool& aTarget )
871{
872 nlohmann::json::json_pointer ptr = JSON_SETTINGS_INTERNALS::PointerFromString( aPath );
873
874 if( aObj.contains( ptr ) && aObj.at( ptr ).is_boolean() )
875 {
876 aTarget = aObj.at( ptr ).get<bool>();
877 return true;
878 }
879
880 return false;
881}
882
883
884bool JSON_SETTINGS::SetIfPresent( const nlohmann::json& aObj, const std::string& aPath, int& aTarget )
885{
886 nlohmann::json::json_pointer ptr = JSON_SETTINGS_INTERNALS::PointerFromString( aPath );
887
888 if( aObj.contains( ptr ) && aObj.at( ptr ).is_number_integer() )
889 {
890 aTarget = aObj.at( ptr ).get<int>();
891 return true;
892 }
893
894 return false;
895}
896
897
898bool JSON_SETTINGS::SetIfPresent( const nlohmann::json& aObj, const std::string& aPath, unsigned int& aTarget )
899{
900 nlohmann::json::json_pointer ptr = JSON_SETTINGS_INTERNALS::PointerFromString( aPath );
901
902 if( aObj.contains( ptr ) && aObj.at( ptr ).is_number_unsigned() )
903 {
904 aTarget = aObj.at( ptr ).get<unsigned int>();
905 return true;
906 }
907
908 return false;
909}
910
911
912template<typename ValueType>
913bool JSON_SETTINGS::fromLegacy( wxConfigBase* aConfig, const std::string& aKey, const std::string& aDest )
914{
915 ValueType val;
916
917 if( aConfig->Read( aKey, &val ) )
918 {
919 try
920 {
921 ( *m_internals )[aDest] = val;
922 }
923 catch( ... )
924 {
925 wxASSERT_MSG( false, wxT( "Could not write value in fromLegacy!" ) );
926 return false;
927 }
928
929 return true;
930 }
931
932 return false;
933}
934
935
936// Explicitly declare these because we only support a few types anyway, and it means we can keep
937// wxConfig detail out of the header file
938template
939KICOMMON_API bool JSON_SETTINGS::fromLegacy<int>( wxConfigBase*, const std::string&, const std::string& );
940
941template
942KICOMMON_API bool JSON_SETTINGS::fromLegacy<double>( wxConfigBase*, const std::string&, const std::string& );
943
944template
945KICOMMON_API bool JSON_SETTINGS::fromLegacy<bool>( wxConfigBase*, const std::string&, const std::string& );
946
947
948bool JSON_SETTINGS::fromLegacyString( wxConfigBase* aConfig, const std::string& aKey, const std::string& aDest )
949{
950 wxString str;
951
952 if( aConfig->Read( aKey, &str ) )
953 {
954 try
955 {
956 ( *m_internals )[aDest] = str.ToUTF8();
957 }
958 catch( ... )
959 {
960 wxASSERT_MSG( false, wxT( "Could not write value in fromLegacyString!" ) );
961 return false;
962 }
963
964 return true;
965 }
966
967 return false;
968}
969
970
971bool JSON_SETTINGS::fromLegacyColor( wxConfigBase* aConfig, const std::string& aKey, const std::string& aDest )
972{
973 wxString str;
974
975 if( aConfig->Read( aKey, &str ) )
976 {
977 KIGFX::COLOR4D color;
978 color.SetFromWxString( str );
979
980 try
981 {
982 nlohmann::json js = nlohmann::json::array( { color.r, color.g, color.b, color.a } );
983 ( *m_internals )[aDest] = std::move( js );
984 }
985 catch( ... )
986 {
987 wxASSERT_MSG( false, wxT( "Could not write value in fromLegacyColor!" ) );
988 return false;
989 }
990
991 return true;
992 }
993
994 return false;
995}
996
997
999{
1000 wxLogTrace( traceSettings, wxT( "AddNestedSettings %s" ), aSettings->GetFilename() );
1001 m_nested_settings.push_back( aSettings );
1002}
1003
1004
1006{
1007 if( !aSettings || !m_manager )
1008 return;
1009
1010 auto it = std::find_if( m_nested_settings.begin(), m_nested_settings.end(),
1011 [&aSettings]( const JSON_SETTINGS* aPtr )
1012 {
1013 return aPtr == aSettings;
1014 } );
1015
1016 if( it != m_nested_settings.end() )
1017 {
1018 wxLogTrace( traceSettings, wxT( "Flush and release %s" ), ( *it )->GetFilename() );
1019
1020 // Flush the nested state into the parent and propagate genuine dirtiness so a later parent
1021 // save persists it; the nested object is gone by then, so this is the parent's only signal.
1022 // Default-fill of params absent from an older file no longer reports modified, so releasing
1023 // an unchanged nested setting on editor close does not falsely dirty the parent (#24402).
1024 m_modified |= ( *it )->SaveToFile();
1025 m_nested_settings.erase( it );
1026 }
1027
1028 aSettings->SetParent( nullptr );
1029}
1030
1031
1032// Specializations to allow conversion between wxString and std::string via JSON_SETTINGS API
1033template<>
1034std::optional<wxString> JSON_SETTINGS::Get( const std::string& aPath ) const
1035{
1036 if( std::optional<nlohmann::json> opt_json = GetJson( aPath ) )
1037 return wxString( opt_json->get<std::string>().c_str(), wxConvUTF8 );
1038
1039 return std::nullopt;
1040}
1041
1042
1043template<>
1044void JSON_SETTINGS::Set<wxString>( const std::string& aPath, wxString aVal )
1045{
1046 ( *m_internals )[aPath] = aVal.ToUTF8();
1047}
1048
1049
1050template<typename ResultType>
1051ResultType JSON_SETTINGS::fetchOrDefault( const nlohmann::json& aJson, const std::string& aKey,
1052 ResultType aDefault )
1053{
1054 ResultType ret = std::move( aDefault );
1055
1056 try
1057 {
1058 if( aJson.contains( aKey ) )
1059 ret = aJson.at( aKey ).get<ResultType>();
1060 }
1061 catch( ... )
1062 {
1063 }
1064
1065 return ret;
1066}
1067
1068
1069template
1070KICOMMON_API std::string JSON_SETTINGS::fetchOrDefault( const nlohmann::json& aJson, const std::string& aKey,
1071 std::string aDefault );
1072
1073
1074template
1075KICOMMON_API bool JSON_SETTINGS::fetchOrDefault( const nlohmann::json& aJson, const std::string& aKey, bool aDefault );
static nlohmann::json::json_pointer PointerFromString(std::string aPath)
Builds a JSON pointer based on a given string.
bool fromLegacyString(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy wxConfig string value to a given JSON pointer value.
bool fromLegacy(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy wxConfig value to a given JSON pointer value.
virtual wxString getFileExt() const
const nlohmann::json & originalJson() const
virtual std::map< std::string, nlohmann::json > GetFileHistories()
bool flushToStore()
Flushes nested settings and parameters into the JSON store.
void Set(const std::string &aPath, ValueType aVal)
Stores a value into the JSON document Will throw an exception if ValueType isn't something that the l...
std::optional< nlohmann::json > GetJson(const std::string &aPath) const
Fetches a JSON object that is a subset of this JSON_SETTINGS object, using a path of the form "key1....
wxString m_filename
The filename (not including path) of this settings file (inicode)
SETTINGS_MANAGER * m_manager
A pointer to the settings manager managing this file (may be null)
bool Contains(const std::string &aPath) const
bool m_fileSynced
True once the store has been synchronized with an on-disk file (loaded or saved)
bool LoadFromRawFile(const wxString &aPath)
bool m_isFutureFormat
Set to true if this settings is loaded from a file with a newer schema version than is known.
virtual ~JSON_SETTINGS()
std::vector< NESTED_SETTINGS * > m_nested_settings
Nested settings files that live inside this one, if any.
bool m_modified
True if the JSON data store has been written to since the last file write.
virtual bool LoadFromFile(const wxString &aDirectory="")
Loads the backing file from disk and then calls Load()
bool m_createIfDefault
Whether or not the backing store file should be created if all parameters are still at their default ...
bool m_writeFile
Whether or not the backing store file should be written.
static bool SetIfPresent(const nlohmann::json &aObj, const std::string &aPath, wxString &aTarget)
Sets the given string if the given key/path is present.
virtual void Load()
Updates the parameters of this object based on the current JSON document contents.
bool parseAndMigrate(const std::function< bool(nlohmann::json &)> &aParse, bool &aMigrated, bool &aParseError)
Parses a document into the store through aParse, then checks the schema version and migrates.
void ResetToDefaults()
Resets all parameters to default values.
bool fromLegacyColor(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy COLOR4D stored in a wxConfig string to a given JSON pointer value.
wxString GetFullFilename() const
bool m_createIfMissing
Whether or not the backing store file should be created it if doesn't exist.
bool Migrate()
Migrates the schema of this settings from the version in the file to the latest version.
std::optional< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
bool m_lastSaveFailed
True if the most recent SaveToFile() had changes to write and could not write them.
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
virtual bool MigrateFromLegacy(wxConfigBase *aLegacyConfig)
Migrates from wxConfig to JSON-based configuration.
const std::string FormatAsString()
void registerMigration(int aOldSchemaVersion, int aNewSchemaVersion, std::function< bool(void)> aMigrator)
Registers a migration from one schema version to another.
nlohmann::json & At(const std::string &aPath)
Wrappers for the underlying JSON API so that most consumers don't need json.hpp All of these function...
JSON_SETTINGS_INTERNALS * Internals()
static ResultType fetchOrDefault(const nlohmann::json &aJson, const std::string &aKey, ResultType aDefault=ResultType())
Helper to retrieve a value from a JSON object (dictionary) as a certain result type.
void ReleaseNestedSettings(NESTED_SETTINGS *aSettings)
Saves and frees a nested settings object, if it exists within this one.
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
bool m_deleteLegacyAfterMigration
Whether or not to delete legacy file after migration.
std::unique_ptr< JSON_SETTINGS_INTERNALS > m_internals
friend class NESTED_SETTINGS
SETTINGS_LOC m_location
The location of this settings file (.
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
Calls Store() and then writes the contents of the JSON document to a file.
virtual wxString getLegacyFileExt() const
bool m_resetParamsIfMissing
Whether or not to set parameters to their default value if missing from JSON on Load()
std::string formatFileContents()
Overlays the current store on the original file contents.
bool LoadFromBuffer(const std::string &aContents)
Loads the document from file contents already in memory, so the caller can bound the read of the file...
std::map< int, std::pair< int, std::function< bool()> > > m_migrators
A map of starting schema version to a pair of <ending version, migrator function>
int m_schemaVersion
Version of this settings schema.
wxString m_legacy_filename
The filename of the wxConfig legacy file (if different from m_filename)
void AddNestedSettings(NESTED_SETTINGS *aSettings)
Transfers ownership of a given NESTED_SETTINGS to this object.
virtual bool Store()
Stores the current parameters into the JSON document represented by this object Note: this doesn't do...
wxString GetFilename() const
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
double r
Red component.
Definition color4d.h:391
double g
Green component.
Definition color4d.h:392
bool SetFromWxString(const wxString &aColorString)
Set color values by parsing a string using wxColour::Set().
Definition color4d.cpp:127
double a
Alpha component.
Definition color4d.h:394
double b
Blue component.
Definition color4d.h:393
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
void SetParent(JSON_SETTINGS *aParent, bool aLoadFromFile=true)
#define ALLOW_EXCEPTIONS
#define IGNORE_COMMENTS
SETTINGS_LOC
#define traceSettings
#define KICOMMON_API
Definition kicommon.h:27
bool AtomicWriteFile(const wxString &aTargetPath, const void *aData, size_t aSize, wxString *aError=nullptr)
Writes aData to aTargetPath via a sibling temp file, fsyncs the data and directory,...
std::vector< FAB_LAYER_COLOR > dummy
Common grid settings, available to every frame.
std::string path