From d56d127d3f8385e02a52bb979116ac8672be9157 Mon Sep 17 00:00:00 2001 From: Martin Koegler Date: Sat, 10 Dec 2016 17:56:04 +0100 Subject: [PATCH] Implement XDG file layout --- include/wx/stdpaths.h | 16 ++++++++++++++++ interface/wx/stdpaths.h | 26 ++++++++++++++++++++++++++ src/common/stdpbase.cpp | 1 + src/unix/stdpaths.cpp | 13 ++++++++++++- 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/include/wx/stdpaths.h b/include/wx/stdpaths.h index 382a99dfd6..d048888ddb 100644 --- a/include/wx/stdpaths.h +++ b/include/wx/stdpaths.h @@ -60,6 +60,12 @@ public: Dir_Videos }; + enum + { + FileLayout_Classic = 0, + FileLayout_XDG = 1 + }; + // return the global standard paths object static wxStandardPaths& Get(); @@ -168,6 +174,15 @@ public: bool UsesAppInfo(int info) const { return (m_usedAppInfo & info) != 0; } + void SetFileLayout(int layout) + { + m_usedFileLayout = layout; + } + + int GetFileLayout() const + { + return m_usedFileLayout; + } protected: // Ctor is protected as this is a base class which should never be created @@ -184,6 +199,7 @@ protected: // combination of AppInfo_XXX flags used by AppendAppInfo() int m_usedAppInfo; + int m_usedFileLayout; }; #if wxUSE_STDPATHS diff --git a/interface/wx/stdpaths.h b/interface/wx/stdpaths.h index 6484c94cf1..81f218f4a0 100644 --- a/interface/wx/stdpaths.h +++ b/interface/wx/stdpaths.h @@ -135,6 +135,19 @@ public: Dir_Videos }; + enum + { + /** + Use the classic file layout + */ + FileLayout_Classic, + + /** + Use a XDG styled file layout (Unix) + */ + FileLayout_XDG + }; + /** MSW-specific function undoing the effect of IgnoreAppSubDir() calls. @@ -444,6 +457,19 @@ public: */ void UseAppInfo(int info); + /** + Returns the current file layout + Valid values for @a are: + - @c FileLayout_Classic, + - @c FileLayout_XDG + */ + void SetFileLayout(int layout); + + /** + Returns the current file layout + */ + int GetFileLayout() const; + /** Return the file name which would be used by wxFileConfig as local, user-specific, file if it were constructed with @a basename. diff --git a/src/common/stdpbase.cpp b/src/common/stdpbase.cpp index 143c4f117c..e29bae8d14 100644 --- a/src/common/stdpbase.cpp +++ b/src/common/stdpbase.cpp @@ -97,6 +97,7 @@ wxStandardPathsBase::wxStandardPathsBase() // Derived classes can call this in their constructors // to set the platform-specific settings UseAppInfo(AppInfo_AppName); + SetFileLayout(FileLayout_Classic); } wxStandardPathsBase::~wxStandardPathsBase() diff --git a/src/unix/stdpaths.cpp b/src/unix/stdpaths.cpp index fecde0c634..ec59b30e43 100644 --- a/src/unix/stdpaths.cpp +++ b/src/unix/stdpaths.cpp @@ -53,6 +53,16 @@ void wxStandardPaths::SetInstallPrefix(const wxString& prefix) wxString wxStandardPaths::GetUserConfigDir() const { + if (GetFileLayout() & FileLayout_XDG) + { + wxString configPath; + if (wxGetenv(wxT("XDG_CONFIG_HOME"))) + configPath = wxGetenv(wxT("XDG_CONFIG_HOME")); + else + configPath = wxFileName::GetHomeDir() + wxT("/.config"); + return configPath; + } + return wxFileName::GetHomeDir(); } @@ -236,6 +246,7 @@ wxStandardPaths::GetLocalizedResourcesDir(const wxString& lang, wxString wxStandardPaths::GetUserDir(Dir userDir) const { + if (GetFileLayout() & FileLayout_XDG) { wxLogNull logNull; wxString homeDir = wxFileName::GetHomeDir(); @@ -312,7 +323,7 @@ wxString wxStandardPaths::GetUserDir(Dir userDir) const wxString wxStandardPaths::MakeConfigFileName(const wxString& basename, int style) const { wxFileName fn(wxEmptyString, basename); - if (style & wxCONFIG_USE_SUBDIR) + if (style & wxCONFIG_USE_SUBDIR || GetFileLayout() & FileLayout_XDG) fn.SetExt(wxT("conf")); else fn.SetName(wxT('.') + fn.GetName());