diff --git a/include/config.h b/include/config.h index 1841dad..b7cc130 100644 --- a/include/config.h +++ b/include/config.h @@ -41,7 +41,7 @@ class Config std::string sToken; std::string sSecret; std::string sVersionString; - std::string sHome; + std::string sConfigDirectory; std::string sCookiePath; std::string sConfigFilePath; unsigned int iInstallerType; diff --git a/main.cpp b/main.cpp index fc817e2..1248890 100644 --- a/main.cpp +++ b/main.cpp @@ -33,12 +33,29 @@ int main(int argc, char *argv[]) { Config config; config.sVersionString = VERSION_STRING; - config.sHome = (std::string)getenv("HOME"); - config.sCookiePath = config.sHome + "/.gogdownloader/cookies.txt"; - config.sConfigFilePath = config.sHome + "/.gogdownloader/config.cfg"; - config.sXMLDirectory = config.sHome + "/.gogdownloader/xml"; + char *xdgconfig = getenv("XDG_CONFIG_HOME"); + char *xdgcache = getenv("XDG_CACHE_HOME"); + std::string home = (std::string)getenv("HOME"); - // Create gogdownloader directories + if (xdgconfig) + { + config.sConfigDirectory = (std::string)xdgconfig + "/lgogdownloader"; + config.sCookiePath = config.sConfigDirectory + "/cookies.txt"; + config.sConfigFilePath = config.sConfigDirectory + "/config.cfg"; + } + else + { + config.sConfigDirectory = home + "/.config/lgogdownloader"; + config.sCookiePath = config.sConfigDirectory + "/cookies.txt"; + config.sConfigFilePath = config.sConfigDirectory + "/config.cfg"; + } + + if (xdgcache) + config.sXMLDirectory = (std::string)xdgcache + "/lgogdownloader/xml"; + else + config.sXMLDirectory = home + "/.cache/lgogdownloader/xml"; + + // Create lgogdownloader directories boost::filesystem::path path = config.sXMLDirectory; if (!boost::filesystem::exists(path)) { @@ -49,6 +66,16 @@ int main(int argc, char *argv[]) } } + path = config.sConfigDirectory; + if (!boost::filesystem::exists(path)) + { + if (!boost::filesystem::create_directories(path)) + { + std::cout << "Failed to create directory: " << path << std::endl; + return 1; + } + } + // Create help text for --platform option std::string platform_text = "Select which installers are downloaded\n"; unsigned int platform_sum = 0;