diff options
author | sr55 <[email protected]> | 2016-05-13 19:11:16 +0100 |
---|---|---|
committer | sr55 <[email protected]> | 2016-05-13 19:11:16 +0100 |
commit | 9ff44bb0ac9c5cd7810743b3eddd904452adf86c (patch) | |
tree | 4feec61cc5704e4471a280eadde03d2356a6139a /win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs | |
parent | 16314ab0db954c46f64632e4111836061bd9748d (diff) |
WinGui: Don't allow the user to enter invalid filename characters in the "File Format" autoname text box. Also sanitise the input on startup for legacy users which bad options. Fixes #182
Diffstat (limited to 'win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs')
-rw-r--r-- | win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs | 43 |
1 files changed, 40 insertions, 3 deletions
diff --git a/win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs index 0b1b16df3..b1962e48b 100644 --- a/win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs @@ -15,6 +15,7 @@ namespace HandBrakeWPF.ViewModels using System.Globalization;
using System.IO;
using System.Linq;
+ using System.Text;
using System.Windows;
using Caliburn.Micro;
@@ -42,6 +43,8 @@ namespace HandBrakeWPF.ViewModels private readonly IUserSettingService userSettingService;
private readonly IUpdateService updateService;
+ private readonly IErrorService errorService;
+
private string arguments;
private string autoNameDefaultPath;
private bool automaticallyNameFiles;
@@ -106,11 +109,12 @@ namespace HandBrakeWPF.ViewModels /// <param name="aboutViewModel">
/// The about View Model.
/// </param>
- public OptionsViewModel(IUserSettingService userSettingService, IUpdateService updateService, IAboutViewModel aboutViewModel)
+ public OptionsViewModel(IUserSettingService userSettingService, IUpdateService updateService, IAboutViewModel aboutViewModel, IErrorService errorService)
{
this.Title = "Options";
this.userSettingService = userSettingService;
this.updateService = updateService;
+ this.errorService = errorService;
this.AboutViewModel = aboutViewModel;
this.OnLoad();
@@ -390,7 +394,11 @@ namespace HandBrakeWPF.ViewModels set
{
- this.autonameFormat = value;
+ if (this.IsValidAutonameFormat(value, false))
+ {
+ this.autonameFormat = value;
+ }
+
this.NotifyOfPropertyChange("AutonameFormat");
}
}
@@ -1176,7 +1184,8 @@ namespace HandBrakeWPF.ViewModels this.AutoNameDefaultPath = "Click 'Browse' to set the default location";
// Store auto name format
- this.AutonameFormat = this.userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNameFormat) ?? string.Empty;
+ string anf = this.userSettingService.GetUserSetting<string>(UserSettingConstants.AutoNameFormat) ?? string.Empty;
+ this.AutonameFormat = this.IsValidAutonameFormat(anf, true) ? anf : "{source}-{title}";
// Use iPod/iTunes friendly .m4v extension for MP4 files.
this.mp4ExtensionOptions.Clear();
@@ -1409,5 +1418,33 @@ namespace HandBrakeWPF.ViewModels {
this.SelectedTab = tab;
}
+
+ /// <summary>
+ /// Validate the Autoname Fileformat string
+ /// </summary>
+ /// <param name="input">The format string</param>
+ /// <param name="isSilent">Don't show an error dialog if true.</param>
+ /// <returns>True if valid</returns>
+ private bool IsValidAutonameFormat(string input, bool isSilent)
+ {
+ foreach (var characterToTest in input)
+ {
+ // we binary search for the character in the invalid set. This should be lightning fast.
+ if (Array.BinarySearch(Path.GetInvalidFileNameChars(), characterToTest) >= 0)
+ {
+ if (!isSilent)
+ {
+ this.errorService.ShowMessageBox(
+ ResourcesUI.OptionsView_InvalidFileFormatChars,
+ Resources.Error,
+ MessageBoxButton.OK,
+ MessageBoxImage.Error);
+ }
+ return false;
+ }
+ }
+
+ return true;
+ }
}
}
\ No newline at end of file |