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 LOCKFILE;
38
39
41#define PROJECT_BACKUPS_DIR_SUFFIX wxT( "-backups" )
42
43
45{
46public:
47 SETTINGS_MANAGER( bool aHeadless = false );
48
50
54 bool IsOK() { return m_ok; }
55
61 void SetKiway( KIWAY* aKiway ) { m_kiway = aKiway; }
62
68 template<typename T>
69 T* RegisterSettings( T* aSettings, bool aLoadNow = true )
70 {
71 return static_cast<T*>( registerSettings( aSettings, aLoadNow ) );
72 }
73
74 void Load();
75
76 void Load( JSON_SETTINGS* aSettings );
77
78 void Save();
79
80 void Save( JSON_SETTINGS* aSettings );
81
86 void FlushAndRelease( JSON_SETTINGS* aSettings, bool aSave = true );
87
98 template<typename T>
100 {
101 T* ret = nullptr;
102 size_t typeHash = typeid( T ).hash_code();
103
104 if( m_app_settings_cache.count( typeHash ) )
105 ret = dynamic_cast<T*>( m_app_settings_cache.at( typeHash ) );
106
107 if( ret )
108 return ret;
109
110 auto it = std::find_if( m_settings.begin(), m_settings.end(),
111 []( const std::unique_ptr<JSON_SETTINGS>& aSettings )
112 {
113 return dynamic_cast<T*>( aSettings.get() );
114 } );
115
116 if( it != m_settings.end() )
117 {
118 ret = dynamic_cast<T*>( it->get() );
119 }
120 else
121 {
122 throw std::runtime_error( "Tried to GetAppSettings before registering" );
123 }
124
125 m_app_settings_cache[typeHash] = ret;
126
127 return ret;
128 }
129
137 COLOR_SETTINGS* GetColorSettings( const wxString& aName = "user" );
138
139 std::vector<COLOR_SETTINGS*> GetColorSettingsList()
140 {
141 std::vector<COLOR_SETTINGS*> ret;
142
143 for( const std::pair<const wxString, COLOR_SETTINGS*>& entry : m_color_settings )
144 ret.push_back( entry.second );
145
146 std::sort( ret.begin(), ret.end(), []( COLOR_SETTINGS* a, COLOR_SETTINGS* b )
147 {
148 return a->GetName() < b->GetName();
149 } );
150
151 return ret;
152 }
153
163 void SaveColorSettings( COLOR_SETTINGS* aSettings, const std::string& aNamespace = "" );
164
170 COLOR_SETTINGS* AddNewColorSettings( const wxString& aFilename );
171
178
184
190 wxString GetPathForSettingsFile( JSON_SETTINGS* aSettings );
191
205 bool MigrateIfNeeded();
206
211 void SetMigrationSource( const wxString& aSource ) { m_migration_source = aSource; }
212
213 void SetMigrateLibraryTables( bool aMigrate = true ) { m_migrateLibraryTables = aMigrate; }
214
223 bool GetPreviousVersionPaths( std::vector<wxString>* aName = nullptr );
224
228 void ReloadColorSettings();
229
236 bool LoadProject( const wxString& aFullPath, bool aSetActive = true );
237
244 bool UnloadProject( PROJECT* aProject, bool aSave = true );
245
251 bool IsProjectOpen() const;
252
257 PROJECT& Prj() const;
258
264 PROJECT* GetProject( const wxString& aFullPath ) const;
265
269 std::vector<wxString> GetOpenProjects() const;
270
277 bool SaveProject( const wxString& aFullPath = wxEmptyString, PROJECT* aProject = nullptr );
278
286 void SaveProjectAs( const wxString& aFullPath, PROJECT* aProject = nullptr );
287
294 void SaveProjectCopy( const wxString& aFullPath, PROJECT* aProject = nullptr );
295
299 wxString GetProjectBackupsPath() const;
300
306 bool BackupProject( REPORTER& aReporter ) const;
307
313 bool TriggerBackupIfNeeded( REPORTER& aReporter ) const;
314
323 static bool IsSettingsPathValid( const wxString& aPath );
324
329 static wxString GetColorSettingsPath();
330
337 static std::string GetSettingsVersion();
338
342 static wxString GetUserSettingsPath();
343private:
344 JSON_SETTINGS* registerSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true );
345
350 static int compareVersions( const std::string& aFirst, const std::string& aSecond );
351
359 static bool extractVersion( const std::string& aVersionString, int* aMajor = nullptr,
360 int* aMinor = nullptr );
361
367 COLOR_SETTINGS* loadColorSettingsByName( const wxString& aName );
368
369 COLOR_SETTINGS* registerColorSettings( const wxString& aFilename, bool aAbsolutePath = false );
370
372
378 bool loadProjectFile( PROJECT& aProject );
379
386 bool unloadProjectFile( PROJECT* aProject, bool aSave );
387
388 // Helper to create built-in colors and register them
390
391private:
392
395
398
399 std::vector<std::unique_ptr<JSON_SETTINGS>> m_settings;
400
401 std::unordered_map<wxString, COLOR_SETTINGS*> m_color_settings;
402
404 std::unordered_map<size_t, JSON_SETTINGS*> m_app_settings_cache;
405
406 // Convenience shortcut
408
410
413
415 bool m_ok;
416
418 std::vector<std::unique_ptr<PROJECT>> m_projects_list;
419
421 std::map<wxString, PROJECT*> m_projects;
422
424 std::map<wxString, PROJECT_FILE*> m_project_files;
425
427 std::unique_ptr<LOCKFILE> m_project_lock;
428
429 static wxString backupDateTimeFormat;
430};
431
432#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: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
static int compareVersions(const std::string &aFirst, const std::string &aSecond)
Compares two settings versions, like "5.99" and "6.0".
std::unique_ptr< LOCKFILE > m_project_lock
Lock for loaded project (expand to multiple once we support MDI)
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)
T * GetAppSettings()
Returns a handle to the a given settings by type If the settings have already been loaded,...
static wxString GetUserSettingsPath()
A proxy for PATHS::GetUserSettingsPath rather than fighting swig.
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.
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.
std::map< wxString, PROJECT * > m_projects
Loaded projects, mapped according to project full name.
static bool extractVersion(const std::string &aVersionString, int *aMajor=nullptr, int *aMinor=nullptr)
Extracts the numeric version from a given settings string.
COLOR_SETTINGS * registerColorSettings(const wxString &aFilename, bool aAbsolutePath=false)
bool m_headless
True if running outside a UI context.
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,...
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.
void registerBuiltinColorSettings()
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.
PROJECT & Prj()
Definition: kicad.cpp:591