1
0
Fork 0
mirror of https://github.com/wagesj45/mdfinder.git synced 2024-12-21 08:02:28 -06:00

Periodic work

Safety check in.
This commit is contained in:
Jordan Wages 2019-02-16 23:43:50 -06:00
parent f5b2f9e0f6
commit c0afde943e
4 changed files with 83 additions and 40 deletions

View file

@ -49,7 +49,7 @@
</Grid.ColumnDefinitions>
<ContentControl Grid.Column="0" Template="{StaticResource FolderIcon}" Width="16" Margin="0,0,4,0" />
<TextBox Name="txtScanLocation" Grid.Column="1" IsReadOnly="True" />
<Button Grid.Column="2" MinWidth="25" Name="btnFilePicker">...</Button>
<Button Grid.Column="2" MinWidth="25" Name="btnFilePicker" Click="btnFilePicker_Click">...</Button>
</Grid>
</GroupBox>
<GroupBox Grid.ColumnSpan="3" Grid.Row="1" Header="{x:Static loc:Localization.ActionBarLabel}">
@ -61,7 +61,7 @@
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Name="btnProcess" ToolTip="{x:Static loc:Localization.ScanTooltip}" IsEnabled="{Binding Scanner.IsScanning, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type mdfinder:MainWindow}}, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource InverseBoolConverter}}">
<Button Grid.Row="0" Name="btnScan" ToolTip="{x:Static loc:Localization.ScanTooltip}" IsEnabled="{Binding Scanner.IsScanning, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type mdfinder:MainWindow}}, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource InverseBoolConverter}}" Click="btnScan_Click">
<StackPanel Orientation="Horizontal">
<ContentControl MaxWidth="16" HorizontalAlignment="Center" Template="{StaticResource ScanIcon}" />
<Label Content="{x:Static loc:Localization.ScanLabel}" />
@ -70,9 +70,7 @@
</Grid>
</GroupBox>
<DataGrid Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type mdfinder:MainWindow}} }" />
<StatusBar Grid.Row="3" Grid.ColumnSpan="4">
<ProgressBar />
</StatusBar>
<ProgressBar Grid.Row="3" Grid.ColumnSpan="4" Name="progressBar" Minimum="0" Height="16"/>
</Grid>
</DockPanel>
</Window>

View file

@ -12,6 +12,8 @@ using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Threading;
namespace mdfinder
{
@ -24,6 +26,8 @@ namespace mdfinder
public DBHelper Database { get; set; }
public Scanner Scanner { get; set; }
#endregion
#region Constructors
@ -32,8 +36,40 @@ namespace mdfinder
public MainWindow()
{
this.Database = new DBHelper();
this.Scanner = new Scanner();
this.Scanner.DirectoryFound += (sender, args) => Dispatcher.Invoke(() => txtScanLocation.Text = args.Directory.Name);
//this.Scanner.FilesFound += (sender, args) => args.Files;
this.Scanner.ReportProgress += (sender, args) => Dispatcher.Invoke(() => { if (args.Processed > 0) { this.progressBar.Value = args.Percentage * 100; } });
InitializeComponent();
}
}
#endregion
#region Methods
private void btnFilePicker_Click(object sender, RoutedEventArgs e)
{
var fbd = new FolderBrowserDialog();
if(fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtScanLocation.Text = fbd.SelectedPath;
}
}
private void btnScan_Click(object sender, RoutedEventArgs e)
{
var location = txtScanLocation.Text;
if (!this.Scanner.IsScanning)
{
new Thread(() =>
{
this.Scanner.Scan(location);
}).Start();
}
}
#endregion
}

View file

@ -8,37 +8,37 @@ using System.Threading.Tasks;
namespace mdfinder
{
/// <summary> Scans directories, logging files and their attributes. </summary>
public static class Scanner
public class Scanner
{
#region Members
/// <summary> Event queue for all listeners interested in FilesFound events. </summary>
public static event EventHandler<FilesFoundEventArgs> FilesFound;
public event EventHandler<FilesFoundEventArgs> FilesFound;
/// <summary> Event queue for all listeners interested in DirectoryFound events. </summary>
public static event EventHandler<DirectoryFoundEventArgs> DirectoryFound;
public event EventHandler<DirectoryFoundEventArgs> DirectoryFound;
/// <summary> Event queue for all listeners interested in ReportProgress events. </summary>
public static event EventHandler<ProgressReportEventArgs> ReportProgress;
public event EventHandler<ProgressReportEventArgs> ReportProgress;
#endregion
#region Properties
public static uint Processed { get; private set; }
public uint Processed { get; private set; }
public static uint Total { get; private set; }
public uint Total { get; private set; }
public static bool IsScanning { get; private set; }
public bool IsScanning { get; private set; }
#endregion
#region Methods
public static void Scan(string path)
public void Scan(string path)
{
Processed = 0;
Total = 0;
this.Processed = 0;
this.Total = 0;
var scanPath = new DirectoryInfo(path);
if (scanPath.Exists)
@ -47,51 +47,58 @@ namespace mdfinder
}
}
private static void Scan(DirectoryInfo directory)
private void Scan(DirectoryInfo directory)
{
var files = directory.GetFiles();
var fileBatches = files.Bin(Properties.Settings.Default.FilesFoundAlert);
var subdirectories = directory.GetDirectories();
Total += (uint)files.Count();
foreach (var subdirectory in subdirectories)
try
{
OnDirectoryFound(subdirectory);
var files = directory.GetFiles();
var fileBatches = files.Bin(Properties.Settings.Default.FilesFoundAlert);
var subdirectories = directory.GetDirectories();
Scan(subdirectory);
this.Total += (uint)files.Count();
foreach (var subdirectory in subdirectories)
{
OnDirectoryFound(subdirectory);
Scan(subdirectory);
}
foreach (var batch in fileBatches)
{
OnFilesFound(batch);
this.Processed += (uint)batch.Count();
OnReportProgress(this.Processed, this.Total);
}
}
foreach (var batch in fileBatches)
catch(UnauthorizedAccessException unauthorizedAccessException)
{
OnFilesFound(batch);
Processed += (uint)batch.Count();
OnReportProgress(Processed, Total);
//Ignore and just continue.
}
}
/// <summary> Executes the files found action. </summary>
/// <param name="files"> The files. </param>
private static void OnFilesFound(IEnumerable<FileInfo> files)
private void OnFilesFound(IEnumerable<FileInfo> files)
{
FilesFound?.Invoke(null, new FilesFoundEventArgs(files));
this.FilesFound?.Invoke(this, new FilesFoundEventArgs(files));
}
/// <summary> Executes the directory found action. </summary>
/// <param name="directory"> Pathname of the directory. </param>
private static void OnDirectoryFound(DirectoryInfo directory)
private void OnDirectoryFound(DirectoryInfo directory)
{
DirectoryFound?.Invoke(null, new DirectoryFoundEventArgs(directory));
this.DirectoryFound?.Invoke(this, new DirectoryFoundEventArgs(directory));
}
/// <summary> Executes the report progress action. </summary>
/// <param name="processed"> The processed. </param>
/// <param name="total"> Number of. </param>
private static void OnReportProgress(uint processed, uint total)
private void OnReportProgress(uint processed, uint total)
{
ReportProgress?.Invoke(null, new ProgressReportEventArgs(processed, total));
this.ReportProgress?.Invoke(this, new ProgressReportEventArgs(processed, total));
}
#endregion
@ -175,7 +182,8 @@ namespace mdfinder
/// <param name="total"> The total discovereditem count. </param>
public ProgressReportEventArgs(uint processed, uint total)
{
this.Processed = processed;
this.Total = total;
}
#endregion

View file

@ -54,6 +54,7 @@
<Reference Include="System.Data" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Security" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />