KiCad PCB EDA Suite
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 <typeinfo>
26#include <core/wx_stl_compat.h> // for wxString hash
28
29class COLOR_SETTINGS;
30class COMMON_SETTINGS;
31class KIWAY;
32class PROJECT;
33class PROJECT_FILE;
34class REPORTER;
35class wxSingleInstanceChecker;
36
37
39#define PROJECT_BACKUPS_DIR_SUFFIX wxT( "-backups" )
40
41
43{
44public:
45 SETTINGS_MANAGER( bool aHeadless = false );
46
48
52 bool IsOK() { return m_ok; }
53
59 void SetKiway( KIWAY* aKiway ) { m_kiway = aKiway; }
60
66 template<typename T>
67 T* RegisterSettings( T* aSettings, bool aLoadNow = true )
68 {
69 return static_cast<T*>( registerSettings( aSettings, aLoadNow ) );
70 }
71
72 void Load();
73
74 void Load( JSON_SETTINGS* aSettings );
75
76 void Save();
77
78 void Save( JSON_SETTINGS* aSettings );
79
84 void FlushAndRelease( JSON_SETTINGS* aSettings, bool aSave = true );
85
96 template<typename T>
97 T* GetAppSettings( bool aLoadNow = true )
98 {
99 T* ret = nullptr;
100 size_t typeHash = typeid( T ).hash_code();
101
102 if( m_app_settings_cache.count( typeHash ) )
103 ret = dynamic_cast<T*>( m_app_settings_cache.at( typeHash ) );
104
105 if( ret )
106 return ret;
107
108 auto it = std::find_if( m_settings.begin(), m_settings.end(),
109 []( const std::unique_ptr<JSON_SETTINGS>& aSettings )
110 {
111 return dynamic_cast<T*>( aSettings.get() );
112 } );
113
114 if( it != m_settings.end() )
115 {
116 ret = dynamic_cast<T*>( it->get() );
117 }
118 else
119 {
120 try
121 {
122 ret = static_cast<T*>( RegisterSettings( new T, aLoadNow ) );
123 }
124 catch( ... )
125 {
126 }
127
128 }
129
130 m_app_settings_cache[typeHash] = ret;
131
132 return ret;
133 }
134
142 COLOR_SETTINGS* GetColorSettings( const wxString& aName = "user" );
143
144 std::vector<COLOR_SETTINGS*> GetColorSettingsList()
145 {
146 std::vector<COLOR_SETTINGS*> ret;
147
148 for( const std::pair<const wxString, COLOR_SETTINGS*>& entry : m_color_settings )
149 ret.push_back( entry.second );
150
151 std::sort( ret.begin(), ret.end(), []( COLOR_SETTINGS* a, COLOR_SETTINGS* b )
152 {
153 return a->GetName() < b->GetName();
154 } );
155
156 return ret;
157 }
158
168 void SaveColorSettings( COLOR_SETTINGS* aSettings, const std::string& aNamespace = "" );
169
175 COLOR_SETTINGS* AddNewColorSettings( const wxString& aFilename );
176
183
189
195 wxString GetPathForSettingsFile( JSON_SETTINGS* aSettings );
196
210 bool MigrateIfNeeded();
211
216 void SetMigrationSource( const wxString& aSource ) { m_migration_source = aSource; }
217
218 void SetMigrateLibraryTables( bool aMigrate = true ) { m_migrateLibraryTables = aMigrate; }
219
228 bool GetPreviousVersionPaths( std::vector<wxString>* aName = nullptr );
229
233 void ReloadColorSettings();
234
241 bool LoadProject( const wxString& aFullPath, bool aSetActive = true );
242
249 bool UnloadProject( PROJECT* aProject, bool aSave = true );
250
256 bool IsProjectOpen() const;
257
262 PROJECT& Prj() const;
263
269 PROJECT* GetProject( const wxString& aFullPath ) const;
270
274 std::vector<wxString> GetOpenProjects() const;
275
282 bool SaveProject( const wxString& aFullPath = wxEmptyString, PROJECT* aProject = nullptr );
283
291 void SaveProjectAs( const wxString& aFullPath, PROJECT* aProject = nullptr );
292
299 void SaveProjectCopy( const wxString& aFullPath, PROJECT* aProject = nullptr );
300
304 wxString GetProjectBackupsPath() const;
305
311 bool BackupProject( REPORTER& aReporter ) const;
312
318 bool TriggerBackupIfNeeded( REPORTER& aReporter ) const;
319
328 static bool IsSettingsPathValid( const wxString& aPath );
329
334 static wxString GetColorSettingsPath();
335
345 static wxString GetUserSettingsPath();
346
353 static std::string GetSettingsVersion();
354
355private:
356
357 JSON_SETTINGS* registerSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true );
358
373 static wxString calculateUserSettingsPath( bool aIncludeVer = true, bool aUseEnv = true );
374
379 static int compareVersions( const std::string& aFirst, const std::string& aSecond );
380
388 static bool extractVersion( const std::string& aVersionString, int* aMajor, int* aMinor );
389
395 COLOR_SETTINGS* loadColorSettingsByName( const wxString& aName );
396
397 COLOR_SETTINGS* registerColorSettings( const wxString& aFilename, bool aAbsolutePath = false );
398
400
406 bool loadProjectFile( PROJECT& aProject );
407
414 bool unloadProjectFile( PROJECT* aProject, bool aSave );
415
416private:
417
420
423
424 std::vector<std::unique_ptr<JSON_SETTINGS>> m_settings;
425
426 std::unordered_map<wxString, COLOR_SETTINGS*> m_color_settings;
427
429 std::unordered_map<size_t, JSON_SETTINGS*> m_app_settings_cache;
430
431 // Convenience shortcut
433
435
438
440 bool m_ok;
441
443 std::vector<std::unique_ptr<PROJECT>> m_projects_list;
444
446 std::map<wxString, PROJECT*> m_projects;
447
449 std::map<wxString, PROJECT_FILE*> m_project_files;
450
452 std::unique_ptr<wxSingleInstanceChecker> m_project_lock;
453
454 static wxString backupDateTimeFormat;
455};
456
457#endif
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:267
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:65
Container for project specific data.
Definition: project.h:63
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:71
static int compareVersions(const std::string &aFirst, const std::string &aSecond)
Compares two settings versions, like "5.99" and "6.0".
T * GetAppSettings(bool aLoadNow=true)
Returns a handle to the a given settings by type If the settings have already been loaded,...
wxString GetPathForSettingsFile(JSON_SETTINGS *aSettings)
Returns the path a given settings file should be loaded from / stored to.
static std::string GetSettingsVersion()
Parses the current KiCad build version and extracts the major and minor revision to use as the name o...
void SaveProjectAs(const wxString &aFullPath, PROJECT *aProject=nullptr)
Sets the currently loaded project path and saves it (pointers remain valid) Note that this will not m...
void SetMigrateLibraryTables(bool aMigrate=true)
JSON_SETTINGS * registerSettings(JSON_SETTINGS *aSettings, bool aLoadNow=true)
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
COLOR_SETTINGS * GetColorSettings(const wxString &aName="user")
Retrieves a color settings object that applications can read colors from.
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
std::unique_ptr< wxSingleInstanceChecker > m_project_lock
Lock for loaded project (expand to multiple once we support MDI)
void SaveProjectCopy(const wxString &aFullPath, PROJECT *aProject=nullptr)
Saves a copy of the current project under the given path.
bool MigrateIfNeeded()
Handles the initialization of the user settings directory and migration from previous KiCad versions ...
wxString m_migration_source
void SaveColorSettings(COLOR_SETTINGS *aSettings, const std::string &aNamespace="")
Safely saves a COLOR_SETTINGS to disk, preserving any changes outside the given namespace.
bool BackupProject(REPORTER &aReporter) const
Creates a backup archive of the current project.
static wxString calculateUserSettingsPath(bool aIncludeVer=true, bool aUseEnv=true)
Determines the base path for user settings files.
std::map< wxString, PROJECT * > m_projects
Loaded projects, mapped according to project full name.
COLOR_SETTINGS * registerColorSettings(const wxString &aFilename, bool aAbsolutePath=false)
bool m_headless
True if running outside a UI context.
SETTINGS_MANAGER(bool aHeadless=false)
static wxString GetColorSettingsPath()
Returns the path where color scheme files are stored; creating it if missing (normally .
COMMON_SETTINGS * GetCommonSettings() const
Retrieves the common settings shared by all applications.
bool SaveProject(const wxString &aFullPath=wxEmptyString, PROJECT *aProject=nullptr)
Saves a loaded project.
void SetMigrationSource(const wxString &aSource)
Helper for DIALOG_MIGRATE_SETTINGS to specify a source for migration.
wxString GetProjectBackupsPath() const
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Loads a project or sets up a new project with a specified path.
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
COLOR_SETTINGS * loadColorSettingsByName(const wxString &aName)
Attempts to load a color theme by name (the color theme directory and .json ext are assumed)
void SetKiway(KIWAY *aKiway)
Associate this setting manager with the given Kiway.
std::vector< COLOR_SETTINGS * > GetColorSettingsList()
bool IsProjectOpen() const
Helper for checking if we have a project open TODO: This should be deprecated along with Prj() once w...
bool GetPreviousVersionPaths(std::vector< wxString > *aName=nullptr)
Retrieves the name of the most recent previous KiCad version that can be found in the user settings d...
static bool IsSettingsPathValid(const wxString &aPath)
Checks if a given path is probably a valid KiCad configuration directory.
std::vector< std::unique_ptr< PROJECT > > m_projects_list
Loaded projects (ownership here)
PROJECT * GetProject(const wxString &aFullPath) const
Retrieves a loaded project by name.
bool UnloadProject(PROJECT *aProject, bool aSave=true)
Saves, unloads and unregisters the given PROJECT.
std::vector< wxString > GetOpenProjects() const
std::vector< std::unique_ptr< JSON_SETTINGS > > m_settings
bool TriggerBackupIfNeeded(REPORTER &aReporter) const
Calls BackupProject if a new backup is needed according to the current backup policy.
bool m_migrateLibraryTables
If true, the symbol and footprint library tables will be migrated from the previous version.
bool unloadProjectFile(PROJECT *aProject, bool aSave)
Optionally saves, and then unloads and unregisters the given PROJECT_FILE.
COLOR_SETTINGS * GetMigratedColorSettings()
Returns a color theme for storing colors migrated from legacy (5.x and earlier) settings,...
static bool extractVersion(const std::string &aVersionString, int *aMajor, int *aMinor)
Extracts the numeric version from a given settings string.
std::unordered_map< size_t, JSON_SETTINGS * > m_app_settings_cache
Cache for app settings.
COLOR_SETTINGS * AddNewColorSettings(const wxString &aFilename)
Registers a new color settings object with the given filename.
bool m_ok
True if settings loaded successfully at construction.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
bool loadProjectFile(PROJECT &aProject)
Registers a PROJECT_FILE and attempts to load it from disk.
static wxString backupDateTimeFormat
void ReloadColorSettings()
Re-scans the color themes directory, reloading any changes it finds.
COMMON_SETTINGS * m_common_settings
KIWAY * m_kiway
The kiway this settings manager interacts with.
void FlushAndRelease(JSON_SETTINGS *aSettings, bool aSave=true)
If the given settings object is registered, save it to disk and unregister it.