### Eclipse Workspace Patch 1.0 #P snowballs2 Index: client/src/configuration.cpp =================================================================== --- client/src/configuration.cpp (revision 1651) +++ client/src/configuration.cpp (working copy) @@ -40,6 +40,7 @@ // Project includes #include "snowballs_client.h" #include "snowballs_config.h" +#include "userpaths.h" using namespace std; using namespace NLMISC; @@ -59,8 +60,23 @@ bool CConfiguration::init() { + string confFP = CUserPaths::getSnowballsConfigDir() + "client.cfg"; + if (!CUserPaths::isFile(confFP)) + { + CIFile confTemplate(SBCLIENT_CONFIG_FILE); + uint8 *buf = new uint8[confTemplate.getFileSize()]; + confTemplate.serialBuffer (buf, confTemplate.getFileSize()); + + COFile userConfFile (confFP); + userConfFile.serialBuffer (buf, confTemplate.getFileSize()); + + confTemplate.close(); + userConfFile.close(); + delete []buf; + } + nlassert(!ConfigFile); ConfigFile = new CConfigFile(); nlassert(ConfigFile); - ConfigFile->load(SBCLIENT_CONFIG_FILE); + ConfigFile->load(confFP, false); // set the search paths (kinda important) CConfigFile::CVar *var; Index: client/src/userpaths.cpp =================================================================== --- client/src/userpaths.cpp (revision 0) +++ client/src/userpaths.cpp (revision 0) @@ -0,0 +1,86 @@ +#include "userpaths.h" +#include +#include +#include +#include + +using namespace std; + +namespace SBCLIENT +{ + +string CUserPaths::getHomeDir() +{ +#ifdef NL_OS_WINDOWS + nlerror ("TODO: Windows home dir"); + return ""; +#else + char *home = getenv("HOME"); + if (NULL == home || '\0' == home[0]) + { + nlerror ("No home dir"); + } + return string(home) + "/"; +#endif +} + +string CUserPaths::getConfigDir() +{ +#ifdef NL_OS_WINDOWS + string configDir = ""; + nlerror ("TODO: Windows users's config dir"); +#else + char *xdgConfigHome = getenv ("XDG_CONFIG_HOME"); + + string configDir; + if (NULL == xdgConfigHome || '\0' == xdgConfigHome[0]) + { + configDir = getHomeDir() + ".config/"; + if (!isDir(configDir)) + { + if (0 != mkdir (configDir.c_str(), S_IRUSR | S_IWUSR | S_IXUSR) ) + { + //glib use temp here + nlerror ("Can't create %s", configDir.c_str()); + } + } + } + else + { + configDir = string(xdgConfigHome) + "/"; + } +#endif + return configDir; +} + +string CUserPaths::getSnowballsConfigDir() +{ + string snowballsConfigDir = getConfigDir() + "snowballs/"; + if (!isDir(snowballsConfigDir)) + { + if (0 != mkdir (snowballsConfigDir.c_str(), S_IRUSR | S_IWUSR | S_IXUSR) ) + nlerror ("Can't create %s", snowballsConfigDir.c_str()); + } + + return snowballsConfigDir; +} + +bool CUserPaths::isDir(string path) +{ + struct stat sInfo; + if (0 != stat (path.c_str(), &sInfo)) + return false; + + return bool(S_ISDIR(sInfo.st_mode)); +} + +bool CUserPaths::isFile(string path) +{ + struct stat sInfo; + if (0 != stat (path.c_str(), &sInfo)) + return false; + + return bool(S_ISREG(sInfo.st_mode)); +} + +} /* namespace SBCLIENT */ \ No newline at end of file Index: client/src/userpaths.h =================================================================== --- client/src/userpaths.h (revision 0) +++ client/src/userpaths.h (revision 0) @@ -0,0 +1,22 @@ +#ifndef USER_PATHS_H +#define USER_PATHS_H + +#include + +namespace SBCLIENT +{ + +class CUserPaths +{ + public: + static std::string getHomeDir (); + static std::string getConfigDir (); + static std::string getSnowballsConfigDir (); + static bool isDir (std::string path); + static bool isFile (std::string path); +}; + + +} /* namespace SBCLIENT */ + +#endif