using System.Windows.Input;
using JdeScoping.ConfigManager.Core.Constants;
namespace JdeScoping.ConfigManager.Ui.ViewModels.Dialogs;
///
/// View model for unlocking an existing secure store.
///
public class UnlockStoreDialogViewModel : ViewModelBase
{
private readonly string _storePath;
private string _keyFilePath = string.Empty;
///
/// Initializes a new instance of the class.
///
/// The path to the store file to unlock.
public UnlockStoreDialogViewModel(string storePath)
{
_storePath = storePath ?? throw new ArgumentNullException(nameof(storePath));
BrowseKeyFilePathCommand = new RelayCommand(BrowseKeyFilePath);
}
///
/// Gets the path to the store file to unlock (read-only).
///
public string StorePath => _storePath;
///
/// Gets or sets the path to the key file for decryption.
///
public string KeyFilePath
{
get => _keyFilePath;
set
{
if (SetProperty(ref _keyFilePath, value))
NotifyValidationChanged();
}
}
///
/// Gets the command to browse for key file path location.
///
public ICommand BrowseKeyFilePathCommand { get; }
///
/// Gets a value indicating whether the dialog input is valid.
///
public bool IsValid
{
get
{
if (string.IsNullOrWhiteSpace(KeyFilePath))
return false;
return System.IO.File.Exists(KeyFilePath);
}
}
///
/// Gets the validation error message, or null if valid.
///
public string? ValidationError
{
get
{
if (string.IsNullOrWhiteSpace(KeyFilePath))
return SecureStoreStrings.KeyFilePathRequired;
if (!System.IO.File.Exists(KeyFilePath))
return SecureStoreStrings.KeyFileNotFound;
return null;
}
}
///
/// Event raised to request open file dialog for key file path.
/// Parameters: title, fileTypeName, pattern
/// Returns: selected file path or null
///
public event Func>? OnShowOpenFileDialog;
private void NotifyValidationChanged()
{
OnPropertyChanged(nameof(IsValid));
OnPropertyChanged(nameof(ValidationError));
}
private async void BrowseKeyFilePath()
{
if (OnShowOpenFileDialog == null)
return;
var path = await OnShowOpenFileDialog(
SecureStoreStrings.SelectKeyFile,
SecureStoreFileExtensions.KeyTypeName,
SecureStoreFileExtensions.KeyPattern);
if (!string.IsNullOrEmpty(path))
{
KeyFilePath = path;
}
}
}