From 4c144067882a1f69e2443b1cfb2c497cc4306257 Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Wed, 27 Jan 2021 21:06:13 -0500 Subject: [PATCH] Further path refinements --- common/paths.cpp | 100 ++++++++++++++++++++++----- common/settings/settings_manager.cpp | 3 + include/paths.h | 33 +++++++++ 3 files changed, 119 insertions(+), 17 deletions(-) diff --git a/common/paths.cpp b/common/paths.cpp index d44ddd414a..bc892b0f73 100644 --- a/common/paths.cpp +++ b/common/paths.cpp @@ -34,16 +34,23 @@ #define KICAD_PATH_STR "kicad" #endif + +void PATHS::getUserDocumentPath( wxFileName& aPath ) +{ + aPath.AssignDir( KIPLATFORM::ENV::GetDocumentsPath() ); + aPath.AppendDir( KICAD_PATH_STR ); + aPath.AppendDir( SETTINGS_MANAGER::GetSettingsVersion() ); +} + + wxString PATHS::GetUserPluginsPath() { wxFileName tmp; + getUserDocumentPath( tmp ); - tmp.AssignDir( KIPLATFORM::ENV::GetDocumentsPath() ); - tmp.AppendDir( KICAD_PATH_STR ); - tmp.AppendDir( SETTINGS_MANAGER::GetSettingsVersion() ); tmp.AppendDir( "plugins" ); - return tmp.GetFullPath(); + return tmp.GetPath(); } @@ -54,46 +61,73 @@ wxString PATHS::GetUserPlugins3DPath() tmp.AssignDir( PATHS::GetUserPluginsPath() ); tmp.AppendDir( "3d" ); - return tmp.GetFullPath(); + return tmp.GetPath(); } wxString PATHS::GetUserScriptingPath() { wxFileName tmp; + getUserDocumentPath( tmp ); - tmp.AssignDir( KIPLATFORM::ENV::GetDocumentsPath() ); - tmp.AppendDir( KICAD_PATH_STR ); - tmp.AppendDir( SETTINGS_MANAGER::GetSettingsVersion() ); tmp.AppendDir( "scripting" ); - return tmp.GetFullPath(); + return tmp.GetPath(); } wxString PATHS::GetUserTemplatesPath() { wxFileName tmp; + getUserDocumentPath( tmp ); - tmp.AssignDir( KIPLATFORM::ENV::GetDocumentsPath() ); - tmp.AppendDir( KICAD_PATH_STR ); - tmp.AppendDir( SETTINGS_MANAGER::GetSettingsVersion() ); tmp.AppendDir( "template" ); - return tmp.GetFullPath(); + return tmp.GetPath(); +} + + +wxString PATHS::GetDefaultUserSymbolsPath() +{ + wxFileName tmp; + getUserDocumentPath( tmp ); + + tmp.AppendDir( "symbols" ); + + return tmp.GetPath(); +} + + +wxString PATHS::GetDefaultUserFootprintsPath() +{ + wxFileName tmp; + getUserDocumentPath( tmp ); + + tmp.AppendDir( "footprints" ); + + return tmp.GetPath(); +} + + +wxString PATHS::GetDefaultUser3DModelsPath() +{ + wxFileName tmp; + getUserDocumentPath( tmp ); + + tmp.AppendDir( "3dmodels" ); + + return tmp.GetPath(); } wxString PATHS::GetDefaultUserProjectsPath() { wxFileName tmp; + getUserDocumentPath( tmp ); - tmp.AssignDir( KIPLATFORM::ENV::GetDocumentsPath() ); - tmp.AppendDir( KICAD_PATH_STR ); - tmp.AppendDir( SETTINGS_MANAGER::GetSettingsVersion() ); tmp.AppendDir( "projects" ); - return tmp.GetFullPath(); + return tmp.GetPath(); } @@ -165,4 +199,36 @@ wxString PATHS::GetUserCachePath() tmp.AppendDir( SETTINGS_MANAGER::GetSettingsVersion() ); return tmp.GetPathWithSep(); +} + + +bool PATHS::EnsurePathExists( const wxString& aPath ) +{ + wxFileName path( aPath ); + if( !path.Normalize() ) + { + return false; + } + + if( !wxFileName::DirExists( aPath ) ) + { + if( !wxFileName::Mkdir( aPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) ) + { + return false; + } + } + + return true; +} + + +void PATHS::EnsureUserPathsExist() +{ + EnsurePathExists( GetUserPluginsPath() ); + EnsurePathExists( GetUserPlugins3DPath() ); + EnsurePathExists( GetUserScriptingPath() ); + EnsurePathExists( GetDefaultUserProjectsPath() ); + EnsurePathExists( GetDefaultUserSymbolsPath() ); + EnsurePathExists( GetDefaultUserFootprintsPath() ); + EnsurePathExists( GetDefaultUser3DModelsPath() ); } \ No newline at end of file diff --git a/common/settings/settings_manager.cpp b/common/settings/settings_manager.cpp index 594abfe881..279d5198c9 100644 --- a/common/settings/settings_manager.cpp +++ b/common/settings/settings_manager.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -54,6 +55,8 @@ SETTINGS_MANAGER::SETTINGS_MANAGER( bool aHeadless ) : m_migration_source(), m_migrateLibraryTables( true ) { + PATHS::EnsureUserPathsExist(); + // Check if the settings directory already exists, and if not, perform a migration if possible if( !MigrateIfNeeded() ) { diff --git a/include/paths.h b/include/paths.h index e01cb78045..edf27cebd9 100644 --- a/include/paths.h +++ b/include/paths.h @@ -26,6 +26,7 @@ class PATHS { public: + /** * Gets the user path for python scripts */ @@ -51,6 +52,21 @@ public: */ static wxString GetDefaultUserProjectsPath(); + /** + * Gets the default path we point users to create projects + */ + static wxString GetDefaultUserSymbolsPath(); + + /** + * Gets the default path we point users to create projects + */ + static wxString GetDefaultUserFootprintsPath(); + + /** + * Gets the default path we point users to create projects + */ + static wxString GetDefaultUser3DModelsPath(); + /** * Gets the stock (install) scripting path */ @@ -71,9 +87,26 @@ public: */ static wxString GetUserCachePath(); + /** + * Attempts to create a given path if it does not exist + */ + static bool EnsurePathExists( const wxString& aPath ); + + /** + * Ensures/creates user default paths + */ + static void EnsureUserPathsExist(); + private: // we are a static helper PATHS() {} + + /** + * Gets the user path for the current kicad version which acts as the root for other user paths + * + * @param aPath Variable to receive the path + */ + static void getUserDocumentPath( wxFileName& aPath ); }; #endif \ No newline at end of file