From 4176f22d79bb1ee09f4aec1b5a875b450e76c033 Mon Sep 17 00:00:00 2001 From: Jacopo Santoni Date: Thu, 28 Jun 2018 05:06:19 +0200 Subject: [PATCH] Added preferred dump speed option (#77) * Split type combobox into system combobox and disc type combobox * corrected indentation for xaml file * fixed merge with head * fixed format * fixed issues for PR, added KnownSystem.CUSTOM * removed Updater.cs which ended by error in commit * fixed GetOuptutName() for new drive/system combobox * added max dump speed option - added slider element into OptionsWindow - moved drive speeds to UIElements from MainWindow - added setting to Options which is saved still need to understand how to manage DVD/CD-ROM different speeds * kept working on max dump speed settings - rewrote max speed list management in Options - added second slider in OptionsWindow - added callback from OptionsWindow to MainWindow on updated options * final tweaks before PR * renamed maxDumpSpeed to preferredDumpSpeed * restored SetCurrentDiscType functionality * fixes for PR * fixes for PR --- App.config | 2 ++ Data/Constants.cs | 41 +++++++++++++++++++++-- MainWindow.xaml.cs | 28 ++++++++++------ Options.cs | 24 ++++++++++++++ OptionsWindow.xaml | 77 +++++++++++++++++++++++++++++-------------- OptionsWindow.xaml.cs | 37 ++++++++++++++------- 6 files changed, 161 insertions(+), 48 deletions(-) diff --git a/App.config b/App.config index 5c968b20..df13d9d4 100644 --- a/App.config +++ b/App.config @@ -4,5 +4,7 @@ + + diff --git a/Data/Constants.cs b/Data/Constants.cs index a4e375fc..cc884395 100644 --- a/Data/Constants.cs +++ b/Data/Constants.cs @@ -1,13 +1,50 @@ -namespace DICUI.Data +using System; +using System.Collections.Generic; +using System.Linq; +using System.Windows.Media; + +namespace DICUI.Data { /// - /// Text for UI elements + /// Variables for UI elements /// public static class UIElements { public const string StartDumping = "Start Dumping"; public const string StopDumping = "Stop Dumping"; public const string FloppyDriveString = "<>"; + + private static IReadOnlyList AllowedDriveSpeedsForCD { get; } = new List { 1, 2, 3, 4, 6, 8, 12, 16, 20, 24, 32, 40, 44, 48, 52, 56, 72 }; + private static IReadOnlyList AllowedDriveSpeedsForDVD { get; } = AllowedDriveSpeedsForCD.Where(s => s <= 24).ToList(); + private static IReadOnlyList AllowedDriveSpeedsForBD { get; } = AllowedDriveSpeedsForCD.Where(s => s <= 16).ToList(); + private static IReadOnlyList AllowedDriveSpeedsForUnknownType { get; } = AllowedDriveSpeedsForCD; /*TODO: all or maybe just 1? eg new List { 1 };*/ + + public static IReadOnlyList GetAllowedDriveSpeedsForMediaType(MediaType? type) + { + switch (type) + { + case MediaType.CD: + case MediaType.GDROM: + return AllowedDriveSpeedsForCD; + case MediaType.DVD: + case MediaType.HDDVD: + case MediaType.GameCubeGameDisc: + case MediaType.WiiOpticalDisc: + case MediaType.WiiUOpticalDisc: + return AllowedDriveSpeedsForDVD; + //TODO: we should return them all since DIC doens't support them in any case + case MediaType.BluRay: + return AllowedDriveSpeedsForBD; + default: + return AllowedDriveSpeedsForUnknownType; + } + } + + public static DoubleCollection GetDoubleCollectionFromIntList(IReadOnlyList list) + => new DoubleCollection(list.Select(i => Convert.ToDouble(i)).ToList()); + + public static DoubleCollection AllowedDriveSpeedsForCDAsCollection { get; } = GetDoubleCollectionFromIntList(AllowedDriveSpeedsForCD); + public static DoubleCollection AllowedDriveSpeedsForDVDAsCollection { get; } = GetDoubleCollectionFromIntList(AllowedDriveSpeedsForDVD); } /// diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 8dbb0a8a..d0381047 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -17,7 +17,6 @@ namespace DICUI // Private UI-related variables private List> _drives { get; set; } private MediaType? _currentMediaType { get; set; } - private List _driveSpeeds { get { return new List { 1, 2, 3, 4, 6, 8, 12, 16, 20, 24, 32, 40, 44, 48, 52, 56, 72 }; } } private List> _systems { get; set; } private List _mediaTypes { get; set; } @@ -113,7 +112,7 @@ namespace DICUI // lazy initialization if (_optionsWindow == null) { - _optionsWindow = new OptionsWindow(_options); + _optionsWindow = new OptionsWindow(this, _options); _optionsWindow.Closed += delegate { _optionsWindow = null; @@ -136,6 +135,12 @@ namespace DICUI EnsureDiscInformation(); } + public void OnOptionsUpdated() + { + GetOutputNames(); + //TODO: here we should adjust maximum speed if it changed in options + } + #endregion #region Helpers @@ -210,8 +215,9 @@ namespace DICUI /// private void PopulateDriveSpeeds() { - cmb_DriveSpeed.ItemsSource = _driveSpeeds; - cmb_DriveSpeed.SelectedItem = 8; + var values = UIElements.GetAllowedDriveSpeedsForMediaType(_currentMediaType); + cmb_DriveSpeed.ItemsSource = values; + cmb_DriveSpeed.SelectedIndex = values.Count / 2; } /// @@ -483,12 +489,14 @@ namespace DICUI return; } - // If the value is in the list, we can set it immediately - if (_driveSpeeds.Contains(speed)) - cmb_DriveSpeed.SelectedValue = speed; - // Otherwise, we need to set the next lowest value - else - cmb_DriveSpeed.SelectedValue = _driveSpeeds.Where(s => s < speed).Last(); + // choose speed value according to maximum value reported by DIC adjusted to a precise speed from list + // and the one choosen in options + int chosenSpeed = Math.Min( + UIElements.GetAllowedDriveSpeedsForMediaType(_currentMediaType).Where(s => s <= speed).Last(), + _options.preferredDumpSpeedCD + ); + + cmb_DriveSpeed.SelectedValue = chosenSpeed; } /// diff --git a/Options.cs b/Options.cs index b5ff29c9..88442f8e 100644 --- a/Options.cs +++ b/Options.cs @@ -1,6 +1,7 @@ using System; using System.Configuration; using System.Reflection; +using DICUI.Data; namespace DICUI { @@ -10,6 +11,9 @@ namespace DICUI public string dicPath { get; private set; } public string subdumpPath { get; private set; } + public int preferredDumpSpeedCD { get; set; } + public int preferredDumpSpeedDVD { get; set; } + public void Save() { Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); @@ -23,6 +27,13 @@ namespace DICUI } ); + //TODO: is remove needed, doesn't the value get directly overridden? + configFile.AppSettings.Settings.Remove("preferredDumpSpeedCD"); + configFile.AppSettings.Settings.Add("preferredDumpSpeedCD", Convert.ToString(preferredDumpSpeedCD)); + + configFile.AppSettings.Settings.Remove("preferredDumpSpeedDVD"); + configFile.AppSettings.Settings.Add("preferredDumpSpeedDVD", Convert.ToString(preferredDumpSpeedDVD)); + configFile.Save(ConfigurationSaveMode.Modified); } @@ -32,6 +43,9 @@ namespace DICUI dicPath = ConfigurationManager.AppSettings["dicPath"] ?? @"Programs\DiscImageCreator.exe"; subdumpPath = ConfigurationManager.AppSettings["subdumpPath"] ?? "subdump.exe"; defaultOutputPath = ConfigurationManager.AppSettings["defaultOutputPath"] ?? "ISO"; + + this.preferredDumpSpeedCD = Int32.TryParse(ConfigurationManager.AppSettings["preferredDumpSpeedCD"], out int maxDumpSpeedCD) ? maxDumpSpeedCD : 72; + this.preferredDumpSpeedDVD = Int32.TryParse(ConfigurationManager.AppSettings["preferredDumpSpeedDVD"], out int maxDumpSpeedDVD) ? maxDumpSpeedDVD : 72; } @@ -46,5 +60,15 @@ namespace DICUI { return GetType().GetProperty(key, BindingFlags.Public | BindingFlags.Instance).GetValue(this) as string; } + + public int GetPreferredDumpSpeedForMediaType(MediaType? type) + { + switch (type) + { + case MediaType.CD: return preferredDumpSpeedCD; + case MediaType.DVD: return preferredDumpSpeedDVD; + default: return 8; + } + } } } diff --git a/OptionsWindow.xaml b/OptionsWindow.xaml index d4aeef5a..608dfec2 100644 --- a/OptionsWindow.xaml +++ b/OptionsWindow.xaml @@ -4,55 +4,82 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:DICUI" + xmlns:ldata="clr-namespace:DICUI.Data" mc:Ignorable="d" - Title="Options" Height="260" Width="515.132"> + Title="Options" Height="300" Width="515.132"> + - + - + + + + + + + + + + + + + + public partial class OptionsWindow : Window { + private readonly MainWindow _mainWindow; private readonly Options _options; - public OptionsWindow(Options options) + public OptionsWindow(MainWindow mainWindow, Options options) { InitializeComponent(); - _options = options; + _mainWindow = mainWindow; + _options = options; } private OpenFileDialog CreateOpenFileDialog() @@ -94,22 +96,35 @@ namespace DICUI } } - private void btn_Accept_Click(object sender, EventArgs e) + public void Refresh() { - Array.ForEach(PathSettings(), setting => _options.Set(setting, TextBoxForPathSetting(setting).Text)); - _options.Save(); - Hide(); + Array.ForEach(PathSettings(), setting => TextBoxForPathSetting(setting).Text = _options.Get(setting)); + + slider_DumpSpeedCD.Value = _options.preferredDumpSpeedCD; + slider_DumpSpeedDVD.Value = _options.preferredDumpSpeedDVD; } - private void btn_Cancel_Click(object sender, EventArgs e) + #region Event Handlers + + private void OnAcceptClick(object sender, EventArgs e) + { + Array.ForEach(PathSettings(), setting => _options.Set(setting, TextBoxForPathSetting(setting).Text)); + + _options.preferredDumpSpeedCD = Convert.ToInt32(slider_DumpSpeedCD.Value); + _options.preferredDumpSpeedDVD = Convert.ToInt32(slider_DumpSpeedDVD.Value); + + _options.Save(); + Hide(); + + _mainWindow.OnOptionsUpdated(); + } + + private void OnCancelClick(object sender, EventArgs e) { // just hide the window and don't care Hide(); } - public void Refresh() - { - Array.ForEach(PathSettings(), setting => TextBoxForPathSetting(setting).Text = _options.Get(setting)); - } + #endregion } }