@ -17,6 +17,7 @@ namespace fileFinder
{
HintModel hints = new HintModel ( ) ;
TaskController mainController ;
SearchQueryModel searchQueryModel ;
System . Timers . Timer updateInfoTimer = new System . Timers . Timer ( 1 0 0 0 ) ;
public MainForm ( )
@ -26,6 +27,22 @@ namespace fileFinder
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 ) ;
}
private void fillImagesToResultViewer ( )
{
try
{
ImageList iconList = new ImageList ( ) ;
@ -36,7 +53,8 @@ namespace fileFinder
resultViewer . ImageList = iconList ;
resultViewer . ImageIndex = 3 ;
resultViewer . SelectedImageIndex = 3 ;
} catch { }
}
catch { }
}
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 )
{
if ( ! mainController . isPaused )
@ -146,58 +92,44 @@ namespace fileFinder
Progress < ProgressReportModel > progress = new Progress < ProgressReportModel > ( ) ;
progress . ProgressChanged + = reportProgress ;
if ( mainController . isStopped )
if ( mainController . isStopped | | mainController . isTaskChanged )
{
searchQueryModel = mainController . updateSearchQueryModel ( curDirTextBox . Text ,
queryTextBox . Text , innerQueryTextBox . Text ) ;
handleSearchBtn . Text = "Pause Task" ;
waitOrPauseLabel . Visible = true ;
resultViewer . Enabled = false ;
mainController . beginTask ( ) ;
TreeNode tN = await Task . Run ( ( ) = > buildResultTree ( mainController , progress ) ) ;
TreeNode tN = await Task . Run ( ( ) = > mainController . buildResultTree ( searchQueryModel , progress ) ) ;
updateResultViewer ( tN ) ;
changeVisibilityResultViewer ( ) ;
handleSearchBtn . Text = "Start Task" ;
} else if ( mainController . isPaused )
{
handleSearchBtn . Text = "Pause Task" ;
waitOrPauseLabel . Visible = true ;
resultViewer . Enabled = false ;
changeVisibilityResultViewer ( ) ;
mainController . resumeTask ( ) ;
} else
{
handleSearchBtn . Text = "Resume Task" ;
waitOrPauseLabel . Visible = false ;
resultViewer . Enabled = true ;
changeVisibilityResultViewer ( ) ;
mainController . pauseTask ( ) ;
}
}
public async Task fillChildNode ( TreeNode node , String item )
private void changeVisibilityResultViewer ( )
{
int backSlashIndex = item . IndexOf ( "\\" ) ;
switch ( backSlashIndex )
if ( mainController . isStopped | | mainController . isPaused )
{
case - 1 :
node . Nodes . Add ( item , item , 2 , 2 ) ;
break ;
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 ) ;
nodeIndex = node . Nodes . IndexOfKey ( currentNodeName ) ;
await fillChildNode ( node . Nodes [ nodeIndex ] , item . Remove ( 0 , backSlashIndex + 1 ) ) ;
}
break ;
waitOrPauseLabel . Visible = false ;
resultViewer . Enabled = true ;
} else
{
waitOrPauseLabel . Visible = true ;
resultViewer . Enabled = false ;
}
}
public void updateInfoLabel ( string fileUrl , int counter )
{
TimeSpan tS = mainController . elapsedTime ( ) ;
@ -230,10 +162,25 @@ namespace fileFinder
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 . fileInnerQuery = innerQueryTextBox . Text ;
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 ;
}
}
}