Subversion Repositories Code-Repo

Rev

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