@@ -737,13 +737,53 @@ public RelayCommand ClearCompletedCommand
737737 {
738738 var removedItems = new List < EncodeResultViewModel > ( this . CompletedJobs ) ;
739739 this . CompletedJobs . Clear ( ) ;
740+ var deletionCandidates = new List < string > ( ) ;
740741
741742 foreach ( var removedItem in removedItems )
742743 {
743- if ( removedItem . Job != null )
744+ if ( removedItem . Job . HandBrakeInstance != null )
744745 {
745746 this . CleanupHandBrakeInstanceIfUnused ( removedItem . Job . HandBrakeInstance ) ;
746747 }
748+
749+ // Delete file if setting is enabled and item succeeded
750+ if ( Settings . Default . DeleteSourceFilesOnClearingCompleted && removedItem . EncodeResult . Succeeded )
751+ {
752+ // And if file exists and is not read-only
753+ string sourcePath = removedItem . Job . Job . SourcePath ;
754+ if ( File . Exists ( sourcePath ) && ! new FileInfo ( sourcePath ) . IsReadOnly )
755+ {
756+ // And if it's not currently scanned or in the encode queue
757+ bool sourceInEncodeQueue = this . EncodeQueue . Any ( job => string . Compare ( job . Job . SourcePath , sourcePath , StringComparison . OrdinalIgnoreCase ) == 0 ) ;
758+ if ( ! sourceInEncodeQueue &&
759+ string . Compare ( this . main . SourcePath , sourcePath , StringComparison . OrdinalIgnoreCase ) != 0 )
760+ {
761+ deletionCandidates . Add ( sourcePath ) ;
762+ }
763+ }
764+ }
765+ }
766+
767+ if ( deletionCandidates . Count > 0 )
768+ {
769+ MessageBoxResult dialogResult = Utilities . MessageBox . Show (
770+ "Are you sure you want to delete " + deletionCandidates . Count + " source file(s)?" ,
771+ "Confirm delete" ,
772+ MessageBoxButton . YesNo ) ;
773+ if ( dialogResult == MessageBoxResult . Yes )
774+ {
775+ foreach ( string fileToDelete in deletionCandidates )
776+ {
777+ try
778+ {
779+ File . Delete ( fileToDelete ) ;
780+ }
781+ catch ( IOException exception )
782+ {
783+ Utilities . MessageBox . Show ( "Could not delete " + fileToDelete + Environment . NewLine + Environment . NewLine + exception ) ;
784+ }
785+ }
786+ }
747787 }
748788
749789 this . RaisePropertyChanged ( ( ) => this . CompletedItemsCount ) ;
@@ -978,7 +1018,7 @@ public void CleanupHandBrakeInstanceIfUnused(HandBrakeInstance instance)
9781018
9791019 foreach ( EncodeResultViewModel resultVM in this . CompletedJobs )
9801020 {
981- if ( resultVM . Job != null && instance == resultVM . Job . HandBrakeInstance )
1021+ if ( instance == resultVM . Job . HandBrakeInstance )
9821022 {
9831023 return ;
9841024 }
@@ -1009,7 +1049,7 @@ public void CleanupHandBrakeInstances()
10091049
10101050 foreach ( EncodeResultViewModel resultVM in this . CompletedJobs )
10111051 {
1012- if ( resultVM . Job != null )
1052+ if ( resultVM . Job . HandBrakeInstance != null )
10131053 {
10141054 instances . Add ( resultVM . Job . HandBrakeInstance ) ;
10151055 }
@@ -1231,11 +1271,7 @@ private void OnEncodeCompleted(object sender, EncodeCompletedEventArgs e)
12311271 this . logger . LogError ( "Encode failed. HandBrake reported no error but the output file was empty." ) ;
12321272 }
12331273
1234- EncodeJobViewModel resultJob = null ;
1235- if ( Settings . Default . KeepScansAfterCompletion )
1236- {
1237- resultJob = this . CurrentJob ;
1238- }
1274+ EncodeJobViewModel finishedJob = this . CurrentJob ;
12391275
12401276 this . CompletedJobs . Add ( new EncodeResultViewModel (
12411277 new EncodeResult
@@ -1244,17 +1280,19 @@ private void OnEncodeCompleted(object sender, EncodeCompletedEventArgs e)
12441280 Succeeded = succeeded ,
12451281 EncodeTime = this . CurrentJob . EncodeTime
12461282 } ,
1247- resultJob ) ) ;
1283+ finishedJob ) ) ;
12481284 this . RaisePropertyChanged ( ( ) => this . CompletedItemsCount ) ;
12491285 this . RaisePropertyChanged ( ( ) => this . CompletedTabHeader ) ;
12501286
1251- HandBrakeInstance finishedInstance = this . EncodeQueue [ 0 ] . HandBrakeInstance ;
12521287 this . EncodeQueue . RemoveAt ( 0 ) ;
12531288 this . RaisePropertyChanged ( ( ) => this . QueuedTabHeader ) ;
12541289
1290+ // Wait until after it's removed from the queue before running cleanup: otherwise it will find
1291+ // the instance "in use" in the queue and not do removal.
12551292 if ( ! Settings . Default . KeepScansAfterCompletion )
12561293 {
1257- this . CleanupHandBrakeInstanceIfUnused ( finishedInstance ) ;
1294+ this . CleanupHandBrakeInstanceIfUnused ( finishedJob . HandBrakeInstance ) ;
1295+ finishedJob . HandBrakeInstance = null ;
12581296 }
12591297
12601298 this . logger . Log ( "Job completed" ) ;
@@ -1387,7 +1425,7 @@ private void RefreshEncodeCompleteActions()
13871425
13881426 foreach ( EncodeResultViewModel result in this . CompletedJobs )
13891427 {
1390- if ( result . Job != null && result . Job . Job . SourceType == SourceType . Dvd )
1428+ if ( result . Job . Job . SourceType == SourceType . Dvd )
13911429 {
13921430 string driveLetter = result . Job . Job . SourcePath . Substring ( 0 , 1 ) . ToUpperInvariant ( ) ;
13931431 if ( ! applicableDrives . Contains ( driveLetter ) )
0 commit comments