Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
9 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.IO;
10
using System.Xml.Linq;
11
 
12
namespace SWAT_Office_App
13
{
14
    public partial class Settings_Form : Form
15
    {
53 Kevin 16
        // Global static variables
17
        public static string globalAdminPassword = "tr33b3@rd";
9 Kevin 18
        public static bool defaultSettings { get; set; }
50 Kevin 19
        public static bool showFileSizes { get; set; }
9 Kevin 20
        public static List<string> systemAccounts = new List<string>();
21
        public static List<string> hiddenAccounts = new List<string>();
22
        public static string sharedFolderLocation { get; set; }
23
        public static string usbMasterx32CopyLocation { get; set; }
24
        public static string usbMasterx64CopyLocation { get; set; }
50 Kevin 25
 
26
        // Static variables for use by DriveLogger
27
        public static int driveLoggerRefreshInterval { get; set; }
41 Kevin 28
        public static List<string> driveLoggerDockLabels = new List<string>();
48 Kevin 29
        public static List<string> driveLoggerDrivesToIgnore = new List<string>();
9 Kevin 30
 
31
        private static Settings_Form _Settings_Form = null;
45 Kevin 32
        // Singleton instance so the settings file isnt loaded every time
9 Kevin 33
        public static Settings_Form settings_Form_Instance
34
        {
35
            set
36
            {
37
                _Settings_Form = value;
38
            }
39
            get
40
            {
41
                if (_Settings_Form == null)
42
                    _Settings_Form = new Settings_Form();
43
                return _Settings_Form;
44
            }
45
        }
46
        public Settings_Form()
47
        {
48
            ImportSettings();
49
            InitializeComponent();
50
 
51
            this.listBox_SystemAccounts.SelectedIndexChanged += new EventHandler(this.listBox_SystemAccounts_SelectedIndexChanged);
52
            this.listBox_HiddenAccounts.SelectedIndexChanged += new EventHandler(this.listBox_HiddenAccounts_SelectedIndexChanged);
48 Kevin 53
            this.Load += new EventHandler(Settings_Form_Load);
9 Kevin 54
        }
55
        private void Settings_Form_Load(object sender, EventArgs e)
56
        {
57
            OpenUpdateSettings();
58
            settings_Form_Instance.btn_SystemRemove.Enabled = false;
59
        }
60
        public void listBox_SystemAccounts_SelectedIndexChanged(object sender, EventArgs e)
61
        {
62
            settings_Form_Instance.btn_SystemRemove.Enabled = true;
63
        }
64
        public void listBox_HiddenAccounts_SelectedIndexChanged(object sender, EventArgs e)
65
        {
66
            settings_Form_Instance.btn_HiddenRemove.Enabled = true;
67
        }
68
        public static void ImportSettings()
69
        {
45 Kevin 70
            // Imports settings from Settings.xml
9 Kevin 71
            try
72
            {
73
                if (File.Exists("Settings.xml"))
74
                {
75
                    // XML Parsing using System.XML.Linq
76
                    XElement Settings = XElement.Load("Settings.xml");
77
 
50 Kevin 78
                    showFileSizes = bool.Parse(Settings.Element("ProgramSettings").Element("ShowFileSizes").Value);
79
 
9 Kevin 80
                    systemAccounts.Clear();
81
                    string tempSystemAccounts = Settings.Element("ProgramSettings").Element("SystemAccounts").Value;
82
                    string[] tempStringArray = tempSystemAccounts.Split(new char[] { ',' });
83
                    foreach (string str in tempStringArray)
84
                        systemAccounts.Add(str.Trim());
85
 
86
                    hiddenAccounts.Clear();
87
                    string tempHiddenAccounts = Settings.Element("ProgramSettings").Element("HiddenAccounts").Value;
88
                    tempStringArray = tempHiddenAccounts.Split(new char[] { ',' });
89
                    foreach (string str in tempStringArray)
90
                        hiddenAccounts.Add(str.Trim());
91
 
41 Kevin 92
                    driveLoggerDockLabels.Clear();
93
                    string tempDriveLoggerDockLabels = Settings.Element("ProgramSettings").Element("DockLabels").Value;
94
                    tempStringArray = tempDriveLoggerDockLabels.Split(new char[] { ',' });
95
                    foreach (string str in tempStringArray)
96
                        driveLoggerDockLabels.Add(str.Trim());
97
 
98
                    driveLoggerRefreshInterval = int.Parse(Settings.Element("ProgramSettings").Element("DockRefreshInterval").Value);
99
 
48 Kevin 100
                    driveLoggerDrivesToIgnore.Clear();
101
                    string tempDrivesToIgnore = Settings.Element("ProgramSettings").Element("DockDrivesToIgnore").Value;
102
                    tempStringArray = tempDrivesToIgnore.Split(new char[] { ',' });
103
                    foreach (string str in tempStringArray)
104
                        driveLoggerDrivesToIgnore.Add(str.Trim());
105
 
9 Kevin 106
                    sharedFolderLocation = Settings.Element("ProgramSettings").Element("SharedFolderLocation").Value;
107
                    usbMasterx32CopyLocation = Settings.Element("ProgramSettings").Element("USBMasterCopyx32Location").Value;
108
                    usbMasterx64CopyLocation = Settings.Element("ProgramSettings").Element("USBMasterCopyx64Location").Value;
109
                }
110
                else
111
                {
53 Kevin 112
                    MessageBox.Show("No existing setting file detected. Using default settings");
113
                    DebugText.appendText("No existing setting file detected. Using default settings");
9 Kevin 114
                    WriteDefaultConfigFile();
115
                }
116
            }
117
            catch (Exception e)
118
            {
53 Kevin 119
                //MessageBox.Show(e.ToString(), "Error");
120
                DebugText.appendText(e.ToString());
121
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
9 Kevin 122
            }
123
        }
124
        private static void OpenUpdateSettings()
125
        {
126
            // Update the buttons on the form with the imported settings
127
            settings_Form_Instance.txt_ShareLocation.Text = sharedFolderLocation;
128
            settings_Form_Instance.txt_USBMasterCopyLocation_x32.Text = usbMasterx32CopyLocation;
129
            settings_Form_Instance.txt_USBMasterCopyLocation_x64.Text = usbMasterx64CopyLocation;
130
            PaintListBox();
131
        }
132
        private static void ExitUpdateSettings()
133
        {
134
            // Update the static variables from the form
135
            sharedFolderLocation = settings_Form_Instance.txt_ShareLocation.Text;
136
            usbMasterx32CopyLocation = settings_Form_Instance.txt_USBMasterCopyLocation_x32.Text;
137
            usbMasterx64CopyLocation = settings_Form_Instance.txt_USBMasterCopyLocation_x64.Text;
138
        }
139
        private static void ExportSettings()
140
        {
45 Kevin 141
            // Exports all settings to Settings.xml
9 Kevin 142
            try
143
            {
144
                // XML Parsing using System.XML.Linq
145
                XElement Settings = XElement.Load("Settings.xml");
146
 
147
                string tempString = "";
148
                foreach (string str in systemAccounts)
149
                    tempString = tempString.Insert(tempString.Length, str + ',');
150
                if (tempString.Length > 0)
151
                    tempString = tempString.Remove(tempString.Length - 1, 1);
152
                Settings.Element("ProgramSettings").SetElementValue("SystemAccounts", tempString);
153
 
154
                tempString = "";
155
                foreach (string str in hiddenAccounts)
156
                    tempString = tempString.Insert(tempString.Length, str + ',');
157
                if (tempString.Length > 0)
158
                    tempString = tempString.Remove(tempString.Length - 1, 1);
159
                Settings.Element("ProgramSettings").SetElementValue("HiddenAccounts", tempString);
160
 
161
                Settings.Element("ProgramSettings").SetElementValue("SharedFolderLocation", sharedFolderLocation);
162
                Settings.Element("ProgramSettings").SetElementValue("USBMasterCopyx32Location", usbMasterx32CopyLocation);
163
                Settings.Element("ProgramSettings").SetElementValue("USBMasterCopyx64Location", usbMasterx64CopyLocation);
164
 
165
                Settings.Save("Settings.xml");
166
            }
167
            catch (Exception e)
168
            {
53 Kevin 169
                //MessageBox.Show(e.ToString(), "Error");
170
                DebugText.appendText(e.ToString());
171
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
9 Kevin 172
            }
173
        }
174
        private static void WriteDefaultConfigFile()
175
        {
176
            // Writes the default settings configuration
177
            try
178
            {
179
                XElement Settings = new XElement("Settings",
180
                    new XElement("ProgramSettings",
181
                        new XElement("SystemAccounts", "Administrators,Administrator"),
182
                        new XElement("HiddenAccounts", "Administrator,Guest"),
183
                        new XElement("SharedFolderLocation", "C:\\"),
184
                        new XElement("USBMasterCopyx32Location", "C:\\"),
41 Kevin 185
                        new XElement("USBMasterCopyx64Location", "C:\\"),
186
                        new XElement("DockLabels", "Dock 1,Dock 2,Dock 3,External USB"),
48 Kevin 187
                        new XElement("DockRefreshInterval", 1000),
188
                        new XElement("DockDrivesToIgnore", "C:\\")
9 Kevin 189
                        )
190
                    );
191
                Settings.Save("Settings.xml");
192
            }
193
            catch (Exception e)
194
            {
53 Kevin 195
                //MessageBox.Show(e.ToString(), "Error");
196
                DebugText.appendText(e.ToString());
197
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
9 Kevin 198
            }
199
        }
200
        private static void PaintListBox()
201
        {
53 Kevin 202
            // Paints & Refreshes the listbox of system and hidden accounts
9 Kevin 203
            settings_Form_Instance.listBox_SystemAccounts.Items.Clear();
204
            foreach (string user in systemAccounts)
205
                settings_Form_Instance.listBox_SystemAccounts.Items.Add(user);
206
            settings_Form_Instance.listBox_HiddenAccounts.Items.Clear();
207
            foreach (string user in hiddenAccounts)
208
                settings_Form_Instance.listBox_HiddenAccounts.Items.Add(user);
209
        }
210
        private void btn_Ok_Click(object sender, EventArgs e)
211
        {
212
            ExitUpdateSettings();
213
            ExportSettings();
214
            this.Close();
215
        }
216
        private void btn_Cancel_Click(object sender, EventArgs e)
217
        {
218
            this.Close();
219
        }
220
        private void btn_Default_Click(object sender, EventArgs e)
221
        {
53 Kevin 222
            // Resets settings to default settings
9 Kevin 223
            try
224
            {
225
                if (MessageBox.Show("Reset configurations to the default settings?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
226
                {
227
                    File.Delete("Settings.xml");
228
                    WriteDefaultConfigFile();
229
                    ImportSettings();
230
                    OpenUpdateSettings();
231
                }
232
            }
233
            catch (Exception ex)
234
            {
53 Kevin 235
                //MessageBox.Show(e.ToString(), "Error");
236
                DebugText.appendText(ex.ToString());
237
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
9 Kevin 238
            }
239
        }
240
        private void btn_SystemAdd_Click(object sender, EventArgs e)
241
        {
242
            AddSystemAccount_Form newForm = new AddSystemAccount_Form();
243
            newForm.ShowDialog();
244
            PaintListBox();
245
        }
246
        private void btn_SystemRemove_Click(object sender, EventArgs e)
247
        {
248
            systemAccounts.Remove(this.listBox_SystemAccounts.SelectedItem.ToString());
249
            PaintListBox();
250
        }
251
        private void btn_HiddenAdd_Click(object sender, EventArgs e)
252
        {
253
            AddHiddenAccount_Form newForm = new AddHiddenAccount_Form();
254
            newForm.ShowDialog();
255
            PaintListBox();
256
        }
257
        private void btn_HiddenRemove_Click(object sender, EventArgs e)
258
        {
259
            hiddenAccounts.Remove(this.listBox_HiddenAccounts.SelectedItem.ToString());
260
            PaintListBox();
261
        }
262
        private void btn_ShareLocationChange_Click(object sender, EventArgs e)
263
        {
264
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
265
            {
266
                this.txt_ShareLocation.Text = folderBrowserDialog.SelectedPath;
267
                sharedFolderLocation = folderBrowserDialog.SelectedPath;
268
            }
269
        }
270
        private void btn_USBLocationx32Change_Click(object sender, EventArgs e)
271
        {
272
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
273
            {
274
                this.txt_USBMasterCopyLocation_x32.Text = folderBrowserDialog.SelectedPath;
275
                usbMasterx32CopyLocation = folderBrowserDialog.SelectedPath;
276
            }
277
        }
278
        private void btn_USBLocationx64Change_Click(object sender, EventArgs e)
279
        {
280
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
281
            {
282
                this.txt_USBMasterCopyLocation_x64.Text = folderBrowserDialog.SelectedPath;
283
                usbMasterx64CopyLocation = folderBrowserDialog.SelectedPath;
284
            }
285
        }
286
    }
287
}