Simplify and remove duplicate code for getting config directory paths

This commit is contained in:
Sude 2015-08-29 14:26:36 +03:00
parent f992466ddf
commit 06d211f534
3 changed files with 38 additions and 37 deletions

View File

@ -41,6 +41,9 @@ namespace Util
int getTerminalWidth();
void getDownloaderUrlsFromJSON(const Json::Value &root, std::vector<std::string> &urls);
std::vector<std::string> getDLCNamesFromJSON(const Json::Value &root);
std::string getHomeDir();
std::string getConfigHome();
std::string getCacheHome();
}
#endif // UTIL_H

View File

@ -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))

View File

@ -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<std::string> 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;
}