Приложение, которое ищет файлы по заданному шаблону имени файла и по заданной фразе для внутреннего содержимого файла.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
7.0 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Timers;
namespace fileFinder
{
public partial class MainForm : Form
{
HintModel hints = new HintModel();
TaskController mainController = new TaskController();
SearchQueryModel searchQueryModel;
System.Timers.Timer updateInfoTimer = new System.Timers.Timer(1000);
public MainForm()
{
InitializeComponent();
this.updateInfoTimer.Elapsed += refreshInfoByTimer;
this.updateInfoTimer.AutoReset = true;
this.updateInfoTimer.Enabled = true;
fillImagesToResultViewer();
restoreLastSession();
}
private void restoreLastSession()
{
MainSettings sets = MainSettings.Default;
curDirTextBox.Text = sets.fileUrl;
innerQueryTextBox.Text = sets.fileInnerQuery;
queryTextBox.Text = sets.fileNameQuery;
searchQueryModel = mainController.updateSearchQueryModel(sets.fileUrl,
sets.fileInnerQuery, sets.fileNameQuery);
mainController.isTaskChanged = false;
}
private void fillImagesToResultViewer()
{
try
{
ImageList iconList = new ImageList();
iconList.Images.Add(Image.FromFile("Folder.png")); // 0
iconList.Images.Add(Image.FromFile("OpenedFolder.png")); // 1
iconList.Images.Add(Image.FromFile("File.png")); // 2
iconList.Images.Add(Image.FromFile("Search.png")); // 3
resultViewer.ImageList = iconList;
resultViewer.ImageIndex = 3;
resultViewer.SelectedImageIndex = 3;
}
catch { }
}
private void dirSelectBtn_Click(object sender, EventArgs e)
{
using (var dialog = new FolderBrowserDialog())
{
DialogResult result = dialog.ShowDialog();
if (result == DialogResult.OK && !String.IsNullOrWhiteSpace(dialog.SelectedPath))
{
curDirTextBox.Text = dialog.SelectedPath;
hints.isDirectoryHintActive = false;
}
}
}
private void curDirTextBox_Enter(object sender, EventArgs e)
{
//if (hints.isDirectoryHintActive)
//{
// hints.isDirectoryHintActive = false;
// curDirTextBox.Text = "";
//}
}
private void reportProgress(object sender, ProgressReportModel report)
{
if (!mainController.isPaused)
updateInfoLabel(report.currentFileUrl, report.progress);
else if (mainController.isPaused)
updateResultViewer(report.currentTreeNode);
}
private async void handleSearchBtn_Click(object sender, EventArgs e)
{
Progress<ProgressReportModel> progress = new Progress<ProgressReportModel>();
progress.ProgressChanged += reportProgress;
if (mainController.isStopped || mainController.isTaskChanged)
{
mainController.stopTask();
mainController = new TaskController();
searchQueryModel = mainController.updateSearchQueryModel(curDirTextBox.Text,
queryTextBox.Text, innerQueryTextBox.Text);
handleSearchBtn.Text = "Pause Task";
mainController.beginTask();
changeVisibilityResultViewer();
TreeNode tN = await Task.Run(() => mainController.buildResultTree(searchQueryModel, progress));
if (tN != null)
{
updateResultViewer(tN);
changeVisibilityResultViewer();
handleSearchBtn.Text = "Start Task";
}
} else if (mainController.isPaused)
{
handleSearchBtn.Text = "Pause Task";
mainController.resumeTask();
changeVisibilityResultViewer();
} else
{
handleSearchBtn.Text = "Resume Task";
mainController.pauseTask();
changeVisibilityResultViewer();
}
}
private void changeVisibilityResultViewer()
{
if (mainController.isStopped || mainController.isPaused)
{
waitOrPauseLabel.Visible = false;
resultViewer.Enabled = true;
} else
{
waitOrPauseLabel.Visible = true;
resultViewer.Enabled = false;
}
}
public void updateInfoLabel (string fileUrl, int counter)
{
TimeSpan tS = mainController.elapsedTime();
string time = String.Format("{0:00}:{1:00}:{2:00}", tS.Hours, tS.Minutes, tS.Seconds);
infoLabel.Text = "Time: " + time + " " + "| Processed files: " + counter +
" | Current file: " + fileUrl;
}
public void updateResultViewer(TreeNode tN)
{
if (tN != null)
{
resultViewer.Nodes.Clear();
resultViewer.Nodes.Add(tN);
}
}
public void refreshInfoByTimer (object sender, ElapsedEventArgs e)
{
Action refresh = () =>
{
TimeSpan tS = mainController.elapsedTime();
string time = String.Format("{0:00}:{1:00}:{2:00}", tS.Hours, tS.Minutes, tS.Seconds);
infoLabel.Text = "Time: " + time + " |" +
infoLabel.Text.Substring(infoLabel.Text.IndexOf("|") + 1);
};
try { this.Invoke(refresh); } catch { }
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
MainSettings.Default.fileUrl = curDirTextBox.Text;
MainSettings.Default.fileNameQuery = queryTextBox.Text;
MainSettings.Default.fileInnerQuery = innerQueryTextBox.Text;
MainSettings.Default.Save();
}
private void curDirTextBox_TextChanged(object sender, EventArgs e)
{
changeTaskBtnStatus();
}
private void queryTextBox_TextChanged(object sender, EventArgs e)
{
changeTaskBtnStatus();
}
private void innerQueryTextBox_TextChanged(object sender, EventArgs e)
{
changeTaskBtnStatus();
}
private void changeTaskBtnStatus()
{
if (!mainController.isTaskChanged)
{
mainController.isTaskChanged = true;
handleSearchBtn.Text = "Start New Task";
}
}
}
}