Subversion Repositories Code-Repo

Rev

Rev 9 | 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.IO;
using System.Xml.Linq;

namespace SWAT_Office_App
{
    public partial class Settings_Form : Form
    {
        public static bool defaultSettings { get; set; }
        public static List<string> systemAccounts = new List<string>();
        public static List<string> hiddenAccounts = new List<string>();
        public static string sharedFolderLocation { get; set; }
        public static string usbMasterx32CopyLocation { get; set; }
        public static string usbMasterx64CopyLocation { get; set; }
        public static List<string> driveLoggerDockLabels = new List<string>();
        public static int driveLoggerRefreshInterval { get; set; }

        private static Settings_Form _Settings_Form = null;
        // Singleton instance
        public static Settings_Form settings_Form_Instance
        {
            set
            {
                _Settings_Form = value;
            }
            get
            {
                if (_Settings_Form == null)
                    _Settings_Form = new Settings_Form();
                return _Settings_Form;
            }
        }
        public Settings_Form()
        {
            ImportSettings();
            InitializeComponent();
            
            this.listBox_SystemAccounts.SelectedIndexChanged += new EventHandler(this.listBox_SystemAccounts_SelectedIndexChanged);
            this.listBox_HiddenAccounts.SelectedIndexChanged += new EventHandler(this.listBox_HiddenAccounts_SelectedIndexChanged);
        }
        private void Settings_Form_Load(object sender, EventArgs e)
        {
            OpenUpdateSettings();
            settings_Form_Instance.btn_SystemRemove.Enabled = false;
        }
        public void listBox_SystemAccounts_SelectedIndexChanged(object sender, EventArgs e)
        {
            settings_Form_Instance.btn_SystemRemove.Enabled = true;
        }
        public void listBox_HiddenAccounts_SelectedIndexChanged(object sender, EventArgs e)
        {
            settings_Form_Instance.btn_HiddenRemove.Enabled = true;
        }
        public static void ImportSettings()
        {
            try
            {
                if (File.Exists("Settings.xml"))
                {
                    // XML Parsing using System.XML.Linq
                    XElement Settings = XElement.Load("Settings.xml");

                    systemAccounts.Clear();
                    string tempSystemAccounts = Settings.Element("ProgramSettings").Element("SystemAccounts").Value;
                    string[] tempStringArray = tempSystemAccounts.Split(new char[] { ',' });
                    foreach (string str in tempStringArray)
                        systemAccounts.Add(str.Trim());

                    hiddenAccounts.Clear();
                    string tempHiddenAccounts = Settings.Element("ProgramSettings").Element("HiddenAccounts").Value;
                    tempStringArray = tempHiddenAccounts.Split(new char[] { ',' });
                    foreach (string str in tempStringArray)
                        hiddenAccounts.Add(str.Trim());

                    driveLoggerDockLabels.Clear();
                    string tempDriveLoggerDockLabels = Settings.Element("ProgramSettings").Element("DockLabels").Value;
                    tempStringArray = tempDriveLoggerDockLabels.Split(new char[] { ',' });
                    foreach (string str in tempStringArray)
                        driveLoggerDockLabels.Add(str.Trim());

                    driveLoggerRefreshInterval = int.Parse(Settings.Element("ProgramSettings").Element("DockRefreshInterval").Value);

                    sharedFolderLocation = Settings.Element("ProgramSettings").Element("SharedFolderLocation").Value;
                    usbMasterx32CopyLocation = Settings.Element("ProgramSettings").Element("USBMasterCopyx32Location").Value;
                    usbMasterx64CopyLocation = Settings.Element("ProgramSettings").Element("USBMasterCopyx64Location").Value;
                }
                else
                {
                    WriteDefaultConfigFile();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
        private static void OpenUpdateSettings()
        {
            // Update the buttons on the form with the imported settings
            settings_Form_Instance.txt_ShareLocation.Text = sharedFolderLocation;
            settings_Form_Instance.txt_USBMasterCopyLocation_x32.Text = usbMasterx32CopyLocation;
            settings_Form_Instance.txt_USBMasterCopyLocation_x64.Text = usbMasterx64CopyLocation;
            PaintListBox();
        }
        private static void ExitUpdateSettings()
        {
            // Update the static variables from the form
            sharedFolderLocation = settings_Form_Instance.txt_ShareLocation.Text;
            usbMasterx32CopyLocation = settings_Form_Instance.txt_USBMasterCopyLocation_x32.Text;
            usbMasterx64CopyLocation = settings_Form_Instance.txt_USBMasterCopyLocation_x64.Text;
        }
        private static void ExportSettings()
        {
            try
            {
                // XML Parsing using System.XML.Linq
                XElement Settings = XElement.Load("Settings.xml");

                string tempString = "";
                foreach (string str in systemAccounts)
                    tempString = tempString.Insert(tempString.Length, str + ',');
                if (tempString.Length > 0)
                    tempString = tempString.Remove(tempString.Length - 1, 1);
                Settings.Element("ProgramSettings").SetElementValue("SystemAccounts", tempString);

                tempString = "";
                foreach (string str in hiddenAccounts)
                    tempString = tempString.Insert(tempString.Length, str + ',');
                if (tempString.Length > 0)
                    tempString = tempString.Remove(tempString.Length - 1, 1);
                Settings.Element("ProgramSettings").SetElementValue("HiddenAccounts", tempString);

                Settings.Element("ProgramSettings").SetElementValue("SharedFolderLocation", sharedFolderLocation);
                Settings.Element("ProgramSettings").SetElementValue("USBMasterCopyx32Location", usbMasterx32CopyLocation);
                Settings.Element("ProgramSettings").SetElementValue("USBMasterCopyx64Location", usbMasterx64CopyLocation);

                Settings.Save("Settings.xml");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
        private static void WriteDefaultConfigFile()
        {
            // Writes the default settings configuration
            try
            {
                XElement Settings = new XElement("Settings",
                    new XElement("ProgramSettings",
                        new XElement("SystemAccounts", "Administrators,Administrator"),
                        new XElement("HiddenAccounts", "Administrator,Guest"),
                        new XElement("SharedFolderLocation", "C:\\"),
                        new XElement("USBMasterCopyx32Location", "C:\\"),
                        new XElement("USBMasterCopyx64Location", "C:\\"),
                        new XElement("DockLabels", "Dock 1,Dock 2,Dock 3,External USB"),
                        new XElement("DockRefreshInterval", 1000)
                        )
                    );
                Settings.Save("Settings.xml");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
        private static void PaintListBox()
        {
            // Paints&Refreshes the listbox of system accounts
            settings_Form_Instance.listBox_SystemAccounts.Items.Clear();
            foreach (string user in systemAccounts)
                settings_Form_Instance.listBox_SystemAccounts.Items.Add(user);
            settings_Form_Instance.listBox_HiddenAccounts.Items.Clear();
            foreach (string user in hiddenAccounts)
                settings_Form_Instance.listBox_HiddenAccounts.Items.Add(user);
        }
        private void btn_Ok_Click(object sender, EventArgs e)
        {
            ExitUpdateSettings();
            ExportSettings();
            this.Close();
        }
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btn_Default_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Reset configurations to the default settings?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    File.Delete("Settings.xml");
                    WriteDefaultConfigFile();
                    ImportSettings();
                    OpenUpdateSettings();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void btn_SystemAdd_Click(object sender, EventArgs e)
        {
            AddSystemAccount_Form newForm = new AddSystemAccount_Form();
            newForm.ShowDialog();
            PaintListBox();
        }
        private void btn_SystemRemove_Click(object sender, EventArgs e)
        {
            systemAccounts.Remove(this.listBox_SystemAccounts.SelectedItem.ToString());
            PaintListBox();
        }
        private void btn_HiddenAdd_Click(object sender, EventArgs e)
        {
            AddHiddenAccount_Form newForm = new AddHiddenAccount_Form();
            newForm.ShowDialog();
            PaintListBox();
        }
        private void btn_HiddenRemove_Click(object sender, EventArgs e)
        {
            hiddenAccounts.Remove(this.listBox_HiddenAccounts.SelectedItem.ToString());
            PaintListBox();
        }
        private void btn_ShareLocationChange_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                this.txt_ShareLocation.Text = folderBrowserDialog.SelectedPath;
                sharedFolderLocation = folderBrowserDialog.SelectedPath;
            }

        }
        private void btn_USBLocationx32Change_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                this.txt_USBMasterCopyLocation_x32.Text = folderBrowserDialog.SelectedPath;
                usbMasterx32CopyLocation = folderBrowserDialog.SelectedPath;
            }
        }
        private void btn_USBLocationx64Change_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                this.txt_USBMasterCopyLocation_x64.Text = folderBrowserDialog.SelectedPath;
                usbMasterx64CopyLocation = folderBrowserDialog.SelectedPath;
            }
        }
    }
}