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