KiCad PCB EDA Suite
SETTINGS_MANAGER Class Reference

#include <settings_manager.h>

Public Member Functions

 SETTINGS_MANAGER (bool aHeadless=false)
 
 ~SETTINGS_MANAGER ()
 
bool IsOK ()
 
void SetKiway (KIWAY *aKiway)
 Associate this setting manager with the given Kiway. More...
 
template<typename T >
T * RegisterSettings (T *aSettings, bool aLoadNow=true)
 Takes ownership of the pointer passed in. More...
 
void Load ()
 
void Load (JSON_SETTINGS *aSettings)
 
void Save ()
 
void Save (JSON_SETTINGS *aSettings)
 
void FlushAndRelease (JSON_SETTINGS *aSettings, bool aSave=true)
 If the given settings object is registered, save it to disk and unregister it. More...
 
template<typename T >
T * GetAppSettings (bool aLoadNow=true)
 Returns a handle to the a given settings by type If the settings have already been loaded, returns the existing pointer. More...
 
COLOR_SETTINGSGetColorSettings (const wxString &aName="user")
 Retrieves a color settings object that applications can read colors from. More...
 
std::vector< COLOR_SETTINGS * > GetColorSettingsList ()
 
void SaveColorSettings (COLOR_SETTINGS *aSettings, const std::string &aNamespace="")
 Safely saves a COLOR_SETTINGS to disk, preserving any changes outside the given namespace. More...
 
COLOR_SETTINGSAddNewColorSettings (const wxString &aFilename)
 Registers a new color settings object with the given filename. More...
 
COLOR_SETTINGSGetMigratedColorSettings ()
 Returns a color theme for storing colors migrated from legacy (5.x and earlier) settings, creating the theme if necessary. More...
 
COMMON_SETTINGSGetCommonSettings () const
 Retrieves the common settings shared by all applications. More...
 
wxString GetPathForSettingsFile (JSON_SETTINGS *aSettings)
 Returns the path a given settings file should be loaded from / stored to. More...
 
bool MigrateIfNeeded ()
 Handles the initialization of the user settings directory and migration from previous KiCad versions as needed. More...
 
void SetMigrationSource (const wxString &aSource)
 Helper for DIALOG_MIGRATE_SETTINGS to specify a source for migration. More...
 
void SetMigrateLibraryTables (bool aMigrate=true)
 
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 directory. More...
 
void ReloadColorSettings ()
 Re-scans the color themes directory, reloading any changes it finds. More...
 
bool LoadProject (const wxString &aFullPath, bool aSetActive=true)
 Loads a project or sets up a new project with a specified path. More...
 
bool UnloadProject (PROJECT *aProject, bool aSave=true)
 Saves, unloads and unregisters the given PROJECT. More...
 
bool IsProjectOpen () const
 Helper for checking if we have a project open TODO: This should be deprecated along with Prj() once we support multiple projects fully. More...
 
PROJECTPrj () const
 A helper while we are not MDI-capable – return the one and only project. More...
 
PROJECTGetProject (const wxString &aFullPath) const
 Retrieves a loaded project by name. More...
 
std::vector< wxString > GetOpenProjects () const
 
bool SaveProject (const wxString &aFullPath=wxEmptyString, PROJECT *aProject=nullptr)
 Saves a loaded project. More...
 
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 modify the read-only state of the project, so it will have no effect if the project is marked as read-only! More...
 
void SaveProjectCopy (const wxString &aFullPath, PROJECT *aProject=nullptr)
 Saves a copy of the current project under the given path. More...
 
wxString GetProjectBackupsPath () const
 
bool BackupProject (REPORTER &aReporter) const
 Creates a backup archive of the current project. More...
 
bool TriggerBackupIfNeeded (REPORTER &aReporter) const
 Calls BackupProject if a new backup is needed according to the current backup policy. More...
 

Static Public Member Functions

static bool IsSettingsPathValid (const wxString &aPath)
 Checks if a given path is probably a valid KiCad configuration directory. More...
 
static wxString GetColorSettingsPath ()
 Returns the path where color scheme files are stored; creating it if missing (normally . More...
 
static wxString GetUserSettingsPath ()
 Return the user configuration path used to store KiCad's configuration files. More...
 
static std::string GetSettingsVersion ()
 Parses the current KiCad build version and extracts the major and minor revision to use as the name of the settings directory for this KiCad version. More...
 

Private Member Functions

JSON_SETTINGSregisterSettings (JSON_SETTINGS *aSettings, bool aLoadNow=true)
 
COLOR_SETTINGSloadColorSettingsByName (const wxString &aName)
 Attempts to load a color theme by name (the color theme directory and .json ext are assumed) More...
 
COLOR_SETTINGSregisterColorSettings (const wxString &aFilename, bool aAbsolutePath=false)
 
void loadAllColorSettings ()
 
bool loadProjectFile (PROJECT &aProject)
 Registers a PROJECT_FILE and attempts to load it from disk. More...
 
bool unloadProjectFile (PROJECT *aProject, bool aSave)
 Optionally saves, and then unloads and unregisters the given PROJECT_FILE. More...
 

Static Private Member Functions

static wxString calculateUserSettingsPath (bool aIncludeVer=true, bool aUseEnv=true)
 Determines the base path for user settings files. More...
 
static int compareVersions (const std::string &aFirst, const std::string &aSecond)
 Compares two settings versions, like "5.99" and "6.0". More...
 
static bool extractVersion (const std::string &aVersionString, int *aMajor, int *aMinor)
 Extracts the numeric version from a given settings string. More...
 

Private Attributes

bool m_headless
 True if running outside a UI context. More...
 
KIWAYm_kiway
 The kiway this settings manager interacts with. More...
 
std::vector< std::unique_ptr< JSON_SETTINGS > > m_settings
 
std::unordered_map< wxString, COLOR_SETTINGS * > m_color_settings
 
std::unordered_map< size_t, JSON_SETTINGS * > m_app_settings_cache
 Cache for app settings. More...
 
COMMON_SETTINGSm_common_settings
 
wxString m_migration_source
 
bool m_migrateLibraryTables
 If true, the symbol and footprint library tables will be migrated from the previous version. More...
 
bool m_ok
 True if settings loaded successfully at construction. More...
 
std::vector< std::unique_ptr< PROJECT > > m_projects_list
 Loaded projects (ownership here) More...
 
std::map< wxString, PROJECT * > m_projects
 Loaded projects, mapped according to project full name. More...
 
std::map< wxString, PROJECT_FILE * > m_project_files
 Loaded project files, mapped according to project full name. More...
 
std::unique_ptr< wxSingleInstanceChecker > m_project_lock
 Lock for loaded project (expand to multiple once we support MDI) More...
 

Static Private Attributes

static wxString backupDateTimeFormat = wxT( "%Y-%m-%d_%H%M%S" )
 

Detailed Description

Definition at line 42 of file settings_manager.h.

Constructor & Destructor Documentation

◆ SETTINGS_MANAGER()

SETTINGS_MANAGER::SETTINGS_MANAGER ( bool  aHeadless = false)

Definition at line 51 of file settings_manager.cpp.

51  :
52  m_headless( aHeadless ),
53  m_kiway( nullptr ),
54  m_common_settings( nullptr ),
57 {
59 
60  // Check if the settings directory already exists, and if not, perform a migration if possible
61  if( !MigrateIfNeeded() )
62  {
63  m_ok = false;
64  return;
65  }
66 
67  m_ok = true;
68 
69  // create the common settings shared by all applications. Not loaded immediately
71 }
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
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.
bool MigrateIfNeeded()
Handles the initialization of the user settings directory and migration from previous KiCad versions ...
wxString m_migration_source
bool m_ok
True if settings loaded successfully at construction.
bool m_headless
True if running outside a UI context.
static void EnsureUserPathsExist()
Ensures/creates user default paths.
Definition: paths.cpp:361
COMMON_SETTINGS * m_common_settings

References PATHS::EnsureUserPathsExist(), m_common_settings, m_ok, MigrateIfNeeded(), and RegisterSettings().

◆ ~SETTINGS_MANAGER()

SETTINGS_MANAGER::~SETTINGS_MANAGER ( )

Definition at line 73 of file settings_manager.cpp.

74 {
75  m_settings.clear();
76  m_color_settings.clear();
77  m_projects.clear();
78 }
std::map< wxString, PROJECT * > m_projects
Loaded projects, mapped according to project full name.
std::unordered_map< wxString, COLOR_SETTINGS * > m_color_settings
std::vector< std::unique_ptr< JSON_SETTINGS > > m_settings

References m_color_settings, m_projects, and m_settings.

Member Function Documentation

◆ AddNewColorSettings()

COLOR_SETTINGS * SETTINGS_MANAGER::AddNewColorSettings ( const wxString &  aFilename)

Registers a new color settings object with the given filename.

Parameters
aFilenameis the location to store the new settings object
Returns
a pointer to the new object

Definition at line 266 of file settings_manager.cpp.

267 {
268  if( aName.EndsWith( wxT( ".json" ) ) )
269  return registerColorSettings( aName.BeforeLast( '.' ) );
270  else
271  return registerColorSettings( aName );
272 }
COLOR_SETTINGS * registerColorSettings(const wxString &aFilename, bool aAbsolutePath=false)

References registerColorSettings().

Referenced by FOOTPRINT_EDITOR_SETTINGS::MigrateFromLegacy(), COLOR_SETTINGS::migrateSchema0to1(), and PANEL_COLOR_SETTINGS::OnThemeChanged().

◆ BackupProject()

bool SETTINGS_MANAGER::BackupProject ( REPORTER aReporter) const

Creates a backup archive of the current project.

Parameters
aReporteris used for progress reporting
Returns
true if everything succeeded

Definition at line 1113 of file settings_manager.cpp.

1114 {
1115  wxDateTime timestamp = wxDateTime::Now();
1116 
1117  wxString fileName = wxString::Format( wxT( "%s-%s" ), Prj().GetProjectName(),
1118  timestamp.Format( backupDateTimeFormat ) );
1119 
1120  wxFileName target;
1121  target.SetPath( GetProjectBackupsPath() );
1122  target.SetName( fileName );
1123  target.SetExt( ArchiveFileExtension );
1124 
1125  wxDir dir( target.GetPath() );
1126 
1127  if( !target.DirExists() && !wxMkdir( target.GetPath() ) )
1128  {
1129  wxLogTrace( traceSettings, wxT( "Could not create project backup path %s" ), target.GetPath() );
1130  return false;
1131  }
1132 
1133  if( !target.IsDirWritable() )
1134  {
1135  wxLogTrace( traceSettings, wxT( "Backup directory %s is not writable" ), target.GetPath() );
1136  return false;
1137  }
1138 
1139  wxLogTrace( traceSettings, wxT( "Backing up project to %s" ), target.GetPath() );
1140 
1141  PROJECT_ARCHIVER archiver;
1142 
1143  return archiver.Archive( Prj().GetProjectPath(), target.GetFullPath(), aReporter );
1144 }
static wxString backupDateTimeFormat
bool Archive(const wxString &aSrcDir, const wxString &aDestFile, REPORTER &aReporter, bool aVerbose=true, bool aIncludeExtraFiles=false)
Creates an archive of the project.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
wxString GetProjectBackupsPath() const
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.
const std::string ArchiveFileExtension

References PROJECT_ARCHIVER::Archive(), ArchiveFileExtension, backupDateTimeFormat, Format(), GetProjectBackupsPath(), Prj(), and traceSettings.

Referenced by TriggerBackupIfNeeded().

◆ calculateUserSettingsPath()

wxString SETTINGS_MANAGER::calculateUserSettingsPath ( bool  aIncludeVer = true,
bool  aUseEnv = true 
)
staticprivate

Determines the base path for user settings files.

The configuration path order of precedence is determined by the following criteria:

  • The value of the KICAD_CONFIG_HOME environment variable
  • The value of the XDG_CONFIG_HOME environment variable.
  • The result of the call to wxStandardPaths::GetUserConfigDir() with ".config" appended as required on Linux builds.
Parameters
aIncludeVerwill append the current KiCad version if true (default)
aUseEnvwill prefer the base path found in the KICAD_CONFIG_DIR if found (default)
Returns
A string containing the config path for Kicad

Definition at line 709 of file settings_manager.cpp.

710 {
711  wxFileName cfgpath;
712 
713  // http://docs.wxwidgets.org/3.0/classwx_standard_paths.html#a7c7cf595d94d29147360d031647476b0
714 
715  wxString envstr;
716  if( aUseEnv && wxGetEnv( wxT( "KICAD_CONFIG_HOME" ), &envstr ) && !envstr.IsEmpty() )
717  {
718  // Override the assignment above with KICAD_CONFIG_HOME
719  cfgpath.AssignDir( envstr );
720  }
721  else
722  {
723  cfgpath.AssignDir( KIPLATFORM::ENV::GetUserConfigPath() );
724 
725  cfgpath.AppendDir( TO_STR( KICAD_CONFIG_DIR ) );
726  }
727 
728  if( aIncludeVer )
729  cfgpath.AppendDir( GetSettingsVersion() );
730 
731  return cfgpath.GetPath();
732 }
#define TO_STR(x)
Definition: macros.h:105
wxString GetUserConfigPath()
Retrieves the operating system specific path for a user's configuration store.
static std::string GetSettingsVersion()
Parses the current KiCad build version and extracts the major and minor revision to use as the name o...

References GetSettingsVersion(), KIPLATFORM::ENV::GetUserConfigPath(), and TO_STR.

Referenced by GetPreviousVersionPaths(), and GetUserSettingsPath().

◆ compareVersions()

int SETTINGS_MANAGER::compareVersions ( const std::string &  aFirst,
const std::string &  aSecond 
)
staticprivate

Compares two settings versions, like "5.99" and "6.0".

Returns
-1 if aFirst is older than aSecond, 1 if aFirst is newer than aSecond, 0 otherwise

Definition at line 742 of file settings_manager.cpp.

743 {
744  int a_maj = 0;
745  int a_min = 0;
746  int b_maj = 0;
747  int b_min = 0;
748 
749  if( !extractVersion( aFirst, &a_maj, &a_min ) || !extractVersion( aSecond, &b_maj, &b_min ) )
750  {
751  wxLogTrace( traceSettings, wxT( "compareSettingsVersions: bad input (%s, %s)" ), aFirst, aSecond );
752  return -1;
753  }
754 
755  if( a_maj < b_maj )
756  {
757  return -1;
758  }
759  else if( a_maj > b_maj )
760  {
761  return 1;
762  }
763  else
764  {
765  if( a_min < b_min )
766  {
767  return -1;
768  }
769  else if( a_min > b_min )
770  {
771  return 1;
772  }
773  else
774  {
775  return 0;
776  }
777  }
778 }
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.
static bool extractVersion(const std::string &aVersionString, int *aMajor, int *aMinor)
Extracts the numeric version from a given settings string.

References extractVersion(), and traceSettings.

Referenced by GetPreviousVersionPaths().

◆ extractVersion()

bool SETTINGS_MANAGER::extractVersion ( const std::string &  aVersionString,
int *  aMajor,
int *  aMinor 
)
staticprivate

Extracts the numeric version from a given settings string.

Parameters
aVersionStringis the string to split at the "."
aMajorwill store the first part
aMinorwill store the second part
Returns
true if extraction succeeded

Definition at line 781 of file settings_manager.cpp.

782 {
783  std::regex re_version( "(\\d+)\\.(\\d+)" );
784  std::smatch match;
785 
786  if( std::regex_match( aVersionString, match, re_version ) )
787  {
788  try
789  {
790  *aMajor = std::stoi( match[1].str() );
791  *aMinor = std::stoi( match[2].str() );
792  }
793  catch( ... )
794  {
795  return false;
796  }
797 
798  return true;
799  }
800 
801  return false;
802 }

Referenced by compareVersions().

◆ FlushAndRelease()

void SETTINGS_MANAGER::FlushAndRelease ( JSON_SETTINGS aSettings,
bool  aSave = true 
)

If the given settings object is registered, save it to disk and unregister it.

Parameters
aSettingsis the object to release

Definition at line 149 of file settings_manager.cpp.

150 {
151  auto it = std::find_if( m_settings.begin(), m_settings.end(),
152  [&aSettings]( const std::unique_ptr<JSON_SETTINGS>& aPtr )
153  {
154  return aPtr.get() == aSettings;
155  } );
156 
157  if( it != m_settings.end() )
158  {
159  wxLogTrace( traceSettings, wxT( "Flush and release %s" ), ( *it )->GetFullFilename() );
160 
161  if( aSave )
162  ( *it )->SaveToFile( GetPathForSettingsFile( it->get() ) );
163 
164  size_t typeHash = typeid( *it->get() ).hash_code();
165 
166  if( m_app_settings_cache.count( typeHash ) )
167  m_app_settings_cache.erase( typeHash );
168 
169  m_settings.erase( it );
170  }
171 }
std::unordered_map< size_t, JSON_SETTINGS * > m_app_settings_cache
Cache for app settings.
std::vector< std::unique_ptr< JSON_SETTINGS > > m_settings
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.
wxString GetPathForSettingsFile(JSON_SETTINGS *aSettings)
Returns the path a given settings file should be loaded from / stored to.

References GetPathForSettingsFile(), m_app_settings_cache, m_settings, and traceSettings.

Referenced by FOOTPRINT_EDIT_FRAME::doCloseWindow(), and unloadProjectFile().

◆ GetAppSettings()

template<typename T >
T* SETTINGS_MANAGER::GetAppSettings ( bool  aLoadNow = true)
inline

Returns a handle to the a given settings by type If the settings have already been loaded, returns the existing pointer.

If the settings have not been loaded, creates a new object owned by the settings manager and returns a pointer to it.

Template Parameters
Tis a type derived from APP_SETTINGS_BASE
Parameters
aLoadNowis true to load the registered file from disk immediately
Returns
a pointer to a loaded settings object

Definition at line 97 of file settings_manager.h.

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  }
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
std::unordered_map< size_t, JSON_SETTINGS * > m_app_settings_cache
Cache for app settings.
std::vector< std::unique_ptr< JSON_SETTINGS > > m_settings

References m_app_settings_cache, m_settings, and RegisterSettings().

Referenced by PL_EDITOR_FRAME::CommonSettingsChanged(), DIALOG_EXPORT_SVG::CreateSVGFile(), DIALOG_PCM::DIALOG_PCM(), SYMBOL_EDITOR_DRAWING_TOOLS::DrawShape(), SYMBOL_EDIT_FRAME::GetColorSettings(), SCH_BASE_FRAME::GetColorSettings(), LIB_PIN::LIB_PIN(), PANEL_PACKAGES_VIEW::OnDownloadVersionClicked(), DIALOG_PCM::OnManageRepositoriesClicked(), PANEL_EESCHEMA_COLOR_SETTINGS::PANEL_EESCHEMA_COLOR_SETTINGS(), PANEL_FP_EDITOR_COLOR_SETTINGS::PANEL_FP_EDITOR_COLOR_SETTINGS(), PANEL_PCBNEW_COLOR_SETTINGS::PANEL_PCBNEW_COLOR_SETTINGS(), PL_DRAW_PANEL_GAL::PL_DRAW_PANEL_GAL(), DIALOG_PLOT::Plot(), PANEL_PL_EDITOR_COLOR_SETTINGS::TransferDataFromWindow(), PANEL_SYM_COLOR_SETTINGS::TransferDataFromWindow(), PANEL_FP_EDITOR_COLOR_SETTINGS::TransferDataFromWindow(), PANEL_PCBNEW_COLOR_SETTINGS::TransferDataFromWindow(), and PANEL_EESCHEMA_COLOR_SETTINGS::TransferDataFromWindow().

◆ GetColorSettings()

COLOR_SETTINGS * SETTINGS_MANAGER::GetColorSettings ( const wxString &  aName = "user")

Retrieves a color settings object that applications can read colors from.

If the given settings file cannot be found, returns the default settings.

Parameters
aNameis the name of the color scheme to load
Returns
a loaded COLOR_SETTINGS object

Definition at line 174 of file settings_manager.cpp.

175 {
176  if( m_color_settings.count( aName ) )
177  return m_color_settings.at( aName );
178 
179  if( !aName.empty() )
180  {
181  COLOR_SETTINGS* ret = loadColorSettingsByName( aName );
182 
183  if( !ret )
184  {
185  ret = registerColorSettings( aName );
186  *ret = *m_color_settings.at( "_builtin_default" );
187  ret->SetFilename( wxT( "user" ) );
188  ret->SetReadOnly( false );
189  }
190 
191  return ret;
192  }
193 
194  // This had better work
195  return m_color_settings.at( "_builtin_default" );
196 }
void SetReadOnly(bool aReadOnly)
Definition: json_settings.h:84
COLOR_SETTINGS * registerColorSettings(const wxString &aFilename, bool aAbsolutePath=false)
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)
Color settings are a bit different than most of the settings objects in that there can be more than o...
void SetFilename(const wxString &aFilename)
Definition: json_settings.h:76

References loadColorSettingsByName(), m_color_settings, registerColorSettings(), JSON_SETTINGS::SetFilename(), and JSON_SETTINGS::SetReadOnly().

Referenced by PL_EDITOR_FRAME::CommonSettingsChanged(), DIALOG_EXPORT_SVG::CreateSVGFile(), DIALOG_PLOT_SCHEMATIC::getColorSettings(), SYMBOL_EDIT_FRAME::GetColorSettings(), SCH_BASE_FRAME::GetColorSettings(), GERBER_LAYER_WIDGET::OnLayerColorChange(), GERBER_LAYER_WIDGET::OnRenderColorChange(), PANEL_EESCHEMA_COLOR_SETTINGS::PANEL_EESCHEMA_COLOR_SETTINGS(), PANEL_FP_EDITOR_COLOR_SETTINGS::PANEL_FP_EDITOR_COLOR_SETTINGS(), PANEL_PCBNEW_COLOR_SETTINGS::PANEL_PCBNEW_COLOR_SETTINGS(), PL_DRAW_PANEL_GAL::PL_DRAW_PANEL_GAL(), DIALOG_PLOT::Plot(), SCH_PRINTOUT::PrintPage(), PANEL_COLOR_SETTINGS::saveCurrentTheme(), PANEL_SYM_COLOR_SETTINGS::TransferDataFromWindow(), and PANEL_SYM_COLOR_SETTINGS::TransferDataToWindow().

◆ GetColorSettingsList()

std::vector<COLOR_SETTINGS*> SETTINGS_MANAGER::GetColorSettingsList ( )
inline

Definition at line 144 of file settings_manager.h.

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  }
const wxString & GetName() const
std::unordered_map< wxString, COLOR_SETTINGS * > m_color_settings
Color settings are a bit different than most of the settings objects in that there can be more than o...

References COLOR_SETTINGS::GetName(), and m_color_settings.

Referenced by DIALOG_PLOT_SCHEMATIC::initDlg(), PANEL_SYM_COLOR_SETTINGS::TransferDataToWindow(), and DIALOG_PRINT_PCBNEW::TransferDataToWindow().

◆ GetColorSettingsPath()

wxString SETTINGS_MANAGER::GetColorSettingsPath ( )
static

Returns the path where color scheme files are stored; creating it if missing (normally .

/colors/ under the user settings path)

Definition at line 677 of file settings_manager.cpp.

678 {
679  wxFileName path;
680 
681  path.AssignDir( GetUserSettingsPath() );
682  path.AppendDir( "colors" );
683 
684  if( !path.DirExists() )
685  {
686  if( !wxMkdir( path.GetPath() ) )
687  {
688  wxLogTrace( traceSettings,
689  wxT( "GetColorSettingsPath(): Path %s missing and could not be created!" ),
690  path.GetPath() );
691  }
692  }
693 
694  return path.GetPath();
695 }
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.

References GetUserSettingsPath(), path, and traceSettings.

Referenced by GetPathForSettingsFile(), loadAllColorSettings(), loadColorSettingsByName(), PANEL_COLOR_SETTINGS::OnBtnOpenThemeFolderClicked(), PANEL_COLOR_SETTINGS::OnThemeChanged(), and SaveColorSettings().

◆ GetCommonSettings()

COMMON_SETTINGS* SETTINGS_MANAGER::GetCommonSettings ( ) const
inline

Retrieves the common settings shared by all applications.

Returns
a pointer to a loaded COMMON_SETTINGS

Definition at line 188 of file settings_manager.h.

188 { return m_common_settings; }
COMMON_SETTINGS * m_common_settings

References m_common_settings.

Referenced by TriggerBackupIfNeeded().

◆ GetMigratedColorSettings()

COLOR_SETTINGS * SETTINGS_MANAGER::GetMigratedColorSettings ( )

Returns a color theme for storing colors migrated from legacy (5.x and earlier) settings, creating the theme if necessary.

This theme will be called "user.json" / "User".

Returns
the color settings to be used for migrating legacy settings

Definition at line 275 of file settings_manager.cpp.

276 {
277  if( !m_color_settings.count( "user" ) )
278  {
279  COLOR_SETTINGS* settings = registerColorSettings( wxT( "user" ) );
280  settings->SetName( wxT( "User" ) );
281  Save( settings );
282  }
283 
284  return m_color_settings.at( "user" );
285 }
COLOR_SETTINGS * registerColorSettings(const wxString &aFilename, bool aAbsolutePath=false)
void SetName(const wxString &aName)
std::unordered_map< wxString, COLOR_SETTINGS * > m_color_settings
Color settings are a bit different than most of the settings objects in that there can be more than o...

References m_color_settings, registerColorSettings(), Save(), and COLOR_SETTINGS::SetName().

◆ GetOpenProjects()

std::vector< wxString > SETTINGS_MANAGER::GetOpenProjects ( ) const
Returns
a list of open projects

Definition at line 947 of file settings_manager.cpp.

948 {
949  std::vector<wxString> ret;
950 
951  for( const std::pair<const wxString, PROJECT*>& pair : m_projects )
952  ret.emplace_back( pair.first );
953 
954  return ret;
955 }
std::map< wxString, PROJECT * > m_projects
Loaded projects, mapped according to project full name.

References m_projects.

Referenced by KICAD_MANAGER_FRAME::canCloseWindow().

◆ GetPathForSettingsFile()

wxString SETTINGS_MANAGER::GetPathForSettingsFile ( JSON_SETTINGS aSettings)

Returns the path a given settings file should be loaded from / stored to.

Parameters
aSettingsis the settings object
Returns
a path based on aSettings->m_location

Definition at line 380 of file settings_manager.cpp.

381 {
382  wxASSERT( aSettings );
383 
384  switch( aSettings->GetLocation() )
385  {
386  case SETTINGS_LOC::USER:
387  return GetUserSettingsPath();
388 
390  // TODO: MDI support
391  return Prj().GetProjectPath();
392 
394  return GetColorSettingsPath();
395 
396  case SETTINGS_LOC::NONE:
397  return "";
398 
399  default:
400  wxASSERT_MSG( false, wxT( "Unknown settings location!" ) );
401  }
402 
403  return "";
404 }
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:122
The color scheme directory (e.g. ~/.config/kicad/colors/)
The settings directory inside a project folder.
SETTINGS_LOC GetLocation() const
Definition: json_settings.h:79
No directory prepended, full path in filename (used for PROJECT_FILE)
static wxString GetColorSettingsPath()
Returns the path where color scheme files are stored; creating it if missing (normally .
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
The main config directory (e.g. ~/.config/kicad/)

References COLORS, GetColorSettingsPath(), JSON_SETTINGS::GetLocation(), PROJECT::GetProjectPath(), GetUserSettingsPath(), NONE, Prj(), PROJECT, and USER.

Referenced by FlushAndRelease(), Load(), MigrateIfNeeded(), PANEL_COLOR_SETTINGS::OnThemeChanged(), registerSettings(), Save(), and unloadProjectFile().

◆ GetPreviousVersionPaths()

bool SETTINGS_MANAGER::GetPreviousVersionPaths ( std::vector< wxString > *  aName = nullptr)

Retrieves the name of the most recent previous KiCad version that can be found in the user settings directory.

For legacy versions (5.x, and 5.99 builds before this code was written), this will return "5.x"

Parameters
aNameis filled with the name of the previous version, if one exists
Returns
true if a previous version to migrate from exists

Definition at line 568 of file settings_manager.cpp.

569 {
570  wxASSERT( aPaths );
571 
572  aPaths->clear();
573 
574  wxDir dir;
575  std::vector<wxFileName> base_paths;
576 
577  base_paths.emplace_back( wxFileName( calculateUserSettingsPath( false ), "" ) );
578 
579  // If the env override is set, also check the default paths
580  if( wxGetEnv( wxT( "KICAD_CONFIG_HOME" ), nullptr ) )
581  base_paths.emplace_back( wxFileName( calculateUserSettingsPath( false, false ), "" ) );
582 
583 #ifdef __WXGTK__
584  // When running inside FlatPak, KIPLATFORM::ENV::GetUserConfigPath() will return a sandboxed
585  // path. In case the user wants to move from non-FlatPak KiCad to FlatPak KiCad, let's add our
586  // best guess as to the non-FlatPak config path. Unfortunately FlatPak also hides the host
587  // XDG_CONFIG_HOME, so if the user customizes their config path, they will have to browse
588  // for it.
589  {
590  wxFileName wxGtkPath;
591  wxGtkPath.AssignDir( "~/.config/kicad" );
592  wxGtkPath.MakeAbsolute();
593  base_paths.emplace_back( wxGtkPath.GetPath() );
594 
595  // We also want to pick up regular flatpak if we are nightly
596  wxGtkPath.AssignDir( "~/.var/app/org.kicad.KiCad/config/kicad" );
597  wxGtkPath.MakeAbsolute();
598  base_paths.emplace_back( wxGtkPath.GetPath() );
599  }
600 #endif
601 
602  wxString subdir;
603  std::string mine = GetSettingsVersion();
604 
605  auto check_dir = [&] ( const wxString& aSubDir )
606  {
607  // Only older versions are valid for migration
608  if( compareVersions( aSubDir.ToStdString(), mine ) <= 0 )
609  {
610  wxString sub_path = dir.GetNameWithSep() + aSubDir;
611 
612  if( IsSettingsPathValid( sub_path ) )
613  {
614  aPaths->push_back( sub_path );
615  wxLogTrace( traceSettings, wxT( "GetPreviousVersionName: %s is valid" ), sub_path );
616  }
617  }
618  };
619 
620  std::set<wxString> checkedPaths;
621 
622  for( auto base_path : base_paths )
623  {
624  if( checkedPaths.count( base_path.GetFullPath() ) )
625  continue;
626 
627  checkedPaths.insert( base_path.GetFullPath() );
628 
629  if( !dir.Open( base_path.GetFullPath() ) )
630  {
631  wxLogTrace( traceSettings, wxT( "GetPreviousVersionName: could not open base path %s" ),
632  base_path.GetFullPath() );
633  continue;
634  }
635 
636  wxLogTrace( traceSettings, wxT( "GetPreviousVersionName: checking base path %s" ),
637  base_path.GetFullPath() );
638 
639  if( dir.GetFirst( &subdir, wxEmptyString, wxDIR_DIRS ) )
640  {
641  if( subdir != mine )
642  check_dir( subdir );
643 
644  while( dir.GetNext( &subdir ) )
645  {
646  if( subdir != mine )
647  check_dir( subdir );
648  }
649  }
650 
651  // If we didn't find one yet, check for legacy settings without a version directory
652  if( IsSettingsPathValid( dir.GetNameWithSep() ) )
653  {
654  wxLogTrace( traceSettings,
655  wxT( "GetPreviousVersionName: root path %s is valid" ), dir.GetName() );
656  aPaths->push_back( dir.GetName() );
657  }
658  }
659 
660  return aPaths->size() > 0;
661 }
static bool IsSettingsPathValid(const wxString &aPath)
Checks if a given path is probably a valid KiCad configuration directory.
static int compareVersions(const std::string &aFirst, const std::string &aSecond)
Compares two settings versions, like "5.99" and "6.0".
static wxString calculateUserSettingsPath(bool aIncludeVer=true, bool aUseEnv=true)
Determines the base path for user settings files.
static std::string GetSettingsVersion()
Parses the current KiCad build version and extracts the major and minor revision to use as the name o...
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.

References calculateUserSettingsPath(), compareVersions(), GetSettingsVersion(), IsSettingsPathValid(), and traceSettings.

Referenced by DIALOG_MIGRATE_SETTINGS::TransferDataToWindow().

◆ GetProject()

PROJECT * SETTINGS_MANAGER::GetProject ( const wxString &  aFullPath) const

Retrieves a loaded project by name.

Parameters
aFullPathis the full path including name and extension to the project file
Returns
a pointer to the project if loaded, or nullptr

Definition at line 938 of file settings_manager.cpp.

939 {
940  if( m_projects.count( aFullPath ) )
941  return m_projects.at( aFullPath );
942 
943  return nullptr;
944 }
std::map< wxString, PROJECT * > m_projects
Loaded projects, mapped according to project full name.

References m_projects.

Referenced by GetDefaultProject(), LoadBoard(), NewBoard(), DIALOG_SCHEMATIC_SETUP::OnAuxiliaryAction(), and DIALOG_BOARD_SETUP::OnAuxiliaryAction().

◆ GetProjectBackupsPath()

wxString SETTINGS_MANAGER::GetProjectBackupsPath ( ) const
Returns
the full path to where project backups should be stored

Definition at line 1104 of file settings_manager.cpp.

1105 {
1107 }
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:122
#define PROJECT_BACKUPS_DIR_SUFFIX
Project settings path will be <projectname> + this.
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition: project.cpp:128

References PROJECT::GetProjectName(), PROJECT::GetProjectPath(), Prj(), and PROJECT_BACKUPS_DIR_SUFFIX.

Referenced by BackupProject(), and TriggerBackupIfNeeded().

◆ GetSettingsVersion()

std::string SETTINGS_MANAGER::GetSettingsVersion ( )
static

Parses the current KiCad build version and extracts the major and minor revision to use as the name of the settings directory for this KiCad version.

Returns
a string such as "5.1"

Definition at line 735 of file settings_manager.cpp.

736 {
737  // CMake computes the major.minor string for us.
738  return GetMajorMinorVersion().ToStdString();
739 }
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.

References GetMajorMinorVersion().

Referenced by calculateUserSettingsPath(), GetPreviousVersionPaths(), PATHS::GetUserCachePath(), PATHS::getUserDocumentPath(), and DIALOG_MIGRATE_SETTINGS::TransferDataToWindow().

◆ GetUserSettingsPath()

wxString SETTINGS_MANAGER::GetUserSettingsPath ( )
static

Return the user configuration path used to store KiCad's configuration files.

See also
calculateUserSettingsPath

NOTE: The path is cached at startup, it will never change during program lifetime!

Returns
A string containing the config path for Kicad

Definition at line 698 of file settings_manager.cpp.

699 {
700  static wxString user_settings_path;
701 
702  if( user_settings_path.empty() )
703  user_settings_path = calculateUserSettingsPath();
704 
705  return user_settings_path;
706 }
static wxString calculateUserSettingsPath(bool aIncludeVer=true, bool aUseEnv=true)
Determines the base path for user settings files.

References calculateUserSettingsPath().

Referenced by getAdvancedCfgFilename(), GetColorSettingsPath(), SYMBOL_LIB_TABLE::GetGlobalTableFileName(), FP_LIB_TABLE::GetGlobalTableFileName(), GetPathForSettingsFile(), MigrateIfNeeded(), PLUGIN_CONTENT_MANAGER::PLUGIN_CONTENT_MANAGER(), ReadHotKeyConfig(), ReadLegacyHotkeyConfigFile(), WriteHotKeyConfig(), and PLUGIN_CONTENT_MANAGER::~PLUGIN_CONTENT_MANAGER().

◆ IsOK()

bool SETTINGS_MANAGER::IsOK ( )
inline
Returns
true if settings load was successful

Definition at line 52 of file settings_manager.h.

52 { return m_ok; }
bool m_ok
True if settings loaded successfully at construction.

References m_ok.

◆ IsProjectOpen()

bool SETTINGS_MANAGER::IsProjectOpen ( ) const

Helper for checking if we have a project open TODO: This should be deprecated along with Prj() once we support multiple projects fully.

Returns
true if a call to Prj() will succeed

Definition at line 932 of file settings_manager.cpp.

933 {
934  return !m_projects.empty();
935 }
std::map< wxString, PROJECT * > m_projects
Loaded projects, mapped according to project full name.

References m_projects.

Referenced by PCB_BASE_EDIT_FRAME::doCloseWindow(), and FOOTPRINT_EDIT_FRAME::doCloseWindow().

◆ IsSettingsPathValid()

bool SETTINGS_MANAGER::IsSettingsPathValid ( const wxString &  aPath)
static

Checks if a given path is probably a valid KiCad configuration directory.

Actually it just checks if a file called "kicad_common" exists, because that's probably good enough for now.

Parameters
aPathis the path to check
Returns
true if the path contains KiCad settings

Definition at line 664 of file settings_manager.cpp.

665 {
666  wxFileName test( aPath, "kicad_common" );
667 
668  if( test.Exists() )
669  return true;
670 
671  test.SetExt( "json" );
672 
673  return test.Exists();
674 }

Referenced by GetPreviousVersionPaths(), and DIALOG_MIGRATE_SETTINGS::validatePath().

◆ Load() [1/2]

void SETTINGS_MANAGER::Load ( )

Definition at line 97 of file settings_manager.cpp.

98 {
99  // TODO(JE) We should check for dirty settings here and write them if so, because
100  // Load() could be called late in the application lifecycle
101 
102  for( auto&& settings : m_settings )
103  settings->LoadFromFile( GetPathForSettingsFile( settings.get() ) );
104 }
std::vector< std::unique_ptr< JSON_SETTINGS > > m_settings
wxString GetPathForSettingsFile(JSON_SETTINGS *aSettings)
Returns the path a given settings file should be loaded from / stored to.

References GetPathForSettingsFile(), and m_settings.

Referenced by PGM_BASE::InitPgm().

◆ Load() [2/2]

void SETTINGS_MANAGER::Load ( JSON_SETTINGS aSettings)

Definition at line 107 of file settings_manager.cpp.

108 {
109  auto it = std::find_if( m_settings.begin(), m_settings.end(),
110  [&aSettings]( const std::unique_ptr<JSON_SETTINGS>& aPtr )
111  {
112  return aPtr.get() == aSettings;
113  } );
114 
115  if( it != m_settings.end() )
116  ( *it )->LoadFromFile( GetPathForSettingsFile( it->get() ) );
117 }
std::vector< std::unique_ptr< JSON_SETTINGS > > m_settings
wxString GetPathForSettingsFile(JSON_SETTINGS *aSettings)
Returns the path a given settings file should be loaded from / stored to.

References GetPathForSettingsFile(), and m_settings.

◆ loadAllColorSettings()

void SETTINGS_MANAGER::loadAllColorSettings ( )
private

Definition at line 288 of file settings_manager.cpp.

289 {
290  // Create the built-in color settings
292  m_color_settings[settings->GetFilename()] = RegisterSettings( settings, false );
293 
294  wxFileName third_party_path;
295  const ENV_VAR_MAP& env = Pgm().GetLocalEnvVariables();
296  auto it = env.find( "KICAD6_3RD_PARTY" );
297 
298  if( it != env.end() && !it->second.GetValue().IsEmpty() )
299  third_party_path.SetPath( it->second.GetValue() );
300  else
301  third_party_path.SetPath( PATHS::GetDefault3rdPartyPath() );
302 
303  third_party_path.AppendDir( "colors" );
304 
305  wxDir third_party_colors_dir( third_party_path.GetFullPath() );
306  wxString color_settings_path = GetColorSettingsPath();
307 
308  // Search for and load any other settings
309  JSON_DIR_TRAVERSER loader( [&]( const wxFileName& aFilename )
310  {
311  registerColorSettings( aFilename.GetName() );
312  } );
313 
314  JSON_DIR_TRAVERSER thirdPartyLoader(
315  [&]( const wxFileName& aFilename )
316  {
317  COLOR_SETTINGS* settings = registerColorSettings( aFilename.GetFullPath(), true );
318  settings->SetReadOnly( true );
319  } );
320 
321  wxDir colors_dir( color_settings_path );
322 
323  if( colors_dir.IsOpened() )
324  {
325  if( third_party_colors_dir.IsOpened() )
326  third_party_colors_dir.Traverse( thirdPartyLoader );
327 
328  colors_dir.Traverse( loader );
329  }
330 }
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
void SetReadOnly(bool aReadOnly)
Definition: json_settings.h:84
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
COLOR_SETTINGS * registerColorSettings(const wxString &aFilename, bool aAbsolutePath=false)
static std::vector< COLOR_SETTINGS * > CreateBuiltinColorSettings()
Constructs and returns a list of color settings objects based on the built-in color themes.
static wxString GetColorSettingsPath()
Returns the path where color scheme files are stored; creating it if missing (normally .
std::unordered_map< wxString, COLOR_SETTINGS * > m_color_settings
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
Color settings are a bit different than most of the settings objects in that there can be more than o...
static wxString GetDefault3rdPartyPath()
Gets the default path for PCM packages.
Definition: paths.cpp:129

References COLOR_SETTINGS::CreateBuiltinColorSettings(), GetColorSettingsPath(), PATHS::GetDefault3rdPartyPath(), m_color_settings, Pgm(), registerColorSettings(), RegisterSettings(), and JSON_SETTINGS::SetReadOnly().

Referenced by ReloadColorSettings().

◆ loadColorSettingsByName()

COLOR_SETTINGS * SETTINGS_MANAGER::loadColorSettingsByName ( const wxString &  aName)
private

Attempts to load a color theme by name (the color theme directory and .json ext are assumed)

Parameters
aNameis the filename of the color theme (without the extension or path)
Returns
the loaded settings, or nullptr if load failed

Definition at line 199 of file settings_manager.cpp.

200 {
201  wxLogTrace( traceSettings, wxT( "Attempting to load color theme %s" ), aName );
202 
203  wxFileName fn( GetColorSettingsPath(), aName, "json" );
204 
205  if( !fn.IsOk() || !fn.Exists() )
206  {
207  wxLogTrace( traceSettings, wxT( "Theme file %s.json not found, falling back to user" ), aName );
208  return nullptr;
209  }
210 
211  COLOR_SETTINGS* settings = RegisterSettings( new COLOR_SETTINGS( aName ) );
212 
213  if( settings->GetFilename() != aName.ToStdString() )
214  {
215  wxLogTrace( traceSettings, wxT( "Warning: stored filename is actually %s, " ),
216  settings->GetFilename() );
217  }
218 
219  m_color_settings[aName] = settings;
220 
221  return settings;
222 }
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
wxString GetFilename() const
Definition: json_settings.h:72
static wxString GetColorSettingsPath()
Returns the path where color scheme files are stored; creating it if missing (normally .
std::unordered_map< wxString, COLOR_SETTINGS * > m_color_settings
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.

References GetColorSettingsPath(), JSON_SETTINGS::GetFilename(), m_color_settings, RegisterSettings(), and traceSettings.

Referenced by GetColorSettings().

◆ LoadProject()

bool SETTINGS_MANAGER::LoadProject ( const wxString &  aFullPath,
bool  aSetActive = true 
)

Loads a project or sets up a new project with a specified path.

Parameters
aFullPathis the full path to the project
aSetActiveif true will set the loaded project as the active project
Returns
true if the PROJECT_FILE was successfully loaded from disk

Definition at line 805 of file settings_manager.cpp.

806 {
807  // Normalize path to new format even if migrating from a legacy file
808  wxFileName path( aFullPath );
809 
810  if( path.GetExt() == LegacyProjectFileExtension )
811  path.SetExt( ProjectFileExtension );
812 
813  wxString fullPath = path.GetFullPath();
814 
815  // If already loaded, we are all set. This might be called more than once over a project's
816  // lifetime in case the project is first loaded by the KiCad manager and then eeschema or
817  // pcbnew try to load it again when they are launched.
818  if( m_projects.count( fullPath ) )
819  return true;
820 
821  bool readOnly = false;
822  std::unique_ptr<wxSingleInstanceChecker> lockFile = ::LockFile( fullPath );
823 
824  if( !lockFile )
825  {
826  wxLogTrace( traceSettings, wxT( "Project %s is locked; opening read-only" ), fullPath );
827  readOnly = true;
828  }
829 
830  // No MDI yet
831  if( aSetActive && !m_projects.empty() )
832  {
833  PROJECT* oldProject = m_projects.begin()->second;
834  unloadProjectFile( oldProject, false );
835  m_projects.erase( m_projects.begin() );
836 
837  auto it = std::find_if( m_projects_list.begin(), m_projects_list.end(),
838  [&]( const std::unique_ptr<PROJECT>& ptr )
839  {
840  return ptr.get() == oldProject;
841  } );
842 
843  wxASSERT( it != m_projects_list.end() );
844  m_projects_list.erase( it );
845  }
846 
847  wxLogTrace( traceSettings, wxT( "Load project %s" ), fullPath );
848 
849  std::unique_ptr<PROJECT> project = std::make_unique<PROJECT>();
850  project->setProjectFullName( fullPath );
851 
852  bool success = loadProjectFile( *project );
853 
854  if( success )
855  {
856  project->SetReadOnly( readOnly || project->GetProjectFile().IsReadOnly() );
857 
858  if( lockFile )
859  m_project_lock.reset( lockFile.release() );
860  }
861 
862  m_projects_list.push_back( std::move( project ) );
863  m_projects[fullPath] = m_projects_list.back().get();
864 
865  wxString fn( path.GetName() );
866 
867  PROJECT_LOCAL_SETTINGS* settings = new PROJECT_LOCAL_SETTINGS( m_projects[fullPath], fn );
868 
869  if( aSetActive )
870  settings = RegisterSettings( settings );
871  else
872  settings->LoadFromFile( path.GetPath() );
873 
874  m_projects[fullPath]->setLocalSettings( settings );
875 
876  if( aSetActive && m_kiway )
878 
879  return success;
880 }
Container for project specific data.
Definition: project.h:62
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.
const std::string ProjectFileExtension
KIWAY * m_kiway
The kiway this settings manager interacts with.
The project local settings are things that are attached to a particular project, but also might be pa...
bool unloadProjectFile(PROJECT *aProject, bool aSave)
Optionally saves, and then unloads and unregisters the given PROJECT_FILE.
virtual void ProjectChanged()
Calls ProjectChanged() on all KIWAY_PLAYERs.
Definition: kiway.cpp:587
std::unique_ptr< wxSingleInstanceChecker > LockFile(const wxString &aFileName)
Test to see if aFileName can be locked (is not already locked) and only then returns a wxSingleInstan...
Definition: lockfile.cpp:34
const std::string LegacyProjectFileExtension
std::map< wxString, PROJECT * > m_projects
Loaded projects, mapped according to project full name.
bool loadProjectFile(PROJECT &aProject)
Registers a PROJECT_FILE and attempts to load it from disk.
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.
std::vector< std::unique_ptr< PROJECT > > m_projects_list
Loaded projects (ownership here)

References LegacyProjectFileExtension, loadProjectFile(), LockFile(), m_kiway, m_project_lock, m_projects, m_projects_list, path, project, KIWAY::ProjectChanged(), ProjectFileExtension, RegisterSettings(), traceSettings, and unloadProjectFile().

Referenced by KICAD_MANAGER_FRAME::CreateNewProject(), GetDefaultProject(), PGM_BASE::InitPgm(), LoadBoard(), KI_TEST::LoadBoard(), TEST_SCH_SHEET_LIST_FIXTURE::loadSchematic(), TEST_NETLISTS_FIXTURE::loadSchematic(), NewBoard(), DIALOG_SCHEMATIC_SETUP::OnAuxiliaryAction(), DIALOG_BOARD_SETUP::OnAuxiliaryAction(), SCH_EDIT_FRAME::OnImportProject(), KICAD_MANAGER_FRAME::OnUnarchiveFiles(), PCB_EDIT_FRAME::OpenProjectFiles(), SCH_EDIT_FRAME::OpenProjectFiles(), SCH::readSchematicFromFile(), TEST_NETLIST_EXPORTER_PSPICE_SIM::TEST_NETLIST_EXPORTER_PSPICE_SIM(), and UnloadProject().

◆ loadProjectFile()

bool SETTINGS_MANAGER::loadProjectFile ( PROJECT aProject)
private

Registers a PROJECT_FILE and attempts to load it from disk.

Parameters
aProjectis the project object to load the file for
Returns
true if the PROJECT_FILE was successfully loaded

Definition at line 1050 of file settings_manager.cpp.

1051 {
1052  wxFileName fullFn( aProject.GetProjectFullName() );
1053  wxString fn( fullFn.GetName() );
1054 
1055  PROJECT_FILE* file = RegisterSettings( new PROJECT_FILE( fn ), false );
1056 
1057  m_project_files[aProject.GetProjectFullName()] = file;
1058 
1059  aProject.setProjectFile( file );
1060  file->SetProject( &aProject );
1061 
1062  wxString path( fullFn.GetPath() );
1063 
1064  return file->LoadFromFile( path );
1065 }
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
std::map< wxString, PROJECT_FILE * > m_project_files
Loaded project files, mapped according to project full name.
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:64
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition: project.cpp:116
virtual void setProjectFile(PROJECT_FILE *aFile)
Set the backing store file for this project.
Definition: project.h:320

References PROJECT::GetProjectFullName(), m_project_files, path, RegisterSettings(), and PROJECT::setProjectFile().

Referenced by LoadProject().

◆ MigrateIfNeeded()

bool SETTINGS_MANAGER::MigrateIfNeeded ( )

Handles the initialization of the user settings directory and migration from previous KiCad versions as needed.

This method will check for the existence of the user settings path for this KiCad version. If it exists, settings load will proceed normally using that path.

If that directory is empty or does not exist, the migration wizard will be launched, which will give users the option to migrate settings from a previous KiCad version (if one is found), manually specify a directory to migrate fromm, or start with default settings.

Returns
true if migration was successful or not necessary, false otherwise

Definition at line 483 of file settings_manager.cpp.

484 {
485  if( m_headless )
486  {
487  wxLogTrace( traceSettings, wxT( "Settings migration not checked; running headless" ) );
488  return false;
489  }
490 
491  wxFileName path( GetUserSettingsPath(), "" );
492  wxLogTrace( traceSettings, wxT( "Using settings path %s" ), path.GetFullPath() );
493 
494  if( path.DirExists() )
495  {
496  wxFileName common = path;
497  common.SetName( "kicad_common" );
498  common.SetExt( "json" );
499 
500  if( common.Exists() )
501  {
502  wxLogTrace( traceSettings, wxT( "Path exists and has a kicad_common, continuing!" ) );
503  return true;
504  }
505  }
506 
507  // Now we have an empty path, let's figure out what to put in it
508  DIALOG_MIGRATE_SETTINGS dlg( this );
509 
510  if( dlg.ShowModal() != wxID_OK )
511  {
512  wxLogTrace( traceSettings, wxT( "Migration dialog canceled; exiting" ) );
513  return false;
514  }
515 
516  if( !path.DirExists() )
517  {
518  wxLogTrace( traceSettings, wxT( "Path didn't exist; creating it" ) );
519  path.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
520  }
521 
522  if( m_migration_source.IsEmpty() )
523  {
524  wxLogTrace( traceSettings, wxT( "No migration source given; starting with defaults" ) );
525  return true;
526  }
527 
528  wxLogTrace( traceSettings, wxT( "Migrating from path %s" ), m_migration_source );
529 
531  wxDir source_dir( m_migration_source );
532 
533  source_dir.Traverse( traverser );
534 
535  if( !traverser.GetErrors().empty() )
536  DisplayErrorMessage( nullptr, traverser.GetErrors() );
537 
538  // Remove any library configuration if we didn't choose to import
540  {
541  COMMON_SETTINGS common;
542  wxString commonPath = GetPathForSettingsFile( &common );
543  common.LoadFromFile( commonPath );
544 
545  const std::vector<wxString> libKeys = {
546  wxT( "KICAD6_SYMBOL_DIR" ),
547  wxT( "KICAD6_3DMODEL_DIR" ),
548  wxT( "KICAD6_FOOTPRINT_DIR" ),
549  wxT( "KICAD6_TEMPLATE_DIR" ), // Stores the default library table to be copied
550 
551  // Deprecated keys
552  wxT( "KICAD_PTEMPLATES" ),
553  wxT( "KISYS3DMOD" ),
554  wxT( "KISYSMOD" ),
555  wxT( "KICAD_SYMBOL_DIR" ),
556  };
557 
558  for( const wxString& key : libKeys )
559  common.m_Env.vars.erase( key );
560 
561  common.SaveToFile( commonPath );
562  }
563 
564  return true;
565 }
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:292
bool m_migrateLibraryTables
If true, the symbol and footprint library tables will be migrated from the previous version.
virtual bool LoadFromFile(const wxString &aDirectory="")
Loads the backing file from disk and then calls Load()
wxString m_migration_source
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
bool m_headless
True if running outside a UI context.
ENVIRONMENT m_Env
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.
wxString GetPathForSettingsFile(JSON_SETTINGS *aSettings)
Returns the path a given settings file should be loaded from / stored to.

References DisplayErrorMessage(), GetPathForSettingsFile(), GetUserSettingsPath(), JSON_SETTINGS::LoadFromFile(), COMMON_SETTINGS::m_Env, m_headless, m_migrateLibraryTables, m_migration_source, path, JSON_SETTINGS::SaveToFile(), traceSettings, and COMMON_SETTINGS::ENVIRONMENT::vars.

Referenced by SETTINGS_MANAGER().

◆ Prj()

PROJECT & SETTINGS_MANAGER::Prj ( ) const

A helper while we are not MDI-capable – return the one and only project.

Returns
the loaded project

Definition at line 924 of file settings_manager.cpp.

925 {
926  // No MDI yet: First project in the list is the active project
927  wxASSERT_MSG( m_projects_list.size(), wxT( "no project in list" ) );
928  return *m_projects_list.begin()->get();
929 }
std::vector< std::unique_ptr< PROJECT > > m_projects_list
Loaded projects (ownership here)

References m_projects_list.

Referenced by BackupProject(), PCB_EDIT_FRAME::Files_io_from_id(), GetPathForSettingsFile(), GetProjectBackupsPath(), KI_TEST::LoadBoard(), TEST_SCH_SHEET_LIST_FIXTURE::loadSchematic(), TEST_NETLISTS_FIXTURE::loadSchematic(), PCB_EDIT_FRAME::OpenProjectFiles(), SaveProject(), SaveProjectAs(), SaveProjectCopy(), TEST_NETLIST_EXPORTER_PSPICE_SIM::TEST_NETLIST_EXPORTER_PSPICE_SIM(), TriggerBackupIfNeeded(), and WriteDRCReport().

◆ registerColorSettings()

COLOR_SETTINGS * SETTINGS_MANAGER::registerColorSettings ( const wxString &  aFilename,
bool  aAbsolutePath = false 
)
private

Definition at line 253 of file settings_manager.cpp.

254 {
255  if( !m_color_settings.count( aName ) )
256  {
257  COLOR_SETTINGS* colorSettings = RegisterSettings( new COLOR_SETTINGS( aName,
258  aAbsolutePath ) );
259  m_color_settings[aName] = colorSettings;
260  }
261 
262  return m_color_settings.at( aName );
263 }
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
std::unordered_map< wxString, COLOR_SETTINGS * > m_color_settings
Color settings are a bit different than most of the settings objects in that there can be more than o...

References m_color_settings, and RegisterSettings().

Referenced by AddNewColorSettings(), GetColorSettings(), GetMigratedColorSettings(), and loadAllColorSettings().

◆ RegisterSettings()

template<typename T >
T* SETTINGS_MANAGER::RegisterSettings ( T *  aSettings,
bool  aLoadNow = true 
)
inline

Takes ownership of the pointer passed in.

Parameters
aSettingsis a settings object to register
Returns
a handle to the owned pointer

Definition at line 67 of file settings_manager.h.

68  {
69  return static_cast<T*>( registerSettings( aSettings, aLoadNow ) );
70  }
JSON_SETTINGS * registerSettings(JSON_SETTINGS *aSettings, bool aLoadNow=true)

References registerSettings().

Referenced by GetAppSettings(), loadAllColorSettings(), loadColorSettingsByName(), LoadProject(), loadProjectFile(), PCBCALC::IFACE::OnKifaceStart(), CV::IFACE::OnKifaceStart(), PGE::IFACE::OnKifaceStart(), GERBV::IFACE::OnKifaceStart(), PCB::IFACE::OnKifaceStart(), SCH::IFACE::OnKifaceStart(), PGM_KICAD::OnPgmInit(), registerColorSettings(), and SETTINGS_MANAGER().

◆ registerSettings()

JSON_SETTINGS * SETTINGS_MANAGER::registerSettings ( JSON_SETTINGS aSettings,
bool  aLoadNow = true 
)
private

Definition at line 81 of file settings_manager.cpp.

82 {
83  std::unique_ptr<JSON_SETTINGS> ptr( aSettings );
84 
85  ptr->SetManager( this );
86 
87  wxLogTrace( traceSettings, wxT( "Registered new settings object <%s>" ), ptr->GetFullFilename() );
88 
89  if( aLoadNow )
90  ptr->LoadFromFile( GetPathForSettingsFile( ptr.get() ) );
91 
92  m_settings.push_back( std::move( ptr ) );
93  return m_settings.back().get();
94 }
std::vector< std::unique_ptr< JSON_SETTINGS > > m_settings
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.
wxString GetPathForSettingsFile(JSON_SETTINGS *aSettings)
Returns the path a given settings file should be loaded from / stored to.

References GetPathForSettingsFile(), m_settings, and traceSettings.

Referenced by RegisterSettings().

◆ ReloadColorSettings()

void SETTINGS_MANAGER::ReloadColorSettings ( )

Re-scans the color themes directory, reloading any changes it finds.

Definition at line 333 of file settings_manager.cpp.

334 {
335  m_color_settings.clear();
337 }
std::unordered_map< wxString, COLOR_SETTINGS * > m_color_settings

References loadAllColorSettings(), and m_color_settings.

Referenced by PANEL_EESCHEMA_COLOR_SETTINGS::PANEL_EESCHEMA_COLOR_SETTINGS(), and PANEL_FP_EDITOR_COLOR_SETTINGS::PANEL_FP_EDITOR_COLOR_SETTINGS().

◆ Save() [1/2]

void SETTINGS_MANAGER::Save ( )

Definition at line 120 of file settings_manager.cpp.

121 {
122  for( auto&& settings : m_settings )
123  {
124  // Never automatically save color settings, caller should use SaveColorSettings
125  if( dynamic_cast<COLOR_SETTINGS*>( settings.get() ) )
126  continue;
127 
128  settings->SaveToFile( GetPathForSettingsFile( settings.get() ) );
129  }
130 }
std::vector< std::unique_ptr< JSON_SETTINGS > > m_settings
wxString GetPathForSettingsFile(JSON_SETTINGS *aSettings)
Returns the path a given settings file should be loaded from / stored to.

References GetPathForSettingsFile(), and m_settings.

Referenced by GetMigratedColorSettings(), FOOTPRINT_EDITOR_SETTINGS::MigrateFromLegacy(), and COLOR_SETTINGS::migrateSchema0to1().

◆ Save() [2/2]

void SETTINGS_MANAGER::Save ( JSON_SETTINGS aSettings)

Definition at line 133 of file settings_manager.cpp.

134 {
135  auto it = std::find_if( m_settings.begin(), m_settings.end(),
136  [&aSettings]( const std::unique_ptr<JSON_SETTINGS>& aPtr )
137  {
138  return aPtr.get() == aSettings;
139  } );
140 
141  if( it != m_settings.end() )
142  {
143  wxLogTrace( traceSettings, wxT( "Saving %s" ), ( *it )->GetFullFilename() );
144  ( *it )->SaveToFile( GetPathForSettingsFile( it->get() ) );
145  }
146 }
std::vector< std::unique_ptr< JSON_SETTINGS > > m_settings
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.
wxString GetPathForSettingsFile(JSON_SETTINGS *aSettings)
Returns the path a given settings file should be loaded from / stored to.

References GetPathForSettingsFile(), m_settings, and traceSettings.

◆ SaveColorSettings()

void SETTINGS_MANAGER::SaveColorSettings ( COLOR_SETTINGS aSettings,
const std::string &  aNamespace = "" 
)

Safely saves a COLOR_SETTINGS to disk, preserving any changes outside the given namespace.

A color settings namespace is one of the top-level JSON objects like "board", etc. This will perform a read-modify-write

Parameters
aSettingsis a pointer to a valid COLOR_SETTINGS object managed by SETTINGS_MANAGER
aNamespaceis the namespace of settings to save

Definition at line 340 of file settings_manager.cpp.

341 {
342  // The passed settings should already be managed
343  wxASSERT( std::find_if( m_color_settings.begin(), m_color_settings.end(),
344  [aSettings] ( const std::pair<wxString, COLOR_SETTINGS*>& el )
345  {
346  return el.second->GetFilename() == aSettings->GetFilename();
347  }
348  ) != m_color_settings.end() );
349 
350  if( aSettings->IsReadOnly() )
351  return;
352 
353  if( !aSettings->Store() )
354  {
355  wxLogTrace( traceSettings, wxT( "Color scheme %s not modified; skipping save" ),
356  aNamespace );
357  return;
358  }
359 
360  wxASSERT( aSettings->Contains( aNamespace ) );
361 
362  wxLogTrace( traceSettings, wxT( "Saving color scheme %s, preserving %s" ),
363  aSettings->GetFilename(),
364  aNamespace );
365 
366  OPT<nlohmann::json> backup = aSettings->GetJson( aNamespace );
367  wxString path = GetColorSettingsPath();
368 
369  aSettings->LoadFromFile( path );
370 
371  if( backup )
372  ( *aSettings->Internals() )[aNamespace].update( *backup );
373 
374  aSettings->Load();
375 
376  aSettings->SaveToFile( path, true );
377 }
virtual bool Store()
Stores the current parameters into the JSON document represented by this object Note: this doesn't do...
JSON_SETTINGS_INTERNALS * Internals()
wxString GetFilename() const
Definition: json_settings.h:72
virtual bool LoadFromFile(const wxString &aDirectory="")
Loads the backing file from disk and then calls Load()
OPT< nlohmann::json > GetJson(const std::string &aPath) const
Fetches a JSON object that is a subset of this JSON_SETTINGS object, using a path of the form "key1....
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
static wxString GetColorSettingsPath()
Returns the path where color scheme files are stored; creating it if missing (normally .
std::unordered_map< wxString, COLOR_SETTINGS * > m_color_settings
bool IsReadOnly() const
Definition: json_settings.h:83
boost::optional< T > OPT
Definition: optional.h:7
bool Contains(const std::string &aPath) const
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.
virtual void Load()
Updates the parameters of this object based on the current JSON document contents.

References JSON_SETTINGS::Contains(), GetColorSettingsPath(), JSON_SETTINGS::GetFilename(), JSON_SETTINGS::GetJson(), JSON_SETTINGS::Internals(), JSON_SETTINGS::IsReadOnly(), JSON_SETTINGS::Load(), JSON_SETTINGS::LoadFromFile(), m_color_settings, path, JSON_SETTINGS::SaveToFile(), JSON_SETTINGS::Store(), and traceSettings.

Referenced by FOOTPRINT_EDITOR_SETTINGS::MigrateFromLegacy(), PANEL_FP_EDITOR_COLOR_SETTINGS::PANEL_FP_EDITOR_COLOR_SETTINGS(), PANEL_COLOR_SETTINGS::saveCurrentTheme(), FOOTPRINT_EDIT_FRAME::SaveSettings(), and PCB_EDIT_FRAME::SaveSettings().

◆ SaveProject()

bool SETTINGS_MANAGER::SaveProject ( const wxString &  aFullPath = wxEmptyString,
PROJECT aProject = nullptr 
)

Saves a loaded project.

Parameters
aFullPathis the project name to save. If empty, will save the first loaded project.
aProjectis the project to save, or nullptr to save the active project (Prj() return)
Returns
true if save was successful

Definition at line 958 of file settings_manager.cpp.

959 {
960  if( !aProject )
961  aProject = &Prj();
962 
963  wxString path = aFullPath;
964 
965  if( path.empty() )
966  path = aProject->GetProjectFullName();
967 
968  // TODO: refactor for MDI
969  if( aProject->IsReadOnly() )
970  return false;
971 
972  if( !m_project_files.count( path ) )
973  return false;
974 
976  wxString projectPath = aProject->GetProjectPath();
977 
978  project->SaveToFile( projectPath );
979  aProject->GetLocalSettings().SaveToFile( projectPath );
980 
981  return true;
982 }
virtual bool IsReadOnly() const
Definition: project.h:124
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
bool SaveToFile(const wxString &aDirectory="", bool aForce=false) override
std::map< wxString, PROJECT_FILE * > m_project_files
Loaded project files, mapped according to project full name.
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:122
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:64
virtual PROJECT_LOCAL_SETTINGS & GetLocalSettings() const
Definition: project.h:151
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition: project.cpp:116

References PROJECT::GetLocalSettings(), PROJECT::GetProjectFullName(), PROJECT::GetProjectPath(), PROJECT::IsReadOnly(), m_project_files, path, Prj(), project, and PROJECT_LOCAL_SETTINGS::SaveToFile().

Referenced by KICAD_MANAGER_FRAME::CloseProject(), KICAD_MANAGER_FRAME::CreateNewProject(), PCB_EDIT_FRAME::Files_io_from_id(), SCH_EDIT_FRAME::OpenProjectFiles(), SCH_EDIT_FRAME::SaveProject(), SaveProjectAs(), SCH_EDIT_FRAME::SaveProjectSettings(), and PCB_EDIT_FRAME::SaveProjectSettings().

◆ SaveProjectAs()

void SETTINGS_MANAGER::SaveProjectAs ( const wxString &  aFullPath,
PROJECT aProject = nullptr 
)

Sets the currently loaded project path and saves it (pointers remain valid) Note that this will not modify the read-only state of the project, so it will have no effect if the project is marked as read-only!

Parameters
aFullPathis the full filename to set for the project
aProjectis the project to save, or nullptr to save the active project (Prj() return)

Definition at line 985 of file settings_manager.cpp.

986 {
987  if( !aProject )
988  aProject = &Prj();
989 
990  wxString oldName = aProject->GetProjectFullName();
991 
992  if( aFullPath.IsSameAs( oldName ) )
993  {
994  SaveProject( aFullPath, aProject );
995  return;
996  }
997 
998  // Changing this will cause UnloadProject to not save over the "old" project when loading below
999  aProject->setProjectFullName( aFullPath );
1000 
1001  wxFileName fn( aFullPath );
1002 
1003  PROJECT_FILE* project = m_project_files.at( oldName );
1004 
1005  // Ensure read-only flags are copied; this allows doing a "Save As" on a standalong board/sch
1006  // without creating project files if the checkbox is turned off
1007  project->SetReadOnly( aProject->IsReadOnly() );
1008  aProject->GetLocalSettings().SetReadOnly( aProject->IsReadOnly() );
1009 
1010  project->SetFilename( fn.GetName() );
1011  project->SaveToFile( fn.GetPath() );
1012 
1013  aProject->GetLocalSettings().SetFilename( fn.GetName() );
1014  aProject->GetLocalSettings().SaveToFile( fn.GetPath() );
1015 
1016  m_project_files[fn.GetFullPath()] = project;
1017  m_project_files.erase( oldName );
1018 
1019  m_projects[fn.GetFullPath()] = m_projects[oldName];
1020  m_projects.erase( oldName );
1021 }
virtual bool IsReadOnly() const
Definition: project.h:124
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
bool SaveProject(const wxString &aFullPath=wxEmptyString, PROJECT *aProject=nullptr)
Saves a loaded project.
void SetReadOnly(bool aReadOnly)
Definition: json_settings.h:84
bool SaveToFile(const wxString &aDirectory="", bool aForce=false) override
std::map< wxString, PROJECT_FILE * > m_project_files
Loaded project files, mapped according to project full name.
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:64
virtual PROJECT_LOCAL_SETTINGS & GetLocalSettings() const
Definition: project.h:151
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition: project.cpp:116
std::map< wxString, PROJECT * > m_projects
Loaded projects, mapped according to project full name.
virtual void setProjectFullName(const wxString &aFullPathAndName)
Set the full directory, basename, and extension of the project.
Definition: project.cpp:84
void SetFilename(const wxString &aFilename)
Definition: json_settings.h:76

References PROJECT::GetLocalSettings(), PROJECT::GetProjectFullName(), PROJECT::IsReadOnly(), m_project_files, m_projects, Prj(), project, SaveProject(), PROJECT_LOCAL_SETTINGS::SaveToFile(), JSON_SETTINGS::SetFilename(), PROJECT::setProjectFullName(), and JSON_SETTINGS::SetReadOnly().

Referenced by SaveBoard(), PCB_EDIT_FRAME::SavePcbFile(), and SCH_EDIT_FRAME::SaveProject().

◆ SaveProjectCopy()

void SETTINGS_MANAGER::SaveProjectCopy ( const wxString &  aFullPath,
PROJECT aProject = nullptr 
)

Saves a copy of the current project under the given path.

Will save the copy even if the current project is marked as read-only.

Parameters
aFullPathis the full filename to set for the project
aProjectis the project to save, or nullptr to save the active project (Prj() return)

Definition at line 1024 of file settings_manager.cpp.

1025 {
1026  if( !aProject )
1027  aProject = &Prj();
1028 
1030  wxString oldName = project->GetFilename();
1031  wxFileName fn( aFullPath );
1032 
1033  bool readOnly = project->IsReadOnly();
1034  project->SetReadOnly( false );
1035 
1036  project->SetFilename( fn.GetName() );
1037  project->SaveToFile( fn.GetPath() );
1038  project->SetFilename( oldName );
1039 
1040  PROJECT_LOCAL_SETTINGS& localSettings = aProject->GetLocalSettings();
1041 
1042  localSettings.SetFilename( fn.GetName() );
1043  localSettings.SaveToFile( fn.GetPath() );
1044  localSettings.SetFilename( oldName );
1045 
1046  project->SetReadOnly( readOnly );
1047 }
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
The project local settings are things that are attached to a particular project, but also might be pa...
bool SaveToFile(const wxString &aDirectory="", bool aForce=false) override
std::map< wxString, PROJECT_FILE * > m_project_files
Loaded project files, mapped according to project full name.
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:64
virtual PROJECT_LOCAL_SETTINGS & GetLocalSettings() const
Definition: project.h:151
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition: project.cpp:116
void SetFilename(const wxString &aFilename)
Definition: json_settings.h:76

References PROJECT::GetLocalSettings(), PROJECT::GetProjectFullName(), m_project_files, Prj(), project, PROJECT_LOCAL_SETTINGS::SaveToFile(), and JSON_SETTINGS::SetFilename().

Referenced by PCB_EDIT_FRAME::SavePcbCopy(), and SCH_EDIT_FRAME::SaveProject().

◆ SetKiway()

void SETTINGS_MANAGER::SetKiway ( KIWAY aKiway)
inline

Associate this setting manager with the given Kiway.

Parameters
aKiwayis the kiway this settings manager should use

Definition at line 59 of file settings_manager.h.

59 { m_kiway = aKiway; }
KIWAY * m_kiway
The kiway this settings manager interacts with.

References m_kiway.

Referenced by PGM_KICAD::OnPgmInit(), and PGM_SINGLE_TOP::OnPgmInit().

◆ SetMigrateLibraryTables()

void SETTINGS_MANAGER::SetMigrateLibraryTables ( bool  aMigrate = true)
inline

Definition at line 218 of file settings_manager.h.

218 { m_migrateLibraryTables = aMigrate; }
bool m_migrateLibraryTables
If true, the symbol and footprint library tables will be migrated from the previous version.

References m_migrateLibraryTables.

Referenced by DIALOG_MIGRATE_SETTINGS::TransferDataFromWindow().

◆ SetMigrationSource()

void SETTINGS_MANAGER::SetMigrationSource ( const wxString &  aSource)
inline

Helper for DIALOG_MIGRATE_SETTINGS to specify a source for migration.

Parameters
aSourceis a directory containing settings files to migrate from (can be empty)

Definition at line 216 of file settings_manager.h.

216 { m_migration_source = aSource; }
wxString m_migration_source

References m_migration_source.

Referenced by DIALOG_MIGRATE_SETTINGS::TransferDataFromWindow().

◆ TriggerBackupIfNeeded()

bool SETTINGS_MANAGER::TriggerBackupIfNeeded ( REPORTER aReporter) const

Calls BackupProject if a new backup is needed according to the current backup policy.

Parameters
aReporteris used for progress reporting
Returns
if everything succeeded

Definition at line 1177 of file settings_manager.cpp.

1178 {
1180 
1181  if( !settings.enabled )
1182  return true;
1183 
1184  wxString prefix = Prj().GetProjectName() + '-';
1185 
1186  auto modTime =
1187  [&prefix]( const wxString& aFile )
1188  {
1189  wxDateTime dt;
1190  wxString fn( wxFileName( aFile ).GetName() );
1191  fn.Replace( prefix, "" );
1192  dt.ParseFormat( fn, backupDateTimeFormat );
1193  return dt;
1194  };
1195 
1196  wxFileName projectPath( Prj().GetProjectPath() );
1197 
1198  // Skip backup if project path isn't valid or writable
1199  if( !projectPath.IsOk() || !projectPath.Exists() || !projectPath.IsDirWritable() )
1200  return true;
1201 
1202  wxString backupPath = GetProjectBackupsPath();
1203 
1204  if( !wxDirExists( backupPath ) )
1205  {
1206  wxLogTrace( traceSettings, wxT( "Backup path %s doesn't exist, creating it" ), backupPath );
1207 
1208  if( !wxMkdir( backupPath ) )
1209  {
1210  wxLogTrace( traceSettings, wxT( "Could not create backups path! Skipping backup" ) );
1211  return false;
1212  }
1213  }
1214 
1215  wxDir dir( backupPath );
1216 
1217  if( !dir.IsOpened() )
1218  {
1219  wxLogTrace( traceSettings, wxT( "Could not open project backups path %s" ), dir.GetName() );
1220  return false;
1221  }
1222 
1223  std::vector<wxString> files;
1224 
1225  VECTOR_INSERT_TRAVERSER traverser( files,
1226  [&modTime]( const wxString& aFile )
1227  {
1228  return modTime( aFile ).IsValid();
1229  } );
1230 
1231  dir.Traverse( traverser, wxT( "*.zip" ) );
1232 
1233  // Sort newest-first
1234  std::sort( files.begin(), files.end(),
1235  [&]( const wxString& aFirst, const wxString& aSecond ) -> bool
1236  {
1237  wxDateTime first = modTime( aFirst );
1238  wxDateTime second = modTime( aSecond );
1239 
1240  return first.GetTicks() > second.GetTicks();
1241  } );
1242 
1243  // Do we even need to back up?
1244  if( !files.empty() )
1245  {
1246  wxDateTime lastTime = modTime( files[0] );
1247 
1248  if( lastTime.IsValid() )
1249  {
1250  wxTimeSpan delta = wxDateTime::Now() - modTime( files[0] );
1251 
1252  if( delta.IsShorterThan( wxTimeSpan::Seconds( settings.min_interval ) ) )
1253  return true;
1254  }
1255  }
1256 
1257  // Now that we know a backup is needed, apply the retention policy
1258 
1259  // Step 1: if we're over the total file limit, remove the oldest
1260  if( !files.empty() && settings.limit_total_files > 0 )
1261  {
1262  while( files.size() > static_cast<size_t>( settings.limit_total_files ) )
1263  {
1264  wxRemoveFile( files.back() );
1265  files.pop_back();
1266  }
1267  }
1268 
1269  // Step 2: Stay under the total size limit
1270  if( settings.limit_total_size > 0 )
1271  {
1272  wxULongLong totalSize = 0;
1273 
1274  for( const wxString& file : files )
1275  totalSize += wxFileName::GetSize( file );
1276 
1277  while( !files.empty() && totalSize > static_cast<wxULongLong>( settings.limit_total_size ) )
1278  {
1279  totalSize -= wxFileName::GetSize( files.back() );
1280  wxRemoveFile( files.back() );
1281  files.pop_back();
1282  }
1283  }
1284 
1285  // Step 3: Stay under the daily limit
1286  if( settings.limit_daily_files > 0 && files.size() > 1 )
1287  {
1288  wxDateTime day = modTime( files[0] );
1289  int num = 1;
1290 
1291  wxASSERT( day.IsValid() );
1292 
1293  std::vector<wxString> filesToDelete;
1294 
1295  for( size_t i = 1; i < files.size(); i++ )
1296  {
1297  wxDateTime dt = modTime( files[i] );
1298 
1299  if( dt.IsSameDate( day ) )
1300  {
1301  num++;
1302 
1303  if( num > settings.limit_daily_files )
1304  filesToDelete.emplace_back( files[i] );
1305  }
1306  else
1307  {
1308  day = dt;
1309  num = 1;
1310  }
1311  }
1312 
1313  for( const wxString& file : filesToDelete )
1314  wxRemoveFile( file );
1315  }
1316 
1317  return BackupProject( aReporter );
1318 }
static wxString backupDateTimeFormat
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
unsigned long long limit_total_size
Maximum total size of backups (bytes), 0 for unlimited.
bool enabled
Automatically back up the project when files are saved.
AUTO_BACKUP m_Backup
bool BackupProject(REPORTER &aReporter) const
Creates a backup archive of the current project.
COMMON_SETTINGS * GetCommonSettings() const
Retrieves the common settings shared by all applications.
wxString GetProjectBackupsPath() const
int min_interval
Minimum time, in seconds, between subsequent backups.
constexpr int delta
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition: project.cpp:128
int limit_daily_files
Maximum files to keep per day, 0 for unlimited.
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.
int limit_total_files
Maximum number of backup archives to retain.

References backupDateTimeFormat, BackupProject(), delta, COMMON_SETTINGS::AUTO_BACKUP::enabled, GetCommonSettings(), GetProjectBackupsPath(), PROJECT::GetProjectName(), COMMON_SETTINGS::AUTO_BACKUP::limit_daily_files, COMMON_SETTINGS::AUTO_BACKUP::limit_total_files, COMMON_SETTINGS::AUTO_BACKUP::limit_total_size, COMMON_SETTINGS::m_Backup, COMMON_SETTINGS::AUTO_BACKUP::min_interval, Prj(), and traceSettings.

Referenced by KICAD_MANAGER_FRAME::CloseProject(), PCB_EDIT_FRAME::doAutoSave(), and SCH_EDIT_FRAME::doAutoSave().

◆ UnloadProject()

bool SETTINGS_MANAGER::UnloadProject ( PROJECT aProject,
bool  aSave = true 
)

Saves, unloads and unregisters the given PROJECT.

Parameters
aProjectis the project object to unload
aSaveif true will save the project before unloading
Returns
true if the PROJECT file was successfully saved

Definition at line 883 of file settings_manager.cpp.

884 {
885  if( !aProject || !m_projects.count( aProject->GetProjectFullName() ) )
886  return false;
887 
888  if( !unloadProjectFile( aProject, aSave ) )
889  return false;
890 
891  wxString projectPath = aProject->GetProjectFullName();
892  wxLogTrace( traceSettings, wxT( "Unload project %s" ), projectPath );
893 
894  PROJECT* toRemove = m_projects.at( projectPath );
895  auto it = std::find_if( m_projects_list.begin(), m_projects_list.end(),
896  [&]( const std::unique_ptr<PROJECT>& ptr )
897  {
898  return ptr.get() == toRemove;
899  } );
900 
901  wxASSERT( it != m_projects_list.end() );
902  m_projects_list.erase( it );
903 
904  m_projects.erase( projectPath );
905 
906  // Immediately reload a null project; this is required until the rest of the application
907  // is refactored to not assume that Prj() always works
908  if( m_projects.empty() )
909  LoadProject( "" );
910 
911  // Remove the reference in the environment to the previous project
912  wxSetEnv( PROJECT_VAR_NAME, "" );
913 
914  // Release lock on the file, in case we had one
915  m_project_lock = nullptr;
916 
917  if( m_kiway )
919 
920  return true;
921 }
Container for project specific data.
Definition: project.h:62
std::unique_ptr< wxSingleInstanceChecker > m_project_lock
Lock for loaded project (expand to multiple once we support MDI)
KIWAY * m_kiway
The kiway this settings manager interacts with.
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition: project.h:38
bool unloadProjectFile(PROJECT *aProject, bool aSave)
Optionally saves, and then unloads and unregisters the given PROJECT_FILE.
virtual void ProjectChanged()
Calls ProjectChanged() on all KIWAY_PLAYERs.
Definition: kiway.cpp:587
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition: project.cpp:116
std::map< wxString, PROJECT * > m_projects
Loaded projects, mapped according to project full name.
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Loads a project or sets up a new project with a specified path.
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.
std::vector< std::unique_ptr< PROJECT > > m_projects_list
Loaded projects (ownership here)

References PROJECT::GetProjectFullName(), LoadProject(), m_kiway, m_project_lock, m_projects, m_projects_list, PROJECT_VAR_NAME, KIWAY::ProjectChanged(), traceSettings, and unloadProjectFile().

Referenced by KICAD_MANAGER_FRAME::CloseProject(), PCB_BASE_EDIT_FRAME::doCloseWindow(), PCB_EDIT_FRAME::Files_io_from_id(), DIALOG_SCHEMATIC_SETUP::OnAuxiliaryAction(), DIALOG_BOARD_SETUP::OnAuxiliaryAction(), SCH_EDIT_FRAME::OnImportProject(), KICAD_MANAGER_FRAME::OnUnarchiveFiles(), PCB_EDIT_FRAME::OpenProjectFiles(), SCH_EDIT_FRAME::OpenProjectFiles(), and SCH_EDIT_FRAME::~SCH_EDIT_FRAME().

◆ unloadProjectFile()

bool SETTINGS_MANAGER::unloadProjectFile ( PROJECT aProject,
bool  aSave 
)
private

Optionally saves, and then unloads and unregisters the given PROJECT_FILE.

Parameters
aProjectis the project object to unload the file for
aSaveif true will save the project file before unloading
Returns
true if the PROJECT file was successfully saved

Definition at line 1068 of file settings_manager.cpp.

1069 {
1070  if( !aProject )
1071  return false;
1072 
1073  wxString name = aProject->GetProjectFullName();
1074 
1075  if( !m_project_files.count( name ) )
1076  return false;
1077 
1079 
1080  auto it = std::find_if( m_settings.begin(), m_settings.end(),
1081  [&file]( const std::unique_ptr<JSON_SETTINGS>& aPtr )
1082  {
1083  return aPtr.get() == file;
1084  } );
1085 
1086  if( it != m_settings.end() )
1087  {
1088  wxString projectPath = GetPathForSettingsFile( it->get() );
1089 
1090  FlushAndRelease( &aProject->GetLocalSettings(), aSave );
1091 
1092  if( aSave )
1093  ( *it )->SaveToFile( projectPath );
1094 
1095  m_settings.erase( it );
1096  }
1097 
1098  m_project_files.erase( name );
1099 
1100  return true;
1101 }
std::map< wxString, PROJECT_FILE * > m_project_files
Loaded project files, mapped according to project full name.
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:64
virtual PROJECT_LOCAL_SETTINGS & GetLocalSettings() const
Definition: project.h:151
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition: project.cpp:116
std::vector< std::unique_ptr< JSON_SETTINGS > > m_settings
const char * name
Definition: DXF_plotter.cpp:56
void FlushAndRelease(JSON_SETTINGS *aSettings, bool aSave=true)
If the given settings object is registered, save it to disk and unregister it.
wxString GetPathForSettingsFile(JSON_SETTINGS *aSettings)
Returns the path a given settings file should be loaded from / stored to.

References FlushAndRelease(), PROJECT::GetLocalSettings(), GetPathForSettingsFile(), PROJECT::GetProjectFullName(), m_project_files, m_settings, and name.

Referenced by LoadProject(), and UnloadProject().

Member Data Documentation

◆ backupDateTimeFormat

wxString SETTINGS_MANAGER::backupDateTimeFormat = wxT( "%Y-%m-%d_%H%M%S" )
staticprivate

Definition at line 454 of file settings_manager.h.

Referenced by BackupProject(), and TriggerBackupIfNeeded().

◆ m_app_settings_cache

std::unordered_map<size_t, JSON_SETTINGS*> SETTINGS_MANAGER::m_app_settings_cache
private

Cache for app settings.

Definition at line 429 of file settings_manager.h.

Referenced by FlushAndRelease(), and GetAppSettings().

◆ m_color_settings

◆ m_common_settings

COMMON_SETTINGS* SETTINGS_MANAGER::m_common_settings
private

Definition at line 432 of file settings_manager.h.

Referenced by GetCommonSettings(), and SETTINGS_MANAGER().

◆ m_headless

bool SETTINGS_MANAGER::m_headless
private

True if running outside a UI context.

Definition at line 419 of file settings_manager.h.

Referenced by MigrateIfNeeded().

◆ m_kiway

KIWAY* SETTINGS_MANAGER::m_kiway
private

The kiway this settings manager interacts with.

Definition at line 422 of file settings_manager.h.

Referenced by LoadProject(), SetKiway(), and UnloadProject().

◆ m_migrateLibraryTables

bool SETTINGS_MANAGER::m_migrateLibraryTables
private

If true, the symbol and footprint library tables will be migrated from the previous version.

Definition at line 437 of file settings_manager.h.

Referenced by MigrateIfNeeded(), and SetMigrateLibraryTables().

◆ m_migration_source

wxString SETTINGS_MANAGER::m_migration_source
private

Definition at line 434 of file settings_manager.h.

Referenced by MigrateIfNeeded(), and SetMigrationSource().

◆ m_ok

bool SETTINGS_MANAGER::m_ok
private

True if settings loaded successfully at construction.

Definition at line 440 of file settings_manager.h.

Referenced by IsOK(), and SETTINGS_MANAGER().

◆ m_project_files

std::map<wxString, PROJECT_FILE*> SETTINGS_MANAGER::m_project_files
private

Loaded project files, mapped according to project full name.

Definition at line 449 of file settings_manager.h.

Referenced by loadProjectFile(), SaveProject(), SaveProjectAs(), SaveProjectCopy(), and unloadProjectFile().

◆ m_project_lock

std::unique_ptr<wxSingleInstanceChecker> SETTINGS_MANAGER::m_project_lock
private

Lock for loaded project (expand to multiple once we support MDI)

Definition at line 452 of file settings_manager.h.

Referenced by LoadProject(), and UnloadProject().

◆ m_projects

std::map<wxString, PROJECT*> SETTINGS_MANAGER::m_projects
private

Loaded projects, mapped according to project full name.

Definition at line 446 of file settings_manager.h.

Referenced by GetOpenProjects(), GetProject(), IsProjectOpen(), LoadProject(), SaveProjectAs(), UnloadProject(), and ~SETTINGS_MANAGER().

◆ m_projects_list

std::vector<std::unique_ptr<PROJECT> > SETTINGS_MANAGER::m_projects_list
private

Loaded projects (ownership here)

Definition at line 443 of file settings_manager.h.

Referenced by LoadProject(), Prj(), and UnloadProject().

◆ m_settings

std::vector<std::unique_ptr<JSON_SETTINGS> > SETTINGS_MANAGER::m_settings
private

The documentation for this class was generated from the following files: