From c184312535fda1da4b6a8ec73962e6ea8a753b38 Mon Sep 17 00:00:00 2001 From: Carlos Torres Date: Wed, 17 Apr 2024 01:28:35 -0300 Subject: [PATCH] Default to XDG dir for configuration 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 --- src/main.cpp | 16 ++++++++++++++-- src/nchat.1 | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 2605c978..427e8ae4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 SetupProfile(); static void ShowHelp(); @@ -121,12 +125,20 @@ static std::vector 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 @@ -534,7 +546,7 @@ void ShowHelp() "Usage: nchat [OPTION]\n" "\n" "Command-line Options:\n" - " -d, --confdir use a different directory than ~/.nchat\n" + " -d, --confdir 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" diff --git a/src/nchat.1 b/src/nchat.1 index 37684953..e0d6ac40 100644 --- a/src/nchat.1 +++ b/src/nchat.1 @@ -10,7 +10,7 @@ nchat is a terminal\-based telegram / whatsapp client. .SS "Command-line Options:" .TP \fB\-d\fR, \fB\-\-confdir\fR -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