/USBHelperLauncher/HostsDialog.cs

https://github.com/FailedShack/USBHelperLauncher · C# · 192 lines · 171 code · 21 blank · 0 comment · 15 complexity · 66ec4a82c070ab015608a1ca7a950300 MD5 · raw file

  1. using System;
  2. using System.ComponentModel;
  3. using System.Data;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text.RegularExpressions;
  8. using System.Windows.Forms;
  9. namespace USBHelperLauncher
  10. {
  11. public partial class HostsDialog : Form
  12. {
  13. private bool PassedValidation { get; set; }
  14. private bool Modified { get; set; }
  15. private Hosts Hosts { get; set; }
  16. private string originalTitle;
  17. private DataTable dt;
  18. public HostsDialog()
  19. {
  20. InitializeComponent();
  21. dataGridView.RowValidated += DataGridView_RowValidated;
  22. dataGridView.CellValidating += DataGridView_CellValidating;
  23. dataGridView.CellValueChanged += DataGridView_CellValueChanged;
  24. originalTitle = Text;
  25. dt = new DataTable();
  26. dt.Columns.Add("Hostname", typeof(string));
  27. dt.Columns.Add("IP Address", typeof(string));
  28. dataGridView.DataSource = dt;
  29. }
  30. private void DataGridView_RowValidated(object sender, DataGridViewCellEventArgs e)
  31. {
  32. PassedValidation = true;
  33. }
  34. private void DataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  35. {
  36. Modified = true;
  37. Text = originalTitle + " (Unsaved)";
  38. }
  39. private void DataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
  40. {
  41. dataGridView.Rows[e.RowIndex].ErrorText = "";
  42. string value = (string)e.FormattedValue;
  43. switch (e.ColumnIndex)
  44. {
  45. case 0:
  46. if (value == string.Empty || Regex.IsMatch(value, @"\s"))
  47. {
  48. e.Cancel = true;
  49. dataGridView.Rows[e.RowIndex].ErrorText = "Invalid hostname";
  50. PassedValidation = false;
  51. }
  52. else
  53. {
  54. foreach (DataGridViewRow row in dataGridView.Rows)
  55. {
  56. if (row.Index != e.RowIndex && string.Equals((string)row.Cells[0].FormattedValue, value, StringComparison.OrdinalIgnoreCase))
  57. {
  58. e.Cancel = true;
  59. dataGridView.Rows[e.RowIndex].ErrorText = "Hostname already defined";
  60. PassedValidation = false;
  61. break;
  62. }
  63. }
  64. }
  65. break;
  66. case 1:
  67. if (!ValidateIPv4(value))
  68. {
  69. e.Cancel = true;
  70. dataGridView.Rows[e.RowIndex].ErrorText = "Invalid IP Address";
  71. PassedValidation = false;
  72. }
  73. break;
  74. }
  75. }
  76. private void openButton_Click(object sender, EventArgs e)
  77. {
  78. openFileDialog.ShowDialog();
  79. }
  80. private void loadToolStripMenuItem_Click(object sender, EventArgs e)
  81. {
  82. openFileDialog.ShowDialog();
  83. }
  84. private void saveToolStripMenuItem_Click(object sender, EventArgs e)
  85. {
  86. if (CanSave())
  87. {
  88. SaveHosts(Hosts);
  89. Hosts.Save(Program.GetHostsFile());
  90. }
  91. }
  92. private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
  93. {
  94. if (CanSave())
  95. {
  96. saveFileDialog.ShowDialog();
  97. }
  98. }
  99. private void saveButton_Click(object sender, EventArgs e)
  100. {
  101. if (CanSave())
  102. {
  103. SaveHosts(Hosts);
  104. Hosts.Save(Program.GetHostsFile());
  105. }
  106. }
  107. private void saveFileDialog_FileOk(object sender, CancelEventArgs e)
  108. {
  109. Hosts hosts = new Hosts();
  110. SaveHosts(hosts);
  111. hosts.Save(saveFileDialog.FileName);
  112. }
  113. private void openFileDialog_FileOk(object sender, CancelEventArgs e)
  114. {
  115. Hosts hosts = Hosts.Load(openFileDialog.FileName);
  116. LoadHosts(hosts);
  117. }
  118. private void SaveHosts(Hosts hosts)
  119. {
  120. hosts.Clear();
  121. foreach (DataRow row in dt.Rows)
  122. {
  123. hosts.Add(row.Field<string>(0), IPAddress.Parse(row.Field<string>(1)));
  124. }
  125. Modified = false;
  126. Text = originalTitle;
  127. }
  128. private void LoadHosts(Hosts hosts)
  129. {
  130. PassedValidation = false;
  131. dt.Clear();
  132. foreach (string host in hosts.GetHosts())
  133. {
  134. dt.Rows.Add(host, hosts.Get(host).ToString());
  135. }
  136. }
  137. private bool CanSave()
  138. {
  139. if (!PassedValidation)
  140. {
  141. MessageBox.Show("You must properly fill in all fields before saving.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  142. return false;
  143. }
  144. return true;
  145. }
  146. private static bool ValidateIPv4(string ipString)
  147. {
  148. if (string.IsNullOrWhiteSpace(ipString))
  149. {
  150. return false;
  151. }
  152. string[] splitValues = ipString.Split('.');
  153. if (splitValues.Length != 4)
  154. {
  155. return false;
  156. }
  157. return splitValues.All(r => byte.TryParse(r, out _));
  158. }
  159. private void HostsDialog_Load(object sender, EventArgs e)
  160. {
  161. Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
  162. Hosts = Program.Hosts;
  163. LoadHosts(Hosts);
  164. }
  165. private void clearButton_Click(object sender, EventArgs e)
  166. {
  167. PassedValidation = false;
  168. dt.Clear();
  169. }
  170. }
  171. }