From: John Darrington <john@darrington.wattle.id.au>
Date: Wed, 25 Feb 2009 00:54:45 +0000 (+0900)
Subject: Prompt for save on closing data window
X-Git-Tag: v0.7.3~275
X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=211e4f7fcacb8aab37e2fbc3e7387fb448f8eb56;p=pspp-builds.git

Prompt for save on closing data window
---

diff --git a/src/ui/gui/psppire-data-window.c b/src/ui/gui/psppire-data-window.c
index ac5ce610..84fc5071 100644
--- a/src/ui/gui/psppire-data-window.c
+++ b/src/ui/gui/psppire-data-window.c
@@ -339,9 +339,6 @@ psppire_data_window_load_file (PsppireDataWindow *de,
     }
 
   psppire_window_set_unsaved (PSPPIRE_WINDOW (de), FALSE);
-  free (de->file_name);
-
-  de->file_name = g_strdup (file_name);
 }
 
 
@@ -376,13 +373,16 @@ open_data_dialog (GtkAction *action, PsppireDataWindow *de)
   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
 
 
-  if (de->file_name)
-    {
-      gchar *dir_name = g_path_get_dirname (de->file_name);
-      gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
-					   dir_name);
-      free (dir_name);
-    }
+  {
+    gchar *dir_name;
+    gchar *filename = NULL;
+    g_object_get (de, "filename", &filename, NULL);
+
+    dir_name = g_path_get_dirname (filename);
+    gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
+					 dir_name);
+    free (dir_name);
+  }
 
   switch (gtk_dialog_run (GTK_DIALOG (dialog)))
     {
@@ -420,39 +420,38 @@ name_has_suffix (const gchar *name)
 }
 
 
-/* Append SUFFIX to the filename of DE */
-static void
-append_filename_suffix (PsppireDataWindow *de, const gchar *suffix)
-{
-  if ( ! name_has_suffix (de->file_name))
-    {
-      gchar *s = de->file_name;
-      de->file_name = g_strconcat (de->file_name, suffix, NULL);
-      g_free (s);
-    }
-}
-
 /* Save DE to file */
 static void
 save_file (PsppireDataWindow *de)
 {
+  gchar *fn = NULL;
+  GString *fnx;
   struct getl_interface *sss;
   struct string file_name ;
 
-  g_assert (de->file_name);
+  g_object_get (de, "filename", &fn, NULL);
+
+  fnx = g_string_new (fn);
+
+  if ( ! name_has_suffix (fnx->str))
+    {
+      if ( de->save_as_portable)
+	g_string_append (fnx, ".por");
+      else
+	g_string_append (fnx, ".sav");
+    }
 
   ds_init_empty (&file_name);
-  syntax_gen_string (&file_name, ss_cstr (de->file_name));
+  syntax_gen_string (&file_name, ss_cstr (fnx->str));
+  g_string_free (fnx, FALSE);
 
   if ( de->save_as_portable )
     {
-      append_filename_suffix (de, ".por");
       sss = create_syntax_string_source ("EXPORT OUTFILE=%s.",
 					 ds_cstr (&file_name));
     }
   else
     {
-      append_filename_suffix (de, ".sav");
       sss = create_syntax_string_source ("SAVE OUTFILE=%s.",
 					 ds_cstr (&file_name));
     }
@@ -534,22 +533,28 @@ data_save_as_dialog (PsppireDataWindow *de)
     {
     case GTK_RESPONSE_ACCEPT:
       {
-	g_free (de->file_name);
-
-	de->file_name =
-	  gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
+	GString *filename =
+	  g_string_new
+	  (
+	   gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog))
+	   );
 
 	de->save_as_portable =
 	  ! gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_sys));
 
-	if ( de->save_as_portable)
-	  append_filename_suffix (de, ".por");
-	else
-	  append_filename_suffix (de, ".sav");
+	if ( ! name_has_suffix (filename->str))
+	  {
+	    if ( de->save_as_portable)
+	      g_string_append (filename, ".por");
+	    else
+	      g_string_append (filename, ".sav");
+	  }
+
+	psppire_window_set_filename (PSPPIRE_WINDOW (de), filename->str);
 
 	save_file (de);
 
-	psppire_window_set_filename (PSPPIRE_WINDOW (de), de->file_name);
+	g_string_free (filename, TRUE);
       }
       break;
     default:
@@ -561,12 +566,13 @@ data_save_as_dialog (PsppireDataWindow *de)
 
 
 /* Callback for data_save action.
-   If there's an existing file name, then just save,
-   otherwise prompt for a file name, then save */
+ */
 static void
 data_save (PsppireDataWindow *de)
 {
-  if (de->file_name)
+  const gchar *fn = psppire_window_get_filename (PSPPIRE_WINDOW (de));
+
+  if ( NULL != fn)
     save_file (de);
   else
     data_save_as_dialog (de);
@@ -583,9 +589,6 @@ new_file (GtkAction *action, PsppireDataWindow *de)
 
   execute_syntax (sss);
 
-  g_free (de->file_name);
-  de->file_name = NULL;
-
   psppire_window_set_filename (PSPPIRE_WINDOW (de), NULL);
 }
 
@@ -1001,6 +1004,11 @@ on_delete (GtkWidget *w, GdkEvent *event, gpointer user_data)
 {
   PsppireDataWindow *dw = PSPPIRE_DATA_WINDOW (user_data);
 
+  if (psppire_window_query_save (PSPPIRE_WINDOW (dw)))
+    {
+      data_save (dw);
+    }
+
   return FALSE;
 }
 
@@ -1023,8 +1031,6 @@ psppire_data_window_init (PsppireDataWindow *de)
   hb = get_widget_assert (de->builder, "handlebox1");
   sb = get_widget_assert (de->builder, "status-bar");
 
-  de->file_name = NULL;
-
   de->data_editor =
     PSPPIRE_DATA_EDITOR (psppire_data_editor_new (the_var_store, the_data_store));
 
diff --git a/src/ui/gui/psppire-data-window.h b/src/ui/gui/psppire-data-window.h
index ecf86bb9..bd9f516e 100644
--- a/src/ui/gui/psppire-data-window.h
+++ b/src/ui/gui/psppire-data-window.h
@@ -65,11 +65,6 @@ struct _PsppireDataWindow
 
 
   gboolean save_as_portable;
-
-  /* Name of the file this data is associated with (ie, was loaded from or
-     has been  saved to), in "filename encoding",  or NULL, if it's not
-     associated with any file */
-  gchar *file_name;
 };
 
 struct _PsppireDataWindowClass
diff --git a/src/ui/gui/psppire-syntax-window.c b/src/ui/gui/psppire-syntax-window.c
index 7abf01f7..07e73c13 100644
--- a/src/ui/gui/psppire-syntax-window.c
+++ b/src/ui/gui/psppire-syntax-window.c
@@ -255,67 +255,6 @@ save_editor_to_file (PsppireSyntaxWindow *se,
   return result;
 }
 
-/* If the buffer's modified flag is set, then save it, and close the window.
-   Otherwise just close the window.
-*/
-static void
-save_if_modified (PsppireWindow *se)
-{
-  if ( TRUE == psppire_window_get_unsaved (se))
-    {
-      gint response;
-      GtkWidget *dialog;
-
-      const gchar *description;
-      const gchar *filename = psppire_window_get_filename (se);
-
-      g_object_get (se, "description", &description, NULL);
-
-      g_return_if_fail (filename != NULL);
-
-      dialog =
-	gtk_message_dialog_new (GTK_WINDOW (se),
-				GTK_DIALOG_MODAL,
-				GTK_MESSAGE_QUESTION,
-				GTK_BUTTONS_NONE,
-				_("Save contents of %s to \"%s\"?"),
-				description,
-				filename);
-
-      gtk_dialog_add_button  (GTK_DIALOG (dialog),
-			      GTK_STOCK_YES,
-			      GTK_RESPONSE_ACCEPT);
-
-      gtk_dialog_add_button  (GTK_DIALOG (dialog),
-			      GTK_STOCK_NO,
-			      GTK_RESPONSE_REJECT);
-
-      gtk_dialog_add_button  (GTK_DIALOG (dialog),
-			      GTK_STOCK_CANCEL,
-			      GTK_RESPONSE_CANCEL);
-
-      response = gtk_dialog_run (GTK_DIALOG (dialog));
-
-      gtk_widget_destroy (dialog);
-
-      if ( response == GTK_RESPONSE_ACCEPT )
-	{
-	  GError *err = NULL;
-
-	  if ( ! save_editor_to_file (se, filename, &err) )
-	    {
-	      msg (ME, err->message);
-	      g_error_free (err);
-	    }
-	  psppire_window_set_filename (se, filename);
-	}
-
-      if ( response == GTK_RESPONSE_CANCEL )
-	return ;
-    }
-
-  gtk_widget_destroy (GTK_WIDGET (se));
-}
 
 /* Callback for the File->SaveAs menuitem */
 static void
@@ -395,8 +334,8 @@ on_syntax_save (GtkMenuItem *menuitem, gpointer user_data)
 static gboolean
 on_quit (GtkMenuItem *menuitem, gpointer    user_data)
 {
-  PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
-  save_if_modified (se);
+  PsppireWindow *se = PSPPIRE_WINDOW (user_data);
+
   return FALSE;
 }
 
@@ -406,9 +345,22 @@ on_quit (GtkMenuItem *menuitem, gpointer    user_data)
 static gboolean
 on_delete (GtkWidget *w, GdkEvent *event, gpointer user_data)
 {
-  PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
+  PsppireWindow *se = PSPPIRE_WINDOW (user_data);
+
+  if ( psppire_window_query_save (se) )
+    {
+      gchar *filename = NULL;
+      GError *err = NULL;
+
+      g_object_get (se, "filename", &filename, NULL);
+
+      if ( ! save_editor_to_file (se, filename, &err) )
+	{
+	  msg (ME, err->message);
+	  g_error_free (err);
+	}
+    }
 
-  save_if_modified (se);
   return FALSE;
 }
 
diff --git a/src/ui/gui/psppire-window.c b/src/ui/gui/psppire-window.c
index 61bebb28..1e2e75d3 100644
--- a/src/ui/gui/psppire-window.c
+++ b/src/ui/gui/psppire-window.c
@@ -16,6 +16,8 @@
 
 #include <config.h>
 
+#include <gtk/gtkstock.h>
+#include <gtk/gtkmessagedialog.h>
 #include <gtk/gtksignal.h>
 #include <gtk/gtkwindow.h>
 #include <gtk/gtkcheckmenuitem.h>
@@ -388,6 +390,60 @@ psppire_window_init (PsppireWindow *window)
 }
 
 
+/* If the buffer's modified flag is set,
+   ask the user if the buffer should be saved.
+   Return TRUE if is should.
+*/
+gboolean
+psppire_window_query_save (PsppireWindow *se)
+{
+  gint response;
+  GtkWidget *dialog;
+
+  const gchar *description;
+  const gchar *filename = psppire_window_get_filename (se);
+
+  if ( ! psppire_window_get_unsaved (se))
+    return FALSE;
+
+  g_object_get (se, "description", &description, NULL);
+
+  g_return_val_if_fail (filename != NULL, FALSE);
+
+  dialog =
+    gtk_message_dialog_new (GTK_WINDOW (se),
+			    GTK_DIALOG_MODAL,
+			    GTK_MESSAGE_QUESTION,
+			    GTK_BUTTONS_NONE,
+			    _("Save contents of %s to \"%s\"?"),
+			    description,
+			    filename);
+
+  gtk_dialog_add_button  (GTK_DIALOG (dialog),
+			  GTK_STOCK_YES,
+			  GTK_RESPONSE_ACCEPT);
+
+  gtk_dialog_add_button  (GTK_DIALOG (dialog),
+			  GTK_STOCK_NO,
+			  GTK_RESPONSE_REJECT);
+
+  gtk_dialog_add_button  (GTK_DIALOG (dialog),
+			  GTK_STOCK_CANCEL,
+			  GTK_RESPONSE_CANCEL);
+
+  response = gtk_dialog_run (GTK_DIALOG (dialog));
+
+  gtk_widget_destroy (dialog);
+
+  if ( response == GTK_RESPONSE_ACCEPT )
+    {
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
+
 const gchar *
 psppire_window_get_filename (PsppireWindow *w)
 {
diff --git a/src/ui/gui/psppire-window.h b/src/ui/gui/psppire-window.h
index 3d1873e4..ab0efc3f 100644
--- a/src/ui/gui/psppire-window.h
+++ b/src/ui/gui/psppire-window.h
@@ -76,6 +76,10 @@ void psppire_window_set_unsaved (PsppireWindow *, gboolean );
 
 gboolean psppire_window_get_unsaved (PsppireWindow *);
 
+
+gboolean psppire_window_query_save (PsppireWindow *);
+
+
 G_END_DECLS
 
 #endif /* __PSPPIRE_WINDOW_H__ */