From 6c5fc4da299b740236d244dc3c7359eea64ca6ab Mon Sep 17 00:00:00 2001 From: Geert Janssens Date: Thu, 4 Apr 2019 20:34:55 +0200 Subject: [PATCH] Report fs cleanup - Add a few convenience functions These will provide easy access to some of the most important scm related runtime directories --- libgnucash/core-utils/core-utils.i | 10 ++++ libgnucash/core-utils/core-utils.scm | 4 ++ libgnucash/core-utils/gnc-filepath-utils.cpp | 48 +++++++++++++++++++- libgnucash/core-utils/gnc-filepath-utils.h | 2 + libgnucash/core-utils/gnc-path.c | 35 ++++++++++++-- libgnucash/core-utils/gnc-path.h | 12 +++++ 6 files changed, 105 insertions(+), 6 deletions(-) diff --git a/libgnucash/core-utils/core-utils.i b/libgnucash/core-utils/core-utils.i index 63a108a09d..c14b3721cf 100644 --- a/libgnucash/core-utils/core-utils.i +++ b/libgnucash/core-utils/core-utils.i @@ -56,6 +56,12 @@ void %newobject gnc_path_get_bindir; gchar * gnc_path_get_bindir(void); +%newobject gnc_path_get_scmdir; +gchar * gnc_path_get_scmdir(void); + +%newobject gnc_path_get_reportsdir; +gchar * gnc_path_get_reportsdir(void); + %newobject gnc_path_get_stdreportsdir; gchar * gnc_path_get_stdreportsdir(void); @@ -65,8 +71,12 @@ gchar * gnc_path_find_localized_html_file(const gchar *); %newobject gnc_build_userdata_path; gchar * gnc_build_userdata_path(const gchar *); +%newobject gnc_file_path_absolute; +gchar *gnc_file_path_absolute (const gchar *, const gchar *); + gchar * gnc_build_report_path(const gchar *); gchar * gnc_build_stdreports_path(const gchar *); +gchar * gnc_build_reports_path(const gchar *); void gnc_scm_log_warn(const gchar *); void gnc_scm_log_error(const gchar *); diff --git a/libgnucash/core-utils/core-utils.scm b/libgnucash/core-utils/core-utils.scm index 794ab22132..68e956768a 100644 --- a/libgnucash/core-utils/core-utils.scm +++ b/libgnucash/core-utils/core-utils.scm @@ -35,11 +35,15 @@ (re-export gnc-prefs-is-debugging-enabled) (re-export gnc-path-get-bindir) +(re-export gnc-path-get-scmdir) +(re-export gnc-path-get-reportsdir) (re-export gnc-path-get-stdreportsdir) (re-export gnc-path-find-localized-html-file) (re-export gnc-build-userdata-path) (re-export gnc-build-report-path) (re-export gnc-build-stdreports-path) +(re-export gnc-build-reports-path) +(re-export gnc-file-path-absolute) (re-export gnc-utf8?) (re-export gnc-utf8-strip-invalid-strdup) (re-export gnc-locale-from-utf8) diff --git a/libgnucash/core-utils/gnc-filepath-utils.cpp b/libgnucash/core-utils/gnc-filepath-utils.cpp index ac46d4227d..2b0dcb054a 100644 --- a/libgnucash/core-utils/gnc-filepath-utils.cpp +++ b/libgnucash/core-utils/gnc-filepath-utils.cpp @@ -1024,6 +1024,21 @@ gnc_userdata_dir_as_path (void) return gnc_userdata_home; } +static const bfs::path& +gnc_userconfig_dir_as_path (void) +{ + if (gnc_userdata_home.empty()) + /* Don't create missing directories automatically except + * if the target directory is the temporary directory. This + * should be done properly at a higher level (in the gui + * code most likely) very early in application startup. + * This call is just a fallback to prevent the code from + * crashing because no directories were configured. */ + gnc_filepath_init(); + + return gnc_userconfig_home; +} + gchar *gnc_file_path_absolute (const gchar *prefix, const gchar *relative) { bfs::path path_relative (relative); @@ -1054,7 +1069,7 @@ gchar *gnc_file_path_absolute (const gchar *prefix, const gchar *relative) } /** @fn gchar * gnc_build_userdata_path (const gchar *filename) - * @brief Make a path to filename in the user's configuration directory. + * @brief Make a path to filename in the user's gnucash data directory. * * @param filename The name of the file * @@ -1068,6 +1083,21 @@ gnc_build_userdata_path (const gchar *filename) return g_strdup((gnc_userdata_dir_as_path() / filename).string().c_str()); } +/** @fn gchar * gnc_build_userconfig_path (const gchar *filename) + * @brief Make a path to filename in the user's configuration directory. + * + * @param filename The name of the file + * + * @return An absolute path. The returned string should be freed by the user + * using g_free(). + */ + +gchar * +gnc_build_userconfig_path (const gchar *filename) +{ + return g_strdup((gnc_userconfig_dir_as_path() / filename).string().c_str()); +} + /* Test whether c is a valid character for a win32 file name. * If so return false, otherwise return true. */ @@ -1151,6 +1181,22 @@ gnc_build_report_path (const gchar *filename) return result; } +/** @fn gchar * gnc_build_reports_path (const gchar *dirname) + * @brief Make a path to dirname in the reports directory. + * + * @param dirname The name of the subdirectory + * + * @return An absolute path. The returned string should be freed by the user + * using g_free(). + */ + +gchar * +gnc_build_reports_path (const gchar *dirname) +{ + gchar *result = g_build_filename(gnc_path_get_reportsdir(), dirname, (gchar *)NULL); + return result; +} + /** @fn gchar * gnc_build_stdreports_path (const gchar *filename) * @brief Make a path to filename in the standard reports directory. * diff --git a/libgnucash/core-utils/gnc-filepath-utils.h b/libgnucash/core-utils/gnc-filepath-utils.h index f461438428..1c73c8cb6e 100644 --- a/libgnucash/core-utils/gnc-filepath-utils.h +++ b/libgnucash/core-utils/gnc-filepath-utils.h @@ -106,10 +106,12 @@ char * gnc_filepath_init (void); const gchar *gnc_userdata_dir (void); gchar *gnc_build_userdata_path (const gchar *filename); +gchar *gnc_build_userconfig_path (const gchar *filename); gchar *gnc_build_book_path (const gchar *filename); gchar *gnc_build_translog_path (const gchar *filename); gchar *gnc_build_data_path (const gchar *filename); gchar *gnc_build_report_path (const gchar *filename); +gchar *gnc_build_reports_path (const gchar *dirname); gchar *gnc_build_stdreports_path (const gchar *filename); const gchar *gnc_userconfig_dir (void); diff --git a/libgnucash/core-utils/gnc-path.c b/libgnucash/core-utils/gnc-path.c index db95757a4b..2d8a306aa3 100644 --- a/libgnucash/core-utils/gnc-path.c +++ b/libgnucash/core-utils/gnc-path.c @@ -170,11 +170,11 @@ gchar *gnc_path_get_accountsdir() return result; } -/** Returns the file path to the report directory, usually - * "$prefix/share/gnucash/scm/gnucash/report". +/** Returns the file path to the directory containing all guile scripts, usually + * "$prefix/share/gnucash/scm". * * @returns A newly allocated string. */ -gchar *gnc_path_get_reportdir() +gchar *gnc_path_get_scmdir() { /* Careful: if the cmake variable SCHEME_INSTALLED_SOURCE_DIR gets changed * in toplevel CMakeLists.txt, this path should probably change as well. @@ -187,13 +187,38 @@ gchar *gnc_path_get_reportdir() * runtime. */ gchar *pkgdatadir = gnc_path_get_pkgdatadir (); - gchar *result = g_build_filename (pkgdatadir, "scm", - "gnucash", "report", (char*)NULL); + gchar *result = g_build_filename (pkgdatadir, "scm", (char*)NULL); g_free (pkgdatadir); return result; } +/** Returns the file path to the report directory, usually + * "$prefix/share/gnucash/scm/gnucash/report". + * + * @returns A newly allocated string. */ +gchar *gnc_path_get_reportdir() +{ + gchar *scmdir = gnc_path_get_scmdir (); + gchar *result = g_build_filename (scmdir, "gnucash", "report", (char*)NULL); + g_free (scmdir); + + return result; +} + +/** Returns the file path to the reports directory, usually + * "$prefix/share/gnucash/scm/gnucash/report/reports". + * + * @returns A newly allocated string. */ +gchar *gnc_path_get_reportsdir() +{ + gchar *reportdir = gnc_path_get_reportdir (); + gchar *result = g_build_filename (reportdir, "reports", NULL); + g_free (reportdir); + //printf("Returning reportsdir %s\n", result); + return result; +} + /** Returns the file path to the standard * reports, usually * "$prefix/share/gnucash/scm/gnucash/report/reports/standard". diff --git a/libgnucash/core-utils/gnc-path.h b/libgnucash/core-utils/gnc-path.h index 14f3ccacef..ac65ea7727 100644 --- a/libgnucash/core-utils/gnc-path.h +++ b/libgnucash/core-utils/gnc-path.h @@ -92,12 +92,24 @@ gchar *gnc_path_get_localedir(void); * @returns A newly allocated string. */ gchar *gnc_path_get_accountsdir(void); +/** Returns the file path to the directory containing all guile scripts, usually + * "$prefix/share/gnucash/scm". + * + * @returns A newly allocated string. */ +gchar *gnc_path_get_scmdir(void); + /** Returns the file path to the report directory, usually * "$prefix/share/gnucash/scm/gnucash/report". * * @returns A newly allocated string. */ gchar *gnc_path_get_reportdir(void); +/** Returns the file path to the reports, usually + * "$prefix/share/gnucash/scm/gnucash/report/reports". + * + * @returns A newly allocated string. */ +gchar *gnc_path_get_reportsdir(void); + /** Returns the file path to the standard * reports, usually * "$prefix/share/gnucash/scm/gnucash/report/reports/standard".