-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cs
297 lines (265 loc) · 10.5 KB
/
Main.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
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
286
287
288
289
290
291
292
293
294
295
296
297
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
namespace FileSystemsWatcherTest
{
public partial class MainForm : Form
{
private BindingList<Commander> mCommanders;
private BindingSource mBinding;
//////////////////////////////////////////////////
// Constructor
public MainForm()
{
mCommanders = new BindingList<Commander>();
mBinding = new BindingSource();
InitializeComponent();
}
//////////////////////////////////////////////////
// Control event handlers
private void addPathButton_Click(object sender, EventArgs e)
{
SetupForm dlg = new SetupForm();
if (dlg.ShowDialog() == DialogResult.OK)
{
Commander c = new Commander(dlg.CommanderName,
dlg.Filter,
dlg.WorkingDirectory,
dlg.Commands,
dlg.Paths);
c.Log += CommanderLog;
mBinding.Add(c);
c.Start();
WriteXml(mCommanders);
}
dlg.Dispose();
}
private void deletePathButton_Click(object sender, EventArgs e)
{
ArrayList Commanders = new ArrayList(commandersWindow.SelectedItems);
foreach (Commander c in Commanders)
{
c.Stop();
mBinding.Remove(c);
}
}
private void editButton_Click(object sender, EventArgs e)
{
if (commandersWindow.SelectedItems.Count == 1)
{
int index = commandersWindow.SelectedIndex;
Commander c = mCommanders[index];
SetupForm dlg = new SetupForm(c);
c.Stop();
if (dlg.ShowDialog() == DialogResult.OK)
{
c.Shutdown();
mBinding.Remove(c);
c = new Commander(dlg.CommanderName,
dlg.Filter,
dlg.WorkingDirectory,
dlg.Commands,
dlg.Paths);
c.Log += CommanderLog;
mBinding.Insert(index, c);
WriteXml(mCommanders);
}
c.Start();
dlg.Dispose();
commandersWindow.ClearSelected();
commandersWindow.SelectedIndex = index;
}
else
{
MessageBox.Show("Select one item to edit.");
}
}
private void copyButton_Click(object sender, EventArgs e)
{
if (commandersWindow.SelectedItems.Count == 1)
{
int index = commandersWindow.SelectedIndex;
Commander c = new Commander(mCommanders[index]);
SetupForm dlg = new SetupForm(c);
if (dlg.ShowDialog() == DialogResult.OK)
{
c = new Commander(dlg.CommanderName,
dlg.Filter,
dlg.WorkingDirectory,
dlg.Commands,
dlg.Paths);
c.Log += CommanderLog;
mBinding.Add(c);
WriteXml(mCommanders);
c.Start();
}
dlg.Dispose();
commandersWindow.ClearSelected();
commandersWindow.SelectedIndex = index;
}
else
{
MessageBox.Show("Select one item.");
}
}
//////////////////////////////////////////////////
// Form event handlers
private void MainForm_Load(object sender, EventArgs e)
{
LoadXml(mCommanders);
mBinding.DataSource = mCommanders;
commandersWindow.DataSource = mBinding;
StartCommanders();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
StopCommanders();
WriteXml(mCommanders);
}
//////////////////////////////////////////////////
// Commander event handlers
private void CommanderLog(object source, CommanderLogEventArgs e)
{
if (InvokeRequired)
{
logWindow.Invoke(new MethodInvoker(delegate {
logWindow.Items.Insert(0, e.Message);
}));
}
}
private void StartCommanders()
{
foreach (Commander c in mBinding)
{
c.Start();
}
}
private void StopCommanders()
{
foreach (Commander c in mBinding)
{
c.Stop();
c.Shutdown();
}
}
private string GetSettingsFile()
{
string folderName = Path.Combine(
Environment.GetFolderPath(
Environment.SpecialFolder.LocalApplicationData),
"WatcherAndCommander");
if (!Directory.Exists(folderName))
{
Directory.CreateDirectory(folderName);
}
return Path.Combine(folderName, "settings.xml");
}
private void LoadXml(BindingList<Commander> l)
{
string fileName = GetSettingsFile();
XmlDocument xmlDoc = new XmlDocument();
XmlElement xmlCommanders;
if (!File.Exists(fileName))
{
return;
}
xmlDoc.Load(fileName);
xmlCommanders = xmlDoc["Commanders"];
foreach (XmlNode xmlCommander in xmlCommanders)
{
string name;
string filter = "";
string workingDirectory = "";
List<string> commands = new List<string>();
List<string> paths = new List<string>();
Commander c;
// Read the commander settings into temporary variables
name = xmlCommander["Name"].InnerText;
if (xmlCommander["Filter"] != null)
{
filter = xmlCommander["Filter"].InnerText;
}
if (xmlCommander["WorkingDirectory"] != null)
{
workingDirectory = xmlCommander["WorkingDirectory"].InnerText;
}
if (xmlCommander["Commands"] != null)
{
commands = new List<string>(xmlCommander["Commands"].InnerText.Split(
new string[] {Environment.NewLine},
StringSplitOptions.None));
}
if (xmlCommander["Paths"] != null)
{
foreach (XmlNode xmlPath in xmlCommander["Paths"])
{
paths.Add(xmlPath.InnerText);
}
}
// Create the new commander and add it to the list
c = new Commander(name,
filter,
workingDirectory,
commands,
paths);
c.Log += CommanderLog;
mCommanders.Add(c);
}
}
private void WriteXml(BindingList<Commander> l)
{
string fileName = GetSettingsFile();
XmlDocument xmlDoc = new XmlDocument();
XmlNode xmlDocNode = xmlDoc.CreateXmlDeclaration("1.0",
"UTF-8",
null);
XmlElement xmlCommanders = xmlDoc.CreateElement("Commanders");
xmlDoc.AppendChild(xmlDocNode);
foreach (Commander c in mCommanders)
{
XmlElement xmlCommander = xmlDoc.CreateElement("Commander");
XmlElement xmlName = xmlDoc.CreateElement("Name");
XmlElement xmlFilter = xmlDoc.CreateElement("Filter");
XmlElement xmlWorkingDirectory = xmlDoc.CreateElement("WorkingDirectory");
XmlElement xmlCommands = xmlDoc.CreateElement("Commands");
XmlElement xmlPaths = xmlDoc.CreateElement("Paths");
// Set the commander name
xmlName.InnerText = c.Name;
xmlCommander.AppendChild(xmlName);
// Set the commander filter
xmlFilter.InnerText = c.Filter;
xmlCommander.AppendChild(xmlFilter);
// Set the commander working directory
xmlWorkingDirectory.InnerText = c.WorkingDirectory;
xmlCommander.AppendChild(xmlWorkingDirectory);
// Set the commander commands as a string separated by
// linebreaks to preserve command order
xmlCommands.InnerText = string.Join(Environment.NewLine,
c.Commands.ToArray());
xmlCommander.AppendChild(xmlCommands);
// Set the commander paths
foreach (string s in c.Paths)
{
XmlElement xmlPath = xmlDoc.CreateElement("Path");
xmlPath.InnerText = s;
xmlPaths.AppendChild(xmlPath);
}
xmlCommander.AppendChild(xmlPaths);
// Finally append the commander to the list of commanders
xmlCommanders.AppendChild(xmlCommander);
}
// Append the list of commanders to the document
xmlDoc.AppendChild(xmlCommanders);
// Save the XML file
xmlDoc.Save(fileName);
}
}
}