corrade-vassal – Blame information for rev 20

Subversion Repositories:
Rev:
Rev Author Line No. Line
7 zed 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6  
7 using System;
2 zed 8 using System.Drawing;
9 using System.Linq;
16 eva 10 using System.Net;
11 using System.Net.Http.Headers;
7 zed 12 using System.Net.Sockets;
13 using System.Reflection;
2 zed 14 using System.Threading;
15 using System.Windows.Forms;
16 using OpenMetaverse;
16 eva 17 using wasSharp;
2 zed 18  
19 namespace Vassal
20 {
21 public partial class SettingsForm : Form
22 {
23 private static SettingsForm mainForm;
24  
25 private readonly Action GetUserConfiguration = () =>
26 {
27 // general
28 mainForm.HTTPServerURL.Text = Vassal.vassalConfiguration.HTTPServerURL;
29 mainForm.Group.Text = Vassal.vassalConfiguration.Group;
30 mainForm.Password.Text = Vassal.vassalConfiguration.Password;
31 mainForm.TeleportTimeout.Text = Vassal.vassalConfiguration.TeleportTimeout.ToString(Utils.EnUsCulture);
16 eva 32 mainForm.ServicesTimeout.Text = Vassal.vassalConfiguration.ServicesTimeout.ToString(Utils.EnUsCulture);
5 eva 33 mainForm.RegionRestartDelay.Text = Vassal.vassalConfiguration.RegionRestartDelay.ToString(Utils.EnUsCulture);
2 zed 34  
35 // filters
36 mainForm.ActiveInputFilters.Items.Clear();
16 eva 37 foreach (var filter in Vassal.vassalConfiguration.InputFilters)
2 zed 38 {
39 mainForm.ActiveInputFilters.Items.Add(new ListViewItem
40 {
16 eva 41 Text = Reflection.GetDescriptionFromEnumValue(filter),
2 zed 42 Tag = filter
43 });
44 }
45 mainForm.ActiveOutputFilters.Items.Clear();
46 mainForm.ActiveInputFilters.DisplayMember = "Text";
16 eva 47 foreach (var filter in Vassal.vassalConfiguration.OutputFilters)
2 zed 48 {
49 mainForm.ActiveOutputFilters.Items.Add(new ListViewItem
50 {
16 eva 51 Text = Reflection.GetDescriptionFromEnumValue(filter),
2 zed 52 Tag = filter
53 });
54 }
55 mainForm.ActiveOutputFilters.DisplayMember = "Text";
56  
57 // cryptography
58 mainForm.ENIGMARotorSequence.Items.Clear();
16 eva 59 foreach (var rotor in Vassal.vassalConfiguration.ENIGMA.rotors)
2 zed 60 {
61 mainForm.ENIGMARotorSequence.Items.Add(new ListViewItem
62 {
63 Text = rotor.ToString(),
64 Tag = rotor
65 });
66 }
67 mainForm.ENIGMARotorSequence.DisplayMember = "Text";
68 mainForm.ENIGMAPlugSequence.Items.Clear();
16 eva 69 foreach (var plug in Vassal.vassalConfiguration.ENIGMA.plugs)
2 zed 70 {
71 mainForm.ENIGMAPlugSequence.Items.Add(new ListViewItem
72 {
73 Text = plug.ToString(),
74 Tag = plug
75 });
76 }
77 mainForm.ENIGMAPlugSequence.DisplayMember = "Text";
78 mainForm.ENIGMAReflector.Text = Vassal.vassalConfiguration.ENIGMA.reflector.ToString();
79 mainForm.VIGENERESecret.Text = Vassal.vassalConfiguration.VIGENERESecret;
16 eva 80  
81 // HTTP
82 string mediaType;
83 switch (Vassal.vassalConfiguration.InputFilters.LastOrDefault())
84 {
85 case Filter.RFC1738:
86 mediaType = @"application/x-www-form-urlencoded";
87 break;
88 default:
89 mediaType = @"text/plain";
90 break;
91 }
20 office 92 Vassal.HTTPClient = new Web.wasHTTPClient(new ProductInfoHeaderValue(@"Vassal", Vassal.VASSAL_VERSION), mediaType, 60000);
2 zed 93 };
94  
8 eva 95 private readonly Action SetUserConfiguration = () =>
96 {
97 // general
98 Vassal.vassalConfiguration.HTTPServerURL = mainForm.HTTPServerURL.Text;
99 Vassal.vassalConfiguration.Group = mainForm.Group.Text;
100 Vassal.vassalConfiguration.Password = mainForm.Password.Text;
101 uint outUint;
16 eva 102 if (uint.TryParse(mainForm.ServicesTimeout.Text, out outUint))
103 {
104 Vassal.vassalConfiguration.ServicesTimeout = outUint;
105 }
8 eva 106 if (uint.TryParse(mainForm.TeleportTimeout.Text, out outUint))
107 {
108 Vassal.vassalConfiguration.TeleportTimeout = outUint;
109 }
110 if (uint.TryParse(mainForm.RegionRestartDelay.Text, out outUint))
111 {
112 Vassal.vassalConfiguration.RegionRestartDelay = outUint;
113 }
114  
115 // filters
116 Vassal.vassalConfiguration.InputFilters =
117 mainForm.ActiveInputFilters.Items.Cast<ListViewItem>().Select(o => (Filter) o.Tag).ToList();
118 Vassal.vassalConfiguration.OutputFilters =
119 mainForm.ActiveOutputFilters.Items.Cast<ListViewItem>().Select(o => (Filter) o.Tag).ToList();
120  
121 // cryptography
122 Vassal.vassalConfiguration.ENIGMA = new ENIGMA
123 {
124 rotors = mainForm.ENIGMARotorSequence.Items.Cast<ListViewItem>().Select(o => (char) o.Tag).ToArray(),
125 plugs = mainForm.ENIGMAPlugSequence.Items.Cast<ListViewItem>().Select(o => (char) o.Tag).ToArray(),
126 reflector = mainForm.ENIGMAReflector.Text[0]
127 };
128  
129 Vassal.vassalConfiguration.VIGENERESecret = mainForm.VIGENERESecret.Text;
16 eva 130  
131 // HTTP
132 string mediaType;
133 switch (Vassal.vassalConfiguration.InputFilters.LastOrDefault())
134 {
135 case Filter.RFC1738:
136 mediaType = @"application/x-www-form-urlencoded";
137 break;
138 default:
139 mediaType = @"text/plain";
140 break;
141 }
142 // Create HTTP Client
143 Vassal.HTTPClient = new Web.wasHTTPClient(new ProductInfoHeaderValue(@"Vassal",
20 office 144 Vassal.VASSAL_VERSION), mediaType, 60000);
8 eva 145 };
146  
2 zed 147 public SettingsForm()
148 {
149 InitializeComponent();
150 mainForm = this;
151 }
152  
153 private void LoadSettingsRequested(object sender, EventArgs e)
154 {
8 eva 155 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 156 {
157 switch (mainForm.LoadSettingsDialog.ShowDialog())
158 {
159 case DialogResult.OK:
16 eva 160 var file = mainForm.LoadSettingsDialog.FileName;
2 zed 161 new Thread(() =>
162 {
8 eva 163 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 164 {
165 try
166 {
167 mainForm.StatusText.Text = @"loading settings...";
168 mainForm.StatusProgress.Value = 0;
169  
170 // load settings
171 VassalConfiguration.Load(file, ref Vassal.vassalConfiguration);
172 mainForm.StatusProgress.Value = 50;
173 GetUserConfiguration.Invoke();
174  
175 mainForm.StatusText.Text = @"settings loaded";
176 mainForm.StatusProgress.Value = 100;
177 }
178 catch (Exception ex)
179 {
180 mainForm.StatusText.Text = ex.Message;
181 }
182 }));
183 })
8 eva 184 {IsBackground = true, Priority = ThreadPriority.Normal}.Start();
2 zed 185 break;
186 }
187 }));
188 }
189  
190 private void SaveSettingsRequested(object sender, EventArgs e)
191 {
8 eva 192 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 193 {
194 switch (mainForm.SaveSettingsDialog.ShowDialog())
195 {
196 case DialogResult.OK:
16 eva 197 var file = mainForm.SaveSettingsDialog.FileName;
2 zed 198 new Thread(() =>
199 {
8 eva 200 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 201 {
202 try
203 {
204 mainForm.StatusText.Text = @"saving settings...";
205 mainForm.StatusProgress.Value = 0;
206  
207 // apply configuration
208 SetUserConfiguration.Invoke();
209 mainForm.StatusProgress.Value = 50;
210  
211 // save settings
212 VassalConfiguration.Save(file, ref Vassal.vassalConfiguration);
213  
214 mainForm.StatusText.Text = @"settings saved";
215 mainForm.StatusProgress.Value = 100;
216 }
217 catch (Exception ex)
218 {
219 mainForm.StatusText.Text = ex.Message;
220 }
221 }));
222 })
8 eva 223 {IsBackground = true, Priority = ThreadPriority.Normal}.Start();
2 zed 224 break;
225 }
226 }));
227 }
228  
229 private void AddInputDecoderRequested(object sender, EventArgs e)
230 {
8 eva 231 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 232 {
233 if (string.IsNullOrEmpty(InputDecode.Text))
234 {
235 InputDecode.BackColor = Color.MistyRose;
236 return;
237 }
238 InputDecode.BackColor = Color.Empty;
239 ActiveInputFilters.Items.Add(new ListViewItem
240 {
241 Text = InputDecode.Text,
20 office 242 Tag = Reflection.GetEnumValueFromName<Filter>(InputDecode.Text)
2 zed 243 });
244 }));
245 }
246  
247 private void AddInputDecryptionRequested(object sender, EventArgs e)
248 {
8 eva 249 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 250 {
251 if (string.IsNullOrEmpty(InputDecryption.Text))
252 {
253 InputDecryption.BackColor = Color.MistyRose;
254 return;
255 }
256 InputDecryption.BackColor = Color.Empty;
257 ActiveInputFilters.Items.Add(new ListViewItem
258 {
259 Text = InputDecryption.Text,
20 office 260 Tag = Reflection.GetEnumValueFromName<Filter>(InputDecryption.Text)
2 zed 261 });
262 }));
263 }
264  
265 private void AddOutputEncryptionRequested(object sender, EventArgs e)
266 {
8 eva 267 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 268 {
269 if (string.IsNullOrEmpty(OutputEncrypt.Text))
270 {
271 OutputEncrypt.BackColor = Color.MistyRose;
272 return;
273 }
274 OutputEncrypt.BackColor = Color.Empty;
275 ActiveOutputFilters.Items.Add(new ListViewItem
276 {
277 Text = OutputEncrypt.Text,
20 office 278 Tag = Reflection.GetEnumValueFromName<Filter>(OutputEncrypt.Text)
2 zed 279 });
280 }));
281 }
282  
283 private void AddOutputEncoderRequested(object sender, EventArgs e)
284 {
8 eva 285 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 286 {
287 if (string.IsNullOrEmpty(OutputEncode.Text))
288 {
289 OutputEncode.BackColor = Color.MistyRose;
290 return;
291 }
292 OutputEncode.BackColor = Color.Empty;
293 ActiveOutputFilters.Items.Add(new ListViewItem
294 {
295 Text = OutputEncode.Text,
20 office 296 Tag = Reflection.GetEnumValueFromName<Filter>(OutputEncode.Text)
2 zed 297 });
298 }));
299 }
300  
301 private void DeleteSelectedOutputFilterRequested(object sender, EventArgs e)
302 {
8 eva 303 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 304 {
16 eva 305 var listViewItem = ActiveOutputFilters.SelectedItem as ListViewItem;
2 zed 306 if (listViewItem == null)
307 {
308 ActiveOutputFilters.BackColor = Color.MistyRose;
309 return;
310 }
311 ActiveOutputFilters.BackColor = Color.Empty;
312 ActiveOutputFilters.Items.RemoveAt(ActiveOutputFilters.SelectedIndex);
313 }));
314 }
315  
316 private void DeleteSelectedInputFilterRequested(object sender, EventArgs e)
317 {
8 eva 318 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 319 {
16 eva 320 var listViewItem = ActiveInputFilters.SelectedItem as ListViewItem;
2 zed 321 if (listViewItem == null)
322 {
323 ActiveInputFilters.BackColor = Color.MistyRose;
324 return;
325 }
326 ActiveInputFilters.BackColor = Color.Empty;
327 ActiveInputFilters.Items.RemoveAt(ActiveInputFilters.SelectedIndex);
328 }));
329 }
330  
331 private void AddENIGMARotorRequested(object sender, EventArgs e)
332 {
8 eva 333 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 334 {
335 if (string.IsNullOrEmpty(ENIGMARotor.Text))
336 {
337 ENIGMARotor.BackColor = Color.MistyRose;
338 return;
339 }
340 ENIGMARotor.BackColor = Color.Empty;
341 ENIGMARotorSequence.Items.Add(new ListViewItem
342 {
343 Text = ENIGMARotor.Text,
344 Tag = ENIGMARotor.Text[0]
345 });
346 }));
347 }
348  
349 private void DeleteENIGMARotorRequested(object sender, EventArgs e)
350 {
8 eva 351 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 352 {
16 eva 353 var listViewItem = ENIGMARotorSequence.SelectedItem as ListViewItem;
2 zed 354 if (listViewItem == null)
355 {
356 ENIGMARotorSequence.BackColor = Color.MistyRose;
357 return;
358 }
359 ENIGMARotorSequence.BackColor = Color.Empty;
360 ENIGMARotorSequence.Items.RemoveAt(ENIGMARotorSequence.SelectedIndex);
361 }));
362 }
363  
364 private void AddENIGMAPlugRequested(object sender, EventArgs e)
365 {
8 eva 366 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 367 {
368 if (string.IsNullOrEmpty(ENIGMARing.Text))
369 {
370 ENIGMARing.BackColor = Color.MistyRose;
371 return;
372 }
373 ENIGMARing.BackColor = Color.Empty;
374 ENIGMAPlugSequence.Items.Add(new ListViewItem
375 {
376 Text = ENIGMARing.Text,
377 Tag = ENIGMARing.Text[0]
378 });
379 }));
380 }
381  
382 private void DeleteENIGMAPlugRequested(object sender, EventArgs e)
383 {
8 eva 384 mainForm.BeginInvoke((MethodInvoker) (() =>
2 zed 385 {
16 eva 386 var listViewItem = ENIGMAPlugSequence.SelectedItem as ListViewItem;
2 zed 387 if (listViewItem == null)
388 {
389 ENIGMAPlugSequence.BackColor = Color.MistyRose;
390 return;
391 }
392 ENIGMAPlugSequence.BackColor = Color.Empty;
393 ENIGMAPlugSequence.Items.RemoveAt(ENIGMAPlugSequence.SelectedIndex);
394 }));
395 }
396  
397 private void SettingsFormShown(object sender, EventArgs e)
398 {
399 GetUserConfiguration.Invoke();
400 }
5 eva 401  
402 private void SettingsFormClosing(object sender, FormClosingEventArgs e)
403 {
404 // apply configuration
405 SetUserConfiguration.Invoke();
406 // save settings
407 VassalConfiguration.Save(Vassal.VASSAL_CONSTANTS.VASSAL_CONFIGURATION_FILE, ref Vassal.vassalConfiguration);
7 zed 408 // Spawn a thread to check Corrade's connection status.
409 new Thread(() =>
410 {
16 eva 411 var tcpClient = new TcpClient();
7 zed 412 try
413 {
16 eva 414 var uri = new Uri(Vassal.vassalConfiguration.HTTPServerURL);
7 zed 415 tcpClient.Connect(uri.Host, uri.Port);
416 // port open
8 eva 417 Vassal.vassalForm.BeginInvoke((MethodInvoker) (() => { Vassal.vassalForm.Tabs.Enabled = true; }));
7 zed 418 // set the loading spinner
8 eva 419 if (Vassal.vassalForm.RegionAvatarsMap.Image == null)
7 zed 420 {
16 eva 421 var thisAssembly = Assembly.GetExecutingAssembly();
422 var file =
8 eva 423 thisAssembly.GetManifestResourceStream("Vassal.img.loading.gif");
424 switch (file != null)
425 {
426 case true:
427 Vassal.vassalForm.BeginInvoke((MethodInvoker) (() =>
428 {
429 Vassal.vassalForm.RegionAvatarsMap.SizeMode = PictureBoxSizeMode.CenterImage;
430 Vassal.vassalForm.RegionAvatarsMap.Image = Image.FromStream(file);
431 Vassal.vassalForm.RegionAvatarsMap.Refresh();
432 }));
433 break;
434 }
7 zed 435 }
436 }
437 catch (Exception)
438 {
439 // port closed
8 eva 440 Vassal.vassalForm.BeginInvoke((MethodInvoker) (() => { Vassal.vassalForm.Tabs.Enabled = false; }));
7 zed 441 }
442 })
8 eva 443 {IsBackground = true}.Start();
444  
445 // set parameters for Vassal
13 eva 446 mainForm.Invoke((MethodInvoker) (() =>
8 eva 447 {
13 eva 448 Vassal.vassalForm.Invoke((MethodInvoker) (() =>
8 eva 449 {
450 if (string.IsNullOrEmpty(Vassal.vassalForm.RegionRestartDelayBox.Text))
451 {
452 Vassal.vassalForm.RegionRestartDelayBox.Text = mainForm.RegionRestartDelay.Text;
453 }
454 Vassal.vassalForm.VassalStatusGroup.Enabled = true;
455 }));
456 }));
5 eva 457 }
16 eva 458  
459 private void ShowToolTip(object sender, EventArgs e)
460 {
461 mainForm.BeginInvoke(
462 (Action) (() =>
463 {
464 var pictureBox = sender as PictureBox;
465 if (pictureBox != null)
466 {
467 toolTip1.Show(toolTip1.GetToolTip(pictureBox), pictureBox);
468 }
469 }));
470 }
2 zed 471 }
8 eva 472 }