Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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");
16 Kevin 36
                foreach (XElement elem in Settings.Elements("Applications").Elements("Application"))
10 Kevin 37
                {
16 Kevin 38
                    DebugText.appendText("Adding application " + elem.Element("Name").Value + " into softwareList");
39
                    form_Software.softwareList.Add(elem.Element("Name").Value);
10 Kevin 40
                }
16 Kevin 41
                // Alternate way of reading list of applications
42
                //IEnumerable<XElement> app =
43
                //    from el in Settings.Elements("Applications").Elements("Application")
44
                //    select el;
45
                //foreach (XElement el in app)
46
                //{
47
                //    DebugText.appendText("Adding application " + el.Element("Name").Value + " into softwareList");
48
                //    form_Software.softwareList.Add(el.Element("Name").Value);
49
                //}
10 Kevin 50
 
51
                // Reads the list of drivers from the USBSettings.xml file into form_Drivers.driverList
52
                DebugText.appendText("Parsing list of drivers into List driverList");
16 Kevin 53
                foreach (XElement elem in Settings.Elements("Drivers").Elements("Driver"))
10 Kevin 54
                {
16 Kevin 55
                    DebugText.appendText("Adding driver " + elem.Element("Name").Value + " into driverList");
56
                    form_Drivers.driverList.Add(elem.Element("Name").Value);
10 Kevin 57
                }
16 Kevin 58
                // Alternate way of reading list of drivers
59
                //IEnumerable<XElement> driv =
60
                //    from el in Settings.Elements("Drivers").Elements("Driver")
61
                //    select el;
62
                //foreach (XElement el in driv)
63
                //{
64
                //    DebugText.appendText("Adding driver " + el.Element("Name").Value + " into driverList");
65
                //    form_Drivers.driverList.Add(el.Element("Name").Value);
66
                //}
10 Kevin 67
            }
68
            catch (Exception e)
69
            {
70
                DebugText.appendText("Exception Thrown: " + e.ToString());
71
                MessageBox.Show(e.ToString());
72
            }
73
        }
74
        public static bool isDefaultCheck(string applicationName)
75
        {
76
            try
77
            {
78
                // Checks if passed application name is marked as default
79
                bool isDefault = false;
16 Kevin 80
                foreach (XElement elem in Settings.Elements("Applications").Elements("Application"))
81
                {
82
                    if (elem.Element("Name").Value == applicationName)
83
                        if (elem.Element("Default").Value.ToLower() == "yes")
84
                        {
85
                            isDefault = true;
86
                            break;
87
                        }
88
                }
89
                // Alternate method of finding if default is marked
90
                //IEnumerable<XElement> def =
91
                //    from el in Settings.Elements("Applications").Elements("Application")
92
                //    where (string)el.Element("Name") == applicationName
93
                //    select el;
94
                //if ((string)def.First().Element("Default").Value.ToLower() == "yes")
95
                //    isDefault = true;
10 Kevin 96
                return isDefault;
97
            }
98
            catch (Exception e)
99
            {
100
                DebugText.appendText("Exception Thrown: " + e.ToString());
101
                MessageBox.Show(e.ToString());
102
                return false;
103
            }
104
        }
105
        public static string[] getSetupLocation(string fileName)
106
        {
107
            try
108
            {
109
                // Pulls the location and arguments for the specified application/driver from USBSettings.xml
110
                DebugText.appendText("Getting setup locations for application " + fileName);
111
                IEnumerable<XElement> loc =
112
                    from el in Settings.Elements("Applications").Elements("Application")
113
                    where (string)el.Element("Name") == fileName
114
                    select el;
16 Kevin 115
                // Checks drivers if fileName is not found in Applications
10 Kevin 116
                if (loc.Count() == 0)
117
                {
118
                    loc =
119
                        from el in Settings.Elements("Drivers").Elements("Driver")
120
                        where (string)el.Element("Name") == fileName
121
                        select el;
122
                }
123
                // Creates a string array holding alternating lines of setup locations and arguments for that setup file
124
                string[] appSetupFile = new string[loc.Elements("Setup").Elements("Location").Count() * 2];
125
                int counter = 0;
126
                foreach (XElement elem in loc.Elements("Setup").Elements("Location"))
127
                {
128
                    DebugText.appendText("Setup location for " + fileName + " is at " + (string)elem.Value + " with arguments " + (string)elem.Attribute("Args"));
129
                    appSetupFile[counter] = (string)elem.Value;
130
                    appSetupFile[counter + 1] = (string)elem.Attribute("Args");
131
                    counter += 2;
132
                }
133
 
134
                return appSetupFile;
135
            }
136
            catch (Exception e)
137
            {
138
                DebugText.appendText("Exception Thrown: " + e.ToString());
139
                MessageBox.Show(e.ToString());
140
                return null;
141
            }
142
        }
143
    }
144
}