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
100 template<typename T>
101 T* GetAppSettings( const wxString& aFilename )
102 {
103 T* ret = nullptr;
104 size_t typeHash = typeid( T ).hash_code();
105
106 if( m_app_settings_cache.count( typeHash ) )
107 ret = static_cast<T*>( m_app_settings_cache.at( typeHash ) );
108
109 if( ret )
110 return ret;
111
112#if defined(__clang__)
113 auto it = std::find_if( m_settings.begin(), m_settings.end(),
114 [&]( const std::unique_ptr<JSON_SETTINGS>& aSettings )
115 {
116 return aSettings->GetFilename() == aFilename;
117 } );
118#else
119 auto it = std::find_if( m_settings.begin(), m_settings.end(),
120 []( const std::unique_ptr<JSON_SETTINGS>& aSettings )
121 {
122 return dynamic_cast<T*>( aSettings.get() );
123 } );
124#endif
125
126 if( it != m_settings.end() )
127 {
128 ret = dynamic_cast<T*>( it->get() );
129 }
130 else
131 {
132 throw std::runtime_error( "Tried to GetAppSettings before registering" );
133 }
134
135 m_app_settings_cache[typeHash] = ret;
136
137 return ret;
138 }
139
147 COLOR_SETTINGS* GetColorSettings( const wxString& aName = "user" );
148
149 std::vector<COLOR_SETTINGS*> GetColorSettingsList()
150 {
151 std::vector<COLOR_SETTINGS*> ret;
152
153 for( const std::pair<const wxString, COLOR_SETTINGS*>& entry : m_color_settings )
154 ret.push_back( entry.second );
155
156 std::sort( ret.begin(), ret.end(), []( COLOR_SETTINGS* a, COLOR_SETTINGS* b )
157 {
158 return a->GetName() < b->GetName();
159 } );
160
161 return ret;
162 }
163
173 void SaveColorSettings( COLOR_SETTINGS* aSettings, const std::string& aNamespace = "" );
174
180 COLOR_SETTINGS* AddNewColorSettings( const wxString& aFilename );
181
187 COLOR_SETTINGS* GetMigratedColorSettings();
188
193 COMMON_SETTINGS* GetCommonSettings() const { return m_common_settings; }
194
200 wxString GetPathForSettingsFile( JSON_SETTINGS* aSettings );
201
215 bool MigrateIfNeeded();
216
221 void SetMigrationSource( const wxString& aSource ) { m_migration_source = aSource; }
222
223 void SetMigrateLibraryTables( bool aMigrate = true ) { m_migrateLibraryTables = aMigrate; }
224
233 bool GetPreviousVersionPaths( std::vector<wxString>* aName = nullptr );
234
238 void ReloadColorSettings();
239
246 bool LoadProject( const wxString& aFullPath, bool aSetActive = true );
247
254 bool UnloadProject( PROJECT* aProject, bool aSave = true );
255
261 bool IsProjectOpen() const;
262
267 bool IsProjectOpenNotDummy() const;
268
273 PROJECT& Prj() const;
274
280 PROJECT* GetProject( const wxString& aFullPath ) const;
281
285 std::vector<wxString> GetOpenProjects() const;
286
293 bool SaveProject( const wxString& aFullPath = wxEmptyString, PROJECT* aProject = nullptr );
294
302 void SaveProjectAs( const wxString& aFullPath, PROJECT* aProject = nullptr );
303
310 void SaveProjectCopy( const wxString& aFullPath, PROJECT* aProject = nullptr );
311
315 wxString GetProjectBackupsPath() const;
316
324 bool BackupProject( REPORTER& aReporter, wxFileName& aTarget ) const;
325
331 bool TriggerBackupIfNeeded( REPORTER& aReporter ) const;
332
341 static bool IsSettingsPathValid( const wxString& aPath );
342
347 static wxString GetColorSettingsPath();
348
355 static std::string GetSettingsVersion();
356
360 static wxString GetUserSettingsPath();
361private:
362 JSON_SETTINGS* registerSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true );
363
368 static int compareVersions( const std::string& aFirst, const std::string& aSecond );
369
377 static bool extractVersion( const std::string& aVersionString, int* aMajor = nullptr,
378 int* aMinor = nullptr );
379
385 COLOR_SETTINGS* loadColorSettingsByName( const wxString& aName );
386
387 COLOR_SETTINGS* registerColorSettings( const wxString& aFilename, bool aAbsolutePath = false );
388
389 void loadAllColorSettings();
390
396 bool loadProjectFile( PROJECT& aProject );
397
404 bool unloadProjectFile( PROJECT* aProject, bool aSave );
405
406 // Helper to create built-in colors and register them
407 void registerBuiltinColorSettings();
408
409private:
410
413
416
417 std::vector<std::unique_ptr<JSON_SETTINGS>> m_settings;
418
419 std::unordered_map<wxString, COLOR_SETTINGS*> m_color_settings;
420
422 std::unordered_map<size_t, JSON_SETTINGS*> m_app_settings_cache;
423
424 // Convenience shortcut
426
428
431
433 bool m_ok;
434
436 std::vector<std::unique_ptr<PROJECT>> m_projects_list;
437
439 std::map<wxString, PROJECT*> m_projects;
440
442 std::map<wxString, PROJECT_FILE*> m_project_files;
443
445 std::unique_ptr<LOCKFILE> m_project_lock;
446
447 static wxString backupDateTimeFormat;
448};
449
450#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 * 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)
Returns a handle to the a given settings by type If the settings have already been loaded,...
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:597
#define KICOMMON_API
Definition: kicommon.h:28