home-dir.diff
| client/src/configuration.cpp (working copy) | ||
|---|---|---|
| 40 | 40 |
// Project includes |
| 41 | 41 |
#include "snowballs_client.h" |
| 42 | 42 |
#include "snowballs_config.h" |
| 43 |
#include "userpaths.h" |
|
| 43 | 44 | |
| 44 | 45 |
using namespace std; |
| 45 | 46 |
using namespace NLMISC; |
| ... | ... | |
| 59 | 60 | |
| 60 | 61 |
bool CConfiguration::init() |
| 61 | 62 |
{
|
| 63 |
string confFP = CUserPaths::getSnowballsConfigDir() + "client.cfg"; |
|
| 64 |
if (!CUserPaths::isFile(confFP)) |
|
| 65 |
{
|
|
| 66 |
CIFile confTemplate(SBCLIENT_CONFIG_FILE); |
|
| 67 |
uint8 *buf = new uint8[confTemplate.getFileSize()]; |
|
| 68 |
confTemplate.serialBuffer (buf, confTemplate.getFileSize()); |
|
| 69 | ||
| 70 |
COFile userConfFile (confFP); |
|
| 71 |
userConfFile.serialBuffer (buf, confTemplate.getFileSize()); |
|
| 72 | ||
| 73 |
confTemplate.close(); |
|
| 74 |
userConfFile.close(); |
|
| 75 |
delete []buf; |
|
| 76 |
} |
|
| 77 | ||
| 62 | 78 |
nlassert(!ConfigFile); ConfigFile = new CConfigFile(); nlassert(ConfigFile); |
| 63 |
ConfigFile->load(SBCLIENT_CONFIG_FILE);
|
|
| 79 |
ConfigFile->load(confFP, false);
|
|
| 64 | 80 | |
| 65 | 81 |
// set the search paths (kinda important) |
| 66 | 82 |
CConfigFile::CVar *var; |
| client/src/userpaths.cpp (revision 0) | ||
|---|---|---|
| 1 |
#include "userpaths.h" |
|
| 2 |
#include <stdlib.h> |
|
| 3 |
#include <sys/stat.h> |
|
| 4 |
#include <nel/misc/common.h> |
|
| 5 |
#include <nel/misc/debug.h> |
|
| 6 | ||
| 7 |
using namespace std; |
|
| 8 | ||
| 9 |
namespace SBCLIENT |
|
| 10 |
{
|
|
| 11 | ||
| 12 |
string CUserPaths::getHomeDir() |
|
| 13 |
{
|
|
| 14 |
#ifdef NL_OS_WINDOWS |
|
| 15 |
nlerror ("TODO: Windows home dir");
|
|
| 16 |
return ""; |
|
| 17 |
#else |
|
| 18 |
char *home = getenv("HOME");
|
|
| 19 |
if (NULL == home || '\0' == home[0]) |
|
| 20 |
{
|
|
| 21 |
nlerror ("No home dir");
|
|
| 22 |
} |
|
| 23 |
return string(home) + "/"; |
|
| 24 |
#endif |
|
| 25 |
} |
|
| 26 | ||
| 27 |
string CUserPaths::getConfigDir() |
|
| 28 |
{
|
|
| 29 |
#ifdef NL_OS_WINDOWS |
|
| 30 |
string configDir = ""; |
|
| 31 |
nlerror ("TODO: Windows users's config dir");
|
|
| 32 |
#else |
|
| 33 |
char *xdgConfigHome = getenv ("XDG_CONFIG_HOME");
|
|
| 34 | ||
| 35 |
string configDir; |
|
| 36 |
if (NULL == xdgConfigHome || '\0' == xdgConfigHome[0]) |
|
| 37 |
{
|
|
| 38 |
configDir = getHomeDir() + ".config/"; |
|
| 39 |
if (!isDir(configDir)) |
|
| 40 |
{
|
|
| 41 |
if (0 != mkdir (configDir.c_str(), S_IRUSR | S_IWUSR | S_IXUSR) ) |
|
| 42 |
{
|
|
| 43 |
//glib use temp here |
|
| 44 |
nlerror ("Can't create %s", configDir.c_str());
|
|
| 45 |
} |
|
| 46 |
} |
|
| 47 |
} |
|
| 48 |
else |
|
| 49 |
{
|
|
| 50 |
configDir = string(xdgConfigHome) + "/"; |
|
| 51 |
} |
|
| 52 |
#endif |
|
| 53 |
return configDir; |
|
| 54 |
} |
|
| 55 | ||
| 56 |
string CUserPaths::getSnowballsConfigDir() |
|
| 57 |
{
|
|
| 58 |
string snowballsConfigDir = getConfigDir() + "snowballs/"; |
|
| 59 |
if (!isDir(snowballsConfigDir)) |
|
| 60 |
{
|
|
| 61 |
if (0 != mkdir (snowballsConfigDir.c_str(), S_IRUSR | S_IWUSR | S_IXUSR) ) |
|
| 62 |
nlerror ("Can't create %s", snowballsConfigDir.c_str());
|
|
| 63 |
} |
|
| 64 | ||
| 65 |
return snowballsConfigDir; |
|
| 66 |
} |
|
| 67 | ||
| 68 |
bool CUserPaths::isDir(string path) |
|
| 69 |
{
|
|
| 70 |
struct stat sInfo; |
|
| 71 |
if (0 != stat (path.c_str(), &sInfo)) |
|
| 72 |
return false; |
|
| 73 | ||
| 74 |
return bool(S_ISDIR(sInfo.st_mode)); |
|
| 75 |
} |
|
| 76 | ||
| 77 |
bool CUserPaths::isFile(string path) |
|
| 78 |
{
|
|
| 79 |
struct stat sInfo; |
|
| 80 |
if (0 != stat (path.c_str(), &sInfo)) |
|
| 81 |
return false; |
|
| 82 | ||
| 83 |
return bool(S_ISREG(sInfo.st_mode)); |
|
| 84 |
} |
|
| 85 | ||
| 86 |
} /* namespace SBCLIENT */ |
|
| client/src/userpaths.h (revision 0) | ||
|---|---|---|
| 1 |
#ifndef USER_PATHS_H |
|
| 2 |
#define USER_PATHS_H |
|
| 3 | ||
| 4 |
#include <string> |
|
| 5 | ||
| 6 |
namespace SBCLIENT |
|
| 7 |
{
|
|
| 8 | ||
| 9 |
class CUserPaths |
|
| 10 |
{
|
|
| 11 |
public: |
|
| 12 |
static std::string getHomeDir (); |
|
| 13 |
static std::string getConfigDir (); |
|
| 14 |
static std::string getSnowballsConfigDir (); |
|
| 15 |
static bool isDir (std::string path); |
|
| 16 |
static bool isFile (std::string path); |
|
| 17 |
}; |
|
| 18 | ||
| 19 | ||
| 20 |
} /* namespace SBCLIENT */ |
|
| 21 | ||
| 22 |
#endif |
|