Horizon – Diff between revs 3 and 5

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 3 Rev 5
Line 12... Line 12...
12 using System.Threading.Tasks; 12 using System.Threading.Tasks;
13 using System.Windows.Forms; 13 using System.Windows.Forms;
14 using Horizon.Database; 14 using Horizon.Database;
15 using Horizon.Utilities; 15 using Horizon.Utilities;
16 using Microsoft.WindowsAPICodePack.Dialogs; 16 using Microsoft.WindowsAPICodePack.Dialogs;
-   17 using Org.BouncyCastle.Crypto;
17 using Serilog; 18 using Serilog;
Line 18... Line 19...
18   19  
19 namespace Horizon.Snapshots 20 namespace Horizon.Snapshots
20 { 21 {
Line 56... Line 57...
56   57  
Line 57... Line 58...
57 #endregion 58 #endregion
Line 58... Line 59...
58   59  
59 #region Constructors, Destructors and Finalizers 60 #region Constructors, Destructors and Finalizers
60   61  
61 public SnapshotManagerForm() 62 private SnapshotManagerForm()
Line 62... Line 63...
62 { 63 {
Line 590... Line 591...
590 { 591 {
591 toolStripProgressBar1.Minimum = 0; 592 toolStripProgressBar1.Minimum = 0;
592 toolStripProgressBar1.Maximum = (int)await _snapshotDatabase.CountSnapshots(_cancellationToken); 593 toolStripProgressBar1.Maximum = (int)await _snapshotDatabase.CountSnapshots(_cancellationToken);
Line 593... Line 594...
593   594  
594 var snapshotQueue = new ConcurrentQueue<Snapshot>(); -  
Line 595... Line 595...
595 var snapshotsQueuedTaskCompletionSource = new TaskCompletionSource<object>(); 595 var snapshotQueue = new ConcurrentQueue<Snapshot>();
596   596  
597 async void IdleHandler(object idleHandlerSender, EventArgs idleHandlerArgs) -  
598 { -  
599 await snapshotsQueuedTaskCompletionSource.Task; 597 void IdleHandler(object idleHandlerSender, EventArgs idleHandlerArgs)
600   598 {
601 try 599 try
602 { 600 {
603 if (!snapshotQueue.TryDequeue(out var snapshot)) 601 if (snapshotQueue.IsEmpty)
Line 604... Line 602...
604 { 602 {
605 Application.Idle -= IdleHandler; 603 Application.Idle -= IdleHandler;
-   604  
Line -... Line 605...
-   605 dataGridView1.Sort(dataGridView1.Columns["TimeColumn"], ListSortDirection.Descending);
-   606 toolStripStatusLabel1.Text = "Done.";
606   607 }
607 dataGridView1.Sort(dataGridView1.Columns["TimeColumn"], ListSortDirection.Descending); 608  
Line 608... Line 609...
608 toolStripStatusLabel1.Text = "Done."; 609 if (!snapshotQueue.TryDequeue(out var snapshot))
Line 627... Line 628...
627 catch (Exception exception) 628 catch (Exception exception)
628 { 629 {
629 Log.Error(exception, "Could not update data grid view."); 630 Log.Error(exception, "Could not update data grid view.");
630 } 631 }
631 } 632 }
632   633
633 Application.Idle += IdleHandler; -  
634 try 634 try
635 { 635 {
636 foreach (var snapshot in await _snapshotDatabase.LoadSnapshots(_cancellationToken)) 636 await foreach (var snapshot in _snapshotDatabase.LoadSnapshots(_cancellationToken).WithCancellation(_cancellationToken))
637 { 637 {
638 snapshotQueue.Enqueue(snapshot); 638 snapshotQueue.Enqueue(snapshot);
639 } 639 }
Line 640... Line 640...
640   640  
641 snapshotsQueuedTaskCompletionSource.TrySetResult(new { }); 641 Application.Idle += IdleHandler;
642 } 642 }
643 catch (Exception exception) 643 catch (Exception exception)
644 { 644 {
Line 674... Line 674...
674 } 674 }
Line 675... Line 675...
675   675  
676 e.Effect = DragDropEffects.None; 676 e.Effect = DragDropEffects.None;
Line -... Line 677...
-   677 }
-   678  
-   679 private async Task CreateSnapshots(IReadOnlyList<string> files, Bitmap screenCapture, TrackedFolders.TrackedFolders trackedFolders, IProgress<CreateSnapshotProgress> progress, CancellationToken cancellationToken)
-   680 {
-   681 Parallel.ForEach(files, new ParallelOptions { MaxDegreeOfParallelism = 512 }, async file =>
-   682 {
-   683 var color = Color.Empty;
-   684 if (_mainForm.TrackedFolders.TryGet(file, out var folder))
-   685 {
-   686 color = folder.Color;
-   687 }
-   688  
-   689 var fileInfo = File.GetAttributes(file);
-   690 if (fileInfo.HasFlag(FileAttributes.Directory))
-   691 {
-   692 foreach (var directoryFile in Directory.GetFiles(file, "*.*", SearchOption.AllDirectories))
-   693 {
-   694 var name = Path.GetFileName(directoryFile);
-   695 var path = Path.Combine(Path.GetDirectoryName(directoryFile), name);
-   696  
-   697 try
-   698 {
-   699 await _snapshotDatabase.CreateSnapshot(name, path, screenCapture, color,
-   700 _cancellationToken);
-   701  
-   702 progress.Report(new CreateSnapshotProgressSuccess(file));
-   703 }
-   704 catch (Exception exception)
-   705 {
-   706 progress.Report(new CreateSnapshotProgressFailure(file, exception));
-   707 }
-   708 }
-   709  
-   710 return;
-   711 }
-   712  
-   713 var fileName = Path.GetFileName(file);
-   714 var pathName = Path.Combine(Path.GetDirectoryName(file), fileName);
-   715  
-   716 try
-   717 {
-   718 await _snapshotDatabase.CreateSnapshot(fileName, pathName, screenCapture, color,
-   719 _cancellationToken);
-   720  
-   721 progress.Report(new CreateSnapshotProgressSuccess(file));
-   722 }
-   723 catch (Exception exception)
-   724 {
-   725 progress.Report(new CreateSnapshotProgressFailure(file, exception));
-   726 }
677 } 727 });
678   728 }
679 private async void DataGridView1_DragDrop(object sender, DragEventArgs e) 729 private async void DataGridView1_DragDrop(object sender, DragEventArgs e)
680 { 730 {
681 if (!e.Data.GetDataPresent(DataFormats.FileDrop)) 731 if (!e.Data.GetDataPresent(DataFormats.FileDrop))
Line 688... Line 738...
688 toolStripProgressBar1.Minimum = 0; 738 toolStripProgressBar1.Minimum = 0;
689 toolStripProgressBar1.Maximum = files.Length; 739 toolStripProgressBar1.Maximum = files.Length;
690 toolStripStatusLabel1.Text = "Snapshotting files..."; 740 toolStripStatusLabel1.Text = "Snapshotting files...";
Line 691... Line 741...
691   741  
-   742 var screenCapture = ScreenCapture.Capture((CaptureMode)_mainForm.Configuration.CaptureMode);
-   743  
-   744 var progress = new Progress<CreateSnapshotProgress>(createSnapshotProgress =>
-   745 {
-   746 switch (createSnapshotProgress)
-   747 {
-   748 case CreateSnapshotProgressSuccess createSnapshotProgressSuccess:
-   749 toolStripStatusLabel1.Text = $"Snapshot taken of {createSnapshotProgressSuccess.File}.";
-   750 break;
-   751 case CreateSnapshotProgressFailure createSnapshotProgressFailure:
-   752 if (createSnapshotProgressFailure.Exception is SQLiteException { ResultCode: SQLiteErrorCode.Constraint })
-   753 {
-   754 toolStripStatusLabel1.Text = $"Snapshot of file {createSnapshotProgressFailure.File} already exists.";
-   755 break;
-   756 }
-   757  
-   758 toolStripStatusLabel1.Text = $"Could not snapshot file {createSnapshotProgressFailure.File}";
-   759 Log.Warning(createSnapshotProgressFailure.Exception, $"Could not snapshot file {createSnapshotProgressFailure.File}");
-   760 break;
-   761 }
-   762  
-   763 toolStripProgressBar1.Increment(1);
-   764 statusStrip1.Update();
-   765 });
-   766  
-   767 await Task.Factory.StartNew(async () =>
-   768 {
-   769 await CreateSnapshots(files, screenCapture, _mainForm.TrackedFolders, progress, _cancellationToken);
-   770 }, _cancellationToken);
-   771  
-   772 /*
692 var screenCapture = ScreenCapture.Capture((CaptureMode)_mainForm.Configuration.CaptureMode); 773  
693 for (var i = 0; i < files.Length; ++i) 774 foreach (var file in files)
694 { 775 {
695 var color = Color.Empty; 776 var color = Color.Empty;
696 if (_mainForm.TrackedFolders.TryGet(files[i], out var folder)) 777 if (_mainForm.TrackedFolders.TryGet(file, out var folder))
697 { 778 {
698 color = folder.Color; 779 color = folder.Color;
Line 699... Line 780...
699 } 780 }
-   781  
700   782 var fileInfo = File.GetAttributes(file);
701 if (Directory.Exists(files[i])) 783 if (fileInfo.HasFlag(FileAttributes.Directory))
702 { 784 {
703 foreach (var directoryFile in Directory.GetFiles(files[i], "*.*", SearchOption.AllDirectories)) 785 foreach (var directoryFile in Directory.GetFiles(file, "*.*", SearchOption.AllDirectories))
704 { 786 {
Line 705... Line 787...
705 var name = Path.GetFileName(directoryFile); 787 var name = Path.GetFileName(directoryFile);
706 var path = Path.Combine(Path.GetDirectoryName(directoryFile), name); 788 var path = Path.Combine(Path.GetDirectoryName(directoryFile), name);
707   789  
708 try 790 try
Line -... Line 791...
-   791 {
709 { 792 await _snapshotDatabase.CreateSnapshot(name, path, screenCapture, color,
710 await _snapshotDatabase.CreateSnapshot(name, path, screenCapture, color, 793 _cancellationToken);
711 _cancellationToken); 794  
712   795
713 toolStripProgressBar1.Value = i + 1; 796 toolStripStatusLabel1.Text = $"Snapshot taken of {file}.";
714 toolStripStatusLabel1.Text = $"Snapshot taken of {files[i]}."; 797 toolStripProgressBar1.Increment(1);
715 statusStrip1.Update(); 798 statusStrip1.Update();
716 } 799 }
717 catch (SQLiteException exception) 800 catch (SQLiteException exception)
718 { 801 {
719 if (exception.ResultCode == SQLiteErrorCode.Constraint) -  
720 { 802 if (exception.ResultCode == SQLiteErrorCode.Constraint)
-   803 {
721 Log.Information(exception, "Snapshot already exists."); 804 Log.Information(exception, "Snapshot already exists.");
722   805
723 toolStripProgressBar1.Value = i + 1; 806 toolStripStatusLabel1.Text = "Snapshot already exists.";
724 toolStripStatusLabel1.Text = "Snapshot already exists."; 807 toolStripProgressBar1.Increment(1);
725 statusStrip1.Update(); 808 statusStrip1.Update();
Line 732... Line 815...
732 } 815 }
Line 733... Line 816...
733   816  
734 continue; 817 continue;
Line 735... Line 818...
735 } 818 }
736   819  
Line 737... Line 820...
737 var fileName = Path.GetFileName(files[i]); 820 var fileName = Path.GetFileName(file);
738 var pathName = Path.Combine(Path.GetDirectoryName(files[i]), fileName); 821 var pathName = Path.Combine(Path.GetDirectoryName(file), fileName);
739   822  
740 try 823 try
741 { 824 {
742 await _snapshotDatabase.CreateSnapshot(fileName, pathName, screenCapture, color, 825 await _snapshotDatabase.CreateSnapshot(fileName, pathName, screenCapture, color,
743 _cancellationToken); 826 _cancellationToken);
744   827
745 toolStripProgressBar1.Value = i + 1; 828 toolStripStatusLabel1.Text = $"Snapshot taken of {file}.";
746 toolStripStatusLabel1.Text = $"Snapshot taken of {files[i]}."; 829 toolStripProgressBar1.Increment(1);
747 statusStrip1.Update(); 830 statusStrip1.Update();
748 } 831 }
749 catch (SQLiteException exception) 832 catch (SQLiteException exception)
750 { 833 {
751 if (exception.ResultCode == SQLiteErrorCode.Constraint) 834 if (exception.ResultCode == SQLiteErrorCode.Constraint)
752 { -  
753 Log.Information(exception, "Snapshot already exists."); 835 {
-   836 Log.Information(exception, "Snapshot already exists.");
754   837
755 toolStripProgressBar1.Value = i + 1; 838 toolStripStatusLabel1.Text = "Snapshot already exists.";
756 toolStripStatusLabel1.Text = "Snapshot already exists."; 839 toolStripProgressBar1.Increment(1);
757 statusStrip1.Update(); 840 statusStrip1.Update();
758 } 841 }
759 } 842 }
760 catch (Exception exception) 843 catch (Exception exception)
761 { 844 {
-   845 Log.Warning(exception, "Could not create snapshot");
762 Log.Warning(exception, "Could not create snapshot"); 846 }
Line 763... Line 847...
763 } 847 }
764 } 848 */
765 } 849 }