-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
SaltSourcesForm.cs
173 lines (143 loc) · 4.01 KB
/
SaltSourcesForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using KeePass.UI;
namespace KeePassDiceware
{
public partial class SaltSourcesForm : Form
{
public List<SaltSource> Result { get; private set; } = null;
private BindingList<SaltSource> DataSource { get; set; } = null;
internal bool DataValid
{
get => okButton.Enabled;
set => okButton.Enabled = value;
}
private readonly List<DataGridViewCell> _pendingErrors = new();
public SaltSourcesForm()
{
InitializeComponent();
SaltSourceDataGridView.AutoGenerateColumns = false;
}
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 OkButton_Click(object sender, EventArgs e)
{
Result = DataSource.ToList();
DialogResult = DialogResult.OK;
Close();
}
private void CancelButton_Click(object sender, 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);
}
}
}
internal void PopulateSaltSources(List<SaltSource> saltSources)
{
_pendingErrors.Clear();
DataSource = new(saltSources.ConvertAll(ss => ss.Clone() as SaltSource))
{
AllowNew = true,
AllowRemove = true,
AllowEdit = true,
};
DataSource.AddingNew += DataSource_AddingNew;
DataSource.ListChanged += DataSource_ListChanged;
SaltSourceDataGridView.DataSource = DataSource;
BindingSource bindingSource = new(DataSource, null);
SaltSourceDataGridView.DataSource = bindingSource;
CheckValidation();
}
private void DataSource_ListChanged(object sender, ListChangedEventArgs e) => CheckValidation();
private void DataSource_AddingNew(object sender, AddingNewEventArgs e)
{
SaltSource created = new()
{
Name = "New Source"
};
e.NewObject = created;
}
private void SaltSourceDataGridView_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
// End of edition on each click on column of checkbox
if (e.ColumnIndex == SourceEnabled.Index && e.RowIndex != -1)
{
SaltSourceDataGridView.EndEdit();
}
}
private void SaltSourceDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
// End of edition on each click on column of checkbox
if (e.ColumnIndex == SourceEnabled.Index && e.RowIndex != -1)
{
SaltSourceDataGridView.EndEdit();
}
}
private void SaltSourceDataGridView_CellValidated(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex < 0 || e.RowIndex < 0)
{
return;
}
CheckValidation();
}
private void CheckValidation()
{
int nameErrors = ValidateSourceNames();
DataValid = nameErrors == 0;
}
private int ValidateSourceNames()
{
int errors = 0;
foreach (DataGridViewRow row in SaltSourceDataGridView.Rows)
{
if (row is null)
{
continue;
}
if (row.DataBoundItem is not SaltSource ss)
{
continue;
}
if (DataSource.Count(oss => oss.Key == ss.Key) < 2)
{
row.ErrorText = string.Empty;
}
else
{
row.ErrorText = "This source's name is too similar to another source. Please make it more unique.";
errors++;
}
}
return errors;
}
private void RestoreDefaultsButton_Click(object sender, EventArgs e)
{
PopulateSaltSources(SaltSource.Default);
}
}
}