Skip to content

Commit b6f3be9

Browse files
author
RandomEngy
committed
Add an option to prompt to delete source files after clearing successful
completed items.
1 parent d0b7da1 commit b6f3be9

7 files changed

Lines changed: 97 additions & 18 deletions

File tree

VidCoder/Properties/Settings.Designer.cs

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

VidCoder/Properties/Settings.settings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,8 @@
161161
<Setting Name="MinimumTitleLengthSeconds" Type="System.Int32" Scope="User">
162162
<Value Profile="(Default)">10</Value>
163163
</Setting>
164+
<Setting Name="DeleteSourceFilesOnClearingCompleted" Type="System.Boolean" Scope="User">
165+
<Value Profile="(Default)">False</Value>
166+
</Setting>
164167
</Settings>
165168
</SettingsFile>

VidCoder/View/OptionsDialog.xaml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
<RowDefinition Height="22" />
139139
<RowDefinition Height="22" />
140140
<RowDefinition Height="22" />
141+
<RowDefinition Height="22" />
141142
<RowDefinition Height="Auto" />
142143
<RowDefinition Height="24" />
143144
<RowDefinition Height="Auto" />
@@ -168,25 +169,30 @@
168169
Content="Keep scans after completion (allows editing completed items)"
169170
IsChecked="{Binding KeepScansAfterCompletion}"
170171
Height="16" HorizontalAlignment="Left" Margin="15,0,0,0" VerticalAlignment="Center" />
171-
<Label
172+
<CheckBox
172173
Grid.Row="4"
174+
Content="Prompt to delete source files after clearing successful completed items"
175+
IsChecked="{Binding DeleteSourceFilesOnClearingCompleted}"
176+
Height="16" HorizontalAlignment="Left" Margin="15,0,0,0" VerticalAlignment="Center" />
177+
<Label
178+
Grid.Row="5"
173179
Content="Logging Verbosity:" Height="28" HorizontalAlignment="Left" Margin="6,0,0,0" VerticalAlignment="Center" />
174180
<ComboBox
175-
Grid.Row="4"
181+
Grid.Row="5"
176182
Height="23" HorizontalAlignment="Left" Margin="121,0,0,0" SelectedValue="{Binding LogVerbosity}" SelectedValuePath="Content" VerticalAlignment="Center" Width="42">
177183
<ComboBoxItem Content="0" />
178184
<ComboBoxItem Content="1" />
179185
<ComboBoxItem Content="2" />
180186
</ComboBox>
181187
<TextBlock
182-
Grid.Row="4"
188+
Grid.Row="5"
183189
HorizontalAlignment="Left" Margin="182,0,0,0" Text="Log verbosity will take effect on next scan or program restart." TextWrapping="Wrap" VerticalAlignment="Center" Visibility="{Binding LogVerbosityWarningVisible, Converter={StaticResource VisibilityConverter}}" />
184190

185191
<TextBlock
186-
Grid.Row="5"
192+
Grid.Row="6"
187193
Height="23" HorizontalAlignment="Left" Margin="11,5,0,0" Text="Minimum length of title to scan (seconds):" VerticalAlignment="Center" />
188194
<my:NumberBox
189-
Grid.Row="5" HorizontalAlignment="Left" Margin="243,2,0,0" VerticalAlignment="Top" AllowEmpty="False" Width="45"
195+
Grid.Row="6" HorizontalAlignment="Left" Margin="243,2,0,0" VerticalAlignment="Top" AllowEmpty="False" Width="45"
190196
Number="{Binding MinimumTitleLengthSeconds, Mode=TwoWay}" Minimum="0" />
191197
</Grid>
192198
</TabItem>

VidCoder/ViewModel/Components/ProcessingViewModel.cs

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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))

VidCoder/ViewModel/DataModels/EncodeResultViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public bool EditVisible
108108
{
109109
get
110110
{
111-
return this.Job != null;
111+
return this.Job.HandBrakeInstance != null;
112112
}
113113
}
114114

VidCoder/ViewModel/OptionsDialogViewModel.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public OptionsDialogViewModel(IUpdater updateService)
6363
this.showAudioTrackNameField = Settings.Default.ShowAudioTrackNameField;
6464
this.keepScansAfterCompletion = Settings.Default.KeepScansAfterCompletion;
6565
this.enableLibDvdNav = Settings.Default.EnableLibDvdNav;
66+
this.deleteSourceFilesOnClearingCompleted = Settings.Default.DeleteSourceFilesOnClearingCompleted;
6667
this.minimumTitleLengthSeconds = Settings.Default.MinimumTitleLengthSeconds;
6768
this.autoPauseProcesses = new ObservableCollection<string>();
6869
StringCollection autoPauseStringCollection = Settings.Default.AutoPauseProcesses;
@@ -412,6 +413,21 @@ public bool KeepScansAfterCompletion
412413
}
413414
}
414415

416+
private bool deleteSourceFilesOnClearingCompleted;
417+
public bool DeleteSourceFilesOnClearingCompleted
418+
{
419+
get
420+
{
421+
return this.deleteSourceFilesOnClearingCompleted;
422+
}
423+
424+
set
425+
{
426+
this.deleteSourceFilesOnClearingCompleted = value;
427+
this.RaisePropertyChanged(() => this.DeleteSourceFilesOnClearingCompleted);
428+
}
429+
}
430+
415431
public bool LogVerbosityWarningVisible
416432
{
417433
get
@@ -470,6 +486,7 @@ public RelayCommand SaveSettingsCommand
470486
Settings.Default.ShowAudioTrackNameField = this.ShowAudioTrackNameField;
471487
Settings.Default.EnableLibDvdNav = this.EnableLibDvdNav;
472488
Settings.Default.KeepScansAfterCompletion = this.KeepScansAfterCompletion;
489+
Settings.Default.DeleteSourceFilesOnClearingCompleted = this.DeleteSourceFilesOnClearingCompleted;
473490
Settings.Default.MinimumTitleLengthSeconds = this.MinimumTitleLengthSeconds;
474491
Settings.Default.Save();
475492

VidCoder/app.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@
168168
<setting name="MinimumTitleLengthSeconds" serializeAs="String">
169169
<value>10</value>
170170
</setting>
171+
<setting name="DeleteSourceFilesOnClearingCompleted" serializeAs="String">
172+
<value>False</value>
173+
</setting>
171174
</VidCoder.Properties.Settings>
172175
</userSettings>
173176
</configuration>

0 commit comments

Comments
 (0)