From 7c24ae9ac7f48aa4b1ba3a17db2e9b6cd57b7d41 Mon Sep 17 00:00:00 2001 From: Lukasz Gromanowski Date: Sat, 21 Jan 2012 01:14:35 +0100 Subject: [PATCH] Issue #168 - Configuration cleanup - WIP This is "work in progress" commit, it shall not be merged alone, without succeeding commits (it's not fully functional). Signed-off-by: Lukasz Gromanowski --- apps/launcher/datafilespage.cpp | 12 +- apps/launcher/datafilespage.hpp | 6 +- apps/launcher/graphicspage.cpp | 13 +- apps/launcher/graphicspage.hpp | 7 +- apps/launcher/maindialog.cpp | 10 +- apps/launcher/maindialog.hpp | 4 +- apps/openmw/engine.cpp | 6 +- apps/openmw/engine.hpp | 10 +- apps/openmw/main.cpp | 10 +- components/CMakeLists.txt | 6 +- components/cfg/configurationmanager.cpp | 157 ---------------- components/cfg/configurationmanager.hpp | 62 ------- components/files/configurationmanager.cpp | 177 +++++++++++++++++++ components/files/configurationmanager.hpp | 65 +++++++ components/files/{path.hpp => fixedpath.hpp} | 78 +++----- components/files/linuxpath.cpp | 82 ++------- components/files/linuxpath.hpp | 30 +--- components/files/macospath.cpp | 62 +------ components/files/macospath.hpp | 28 +-- components/files/windowspath.cpp | 37 ++-- components/files/windowspath.hpp | 30 +--- 21 files changed, 354 insertions(+), 538 deletions(-) delete mode 100644 components/cfg/configurationmanager.cpp delete mode 100644 components/cfg/configurationmanager.hpp create mode 100644 components/files/configurationmanager.cpp create mode 100644 components/files/configurationmanager.hpp rename components/files/{path.hpp => fixedpath.hpp} (68%) diff --git a/apps/launcher/datafilespage.cpp b/apps/launcher/datafilespage.cpp index c8311846f..8b59f1b81 100644 --- a/apps/launcher/datafilespage.cpp +++ b/apps/launcher/datafilespage.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include "datafilespage.hpp" #include "lineedit.hpp" @@ -26,7 +26,9 @@ bool rowSmallerThan(const QModelIndex &index1, const QModelIndex &index2) return index1.row() <= index2.row(); } -DataFilesPage::DataFilesPage(QWidget *parent) : QWidget(parent) +DataFilesPage::DataFilesPage(Files::ConfigurationManager& cfg, QWidget *parent) + : QWidget(parent) + , mCfgMgr(cfg) { mDataFilesModel = new QStandardItemModel(); // Contains all plugins with masters mPluginsModel = new PluginsModel(); // Contains selectable plugins @@ -236,13 +238,11 @@ void DataFilesPage::setupDataFiles(const QStringList &paths, bool strict) void DataFilesPage::setupConfig() { - Cfg::ConfigurationManager cfg; - - QString config = (cfg.getRuntimeConfigPath() / "launcher.cfg").string().c_str(); + QString config = (mCfgMgr.getLocalPath() / "launcher.cfg").string().c_str(); QFile file(config); if (!file.exists()) { - config = QString::fromStdString((cfg.getLocalConfigPath() / "launcher.cfg").string()); + config = QString::fromStdString((mCfgMgr.getUserPath() / "launcher.cfg").string()); } file.setFileName(config); // Just for displaying information diff --git a/apps/launcher/datafilespage.hpp b/apps/launcher/datafilespage.hpp index 2d0a385a7..db1068abd 100644 --- a/apps/launcher/datafilespage.hpp +++ b/apps/launcher/datafilespage.hpp @@ -19,12 +19,14 @@ class PluginsModel; class PluginsView; class ComboBox; +namespace Files { struct ConfigurationManager; } + class DataFilesPage : public QWidget { Q_OBJECT public: - DataFilesPage(QWidget *parent = 0); + DataFilesPage(Files::ConfigurationManager& cfg, QWidget *parent = 0); ComboBox *mProfilesComboBox; QSettings *mLauncherConfig; @@ -81,6 +83,8 @@ private: QAction *mCheckAction; QAction *mUncheckAction; + Files::ConfigurationManager& mCfgMgr; + void addPlugins(const QModelIndex &index); void removePlugins(const QModelIndex &index); void uncheckPlugins(); diff --git a/apps/launcher/graphicspage.cpp b/apps/launcher/graphicspage.cpp index 92fbf3350..d41a33356 100644 --- a/apps/launcher/graphicspage.cpp +++ b/apps/launcher/graphicspage.cpp @@ -1,8 +1,11 @@ #include #include "graphicspage.hpp" +#include -GraphicsPage::GraphicsPage(QWidget *parent) : QWidget(parent) +GraphicsPage::GraphicsPage(Files::ConfigurationManager& cfg, QWidget *parent) + : QWidget(parent) + , mCfgMgr(cfg) { QGroupBox *rendererGroup = new QGroupBox(tr("Renderer"), this); @@ -147,21 +150,21 @@ void GraphicsPage::createPages() void GraphicsPage::setupConfig() { - QString ogreCfg = mCfg.getOgreConfigPath().string().c_str(); + QString ogreCfg = mCfgMgr.getOgreConfigPath().string().c_str(); QFile file(ogreCfg); mOgreConfig = new QSettings(ogreCfg, QSettings::IniFormat); } void GraphicsPage::setupOgre() { - QString pluginCfg = mCfg.getPluginsConfigPath().string().c_str(); + QString pluginCfg = mCfgMgr.getPluginsConfigPath().string().c_str(); QFile file(pluginCfg); // Create a log manager so we can surpress debug text to stdout/stderr Ogre::LogManager* logMgr = OGRE_NEW Ogre::LogManager; - logMgr->createLog((mCfg.getLogPath().string() + "/launcherOgre.log"), true, false, false); + logMgr->createLog((mCfgMgr.getLogPath().string() + "/launcherOgre.log"), true, false, false); - QString ogreCfg = QString::fromStdString(mCfg.getOgreConfigPath().string()); + QString ogreCfg = QString::fromStdString(mCfgMgr.getOgreConfigPath().string()); file.setFileName(ogreCfg); //we need to check that the path to the configuration file exists before we diff --git a/apps/launcher/graphicspage.hpp b/apps/launcher/graphicspage.hpp index 5d50cfc61..ffd7a41b8 100644 --- a/apps/launcher/graphicspage.hpp +++ b/apps/launcher/graphicspage.hpp @@ -7,19 +7,20 @@ #include #include #include -#include class QComboBox; class QCheckBox; class QStackedWidget; class QSettings; +namespace Files { struct ConfigurationManager; } + class GraphicsPage : public QWidget { Q_OBJECT public: - GraphicsPage(QWidget *parent = 0); + GraphicsPage(Files::ConfigurationManager& cfg, QWidget *parent = 0); QSettings *mOgreConfig; @@ -29,7 +30,7 @@ public slots: void rendererChanged(const QString &renderer); private: - Cfg::ConfigurationManager mCfg; + Files::ConfigurationManager& mCfgMgr; Ogre::Root *mOgre; Ogre::RenderSystem *mSelectedRenderSystem; Ogre::RenderSystem *mOpenGLRenderSystem; diff --git a/apps/launcher/maindialog.cpp b/apps/launcher/maindialog.cpp index 4ec8b309c..3bef0b6f9 100644 --- a/apps/launcher/maindialog.cpp +++ b/apps/launcher/maindialog.cpp @@ -152,20 +152,20 @@ QStringList MainDialog::readConfig(const QString &fileName) void MainDialog::createPages() { mPlayPage = new PlayPage(this); - mGraphicsPage = new GraphicsPage(this); - mDataFilesPage = new DataFilesPage(this); + mGraphicsPage = new GraphicsPage(mCfgMgr, this); + mDataFilesPage = new DataFilesPage(mCfgMgr, this); // Retrieve all data entries from the configs QStringList dataDirs; // Global location - QFile file(QString::fromStdString((mCfg.getGlobalConfigPath()/"openmw.cfg").string())); + QFile file(QString::fromStdString((mCfgMgr.getGlobalPath()/"openmw.cfg").string())); if (file.exists()) { dataDirs = readConfig(file.fileName()); } // User location - file.setFileName(QString::fromStdString((mCfg.getLocalConfigPath()/"openmw.cfg").string())); + file.setFileName(QString::fromStdString((mCfgMgr.getUserPath()/"openmw.cfg").string())); if (file.exists()) { dataDirs = readConfig(file.fileName()); } @@ -328,7 +328,7 @@ void MainDialog::writeConfig() dataFiles.append(mDataFilesPage->checkedPlugins()); // Open the config as a QFile - QFile file(QString::fromStdString((mCfg.getLocalConfigPath()/"openmw.cfg").string())); + QFile file(QString::fromStdString((mCfgMgr.getUserPath()/"openmw.cfg").string())); if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) { // File cannot be opened or created diff --git a/apps/launcher/maindialog.hpp b/apps/launcher/maindialog.hpp index 047050902..718fde4f7 100644 --- a/apps/launcher/maindialog.hpp +++ b/apps/launcher/maindialog.hpp @@ -3,7 +3,7 @@ #include -#include +#include class QListWidget; class QListWidgetItem; @@ -47,7 +47,7 @@ private: QStringList mDataDirs; bool mStrict; - Cfg::ConfigurationManager mCfg; + Files::ConfigurationManager mCfgMgr; }; #endif diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index 7fa98f8e2..bbc68b8e2 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -17,7 +17,9 @@ #include #include #include -#include +#include +#include + #include #include "mwinput/inputmanager.hpp" @@ -154,7 +156,7 @@ bool OMW::Engine::frameRenderingQueued (const Ogre::FrameEvent& evt) return true; } -OMW::Engine::Engine(Cfg::ConfigurationManager& configurationManager) +OMW::Engine::Engine(Files::ConfigurationManager& configurationManager) : mOgre (0) , mPhysicEngine (0) , mShowFPS (false) diff --git a/apps/openmw/engine.hpp b/apps/openmw/engine.hpp index 443f790a4..97079f5a5 100644 --- a/apps/openmw/engine.hpp +++ b/apps/openmw/engine.hpp @@ -11,7 +11,6 @@ #include #include -#include #include "mwworld/environment.hpp" #include "mwworld/ptr.hpp" @@ -54,6 +53,11 @@ namespace OEngine } } +namespace Files +{ + struct ConfigurationManager; +} + namespace OMW { /// \brief Main engine class, that brings together all the components of OpenMW @@ -104,7 +108,7 @@ namespace OMW virtual bool frameRenderingQueued (const Ogre::FrameEvent& evt); public: - Engine(Cfg::ConfigurationManager& configurationManager); + Engine(Files::ConfigurationManager& configurationManager); virtual ~Engine(); /// Enable strict filesystem mode (do not fold case) @@ -159,7 +163,7 @@ namespace OMW void setEncoding(const std::string& encoding); private: - Cfg::ConfigurationManager& mCfgMgr; + Files::ConfigurationManager& mCfgMgr; }; } diff --git a/apps/openmw/main.cpp b/apps/openmw/main.cpp index 933d1c48a..0f66925d3 100644 --- a/apps/openmw/main.cpp +++ b/apps/openmw/main.cpp @@ -6,9 +6,9 @@ #include #include -#include +#include #include -#include +#include #include "engine.hpp" @@ -46,7 +46,7 @@ using namespace std; * \retval true - Everything goes OK * \retval false - Error */ -bool parseOptions (int argc, char** argv, OMW::Engine& engine, Cfg::ConfigurationManager& cfgMgr) +bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::ConfigurationManager& cfgMgr) { // Create a local alias for brevity namespace bpo = boost::program_options; @@ -166,7 +166,7 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Cfg::Configuratio if (dataDirs.empty()) { - dataDirs.push_back(cfgMgr.getLocalDataPath()); + dataDirs.push_back(cfgMgr.getDataPath("local:data?")); } engine.setDataDirs(dataDirs); @@ -220,7 +220,7 @@ int main(int argc, char**argv) try { - Cfg::ConfigurationManager cfgMgr; + Files::ConfigurationManager cfgMgr; OMW::Engine engine(cfgMgr); if (parseOptions(argc, argv, engine, cfgMgr)) diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index 75b8aff8c..3f7d6d49a 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -6,10 +6,6 @@ add_component_dir (bsa bsa_archive bsa_file ) -add_component_dir (cfg - configurationmanager - ) - add_component_dir (nif controlled effect nif_types record controller extra node record_ptr data nif_file property ) @@ -47,7 +43,7 @@ add_component_dir (misc ) add_component_dir (files - linuxpath windowspath macospath path multidircollection collections fileops + linuxpath windowspath macospath fixedpath multidircollection collections fileops configurationmanager ) add_component_dir (compiler diff --git a/components/cfg/configurationmanager.cpp b/components/cfg/configurationmanager.cpp deleted file mode 100644 index 0998debee..000000000 --- a/components/cfg/configurationmanager.cpp +++ /dev/null @@ -1,157 +0,0 @@ -#include "configurationmanager.hpp" - -#include -#include -#include - -namespace Cfg -{ - -static const char* const openmwCfgFile = "openmw.cfg"; -static const char* const ogreCfgFile = "ogre.cfg"; -static const char* const pluginsCfgFile = "plugins.cfg"; - - -ConfigurationManager::ConfigurationManager() - : mPath("openmw") -{ - /** - * According to task #168 plugins.cfg file shall be located in global - * configuration path or in runtime configuration path. - */ - mPluginsCfgPath = mPath.getGlobalConfigPath() / pluginsCfgFile; - if (!boost::filesystem::is_regular_file(mPluginsCfgPath)) - { - mPluginsCfgPath = mPath.getRuntimeConfigPath() / pluginsCfgFile; - if (!boost::filesystem::is_regular_file(mPluginsCfgPath)) - { - std::cerr << "Failed to find " << pluginsCfgFile << " file!" << std::endl; - mPluginsCfgPath.clear(); - } - } - - /** - * According to task #168 ogre.cfg file shall be located only - * in user configuration path. - */ - mOgreCfgPath = mPath.getLocalConfigPath() / ogreCfgFile; - - mLogPath = mPath.getLocalConfigPath(); -} - -ConfigurationManager::~ConfigurationManager() -{ -} - -void ConfigurationManager::readConfiguration(boost::program_options::variables_map& variables, - boost::program_options::options_description& description) -{ - loadConfig(mPath.getLocalConfigPath(), variables, description); - boost::program_options::notify(variables); - loadConfig(mPath.getRuntimeConfigPath(), variables, description); - boost::program_options::notify(variables); - loadConfig(mPath.getGlobalConfigPath(), variables, description); - boost::program_options::notify(variables); -} - -void ConfigurationManager::loadConfig(const boost::filesystem::path& path, - boost::program_options::variables_map& variables, - boost::program_options::options_description& description) -{ - boost::filesystem::path cfgFile(path); - cfgFile /= std::string(openmwCfgFile); - if (boost::filesystem::is_regular_file(cfgFile)) - { - std::cout << "Loading config file: " << cfgFile.string() << "... "; - - std::ifstream configFileStream(cfgFile.string().c_str()); - if (configFileStream.is_open()) - { - boost::program_options::store(boost::program_options::parse_config_file( - configFileStream, description), variables); - - std::cout << "done." << std::endl; - } - else - { - std::cout << "failed." << std::endl; - } - } -} - -const boost::filesystem::path& ConfigurationManager::getGlobalConfigPath() const -{ - return mPath.getGlobalConfigPath(); -} - -void ConfigurationManager::setGlobalConfigPath(const boost::filesystem::path& newPath) -{ - mPath.setGlobalConfigPath(newPath); -} - -const boost::filesystem::path& ConfigurationManager::getLocalConfigPath() const -{ - return mPath.getLocalConfigPath(); -} - -void ConfigurationManager::setLocalConfigPath(const boost::filesystem::path& newPath) -{ - mPath.setLocalConfigPath(newPath); -} - -const boost::filesystem::path& ConfigurationManager::getRuntimeConfigPath() const -{ - return mPath.getRuntimeConfigPath(); -} - -void ConfigurationManager::setRuntimeConfigPath(const boost::filesystem::path& newPath) -{ - mPath.setRuntimeConfigPath(newPath); -} - -const boost::filesystem::path& ConfigurationManager::getGlobalDataPath() const -{ - return mPath.getGlobalDataPath(); -} - -void ConfigurationManager::setGlobalDataPath(const boost::filesystem::path& newPath) -{ - mPath.setGlobalDataPath(newPath); -} - -const boost::filesystem::path& ConfigurationManager::getLocalDataPath() const -{ - return mPath.getLocalDataPath(); -} - -void ConfigurationManager::setLocalDataPath(const boost::filesystem::path& newPath) -{ - mPath.setLocalDataPath(newPath); -} - -const boost::filesystem::path& ConfigurationManager::getRuntimeDataPath() const -{ - return mPath.getRuntimeDataPath(); -} - -void ConfigurationManager::setRuntimeDataPath(const boost::filesystem::path& newPath) -{ - mPath.setRuntimeDataPath(newPath); -} - -const boost::filesystem::path& ConfigurationManager::getOgreConfigPath() const -{ - return mOgreCfgPath; -} - -const boost::filesystem::path& ConfigurationManager::getPluginsConfigPath() const -{ - return mPluginsCfgPath; -} - -const boost::filesystem::path& ConfigurationManager::getLogPath() const -{ - return mLogPath; -} - -} /* namespace Cfg */ diff --git a/components/cfg/configurationmanager.hpp b/components/cfg/configurationmanager.hpp deleted file mode 100644 index 7f13d0914..000000000 --- a/components/cfg/configurationmanager.hpp +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef COMPONENTS_CFG_CONFIGURATIONMANAGER_HPP -#define COMPONENTS_CFG_CONFIGURATIONMANAGER_HPP - -#include -#include - -#include - -/** - * \namespace Cfg - */ -namespace Cfg -{ - -/** - * \struct ConfigurationManager - */ -struct ConfigurationManager -{ - ConfigurationManager(); - virtual ~ConfigurationManager(); - - void readConfiguration(boost::program_options::variables_map& variables, - boost::program_options::options_description& description); - - const boost::filesystem::path& getGlobalConfigPath() const; - void setGlobalConfigPath(const boost::filesystem::path& newPath); - - const boost::filesystem::path& getLocalConfigPath() const; - void setLocalConfigPath(const boost::filesystem::path& newPath); - - const boost::filesystem::path& getRuntimeConfigPath() const; - void setRuntimeConfigPath(const boost::filesystem::path& newPath); - - const boost::filesystem::path& getGlobalDataPath() const; - void setGlobalDataPath(const boost::filesystem::path& newPath); - - const boost::filesystem::path& getLocalDataPath() const; - void setLocalDataPath(const boost::filesystem::path& newPath); - - const boost::filesystem::path& getRuntimeDataPath() const; - void setRuntimeDataPath(const boost::filesystem::path& newPath); - - const boost::filesystem::path& getOgreConfigPath() const; - const boost::filesystem::path& getPluginsConfigPath() const; - const boost::filesystem::path& getLogPath() const; - - private: - void loadConfig(const boost::filesystem::path& path, - boost::program_options::variables_map& variables, - boost::program_options::options_description& description); - - Files::Path<> mPath; - - boost::filesystem::path mOgreCfgPath; - boost::filesystem::path mPluginsCfgPath; - boost::filesystem::path mLogPath; -}; - -} /* namespace Cfg */ - -#endif /* COMPONENTS_CFG_CONFIGURATIONMANAGER_HPP */ diff --git a/components/files/configurationmanager.cpp b/components/files/configurationmanager.cpp new file mode 100644 index 000000000..224fd2649 --- /dev/null +++ b/components/files/configurationmanager.cpp @@ -0,0 +1,177 @@ +#include "configurationmanager.hpp" + +#include +#include +#include +#include + +namespace Files +{ + +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?"; + +ConfigurationManager::ConfigurationManager() + : mFixedPath("openmw") +{ + setupTokensMapping(); + + /** + * According to task #168 plugins.cfg file shall be located in global + * configuration path or in runtime configuration path. + */ + mPluginsCfgPath = mFixedPath.getGlobalPath() / pluginsCfgFile; + if (!boost::filesystem::is_regular_file(mPluginsCfgPath)) + { + mPluginsCfgPath = mFixedPath.getLocalPath() / pluginsCfgFile; + if (!boost::filesystem::is_regular_file(mPluginsCfgPath)) + { + std::cerr << "Failed to find " << pluginsCfgFile << " file!" << std::endl; + mPluginsCfgPath.clear(); + } + } + + /** + * According to task #168 ogre.cfg file shall be located only + * in user configuration path. + */ + mOgreCfgPath = mFixedPath.getUserPath() / ogreCfgFile; + + /** + * FIXME: Logs shoudn't be stored in the same dir where configuration is placed. + */ + mLogPath = mFixedPath.getUserPath(); +} + +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)); +} + +void ConfigurationManager::readConfiguration(boost::program_options::variables_map& variables, + boost::program_options::options_description& description) +{ + loadConfig(mFixedPath.getUserPath(), variables, description); + boost::program_options::notify(variables); + loadConfig(mFixedPath.getLocalPath(), variables, description); + boost::program_options::notify(variables); + loadConfig(mFixedPath.getGlobalPath(), variables, description); + boost::program_options::notify(variables); +} + +void ConfigurationManager::loadConfig(const boost::filesystem::path& path, + boost::program_options::variables_map& variables, + boost::program_options::options_description& description) +{ + boost::filesystem::path cfgFile(path); + cfgFile /= std::string(openmwCfgFile); + if (boost::filesystem::is_regular_file(cfgFile)) + { + std::cout << "Loading config file: " << cfgFile.string() << "... "; + + std::ifstream configFileStream(cfgFile.string().c_str()); + if (configFileStream.is_open()) + { + boost::program_options::store(boost::program_options::parse_config_file( + configFileStream, description), variables); + + std::cout << "done." << std::endl; + } + else + { + std::cout << "failed." << std::endl; + } + } +} + +const boost::filesystem::path& ConfigurationManager::getGlobalPath() const +{ + return mFixedPath.getGlobalPath(); +} + +const boost::filesystem::path& ConfigurationManager::getUserPath() const +{ + return mFixedPath.getUserPath(); +} + +const boost::filesystem::path& ConfigurationManager::getLocalPath() const +{ + return mFixedPath.getLocalPath(); +} + +const boost::filesystem::path& ConfigurationManager::getDataPath(const std::string& type) const +{ + TokensMappingContainer::const_iterator it = mTokensMapping.find(type); + if (it != mTokensMapping.end()) + { + return ((this)->*(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; +} + +const boost::filesystem::path& ConfigurationManager::getPluginsConfigPath() const +{ + return mPluginsCfgPath; +} + +const boost::filesystem::path& ConfigurationManager::getLogPath() const +{ + return mLogPath; +} + +} /* namespace Cfg */ diff --git a/components/files/configurationmanager.hpp b/components/files/configurationmanager.hpp new file mode 100644 index 000000000..3004b6281 --- /dev/null +++ b/components/files/configurationmanager.hpp @@ -0,0 +1,65 @@ +#ifndef COMPONENTS_FILES_CONFIGURATIONMANAGER_HPP +#define COMPONENTS_FILES_CONFIGURATIONMANAGER_HPP + +#include + +#include +#include + +#include + +/** + * \namespace Files + */ +namespace Files +{ + +/** + * \struct ConfigurationManager + */ +struct ConfigurationManager +{ + ConfigurationManager(); + virtual ~ConfigurationManager(); + + void readConfiguration(boost::program_options::variables_map& variables, + boost::program_options::options_description& description); + + /**< Fixed paths */ + const boost::filesystem::path& getGlobalPath() const; + const boost::filesystem::path& getUserPath() const; + const boost::filesystem::path& getLocalPath() const ; + + const boost::filesystem::path& getDataPath(const std::string& type) const; + + const boost::filesystem::path& getOgreConfigPath() const; + const boost::filesystem::path& getPluginsConfigPath() const; + const boost::filesystem::path& getLogPath() const; + + private: + typedef const boost::filesystem::path& (ConfigurationManager::*path_type_f)() const; + typedef std::tr1::unordered_map TokensMappingContainer; + + void loadConfig(const boost::filesystem::path& path, + boost::program_options::variables_map& variables, + boost::program_options::options_description& description); + + 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; + + boost::filesystem::path mOgreCfgPath; + boost::filesystem::path mPluginsCfgPath; + boost::filesystem::path mLogPath; + + TokensMappingContainer mTokensMapping; +}; + +} /* namespace Cfg */ + +#endif /* COMPONENTS_FILES_CONFIGURATIONMANAGER_HPP */ diff --git a/components/files/path.hpp b/components/files/fixedpath.hpp similarity index 68% rename from components/files/path.hpp rename to components/files/fixedpath.hpp index 78de9c585..4e2b20e3c 100644 --- a/components/files/path.hpp +++ b/components/files/fixedpath.hpp @@ -18,10 +18,10 @@ * along with this program. If not, see . */ -/** \file components/files/path.hpp */ +/** \file components/files/fixedpath.hpp */ -#ifndef COMPONENTS_FILES_PATH_HPP -#define COMPONENTS_FILES_PATH_HPP +#ifndef COMPONENTS_FILES_FIXEDPATH_HPP +#define COMPONENTS_FILES_FIXEDPATH_HPP #include #include @@ -59,7 +59,7 @@ template < class P = TargetPathType > -struct Path +struct FixedPath { typedef P PathType; @@ -68,21 +68,21 @@ struct Path * * \param [in] application_name - Name of the application */ - Path(const std::string& application_name) + FixedPath(const std::string& application_name) : mPath() - , mLocalConfigPath(mPath.getLocalConfigPath()) - , mGlobalConfigPath(mPath.getGlobalConfigPath()) - , mRuntimeConfigPath(mPath.getRuntimeConfigPath()) - , mLocalDataPath(mPath.getLocalDataPath()) - , mGlobalDataPath(mPath.getGlobalDataPath()) - , mRuntimeDataPath(mPath.getRuntimeDataPath()) + , mUserPath(mPath.getUserPath()) + , mGlobalPath(mPath.getGlobalPath()) + , mLocalPath(mPath.getLocalPath()) + , mLocalDataPath() + , mGlobalDataPath() + , mRuntimeDataPath() { if (!application_name.empty()) { boost::filesystem::path suffix(application_name + std::string("/")); - mLocalConfigPath /= suffix; - mGlobalConfigPath /= suffix; + mUserPath /= suffix; + mGlobalPath /= suffix; mLocalDataPath /= suffix; mGlobalDataPath /= suffix; @@ -94,19 +94,9 @@ struct Path * * \return boost::filesystem::path */ - const boost::filesystem::path& getLocalConfigPath() const + const boost::filesystem::path& getUserPath() const { - return mLocalConfigPath; - } - - /** - * \brief Sets new local configuration path. - * - * \param [in] path - New path - */ - void setLocalConfigPath(const boost::filesystem::path& path) - { - mLocalConfigPath = path; + return mUserPath; } /** @@ -114,19 +104,9 @@ struct Path * * \return boost::filesystem::path */ - const boost::filesystem::path& getGlobalConfigPath() const + const boost::filesystem::path& getGlobalPath() const { - return mGlobalConfigPath; - } - - /** - * \brief Sets new global configuration path. - * - * \param [in] path - New path - */ - void setGlobalConfigPath(const boost::filesystem::path& path) - { - mGlobalConfigPath = path; + return mGlobalPath; } /** @@ -134,19 +114,9 @@ struct Path * * \return boost::filesystem::path */ - const boost::filesystem::path& getRuntimeConfigPath() const - { - return mRuntimeConfigPath; - } - - /** - * \brief Sets new runtime configuration path. - * - * \param [in] path - New path - */ - void setRuntimeConfigPath(const boost::filesystem::path& path) + const boost::filesystem::path& getLocalPath() const { - mRuntimeConfigPath = path; + return mLocalPath; } /** @@ -212,11 +182,9 @@ struct Path private: PathType mPath; - boost::filesystem::path mLocalConfigPath; /**< User local path to the configuration files */ - boost::filesystem::path mGlobalConfigPath; /**< Global path to the configuration files */ - boost::filesystem::path mRuntimeConfigPath; /**< Runtime path to the configuration files. - By default it is the same directory where - application was run */ + boost::filesystem::path mUserPath; /**< User path */ + 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 */ @@ -229,4 +197,4 @@ struct Path } /* namespace Files */ -#endif /* COMPONENTS_FILES_PATH_HPP */ +#endif /* COMPONENTS_FILES_FIXEDPATH_HPP */ diff --git a/components/files/linuxpath.cpp b/components/files/linuxpath.cpp index 27581a1e2..11ddc3104 100644 --- a/components/files/linuxpath.cpp +++ b/components/files/linuxpath.cpp @@ -35,9 +35,9 @@ namespace Files { -boost::filesystem::path LinuxPath::getLocalConfigPath() const +boost::filesystem::path LinuxPath::getUserPath() const { - boost::filesystem::path localConfigPath("."); + boost::filesystem::path userPath("."); boost::filesystem::path suffix("/"); const char* theDir = getenv("OPENMW_CONFIG"); @@ -63,17 +63,17 @@ boost::filesystem::path LinuxPath::getLocalConfigPath() const } if (theDir != NULL) { - localConfigPath = boost::filesystem::path(theDir); + userPath = boost::filesystem::path(theDir); } - localConfigPath /= suffix; + userPath /= suffix; - return localConfigPath; + return userPath; } -boost::filesystem::path LinuxPath::getGlobalConfigPath() const +boost::filesystem::path LinuxPath::getGlobalPath() const { - boost::filesystem::path globalConfigPath("/etc/xdg/"); + boost::filesystem::path globalPath("/etc/xdg/"); char* theDir = getenv("XDG_CONFIG_DIRS"); if (theDir != NULL) @@ -82,79 +82,19 @@ boost::filesystem::path LinuxPath::getGlobalConfigPath() const char* ptr = strtok(theDir, ":"); if (ptr != NULL) { - globalConfigPath = boost::filesystem::path(ptr); - globalConfigPath /= boost::filesystem::path("/"); + globalPath = boost::filesystem::path(ptr); + globalPath /= boost::filesystem::path("/"); } } - return globalConfigPath; + return globalPath; } -boost::filesystem::path LinuxPath::getRuntimeConfigPath() const +boost::filesystem::path LinuxPath::getLocalPath() const { return boost::filesystem::path("./"); } -boost::filesystem::path LinuxPath::getLocalDataPath() 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::getRuntimeDataPath() const -{ - return boost::filesystem::path("./data/"); -} - - } /* namespace Files */ #endif /* defined(__linux__) || defined(__FreeBSD__) */ diff --git a/components/files/linuxpath.hpp b/components/files/linuxpath.hpp index 53f7a73b4..af92326be 100644 --- a/components/files/linuxpath.hpp +++ b/components/files/linuxpath.hpp @@ -39,18 +39,18 @@ namespace Files struct LinuxPath { /** - * \brief Return path to the local configuration directory. + * \brief Return path to the user directory. * * \return boost::filesystem::path */ - boost::filesystem::path getLocalConfigPath() const; + boost::filesystem::path getUserPath() const; /** * \brief Return path to the global (system) configuration directory. * * \return boost::filesystem::path */ - boost::filesystem::path getGlobalConfigPath() const; + boost::filesystem::path getGlobalPath() const; /** * \brief Return path to the runtime configuration directory which is the @@ -58,29 +58,7 @@ struct LinuxPath * * \return boost::filesystem::path */ - boost::filesystem::path getRuntimeConfigPath() const; - - /** - * \brief Return path to the local data directory. - * - * \return boost::filesystem::path - */ - boost::filesystem::path getLocalDataPath() const; - - /** - * \brief Return path to the global (system) data directory. - * - * \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 getRuntimeDataPath() const; + boost::filesystem::path getLocalPath() const; }; } /* namespace Files */ diff --git a/components/files/macospath.cpp b/components/files/macospath.cpp index 46e775030..1ffb44399 100644 --- a/components/files/macospath.cpp +++ b/components/files/macospath.cpp @@ -34,9 +34,9 @@ namespace Files { -boost::filesystem::path MacOsPath::getLocalConfigPath() const +boost::filesystem::path MacOsPath::getUserPath() const { - boost::filesystem::path localConfigPath("."); + boost::filesystem::path userPath("."); boost::filesystem::path suffix("/"); const char* theDir = getenv("HOME"); @@ -50,69 +50,25 @@ boost::filesystem::path MacOsPath::getLocalConfigPath() const } if (theDir != NULL) { - localConfigPath = boost::filesystem::path(theDir) / "Library/Preferences/"; + userPath = boost::filesystem::path(theDir) / "Library/Preferences/"; } - localConfigPath /= suffix; + userPath /= suffix; - return localConfigPath; + return userPath; } -boost::filesystem::path MacOsPath::getGlobalConfigPath() const +boost::filesystem::path MacOsPath::getGlobalPath() const { - boost::filesystem::path globalConfigPath("/Library/Preferences/"); - return globalConfigPath; + boost::filesystem::path globalPath("/Library/Preferences/"); + return globalPath; } -boost::filesystem::path MacOsPath::getRuntimeConfigPath() const +boost::filesystem::path MacOsPath::getLocalPath() const { return boost::filesystem::path("./"); } -boost::filesystem::path MacOsPath::getLocalDataPath() 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::getRuntimeDataPath() const -{ - return boost::filesystem::path("./data/"); -} - - } /* namespace Files */ #endif /* defined(macintosh) || defined(Macintosh) || defined(__APPLE__) || defined(__MACH__) */ diff --git a/components/files/macospath.hpp b/components/files/macospath.hpp index 26f2c907f..2087a21c7 100644 --- a/components/files/macospath.hpp +++ b/components/files/macospath.hpp @@ -43,14 +43,14 @@ struct MacOsPath * * \return boost::filesystem::path */ - boost::filesystem::path getLocalConfigPath() const; + boost::filesystem::path getUserPath() const; /** * \brief Return path to the global (system) configuration directory. * * \return boost::filesystem::path */ - boost::filesystem::path getGlobalConfigPath() const; + boost::filesystem::path getGlobalPath() const; /** * \brief Return path to the runtime configuration directory which is the @@ -58,29 +58,7 @@ struct MacOsPath * * \return boost::filesystem::path */ - boost::filesystem::path getRuntimeConfigPath() const; - - /** - * \brief Return path to the local data directory. - * - * \return boost::filesystem::path - */ - boost::filesystem::path getLocalDataPath() const; - - /** - * \brief Return path to the global (system) data directory. - * - * \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 getRuntimeDataPath() const; + boost::filesystem::path getLocalPath() const; }; } /* namespace Files */ diff --git a/components/files/windowspath.cpp b/components/files/windowspath.cpp index f42f149c1..7a4bab1df 100644 --- a/components/files/windowspath.cpp +++ b/components/files/windowspath.cpp @@ -10,9 +10,9 @@ namespace Files { -boost::filesystem::path WindowsPath::getLocalConfigPath() const +boost::filesystem::path WindowsPath::getUserPath() const { - boost::filesystem::path localConfigPath("."); + boost::filesystem::path userPath("."); boost::filesystem::path suffix("/"); TCHAR path[MAX_PATH]; @@ -21,17 +21,17 @@ boost::filesystem::path WindowsPath::getLocalConfigPath() const if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, 0, path))) { PathAppend(path, TEXT("My Games")); - localConfigPath = boost::filesystem::path(path); + userPath = boost::filesystem::path(path); } - localConfigPath /= suffix; + userPath /= suffix; - return localConfigPath; + return userPath; } -boost::filesystem::path WindowsPath::getGlobalConfigPath() const +boost::filesystem::path WindowsPath::getGlobalPath() const { - boost::filesystem::path globalConfigPath("."); + boost::filesystem::path globalPath("."); boost::filesystem::path suffix("/"); TCHAR path[MAX_PATH]; @@ -39,34 +39,19 @@ boost::filesystem::path WindowsPath::getGlobalConfigPath() const if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES | CSIDL_FLAG_CREATE, NULL, 0, path))) { - globalConfigPath = boost::filesystem::path(path); + globalPath = boost::filesystem::path(path); } - globalConfigPath /= suffix; + globalPath /= suffix; - return globalConfigPath; + return globalPath; } -boost::filesystem::path WindowsPath::getRuntimeConfigPath() const +boost::filesystem::path WindowsPath::getLocalPath() const { return boost::filesystem::path("./"); } -boost::filesystem::path WindowsPath::getLocalDataPath() const -{ - return getLocalConfigPath(); -} - -boost::filesystem::path WindowsPath::getGlobalDataPath() const -{ - return getGlobalConfigPath(); -} - -boost::filesystem::path WindowsPath::getRuntimeDataPath() const -{ - return boost::filesystem::path("./data/"); -} - } /* namespace Files */ #endif /* defined(_WIN32) || defined(__WINDOWS__) */ diff --git a/components/files/windowspath.hpp b/components/files/windowspath.hpp index 47dfc08d8..78b3981bb 100644 --- a/components/files/windowspath.hpp +++ b/components/files/windowspath.hpp @@ -43,44 +43,22 @@ struct WindowsPath * * \return boost::filesystem::path */ - boost::filesystem::path getLocalConfigPath() const; + boost::filesystem::path getUserPath() const; /** * \brief Returns "X:\Program Files\" * * \return boost::filesystem::path */ - boost::filesystem::path getGlobalConfigPath() const; + boost::filesystem::path getGlobalPath() const; /** - * \brief Return runtime configuration path which is a location where + * \brief Return local path which is a location where * an application was started * * \return boost::filesystem::path */ - boost::filesystem::path getRuntimeConfigPath() const; - - /** - * \brief Return same path like getLocalConfigPath - * - * \return boost::filesystem::path - */ - boost::filesystem::path getLocalDataPath() 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 getRuntimeDataPath() const; + boost::filesystem::path getLocalPath() const; }; } /* namespace Files */