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
18 * along with this program. If not, see <https://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 wxFileName;
38class LOCKFILE;
39
40
42#define PROJECT_BACKUPS_DIR_SUFFIX wxT( "-backups" )
43
44#define DEFAULT_THEME wxString( wxT( "user" ) )
45
46
48{
49public:
51
53
59 bool SettingsDirectoryValid() const;
60
64 bool IsOK() { return m_ok; }
65
71 void SetKiway( KIWAY* aKiway ) { m_kiway = aKiway; }
72
79 template<typename T>
80 T* RegisterSettings( T* aSettings, bool aLoadNow = true )
81 {
82 return static_cast<T*>( registerSettings( aSettings, aLoadNow ) );
83 }
84
85 void Load();
86
87 void Load( JSON_SETTINGS* aSettings );
88
89 void Save();
90
91 void Save( JSON_SETTINGS* aSettings );
92
98 void FlushAndRelease( JSON_SETTINGS* aSettings, bool aSave = true );
99
103 void ResetToDefaults();
104
108 void ClearFileHistory();
109
122 template<typename T>
123 T* GetAppSettings( const char* aFilename )
124 {
125 std::lock_guard lock( m_app_settings_mutex );
126
127 T* ret = nullptr;
128 size_t typeHash = typeid( T ).hash_code();
129
130 if( m_app_settings_cache.count( typeHash ) )
131 ret = static_cast<T*>( m_app_settings_cache.at( typeHash ) );
132
133 if( ret )
134 return ret;
135
136#if defined(__clang__)
137 auto it = std::find_if( m_settings.begin(), m_settings.end(),
138 [&]( const std::unique_ptr<JSON_SETTINGS>& aSettings )
139 {
140 return aSettings->GetFilename() == aFilename;
141 } );
142#else
143 auto it = std::find_if( m_settings.begin(), m_settings.end(),
144 []( const std::unique_ptr<JSON_SETTINGS>& aSettings )
145 {
146 return dynamic_cast<T*>( aSettings.get() );
147 } );
148#endif
149
150 if( it != m_settings.end() )
151 {
152 // Do NOT use dynamic_cast here. CLang will think it's the wrong class across
153 // compile boundaries and return nullptr.
154 ret = static_cast<T*>( it->get() );
155 }
156 else
157 {
158 wxFAIL_MSG( wxString::Format( "Tried to GetAppSettings before registering: %s", aFilename ) );
159 }
160
161 m_app_settings_cache[typeHash] = ret;
162
163 return ret;
164 }
165
178 template<typename T>
179 T* GetToolbarSettings( const wxString& aFilename )
180 {
181 T* ret = nullptr;
182
183#if defined(__clang__)
184 auto it = std::find_if( m_settings.begin(), m_settings.end(),
185 [&]( const std::unique_ptr<JSON_SETTINGS>& aSettings )
186 {
187 return aSettings->GetFilename() == aFilename;
188 } );
189#else
190 auto it = std::find_if( m_settings.begin(), m_settings.end(),
191 []( const std::unique_ptr<JSON_SETTINGS>& aSettings )
192 {
193 return dynamic_cast<T*>( aSettings.get() );
194 } );
195#endif
196
197 if( it != m_settings.end() )
198 {
199 // Do NOT use dynamic_cast here. CLang will think it's the wrong class across
200 // compile boundaries and return nullptr.
201 ret = static_cast<T*>( it->get() );
202 }
203 else
204 {
205 ret = RegisterSettings( new T );
206 }
207
208 return ret;
209 }
210
219 COLOR_SETTINGS* GetColorSettings( const wxString& aName );
220
221 std::vector<COLOR_SETTINGS*> GetColorSettingsList();
222
232 void SaveColorSettings( COLOR_SETTINGS* aSettings, const std::string& aNamespace = "" );
233
240 COLOR_SETTINGS* AddNewColorSettings( const wxString& aFilename );
241
250 COLOR_SETTINGS* GetMigratedColorSettings();
251
258
265 wxString GetPathForSettingsFile( JSON_SETTINGS* aSettings );
266
272 bool MigrateFromPreviousVersion( const wxString& aSourcePath );
273
284 bool GetPreviousVersionPaths( std::vector<wxString>* aName = nullptr );
285
289 void ReloadColorSettings();
290
298 bool LoadProject( const wxString& aFullPath, bool aSetActive = true );
299
307 bool UnloadProject( PROJECT* aProject, bool aSave = true );
308
316 bool IsProjectOpen() const;
317
323 bool IsProjectOpenNotDummy() const;
324
330 PROJECT& Prj() const;
331
338 PROJECT* GetProject( const wxString& aFullPath ) const;
339
345 bool IsProjectLoaded( PROJECT* aProject ) const;
346
352 PROJECT* GetProjectForPath( const wxString& aProjectPath ) const;
353
357 std::vector<wxString> GetOpenProjects() const;
358
362 void SyncGlobalFieldNameTemplatesToProjects();
363
371 bool SaveProject( const wxString& aFullPath = wxEmptyString, PROJECT* aProject = nullptr );
372
382 void SaveProjectAs( const wxString& aFullPath, PROJECT* aProject = nullptr );
383
393 bool SaveProjectCopy( const wxString& aFullPath, PROJECT* aProject = nullptr );
394
398 wxString GetProjectBackupsPath() const;
399
410 wxString GetBackupRootForProject( const PROJECT* aProject = nullptr ) const;
411
417 wxString GetLocalHistoryDirForProject( const PROJECT* aProject = nullptr ) const;
418
425 wxString GetLocalHistoryDirForPath( const wxString& aProjectPath ) const;
426
434 wxString GetAutosaveRootForProject( const PROJECT* aProject = nullptr ) const;
435
444 bool BackupProject( REPORTER& aReporter, wxFileName& aTarget ) const;
445
452 bool TriggerBackupIfNeeded( REPORTER& aReporter ) const;
453
463 static bool IsSettingsPathValid( const wxString& aPath );
464
469 static wxString GetColorSettingsPath();
470
475 static wxString GetToolbarSettingsPath();
476
483 static std::string GetSettingsVersion();
484
488 static wxString GetUserSettingsPath();
489
490private:
491 JSON_SETTINGS* registerSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true );
492
498 static int compareVersions( const std::string& aFirst, const std::string& aSecond );
499
508 static bool extractVersion( const std::string& aVersionString, int* aMajor = nullptr,
509 int* aMinor = nullptr );
510
517 COLOR_SETTINGS* loadColorSettingsByName( const wxString& aName );
518
519 COLOR_SETTINGS* registerColorSettings( const wxString& aFilename, bool aAbsolutePath = false );
520
521 void loadAllColorSettings();
522
528 static wxString projectKeySuffix( const PROJECT* aProject );
529
533 const PROJECT& resolveProject( const PROJECT* aProject ) const;
534
541 bool loadProjectFile( PROJECT& aProject );
542
550 bool unloadProjectFile( PROJECT* aProject, bool aSave );
551
553 void registerBuiltinColorSettings();
554
555private:
556
559
560 std::vector<std::unique_ptr<JSON_SETTINGS>> m_settings;
561
562 std::unordered_map<wxString, COLOR_SETTINGS*> m_color_settings;
563
565 std::unordered_map<size_t, JSON_SETTINGS*> m_app_settings_cache;
567
568 // Convenience shortcut
570
573
575 bool m_ok;
576
578 std::vector<std::unique_ptr<PROJECT>> m_projects_list;
579
581 std::map<wxString, PROJECT*> m_projects;
582
584 std::map<wxString, PROJECT_FILE*> m_project_files;
585
586 static wxString backupDateTimeFormat;
587};
588
589
590template<typename T>
591T* GetAppSettings( const char* aFilename )
592{
593 if( PGM_BASE* pgm = PgmOrNull() )
594 return pgm->GetSettingsManager().GetAppSettings<T>( aFilename );
595
596 return nullptr;
597}
598
599
600template<typename T>
601T* GetToolbarSettings( const wxString& aFilename )
602{
603 if( PGM_BASE* pgm = PgmOrNull() )
604 return pgm->GetSettingsManager().GetToolbarSettings<T>( aFilename );
605
606 return nullptr;
607}
608
609
610inline COLOR_SETTINGS* GetColorSettings( const wxString& aName )
611{
612 return Pgm().GetSettingsManager().GetColorSettings( aName );
613}
614
615#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:340
Advisory lock over a file, taken by writing a sibling lock file and holding an exclusive lock on it f...
Definition lockfile.h:60
Container for data for KiCad programs.
Definition pgm_base.h:101
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:123
The backing store for a PROJECT, in JSON format.
Container for project specific data.
Definition project.h:63
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:727
#define KICOMMON_API
Definition kicommon.h:27
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)