Horizon

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 4  →  ?path2? @ 5
/Horizon/Snapshots/SnapshotManagerForm.cs
@@ -14,6 +14,7 @@
using Horizon.Database;
using Horizon.Utilities;
using Microsoft.WindowsAPICodePack.Dialogs;
using Org.BouncyCastle.Crypto;
using Serilog;
 
namespace Horizon.Snapshots
@@ -58,7 +59,7 @@
 
#region Constructors, Destructors and Finalizers
 
public SnapshotManagerForm()
private SnapshotManagerForm()
{
InitializeComponent();
Utilities.WindowState.FormTracker.Track(this);
@@ -592,21 +593,21 @@
toolStripProgressBar1.Maximum = (int)await _snapshotDatabase.CountSnapshots(_cancellationToken);
 
var snapshotQueue = new ConcurrentQueue<Snapshot>();
var snapshotsQueuedTaskCompletionSource = new TaskCompletionSource<object>();
 
async void IdleHandler(object idleHandlerSender, EventArgs idleHandlerArgs)
void IdleHandler(object idleHandlerSender, EventArgs idleHandlerArgs)
{
await snapshotsQueuedTaskCompletionSource.Task;
 
try
{
if (!snapshotQueue.TryDequeue(out var snapshot))
if (snapshotQueue.IsEmpty)
{
Application.Idle -= IdleHandler;
 
dataGridView1.Sort(dataGridView1.Columns["TimeColumn"], ListSortDirection.Descending);
toolStripStatusLabel1.Text = "Done.";
}
 
if (!snapshotQueue.TryDequeue(out var snapshot))
{
return;
}
 
@@ -629,16 +630,15 @@
Log.Error(exception, "Could not update data grid view.");
}
}
 
Application.Idle += IdleHandler;
try
{
foreach (var snapshot in await _snapshotDatabase.LoadSnapshots(_cancellationToken))
await foreach (var snapshot in _snapshotDatabase.LoadSnapshots(_cancellationToken).WithCancellation(_cancellationToken))
{
snapshotQueue.Enqueue(snapshot);
}
 
snapshotsQueuedTaskCompletionSource.TrySetResult(new { });
Application.Idle += IdleHandler;
}
catch (Exception exception)
{
@@ -676,6 +676,56 @@
e.Effect = DragDropEffects.None;
}
 
private async Task CreateSnapshots(IReadOnlyList<string> files, Bitmap screenCapture, TrackedFolders.TrackedFolders trackedFolders, IProgress<CreateSnapshotProgress> progress, CancellationToken cancellationToken)
{
Parallel.ForEach(files, new ParallelOptions { MaxDegreeOfParallelism = 512 }, async file =>
{
var color = Color.Empty;
if (_mainForm.TrackedFolders.TryGet(file, out var folder))
{
color = folder.Color;
}
 
var fileInfo = File.GetAttributes(file);
if (fileInfo.HasFlag(FileAttributes.Directory))
{
foreach (var directoryFile in Directory.GetFiles(file, "*.*", SearchOption.AllDirectories))
{
var name = Path.GetFileName(directoryFile);
var path = Path.Combine(Path.GetDirectoryName(directoryFile), name);
 
try
{
await _snapshotDatabase.CreateSnapshot(name, path, screenCapture, color,
_cancellationToken);
 
progress.Report(new CreateSnapshotProgressSuccess(file));
}
catch (Exception exception)
{
progress.Report(new CreateSnapshotProgressFailure(file, exception));
}
}
 
return;
}
 
var fileName = Path.GetFileName(file);
var pathName = Path.Combine(Path.GetDirectoryName(file), fileName);
 
try
{
await _snapshotDatabase.CreateSnapshot(fileName, pathName, screenCapture, color,
_cancellationToken);
 
progress.Report(new CreateSnapshotProgressSuccess(file));
}
catch (Exception exception)
{
progress.Report(new CreateSnapshotProgressFailure(file, exception));
}
});
}
private async void DataGridView1_DragDrop(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent(DataFormats.FileDrop))
@@ -690,17 +740,49 @@
toolStripStatusLabel1.Text = "Snapshotting files...";
 
var screenCapture = ScreenCapture.Capture((CaptureMode)_mainForm.Configuration.CaptureMode);
for (var i = 0; i < files.Length; ++i)
 
var progress = new Progress<CreateSnapshotProgress>(createSnapshotProgress =>
{
switch (createSnapshotProgress)
{
case CreateSnapshotProgressSuccess createSnapshotProgressSuccess:
toolStripStatusLabel1.Text = $"Snapshot taken of {createSnapshotProgressSuccess.File}.";
break;
case CreateSnapshotProgressFailure createSnapshotProgressFailure:
if (createSnapshotProgressFailure.Exception is SQLiteException { ResultCode: SQLiteErrorCode.Constraint })
{
toolStripStatusLabel1.Text = $"Snapshot of file {createSnapshotProgressFailure.File} already exists.";
break;
}
 
toolStripStatusLabel1.Text = $"Could not snapshot file {createSnapshotProgressFailure.File}";
Log.Warning(createSnapshotProgressFailure.Exception, $"Could not snapshot file {createSnapshotProgressFailure.File}");
break;
}
 
toolStripProgressBar1.Increment(1);
statusStrip1.Update();
});
 
await Task.Factory.StartNew(async () =>
{
await CreateSnapshots(files, screenCapture, _mainForm.TrackedFolders, progress, _cancellationToken);
}, _cancellationToken);
 
/*
 
foreach (var file in files)
{
var color = Color.Empty;
if (_mainForm.TrackedFolders.TryGet(files[i], out var folder))
if (_mainForm.TrackedFolders.TryGet(file, out var folder))
{
color = folder.Color;
}
 
if (Directory.Exists(files[i]))
var fileInfo = File.GetAttributes(file);
if (fileInfo.HasFlag(FileAttributes.Directory))
{
foreach (var directoryFile in Directory.GetFiles(files[i], "*.*", SearchOption.AllDirectories))
foreach (var directoryFile in Directory.GetFiles(file, "*.*", SearchOption.AllDirectories))
{
var name = Path.GetFileName(directoryFile);
var path = Path.Combine(Path.GetDirectoryName(directoryFile), name);
@@ -710,8 +792,9 @@
await _snapshotDatabase.CreateSnapshot(name, path, screenCapture, color,
_cancellationToken);
 
toolStripProgressBar1.Value = i + 1;
toolStripStatusLabel1.Text = $"Snapshot taken of {files[i]}.";
toolStripStatusLabel1.Text = $"Snapshot taken of {file}.";
toolStripProgressBar1.Increment(1);
statusStrip1.Update();
}
catch (SQLiteException exception)
@@ -719,9 +802,9 @@
if (exception.ResultCode == SQLiteErrorCode.Constraint)
{
Log.Information(exception, "Snapshot already exists.");
 
toolStripProgressBar1.Value = i + 1;
toolStripStatusLabel1.Text = "Snapshot already exists.";
toolStripProgressBar1.Increment(1);
statusStrip1.Update();
}
}
@@ -734,16 +817,16 @@
continue;
}
 
var fileName = Path.GetFileName(files[i]);
var pathName = Path.Combine(Path.GetDirectoryName(files[i]), fileName);
var fileName = Path.GetFileName(file);
var pathName = Path.Combine(Path.GetDirectoryName(file), fileName);
 
try
{
await _snapshotDatabase.CreateSnapshot(fileName, pathName, screenCapture, color,
_cancellationToken);
 
toolStripProgressBar1.Value = i + 1;
toolStripStatusLabel1.Text = $"Snapshot taken of {files[i]}.";
toolStripStatusLabel1.Text = $"Snapshot taken of {file}.";
toolStripProgressBar1.Increment(1);
statusStrip1.Update();
}
catch (SQLiteException exception)
@@ -751,9 +834,9 @@
if (exception.ResultCode == SQLiteErrorCode.Constraint)
{
Log.Information(exception, "Snapshot already exists.");
 
toolStripProgressBar1.Value = i + 1;
toolStripStatusLabel1.Text = "Snapshot already exists.";
toolStripProgressBar1.Increment(1);
statusStrip1.Update();
}
}
@@ -762,6 +845,7 @@
Log.Warning(exception, "Could not create snapshot");
}
}
*/
}
 
private async void FileToolStripMenuItem_Click(object sender, EventArgs e)