r18741 - gnucash/trunk - Reverted 18699

John Ralls jralls at code.gnucash.org
Fri Feb 26 16:56:50 EST 2010


Author: jralls
Date: 2010-02-26 16:56:50 -0500 (Fri, 26 Feb 2010)
New Revision: 18741
Trac: http://svn.gnucash.org/trac/changeset/18741

Modified:
   gnucash/trunk/AUTHORS
   gnucash/trunk/src/bin/gnucash-setup-env-osx.in
   gnucash/trunk/src/engine/gnc-filepath-utils.c
   gnucash/trunk/src/engine/gnc-filepath-utils.h
Log:
Reverted 18699

Modified: gnucash/trunk/AUTHORS
===================================================================
--- gnucash/trunk/AUTHORS	2010-02-26 21:28:27 UTC (rev 18740)
+++ gnucash/trunk/AUTHORS	2010-02-26 21:56:50 UTC (rev 18741)
@@ -240,7 +240,6 @@
 Gavin Porter <maufk at csv.warwick.ac.uk> for euro style dates
 Tomas Pospisek <tpo at spin.ch> Debian patches
 Paul Poulain <famille.p.poulain at free.fr> French translations
-John Ralls <jralls at ceridwen.fremont.ca.us> MacOSX integration
 Ron Record <rr at sco.com> for SCO Unixware & OpenServer binaries
 Keith Refson <Keith.Refson at earth.ox.ac.uk> Solaris fixes
 Jerry Quinn <jlquinn at optonline.net> Bugfixes

Modified: gnucash/trunk/src/bin/gnucash-setup-env-osx.in
===================================================================
--- gnucash/trunk/src/bin/gnucash-setup-env-osx.in	2010-02-26 21:28:27 UTC (rev 18740)
+++ gnucash/trunk/src/bin/gnucash-setup-env-osx.in	2010-02-26 21:56:50 UTC (rev 18741)
@@ -33,7 +33,6 @@
 export LD_LIBRARY_PATH
 export DYLD_LIBRARY_PATH
 export GNC_STANDARD_REPORTS_DIR
-export GNC_DOT_DIR="$HOME/Library/Application Support/Gnucash"
 
 if test -z "$DBUS_SESSION_BUS_ADDRESS"; then
     eval `dbus-launch --sh-syntax --exit-with-session --config-file=@-PREFIX-@/etc/dbus-1/session.conf`

Modified: gnucash/trunk/src/engine/gnc-filepath-utils.c
===================================================================
--- gnucash/trunk/src/engine/gnc-filepath-utils.c	2010-02-26 21:28:27 UTC (rev 18740)
+++ gnucash/trunk/src/engine/gnc-filepath-utils.c	2010-02-26 21:56:50 UTC (rev 18741)
@@ -48,13 +48,133 @@
 #include <errno.h>
 
 #include "gnc-engine.h"
-#include "gnc-path.h"
 #include "gnc-filepath-utils.h"
 
-
 static QofLogModule log_module = GNC_MOD_BACKEND;
 
 
+/* ====================================================================== */
+/* 
+ * If $HOME/.gnucash/data directory doesn't exist, then create it.
+ */
+
+static void 
+MakeHomeDir (void) 
+{
+  const gchar *home;
+  char *path;
+  char *data;
+
+  /* Punt. Can't figure out where home is. */
+  home = g_get_home_dir();
+  if (!home) return;
+
+  path = g_build_filename(home, ".gnucash", (gchar *)NULL);
+
+  if (!g_file_test(path, G_FILE_TEST_EXISTS))
+  {
+    /* Go ahead and make it. Don't bother much with checking mkdir 
+     * for errors; seems pointless. */
+    g_mkdir (path, S_IRWXU);   /* perms = S_IRWXU = 0700 */
+  }
+
+  data = g_build_filename (path, "data", (gchar *)NULL);
+  if (!g_file_test(data, G_FILE_TEST_EXISTS))
+    g_mkdir (data, S_IRWXU);
+
+  g_free (path);
+  g_free (data);
+}
+
+/* ====================================================================== */
+
+/* XXX hack alert -- we should be yanking this out of some config file */
+/* These are obviously meant to be hard-coded paths to the gnucash
+   data file. That is insane. These should be thrown out
+   altogether. On non-Unix systems (Windows) these paths would not
+   only have different directory separator characters but these
+   would certainly be completely different paths. I'd vote to
+   throw this out completely. -- cstim, 2006-07-19 */
+static char * searchpaths[] =
+{
+   "/usr/share/gnucash/data",
+   "/usr/local/share/gnucash/data",
+   "/usr/share/gnucash/accounts",
+   "/usr/local/share/gnucash/accounts",
+   NULL,
+};
+
+typedef gboolean (*pathGenerator)(char *pathbuf, int which);
+
+static gboolean
+xaccCwdPathGenerator(char *pathbuf, int which)
+{
+    if(which != 0)
+    {
+        return FALSE;
+    }
+    else
+    {
+        /* try to find a file by this name in the cwd ... */
+        if (getcwd (pathbuf, PATH_MAX) == NULL)
+            return FALSE;
+
+        return TRUE;
+    }
+}
+
+static gboolean
+xaccDataPathGenerator(char *pathbuf, int which)
+{
+    if(which != 0)
+    {
+        return FALSE;
+    }
+    else
+    {
+        const gchar *home;
+	gchar *tmppath;
+    
+        home = g_get_home_dir ();
+        if (!home)
+            return FALSE;
+
+	tmppath = g_build_filename (home, ".gnucash", "data", (gchar *)NULL);
+        if (strlen(tmppath) >= PATH_MAX)
+	{
+	    g_free (tmppath);
+	    return FALSE;
+	}
+
+        g_strlcpy (pathbuf, tmppath, PATH_MAX);
+	g_free (tmppath);
+        return TRUE;
+    }
+}
+
+static gboolean
+xaccUserPathPathGenerator(char *pathbuf, int which)
+{
+    char *path = NULL;
+    
+    if(searchpaths[which] == NULL)
+    {
+        return FALSE;
+    }
+    else
+    {
+        path = searchpaths[which];
+        
+        if (PATH_MAX <= strlen(path))
+            return FALSE;
+
+        g_strlcpy (pathbuf, path, PATH_MAX);
+        return TRUE;
+    }
+}
+
+/* ====================================================================== */
+
 /**
  * Scrubs a filename by changing "strange" chars (e.g. those that are not
  * valid in a win32 file name) to "_".
@@ -74,36 +194,14 @@
     }
 }
 
-/** \fn gchar * check_file_return_if_true (path)
- *  \brief Check if the path exists and is a regular file
- *
- * \param path -- freed if the path doesn't exist or isn't a regular file
- *
- *  \return NULL or the path
- */
-
-static gchar *
-check_path_return_if_valid(gchar *path)
-{
-    ENTER("Path: %s", path);
-    if (g_file_test(path, G_FILE_TEST_IS_REGULAR))
-    {
-	LEAVE("found %s", path);
-	return path;
-    }
-    g_free (path);
-    return NULL;
-}
-
-char *
+char * 
 xaccResolveFilePath (const char * filefrag)
 {
   char pathbuf[PATH_MAX];
-/*  pathGenerator gens[4];*/
+  pathGenerator gens[4];
   char *filefrag_dup;
   int namelen;
   int i;
-  gchar *fullpath = NULL, *tmp_path = NULL;
 
   /* seriously invalid */
   if (!filefrag)
@@ -139,61 +237,73 @@
 	    return g_strdup( filefrag + 4);
   }
 
-
   /* get conservative on the length so that sprintf(getpid()) works ... */
   /* strlen ("/.LCK") + sprintf (%x%d) */
-  namelen = strlen (filefrag) + 25;
+  namelen = strlen (filefrag) + 25; 
 
-  /* Look in the current working directory */
-  tmp_path = g_get_current_dir();
-  fullpath = g_build_filename(tmp_path, filefrag, (gchar *)NULL);
-  g_free(tmp_path);
-  fullpath = check_path_return_if_valid(fullpath);
-  if (fullpath != NULL)
+  gens[0] = xaccCwdPathGenerator;
+  gens[1] = xaccDataPathGenerator;
+  gens[2] = xaccUserPathPathGenerator;
+  gens[3] = NULL;
+
+  for (i = 0; gens[i] != NULL; i++) 
   {
-      LEAVE("found %s", fullpath);
-      return fullpath;
-  }
+      int j;
+      for(j = 0; gens[i](pathbuf, j) ; j++)
+      {
+	  gchar *fullpath = g_build_filename(pathbuf, filefrag, (gchar *)NULL);
 
-  /* Look in the data dir (e.g. $PREFIX/share/gnucash) */
-  tmp_path = gnc_path_get_pkgdatadir();
-  fullpath = g_build_filename(tmp_path, filefrag, (gchar *)NULL);
-  g_free(tmp_path);
-  fullpath = check_path_return_if_valid(fullpath);
-  if (fullpath != NULL)
-   {
-      LEAVE("found %s", fullpath);
-      return fullpath;
+	  if (g_file_test(fullpath, G_FILE_TEST_IS_REGULAR))
+	  {
+	      LEAVE("found %s", fullpath);
+	      return fullpath;
+          }
+	  g_free (fullpath);
+      }
   }
+  /* OK, we didn't find the file. */
 
-  /* Look in the config dir (e.g. $PREFIX/etc/gnucash) */
-  tmp_path = gnc_path_get_accountsdir();
-  fullpath = g_build_filename(tmp_path, filefrag, (gchar *)NULL);
-  g_free(tmp_path);
-  fullpath = check_path_return_if_valid(fullpath);
-  if (fullpath != NULL)
+  /* make sure that the gnucash home dir exists. */
+  MakeHomeDir();
+
+  filefrag_dup = g_strdup (filefrag);
+
+  /* Replace "strange" chars with "_" for non-file backends. */
+  if (strstr (filefrag, "://"))
   {
-      LEAVE("found %s", fullpath);
-      return fullpath;
+	scrub_filename(filefrag_dup);
   }
 
-  /* Look in the users config dir (e.g. $HOME/.gnucash/data) */
-  fullpath = gnc_build_data_path(filefrag);
-  if (g_file_test(fullpath, G_FILE_TEST_IS_REGULAR))
+  /* Lets try creating a new file in $HOME/.gnucash/data */
+  if (xaccDataPathGenerator(pathbuf, 0))
   {
-      LEAVE("found %s", fullpath);
-      return fullpath;
+      gchar *result;
+      result = g_build_filename(pathbuf, filefrag_dup, (gchar *)NULL);
+      g_free (filefrag_dup);
+      LEAVE("create new file %s", result);
+      return result;
+  } 
+
+  /* OK, we still didn't find the file */
+  /* Lets try creating a new file in the cwd */
+  if (xaccCwdPathGenerator(pathbuf, 0))
+  {
+      gchar *result;
+      result = g_build_filename(pathbuf, filefrag_dup, (gchar *)NULL);
+      g_free (filefrag_dup);
+      LEAVE("create new file %s", result);
+      return result;
   }
-  /* OK, it's not there. Note that it needs to be created and pass it
-   * back anyway */
-  LEAVE("create new file %s", fullpath);
-  return fullpath;
 
+  g_free (filefrag_dup);
+
+  LEAVE("%s not found", filefrag);
+  return NULL;
 }
 
 /* ====================================================================== */
 
-char *
+char * 
 xaccResolveURL (const char * pathfrag)
 {
   GList* list;
@@ -209,7 +319,7 @@
    * to make sure the uri is in good form.
    */
 
-  if (!g_ascii_strncasecmp (pathfrag, "http://", 7) ||
+  if (!g_ascii_strncasecmp (pathfrag, "http://", 7)      ||
       !g_ascii_strncasecmp (pathfrag, "https://", 8))
   {
     return g_strdup(pathfrag);
@@ -230,6 +340,15 @@
 	}
   }
   g_list_free(list);
+
+  /* "file:" and "xml:" are handled specially */
+  if (!g_ascii_strncasecmp (pathfrag, "file:", 5)) {
+    return (xaccResolveFilePath (pathfrag));
+  }
+  if (!g_ascii_strncasecmp (pathfrag, "xml:", 4)) {
+    return (g_strdup_printf( "xml:%s", xaccResolveFilePath (pathfrag)) );
+  }
+
   return (xaccResolveFilePath (pathfrag));
 }
 
@@ -276,7 +395,7 @@
 		  "the file and start GnuCash again.\n"),
 		dirname);
       exit(1);
-
+      
     default:
       g_fprintf(stderr,
 		_("An unknown error occurred when validating that the\n"
@@ -311,19 +430,16 @@
 const gchar *
 gnc_dotgnucash_dir (void)
 {
-    static gchar *dotgnucash = NULL;
-    gchar *tmp_dir;
+  static gchar *dotgnucash = NULL, *tmp_dir;
+  const gchar *home;
 
   if (dotgnucash)
     return dotgnucash;
 
   dotgnucash = g_strdup(g_getenv("GNC_DOT_DIR"));
-
-  if (!dotgnucash)
-  {
-    const gchar *home = g_get_home_dir();
-    if (!home)
-    {
+  if (!dotgnucash) {
+    home = g_get_home_dir();
+    if (!home) {
       g_warning("Cannot find home directory. Using tmp directory instead.");
       home = g_get_tmp_dir();
     }
@@ -353,26 +469,13 @@
 gchar *
 gnc_build_book_path (const gchar *filename)
 {
-  gchar* filename_dup = g_strdup(filename);
-  gchar* result = NULL;
+  char* filename_dup = g_strdup(filename);
+  char* result;
 
   scrub_filename(filename_dup);
-  result = g_build_filename(gnc_dotgnucash_dir(), "books",
-			    filename_dup, (gchar *)NULL);
+  result = g_build_filename(gnc_dotgnucash_dir(), "books", filename_dup, (gchar *)NULL);
   g_free(filename_dup);
   return result;
 }
 
-gchar *
-gnc_build_data_path (const gchar *filename)
-{
-  gchar* filename_dup = g_strdup(filename);
-  gchar* result;
-
-  scrub_filename(filename_dup);
-  result = g_build_filename(gnc_dotgnucash_dir(), "data", filename_dup, (gchar *)NULL);
-  g_free(filename_dup);
-  return result;
-}
-
 /* =============================== END OF FILE ========================== */

Modified: gnucash/trunk/src/engine/gnc-filepath-utils.h
===================================================================
--- gnucash/trunk/src/engine/gnc-filepath-utils.h	2010-02-26 21:28:27 UTC (rev 18740)
+++ gnucash/trunk/src/engine/gnc-filepath-utils.h	2010-02-26 21:56:50 UTC (rev 18741)
@@ -49,6 +49,5 @@
 const gchar *gnc_dotgnucash_dir (void);
 gchar *gnc_build_dotgnucash_path (const gchar *filename);
 gchar *gnc_build_book_path (const gchar *filename);
-gchar *gnc_build_data_path (const gchar *filename);
 
 #endif /* GNC_FILEPATH_UTILS_H */



More information about the gnucash-changes mailing list