From f0eacba971be04bd52971a8a23263d38cde0e661 Mon Sep 17 00:00:00 2001
From: Ben Pfaff <blp@cs.stanford.edu>
Date: Sat, 24 Nov 2012 21:41:00 -0800
Subject: [PATCH] psppire-data-window: Only allow saving a file with at least
 one variable.

A data file with no variables can't be saved to a file.  Until now, this
wasn't detected until the user had already chosen a file name, and the
message used to report it was confusing: "SAVE is allowed only after the
active file has been defined."  With this commit, the menu item and toolbar
item for save operations is, instead, disabled if the file has no
variables.

Bug #30700.
Reported by eric thivant, via Harry Thijssen, with help from Michel
Boaventura.
---
 src/ui/gui/psppire-data-window.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/src/ui/gui/psppire-data-window.c b/src/ui/gui/psppire-data-window.c
index a2cfcbbffc..3a032f0a28 100644
--- a/src/ui/gui/psppire-data-window.c
+++ b/src/ui/gui/psppire-data-window.c
@@ -904,12 +904,25 @@ connect_action (PsppireDataWindow *dw, const char *action_name,
 				    GCallback handler)
 {
   GtkAction *action = get_action_assert (dw->builder, action_name);
- 
+
   g_signal_connect_swapped (action, "activate", handler, dw);
 
   return action;
 }
 
+/* Only a data file with at least one variable can be saved. */
+static void
+enable_save (PsppireDataWindow *dw)
+{
+  PsppireDict *dict = dw->var_store->dictionary;
+  gboolean enable = psppire_dict_get_var_cnt (dict) > 0;
+
+  gtk_action_set_sensitive (get_action_assert (dw->builder, "file_save"),
+                            enable);
+  gtk_action_set_sensitive (get_action_assert (dw->builder, "file_save_as"),
+                            enable);
+}
+
 /* Initializes as much of a PsppireDataWindow as we can and must before the
    dataset has been set.
 
@@ -1006,6 +1019,13 @@ psppire_data_window_finish_init (PsppireDataWindow *de,
 		    G_CALLBACK (on_split_change),
 		    de);
 
+  g_signal_connect_swapped (dict, "backend-changed",
+                            G_CALLBACK (enable_save), de);
+  g_signal_connect_swapped (dict, "variable-inserted",
+                            G_CALLBACK (enable_save), de);
+  g_signal_connect_swapped (dict, "variable-deleted",
+                            G_CALLBACK (enable_save), de);
+  enable_save (de);
 
   connect_action (de, "edit_copy", G_CALLBACK (on_edit_copy));
 
-- 
2.30.2