AUDIT: r21459 - gnucash/trunk/src - Handle specifying a directory as a dataset file.
John Ralls
jralls at code.gnucash.org
Tue Oct 18 20:03:00 EDT 2011
Author: jralls
Date: 2011-10-18 20:02:59 -0400 (Tue, 18 Oct 2011)
New Revision: 21459
Trac: http://svn.gnucash.org/trac/changeset/21459
Modified:
gnucash/trunk/src/gnome-utils/dialog-file-access.c
gnucash/trunk/src/gnome-utils/gnc-file.c
gnucash/trunk/src/libqof/qof/qofsession.c
Log:
Handle specifying a directory as a dataset file.
The file chooser dialog would return a directory if the user selected
one and clicked "Open"; then, or if the users specified a directory on
the command line, Gnucash would present the rather misleading error "No
backend found".
So, first, if the user selects a directory and clicks open, the chooser
will now open the directory for browsing, just as if she had
double-clicked on the directory name in the tree view.
Next, if a directory is presented to qof_session_begin it will detect
that and set ERR_BACKEND_BAD_URL, which gnc_post_file_open will detect
and re-present the file chooser, open to that directory. (To prevent
confusion, gnc_post_file_open will put up the error dialog for BAD_URL;
the new file chooser dialog will open after that's dismissed. Since
there are other possible causes of a BAD_URL, if the filename isn't a
directory the chooser will open to the registered default directory from
GConf.)
BP
Modified: gnucash/trunk/src/gnome-utils/dialog-file-access.c
===================================================================
--- gnucash/trunk/src/gnome-utils/dialog-file-access.c 2011-10-18 18:10:57 UTC (rev 21458)
+++ gnucash/trunk/src/gnome-utils/dialog-file-access.c 2011-10-19 00:02:59 UTC (rev 21459)
@@ -131,6 +131,13 @@
{
return;
}
+
+ if ( g_file_test( g_filename_from_uri( url, NULL, NULL ),
+ G_FILE_TEST_IS_DIR ))
+ {
+ gtk_file_chooser_set_current_folder_uri( faw->fileChooser, url );
+ return;
+ }
if ( faw->type == FILE_ACCESS_OPEN )
{
gnc_file_open_file( url );
Modified: gnucash/trunk/src/gnome-utils/gnc-file.c
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-file.c 2011-10-18 18:10:57 UTC (rev 21458)
+++ gnucash/trunk/src/gnome-utils/gnc-file.c 2011-10-19 00:02:59 UTC (rev 21459)
@@ -631,7 +631,7 @@
ENTER(" ");
-
+RESTART:
if (!filename) return FALSE;
/* Convert user input into a normalized uri
@@ -697,8 +697,26 @@
qof_session_begin (new_session, newfile, FALSE, FALSE, FALSE);
io_err = qof_session_get_error (new_session);
+
+ if (ERR_BACKEND_BAD_URL == io_err)
+ {
+ gchar *directory;
+ show_session_error (io_err, newfile, GNC_FILE_DIALOG_OPEN);
+ io_err = ERR_BACKEND_NO_ERR;
+ if (g_file_test (filename, G_FILE_TEST_IS_DIR))
+ directory = g_strdup (filename);
+ else
+ directory = gnc_get_default_directory (GCONF_DIR_OPEN_SAVE);
+
+ filename = gnc_file_dialog (NULL, NULL, directory,
+ GNC_FILE_DIALOG_OPEN);
+ qof_session_destroy (new_session);
+ new_session = NULL;
+ g_free (directory);
+ goto RESTART;
+ }
/* if file appears to be locked, ask the user ... */
- if (ERR_BACKEND_LOCKED == io_err || ERR_BACKEND_READONLY == io_err)
+ else if (ERR_BACKEND_LOCKED == io_err || ERR_BACKEND_READONLY == io_err)
{
GtkWidget *dialog;
gchar *displayname = NULL;
Modified: gnucash/trunk/src/libqof/qof/qofsession.c
===================================================================
--- gnucash/trunk/src/libqof/qof/qofsession.c 2011-10-18 18:10:57 UTC (rev 21458)
+++ gnucash/trunk/src/libqof/qof/qofsession.c 2011-10-19 00:02:59 UTC (rev 21459)
@@ -421,7 +421,7 @@
qof_session_begin (QofSession *session, const char * book_id,
gboolean ignore_lock, gboolean create, gboolean force)
{
- gchar **splituri;
+ gchar *scheme = NULL, *filename = NULL;
if (!session) return;
@@ -449,23 +449,35 @@
LEAVE("push error missing book_id");
return;
}
+ scheme = g_uri_parse_scheme (book_id);
+ if (g_strcmp0 (scheme, "file") == 0)
+ filename = g_filename_from_uri (book_id, NULL, NULL);
+ else if (!scheme)
+ filename = g_strdup (book_id);
+ if (filename && g_file_test (filename, G_FILE_TEST_IS_DIR))
+ {
+ if (ERR_BACKEND_NO_ERR == qof_session_get_error(session))
+ qof_session_push_error (session, ERR_BACKEND_BAD_URL, NULL);
+ g_free (filename);
+ g_free (scheme);
+ LEAVE("Can't open a directory");
+ return;
+ }
+
+
/* destroy the old backend */
qof_session_destroy_backend(session);
/* Store the session URL */
session->book_id = g_strdup (book_id);
- /* Look for something of the form of "file://", "http://" or
- * "postgres://". Everything before the colon is the access
- * method. Load the first backend found for that access method.
- */
- splituri = g_strsplit ( book_id, "://", 2 );
- if ( splituri[1] == NULL ) /* no access method in the uri, use generic "file" backend */
+ if (filename)
qof_session_load_backend(session, "file");
else /* access method found, load appropriate backend */
- qof_session_load_backend(session, splituri[0]);
- g_strfreev ( splituri );
+ qof_session_load_backend(session, scheme);
+ g_free (filename);
+ g_free (scheme);
/* No backend was found. That's bad. */
if (NULL == session->backend)
More information about the gnucash-changes
mailing list