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 Software_Form : Form
15
    {
16
        private static List<string> subGroups = new List<string>();
17
        private static List<Process> toInstall = new List<Process>();
18
        private static List<CheckBox> formCheckBoxes = new List<CheckBox>();
19
        private static List<RadioButton> formRadioButton = new List<RadioButton>();
20
        public Software_Form()
21
        {
22
            // Draws the form with a checkbox for each software specified in the text file
23
            InitializeComponent();
24
            try
25
            {
26
                if (SettingsParser.ApplicationsList.Count() == 0)
27
                {
28
                    Debug.appendText("No software were found in USBSettings.xml");
29
                    MessageBox.Show("No software were found in USBSettings.xml");
30
                    this.Close();
31
                }
32
                else
33
                {
34
                    Debug.appendText("Software form initializing");
35
                    // Clears all lists
36
                    subGroups.Clear();
37
                    toInstall.Clear();
38
                    formCheckBoxes.Clear();
39
                    formRadioButton.Clear();
40
                    // Location placeholder 
41
                    int nextXLocation = 6, nextYLocation = 19;
42
                    // Extracts all groups from ApplicationsList
43
                    for (int i = 0; i < SettingsParser.ApplicationsList.Count(); i++)
44
                        if (SettingsParser.ApplicationsList[i].Group != null)
45
                        {
46
                            // Check if group exists already
47
                            bool groupExist = false;
48
                            for (int j = 0; j < subGroups.Count(); j++)
49
                                if (SettingsParser.ApplicationsList[i].Group == subGroups[j])
50
                                {
51
                                    groupExist = true;
52
                                    break;
53
                                }
54
                            if (!groupExist)
55
                                subGroups.Add(SettingsParser.ApplicationsList[i].Group);
56
                        }
57
                    // Updates the groupbox size to hold all the checkboxes and subgroups
58
                    this.groupBox_SoftwareList.Size = new Size(SettingsParser.softwareFormWidth - 30, 19 + SettingsParser.ApplicationsList.Count() * 23 + (subGroups.Count() * 25));
59
                    // Updates the window size to hold all elements
60
                    this.Size = new Size(SettingsParser.softwareFormWidth, 101 + SettingsParser.ApplicationsList.Count() * 23 + (subGroups.Count() * 25));
61
                    // Adds a checkbox for each software item that is ungrouped, adds checkbox to list formCheckBoxes
62
                    for (int i = 0; i < SettingsParser.ApplicationsList.Count(); i++)
63
                    {
64
                        if (SettingsParser.ApplicationsList[i].Group == null)
65
                        {
66
                            // Creates a new checkbox
67
                            CheckBox chkBox = new CheckBox();
68
                            chkBox.Name = "chk_" + SettingsParser.ApplicationsList[i].Name;
69
                            chkBox.AutoSize = true;
70
                            chkBox.Text = SettingsParser.ApplicationsList[i].Name;
71
                            if (SettingsParser.ApplicationsList[i].Default)
72
                                chkBox.Checked = true;
73
                            chkBox.Location = new Point(nextXLocation, nextYLocation);
74
                            // Increments the location placeholder for the next element
75
                            nextYLocation += 23;
76
                            // Adds the checkbox to the parent groupbox
77
                            this.groupBox_SoftwareList.Controls.Add(chkBox);
78
                            // Adds the checkbox to formCheckBoxes list
79
                            formCheckBoxes.Add(chkBox);
80
                        }
81
                    }
82
                    // Adds a groupbox for each entry specified in subGroups as well as any applications under the group
83
                    for (int i = 0; i < subGroups.Count(); i++)
84
                    {
85
                        // Sets the internal location coordinates for the next element
86
                        int subNextXLocation = 6;
87
                        int subNextYLocation = 19;
88
                        // Creates a new groupbox
89
                        GroupBox grpBox = new GroupBox();
90
                        grpBox.Name = "grp_" + subGroups[i];
91
                        grpBox.Text = subGroups[i];
92
                        // Finds the number of applications under the group
93
                        int numberOfSubentries = 0;
94
                        for (int j = 0; j < SettingsParser.ApplicationsList.Count(); j++)
95
                            if (SettingsParser.ApplicationsList[j].Group == subGroups[i])
96
                            {
97
                                // If the group matches, creates a new radiobutton
98
                                numberOfSubentries++;
99
                                RadioButton rdoBtn = new RadioButton();
100
                                rdoBtn.Name = "rdo_" + SettingsParser.ApplicationsList[j].Name;
101
                                rdoBtn.AutoSize = true;
102
                                rdoBtn.Text = SettingsParser.ApplicationsList[j].Name;
103
                                if (SettingsParser.ApplicationsList[j].Default)
104
                                    rdoBtn.Checked = true;
105
                                rdoBtn.Location = new Point(subNextXLocation, subNextYLocation);
106
                                // Increments the location placeholder for the next element
107
                                subNextYLocation += 23;
108
                                // Adds the radiobutton to the parent groupbox
109
                                grpBox.Controls.Add(rdoBtn);
110
                                // Adds the radiobutton to formRadioButton list
111
                                formRadioButton.Add(rdoBtn);
112
                            }
113
                        grpBox.Size = new Size(SettingsParser.softwareFormWidth - 42, 19 + numberOfSubentries * 23);
114
                        grpBox.Location = new Point(nextXLocation, nextYLocation);
115
                        nextYLocation += 25 + numberOfSubentries * 23;
116
                        // Adds the created groupbox to the parent groupbox
117
                        this.groupBox_SoftwareList.Controls.Add(grpBox);
118
                    }
119
                }
120
            }
121
            catch (Exception ex)
122
            {
123
                Debug.appendText("Exception Thrown: " + ex.ToString());
124
                MessageBox.Show(ex.ToString());
125
            }
126
        }
127
        private void updateToInstallList()
128
        {
129
            try
130
            {
131
                Debug.appendText("Updating list of processes to run from list of software checked");
132
                // Updates list toInstall with processes for each software checked in formCheckBoxes
133
                for (int i = 0; i < formCheckBoxes.Count(); i++)
134
                {
135
                    if (formCheckBoxes[i].Checked)
136
                    {
137
                        string[] temp = null;
138
                        // Pulls the setup locations
139
                        for (int j = 0; j < SettingsParser.ApplicationsList.Count(); j++)
140
                            if (SettingsParser.ApplicationsList[j].Name == formCheckBoxes[i].Text)
141
                            {
142
                                temp = SettingsParser.ApplicationsList[j].SetupFileLocation;
143
                                break;
144
                            }
145
                        for (int j = 0; j < temp.Count(); j += 2)
146
                        {
147
                            Process proc = new Process();
148
                            proc.StartInfo.FileName = Initialization.pathToSWATDrive + temp[j];
149
                            proc.StartInfo.Arguments = temp[j + 1];
150
                            toInstall.Add(proc);
151
                        }
152
                    }
153
                }
154
                // Updates list toInstall with processes for each software checked in formRadioButton
155
                for (int i = 0; i < formRadioButton.Count(); i++)
156
                {
157
                    if (formRadioButton[i].Checked)
158
                    {
159
                        string[] temp = null;
160
                        // Pulls the setup locations
161
                        for (int j = 0; j < SettingsParser.ApplicationsList.Count(); j++)
162
                            if (SettingsParser.ApplicationsList[j].Name == formRadioButton[i].Text)
163
                            {
164
                                temp = SettingsParser.ApplicationsList[j].SetupFileLocation;
165
                                break;
166
                            }
167
                        for (int j = 0; j < temp.Count(); j += 2)
168
                        {
169
                            Process proc = new Process();
170
                            proc.StartInfo.FileName = Initialization.pathToSWATDrive + temp[j];
171
                            proc.StartInfo.Arguments = temp[j + 1];
172
                            toInstall.Add(proc);
173
                        }
174
                    }
175
                }
176
            }
177
            catch (Exception e)
178
            {
179
                Debug.appendText("Exception Thrown: " + e.ToString());
180
                MessageBox.Show(e.ToString());
181
            }
182
        }
183
        private void startSoftwareInstaller(List<Process> toInstall, int ID)
184
        {
185
            Debug.appendText("Starting to install softwares from list of processes");
186
            try
187
            {
188
                this.WindowState = FormWindowState.Minimized;
189
                // Goes through list toInstall and runs each process after the previous one ends
190
                int tempID = ID;
191
                if (tempID < toInstall.Count)
192
                {
193
                    Debug.appendText("Running setup " + toInstall[ID].StartInfo.FileName + " with arguments " + toInstall[ID].StartInfo.Arguments);
194
                    toInstall[ID].Start();
195
                    while (!toInstall[ID].HasExited)
196
                    {
197
                        // Waits for installer to finish before starting the next one
198
                        Thread.Sleep(3000);
199
                    }
200
                    if (toInstall[ID].HasExited)
201
                    {
202
                        Debug.appendText("Setup is detected as finished, running next setup");
203
                        tempID++;
204
                        startSoftwareInstaller(toInstall, tempID);
205
                    }
206
                }
207
                this.WindowState = FormWindowState.Normal;
208
            }
209
            catch (Exception e)
210
            {
211
                Debug.appendText("Exception Thrown: " + e.ToString());
212
                MessageBox.Show(e.ToString());
213
            }
214
        }
215
        private void btn_Ok_Click(object sender, EventArgs e)
216
        {
217
            updateToInstallList();
218
            startSoftwareInstaller(toInstall, 0);
219
            this.Close();
220
        }
221
        private void btn_Cancel_Click(object sender, EventArgs e)
222
        {
223
            Debug.appendText("Closing software form");
224
            this.Close();
225
        }
226
        private void btn_Reset_Click(object sender, EventArgs e)
227
        {
228
            for (int i = 0; i < formCheckBoxes.Count(); i++)
229
                formCheckBoxes[i].Checked = false;
230
            for (int i = 0; i < formRadioButton.Count(); i++)
231
                formRadioButton[i].Checked = false;
232
        }
233
    }
234
}