Menu

[r1586]: / trunk / GoogleContactsSync / ProxySettings.cs  Maximize  Restore  History

Download this file

213 lines (181 with data), 7.9 kB

  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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using Microsoft.Win32;
using System;
using System.Drawing;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace GoContactSyncMod
{
internal partial class ProxySettingsForm : Form
{
private static IWebProxy _systemProxy = new WebProxy();
private void Form_Changed(object sender, EventArgs e)
{
FormSettings();
}
public ProxySettingsForm()
{
/* Cannot set Font in designer as there is automatic sorting and Font will be set after AutoScaleDimensions
* This will prevent application to work correctly with high DPI systems. */
Font = new Font("Verdana", 8.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
InitializeComponent();
if (null == _systemProxy)
{
_systemProxy = WebRequest.DefaultWebProxy;
}
cbUseGlobalSettings.Visible = true;
}
private static void SetBgColor(TextBox box, bool isValid)
{
if (box.Enabled)
{
box.BackColor = !isValid ? Color.LightPink : Color.LightGreen;
}
}
private bool ValidCredentials
{
get
{
var userNameIsValid = Regex.IsMatch(UserName.Text, @"^(?'id'[a-z0-9\\\/\@\'\%\._\+\s\-]+)$", RegexOptions.IgnoreCase);
var passwordIsValid = !string.IsNullOrEmpty(Password.Text.Trim());
var AddressIsValid = Regex.IsMatch(Address.Text, @"^(?'url'[\w\d#@%;$()~_?\-\\\.&]+)$", RegexOptions.IgnoreCase);
var PortIsValid = Regex.IsMatch(Port.Text, @"^(?'port'[0-9]{2,6})$", RegexOptions.IgnoreCase);
SetBgColor(UserName, userNameIsValid);
SetBgColor(Password, passwordIsValid);
SetBgColor(Address, AddressIsValid);
SetBgColor(Port, PortIsValid);
return (((userNameIsValid && passwordIsValid) || !Authorization.Checked) && AddressIsValid && PortIsValid) || SystemProxy.Checked;
}
}
private void FormSettings()
{
Address.Enabled = CustomProxy.Checked;
Port.Enabled = CustomProxy.Checked;
Authorization.Enabled = CustomProxy.Checked;
UserName.Enabled = CustomProxy.Checked && Authorization.Checked;
Password.Enabled = CustomProxy.Checked && Authorization.Checked;
_ = ValidCredentials;
}
public void ProxySet()
{
if (CustomProxy.Checked && !string.IsNullOrEmpty(Address.Text))
{
try
{
var myProxy = new WebProxy(Address.Text);
if (!string.IsNullOrEmpty(Port.Text))
{
myProxy = new WebProxy(Address.Text, Convert.ToInt16(Port.Text));
}
myProxy.BypassProxyOnLocal = true;
myProxy.UseDefaultCredentials = true;
if (Authorization.Checked)
{
myProxy.Credentials = new NetworkCredential(UserName.Text, Password.Text);
}
WebRequest.DefaultWebProxy = myProxy;
}
catch (Exception ex)
{
ErrorHandler.Handle(ex);
}
}
else // to do set defaul system proxy
{
WebRequest.DefaultWebProxy = _systemProxy;
}
}
public void LoadSettings(string _profile)
{
var regKeyAppRoot = Registry.CurrentUser.CreateSubKey(SettingsForm.AppRootKey);
if (null != regKeyAppRoot.GetValue("UseGlobalProxySettings"))
{
cbUseGlobalSettings.Checked = Convert.ToBoolean(regKeyAppRoot.GetValue("UseGlobalProxySettings"));
}
regKeyAppRoot = Registry.CurrentUser.CreateSubKey(SettingsForm.AppRootKey + (_profile != null && !cbUseGlobalSettings.Checked ? ('\\' + _profile) : ""));
if (regKeyAppRoot.GetValue("ProxyUsage") != null && Convert.ToBoolean(regKeyAppRoot.GetValue("ProxyUsage")))
{
CustomProxy.Checked = true;
SystemProxy.Checked = !CustomProxy.Checked;
}
if (regKeyAppRoot.GetValue("ProxyURL") != null)
{
Address.Text = (string)regKeyAppRoot.GetValue("ProxyURL");
}
if (regKeyAppRoot.GetValue("ProxyPort") != null)
{
Port.Text = (string)regKeyAppRoot.GetValue("ProxyPort");
}
if (regKeyAppRoot.GetValue("ProxyAuth") != null && Convert.ToBoolean(regKeyAppRoot.GetValue("ProxyAuth")))
{
Authorization.Checked = true;
if (regKeyAppRoot.GetValue("ProxyUsername") != null)
{
UserName.Text = regKeyAppRoot.GetValue("ProxyUsername") as string;
if (regKeyAppRoot.GetValue("ProxyPassword") != null)
{
Password.Text = Encryption.DecryptPassword(UserName.Text, regKeyAppRoot.GetValue("ProxyPassword") as string);
}
}
}
FormSettings();
ProxySet();
}
public void ClearSettings()
{
if (!cbUseGlobalSettings.Checked)
{
SystemProxy.Checked = true;
CustomProxy.Checked = Authorization.Checked = !SystemProxy.Checked;
Address.Text = Port.Text = UserName.Text = Password.Text;
}
}
public void SaveSettings(string _profile)
{
var regKeyAppRoot = Registry.CurrentUser.CreateSubKey(SettingsForm.AppRootKey);
regKeyAppRoot.SetValue("UseGlobalProxySettings", cbUseGlobalSettings.Checked);
regKeyAppRoot = Registry.CurrentUser.CreateSubKey(SettingsForm.AppRootKey + (_profile != null && !cbUseGlobalSettings.Checked ? ('\\' + _profile) : ""));
regKeyAppRoot.SetValue("ProxyUsage", CustomProxy.Checked);
if (CustomProxy.Checked)
{
if (!string.IsNullOrEmpty(Address.Text))
{
regKeyAppRoot.SetValue("ProxyURL", Address.Text);
if (!string.IsNullOrEmpty(Port.Text))
{
regKeyAppRoot.SetValue("ProxyPort", Port.Text);
}
}
regKeyAppRoot.SetValue("ProxyAuth", Authorization.Checked);
if (Authorization.Checked)
{
if (!string.IsNullOrEmpty(UserName.Text))
{
regKeyAppRoot.SetValue("ProxyUsername", UserName.Text);
if (!string.IsNullOrEmpty(Password.Text))
{
regKeyAppRoot.SetValue("ProxyPassword", Encryption.EncryptPassword(UserName.Text, Password.Text));
}
}
}
}
}
private void CancelButton_Click(object sender, EventArgs e)
{
if (!ValidCredentials)
{
SystemProxy.Checked = true;
}
Hide();
}
private void OkButton_Click(object sender, EventArgs e)
{
if (!ValidCredentials)
{
return;
}
ProxySet();
Hide();
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.