Files
MPF/MPF.UI/Windows/OptionsWindow.xaml.cs

284 lines
11 KiB
C#
Raw Permalink Normal View History

2023-10-07 11:47:24 -04:00
using System;
2023-10-07 22:48:43 -04:00
using System.IO;
using System.Threading.Tasks;
2023-10-07 22:40:51 -04:00
using System.Windows;
2023-11-30 23:34:44 -05:00
using System.Windows.Controls;
2023-10-07 22:48:43 -04:00
using System.Windows.Forms;
2024-05-28 14:12:36 -04:00
using MPF.Frontend;
2024-05-23 15:40:12 -04:00
using MPF.Frontend.ViewModels;
2023-10-07 22:40:51 -04:00
using WPFCustomMessageBox;
2023-12-01 23:58:01 -05:00
#pragma warning disable IDE1006 // Naming Styles
2024-05-23 21:35:02 -04:00
namespace MPF.UI.Windows
{
/// <summary>
/// Interaction logic for OptionsWindow.xaml
/// </summary>
public partial class OptionsWindow : WindowBase
{
2020-10-11 22:06:40 -07:00
/// <summary>
/// Read-only access to the current options view model
2020-10-11 22:06:40 -07:00
/// </summary>
2023-10-11 11:49:56 -04:00
public OptionsViewModel OptionsViewModel => DataContext as OptionsViewModel ?? new OptionsViewModel(new Options());
2020-10-11 22:06:40 -07:00
2023-11-30 23:34:44 -05:00
#if NET35
private System.Windows.Controls.Button? _AaruPathButton => ItemHelper.FindChild<System.Windows.Controls.Button>(this, "AaruPathButton");
private System.Windows.Controls.Button? _AcceptButton => ItemHelper.FindChild<System.Windows.Controls.Button>(this, "AcceptButton");
private System.Windows.Controls.Button? _CancelButton => ItemHelper.FindChild<System.Windows.Controls.Button>(this, "CancelButton");
private System.Windows.Controls.Button? _DefaultOutputPathButton => ItemHelper.FindChild<System.Windows.Controls.Button>(this, "DefaultOutputPathButton");
private System.Windows.Controls.Button? _DiscImageCreatorPathButton => ItemHelper.FindChild<System.Windows.Controls.Button>(this, "DiscImageCreatorPathButton");
private System.Windows.Controls.Button? _RedumperPathButton => ItemHelper.FindChild<System.Windows.Controls.Button>(this, "RedumperPathButton");
private System.Windows.Controls.Button? _RedumpLoginTestButton => ItemHelper.FindChild<System.Windows.Controls.Button>(this, "RedumpLoginTestButton");
private PasswordBox? _RedumpPasswordBox => ItemHelper.FindChild<PasswordBox>(this, "RedumpPasswordBox");
private System.Windows.Controls.TextBox? _RedumpUsernameTextBox => ItemHelper.FindChild<System.Windows.Controls.TextBox>(this, "RedumpUsernameTextBox");
#endif
/// <summary>
/// Constructor
/// </summary>
2023-09-25 15:54:18 -04:00
public OptionsWindow(Options options)
{
2023-11-30 23:34:44 -05:00
#if NET40_OR_GREATER || NETCOREAPP
InitializeComponent();
2023-11-30 23:34:44 -05:00
#endif
2023-11-22 23:50:37 -05:00
#if NET40_OR_GREATER || NETCOREAPP
DumpSpeedCDTextBox.IsReadOnlyCaretVisible = false;
DumpSpeedDVDTextBox.IsReadOnlyCaretVisible = false;
DumpSpeedHDDVDTextBox.IsReadOnlyCaretVisible = false;
DumpSpeedBDTextBox.IsReadOnlyCaretVisible = false;
#endif
#if NET452_OR_GREATER || NETCOREAPP
2023-11-15 12:10:54 -05:00
var chrome = new System.Windows.Shell.WindowChrome
{
CaptionHeight = 0,
ResizeBorderThickness = new Thickness(0),
};
System.Windows.Shell.WindowChrome.SetWindowChrome(this, chrome);
#endif
2023-10-07 11:47:24 -04:00
DataContext = new OptionsViewModel(options);
2023-10-07 21:31:39 -04:00
// Set initial value for binding
2023-11-30 23:34:44 -05:00
#if NET35
_RedumpPasswordBox!.Password = options.RedumpPassword;
#else
2023-10-07 21:31:39 -04:00
RedumpPasswordBox.Password = options.RedumpPassword;
2023-11-30 23:34:44 -05:00
#endif
2023-10-07 21:31:39 -04:00
2023-10-07 11:47:24 -04:00
// Add handlers
2023-11-30 23:34:44 -05:00
#if NET35
_AaruPathButton!.Click += BrowseForPathClick;
_DiscImageCreatorPathButton!.Click += BrowseForPathClick;
_RedumperPathButton!.Click += BrowseForPathClick;
_DefaultOutputPathButton!.Click += BrowseForPathClick;
_AcceptButton!.Click += OnAcceptClick;
_CancelButton!.Click += OnCancelClick;
_RedumpPasswordBox!.PasswordChanged += OnPasswordChanged;
_RedumpLoginTestButton!.Click += OnRedumpTestClick;
#else
2023-10-07 11:47:24 -04:00
AaruPathButton.Click += BrowseForPathClick;
DiscImageCreatorPathButton.Click += BrowseForPathClick;
RedumperPathButton.Click += BrowseForPathClick;
2023-10-07 11:47:24 -04:00
DefaultOutputPathButton.Click += BrowseForPathClick;
AcceptButton.Click += OnAcceptClick;
CancelButton.Click += OnCancelClick;
2023-10-07 21:31:39 -04:00
RedumpPasswordBox.PasswordChanged += OnPasswordChanged;
2023-10-07 11:47:24 -04:00
RedumpLoginTestButton.Click += OnRedumpTestClick;
2023-11-30 23:34:44 -05:00
#endif
}
2023-10-07 11:47:24 -04:00
/// <summary>
/// Handler for OptionsWindow OnContentRendered event
/// </summary>
protected override void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);
// Set the window title
OptionsViewModel.Title = this.Title;
}
2023-10-07 22:48:43 -04:00
#region UI Commands
/// <summary>
/// Browse and set a path based on the invoking button
/// </summary>
2023-10-11 11:49:56 -04:00
private void BrowseForPath(Window parent, System.Windows.Controls.Button? button)
2023-10-07 22:48:43 -04:00
{
// If the button is null, we can't do anything
if (button == null)
return;
// Strips button prefix to obtain the setting name
string pathSettingName = button.Name.Substring(0, button.Name.IndexOf("Button"));
2023-10-07 22:48:43 -04:00
// TODO: hack for now, then we'll see
bool shouldBrowseForPath = pathSettingName == "DefaultOutputPath";
2023-10-11 11:49:56 -04:00
var currentPath = TextBoxForPathSetting(parent, pathSettingName)?.Text;
var initialDirectory = AppDomain.CurrentDomain.BaseDirectory;
2023-10-07 22:48:43 -04:00
if (!shouldBrowseForPath && !string.IsNullOrEmpty(currentPath))
initialDirectory = Path.GetDirectoryName(Path.GetFullPath(currentPath));
CommonDialog dialog = shouldBrowseForPath
? (CommonDialog)CreateFolderBrowserDialog()
: CreateOpenFileDialog(initialDirectory);
using (dialog)
{
DialogResult result = dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
string path = string.Empty;
bool exists = false;
if (shouldBrowseForPath && dialog is FolderBrowserDialog folderBrowserDialog)
{
path = folderBrowserDialog.SelectedPath;
exists = Directory.Exists(path);
}
else if (dialog is OpenFileDialog openFileDialog)
{
path = openFileDialog.FileName;
exists = File.Exists(path);
}
if (exists)
{
OptionsViewModel.Options[pathSettingName] = path;
2023-10-11 11:49:56 -04:00
var textBox = TextBoxForPathSetting(parent, pathSettingName);
if (textBox != null)
textBox.Text = path;
2023-10-07 22:48:43 -04:00
}
else
{
CustomMessageBox.Show(
"Specified path doesn't exist!",
"Error",
MessageBoxButton.OK,
MessageBoxImage.Error
);
}
}
}
}
/// <summary>
/// Find a TextBox by setting name
/// </summary>
/// <param name="name">Setting name to find</param>
/// <returns>TextBox for that setting</returns>
2023-10-11 12:26:24 -04:00
private static System.Windows.Controls.TextBox? TextBoxForPathSetting(Window parent, string name) =>
2023-10-07 22:48:43 -04:00
parent.FindName(name + "TextBox") as System.Windows.Controls.TextBox;
/// <summary>
/// Create an open folder dialog box
/// </summary>
2023-10-11 12:37:31 -04:00
private static FolderBrowserDialog CreateFolderBrowserDialog() => new();
2023-10-07 22:48:43 -04:00
/// <summary>
/// Create an open file dialog box
/// </summary>
2023-10-11 11:49:56 -04:00
private static OpenFileDialog CreateOpenFileDialog(string? initialDirectory)
2023-10-07 22:48:43 -04:00
{
return new OpenFileDialog()
{
InitialDirectory = initialDirectory,
Filter = "Executables (*.exe)|*.exe",
FilterIndex = 0,
RestoreDirectory = true,
};
}
/// <summary>
/// Test Redump credentials for validity
/// </summary>
private async Task ValidateRedumpCredentials()
{
2023-11-30 23:34:44 -05:00
#if NET35
(bool? success, string? message) = await OptionsViewModel.TestRedumpLogin(_RedumpUsernameTextBox!.Text, _RedumpPasswordBox!.Password);
#else
2023-11-20 13:15:06 -05:00
(bool? success, string? message) = await OptionsViewModel.TestRedumpLogin(RedumpUsernameTextBox.Text, RedumpPasswordBox.Password);
2023-11-30 23:34:44 -05:00
#endif
2023-10-07 22:48:43 -04:00
if (success == true)
CustomMessageBox.Show(this, message, "Success", MessageBoxButton.OK, MessageBoxImage.Information);
else if (success == false)
CustomMessageBox.Show(this, message, "Failure", MessageBoxButton.OK, MessageBoxImage.Error);
else
CustomMessageBox.Show(this, message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
#endregion
2023-10-07 22:48:43 -04:00
2023-10-07 11:47:24 -04:00
#region Event Handlers
/// <summary>
/// Handler for generic Click event
/// </summary>
private void BrowseForPathClick(object sender, EventArgs e) =>
2023-10-07 22:48:43 -04:00
BrowseForPath(this, sender as System.Windows.Controls.Button);
2023-10-07 11:47:24 -04:00
/// <summary>
/// Alert user of non-redump mode implications
/// </summary>
private void NonRedumpModeClicked(object sender, EventArgs e)
{
if (OptionsViewModel.Options.RedumperNonRedumpMode)
CustomMessageBox.Show(this, "All logs generated with these options will not be acceptable for Redump submission", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
else
OptionsViewModel.NonRedumpModeUnChecked();
}
2023-10-07 11:47:24 -04:00
/// <summary>
/// Handler for AcceptButton Click event
/// </summary>
private void OnAcceptClick(object sender, EventArgs e)
{
2023-10-07 22:40:51 -04:00
OptionsViewModel.SavedSettings = true;
2023-10-07 11:47:24 -04:00
Close();
}
/// <summary>
/// Handler for CancelButtom Click event
/// </summary>
private void OnCancelClick(object sender, EventArgs e)
2023-10-07 22:40:51 -04:00
{
OptionsViewModel.SavedSettings = false;
Close();
}
2023-10-07 11:47:24 -04:00
2023-10-07 21:31:39 -04:00
/// <summary>
2023-10-07 22:48:43 -04:00
/// Handler for RedumpPasswordBox PasswordChanged event
2023-10-07 21:31:39 -04:00
/// </summary>
private void OnPasswordChanged(object sender, EventArgs e)
{
2023-11-30 23:34:44 -05:00
#if NET35
OptionsViewModel.Options.RedumpPassword = _RedumpPasswordBox!.Password;
#else
2023-10-07 21:31:39 -04:00
OptionsViewModel.Options.RedumpPassword = RedumpPasswordBox.Password;
2023-11-30 23:34:44 -05:00
#endif
2023-10-07 21:31:39 -04:00
}
2023-10-07 11:47:24 -04:00
/// <summary>
/// Test Redump credentials for validity
/// </summary>
#if NET40
private void OnRedumpTestClick(object sender, EventArgs e)
{
var validateTask = ValidateRedumpCredentials();
validateTask.Wait();
}
#else
2023-10-07 22:48:43 -04:00
private async void OnRedumpTestClick(object sender, EventArgs e) => await ValidateRedumpCredentials();
#endif
2023-10-07 22:40:51 -04:00
2023-11-30 23:34:44 -05:00
#endregion
}
}