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
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
100 void FlushAndRelease( JSON_SETTINGS* aSettings, bool aSave = true );
101
105 void ResetToDefaults();
106
110 void ClearFileHistory();
111
124 template<typename T>
125 T* GetAppSettings( const char* aFilename )
126 {
127 std::lock_guard lock( m_app_settings_mutex );
128
129 T* ret = nullptr;
130 size_t typeHash = typeid( T ).hash_code();
131
132 if( m_app_settings_cache.count( typeHash ) )
133 ret = static_cast<T*>( m_app_settings_cache.at( typeHash ) );
134
135 if( ret )
136 return ret;
137
138#if defined(__clang__)
139 auto it = std::find_if( m_settings.begin(), m_settings.end(),
140 [&]( const std::unique_ptr<JSON_SETTINGS>& aSettings )
141 {
142 return aSettings->GetFilename() == aFilename;
143 } );
144#else
145 auto it = std::find_if( m_settings.begin(), m_settings.end(),
146 []( const std::unique_ptr<JSON_SETTINGS>& aSettings )
147 {
148 return dynamic_cast<T*>( aSettings.get() );
149 } );
150#endif
151
152 if( it != m_settings.end() )
153 {
154 // Do NOT use dynamic_cast here. CLang will think it's the wrong class across
155 // compile boundaries and return nullptr.
156 ret = static_cast<T*>( it->get() );
157 }
158 else
159 {
160 wxFAIL_MSG( wxString::Format( "Tried to GetAppSettings before registering: %s", aFilename ) );
161 }
162
163 m_app_settings_cache[typeHash] = ret;
164
165 return ret;
166 }
167
180 template<typename T>
181 T* GetToolbarSettings( const wxString& aFilename )
182 {
183 T* ret = nullptr;
184
185#if defined(__clang__)
186 auto it = std::find_if( m_settings.begin(), m_settings.end(),
187 [&]( const std::unique_ptr<JSON_SETTINGS>& aSettings )
188 {
189 return aSettings->GetFilename() == aFilename;
190 } );
191#else
192 auto it = std::find_if( m_settings.begin(), m_settings.end(),
193 []( const std::unique_ptr<JSON_SETTINGS>& aSettings )
194 {
195 return dynamic_cast<T*>( aSettings.get() );
196 } );
197#endif
198
199 if( it != m_settings.end() )
200 {
201 // Do NOT use dynamic_cast here. CLang will think it's the wrong class across
202 // compile boundaries and return nullptr.
203 ret = static_cast<T*>( it->get() );
204 }
205 else
206 {
207 ret = RegisterSettings( new T );
208 }
209
210 return ret;
211 }
212
221 COLOR_SETTINGS* GetColorSettings( const wxString& aName );
222
223 std::vector<COLOR_SETTINGS*> GetColorSettingsList();
224
234 void SaveColorSettings( COLOR_SETTINGS* aSettings, const std::string& aNamespace = "" );
235
242 COLOR_SETTINGS* AddNewColorSettings( const wxString& aFilename );
243
252 COLOR_SETTINGS* GetMigratedColorSettings();
253
260
267 wxString GetPathForSettingsFile( JSON_SETTINGS* aSettings );
268
274 bool MigrateFromPreviousVersion( const wxString& aSourcePath );
275
286 bool GetPreviousVersionPaths( std::vector<wxString>* aName = nullptr );
287
291 void ReloadColorSettings();
292
300 bool LoadProject( const wxString& aFullPath, bool aSetActive = true );
301
309 bool UnloadProject( PROJECT* aProject, bool aSave = true );
310
318 bool IsProjectOpen() const;
319
325 bool IsProjectOpenNotDummy() const;
326
332 PROJECT& Prj() const;
333
340 PROJECT* GetProject( const wxString& aFullPath ) const;
341
347 bool IsProjectLoaded( PROJECT* aProject ) const;
348
354 PROJECT* GetProjectForPath( const wxString& aProjectPath ) const;
355
359 std::vector<wxString> GetOpenProjects() const;
360
364 void SyncGlobalFieldNameTemplatesToProjects();
365
373 bool SaveProject( const wxString& aFullPath = wxEmptyString, PROJECT* aProject = nullptr );
374
384 void SaveProjectAs( const wxString& aFullPath, PROJECT* aProject = nullptr );
385
395 bool SaveProjectCopy( const wxString& aFullPath, PROJECT* aProject = nullptr );
396
400 wxString GetProjectBackupsPath() const;
401
412 wxString GetBackupRootForProject( const PROJECT* aProject = nullptr ) const;
413
419 wxString GetLocalHistoryDirForProject( const PROJECT* aProject = nullptr ) const;
420
427 wxString GetLocalHistoryDirForPath( const wxString& aProjectPath ) const;
428
436 wxString GetAutosaveRootForProject( const PROJECT* aProject = nullptr ) const;
437
446 bool BackupProject( REPORTER& aReporter, wxFileName& aTarget ) const;
447
454 bool TriggerBackupIfNeeded( REPORTER& aReporter ) const;
455
465 static bool IsSettingsPathValid( const wxString& aPath );
466
471 static wxString GetColorSettingsPath();
472
477 static wxString GetToolbarSettingsPath();
478
485 static std::string GetSettingsVersion();
486
490 static wxString GetUserSettingsPath();
491
492private:
493 JSON_SETTINGS* registerSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true );
494
500 static int compareVersions( const std::string& aFirst, const std::string& aSecond );
501
510 static bool extractVersion( const std::string& aVersionString, int* aMajor = nullptr,
511 int* aMinor = nullptr );
512
519 COLOR_SETTINGS* loadColorSettingsByName( const wxString& aName );
520
521 COLOR_SETTINGS* registerColorSettings( const wxString& aFilename, bool aAbsolutePath = false );
522
523 void loadAllColorSettings();
524
530 static wxString projectKeySuffix( const PROJECT* aProject );
531
535 const PROJECT& resolveProject( const PROJECT* aProject ) const;
536
543 bool loadProjectFile( PROJECT& aProject );
544
552 bool unloadProjectFile( PROJECT* aProject, bool aSave );
553
555 void registerBuiltinColorSettings();
556
557private:
558
561
562 std::vector<std::unique_ptr<JSON_SETTINGS>> m_settings;
563
564 std::unordered_map<wxString, COLOR_SETTINGS*> m_color_settings;
565
567 std::unordered_map<size_t, JSON_SETTINGS*> m_app_settings_cache;
569
570 // Convenience shortcut
572
575
577 bool m_ok;
578
580 std::vector<std::unique_ptr<PROJECT>> m_projects_list;
581
583 std::map<wxString, PROJECT*> m_projects;
584
586 std::map<wxString, PROJECT_FILE*> m_project_files;
587
588 static wxString backupDateTimeFormat;
589};
590
591
592template<typename T>
593T* GetAppSettings( const char* aFilename )
594{
595 if( PGM_BASE* pgm = PgmOrNull() )
596 return pgm->GetSettingsManager().GetAppSettings<T>( aFilename );
597
598 return nullptr;
599}
600
601
602template<typename T>
603T* GetToolbarSettings( const wxString& aFilename )
604{
605 if( PGM_BASE* pgm = PgmOrNull() )
606 return pgm->GetSettingsManager().GetToolbarSettings<T>( aFilename );
607
608 return nullptr;
609}
610
611
612inline COLOR_SETTINGS* GetColorSettings( const wxString& aName )
613{
614 return Pgm().GetSettingsManager().GetColorSettings( aName );
615}
616
617#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:348
Advisory lock over a file, taken by writing a sibling lock file and holding an exclusive lock on it f...
Definition lockfile.h:54
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:733
#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)