KiCad PCB EDA Suite
Loading...
Searching...
No Matches
settings_manager.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 (C) 2021, 2023 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 along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
22#include <regex>
23#include <wx/debug.h>
24#include <wx/dir.h>
25#include <wx/filename.h>
26#include <wx/snglinst.h>
27#include <wx/stdpaths.h>
28#include <wx/utils.h>
29
30#include <build_version.h>
31#include <confirm.h>
33#include <gestfich.h>
35#include <kiway.h>
36#include <lockfile.h>
37#include <macros.h>
38#include <pgm_base.h>
39#include <paths.h>
40#include <project.h>
49
50
52 m_headless( aHeadless ),
53 m_kiway( nullptr ),
54 m_common_settings( nullptr ),
55 m_migration_source(),
56 m_migrateLibraryTables( true )
57{
58 // Check if the settings directory already exists, and if not, perform a migration if possible
59 if( !MigrateIfNeeded() )
60 {
61 m_ok = false;
62 return;
63 }
64
65 m_ok = true;
66
67 // create the common settings shared by all applications. Not loaded immediately
69
70 // Create the built-in color settings
71 // Here to allow the Python API to access the built-in colors
73
74 wxFileName commonSettings( GetPathForSettingsFile( m_common_settings ),
76
77 if( !wxFileExists( commonSettings.GetFullPath() ) )
78 {
81 }
82}
83
85{
86 for( std::unique_ptr<PROJECT>& project : m_projects_list )
87 project.reset();
88
89 m_projects.clear();
90
91 for( std::unique_ptr<JSON_SETTINGS>& settings : m_settings )
92 settings.reset();
93
94 m_settings.clear();
95
96 m_color_settings.clear();
97}
98
99
101{
102 std::unique_ptr<JSON_SETTINGS> ptr( aSettings );
103
104 ptr->SetManager( this );
105
106 wxLogTrace( traceSettings, wxT( "Registered new settings object <%s>" ),
107 ptr->GetFullFilename() );
108
109 if( aLoadNow )
110 ptr->LoadFromFile( GetPathForSettingsFile( ptr.get() ) );
111
112 m_settings.push_back( std::move( ptr ) );
113 return m_settings.back().get();
114}
115
116
118{
119 // TODO(JE) We should check for dirty settings here and write them if so, because
120 // Load() could be called late in the application lifecycle
121
122 std::vector<JSON_SETTINGS*> toLoad;
123
124 // Cache a copy of raw pointers; m_settings may be modified during the load loop
125 std::transform( m_settings.begin(), m_settings.end(), std::back_inserter( toLoad ),
126 []( std::unique_ptr<JSON_SETTINGS>& aSettings )
127 {
128 return aSettings.get();
129 } );
130
131 for( JSON_SETTINGS* settings : toLoad )
132 settings->LoadFromFile( GetPathForSettingsFile( settings ) );
133}
134
135
137{
138 auto it = std::find_if( m_settings.begin(), m_settings.end(),
139 [&aSettings]( const std::unique_ptr<JSON_SETTINGS>& aPtr )
140 {
141 return aPtr.get() == aSettings;
142 } );
143
144 if( it != m_settings.end() )
145 ( *it )->LoadFromFile( GetPathForSettingsFile( it->get() ) );
146}
147
148
150{
151 for( auto&& settings : m_settings )
152 {
153 // Never automatically save color settings, caller should use SaveColorSettings
154 if( dynamic_cast<COLOR_SETTINGS*>( settings.get() ) )
155 continue;
156
157 settings->SaveToFile( GetPathForSettingsFile( settings.get() ) );
158 }
159}
160
161
163{
164 auto it = std::find_if( m_settings.begin(), m_settings.end(),
165 [&aSettings]( const std::unique_ptr<JSON_SETTINGS>& aPtr )
166 {
167 return aPtr.get() == aSettings;
168 } );
169
170 if( it != m_settings.end() )
171 {
172 wxLogTrace( traceSettings, wxT( "Saving %s" ), ( *it )->GetFullFilename() );
173 ( *it )->SaveToFile( GetPathForSettingsFile( it->get() ) );
174 }
175}
176
177
179{
180 auto it = std::find_if( m_settings.begin(), m_settings.end(),
181 [&aSettings]( const std::unique_ptr<JSON_SETTINGS>& aPtr )
182 {
183 return aPtr.get() == aSettings;
184 } );
185
186 if( it != m_settings.end() )
187 {
188 wxLogTrace( traceSettings, wxT( "Flush and release %s" ), ( *it )->GetFullFilename() );
189
190 if( aSave )
191 ( *it )->SaveToFile( GetPathForSettingsFile( it->get() ) );
192
193 size_t typeHash = typeid( *it->get() ).hash_code();
194
195 if( m_app_settings_cache.count( typeHash ) )
196 m_app_settings_cache.erase( typeHash );
197
198 m_settings.erase( it );
199 }
200}
201
202
204{
205 // Find settings the fast way
206 if( m_color_settings.count( aName ) )
207 return m_color_settings.at( aName );
208
209 // Maybe it's the display name (cli is one method of invoke)
210 auto it = std::find_if( m_color_settings.begin(), m_color_settings.end(),
211 [&aName]( const std::pair<wxString, COLOR_SETTINGS*>& p )
212 {
213 return p.second->GetName().Lower() == aName.Lower();
214 } );
215
216 if( it != m_color_settings.end() )
217 {
218 return it->second;
219 }
220
221 // No match? See if we can load it
222 if( !aName.empty() )
223 {
225
226 if( !ret )
227 {
228 ret = registerColorSettings( aName );
230 ret->SetFilename( wxT( "user" ) );
231 ret->SetReadOnly( false );
232 }
233
234 return ret;
235 }
236
237 // This had better work
239}
240
241
243{
244 wxLogTrace( traceSettings, wxT( "Attempting to load color theme %s" ), aName );
245
246 wxFileName fn( GetColorSettingsPath(), aName, wxS( "json" ) );
247
248 if( !fn.IsOk() || !fn.Exists() )
249 {
250 wxLogTrace( traceSettings, wxT( "Theme file %s.json not found, falling back to user" ),
251 aName );
252 return nullptr;
253 }
254
255 COLOR_SETTINGS* settings = RegisterSettings( new COLOR_SETTINGS( aName ) );
256
257 if( settings->GetFilename() != aName.ToStdString() )
258 {
259 wxLogTrace( traceSettings, wxT( "Warning: stored filename is actually %s, " ),
260 settings->GetFilename() );
261 }
262
263 m_color_settings[aName] = settings;
264
265 return settings;
266}
267
268
269class JSON_DIR_TRAVERSER : public wxDirTraverser
270{
271private:
272 std::function<void( const wxFileName& )> m_action;
273
274public:
275 explicit JSON_DIR_TRAVERSER( std::function<void( const wxFileName& )> aAction )
276 : m_action( std::move( aAction ) )
277 {
278 }
279
280 wxDirTraverseResult OnFile( const wxString& aFilePath ) override
281 {
282 wxFileName file( aFilePath );
283
284 if( file.GetExt() == wxS( "json" ) )
285 m_action( file );
286
287 return wxDIR_CONTINUE;
288 }
289
290 wxDirTraverseResult OnDir( const wxString& dirPath ) override
291 {
292 return wxDIR_CONTINUE;
293 }
294};
295
296
297COLOR_SETTINGS* SETTINGS_MANAGER::registerColorSettings( const wxString& aName, bool aAbsolutePath )
298{
299 if( !m_color_settings.count( aName ) )
300 {
301 COLOR_SETTINGS* colorSettings = RegisterSettings( new COLOR_SETTINGS( aName,
302 aAbsolutePath ) );
303 m_color_settings[aName] = colorSettings;
304 }
305
306 return m_color_settings.at( aName );
307}
308
309
311{
312 if( aName.EndsWith( wxT( ".json" ) ) )
313 return registerColorSettings( aName.BeforeLast( '.' ) );
314 else
315 return registerColorSettings( aName );
316}
317
318
320{
321 if( !m_color_settings.count( "user" ) )
322 {
323 COLOR_SETTINGS* settings = registerColorSettings( wxT( "user" ) );
324 settings->SetName( wxT( "User" ) );
325 Save( settings );
326 }
327
328 return m_color_settings.at( "user" );
329}
330
331
333{
335 m_color_settings[settings->GetFilename()] = RegisterSettings( settings, false );
336}
337
338
340{
341 // Create the built-in color settings
343
344 wxFileName third_party_path;
345 const ENV_VAR_MAP& env = Pgm().GetLocalEnvVariables();
346 auto it = env.find( wxS( "KICAD7_3RD_PARTY" ) );
347
348 if( it != env.end() && !it->second.GetValue().IsEmpty() )
349 third_party_path.SetPath( it->second.GetValue() );
350 else
351 third_party_path.SetPath( PATHS::GetDefault3rdPartyPath() );
352
353 third_party_path.AppendDir( wxS( "colors" ) );
354
355 // PCM-managed themes
356 wxDir third_party_colors_dir( third_party_path.GetFullPath() );
357
358 // System-installed themes
359 wxDir system_colors_dir( PATHS::GetStockDataPath( false ) + "/colors" );
360
361 // User-created themes
362 wxDir colors_dir( GetColorSettingsPath() );
363
364 // Search for and load any other settings
365 JSON_DIR_TRAVERSER loader( [&]( const wxFileName& aFilename )
366 {
367 registerColorSettings( aFilename.GetName() );
368 } );
369
370 JSON_DIR_TRAVERSER readOnlyLoader(
371 [&]( const wxFileName& aFilename )
372 {
373 COLOR_SETTINGS* settings = registerColorSettings( aFilename.GetFullPath(), true );
374 settings->SetReadOnly( true );
375 } );
376
377 if( system_colors_dir.IsOpened() )
378 system_colors_dir.Traverse( readOnlyLoader );
379
380 if( third_party_colors_dir.IsOpened() )
381 third_party_colors_dir.Traverse( readOnlyLoader );
382
383 if( colors_dir.IsOpened() )
384 colors_dir.Traverse( loader );
385}
386
387
389{
390 m_color_settings.clear();
392}
393
394
395void SETTINGS_MANAGER::SaveColorSettings( COLOR_SETTINGS* aSettings, const std::string& aNamespace )
396{
397 // The passed settings should already be managed
398 wxASSERT( std::find_if( m_color_settings.begin(), m_color_settings.end(),
399 [aSettings] ( const std::pair<wxString, COLOR_SETTINGS*>& el )
400 {
401 return el.second->GetFilename() == aSettings->GetFilename();
402 }
403 ) != m_color_settings.end() );
404
405 if( aSettings->IsReadOnly() )
406 return;
407
408 if( !aSettings->Store() )
409 {
410 wxLogTrace( traceSettings, wxT( "Color scheme %s not modified; skipping save" ),
411 aNamespace );
412 return;
413 }
414
415 wxASSERT( aSettings->Contains( aNamespace ) );
416
417 wxLogTrace( traceSettings, wxT( "Saving color scheme %s, preserving %s" ),
418 aSettings->GetFilename(),
419 aNamespace );
420
421 std::optional<nlohmann::json> backup = aSettings->GetJson( aNamespace );
422 wxString path = GetColorSettingsPath();
423
424 aSettings->LoadFromFile( path );
425
426 if( backup )
427 ( *aSettings->Internals() )[aNamespace].update( *backup );
428
429 aSettings->Load();
430
431 aSettings->SaveToFile( path, true );
432}
433
434
436{
437 wxASSERT( aSettings );
438
439 switch( aSettings->GetLocation() )
440 {
441 case SETTINGS_LOC::USER:
443
444 case SETTINGS_LOC::PROJECT:
445 // TODO: MDI support
446 return Prj().GetProjectPath();
447
448 case SETTINGS_LOC::COLORS:
449 return GetColorSettingsPath();
450
451 case SETTINGS_LOC::NONE:
452 return "";
453
454 default:
455 wxASSERT_MSG( false, wxT( "Unknown settings location!" ) );
456 }
457
458 return "";
459}
460
461
462class MIGRATION_TRAVERSER : public wxDirTraverser
463{
464private:
465 wxString m_src;
466 wxString m_dest;
467 wxString m_errors;
469
470public:
471 MIGRATION_TRAVERSER( const wxString& aSrcDir, const wxString& aDestDir, bool aMigrateTables ) :
472 m_src( aSrcDir ),
473 m_dest( aDestDir ),
474 m_migrateTables( aMigrateTables )
475 {
476 }
477
478 wxString GetErrors() { return m_errors; }
479
480 wxDirTraverseResult OnFile( const wxString& aSrcFilePath ) override
481 {
482 wxFileName file( aSrcFilePath );
483
484 if( !m_migrateTables && ( file.GetName() == wxT( "sym-lib-table" ) ||
485 file.GetName() == wxT( "fp-lib-table" ) ) )
486 {
487 return wxDIR_CONTINUE;
488 }
489
490 // Skip migrating PCM installed packages as packages themselves are not moved
491 if( file.GetFullName() == wxT( "installed_packages.json" ) )
492 return wxDIR_CONTINUE;
493
494 // Don't migrate hotkeys config files; we don't have a reasonable migration handler for them
495 // and so there is no way to resolve conflicts at the moment
496 if( file.GetExt() == wxT( "hotkeys" ) )
497 return wxDIR_CONTINUE;
498
499 wxString path = file.GetPath();
500
501 path.Replace( m_src, m_dest, false );
502 file.SetPath( path );
503
504 wxLogTrace( traceSettings, wxT( "Copying %s to %s" ), aSrcFilePath, file.GetFullPath() );
505
506 // For now, just copy everything
507 KiCopyFile( aSrcFilePath, file.GetFullPath(), m_errors );
508
509 return wxDIR_CONTINUE;
510 }
511
512 wxDirTraverseResult OnDir( const wxString& dirPath ) override
513 {
514 wxFileName dir( dirPath );
515
516 // Whitelist of directories to migrate
517 if( dir.GetName() == wxS( "colors" ) ||
518 dir.GetName() == wxS( "3d" ) )
519 {
520
521 wxString path = dir.GetPath();
522
523 path.Replace( m_src, m_dest, false );
524 dir.SetPath( path );
525
526 wxMkdir( dir.GetFullPath() );
527
528 return wxDIR_CONTINUE;
529 }
530 else
531 {
532 return wxDIR_IGNORE;
533 }
534 }
535};
536
537
539{
540 wxFileName path( PATHS::GetUserSettingsPath(), wxS( "" ) );
541 wxLogTrace( traceSettings, wxT( "Using settings path %s" ), path.GetFullPath() );
542
543 if( m_headless )
544 {
545 // Special case namely for cli
546 // Ensure the settings directory at least exists to prevent additional loading errors
547 // from subdirectories.
548 // TODO review headless (unit tests) vs cli needs, this should be fine for unit tests though
549 if( !path.DirExists() )
550 {
551 wxLogTrace( traceSettings, wxT( "Path didn't exist; creating it" ) );
552 path.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
553 }
554
555 wxLogTrace( traceSettings, wxT( "Settings migration not checked; running headless" ) );
556 return true;
557 }
558
559 if( path.DirExists() )
560 {
561 wxFileName common = path;
562 common.SetName( wxS( "kicad_common" ) );
563 common.SetExt( wxS( "json" ) );
564
565 if( common.Exists() )
566 {
567 wxLogTrace( traceSettings, wxT( "Path exists and has a kicad_common, continuing!" ) );
568 return true;
569 }
570 }
571
572 // Now we have an empty path, let's figure out what to put in it
573 DIALOG_MIGRATE_SETTINGS dlg( this );
574
575 if( dlg.ShowModal() != wxID_OK )
576 {
577 wxLogTrace( traceSettings, wxT( "Migration dialog canceled; exiting" ) );
578 return false;
579 }
580
581 if( !path.DirExists() )
582 {
583 wxLogTrace( traceSettings, wxT( "Path didn't exist; creating it" ) );
584 path.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
585 }
586
587 if( m_migration_source.IsEmpty() )
588 {
589 wxLogTrace( traceSettings, wxT( "No migration source given; starting with defaults" ) );
590 return true;
591 }
592
593 wxLogTrace( traceSettings, wxT( "Migrating from path %s" ), m_migration_source );
594
596 wxDir source_dir( m_migration_source );
597
598 source_dir.Traverse( traverser );
599
600 if( !traverser.GetErrors().empty() )
601 DisplayErrorMessage( nullptr, traverser.GetErrors() );
602
603 // Remove any library configuration if we didn't choose to import
605 {
606 COMMON_SETTINGS common;
607 wxString commonPath = GetPathForSettingsFile( &common );
608 common.LoadFromFile( commonPath );
609
610 const std::vector<wxString> libKeys = {
611 wxT( "KICAD6_SYMBOL_DIR" ),
612 wxT( "KICAD6_3DMODEL_DIR" ),
613 wxT( "KICAD6_FOOTPRINT_DIR" ),
614 wxT( "KICAD6_TEMPLATE_DIR" ), // Stores the default library table to be copied
615 wxT( "KICAD7_SYMBOL_DIR" ),
616 wxT( "KICAD7_3DMODEL_DIR" ),
617 wxT( "KICAD7_FOOTPRINT_DIR" ),
618 wxT( "KICAD7_TEMPLATE_DIR" ),
619 wxT( "KICAD8_SYMBOL_DIR" ),
620 wxT( "KICAD8_3DMODEL_DIR" ),
621 wxT( "KICAD8_FOOTPRINT_DIR" ),
622 wxT( "KICAD8_TEMPLATE_DIR" ),
623
624 // Deprecated keys
625 wxT( "KICAD_PTEMPLATES" ),
626 wxT( "KISYS3DMOD" ),
627 wxT( "KISYSMOD" ),
628 wxT( "KICAD_SYMBOL_DIR" ),
629 };
630
631 for( const wxString& key : libKeys )
632 common.m_Env.vars.erase( key );
633
634 common.SaveToFile( commonPath );
635 }
636
637 return true;
638}
639
640
641bool SETTINGS_MANAGER::GetPreviousVersionPaths( std::vector<wxString>* aPaths )
642{
643 wxASSERT( aPaths );
644
645 aPaths->clear();
646
647 wxDir dir;
648 std::vector<wxFileName> base_paths;
649
650 base_paths.emplace_back( wxFileName( PATHS::CalculateUserSettingsPath( false ), wxS( "" ) ) );
651
652 // If the env override is set, also check the default paths
653 if( wxGetEnv( wxT( "KICAD_CONFIG_HOME" ), nullptr ) )
654 base_paths.emplace_back( wxFileName( PATHS::CalculateUserSettingsPath( false, false ),
655 wxS( "" ) ) );
656
657#ifdef __WXGTK__
658 // When running inside FlatPak, KIPLATFORM::ENV::GetUserConfigPath() will return a sandboxed
659 // path. In case the user wants to move from non-FlatPak KiCad to FlatPak KiCad, let's add our
660 // best guess as to the non-FlatPak config path. Unfortunately FlatPak also hides the host
661 // XDG_CONFIG_HOME, so if the user customizes their config path, they will have to browse
662 // for it.
663 {
664 wxFileName wxGtkPath;
665 wxGtkPath.AssignDir( wxS( "~/.config/kicad" ) );
666 wxGtkPath.MakeAbsolute();
667 base_paths.emplace_back( wxGtkPath );
668
669 // We also want to pick up regular flatpak if we are nightly
670 wxGtkPath.AssignDir( wxS( "~/.var/app/org.kicad.KiCad/config/kicad" ) );
671 wxGtkPath.MakeAbsolute();
672 base_paths.emplace_back( wxGtkPath );
673 }
674#endif
675
676 wxString subdir;
677 std::string mine = GetSettingsVersion();
678
679 auto check_dir = [&] ( const wxString& aSubDir )
680 {
681 // Only older versions are valid for migration
682 if( compareVersions( aSubDir.ToStdString(), mine ) <= 0 )
683 {
684 wxString sub_path = dir.GetNameWithSep() + aSubDir;
685
686 if( IsSettingsPathValid( sub_path ) )
687 {
688 aPaths->push_back( sub_path );
689 wxLogTrace( traceSettings, wxT( "GetPreviousVersionName: %s is valid" ), sub_path );
690 }
691 }
692 };
693
694 std::set<wxString> checkedPaths;
695
696 for( const wxFileName& base_path : base_paths )
697 {
698 if( checkedPaths.count( base_path.GetFullPath() ) )
699 continue;
700
701 checkedPaths.insert( base_path.GetFullPath() );
702
703 if( !dir.Open( base_path.GetFullPath() ) )
704 {
705 wxLogTrace( traceSettings, wxT( "GetPreviousVersionName: could not open base path %s" ),
706 base_path.GetFullPath() );
707 continue;
708 }
709
710 wxLogTrace( traceSettings, wxT( "GetPreviousVersionName: checking base path %s" ),
711 base_path.GetFullPath() );
712
713 if( dir.GetFirst( &subdir, wxEmptyString, wxDIR_DIRS ) )
714 {
715 if( subdir != mine )
716 check_dir( subdir );
717
718 while( dir.GetNext( &subdir ) )
719 {
720 if( subdir != mine )
721 check_dir( subdir );
722 }
723 }
724
725 // If we didn't find one yet, check for legacy settings without a version directory
726 if( IsSettingsPathValid( dir.GetNameWithSep() ) )
727 {
728 wxLogTrace( traceSettings,
729 wxT( "GetPreviousVersionName: root path %s is valid" ), dir.GetName() );
730 aPaths->push_back( dir.GetName() );
731 }
732 }
733
734 std::sort( aPaths->begin(), aPaths->end(),
735 [&]( const wxString& a, const wxString& b ) -> bool
736 {
737 wxString verA = wxFileName::DirName( a ).GetDirs().back();
738 wxString verB = wxFileName::DirName( b ).GetDirs().back();
739
740 if( !extractVersion( verA.ToStdString() )
741 || !extractVersion( verB.ToStdString() ) )
742 {
743 return false;
744 }
745
746 return compareVersions( verA.ToStdString(), verB.ToStdString() ) >= 0;
747 } );
748
749 return aPaths->size() > 0;
750}
751
752
753bool SETTINGS_MANAGER::IsSettingsPathValid( const wxString& aPath )
754{
755 wxFileName test( aPath, wxS( "kicad_common" ) );
756
757 if( test.Exists() )
758 return true;
759
760 test.SetExt( "json" );
761
762 return test.Exists();
763}
764
765
767{
768 wxFileName path;
769
770 path.AssignDir( PATHS::GetUserSettingsPath() );
771 path.AppendDir( wxS( "colors" ) );
772
773 if( !path.DirExists() )
774 {
775 if( !wxMkdir( path.GetPath() ) )
776 {
777 wxLogTrace( traceSettings,
778 wxT( "GetColorSettingsPath(): Path %s missing and could not be created!" ),
779 path.GetPath() );
780 }
781 }
782
783 return path.GetPath();
784}
785
786
788{
789 // CMake computes the major.minor string for us.
790 return GetMajorMinorVersion().ToStdString();
791}
792
793
794int SETTINGS_MANAGER::compareVersions( const std::string& aFirst, const std::string& aSecond )
795{
796 int a_maj = 0;
797 int a_min = 0;
798 int b_maj = 0;
799 int b_min = 0;
800
801 if( !extractVersion( aFirst, &a_maj, &a_min ) || !extractVersion( aSecond, &b_maj, &b_min ) )
802 {
803 wxLogTrace( traceSettings, wxT( "compareSettingsVersions: bad input (%s, %s)" ),
804 aFirst, aSecond );
805 return -1;
806 }
807
808 if( a_maj < b_maj )
809 {
810 return -1;
811 }
812 else if( a_maj > b_maj )
813 {
814 return 1;
815 }
816 else
817 {
818 if( a_min < b_min )
819 {
820 return -1;
821 }
822 else if( a_min > b_min )
823 {
824 return 1;
825 }
826 else
827 {
828 return 0;
829 }
830 }
831}
832
833
834bool SETTINGS_MANAGER::extractVersion( const std::string& aVersionString, int* aMajor, int* aMinor )
835{
836 std::regex re_version( "(\\d+)\\.(\\d+)" );
837 std::smatch match;
838
839 if( std::regex_match( aVersionString, match, re_version ) )
840 {
841 try
842 {
843 int major = std::stoi( match[1].str() );
844 int minor = std::stoi( match[2].str() );
845
846 if( aMajor )
847 *aMajor = major;
848
849 if( aMinor )
850 *aMinor = minor;
851 }
852 catch( ... )
853 {
854 return false;
855 }
856
857 return true;
858 }
859
860 return false;
861}
862
863
864bool SETTINGS_MANAGER::LoadProject( const wxString& aFullPath, bool aSetActive )
865{
866 // Normalize path to new format even if migrating from a legacy file
867 wxFileName path( aFullPath );
868
871
872 wxString fullPath = path.GetFullPath();
873
874 // If already loaded, we are all set. This might be called more than once over a project's
875 // lifetime in case the project is first loaded by the KiCad manager and then eeschema or
876 // pcbnew try to load it again when they are launched.
877 if( m_projects.count( fullPath ) )
878 return true;
879
880 bool readOnly = false;
881 LOCKFILE lockFile( fullPath );
882
883 if( !lockFile.Valid() )
884 {
885 wxLogTrace( traceSettings, wxT( "Project %s is locked; opening read-only" ), fullPath );
886 readOnly = true;
887 }
888
889 // No MDI yet
890 if( aSetActive && !m_projects.empty() )
891 {
892 PROJECT* oldProject = m_projects.begin()->second;
893 unloadProjectFile( oldProject, false );
894 m_projects.erase( m_projects.begin() );
895
896 auto it = std::find_if( m_projects_list.begin(), m_projects_list.end(),
897 [&]( const std::unique_ptr<PROJECT>& ptr )
898 {
899 return ptr.get() == oldProject;
900 } );
901
902 wxASSERT( it != m_projects_list.end() );
903 m_projects_list.erase( it );
904 }
905
906 wxLogTrace( traceSettings, wxT( "Load project %s" ), fullPath );
907
908 std::unique_ptr<PROJECT> project = std::make_unique<PROJECT>();
909 project->setProjectFullName( fullPath );
910
911 if( aSetActive )
912 {
913 // until multiple projects are in play, set an environment variable for the
914 // the project pointer.
915 wxFileName projectPath( fullPath );
916 wxSetEnv( PROJECT_VAR_NAME, projectPath.GetPath() );
917 }
918
919 bool success = loadProjectFile( *project );
920
921 if( success )
922 {
923 project->SetReadOnly( readOnly || project->GetProjectFile().IsReadOnly() );
924
925 if( lockFile && aSetActive )
926 m_project_lock.reset( new LOCKFILE( std::move( lockFile ) ) );
927 }
928
929 m_projects_list.push_back( std::move( project ) );
930 m_projects[fullPath] = m_projects_list.back().get();
931
932 wxString fn( path.GetName() );
933
934 PROJECT_LOCAL_SETTINGS* settings = new PROJECT_LOCAL_SETTINGS( m_projects[fullPath], fn );
935
936 if( aSetActive )
937 settings = RegisterSettings( settings );
938 else
939 settings->LoadFromFile( path.GetPath() );
940
941 m_projects[fullPath]->setLocalSettings( settings );
942
943 if( aSetActive && m_kiway )
945
946 return success;
947}
948
949
950bool SETTINGS_MANAGER::UnloadProject( PROJECT* aProject, bool aSave )
951{
952 if( !aProject || !m_projects.count( aProject->GetProjectFullName() ) )
953 return false;
954
955 if( !unloadProjectFile( aProject, aSave ) )
956 return false;
957
958 wxString projectPath = aProject->GetProjectFullName();
959 wxLogTrace( traceSettings, wxT( "Unload project %s" ), projectPath );
960
961 PROJECT* toRemove = m_projects.at( projectPath );
962 bool wasActiveProject = m_projects_list.begin()->get() == toRemove;
963
964 auto it = std::find_if( m_projects_list.begin(), m_projects_list.end(),
965 [&]( const std::unique_ptr<PROJECT>& ptr )
966 {
967 return ptr.get() == toRemove;
968 } );
969
970 wxASSERT( it != m_projects_list.end() );
971 m_projects_list.erase( it );
972
973 m_projects.erase( projectPath );
974
975 if( wasActiveProject )
976 {
977 // Immediately reload a null project; this is required until the rest of the application
978 // is refactored to not assume that Prj() always works
979 if( m_projects_list.empty() )
980 LoadProject( "" );
981
982 // Remove the reference in the environment to the previous project
983 wxSetEnv( PROJECT_VAR_NAME, wxS( "" ) );
984
985 // Release lock on the file, in case we had one
986 m_project_lock = nullptr;
987
988 if( m_kiway )
990 }
991
992 return true;
993}
994
995
997{
998 // No MDI yet: First project in the list is the active project
999 wxASSERT_MSG( m_projects_list.size(), wxT( "no project in list" ) );
1000 return *m_projects_list.begin()->get();
1001}
1002
1003
1005{
1006 return !m_projects.empty();
1007}
1008
1009
1010PROJECT* SETTINGS_MANAGER::GetProject( const wxString& aFullPath ) const
1011{
1012 if( m_projects.count( aFullPath ) )
1013 return m_projects.at( aFullPath );
1014
1015 return nullptr;
1016}
1017
1018
1019std::vector<wxString> SETTINGS_MANAGER::GetOpenProjects() const
1020{
1021 std::vector<wxString> ret;
1022
1023 for( const std::pair<const wxString, PROJECT*>& pair : m_projects )
1024 ret.emplace_back( pair.first );
1025
1026 return ret;
1027}
1028
1029
1030bool SETTINGS_MANAGER::SaveProject( const wxString& aFullPath, PROJECT* aProject )
1031{
1032 if( !aProject )
1033 aProject = &Prj();
1034
1035 wxString path = aFullPath;
1036
1037 if( path.empty() )
1038 path = aProject->GetProjectFullName();
1039
1040 // TODO: refactor for MDI
1041 if( aProject->IsReadOnly() )
1042 return false;
1043
1044 if( !m_project_files.count( path ) )
1045 return false;
1046
1048 wxString projectPath = aProject->GetProjectPath();
1049
1050 project->SaveToFile( projectPath );
1051 aProject->GetLocalSettings().SaveToFile( projectPath );
1052
1053 return true;
1054}
1055
1056
1057void SETTINGS_MANAGER::SaveProjectAs( const wxString& aFullPath, PROJECT* aProject )
1058{
1059 if( !aProject )
1060 aProject = &Prj();
1061
1062 wxString oldName = aProject->GetProjectFullName();
1063
1064 if( aFullPath.IsSameAs( oldName ) )
1065 {
1066 SaveProject( aFullPath, aProject );
1067 return;
1068 }
1069
1070 // Changing this will cause UnloadProject to not save over the "old" project when loading below
1071 aProject->setProjectFullName( aFullPath );
1072
1073 wxFileName fn( aFullPath );
1074
1075 PROJECT_FILE* project = m_project_files.at( oldName );
1076
1077 // Ensure read-only flags are copied; this allows doing a "Save As" on a standalong board/sch
1078 // without creating project files if the checkbox is turned off
1079 project->SetReadOnly( aProject->IsReadOnly() );
1080 aProject->GetLocalSettings().SetReadOnly( aProject->IsReadOnly() );
1081
1082 project->SetFilename( fn.GetName() );
1083 project->SaveToFile( fn.GetPath() );
1084
1085 aProject->GetLocalSettings().SetFilename( fn.GetName() );
1086 aProject->GetLocalSettings().SaveToFile( fn.GetPath() );
1087
1088 m_project_files[fn.GetFullPath()] = project;
1089 m_project_files.erase( oldName );
1090
1091 m_projects[fn.GetFullPath()] = m_projects[oldName];
1092 m_projects.erase( oldName );
1093}
1094
1095
1096void SETTINGS_MANAGER::SaveProjectCopy( const wxString& aFullPath, PROJECT* aProject )
1097{
1098 if( !aProject )
1099 aProject = &Prj();
1100
1102 wxString oldName = project->GetFilename();
1103 wxFileName fn( aFullPath );
1104
1105 bool readOnly = project->IsReadOnly();
1106 project->SetReadOnly( false );
1107
1108 project->SetFilename( fn.GetName() );
1109 project->SaveToFile( fn.GetPath() );
1110 project->SetFilename( oldName );
1111
1112 PROJECT_LOCAL_SETTINGS& localSettings = aProject->GetLocalSettings();
1113
1114 localSettings.SetFilename( fn.GetName() );
1115 localSettings.SaveToFile( fn.GetPath() );
1116 localSettings.SetFilename( oldName );
1117
1118 project->SetReadOnly( readOnly );
1119}
1120
1121
1123{
1124 wxFileName fullFn( aProject.GetProjectFullName() );
1125 wxString fn( fullFn.GetName() );
1126
1127 PROJECT_FILE* file = RegisterSettings( new PROJECT_FILE( fn ), false );
1128
1129 m_project_files[aProject.GetProjectFullName()] = file;
1130
1131 aProject.setProjectFile( file );
1132 file->SetProject( &aProject );
1133
1134 wxString path( fullFn.GetPath() );
1135
1136 return file->LoadFromFile( path );
1137}
1138
1139
1141{
1142 if( !aProject )
1143 return false;
1144
1145 wxString name = aProject->GetProjectFullName();
1146
1147 if( !m_project_files.count( name ) )
1148 return false;
1149
1151
1152 auto it = std::find_if( m_settings.begin(), m_settings.end(),
1153 [&file]( const std::unique_ptr<JSON_SETTINGS>& aPtr )
1154 {
1155 return aPtr.get() == file;
1156 } );
1157
1158 if( it != m_settings.end() )
1159 {
1160 wxString projectPath = GetPathForSettingsFile( it->get() );
1161
1162 FlushAndRelease( &aProject->GetLocalSettings(), aSave );
1163
1164 if( aSave )
1165 ( *it )->SaveToFile( projectPath );
1166
1167 m_settings.erase( it );
1168 }
1169
1170 m_project_files.erase( name );
1171
1172 return true;
1173}
1174
1175
1177{
1179}
1180
1181
1182wxString SETTINGS_MANAGER::backupDateTimeFormat = wxT( "%Y-%m-%d_%H%M%S" );
1183
1184
1186{
1187 wxDateTime timestamp = wxDateTime::Now();
1188
1189 wxString fileName = wxString::Format( wxT( "%s-%s" ), Prj().GetProjectName(),
1190 timestamp.Format( backupDateTimeFormat ) );
1191
1192 wxFileName target;
1193 target.SetPath( GetProjectBackupsPath() );
1194 target.SetName( fileName );
1195 target.SetExt( FILEEXT::ArchiveFileExtension );
1196
1197 if( !target.DirExists() && !wxMkdir( target.GetPath() ) )
1198 {
1199 wxLogTrace( traceSettings, wxT( "Could not create project backup path %s" ),
1200 target.GetPath() );
1201 return false;
1202 }
1203
1204 if( !target.IsDirWritable() )
1205 {
1206 wxLogTrace( traceSettings, wxT( "Backup directory %s is not writable" ), target.GetPath() );
1207 return false;
1208 }
1209
1210 wxLogTrace( traceSettings, wxT( "Backing up project to %s" ), target.GetPath() );
1211
1212 PROJECT_ARCHIVER archiver;
1213
1214 return archiver.Archive( Prj().GetProjectPath(), target.GetFullPath(), aReporter );
1215}
1216
1217
1218class VECTOR_INSERT_TRAVERSER : public wxDirTraverser
1219{
1220public:
1221 VECTOR_INSERT_TRAVERSER( std::vector<wxString>& aVec,
1222 std::function<bool( const wxString& )> aCond ) :
1223 m_files( aVec ),
1224 m_condition( aCond )
1225 {
1226 }
1227
1228 wxDirTraverseResult OnFile( const wxString& aFile ) override
1229 {
1230 if( m_condition( aFile ) )
1231 m_files.emplace_back( aFile );
1232
1233 return wxDIR_CONTINUE;
1234 }
1235
1236 wxDirTraverseResult OnDir( const wxString& aDirName ) override
1237 {
1238 return wxDIR_CONTINUE;
1239 }
1240
1241private:
1242 std::vector<wxString>& m_files;
1243
1244 std::function<bool( const wxString& )> m_condition;
1245};
1246
1247
1249{
1251
1252 if( !settings.enabled )
1253 return true;
1254
1255 wxString prefix = Prj().GetProjectName() + '-';
1256
1257 auto modTime =
1258 [&prefix]( const wxString& aFile )
1259 {
1260 wxDateTime dt;
1261 wxString fn( wxFileName( aFile ).GetName() );
1262 fn.Replace( prefix, wxS( "" ) );
1263 dt.ParseFormat( fn, backupDateTimeFormat );
1264 return dt;
1265 };
1266
1267 wxFileName projectPath( Prj().GetProjectPath() );
1268
1269 // Skip backup if project path isn't valid or writable
1270 if( !projectPath.IsOk() || !projectPath.Exists() || !projectPath.IsDirWritable() )
1271 return true;
1272
1273 wxString backupPath = GetProjectBackupsPath();
1274
1275 if( !wxDirExists( backupPath ) )
1276 {
1277 wxLogTrace( traceSettings, wxT( "Backup path %s doesn't exist, creating it" ), backupPath );
1278
1279 if( !wxMkdir( backupPath ) )
1280 {
1281 wxLogTrace( traceSettings, wxT( "Could not create backups path! Skipping backup" ) );
1282 return false;
1283 }
1284 }
1285
1286 wxDir dir( backupPath );
1287
1288 if( !dir.IsOpened() )
1289 {
1290 wxLogTrace( traceSettings, wxT( "Could not open project backups path %s" ), dir.GetName() );
1291 return false;
1292 }
1293
1294 std::vector<wxString> files;
1295
1296 VECTOR_INSERT_TRAVERSER traverser( files,
1297 [&modTime]( const wxString& aFile )
1298 {
1299 return modTime( aFile ).IsValid();
1300 } );
1301
1302 dir.Traverse( traverser, wxT( "*.zip" ) );
1303
1304 // Sort newest-first
1305 std::sort( files.begin(), files.end(),
1306 [&]( const wxString& aFirst, const wxString& aSecond ) -> bool
1307 {
1308 wxDateTime first = modTime( aFirst );
1309 wxDateTime second = modTime( aSecond );
1310
1311 return first.GetTicks() > second.GetTicks();
1312 } );
1313
1314 // Do we even need to back up?
1315 if( !files.empty() )
1316 {
1317 wxDateTime lastTime = modTime( files[0] );
1318
1319 if( lastTime.IsValid() )
1320 {
1321 wxTimeSpan delta = wxDateTime::Now() - modTime( files[0] );
1322
1323 if( delta.IsShorterThan( wxTimeSpan::Seconds( settings.min_interval ) ) )
1324 return true;
1325 }
1326 }
1327
1328 // Now that we know a backup is needed, apply the retention policy
1329
1330 // Step 1: if we're over the total file limit, remove the oldest
1331 if( !files.empty() && settings.limit_total_files > 0 )
1332 {
1333 while( files.size() > static_cast<size_t>( settings.limit_total_files ) )
1334 {
1335 wxRemoveFile( files.back() );
1336 files.pop_back();
1337 }
1338 }
1339
1340 // Step 2: Stay under the total size limit
1341 if( settings.limit_total_size > 0 )
1342 {
1343 wxULongLong totalSize = 0;
1344
1345 for( const wxString& file : files )
1346 totalSize += wxFileName::GetSize( file );
1347
1348 while( !files.empty() && totalSize > static_cast<wxULongLong>( settings.limit_total_size ) )
1349 {
1350 totalSize -= wxFileName::GetSize( files.back() );
1351 wxRemoveFile( files.back() );
1352 files.pop_back();
1353 }
1354 }
1355
1356 // Step 3: Stay under the daily limit
1357 if( settings.limit_daily_files > 0 && files.size() > 1 )
1358 {
1359 wxDateTime day = modTime( files[0] );
1360 int num = 1;
1361
1362 wxASSERT( day.IsValid() );
1363
1364 std::vector<wxString> filesToDelete;
1365
1366 for( size_t i = 1; i < files.size(); i++ )
1367 {
1368 wxDateTime dt = modTime( files[i] );
1369
1370 if( dt.IsSameDate( day ) )
1371 {
1372 num++;
1373
1374 if( num > settings.limit_daily_files )
1375 filesToDelete.emplace_back( files[i] );
1376 }
1377 else
1378 {
1379 day = dt;
1380 num = 1;
1381 }
1382 }
1383
1384 for( const wxString& file : filesToDelete )
1385 wxRemoveFile( file );
1386 }
1387
1388 return BackupProject( aReporter );
1389}
1390
1391
1393{
1395}
const char * name
Definition: DXF_plotter.cpp:57
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
Color settings are a bit different than most of the settings objects in that there can be more than o...
void SetName(const wxString &aName)
static std::vector< COLOR_SETTINGS * > CreateBuiltinColorSettings()
Constructs and returns a list of color settings objects based on the built-in color themes.
static const wxString COLOR_BUILTIN_DEFAULT
AUTO_BACKUP m_Backup
ENVIRONMENT m_Env
std::function< void(const wxFileName &)> m_action
JSON_DIR_TRAVERSER(std::function< void(const wxFileName &)> aAction)
wxDirTraverseResult OnDir(const wxString &dirPath) override
wxDirTraverseResult OnFile(const wxString &aFilePath) override
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....
bool Contains(const std::string &aPath) const
SETTINGS_LOC GetLocation() const
Definition: json_settings.h:80
virtual bool LoadFromFile(const wxString &aDirectory="")
Loads the backing file from disk and then calls Load()
virtual void Load()
Updates the parameters of this object based on the current JSON document contents.
bool IsReadOnly() const
Definition: json_settings.h:84
void SetReadOnly(bool aReadOnly)
Definition: json_settings.h:85
wxString GetFullFilename() const
JSON_SETTINGS_INTERNALS * Internals()
void SetFilename(const wxString &aFilename)
Definition: json_settings.h:77
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
Calls Store() and then writes the contents of the JSON document to a file.
virtual bool Store()
Stores the current parameters into the JSON document represented by this object Note: this doesn't do...
wxString GetFilename() const
Definition: json_settings.h:73
virtual void ProjectChanged()
Calls ProjectChanged() on all KIWAY_PLAYERs.
Definition: kiway.cpp:666
bool Valid() const
Definition: lockfile.h:236
wxDirTraverseResult OnDir(const wxString &dirPath) override
MIGRATION_TRAVERSER(const wxString &aSrcDir, const wxString &aDestDir, bool aMigrateTables)
wxDirTraverseResult OnFile(const wxString &aSrcFilePath) override
static wxString CalculateUserSettingsPath(bool aIncludeVer=true, bool aUseEnv=true)
Determines the base path for user settings files.
Definition: paths.cpp:521
static wxString GetDefault3rdPartyPath()
Gets the default path for PCM packages.
Definition: paths.cpp:130
static wxString GetStockDataPath(bool aRespectRunFromBuildDir=true)
Gets the stock (install) data path, which is the base path for things like scripting,...
Definition: paths.cpp:151
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
Definition: paths.cpp:510
bool Archive(const wxString &aSrcDir, const wxString &aDestFile, REPORTER &aReporter, bool aVerbose=true, bool aIncludeExtraFiles=false)
Creates an archive of the project.
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:70
void SetProject(PROJECT *aProject)
Definition: project_file.h:86
The project local settings are things that are attached to a particular project, but also might be pa...
bool SaveToFile(const wxString &aDirectory="", bool aForce=false) override
Calls Store() and then writes the contents of the JSON document to a file.
Container for project specific data.
Definition: project.h:62
virtual void setProjectFile(PROJECT_FILE *aFile)
Set the backing store file for this project.
Definition: project.h:298
virtual bool IsReadOnly() const
Definition: project.h:142
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition: project.cpp:129
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:135
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition: project.cpp:147
virtual PROJECT_LOCAL_SETTINGS & GetLocalSettings() const
Definition: project.h:172
virtual void setProjectFullName(const wxString &aFullPathAndName)
Set the full directory, basename, and extension of the project.
Definition: project.cpp:105
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:71
static int compareVersions(const std::string &aFirst, const std::string &aSecond)
Compares two settings versions, like "5.99" and "6.0".
std::unique_ptr< LOCKFILE > m_project_lock
Lock for loaded project (expand to multiple once we support MDI)
wxString GetPathForSettingsFile(JSON_SETTINGS *aSettings)
Returns the path a given settings file should be loaded from / stored to.
static std::string GetSettingsVersion()
Parses the current KiCad build version and extracts the major and minor revision to use as the name o...
void SaveProjectAs(const wxString &aFullPath, PROJECT *aProject=nullptr)
Sets the currently loaded project path and saves it (pointers remain valid) Note that this will not m...
JSON_SETTINGS * registerSettings(JSON_SETTINGS *aSettings, bool aLoadNow=true)
static wxString GetUserSettingsPath()
A proxy for PATHS::GetUserSettingsPath rather than fighting swig.
COLOR_SETTINGS * GetColorSettings(const wxString &aName="user")
Retrieves a color settings object that applications can read colors from.
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
void SaveProjectCopy(const wxString &aFullPath, PROJECT *aProject=nullptr)
Saves a copy of the current project under the given path.
bool MigrateIfNeeded()
Handles the initialization of the user settings directory and migration from previous KiCad versions ...
wxString m_migration_source
void SaveColorSettings(COLOR_SETTINGS *aSettings, const std::string &aNamespace="")
Safely saves a COLOR_SETTINGS to disk, preserving any changes outside the given namespace.
bool BackupProject(REPORTER &aReporter) const
Creates a backup archive of the current project.
std::map< wxString, PROJECT * > m_projects
Loaded projects, mapped according to project full name.
static bool extractVersion(const std::string &aVersionString, int *aMajor=nullptr, int *aMinor=nullptr)
Extracts the numeric version from a given settings string.
COLOR_SETTINGS * registerColorSettings(const wxString &aFilename, bool aAbsolutePath=false)
bool m_headless
True if running outside a UI context.
SETTINGS_MANAGER(bool aHeadless=false)
static wxString GetColorSettingsPath()
Returns the path where color scheme files are stored; creating it if missing (normally .
COMMON_SETTINGS * GetCommonSettings() const
Retrieves the common settings shared by all applications.
bool SaveProject(const wxString &aFullPath=wxEmptyString, PROJECT *aProject=nullptr)
Saves a loaded project.
wxString GetProjectBackupsPath() const
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Loads a project or sets up a new project with a specified path.
std::map< wxString, PROJECT_FILE * > m_project_files
Loaded project files, mapped according to project full name.
std::unordered_map< wxString, COLOR_SETTINGS * > m_color_settings
COLOR_SETTINGS * loadColorSettingsByName(const wxString &aName)
Attempts to load a color theme by name (the color theme directory and .json ext are assumed)
bool IsProjectOpen() const
Helper for checking if we have a project open TODO: This should be deprecated along with Prj() once w...
bool GetPreviousVersionPaths(std::vector< wxString > *aName=nullptr)
Retrieves the name of the most recent previous KiCad version that can be found in the user settings d...
static bool IsSettingsPathValid(const wxString &aPath)
Checks if a given path is probably a valid KiCad configuration directory.
std::vector< std::unique_ptr< PROJECT > > m_projects_list
Loaded projects (ownership here)
PROJECT * GetProject(const wxString &aFullPath) const
Retrieves a loaded project by name.
bool UnloadProject(PROJECT *aProject, bool aSave=true)
Saves, unloads and unregisters the given PROJECT.
std::vector< wxString > GetOpenProjects() const
std::vector< std::unique_ptr< JSON_SETTINGS > > m_settings
bool TriggerBackupIfNeeded(REPORTER &aReporter) const
Calls BackupProject if a new backup is needed according to the current backup policy.
bool m_migrateLibraryTables
If true, the symbol and footprint library tables will be migrated from the previous version.
bool unloadProjectFile(PROJECT *aProject, bool aSave)
Optionally saves, and then unloads and unregisters the given PROJECT_FILE.
COLOR_SETTINGS * GetMigratedColorSettings()
Returns a color theme for storing colors migrated from legacy (5.x and earlier) settings,...
std::unordered_map< size_t, JSON_SETTINGS * > m_app_settings_cache
Cache for app settings.
COLOR_SETTINGS * AddNewColorSettings(const wxString &aFilename)
Registers a new color settings object with the given filename.
bool m_ok
True if settings loaded successfully at construction.
void registerBuiltinColorSettings()
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
bool loadProjectFile(PROJECT &aProject)
Registers a PROJECT_FILE and attempts to load it from disk.
static wxString backupDateTimeFormat
void ReloadColorSettings()
Re-scans the color themes directory, reloading any changes it finds.
COMMON_SETTINGS * m_common_settings
KIWAY * m_kiway
The kiway this settings manager interacts with.
void FlushAndRelease(JSON_SETTINGS *aSettings, bool aSave=true)
If the given settings object is registered, save it to disk and unregister it.
wxDirTraverseResult OnFile(const wxString &aFile) override
wxDirTraverseResult OnDir(const wxString &aDirName) override
std::vector< wxString > & m_files
VECTOR_INSERT_TRAVERSER(std::vector< wxString > &aVec, std::function< bool(const wxString &)> aCond)
std::function< bool(const wxString &)> m_condition
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:305
This file is part of the common library.
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition: gestfich.cpp:305
static const std::string ProjectFileExtension
static const std::string LegacyProjectFileExtension
static const std::string ArchiveFileExtension
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
File locking utilities.
This file contains miscellaneous commonly used macros and functions.
STL namespace.
see class PGM_BASE
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition: project.h:39
#define PROJECT_BACKUPS_DIR_SUFFIX
Project settings path will be <projectname> + this.
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:119
int min_interval
Minimum time, in seconds, between subsequent backups.
unsigned long long limit_total_size
Maximum total size of backups (bytes), 0 for unlimited.
int limit_total_files
Maximum number of backup archives to retain.
int limit_daily_files
Maximum files to keep per day, 0 for unlimited.
bool enabled
Automatically back up the project when files are saved.
constexpr int delta
Definition of file extensions used in Kicad.