From b9a81e831e96d4f96dd4671663e75ec3acf3e7f6 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Mon, 2 May 2011 14:30:05 +0100 Subject: [PATCH] Use and XDG compliant config file location --- src/ConfigFile.cpp | 4 ++-- src/ConfigFile.hpp | 4 ++-- src/HighScores.cpp | 2 +- src/Main.cpp | 51 +++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/ConfigFile.cpp b/src/ConfigFile.cpp index a6bf4a0..7a4b2f0 100644 --- a/src/ConfigFile.cpp +++ b/src/ConfigFile.cpp @@ -1,5 +1,5 @@ // ConfigFile.cpp -- Settings persistence. -// Copyright (C) 2008 Nick Gasson +// Copyright (C) 2008, 2011 Nick Gasson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -21,7 +21,7 @@ #include #include -ConfigFile::ConfigFile(string filename) +ConfigFile::ConfigFile(const string& filename) : dirty(false), filename(filename) { ifstream ifs((GetConfigDir() + filename).c_str(), ifstream::in); diff --git a/src/ConfigFile.hpp b/src/ConfigFile.hpp index b026715..df9fc6a 100644 --- a/src/ConfigFile.hpp +++ b/src/ConfigFile.hpp @@ -1,6 +1,6 @@ // // ConfigFile.hpp -- Settings persistence. -// Copyright (C) 2008-2009 Nick Gasson +// Copyright (C) 2008-2009, 2011 Nick Gasson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -26,7 +26,7 @@ class ConfigFile { public: - ConfigFile(string filename = ".lander.config"); + ConfigFile(const string& filename = "config"); ~ConfigFile(); void Flush(); diff --git a/src/HighScores.cpp b/src/HighScores.cpp index 1f663a6..633962f 100644 --- a/src/HighScores.cpp +++ b/src/HighScores.cpp @@ -291,7 +291,7 @@ ScoreFile::~ScoreFile() string ScoreFile::GetHighScoreFile() { - return GetConfigDir() + ".lander.scores"; + return GetConfigDir() + "scores"; } bool ScoreFile::ScoreEntry::operator<(const ScoreFile::ScoreEntry& rhs) const diff --git a/src/Main.cpp b/src/Main.cpp index e2d5716..b6d8f48 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -1,6 +1,6 @@ // // Main.cpp - Program entry point. -// Copyright (C) 2006-2010 Nick Gasson +// Copyright (C) 2006-2011 Nick Gasson // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -78,6 +78,27 @@ void RecreateScreens() sm.AddScreen("OPTIONS", options); } +static void MigrateConfigFiles() +{ + // Earlier versions of Lander stored config files directly in the + // user's home directory. Now use use the XDG-compliant .config/lander + // directory but we should move old configs and high scores there first + + using namespace boost::filesystem; + + const path cfg = GetConfigDir(); + const path home = getenv("HOME"); + + const path old_config = home / ".lander.config"; + const path old_scores = home / ".lander.scores"; + + if (exists(old_config)) + rename(old_config, cfg / "config"); + + if (exists(old_scores)) + rename(old_scores, cfg / "scores"); +} + // // Entry point. // @@ -98,11 +119,15 @@ int main(int argc, char **argv) const int DEFAULT_SOUND = true; cout << "Lunar Lander " << VERSION << endl << endl - << "Copyright (C) 2006-2010 Nick Gasson" << endl + << "Copyright (C) 2006-2011 Nick Gasson" << endl << "This program comes with ABSOLUTELY NO WARRANTY. This is free software, and" << endl << "you are welcome to redistribute it under certain conditions. See the GNU" << endl << "General Public Licence for details." << endl << endl; +#ifdef UNIX + MigrateConfigFiles(); +#endif + { ConfigFile cfile; width = cfile.get_int("hres", DEFAULT_HRES); @@ -194,12 +219,28 @@ string LocateResource(const string& file) string GetConfigDir() { + using namespace boost::filesystem; + #ifdef UNIX - return string(getenv("HOME")) + "/"; + path p; + const char *config = getenv("XDG_CONFIG_HOME"); + if (config == NULL || *config == '\0') { + const char *home = getenv("HOME"); + if (home == NULL) + throw runtime_error("HOME not set"); + + p = home; + p /= ".config"; + } + else + p = config; + + p /= "lander"; + create_directories(p); + + return p.file_string() + "/"; #else #ifdef WIN32 - using namespace boost::filesystem; - path appdata(getenv("APPDATA")); appdata /= "doof.me.uk"; appdata /= "Lander"; -- 2.39.2