random-file-runner/MainForm.cs

253 lines
7.1 KiB
C#

using RandomFileRunner.IO;
using RyzStudio.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RandomFileRunner
{
public partial class MainForm : Form
{
protected readonly Random randy;
protected OptionsForm optionsForm = null;
protected bool isBusy = false;
protected bool requestCancel = false;
protected List<string> foundFiles = null;
protected Process currentProcess = null;
public MainForm()
{
InitializeComponent();
textBox1.Text = "*.*";
memoBox1.InnerTextBox.WordWrap = false;
randy = new Random();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (this.IsBusy)
{
e.Cancel = true;
return;
}
base.OnFormClosing(e);
}
public bool IsBusy
{
get => isBusy;
set
{
isBusy = value;
ThreadControl.SetValue(pictureBox1, (isBusy ? AppResource.loading_block : null));
ThreadControl.SetEnable(textBox1, !isBusy);
//ThreadControl.SetEnable(button2, !isBusy);
button2.LabelText = (isBusy? "&Cancel" : "&Search");
ThreadControl.SetEnable(button3, !isBusy);
ThreadControl.SetEnable(button4, !isBusy);
ThreadControl.SetEnable(memoBox1, !isBusy);
ThreadControl.SetEnable(button5, !isBusy);
//ThreadControl.SetEnable(button1, !isBusy);
}
}
public AppSession CurrentSession { get; set; } = new AppSession();
protected void AddSearchItem(string line)
{
memoBox1.Text = memoBox1.Text.Trim();
// above line-break
if (!string.IsNullOrWhiteSpace(memoBox1.Text)) memoBox1.Text += Environment.NewLine;
memoBox1.Text += line + Environment.NewLine;
}
protected bool SearchDirecory_OnFound(string file, ulong searchCount, int searchQueue)
{
if (!string.IsNullOrWhiteSpace(file))
{
foundFiles.Add(file);
}
//ThreadControl.SetText(label2, foundFiles.Count.ToString());
ThreadControl.SetText(label2, foundFiles.Count.ToString("#,#", System.Globalization.CultureInfo.CurrentCulture) + Environment.NewLine + searchQueue.ToString("#,#", System.Globalization.CultureInfo.CurrentCulture));
return true;
}
/// <summary>
/// Search
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void button2_MouseClick(object sender, MouseEventArgs e)
{
await Task.Run(() =>
{
if (this.IsBusy)
{
requestCancel = true;
button2.LabelText = "&Cancelling...";
return;
}
this.IsBusy = true;
requestCancel = false;
foundFiles = new List<string>();
currentProcess = null;
string[] itemList = memoBox1.Text?.Trim().Split('\n');
for (int i=0; i< itemList.Length; i++)
{
if (string.IsNullOrWhiteSpace(itemList[i])) continue;
if (requestCancel) break;
string item = itemList[i]?.Trim();
if (File.Exists(item))
{
if (SmartFileSystem.IsFileAccessible(item))
{
foundFiles.Add(item);
ThreadControl.SetText(label2, foundFiles.Count.ToString());
continue;
}
}
if (Directory.Exists(item))
{
SmartFileSystem.GetFiles(item, "*.txt", SearchDirecory_OnFound);
ThreadControl.SetText(label2, foundFiles.Count.ToString());
continue;
}
}
ThreadControl.SetText(label2, foundFiles.Count.ToString("#,#", System.Globalization.CultureInfo.CurrentCulture) + " File" + ((foundFiles.Count == 1) ? "" : "s") + " Found");
this.IsBusy = false;
requestCancel = false;
});
}
/// <summary>
/// Add directory
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void addDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.IsBusy) return;
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
AddSearchItem(folderBrowserDialog1.SelectedPath);
}
}
/// <summary>
/// Add file
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void addFileToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.IsBusy) return;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
AddSearchItem(openFileDialog1.FileName);
}
}
/// <summary>
/// Clear
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_MouseClick(object sender, MouseEventArgs e)
{
if (this.IsBusy) return;
memoBox1.Text = string.Empty;
}
private void memoBox1_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
if (this.IsBusy)
{
e.Effect = DragDropEffects.None;
}
else
{
e.Effect = DragDropEffects.Copy;
}
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void memoBox1_DragDrop(object sender, DragEventArgs e)
{
if (this.IsBusy) return;
string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];
if (fileList == null)
{
return;
}
foreach (string item in fileList)
{
AddSearchItem(item);
}
}
/// <summary>
/// Run next
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_MouseClick(object sender, MouseEventArgs e)
{
}
/// <summary>
/// Close
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_MouseClick(object sender, MouseEventArgs e)
{
this.Close();
}
}
}