| 10 |
Kevin |
1 |
using System;
|
|
|
2 |
using System.Collections.Generic;
|
|
|
3 |
using System.Linq;
|
|
|
4 |
using System.Text;
|
|
|
5 |
using System.IO;
|
|
|
6 |
using System.Xml.Linq;
|
|
|
7 |
using System.Windows.Forms;
|
|
|
8 |
|
|
|
9 |
namespace SWAT_USB_App
|
|
|
10 |
{
|
|
|
11 |
class SettingsParser
|
|
|
12 |
{
|
|
|
13 |
private static string settingsFileLocation = Initialization.pathToSWATDrive + "USBSettings.xml";
|
|
|
14 |
private static XElement Settings;
|
|
|
15 |
|
|
|
16 |
public static bool settingsFileExist()
|
|
|
17 |
{
|
|
|
18 |
if (File.Exists(Initialization.pathToSWATDrive + "USBSettings.xml"))
|
|
|
19 |
return true;
|
|
|
20 |
else
|
|
|
21 |
return false;
|
|
|
22 |
}
|
|
|
23 |
public static void readSettingsFile()
|
|
|
24 |
{
|
|
|
25 |
try
|
|
|
26 |
{
|
|
|
27 |
DebugText.appendText("Parsing USBSettings.xml");
|
|
|
28 |
// Clears the lists for software and drivers
|
|
|
29 |
form_Software.softwareList.Clear();
|
|
|
30 |
form_Drivers.driverList.Clear();
|
|
|
31 |
|
|
|
32 |
Settings = XElement.Load(settingsFileLocation);
|
|
|
33 |
|
|
|
34 |
// Reads the list of applications from the USBSettings.xml file into form_Software.softwareList
|
|
|
35 |
DebugText.appendText("Parsing list of applications into List softwareList");
|
|
|
36 |
IEnumerable<XElement> app =
|
|
|
37 |
from el in Settings.Elements("Applications").Elements("Application")
|
|
|
38 |
select el;
|
|
|
39 |
foreach (XElement el in app)
|
|
|
40 |
{
|
|
|
41 |
DebugText.appendText("Adding application " + el.Element("Name").Value + " into softwareList");
|
|
|
42 |
form_Software.softwareList.Add(el.Element("Name").Value);
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
// Reads the list of drivers from the USBSettings.xml file into form_Drivers.driverList
|
|
|
46 |
DebugText.appendText("Parsing list of drivers into List driverList");
|
|
|
47 |
IEnumerable<XElement> driv =
|
|
|
48 |
from el in Settings.Elements("Drivers").Elements("Driver")
|
|
|
49 |
select el;
|
|
|
50 |
foreach (XElement el in driv)
|
|
|
51 |
{
|
|
|
52 |
DebugText.appendText("Adding driver " + el.Element("Name").Value + " into driverList");
|
|
|
53 |
form_Drivers.driverList.Add(el.Element("Name").Value);
|
|
|
54 |
}
|
|
|
55 |
}
|
|
|
56 |
catch (Exception e)
|
|
|
57 |
{
|
|
|
58 |
DebugText.appendText("Exception Thrown: " + e.ToString());
|
|
|
59 |
MessageBox.Show(e.ToString());
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
public static bool isDefaultCheck(string applicationName)
|
|
|
63 |
{
|
|
|
64 |
try
|
|
|
65 |
{
|
|
|
66 |
// Checks if passed application name is marked as default
|
|
|
67 |
bool isDefault = false;
|
|
|
68 |
|
|
|
69 |
IEnumerable<XElement> def =
|
|
|
70 |
from el in Settings.Elements("Applications").Elements("Application")
|
|
|
71 |
where (string)el.Element("Name") == applicationName
|
|
|
72 |
select el;
|
|
|
73 |
if ((string)def.First().Element("Default").Value.ToLower() == "yes")
|
|
|
74 |
isDefault = true;
|
|
|
75 |
return isDefault;
|
|
|
76 |
}
|
|
|
77 |
catch (Exception e)
|
|
|
78 |
{
|
|
|
79 |
DebugText.appendText("Exception Thrown: " + e.ToString());
|
|
|
80 |
MessageBox.Show(e.ToString());
|
|
|
81 |
return false;
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
public static string[] getSetupLocation(string fileName)
|
|
|
85 |
{
|
|
|
86 |
try
|
|
|
87 |
{
|
|
|
88 |
// Pulls the location and arguments for the specified application/driver from USBSettings.xml
|
|
|
89 |
DebugText.appendText("Getting setup locations for application " + fileName);
|
|
|
90 |
IEnumerable<XElement> loc =
|
|
|
91 |
from el in Settings.Elements("Applications").Elements("Application")
|
|
|
92 |
where (string)el.Element("Name") == fileName
|
|
|
93 |
select el;
|
|
|
94 |
if (loc.Count() == 0)
|
|
|
95 |
{
|
|
|
96 |
loc =
|
|
|
97 |
from el in Settings.Elements("Drivers").Elements("Driver")
|
|
|
98 |
where (string)el.Element("Name") == fileName
|
|
|
99 |
select el;
|
|
|
100 |
}
|
|
|
101 |
// Creates a string array holding alternating lines of setup locations and arguments for that setup file
|
|
|
102 |
string[] appSetupFile = new string[loc.Elements("Setup").Elements("Location").Count() * 2];
|
|
|
103 |
int counter = 0;
|
|
|
104 |
foreach (XElement elem in loc.Elements("Setup").Elements("Location"))
|
|
|
105 |
{
|
|
|
106 |
DebugText.appendText("Setup location for " + fileName + " is at " + (string)elem.Value + " with arguments " + (string)elem.Attribute("Args"));
|
|
|
107 |
appSetupFile[counter] = (string)elem.Value;
|
|
|
108 |
appSetupFile[counter + 1] = (string)elem.Attribute("Args");
|
|
|
109 |
counter += 2;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
return appSetupFile;
|
|
|
113 |
}
|
|
|
114 |
catch (Exception e)
|
|
|
115 |
{
|
|
|
116 |
DebugText.appendText("Exception Thrown: " + e.ToString());
|
|
|
117 |
MessageBox.Show(e.ToString());
|
|
|
118 |
return null;
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
}
|