Use local AppData folder for user config on Windows.

master
Torsten Paul 2014-11-02 16:39:51 +01:00
parent 073c6fe2b3
commit 34981284fc
4 changed files with 40 additions and 25 deletions

View File

@ -26,11 +26,11 @@ std::string PlatformUtils::userConfigPath()
const char *xdg_env = getenv("XDG_CONFIG_HOME");
if (xdg_env && fs::exists(fs::path(xdg_env))) {
config_path = fs::path(xdg_env) / "OpenSCAD";
config_path = fs::path(xdg_env) / OPENSCAD_FOLDER_NAME;
} else {
const char *home = getenv("HOME");
if (home) {
config_path = fs::path(home) / ".config" / "OpenSCAD";
config_path = fs::path(home) / ".config" / OPENSCAD_FOLDER_NAME;
}
}

View File

@ -47,6 +47,27 @@ std::string winapi_wstr_to_utf8( std::wstring wstr )
return utf8_str;
}
// see http://msdn.microsoft.com/en-us/library/windows/desktop/bb762494%28v=vs.85%29.aspx
static const std::string getFolderPath(int nFolder)
{
std::wstring path(MAX_PATH,0);
HWND hwndOwner = 0;
HANDLE hToken = NULL;
DWORD dwFlags = SHGFP_TYPE_CURRENT;
LPTSTR pszPath = &path[0];
int result = SHGetFolderPathW( hwndOwner, nFolder, hToken, dwFlags, pszPath );
if (result == S_OK) {
path = std::wstring( path.c_str() ); // strip extra NULLs
//std::wcerr << "wchar path:" << "\n";
const std::string retval = winapi_wstr_to_utf8( path );
//PRINTB("Path found: %s",retval);
return retval;
}
return "";
}
// retrieve the path to 'My Documents' for the current user under windows
// In XP this is 'c:\documents and settings\username\my documents'
@ -55,32 +76,20 @@ std::string winapi_wstr_to_utf8( std::wstring wstr )
// Mingw does not provide access to the updated SHGetKnownFolderPath
std::string PlatformUtils::documentsPath()
{
std::string retval;
std::wstring path(MAX_PATH,0);
HWND hwndOwner = 0;
int nFolder = CSIDL_PERSONAL;
HANDLE hToken = NULL;
DWORD dwFlags = SHGFP_TYPE_CURRENT;
LPTSTR pszPath = &path[0];
int result = SHGetFolderPathW( hwndOwner, nFolder, hToken, dwFlags, pszPath );
if (result == S_OK) {
path = std::wstring( path.c_str() ); // stip extra NULLs
//std::wcerr << "wchar path:" << "\n";
retval = winapi_wstr_to_utf8( path );
//PRINTB("Path found: %s",retval);
} else {
PRINT("ERROR: Could not find My Documents location");
retval = "";
const std::string retval = getFolderPath(CSIDL_PERSONAL);
if (retval.empty()) {
PRINT("ERROR: Could not find My Documents location");
}
return retval;
}
std::string PlatformUtils::userConfigPath()
{
return "";
const std::string retval = getFolderPath(CSIDL_LOCAL_APPDATA);
if (retval.empty()) {
PRINT("ERROR: Could not find Local AppData location");
}
return retval + std::string("/") + PlatformUtils::OPENSCAD_FOLDER_NAME;
}
#include <io.h>

View File

@ -14,6 +14,8 @@ namespace {
std::string applicationpath;
}
const char *PlatformUtils::OPENSCAD_FOLDER_NAME = "OpenSCAD";
void PlatformUtils::registerApplicationPath(const std::string &apppath)
{
applicationpath = apppath;
@ -52,7 +54,7 @@ std::string PlatformUtils::userLibraryPath()
//PRINTB("path size %i",boosty::stringy(path).size());
//PRINTB("lib path found: [%s]", path );
if (path.empty()) return "";
path /= "OpenSCAD";
path /= OPENSCAD_FOLDER_NAME;
path /= "libraries";
//PRINTB("Appended path %s", path );
//PRINTB("Exists: %i", fs::exists(path) );
@ -71,7 +73,7 @@ std::string PlatformUtils::backupPath()
if (pathstr=="") return "";
path = boosty::canonical(fs::path( pathstr ));
if (path.empty()) return "";
path /= "OpenSCAD";
path /= OPENSCAD_FOLDER_NAME;
path /= "backups";
} catch (const fs::filesystem_error& ex) {
PRINTB("ERROR: %s",ex.what());

View File

@ -3,6 +3,7 @@
#include <string>
namespace PlatformUtils {
extern const char *OPENSCAD_FOLDER_NAME;
void registerApplicationPath(const std::string &applicationpath);
std::string applicationPath();
@ -13,7 +14,10 @@ namespace PlatformUtils {
/**
* Base path where user configuration can be read and written to. On
* Linux this is the $XDG_CONFIG_HOME.
* Linux this is the $XDG_CONFIG_HOME, on Windows the local AppData
* folder CSIDL_LOCAL_APPDATA.
* On success the returned path can be used directly as base folder
* as it already includes an OpenSCAD specific part.
*
* @return absolute path to the writable configuration folder or
* an empty string if the config path does not exist.