/Horizon/Snapshots/SnapshotManagerForm.cs |
@@ -35,6 +35,12 @@ |
|
#endregion |
|
#region Public Events & Delegates |
|
public event EventHandler<PreviewRetrievedEventArgs> PreviewRetrieved; |
|
#endregion |
|
#region Private Delegates, Events, Enums, Properties, Indexers and Fields |
|
private readonly MainForm _mainForm; |
@@ -43,7 +49,7 @@ |
|
private HexViewForm _hexViewForm; |
|
private SnapshotNoteForm _snapshotNoteForm; |
private SnapshotNoteForm _snapshotNote; |
|
private SnapshotPreviewForm _snapshotPreviewForm; |
|
@@ -265,10 +271,10 @@ |
{ |
try |
{ |
var hash = (string)rows[i].Cells["HashColumn"].Value; |
var fileStream = await _snapshotDatabase.RetrieveFileStreamAsync( |
(string)rows[i].Cells["HashColumn"].Value, |
cancellationToken); |
|
var fileStream = await _snapshotDatabase.RetrieveFileStreamAsync(hash, cancellationToken); |
|
progress.Report(new DataGridViewRowProgressSuccessRetrieveFileStream(rows[i], i, fileStream)); |
} |
catch (Exception exception) |
@@ -280,7 +286,7 @@ |
|
private void SnapshotManagerForm_Resize(object sender, EventArgs e) |
{ |
if (_snapshotPreviewForm is { Visible: true }) |
if (_snapshotPreviewForm != null) |
{ |
_snapshotPreviewForm.WindowState = WindowState; |
} |
@@ -297,10 +303,18 @@ |
Process.Start("explorer.exe", $"/select, \"{(string)row.Cells["PathColumn"].Value}\""); |
} |
|
private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) |
private async void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) |
{ |
var dataGridView = (DataGridView)sender; |
|
if (_snapshotPreviewForm == null) |
{ |
_snapshotPreviewForm = new SnapshotPreviewForm(this, _snapshotDatabase); |
_snapshotPreviewForm.Owner = this; |
_snapshotPreviewForm.Closing += SnapshotPreviewForm_Closing; |
_snapshotPreviewForm.Show(); |
} |
|
var row = GetSelectedDataGridViewRows(dataGridView).FirstOrDefault(); |
if (row == null) |
{ |
@@ -309,26 +323,27 @@ |
|
var hash = (string)row.Cells["HashColumn"].Value; |
|
if (_snapshotPreviewForm is { Visible: true }) |
var snapshotPreview = |
await Task.Run(async () => await _snapshotDatabase.RetrievePreviewAsync(hash, _cancellationToken), |
_cancellationToken); |
|
if (snapshotPreview == null) |
{ |
_snapshotPreviewForm.Close(); |
return; |
} |
|
_snapshotPreviewForm = new SnapshotPreviewForm(this, hash, _snapshotDatabase, _cancellationToken); |
_snapshotPreviewForm.Owner = this; |
_snapshotPreviewForm.Closing += SnapshotPreviewForm_Closing; |
_snapshotPreviewForm.Show(); |
PreviewRetrieved?.Invoke(this, new PreviewRetrievedEventArgs(snapshotPreview)); |
} |
|
private void SnapshotPreviewForm_Closing(object sender, CancelEventArgs e) |
{ |
if (_snapshotPreviewForm is { Visible: false }) |
if (_snapshotPreviewForm == null) |
{ |
return; |
} |
|
_snapshotPreviewForm.Closing -= SnapshotPreviewForm_Closing; |
_snapshotPreviewForm.Dispose(); |
_snapshotPreviewForm = null; |
} |
|
private async void NoneToolStripMenuItem_Click(object sender, EventArgs e) |
@@ -634,25 +649,9 @@ |
// Start browsing for network services. |
_horizonServiceBrowser.Browse("_horizon._tcp", "local"); |
|
int count; |
try |
{ |
count = (int)await _snapshotDatabase.CountSnapshotsAsync(_cancellationToken); |
} |
catch (Exception exception) |
{ |
Log.Error(exception, "Could not count snapshots."); |
return; |
} |
|
if (count == 0) |
{ |
return; |
} |
|
// Load snapshots. |
toolStripProgressBar1.Minimum = 0; |
toolStripProgressBar1.Maximum = count; |
toolStripProgressBar1.Maximum = (int)await _snapshotDatabase.CountSnapshotsAsync(_cancellationToken); |
|
var snapshotQueue = new ConcurrentQueue<Snapshot>(); |
|
@@ -792,14 +791,16 @@ |
{ |
_cancellationTokenSource.Cancel(); |
|
if (_snapshotPreviewForm is { Visible: true }) |
if (_snapshotPreviewForm != null) |
{ |
_snapshotPreviewForm.Close(); |
_snapshotPreviewForm = null; |
} |
|
if (_hexViewForm is { Visible: true }) |
if (_hexViewForm != null) |
{ |
_hexViewForm.Close(); |
_hexViewForm = null; |
} |
} |
|
@@ -834,7 +835,8 @@ |
|
try |
{ |
await _snapshotDatabase.CreateSnapshotAsync(name, path, screenCapture, color, _cancellationToken); |
await _snapshotDatabase.CreateSnapshotAsync(name, path, screenCapture, color, |
_cancellationToken); |
|
progress.Report(new CreateSnapshotProgressSuccess(file)); |
} |
@@ -848,7 +850,6 @@ |
} |
|
var fileName = Path.GetFileName(file); |
|
var pathName = Path.Combine(Path.GetDirectoryName(file), fileName); |
|
try |
@@ -1043,8 +1044,13 @@ |
} |
} |
|
private void NoteToolStripMenuItem_Click(object sender, EventArgs e) |
private async void NoteToolStripMenuItem_Click(object sender, EventArgs e) |
{ |
if (_snapshotNote != null) |
{ |
return; |
} |
|
var row = GetSelectedDataGridViewRows(dataGridView1).FirstOrDefault(); |
if (row == null) |
{ |
@@ -1051,21 +1057,29 @@ |
return; |
} |
|
var hash = (string)row.Cells["HashColumn"].Value; |
try |
{ |
var snapshotPreview = await _snapshotDatabase.RetrievePreviewAsync( |
(string)row.Cells["HashColumn"].Value, _cancellationToken); |
|
if (_snapshotNoteForm is { Visible: true }) |
if (snapshotPreview == null) |
{ |
_snapshotNoteForm.Close(); |
return; |
} |
|
_snapshotNoteForm = new SnapshotNoteForm(hash, _snapshotDatabase, _cancellationToken); |
_snapshotNoteForm.Owner = this; |
_snapshotNoteForm.SaveNote += SnapshotNoteFormSaveNoteForm; |
_snapshotNoteForm.Closing += SnapshotNoteForm_Closing; |
_snapshotNoteForm.Show(); |
_snapshotNote = new SnapshotNoteForm(this, snapshotPreview); |
_snapshotNote.Owner = this; |
_snapshotNote.SaveNote += SnapshotNote_SaveNote; |
_snapshotNote.Closing += SnapshotNote_Closing; |
_snapshotNote.Show(); |
} |
catch (Exception exception) |
{ |
Log.Error(exception, "Could not open notes form."); |
} |
} |
|
private async void SnapshotNoteFormSaveNoteForm(object sender, SaveNoteEventArgs e) |
private async void SnapshotNote_SaveNote(object sender, SaveNoteEventArgs e) |
{ |
var rows = GetSelectedDataGridViewRows(dataGridView1); |
|
@@ -1105,19 +1119,19 @@ |
} |
} |
|
private void SnapshotNoteForm_Closing(object sender, CancelEventArgs e) |
private void SnapshotNote_Closing(object sender, CancelEventArgs e) |
{ |
if (_snapshotNoteForm is { Visible: false }) |
if (_snapshotNote == null) |
{ |
return; |
} |
|
_snapshotNoteForm.SaveNote -= SnapshotNoteFormSaveNoteForm; |
_snapshotNoteForm.Closing -= SnapshotNoteForm_Closing; |
_snapshotNoteForm.Dispose(); |
_snapshotNote.Closing -= SnapshotNote_Closing; |
_snapshotNote.Close(); |
_snapshotNote = null; |
} |
|
private void ViewHexToolStripMenuItem_Click(object sender, EventArgs e) |
private async void ViewHexToolStripMenuItem_Click(object sender, EventArgs e) |
{ |
var rows = GetSelectedDataGridViewRows(dataGridView1); |
var row = rows.FirstOrDefault(); |
@@ -1128,32 +1142,33 @@ |
|
var hash = (string)row.Cells["HashColumn"].Value; |
|
if (_hexViewForm is { Visible: true }) |
using (var memoryStream = await _snapshotDatabase.RetrieveFileStreamAsync(hash, _cancellationToken)) |
{ |
_hexViewForm.Close(); |
if (memoryStream == null) |
{ |
return; |
} |
|
_hexViewForm = new HexViewForm(hash, _snapshotDatabase, _cancellationToken); |
if (_hexViewForm != null) |
{ |
_hexViewForm.UpdateData(memoryStream.ToArray()); |
_hexViewForm.Activate(); |
return; |
} |
|
_hexViewForm = new HexViewForm(_snapshotDatabase, hash, memoryStream.ToArray()); |
_hexViewForm.Owner = this; |
_hexViewForm.Closing += HexViewForm_Closing; |
_hexViewForm.SaveData += HexViewForm_SaveData; |
|
_hexViewForm.Show(); |
} |
} |
|
private async void HexViewForm_SaveData(object sender, SaveDataEventArgs e) |
{ |
string hash; |
var hash = await _snapshotDatabase.UpdateFileAsync(e.Hash, e.Data, _cancellationToken); |
|
try |
{ |
hash = await _snapshotDatabase.UpdateFileAsync(e.Hash, e.Data, _cancellationToken); |
} |
catch (Exception exception) |
{ |
Log.Error(exception, "Could not update snapshot data."); |
return; |
} |
|
if (string.IsNullOrEmpty(hash)) |
{ |
return; |
@@ -1188,7 +1203,7 @@ |
|
private void HexViewForm_Closing(object sender, CancelEventArgs e) |
{ |
if (_hexViewForm is { Visible: false }) |
if (_hexViewForm == null) |
{ |
return; |
} |
@@ -1195,7 +1210,8 @@ |
|
_hexViewForm.SaveData -= HexViewForm_SaveData; |
_hexViewForm.Closing -= HexViewForm_Closing; |
_hexViewForm.Dispose(); |
_hexViewForm.Close(); |
_hexViewForm = null; |
} |
|
private async void FileToolStripMenuItem2_Click(object sender, EventArgs e) |
@@ -1476,10 +1492,9 @@ |
{ |
try |
{ |
var hash = (string)rows[index].Cells["HashColumn"].Value; |
await _snapshotDatabase.RemoveFileAsync((string)rows[index].Cells["HashColumn"].Value, |
cancellationToken); |
|
await _snapshotDatabase.RemoveFileAsync(hash, cancellationToken); |
|
progress.Report(new DataGridViewRowProgressSuccess(rows[index], index)); |
} |
catch (Exception exception) |
@@ -1493,15 +1508,8 @@ |
{ |
var hashes = rows.Select(row => (string)row.Cells["HashColumn"].Value); |
|
try |
{ |
await _snapshotDatabase.RemoveFileFastAsync(hashes, cancellationToken); |
} |
catch (Exception exception) |
{ |
Log.Error(exception, "Failed to remove files."); |
} |
} |
|
private async Task UpdateNote(IReadOnlyList<DataGridViewRow> rows, string note, |
IProgress<DataGridViewRowProgress> progress, CancellationToken cancellationToken) |
@@ -1512,10 +1520,9 @@ |
{ |
try |
{ |
var hash = (string)rows[i].Cells["HashColumn"].Value; |
await _snapshotDatabase.UpdateNoteAsync((string)rows[i].Cells["HashColumn"].Value, note, |
cancellationToken); |
|
await _snapshotDatabase.UpdateNoteAsync(hash, note,cancellationToken); |
|
progress.Report(new DataGridViewRowProgressSuccess(rows[i], i)); |
} |
catch (Exception exception) |
@@ -1545,10 +1552,9 @@ |
{ |
try |
{ |
var hash = (string)rows[i].Cells["HashColumn"].Value; |
await _snapshotDatabase.RemoveColorAsync((string)rows[i].Cells["HashColumn"].Value, |
cancellationToken); |
|
await _snapshotDatabase.RemoveColorAsync(hash, cancellationToken); |
|
progress.Report(new DataGridViewRowProgressSuccess(rows[i], i)); |
} |
catch (Exception exception) |
@@ -1567,10 +1573,9 @@ |
{ |
try |
{ |
var hash = (string)rows[i].Cells["HashColumn"].Value; |
await _snapshotDatabase.UpdateColorAsync((string)rows[i].Cells["HashColumn"].Value, color, |
cancellationToken); |
|
await _snapshotDatabase.UpdateColorAsync(hash, color, cancellationToken); |
|
progress.Report(new DataGridViewRowProgressSuccess(rows[i], i)); |
} |
catch (Exception exception) |
@@ -1600,10 +1605,9 @@ |
{ |
try |
{ |
var hash = (string)rows[i].Cells["HashColumn"].Value; |
|
var completeSnapshot = |
await _snapshotDatabase.GenerateTransferSnapshotAsync(hash, cancellationToken); |
await _snapshotDatabase.GenerateTransferSnapshotAsync( |
(string)rows[i].Cells["HashColumn"].Value, cancellationToken); |
|
var jsonSnapshot = JsonConvert.SerializeObject(completeSnapshot); |
|
@@ -1662,11 +1666,10 @@ |
{ |
try |
{ |
var path = (string)rows[i].Cells["NameColumn"].Value; |
var hash = (string)rows[i].Cells["HashColumn"].Value; |
await _snapshotDatabase.RevertFileAsync((string)rows[i].Cells["NameColumn"].Value, |
(string)rows[i].Cells["HashColumn"].Value, |
cancellationToken, _mainForm.Configuration.AtomicOperations); |
|
await _snapshotDatabase.RevertFileAsync(path, hash, cancellationToken, _mainForm.Configuration.AtomicOperations); |
|
progress.Report(new DataGridViewRowProgressSuccess(rows[i], i)); |
} |
catch (Exception exception) |
@@ -1685,12 +1688,12 @@ |
{ |
try |
{ |
var hash = (string)rows[i].Cells["HashColumn"].Value; |
var fileInfo = new FileInfo((string)rows[i].Cells["NameColumn"].Value); |
var file = fileInfo.Name; |
var path = Path.Combine(directory, file); |
|
await _snapshotDatabase.SaveFileAsync(path, hash, cancellationToken); |
await _snapshotDatabase.SaveFileAsync(path, (string)rows[i].Cells["HashColumn"].Value, |
cancellationToken); |
|
progress.Report(new DataGridViewRowProgressSuccess(rows[i], i)); |
} |
@@ -1711,10 +1714,10 @@ |
{ |
try |
{ |
var hash = (string)rows[i].Cells["HashColumn"].Value; |
var path = Path.Combine(directory, (string)rows[i].Cells["NameColumn"].Value); |
|
await _snapshotDatabase.RelocateFileAsync(hash, path, cancellationToken); |
await _snapshotDatabase.RelocateFileAsync((string)rows[i].Cells["HashColumn"].Value, path, |
cancellationToken); |
|
progress.Report(new DataGridViewRowProgressSuccess(rows[i], i)); |
} |
@@ -1735,21 +1738,22 @@ |
{ |
try |
{ |
var hash = (string)rows[i].Cells["HashColumn"].Value; |
|
using var memoryStream = await _snapshotDatabase.RetrieveFileStreamAsync(hash, cancellationToken); |
using (var memoryStream = |
await _snapshotDatabase.RetrieveFileStreamAsync((string)rows[i].Cells["HashColumn"].Value, |
cancellationToken)) |
{ |
if (memoryStream == null) |
{ |
continue; |
} |
|
using var md5 = MD5.Create(); |
|
using (var md5 = MD5.Create()) |
{ |
var recomputedHash = md5.ComputeHash(memoryStream); |
var hashHex = BitConverter.ToString(recomputedHash).Replace("-", "") |
.ToLowerInvariant(); |
|
await _snapshotDatabase.UpdateHashAsync(hash, hashHex, |
await _snapshotDatabase.UpdateHashAsync((string)rows[i].Cells["HashColumn"].Value, hashHex, |
cancellationToken); |
|
rows[i].Cells["HashColumn"].Value = hashHex; |
@@ -1756,6 +1760,8 @@ |
|
progress.Report(new DataGridViewRowProgressSuccess(rows[i], i)); |
} |
} |
} |
catch (Exception exception) |
{ |
progress.Report(new DataGridViewRowProgressFailure(rows[i], i, exception)); |
@@ -1794,7 +1800,6 @@ |
var newPath = Path.Combine(targetPath, rootPath, relPath); |
|
var hash = (string)rows[i].Cells["HashColumn"].Value; |
|
await _snapshotDatabase.SaveFileAsync(newPath, hash, cancellationToken); |
|
progress.Report(new DataGridViewRowProgressSuccess(rows[i], i)); |
@@ -1821,10 +1826,9 @@ |
{ |
try |
{ |
var hash = (string)rows[i].Cells["HashColumn"].Value; |
await _snapshotDatabase.NormalizeTimeAsync((string)rows[i].Cells["HashColumn"].Value, |
cancellationToken); |
|
await _snapshotDatabase.NormalizeTimeAsync(hash, cancellationToken); |
|
progress.Report(new DataGridViewRowProgressSuccess(rows[i], i)); |
} |
catch (Exception exception) |
@@ -1886,10 +1890,9 @@ |
{ |
try |
{ |
var hash = (string)rows[i].Cells["HashColumn"].Value; |
await _snapshotDatabase.DeleteScreenshotAsync((string)rows[i].Cells["HashColumn"].Value, |
cancellationToken); |
|
await _snapshotDatabase.DeleteScreenshotAsync(hash, cancellationToken); |
|
progress.Report(new DataGridViewRowProgressSuccess(rows[i], i)); |
} |
catch (Exception exception) |