From d56cf71466f586785dfd1e75206736ee4e9cc963 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 2 Jan 2024 02:07:25 +0100 Subject: [PATCH] Add wxStandardPaths::Dir_Config This allows using GetUserDir(Dir_Config) to retrieve XDG-compliant location of the configuration files, independently of the file layout used by the global wxStandardPaths object. Recommend using this directory instead of GetUserDataDir() for multiple configuration files under Unix. --- include/wx/stdpaths.h | 1 + interface/wx/stdpaths.h | 16 ++++++++++++++-- src/msw/stdpaths.cpp | 3 +++ src/osx/cocoa/stdpaths.mm | 8 +++++++- src/unix/stdpaths.cpp | 6 +++++- 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/include/wx/stdpaths.h b/include/wx/stdpaths.h index 3afb5de4a5..7d0858abd8 100644 --- a/include/wx/stdpaths.h +++ b/include/wx/stdpaths.h @@ -51,6 +51,7 @@ public: enum Dir { Dir_Cache, + Dir_Config, Dir_Documents, Dir_Desktop, Dir_Downloads, diff --git a/interface/wx/stdpaths.h b/interface/wx/stdpaths.h index 40baffff73..46e55a61d7 100644 --- a/interface/wx/stdpaths.h +++ b/interface/wx/stdpaths.h @@ -79,6 +79,18 @@ public: */ Dir_Cache, + /** + Directory containing configuration information. + + Example return values: + - Unix: `~/.config` + - Windows: `C:\Users\username\AppData\Roaming` + - Mac: @c `~/Library/Preferences` + + @since 3.3.0 + */ + Dir_Config, + /** Directory containing user documents. @@ -384,8 +396,8 @@ public: - Mac: @c ~/Library/Preferences Only use this method if you have a single configuration file to put in this - directory, otherwise GetUserDataDir() is more appropriate as the latter - adds @c appinfo to the path, unlike this function. + directory, otherwise calling AppendAppInfo() with the value returned by + GetDir() with wxStandardPaths::Dir_Config is more appropriate. */ virtual wxString GetUserConfigDir() const; diff --git a/src/msw/stdpaths.cpp b/src/msw/stdpaths.cpp index 5fbaf97b1f..fca59bbfd4 100644 --- a/src/msw/stdpaths.cpp +++ b/src/msw/stdpaths.cpp @@ -189,6 +189,9 @@ wxString wxStandardPaths::GetUserDir(Dir userDir) const case Dir_Cache: csidl = CSIDL_LOCAL_APPDATA; break; + case Dir_Config: + csidl = CSIDL_APPDATA; + break; case Dir_Desktop: csidl = CSIDL_DESKTOPDIRECTORY; break; diff --git a/src/osx/cocoa/stdpaths.mm b/src/osx/cocoa/stdpaths.mm index 85418fb5af..584362c4bd 100644 --- a/src/osx/cocoa/stdpaths.mm +++ b/src/osx/cocoa/stdpaths.mm @@ -101,12 +101,18 @@ wxStandardPaths::GetLocalizedResourcesDir(const wxString& lang, wxString wxStandardPaths::GetUserDir(Dir userDir) const { + wxString subdir; + NSSearchPathDirectory dirType; switch (userDir) { case Dir_Cache: dirType = NSCachesDirectory; break; + case Dir_Config: + dirType = NSLibraryDirectory; + subdir = "/Preferences"; + break; case Dir_Desktop: dirType = NSDesktopDirectory; break; @@ -127,7 +133,7 @@ wxString wxStandardPaths::GetUserDir(Dir userDir) const break; } - return GetFMDirectory(dirType, NSUserDomainMask); + return GetFMDirectory(dirType, NSUserDomainMask) + subdir; } wxString diff --git a/src/unix/stdpaths.cpp b/src/unix/stdpaths.cpp index b492601d87..8d49f4b11b 100644 --- a/src/unix/stdpaths.cpp +++ b/src/unix/stdpaths.cpp @@ -274,7 +274,11 @@ wxString wxStandardPaths::GetUserDir(Dir userDir) const return cacheDir; } - const wxFileName dirsFile(GetXDGConfigHome(), wxS("user-dirs.dirs")); + const wxString configDir = GetXDGConfigHome(); + if (userDir == Dir_Config) + return configDir; + + const wxFileName dirsFile(configDir, wxS("user-dirs.dirs")); if ( dirsFile.FileExists() ) { wxString userDirId;