diff --git a/include/util.h b/include/util.h index 841b663..b943b3d 100644 --- a/include/util.h +++ b/include/util.h @@ -41,6 +41,9 @@ namespace Util int getTerminalWidth(); void getDownloaderUrlsFromJSON(const Json::Value &root, std::vector &urls); std::vector getDLCNamesFromJSON(const Json::Value &root); + std::string getHomeDir(); + std::string getConfigHome(); + std::string getCacheHome(); } #endif // UTIL_H diff --git a/main.cpp b/main.cpp index e422c4c..0e71f6f 100644 --- a/main.cpp +++ b/main.cpp @@ -64,17 +64,15 @@ int main(int argc, char *argv[]) { Config config; config.sVersionString = VERSION_STRING; - char *xdgconfig = getenv("XDG_CONFIG_HOME"); - char *xdgcache = getenv("XDG_CACHE_HOME"); - std::string home = (std::string)getenv("HOME"); - - if (xdgcache) - config.sCacheDirectory = (std::string)xdgcache + "/lgogdownloader"; - else - config.sCacheDirectory = home + "/.cache/lgogdownloader"; + config.sCacheDirectory = Util::getCacheHome() + "/lgogdownloader"; config.sXMLDirectory = config.sCacheDirectory + "/xml"; + config.sConfigDirectory = Util::getConfigHome() + "/lgogdownloader"; + config.sCookiePath = config.sConfigDirectory + "/cookies.txt"; + config.sConfigFilePath = config.sConfigDirectory + "/config.cfg"; + config.sBlacklistFilePath = config.sConfigDirectory + "/blacklist.txt"; + // Create help text for --platform option std::string platform_text = "Select which installers are downloaded\n"; unsigned int platform_sum = 0; @@ -221,19 +219,6 @@ int main(int argc, char *argv[]) return 0; } - if (xdgconfig) - { - config.sConfigDirectory = (std::string)xdgconfig + "/lgogdownloader"; - } - else - { - config.sConfigDirectory = home + "/.config/lgogdownloader"; - } - - config.sCookiePath = config.sConfigDirectory + "/cookies.txt"; - config.sConfigFilePath = config.sConfigDirectory + "/config.cfg"; - config.sBlacklistFilePath = config.sConfigDirectory + "/blacklist.txt"; - // Create lgogdownloader directories boost::filesystem::path path = config.sXMLDirectory; if (!boost::filesystem::exists(path)) diff --git a/src/util.cpp b/src/util.cpp index 7a8f661..5dc9a3b 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -97,14 +97,7 @@ int Util::createXML(std::string filepath, uintmax_t chunk_size, std::string xml_ if (xml_dir.empty()) { - char *xdgcache = getenv("XDG_CACHE_HOME"); - if (xdgcache) - xml_dir = (std::string)xdgcache + "/lgogdownloader/xml"; - else - { - std::string home = (std::string)getenv("HOME"); - xml_dir = home + "/.cache/lgogdownloader/xml"; - } + xml_dir = Util::getCacheHome() + "/lgogdownloader/xml"; } // Make sure directory exists @@ -229,14 +222,7 @@ int Util::getGameSpecificConfig(std::string gamename, gameSpecificConfig* conf, if (directory.empty()) { - char *xdghome = getenv("XDG_CONFIG_HOME"); - if (xdghome) - directory = (std::string)xdghome + "/lgogdownloader/gamespecific"; - else - { - std::string home = (std::string)getenv("HOME"); - directory = home + "/.config/lgogdownloader/gamespecific"; - } + directory = Util::getConfigHome() + "/lgogdownloader/gamespecific"; } std::string filepath = directory + "/" + gamename + ".conf"; @@ -392,3 +378,30 @@ std::vector Util::getDLCNamesFromJSON(const Json::Value &root) } return dlcnames; } + +std::string Util::getHomeDir() +{ + return (std::string)getenv("HOME"); +} + +std::string Util::getConfigHome() +{ + std::string configHome; + char *xdgconfig = getenv("XDG_CONFIG_HOME"); + if (xdgconfig) + configHome = (std::string)xdgconfig; + else + configHome = Util::getHomeDir() + "/.config"; + return configHome; +} + +std::string Util::getCacheHome() +{ + std::string cacheHome; + char *xdgcache = getenv("XDG_CACHE_HOME"); + if (xdgcache) + cacheHome = (std::string)xdgcache; + else + cacheHome = Util::getHomeDir() + "/.cache"; + return cacheHome; +}