TuttleOFX  1
TuttleOFX/libraries/tuttle/src/tuttle/host/Preferences.cpp
Go to the documentation of this file.
00001 #include "Preferences.hpp"
00002 
00003 #include <tuttle/common/system/system.hpp>
00004 #include <tuttle/common/utils/global.hpp>
00005 
00006 #include <boost/filesystem/path.hpp>
00007 #include <boost/filesystem/operations.hpp>
00008 
00009 #ifdef __WINDOWS__
00010 #include <windows.h>
00011 #include <shlobj.h>
00012 #elif defined( __UNIX__ )
00013 #include <pwd.h>
00014 #include <sys/types.h>
00015 #endif
00016 
00017 namespace tuttle {
00018 namespace host {
00019 
00020 Preferences::Preferences()
00021 : _home( buildTuttleHome() )
00022 , _temp( buildTuttleTemp() )
00023 {}
00024 
00025 boost::filesystem::path Preferences::buildTuttleHome() const
00026 {
00027         boost::filesystem::path tuttleHome;
00028         
00029         char* env_tuttle_cache = NULL;
00030 #ifdef __WINDOWS__
00031         char winPath[ MAX_PATH ];
00032 #elif defined(__UNIX__)
00033         struct passwd* userInfos = NULL;
00034 #endif
00035         
00036         if(( env_tuttle_cache = std::getenv("TUTTLE_HOME") ))
00037         {
00038                 tuttleHome = env_tuttle_cache;
00039         }
00040 #ifdef __WINDOWS__
00041         else if( ( env_tuttle_cache = std::getenv("APPDATA") ) ) // application data directory
00042         {
00043                 // windows root path for application settings
00044                 tuttleHome = boost::filesystem::path(env_tuttle_cache) / "TuttleOFX";
00045         }
00046         else if( SHGetFolderPathA( NULL, CSIDL_LOCAL_APPDATA, NULL, 0, winPath ) == S_OK ) // same as previous...
00047         {
00048                 // windows root path for application settings
00049                 tuttleHome = boost::filesystem::path(winPath) / "TuttleOFX";
00050         }
00051         // if APPDATA doesn't exist, try to use USERPROFILE home directory
00052         // as on UNIX platforms even if it's not standard.
00053         else if( ( env_tuttle_cache = std::getenv("USERPROFILE") ) ) // home directory
00054         {
00055                 // windows root path for user home directory
00056                 tuttleHome = boost::filesystem::path(env_tuttle_cache) / ".tuttleofx";
00057         }
00058         else if( SHGetFolderPathA( NULL, CSIDL_PROFILE, NULL, 0, winPath ) == S_OK ) // same as previous...
00059         {
00060                 // windows root path for user home directory
00061                 tuttleHome = boost::filesystem::path(winPath) / ".tuttleofx";
00062         }
00063 #elif defined(__UNIX__)
00064         // UNIX home directory to store application settings
00065         else if(( env_tuttle_cache = std::getenv("HOME") )) // UNIX HOME
00066         {
00067                 /* getpwuid documentation:
00068                  * An application that wants to determine its user's home directory
00069                  * should inspect the value of HOME (rather than the value
00070                  * getpwuid(getuid())->pw_dir ) since this allows the user to modify
00071                  * their notion of "the home directory" during a login session.
00072                  */
00073                 tuttleHome = boost::filesystem::path(env_tuttle_cache) / ".tuttleofx";
00074         }
00075         else if( ( userInfos = getpwuid(getuid()) ) && userInfos->pw_dir )
00076         {
00077                 tuttleHome = boost::filesystem::path(userInfos->pw_dir) / ".tuttleofx";
00078         }
00079 #endif
00080         else // use temp directory
00081         {
00082                 tuttleHome = boost::filesystem::temp_directory_path() / "tuttleofx";
00083         }
00084         
00085         if( ! tuttleHome.empty() &&
00086             ! boost::filesystem::exists( tuttleHome ) )
00087         {
00088                 boost::filesystem::create_directories( tuttleHome );
00089         }
00090         
00091         return tuttleHome;
00092 }
00093 
00094 boost::filesystem::path Preferences::buildTuttleTemp() const
00095 {
00096         const boost::filesystem::path tuttleTmp = boost::filesystem::temp_directory_path() / "tuttle";
00097         if( ! boost::filesystem::exists( tuttleTmp ) )
00098         {
00099                 boost::filesystem::create_directories( tuttleTmp );
00100         }
00101         
00102         return tuttleTmp;
00103 }
00104 
00105 boost::filesystem::path Preferences::buildTuttleTestPath() const
00106 {
00107         const boost::filesystem::path tuttleTest = boost::filesystem::current_path() / ".tests";
00108         if( ! boost::filesystem::exists( tuttleTest ) )
00109         {
00110                 boost::filesystem::create_directories( tuttleTest );
00111         }
00112         
00113         return tuttleTest;
00114 }
00115 
00116 
00117 }
00118 }
00119