Skip to content

Commit

Permalink
Add Settings window, to optionally change the default file path for F…
Browse files Browse the repository at this point in the history
…avorites
  • Loading branch information
Maxhy committed Mar 7, 2024
1 parent de7e5ac commit 7591754
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Leosac.WpfApp" Version="1.15.1">
<PackageReference Include="Leosac.WpfApp" Version="1.16.0">
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
<PackageReference Include="MaterialDesignThemes" Version="4.9.0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Leosac.WpfApp" Version="1.15.1">
<PackageReference Include="Leosac.WpfApp" Version="1.16.0">
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
<PackageReference Include="MaterialDesignThemes" Version="4.9.0">
Expand Down
23 changes: 22 additions & 1 deletion KeyManager.Library.UI/Favorites.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Favorites : PermanentConfig<Favorites>
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod()?.DeclaringType);
private static readonly object _objlock = new();
private static Favorites? _singleton;
private static KMSettings? _settings;

public static EventHandler? SingletonCreated { get; set; }
private static void OnSingletonCreated()
Expand All @@ -29,7 +30,15 @@ private static void OnSingletonCreated()
{
try
{
_singleton = LoadFromFile();
_settings = KMSettings.LoadFromFile();
if (!string.IsNullOrEmpty(_settings?.FavoritesPath))
{
_singleton = LoadFromFile(_settings.FavoritesPath);
}
else
{
_singleton = LoadFromFile();
}
OnSingletonCreated();
}
catch (Exception ex)
Expand All @@ -42,6 +51,18 @@ private static void OnSingletonCreated()
}
}

public override void SaveToFile()
{
if (!string.IsNullOrEmpty(_settings?.FavoritesPath))
{
SaveToFile(_settings.FavoritesPath);
}
else
{
base.SaveToFile();
}
}

public ObservableCollection<Favorite> KeyStores { get; set; } = new ObservableCollection<Favorite>();

public Favorite CreateFromKeyStore(KeyStore.KeyStore store)
Expand Down
14 changes: 14 additions & 0 deletions KeyManager.Library.UI/KMSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Leosac.WpfApp;

namespace Leosac.KeyManager.Library.UI
{
public class KMSettings : PermanentConfig<KMSettings>
{
private string? _favoritesPath;
public string? FavoritesPath
{
get => _favoritesPath;
set => SetProperty(ref _favoritesPath, value);
}
}
}
2 changes: 1 addition & 1 deletion KeyManager.Library.UI/KeyManager.Library.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Leosac.WpfApp" Version="1.15.1" />
<PackageReference Include="Leosac.WpfApp" Version="1.16.0" />
<PackageReference Include="MaterialDesignThemes" Version="4.9.0" />
<PackageReference Include="Net.Codecrete.QrCodeGenerator" Version="2.0.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
Expand Down
2 changes: 1 addition & 1 deletion KeyManager.Library/KeyStore/KeyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public virtual async Task Publish(KeyStore store, Func<string, KeyStore?> getFav
}
else
{
log.Info("Dry Run, skipping key entries storing.");
log.Info("Dry Run, skipping the storage of key entries.");
}
}
finally
Expand Down
1 change: 1 addition & 0 deletions KeyManager/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Leosac.KeyManager.Library.UI;
using Leosac.WpfApp;
using Newtonsoft.Json;
using System.Configuration;
using System.Linq;
using System.Windows;

Expand Down
6 changes: 6 additions & 0 deletions KeyManager/KMLeosacAppInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public KMLeosacAppInfo()
ApplicationCode = "lkm";
ApplicationUrl = "https://leosac.com/key-manager/";
ApplicationLogo = "/images/leosac_key.png";
SettingsCommand = new RelayCommand(() =>
{
var settingsWindow = new SettingsWindow();
settingsWindow.DataContext = KMSettings.LoadFromFile();
settingsWindow.ShowDialog();
});
}

public override void InitializeMainWindow(MainWindowViewModel model)
Expand Down
2 changes: 1 addition & 1 deletion KeyManager/KeyManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Leosac.WpfApp" Version="1.15.1" />
<PackageReference Include="Leosac.WpfApp" Version="1.16.0" />
<PackageReference Include="log4net" Version="2.0.15" />
<PackageReference Include="MaterialDesignThemes" Version="4.9.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
Expand Down
36 changes: 36 additions & 0 deletions KeyManager/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions KeyManager/Properties/Resources.fr.resx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
<data name="Back" xml:space="preserve">
<value>Retour</value>
</data>
<data name="Browse" xml:space="preserve">
<value>Parcourir</value>
</data>
<data name="Cancel" xml:space="preserve">
<value>Annuler</value>
</data>
Expand Down Expand Up @@ -159,6 +162,15 @@
<data name="Favorites" xml:space="preserve">
<value>Magasins de clés favoris</value>
</data>
<data name="FavoritesPath" xml:space="preserve">
<value>Chemin d'accès au fichier de favoris</value>
</data>
<data name="FavoritesPathHelper" xml:space="preserve">
<value>Le fichier JSON où sont stockés les favoris. Si non défini, le chemin par défaut est utilisé.</value>
</data>
<data name="General" xml:space="preserve">
<value>Général</value>
</data>
<data name="Import" xml:space="preserve">
<value>Importer</value>
</data>
Expand Down
12 changes: 12 additions & 0 deletions KeyManager/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
<data name="Back" xml:space="preserve">
<value>Back</value>
</data>
<data name="Browse" xml:space="preserve">
<value>Browse</value>
</data>
<data name="Cancel" xml:space="preserve">
<value>Cancel</value>
</data>
Expand Down Expand Up @@ -159,6 +162,15 @@
<data name="Favorites" xml:space="preserve">
<value>Key Store Favorites</value>
</data>
<data name="FavoritesPath" xml:space="preserve">
<value>Favorites file path</value>
</data>
<data name="FavoritesPathHelper" xml:space="preserve">
<value>The path to the JSON file where favorites are saved. If undefined, the default path will be used.</value>
</data>
<data name="General" xml:space="preserve">
<value>General</value>
</data>
<data name="Import" xml:space="preserve">
<value>Import</value>
</data>
Expand Down
40 changes: 40 additions & 0 deletions KeyManager/SettingsWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Window x:Class="Leosac.KeyManager.SettingsWindow"
xmlns="http:https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http:https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http:https://schemas.microsoft.com/expression/blend/2008"
xmlns:libui="clr-namespace:Leosac.KeyManager.Library.UI;assembly=KeyManager.Library.UI"
xmlns:mc="http:https://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Leosac.KeyManager"
xmlns:materialDesign="http:https://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:properties="clr-namespace:Leosac.KeyManager.Properties"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance libui:KMSettings}"
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
TextElement.FontWeight="Regular"
TextElement.FontSize="11"
TextOptions.TextFormattingMode="Ideal"
TextOptions.TextRenderingMode="Auto"
Background="{DynamicResource MaterialDesignPaper}"
FontFamily="{DynamicResource MaterialDesignFont}"
Title="Settings" Height="450" Width="800"
Closing="Window_Closing">
<DockPanel LastChildFill="True">
<WrapPanel DockPanel.Dock="Bottom" HorizontalAlignment="Right" Margin="5,5,50,5">
<Button Content="{x:Static properties:Resources.Close}" Width="200" IsDefault="True" x:Name="btnClose" Click="BtnClose_Click" />
</WrapPanel>
<Grid>
<TabControl>
<TabItem Header="{x:Static properties:Resources.General}">
<StackPanel>
<DockPanel LastChildFill="True">
<Button Content="{x:Static properties:Resources.Browse}" Click="Browse_Click" DockPanel.Dock="Right" Margin="5" />
<TextBox Text="{Binding FavoritesPath}" Margin="5,5,5,15"
materialDesign:HintAssist.HelperText="{x:Static properties:Resources.FavoritesPathHelper}"
materialDesign:HintAssist.Hint="{x:Static properties:Resources.FavoritesPath}"/>
</DockPanel>
</StackPanel>
</TabItem>
</TabControl>
</Grid>
</DockPanel>
</Window>
41 changes: 41 additions & 0 deletions KeyManager/SettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Leosac.KeyManager.Library.UI;
using Microsoft.Win32;
using System.Windows;

namespace Leosac.KeyManager
{
/// <summary>
/// Interaction logic for SettingsWindow.xaml
/// </summary>
public partial class SettingsWindow : Window
{
public SettingsWindow()
{
InitializeComponent();
}

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (DataContext is KMSettings settings)
{
settings.SaveToFile();
}
}

private void BtnClose_Click(object sender, RoutedEventArgs e)
{
this.Close();
}

private void Browse_Click(object sender, RoutedEventArgs e)
{
var ofd = new OpenFileDialog();
ofd.Filter = "JSON files (*.json)|*.json";
ofd.CheckFileExists = false;
if (ofd.ShowDialog() == true && DataContext is KMSettings settings)
{
settings.FavoritesPath = ofd.FileName;
}
}
}
}

0 comments on commit 7591754

Please sign in to comment.