From d7abf2b60fa074cfb0249b618ba7aa0380794496 Mon Sep 17 00:00:00 2001 From: circled-square Date: Mon, 19 Jun 2023 11:25:54 +0200 Subject: [PATCH 1/4] allow placing calfrc under $XDG_CONFIG_HOME - first check if ~/.calfrc exists, if it does use it - otherwise attempt to create $XDG_CONFIG_HOME/calf/ if it does not exist - if the folder now exists use as path $XDG_CONFIG_HOME/calf/calfrc, otherwise fall back to ~/.calfrc --- src/gui.cpp | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/gui.cpp b/src/gui.cpp index 8283971b9..f877f85b2 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -26,6 +26,8 @@ #include +#include + using namespace calf_plugins; using namespace std; @@ -502,10 +504,31 @@ bool window_update_controller::check_redraw(GtkWidget *toplevel) gui_environment::gui_environment() { keyfile = g_key_file_new(); + + string filename; - gchar *fn = g_build_filename(getenv("HOME"), ".calfrc", NULL); - string filename = fn; - g_free(fn); + gchar *fn_under_home = g_build_filename(getenv("HOME"), ".calfrc", NULL); + char *xdg_conf = getenv("XDG_CONFIG_HOME"); + gchar *calf_conf_dir = g_build_filename(xdg_conf, "calf", NULL); + gchar *fn_under_conf = g_build_filename(calf_conf_dir, "calfrc", NULL); + + //if $HOME/.calfrc exists or XDG_CONFIG_HOME is not set use $HOME/.calfrc + if(g_file_test(fn_under_home, G_FILE_TEST_IS_REGULAR) || xdg_conf == NULL) { + filename = fn_under_home; + } else { + //if $XDG_CONFIG_HOME/calf does not exist, create it + mkdir(calf_conf_dir, 0644); + + if(g_file_test(calf_conf_dir, G_FILE_TEST_IS_DIR)) + filename = fn_under_conf; + else + filename = fn_under_home; //failed creating directory: fall back to $HOME/.calfrc + } + + g_free(calf_conf_dir); + g_free(fn_under_conf); + g_free(fn_under_home); + g_key_file_load_from_file(keyfile, filename.c_str(), (GKeyFileFlags)(G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS), NULL); config_db = new calf_utils::gkeyfile_config_db(keyfile, filename.c_str(), "gui"); From d0754f9816142b9024e59cc1d739d9bbdbd0f080 Mon Sep 17 00:00:00 2001 From: circled-square Date: Wed, 18 Oct 2023 10:41:51 +0200 Subject: [PATCH 2/4] checking if XDG_CONFIG_HOME/calf already exists before calling mkdir --- src/gui.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/gui.cpp b/src/gui.cpp index f877f85b2..4a0b0c366 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -517,7 +517,8 @@ gui_environment::gui_environment() filename = fn_under_home; } else { //if $XDG_CONFIG_HOME/calf does not exist, create it - mkdir(calf_conf_dir, 0644); + if(!g_file_test(calf_conf_dir, G_FILE_TEST_EXISTS)) + mkdir(calf_conf_dir, 0644); if(g_file_test(calf_conf_dir, G_FILE_TEST_IS_DIR)) filename = fn_under_conf; From 55e7466e1543268981a7a81e339e558d9d9ae60d Mon Sep 17 00:00:00 2001 From: circled-square Date: Wed, 18 Oct 2023 10:48:29 +0200 Subject: [PATCH 3/4] replaced mkdir for g_mkdir_with_parents to spare including sys/stat.h --- src/gui.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/gui.cpp b/src/gui.cpp index 4a0b0c366..95f085353 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -26,8 +26,6 @@ #include -#include - using namespace calf_plugins; using namespace std; @@ -518,7 +516,7 @@ gui_environment::gui_environment() } else { //if $XDG_CONFIG_HOME/calf does not exist, create it if(!g_file_test(calf_conf_dir, G_FILE_TEST_EXISTS)) - mkdir(calf_conf_dir, 0644); + g_mkdir_with_parents(calf_conf_dir, 0644); if(g_file_test(calf_conf_dir, G_FILE_TEST_IS_DIR)) filename = fn_under_conf; From 12692dc115a1157389dd44decdf6e213cb914f78 Mon Sep 17 00:00:00 2001 From: circled-square Date: Tue, 24 Oct 2023 15:16:44 +0200 Subject: [PATCH 4/4] changed mode to 0755 for the config directory --- src/gui.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gui.cpp b/src/gui.cpp index 95f085353..42f4d4101 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -516,7 +516,7 @@ gui_environment::gui_environment() } else { //if $XDG_CONFIG_HOME/calf does not exist, create it if(!g_file_test(calf_conf_dir, G_FILE_TEST_EXISTS)) - g_mkdir_with_parents(calf_conf_dir, 0644); + g_mkdir_with_parents(calf_conf_dir, 0755); if(g_file_test(calf_conf_dir, G_FILE_TEST_IS_DIR)) filename = fn_under_conf;