-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecallForm.cs
More file actions
285 lines (255 loc) · 10 KB
/
RecallForm.cs
File metadata and controls
285 lines (255 loc) · 10 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
using System.Xml;
namespace Recall
{
public partial class RecallForm : Form
{
private static string xmlPath = Path.Combine(RecallInfo.Info?.get_Variable("PluginPath") ?? "", "Recall.xml");
private bool isClosingDueToEscKey = false;
public IEnumerable<RecallItem> GetComboBoxItems()
{
foreach (var item in comboBox1.Items)
{
if (item is RecallItem recallItem)
{
yield return recallItem;
}
}
}
public RecallForm()
{
InitializeComponent();
#pragma warning disable CS8622 // Nullability of reference types in type of parameter doesn't match the target delegate (possibly because of nullability attributes).
comboBox1.SelectedIndexChanged += ComboBox1_SelectedIndexChanged;
#pragma warning restore CS8622 // Nullability of reference types in type of parameter doesn't match the target delegate (possibly because of nullability attributes).
xmlPath = Path.Combine(RecallInfo.Info?.get_Variable("PluginPath") ?? "", "Recall.xml");
if (File.Exists(xmlPath))
{
LoadFromXML();
}
else
{
var xmlDocument = new XmlDocument();
XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null);
xmlDocument.AppendChild(xmlDeclaration);
XmlElement rootNode = xmlDocument.CreateElement("RecallData");
xmlDocument.AppendChild(rootNode);
xmlDocument.Save(xmlPath);
}
}
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem is RecallItem selectedItem)
{
textBox.Text = selectedItem.Info;
}
}
private void TextBox_TextChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex != -1)
{
if (comboBox1.SelectedItem is RecallItem selectedItem)
{
selectedItem.Info = textBox.Text;
}
}
}
private void ButtonAdd_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(comboBox1.Text))
{
var existingItem = FindItemByName(comboBox1.Text);
if (existingItem != null)
{
existingItem.Info = textBox.Text;
}
else
{
RecallItem item = new(comboBox1.Text, textBox.Text);
comboBox1.Items.Add(item);
}
SaveToXML();
textBox.Clear();
comboBox1.Text = string.Empty;
}
LoadFromXML();
}
private void ButtonRemove_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex != -1)
{
if (comboBox1.SelectedItem is RecallItem selectedItem)
{
var result = MessageBox.Show($"Are you sure you want to remove '{selectedItem.Name}'?", "Confirm Deletion",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
comboBox1.Items.Remove(selectedItem);
textBox.Clear();
}
}
}
SaveToXML();
}
private void ButtonLoad_Click(object sender, EventArgs e)
{
LoadFromXML();
}
private void RecallForm_KeyDown(object? sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
e.SuppressKeyPress = true;
isClosingDueToEscKey = true;
this.Close();
}
}
private void RecallForm_FormClosing(object? sender, FormClosingEventArgs e)
{
if (isClosingDueToEscKey)
{
// Unregister the event handlers before closing the form.
this.KeyDown -= (s, ev) => RecallForm_KeyDown(s, ev);
this.FormClosing -= (s, ev) => RecallForm_FormClosing(s, ev);
}
else
{
isClosingDueToEscKey = true;
this.Close();
}
}
private void RecallForm_FormClosed(object? sender, FormClosedEventArgs e)
{
this.KeyDown -= (s, ev) => RecallForm_KeyDown(s, ev);
this.FormClosing -= (s, ev) => RecallForm_FormClosing(s, ev);
this.FormClosed -= (s, ev) => RecallForm_FormClosed(s, ev);
}
private RecallItem? FindItemByName(string name)
{
foreach (var item in comboBox1.Items)
{
if (item is RecallItem recallItem && recallItem.Name == name)
{
return recallItem;
}
}
return null;
}
internal void SaveToXML()
{
string pluginPath = RecallInfo.Info?.get_Variable("PluginPath") ?? string.Empty;
if (!string.IsNullOrEmpty(pluginPath))
{
string xmlPath = Path.Combine(pluginPath, "Recall.xml");
XmlDocument xmlDocument = new();
if (File.Exists(xmlPath))
{
xmlDocument.Load(xmlPath);
}
else
{
XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null);
xmlDocument.AppendChild(xmlDeclaration);
XmlElement rootNode = xmlDocument.CreateElement("RecallData");
xmlDocument.AppendChild(rootNode);
}
XmlElement? rootElement = xmlDocument.DocumentElement;
if (rootElement != null)
{
rootElement.RemoveAll();
var sortedItems = comboBox1.Items.Cast<RecallItem>().OrderBy(item => item.Name).ToList();
foreach (RecallItem item in sortedItems)
{
XmlElement newItemNode = xmlDocument.CreateElement("Item");
newItemNode.SetAttribute("Name", item.Name);
XmlElement infoElement = xmlDocument.CreateElement("Info");
infoElement.InnerText = item.Info.Replace(Environment.NewLine, "\n");
newItemNode.AppendChild(infoElement);
rootElement.AppendChild(newItemNode);
}
xmlDocument.Save(xmlPath);
}
else
{
RecallInfo.Info?.EchoText("Failed to create XML root element.");
}
}
else
{
RecallInfo.Info?.EchoText("Plugin path is not set.");
}
}
internal void LoadFromXML()
{
string pluginPath = RecallInfo.Info?.get_Variable("PluginPath") ?? string.Empty;
if (!string.IsNullOrEmpty(pluginPath))
{
string xmlPath = Path.Combine(pluginPath, "Recall.xml");
if (File.Exists(xmlPath))
{
XmlDocument xmlDocument = new();
try
{
xmlDocument.Load(xmlPath);
XmlElement? rootElement = xmlDocument.DocumentElement;
comboBox1.Items.Clear();
if (rootElement != null)
{
foreach (XmlNode node in rootElement.ChildNodes)
{
if (node is XmlElement element)
{
string name = element.GetAttribute("Name");
string info = element["Info"]?.InnerText.Replace("\n", Environment.NewLine) ?? string.Empty;
RecallItem item = new(name, info);
comboBox1.Items.Add(item);
}
}
}
else
{
RecallInfo.Info?.EchoText("Invalid XML structure.");
}
}
catch (Exception ex)
{
RecallInfo.Info?.EchoText($"Failed to load XML: {ex.Message}");
}
}
else
{
RecallInfo.Info?.EchoText("XML file not found.");
}
}
else
{
RecallInfo.Info?.EchoText("Plugin path is not set.");
}
}
private void ButtonEcho_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedItem is RecallItem selectedItem)
{
string normalizedInfo = selectedItem.Info.Replace("\r\n", "\n");
RecallActions.Display(selectedItem.Name, normalizedInfo);
}
else
{
RecallInfo.Info?.EchoText("No item selected to echo.");
}
}
}
public class RecallItem
{
public string Name { get; set; }
public string Info { get; set; }
public RecallItem(string name, string info)
{
Name = name;
Info = info;
}
public override string ToString()
{
return Name;
}
}
}