-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
DicewareOptionsForm.cs
209 lines (173 loc) · 6.29 KB
/
DicewareOptionsForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using KeePass.UI;
namespace KeePassDiceware
{
public partial class DicewareOptionsForm : Form
{
private Options _options = new();
public Options Options
{
get => _options;
set
{
_options = value;
PopulateInterface();
}
}
private List<SaltSource> _saltSources = new();
private List<WordList> _wordLists = new();
private readonly ListViewGroup[] _wordListCategories = Enum.GetNames(typeof(WordList.CategoryEnum))
.Select(wl => new ListViewGroup(wl, wl)).ToArray();
public DicewareOptionsForm()
{
InitializeComponent();
InitializeEnumOptions();
}
private void InitializeEnumOptions()
{
EnumDisplay<WordCasingType>[] casings = EnumTools.GetDisplays<WordCasingType>();
wordCasingComboBox.Items.AddRange(casings);
wordCasingComboBox.SelectedIndex = 0;
EnumDisplay<L33tSpeakType>[] l33tSpeak = EnumTools.GetDisplays<L33tSpeakType>();
l33tSpeakComboBox.Items.AddRange(l33tSpeak);
l33tSpeakComboBox.SelectedIndex = 0;
EnumDisplay<AdvancedStrategy>[] advancedStrat = EnumTools.GetDisplays<AdvancedStrategy>();
advancedStrategyComboBox.Items.AddRange(advancedStrat);
advancedStrategyComboBox.SelectedIndex = 0;
EnumDisplay<SaltType>[] salts = EnumTools.GetDisplays<SaltType>();
saltComboBox.Items.AddRange(salts);
saltComboBox.SelectedIndex = 0;
_saltSources.Clear();
activeSaltSourcesListView.Columns.Add("Salt sources");
_wordLists.Clear();
activeWordListsListView.Columns.Add("Word List");
activeWordListsListView.Groups.AddRange(_wordListCategories);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GlobalWindowManager.AddWindow(this);
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
GlobalWindowManager.RemoveWindow(this);
}
private void PopulateInterface()
{
wordCountNumericUpDown.Value = Options.WordCount;
wordSeparatorPromptedTextBox.Text = Options.WordSeparator;
wordCasingComboBox.SelectedIndex = wordCasingComboBox.FindStringExact(Options.WordCasing.GetDescription());
advancedStrategyComboBox.SelectedIndex = advancedStrategyComboBox.FindStringExact(Options.AdvancedStrategy.GetDescription());
l33tSpeakComboBox.SelectedIndex = l33tSpeakComboBox.FindStringExact(Options.L33tSpeak.GetDescription());
saltComboBox.SelectedIndex = saltComboBox.FindStringExact(Options.Salt.GetDescription());
_saltSources = new(Options.SaltSources);
_wordLists = new(Options.WordLists);
UpdateSaltSourcesListView();
UpdateWordListsListView();
}
private void UpdateSaltSourcesListView()
{
string[] sources = _saltSources.Where(ss => ss.Enabled)
.Select(ss => $"{ss.Name} "
+ (ss.MinimumAmount == ss.MaximumAmount
? $"({ss.MinimumAmount})"
: $"({ss.MinimumAmount}-{ss.MaximumAmount})")
).ToArray();
activeSaltSourcesListView.Items.Clear();
foreach (string rowString in sources)
{
ListViewItem rowItem = new ListViewItem(rowString);
activeSaltSourcesListView.Items.Add(rowItem);
}
activeSaltSourcesListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
}
private void UpdateWordListsListView()
{
ListViewItem[] lists = _wordLists.Where(wl => wl.Enabled)
.Select(wl => new ListViewItem(wl.Name,
_wordListCategories.First(g => g.Name == Enum.GetName(typeof(WordList.CategoryEnum), wl.Category))))
.ToArray();
activeWordListsListView.Items.Clear();
activeWordListsListView.Items.AddRange(lists);
activeWordListsListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
}
private Options GetOptions()
{
return new Options
{
WordCount = (int)wordCountNumericUpDown.Value,
WordSeparator = wordSeparatorPromptedTextBox.Text,
WordCasing = EnumTools.FromDescription<WordCasingType>(wordCasingComboBox.SelectedItem.ToString()),
L33tSpeak = EnumTools.FromDescription<L33tSpeakType>(l33tSpeakComboBox.SelectedItem.ToString()),
AdvancedStrategy = EnumTools.FromDescription<AdvancedStrategy>(advancedStrategyComboBox.SelectedItem.ToString()),
Salt = EnumTools.FromDescription<SaltType>(saltComboBox.SelectedItem.ToString()),
SaltSources = new(_saltSources),
WordLists = new(_wordLists)
};
}
private void OkButton_Click(object sender, System.EventArgs e)
{
Options = GetOptions();
if (Options.WordLists.Count() == 0)
{
MessageBox.Show(this, $"No word lists were selected. This will prevent the plugin from generating passwords. Please select at least one list.", "Options Validation Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
DialogResult = DialogResult.Cancel;
return;
}
DialogResult = DialogResult.OK;
Close();
}
private void CancelButton_Click(object sender, System.EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void LinkLabel_LinkClicked_OpenTagAsLink(object sender, LinkLabelLinkClickedEventArgs e)
{
if (sender is LinkLabel ll && ll.Tag is string url)
{
try
{
System.Diagnostics.Process.Start($"{url}?utm_source={nameof(KeePassDiceware)}");
}
catch (Exception ex)
{
MessageBox.Show(this, $"Failed to open URL: {url}\n\n{ex.Message}", "Link Open Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void EditSaltSourcesButton_Click(object sender, EventArgs e)
{
SaltSourcesForm ssf = new();
ssf.PopulateSaltSources(_saltSources);
if (ssf.ShowDialog(this) == DialogResult.Cancel)
{
return;
}
_saltSources = ssf.Result;
UpdateSaltSourcesListView();
}
private void EditWordListsButton_Click(object sender, EventArgs e)
{
WordListsForm wlf = new(_wordLists);
if (wlf.ShowDialog(this) == DialogResult.Cancel)
{
return;
}
_wordLists = wlf.Result;
UpdateWordListsListView();
}
private void ActiveSaltSourcesListView_SelectedIndexChanged(object sender, EventArgs e)
{
((ListView)sender).SelectedItems.Clear();
}
private void WordListsListView_SelectedIndexChanged(object sender, EventArgs e)
{
((ListView)sender).SelectedItems.Clear();
}
}
}