Files
PowerToys/src/modules/peek/Peek.UI/Helpers/DeleteErrorMessageHelper.cs
Dave Rayment 8e90d8e4c5 [Peek]Add Delete functionality (#35418)
* Add Delete functionality for Peek.

* Updated the "No More Files" text block to use a Uid to load its resource text. Also altered the text style to be consistent with the FailedFallbackPreviewControl error page.

* Revert "Delete Directory.Packages.props"

This reverts commit 3a10918c9f91de64785722e4bdb33c58d1c2daea.

* Attempt to appease the spell-checking bot by renaming flag const.

* Show error message InfoBar if file deletion failed.

* Resolve XAML styling.

* XAML styling fix.

* Settings app updates for new delete confirmation setting.

* Add delete confirmation dialog and settings to Peek. Add shell notification event after delete operation.

* Spelling updates.

* Spelling update.

* Remove permanent delete parameter, YAGNI. Add hwnd parameter to delete so warning dialogs are correctly parented. Fix flags to not hide permanent delete warning.

* Simplify delete confirmation dialog. Remove workaround for focus visual issue. Ensure delete confirmation dialog is closed when the main window visibility is toggled.

* Fix delete delay. Do not regard user cancellations of permanent deletes as an error, but log them as info anyway. More descriptive name for delete confirmation dialog checkbox.

* Fix multiple Content_KeyUp events being raised for MainWindow.

* Synchronise ConfirmFileDelete setting between Peek and Settings app.

* Update following review: split System usings from others; do not log deleted item name.

* Fix XAML style
2025-03-18 08:59:20 +00:00

101 lines
3.6 KiB
C#

// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using ManagedCommon;
using static Peek.Common.Helpers.ResourceLoaderInstance;
namespace Peek.UI.Helpers;
public static class DeleteErrorMessageHelper
{
/// <summary>
/// The "Could not delete 'filename'." message, which begins every user-facing error string.
/// </summary>
private static readonly CompositeFormat UserMessagePrefix =
CompositeFormat.Parse(ResourceLoader.GetString("DeleteFileError_Prefix") + " ");
/// <summary>
/// The message displayed if the delete failed but the error code isn't covered in the
/// <see cref="DeleteFileErrors"/> collection.
/// </summary>
private static readonly string GenericErrorMessage = ResourceLoader.GetString("DeleteFileError_Generic");
/// <summary>
/// The collection of the most common error codes with their matching log messages and user-
/// facing descriptions.
/// </summary>
private static readonly Dictionary<int, (string LogMessage, string UserMessage)> DeleteFileErrors = new()
{
{
2,
(
"The system cannot find the file specified.",
ResourceLoader.GetString("DeleteFileError_NotFound")
)
},
{
3,
(
"The system cannot find the path specified.",
ResourceLoader.GetString("DeleteFileError_NotFound")
)
},
{
5,
(
"Access is denied.",
ResourceLoader.GetString("DeleteFileError_AccessDenied")
)
},
{
19,
(
"The media is write protected.",
ResourceLoader.GetString("DeleteFileError_WriteProtected")
)
},
{
32,
(
"The process cannot access the file because it is being used by another process.",
ResourceLoader.GetString("DeleteFileError_FileInUse")
)
},
{
33,
(
"The process cannot access the file because another process has locked a portion of the file.",
ResourceLoader.GetString("DeleteFileError_FileInUse")
)
},
};
/// <summary>
/// Logs an error message in response to a failed file deletion attempt.
/// </summary>
/// <param name="errorCode">The error code returned from the delete call.</param>
public static void LogError(int errorCode) =>
Logger.LogError(DeleteFileErrors.TryGetValue(errorCode, out var messages) ?
messages.LogMessage :
$"Error {errorCode} occurred while deleting the file.");
/// <summary>
/// Gets the message to display in the UI for a specific delete error code.
/// </summary>
/// <param name="filename">The name of the file which could not be deleted.</param>
/// <param name="errorCode">The error code result from the delete call.</param>
/// <returns>A string containing the message to show in the user interface.</returns>
public static string GetUserErrorMessage(string filename, int errorCode)
{
string prefix = string.Format(CultureInfo.InvariantCulture, UserMessagePrefix, filename);
return DeleteFileErrors.TryGetValue(errorCode, out var messages) ?
prefix + messages.UserMessage :
prefix + GenericErrorMessage;
}
}