diff --git a/src/gnome-utils/gnc-main-window.c b/src/gnome-utils/gnc-main-window.c index 3562f099cc..2cf256d847 100644 --- a/src/gnome-utils/gnc-main-window.c +++ b/src/gnome-utils/gnc-main-window.c @@ -657,6 +657,29 @@ gnc_main_window_restore_window (GncMainWindow *window, GncMainWindowSaveData *da window, data, data->key_file, data->window_num); window_group = g_strdup_printf(WINDOW_STRING, data->window_num + 1); + /* Deal with the uncommon case that the state file defines a window + * but no pages. An example to get in such a situation can be found + * here: https://bugzilla.gnome.org/show_bug.cgi?id=436479#c3 + * If this happens on the first window, we will open an account hierarchy + * to avoid confusing the user by presenting a completely empty window. + * If it happens on a later window, we'll just skip restoring that window. + */ + if (!g_key_file_has_group (data->key_file, window_group) || + !g_key_file_has_key (data->key_file, window_group, WINDOW_PAGECOUNT, &error)) + { + if (window) + { + gnc_main_window_restore_default_state (window); + PINFO ("saved state had an empty first main window\n" + "an account hierarchy page was added automatically to avoid confusion"); + } + else + PINFO ("saved state had an empty main window, skipping restore"); + + goto cleanup; + } + + /* Get this window's notebook info */ page_count = g_key_file_get_integer(data->key_file, window_group, WINDOW_PAGECOUNT, &error); @@ -668,7 +691,7 @@ gnc_main_window_restore_window (GncMainWindow *window, GncMainWindowSaveData *da } if (page_count == 0) { - /* Shound never happen, but has during alpha testing. Having this + /* Should never happen, but has during alpha testing. Having this * check doesn't hurt anything. */ goto cleanup; } @@ -911,15 +934,15 @@ gnc_main_window_restore_all_windows(const GKeyFile *keyfile) } void -gnc_main_window_restore_default_state(void) +gnc_main_window_restore_default_state(GncMainWindow *window) { GtkAction *action; - GncMainWindow *window; /* The default state should be to have an Account Tree page open * in the window. */ DEBUG("no saved state file"); - window = g_list_nth_data(active_windows, 0); + if (!window) + window = g_list_nth_data(active_windows, 0); action = gnc_main_window_find_action(window, "ViewAccountTreeAction"); gtk_action_activate(action); } diff --git a/src/gnome-utils/gnc-main-window.h b/src/gnome-utils/gnc-main-window.h index b1f924a086..9c1cb4a0cf 100644 --- a/src/gnome-utils/gnc-main-window.h +++ b/src/gnome-utils/gnc-main-window.h @@ -336,7 +336,7 @@ void gnc_main_window_save_all_windows(GKeyFile *keyfile); /** Restore the persistent state of one window to a sane default. */ -void gnc_main_window_restore_default_state(void); +void gnc_main_window_restore_default_state(GncMainWindow *window); /** * gnc_gtk_action_group_set_translation_domain: diff --git a/src/gnome/top-level.c b/src/gnome/top-level.c index 4b4ca14e24..33e1dd875f 100644 --- a/src/gnome/top-level.c +++ b/src/gnome/top-level.c @@ -263,7 +263,7 @@ gnc_restore_all_state (gpointer session, gpointer unused) STATE_FILE_BOOK_GUID, &error); if (error) { - gnc_main_window_restore_default_state(); + gnc_main_window_restore_default_state(NULL); g_warning("error reading group %s key %s: %s", STATE_FILE_TOP, STATE_FILE_BOOK_GUID, error->message); LEAVE("no guid in state file");