KiCad PCB EDA Suite
Loading...
Searching...
No Matches
settings_manager.h
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 along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef _SETTINGS_MANAGER_H
22#define _SETTINGS_MANAGER_H
23
24#include <algorithm>
25#include <mutex>
26#include <typeinfo>
27#include <core/wx_stl_compat.h> // for wxString hash
29#include <pgm_base.h>
30
31class COLOR_SETTINGS;
32class COMMON_SETTINGS;
33class KIWAY;
34class PROJECT;
35class PROJECT_FILE;
36class REPORTER;
37class wxSingleInstanceChecker;
38class wxFileName;
39class LOCKFILE;
40
41
43#define PROJECT_BACKUPS_DIR_SUFFIX wxT( "-backups" )
44
45#define DEFAULT_THEME wxString( wxT( "user" ) )
46
47
49{
50public:
51 SETTINGS_MANAGER( bool aHeadless = false );
52
54
58 bool IsOK() { return m_ok; }
59
65 void SetKiway( KIWAY* aKiway ) { m_kiway = aKiway; }
66
73 template<typename T>
74 T* RegisterSettings( T* aSettings, bool aLoadNow = true )
75 {
76 return static_cast<T*>( registerSettings( aSettings, aLoadNow ) );
77 }
78
79 void Load();
80
81 void Load( JSON_SETTINGS* aSettings );
82
83 void Save();
84
85 void Save( JSON_SETTINGS* aSettings );
86
92 void FlushAndRelease( JSON_SETTINGS* aSettings, bool aSave = true );
93
106 template<typename T>
107 T* GetAppSettings( const char* aFilename )
108 {
109 std::lock_guard lock( m_app_settings_mutex );
110
111 T* ret = nullptr;
112 size_t typeHash = typeid( T ).hash_code();
113
114 if( m_app_settings_cache.count( typeHash ) )
115 ret = static_cast<T*>( m_app_settings_cache.at( typeHash ) );
116
117 if( ret )
118 return ret;
119
120#if defined(__clang__)
121 auto it = std::find_if( m_settings.begin(), m_settings.end(),
122 [&]( const std::unique_ptr<JSON_SETTINGS>& aSettings )
123 {
124 return aSettings->GetFilename() == aFilename;
125 } );
126#else
127 auto it = std::find_if( m_settings.begin(), m_settings.end(),
128 []( const std::unique_ptr<JSON_SETTINGS>& aSettings )
129 {
130 return dynamic_cast<T*>( aSettings.get() );
131 } );
132#endif
133
134 if( it != m_settings.end() )
135 {
136 // Do NOT use dynamic_cast here. CLang will think it's the wrong class across
137 // compile boundaries and return nullptr.
138 ret = static_cast<T*>( it->get() );
139 }
140 else
141 {
142 wxFAIL_MSG( "Tried to GetAppSettings before registering" );
143 }
144
145 m_app_settings_cache[typeHash] = ret;
146
147 return ret;
148 }
149
162 template<typename T>
163 T* GetToolbarSettings( const wxString& aFilename )
164 {
165 T* ret = nullptr;
166
167#if defined(__clang__)
168 auto it = std::find_if( m_settings.begin(), m_settings.end(),
169 [&]( const std::unique_ptr<JSON_SETTINGS>& aSettings )
170 {
171 return aSettings->GetFilename() == aFilename;
172 } );
173#else
174 auto it = std::find_if( m_settings.begin(), m_settings.end(),
175 []( const std::unique_ptr<JSON_SETTINGS>& aSettings )
176 {
177 return dynamic_cast<T*>( aSettings.get() );
178 } );
179#endif
180
181 if( it != m_settings.end() )
182 {
183 // Do NOT use dynamic_cast here. CLang will think it's the wrong class across
184 // compile boundaries and return nullptr.
185 ret = static_cast<T*>( it->get() );
186 }
187 else
188 {
189 ret = RegisterSettings( new T );
190 }
191
192 return ret;
193 }
194
203 COLOR_SETTINGS* GetColorSettings( const wxString& aName );
204
205 std::vector<COLOR_SETTINGS*> GetColorSettingsList()
206 {
207 std::vector<COLOR_SETTINGS*> ret;
208
209 for( const std::pair<const wxString, COLOR_SETTINGS*>& entry : m_color_settings )
210 ret.push_back( entry.second );
211
212 std::sort( ret.begin(), ret.end(), []( COLOR_SETTINGS* a, COLOR_SETTINGS* b )
213 {
214 return a->GetName() < b->GetName();
215 } );
216
217 return ret;
218 }
219
229 void SaveColorSettings( COLOR_SETTINGS* aSettings, const std::string& aNamespace = "" );
230
237 COLOR_SETTINGS* AddNewColorSettings( const wxString& aFilename );
238
247 COLOR_SETTINGS* GetMigratedColorSettings();
248
254 COMMON_SETTINGS* GetCommonSettings() const { return m_common_settings; }
255
262 wxString GetPathForSettingsFile( JSON_SETTINGS* aSettings );
263
277 bool MigrateIfNeeded();
278
284 void SetMigrationSource( const wxString& aSource ) { m_migration_source = aSource; }
285
286 void SetMigrateLibraryTables( bool aMigrate = true ) { m_migrateLibraryTables = aMigrate; }
287
298 bool GetPreviousVersionPaths( std::vector<wxString>* aName = nullptr );
299
303 void ReloadColorSettings();
304
312 bool LoadProject( const wxString& aFullPath, bool aSetActive = true );
313
321 bool UnloadProject( PROJECT* aProject, bool aSave = true );
322
330 bool IsProjectOpen() const;
331
337 bool IsProjectOpenNotDummy() const;
338
344 PROJECT& Prj() const;
345
352 PROJECT* GetProject( const wxString& aFullPath ) const;
353
357 std::vector<wxString> GetOpenProjects() const;
358
366 bool SaveProject( const wxString& aFullPath = wxEmptyString, PROJECT* aProject = nullptr );
367
377 void SaveProjectAs( const wxString& aFullPath, PROJECT* aProject = nullptr );
378
387 void SaveProjectCopy( const wxString& aFullPath, PROJECT* aProject = nullptr );
388
392 wxString GetProjectBackupsPath() const;
393
402 bool BackupProject( REPORTER& aReporter, wxFileName& aTarget ) const;
403
410 bool TriggerBackupIfNeeded( REPORTER& aReporter ) const;
411
421 static bool IsSettingsPathValid( const wxString& aPath );
422
427 static wxString GetColorSettingsPath();
428
433 static wxString GetToolbarSettingsPath();
434
441 static std::string GetSettingsVersion();
442
446 static wxString GetUserSettingsPath();
447
448private:
449 JSON_SETTINGS* registerSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true );
450
456 static int compareVersions( const std::string& aFirst, const std::string& aSecond );
457
466 static bool extractVersion( const std::string& aVersionString, int* aMajor = nullptr,
467 int* aMinor = nullptr );
468
475 COLOR_SETTINGS* loadColorSettingsByName( const wxString& aName );
476
477 COLOR_SETTINGS* registerColorSettings( const wxString& aFilename, bool aAbsolutePath = false );
478
479 void loadAllColorSettings();
480
487 bool loadProjectFile( PROJECT& aProject );
488
496 bool unloadProjectFile( PROJECT* aProject, bool aSave );
497
499 void registerBuiltinColorSettings();
500
501private:
502
505
508
509 std::vector<std::unique_ptr<JSON_SETTINGS>> m_settings;
510
511 std::unordered_map<wxString, COLOR_SETTINGS*> m_color_settings;
512
514 std::unordered_map<size_t, JSON_SETTINGS*> m_app_settings_cache;
516
517 // Convenience shortcut
519
521
524
526 bool m_ok;
527
529 std::vector<std::unique_ptr<PROJECT>> m_projects_list;
530
532 std::map<wxString, PROJECT*> m_projects;
533
535 std::map<wxString, PROJECT_FILE*> m_project_files;
536
537 static wxString backupDateTimeFormat;
538};
539
540
541template<typename T>
542T* GetAppSettings( const char* aFilename )
543{
544 if( PGM_BASE* pgm = PgmOrNull() )
545 return pgm->GetSettingsManager().GetAppSettings<T>( aFilename );
546
547 return nullptr;
548}
549
550
551template<typename T>
552T* GetToolbarSettings( const wxString& aFilename )
553{
554 if( PGM_BASE* pgm = PgmOrNull() )
555 return pgm->GetSettingsManager().GetToolbarSettings<T>( aFilename );
556
557 return nullptr;
558}
559
560
561inline COLOR_SETTINGS* GetColorSettings( const wxString& aName )
562{
563 return Pgm().GetSettingsManager().GetColorSettings( aName );
564}
565
566#endif
KICAD_PLUGIN_EXPORT SCENEGRAPH * Load(char const *aFileName)
Read a model file and creates a generic display structure.
Color settings are a bit different than most of the settings objects in that there can be more than o...
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:286
Container for data for KiCad programs.
Definition: pgm_base.h:103
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:125
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:68
Container for project specific data.
Definition: project.h:65
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:73
T * GetAppSettings(const char *aFilename)
Return a handle to the a given settings by type.
void SetMigrateLibraryTables(bool aMigrate=true)
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Take ownership of the pointer passed in.
wxString m_migration_source
T * GetToolbarSettings(const wxString &aFilename)
Return a handle to the given toolbar settings.
std::map< wxString, PROJECT * > m_projects
Loaded projects, mapped according to project full name.
bool m_headless
True if running outside a UI context.
COLOR_SETTINGS * GetColorSettings(const wxString &aName)
Retrieve a color settings object that applications can read colors from.
COMMON_SETTINGS * GetCommonSettings() const
Retrieve the common settings shared by all applications.
void SetMigrationSource(const wxString &aSource)
Helper for DIALOG_MIGRATE_SETTINGS to specify a source for migration.
std::mutex m_app_settings_mutex
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
void SetKiway(KIWAY *aKiway)
Associate this setting manager with the given Kiway.
std::vector< COLOR_SETTINGS * > GetColorSettingsList()
std::vector< std::unique_ptr< PROJECT > > m_projects_list
Loaded projects (ownership here).
std::vector< std::unique_ptr< JSON_SETTINGS > > m_settings
bool m_migrateLibraryTables
If true, the symbol and footprint library tables will be migrated from the previous version.
std::unordered_map< size_t, JSON_SETTINGS * > m_app_settings_cache
Cache for app settings.
bool m_ok
True if settings loaded successfully at construction.
static wxString backupDateTimeFormat
COMMON_SETTINGS * m_common_settings
KIWAY * m_kiway
The kiway this settings manager interacts with.
PROJECT & Prj()
Definition: kicad.cpp:608
#define KICOMMON_API
Definition: kicommon.h:28
PGM_BASE & Pgm()
The global program "get" accessor.
Definition: pgm_base.cpp:902
PGM_BASE * PgmOrNull()
Return a reference that can be nullptr when running a shared lib from a script, not from a kicad app.
Definition: pgm_base.cpp:910
see class PGM_BASE
COLOR_SETTINGS * GetColorSettings(const wxString &aName)
T * GetToolbarSettings(const wxString &aFilename)
T * GetAppSettings(const char *aFilename)