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:
52
54
60 bool SettingsDirectoryValid() const;
61
65 bool IsOK() { return m_ok; }
66
72 void SetKiway( KIWAY* aKiway ) { m_kiway = aKiway; }
73
80 template<typename T>
81 T* RegisterSettings( T* aSettings, bool aLoadNow = true )
82 {
83 return static_cast<T*>( registerSettings( aSettings, aLoadNow ) );
84 }
85
86 void Load();
87
88 void Load( JSON_SETTINGS* aSettings );
89
90 void Save();
91
92 void Save( JSON_SETTINGS* aSettings );
93
99 void FlushAndRelease( JSON_SETTINGS* aSettings, bool aSave = true );
100
104 void ResetToDefaults();
105
109 void ClearFileHistory();
110
123 template<typename T>
124 T* GetAppSettings( const char* aFilename )
125 {
126 std::lock_guard lock( m_app_settings_mutex );
127
128 T* ret = nullptr;
129 size_t typeHash = typeid( T ).hash_code();
130
131 if( m_app_settings_cache.count( typeHash ) )
132 ret = static_cast<T*>( m_app_settings_cache.at( typeHash ) );
133
134 if( ret )
135 return ret;
136
137#if defined(__clang__)
138 auto it = std::find_if( m_settings.begin(), m_settings.end(),
139 [&]( const std::unique_ptr<JSON_SETTINGS>& aSettings )
140 {
141 return aSettings->GetFilename() == aFilename;
142 } );
143#else
144 auto it = std::find_if( m_settings.begin(), m_settings.end(),
145 []( const std::unique_ptr<JSON_SETTINGS>& aSettings )
146 {
147 return dynamic_cast<T*>( aSettings.get() );
148 } );
149#endif
150
151 if( it != m_settings.end() )
152 {
153 // Do NOT use dynamic_cast here. CLang will think it's the wrong class across
154 // compile boundaries and return nullptr.
155 ret = static_cast<T*>( it->get() );
156 }
157 else
158 {
159 wxFAIL_MSG( "Tried to GetAppSettings before registering" );
160 }
161
162 m_app_settings_cache[typeHash] = ret;
163
164 return ret;
165 }
166
179 template<typename T>
180 T* GetToolbarSettings( const wxString& aFilename )
181 {
182 T* ret = nullptr;
183
184#if defined(__clang__)
185 auto it = std::find_if( m_settings.begin(), m_settings.end(),
186 [&]( const std::unique_ptr<JSON_SETTINGS>& aSettings )
187 {
188 return aSettings->GetFilename() == aFilename;
189 } );
190#else
191 auto it = std::find_if( m_settings.begin(), m_settings.end(),
192 []( const std::unique_ptr<JSON_SETTINGS>& aSettings )
193 {
194 return dynamic_cast<T*>( aSettings.get() );
195 } );
196#endif
197
198 if( it != m_settings.end() )
199 {
200 // Do NOT use dynamic_cast here. CLang will think it's the wrong class across
201 // compile boundaries and return nullptr.
202 ret = static_cast<T*>( it->get() );
203 }
204 else
205 {
206 ret = RegisterSettings( new T );
207 }
208
209 return ret;
210 }
211
220 COLOR_SETTINGS* GetColorSettings( const wxString& aName );
221
222 std::vector<COLOR_SETTINGS*> GetColorSettingsList();
223
233 void SaveColorSettings( COLOR_SETTINGS* aSettings, const std::string& aNamespace = "" );
234
241 COLOR_SETTINGS* AddNewColorSettings( const wxString& aFilename );
242
251 COLOR_SETTINGS* GetMigratedColorSettings();
252
259
266 wxString GetPathForSettingsFile( JSON_SETTINGS* aSettings );
267
273 bool MigrateFromPreviousVersion( const wxString& aSourcePath );
274
285 bool GetPreviousVersionPaths( std::vector<wxString>* aName = nullptr );
286
290 void ReloadColorSettings();
291
299 bool LoadProject( const wxString& aFullPath, bool aSetActive = true );
300
308 bool UnloadProject( PROJECT* aProject, bool aSave = true );
309
317 bool IsProjectOpen() const;
318
324 bool IsProjectOpenNotDummy() const;
325
331 PROJECT& Prj() const;
332
339 PROJECT* GetProject( const wxString& aFullPath ) const;
340
344 std::vector<wxString> GetOpenProjects() const;
345
353 bool SaveProject( const wxString& aFullPath = wxEmptyString, PROJECT* aProject = nullptr );
354
364 void SaveProjectAs( const wxString& aFullPath, PROJECT* aProject = nullptr );
365
374 void SaveProjectCopy( const wxString& aFullPath, PROJECT* aProject = nullptr );
375
379 wxString GetProjectBackupsPath() const;
380
389 bool BackupProject( REPORTER& aReporter, wxFileName& aTarget ) const;
390
397 bool TriggerBackupIfNeeded( REPORTER& aReporter ) const;
398
408 static bool IsSettingsPathValid( const wxString& aPath );
409
414 static wxString GetColorSettingsPath();
415
420 static wxString GetToolbarSettingsPath();
421
428 static std::string GetSettingsVersion();
429
433 static wxString GetUserSettingsPath();
434
435private:
436 JSON_SETTINGS* registerSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true );
437
443 static int compareVersions( const std::string& aFirst, const std::string& aSecond );
444
453 static bool extractVersion( const std::string& aVersionString, int* aMajor = nullptr,
454 int* aMinor = nullptr );
455
462 COLOR_SETTINGS* loadColorSettingsByName( const wxString& aName );
463
464 COLOR_SETTINGS* registerColorSettings( const wxString& aFilename, bool aAbsolutePath = false );
465
466 void loadAllColorSettings();
467
474 bool loadProjectFile( PROJECT& aProject );
475
483 bool unloadProjectFile( PROJECT* aProject, bool aSave );
484
486 void registerBuiltinColorSettings();
487
488private:
489
492
493 std::vector<std::unique_ptr<JSON_SETTINGS>> m_settings;
494
495 std::unordered_map<wxString, COLOR_SETTINGS*> m_color_settings;
496
498 std::unordered_map<size_t, JSON_SETTINGS*> m_app_settings_cache;
500
501 // Convenience shortcut
503
506
508 bool m_ok;
509
511 std::vector<std::unique_ptr<PROJECT>> m_projects_list;
512
514 std::map<wxString, PROJECT*> m_projects;
515
517 std::map<wxString, PROJECT_FILE*> m_project_files;
518
519 static wxString backupDateTimeFormat;
520};
521
522
523template<typename T>
524T* GetAppSettings( const char* aFilename )
525{
526 if( PGM_BASE* pgm = PgmOrNull() )
527 return pgm->GetSettingsManager().GetAppSettings<T>( aFilename );
528
529 return nullptr;
530}
531
532
533template<typename T>
534T* GetToolbarSettings( const wxString& aFilename )
535{
536 if( PGM_BASE* pgm = PgmOrNull() )
537 return pgm->GetSettingsManager().GetToolbarSettings<T>( aFilename );
538
539 return nullptr;
540}
541
542
543inline COLOR_SETTINGS* GetColorSettings( const wxString& aName )
544{
545 return Pgm().GetSettingsManager().GetColorSettings( aName );
546}
547
548#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:295
Container for data for KiCad programs.
Definition pgm_base.h:109
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:131
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.
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.
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.
std::mutex m_app_settings_mutex
bool SettingsDirectoryValid() const
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< 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:642
#define KICOMMON_API
Definition kicommon.h:28
PGM_BASE & Pgm()
The global program "get" accessor.
PGM_BASE * PgmOrNull()
Return a reference that can be nullptr when running a shared lib from a script, not from a kicad app.
see class PGM_BASE
COLOR_SETTINGS * GetColorSettings(const wxString &aName)
T * GetToolbarSettings(const wxString &aFilename)
T * GetAppSettings(const char *aFilename)