@ -17,6 +17,7 @@ namespace fileFinder
{
{
HintModel hints = new HintModel ( ) ;
HintModel hints = new HintModel ( ) ;
TaskController mainController ;
TaskController mainController ;
SearchQueryModel searchQueryModel ;
System . Timers . Timer updateInfoTimer = new System . Timers . Timer ( 1 0 0 0 ) ;
System . Timers . Timer updateInfoTimer = new System . Timers . Timer ( 1 0 0 0 ) ;
public MainForm ( )
public MainForm ( )
@ -26,6 +27,22 @@ namespace fileFinder
this . updateInfoTimer . Elapsed + = refreshInfoByTimer ;
this . updateInfoTimer . Elapsed + = refreshInfoByTimer ;
this . updateInfoTimer . AutoReset = true ;
this . updateInfoTimer . AutoReset = true ;
this . updateInfoTimer . Enabled = 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 ) ;
}
private void fillImagesToResultViewer ( )
{
try
try
{
{
ImageList iconList = new ImageList ( ) ;
ImageList iconList = new ImageList ( ) ;
@ -36,7 +53,8 @@ namespace fileFinder
resultViewer . ImageList = iconList ;
resultViewer . ImageList = iconList ;
resultViewer . ImageIndex = 3 ;
resultViewer . ImageIndex = 3 ;
resultViewer . SelectedImageIndex = 3 ;
resultViewer . SelectedImageIndex = 3 ;
} catch { }
}
catch { }
}
}
private void dirSelectBtn_Click ( object sender , EventArgs e )
private void dirSelectBtn_Click ( object sender , EventArgs e )
@ -61,78 +79,6 @@ namespace fileFinder
//}
//}
}
}
private async Task < TreeNode > buildResultTree ( TaskController controller , IProgress < ProgressReportModel > report )
{
TreeNode itemsNode = new TreeNode ( ) ;
ProgressReportModel _ report = new ProgressReportModel ( ) ;
List < string > foundFiles = getFileList ( curDirTextBox . Text , queryTextBox . Text ) ;
int counter = 0 ;
foreach ( string item in foundFiles )
{
while ( controller . isPaused )
{
Thread . Sleep ( 1 0 0 ) ;
if ( ! controller . isFormUpdatedAfterPause )
{
controller . isFormUpdatedAfterPause = true ;
_ report . progress = counter ;
_ report . currentFileUrl = item ;
_ report . currentTreeNode = ( TreeNode ) itemsNode . Clone ( ) ;
report . Report ( _ report ) ;
}
}
string [ ] fileLines = getFileContents ( item ) ;
_ report . progress = counter ;
_ report . currentFileUrl = item ;
report . Report ( _ report ) ;
if ( ( fileLines . Length > 0 ) & & ( isFileContainQuery ( fileLines , innerQueryTextBox . Text ) ) )
{
counter + + ;
await fillChildNode ( itemsNode , item . Replace ( curDirTextBox . Text , "" ) ) ;
}
}
controller . stopTask ( ) ;
return itemsNode ;
}
private List < string > getFileList ( string directory , string nameQuery )
{
List < String > fileList = new List < String > ( ) ;
try
{
fileList = Directory . GetFiles ( directory , nameQuery , SearchOption . AllDirectories ) . ToList ( ) ;
}
catch ( Exception ex )
{
MessageBox . Show ( ex . Message , "Не удалось получить доступ к файлу/папке" ) ;
}
return fileList ;
}
private string [ ] getFileContents ( string fileUrl )
{
try
{
return File . ReadAllLines ( fileUrl , Encoding . UTF8 ) ;
}
catch ( Exception ex )
{
MessageBox . Show ( ex . Message , "Ошибка поиска внутри файла!" ) ;
}
return null ;
}
private bool isFileContainQuery ( string [ ] fileLines , string query )
{
foreach ( string item in fileLines )
if ( item . IndexOf ( query ) ! = - 1 )
return true ;
return false ;
}
private void reportProgress ( object sender , ProgressReportModel report )
private void reportProgress ( object sender , ProgressReportModel report )
{
{
if ( ! mainController . isPaused )
if ( ! mainController . isPaused )
@ -146,55 +92,41 @@ namespace fileFinder
Progress < ProgressReportModel > progress = new Progress < ProgressReportModel > ( ) ;
Progress < ProgressReportModel > progress = new Progress < ProgressReportModel > ( ) ;
progress . ProgressChanged + = reportProgress ;
progress . ProgressChanged + = reportProgress ;
if ( mainController . isStopped )
if ( mainController . isStopped | | mainController . isTaskChanged )
{
{
searchQueryModel = mainController . updateSearchQueryModel ( curDirTextBox . Text ,
queryTextBox . Text , innerQueryTextBox . Text ) ;
handleSearchBtn . Text = "Pause Task" ;
handleSearchBtn . Text = "Pause Task" ;
waitOrPauseLabel . Visible = true ;
waitOrPauseLabel . Visible = true ;
resultViewer . Enabled = false ;
resultViewer . Enabled = false ;
mainController . beginTask ( ) ;
mainController . beginTask ( ) ;
TreeNode tN = await Task . Run ( ( ) = > buildResultTree ( mainController , progress ) ) ;
TreeNode tN = await Task . Run ( ( ) = > mainController . buildResultTree ( searchQueryModel , progress ) ) ;
updateResultViewer ( tN ) ;
updateResultViewer ( tN ) ;
changeVisibilityResultViewer ( ) ;
handleSearchBtn . Text = "Start Task" ;
handleSearchBtn . Text = "Start Task" ;
} else if ( mainController . isPaused )
} else if ( mainController . isPaused )
{
{
handleSearchBtn . Text = "Pause Task" ;
handleSearchBtn . Text = "Pause Task" ;
waitOrPauseLabel . Visible = true ;
changeVisibilityResultViewer ( ) ;
resultViewer . Enabled = false ;
mainController . resumeTask ( ) ;
mainController . resumeTask ( ) ;
} else
} else
{
{
handleSearchBtn . Text = "Resume Task" ;
handleSearchBtn . Text = "Resume Task" ;
waitOrPauseLabel . Visible = false ;
changeVisibilityResultViewer ( ) ;
resultViewer . Enabled = true ;
mainController . pauseTask ( ) ;
mainController . pauseTask ( ) ;
}
}
}
}
public async Task fillChildNode ( TreeNode node , String item )
private void changeVisibilityResultViewer ( )
{
{
int backSlashIndex = item . IndexOf ( "\\" ) ;
if ( mainController . isStopped | | mainController . isPaused )
switch ( backSlashIndex )
{
{
case - 1 :
waitOrPauseLabel . Visible = false ;
node . Nodes . Add ( item , item , 2 , 2 ) ;
resultViewer . Enabled = true ;
break ;
} else
case 0 :
item = item . Remove ( 0 , 1 ) ;
await fillChildNode ( node , item ) ;
break ;
default :
String currentNodeName = item . Substring ( 0 , backSlashIndex ) ;
int nodeIndex = node . Nodes . IndexOfKey ( currentNodeName ) ;
if ( nodeIndex ! = - 1 )
await fillChildNode ( node . Nodes [ nodeIndex ] , item . Remove ( 0 , backSlashIndex + 1 ) ) ;
else
{
{
node . Nodes . Add ( currentNodeName , currentNodeName , 0 , 0 ) ;
waitOrPauseLabel . Visible = true ;
nodeIndex = node . Nodes . IndexOfKey ( currentNodeName ) ;
resultViewer . Enabled = false ;
await fillChildNode ( node . Nodes [ nodeIndex ] , item . Remove ( 0 , backSlashIndex + 1 ) ) ;
}
break ;
}
}
}
}
@ -230,10 +162,25 @@ namespace fileFinder
private void MainForm_FormClosing ( object sender , FormClosingEventArgs e )
private void MainForm_FormClosing ( object sender , FormClosingEventArgs e )
{
{
MainSettings . Default . urlDirectory = curDirTextBox . Text ;
MainSettings . Default . fileUrl = curDirTextBox . Text ;
MainSettings . Default . fileNameQuery = queryTextBox . Text ;
MainSettings . Default . fileNameQuery = queryTextBox . Text ;
MainSettings . Default . fileInnerQuery = innerQueryTextBox . Text ;
MainSettings . Default . fileInnerQuery = innerQueryTextBox . Text ;
MainSettings . Default . Save ( ) ;
MainSettings . Default . Save ( ) ;
}
}
private void curDirTextBox_TextChanged ( object sender , EventArgs e )
{
mainController . isTaskChanged = true ;
}
private void queryTextBox_TextChanged ( object sender , EventArgs e )
{
mainController . isTaskChanged = true ;
}
private void innerQueryTextBox_TextChanged ( object sender , EventArgs e )
{
mainController . isTaskChanged = true ;
}
}
}
}
}