From 121ea99824f07ce14cfed11f630e5bfd3f4a05e7 Mon Sep 17 00:00:00 2001
From: Marek Roszko <mark.roszko@gmail.com>
Date: Sat, 23 Sep 2023 19:53:22 -0400
Subject: [PATCH] Move the settings path to PATHS to allow shifting other
 classes into kicommon

---
 3d-viewer/3d_cache/3d_cache.cpp      |  2 +-
 common/advanced_config.cpp           |  4 +--
 common/fp_lib_table.cpp              |  3 +-
 common/hotkeys_basic.cpp             |  8 ++---
 common/paths.cpp                     | 44 +++++++++++++++++++++++--
 common/settings/common_settings.cpp  |  2 +-
 common/settings/settings_manager.cpp | 49 +++-------------------------
 common/widgets/paged_dialog.cpp      |  4 +--
 eeschema/symbol_lib_table.cpp        |  3 +-
 include/paths.h                      | 29 +++++++++++++++-
 include/settings/settings_manager.h  | 28 ----------------
 kicad/pcm/pcm.cpp                    |  4 +--
 12 files changed, 90 insertions(+), 90 deletions(-)

diff --git a/3d-viewer/3d_cache/3d_cache.cpp b/3d-viewer/3d_cache/3d_cache.cpp
index c54e6781e9..a2630b249d 100644
--- a/3d-viewer/3d_cache/3d_cache.cpp
+++ b/3d-viewer/3d_cache/3d_cache.cpp
@@ -736,7 +736,7 @@ S3D_CACHE* PROJECT::Get3DCacheManager( bool aUpdateProjDir )
         cache = new S3D_CACHE();
 
         wxFileName cfgpath;
-        cfgpath.AssignDir( SETTINGS_MANAGER::GetUserSettingsPath() );
+        cfgpath.AssignDir( PATHS::GetUserSettingsPath() );
         cfgpath.AppendDir( wxT( "3d" ) );
 
         cache->SetProgramBase( &Pgm() );
diff --git a/common/advanced_config.cpp b/common/advanced_config.cpp
index 082db729a2..28c2428df8 100644
--- a/common/advanced_config.cpp
+++ b/common/advanced_config.cpp
@@ -24,7 +24,7 @@
 #include <advanced_config.h>
 
 #include <config_params.h>
-#include <settings/settings_manager.h>
+#include <paths.h>
 
 #include <wx/app.h>
 #include <wx/config.h>
@@ -288,7 +288,7 @@ static void dumpCfg( const std::vector<PARAM_CFG*>& aArray )
 static wxFileName getAdvancedCfgFilename()
 {
     const static wxString cfg_filename{ wxS( "kicad_advanced" ) };
-    return wxFileName( SETTINGS_MANAGER::GetUserSettingsPath(), cfg_filename );
+    return wxFileName( PATHS::GetUserSettingsPath(), cfg_filename );
 }
 
 
diff --git a/common/fp_lib_table.cpp b/common/fp_lib_table.cpp
index db0333e278..530dad9e46 100644
--- a/common/fp_lib_table.cpp
+++ b/common/fp_lib_table.cpp
@@ -28,6 +28,7 @@
 #include <footprint_info.h>
 #include <lib_id.h>
 #include <lib_table_lexer.h>
+#include <paths.h>
 #include <pgm_base.h>
 #include <search_stack.h>
 #include <settings/kicad_settings.h>
@@ -654,7 +655,7 @@ wxString FP_LIB_TABLE::GetGlobalTableFileName()
 {
     wxFileName fn;
 
-    fn.SetPath( SETTINGS_MANAGER::GetUserSettingsPath() );
+    fn.SetPath( PATHS::GetUserSettingsPath() );
     fn.SetName( global_tbl_name );
 
     return fn.GetFullPath();
diff --git a/common/hotkeys_basic.cpp b/common/hotkeys_basic.cpp
index 07b0b187f5..4b66029279 100644
--- a/common/hotkeys_basic.cpp
+++ b/common/hotkeys_basic.cpp
@@ -30,7 +30,7 @@
 #include <eda_base_frame.h>
 #include <eda_draw_frame.h>
 #include <wildcards_and_files_ext.h>
-#include <settings/settings_manager.h>
+#include <paths.h>
 
 #include <tool/tool_manager.h>
 #include "dialogs/dialog_hotkey_list.h"
@@ -357,7 +357,7 @@ void ReadHotKeyConfig( const wxString&                             aFileName,
     {
         wxFileName fn( wxS( "user" ) );
         fn.SetExt( HotkeyFileExtension );
-        fn.SetPath( SETTINGS_MANAGER::GetUserSettingsPath() );
+        fn.SetPath( PATHS::GetUserSettingsPath() );
         fileName = fn.GetFullPath();
     }
 
@@ -412,7 +412,7 @@ int WriteHotKeyConfig( const std::vector<TOOL_ACTION*>& aActions )
     wxFileName fn( "user" );
 
     fn.SetExt( HotkeyFileExtension );
-    fn.SetPath( SETTINGS_MANAGER::GetUserSettingsPath() );
+    fn.SetPath( PATHS::GetUserSettingsPath() );
 
     // Read the existing config (all hotkeys)
     ReadHotKeyConfig( fn.GetFullPath(), hotkeys );
@@ -459,7 +459,7 @@ int ReadLegacyHotkeyConfigFile( const wxString& aFilename, std::map<std::string,
     wxFileName fn( aFilename );
 
     fn.SetExt( HotkeyFileExtension );
-    fn.SetPath( SETTINGS_MANAGER::GetUserSettingsPath() );
+    fn.SetPath( PATHS::GetUserSettingsPath() );
 
     if( !wxFile::Exists( fn.GetFullPath() ) )
         return 0;
diff --git a/common/paths.cpp b/common/paths.cpp
index e545b32bfd..0166e7c6f2 100644
--- a/common/paths.cpp
+++ b/common/paths.cpp
@@ -25,8 +25,9 @@
 #include <kiplatform/environment.h>
 #include <paths.h>
 #include <pgm_base.h>
-#include <settings/settings_manager.h>
 #include <config.h>
+#include <build_version.h>
+#include <macros.h>
 
 // lowercase or pretty case depending on platform
 #if defined( __WXMAC__ ) || defined( __WXMSW__ )
@@ -46,7 +47,7 @@ void PATHS::getUserDocumentPath( wxFileName& aPath )
         aPath.AssignDir( KIPLATFORM::ENV::GetDocumentsPath() );
 
     aPath.AppendDir( KICAD_PATH_STR );
-    aPath.AppendDir( SETTINGS_MANAGER::GetSettingsVersion() );
+    aPath.AppendDir( GetMajorMinorVersion().ToStdString() );
 }
 
 
@@ -333,7 +334,7 @@ wxString PATHS::GetUserCachePath()
     }
 
     tmp.AppendDir( KICAD_PATH_STR );
-    tmp.AppendDir( SETTINGS_MANAGER::GetSettingsVersion() );
+    tmp.AppendDir( GetMajorMinorVersion().ToStdString() );
 
     return tmp.GetPathWithSep();
 }
@@ -462,3 +463,40 @@ wxString PATHS::getWindowsKiCadRoot()
     return root.GetPathWithSep();
 }
 #endif
+
+
+wxString PATHS::GetUserSettingsPath()
+{
+    static wxString user_settings_path;
+
+    if( user_settings_path.empty() )
+        user_settings_path = CalculateUserSettingsPath();
+
+    return user_settings_path;
+}
+
+
+wxString PATHS::CalculateUserSettingsPath( bool aIncludeVer, bool aUseEnv )
+{
+    wxFileName cfgpath;
+
+    // http://docs.wxwidgets.org/3.0/classwx_standard_paths.html#a7c7cf595d94d29147360d031647476b0
+
+    wxString envstr;
+    if( aUseEnv && wxGetEnv( wxT( "KICAD_CONFIG_HOME" ), &envstr ) && !envstr.IsEmpty() )
+    {
+        // Override the assignment above with KICAD_CONFIG_HOME
+        cfgpath.AssignDir( envstr );
+    }
+    else
+    {
+        cfgpath.AssignDir( KIPLATFORM::ENV::GetUserConfigPath() );
+
+        cfgpath.AppendDir( TO_STR( KICAD_CONFIG_DIR ) );
+    }
+
+    if( aIncludeVer )
+        cfgpath.AppendDir( GetMajorMinorVersion().ToStdString() );
+
+    return cfgpath.GetPath();
+}
\ No newline at end of file
diff --git a/common/settings/common_settings.cpp b/common/settings/common_settings.cpp
index 7d614ef819..527dbe581a 100644
--- a/common/settings/common_settings.cpp
+++ b/common/settings/common_settings.cpp
@@ -424,7 +424,7 @@ bool COMMON_SETTINGS::migrateSchema1to2()
 bool COMMON_SETTINGS::migrateSchema2to3()
 {
     wxFileName cfgpath;
-    cfgpath.AssignDir( SETTINGS_MANAGER::GetUserSettingsPath() );
+    cfgpath.AssignDir( PATHS::GetUserSettingsPath() );
     cfgpath.AppendDir( wxT( "3d" ) );
     cfgpath.SetFullName( wxS( "3Dresolver.cfg" ) );
     cfgpath.MakeAbsolute();
diff --git a/common/settings/settings_manager.cpp b/common/settings/settings_manager.cpp
index f1359aeb3e..66e0a49e6c 100644
--- a/common/settings/settings_manager.cpp
+++ b/common/settings/settings_manager.cpp
@@ -423,7 +423,7 @@ wxString SETTINGS_MANAGER::GetPathForSettingsFile( JSON_SETTINGS* aSettings )
     switch( aSettings->GetLocation() )
     {
     case SETTINGS_LOC::USER:
-        return GetUserSettingsPath();
+        return PATHS::GetUserSettingsPath();
 
     case SETTINGS_LOC::PROJECT:
         // TODO: MDI support
@@ -521,7 +521,7 @@ public:
 
 bool SETTINGS_MANAGER::MigrateIfNeeded()
 {
-    wxFileName path( GetUserSettingsPath(), wxS( "" ) );
+    wxFileName path( PATHS::GetUserSettingsPath(), wxS( "" ) );
     wxLogTrace( traceSettings, wxT( "Using settings path %s" ), path.GetFullPath() );
 
     if( m_headless )
@@ -626,11 +626,11 @@ bool SETTINGS_MANAGER::GetPreviousVersionPaths( std::vector<wxString>* aPaths )
     wxDir dir;
     std::vector<wxFileName> base_paths;
 
-    base_paths.emplace_back( wxFileName( calculateUserSettingsPath( false ), wxS( "" ) ) );
+    base_paths.emplace_back( wxFileName( PATHS::CalculateUserSettingsPath( false ), wxS( "" ) ) );
 
     // If the env override is set, also check the default paths
     if( wxGetEnv( wxT( "KICAD_CONFIG_HOME" ), nullptr ) )
-        base_paths.emplace_back( wxFileName( calculateUserSettingsPath( false, false ), wxS( "" ) ) );
+        base_paths.emplace_back( wxFileName( PATHS::CalculateUserSettingsPath( false, false ), wxS( "" ) ) );
 
 #ifdef __WXGTK__
     // When running inside FlatPak, KIPLATFORM::ENV::GetUserConfigPath() will return a sandboxed
@@ -745,7 +745,7 @@ wxString SETTINGS_MANAGER::GetColorSettingsPath()
 {
     wxFileName path;
 
-    path.AssignDir( GetUserSettingsPath() );
+    path.AssignDir( PATHS::GetUserSettingsPath() );
     path.AppendDir( wxS( "colors" ) );
 
     if( !path.DirExists() )
@@ -760,45 +760,6 @@ wxString SETTINGS_MANAGER::GetColorSettingsPath()
 
     return path.GetPath();
 }
-
-
-wxString SETTINGS_MANAGER::GetUserSettingsPath()
-{
-    static wxString user_settings_path;
-
-    if( user_settings_path.empty() )
-        user_settings_path = calculateUserSettingsPath();
-
-    return user_settings_path;
-}
-
-
-wxString SETTINGS_MANAGER::calculateUserSettingsPath( bool aIncludeVer, bool aUseEnv )
-{
-    wxFileName cfgpath;
-
-    // http://docs.wxwidgets.org/3.0/classwx_standard_paths.html#a7c7cf595d94d29147360d031647476b0
-
-    wxString envstr;
-    if( aUseEnv && wxGetEnv( wxT( "KICAD_CONFIG_HOME" ), &envstr ) && !envstr.IsEmpty() )
-    {
-        // Override the assignment above with KICAD_CONFIG_HOME
-        cfgpath.AssignDir( envstr );
-    }
-    else
-    {
-        cfgpath.AssignDir( KIPLATFORM::ENV::GetUserConfigPath() );
-
-        cfgpath.AppendDir( TO_STR( KICAD_CONFIG_DIR ) );
-    }
-
-    if( aIncludeVer )
-        cfgpath.AppendDir( GetSettingsVersion() );
-
-    return cfgpath.GetPath();
-}
-
-
 std::string SETTINGS_MANAGER::GetSettingsVersion()
 {
     // CMake computes the major.minor string for us.
diff --git a/common/widgets/paged_dialog.cpp b/common/widgets/paged_dialog.cpp
index 5c2d40e303..db4b793c34 100644
--- a/common/widgets/paged_dialog.cpp
+++ b/common/widgets/paged_dialog.cpp
@@ -32,7 +32,7 @@
 #include <wx/listctrl.h>
 #include <wx/stc/stc.h>
 
-#include <settings/settings_manager.h>
+#include <paths.h>
 
 #include <launch_ext.h>
 
@@ -477,6 +477,6 @@ void PAGED_DIALOG::onResetButton( wxCommandEvent& aEvent )
 
 void PAGED_DIALOG::onOpenPreferencesButton( wxCommandEvent& aEvent )
 {
-    wxString dir( SETTINGS_MANAGER::GetUserSettingsPath() );
+    wxString dir( PATHS::GetUserSettingsPath() );
     LaunchExternal( dir );
 }
diff --git a/eeschema/symbol_lib_table.cpp b/eeschema/symbol_lib_table.cpp
index 1d8a120425..b424659f21 100644
--- a/eeschema/symbol_lib_table.cpp
+++ b/eeschema/symbol_lib_table.cpp
@@ -26,6 +26,7 @@
 
 #include <lib_id.h>
 #include <lib_table_lexer.h>
+#include <paths.h>
 #include <pgm_base.h>
 #include <search_stack.h>
 #include <settings/kicad_settings.h>
@@ -727,7 +728,7 @@ wxString SYMBOL_LIB_TABLE::GetGlobalTableFileName()
 {
     wxFileName fn;
 
-    fn.SetPath( SETTINGS_MANAGER::GetUserSettingsPath() );
+    fn.SetPath( PATHS::GetUserSettingsPath() );
     fn.SetName( global_tbl_name );
 
     return fn.GetFullPath();
diff --git a/include/paths.h b/include/paths.h
index 7dfa9f5d1d..e26928d59b 100644
--- a/include/paths.h
+++ b/include/paths.h
@@ -172,11 +172,38 @@ public:
 
 #ifdef __WXWINDOWS__
     /**
-     * @return The macOS specific bundle data directory for KiCad
+     * @return The directory the font config support files can be found
      */
     static wxString GetWindowsFontConfigDir();
 #endif
 
+    /**
+     * Return the user configuration path used to store KiCad's configuration files.
+     *
+     * @see calculateUserSettingsPath
+     *
+     * NOTE: The path is cached at startup, it will never change during program lifetime!
+     *
+     * @return A string containing the config path for Kicad
+     */
+    static wxString GetUserSettingsPath();
+
+    /**
+     * 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.
+     *
+     * @param aIncludeVer will append the current KiCad version if true (default)
+     * @param aUseEnv will prefer the base path found in the KICAD_CONFIG_DIR if found (default)
+     * @return A string containing the config path for Kicad
+     */
+    static wxString CalculateUserSettingsPath( bool aIncludeVer = true, bool aUseEnv = true );
+
 private:
     // we are a static helper
     PATHS() {}
diff --git a/include/settings/settings_manager.h b/include/settings/settings_manager.h
index 8711879f65..aa21509560 100644
--- a/include/settings/settings_manager.h
+++ b/include/settings/settings_manager.h
@@ -328,17 +328,6 @@ public:
      */
     static wxString GetColorSettingsPath();
 
-    /**
-     * Return the user configuration path used to store KiCad's configuration files.
-     *
-     * @see calculateUserSettingsPath
-     *
-     * NOTE: The path is cached at startup, it will never change during program lifetime!
-     *
-     * @return A string containing the config path for Kicad
-     */
-    static wxString GetUserSettingsPath();
-
     /**
      * 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.
@@ -350,23 +339,6 @@ public:
 private:
     JSON_SETTINGS* registerSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true );
 
-
-    /**
-     * 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.
-     *
-     * @param aIncludeVer will append the current KiCad version if true (default)
-     * @param aUseEnv will prefer the base path found in the KICAD_CONFIG_DIR if found (default)
-     * @return A string containing the config path for Kicad
-     */
-    static wxString calculateUserSettingsPath( bool aIncludeVer = true, bool aUseEnv = true );
-
     /**
      * Compares two settings versions, like "5.99" and "6.0"
      * @return -1 if aFirst is older than aSecond, 1 if aFirst is newer than aSecond, 0 otherwise
diff --git a/kicad/pcm/pcm.cpp b/kicad/pcm/pcm.cpp
index 5b8241278f..8afa256b0b 100644
--- a/kicad/pcm/pcm.cpp
+++ b/kicad/pcm/pcm.cpp
@@ -94,7 +94,7 @@ PLUGIN_CONTENT_MANAGER::PLUGIN_CONTENT_MANAGER(
     }
 
     // Load currently installed packages
-    wxFileName f( SETTINGS_MANAGER::GetUserSettingsPath(), wxT( "installed_packages.json" ) );
+    wxFileName f( PATHS::GetUserSettingsPath(), wxT( "installed_packages.json" ) );
 
     if( f.FileExists() )
     {
@@ -885,7 +885,7 @@ void PLUGIN_CONTENT_MANAGER::SaveInstalledPackages()
             js["packages"].emplace_back( pair.second );
         }
 
-        wxFileName    f( SETTINGS_MANAGER::GetUserSettingsPath(), wxT( "installed_packages.json" ) );
+        wxFileName    f( PATHS::GetUserSettingsPath(), wxT( "installed_packages.json" ) );
         std::ofstream stream( f.GetFullPath().fn_str() );
 
         stream << std::setw( 4 ) << js << std::endl;