Subversion Repositories Code-Repo

Rev

Rev 18 | Blame | Compare with Previous | 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_Software : Form
    {
        public static List<string> softwareList = new List<string>();
        private static List<Process> toInstall = new List<Process>();
        private static List<CheckBox> formCheckBoxes = new List<CheckBox>();
        private static form_Software _software_Form = null;
        public static form_Software software_Form_Instance
        {
            set
            {
                _software_Form = value;
            }
            get
            {
                if (_software_Form == null)
                    _software_Form = new form_Software();
                return _software_Form;
            }
        }
        public form_Software()
        {
            try
            {
                DebugText.appendText("Software form initializing");
                // Draws the form with a checkbox for each software specified in the text file
                InitializeComponent();
                // Location placeholder 
                int nextXLocation = 6, nextYLocation = 19;
                // Updates the groupbox size to hold all the checkboxes
                this.groupBox_SoftwareList.Size = new Size(169, 40 + (softwareList.Count - 1) * 23);
                // Updates the window size to hold all the checkboxes + buttons
                this.Size = new Size(200, 121 + (softwareList.Count - 1) * 23);
                formCheckBoxes.Clear();
                // Adds a checkbox for each software item, adds checkbox to list formCheckBoxes
                foreach (string str in softwareList)
                {DebugText.appendText("Checking if applications in softwareList is marked as default");
                    CheckBox chkBox = new CheckBox();
                    chkBox.Name = "chk_" + str;
                    chkBox.AutoSize = true;
                    // Removes the brackets from the name
                    chkBox.Text = str;
                    // Checks the checkbox if default is set to yes
                    if (SettingsParser.isDefaultCheck(str))
                        chkBox.Checked = true;
                    chkBox.Location = new Point(nextXLocation, nextYLocation);
                    // Increments the location placeholder for the next checkbox
                    nextYLocation += 23;
                    this.groupBox_SoftwareList.Controls.Add(chkBox);
                    formCheckBoxes.Add(chkBox);
                }
            }
            catch (Exception e)
            {
                DebugText.appendText("Exception Thrown: " + e.ToString());
                MessageBox.Show(e.ToString());
            }
        }
        private void updateToInstallList()
        {
            try
            {
                DebugText.appendText("Updating list of processes to run from list of software checked");
                toInstall.Clear();
                // Updates list toInstall with processes for each software checked
                for (int i = 0; i < softwareList.Count; i++)
                {
                    if (formCheckBoxes[i].Checked)
                    {
                        string[] temp;
                        temp = SettingsParser.getSetupLocation(formCheckBoxes[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 startSoftwareInstaller(List<Process> toInstall, int ID)
        {
            DebugText.appendText("Starting to install softwares 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++;
                        startSoftwareInstaller(toInstall, tempID);
                    }
                }
            }
            //catch (Win32Exception e)
            //{
                
            //}
            catch (Exception e)
            {
                DebugText.appendText("Exception Thrown: " + e.ToString());
                MessageBox.Show(e.ToString());
            }
        }
        private void btn_Ok_Click(object sender, EventArgs e)
        {
            updateToInstallList();
            startSoftwareInstaller(toInstall, 0);
            this.Close();
        }
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            DebugText.appendText("Closing software form");
            software_Form_Instance.Close();
        }
    }
}