|
| 1 | +// Copyright (c) 2024 Files Community |
| 2 | +// Licensed under the MIT License. See the LICENSE. |
| 3 | + |
| 4 | +using Microsoft.UI.Xaml.Controls; |
| 5 | +using Windows.Foundation.Metadata; |
| 6 | + |
| 7 | +namespace Files.App.Actions |
| 8 | +{ |
| 9 | + internal sealed class CreateAlternateDataStreamAction : BaseUIAction, IAction |
| 10 | + { |
| 11 | + private readonly IContentPageContext context; |
| 12 | + |
| 13 | + private static readonly IFoldersSettingsService FoldersSettingsService = Ioc.Default.GetRequiredService<IFoldersSettingsService>(); |
| 14 | + private static readonly IApplicationSettingsService ApplicationSettingsService = Ioc.Default.GetRequiredService<IApplicationSettingsService>(); |
| 15 | + |
| 16 | + public string Label |
| 17 | + => Strings.CreateAlternateDataStream.GetLocalizedResource(); |
| 18 | + |
| 19 | + public string Description |
| 20 | + => Strings.CreateAlternateDataStreamDescription.GetLocalizedResource(); |
| 21 | + |
| 22 | + public override bool IsExecutable => |
| 23 | + context.HasSelection && |
| 24 | + context.CanCreateItem && |
| 25 | + UIHelpers.CanShowDialog; |
| 26 | + |
| 27 | + public CreateAlternateDataStreamAction() |
| 28 | + { |
| 29 | + context = Ioc.Default.GetRequiredService<IContentPageContext>(); |
| 30 | + |
| 31 | + context.PropertyChanged += Context_PropertyChanged; |
| 32 | + } |
| 33 | + |
| 34 | + public async Task ExecuteAsync(object? parameter = null) |
| 35 | + { |
| 36 | + var nameDialog = DynamicDialogFactory.GetFor_CreateAlternateDataStreamDialog(); |
| 37 | + await nameDialog.TryShowAsync(); |
| 38 | + |
| 39 | + if (nameDialog.DynamicResult != DynamicDialogResult.Primary) |
| 40 | + return; |
| 41 | + |
| 42 | + var userInput = nameDialog.ViewModel.AdditionalData as string; |
| 43 | + await Task.WhenAll(context.SelectedItems.Select(async selectedItem => |
| 44 | + { |
| 45 | + var isDateOk = Win32Helper.GetFileDateModified(selectedItem.ItemPath, out var dateModified); |
| 46 | + var isReadOnly = Win32Helper.HasFileAttribute(selectedItem.ItemPath, System.IO.FileAttributes.ReadOnly); |
| 47 | + |
| 48 | + // Unset read-only attribute (#7534) |
| 49 | + if (isReadOnly) |
| 50 | + Win32Helper.UnsetFileAttribute(selectedItem.ItemPath, System.IO.FileAttributes.ReadOnly); |
| 51 | + |
| 52 | + if (!Win32Helper.WriteStringToFile($"{selectedItem.ItemPath}:{userInput}", "")) |
| 53 | + { |
| 54 | + var dialog = new ContentDialog |
| 55 | + { |
| 56 | + Title = Strings.ErrorCreatingDataStreamTitle.GetLocalizedResource(), |
| 57 | + Content = Strings.ErrorCreatingDataStreamDescription.GetLocalizedResource(), |
| 58 | + PrimaryButtonText = "Ok".GetLocalizedResource() |
| 59 | + }; |
| 60 | + |
| 61 | + if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8)) |
| 62 | + dialog.XamlRoot = MainWindow.Instance.Content.XamlRoot; |
| 63 | + |
| 64 | + await dialog.TryShowAsync(); |
| 65 | + } |
| 66 | + |
| 67 | + // Restore read-only attribute (#7534) |
| 68 | + if (isReadOnly) |
| 69 | + Win32Helper.SetFileAttribute(selectedItem.ItemPath, System.IO.FileAttributes.ReadOnly); |
| 70 | + |
| 71 | + // Restore date modified |
| 72 | + if (isDateOk) |
| 73 | + Win32Helper.SetFileDateModified(selectedItem.ItemPath, dateModified); |
| 74 | + })); |
| 75 | + |
| 76 | + if (context.ShellPage is null) |
| 77 | + return; |
| 78 | + |
| 79 | + if (FoldersSettingsService.AreAlternateStreamsVisible) |
| 80 | + await context.ShellPage.Refresh_Click(); |
| 81 | + else if (ApplicationSettingsService.ShowDataStreamsAreHiddenPrompt) |
| 82 | + { |
| 83 | + var dialog = new ContentDialog |
| 84 | + { |
| 85 | + Title = Strings.DataStreamsAreHiddenTitle.GetLocalizedResource(), |
| 86 | + Content = Strings.DataStreamsAreHiddenDescription.GetLocalizedResource(), |
| 87 | + PrimaryButtonText = Strings.Yes.GetLocalizedResource(), |
| 88 | + SecondaryButtonText = Strings.DontShowAgain.GetLocalizedResource() |
| 89 | + }; |
| 90 | + |
| 91 | + if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8)) |
| 92 | + dialog.XamlRoot = MainWindow.Instance.Content.XamlRoot; |
| 93 | + |
| 94 | + var result = await dialog.TryShowAsync(); |
| 95 | + if (result == ContentDialogResult.Primary) |
| 96 | + { |
| 97 | + FoldersSettingsService.AreAlternateStreamsVisible = true; |
| 98 | + await context.ShellPage.Refresh_Click(); |
| 99 | + } |
| 100 | + else |
| 101 | + ApplicationSettingsService.ShowDataStreamsAreHiddenPrompt = false; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e) |
| 106 | + { |
| 107 | + switch (e.PropertyName) |
| 108 | + { |
| 109 | + case nameof(IContentPageContext.HasSelection): |
| 110 | + case nameof(IContentPageContext.CanCreateItem): |
| 111 | + OnPropertyChanged(nameof(IsExecutable)); |
| 112 | + break; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments