26#include <wx/filename.h>
27#include <wx/stdpaths.h>
66 if( !
path.DirExists() )
68 wxLogTrace(
traceSettings, wxT(
"Path didn't exist; creating it" ) );
69 path.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
77 wxLogTrace(
traceSettings, wxT(
"Note: no valid settings directory on disk" ) );
101 for( std::unique_ptr<JSON_SETTINGS>& settings :
m_settings )
112 for( std::unique_ptr<JSON_SETTINGS>& settings :
m_settings )
116 std::map<std::string, nlohmann::json> fileHistories = settings->GetFileHistories();
118 settings->Internals()->clear();
121 for(
const auto& [
path, history] : fileHistories )
122 settings->Set(
path, history );
132 for( std::unique_ptr<JSON_SETTINGS>& settings :
m_settings )
136 for(
const auto& [
path, history] : settings->GetFileHistories() )
137 settings->Set(
path, nlohmann::json::array() );
147 std::unique_ptr<JSON_SETTINGS> ptr( aSettings );
149 ptr->SetManager(
this );
151 wxLogTrace(
traceSettings, wxT(
"Registered new settings object <%s>" ),
152 ptr->GetFullFilename() );
186 std::vector<JSON_SETTINGS*> toLoad;
190 []( std::unique_ptr<JSON_SETTINGS>& aSettings )
192 return aSettings.get();
203 [&aSettings](
const std::unique_ptr<JSON_SETTINGS>& aPtr )
205 return aPtr.get() == aSettings;
228 [&aSettings](
const std::unique_ptr<JSON_SETTINGS>& aPtr )
230 return aPtr.get() == aSettings;
235 wxLogTrace(
traceSettings, wxT(
"Saving %s" ), ( *it )->GetFullFilename() );
244 [&aSettings](
const std::unique_ptr<JSON_SETTINGS>& aPtr )
246 return aPtr.get() == aSettings;
251 wxLogTrace(
traceSettings, wxT(
"Flush and release %s" ), ( *it )->GetFullFilename() );
257 size_t typeHash =
typeid( *tmp ).hash_code();
279 [&aName](
const std::pair<wxString, COLOR_SETTINGS*>& p )
281 return p.second->GetName().Lower() == aName.Lower();
312 std::vector<COLOR_SETTINGS*> ret;
314 for(
const std::pair<const wxString, COLOR_SETTINGS*>& entry :
m_color_settings )
315 ret.push_back( entry.second );
318 { return a->GetName() < b->GetName(); } );
326 wxLogTrace(
traceSettings, wxT(
"Attempting to load color theme %s" ), aName );
330 if( !fn.IsOk() || !fn.Exists() )
332 wxLogTrace(
traceSettings, wxT(
"Theme file %s.json not found, falling back to user" ),
339 if( settings->
GetFilename() != aName.ToStdString() )
341 wxLogTrace(
traceSettings, wxT(
"Warning: stored filename is actually %s, " ),
362 wxDirTraverseResult
OnFile(
const wxString& aFilePath )
override
364 wxFileName file( aFilePath );
366 if( file.GetExt() == wxS(
"json" ) )
369 return wxDIR_CONTINUE;
372 wxDirTraverseResult
OnDir(
const wxString& dirPath )
override
374 return wxDIR_CONTINUE;
394 if( aName.EndsWith( wxT(
".json" ) ) )
406 settings->
SetName( wxT(
"User" ) );
426 wxFileName third_party_path;
430 if( it != env.end() && !it->second.GetValue().IsEmpty() )
431 third_party_path.SetPath( it->second.GetValue() );
435 third_party_path.AppendDir( wxS(
"colors" ) );
438 wxDir third_party_colors_dir( third_party_path.GetFullPath() );
453 [&](
const wxFileName& aFilename )
459 if( system_colors_dir.IsOpened() )
460 system_colors_dir.Traverse( readOnlyLoader );
462 if( third_party_colors_dir.IsOpened() )
463 third_party_colors_dir.Traverse( readOnlyLoader );
465 if( colors_dir.IsOpened() )
466 colors_dir.Traverse( loader );
472 std::set<wxString> builtinNames;
481 for(
const std::pair<const wxString, COLOR_SETTINGS*>& entry :
m_color_settings )
487 && builtinNames.count( settings->
GetName() ) )
490 settings->
SetName( wxString::Format( wxS(
"%s (%s)" ), settings->
GetName(),
491 wxFileName( settings->
GetFilename() ).GetName() ) );
508 [aSettings] (
const std::pair<wxString, COLOR_SETTINGS*>& el )
510 return el.second->GetFilename() == aSettings->GetFilename();
517 if( !aSettings->
Store() )
519 wxLogTrace(
traceSettings, wxT(
"Color scheme %s not modified; skipping save" ),
524 wxASSERT( aSettings->
Contains( aNamespace ) );
526 wxLogTrace(
traceSettings, wxT(
"Saving color scheme %s, preserving %s" ),
530 std::optional<nlohmann::json> backup = aSettings->
GetJson( aNamespace );
536 ( *aSettings->
Internals() )[aNamespace].update( *backup );
546 wxASSERT( aSettings );
556 return owner->GetProjectPath();
571 wxASSERT_MSG(
false, wxT(
"Unknown settings location!" ) );
596 wxDirTraverseResult
OnFile(
const wxString& aSrcFilePath )
override
598 wxFileName file( aSrcFilePath );
603 return wxDIR_CONTINUE;
607 if( file.GetFullName() == wxT(
"installed_packages.json" ) )
608 return wxDIR_CONTINUE;
612 if( file.GetExt() == wxT(
"hotkeys" ) )
613 return wxDIR_CONTINUE;
615 wxString
path = file.GetPath();
618 file.SetPath(
path );
620 wxLogTrace(
traceSettings, wxT(
"Copying %s to %s" ), aSrcFilePath, file.GetFullPath() );
625 return wxDIR_CONTINUE;
628 wxDirTraverseResult
OnDir(
const wxString& dirPath )
override
630 wxFileName dir( dirPath );
633 if( dir.GetName() == wxS(
"colors" ) ||
634 dir.GetName() == wxS(
"3d" ) )
637 wxString
path = dir.GetPath();
642 if( !wxDirExists( dir.GetPath() ) )
643 wxMkdir( dir.GetPath() );
645 return wxDIR_CONTINUE;
659 if(
path.DirExists() )
661 wxFileName common =
path;
662 common.SetName( wxS(
"kicad_common" ) );
663 common.SetExt( wxS(
"json" ) );
665 if( common.Exists() )
667 wxLogTrace(
traceSettings, wxT(
"Path exists and has a kicad_common, continuing!" ) );
680 if( aSourcePath.IsEmpty() )
683 wxLogTrace(
traceSettings, wxT(
"Migrating from path %s" ), aSourcePath );
687 wxDir source_dir( aSourcePath );
689 source_dir.Traverse( traverser );
701 const std::vector<wxString> libKeys = {
702 wxT(
"KICAD6_SYMBOL_DIR" ),
703 wxT(
"KICAD6_3DMODEL_DIR" ),
704 wxT(
"KICAD6_FOOTPRINT_DIR" ),
705 wxT(
"KICAD6_TEMPLATE_DIR" ),
706 wxT(
"KICAD7_SYMBOL_DIR" ),
707 wxT(
"KICAD7_3DMODEL_DIR" ),
708 wxT(
"KICAD7_FOOTPRINT_DIR" ),
709 wxT(
"KICAD7_TEMPLATE_DIR" ),
710 wxT(
"KICAD8_SYMBOL_DIR" ),
711 wxT(
"KICAD8_3DMODEL_DIR" ),
712 wxT(
"KICAD8_FOOTPRINT_DIR" ),
713 wxT(
"KICAD8_TEMPLATE_DIR" ),
716 wxT(
"KICAD_PTEMPLATES" ),
719 wxT(
"KICAD_SYMBOL_DIR" ),
722 for(
const wxString& key : libKeys )
739 std::vector<wxFileName> base_paths;
744 if( wxGetEnv( wxT(
"KICAD_CONFIG_HOME" ),
nullptr ) )
755 wxFileName wxGtkPath;
756 wxGtkPath.AssignDir( wxS(
"~/.config/kicad" ) );
757 wxGtkPath.MakeAbsolute();
758 base_paths.emplace_back( wxGtkPath );
761 wxGtkPath.AssignDir( wxS(
"~/.var/app/org.kicad.KiCad/config/kicad" ) );
762 wxGtkPath.MakeAbsolute();
763 base_paths.emplace_back( wxGtkPath );
771 [&] (
const wxString& aSubDir )
776 wxString sub_path = dir.GetNameWithSep() + aSubDir;
780 aPaths->push_back( sub_path );
781 wxLogTrace(
traceSettings, wxT(
"GetPreviousVersionName: %s is valid" ), sub_path );
786 std::set<wxString> checkedPaths;
788 for(
const wxFileName& base_path : base_paths )
790 if( checkedPaths.count( base_path.GetFullPath() ) )
793 checkedPaths.insert( base_path.GetFullPath() );
795 if( !dir.Open( base_path.GetFullPath() ) )
797 wxLogTrace(
traceSettings, wxT(
"GetPreviousVersionName: could not open base path %s" ),
798 base_path.GetFullPath() );
802 wxLogTrace(
traceSettings, wxT(
"GetPreviousVersionName: checking base path %s" ),
803 base_path.GetFullPath() );
805 if( dir.GetFirst( &subdir, wxEmptyString, wxDIR_DIRS ) )
810 while( dir.GetNext( &subdir ) )
821 wxT(
"GetPreviousVersionName: root path %s is valid" ), dir.GetName() );
822 aPaths->push_back( dir.GetName() );
826 std::erase_if( *aPaths,
827 [](
const wxString& aPath ) ->
bool
829 wxFileName fulldir = wxFileName::DirName( aPath );
830 const wxArrayString& dirs = fulldir.GetDirs();
832 if( dirs.empty() || !fulldir.IsDirReadable() )
835 std::string ver = dirs.back().ToStdString();
843 std::sort( aPaths->begin(), aPaths->end(),
844 [&](
const wxString& a,
const wxString& b ) ->
bool
846 wxFileName aPath = wxFileName::DirName( a );
847 wxFileName bPath = wxFileName::DirName( b );
849 const wxArrayString& aDirs = aPath.GetDirs();
850 const wxArrayString& bDirs = bPath.GetDirs();
858 std::string verA = aDirs.back().ToStdString();
859 std::string verB = bDirs.back().ToStdString();
861 if( !extractVersion( verA ) )
864 if( !extractVersion( verB ) )
867 return compareVersions( verA, verB ) > 0;
870 return aPaths->size() > 0;
876 wxFileName
test( aPath, wxS(
"kicad_common" ) );
881 test.SetExt(
"json" );
883 return test.Exists();
892 path.AppendDir( wxS(
"colors" ) );
894 if( !
path.DirExists() )
896 if( !wxMkdir(
path.GetPath() ) )
899 wxT(
"GetColorSettingsPath(): Path %s missing and could not be created!" ),
904 return path.GetPath();
913 path.AppendDir( wxS(
"toolbars" ) );
915 if( !
path.DirExists() )
917 if( !wxMkdir(
path.GetPath() ) )
920 wxT(
"GetToolbarSettingsPath(): Path %s missing and could not be created!" ),
925 return path.GetPath();
945 wxLogTrace(
traceSettings, wxT(
"compareSettingsVersions: bad input (%s, %s)" ),
954 else if( a_maj > b_maj )
964 else if( a_min > b_min )
978 std::regex re_version(
"(\\d+)\\.(\\d+)" );
981 if( std::regex_match( aVersionString, match, re_version ) )
985 int major = std::stoi( match[1].str() );
986 int minor = std::stoi( match[2].str() );
1011 wxFileName
path( aFullPath );
1016 return path.GetFullPath();
1023 wxFileName
path( fullPath );
1034 if( !lockFile.
Valid() && aSetActive && aOverrideLock )
1037 if( !lockFile.
Valid() )
1038 wxLogTrace(
traceSettings, wxT(
"Project %s is locked; opening read-only" ), fullPath );
1052 [&](
const std::pair<const wxString, PROJECT*>& aEntry )
1054 return aEntry.second == oldProject;
1060 wxLogTrace(
traceSettings, wxT(
"Load project %s" ), fullPath );
1062 std::unique_ptr<PROJECT>
project = std::make_unique<PROJECT>();
1063 project->setProjectFullName( fullPath );
1069 wxFileName projectPath( fullPath );
1073 if( wxTheApp && wxTheApp->IsGUI() )
1077 if( !projectPath.GetPath().IsEmpty() )
1078 wxSetWorkingDirectory( projectPath.GetPath() );
1079 else if( switching )
1090 if( projectPath.GetPath().IsEmpty() )
1096 wxFileName vcsContext( projectPath );
1097 vcsContext.MakeAbsolute();
1106 if( lockFile.
Valid() && aSetActive )
1117 wxString fn(
path.GetName() );
1126 m_projects[fullPath]->setLocalSettings( settings );
1150 [&](
const std::unique_ptr<PROJECT>& aPtr )
1152 return aPtr.get() == aProject;
1166 schFace->CancelPreload(
true );
1169 pcbFace->CancelPreload(
true );
1191 wxLogTrace(
traceSettings, wxT(
"Unload project %s" ), projectPath );
1196 if( wasActiveProject )
1209 [&](
const std::unique_ptr<PROJECT>& ptr )
1211 return ptr.get() == toRemove;
1219 if( wasActiveProject )
1236 if( wxTheApp && wxTheApp->IsGUI() )
1252 wxLogTrace(
traceSettings, wxT(
"Prj() called with no project loaded" ) );
1254 static PROJECT s_emptyProject;
1255 return s_emptyProject;
1265 project->SetProjectLock(
nullptr );
1278 && !
m_projects.begin()->second->GetProjectFullName().IsEmpty() );
1293 TEMPLATES::SCOPE::GLOBAL ) )
1296 fieldName, TEMPLATES::SCOPE::GLOBAL );
1306 return it !=
m_projects.end() ? it->second :
nullptr;
1312 std::vector<wxString> ret;
1314 for(
const std::pair<const wxString, PROJECT*>& pair :
m_projects )
1317 if( !pair.first.IsEmpty() )
1318 ret.emplace_back( pair.first );
1330 wxString
path = aFullPath;
1349 project->SaveToFile( projectPath );
1351 return !
project->LastSaveFailed();
1363 if( newName.IsSameAs( oldName ) )
1372 wxFileName fn( newName );
1381 project->SetFilename( fn.GetName() );
1382 project->SaveToFile( fn.GetPath() );
1401 wxString oldName =
project->GetFilename();
1402 wxFileName fn( aFullPath );
1404 bool readOnly =
project->IsReadOnly();
1405 project->SetReadOnly(
false );
1407 project->SetFilename( fn.GetName() );
1408 const bool projectOk =
project->SaveToFile( fn.GetPath() );
1409 project->SetFilename( oldName );
1420 project->SetReadOnly( readOnly );
1429 wxString fn( fullFn.GetName() );
1438 wxString
path( fullFn.GetPath() );
1464 [&file](
const std::unique_ptr<JSON_SETTINGS>& aPtr )
1466 return aPtr.get() == file;
1478 ( *it )->SaveToFile( projectPath );
1481 if( ( *it )->LastSaveFailed() )
1502 return wxEmptyString;
1506 if( fullName.IsEmpty() )
1507 return wxEmptyString;
1509 std::string hashHex;
1510 picosha2::hash256_hex_string( fullName.ToStdString( wxConvUTF8 ), hashHex );
1512 return wxString::Format( wxS(
"%s-%s" ), aProject->
GetProjectName(),
1513 wxString::FromUTF8( hashHex.substr( 0, 12 ).c_str() ) );
1519 return aProject ? *aProject :
Prj();
1530 if( activePath.IsSameAs( aProjectPath ) || activePath.IsSameAs( aProjectPath + wxFILE_SEP_PATH ) )
1552 root.AppendDir( wxS(
"backups" ) );
1556 if( !key.IsEmpty() )
1557 root.AppendDir( key );
1559 return root.GetPathWithSep();
1570 wxFileName p(
project.GetProjectPath(), wxEmptyString );
1571 p.AppendDir( wxS(
".history" ) );
1576 root.AppendDir( wxS(
"local_history" ) );
1580 if( !key.IsEmpty() )
1581 root.AppendDir( key );
1583 return root.GetPath();
1591 wxFileName p( aProjectPath, wxEmptyString );
1592 p.AppendDir( wxS(
".history" ) );
1606 return project.GetProjectPath();
1609 root.AppendDir( wxS(
"autosave" ) );
1613 if( !key.IsEmpty() )
1614 root.AppendDir( key );
1616 return root.GetPathWithSep();
1625 wxDateTime timestamp = wxDateTime::Now();
1627 wxString fileName = wxString::Format( wxT(
"%s-%s" ),
Prj().GetProjectName(),
1630 if( !aTarget.IsOk() )
1633 aTarget.SetName( fileName );
1639 wxLogTrace(
traceSettings, wxT(
"Could not create project backup path %s" ),
1640 aTarget.GetPath() );
1644 if( !aTarget.IsDirWritable() )
1646 wxLogTrace(
traceSettings, wxT(
"Backup directory %s is not writable" ),
1647 aTarget.GetPath() );
1651 wxLogTrace(
traceSettings, wxT(
"Backing up project to %s" ), aTarget.GetPath() );
1661 std::function<
bool(
const wxString& )> aCond ) :
1667 wxDirTraverseResult
OnFile(
const wxString& aFile )
override
1670 m_files.emplace_back( aFile );
1672 return wxDIR_CONTINUE;
1675 wxDirTraverseResult
OnDir(
const wxString& aDirName )
override
1677 return wxDIR_CONTINUE;
1703 [&prefix](
const wxString& aFile )
1706 wxString fn( wxFileName( aFile ).GetName() );
1707 fn.Replace( prefix, wxS(
"" ) );
1712 if(
Prj().GetProjectFullName().IsEmpty() )
1721 wxLogTrace(
traceSettings, wxT(
"Could not create backups path %s! Skipping backup" ),
1726 wxFileName backupRoot( backupPath, wxEmptyString, wxEmptyString );
1730 if( !backupRoot.IsDirWritable() )
1732 wxLogTrace(
traceSettings, wxT(
"Backup directory %s is not writable! Skipping backup" ),
1737 wxDir dir( backupPath );
1739 if( !dir.IsOpened() )
1741 wxLogTrace(
traceSettings, wxT(
"Could not open project backups path %s" ), dir.GetName() );
1745 std::vector<wxString> files;
1748 [&modTime](
const wxString& aFile )
1750 return modTime( aFile ).IsValid();
1753 dir.Traverse( traverser, wxT(
"*.zip" ) );
1756 std::sort( files.begin(), files.end(),
1757 [&](
const wxString& aFirst,
const wxString& aSecond ) ->
bool
1759 wxDateTime first = modTime( aFirst );
1760 wxDateTime second = modTime( aSecond );
1762 return first.GetTicks() > second.GetTicks();
1766 if( !files.empty() )
1768 wxDateTime lastTime = modTime( files[0] );
1770 if( lastTime.IsValid() )
1772 wxTimeSpan
delta = wxDateTime::Now() - modTime( files[0] );
1783 if( !backupSuccessful )
1787 files.insert( files.begin(), target.GetFullPath() );
1790 if( files.size() >= 2
1793 wxRemoveFile( files[0] );
1804 wxRemoveFile( files.back() );
1814 wxULongLong totalSize = 0;
1816 for(
const wxString& file : files )
1817 totalSize += wxFileName::GetSize( file );
1819 while( files.size() > 1 && totalSize > limit )
1821 totalSize -= wxFileName::GetSize( files.back() );
1822 wxRemoveFile( files.back() );
1826 if( totalSize > limit )
1828 aReporter.
Report(
_(
"One backup of this project is larger than the total backup size "
1829 "limit. KiCad kept the new backup; increase the limit in "
1838 wxDateTime day = modTime( files[0] );
1841 wxASSERT( day.IsValid() );
1843 std::vector<wxString> filesToDelete;
1845 for(
size_t i = 1; i < files.size(); i++ )
1847 wxDateTime dt = modTime( files[i] );
1849 if( dt.IsSameDate( day ) )
1854 filesToDelete.emplace_back( files[i] );
1863 for(
const wxString& file : filesToDelete )
1864 wxRemoveFile( file );
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
Color settings are a bit different than most of the settings objects in that there can be more than o...
void SetName(const wxString &aName)
static std::vector< COLOR_SETTINGS * > CreateBuiltinColorSettings()
Constructs and returns a list of color settings objects based on the built-in color themes.
static const wxString COLOR_BUILTIN_CLASSIC
static const wxString COLOR_BUILTIN_DEFAULT
const wxString & GetName() const
std::function< void(const wxFileName &)> m_action
JSON_DIR_TRAVERSER(std::function< void(const wxFileName &)> aAction)
wxDirTraverseResult OnDir(const wxString &dirPath) override
wxDirTraverseResult OnFile(const wxString &aFilePath) override
std::optional< 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....
bool Contains(const std::string &aPath) const
bool IsFileSynced() const
SETTINGS_LOC GetLocation() const
virtual bool LoadFromFile(const wxString &aDirectory="")
Loads the backing file from disk and then calls Load()
virtual const PROJECT * GetOwningProject() const
Project-located settings override this to report the project they belong to so their save path is res...
virtual void Load()
Updates the parameters of this object based on the current JSON document contents.
void SetReadOnly(bool aReadOnly)
JSON_SETTINGS_INTERNALS * Internals()
void SetFilename(const wxString &aFilename)
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
Calls Store() and then writes the contents of the JSON document to a file.
virtual bool Store()
Stores the current parameters into the JSON document represented by this object Note: this doesn't do...
wxString GetFilename() const
void RestartAsyncLoads()
Queues again the libraries that any cancelling abort left INVALID, for every registered adapter.
void AbortAsyncLoads(bool aCancelPlugins=false)
Abort any async library loading operations in progress.
void ProjectChanged()
Notify all adapters that the project has changed.
Advisory lock over a file, taken by writing a sibling lock file and holding an exclusive lock on it f...
bool OverrideLock(bool aRemoveOnRelease=true)
Force the lock, overwriting the data that existed already.
static LOCKFILE Inspect(const wxString &aFilename)
Look at a lock without taking it: nothing is created, nothing is claimed and nothing is removed on re...
wxDirTraverseResult OnDir(const wxString &dirPath) override
MIGRATION_TRAVERSER(const wxString &aSrcDir, const wxString &aDestDir, bool aMigrateTables)
wxDirTraverseResult OnFile(const wxString &aSrcFilePath) override
static wxString CalculateUserSettingsPath(bool aIncludeVer=true, bool aUseEnv=true)
Determines the base path for user settings files.
static wxString GetDefault3rdPartyPath()
Gets the default path for PCM packages.
static bool EnsurePathExists(const wxString &aPath, bool aPathToFile=false)
Attempts to create a given path if it does not exist.
static wxString GetStockDataPath(bool aRespectRunFromBuildDir=true)
Gets the stock (install) data path, which is the base path for things like scripting,...
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
virtual ENV_VAR_MAP & GetLocalEnvVariables() const
void WaitForDesignBlockPreload()
Waits for the design block preload to end, so it no longer reaches the project, and clears the cancel...
void CancelDesignBlockPreload()
Asks the design block preload to stop, without waiting for it.
virtual LIBRARY_MANAGER & GetLibraryManager() const
static bool Archive(const wxString &aSrcDir, const wxString &aDestFile, REPORTER &aReporter, bool aVerbose=true, bool aIncludeExtraFiles=false)
Create an archive of the project.
static bool AreZipArchivesIdentical(const wxString &aZipFileA, const wxString &aZipFileB, REPORTER &aReporter)
Compare the CRCs of all the files in zip archive to determine whether the archives are identical.
The backing store for a PROJECT, in JSON format.
TEMPLATES m_TemplateFieldNames
Project and global field name templates shared by project editors.
bool ShouldAutoSave() const
void SetProject(PROJECT *aProject)
bool LoadFromFile(const wxString &aDirectory="") override
Loads the backing file from disk and then calls Load()
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
Calls Store() and then writes the contents of the JSON document to a file.
bool ShouldAutoSave() const
Container for project specific data.
virtual void setProjectFile(PROJECT_FILE *aFile)
Set the backing store file for this project.
virtual bool IsReadOnly() const
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
virtual const wxString GetProjectPath() const
Return the full path of the project.
virtual const wxString GetProjectName() const
Return the short name of the project.
virtual PROJECT_LOCAL_SETTINGS & GetLocalSettings() const
virtual void setProjectFullName(const wxString &aFullPathAndName)
Set the full directory, basename, and extension of the project.
A pure virtual class used to derive REPORTER objects from.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
static int compareVersions(const std::string &aFirst, const std::string &aSecond)
Compare two settings versions, like "5.99" and "6.0".
wxString GetBackupRootForProject(const PROJECT *aProject=nullptr) const
Resolve the backup root directory for a project, honoring the active BACKUP_LOCATION preference.
void stopLibraryLoads()
Cancels and joins every library preload and async load, so none can touch Prj() during a project chan...
wxString GetPathForSettingsFile(JSON_SETTINGS *aSettings)
Return the path a given settings file should be loaded from / stored to.
static std::string GetSettingsVersion()
Parse the current KiCad build version and extracts the major and minor revision to use as the name of...
void SaveProjectAs(const wxString &aFullPath, PROJECT *aProject=nullptr)
Set the currently loaded project path and saves it (pointers remain valid).
JSON_SETTINGS * registerSettings(JSON_SETTINGS *aSettings, bool aLoadNow=true)
static wxString GetUserSettingsPath()
A proxy for PATHS::GetUserSettingsPath() rather than fighting swig.
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Take ownership of the pointer passed in.
void ReleaseProjectLocks()
Give up the lock of every loaded project without unloading it.
void SaveColorSettings(COLOR_SETTINGS *aSettings, const std::string &aNamespace="")
Safely save a COLOR_SETTINGS to disk, preserving any changes outside the given namespace.
void SyncGlobalFieldNameTemplatesToProjects()
Synchronize the global field name templates into every loaded project.
bool MigrateFromPreviousVersion(const wxString &aSourcePath)
Handle migration of the settings from previous KiCad versions.
std::map< wxString, PROJECT * > m_projects
Loaded projects, mapped according to project full name.
static bool extractVersion(const std::string &aVersionString, int *aMajor=nullptr, int *aMinor=nullptr)
Extract the numeric version from a given settings string.
COLOR_SETTINGS * registerColorSettings(const wxString &aFilename, bool aAbsolutePath=false)
COLOR_SETTINGS * GetColorSettings(const wxString &aName)
Retrieve a color settings object that applications can read colors from.
static wxString GetColorSettingsPath()
Return the path where color scheme files are stored; creating it if missing (normally .
COMMON_SETTINGS * GetCommonSettings() const
Retrieve the common settings shared by all applications.
bool SaveProject(const wxString &aFullPath=wxEmptyString, PROJECT *aProject=nullptr)
Save a loaded project.
bool SaveProjectCopy(const wxString &aFullPath, PROJECT *aProject=nullptr)
Save a copy of the current project under the given path.
wxString GetLocalHistoryDirForProject(const PROJECT *aProject=nullptr) const
Resolve the local-history (.history) storage directory for a project.
void ClearFileHistory()
Clear saved file history from all settings files.
wxString GetProjectBackupsPath() const
void ResetToDefaults()
Reset all program settings to defaults.
const PROJECT & resolveProject(const PROJECT *aProject) const
Pick the project to resolve a backup path against, falling back to Prj().
bool SettingsDirectoryValid() const
wxString GetAutosaveRootForProject(const PROJECT *aProject=nullptr) const
Resolve the autosave-files root for a project.
std::map< wxString, PROJECT_FILE * > m_project_files
Loaded project files, mapped according to project full name.
std::unordered_map< wxString, COLOR_SETTINGS * > m_color_settings
PROJECT * GetProjectForPath(const wxString &aProjectPath) const
Return the active project iff its path matches aProjectPath, else nullptr.
COLOR_SETTINGS * loadColorSettingsByName(const wxString &aName)
Attempt to load a color theme by name (the color theme directory and .json ext are assumed).
std::vector< COLOR_SETTINGS * > GetColorSettingsList()
bool IsProjectOpen() const
Helper for checking if we have a project open.
wxString GetLocalHistoryDirForPath(const wxString &aProjectPath) const
Resolve the local-history directory for a project given by its on-disk path.
static wxString GetToolbarSettingsPath()
Return the path where toolbar configuration files are stored; creating it if missing (normally .
bool GetPreviousVersionPaths(std::vector< wxString > *aName=nullptr)
Retrieve the name of the most recent previous KiCad version that can be found in the user settings di...
static bool IsSettingsPathValid(const wxString &aPath)
Check if a given path is probably a valid KiCad configuration directory.
bool IsProjectLoaded(PROJECT *aProject) const
True if aProject is still owned by the manager.
bool BackupProject(REPORTER &aReporter, wxFileName &aTarget) const
Create a backup archive of the current project.
std::vector< std::unique_ptr< PROJECT > > m_projects_list
Loaded projects (ownership here).
void loadAllColorSettings()
PROJECT * GetProject(const wxString &aFullPath) const
Retrieve a loaded project by name.
bool UnloadProject(PROJECT *aProject, bool aSave=true)
Save, unload and unregister the given PROJECT.
std::vector< wxString > GetOpenProjects() const
std::vector< std::unique_ptr< JSON_SETTINGS > > m_settings
bool TriggerBackupIfNeeded(REPORTER &aReporter) const
Call BackupProject() if a new backup is needed according to the current backup policy.
bool m_migrateLibraryTables
If true, the symbol and footprint library tables will be migrated from the previous version.
bool unloadProjectFile(PROJECT *aProject, bool aSave)
Optionally save, unload and unregister the given PROJECT_FILE.
COLOR_SETTINGS * GetMigratedColorSettings()
Return a color theme for storing colors migrated from legacy (5.x and earlier) settings,...
std::unordered_map< size_t, JSON_SETTINGS * > m_app_settings_cache
Cache for app settings.
COLOR_SETTINGS * AddNewColorSettings(const wxString &aFilename)
Register a new color settings object with the given filename.
bool m_ok
True if settings loaded successfully at construction.
void registerBuiltinColorSettings()
Helper to create built-in colors and register them.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
bool loadProjectFile(PROJECT &aProject)
Register a PROJECT_FILE and attempt to load it from disk.
bool IsProjectOpenNotDummy() const
Helper for checking if we have a project open that is not a dummy project.
static wxString backupDateTimeFormat
void ReloadColorSettings()
Re-scan the color themes directory, reloading any changes it finds.
COMMON_SETTINGS * m_common_settings
bool LoadProject(const wxString &aFullPath, bool aSetActive=true, bool aOverrideLock=false)
Load a project or sets up a new project with a specified path.
KIWAY * m_kiway
The kiway this settings manager interacts with.
void FlushAndRelease(JSON_SETTINGS *aSettings, bool aSave=true)
If the given settings object is registered, save it to disk and unregister it.
static wxString projectKeySuffix(const PROJECT *aProject)
Build "<projectname>-<sha256prefix>" suffix used to disambiguate per-project subdirectories under the...
void AddTemplateFieldName(const TEMPLATE_FIELDNAME &aFieldName, SCOPE aScope)
Insert or append a wanted symbol field name into the field names template.
void DeleteFieldNameTemplates(SCOPE aScope)
Delete the contents of a scope.
wxDirTraverseResult OnFile(const wxString &aFile) override
wxDirTraverseResult OnDir(const wxString &aDirName) override
std::vector< wxString > & m_files
VECTOR_INSERT_TRAVERSER(std::vector< wxString > &aVec, std::function< bool(const wxString &)> aCond)
std::function< bool(const wxString &)> m_condition
@ ZIP
Zip archive snapshots; autosave uses recovery files.
@ PROJECT_DIR
Inside the project directory (default)
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
This file is part of the common library.
Functions related to environment variables, including help functions.
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
static const std::string SymbolLibraryTableFileName
static const std::string ProjectFileExtension
static const std::string FootprintLibraryTableFileName
static const std::string ArchiveFileExtension
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
@ TOOLBARS
The toolbar directory (e.g. ~/.config/kicad/toolbars/)
@ PROJECT
The settings directory inside a project folder.
@ USER
The main config directory (e.g. ~/.config/kicad/)
@ COLORS
The color scheme directory (e.g. ~/.config/kicad/colors/)
@ NONE
No directory prepended, full path in filename (used for PROJECT_FILE)
This file contains miscellaneous commonly used macros and functions.
KICOMMON_API wxString GetVersionedEnvVarName(const wxString &aBaseName)
Construct a versioned environment variable based on this KiCad major version.
void SetContextPath(const wxString &aPath)
Set the filesystem path used for repository discovery and relative file queries.
PGM_BASE & Pgm()
The global program "get" accessor.
PGM_BASE * PgmOrNull()
Return a reference that can be nullptr when running a shared lib from a script, not from a kicad app.
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
static void reloadFromFile(JSON_SETTINGS *aSettings, const wxString &aPath)
static wxString projectKey(const wxString &aFullPath)
static bool managerMayAutoSave(JSON_SETTINGS *aSettings)
#define PROJECT_BACKUPS_DIR_SUFFIX
Project settings path will be <projectname> + this.
int min_interval
Minimum time, in seconds, between subsequent backups.
unsigned long long limit_total_size
Maximum total size of backups (bytes), 0 for unlimited.
int limit_total_files
Maximum number of backup archives to retain.
BACKUP_LOCATION location
Where backups, history, and autosave files live.
int limit_daily_files
Maximum files to keep per day, 0 for unlimited.
BACKUP_FORMAT format
Backup format (incremental git history vs zip archives)
bool enabled
Automatically back up the project when files are saved.
Implement a participant in the KIWAY alchemy.
Hold a name of a symbol's field, field value, and default visibility.
Definition of file extensions used in Kicad.