diff FrmMainWindow.cs @ 23:d661cb257511

Re #86: Initial GTK# GUI (because of Re #53) * Refactor file saving in to a common method * Replace fixed location with dialog
author IBBoard <dev@ibboard.co.uk>
date Mon, 17 Aug 2009 19:49:05 +0000
parents 2cf1af4c9baf
children 01ddadfa9653
line wrap: on
line diff
--- a/FrmMainWindow.cs	Mon Aug 17 19:17:35 2009 +0000
+++ b/FrmMainWindow.cs	Mon Aug 17 19:49:05 2009 +0000
@@ -701,22 +701,7 @@
 
 			if (CanSave())
 			{
-				try
-				{
-					if (WarFoundrySaver.GetSaver().Save(WarFoundryCore.CurrentArmy, loadedArmyPath))
-					{
-						saveArmyButton.Sensitive = false;
-						miSaveArmy.Sensitive = false;
-						CommandStack.setCleanMark();
-						success = true;
-					}
-				}
-				catch (IOException ex)
-				{
-					logger.Error("Saving army failed", ex);
-					MessageDialog md = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, "An error occured while saving the army. Please check the logs for details on what failed");
-					md.Show();
-				}
+				success = SaveArmyToPath(WarFoundryCore.CurrentArmy, loadedArmyPath);
 			}
 
 			return success;
@@ -724,21 +709,49 @@
 
 		private bool SaveCurrentArmyAs()
 		{
-			bool success = false;
-
-			if (WarFoundrySaver.GetSaver().Save(WarFoundryCore.CurrentArmy, Constants.UserDataPath+Constants.DirectoryString+"test.army"))
+			
+			FileChooserDialog fileDialog = new FileChooserDialog("Save file as", this, FileChooserAction.Save, "Cancel", ResponseType.Cancel, "Save", ResponseType.Accept);
+			FileFilter filter = new FileFilter();
+			filter.AddPattern("*.army");
+			filter.Name = "WarFoundry Army files (*.army)";
+			fileDialog.AddFilter(filter);
+			int response = fileDialog.Run();
+			string filePath = null;
+			
+			if (response == (int)ResponseType.Accept)
 			{
-				miSaveArmy.Sensitive = false;
-				//bttnSaveArmy.Sensitive = false;
-				CommandStack.setCleanMark();
-				loadedArmyPath = Constants.UserDataPath+Constants.DirectoryString+"test.army";
-				success = true;
+				filePath = fileDialog.Filename;
 			}
-			else
+			
+			fileDialog.Hide();			
+			fileDialog.Dispose();
+			
+			return SaveArmyToPath(WarFoundryCore.CurrentArmy, filePath);
+		}
+		
+		private bool SaveArmyToPath(Army army, string filePath)
+		{
+			bool success = false;
+			
+			if (filePath!=null)
 			{
-				//MessageBox.Show(this, Translation.GetTranslation("SaveFailed"), Translation.GetTranslation("SaveFailedTitle"), MessageBoxButtons.OK, MessageBoxIcon.Error);
+				if (WarFoundrySaver.GetSaver().Save(WarFoundryCore.CurrentArmy, filePath))
+				{
+					miSaveArmy.Sensitive = false;
+					saveArmyButton.Sensitive = false;
+					CommandStack.setCleanMark();
+					loadedArmyPath = filePath;
+					success = true;
+				}
+				else
+				{
+					MessageDialog dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, "Failed to save file to "+filePath);
+					dialog.Show();
+					dialog.Dispose();
+				}	
 			}
-
+			//else user cancelled
+			
 			return success;
 		}