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