Subversion Repositories Code-Repo

Rev

Rev 10 | Blame | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Windows.Forms;

namespace SWAT_USB_App
{
    class SettingsParser
    {
        private static string settingsFileLocation = Initialization.pathToSWATDrive + "USBSettings.xml";
        private static XElement Settings;

        public static bool settingsFileExist()
        {
            if (File.Exists(Initialization.pathToSWATDrive + "USBSettings.xml"))
                return true;
            else
                return false;
        }
        public static void readSettingsFile()
        {
            try
            {
                DebugText.appendText("Parsing USBSettings.xml");
                // Clears the lists for software and drivers
                form_Software.softwareList.Clear();
                form_Drivers.driverList.Clear();

                Settings = XElement.Load(settingsFileLocation);

                // Reads the list of applications from the USBSettings.xml file into form_Software.softwareList
                DebugText.appendText("Parsing list of applications into List softwareList");
                foreach (XElement elem in Settings.Elements("Applications").Elements("Application"))
                {
                    DebugText.appendText("Adding application " + elem.Element("Name").Value + " into softwareList");
                    form_Software.softwareList.Add(elem.Element("Name").Value);
                }
                // Alternate way of reading list of applications
                //IEnumerable<XElement> app =
                //    from el in Settings.Elements("Applications").Elements("Application")
                //    select el;
                //foreach (XElement el in app)
                //{
                //    DebugText.appendText("Adding application " + el.Element("Name").Value + " into softwareList");
                //    form_Software.softwareList.Add(el.Element("Name").Value);
                //}

                // Reads the list of drivers from the USBSettings.xml file into form_Drivers.driverList
                DebugText.appendText("Parsing list of drivers into List driverList");
                foreach (XElement elem in Settings.Elements("Drivers").Elements("Driver"))
                {
                    DebugText.appendText("Adding driver " + elem.Element("Name").Value + " into driverList");
                    form_Drivers.driverList.Add(elem.Element("Name").Value);
                }
                // Alternate way of reading list of drivers
                //IEnumerable<XElement> driv =
                //    from el in Settings.Elements("Drivers").Elements("Driver")
                //    select el;
                //foreach (XElement el in driv)
                //{
                //    DebugText.appendText("Adding driver " + el.Element("Name").Value + " into driverList");
                //    form_Drivers.driverList.Add(el.Element("Name").Value);
                //}
            }
            catch (Exception e)
            {
                DebugText.appendText("Exception Thrown: " + e.ToString());
                MessageBox.Show(e.ToString());
            }
        }
        public static bool isDefaultCheck(string applicationName)
        {
            try
            {
                // Checks if passed application name is marked as default
                bool isDefault = false;
                foreach (XElement elem in Settings.Elements("Applications").Elements("Application"))
                {
                    if (elem.Element("Name").Value == applicationName)
                        if (elem.Element("Default").Value.ToLower() == "yes")
                        {
                            isDefault = true;
                            break;
                        }
                }
                // Alternate method of finding if default is marked
                //IEnumerable<XElement> def =
                //    from el in Settings.Elements("Applications").Elements("Application")
                //    where (string)el.Element("Name") == applicationName
                //    select el;
                //if ((string)def.First().Element("Default").Value.ToLower() == "yes")
                //    isDefault = true;
                return isDefault;
            }
            catch (Exception e)
            {
                DebugText.appendText("Exception Thrown: " + e.ToString());
                MessageBox.Show(e.ToString());
                return false;
            }
        }
        public static string[] getSetupLocation(string fileName)
        {
            try
            {
                // Pulls the location and arguments for the specified application/driver from USBSettings.xml
                DebugText.appendText("Getting setup locations for application " + fileName);
                IEnumerable<XElement> loc =
                    from el in Settings.Elements("Applications").Elements("Application")
                    where (string)el.Element("Name") == fileName
                    select el;
                // Checks drivers if fileName is not found in Applications
                if (loc.Count() == 0)
                {
                    loc =
                        from el in Settings.Elements("Drivers").Elements("Driver")
                        where (string)el.Element("Name") == fileName
                        select el;
                }
                // Creates a string array holding alternating lines of setup locations and arguments for that setup file
                string[] appSetupFile = new string[loc.Elements("Setup").Elements("Location").Count() * 2];
                int counter = 0;
                foreach (XElement elem in loc.Elements("Setup").Elements("Location"))
                {
                    DebugText.appendText("Setup location for " + fileName + " is at " + (string)elem.Value + " with arguments " + (string)elem.Attribute("Args"));
                    appSetupFile[counter] = (string)elem.Value;
                    appSetupFile[counter + 1] = (string)elem.Attribute("Args");
                    counter += 2;
                }

                return appSetupFile;
            }
            catch (Exception e)
            {
                DebugText.appendText("Exception Thrown: " + e.ToString());
                MessageBox.Show(e.ToString());
                return null;
            }
        }
    }
}