forked from files-community/Files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecompressArchiveDialogViewModel.cs
118 lines (97 loc) · 3.33 KB
/
DecompressArchiveDialogViewModel.cs
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
// Copyright (c) Files Community
// Licensed under the MIT License.
using System.IO;
using System.Text;
using System.Windows.Input;
using Windows.Storage;
namespace Files.App.ViewModels.Dialogs
{
public sealed partial class DecompressArchiveDialogViewModel : ObservableObject
{
private ICommonDialogService CommonDialogService { get; } = Ioc.Default.GetRequiredService<ICommonDialogService>();
private readonly IStorageFile archive;
public BaseStorageFolder DestinationFolder { get; private set; }
private string destinationFolderPath;
public string DestinationFolderPath
{
get => destinationFolderPath;
private set => SetProperty(ref destinationFolderPath, value);
}
private bool openDestinationFolderOnCompletion;
public bool OpenDestinationFolderOnCompletion
{
get => openDestinationFolderOnCompletion;
set => SetProperty(ref openDestinationFolderOnCompletion, value);
}
private bool isArchiveEncrypted;
public bool IsArchiveEncrypted
{
get => isArchiveEncrypted;
set => SetProperty(ref isArchiveEncrypted, value);
}
private bool isArchiveEncodingUndetermined;
public bool IsArchiveEncodingUndetermined
{
get => isArchiveEncodingUndetermined;
set => SetProperty(ref isArchiveEncodingUndetermined, value);
}
private Encoding? detectedEncoding;
public Encoding? DetectedEncoding
{
get => detectedEncoding;
set {
SetProperty(ref detectedEncoding, value);
RefreshEncodingOptions();
}
}
private bool showPathSelection;
public bool ShowPathSelection
{
get => showPathSelection;
set => SetProperty(ref showPathSelection, value);
}
public DisposableArray? Password { get; private set; }
public EncodingItem[] EncodingOptions { get; set; } = EncodingItem.Defaults;
public EncodingItem SelectedEncoding { get; set; }
void RefreshEncodingOptions()
{
if (detectedEncoding != null)
{
EncodingOptions = EncodingItem.Defaults
.Prepend(new EncodingItem(
detectedEncoding,
string.Format(Strings.EncodingDetected.GetLocalizedResource(), detectedEncoding.EncodingName)
))
.ToArray();
}
else
{
EncodingOptions = EncodingItem.Defaults;
}
SelectedEncoding = EncodingOptions.FirstOrDefault();
}
public IRelayCommand PrimaryButtonClickCommand { get; private set; }
public ICommand SelectDestinationCommand { get; private set; }
public DecompressArchiveDialogViewModel(IStorageFile archive)
{
this.archive = archive;
destinationFolderPath = DefaultDestinationFolderPath();
SelectedEncoding = EncodingOptions[0];
// Create commands
SelectDestinationCommand = new AsyncRelayCommand(SelectDestinationAsync);
PrimaryButtonClickCommand = new RelayCommand<DisposableArray>(password => Password = password);
}
private async Task SelectDestinationAsync()
{
bool result = CommonDialogService.Open_FileOpenDialog(MainWindow.Instance.WindowHandle, true, [], Environment.SpecialFolder.Desktop, out var filePath);
if (!result)
return;
DestinationFolder = await StorageHelpers.ToStorageItem<BaseStorageFolder>(filePath);
DestinationFolderPath = (DestinationFolder is not null) ? DestinationFolder.Path : DefaultDestinationFolderPath();
}
private string DefaultDestinationFolderPath()
{
return Path.Combine(Path.GetDirectoryName(archive.Path), Path.GetFileNameWithoutExtension(archive.Path));
}
}
}