using System.Windows.Input;
using JdeScoping.SecureStoreManager.Constants;
namespace JdeScoping.SecureStoreManager.ViewModels;
///
/// View model for creating a new store.
///
public class NewStoreDialogViewModel : ViewModelBase
{
private string _storePath = string.Empty;
private string _keyFilePath = string.Empty;
///
/// Initializes a new instance of the class.
///
public NewStoreDialogViewModel()
{
BrowseStorePathCommand = new RelayCommand(BrowseStorePath);
BrowseKeyFilePathCommand = new RelayCommand(BrowseKeyFilePath);
}
///
/// Gets or sets the path to the store file to create.
///
public string StorePath
{
get => _storePath;
set
{
if (SetProperty(ref _storePath, value))
NotifyValidationChanged();
}
}
///
/// Gets or sets the path to the key file for encryption.
///
public string KeyFilePath
{
get => _keyFilePath;
set
{
if (SetProperty(ref _keyFilePath, value))
NotifyValidationChanged();
}
}
private void NotifyValidationChanged()
{
OnPropertyChanged(nameof(IsValid));
OnPropertyChanged(nameof(ValidationError));
}
///
/// Gets the command to browse for store path location.
///
public ICommand BrowseStorePathCommand { get; }
///
/// 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 => !string.IsNullOrWhiteSpace(StorePath) && !string.IsNullOrWhiteSpace(KeyFilePath);
///
/// Gets the validation error message, or null if valid.
///
public string? ValidationError
{
get
{
if (string.IsNullOrWhiteSpace(StorePath))
return DialogStrings.StorePathRequired;
if (string.IsNullOrWhiteSpace(KeyFilePath))
return DialogStrings.KeyFilePathRequired;
return null;
}
}
///
/// Event raised to request save file dialog for store path.
/// Parameters: title, fileTypeName, pattern, defaultExtension
/// Returns: selected file path or null
///
public event Func>? OnShowSaveFileDialog;
private async void BrowseStorePath()
{
if (OnShowSaveFileDialog == null)
return;
var path = await OnShowSaveFileDialog(
DialogStrings.ChooseStoreLocation,
FileExtensions.StoreTypeName,
FileExtensions.StorePattern,
FileExtensions.StoreExtension);
if (!string.IsNullOrEmpty(path))
{
StorePath = path;
}
}
private async void BrowseKeyFilePath()
{
if (OnShowSaveFileDialog == null)
return;
var path = await OnShowSaveFileDialog(
DialogStrings.ChooseKeyFileLocation,
FileExtensions.KeyTypeName,
FileExtensions.KeyPattern,
FileExtensions.KeyExtension);
if (!string.IsNullOrEmpty(path))
{
KeyFilePath = path;
}
}
}
///
/// View model for opening an existing store.
///
public class OpenStoreDialogViewModel : ViewModelBase
{
private string _storePath = string.Empty;
private string _keyFilePath = string.Empty;
///
/// Initializes a new instance of the class.
///
public OpenStoreDialogViewModel()
{
BrowseStorePathCommand = new RelayCommand(BrowseStorePath);
BrowseKeyFilePathCommand = new RelayCommand(BrowseKeyFilePath);
}
///
/// Gets or sets the path to the store file to open.
///
public string StorePath
{
get => _storePath;
set
{
if (SetProperty(ref _storePath, value))
NotifyValidationChanged();
}
}
///
/// Gets or sets the path to the key file for decryption.
///
public string KeyFilePath
{
get => _keyFilePath;
set
{
if (SetProperty(ref _keyFilePath, value))
NotifyValidationChanged();
}
}
private void NotifyValidationChanged()
{
OnPropertyChanged(nameof(IsValid));
OnPropertyChanged(nameof(ValidationError));
}
///
/// Gets the command to browse for store path location.
///
public ICommand BrowseStorePathCommand { get; }
///
/// 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(StorePath))
return false;
return !string.IsNullOrWhiteSpace(KeyFilePath);
}
}
///
/// Gets the validation error message, or null if valid.
///
public string? ValidationError
{
get
{
if (string.IsNullOrWhiteSpace(StorePath))
return DialogStrings.StorePathRequired;
if (!System.IO.File.Exists(StorePath))
return DialogStrings.StoreFileNotFound;
if (string.IsNullOrWhiteSpace(KeyFilePath))
return DialogStrings.KeyFilePathRequired;
if (!System.IO.File.Exists(KeyFilePath))
return DialogStrings.KeyFileNotFound;
return null;
}
}
///
/// Event raised to request open file dialog for store path.
/// Parameters: title, fileTypeName, pattern
/// Returns: selected file path or null
///
public event Func>? OnShowOpenFileDialog;
private async void BrowseStorePath()
{
if (OnShowOpenFileDialog == null)
return;
var path = await OnShowOpenFileDialog(
DialogStrings.SelectStoreFile,
FileExtensions.StoreTypeName,
FileExtensions.StorePattern);
if (!string.IsNullOrEmpty(path))
{
StorePath = path;
}
}
private async void BrowseKeyFilePath()
{
if (OnShowOpenFileDialog == null)
return;
var path = await OnShowOpenFileDialog(
DialogStrings.SelectKeyFile,
FileExtensions.KeyTypeName,
FileExtensions.KeyPattern);
if (!string.IsNullOrEmpty(path))
{
KeyFilePath = path;
}
}
}
///
/// View model for adding or editing a secret.
///
public class SecretEditDialogViewModel : ViewModelBase
{
private string _key = string.Empty;
private string _value = string.Empty;
private bool _isNewSecret = true;
///
/// Initializes a new instance of the class.
///
public SecretEditDialogViewModel()
{
}
///
/// Initializes a new instance of the class with a key and value for editing.
///
/// The secret key.
/// The secret value.
public SecretEditDialogViewModel(string key, string value)
{
_key = key;
_value = value;
_isNewSecret = false;
}
///
/// Gets or sets the secret key.
///
public string Key
{
get => _key;
set
{
if (SetProperty(ref _key, value))
NotifyValidationChanged();
}
}
///
/// Gets or sets the secret value.
///
public string Value
{
get => _value;
set => SetProperty(ref _value, value);
}
///
/// Gets or sets a value indicating whether this is a new secret being added.
///
public bool IsNewSecret
{
get => _isNewSecret;
set => SetProperty(ref _isNewSecret, value);
}
///
/// Gets a value indicating whether the key field is editable.
///
public bool IsKeyEditable => _isNewSecret;
///
/// Gets the dialog title based on whether this is a new secret or edit.
///
public string DialogTitle => _isNewSecret ? "Add Secret" : "Edit Secret";
///
/// Gets a value indicating whether the dialog input is valid.
///
public bool IsValid => !string.IsNullOrWhiteSpace(Key);
///
/// Gets the validation error message, or null if valid.
///
public string? ValidationError
{
get
{
if (string.IsNullOrWhiteSpace(Key))
return DialogStrings.KeyRequired;
return null;
}
}
private void NotifyValidationChanged()
{
OnPropertyChanged(nameof(IsValid));
OnPropertyChanged(nameof(ValidationError));
}
}