Skip to content

Commit

Permalink
XDG compliant paths
Browse files Browse the repository at this point in the history
  • Loading branch information
scrawl committed Dec 26, 2013
1 parent aef0fd1 commit 33389b9
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 117 deletions.
10 changes: 5 additions & 5 deletions apps/launcher/maindialog.cpp
Expand Up @@ -219,7 +219,7 @@ bool Launcher::MainDialog::showFirstRunDialog()
}

// Create the file if it doesn't already exist, else the importer will fail
QString path = QString::fromStdString(mCfgMgr.getUserPath().string()) + QString("openmw.cfg");
QString path = QString::fromStdString(mCfgMgr.getUserConfigPath().string()) + QString("openmw.cfg");
QFile file(path);

if (!file.exists()) {
Expand Down Expand Up @@ -334,7 +334,7 @@ bool Launcher::MainDialog::setupLauncherSettings()
{
mLauncherSettings.setMultiValueEnabled(true);

QString userPath = QString::fromStdString(mCfgMgr.getUserPath().string());
QString userPath = QString::fromStdString(mCfgMgr.getUserConfigPath().string());

QStringList paths;
paths.append(QString("launcher.cfg"));
Expand Down Expand Up @@ -440,7 +440,7 @@ bool Launcher::expansions(Launcher::UnshieldThread& cd)

bool Launcher::MainDialog::setupGameSettings()
{
QString userPath = QString::fromStdString(mCfgMgr.getUserPath().string());
QString userPath = QString::fromStdString(mCfgMgr.getUserConfigPath().string());
QString globalPath = QString::fromStdString(mCfgMgr.getGlobalPath().string());

// Load the user config file first, separately
Expand Down Expand Up @@ -591,7 +591,7 @@ bool Launcher::MainDialog::setupGraphicsSettings()
{
mGraphicsSettings.setMultiValueEnabled(false);

QString userPath = QString::fromStdString(mCfgMgr.getUserPath().string());
QString userPath = QString::fromStdString(mCfgMgr.getUserConfigPath().string());
QString globalPath = QString::fromStdString(mCfgMgr.getGlobalPath().string());

QFile localDefault(QString("settings-default.cfg"));
Expand Down Expand Up @@ -678,7 +678,7 @@ bool Launcher::MainDialog::writeSettings()
mGraphicsPage->saveSettings();
mDataFilesPage->saveSettings();

QString userPath = QString::fromStdString(mCfgMgr.getUserPath().string());
QString userPath = QString::fromStdString(mCfgMgr.getUserConfigPath().string());
QDir dir(userPath);

if (!dir.exists()) {
Expand Down
4 changes: 0 additions & 4 deletions apps/opencs/editor.cpp
Expand Up @@ -86,10 +86,6 @@ void CS::Editor::setupDataFiles()
return;
}

// Set the charset for reading the esm/esp files
// QString encoding = QString::fromStdString(variables["encoding"].as<std::string>());
//mFileDialog.setEncoding(encoding);

dataDirs.insert (dataDirs.end(), dataLocal.begin(), dataLocal.end());

mDocumentManager.setResourceDir (variables["resources"].as<std::string>());
Expand Down
4 changes: 2 additions & 2 deletions apps/opencs/model/doc/document.cpp
Expand Up @@ -2221,7 +2221,7 @@ void CSMDoc::Document::createBase()

CSMDoc::Document::Document (const Files::ConfigurationManager& configuration, const std::vector< boost::filesystem::path >& files, const boost::filesystem::path& savePath, const boost::filesystem::path& resDir, bool new_)
: mSavePath (savePath), mContentFiles (files), mTools (mData), mResDir(resDir),
mProjectPath ((configuration.getUserPath() / "projects") /
mProjectPath ((configuration.getUserDataPath() / "projects") /
(savePath.filename().string() + ".project")),
mSaving (*this, mProjectPath)
{
Expand Down Expand Up @@ -2254,7 +2254,7 @@ CSMDoc::Document::Document (const Files::ConfigurationManager& configuration, co
}
else
{
boost::filesystem::path locCustomFiltersPath (configuration.getUserPath());
boost::filesystem::path locCustomFiltersPath (configuration.getUserDataPath());
locCustomFiltersPath /= "defaultfilters";

if (boost::filesystem::exists(locCustomFiltersPath))
Expand Down
4 changes: 2 additions & 2 deletions apps/opencs/model/doc/documentmanager.cpp
Expand Up @@ -15,7 +15,7 @@
CSMDoc::DocumentManager::DocumentManager (const Files::ConfigurationManager& configuration)
: mConfiguration (configuration)
{
boost::filesystem::path projectPath = configuration.getUserPath() / "projects";
boost::filesystem::path projectPath = configuration.getUserDataPath() / "projects";

if (!boost::filesystem::is_directory (projectPath))
boost::filesystem::create_directories (projectPath);
Expand Down Expand Up @@ -53,4 +53,4 @@ bool CSMDoc::DocumentManager::removeDocument (Document *document)
void CSMDoc::DocumentManager::setResourceDir (const boost::filesystem::path& parResDir)
{
mResDir = boost::filesystem::system_complete(parResDir);
}
}
2 changes: 1 addition & 1 deletion apps/opencs/model/settings/usersettings.cpp
Expand Up @@ -251,7 +251,7 @@ void CSMSettings::UserSettings::loadSettings (const QString &fileName)
bool localOk = loadFromFile(localFilePath);

//user
mUserFilePath = QString::fromStdString(mCfgMgr.getUserPath().string()) + fileName;
mUserFilePath = QString::fromStdString(mCfgMgr.getUserConfigPath().string()) + fileName;
loadFromFile(mUserFilePath);

if (!(localOk || globalOk))
Expand Down
6 changes: 3 additions & 3 deletions apps/openmw/engine.cpp
Expand Up @@ -301,7 +301,7 @@ std::string OMW::Engine::loadSettings (Settings::Manager & settings)
throw std::runtime_error ("No default settings file found! Make sure the file \"settings-default.cfg\" was properly installed.");

// load user settings if they exist, otherwise just load the default settings as user settings
const std::string settingspath = mCfgMgr.getUserPath().string() + "/settings.cfg";
const std::string settingspath = mCfgMgr.getUserConfigPath().string() + "/settings.cfg";
if (boost::filesystem::exists(settingspath))
settings.loadUser(settingspath);
else if (boost::filesystem::exists(localdefault))
Expand Down Expand Up @@ -373,7 +373,7 @@ void OMW::Engine::prepareEngine (Settings::Manager & settings)
// Create input and UI first to set up a bootstrapping environment for
// showing a loading screen and keeping the window responsive while doing so

std::string keybinderUser = (mCfgMgr.getUserPath() / "input.xml").string();
std::string keybinderUser = (mCfgMgr.getUserConfigPath() / "input.xml").string();
bool keybinderUserExists = boost::filesystem::exists(keybinderUser);
MWInput::InputManager* input = new MWInput::InputManager (*mOgre, *this, keybinderUser, keybinderUserExists, mGrab);
mEnvironment.setInputManager (input);
Expand Down Expand Up @@ -536,7 +536,7 @@ void OMW::Engine::screenshot()
// Count screenshots.
int shotCount = 0;

const std::string screenshotPath = mCfgMgr.getUserPath().string();
const std::string& screenshotPath = mCfgMgr.getUserDataPath().string();

// Find the first unused filename with a do-while
std::ostringstream stream;
Expand Down
8 changes: 7 additions & 1 deletion components/files/configurationmanager.cpp
Expand Up @@ -27,6 +27,7 @@ ConfigurationManager::ConfigurationManager()
setupTokensMapping();

boost::filesystem::create_directories(mFixedPath.getUserConfigPath());
boost::filesystem::create_directories(mFixedPath.getUserDataPath());

mLogPath = mFixedPath.getUserConfigPath();
}
Expand Down Expand Up @@ -144,11 +145,16 @@ const boost::filesystem::path& ConfigurationManager::getGlobalPath() const
return mFixedPath.getGlobalConfigPath();
}

const boost::filesystem::path& ConfigurationManager::getUserPath() const
const boost::filesystem::path& ConfigurationManager::getUserConfigPath() const
{
return mFixedPath.getUserConfigPath();
}

const boost::filesystem::path& ConfigurationManager::getUserDataPath() const
{
return mFixedPath.getUserDataPath();
}

const boost::filesystem::path& ConfigurationManager::getLocalPath() const
{
return mFixedPath.getLocalPath();
Expand Down
2 changes: 1 addition & 1 deletion components/files/configurationmanager.hpp
Expand Up @@ -36,7 +36,7 @@ struct ConfigurationManager

/**< Fixed paths */
const boost::filesystem::path& getGlobalPath() const;
const boost::filesystem::path& getUserPath() const;
const boost::filesystem::path& getUserConfigPath() const;
const boost::filesystem::path& getLocalPath() const;

const boost::filesystem::path& getGlobalDataPath() const;
Expand Down
19 changes: 10 additions & 9 deletions components/files/fixedpath.hpp
Expand Up @@ -48,7 +48,8 @@ struct FixedPath
*/
FixedPath(const std::string& application_name)
: mPath(application_name + "/")
, mUserPath(mPath.getUserConfigPath())
, mUserConfigPath(mPath.getUserConfigPath())
, mUserDataPath(mPath.getUserDataPath())
, mGlobalConfigPath(mPath.getGlobalConfigPath())
, mLocalPath(mPath.getLocalPath())
, mGlobalDataPath(mPath.getGlobalDataPath())
Expand All @@ -59,18 +60,19 @@ struct FixedPath

/**
* \brief Return path pointing to the user local configuration directory.
*
* \return boost::filesystem::path
*/
const boost::filesystem::path& getUserConfigPath() const
{
return mUserPath;
return mUserConfigPath;
}

const boost::filesystem::path& getUserDataPath() const
{
return mUserDataPath;
}

/**
* \brief Return path pointing to the global (system) configuration directory.
*
* \return boost::filesystem::path
*/
const boost::filesystem::path& getGlobalConfigPath() const
{
Expand All @@ -79,8 +81,6 @@ struct FixedPath

/**
* \brief Return path pointing to the directory where application was started.
*
* \return boost::filesystem::path
*/
const boost::filesystem::path& getLocalPath() const
{
Expand All @@ -105,7 +105,8 @@ struct FixedPath
private:
PathType mPath;

boost::filesystem::path mUserPath; /**< User path */
boost::filesystem::path mUserConfigPath; /**< User path */
boost::filesystem::path mUserDataPath;
boost::filesystem::path mGlobalConfigPath; /**< Global path */
boost::filesystem::path mLocalPath; /**< It is the same directory where application was run */

Expand Down
86 changes: 41 additions & 45 deletions components/files/linuxpath.cpp
Expand Up @@ -8,6 +8,39 @@
#include <unistd.h>
#include <boost/filesystem/fstream.hpp>


namespace
{
boost::filesystem::path getUserHome()
{
const char* dir = getenv("HOME");
if (dir == NULL)
{
struct passwd* pwd = getpwuid(getuid());
if (pwd != NULL)
{
dir = pwd->pw_dir;
}
}
if (dir == NULL)
return boost::filesystem::path();
else
return boost::filesystem::path(dir);
}

boost::filesystem::path getEnv(const std::string& envVariable, const boost::filesystem::path& fallback)
{
const char* result = getenv(envVariable.c_str());
if (!result)
return fallback;
boost::filesystem::path dir(result);
if (dir.empty())
return fallback;
else
return dir;
}
}

/**
* \namespace Files
*/
Expand All @@ -21,46 +54,17 @@ LinuxPath::LinuxPath(const std::string& application_name)

boost::filesystem::path LinuxPath::getUserConfigPath() const
{
boost::filesystem::path userPath(".");

const char* theDir = getenv("HOME");
if (theDir == NULL)
{
struct passwd* pwd = getpwuid(getuid());
if (pwd != NULL)
{
theDir = pwd->pw_dir;
}
}

if (theDir != NULL)
{
userPath = boost::filesystem::path(theDir);
}
return getEnv("XDG_CONFIG_HOME", getUserHome() / ".config") / mName;
}

return userPath / ".config" / mName;
boost::filesystem::path LinuxPath::getUserDataPath() const
{
return getEnv("XDG_DATA_HOME", getUserHome() / ".local/share") / mName;
}

boost::filesystem::path LinuxPath::getCachePath() const
{
boost::filesystem::path userPath(".");

const char* theDir = getenv("HOME");
if (theDir == NULL)
{
struct passwd* pwd = getpwuid(getuid());
if (pwd != NULL)
{
theDir = pwd->pw_dir;
}
}

if (theDir != NULL)
{
userPath = boost::filesystem::path(theDir);
}

return userPath / ".cache" / mName;
return getEnv("XDG_CACHE_HOME", getUserHome() / ".cache") / mName;
}

boost::filesystem::path LinuxPath::getGlobalConfigPath() const
Expand All @@ -84,17 +88,9 @@ boost::filesystem::path LinuxPath::getInstallPath() const
{
boost::filesystem::path installPath;

char *homePath = getenv("HOME");
if (homePath == NULL)
{
struct passwd* pwd = getpwuid(getuid());
if (pwd != NULL)
{
homePath = pwd->pw_dir;
}
}
boost::filesystem::path homePath = getUserHome();

if (homePath != NULL)
if (!homePath.empty())
{
boost::filesystem::path wineDefaultRegistry(homePath);
wineDefaultRegistry /= ".wine/system.reg";
Expand Down
2 changes: 2 additions & 0 deletions components/files/linuxpath.hpp
Expand Up @@ -23,6 +23,8 @@ struct LinuxPath
*/
boost::filesystem::path getUserConfigPath() const;

boost::filesystem::path getUserDataPath() const;

/**
* \brief Return path to the global (system) directory where config files can be placed.
*/
Expand Down

0 comments on commit 33389b9

Please sign in to comment.