r19687 - gnucash/trunk/src/gnome-utils - Bug 632166: Restore the old session if "save as" fails.

John Ralls jralls at code.gnucash.org
Tue Oct 19 23:35:03 EDT 2010


Author: jralls
Date: 2010-10-19 23:35:02 -0400 (Tue, 19 Oct 2010)
New Revision: 19687
Trac: http://svn.gnucash.org/trac/changeset/19687

Modified:
   gnucash/trunk/src/gnome-utils/gnc-file.c
Log:
Bug 632166: Restore the old session if "save as" fails.

Save as would make a new session the "current session" and attempt to
save it.  If it failed, the new session (with an invalid database)
would remain the current session -- and would be marked clean. An
XML-only file-save-as dialog would be presented with no instructions
about what was going on.

With this change, if the "save as" fails, the old session is restored,
still marked unsaved. This should be much less confusing to users and
more likely to avoid data loss.  

Yes, there's some duplicated code now, because do_save_as doesn't call
save. That's because save did some extra things that do_save_as
doesn't need and it didn't allow do_save_as to properly correct the
session. This could be refactored into some tiny functions, but that
seems a bit excessive to me.



Modified: gnucash/trunk/src/gnome-utils/gnc-file.c
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-file.c	2010-10-20 02:59:47 UTC (rev 19686)
+++ gnucash/trunk/src/gnome-utils/gnc-file.c	2010-10-20 03:35:02 UTC (rev 19687)
@@ -1122,7 +1122,7 @@
 
 /* Note: this dialog will only be used when dbi is not enabled
  *       paths used in it always refer to files and are
- *       never db uris
+ *       never db uris. See gnc_file_do_save_as for that.
  */
 void
 gnc_file_save_as (void)
@@ -1308,20 +1308,49 @@
     /* if we got to here, then we've successfully gotten a new session */
     /* close up the old file session (if any) */
     qof_session_swap_data (session, new_session);
-    gnc_clear_current_session();
-    session = NULL;
 
     /* XXX At this point, we should really mark the data in the new session
      * as being 'dirty', since we haven't saved it at all under the new
      * session. But I'm lazy...
      */
-    gnc_set_current_session(new_session);
 
     qof_event_resume();
 
+
+    gnc_set_busy_cursor (NULL, TRUE);
+    gnc_window_show_progress(_("Writing file..."), 0.0);
+    qof_session_save (new_session, gnc_window_show_progress);
+    gnc_window_show_progress(NULL, -1.0);
+    gnc_unset_busy_cursor (NULL);
+
+    io_err = qof_session_get_error( new_session );
+    if ( ERR_BACKEND_NO_ERR != io_err )
+    {
+/* Well, poop. The save failed, so the new session is invalid and we
+ * need to restore the old one.
+ */
+        show_session_error (io_err, newfile, GNC_FILE_DIALOG_SAVE);
+	qof_event_suspend();
+	qof_session_swap_data( new_session, session );
+	qof_session_destroy( new_session );
+	new_session = NULL;
+	qof_event_resume();
+    }
+    else
+    {
+/* Yay! Save was successful, we can dump the old session */
+	qof_event_suspend();
+	gnc_clear_current_session();
+	gnc_set_current_session( new_session );
+	qof_event_resume();
+	session = NULL;
+
+	xaccReopenLog();
+	gnc_add_history (session);
+	gnc_hook_run(HOOK_BOOK_SAVED, session);
+    }
     /* --------------- END CORE SESSION CODE -------------- */
 
-    gnc_file_save ();
     save_in_progress--;
 
     g_free (newfile);



More information about the gnucash-changes mailing list