Issues #1609 and #561 have not made it into a stable release #2152

This commit is contained in:
Rudolf-Walter Kiss-Szakács
2017-02-23 08:23:41 +02:00
committed by Alex Fabijanic
parent 2632f34e85
commit 275275baa9
14 changed files with 246 additions and 2 deletions

View File

@@ -289,6 +289,24 @@ public:
static std::string home(); static std::string home();
/// Returns the user's home directory. /// Returns the user's home directory.
static std::string configHome();
/// Returns the user's config directory.
///
/// On Unix systems, this is the '~/.config/'. On Windows systems,
/// this is '%APPDATA%'.
static std::string dataHome();
/// Returns the user's data directory.
///
/// On Unix systems, this is the '~/.local/share/'. On Windows systems,
/// this is '%APPDATA%'.
static std::string cacheHome();
/// Returns the user's cache directory.
///
/// On Unix systems, this is the '~/.cache/'. On Windows systems,
/// this is '%APPDATA%'.
static std::string temp(); static std::string temp();
/// Returns the temporary directory. /// Returns the temporary directory.

View File

@@ -30,6 +30,9 @@ class PathImpl
public: public:
static std::string currentImpl(); static std::string currentImpl();
static std::string homeImpl(); static std::string homeImpl();
static std::string configHomeImpl();
static std::string dataHomeImpl();
static std::string cacheHomeImpl();
static std::string tempImpl(); static std::string tempImpl();
static std::string nullImpl(); static std::string nullImpl();
static std::string expandImpl(const std::string& path); static std::string expandImpl(const std::string& path);

View File

@@ -30,6 +30,9 @@ class Foundation_API PathImpl
public: public:
static std::string currentImpl(); static std::string currentImpl();
static std::string homeImpl(); static std::string homeImpl();
static std::string configHomeImpl();
static std::string dataHomeImpl();
static std::string cacheHomeImpl();
static std::string tempImpl(); static std::string tempImpl();
static std::string nullImpl(); static std::string nullImpl();
static std::string systemImpl(); static std::string systemImpl();

View File

@@ -30,6 +30,9 @@ class Foundation_API PathImpl
public: public:
static std::string currentImpl(); static std::string currentImpl();
static std::string homeImpl(); static std::string homeImpl();
static std::string configHomeImpl();
static std::string dataHomeImpl();
static std::string cacheHomeImpl();
static std::string tempImpl(); static std::string tempImpl();
static std::string nullImpl(); static std::string nullImpl();
static std::string systemImpl(); static std::string systemImpl();

View File

@@ -33,7 +33,6 @@ public:
static std::string configHomeImpl(); static std::string configHomeImpl();
static std::string dataHomeImpl(); static std::string dataHomeImpl();
static std::string cacheHomeImpl(); static std::string cacheHomeImpl();
static std::string tempHomeImpl();
static std::string tempImpl(); static std::string tempImpl();
static std::string configImpl(); static std::string configImpl();
static std::string nullImpl(); static std::string nullImpl();

View File

@@ -569,6 +569,16 @@ std::string Path::home()
} }
std::string Path::cacheHome()
{
#if defined(POCO_OS_FAMILY_UNIX) || defined(POCO_OS_FAMILY_WINDOWS)
return PathImpl::cacheHomeImpl();
#else
return PathImpl::homeImpl();
#endif
}
std::string Path::temp() std::string Path::temp()
{ {
return PathImpl::tempImpl(); return PathImpl::tempImpl();

View File

@@ -74,6 +74,87 @@ std::string PathImpl::homeImpl()
} }
std::string PathImpl::configHomeImpl()
{
#if defined(POCO_VXWORKS)
return PathImpl::homeImpl();
#elif POCO_OS == POCO_OS_MAC_OS_X
std::string path = PathImpl::homeImpl();
std::string::size_type n = path.size();
if (n > 0 && path[n - 1] == '/')
path.append("Library/Preferences/");
return path;
#else
std::string path;
if (EnvironmentImpl::hasImpl("XDG_CONFIG_HOME"))
path = EnvironmentImpl::getImpl("XDG_CONFIG_HOME");
if (!path.empty())
return path;
path = PathImpl::homeImpl();
std::string::size_type n = path.size();
if (n > 0 && path[n - 1] == '/')
path.append(".config/");
return path;
#endif
}
std::string PathImpl::dataHomeImpl()
{
#if defined(POCO_VXWORKS)
return PathImpl::homeImpl();
#elif POCO_OS == POCO_OS_MAC_OS_X
std::string path = PathImpl::homeImpl();
std::string::size_type n = path.size();
if (n > 0 && path[n - 1] == '/')
path.append("Library/Application Support/");
return path;
#else
std::string path;
if (EnvironmentImpl::hasImpl("XDG_DATA_HOME"))
path = EnvironmentImpl::getImpl("XDG_DATA_HOME");
if (!path.empty())
return path;
path = PathImpl::homeImpl();
std::string::size_type n = path.size();
if (n > 0 && path[n - 1] == '/')
path.append(".local/share/");
return path;
#endif
}
std::string PathImpl::cacheHomeImpl()
{
#if defined(POCO_VXWORKS)
return PathImpl::tempImpl();
#elif POCO_OS == POCO_OS_MAC_OS_X
std::string path = PathImpl::homeImpl();
std::string::size_type n = path.size();
if (n > 0 && path[n - 1] == '/')
path.append("Library/Caches/");
return path;
#else
std::string path;
if (EnvironmentImpl::hasImpl("XDG_CACHE_HOME"))
path = EnvironmentImpl::getImpl("XDG_CACHE_HOME");
if (!path.empty())
return path;
path = PathImpl::homeImpl();
std::string::size_type n = path.size();
if (n > 0 && path[n - 1] == '/')
path.append(".cache/");
return path;
#endif
}
std::string PathImpl::tempImpl() std::string PathImpl::tempImpl()
{ {
std::string path; std::string path;

View File

@@ -74,6 +74,54 @@ std::string PathImpl::homeImpl()
} }
std::string PathImpl::configHomeImpl()
{
std::string result;
// if APPDATA environment variable not exist, return home directory instead
try
{
result = EnvironmentImpl::getImpl("APPDATA");
}
catch (NotFoundException&)
{
result = homeImpl();
}
std::string::size_type n = result.size();
if (n > 0 && result[n - 1] != '\\')
result.append("\\");
return result;
}
std::string PathImpl::dataHomeImpl()
{
std::string result;
// if LOCALAPPDATA environment variable not exist, return config home instead
try
{
result = EnvironmentImpl::getImpl("LOCALAPPDATA");
}
catch (NotFoundException&)
{
result = configHomeImpl();
}
std::string::size_type n = result.size();
if (n > 0 && result[n - 1] != '\\')
result.append("\\");
return result;
}
std::string PathImpl::cacheHomeImpl()
{
return tempImpl();
}
std::string PathImpl::tempImpl() std::string PathImpl::tempImpl()
{ {
char buffer[MAX_PATH]; char buffer[MAX_PATH];

View File

@@ -84,6 +84,54 @@ std::string PathImpl::homeImpl()
} }
std::string PathImpl::configHomeImpl()
{
std::string result;
// if APPDATA environment variable no exist, return home directory instead
try
{
result = EnvironmentImpl::getImpl("APPDATA");
}
catch (NotFoundException&)
{
result = homeImpl();
}
std::string::size_type n = result.size();
if (n > 0 && result[n - 1] != '\\')
result.append("\\");
return result;
}
std::string PathImpl::dataHomeImpl()
{
std::string result;
// if LOCALAPPDATA environment variable no exist, return config home instead
try
{
result = EnvironmentImpl::getImpl("LOCALAPPDATA");
}
catch (NotFoundException&)
{
result = configHomeImpl();
}
std::string::size_type n = result.size();
if (n > 0 && result[n - 1] != '\\')
result.append("\\");
return result;
}
std::string PathImpl::cacheHomeImpl()
{
return tempImpl();
}
std::string PathImpl::tempImpl() std::string PathImpl::tempImpl()
{ {
Buffer<wchar_t> buffer(MAX_PATH_LEN); Buffer<wchar_t> buffer(MAX_PATH_LEN);

View File

@@ -49,11 +49,13 @@ std::string PathImpl::cacheHomeImpl()
return homeImpl(); return homeImpl();
} }
std::string PathImpl::tempHomeImpl() std::string PathImpl::tempHomeImpl()
{ {
return tempImpl(); return tempImpl();
} }
std::string PathImpl::configImpl() std::string PathImpl::configImpl()
{ {
return("\\"); return("\\");

View File

@@ -39,6 +39,9 @@ class Util_API SystemConfiguration: public AbstractConfiguration
/// of the first Ethernet adapter found on the system. /// of the first Ethernet adapter found on the system.
/// - system.currentDir: the current working directory /// - system.currentDir: the current working directory
/// - system.homeDir: the user's home directory /// - system.homeDir: the user's home directory
/// - system.configHomeDir: the base directory relative to which user specific configuration files should be stored
/// - system.cacheHomeDir: the base directory relative to which user specific non-essential data files should be stored
/// - system.dataHomeDir: the base directory relative to which user specific data files should be stored
/// - system.tempDir: the system's temporary directory /// - system.tempDir: the system's temporary directory
/// - system.dateTime: the current UTC date and time, formatted in ISO 8601 format. /// - system.dateTime: the current UTC date and time, formatted in ISO 8601 format.
/// - system.pid: the current process ID. /// - system.pid: the current process ID.
@@ -74,6 +77,9 @@ private:
static const std::string NODEID; static const std::string NODEID;
static const std::string CURRENTDIR; static const std::string CURRENTDIR;
static const std::string HOMEDIR; static const std::string HOMEDIR;
static const std::string CONFIGHOMEDIR;
static const std::string CACHEHOMEDIR;
static const std::string DATAHOMEDIR;
static const std::string TEMPDIR; static const std::string TEMPDIR;
static const std::string DATETIME; static const std::string DATETIME;
#if !defined(POCO_VXWORKS) #if !defined(POCO_VXWORKS)

View File

@@ -164,7 +164,9 @@ void Application::init()
_pConfig->setString("application.name", appPath.getFileName()); _pConfig->setString("application.name", appPath.getFileName());
_pConfig->setString("application.baseName", appPath.getBaseName()); _pConfig->setString("application.baseName", appPath.getBaseName());
_pConfig->setString("application.dir", appPath.parent().toString()); _pConfig->setString("application.dir", appPath.parent().toString());
_pConfig->setString("application.configDir", appPath.parent().toString()); _pConfig->setString("application.configDir", Path::configHome() + appPath.getBaseName() + Path::separator());
_pConfig->setString("application.cacheDir", Path::cacheHome() + appPath.getBaseName() + Path::separator());
_pConfig->setString("application.dataDir", Path::dataHome() + appPath.getBaseName() + Path::separator());
processOptions(); processOptions();
} }

View File

@@ -41,6 +41,9 @@ const std::string SystemConfiguration::NODENAME = "system.nodeName";
const std::string SystemConfiguration::NODEID = "system.nodeId"; const std::string SystemConfiguration::NODEID = "system.nodeId";
const std::string SystemConfiguration::CURRENTDIR = "system.currentDir"; const std::string SystemConfiguration::CURRENTDIR = "system.currentDir";
const std::string SystemConfiguration::HOMEDIR = "system.homeDir"; const std::string SystemConfiguration::HOMEDIR = "system.homeDir";
const std::string SystemConfiguration::CONFIGHOMEDIR = "system.configHomeDir";
const std::string SystemConfiguration::CACHEHOMEDIR = "system.cacheHomeDir";
const std::string SystemConfiguration::DATAHOMEDIR = "system.dataHomeDir";
const std::string SystemConfiguration::TEMPDIR = "system.tempDir"; const std::string SystemConfiguration::TEMPDIR = "system.tempDir";
const std::string SystemConfiguration::DATETIME = "system.dateTime"; const std::string SystemConfiguration::DATETIME = "system.dateTime";
#if !defined(POCO_VXWORKS) #if !defined(POCO_VXWORKS)
@@ -106,6 +109,18 @@ bool SystemConfiguration::getRaw(const std::string& key, std::string& value) con
{ {
value = Path::home(); value = Path::home();
} }
else if (key == CONFIGHOMEDIR)
{
value = Path::configHome();
}
else if (key == CACHEHOMEDIR)
{
value = Path::cacheHome();
}
else if (key == DATAHOMEDIR)
{
value = Path::dataHome();
}
else if (key == TEMPDIR) else if (key == TEMPDIR)
{ {
value = Path::temp(); value = Path::temp();
@@ -151,6 +166,9 @@ void SystemConfiguration::enumerate(const std::string& key, Keys& range) const
range.push_back("nodeId"); range.push_back("nodeId");
range.push_back("currentDir"); range.push_back("currentDir");
range.push_back("homeDir"); range.push_back("homeDir");
range.push_back("configHomeDir");
range.push_back("cacheHomeDir");
range.push_back("dataHomeDir");
range.push_back("tempDir"); range.push_back("tempDir");
range.push_back("dateTime"); range.push_back("dateTime");
#if !defined(POCO_VXWORKS) #if !defined(POCO_VXWORKS)

View File

@@ -93,6 +93,9 @@ void SystemConfigurationTest::testKeys()
assert (std::find(keys.begin(), keys.end(), "nodeId") != keys.end()); assert (std::find(keys.begin(), keys.end(), "nodeId") != keys.end());
assert (std::find(keys.begin(), keys.end(), "currentDir") != keys.end()); assert (std::find(keys.begin(), keys.end(), "currentDir") != keys.end());
assert (std::find(keys.begin(), keys.end(), "homeDir") != keys.end()); assert (std::find(keys.begin(), keys.end(), "homeDir") != keys.end());
assert (std::find(keys.begin(), keys.end(), "configHomeDir") != keys.end());
assert (std::find(keys.begin(), keys.end(), "cacheHomeDir") != keys.end());
assert (std::find(keys.begin(), keys.end(), "dataHomeDir") != keys.end());
assert (std::find(keys.begin(), keys.end(), "tempDir") != keys.end()); assert (std::find(keys.begin(), keys.end(), "tempDir") != keys.end());
assert (std::find(keys.begin(), keys.end(), "dateTime") != keys.end()); assert (std::find(keys.begin(), keys.end(), "dateTime") != keys.end());
#if !defined(POCO_VXWORKS) #if !defined(POCO_VXWORKS)