Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
65 Kevin 1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Linq;
7
using System.Text;
8
using System.Windows.Forms;
9
using System.Diagnostics;
10
using System.Threading;
11
 
12
namespace SWAT_USB_App
13
{
14
    public partial class Drivers_Form : Form
15
    {
16
        private static List<Process> toInstall = new List<Process>();
17
        private static List<RadioButton> formRadioButton = new List<RadioButton>();
18
        public Drivers_Form()
19
        {
20
            InitializeComponent();
21
            try
22
            {
23
                if (SettingsParser.DriversList.Count() == 0)
24
                {
25
                    Debug.appendText("No drivers were found in USBSettings.xml");
26
                    MessageBox.Show("No drivers were found in USBSettings.xml");
27
                    this.Close();
28
                }
29
                else
30
                {
31
                    bool modelFound = false;
32
                    Debug.appendText("Driver form initializing");
33
                    if (SettingsParser.driverCheckForMatchingModel)
34
                        Initialization.GetModelNumber();
35
                    // Draws the form with a radiobutton for each driver specified in the text file
36
                    // Location placeholder 
37
                    int nextXLocation = 6, nextYLocation = 19;
38
                    // Updates the groupbox size to hold all the radiobuttons
39
                    this.groupBox_DriverList.Size = new Size(SettingsParser.driverFormWidth - 31, 19 + SettingsParser.DriversList.Count() * 23);
40
                    // Updates the window size to hold all the radiobuttons + buttons
41
                    this.Size = new Size(SettingsParser.driverFormWidth, 101 + SettingsParser.DriversList.Count() * 23);
42
                    formRadioButton.Clear();
43
                    // Adds a radiobutton for each software item, adds rdoBtn to list formRadioButton
44
                    for (int i = 0; i < SettingsParser.DriversList.Count(); i++)
45
                    {
46
                        RadioButton rdoBtn = new RadioButton();
47
                        rdoBtn.Name = "rdo_" + SettingsParser.DriversList[i].Name;
48
                        rdoBtn.AutoSize = true;
49
                        // Removes the brackets from the name
50
                        rdoBtn.Text = SettingsParser.DriversList[i].Name;
51
                        rdoBtn.Location = new Point(nextXLocation, nextYLocation);
52
                        // Increments the location placeholder for the next radiobutton
53
                        nextYLocation += 23;
54
                        this.groupBox_DriverList.Controls.Add(rdoBtn);
55
                        formRadioButton.Add(rdoBtn);
56
                        if (SettingsParser.driverCheckForMatchingModel)
57
                            // If model is detected, check the correct entry
58
                            if (SettingsParser.DriversList[i].Name.ToLower() == Initialization.computerModel.ToLower() && modelFound == false)
59
                            {
60
                                modelFound = true;
61
                                rdoBtn.Checked = true;
62
                            }
63
                    }
64
                    if (SettingsParser.driverCheckForMatchingModel)
65
                    {
66
                        if (modelFound == true)
67
                        {
68
                            MessageBox.Show("Drivers were found for this computer.\n" +
69
                                "Press OK to continue then OK to install drivers.");
70
                        }
71
                        else
72
                        {
73
                            MessageBox.Show("Drivers were not found for this computer.\n" +
74
                                "Please go to the manufacturer's site and manually\n" +
75
                                "download the drivers for this computer.");
76
                        }
77
                    }
78
                }
79
            }
80
            catch (Exception ex)
81
            {
82
                Debug.appendText("Exception Thrown: " + ex.ToString());
83
                MessageBox.Show(ex.ToString());
84
            }
85
        }
86
        private void updateToInstallList()
87
        {
88
            try
89
            {
90
                Debug.appendText("Updating list of processes to run from list of drivers checked");
91
                toInstall.Clear();
92
                // Updates list toInstall with processes for each software checked
93
                for (int i = 0; i < formRadioButton.Count(); i++)
94
                {
95
                    if (formRadioButton[i].Checked)
96
                    {
97
                        string[] temp = null;
98
                        // Pulls the setup locations
99
                        for (int j = 0; j < SettingsParser.DriversList.Count(); j++)
100
                            if (SettingsParser.DriversList[j].Name == formRadioButton[i].Text)
101
                            {
102
                                temp = SettingsParser.DriversList[j].SetupFileLocation;
103
                                break;
104
                            }
105
                        for (int j = 0; j < temp.Count(); j += 2)
106
                        {
107
                            Process proc = new Process();
108
                            proc.StartInfo.FileName = Initialization.pathToSWATDrive + temp[j];
109
                            proc.StartInfo.Arguments = temp[j + 1];
110
                            toInstall.Add(proc);
111
                        }
112
                    }
113
                }
114
            }
115
            catch (Exception e)
116
            {
117
                Debug.appendText("Exception Thrown: " + e.ToString());
118
                MessageBox.Show(e.ToString());
119
            }
120
        }
121
        private void startDriverInstallation(List<Process> toInstall, int ID)
122
        {
123
            Debug.appendText("Starting to install drivers from list of processes");
124
            try
125
            {
126
                this.WindowState = FormWindowState.Minimized;
127
                // Goes through list toInstall and runs each process after the previous one ends
128
                int tempID = ID;
129
                if (tempID < toInstall.Count)
130
                {
131
                    Debug.appendText("Running setup " + toInstall[ID].StartInfo.FileName + " with arguments " + toInstall[ID].StartInfo.Arguments);
132
                    toInstall[ID].Start();
133
                    while (!toInstall[ID].HasExited)
134
                    {
135
                        // Waits for installer to finish before starting the next one
136
                        Thread.Sleep(3000);
137
                    }
138
                    if (toInstall[ID].HasExited)
139
                    {
140
                        Debug.appendText("Setup is detected as finished, running next setup");
141
                        tempID++;
142
                        startDriverInstallation(toInstall, tempID);
143
                    }
144
                }
145
                this.WindowState = FormWindowState.Normal;
146
            }
147
            catch (Exception e)
148
            {
149
                Debug.appendText("Exception Thrown: " + e.ToString());
150
                MessageBox.Show(e.ToString());
151
            }
152
        }
153
        private void btn_Driver_Ok_Click(object sender, EventArgs e)
154
        {
155
            updateToInstallList();
156
            startDriverInstallation(toInstall, 0);
157
            this.Close();
158
        }
159
        private void btn_Driver_Cancel_Click(object sender, EventArgs e)
160
        {
161
            Debug.appendText("Closing driver form");
162
            this.Close();
163
        }
164
    }
165
}