-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathFlattenFolderAction.cs
More file actions
143 lines (118 loc) · 4.08 KB
/
Copy pathFlattenFolderAction.cs
File metadata and controls
143 lines (118 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) Files Community
// Licensed under the MIT License.
using Microsoft.Extensions.Logging;
using Microsoft.UI.Xaml.Controls;
using System.IO;
using Windows.Foundation.Metadata;
using Windows.Storage;
namespace Files.App.Actions
{
[GeneratedRichCommand]
internal sealed partial class FlattenFolderAction : ObservableObject, IAction
{
private readonly IContentPageContext context;
private readonly IGeneralSettingsService GeneralSettingsService = Ioc.Default.GetRequiredService<IGeneralSettingsService>();
public string Label
=> Strings.FlattenFolder.GetLocalizedResource();
public string Description
=> Strings.FlattenFolderDescription.GetLocalizedResource();
public ActionCategory Category
=> ActionCategory.FileSystem;
public RichGlyph Glyph
=> new(themedIconStyle: "App.ThemedIcons.Folder");
public bool IsExecutable =>
GeneralSettingsService.ShowFlattenOptions &&
context.ShellPage is not null &&
context.HasSelection &&
context.SelectedItems.Count is 1 &&
context.SelectedItem is not null &&
context.SelectedItem.PrimaryItemAttribute is StorageItemTypes.Folder;
public FlattenFolderAction()
{
context = Ioc.Default.GetRequiredService<IContentPageContext>();
context.PropertyChanged += Context_PropertyChanged;
GeneralSettingsService.PropertyChanged += GeneralSettingsService_PropertyChanged;
}
public async Task ExecuteAsync(object? parameter = null)
{
if (context.SelectedItem is null)
return;
var optionsDialog = new ContentDialog()
{
Title = Strings.FlattenFolder.GetLocalizedResource(),
Content = Strings.FlattenFolderDialogContent.GetLocalizedResource(),
PrimaryButtonText = Strings.Flatten.GetLocalizedResource(),
SecondaryButtonText = Strings.Cancel.GetLocalizedResource(),
DefaultButton = ContentDialogButton.Primary
};
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
optionsDialog.XamlRoot = MainWindow.Instance.Content.XamlRoot;
var result = await optionsDialog.TryShowAsync();
if (result != ContentDialogResult.Primary)
return;
FlattenFolder(context.SelectedItem.ItemPath!);
}
private void FlattenFolder(string path)
{
var containedFolders = Directory.GetDirectories(path);
var containedFiles = Directory.GetFiles(path);
foreach (var containedFolder in containedFolders)
{
FlattenFolder(containedFolder);
var folderName = Path.GetFileName(containedFolder);
var destinationPath = Path.Combine(context?.SelectedItem?.ItemPath ?? string.Empty, folderName);
if (Directory.Exists(destinationPath))
continue;
try
{
Directory.Move(containedFolder, destinationPath);
}
catch (Exception ex)
{
App.Logger.LogWarning(ex, $"Folder '{LogPathHelper.RedactPath(folderName)}' already exists in the destination folder.");
}
}
foreach (var containedFile in containedFiles)
{
var fileName = Path.GetFileName(containedFile);
var destinationPath = Path.Combine(context?.SelectedItem?.ItemPath ?? string.Empty, fileName);
if (File.Exists(destinationPath))
continue;
try
{
File.Move(containedFile, destinationPath);
}
catch (Exception ex)
{
App.Logger.LogWarning(ex, $"Failed to move file '{LogPathHelper.RedactPath(fileName)}'.");
}
}
if (Directory.GetFiles(path).Length == 0 && Directory.GetDirectories(path).Length == 0)
{
try
{
Directory.Delete(path);
}
catch (Exception ex)
{
App.Logger.LogWarning(ex, $"Failed to delete folder '{LogPathHelper.RedactPath(path)}'.");
}
}
}
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(IContentPageContext.HasSelection):
case nameof(IContentPageContext.SelectedItem):
OnPropertyChanged(nameof(IsExecutable));
break;
}
}
private void GeneralSettingsService_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(IGeneralSettingsService.ShowFlattenOptions))
OnPropertyChanged(nameof(IsExecutable));
}
}
}