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 (C) 2020-2021 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
30class COLOR_SETTINGS;
31class COMMON_SETTINGS;
32class KIWAY;
33class PROJECT;
34class PROJECT_FILE;
35class REPORTER;
36class wxSingleInstanceChecker;
37class wxFileName;
38class LOCKFILE;
39
40
42#define PROJECT_BACKUPS_DIR_SUFFIX wxT( "-backups" )
43
44
46{
47public:
48 SETTINGS_MANAGER( bool aHeadless = false );
49
51
55 bool IsOK() { return m_ok; }
56
62 void SetKiway( KIWAY* aKiway ) { m_kiway = aKiway; }
63
69 template<typename T>
70 T* RegisterSettings( T* aSettings, bool aLoadNow = true )
71 {
72 return static_cast<T*>( registerSettings( aSettings, aLoadNow ) );
73 }
74
75 void Load();
76
77 void Load( JSON_SETTINGS* aSettings );
78
79 void Save();
80
81 void Save( JSON_SETTINGS* aSettings );
82
87 void FlushAndRelease( JSON_SETTINGS* aSettings, bool aSave = true );
88
99 template<typename T>
101 {
102 T* ret = nullptr;
103 size_t typeHash = typeid( T ).hash_code();
104
105 if( m_app_settings_cache.count( typeHash ) )
106 ret = static_cast<T*>( m_app_settings_cache.at( typeHash ) );
107
108 if( ret )
109 return ret;
110
111 auto it = std::find_if( m_settings.begin(), m_settings.end(),
112 []( const std::unique_ptr<JSON_SETTINGS>& aSettings )
113 {
114 return dynamic_cast<T*>( aSettings.get() );
115 } );
116
117 if( it != m_settings.end() )
118 {
119 ret = dynamic_cast<T*>( it->get() );
120 }
121 else
122 {
123 throw std::runtime_error( "Tried to GetAppSettings before registering" );
124 }
125
126 m_app_settings_cache[typeHash] = ret;
127
128 return ret;
129 }
130
131 template<typename T>
132 T* GetAppSettings( const wxString& aFilename )
133 {
134#ifndef __WXMAC__
135 return GetAppSettings<T>();
136#else
137 T* ret = nullptr;
138 size_t typeHash = typeid( T ).hash_code();
139
140 if( m_app_settings_cache.count( typeHash ) )
141 ret = static_cast<T*>( m_app_settings_cache.at( typeHash ) );
142
143 if( ret )
144 return ret;
145
146 auto it = std::find_if( m_settings.begin(), m_settings.end(),
147 [&]( const std::unique_ptr<JSON_SETTINGS>& aSettings )
148 {
149 return aSettings->GetFilename() == aFilename;
150 } );
151
152 if( it != m_settings.end() )
153 {
154 ret = static_cast<T*>( it->get() );
155 }
156 else
157 {
158 throw std::runtime_error( "Tried to GetAppSettings before registering" );
159 }
160
161 m_app_settings_cache[typeHash] = ret;
162
163 return ret;
164#endif
165 }
166
174 COLOR_SETTINGS* GetColorSettings( const wxString& aName = "user" );
175
176 std::vector<COLOR_SETTINGS*> GetColorSettingsList()
177 {
178 std::vector<COLOR_SETTINGS*> ret;
179
180 for( const std::pair<const wxString, COLOR_SETTINGS*>& entry : m_color_settings )
181 ret.push_back( entry.second );
182
183 std::sort( ret.begin(), ret.end(), []( COLOR_SETTINGS* a, COLOR_SETTINGS* b )
184 {
185 return a->GetName() < b->GetName();
186 } );
187
188 return ret;
189 }
190
200 void SaveColorSettings( COLOR_SETTINGS* aSettings, const std::string& aNamespace = "" );
201
207 COLOR_SETTINGS* AddNewColorSettings( const wxString& aFilename );
208
214 COLOR_SETTINGS* GetMigratedColorSettings();
215
220 COMMON_SETTINGS* GetCommonSettings() const { return m_common_settings; }
221
227 wxString GetPathForSettingsFile( JSON_SETTINGS* aSettings );
228
242 bool MigrateIfNeeded();
243
248 void SetMigrationSource( const wxString& aSource ) { m_migration_source = aSource; }
249
250 void SetMigrateLibraryTables( bool aMigrate = true ) { m_migrateLibraryTables = aMigrate; }
251
260 bool GetPreviousVersionPaths( std::vector<wxString>* aName = nullptr );
261
265 void ReloadColorSettings();
266
273 bool LoadProject( const wxString& aFullPath, bool aSetActive = true );
274
281 bool UnloadProject( PROJECT* aProject, bool aSave = true );
282
288 bool IsProjectOpen() const;
289
294 bool IsProjectOpenNotDummy() const;
295
300 PROJECT& Prj() const;
301
307 PROJECT* GetProject( const wxString& aFullPath ) const;
308
312 std::vector<wxString> GetOpenProjects() const;
313
320 bool SaveProject( const wxString& aFullPath = wxEmptyString, PROJECT* aProject = nullptr );
321
329 void SaveProjectAs( const wxString& aFullPath, PROJECT* aProject = nullptr );
330
337 void SaveProjectCopy( const wxString& aFullPath, PROJECT* aProject = nullptr );
338
342 wxString GetProjectBackupsPath() const;
343
351 bool BackupProject( REPORTER& aReporter, wxFileName& aTarget ) const;
352
358 bool TriggerBackupIfNeeded( REPORTER& aReporter ) const;
359
368 static bool IsSettingsPathValid( const wxString& aPath );
369
374 static wxString GetColorSettingsPath();
375
382 static std::string GetSettingsVersion();
383
387 static wxString GetUserSettingsPath();
388private:
389 JSON_SETTINGS* registerSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true );
390
395 static int compareVersions( const std::string& aFirst, const std::string& aSecond );
396
404 static bool extractVersion( const std::string& aVersionString, int* aMajor = nullptr,
405 int* aMinor = nullptr );
406
412 COLOR_SETTINGS* loadColorSettingsByName( const wxString& aName );
413
414 COLOR_SETTINGS* registerColorSettings( const wxString& aFilename, bool aAbsolutePath = false );
415
416 void loadAllColorSettings();
417
423 bool loadProjectFile( PROJECT& aProject );
424
431 bool unloadProjectFile( PROJECT* aProject, bool aSave );
432
433 // Helper to create built-in colors and register them
434 void registerBuiltinColorSettings();
435
436private:
437
440
443
444 std::vector<std::unique_ptr<JSON_SETTINGS>> m_settings;
445
446 std::unordered_map<wxString, COLOR_SETTINGS*> m_color_settings;
447
449 std::unordered_map<size_t, JSON_SETTINGS*> m_app_settings_cache;
450
451 // Convenience shortcut
453
455
458
460 bool m_ok;
461
463 std::vector<std::unique_ptr<PROJECT>> m_projects_list;
464
466 std::map<wxString, PROJECT*> m_projects;
467
469 std::map<wxString, PROJECT_FILE*> m_project_files;
470
472 std::unique_ptr<LOCKFILE> m_project_lock;
473
474 static wxString backupDateTimeFormat;
475};
476
477#endif
KICAD_PLUGIN_EXPORT SCENEGRAPH * Load(char const *aFileName)
reads 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:284
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:72
Container for project specific data.
Definition: project.h:64
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:72
std::unique_ptr< LOCKFILE > m_project_lock
Lock for loaded project (expand to multiple once we support MDI)
void SetMigrateLibraryTables(bool aMigrate=true)
T * GetAppSettings()
Returns a handle to the a given settings by type If the settings have already been loaded,...
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
wxString m_migration_source
std::map< wxString, PROJECT * > m_projects
Loaded projects, mapped according to project full name.
bool m_headless
True if running outside a UI context.
COMMON_SETTINGS * GetCommonSettings() const
Retrieves the common settings shared by all applications.
void SetMigrationSource(const wxString &aSource)
Helper for DIALOG_MIGRATE_SETTINGS to specify a source for migration.
T * GetAppSettings(const wxString &aFilename)
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:595
#define KICOMMON_API
Definition: kicommon.h:28