Skip to content

Commit

Permalink
Add KeyEntryClass to model constructors directly and add definition t…
Browse files Browse the repository at this point in the history
…o Wizard Factory
  • Loading branch information
Maxhy committed Oct 23, 2023
1 parent 519ddcf commit 9a063e7
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public override IList<Leosac.KeyManager.Library.KeyStore.KeyEntry> GetKeyEntries
return entries;
}

public override KeyEntryClass[] KeyEntryClasses => new[] { KeyEntryClass.Symmetric };

private static SAMSymmetricKeyEntry CreateKeyEntry(string id, string label, bool generateKeys = true)
{
var ke = new SAMSymmetricKeyEntry();
Expand Down
7 changes: 5 additions & 2 deletions KeyManager.Library.Plugin.UI/WizardFactory.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System.Windows;
using Leosac.KeyManager.Library.KeyStore;
using System.Windows;

namespace Leosac.KeyManager.Library.Plugin.UI
{
public abstract class WizardFactory : KMFactory<WizardFactory>
{
public abstract Window CreateWizardWindow();

public abstract IList<Leosac.KeyManager.Library.KeyStore.KeyEntry> GetKeyEntries(Window window);
public abstract IList<KeyEntry> GetKeyEntries(Window window);

public abstract KeyEntryClass[] KeyEntryClasses { get; }
}
}
47 changes: 17 additions & 30 deletions KeyManager.Library.UI/Domain/KeyEntriesControlViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@ namespace Leosac.KeyManager.Library.UI.Domain
public class KeyEntriesControlViewModel : ObservableValidator
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod()?.DeclaringType);
public KeyEntriesControlViewModel(ISnackbarMessageQueue snackbarMessageQueue)
public KeyEntriesControlViewModel(ISnackbarMessageQueue snackbarMessageQueue, KeyEntryClass keClass)
{
_snackbarMessageQueue = snackbarMessageQueue;
KeyEntryClass = keClass;
_identifierLock = new object();
Identifiers = new ObservableCollection<SelectableKeyEntryId>();
BindingOperations.EnableCollectionSynchronization(Identifiers, _identifierLock);
WizardFactories = new ObservableCollection<WizardFactory>(WizardFactory.RegisteredFactories);
WizardFactories = new ObservableCollection<WizardFactory>(WizardFactory.RegisteredFactories.Where(f => f.KeyEntryClasses.Contains(KeyEntryClass)));

CreateKeyEntryCommand = new AsyncRelayCommand(
async () =>
{
var model = new KeyEntryDialogViewModel
{
KClass = _keClass
};
model.SetKeyEntry(KeyStore?.GetDefaultKeyEntry(_keClass));
var model = new KeyEntryDialogViewModel(KeyEntryClass);
model.SetKeyEntry(KeyStore?.GetDefaultKeyEntry(KeyEntryClass));
var dialog = new KeyEntryDialog
{
DataContext = model
Expand All @@ -40,12 +38,11 @@ public KeyEntriesControlViewModel(ISnackbarMessageQueue snackbarMessageQueue)
GenerateKeyEntryCommand = new AsyncRelayCommand(
async () =>
{
var model = new KeyEntryDialogViewModel
var model = new KeyEntryDialogViewModel(KeyEntryClass)
{
KClass = _keClass,
ShowKeyMaterials = false
};
model.SetKeyEntry(KeyStore?.GetDefaultKeyEntry(_keClass));
model.SetKeyEntry(KeyStore?.GetDefaultKeyEntry(KeyEntryClass));
var dialog = new KeyEntryDialog
{
DataContext = model
Expand All @@ -56,11 +53,8 @@ public KeyEntriesControlViewModel(ISnackbarMessageQueue snackbarMessageQueue)
EditDefaultKeyEntryCommand = new AsyncRelayCommand(
async () =>
{
var model = new KeyEntryDialogViewModel
{
KClass = _keClass
};
model.SetKeyEntry(KeyStore?.GetDefaultKeyEntry(_keClass, false));
var model = new KeyEntryDialogViewModel(KeyEntryClass);
model.SetKeyEntry(KeyStore?.GetDefaultKeyEntry(KeyEntryClass, false));
var dialog = new KeyEntryDialog
{
DataContext = model
Expand All @@ -75,14 +69,13 @@ public KeyEntriesControlViewModel(ISnackbarMessageQueue snackbarMessageQueue)
{
if (KeyStore != null && identifier?.KeyEntryId != null)
{
var model = new KeyEntryDialogViewModel
var model = new KeyEntryDialogViewModel(KeyEntryClass)
{
KClass = _keClass,
CanChangeFactory = false,
AllowSubmit = KeyStore.CanUpdateKeyEntries,
SubmitButtonText = Properties.Resources.Update
};
model.SetKeyEntry(await KeyStore.Get(identifier.KeyEntryId, _keClass));
model.SetKeyEntry(await KeyStore.Get(identifier.KeyEntryId, KeyEntryClass));
var dialog = new KeyEntryDialog
{
DataContext = model
Expand Down Expand Up @@ -173,15 +166,13 @@ public KeyEntriesControlViewModel(ISnackbarMessageQueue snackbarMessageQueue)
}
});

_keClass = KeyEntryClass.Symmetric;
_identifiersView = CollectionViewSource.GetDefaultView(Identifiers);
_identifiersView.Filter = KeyEntryIdentifiersFilter;
}

protected ISnackbarMessageQueue _snackbarMessageQueue;
private readonly object _identifierLock;
private KeyStore.KeyStore? _keyStore;
private KeyEntryClass _keClass;
private bool _showSelection;
private readonly ICollectionView _identifiersView;
private string? _searchTerms;
Expand All @@ -196,11 +187,7 @@ public KeyEntriesControlViewModel(ISnackbarMessageQueue snackbarMessageQueue)
set => SetProperty(ref _keyStore, value);
}

public KeyEntryClass KeyEntryClass
{
get => _keClass;
set => SetProperty(ref _keClass, value);
}
public KeyEntryClass KeyEntryClass { get; }

public string? SearchTerms
{
Expand Down Expand Up @@ -294,7 +281,7 @@ private async Task EditDefaultKeyEntry(KeyEntryDialog dialog)
{
if (KeyStore != null && model.KeyEntry != null)
{
KeyStore.DefaultKeyEntries[_keClass] = model.KeyEntry;
KeyStore.DefaultKeyEntries[KeyEntryClass] = model.KeyEntry;
}
}
}
Expand Down Expand Up @@ -335,7 +322,7 @@ private async Task DeleteKeyEntry(SelectableKeyEntryId identifier)
{
if (KeyStore != null && identifier.KeyEntryId != null)
{
await KeyStore.Delete(identifier.KeyEntryId, _keClass);
await KeyStore.Delete(identifier.KeyEntryId, KeyEntryClass);
}
Identifiers.Remove(identifier);
}
Expand All @@ -358,7 +345,7 @@ private async Task MoveUpKeyEntry(SelectableKeyEntryId identifier)
{
if (KeyStore != null && identifier.KeyEntryId != null)
{
await KeyStore.MoveUp(identifier.KeyEntryId, _keClass);
await KeyStore.MoveUp(identifier.KeyEntryId, KeyEntryClass);
}
var oldIndex = Identifiers.IndexOf(identifier);
if (oldIndex > 0)
Expand All @@ -385,7 +372,7 @@ private async Task MoveDownKeyEntry(SelectableKeyEntryId identifier)
{
if (KeyStore != null && identifier.KeyEntryId != null)
{
await KeyStore.MoveDown(identifier.KeyEntryId, _keClass);
await KeyStore.MoveDown(identifier.KeyEntryId, KeyEntryClass);
}
var oldIndex = Identifiers.IndexOf(identifier);
if (oldIndex != -1 && oldIndex < Identifiers.Count - 1)
Expand Down Expand Up @@ -484,7 +471,7 @@ public async Task RefreshKeyEntries()
{
if (KeyStore != null)
{
var ids = await KeyStore.GetAll(_keClass);
var ids = await KeyStore.GetAll(KeyEntryClass);
foreach (var id in ids)
{
lock (_identifierLock)
Expand Down
10 changes: 3 additions & 7 deletions KeyManager.Library.UI/Domain/KeyEntryDialogViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ namespace Leosac.KeyManager.Library.UI.Domain
{
public class KeyEntryDialogViewModel : ObservableValidator
{
public KeyEntryDialogViewModel()
public KeyEntryDialogViewModel(KeyEntryClass keClass)
{
KClass = keClass;
_submitButtonText = Leosac.KeyManager.Library.UI.Properties.Resources.OK;
KeyEntryFactories = new ObservableCollection<KeyEntryItem>();
foreach (var factory in KeyEntryUIFactory.RegisteredFactories)
Expand Down Expand Up @@ -51,19 +52,14 @@ public KeyEntryDialogViewModel()
private bool _showKeyMaterials = true;
private bool _allowSubmit = true;
private string _submitButtonText;
private KeyEntryClass _kClass = KeyEntryClass.Symmetric;
private KeyEntry? _keyEntry;
private KeyEntryItem? _selectedFactoryItem;

public ObservableCollection<KeyEntryItem> KeyEntryFactories { get; }

public ObservableCollection<KeyEntryVariant> Variants { get; set; }

public KeyEntryClass KClass
{
get => _kClass;
set => SetProperty(ref _kClass, value);
}
public KeyEntryClass KClass { get; }

public KeyEntry? KeyEntry
{
Expand Down
21 changes: 11 additions & 10 deletions KeyManager.Library.UI/KeyEntriesControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,38 @@
<materialDesign:PopupBox
PlacementMode="BottomAndAlignCentres"
ToolTip="{x:Static properties:Resources.NewKeyEntry}" Margin="5"
Style="{StaticResource MaterialDesignMultiFloatingActionPopupBox}">
Style="{StaticResource MaterialDesignMultiFloatingActionPopupBox}"
Visibility="{Binding KeyStore.CanCreateKeyEntries, Converter={StaticResource BoolToVisibleConverter}}">
<materialDesign:PopupBox.ToggleContent>
<materialDesign:PackIcon Kind="KeyAdd" Height="24" Width="24" Cursor="Hand" />
</materialDesign:PopupBox.ToggleContent>
<StackPanel Orientation="Vertical">
<Button Style="{StaticResource MaterialDesignFloatingActionButton}"
Command="{Binding DataContext.CreateKeyEntryCommand, ElementName=keyStoreControl}"
Command="{Binding CreateKeyEntryCommand}"
ToolTip="{x:Static properties:Resources.CreateKeyEntry}" Margin="5">
<materialDesign:PackIcon Kind="Key" Height="24" Width="24" Cursor="Hand" />
</Button>
<Button Style="{StaticResource MaterialDesignFloatingActionButton}"
Command="{Binding DataContext.GenerateKeyEntryCommand, ElementName=keyStoreControl}"
Command="{Binding GenerateKeyEntryCommand}"
ToolTip="{x:Static properties:Resources.GenerateKeyEntry}" Margin="5">
<materialDesign:PackIcon Kind="ShuffleVariant" Height="24" Width="24" Cursor="Hand" />
</Button>
<Button Style="{StaticResource MaterialDesignFloatingActionButton}"
Command="{Binding DataContext.EditDefaultKeyEntryCommand, ElementName=keyStoreControl}"
Command="{Binding EditDefaultKeyEntryCommand}"
ToolTip="{x:Static properties:Resources.EditDefaultKeyEntry}" Margin="5">
<materialDesign:PackIcon Kind="LibraryEdit" Height="24" Width="24" Cursor="Hand" />
</Button>
</StackPanel>
</materialDesign:PopupBox>
<Button Style="{StaticResource MaterialDesignFloatingActionButton}"
Command="{Binding DataContext.ImportCryptogramCommand, ElementName=keyStoreControl}"
IsEnabled="{Binding DataContext.KeyStore.CanUpdateKeyEntries, ElementName=keyStoreControl}"
Command="{Binding ImportCryptogramCommand}"
IsEnabled="{Binding KeyStore.CanUpdateKeyEntries}"
ToolTip="{x:Static properties:Resources.ImportCryptogram}" Margin="5">
<materialDesign:PackIcon Kind="KeyArrowRight" Height="24" Width="24" Cursor="Hand" />
</Button>
<materialDesign:PopupBox
PlacementMode="BottomAndAlignCentres"
IsEnabled="{Binding DataContext.KeyStore.CanUpdateKeyEntries, ElementName=keyStoreControl}"
IsEnabled="{Binding KeyStore.CanUpdateKeyEntries}"
ToolTip="{x:Static properties:Resources.Wizard}" Margin="5"
Style="{StaticResource MaterialDesignMultiFloatingActionPopupBox}">
<materialDesign:PopupBox.ToggleContent>
Expand All @@ -87,9 +88,9 @@
Style="{StaticResource MaterialDesignActionToggleButton}"
ToolTip="{x:Static properties:Resources.ToggleSelection}"
Margin="5" Width="56" Height="56"
IsChecked="{Binding DataContext.ShowSelection, ElementName=keyStoreControl}"
Command="{Binding DataContext.ShowSelectionChangedCommand, ElementName=keyStoreControl}"/>
<WrapPanel Orientation="Horizontal" Visibility="{Binding DataContext.ShowSelection, ElementName=keyStoreControl, Converter={StaticResource BoolToVisibleConverter}}">
IsChecked="{Binding ShowSelection}"
Command="{Binding ShowSelectionChangedCommand}"/>
<WrapPanel Orientation="Horizontal" Visibility="{Binding ShowSelection, Converter={StaticResource BoolToVisibleConverter}}">
<Rectangle VerticalAlignment="Stretch" Width="1" Margin="2" Stroke="Gray" />
<materialDesign:DialogHost DialogClosed="KeyEntryDeletion_OnDialogClosed" DialogTheme="Inherit">
<materialDesign:DialogHost.DialogContent>
Expand Down
21 changes: 12 additions & 9 deletions KeyManager/Domain/EditKeyStoreControlViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public async Task OpenKeyStore()
var classes = KeyStore.SupportedClasses;
foreach (var kclass in classes)
{
var model = new KeyEntriesControlViewModel(_snackbarMessageQueue) { KeyEntryClass = kclass, KeyStore = KeyStore };
var model = new KeyEntriesControlViewModel(_snackbarMessageQueue, kclass) { KeyStore = KeyStore };
_keModels.Add(model);
Tabs.Add(new TabItem
{
Expand Down Expand Up @@ -244,15 +244,18 @@ public async Task Publish()
if (!string.IsNullOrEmpty(favoriteName))
{
var favorites = Favorites.GetSingletonInstance();
var fav = favorites.KeyStores.Where(ks => ks.Name.ToLowerInvariant() == favoriteName.ToLowerInvariant()).SingleOrDefault();
if (fav != null)
if (favorites != null)
{
return fav.CreateKeyStore();
}
else
{
log.Error(string.Format("Cannot found the favorite Key Store `{0}`.", favoriteName));
throw new KeyStoreException("Cannot found the favorite Key Store.");
var fav = favorites.KeyStores.Where(ks => ks.Name.ToLowerInvariant() == favoriteName.ToLowerInvariant()).SingleOrDefault();
if (fav != null)
{
return fav.CreateKeyStore();
}
else
{
log.Error(string.Format("Cannot found the favorite Key Store `{0}`.", favoriteName));
throw new KeyStoreException("Cannot found the favorite Key Store.");
}
}
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion KeyManager/EditKeyStoreControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private void BtnToggleFavorite_Unchecked(object sender, RoutedEventArgs e)
if (model.Favorite != null)
{
var favorites = Favorites.GetSingletonInstance();
if (favorites!.KeyStores.Contains(model.Favorite))
if (favorites != null && favorites.KeyStores.Contains(model.Favorite))
{
favorites.KeyStores.Remove(model.Favorite);
favorites.SaveToFile();
Expand Down

0 comments on commit 9a063e7

Please sign in to comment.