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",
55 Kevin 181
                        new XElement("ShowFileSizes","true"),
9 Kevin 182
                        new XElement("SystemAccounts", "Administrators,Administrator"),
183
                        new XElement("HiddenAccounts", "Administrator,Guest"),
184
                        new XElement("SharedFolderLocation", "C:\\"),
185
                        new XElement("USBMasterCopyx32Location", "C:\\"),
41 Kevin 186
                        new XElement("USBMasterCopyx64Location", "C:\\"),
187
                        new XElement("DockLabels", "Dock 1,Dock 2,Dock 3,External USB"),
48 Kevin 188
                        new XElement("DockRefreshInterval", 1000),
189
                        new XElement("DockDrivesToIgnore", "C:\\")
9 Kevin 190
                        )
191
                    );
192
                Settings.Save("Settings.xml");
193
            }
194
            catch (Exception e)
195
            {
53 Kevin 196
                //MessageBox.Show(e.ToString(), "Error");
197
                DebugText.appendText(e.ToString());
198
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
9 Kevin 199
            }
200
        }
201
        private static void PaintListBox()
202
        {
53 Kevin 203
            // Paints & Refreshes the listbox of system and hidden accounts
9 Kevin 204
            settings_Form_Instance.listBox_SystemAccounts.Items.Clear();
205
            foreach (string user in systemAccounts)
206
                settings_Form_Instance.listBox_SystemAccounts.Items.Add(user);
207
            settings_Form_Instance.listBox_HiddenAccounts.Items.Clear();
208
            foreach (string user in hiddenAccounts)
209
                settings_Form_Instance.listBox_HiddenAccounts.Items.Add(user);
210
        }
211
        private void btn_Ok_Click(object sender, EventArgs e)
212
        {
213
            ExitUpdateSettings();
214
            ExportSettings();
215
            this.Close();
216
        }
217
        private void btn_Cancel_Click(object sender, EventArgs e)
218
        {
219
            this.Close();
220
        }
221
        private void btn_Default_Click(object sender, EventArgs e)
222
        {
53 Kevin 223
            // Resets settings to default settings
9 Kevin 224
            try
225
            {
226
                if (MessageBox.Show("Reset configurations to the default settings?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
227
                {
228
                    File.Delete("Settings.xml");
229
                    WriteDefaultConfigFile();
230
                    ImportSettings();
231
                    OpenUpdateSettings();
232
                }
233
            }
234
            catch (Exception ex)
235
            {
53 Kevin 236
                //MessageBox.Show(e.ToString(), "Error");
237
                DebugText.appendText(ex.ToString());
238
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
9 Kevin 239
            }
240
        }
241
        private void btn_SystemAdd_Click(object sender, EventArgs e)
242
        {
243
            AddSystemAccount_Form newForm = new AddSystemAccount_Form();
244
            newForm.ShowDialog();
245
            PaintListBox();
246
        }
247
        private void btn_SystemRemove_Click(object sender, EventArgs e)
248
        {
249
            systemAccounts.Remove(this.listBox_SystemAccounts.SelectedItem.ToString());
250
            PaintListBox();
251
        }
252
        private void btn_HiddenAdd_Click(object sender, EventArgs e)
253
        {
254
            AddHiddenAccount_Form newForm = new AddHiddenAccount_Form();
255
            newForm.ShowDialog();
256
            PaintListBox();
257
        }
258
        private void btn_HiddenRemove_Click(object sender, EventArgs e)
259
        {
260
            hiddenAccounts.Remove(this.listBox_HiddenAccounts.SelectedItem.ToString());
261
            PaintListBox();
262
        }
263
        private void btn_ShareLocationChange_Click(object sender, EventArgs e)
264
        {
265
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
266
            {
267
                this.txt_ShareLocation.Text = folderBrowserDialog.SelectedPath;
268
                sharedFolderLocation = folderBrowserDialog.SelectedPath;
269
            }
270
        }
271
        private void btn_USBLocationx32Change_Click(object sender, EventArgs e)
272
        {
273
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
274
            {
275
                this.txt_USBMasterCopyLocation_x32.Text = folderBrowserDialog.SelectedPath;
276
                usbMasterx32CopyLocation = folderBrowserDialog.SelectedPath;
277
            }
278
        }
279
        private void btn_USBLocationx64Change_Click(object sender, EventArgs e)
280
        {
281
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
282
            {
283
                this.txt_USBMasterCopyLocation_x64.Text = folderBrowserDialog.SelectedPath;
284
                usbMasterx64CopyLocation = folderBrowserDialog.SelectedPath;
285
            }
286
        }
287
    }
288
}