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 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 <common.h> // for wxString hash
26 
27 class COLOR_SETTINGS;
28 class COMMON_SETTINGS;
29 class KIWAY;
30 class PROJECT;
31 class PROJECT_FILE;
32 class REPORTER;
33 
34 
36 {
37 public:
38  SETTINGS_MANAGER( bool aHeadless = false );
39 
41 
45  bool IsOK() { return m_ok; }
46 
52  void SetKiway( KIWAY* aKiway ) { m_kiway = aKiway; }
53 
59  JSON_SETTINGS* RegisterSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true );
60 
61  void Load();
62 
63  void Load( JSON_SETTINGS* aSettings );
64 
65  void Save();
66 
67  void Save( JSON_SETTINGS* aSettings );
68 
73  void FlushAndRelease( JSON_SETTINGS* aSettings, bool aSave = true );
74 
85  template<typename AppSettings>
86  AppSettings* GetAppSettings( bool aLoadNow = true )
87  {
88  AppSettings* ret = nullptr;
89 
90  auto it = std::find_if( m_settings.begin(), m_settings.end(),
91  []( const std::unique_ptr<JSON_SETTINGS>& aSettings )
92  {
93  return dynamic_cast<AppSettings*>( aSettings.get() );
94  } );
95 
96  if( it != m_settings.end() )
97  {
98  ret = dynamic_cast<AppSettings*>( it->get() );
99  }
100  else
101  {
102  try
103  {
104  ret = static_cast<AppSettings*>( RegisterSettings( new AppSettings, aLoadNow ) );
105  }
106  catch( ... )
107  {
108  wxLogTrace( traceSettings, "Unable to create application settings object" );
109  }
110 
111  }
112 
113  return ret;
114  }
115 
123  COLOR_SETTINGS* GetColorSettings( const wxString& aName = "user" );
124 
125  std::vector<COLOR_SETTINGS*> GetColorSettingsList()
126  {
127  std::vector<COLOR_SETTINGS*> ret;
128 
129  for( const std::pair<const wxString, COLOR_SETTINGS*>& entry : m_color_settings )
130  ret.push_back( entry.second );
131 
132  std::sort( ret.begin(), ret.end(), []( COLOR_SETTINGS* a, COLOR_SETTINGS* b )
133  {
134  return a->GetName() < b->GetName();
135  } );
136 
137  return ret;
138  }
139 
149  void SaveColorSettings( COLOR_SETTINGS* aSettings, const std::string& aNamespace = "" );
150 
156  COLOR_SETTINGS* AddNewColorSettings( const wxString& aFilename );
157 
164 
170 
176  wxString GetPathForSettingsFile( JSON_SETTINGS* aSettings );
177 
191  bool MigrateIfNeeded();
192 
197  void SetMigrationSource( const wxString& aSource ) { m_migration_source = aSource; }
198 
199  void SetMigrateLibraryTables( bool aMigrate = true ) { m_migrateLibraryTables = aMigrate; }
200 
209  bool GetPreviousVersionPaths( std::vector<wxString>* aName = nullptr );
210 
214  void ReloadColorSettings();
215 
222  bool LoadProject( const wxString& aFullPath, bool aSetActive = true );
223 
230  bool UnloadProject( PROJECT* aProject, bool aSave = true );
231 
237  bool IsProjectOpen() const;
238 
243  PROJECT& Prj() const;
244 
250  PROJECT* GetProject( const wxString& aFullPath ) const;
251 
255  std::vector<wxString> GetOpenProjects() const;
256 
262  bool SaveProject( const wxString& aFullPath = wxEmptyString );
263 
268  void SaveProjectAs( const wxString& aFullPath );
269 
273  wxString GetProjectBackupsPath() const;
274 
280  bool BackupProject( REPORTER& aReporter ) const;
281 
287  bool TriggerBackupIfNeeded( REPORTER& aReporter ) const;
288 
297  static bool IsSettingsPathValid( const wxString& aPath );
298 
303  static wxString GetColorSettingsPath();
304 
314  static wxString GetUserSettingsPath();
315 
322  static std::string GetSettingsVersion();
323 
324 private:
325 
340  static wxString calculateUserSettingsPath( bool aIncludeVer = true, bool aUseEnv = true );
341 
346  static int compareVersions( const std::string& aFirst, const std::string& aSecond );
347 
355  static bool extractVersion( const std::string& aVersionString, int* aMajor, int* aMinor );
356 
362  COLOR_SETTINGS* loadColorSettingsByName( const wxString& aName );
363 
364  void registerColorSettings( const wxString& aFilename );
365 
366  void loadAllColorSettings();
367 
373  bool loadProjectFile( PROJECT& aProject );
374 
381  bool unloadProjectFile( PROJECT* aProject, bool aSave );
382 
383 private:
384 
387 
390 
391  std::vector<std::unique_ptr<JSON_SETTINGS>> m_settings;
392 
393  std::unordered_map<wxString, COLOR_SETTINGS*> m_color_settings;
394 
395  // Convenience shortcut
397 
399 
402 
404  bool m_ok;
405 
407  std::vector<std::unique_ptr<PROJECT>> m_projects_list;
408 
410  std::map<wxString, PROJECT*> m_projects;
411 
413  std::map<wxString, PROJECT_FILE*> m_project_files;
414 
415  static wxString backupDateTimeFormat;
416 };
417 
418 #endif
static wxString backupDateTimeFormat
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
PROJECT holds project specific data.
Definition: project.h:63
void SetMigrationSource(const wxString &aSource)
Helper for DIALOG_MIGRATE_SETTINGS to specify a source for migration.
void SaveProjectAs(const wxString &aFullPath)
Sets the currently loaded project path and saves it (pointers remain valid)
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::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 ...
REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:64
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.
PROJECT_FILE is the backing store for a PROJECT, in JSON format.
Definition: project_file.h:62
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.
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)
Retreives 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.
KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the s...
Definition: kiway.h:273
bool m_headless
True if running outside a UI context.
void registerColorSettings(const wxString &aFilename)
AppSettings * GetAppSettings(bool aLoadNow=true)
Returns a handle to the a given settings by type If the settings have already been loaded,...
void SetMigrateLibraryTables(bool aMigrate=true)
std::map< wxString, PROJECT * > m_projects
Loaded projects, mapped according to project full name.
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.
JSON_SETTINGS * RegisterSettings(JSON_SETTINGS *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
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.
The common library.
static std::string GetSettingsVersion()
Parses the current KiCad build version and extracts the major and minor revision to use as the name o...
Color settings are a bit different than most of the settings objects in that there can be more than o...
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.
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.