32 #include <glib/gi18n.h>
33 #include <glib/gprintf.h>
34 #include <glib/gstdio.h>
39 #include <sys/types.h>
48 #include "libqof/qof/qof.h"
51 #include <glib/gwin32.h>
52 #define PATH_MAX MAXPATHLEN
66 scrub_filename(
char* filename)
70 #define STRANGE_CHARS "/:"
71 p = strpbrk(filename, STRANGE_CHARS);
75 p = strpbrk(filename, STRANGE_CHARS);
86 check_path_return_if_valid(gchar *path)
88 if (g_file_test(path, G_FILE_TEST_IS_REGULAR))
126 gchar *fullpath = NULL, *tmp_path = NULL;
131 g_critical(
"filefrag is NULL");
139 if (g_path_is_absolute(filefrag))
140 return g_strdup (filefrag);
143 tmp_path = g_get_current_dir();
144 fullpath = g_build_filename(tmp_path, filefrag, (gchar *)NULL);
146 fullpath = check_path_return_if_valid(fullpath);
147 if (fullpath != NULL)
151 tmp_path = gnc_path_get_pkgdatadir();
152 fullpath = g_build_filename(tmp_path, filefrag, (gchar *)NULL);
154 fullpath = check_path_return_if_valid(fullpath);
155 if (fullpath != NULL)
159 tmp_path = gnc_path_get_accountsdir();
160 fullpath = g_build_filename(tmp_path, filefrag, (gchar *)NULL);
162 fullpath = check_path_return_if_valid(fullpath);
163 if (fullpath != NULL)
168 if (g_file_test(fullpath, G_FILE_TEST_IS_REGULAR))
173 g_warning(
"create new file %s", fullpath);
192 gnc_path_find_localized_html_file_internal (
const gchar * file_name)
194 gchar *full_path = NULL;
196 const gchar *env_doc_path = g_getenv(
"GNC_DOC_PATH");
197 const gchar *default_dirs[] =
200 gnc_path_get_pkgdocdir (),
201 gnc_path_get_pkgdatadir (),
206 if (!file_name || *file_name ==
'\0')
211 dirs = g_strsplit (env_doc_path, G_SEARCHPATH_SEPARATOR_S, -1);
213 dirs = (gchar **)default_dirs;
215 for (i = 0; dirs[i]; i++)
217 full_path = g_build_filename (dirs[i], file_name, (gchar *)NULL);
218 DEBUG (
"Checking for existence of %s", full_path);
219 full_path = check_path_return_if_valid (full_path);
220 if (full_path != NULL)
264 gchar *loc_file_name = NULL;
265 gchar *full_path = NULL;
266 const gchar *
const *lang;
269 if (!file_name || *file_name ==
'\0')
273 if (g_path_is_absolute (file_name))
274 return g_strdup (file_name);
279 for (lang = g_get_language_names (); *lang; lang++)
281 loc_file_name = g_build_filename (*lang, file_name, (gchar *)NULL);
282 full_path = gnc_path_find_localized_html_file_internal (loc_file_name);
283 g_free (loc_file_name);
284 if (full_path != NULL)
291 return gnc_path_find_localized_html_file_internal (file_name);
303 gnc_validate_directory (
const gchar *dirname, gboolean create, gchar **msg)
309 rc = g_stat (dirname, &statbuf);
317 rc = g_mkdir (dirname,
326 *msg = g_strdup_printf(
327 _(
"An error occurred while creating the directory:\n"
329 "Please correct the problem and restart GnuCash.\n"
330 "The reported error was '%s' (errno %d).\n"),
331 dirname, g_strerror(errno) ? g_strerror(errno) :
"", errno);
337 *msg = g_strdup_printf(
338 _(
"Note: the directory\n"
340 "doesn't exist. This is however not fatal.\n"),
344 g_stat (dirname, &statbuf);
348 *msg = g_strdup_printf(
351 "exists but cannot be accessed. This program \n"
352 "must have full access (read/write/execute) to \n"
353 "the directory in order to function properly.\n"),
358 *msg = g_strdup_printf(
361 "exists but it is not a directory. Please delete\n"
362 "the file and start GnuCash again.\n"),
367 *msg = g_strdup_printf(
368 _(
"An unknown error occurred when validating that the\n"
370 "directory exists and is usable. Please correct the\n"
371 "problem and restart GnuCash. The reported error \n"
372 "was '%s' (errno %d)."),
373 dirname, g_strerror(errno) ? g_strerror(errno) :
"", errno);
378 if ((statbuf.st_mode & S_IFDIR) != S_IFDIR)
380 *msg = g_strdup_printf(
383 "exists but it is not a directory. Please delete\n"
384 "the file and start GnuCash again.\n"),
390 if ((statbuf.st_mode & S_IRWXU) != S_IRWXU)
392 *msg = g_strdup_printf(
393 _(
"The permissions are wrong on the directory\n"
395 "They must be at least 'rwx' for the user.\n"),
415 static gchar *dotgnucash = NULL;
417 gchar *errmsg = NULL;
422 dotgnucash = g_strdup(g_getenv(
"GNC_DOT_DIR"));
426 const gchar *home = g_get_home_dir();
427 if (!home || !gnc_validate_directory(home, FALSE, &errmsg))
430 g_warning(
"Cannot find suitable home directory. Using tmp directory instead.");
431 home = g_get_tmp_dir();
435 dotgnucash = g_build_filename(home,
".gnucash", (gchar *)NULL);
437 if (!gnc_validate_directory(dotgnucash, TRUE, &errmsg))
439 const gchar *tmp = g_get_tmp_dir();
442 g_warning(
"Cannot find suitable .gnucash directory in home directory. Using tmp directory instead.");
445 dotgnucash = g_build_filename(tmp,
".gnucash", (gchar *)NULL);
447 if (!gnc_validate_directory(dotgnucash, TRUE, &errmsg))
452 tmp_dir = g_build_filename(dotgnucash,
"books", (gchar *)NULL);
453 if (!gnc_validate_directory(tmp_dir, TRUE, &errmsg))
456 tmp_dir = g_build_filename(dotgnucash,
"checks", (gchar *)NULL);
457 if (!gnc_validate_directory(tmp_dir, TRUE, &errmsg))
460 tmp_dir = g_build_filename(tmp_dir,
"translog", (gchar *)NULL);
461 if (!gnc_validate_directory(dotgnucash, TRUE, &errmsg))
493 gchar* filename_dup = g_strdup(filename);
494 gchar* result = NULL;
496 scrub_filename(filename_dup);
498 filename_dup, (gchar *)NULL);
499 g_free(filename_dup);
514 gchar* filename_dup = g_strdup(filename);
515 gchar* result = NULL;
517 scrub_filename(filename_dup);
519 filename_dup, (gchar *)NULL);
520 g_free(filename_dup);
535 gchar* filename_dup = g_strdup(filename);
538 scrub_filename(filename_dup);
540 g_free(filename_dup);
555 gchar *result = g_build_filename(gnc_path_get_reportdir(), filename, (gchar *)NULL);
570 gchar *result = g_build_filename(gnc_path_get_stdreportsdir(), filename, (gchar *)NULL);
575 gnc_filepath_locate_file (
const gchar *default_path,
const gchar *name)
579 g_return_val_if_fail (name != NULL, NULL);
581 if (g_path_is_absolute (name))
582 fullname = g_strdup (name);
583 else if (default_path)
584 fullname = g_build_filename (default_path, name, NULL);
588 if (!g_file_test (fullname, G_FILE_TEST_IS_REGULAR))
590 g_warning (
"Could not locate file %s", name);
601 return gnc_filepath_locate_file (gnc_path_get_pkgdatadir(), name);
610 default_path = g_build_filename (gnc_path_get_pkgdatadir (),
"pixmaps", NULL);
611 fullname = gnc_filepath_locate_file (default_path, name);
612 g_free(default_path);
623 default_path = g_build_filename (gnc_path_get_pkgdatadir (),
"ui", NULL);
624 fullname = gnc_filepath_locate_file (default_path, name);
625 g_free(default_path);
633 return gnc_filepath_locate_file (gnc_path_get_pkgdocdir(), name);
gchar * gnc_filepath_locate_data_file(const gchar *name)
gchar * gnc_build_book_path(const gchar *filename)
Make a path to filename in the book subdirectory of the user's configuration directory.
gchar * gnc_build_data_path(const gchar *filename)
Make a path to filename in the data subdirectory of the user's configuration directory.
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
#define DEBUG(format, args...)
gchar * gnc_filepath_locate_ui_file(const gchar *name)
gchar * gnc_build_dotgnucash_path(const gchar *filename)
Make a path to filename in the user's configuration directory.
gchar * gnc_resolve_file_path(const gchar *filefrag)
Create an absolute path when given a relative path; otherwise return the argument.
gchar * gnc_build_stdreports_path(const gchar *filename)
Make a path to filename in the standard reports directory.
gchar * gnc_filepath_locate_doc_file(const gchar *name)
gchar * gnc_build_report_path(const gchar *filename)
Make a path to filename in the report directory.
gchar * gnc_filepath_locate_pixmap(const gchar *name)
gchar * gnc_build_translog_path(const gchar *filename)
Make a path to filename in the translog subdirectory of the user's configuration directory.
gchar * gnc_path_find_localized_html_file(const gchar *file_name)
Find an absolute path to a localized version of a given relative path to a html or html related file...
const gchar * gnc_dotgnucash_dir(void)
Ensure that the user's configuration directory exists and is minimally populated. ...
File path resolution utility functions.
const gchar * QofLogModule