Skip to content

Commit

Permalink
Add Vault window
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Jul 20, 2022
1 parent a936dc0 commit e0e273d
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,18 @@ public static void UpdateScriptConfigs(IEnumerable<ConfigScriptEntry> configScri
}

private static string GetSettingsPath()
{
return GetSettingsPathFor("settings.json");
}

public static string GetSettingsPathFor(string settingsFile)
{
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "ScriptRunner");
if (Directory.Exists(path) == false)
{
Directory.CreateDirectory(path);
}
return Path.Combine(path, "settings.json");

return Path.Combine(path, settingsFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,12 @@ public void RefreshSettings()
BuildUi();
}

public void OpenVaultWindow()
{
var window = new Vault();
window.Show();
}


public bool SelectedActionInstalled
{
Expand Down
63 changes: 63 additions & 0 deletions src/ScriptRunner/ScriptRunner.GUI/ViewModels/VaultViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using ReactiveUI;
using ScriptRunner.GUI.Settings;

namespace ScriptRunner.GUI.ViewModels
{
public class VaultViewModel : ViewModelBase
{
public ObservableCollection<VaultEntry> Entries
{
get => _entries;
set => this.RaiseAndSetIfChanged(ref _entries, value);
}

private ObservableCollection<VaultEntry> _entries;


public VaultViewModel()
{
var vaultPath = AppSettingsService.GetSettingsPathFor("Vault.dat");
if (File.Exists(vaultPath))
{
File.Decrypt(vaultPath);
var content = File.ReadAllText(vaultPath);
File.Encrypt(vaultPath);
var data = JsonSerializer.Deserialize<List<VaultEntry>>(content);
Entries = new ObservableCollection<VaultEntry>(data?? new List<VaultEntry>());
}
else
{
Entries = new ObservableCollection<VaultEntry>()
{

};
}

}

public void AddNewVaultEntry()
{
Entries.Add(new VaultEntry());
}

public void SaveVault()
{
var date = Entries.ToList();
var vaultPath = AppSettingsService.GetSettingsPathFor("Vault.dat");
File.WriteAllText(vaultPath, JsonSerializer.Serialize(date), Encoding.UTF8);
File.Encrypt(vaultPath);
}
}

public class VaultEntry
{
public string Name { get; set; }
public string Secret { get; set; }
}
}
3 changes: 2 additions & 1 deletion src/ScriptRunner/ScriptRunner.GUI/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
</ListBox>
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" VerticalAlignment="Bottom" Grid.Row="1">
<Button i:Attached.Icon="fas fa-cog" Margin="5" Height="35" Width="50" FontSize="18" Command="{Binding OpenSettingsWindow}" ToolTip.Tip="Configure actions sources"/>
<Button i:Attached.Icon="fas fa-sync" Height="35" Width="50" FontSize="18" Command="{Binding RefreshSettings}" ToolTip.Tip="Refresh actions" />
<Button i:Attached.Icon="fas fa-sync" Margin="0,0,5,0" Height="35" Width="50" FontSize="18" Command="{Binding RefreshSettings}" ToolTip.Tip="Refresh actions" />
<Button i:Attached.Icon="fas fa-lock" Height="35" Width="50" FontSize="18" Command="{Binding OpenVaultWindow}" ToolTip.Tip="Open Vault" />
</StackPanel>
</Grid>
<GridSplitter Grid.Column="1" Grid.Row="1" ResizeDirection="Columns"></GridSplitter>
Expand Down
51 changes: 51 additions & 0 deletions src/ScriptRunner/ScriptRunner.GUI/Views/Vault.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:ScriptRunner.GUI.ViewModels"
xmlns:avalonia="clr-namespace:Projektanker.Icons.Avalonia;assembly=Projektanker.Icons.Avalonia"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
Width="800"
Height="450"
x:Class="ScriptRunner.GUI.Views.Vault"
Title="Vault">
<Design.DataContext>
<viewModels:VaultViewModel />
</Design.DataContext>

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel>
<ItemsControl Items="{Binding Entries}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="10,10,10,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="0.5*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="0.5*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Margin="0,0,10,0" VerticalAlignment="Center">Key</Label>
<TextBox Grid.Column="1" Text="{Binding Name, Mode=TwoWay}"></TextBox>
<Label Grid.Column="2" Margin="10,0,10,0" VerticalAlignment="Center">Value</Label>
<TextBox PasswordChar="*" Grid.Column="3" Text="{Binding Secret, Mode=TwoWay}"></TextBox>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button Command="{Binding AddNewVaultEntry}" Margin="20">
<StackPanel Orientation="Horizontal">
<avalonia:Icon Value="fa fa-plus" Margin="4,0"/>
<TextBlock>Add new vault entry</TextBlock>
</StackPanel>
</Button>
</StackPanel>
<StackPanel Grid.Row="1" HorizontalAlignment="Right">
<Button Margin="10" Click="CloseVaultDialog"> Close</Button>
</StackPanel>
</Grid>
</Window>
33 changes: 33 additions & 0 deletions src/ScriptRunner/ScriptRunner.GUI/Views/Vault.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using ScriptRunner.GUI.ViewModels;

namespace ScriptRunner.GUI.Views
{
public partial class Vault : Window
{
public Vault()
{
InitializeComponent();
DataContext = this.ViewModel = new VaultViewModel();
#if DEBUG
this.AttachDevTools();
#endif
}

public VaultViewModel ViewModel { get; set; }

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}

private void CloseVaultDialog(object? sender, RoutedEventArgs e)
{
ViewModel.SaveVault();
Close();
}
}
}

0 comments on commit e0e273d

Please sign in to comment.