From e1e813e113423a1fd5495d83dc0ea0e616770e0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 1 Jan 2011 22:05:26 +0100 Subject: [PATCH] Using winapi to get Application Data and Documents paths. --- src/Utility/Directory.cpp | 28 +++++++++++++++++++++++----- src/Utility/Directory.h | 5 +++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/Utility/Directory.cpp b/src/Utility/Directory.cpp index c8ea6826c..90580c2b8 100644 --- a/src/Utility/Directory.cpp +++ b/src/Utility/Directory.cpp @@ -19,6 +19,10 @@ #include #include +#ifdef _WIN32 +#include +#endif + #include "utilities.h" using namespace std; @@ -97,20 +101,34 @@ bool Directory::fileExists(const std::string& filename) { } string Directory::home() { - /** @todo @c VERSION-0.1 Fix for WIN32 */ + #ifndef _WIN32 char* h = getenv("HOME"); if(!h) return ""; - else return h; + #else + /** @bug Doesn't work at all */ + TCHAR h[MAX_PATH]; + if(!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, h))) + return ""; + #endif + + return h; } string Directory::configurationDir(const std::string& applicationName, bool createIfNotExists) { - /** @todo @c VERSION-0.1 Fix for WIN32 -- it's $ENV{AppData} in CMake */ + #ifndef _WIN32 string h = home(); if(h.empty()) return ""; + string dir = join(h, '.' + lowercase(applicationName)); + #else + TCHAR path[MAX_PATH]; + if(!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, path))) + return ""; + string appdata = path; + if(appdata.empty()) return ""; + string dir = join(appdata, applicationName); + #endif - string dir = join(home(), '.' + lowercase(applicationName)); if(createIfNotExists) mkpath(dir); - return dir; } diff --git a/src/Utility/Directory.h b/src/Utility/Directory.h index e3017a665..6434f8956 100644 --- a/src/Utility/Directory.h +++ b/src/Utility/Directory.h @@ -104,6 +104,11 @@ class UTILITY_EXPORT Directory: public std::vector { * @brief Get application configuration dir * @param name Application name * @param createIfNotExists Create the directory, if not exists already + * + * On Unix, the configuration dir is ~/.name (name is + * lowercased), on Windows the configuration dir is somewhere in + * C:/Document and Settings/user/Application Data/name + * (name is left as is). */ static std::string configurationDir(const std::string& name, bool createIfNotExists = true);