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 <jon@craftyjon.com>
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 
29 class COLOR_SETTINGS;
30 class COMMON_SETTINGS;
31 class KIWAY;
32 class PROJECT;
33 class PROJECT_FILE;
34 class REPORTER;
35 class wxSingleInstanceChecker;
36 
37 
39 #define PROJECT_BACKUPS_DIR_SUFFIX wxT( "-backups" )
40 
41 
43 {
44 public:
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 
281  bool SaveProject( const wxString& aFullPath = wxEmptyString );
282 
289  void SaveProjectAs( const wxString& aFullPath );
290 
295  void SaveProjectCopy( const wxString& aFullPath );
296 
300  wxString GetProjectBackupsPath() const;
301 
307  bool BackupProject( REPORTER& aReporter ) const;
308 
314  bool TriggerBackupIfNeeded( REPORTER& aReporter ) const;
315 
324  static bool IsSettingsPathValid( const wxString& aPath );
325 
330  static wxString GetColorSettingsPath();
331 
341  static wxString GetUserSettingsPath();
342 
349  static std::string GetSettingsVersion();
350 
351 private:
352 
353  JSON_SETTINGS* registerSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true );
354 
369  static wxString calculateUserSettingsPath( bool aIncludeVer = true, bool aUseEnv = true );
370 
375  static int compareVersions( const std::string& aFirst, const std::string& aSecond );
376 
384  static bool extractVersion( const std::string& aVersionString, int* aMajor, int* aMinor );
385 
391  COLOR_SETTINGS* loadColorSettingsByName( const wxString& aName );
392 
393  COLOR_SETTINGS* registerColorSettings( const wxString& aFilename );
394 
395  void loadAllColorSettings();
396 
402  bool loadProjectFile( PROJECT& aProject );
403 
410  bool unloadProjectFile( PROJECT* aProject, bool aSave );
411 
412 private:
413 
416 
419 
420  std::vector<std::unique_ptr<JSON_SETTINGS>> m_settings;
421 
422  std::unordered_map<wxString, COLOR_SETTINGS*> m_color_settings;
423 
425  std::unordered_map<size_t, JSON_SETTINGS*> m_app_settings_cache;
426 
427  // Convenience shortcut
429 
431 
434 
436  bool m_ok;
437 
439  std::vector<std::unique_ptr<PROJECT>> m_projects_list;
440 
442  std::map<wxString, PROJECT*> m_projects;
443 
445  std::map<wxString, PROJECT_FILE*> m_project_files;
446 
448  std::unique_ptr<wxSingleInstanceChecker> m_project_lock;
449 
450  static wxString backupDateTimeFormat;
451 };
452 
453 #endif
static wxString backupDateTimeFormat
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
Container for project specific data.
Definition: project.h:62
void SetMigrationSource(const wxString &aSource)
Helper for DIALOG_MIGRATE_SETTINGS to specify a source for migration.
std::unique_ptr< wxSingleInstanceChecker > m_project_lock
Lock for loaded project (expand to multiple once we support MDI)
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
void SaveProjectAs(const wxString &aFullPath)
Sets the currently loaded project path and saves it (pointers remain valid) Note that this will not m...
static bool IsSettingsPathValid(const wxString &aPath)
Checks if a given path is probably a valid KiCad configuration directory.
std::vector< COLOR_SETTINGS * > GetColorSettingsList()
KIWAY * m_kiway
The kiway this settings manager interacts with.
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.
std::map< wxString, PROJECT_FILE * > m_project_files
Loaded project files, mapped according to project full name.
bool MigrateIfNeeded()
Handles the initialization of the user settings directory and migration from previous KiCad versions ...
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:70
const wxString & GetName() const
COLOR_SETTINGS * AddNewColorSettings(const wxString &aFilename)
Registers a new color settings object with the given filename.
static int compareVersions(const std::string &aFirst, const std::string &aSecond)
Compares two settings versions, like "5.99" and "6.0".
bool BackupProject(REPORTER &aReporter) const
Creates a backup archive of the current project.
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:64
wxString m_migration_source
bool m_ok
True if settings loaded successfully at construction.
bool unloadProjectFile(PROJECT *aProject, bool aSave)
Optionally saves, and then unloads and unregisters the given PROJECT_FILE.
void SaveProjectCopy(const wxString &aFullPath)
Saves a copy of the current project under the given path.
bool IsProjectOpen() const
Helper for checking if we have a project open TODO: This should be deprecated along with Prj() once w...
void SetKiway(KIWAY *aKiway)
Associate this setting manager with the given Kiway.
bool TriggerBackupIfNeeded(REPORTER &aReporter) const
Calls BackupProject if a new backup is needed according to the current backup policy.
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...
COMMON_SETTINGS * GetCommonSettings() const
Retrieves the common settings shared by all applications.
T * GetAppSettings(bool aLoadNow=true)
Returns a handle to the a given settings by type If the settings have already been loaded,...
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:260
bool m_headless
True if running outside a UI context.
void SetMigrateLibraryTables(bool aMigrate=true)
std::map< wxString, PROJECT * > m_projects
Loaded projects, mapped according to project full name.
COLOR_SETTINGS * registerColorSettings(const wxString &aFilename)
SETTINGS_MANAGER(bool aHeadless=false)
static wxString GetColorSettingsPath()
Returns the path where color scheme files are stored; creating it if missing (normally .
wxString GetProjectBackupsPath() const
bool loadProjectFile(PROJECT &aProject)
Registers a PROJECT_FILE and attempts to load it from disk.
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
std::unordered_map< wxString, COLOR_SETTINGS * > m_color_settings
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Loads a project or sets up a new project with a specified path.
COLOR_SETTINGS * loadColorSettingsByName(const wxString &aName)
Attempts to load a color theme by name (the color theme directory and .json ext are assumed)
COLOR_SETTINGS * GetColorSettings(const wxString &aName="user")
Retrieves a color settings object that applications can read colors from.
void ReloadColorSettings()
Re-scans the color themes directory, reloading any changes it finds.
std::vector< std::unique_ptr< JSON_SETTINGS > > m_settings
std::vector< wxString > GetOpenProjects() const
static wxString calculateUserSettingsPath(bool aIncludeVer=true, bool aUseEnv=true)
Determines the base path for user settings files.
bool UnloadProject(PROJECT *aProject, bool aSave=true)
Saves, unloads and unregisters the given PROJECT.
bool SaveProject(const wxString &aFullPath=wxEmptyString)
Saves a loaded project.
void SaveColorSettings(COLOR_SETTINGS *aSettings, const std::string &aNamespace="")
Safely saves a COLOR_SETTINGS to disk, preserving any changes outside the given namespace.
static std::string GetSettingsVersion()
Parses the current KiCad build version and extracts the major and minor revision to use as the name o...
JSON_SETTINGS * registerSettings(JSON_SETTINGS *aSettings, bool aLoadNow=true)
Color settings are a bit different than most of the settings objects in that there can be more than o...
COMMON_SETTINGS * m_common_settings
COLOR_SETTINGS * GetMigratedColorSettings()
Returns a color theme for storing colors migrated from legacy (5.x and earlier) settings,...
void FlushAndRelease(JSON_SETTINGS *aSettings, bool aSave=true)
If the given settings object is registered, save it to disk and unregister it.
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.
static bool extractVersion(const std::string &aVersionString, int *aMajor, int *aMinor)
Extracts the numeric version from a given settings string.
wxString GetPathForSettingsFile(JSON_SETTINGS *aSettings)
Returns the path a given settings file should be loaded from / stored to.