From 047673a493690134eefa44b35e8b9317a2a75b12 Mon Sep 17 00:00:00 2001 From: Geert Janssens Date: Thu, 2 Sep 2021 16:35:15 +0200 Subject: [PATCH] Small refactoring of scm config loading. - Remove and cleanup duplicate code - C++-ify - Run code in gnucash-cli commands related to reports. Users may want to load modified report code via config-user.scm which they want to access while executing cli commands --- gnucash/gnucash-commands.cpp | 6 ++- gnucash/gnucash-commands.hpp | 4 ++ gnucash/gnucash-core-app.cpp | 79 ++++++++---------------------------- gnucash/gnucash-core-app.hpp | 1 + gnucash/gnucash.cpp | 55 +------------------------ 5 files changed, 29 insertions(+), 116 deletions(-) diff --git a/gnucash/gnucash-commands.cpp b/gnucash/gnucash-commands.cpp index 488a93cb99..3a5edb8f36 100644 --- a/gnucash/gnucash-commands.cpp +++ b/gnucash/gnucash-commands.cpp @@ -30,6 +30,7 @@ #endif #include "gnucash-commands.hpp" +#include "gnucash-core-app.hpp" extern "C" { #include @@ -173,8 +174,7 @@ scm_run_report (void *data, scm_c_use_module ("gnucash reports"); gnc_report_init (); - // load_system_config(); - // load_user_config(); + Gnucash::gnc_load_scm_config(); gnc_prefs_init (); qof_event_suspend (); @@ -303,6 +303,7 @@ scm_report_show (void *data, scm_c_use_module ("gnucash app-utils"); scm_c_use_module ("gnucash reports"); gnc_report_init (); + Gnucash::gnc_load_scm_config(); if (!args->file_to_load.empty()) { @@ -334,6 +335,7 @@ scm_report_list ([[maybe_unused]] void *data, scm_c_use_module ("gnucash app-utils"); scm_c_use_module ("gnucash reports"); gnc_report_init (); + Gnucash::gnc_load_scm_config(); scm_call_1 (scm_c_eval_string ("gnc:cmdline-report-list"), scm_current_output_port ()); diff --git a/gnucash/gnucash-commands.hpp b/gnucash/gnucash-commands.hpp index 01511d5847..ae3607147b 100644 --- a/gnucash/gnucash-commands.hpp +++ b/gnucash/gnucash-commands.hpp @@ -40,5 +40,9 @@ namespace Gnucash { int report_list (void); int report_show (const bo_str& file_to_load, const bo_str& run_report); + + // A helper function to load scm config files (SYSCONFIGDIR/config + // and USERCONFIGDIR/config-user.scm) on demand + void gnc_load_scm_config(void); } #endif diff --git a/gnucash/gnucash-core-app.cpp b/gnucash/gnucash-core-app.cpp index 8207d85bf3..4ecde0f7c7 100644 --- a/gnucash/gnucash-core-app.cpp +++ b/gnucash/gnucash-core-app.cpp @@ -86,25 +86,6 @@ gnc_print_unstable_message(void) << bl::format (bl::translate ("To find the last stable version, please refer to {1}")) % PACKAGE_URL << "\n"; } -static gboolean -try_load_config_array(const gchar *fns[]) -{ - gchar *filename; - int i; - - for (i = 0; fns[i]; i++) - { - filename = gnc_build_userdata_path(fns[i]); - if (gfec_try_load(filename)) - { - g_free(filename); - return TRUE; - } - g_free(filename); - } - return FALSE; -} - static void update_message(const gchar *msg) { @@ -112,52 +93,28 @@ update_message(const gchar *msg) g_message("%s", msg); } -static void -load_system_config(void) +void +Gnucash::gnc_load_scm_config (void) { - static int is_system_config_loaded = FALSE; - gchar *system_config_dir; - gchar *system_config; - - if (is_system_config_loaded) return; - - update_message("loading system configuration"); - system_config_dir = gnc_path_get_pkgsysconfdir(); - system_config = g_build_filename(system_config_dir, "config", nullptr); - is_system_config_loaded = gfec_try_load(system_config); - g_free(system_config_dir); - g_free(system_config); -} - -static void -load_user_config(void) -{ - /* Don't continue adding to this list. When 3.0 rolls around bump - the 2.4 files off the list. */ - static const gchar *saved_report_files[] = + static auto is_system_config_loaded = false; + if (!is_system_config_loaded) { - SAVED_REPORTS_FILE, SAVED_REPORTS_FILE_OLD_REV, NULL - }; - static const gchar *stylesheet_files[] = { "stylesheets-2.0", NULL}; - static int is_user_config_loaded = FALSE; - - if (is_user_config_loaded) - return; - else is_user_config_loaded = TRUE; - - update_message("loading user configuration"); - { - gchar *config_filename; - config_filename = g_build_filename (gnc_userconfig_dir (), - "config-user.scm", (char *)NULL); - gfec_try_load(config_filename); - g_free(config_filename); + update_message ("loading system scm configuration"); + auto system_config_dir = gnc_path_get_pkgsysconfdir (); + auto system_config = g_build_filename (system_config_dir, "config", nullptr); + is_system_config_loaded = gfec_try_load (system_config); + g_free (system_config_dir); + g_free (system_config); } - update_message("loading saved reports"); - try_load_config_array(saved_report_files); - update_message("loading stylesheets"); - try_load_config_array(stylesheet_files); + static auto is_user_config_loaded = false; + if (!is_user_config_loaded) + { + update_message("loading user scm configuration"); + auto config_filename = g_build_filename (gnc_userconfig_dir (), "config-user.scm", nullptr); + is_user_config_loaded = gfec_try_load (config_filename); + g_free (config_filename); + } } static void diff --git a/gnucash/gnucash-core-app.hpp b/gnucash/gnucash-core-app.hpp index ed61af1f28..9ff91af336 100644 --- a/gnucash/gnucash-core-app.hpp +++ b/gnucash/gnucash-core-app.hpp @@ -70,5 +70,6 @@ private: char *sys_locale = nullptr; }; +void gnc_load_scm_config(void); } #endif diff --git a/gnucash/gnucash.cpp b/gnucash/gnucash.cpp index d7a76466a0..2ab37cb00f 100644 --- a/gnucash/gnucash.cpp +++ b/gnucash/gnucash.cpp @@ -73,56 +73,6 @@ namespace bl = boost::locale; static QofLogModule log_module = GNC_MOD_GUI; static gchar *userdata_migration_msg = NULL; -static void -update_message(const gchar *msg) -{ - gnc_update_splash_screen(msg, GNC_SPLASH_PERCENTAGE_UNKNOWN); - g_message("%s", msg); -} - -static void -load_system_config(void) -{ - static int is_system_config_loaded = FALSE; - gchar *system_config_dir; - gchar *system_config; - - if (is_system_config_loaded) return; - - update_message("loading system configuration"); - system_config_dir = gnc_path_get_pkgsysconfdir(); - system_config = g_build_filename(system_config_dir, "config", nullptr); - is_system_config_loaded = gfec_try_load(system_config); - g_free(system_config_dir); - g_free(system_config); -} - -static void -load_user_config(void) -{ - /* Don't continue adding to this list. When 3.0 rolls around bump - the 2.4 files off the list. */ - static const gchar *saved_report_files[] = - { - SAVED_REPORTS_FILE, SAVED_REPORTS_FILE_OLD_REV, NULL - }; - static const gchar *stylesheet_files[] = { "stylesheets-2.0", NULL}; - static int is_user_config_loaded = FALSE; - - if (is_user_config_loaded) - return; - else is_user_config_loaded = TRUE; - - update_message("loading user configuration"); - { - gchar *config_filename; - config_filename = g_build_filename (gnc_userconfig_dir (), - "config-user.scm", (char *)NULL); - gfec_try_load(config_filename); - g_free(config_filename); - } -} - static void load_gnucash_plugins() { @@ -207,11 +157,10 @@ scm_run_gnucash (void *data, [[maybe_unused]] int argc, [[maybe_unused]] char ** load_gnucash_plugins(); load_gnucash_modules(); - /* Load the config before starting up the gui. This insures that + /* Load the scm config files before starting up the gui. This ensures that * custom reports have been read into memory before the Reports * menu is created. */ - load_system_config(); - load_user_config(); + Gnucash::gnc_load_scm_config(); /* Setting-up the report menu must come after the module loading but before the gui initializat*ion. */