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 = dynamic_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
138 COLOR_SETTINGS* GetColorSettings( const wxString& aName = "user" );
139
140 std::vector<COLOR_SETTINGS*> GetColorSettingsList()
141 {
142 std::vector<COLOR_SETTINGS*> ret;
143
144 for( const std::pair<const wxString, COLOR_SETTINGS*>& entry : m_color_settings )
145 ret.push_back( entry.second );
146
147 std::sort( ret.begin(), ret.end(), []( COLOR_SETTINGS* a, COLOR_SETTINGS* b )
148 {
149 return a->GetName() < b->GetName();
150 } );
151
152 return ret;
153 }
154
164 void SaveColorSettings( COLOR_SETTINGS* aSettings, const std::string& aNamespace = "" );
165
171 COLOR_SETTINGS* AddNewColorSettings( const wxString& aFilename );
172
178 COLOR_SETTINGS* GetMigratedColorSettings();
179
184 COMMON_SETTINGS* GetCommonSettings() const { return m_common_settings; }
185
191 wxString GetPathForSettingsFile( JSON_SETTINGS* aSettings );
192
206 bool MigrateIfNeeded();
207
212 void SetMigrationSource( const wxString& aSource ) { m_migration_source = aSource; }
213
214 void SetMigrateLibraryTables( bool aMigrate = true ) { m_migrateLibraryTables = aMigrate; }
215
224 bool GetPreviousVersionPaths( std::vector<wxString>* aName = nullptr );
225
229 void ReloadColorSettings();
230
237 bool LoadProject( const wxString& aFullPath, bool aSetActive = true );
238
245 bool UnloadProject( PROJECT* aProject, bool aSave = true );
246
252 bool IsProjectOpen() const;
253
258 PROJECT& Prj() const;
259
265 PROJECT* GetProject( const wxString& aFullPath ) const;
266
270 std::vector<wxString> GetOpenProjects() const;
271
278 bool SaveProject( const wxString& aFullPath = wxEmptyString, PROJECT* aProject = nullptr );
279
287 void SaveProjectAs( const wxString& aFullPath, PROJECT* aProject = nullptr );
288
295 void SaveProjectCopy( const wxString& aFullPath, PROJECT* aProject = nullptr );
296
300 wxString GetProjectBackupsPath() const;
301
309 bool BackupProject( REPORTER& aReporter, wxFileName& aTarget ) const;
310
316 bool TriggerBackupIfNeeded( REPORTER& aReporter ) const;
317
326 static bool IsSettingsPathValid( const wxString& aPath );
327
332 static wxString GetColorSettingsPath();
333
340 static std::string GetSettingsVersion();
341
345 static wxString GetUserSettingsPath();
346private:
347 JSON_SETTINGS* registerSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true );
348
353 static int compareVersions( const std::string& aFirst, const std::string& aSecond );
354
362 static bool extractVersion( const std::string& aVersionString, int* aMajor = nullptr,
363 int* aMinor = nullptr );
364
370 COLOR_SETTINGS* loadColorSettingsByName( const wxString& aName );
371
372 COLOR_SETTINGS* registerColorSettings( const wxString& aFilename, bool aAbsolutePath = false );
373
374 void loadAllColorSettings();
375
381 bool loadProjectFile( PROJECT& aProject );
382
389 bool unloadProjectFile( PROJECT* aProject, bool aSave );
390
391 // Helper to create built-in colors and register them
392 void registerBuiltinColorSettings();
393
394private:
395
398
401
402 std::vector<std::unique_ptr<JSON_SETTINGS>> m_settings;
403
404 std::unordered_map<wxString, COLOR_SETTINGS*> m_color_settings;
405
407 std::unordered_map<size_t, JSON_SETTINGS*> m_app_settings_cache;
408
409 // Convenience shortcut
411
413
416
418 bool m_ok;
419
421 std::vector<std::unique_ptr<PROJECT>> m_projects_list;
422
424 std::map<wxString, PROJECT*> m_projects;
425
427 std::map<wxString, PROJECT_FILE*> m_project_files;
428
430 std::unique_ptr<LOCKFILE> m_project_lock;
431
432 static wxString backupDateTimeFormat;
433};
434
435#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:279
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:70
Container for project specific data.
Definition: project.h:62
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:71
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.
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