diff --git a/libgnucash/core-utils/gnc-filepath-utils.cpp b/libgnucash/core-utils/gnc-filepath-utils.cpp index 7371534b35..6c5c8631da 100644 --- a/libgnucash/core-utils/gnc-filepath-utils.cpp +++ b/libgnucash/core-utils/gnc-filepath-utils.cpp @@ -728,6 +728,7 @@ static std::string migrate_gnc_datahome() } + #if defined G_OS_WIN32 ||defined MAC_INTEGRATION constexpr auto path_package = PACKAGE_NAME; #else @@ -1017,6 +1018,35 @@ gnc_userdata_dir_as_path (void) return gnc_userdata_home; } +gchar *gnc_file_path_absolute (const gchar *prefix, const gchar *relative) +{ + bfs::path path_relative (relative); + path_relative.imbue (bfs_locale); + bfs::path path_absolute; + bfs::path path_head; + + if (prefix == nullptr) + { + const gchar *doc_dir = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS); + if (doc_dir == nullptr) + path_head = bfs::path (gnc_userdata_dir ()); // running as root maybe + else + path_head = bfs::path (doc_dir); + + path_head.imbue (bfs_locale); + path_absolute = absolute (path_relative, path_head); + } + else + { + bfs::path path_head (prefix); + path_head.imbue (bfs_locale); + path_absolute = absolute (path_relative, path_head); + } + path_absolute.imbue (bfs_locale); + + return g_strdup (path_absolute.string().c_str()); +} + /** @fn gchar * gnc_build_userdata_path (const gchar *filename) * @brief Make a path to filename in the user's configuration directory. * diff --git a/libgnucash/core-utils/gnc-filepath-utils.h b/libgnucash/core-utils/gnc-filepath-utils.h index 3e04efc954..f461438428 100644 --- a/libgnucash/core-utils/gnc-filepath-utils.h +++ b/libgnucash/core-utils/gnc-filepath-utils.h @@ -49,6 +49,15 @@ gchar *gnc_resolve_file_path (const gchar *filefrag); */ gchar *gnc_file_path_relative_part (const gchar *prefix, const gchar *path); +/** Given a prefix and a relative path, return the absolute path. + * @param prefix The prefix that is the head of the path + * @param relative The path to add to the prefix to form an absolute path + * @return a char* that must be g_freed containing the absolute path. + * + * If prefix is null, then the gnc_userdata_home is used as the prefix. + */ +gchar *gnc_file_path_absolute (const gchar *prefix, const gchar *relative); + /** @brief Find an absolute path to a localized version of a given * relative path to a html or html related file. * If no localized version exists, an absolute path to the file