-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathHashesViewModel.cs
123 lines (103 loc) · 3.65 KB
/
HashesViewModel.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
119
120
121
122
123
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.
using Files.Shared.Helpers;
using System.IO;
using System.Windows.Input;
namespace Files.App.ViewModels.Properties
{
public class HashesViewModel : ObservableObject, IDisposable
{
private IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetService<IUserSettingsService>()!;
private HashInfoItem _selectedItem;
public HashInfoItem SelectedItem
{
get => _selectedItem;
set => SetProperty(ref _selectedItem, value);
}
public ObservableCollection<HashInfoItem> Hashes { get; set; }
public Dictionary<string, bool> ShowHashes { get; private set; }
public ICommand ToggleIsEnabledCommand { get; private set; }
private ListedItem _item;
private CancellationTokenSource _cancellationTokenSource;
public HashesViewModel(ListedItem item)
{
ToggleIsEnabledCommand = new RelayCommand<string>(ToggleIsEnabled);
_item = item;
_cancellationTokenSource = new();
Hashes = new()
{
new() { Algorithm = "CRC32" },
new() { Algorithm = "MD5" },
new() { Algorithm = "SHA1" },
new() { Algorithm = "SHA256" },
new() { Algorithm = "SHA384" },
new() { Algorithm = "SHA512" },
};
ShowHashes = UserSettingsService.GeneralSettingsService.ShowHashesDictionary ?? new();
// Default settings
ShowHashes.TryAdd("CRC32", true);
ShowHashes.TryAdd("MD5", true);
ShowHashes.TryAdd("SHA1", true);
ShowHashes.TryAdd("SHA256", true);
ShowHashes.TryAdd("SHA384", false);
ShowHashes.TryAdd("SHA512", false);
Hashes.Where(x => ShowHashes[x.Algorithm]).ForEach(x => ToggleIsEnabledCommand.Execute(x.Algorithm));
}
private void ToggleIsEnabled(string? algorithm)
{
var hashInfoItem = Hashes.Where(x => x.Algorithm == algorithm).First();
hashInfoItem.IsEnabled = !hashInfoItem.IsEnabled;
if (ShowHashes[hashInfoItem.Algorithm] != hashInfoItem.IsEnabled)
{
ShowHashes[hashInfoItem.Algorithm] = hashInfoItem.IsEnabled;
UserSettingsService.GeneralSettingsService.ShowHashesDictionary = ShowHashes;
}
// Don't calculate hashes for online files
if (_item.SyncStatusUI.SyncStatus is CloudDriveSyncStatus.FileOnline or CloudDriveSyncStatus.FolderOnline)
{
hashInfoItem.HashValue = "CalculationOnlineFileHashError".GetLocalizedResource();
return;
}
if (hashInfoItem.HashValue is null && hashInfoItem.IsEnabled)
{
hashInfoItem.IsCalculating = true;
MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(async () =>
{
try
{
using (var stream = File.OpenRead(_item.ItemPath))
{
hashInfoItem.HashValue = hashInfoItem.Algorithm switch
{
"CRC32" => await ChecksumHelpers.CreateCRC32(stream, _cancellationTokenSource.Token),
"MD5" => await ChecksumHelpers.CreateMD5(stream, _cancellationTokenSource.Token),
"SHA1" => await ChecksumHelpers.CreateSHA1(stream, _cancellationTokenSource.Token),
"SHA256" => await ChecksumHelpers.CreateSHA256(stream, _cancellationTokenSource.Token),
"SHA384" => await ChecksumHelpers.CreateSHA384(stream, _cancellationTokenSource.Token),
"SHA512" => await ChecksumHelpers.CreateSHA512(stream, _cancellationTokenSource.Token),
_ => throw new InvalidOperationException()
};
}
hashInfoItem.IsCalculated = true;
}
catch (OperationCanceledException)
{
// not an error
}
catch (Exception)
{
hashInfoItem.HashValue = "CalculationError".GetLocalizedResource();
}
finally
{
hashInfoItem.IsCalculating = false;
}
});
}
}
public void Dispose()
{
_cancellationTokenSource.Cancel();
}
}
}