PageRenderTime 35ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/SimpleProxySwitch/MainForm.cs

#
C# | 295 lines | 189 code | 45 blank | 61 comment | 40 complexity | 90964e45996b812b1f2325dddcc41282 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. //////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Project : ProxySwitch
  4. // Module : MainForm
  5. // Description : Mainform of the App.
  6. //
  7. // Repository : $URL$
  8. // Last changed by : $LastChangedBy$
  9. // Revision : $LastChangedRevision$
  10. // Last Changed : $LastChangedDate$
  11. // Author : $Author$
  12. //
  13. // Id : $Id$
  14. //
  15. // Copyright : (c) 2010 Torsten Bär
  16. //
  17. // Published under the MIT License. See license.rtf or http://www.opensource.org/licenses/mit-license.php.
  18. //
  19. //////////////////////////////////////////////////////////////////////////////////
  20. using System;
  21. using System.Collections.Generic;
  22. using System.ComponentModel;
  23. using System.Linq;
  24. using System.Reflection;
  25. using System.Windows.Forms;
  26. using log4net;
  27. using ProxySwitch.Properties;
  28. namespace ProxySwitch
  29. {
  30. /// <summary>
  31. /// Main dialog.
  32. /// </summary>
  33. public partial class MainForm : Form
  34. {
  35. private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="MainForm"/> class.
  38. /// </summary>
  39. public MainForm()
  40. {
  41. Log.Debug("Initializing components");
  42. InitializeComponent();
  43. Log.Debug("Initialized");
  44. }
  45. /// <summary>
  46. /// Raises the <see cref="E:System.Windows.Forms.Form.Load"/> event.
  47. /// </summary>
  48. /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
  49. protected override void OnLoad(EventArgs e)
  50. {
  51. Log.Debug("Loading last setting");
  52. if (!String.IsNullOrEmpty(Settings.Default.ProxySettings))
  53. {
  54. try
  55. {
  56. foreach (ProxySetting serializeableProxySetting in XmlSerializer<List<ProxySetting>>.Deserialize(Settings.Default.ProxySettings))
  57. listViewProxy.Items.Add(new ProxyListViewItem(serializeableProxySetting));
  58. }
  59. catch (Exception exception)
  60. {
  61. Log.Error("Error loading settings", exception);
  62. MessageBox.Show(exception.Message, "Error loading settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
  63. }
  64. Log.DebugFormat("Initialized with {0} settings", listViewProxy.Items.Count);
  65. }
  66. }
  67. /// <summary>
  68. /// Handles the Click event of the button control.
  69. /// </summary>
  70. /// <param name="sender">The source of the event.</param>
  71. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  72. private void button_Click(object sender, EventArgs e)
  73. {
  74. if (Log.IsDebugEnabled)
  75. {
  76. Control control = sender as Control;
  77. Log.DebugFormat("Handling click event of {0}", control != null ? control.Name : "no control");
  78. }
  79. if (sender == buttonNew)
  80. {
  81. Log.Debug("Adding new Proxy");
  82. using (EditProxyForm editProxyForm = new EditProxyForm())
  83. {
  84. Log.Debug("Showing proxy edit form");
  85. if (editProxyForm.ShowDialog(this) == DialogResult.OK)
  86. {
  87. Log.Debug("Proxy edir form successfully closed");
  88. listViewProxy.Items.Add(new ProxyListViewItem(editProxyForm.Setting));
  89. }
  90. else
  91. {
  92. Log.Debug("Proxy edit canceled");
  93. }
  94. }
  95. }
  96. else if(sender == buttonEdit)
  97. {
  98. Log.Debug("Editing current proxy setting");
  99. if(listViewProxy.SelectedItems != null)
  100. {
  101. ProxyListViewItem proxy = listViewProxy.SelectedItems.Cast<ProxyListViewItem>().FirstOrDefault();
  102. if(proxy != null)
  103. {
  104. Log.DebugFormat("Current selected proxy is {0} {1} {2}", proxy.Setting.Address, proxy.Setting.Network, proxy.Setting.Bypass);
  105. using (EditProxyForm proxyForm = new EditProxyForm(proxy.Setting))
  106. {
  107. Log.Debug("Opening edit form");
  108. if(proxyForm.ShowDialog(this) == DialogResult.OK)
  109. {
  110. Log.Debug("Edit proxy finished");
  111. proxy.UpdateSetting(proxyForm.Setting);
  112. }
  113. else
  114. {
  115. Log.Debug("Edit proxy canceled");
  116. }
  117. }
  118. }
  119. }
  120. else
  121. {
  122. Log.Debug("No setting selected");
  123. }
  124. }
  125. else if(sender == buttonDelete)
  126. {
  127. Log.Debug("Deleting selected setting");
  128. if(listViewProxy.SelectedItems != null)
  129. {
  130. ProxyListViewItem proxy = listViewProxy.SelectedItems
  131. .Cast<ProxyListViewItem>()
  132. .FirstOrDefault();
  133. if (proxy != null)
  134. {
  135. Log.DebugFormat("Deleting setting {0} {1} {2}", proxy.Setting.Address, proxy.Setting.Network, proxy.Setting.Bypass);
  136. listViewProxy.Items.Remove(proxy);
  137. }
  138. }
  139. }
  140. else if(sender == buttonApplyProxy)
  141. {
  142. Log.Debug("applying proxy");
  143. SetProxyToCurrentSelection();
  144. if (checkBoxAutoApply.Checked)
  145. ScheduleHelper.CreateSchedule();
  146. else
  147. ScheduleHelper.DisableSchedule();
  148. }
  149. else if (sender == buttonSaveApply || sender == buttonSave)
  150. {
  151. Log.Debug("Applying and saving setting");
  152. SaveCurrentSettings();
  153. if (sender == buttonSaveApply)
  154. {
  155. Log.Debug("Applying setting");
  156. Network.SetProxyForCurrentNetwork(listViewProxy.Items.Cast<ProxyListViewItem>().Where(item => item.Checked).Select(item => item.Setting));
  157. ShowBalloonTip();
  158. ScheduleHelper.CreateSchedule();
  159. }
  160. }
  161. Log.Debug("Event handled");
  162. }
  163. /// <summary>
  164. /// Raises the <see cref="E:System.Windows.Forms.Form.Closing"/> event.
  165. /// </summary>
  166. /// <param name="e">A <see cref="T:System.ComponentModel.CancelEventArgs"/> that contains the event data.</param>
  167. protected override void OnClosing(CancelEventArgs e)
  168. {
  169. Log.Debug("Form is closing");
  170. SaveCurrentSettings();
  171. }
  172. /// <summary>
  173. /// Handles the BalloonTipClosed event of the notifyIcon control.
  174. /// </summary>
  175. /// <param name="sender">The source of the event.</param>
  176. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  177. private void notifyIcon_BalloonTipClosed(object sender, EventArgs e)
  178. {
  179. notifyIcon.Visible = false;
  180. }
  181. /// <summary>
  182. /// Shows the ballon tip.
  183. /// </summary>
  184. private void ShowBalloonTip()
  185. {
  186. notifyIcon.Visible = true;
  187. notifyIcon.BalloonTipText = String.Format("Proxy {0}", Network.ProxyEnabled ? "set to " + Network.SystemProxy.Address : "disabled");
  188. notifyIcon.ShowBalloonTip((int)TimeSpan.FromSeconds(15).TotalMilliseconds);
  189. }
  190. /// <summary>
  191. /// Sets the proxy to current selection.
  192. /// </summary>
  193. private void SetProxyToCurrentSelection()
  194. {
  195. Log.Debug("Setting proxy to current selection");
  196. ProxyListViewItem selectedItem = listViewProxy.SelectedItems
  197. .Cast<ProxyListViewItem>()
  198. .FirstOrDefault();
  199. if (selectedItem != null)
  200. {
  201. SetProxy(selectedItem);
  202. }
  203. else
  204. {
  205. Log.Debug("No item selected");
  206. }
  207. }
  208. /// <summary>
  209. /// Sets the proxy.
  210. /// </summary>
  211. /// <param name="item">The item.</param>
  212. private void SetProxy(ProxyListViewItem item)
  213. {
  214. Log.DebugFormat("Setting proxy to {0} {1} {2}",
  215. item.Setting.Address, item.Setting.Network, item.Setting.Bypass);
  216. Network.SetProxy(item.Setting);
  217. ShowBalloonTip();
  218. }
  219. /// <summary>
  220. /// Saves the current settings.
  221. /// </summary>
  222. private void SaveCurrentSettings()
  223. {
  224. Log.Debug("Saving current setting");
  225. string serialize = XmlSerializer<List<ProxySetting>>
  226. .Serialize(listViewProxy.Items
  227. .Cast<ProxyListViewItem>()
  228. .Select(item => item.Setting)
  229. .ToList());
  230. Settings.Default.ProxySettings = serialize;
  231. Settings.Default.Save();
  232. Log.Debug("setting saved");
  233. }
  234. /// <summary>
  235. /// Handles the MouseDoubleClick event of the listViewProxy control.
  236. /// </summary>
  237. /// <param name="sender">The source of the event.</param>
  238. /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
  239. private void listViewProxy_MouseDoubleClick(object sender, MouseEventArgs e)
  240. {
  241. var item = listViewProxy.GetItemAt(e.Location.X, e.Location.Y);
  242. if (item != null)
  243. {
  244. var proxyListViewItem = item as ProxyListViewItem;
  245. if (proxyListViewItem != null)
  246. SetProxy(proxyListViewItem);
  247. }
  248. }
  249. }
  250. }