diff --git a/Libraries/UniversalEditor.UserInterface/MainWindow.cs b/Libraries/UniversalEditor.UserInterface/MainWindow.cs index 487bb859..be0a3fc4 100644 --- a/Libraries/UniversalEditor.UserInterface/MainWindow.cs +++ b/Libraries/UniversalEditor.UserInterface/MainWindow.cs @@ -1174,8 +1174,12 @@ namespace UniversalEditor.UserInterface { if (document.IsSaved) { + bool inputClosed = false; if (document.InputAccessor != null && document.InputAccessor.IsOpen) + { + inputClosed = true; document.InputAccessor.Close(); + } if (document.OutputAccessor is FileAccessor) { @@ -1183,9 +1187,33 @@ namespace UniversalEditor.UserInterface (document.OutputAccessor as FileAccessor).AllowWrite = true; (document.OutputAccessor as FileAccessor).ForceOverwrite = true; } - document.OutputAccessor.Open(); - document.Save(); - document.OutputAccessor.Close(); + + try + { + document.OutputAccessor.Open(); + document.Save(); + document.OutputAccessor.Close(); + } + catch (UnauthorizedAccessException ex) + { + if (inputClosed) + { + if (document.InputAccessor is FileAccessor) + { + // FIXME: ewww + (document.InputAccessor as FileAccessor).AllowWrite = false; + (document.InputAccessor as FileAccessor).ForceOverwrite = false; + } + document.InputAccessor.Open(); + } + + switch (HandleUnauthorizedAccessException(document, ex)) + { + case MultipleDocumentErrorHandling.CancelAll: return false; + case MultipleDocumentErrorHandling.CancelOne: return true; + case MultipleDocumentErrorHandling.Ignore: break; + } + } DockingWindow di = dckContainer.Items[GetCurrentEditorPage()] as DockingWindow; if (di != null) @@ -1274,7 +1302,20 @@ namespace UniversalEditor.UserInterface page.Document.DataFormat = df; page.Document.Accessor = accessor; } - page.Document.Save(); + + try + { + page.Document.Save(); + } + catch (UnauthorizedAccessException ex) + { + switch (HandleUnauthorizedAccessException(page.Document, ex)) + { + case MultipleDocumentErrorHandling.CancelAll: return false; + case MultipleDocumentErrorHandling.CancelOne: return true; + case MultipleDocumentErrorHandling.Ignore: break; + } + } GetCurrentEditor().Document = page.Document; DockingWindow di = dckContainer.Items[page] as DockingWindow; @@ -1285,6 +1326,25 @@ namespace UniversalEditor.UserInterface } return true; } + + private MultipleDocumentErrorHandling HandleUnauthorizedAccessException(Document document, UnauthorizedAccessException ex) + { + DialogResult dr = MessageDialog.ShowDialog(String.Format("Cannot save the file in its current location. Would you like to choose another location?\r\n\r\n{0}", ex.Message), "Unauthorized", MessageDialogButtons.YesNoCancel, MessageDialogIcon.Warning); + if (dr == DialogResult.Yes) + { + SaveFileAs(document); + } + else if (dr == DialogResult.No) + { + return MultipleDocumentErrorHandling.CancelOne; + } + else if (dr == DialogResult.Cancel) + { + return MultipleDocumentErrorHandling.CancelAll; + } + return MultipleDocumentErrorHandling.Ignore; + } + public bool SaveFileAs(Accessor accessor, DataFormat df) { return SaveFileAs(accessor, df, GetCurrentEditor()?.ObjectModel); diff --git a/Libraries/UniversalEditor.UserInterface/MultipleDocumentErrorHandling.cs b/Libraries/UniversalEditor.UserInterface/MultipleDocumentErrorHandling.cs new file mode 100644 index 00000000..87998802 --- /dev/null +++ b/Libraries/UniversalEditor.UserInterface/MultipleDocumentErrorHandling.cs @@ -0,0 +1,46 @@ +// +// MultipleDocumentErrorHandling.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2021 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace UniversalEditor.UserInterface +{ + /// + /// Describes the way in which to handle an when + /// there are additional s to be processed afterward. + /// + public enum MultipleDocumentErrorHandling + { + /// + /// Ignore the error for the current and proceed + /// to processing the next . + /// + Ignore, + /// + /// Cancels the operation for a single . Other + /// s will continue to be processed. + /// + CancelOne, + /// + /// Cancels the entire operation, including any s + /// that still have yet to be processed. + /// + CancelAll + } +} diff --git a/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj b/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj index ff832bcf..9367359b 100644 --- a/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj +++ b/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj @@ -136,6 +136,7 @@ +