Subversion Repositories Code-Repo

Rev

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

using System;
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.Diagnostics;
using System.Threading;

namespace SWAT_USB_App
{
    public partial class form_Drivers : Form
    {
        public static List<string> driverList = new List<string>();
        private static List<Process> toInstall = new List<Process>();
        private static List<RadioButton> formRadioButton = new List<RadioButton>();
        private static form_Drivers _driver_Form = null;
        public static form_Drivers driver_Form_Instance
        {
            set
            {
                _driver_Form = value;
            }
            get
            {
                if (_driver_Form == null)
                    _driver_Form = new form_Drivers();
                return _driver_Form;
            }
        }
        public form_Drivers()
        {
                this.Load += new EventHandler(form_Drivers_Load);
                InitializeComponent();
        }

        void form_Drivers_Load(object sender, EventArgs e)
        {
            try
            {
                if (driverList.Count == 0)
                {
                    DebugText.appendText("No drivers were found in USBSettings.xml");
                    MessageBox.Show("No drivers were found in USBSettings.xml");
                    this.Close();
                }
                else
                {
                    bool modelFound = false;
                    DebugText.appendText("Driver form initializing");
                    Initialization.GetModelNumber();
                    // Draws the form with a radiobutton for each driver specified in the text file
                    // Location placeholder 
                    int nextXLocation = 6, nextYLocation = 19;
                    // Updates the groupbox size to hold all the radiobuttons
                    this.groupBox_DriverList.Size = new Size(149, 40 + (driverList.Count - 1) * 23);
                    // Updates the window size to hold all the radiobuttons + buttons
                    this.Size = new Size(180, 121 + (driverList.Count - 1) * 23);
                    formRadioButton.Clear();
                    // Adds a radiobutton for each software item, adds rdoBtn to list formRadioButton
                    foreach (string str in driverList)
                    {
                        RadioButton rdoBtn = new RadioButton();
                        rdoBtn.Name = "rdo_" + str;
                        rdoBtn.AutoSize = true;
                        // Removes the brackets from the name
                        rdoBtn.Text = str;
                        rdoBtn.Location = new Point(nextXLocation, nextYLocation);
                        // Increments the location placeholder for the next radiobutton
                        nextYLocation += 23;
                        this.groupBox_DriverList.Controls.Add(rdoBtn);
                        formRadioButton.Add(rdoBtn);
                        // If model is detected, check the correct entry
                        if (str.ToLower() == Initialization.computerModel.ToLower() && modelFound == false)
                        {
                            modelFound = true;
                            rdoBtn.Checked = true;
                        }
                    }
                    if (modelFound == true)
                    {
                        MessageBox.Show("Drivers were found for this computer.\n" +
                            "Press OK to continue then OK to install drivers.");
                    }
                    else
                    {
                        MessageBox.Show("Drivers were not found for this computer.\n" +
                            "Please go to the manufacturer's site and manually\n" +
                            "download the drivers for this computer.");
                    }
                }
            }
            catch (Exception ex)
            {
                DebugText.appendText("Exception Thrown: " + e.ToString());
                MessageBox.Show(ex.ToString());
            }
        }
        private void updateToInstallList()
        {
            try
            {
                DebugText.appendText("Updating list of processes to run from list of drivers checked");
                toInstall.Clear();
                // Updates list toInstall with processes for each software checked
                for (int i = 0; i < driverList.Count; i++)
                {
                    if (formRadioButton[i].Checked)
                    {
                        string[] temp;
                        temp = SettingsParser.getSetupLocation(formRadioButton[i].Text);
                        for (int j = 0; j < temp.Count(); j += 2)
                        {
                            Process proc = new Process();
                            proc.StartInfo.FileName = Initialization.pathToSWATDrive + temp[j];
                            proc.StartInfo.Arguments = temp[j + 1];
                            toInstall.Add(proc);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                DebugText.appendText("Exception Thrown: " + e.ToString());
                MessageBox.Show(e.ToString());
            }
        }
        private void startDriverInstallation(List<Process> toInstall, int ID)
        {
            DebugText.appendText("Starting to install drivers from list of processes");
            try
            {
                // Goes through list toInstall and runs each process after the previous one ends
                int tempID = ID;
                if (tempID < toInstall.Count)
                {
                    DebugText.appendText("Running setup " + toInstall[ID].StartInfo.FileName + " with arguments " + toInstall[ID].StartInfo.Arguments);
                    toInstall[ID].Start();
                    while (!toInstall[ID].HasExited)
                    {
                        // Waits for installer to finish before starting the next one
                        Thread.Sleep(3000);
                    }
                    if (toInstall[ID].HasExited)
                    {
                        DebugText.appendText("Setup is detected as finished, running next setup");
                        tempID++;
                        startDriverInstallation(toInstall, tempID);
                    }
                }
            }
            //catch (Win32Exception e)
            //{

            //}
            catch (Exception e)
            {
                DebugText.appendText("Exception Thrown: " + e.ToString());
                MessageBox.Show(e.ToString());
            }
        }
        private void btn_Driver_Ok_Click(object sender, EventArgs e)
        {
            updateToInstallList();
            startDriverInstallation(toInstall, 0);
            this.Close();
        }
        private void btn_Driver_Cancel_Click(object sender, EventArgs e)
        {
            DebugText.appendText("Closing driver form");
            driver_Form_Instance.Close();
        }
    }
}