Skip to content

Commit

Permalink
Default to XDG dir for configuration
Browse files Browse the repository at this point in the history
This patch changes the default configuration directory, internally
referred to as 'ApplicationDir', to comply with XDG's Base Directory
Specification.

Most Unix tools already operate on the specification. By adhering it,
Nchat becomes more intuitive and integrated with the system.

More information about the spec can be found at:
    https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html

The '--confdir' flag doesn't cover this scenario as it's directed at
customization at invokation and not at defaults.

Signed-off-by: Carlos Torres <torr.ca@mailgw.com>
  • Loading branch information
torrca committed Apr 17, 2024
1 parent 8f7b24e commit c184312
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
#include "wmchat.h"
#endif

static const char *EnvXdgConf = "XDG_CONFIG_HOME";
static const std::string DefaultXdgConfDir = ".config";


static void RemoveProfile();
static std::shared_ptr<Protocol> SetupProfile();
static void ShowHelp();
Expand Down Expand Up @@ -121,12 +125,20 @@ static std::vector<ProtocolBaseFactory*> GetProtocolFactorys()
return protocolFactorys;
}

static std::string GetConfDir()
{
char *confDir = getenv(EnvXdgConf);
if (confDir != NULL && confDir[0] != '\0') {
return std::string(confDir) + std::string("/nchat");
}
return std::string(getenv("HOME")) + "/" + DefaultXdgConfDir + "/" + std::string("nchat");
}

int main(int argc, char* argv[])
{
// Defaults
umask(S_IRWXG | S_IRWXO);
FileUtil::SetApplicationDir(std::string(getenv("HOME")) + std::string("/.nchat"));
FileUtil::SetApplicationDir(GetConfDir());
Log::SetVerboseLevel(Log::INFO_LEVEL);

// Argument handling
Expand Down Expand Up @@ -534,7 +546,7 @@ void ShowHelp()
"Usage: nchat [OPTION]\n"
"\n"
"Command-line Options:\n"
" -d, --confdir <DIR> use a different directory than ~/.nchat\n"
" -d, --confdir <DIR> use a different directory than ~/.config/nchat\n"
" -e, --verbose enable verbose logging\n"
" -ee, --extra-verbose enable extra verbose logging\n"
" -h, --help display this help and exit\n"
Expand Down
2 changes: 1 addition & 1 deletion src/nchat.1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ nchat is a terminal\-based telegram / whatsapp client.
.SS "Command-line Options:"
.TP
\fB\-d\fR, \fB\-\-confdir\fR <DIR>
use a different directory than ~/.nchat
use a different directory than \fB$XDG_CONFIG_HOME\fR/nchat
.TP
\fB\-e\fR, \fB\-\-verbose\fR
enable verbose logging
Expand Down

0 comments on commit c184312

Please sign in to comment.