-
Notifications
You must be signed in to change notification settings - Fork 66
/
GlobalSettingsWindow.cs
152 lines (133 loc) · 5.58 KB
/
GlobalSettingsWindow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Windows.Forms;
using EasyConnect.Protocols;
namespace EasyConnect
{
/// <summary>
/// Displays the global settings for the application: the default protocol and whether they want the toolbar to auto-hide.
/// </summary>
public partial class GlobalSettingsWindow : Form
{
/// <summary>
/// If the user selected an encryption type of <see cref="EncryptionType.Rijndael"/>, this is the encryption key to use with that method.
/// </summary>
protected SecureString _encryptionPassword = null;
/// <summary>
/// Main application form for this window.
/// </summary>
protected MainForm _parentTabs = null;
/// <summary>
/// Default constructor; populates the protocol dropdown.
/// </summary>
public GlobalSettingsWindow()
{
InitializeComponent();
foreach (IProtocol protocol in ConnectionFactory.GetProtocols())
_defaultProtocolDropdown.Items.Add(protocol);
}
/// <summary>
/// If the user selected an encryption type of <see cref="EncryptionType.Rijndael"/>, this is the encryption key to use with that method.
/// </summary>
public SecureString EncryptionPassword
{
get
{
return _encryptionPassword;
}
}
/// <summary>
/// Handler method that's called when the form is closing; sets the properties for various settings and then saves them.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private async void GlobalSettingsWindow_FormClosing(object sender, FormClosingEventArgs e)
{
if (_parentTabs == null)
return;
await ConnectionFactory.SetDefaultProtocol((IProtocol) _defaultProtocolDropdown.SelectedItem);
GlobalSettings.Instance.AutoHideToolbar = _autoHideCheckbox.Checked;
GlobalSettings.Instance.EncryptionType = (EncryptionType) Enum.Parse(typeof (EncryptionType), ((ListItem) _encryptionTypeDropdown.SelectedItem).Value);
GlobalSettings.Instance.EnableAeroPeek = _enableAeroPeekCheckbox.Checked;
if (_parentTabs.AeroPeekEnabled != _enableAeroPeekCheckbox.Checked)
_parentTabs.AeroPeekEnabled = _enableAeroPeekCheckbox.Checked;
await GlobalSettings.Instance.Save();
}
/// <summary>
/// Handler method that's called when the form is shown; sets the state of <see cref="_autoHideCheckbox"/>.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private void GlobalSettingsWindow_Shown(object sender, EventArgs e)
{
_parentTabs = Parent.TopLevelControl as MainForm;
_autoHideCheckbox.Checked = GlobalSettings.Instance.AutoHideToolbar;
_enableAeroPeekCheckbox.Checked = GlobalSettings.Instance.EnableAeroPeek;
List<ListItem> items = new List<ListItem>
{
new ListItem
{
Text = "Password",
Value = "Rijndael"
},
new ListItem
{
Text = "RSA Key Container",
Value = "Rsa"
}
};
_encryptionTypeDropdown.Items.AddRange(items.Cast<object>().ToArray());
// ReSharper disable PossibleInvalidOperationException
_encryptionTypeDropdown.SelectedItem = items.First(i => i.Value == GlobalSettings.Instance.EncryptionType.Value.ToString("G"));
// ReSharper restore PossibleInvalidOperationException
}
/// <summary>
/// Handler method that's called when the selected item in <see cref="_encryptionTypeDropdown"/> is changed. If the user selected
/// <see cref="EncryptionType.Rijndael"/>, we prompt them to enter a password and save it to <see cref="_encryptionPassword"/>, otherwise we set
/// <see cref="_encryptionPassword"/> to null.
/// </summary>
/// <param name="sender">Object from which this event originated, <see cref="_encryptionTypeDropdown"/> in this case.</param>
/// <param name="e">Arguments associated with this event.</param>
private void _encryptionTypeDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
if ((_encryptionTypeDropdown.SelectedItem as ListItem).Value == "Rsa")
_encryptionPassword = null;
// ReSharper disable PossibleInvalidOperationException
else if ((_encryptionTypeDropdown.SelectedItem as ListItem).Value != GlobalSettings.Instance.EncryptionType.Value.ToString("G"))
// ReSharper restore PossibleInvalidOperationException
{
PasswordWindow passwordWindow = new PasswordWindow();
if (passwordWindow.ShowDialog(this) == DialogResult.OK)
_encryptionPassword = passwordWindow.Password;
}
}
/// <summary>
/// Class to provide text and value members to <see cref="ComboBox"/> instances.
/// </summary>
protected class ListItem
{
/// <summary>
/// Text that should be displayed for the item.
/// </summary>
public string Text
{
get;
set;
}
/// <summary>
/// Value that should be used for the item.
/// </summary>
public string Value
{
get;
set;
}
}
private async void GlobalSettingsWindow_Load(object sender, EventArgs e)
{
_defaultProtocolDropdown.SelectedItem = await ConnectionFactory.GetDefaultProtocol();
}
}
}