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 PROJECT;
30 class PROJECT_FILE;
31 class REPORTER;
32 
33 
35 {
36 public:
37  SETTINGS_MANAGER( bool aHeadless = false );
38 
40 
44  bool IsOK() { return m_ok; }
45 
51  JSON_SETTINGS* RegisterSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true );
52 
53  void Load();
54 
55  void Load( JSON_SETTINGS* aSettings );
56 
57  void Save();
58 
59  void Save( JSON_SETTINGS* aSettings );
60 
65  void FlushAndRelease( JSON_SETTINGS* aSettings, bool aSave = true );
66 
77  template<typename AppSettings>
78  AppSettings* GetAppSettings( bool aLoadNow = true )
79  {
80  AppSettings* ret = nullptr;
81 
82  auto it = std::find_if( m_settings.begin(), m_settings.end(),
83  []( const std::unique_ptr<JSON_SETTINGS>& aSettings ) {
84  return dynamic_cast<AppSettings*>( aSettings.get() );
85  } );
86 
87  if( it != m_settings.end() )
88  ret = dynamic_cast<AppSettings*>( it->get() );
89  else
90  ret = static_cast<AppSettings*>( RegisterSettings( new AppSettings, aLoadNow ) );
91 
92  return ret;
93  }
94 
102  COLOR_SETTINGS* GetColorSettings( const wxString& aName = "user" );
103 
104  std::vector<COLOR_SETTINGS*> GetColorSettingsList()
105  {
106  std::vector<COLOR_SETTINGS*> ret;
107 
108  for( const auto& el : m_color_settings )
109  ret.push_back( el.second );
110 
111  std::sort( ret.begin(), ret.end(), []( COLOR_SETTINGS* a, COLOR_SETTINGS* b ) {
112  return a->GetName() < b->GetName();
113  } );
114 
115  return ret;
116  }
117 
127  void SaveColorSettings( COLOR_SETTINGS* aSettings, const std::string& aNamespace = "" );
128 
134  COLOR_SETTINGS* AddNewColorSettings( const wxString& aFilename );
135 
141 
147  std::string GetPathForSettingsFile( JSON_SETTINGS* aSettings );
148 
162  bool MigrateIfNeeded();
163 
168  void SetMigrationSource( const wxString& aSource ) { m_migration_source = aSource; }
169 
178  bool GetPreviousVersionPaths( std::vector<wxString>* aName = nullptr );
179 
183  void ReloadColorSettings();
184 
191  bool LoadProject( const wxString& aFullPath, bool aSetActive = true );
192 
199  bool UnloadProject( PROJECT* aProject, bool aSave = true );
200 
205  PROJECT& Prj() const;
206 
212  PROJECT* GetProject( const wxString& aFullPath ) const;
213 
219  bool SaveProject( const wxString& aFullPath = wxEmptyString );
220 
224  wxString GetProjectBackupsPath() const;
225 
231  bool BackupProject( REPORTER& aReporter ) const;
232 
238  bool TriggerBackupIfNeeded( REPORTER& aReporter ) const;
239 
248  static bool IsSettingsPathValid( const wxString& aPath );
249 
254  static std::string GetColorSettingsPath();
255 
265  static std::string GetUserSettingsPath();
266 
273  static std::string GetSettingsVersion();
274 
275 private:
276 
291  static std::string calculateUserSettingsPath( bool aIncludeVer = true, bool aUseEnv = true );
292 
297  static int compareVersions( const std::string& aFirst, const std::string& aSecond );
298 
306  static bool extractVersion( const std::string& aVersionString, int* aMajor, int* aMinor );
307 
313  COLOR_SETTINGS* loadColorSettingsByName( const wxString& aName );
314 
315  void registerColorSettings( const wxString& aFilename );
316 
317  void loadAllColorSettings();
318 
324  bool loadProjectFile( PROJECT& aProject );
325 
332  bool unloadProjectFile( PROJECT* aProject, bool aSave );
333 
334 private:
335 
338 
339  std::vector<std::unique_ptr<JSON_SETTINGS>> m_settings;
340 
341  std::unordered_map<wxString, COLOR_SETTINGS*> m_color_settings;
342 
343  // Convenience shortcut
345 
347 
349  bool m_ok;
350 
352  std::map<wxString, std::unique_ptr<PROJECT>> m_projects;
353 
355  std::map<wxString, PROJECT_FILE*> m_project_files;
356 
357  static wxString backupDateTimeFormat;
358 };
359 
360 #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:61
void SetMigrationSource(const wxString &aSource)
Helper for DIALOG_MIGRATE_SETTINGS to specify a source for migration.
static bool IsSettingsPathValid(const wxString &aPath)
Checks if a given path is probably a valid KiCad configuration directory.
std::vector< COLOR_SETTINGS * > GetColorSettingsList()
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.
std::string GetPathForSettingsFile(JSON_SETTINGS *aSettings)
Returns the path a given settings file should be loaded from / stored to.
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
static std::string calculateUserSettingsPath(bool aIncludeVer=true, bool aUseEnv=true)
Determines the base path for user settings files.
static std::string GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
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.
std::map< wxString, std::unique_ptr< PROJECT > > m_projects
Loaded projects, mapped according to project full name.
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.
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,...
static std::string GetColorSettingsPath()
Returns the path where color scheme files are stored (normally .
SETTINGS_MANAGER(bool aHeadless=false)
wxString GetProjectBackupsPath() const
bool loadProjectFile(PROJECT &aProject)
Registers a PROJECT_FILE and attempts to load it from disk.
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
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...
COMMON_SETTINGS * m_common_settings
void FlushAndRelease(JSON_SETTINGS *aSettings, bool aSave=true)
If the given settings object is registered, save it to disk and unregister it.
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.