From 406897aa646b387fd29a65c6911ae1e958d24cc5 Mon Sep 17 00:00:00 2001 From: Lukasz Gromanowski Date: Sat, 21 Jan 2012 17:58:49 +0100 Subject: [PATCH] Issue #168 - Configuration cleanup - WIP Sources update. Signed-off-by: Lukasz Gromanowski --- apps/openmw/main.cpp | 2 +- components/files/configurationmanager.cpp | 54 +++--------------- components/files/configurationmanager.hpp | 17 +++--- components/files/fixedpath.hpp | 68 ++++++----------------- components/files/linuxpath.cpp | 65 ++++++++++++++++++++++ components/files/linuxpath.hpp | 23 ++++++++ components/files/macospath.cpp | 48 ++++++++++++++++ components/files/macospath.hpp | 2 + components/files/windowspath.cpp | 29 ++++++++++ components/files/windowspath.hpp | 24 ++++++++ 10 files changed, 228 insertions(+), 104 deletions(-) diff --git a/apps/openmw/main.cpp b/apps/openmw/main.cpp index 0f66925d3..02fa1d765 100644 --- a/apps/openmw/main.cpp +++ b/apps/openmw/main.cpp @@ -166,7 +166,7 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat if (dataDirs.empty()) { - dataDirs.push_back(cfgMgr.getDataPath("local:data?")); + dataDirs.push_back(cfgMgr.getDataPath(Files::localDataToken)); } engine.setDataDirs(dataDirs); diff --git a/components/files/configurationmanager.cpp b/components/files/configurationmanager.cpp index 224fd2649..aaa26977c 100644 --- a/components/files/configurationmanager.cpp +++ b/components/files/configurationmanager.cpp @@ -12,10 +12,10 @@ static const char* const openmwCfgFile = "openmw.cfg"; static const char* const ogreCfgFile = "ogre.cfg"; static const char* const pluginsCfgFile = "plugins.cfg"; -static const char* const mwDataToken = "?mw:data?"; -static const char* const localDataToken = "?local:data?"; -static const char* const userDataToken = "?user:data?"; -static const char* const globalDataToken = "?global:data?"; +const char* const mwDataToken = "?mw:data?"; +const char* const localDataToken = "?local:data?"; +const char* const userDataToken = "?user:data?"; +const char* const globalDataToken = "?global:data?"; ConfigurationManager::ConfigurationManager() : mFixedPath("openmw") @@ -55,10 +55,10 @@ ConfigurationManager::~ConfigurationManager() void ConfigurationManager::setupTokensMapping() { - mTokensMapping.insert(std::make_pair(mwDataToken, &ConfigurationManager::getInstallPath)); - mTokensMapping.insert(std::make_pair(localDataToken, &ConfigurationManager::getLocalDataPath)); - mTokensMapping.insert(std::make_pair(userDataToken, &ConfigurationManager::getUserDataPath)); - mTokensMapping.insert(std::make_pair(globalDataToken, &ConfigurationManager::getGlobalDataPath)); + mTokensMapping.insert(std::make_pair(mwDataToken, &FixedPath<>::getInstallPath)); + mTokensMapping.insert(std::make_pair(localDataToken, &FixedPath<>::getLocalDataPath)); + mTokensMapping.insert(std::make_pair(userDataToken, &FixedPath<>::getUserDataPath)); + mTokensMapping.insert(std::make_pair(globalDataToken, &FixedPath<>::getGlobalDataPath)); } void ConfigurationManager::readConfiguration(boost::program_options::variables_map& variables, @@ -117,48 +117,12 @@ const boost::filesystem::path& ConfigurationManager::getDataPath(const std::stri TokensMappingContainer::const_iterator it = mTokensMapping.find(type); if (it != mTokensMapping.end()) { - return ((this)->*(it->second))(); + return ((mFixedPath).*(it->second))(); } return mFixedPath.getLocalDataPath(); } -const boost::filesystem::path& ConfigurationManager::getInstallPath() const -{ -// TODO: It will be corrected later. - static boost::filesystem::path p("./"); - return p; - - //return mFixedPath.getInstallPath(); -} - -const boost::filesystem::path& ConfigurationManager::getGlobalDataPath() const -{ -// TODO: It will be corrected later. - static boost::filesystem::path p("./"); - return p; - - //return mFixedPath.getGlobalDataPath(); -} - -const boost::filesystem::path& ConfigurationManager::getUserDataPath() const -{ -// TODO: It will be corrected later. - static boost::filesystem::path p("./"); - return p; - - //return mFixedPath.getUserDataPath(); -} - -const boost::filesystem::path& ConfigurationManager::getLocalDataPath() const -{ -// TODO: It will be corrected later. - static boost::filesystem::path p("./"); - return p; - //return mFixedPath.getLocalDataPath(); -} - - const boost::filesystem::path& ConfigurationManager::getOgreConfigPath() const { return mOgreCfgPath; diff --git a/components/files/configurationmanager.hpp b/components/files/configurationmanager.hpp index 3004b6281..d1c999979 100644 --- a/components/files/configurationmanager.hpp +++ b/components/files/configurationmanager.hpp @@ -14,6 +14,12 @@ namespace Files { +extern const char* const mwDataToken; +extern const char* const localDataToken; +extern const char* const userDataToken; +extern const char* const globalDataToken; + + /** * \struct ConfigurationManager */ @@ -37,7 +43,9 @@ struct ConfigurationManager const boost::filesystem::path& getLogPath() const; private: - typedef const boost::filesystem::path& (ConfigurationManager::*path_type_f)() const; + typedef Files::FixedPath<> FixedPathType; + + typedef const boost::filesystem::path& (FixedPathType::*path_type_f)() const; typedef std::tr1::unordered_map TokensMappingContainer; void loadConfig(const boost::filesystem::path& path, @@ -46,12 +54,7 @@ struct ConfigurationManager void setupTokensMapping(); - const boost::filesystem::path& getInstallPath() const; - const boost::filesystem::path& getGlobalDataPath() const; - const boost::filesystem::path& getUserDataPath() const; - const boost::filesystem::path& getLocalDataPath() const; - - Files::FixedPath<> mFixedPath; + FixedPathType mFixedPath; boost::filesystem::path mOgreCfgPath; boost::filesystem::path mPluginsCfgPath; diff --git a/components/files/fixedpath.hpp b/components/files/fixedpath.hpp index 4e2b20e3c..3a27bd21d 100644 --- a/components/files/fixedpath.hpp +++ b/components/files/fixedpath.hpp @@ -73,9 +73,10 @@ struct FixedPath , mUserPath(mPath.getUserPath()) , mGlobalPath(mPath.getGlobalPath()) , mLocalPath(mPath.getLocalPath()) - , mLocalDataPath() - , mGlobalDataPath() - , mRuntimeDataPath() + , mUserDataPath(mPath.getUserDataPath()) + , mGlobalDataPath(mPath.getGlobalDataPath()) + , mLocalDataPath(mPath.getLocalDataPath()) + , mInstallPath(mPath.getInstallPath()) { if (!application_name.empty()) { @@ -119,64 +120,28 @@ struct FixedPath return mLocalPath; } - /** - * \brief Return path pointing to the user local data directory. - * - * \return boost::filesystem::path - */ - const boost::filesystem::path& getLocalDataPath() const + const boost::filesystem::path& getInstallPath() const { - return mLocalDataPath; - } + // TODO: It will be corrected later. + static boost::filesystem::path p("./"); + return p; - /** - * \brief Sets new local data path. - * - * \param [in] path - New path - */ - void setLocalDataPath(const boost::filesystem::path& path) - { - mLocalDataPath = path; + //return mFixedPath.getInstallPath(); } - /** - * \brief Return path pointing to the global (system) data directory. - * - * \return boost::filesystem::path - */ const boost::filesystem::path& getGlobalDataPath() const { return mGlobalDataPath; } - /** - * \brief Sets new global (system) data directory. - * - * \param [in] path - New path - */ - void setGlobalDataPath(const boost::filesystem::path& path) + const boost::filesystem::path& getUserDataPath() const { - mGlobalDataPath = path; + return mUserDataPath; } - /** - * \brief Return path pointing to the directory where application was started. - * - * \return boost::filesystem::path - */ - const boost::filesystem::path& getRuntimeDataPath() const - { - return mRuntimeDataPath; - } - - /** - * \brief Sets new runtime data directory. - * - * \param [in] path - New path - */ - void setRuntimeDataPath(const boost::filesystem::path& path) + const boost::filesystem::path& getLocalDataPath() const { - mRuntimeDataPath = path; + return mLocalDataPath; } private: @@ -186,11 +151,12 @@ struct FixedPath boost::filesystem::path mGlobalPath; /**< Global path */ boost::filesystem::path mLocalPath; /**< It is the same directory where application was run */ - boost::filesystem::path mLocalDataPath; /**< User local application data path (user plugins / mods / etc.) */ - boost::filesystem::path mGlobalDataPath; /**< Global application data path */ - boost::filesystem::path mRuntimeDataPath; /**< Runtime path to the configuration files. + boost::filesystem::path mUserDataPath; /**< User data path */ + boost::filesystem::path mGlobalDataPath; /**< Global application data path */ + boost::filesystem::path mLocalDataPath; /**< Local path to the configuration files. By default it is a 'data' directory in same directory where application was run */ + boost::filesystem::path mInstallPath; }; diff --git a/components/files/linuxpath.cpp b/components/files/linuxpath.cpp index 11ddc3104..8466f0222 100644 --- a/components/files/linuxpath.cpp +++ b/components/files/linuxpath.cpp @@ -95,6 +95,71 @@ boost::filesystem::path LinuxPath::getLocalPath() const return boost::filesystem::path("./"); } +boost::filesystem::path LinuxPath::getUserDataPath() const +{ + boost::filesystem::path localDataPath("."); + boost::filesystem::path suffix("/"); + + const char* theDir = getenv("OPENMW_DATA"); + if (theDir == NULL) + { + theDir = getenv("XDG_DATA_HOME"); + if (theDir == NULL) + { + theDir = getenv("HOME"); + if (theDir == NULL) + { + struct passwd* pwd = getpwuid(getuid()); + if (pwd != NULL) + { + theDir = pwd->pw_dir; + } + } + if (theDir != NULL) + { + suffix = boost::filesystem::path("/.local/share/"); + } + } + } + + if (theDir != NULL) { + localDataPath = boost::filesystem::path(theDir); + } + + localDataPath /= suffix; + return localDataPath; +} + +boost::filesystem::path LinuxPath::getGlobalDataPath() const +{ + boost::filesystem::path globalDataPath("/usr/local/share/"); + + char* theDir = getenv("XDG_DATA_DIRS"); + if (theDir != NULL) + { + // We take only first path from list + char* ptr = strtok(theDir, ":"); + if (ptr != NULL) + { + globalDataPath = boost::filesystem::path(ptr); + globalDataPath /= boost::filesystem::path("/"); + } + } + + return globalDataPath; +} + +boost::filesystem::path LinuxPath::getLocalDataPath() const +{ + return boost::filesystem::path("./data/"); +} + + +boost::filesystem::path LinuxPath::getInstallPath() const +{ + return boost::filesystem::path("./"); +} + } /* namespace Files */ #endif /* defined(__linux__) || defined(__FreeBSD__) */ diff --git a/components/files/linuxpath.hpp b/components/files/linuxpath.hpp index af92326be..51be6242c 100644 --- a/components/files/linuxpath.hpp +++ b/components/files/linuxpath.hpp @@ -59,6 +59,29 @@ struct LinuxPath * \return boost::filesystem::path */ boost::filesystem::path getLocalPath() const; + + /** + * \brief + * + * \return boost::filesystem::path + */ + boost::filesystem::path getUserDataPath() const; + + /** + * \brief + * + * \return boost::filesystem::path + */ + boost::filesystem::path getGlobalDataPath() const; + + /** + * \brief + * + * \return boost::filesystem::path + */ + boost::filesystem::path getLocalDataPath() const; + + boost::filesystem::path getInstallPath() const; }; } /* namespace Files */ diff --git a/components/files/macospath.cpp b/components/files/macospath.cpp index 1ffb44399..87ac42202 100644 --- a/components/files/macospath.cpp +++ b/components/files/macospath.cpp @@ -69,6 +69,54 @@ boost::filesystem::path MacOsPath::getLocalPath() const return boost::filesystem::path("./"); } +boost::filesystem::path MacOsPath::getUserDataPath() const +{ + boost::filesystem::path localDataPath("."); + boost::filesystem::path suffix("/"); + + const char* theDir = getenv("OPENMW_DATA"); + if (theDir == NULL) + { + theDir = getenv("HOME"); + if (theDir == NULL) + { + struct passwd* pwd = getpwuid(getuid()); + if (pwd != NULL) + { + theDir = pwd->pw_dir; + } + } + if (theDir != NULL) + { + suffix = boost::filesystem::path("/Library/Application Support/"); + } + } + + if (theDir != NULL) + { + localDataPath = boost::filesystem::path(theDir); + } + + localDataPath /= suffix; + return localDataPath; +} + +boost::filesystem::path MacOsPath::getGlobalDataPath() const +{ + boost::filesystem::path globalDataPath("/Library/Application Support/"); + return globalDataPath; +} + +boost::filesystem::path MacOsPath::getLocalDataPath() const +{ + return boost::filesystem::path("./data/"); +} + +boost::filesystem::path MacOsPath::getInstallPath() const; +{ + return boost::filesystem::path("./"); +} + } /* namespace Files */ #endif /* defined(macintosh) || defined(Macintosh) || defined(__APPLE__) || defined(__MACH__) */ diff --git a/components/files/macospath.hpp b/components/files/macospath.hpp index 2087a21c7..2194dbb94 100644 --- a/components/files/macospath.hpp +++ b/components/files/macospath.hpp @@ -59,6 +59,8 @@ struct MacOsPath * \return boost::filesystem::path */ boost::filesystem::path getLocalPath() const; + + boost::filesystem::path getInstallPath() const; }; } /* namespace Files */ diff --git a/components/files/windowspath.cpp b/components/files/windowspath.cpp index 7a4bab1df..a54f800d4 100644 --- a/components/files/windowspath.cpp +++ b/components/files/windowspath.cpp @@ -52,6 +52,35 @@ boost::filesystem::path WindowsPath::getLocalPath() const return boost::filesystem::path("./"); } +/** + * FIXME: Someone with Windows system should check this and correct if necessary + */ +boost::filesystem::path WindowsPath::getUserDataPath() const +{ + return getUserConfigPath(); +} + +/** + * FIXME: Someone with Windows system should check this and correct if necessary + */ +boost::filesystem::path WindowsPath::getGlobalDataPath() const +{ + return getGlobalConfigPath(); +} + +/** + * FIXME: Someone with Windows system should check this and correct if necessary + */ +boost::filesystem::path WindowsPath::getLocalDataPath() const +{ + return boost::filesystem::path("./data/"); +} + +boost::filesystem::path WindowsPath::getInstallPath() const; +{ + return boost::filesystem::path("./"); +} + } /* namespace Files */ #endif /* defined(_WIN32) || defined(__WINDOWS__) */ diff --git a/components/files/windowspath.hpp b/components/files/windowspath.hpp index 78b3981bb..95af3ea90 100644 --- a/components/files/windowspath.hpp +++ b/components/files/windowspath.hpp @@ -59,6 +59,30 @@ struct WindowsPath * \return boost::filesystem::path */ boost::filesystem::path getLocalPath() const; + + /** + * \brief Return same path like getUserConfigPath + * + * \return boost::filesystem::path + */ + boost::filesystem::path getUserDataPath() const; + + /** + * \brief Return same path like getGlobalConfigPath + * + * \return boost::filesystem::path + */ + boost::filesystem::path getGlobalDataPath() const; + + /** + * \brief Return runtime data path which is a location where + * an application was started with 'data' suffix. + * + * \return boost::filesystem::path + */ + boost::filesystem::path getLocalDataPath() const; + + boost::filesystem::path getInstallPath() const; }; } /* namespace Files */