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 {
224 std::vector<COLOR_SETTINGS*> ret;
225
226 for( const std::pair<const wxString, COLOR_SETTINGS*>& entry : m_color_settings )
227 ret.push_back( entry.second );
228
229 std::sort( ret.begin(), ret.end(), []( COLOR_SETTINGS* a, COLOR_SETTINGS* b )
230 {
231 return a->GetName() < b->GetName();
232 } );
233
234 return ret;
235 }
236
246 void SaveColorSettings( COLOR_SETTINGS* aSettings, const std::string& aNamespace = "" );
247
254 COLOR_SETTINGS* AddNewColorSettings( const wxString& aFilename );
255
264 COLOR_SETTINGS* GetMigratedColorSettings();
265
272
279 wxString GetPathForSettingsFile( JSON_SETTINGS* aSettings );
280
286 bool MigrateFromPreviousVersion( const wxString& aSourcePath );
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
506 std::vector<std::unique_ptr<JSON_SETTINGS>> m_settings;
507
508 std::unordered_map<wxString, COLOR_SETTINGS*> m_color_settings;
509
511 std::unordered_map<size_t, JSON_SETTINGS*> m_app_settings_cache;
513
514 // Convenience shortcut
516
519
521 bool m_ok;
522
524 std::vector<std::unique_ptr<PROJECT>> m_projects_list;
525
527 std::map<wxString, PROJECT*> m_projects;
528
530 std::map<wxString, PROJECT_FILE*> m_project_files;
531
532 static wxString backupDateTimeFormat;
533};
534
535
536template<typename T>
537T* GetAppSettings( const char* aFilename )
538{
539 if( PGM_BASE* pgm = PgmOrNull() )
540 return pgm->GetSettingsManager().GetAppSettings<T>( aFilename );
541
542 return nullptr;
543}
544
545
546template<typename T>
547T* GetToolbarSettings( const wxString& aFilename )
548{
549 if( PGM_BASE* pgm = PgmOrNull() )
550 return pgm->GetSettingsManager().GetToolbarSettings<T>( aFilename );
551
552 return nullptr;
553}
554
555
556inline COLOR_SETTINGS* GetColorSettings( const wxString& aName )
557{
558 return Pgm().GetSettingsManager().GetColorSettings( aName );
559}
560
561#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:292
Container for data for KiCad programs.
Definition pgm_base.h:106
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:128
The backing store for a PROJECT, in JSON format.
Container for project specific data.
Definition project.h:66
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< 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:623
#define KICOMMON_API
Definition kicommon.h:28
PGM_BASE & Pgm()
The global program "get" accessor.
Definition pgm_base.cpp:946
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:954
see class PGM_BASE
COLOR_SETTINGS * GetColorSettings(const wxString &aName)
T * GetToolbarSettings(const wxString &aFilename)
T * GetAppSettings(const char *aFilename)