Skip to content

Commit 65af51a

Browse files
committed
Settings
Added new settings. Not the greatest, but there is no performance impact and this will allow me to split out the command line and also allow multiple instances to be scheduled at once without overwriting the user settings file.
1 parent 6c44729 commit 65af51a

File tree

9 files changed

+612
-439
lines changed

9 files changed

+612
-439
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Xml;
3+
4+
namespace Snap2HTMLNG.Shared.Settings
5+
{
6+
/// <summary>
7+
/// Configuration Class used for reading and writing user settings
8+
/// </summary>
9+
public class XmlConfigurator
10+
{
11+
12+
/// <summary>
13+
/// The UserSettings file, saved in the Current Running Directory of the executable
14+
/// </summary>
15+
private static readonly string SettingsFile = "UserSettings.xml";
16+
17+
/// <summary>
18+
/// Writes the setting value to the <see cref="SettingsFile"/>
19+
/// </summary>
20+
/// <param name="nodeList">
21+
/// string[] array of nodes in the settings file, see <see cref="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/arrays"/>
22+
/// </param>
23+
/// <param name="valueList">
24+
/// string[] array of values for the nodes in the settings file
25+
/// </param>
26+
/// <param name="rootNode">
27+
/// Root Node of the Configuration File
28+
/// </param>
29+
public static void Write (string[] nodeList, string[] valueList, string rootNode = "UserSettings")
30+
{
31+
XmlWriterSettings XmlSettings = new XmlWriterSettings()
32+
{
33+
Indent = true,
34+
IndentChars = " ",
35+
NewLineChars = Environment.NewLine
36+
};
37+
38+
using (XmlWriter w = XmlWriter.Create(SettingsFile, XmlSettings))
39+
{
40+
w.WriteStartElement(rootNode);
41+
int index = 0;
42+
43+
foreach (var i in nodeList)
44+
{
45+
w.WriteElementString(i, valueList[index++]);
46+
}
47+
w.WriteEndElement();
48+
w.Flush();
49+
}
50+
}
51+
52+
/// <summary>
53+
/// Reads the setting value from <see cref="SettingsFile"/>
54+
/// </summary>
55+
/// <param name="node">
56+
/// Settings Node you require data from
57+
/// </param>
58+
/// <param name="rootNode">
59+
/// Root Node of the Configuration File
60+
/// </param>
61+
/// <returns>
62+
/// Node Value from Configuration File as a <see cref="string"/>
63+
/// </returns>
64+
public static string Read(string node, string rootNode = "UserSettings")
65+
{
66+
XmlDocument xml = new XmlDocument();
67+
xml.Load(SettingsFile);
68+
69+
XmlNodeList nodeList = xml.GetElementsByTagName(rootNode);
70+
string nodeValue = string.Empty;
71+
foreach(XmlElement element in nodeList)
72+
{
73+
nodeValue = element[node].InnerText;
74+
}
75+
return nodeValue;
76+
}
77+
}
78+
}

Snap2HTML-NG.Shared/Snap2HTML-NG.Shared.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<Compile Include="Models\SnappedFile.cs" />
4545
<Compile Include="Models\SnappedFolder.cs" />
4646
<Compile Include="Properties\AssemblyInfo.cs" />
47+
<Compile Include="Settings\XmlConfigurator.cs" />
4748
<Compile Include="Utils\Legacy.cs" />
4849
</ItemGroup>
4950
<ItemGroup>
@@ -53,6 +54,9 @@
5354
<Content Include="template.html">
5455
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
5556
</Content>
57+
<Content Include="UserSettings.xml">
58+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
59+
</Content>
5660
</ItemGroup>
5761
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5862
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
3+
<UserSettings>
4+
5+
<RootFolder></RootFolder>
6+
<Title></Title>
7+
<OutputFile></OutputFile>
8+
<SkipHiddenItems>true</SkipHiddenItems>
9+
<SkipSystemItems>true</SkipSystemItems>
10+
<OpenInBrowserAfterCapture>false</OpenInBrowserAfterCapture>
11+
<LinkFiles>false</LinkFiles>
12+
<LinkRoot></LinkRoot>
13+
<SearchPattern>*</SearchPattern>
14+
15+
</UserSettings>

Snap2HTML/Forms/frmMain.Designer.cs

Lines changed: 134 additions & 89 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Snap2HTML/Forms/frmMain.cs

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Windows.Forms;
55
using CommandLine.Utility;
6+
using Snap2HTMLNG.Shared.Settings;
67
using Snap2HTMLNG.Shared.Utils;
78

89
namespace Snap2HTMLNG
@@ -73,49 +74,48 @@ private void frmMain_Shown(object sender, EventArgs e)
7374
}
7475
}
7576

76-
var settings = new Model.SnapSettings();
7777
if (arguments.Exists("path") && arguments.Exists("outfile"))
7878
{
7979
this.runningAutomated = true;
8080

81-
settings.rootFolder = arguments.Single("path");
82-
settings.outputFile = arguments.Single("outfile");
81+
var rootFolder = arguments.Single("path");
82+
var outputFile = arguments.Single("outfile");
8383

8484
// First validate paths
85-
if (!Directory.Exists(settings.rootFolder))
85+
if (!Directory.Exists(rootFolder))
8686
{
8787
if (!arguments.Exists("silent"))
8888
{
89-
MessageBox.Show("Input path does not exist: " + settings.rootFolder, "Automation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
89+
MessageBox.Show("Input path does not exist: " + rootFolder, "Automation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
9090
}
9191
Application.Exit();
9292
}
93-
if (!Directory.Exists(Path.GetDirectoryName(settings.outputFile)))
93+
if (!Directory.Exists(Path.GetDirectoryName(outputFile)))
9494
{
9595
if (!arguments.Exists("silent"))
9696
{
97-
MessageBox.Show("Output path does not exist: " + Path.GetDirectoryName(settings.outputFile), "Automation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
97+
MessageBox.Show("Output path does not exist: " + Path.GetDirectoryName(outputFile), "Automation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
9898
}
9999
Application.Exit();
100100
}
101101

102102
// Rest of settings
103103

104-
settings.skipHiddenItems = !arguments.Exists("hidden");
105-
settings.skipSystemItems = !arguments.Exists("system");
106-
settings.openInBrowser = false;
104+
bool skipHiddenItems = !arguments.Exists("hidden");
105+
bool skipSystemItems = !arguments.Exists("system");
106+
bool openInBrowser = false;
107107

108-
settings.linkFiles = false;
108+
bool linkFiles = false;
109109
if (arguments.Exists("link"))
110110
{
111-
settings.linkFiles = true;
112-
settings.linkRoot = arguments.Single("link");
111+
linkFiles = true;
112+
string linkRoot = arguments.Single("link");
113113
}
114114

115-
settings.title = "Snapshot of " + settings.rootFolder;
115+
string title = "Snapshot of " + rootFolder;
116116
if (arguments.Exists("title"))
117117
{
118-
settings.title = arguments.Single("title");
118+
title = arguments.Single("title");
119119
}
120120

121121
}
@@ -132,7 +132,7 @@ private void frmMain_Shown(object sender, EventArgs e)
132132

133133
if (this.runningAutomated)
134134
{
135-
StartProcessing(settings);
135+
StartProcessing();
136136
}
137137
}
138138

@@ -189,55 +189,77 @@ private void cmdCreate_Click(object sender, EventArgs e)
189189

190190
if (!saveFileDialog1.FileName.ToLower().EndsWith(".html")) saveFileDialog1.FileName += ".html";
191191

192-
// begin generating html
193-
var settings = new Model.SnapSettings()
192+
// Declare the user settings nodes that are available in UserSettings.xml (see Shared.UserSettings.xml)
193+
string[] nodes = {
194+
"RootFolder",
195+
"Title",
196+
"OutputFile",
197+
"SkipHiddenItems",
198+
"SkipSystemItems",
199+
"OpenInBrowserAfterCapture",
200+
"LinkFiles",
201+
"LinkRoot",
202+
"SearchPattern"
203+
};
204+
205+
// Declare our actual values to be saved to the nodes
206+
string[] values =
194207
{
195-
rootFolder = txtRoot.Text,
196-
title = txtTitle.Text,
197-
outputFile = saveFileDialog1.FileName,
198-
skipHiddenItems = !chkHidden.Checked,
199-
skipSystemItems = !chkSystem.Checked,
200-
openInBrowser = chkOpenOutput.Checked,
201-
linkFiles = chkLinkFiles.Checked,
202-
linkRoot = txtLinkRoot.Text,
203-
searchPattern = txtSearchPattern.Text,
208+
txtRoot.Text,
209+
txtTitle.Text,
210+
saveFileDialog1.FileName,
211+
chkHidden.Checked.ToString(),
212+
chkSystem.Checked.ToString(),
213+
chkOpenOutput.Checked.ToString(),
214+
chkLinkFiles.Checked.ToString(),
215+
txtLinkRoot.Text,
216+
txtSearchPattern.Text
204217
};
205218

206-
StartProcessing(settings);
219+
// Write the settings to the SettingsFile // TODO: Do I need to move this so we can run from schedule multiple different times? I think so.
220+
XmlConfigurator.Write(nodes, values);
221+
222+
// begin generating html
223+
StartProcessing();
207224
}
208225
}
209226

210-
private void StartProcessing(Model.SnapSettings settings)
227+
private void StartProcessing()
211228
{
212229
// ensure source path format
213-
settings.rootFolder = Path.GetFullPath(settings.rootFolder);
214-
if (settings.rootFolder.EndsWith(@"\")) settings.rootFolder = settings.rootFolder.Substring(0, settings.rootFolder.Length - 1);
215-
if (Legacy.IsWildcardMatch("?:", settings.rootFolder, false)) settings.rootFolder += @"\"; // add backslash to path if only letter and colon eg "c:"
230+
var rootFolder = Path.GetFullPath(XmlConfigurator.Read("RootFolder"));
231+
232+
if (rootFolder.EndsWith(@"\")) rootFolder = rootFolder.Substring(0, rootFolder.Length - 1);
233+
if (Legacy.IsWildcardMatch("?:", rootFolder, false)) rootFolder += @"\"; // add backslash to path if only letter and colon eg "c:"
216234

217235
// add slash or backslash to end of link (in cases where it is clear that we we can)
218-
if (settings.linkFiles)
236+
237+
238+
bool linkFiles = bool.Parse(XmlConfigurator.Read("LinkFiles"));
239+
string linkRoot = XmlConfigurator.Read("LinkRoot");
240+
if (linkFiles)
219241
{
220-
if (!settings.linkRoot.EndsWith(@"/"))
242+
if (!linkRoot.EndsWith(@"/"))
221243
{
222-
if (settings.linkRoot.ToLower().StartsWith(@"http") || settings.linkRoot.ToLower().StartsWith(@"https")) // web site
244+
if (linkRoot.ToLower().StartsWith(@"http") || linkRoot.ToLower().StartsWith(@"https")) // web site
223245
{
224-
settings.linkRoot += @"/";
246+
linkRoot += @"/";
225247
}
226-
if (Legacy.IsWildcardMatch("?:*", settings.linkRoot, false)) // local disk
248+
if (Legacy.IsWildcardMatch("?:*", linkRoot, false)) // local disk
227249
{
228-
settings.linkRoot += @"\";
250+
linkRoot += @"\";
229251
}
230-
if (settings.linkRoot.StartsWith(@"\\")) // unc path
252+
if (linkRoot.StartsWith(@"\\")) // unc path
231253
{
232-
settings.linkRoot += @"\";
254+
linkRoot += @"\";
233255
}
234256
}
235257
}
236258

237259
Cursor.Current = Cursors.WaitCursor;
238-
this.Text = "Snap2HTML (Working... Press Escape to Cancel)";
260+
Text = "Snap2HTML (Working... Press Escape to Cancel)";
239261
tabCtrl.Enabled = false;
240-
backgroundWorker.RunWorkerAsync(argument: settings);
262+
backgroundWorker.RunWorkerAsync();
241263
}
242264

243265
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)

0 commit comments

Comments
 (0)