GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Functions
gnc-filepath-utils.h File Reference

File path resolution utility functions. More...

Go to the source code of this file.

Functions

gchar * gnc_resolve_file_path (const gchar *filefrag)
 Create an absolute path when given a relative path; otherwise return the argument. More...
 
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. If no localized version exists, an absolute path to the file is searched for. If that file doesn't exist either, returns NULL. More...
 
const gchar * gnc_dotgnucash_dir (void)
 Ensure that the user's configuration directory exists and is minimally populated. More...
 
gchar * gnc_build_dotgnucash_path (const gchar *filename)
 Make a path to filename in the user's configuration directory. More...
 
gchar * gnc_build_book_path (const gchar *filename)
 Make a path to filename in the book subdirectory of the user's configuration directory. More...
 
gchar * gnc_build_translog_path (const gchar *filename)
 Make a path to filename in the translog subdirectory of the user's configuration directory. More...
 
gchar * gnc_build_data_path (const gchar *filename)
 Make a path to filename in the data subdirectory of the user's configuration directory. More...
 
gchar * gnc_build_report_path (const gchar *filename)
 Make a path to filename in the report directory. More...
 
gchar * gnc_build_stdreports_path (const gchar *filename)
 Make a path to filename in the standard reports directory. More...
 
gchar * gnc_filepath_locate_pixmap (const gchar *name)
 
gchar * gnc_filepath_locate_data_file (const gchar *name)
 
gchar * gnc_filepath_locate_ui_file (const gchar *name)
 
gchar * gnc_filepath_locate_doc_file (const gchar *name)
 

Detailed Description

File path resolution utility functions.

Author
Copyright (c) 1998-2004 Linas Vepstas linas.nosp@m.@lin.nosp@m.as.or.nosp@m.g
Copyright (c) 2000 Dave Peticolas

Definition in file gnc-filepath-utils.h.

Function Documentation

gchar* gnc_build_book_path ( const gchar *  filename)

Make a path to filename in the book subdirectory of the user's configuration directory.

Parameters
filenameThe name of the file
Returns
An absolute path.

Definition at line 491 of file gnc-filepath-utils.c.

492 {
493  gchar* filename_dup = g_strdup(filename);
494  gchar* result = NULL;
495 
496  scrub_filename(filename_dup);
497  result = g_build_filename(gnc_dotgnucash_dir(), "books",
498  filename_dup, (gchar *)NULL);
499  g_free(filename_dup);
500  return result;
501 }
const gchar * gnc_dotgnucash_dir(void)
Ensure that the user's configuration directory exists and is minimally populated. ...
gchar* gnc_build_data_path ( const gchar *  filename)

Make a path to filename in the data subdirectory of the user's configuration directory.

Parameters
filenameThe name of the file
Returns
An absolute path.

Definition at line 533 of file gnc-filepath-utils.c.

534 {
535  gchar* filename_dup = g_strdup(filename);
536  gchar* result;
537 
538  scrub_filename(filename_dup);
539  result = g_build_filename(gnc_dotgnucash_dir(), "data", filename_dup, (gchar *)NULL);
540  g_free(filename_dup);
541  return result;
542 }
const gchar * gnc_dotgnucash_dir(void)
Ensure that the user's configuration directory exists and is minimally populated. ...
gchar* gnc_build_dotgnucash_path ( const gchar *  filename)

Make a path to filename in the user's configuration directory.

Parameters
filenameThe name of the file
Returns
An absolute path.

Definition at line 477 of file gnc-filepath-utils.c.

478 {
479  return g_build_filename(gnc_dotgnucash_dir(), filename, (gchar *)NULL);
480 }
const gchar * gnc_dotgnucash_dir(void)
Ensure that the user's configuration directory exists and is minimally populated. ...
gchar* gnc_build_report_path ( const gchar *  filename)

Make a path to filename in the report directory.

Parameters
filenameThe name of the file
Returns
An absolute path.

Definition at line 553 of file gnc-filepath-utils.c.

554 {
555  gchar *result = g_build_filename(gnc_path_get_reportdir(), filename, (gchar *)NULL);
556  return result;
557 }
gchar* gnc_build_stdreports_path ( const gchar *  filename)

Make a path to filename in the standard reports directory.

Parameters
filenameThe name of the file
Returns
An absolute path.

Definition at line 568 of file gnc-filepath-utils.c.

569 {
570  gchar *result = g_build_filename(gnc_path_get_stdreportsdir(), filename, (gchar *)NULL);
571  return result;
572 }
gchar* gnc_build_translog_path ( const gchar *  filename)

Make a path to filename in the translog subdirectory of the user's configuration directory.

Parameters
filenameThe name of the file
Returns
An absolute path.

Definition at line 512 of file gnc-filepath-utils.c.

513 {
514  gchar* filename_dup = g_strdup(filename);
515  gchar* result = NULL;
516 
517  scrub_filename(filename_dup);
518  result = g_build_filename(gnc_dotgnucash_dir(), "translog",
519  filename_dup, (gchar *)NULL);
520  g_free(filename_dup);
521  return result;
522 }
const gchar * gnc_dotgnucash_dir(void)
Ensure that the user's configuration directory exists and is minimally populated. ...
const gchar* gnc_dotgnucash_dir ( void  )

Ensure that the user's configuration directory exists and is minimally populated.

Note that the default path is $HOME/.gnucash; This can be changed by the environment variable $GNC_DOT_DIR.

Returns
An absolute path to the configuration directory

Definition at line 413 of file gnc-filepath-utils.c.

414 {
415  static gchar *dotgnucash = NULL;
416  gchar *tmp_dir;
417  gchar *errmsg = NULL;
418 
419  if (dotgnucash)
420  return dotgnucash;
421 
422  dotgnucash = g_strdup(g_getenv("GNC_DOT_DIR"));
423 
424  if (!dotgnucash)
425  {
426  const gchar *home = g_get_home_dir();
427  if (!home || !gnc_validate_directory(home, FALSE, &errmsg))
428  {
429  g_free(errmsg);
430  g_warning("Cannot find suitable home directory. Using tmp directory instead.");
431  home = g_get_tmp_dir();
432  }
433  g_assert(home);
434 
435  dotgnucash = g_build_filename(home, ".gnucash", (gchar *)NULL);
436  }
437  if (!gnc_validate_directory(dotgnucash, TRUE, &errmsg))
438  {
439  const gchar *tmp = g_get_tmp_dir();
440  g_free(errmsg);
441  g_free(dotgnucash);
442  g_warning("Cannot find suitable .gnucash directory in home directory. Using tmp directory instead.");
443  g_assert(tmp);
444 
445  dotgnucash = g_build_filename(tmp, ".gnucash", (gchar *)NULL);
446 
447  if (!gnc_validate_directory(dotgnucash, TRUE, &errmsg))
448  exit(1);
449  }
450 
451  /* Since we're in code that is only executed once.... */
452  tmp_dir = g_build_filename(dotgnucash, "books", (gchar *)NULL);
453  if (!gnc_validate_directory(tmp_dir, TRUE, &errmsg))
454  exit(1);
455  g_free(tmp_dir);
456  tmp_dir = g_build_filename(dotgnucash, "checks", (gchar *)NULL);
457  if (!gnc_validate_directory(tmp_dir, TRUE, &errmsg))
458  exit(1);
459  g_free(tmp_dir);
460  tmp_dir = g_build_filename(tmp_dir, "translog", (gchar *)NULL);
461  if (!gnc_validate_directory(dotgnucash, TRUE, &errmsg))
462  exit(1);
463  g_free(tmp_dir);
464 
465  return dotgnucash;
466 }
gchar* gnc_filepath_locate_data_file ( const gchar *  name)

Given a file name, find the file in the directories associated with this application. This routine will display an error message if it can't find the file.

Parameters
nameThe name of the file to be found.
Returns
the full path name of the file, or NULL of the file can't be found.
Note
It is the caller's responsibility to free the returned string.

Definition at line 599 of file gnc-filepath-utils.c.

600 {
601  return gnc_filepath_locate_file (gnc_path_get_pkgdatadir(), name);
602 }
gchar* gnc_filepath_locate_doc_file ( const gchar *  name)

Given a documentation file name, find the file in the doc directory associated with this application. This routine will display an error message if it can't find the file.

Parameters
nameThe name of the file to be found.
Returns
the full path name of the file, or NULL of the file can't be found.
Note
It is the caller's responsibility to free the returned string.

Definition at line 631 of file gnc-filepath-utils.c.

632 {
633  return gnc_filepath_locate_file (gnc_path_get_pkgdocdir(), name);
634 }
gchar* gnc_filepath_locate_pixmap ( const gchar *  name)

Given a pixmap/pixbuf file name, find the file in the pixmap directory associated with this application. This routine will display an error message if it can't find the file.

Parameters
nameThe name of the file to be found.
Returns
the full path name of the file, or NULL of the file can't be found.
Note
It is the caller's responsibility to free the returned string.

Definition at line 605 of file gnc-filepath-utils.c.

606 {
607  gchar *default_path;
608  gchar *fullname;
609 
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);
613 
614  return fullname;
615 }
gchar* gnc_filepath_locate_ui_file ( const gchar *  name)

Given a ui file name, find the file in the ui directory associated with this application. This routine will display an error message if it can't find the file.

Parameters
nameThe name of the file to be found.
Returns
the full path name of the file, or NULL of the file can't be found.
Note
It is the caller's responsibility to free the returned string.

Definition at line 618 of file gnc-filepath-utils.c.

619 {
620  gchar *default_path;
621  gchar *fullname;
622 
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);
626 
627  return fullname;
628 }
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. If no localized version exists, an absolute path to the file is searched for. If that file doesn't exist either, returns NULL.

Warning
file_name should be a simple path fragment. It shouldn't contain xml:// or http:// or <whatever>:// other protocol specifiers.

If passed a string which g_path_is_absolute declares an absolute path, return the argument.

Otherwise, assume that file_name is a well-formed relative path and try to find a file with its path relative to

  • a localized subdirectory in the html directory of the user's configuration directory (e.g. $HOME/.gnucash/html/de_DE, $HOME/.gnucash/html/en,...)
  • a localized subdirectory in the gnucash documentation directory (e.g. /usr/share/doc/gnucash/C,...)
  • the html directory of the user's configuration directory (e.g. $HOME/.gnucash/html)
  • the gnucash documentation directory (e.g. /usr/share/doc/gnucash/)

The paths are searched for in that order. If a matching file is found, return the absolute path to it.

If one isn't found, return NULL.

Parameters
file_nameThe file path to resolve
Returns
An absolute file path or NULL if no file is found.
Warning
file_name should be a simple path fragment. It shouldn't contain xml:// or http:// or <whatever>:// other protocol specifiers.

If passed a string which g_path_is_absolute declares an absolute path, return the argument.

Otherwise, assume that file_name is a well-formed relative path and try to find a file with its path relative to

  • a localized subdirectory in the html directory of the user's configuration directory (e.g. $HOME/.gnucash/html/de_DE, $HOME/.gnucash/html/en,...)
  • a localized subdirectory in the gnucash documentation directory (e.g. /usr/share/doc/gnucash/C,...)
  • the html directory of the user's configuration directory (e.g. $HOME/.gnucash/html)
  • the gnucash documentation directory (e.g. /usr/share/doc/gnucash/)
  • last resort option: the gnucash data directory (e.g. /usr/share/gnucash/)

The paths are searched for in that order. If a matching file is found, return the absolute path to it.

If one isn't found, return NULL.

Parameters
file_nameThe file path to resolve
Returns
An absolute file path or NULL if no file is found.

Definition at line 262 of file gnc-filepath-utils.c.

263 {
264  gchar *loc_file_name = NULL;
265  gchar *full_path = NULL;
266  const gchar * const *lang;
267  int i;
268 
269  if (!file_name || *file_name == '\0')
270  return NULL;
271 
272  /* An absolute path is returned unmodified. */
273  if (g_path_is_absolute (file_name))
274  return g_strdup (file_name);
275 
276  /* First try to find the file in any of the localized directories
277  * the user has set up on his system
278  */
279  for (lang = g_get_language_names (); *lang; lang++)
280  {
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)
285  return full_path;
286  }
287 
288  /* If not found in a localized directory, try to find the file
289  * in any of the base directories
290  */
291  return gnc_path_find_localized_html_file_internal (file_name);
292 
293 }
gchar* gnc_resolve_file_path ( const gchar *  filefrag)

Create an absolute path when given a relative path; otherwise return the argument.

The gnc_resolve_file_path() routine is a utility that will accept a fragmentary filename as input, and resolve it into a fully qualified path in the file system, i.e. a path that begins with a leading slash. First, the current working directory is searched for the file. Next, the directory $HOME/.gnucash/data, and finally, a list of other (configurable) paths. If the file is not found, then the path $HOME/.gnucash/data is used. If $HOME is not defined, then the current working directory is used.

Warning
filefrag should be a simple path fragment. It shouldn't contain xml:// or http:// or <whatever>:// other protocol specifiers.

If passed a string which g_path_is_absolute declares an absolute path, return the argument.

Otherwise, assume that filefrag is a well-formed relative path and try to find a file with its path relative to

  • the current working directory,
  • the installed system-wide data directory (e.g., /usr/local/share/gnucash),
  • the installed system configuration directory (e.g., /usr/local/etc/gnucash),
  • or in the user's configuration directory (e.g., $HOME/.gnucash/data)

The paths are searched for in that order. If a matching file is found, return the absolute path to it.

If one isn't found, return a absolute path relative to the user's configuration directory and note in the trace file that it needs to be created.

Parameters
filefragThe file path to resolve
Returns
An absolute file path.

Definition at line 124 of file gnc-filepath-utils.c.

125 {
126  gchar *fullpath = NULL, *tmp_path = NULL;
127 
128  /* seriously invalid */
129  if (!filefrag)
130  {
131  g_critical("filefrag is NULL");
132  return NULL;
133  }
134 
135  /* ---------------------------------------------------- */
136  /* OK, now we try to find or build an absolute file path */
137 
138  /* check for an absolute file path */
139  if (g_path_is_absolute(filefrag))
140  return g_strdup (filefrag);
141 
142  /* Look in the current working directory */
143  tmp_path = g_get_current_dir();
144  fullpath = g_build_filename(tmp_path, filefrag, (gchar *)NULL);
145  g_free(tmp_path);
146  fullpath = check_path_return_if_valid(fullpath);
147  if (fullpath != NULL)
148  return fullpath;
149 
150  /* Look in the data dir (e.g. $PREFIX/share/gnucash) */
151  tmp_path = gnc_path_get_pkgdatadir();
152  fullpath = g_build_filename(tmp_path, filefrag, (gchar *)NULL);
153  g_free(tmp_path);
154  fullpath = check_path_return_if_valid(fullpath);
155  if (fullpath != NULL)
156  return fullpath;
157 
158  /* Look in the config dir (e.g. $PREFIX/share/gnucash/accounts) */
159  tmp_path = gnc_path_get_accountsdir();
160  fullpath = g_build_filename(tmp_path, filefrag, (gchar *)NULL);
161  g_free(tmp_path);
162  fullpath = check_path_return_if_valid(fullpath);
163  if (fullpath != NULL)
164  return fullpath;
165 
166  /* Look in the users config dir (e.g. $HOME/.gnucash/data) */
167  fullpath = gnc_build_data_path(filefrag);
168  if (g_file_test(fullpath, G_FILE_TEST_IS_REGULAR))
169  return fullpath;
170 
171  /* OK, it's not there. Note that it needs to be created and pass it
172  * back anyway */
173  g_warning("create new file %s", fullpath);
174  return fullpath;
175 
176 }
gchar * gnc_build_data_path(const gchar *filename)
Make a path to filename in the data subdirectory of the user's configuration directory.