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