forked from files-community/Files
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Added support for creating alternate data streams (files-com…
- Loading branch information
Showing
14 changed files
with
267 additions
and
6 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
src/Files.App/Actions/FileSystem/CreateAlternateDataStreamAction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright (c) 2024 Files Community | ||
// Licensed under the MIT License. See the LICENSE. | ||
|
||
using Microsoft.UI.Xaml.Controls; | ||
using Windows.Foundation.Metadata; | ||
|
||
namespace Files.App.Actions | ||
{ | ||
internal sealed class CreateAlternateDataStreamAction : BaseUIAction, IAction | ||
{ | ||
private readonly IContentPageContext context; | ||
|
||
private static readonly IFoldersSettingsService FoldersSettingsService = Ioc.Default.GetRequiredService<IFoldersSettingsService>(); | ||
private static readonly IApplicationSettingsService ApplicationSettingsService = Ioc.Default.GetRequiredService<IApplicationSettingsService>(); | ||
|
||
public string Label | ||
=> Strings.CreateAlternateDataStream.GetLocalizedResource(); | ||
|
||
public string Description | ||
=> Strings.CreateAlternateDataStreamDescription.GetLocalizedResource(); | ||
|
||
public override bool IsExecutable => | ||
context.HasSelection && | ||
context.CanCreateItem && | ||
UIHelpers.CanShowDialog; | ||
|
||
public CreateAlternateDataStreamAction() | ||
{ | ||
context = Ioc.Default.GetRequiredService<IContentPageContext>(); | ||
|
||
context.PropertyChanged += Context_PropertyChanged; | ||
} | ||
|
||
public async Task ExecuteAsync(object? parameter = null) | ||
{ | ||
var nameDialog = DynamicDialogFactory.GetFor_CreateAlternateDataStreamDialog(); | ||
await nameDialog.TryShowAsync(); | ||
|
||
if (nameDialog.DynamicResult != DynamicDialogResult.Primary) | ||
return; | ||
|
||
var userInput = nameDialog.ViewModel.AdditionalData as string; | ||
await Task.WhenAll(context.SelectedItems.Select(async selectedItem => | ||
{ | ||
var isDateOk = Win32Helper.GetFileDateModified(selectedItem.ItemPath, out var dateModified); | ||
var isReadOnly = Win32Helper.HasFileAttribute(selectedItem.ItemPath, System.IO.FileAttributes.ReadOnly); | ||
// Unset read-only attribute (#7534) | ||
if (isReadOnly) | ||
Win32Helper.UnsetFileAttribute(selectedItem.ItemPath, System.IO.FileAttributes.ReadOnly); | ||
if (!Win32Helper.WriteStringToFile($"{selectedItem.ItemPath}:{userInput}", "")) | ||
{ | ||
var dialog = new ContentDialog | ||
{ | ||
Title = Strings.ErrorCreatingDataStreamTitle.GetLocalizedResource(), | ||
Content = Strings.ErrorCreatingDataStreamDescription.GetLocalizedResource(), | ||
PrimaryButtonText = "Ok".GetLocalizedResource() | ||
}; | ||
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8)) | ||
dialog.XamlRoot = MainWindow.Instance.Content.XamlRoot; | ||
await dialog.TryShowAsync(); | ||
} | ||
// Restore read-only attribute (#7534) | ||
if (isReadOnly) | ||
Win32Helper.SetFileAttribute(selectedItem.ItemPath, System.IO.FileAttributes.ReadOnly); | ||
// Restore date modified | ||
if (isDateOk) | ||
Win32Helper.SetFileDateModified(selectedItem.ItemPath, dateModified); | ||
})); | ||
|
||
if (context.ShellPage is null) | ||
return; | ||
|
||
if (FoldersSettingsService.AreAlternateStreamsVisible) | ||
await context.ShellPage.Refresh_Click(); | ||
else if (ApplicationSettingsService.ShowDataStreamsAreHiddenPrompt) | ||
{ | ||
var dialog = new ContentDialog | ||
{ | ||
Title = Strings.DataStreamsAreHiddenTitle.GetLocalizedResource(), | ||
Content = Strings.DataStreamsAreHiddenDescription.GetLocalizedResource(), | ||
PrimaryButtonText = Strings.Yes.GetLocalizedResource(), | ||
SecondaryButtonText = Strings.DontShowAgain.GetLocalizedResource() | ||
}; | ||
|
||
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8)) | ||
dialog.XamlRoot = MainWindow.Instance.Content.XamlRoot; | ||
|
||
var result = await dialog.TryShowAsync(); | ||
if (result == ContentDialogResult.Primary) | ||
{ | ||
FoldersSettingsService.AreAlternateStreamsVisible = true; | ||
await context.ShellPage.Refresh_Click(); | ||
} | ||
else | ||
ApplicationSettingsService.ShowDataStreamsAreHiddenPrompt = false; | ||
} | ||
} | ||
|
||
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e) | ||
{ | ||
switch (e.PropertyName) | ||
{ | ||
case nameof(IContentPageContext.HasSelection): | ||
case nameof(IContentPageContext.CanCreateItem): | ||
OnPropertyChanged(nameof(IsExecutable)); | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters