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
97 void ResetToDefaults();
98
102 void ClearFileHistory();
103
116 template<typename T>
117 T* GetAppSettings( const char* aFilename )
118 {
119 std::lock_guard lock( m_app_settings_mutex );
120
121 T* ret = nullptr;
122 size_t typeHash = typeid( T ).hash_code();
123
124 if( m_app_settings_cache.count( typeHash ) )
125 ret = static_cast<T*>( m_app_settings_cache.at( typeHash ) );
126
127 if( ret )
128 return ret;
129
130#if defined(__clang__)
131 auto it = std::find_if( m_settings.begin(), m_settings.end(),
132 [&]( const std::unique_ptr<JSON_SETTINGS>& aSettings )
133 {
134 return aSettings->GetFilename() == aFilename;
135 } );
136#else
137 auto it = std::find_if( m_settings.begin(), m_settings.end(),
138 []( const std::unique_ptr<JSON_SETTINGS>& aSettings )
139 {
140 return dynamic_cast<T*>( aSettings.get() );
141 } );
142#endif
143
144 if( it != m_settings.end() )
145 {
146 // Do NOT use dynamic_cast here. CLang will think it's the wrong class across
147 // compile boundaries and return nullptr.
148 ret = static_cast<T*>( it->get() );
149 }
150 else
151 {
152 wxFAIL_MSG( "Tried to GetAppSettings before registering" );
153 }
154
155 m_app_settings_cache[typeHash] = ret;
156
157 return ret;
158 }
159
172 template<typename T>
173 T* GetToolbarSettings( const wxString& aFilename )
174 {
175 T* ret = nullptr;
176
177#if defined(__clang__)
178 auto it = std::find_if( m_settings.begin(), m_settings.end(),
179 [&]( const std::unique_ptr<JSON_SETTINGS>& aSettings )
180 {
181 return aSettings->GetFilename() == aFilename;
182 } );
183#else
184 auto it = std::find_if( m_settings.begin(), m_settings.end(),
185 []( const std::unique_ptr<JSON_SETTINGS>& aSettings )
186 {
187 return dynamic_cast<T*>( aSettings.get() );
188 } );
189#endif
190
191 if( it != m_settings.end() )
192 {
193 // Do NOT use dynamic_cast here. CLang will think it's the wrong class across
194 // compile boundaries and return nullptr.
195 ret = static_cast<T*>( it->get() );
196 }
197 else
198 {
199 ret = RegisterSettings( new T );
200 }
201
202 return ret;
203 }
204
213 COLOR_SETTINGS* GetColorSettings( const wxString& aName );
214
215 std::vector<COLOR_SETTINGS*> GetColorSettingsList()
216 {
217 std::vector<COLOR_SETTINGS*> ret;
218
219 for( const std::pair<const wxString, COLOR_SETTINGS*>& entry : m_color_settings )
220 ret.push_back( entry.second );
221
222 std::sort( ret.begin(), ret.end(), []( COLOR_SETTINGS* a, COLOR_SETTINGS* b )
223 {
224 return a->GetName() < b->GetName();
225 } );
226
227 return ret;
228 }
229
239 void SaveColorSettings( COLOR_SETTINGS* aSettings, const std::string& aNamespace = "" );
240
247 COLOR_SETTINGS* AddNewColorSettings( const wxString& aFilename );
248
257 COLOR_SETTINGS* GetMigratedColorSettings();
258
265
272 wxString GetPathForSettingsFile( JSON_SETTINGS* aSettings );
273
287 bool MigrateIfNeeded();
288
294 void SetMigrationSource( const wxString& aSource ) { m_migration_source = aSource; }
295
296 void SetMigrateLibraryTables( bool aMigrate = true ) { m_migrateLibraryTables = aMigrate; }
297
308 bool GetPreviousVersionPaths( std::vector<wxString>* aName = nullptr );
309
313 void ReloadColorSettings();
314
322 bool LoadProject( const wxString& aFullPath, bool aSetActive = true );
323
331 bool UnloadProject( PROJECT* aProject, bool aSave = true );
332
340 bool IsProjectOpen() const;
341
347 bool IsProjectOpenNotDummy() const;
348
354 PROJECT& Prj() const;
355
362 PROJECT* GetProject( const wxString& aFullPath ) const;
363
367 std::vector<wxString> GetOpenProjects() const;
368
376 bool SaveProject( const wxString& aFullPath = wxEmptyString, PROJECT* aProject = nullptr );
377
387 void SaveProjectAs( const wxString& aFullPath, PROJECT* aProject = nullptr );
388
397 void SaveProjectCopy( const wxString& aFullPath, PROJECT* aProject = nullptr );
398
402 wxString GetProjectBackupsPath() const;
403
412 bool BackupProject( REPORTER& aReporter, wxFileName& aTarget ) const;
413
420 bool TriggerBackupIfNeeded( REPORTER& aReporter ) const;
421
431 static bool IsSettingsPathValid( const wxString& aPath );
432
437 static wxString GetColorSettingsPath();
438
443 static wxString GetToolbarSettingsPath();
444
451 static std::string GetSettingsVersion();
452
456 static wxString GetUserSettingsPath();
457
458private:
459 JSON_SETTINGS* registerSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true );
460
466 static int compareVersions( const std::string& aFirst, const std::string& aSecond );
467
476 static bool extractVersion( const std::string& aVersionString, int* aMajor = nullptr,
477 int* aMinor = nullptr );
478
485 COLOR_SETTINGS* loadColorSettingsByName( const wxString& aName );
486
487 COLOR_SETTINGS* registerColorSettings( const wxString& aFilename, bool aAbsolutePath = false );
488
489 void loadAllColorSettings();
490
497 bool loadProjectFile( PROJECT& aProject );
498
506 bool unloadProjectFile( PROJECT* aProject, bool aSave );
507
509 void registerBuiltinColorSettings();
510
511private:
512
515
518
519 std::vector<std::unique_ptr<JSON_SETTINGS>> m_settings;
520
521 std::unordered_map<wxString, COLOR_SETTINGS*> m_color_settings;
522
524 std::unordered_map<size_t, JSON_SETTINGS*> m_app_settings_cache;
526
527 // Convenience shortcut
529
531
534
536 bool m_ok;
537
539 std::vector<std::unique_ptr<PROJECT>> m_projects_list;
540
542 std::map<wxString, PROJECT*> m_projects;
543
545 std::map<wxString, PROJECT_FILE*> m_project_files;
546
547 static wxString backupDateTimeFormat;
548};
549
550
551template<typename T>
552T* GetAppSettings( const char* aFilename )
553{
554 if( PGM_BASE* pgm = PgmOrNull() )
555 return pgm->GetSettingsManager().GetAppSettings<T>( aFilename );
556
557 return nullptr;
558}
559
560
561template<typename T>
562T* GetToolbarSettings( const wxString& aFilename )
563{
564 if( PGM_BASE* pgm = PgmOrNull() )
565 return pgm->GetSettingsManager().GetToolbarSettings<T>( aFilename );
566
567 return nullptr;
568}
569
570
571inline COLOR_SETTINGS* GetColorSettings( const wxString& aName )
572{
573 return Pgm().GetSettingsManager().GetColorSettings( aName );
574}
575
576#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.
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)
JSON_SETTINGS * registerSettings(JSON_SETTINGS *aSettings, bool aLoadNow=true)
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Take ownership of the pointer passed in.
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.
SETTINGS_MANAGER(bool aHeadless=false)
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:612
#define KICOMMON_API
Definition kicommon.h:28
PGM_BASE & Pgm()
The global program "get" accessor.
Definition pgm_base.cpp:913
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:921
see class PGM_BASE
COLOR_SETTINGS * GetColorSettings(const wxString &aName)
T * GetToolbarSettings(const wxString &aFilename)
T * GetAppSettings(const char *aFilename)