@ -15,9 +15,13 @@ namespace fileFinder
{
{
private Stopwatch stopwatch = new Stopwatch ( ) ;
private Stopwatch stopwatch = new Stopwatch ( ) ;
public TaskState state { get ; private set ; }
public TaskState state { get ; private set ; }
private MainForm form ;
private TreeView currentView ;
public TaskController ( )
public TaskController ( MainForm form , TreeView viewer )
{
{
this . form = form ;
currentView = viewer ;
state = TaskState . Created ;
state = TaskState . Created ;
}
}
@ -50,13 +54,12 @@ namespace fileFinder
return stopwatch . Elapsed ;
return stopwatch . Elapsed ;
}
}
public async Task < TreeNode > buildResultTree ( SearchQueryModel query , IProgress < ProgressReportModel > report )
public async void buildResultTree ( SearchQueryModel query , IProgress < ProgressReportModel > report )
{
{
TaskController controller = this ;
TaskController controller = this ;
TreeNode itemsNode = new TreeNode ( ) ;
report . Report ( createReport ( "Индексирование заданного пути" , 0 , 0 , 0 ) ) ;
report . Report ( createReport ( null , "Индексирование заданного пути" , 0 , 0 , 0 ) ) ;
List < string > foundFiles = getFileList ( query . fileUrl , query . fileNameQuery ) ;
List < string > foundFiles = getFileList ( query . fileUrl , query . fileNameQuery ) ;
report . Report ( createReport ( null , null , 0 , 0 , foundFiles . Count ) ) ;
report . Report ( createReport ( null , 0 , 0 , foundFiles . Count ) ) ;
int matchingFiles = 0 , processedFiles = 0 ;
int matchingFiles = 0 , processedFiles = 0 ;
foreach ( string item in foundFiles )
foreach ( string item in foundFiles )
@ -64,34 +67,33 @@ namespace fileFinder
switch ( state )
switch ( state )
{
{
case TaskState . Finished :
case TaskState . Finished :
return null ;
return ;
case TaskState . Pause :
case TaskState . Pause :
report . Report ( createReport ( ( TreeNode ) itemsNode . Clone ( ) ,
report . Report ( createReport ( item , matchingFiles , processedFiles , foundFiles . Count ) ) ;
item , matchingFiles , processedFiles , foundFiles . Count ) ) ;
while ( state = = TaskState . Pause )
while ( state = = TaskState . Pause )
Thread . Sleep ( 1 0 0 ) ;
Thread . Sleep ( 1 0 0 ) ;
goto case TaskState . Work ;
goto case TaskState . Work ;
case TaskState . Work :
case TaskState . Work :
string [ ] fileLines = getFileContents ( item ) ;
string [ ] fileLines = getFileContents ( item ) ;
report . Report ( createReport ( null , item , matchingFiles , processedFiles , foundFiles . Count ) ) ;
report . Report ( createReport ( item , matchingFiles , processedFiles , foundFiles . Count ) ) ;
if ( ( fileLines . Length > 0 ) & & ( isFileContainQuery ( fileLines , query . fileInnerQuery ) ) )
if ( ( fileLines . Length > 0 ) & & ( isFileContainQuery ( fileLines , query . fileInnerQuery ) ) )
{
{
await fillChildNode ( itemsNode , item . Replace ( query . fileUrl , "" ) ) ;
await fillChildNode ( currentView . Nodes , item . Replace ( query . fileUrl , "" ) ) ;
matchingFiles + + ;
matchingFiles + + ;
}
}
processedFiles + + ;
processedFiles + + ;
break ;
break ;
}
}
}
}
report . Report ( createReport ( null , "none" , matchingFiles , processedFiles , foundFiles . Count ) ) ;
report . Report ( createReport ( "none" , matchingFiles , processedFiles , foundFiles . Count ) ) ;
controller . stopTask ( ) ;
controller . stopTask ( ) ;
return itemsNode ;
}
}
private ProgressReportModel createReport ( TreeNode treeNode , string fileUrl , int matchingFiles , int processedFiles , int totalNumOfFiles )
private ProgressReportModel createReport ( string fileUrl , int matchingFiles , int processedFiles , int totalNumOfFiles )
{
{
if ( state = = TaskState . Finished )
return null ;
ProgressReportModel prm = new ProgressReportModel ( ) ;
ProgressReportModel prm = new ProgressReportModel ( ) ;
prm . currentTreeNode = treeNode ;
prm . currentFileUrl = fileUrl ;
prm . currentFileUrl = fileUrl ;
prm . matchingFiles = matchingFiles ;
prm . matchingFiles = matchingFiles ;
prm . processedFiles = processedFiles ;
prm . processedFiles = processedFiles ;
@ -135,13 +137,17 @@ namespace fileFinder
return false ;
return false ;
}
}
public async Task fillChildNode ( TreeNode node , String item )
public async Task fillChildNode ( TreeNodeCollection node , String item )
{
{
int backSlashIndex = item . IndexOf ( "\\" ) ;
int backSlashIndex = item . IndexOf ( "\\" ) ;
switch ( backSlashIndex )
switch ( backSlashIndex )
{
{
case - 1 :
case - 1 :
node . Nodes . Add ( item , item , 2 , 2 ) ;
Action refresh = ( ) = >
{
node . Add ( item , item , 2 , 2 ) ;
} ; try { form . Invoke ( refresh ) ; } catch { }
break ;
break ;
case 0 :
case 0 :
item = item . Remove ( 0 , 1 ) ;
item = item . Remove ( 0 , 1 ) ;
@ -149,15 +155,18 @@ namespace fileFinder
break ;
break ;
default :
default :
String currentNodeName = item . Substring ( 0 , backSlashIndex ) ;
String currentNodeName = item . Substring ( 0 , backSlashIndex ) ;
int nodeIndex = node . Nodes . IndexOfKey ( currentNodeName ) ;
int nodeIndex = node . IndexOfKey ( currentNodeName ) ;
if ( nodeIndex ! = - 1 )
if ( nodeIndex ! = - 1 )
await fillChildNode ( node . Nodes [ nodeIndex ] , item . Remove ( 0 , backSlashIndex + 1 ) ) ;
await fillChildNode ( node [ nodeIndex ] . Nodes , item . Remove ( 0 , backSlashIndex + 1 ) ) ;
else
else
{
{
node . Nodes . Add ( currentNodeName , currentNodeName , 0 , 0 ) ;
Action refresh2 = ( ) = >
nodeIndex = node . Nodes . IndexOfKey ( currentNodeName ) ;
{
await fillChildNode ( node . Nodes [ nodeIndex ] , item . Remove ( 0 , backSlashIndex + 1 ) ) ;
node . Add ( currentNodeName , currentNodeName , 0 , 0 ) ;
} ; try { form . Invoke ( refresh2 ) ; } catch { }
nodeIndex = node . IndexOfKey ( currentNodeName ) ;
await fillChildNode ( node [ nodeIndex ] . Nodes , item . Remove ( 0 , backSlashIndex + 1 ) ) ;
}
}
break ;
break ;
}
}