Subversion Repositories Code-Repo

Rev

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