do not crash if we don't have permission to save the file when invoking Save

This commit is contained in:
Michael Becker 2021-05-13 17:37:15 -04:00
parent faa082de39
commit 98732be788
No known key found for this signature in database
GPG Key ID: 98C333A81F18C22C
3 changed files with 111 additions and 4 deletions

View File

@ -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);

View File

@ -0,0 +1,46 @@
//
// MultipleDocumentErrorHandling.cs
//
// Author:
// Michael Becker <alcexhim@gmail.com>
//
// 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 <http://www.gnu.org/licenses/>.
using System;
namespace UniversalEditor.UserInterface
{
/// <summary>
/// Describes the way in which to handle an <see cref="Exception" /> when
/// there are additional <see cref="Document" />s to be processed afterward.
/// </summary>
public enum MultipleDocumentErrorHandling
{
/// <summary>
/// Ignore the error for the current <see cref="Document" /> and proceed
/// to processing the next <see cref="Document" />.
/// </summary>
Ignore,
/// <summary>
/// Cancels the operation for a single <see cref="Document" />. Other
/// <see cref="Document" />s will continue to be processed.
/// </summary>
CancelOne,
/// <summary>
/// Cancels the entire operation, including any <see cref="Document" />s
/// that still have yet to be processed.
/// </summary>
CancelAll
}
}

View File

@ -136,6 +136,7 @@
<Compile Include="EditorChangingEvent.cs" />
<Compile Include="EditorPropertiesPanel.cs" />
<Compile Include="Editors\FileSystem\FileSystemEditorSettingsGuids.cs" />
<Compile Include="MultipleDocumentErrorHandling.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">