PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/CmsCheckin/Login.cs

https://bitbucket.org/mahalowe/bvcms
C# | 518 lines | 450 code | 65 blank | 3 comment | 53 complexity | 46fc36def38b8760092bf408f3e484fc MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception, AGPL-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Net;
  10. using System.Xml.Linq;
  11. using System.Drawing.Printing;
  12. using System.Xml.Serialization;
  13. using System.IO;
  14. using CmsCheckin.Classes;
  15. namespace CmsCheckin
  16. {
  17. public partial class Login : Form
  18. {
  19. public Login()
  20. {
  21. InitializeComponent();
  22. b1.Click += buttonclick;
  23. b2.Click += buttonclick;
  24. b3.Click += buttonclick;
  25. b4.Click += buttonclick;
  26. b5.Click += buttonclick;
  27. b6.Click += buttonclick;
  28. b7.Click += buttonclick;
  29. b8.Click += buttonclick;
  30. b9.Click += buttonclick;
  31. b0.Click += buttonclick;
  32. bdash.Click += buttonclick;
  33. bequal.Click += buttonclick;
  34. bq.Click += buttonclick;
  35. bw.Click += buttonclick;
  36. be.Click += buttonclick;
  37. br.Click += buttonclick;
  38. bt.Click += buttonclick;
  39. by.Click += buttonclick;
  40. bu.Click += buttonclick;
  41. bi.Click += buttonclick;
  42. bo.Click += buttonclick;
  43. bp.Click += buttonclick;
  44. blbrace.Click += buttonclick;
  45. brbrace.Click += buttonclick;
  46. ba.Click += buttonclick;
  47. bs.Click += buttonclick;
  48. bd.Click += buttonclick;
  49. bf.Click += buttonclick;
  50. bg.Click += buttonclick;
  51. bh.Click += buttonclick;
  52. bj.Click += buttonclick;
  53. bk.Click += buttonclick;
  54. bl.Click += buttonclick;
  55. bz.Click += buttonclick;
  56. bx.Click += buttonclick;
  57. bc.Click += buttonclick;
  58. bv.Click += buttonclick;
  59. bb.Click += buttonclick;
  60. bn.Click += buttonclick;
  61. bm.Click += buttonclick;
  62. bcomma.Click += buttonclick;
  63. bdot.Click += buttonclick;
  64. bslash.Click += buttonclick;
  65. //username.KeyPress += textBox_KeyPress;
  66. //password.KeyPress += textBox_KeyPress;
  67. //URL.KeyPress += textBox_KeyPress;
  68. username.Enter += textbox_Enter;
  69. password.Enter += textbox_Enter;
  70. URL.Enter += textbox_Enter;
  71. building.Enter += textbox_Enter;
  72. PrintKiosks.Enter += textbox_Enter;
  73. PrinterWidth.Enter += textbox_Enter;
  74. PrinterHeight.Enter += textbox_Enter;
  75. password.Focus();
  76. }
  77. public XDocument campuses { get; set; }
  78. private void button1_Click(object sender, EventArgs e)
  79. {
  80. Settings1.Default.URL = URL.Text;
  81. Settings1.Default.username = username.Text;
  82. Settings1.Default.Kiosks = PrintKiosks.Text;
  83. Settings1.Default.PrintMode = PrintMode.Text;
  84. Settings1.Default.Printer = Printer.Text;
  85. Settings1.Default.DisableLocationLabels = DisableLocationLabels.Checked;
  86. Settings1.Default.BuildingMode = BuildingAccessMode.Checked;
  87. Settings1.Default.Building = building.Text;
  88. Settings1.Default.PrinterWidth = PrinterWidth.Text;
  89. Settings1.Default.PrinterHeight = PrinterHeight.Text;
  90. Settings1.Default.AdvancedPageSize = AdvancedPageSize.Checked;
  91. Settings1.Default.UseSSL = UseSSL.Checked;
  92. Settings1.Default.Save();
  93. if(URL.Text.StartsWith("localhost") || !UseSSL.Checked)
  94. Program.URL = "http://" + URL.Text;
  95. else if (Settings1.Default.UseSSL)
  96. Program.URL = "https://" + URL.Text;
  97. else
  98. Program.URL = "http://" + URL.Text;
  99. Program.Username = username.Text;
  100. Program.Password = password.Text;
  101. Program.PrinterWidth = PrinterWidth.Text;
  102. Program.PrinterHeight = PrinterHeight.Text;
  103. Program.DisableLocationLabels = DisableLocationLabels.Checked;
  104. if (BuildingAccessMode.Checked == true)
  105. {
  106. try
  107. {
  108. Program.Building = building.Text;
  109. Program.BuildingInfo = Util.FetchBuildingInfo();
  110. if (Program.BuildingInfo.Activities.Count == 0)
  111. {
  112. CancelClose = true;
  113. return;
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. MessageBox.Show("cannot find " + Program.URL);
  119. CancelClose = true;
  120. throw;
  121. }
  122. }
  123. var wc = Util.CreateWebClient();
  124. try
  125. {
  126. var url = new Uri(new Uri(Program.URL), "Checkin2/Campuses");
  127. var str = wc.DownloadString(url);
  128. if (str == "not authorized")
  129. {
  130. MessageBox.Show(str);
  131. CancelClose = true;
  132. return;
  133. }
  134. campuses = XDocument.Parse(str);
  135. }
  136. catch (WebException ex)
  137. {
  138. MessageBox.Show("cannot find " + Program.URL);
  139. CancelClose = true;
  140. }
  141. if (CancelClose == false && !BuildingAccessMode.Checked)
  142. {
  143. bool bHorizontalCheck = false;
  144. bool bVerticalCheck = false;
  145. if (AdvancedPageSize.Checked)
  146. {
  147. if (PrinterWidth.Text.Length == 0) PrinterWidth.Text = "0";
  148. if (PrinterHeight.Text.Length == 0) PrinterHeight.Text = "0";
  149. bHorizontalCheck = int.Parse( PrinterWidth.Text ) >= 290;
  150. bVerticalCheck = (int.Parse(PrinterHeight.Text) > 70 && int.Parse(PrinterHeight.Text) < 130) ||
  151. (int.Parse(PrinterHeight.Text) > 170 && int.Parse(PrinterHeight.Text) < 230);
  152. }
  153. else
  154. {
  155. bHorizontalCheck = PrinterHelper.getPrinterWidth(Printer.Text) > 290;
  156. bVerticalCheck = (PrinterHelper.getPrinterHeight(Printer.Text) > 70 && PrinterHelper.getPrinterHeight(Printer.Text) < 130) ||
  157. (PrinterHelper.getPrinterHeight(Printer.Text) > 170 && PrinterHelper.getPrinterHeight(Printer.Text) < 230);
  158. }
  159. if (!bHorizontalCheck || !bVerticalCheck)
  160. {
  161. if (MessageBox.Show("The selected printer does not have a valid page size. Do you want to continue?", "Printer Configuration Error", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
  162. {
  163. CancelClose = true;
  164. }
  165. }
  166. }
  167. if (CancelClose == false)
  168. {
  169. this.Hide();
  170. }
  171. }
  172. TextBox current = null;
  173. private void Login_Load(object sender, EventArgs e)
  174. {
  175. password.Focus();
  176. var prtdoc = new PrintDocument();
  177. var defp = prtdoc.PrinterSettings.PrinterName;
  178. foreach (var s in PrinterSettings.InstalledPrinters)
  179. Printer.Items.Add(s);
  180. Printer.SelectedIndex = Printer.FindStringExact(defp);
  181. if (Settings1.Default.Printer.HasValue())
  182. Printer.SelectedIndex = Printer.FindStringExact(Settings1.Default.Printer);
  183. DisableLocationLabels.Checked = Settings1.Default.DisableLocationLabels;
  184. BuildingAccessMode.Checked = Settings1.Default.BuildingMode;
  185. URL.Text = Settings1.Default.URL;
  186. username.Text = Settings1.Default.username;
  187. PrintKiosks.Text = Settings1.Default.Kiosks;
  188. PrintMode.Text = Settings1.Default.PrintMode;
  189. building.Text = Settings1.Default.Building;
  190. AdvancedPageSize.Checked = Settings1.Default.AdvancedPageSize;
  191. PrinterWidth.Text = Settings1.Default.PrinterWidth;
  192. PrinterHeight.Text = Settings1.Default.PrinterHeight;
  193. UseSSL.Checked = Settings1.Default.UseSSL;
  194. if (!Util.IsDebug())
  195. {
  196. this.Height = 570;
  197. PrintTest.Enabled = false;
  198. label5.Enabled = false;
  199. LabelFormat.Enabled = false;
  200. LabelList.Enabled = false;
  201. label10.Enabled = false;
  202. LoadLabelList.Enabled = false;
  203. SaveLabel.Enabled = false;
  204. UseSSL.Enabled = false;
  205. PrintTest.Visible = false;
  206. label5.Visible = false;
  207. LabelFormat.Visible = false;
  208. LabelList.Visible = false;
  209. label10.Visible = false;
  210. LoadLabelList.Visible = false;
  211. SaveLabel.Visible = false;
  212. UseSSL.Visible = false;
  213. }
  214. if (PrintMode.Text == "Print From Server")
  215. {
  216. PrintKiosks.Enabled = true;
  217. label12.Enabled = true;
  218. label1.Enabled = true;
  219. }
  220. else
  221. {
  222. PrintKiosks.Enabled = false;
  223. label12.Enabled = false;
  224. label1.Enabled = false;
  225. }
  226. }
  227. void buttonclick(object sender, EventArgs e)
  228. {
  229. var b = sender as Button;
  230. var d = b.Text[0];
  231. KeyStroke(d);
  232. }
  233. private void textBox_KeyPress(object sender, KeyPressEventArgs e)
  234. {
  235. if (current == null) return;
  236. if (e.KeyChar == '\b') BackSpace();
  237. else KeyStroke(e.KeyChar);
  238. e.Handled = true;
  239. }
  240. private void KeyStroke(char d)
  241. {
  242. if (current == null) return;
  243. current.Text += d;
  244. current.Focus();
  245. current.Select(current.Text.Length, 0);
  246. }
  247. private void BackSpace()
  248. {
  249. if (current == null) return;
  250. var t = current.Text;
  251. var len = t.Length - 1;
  252. if (len < 0) len = 0;
  253. current.Text = t.Substring(0, len);
  254. current.Focus();
  255. current.Select(current.Text.Length, 0);
  256. }
  257. private void textbox_Enter(object sender, EventArgs e)
  258. {
  259. current = (TextBox)sender;
  260. }
  261. private void bbs_Click(object sender, EventArgs e)
  262. {
  263. BackSpace();
  264. }
  265. public bool CancelClose { get; set; }
  266. private void Login_FormClosing(object sender, FormClosingEventArgs e)
  267. {
  268. e.Cancel = CancelClose;
  269. CancelClose = false;
  270. }
  271. private void bshift_Click(object sender, EventArgs e)
  272. {
  273. if (ba.Text == "A")
  274. {
  275. ba.Text = "a";
  276. bb.Text = "b";
  277. bc.Text = "c";
  278. bd.Text = "d";
  279. be.Text = "e";
  280. bf.Text = "f";
  281. bg.Text = "g";
  282. bh.Text = "h";
  283. bi.Text = "i";
  284. bj.Text = "j";
  285. bk.Text = "k";
  286. bl.Text = "l";
  287. bm.Text = "m";
  288. bn.Text = "n";
  289. bo.Text = "o";
  290. bp.Text = "p";
  291. bq.Text = "q";
  292. br.Text = "r";
  293. bs.Text = "s";
  294. bt.Text = "t";
  295. bu.Text = "u";
  296. bv.Text = "v";
  297. bw.Text = "w";
  298. bx.Text = "x";
  299. by.Text = "y";
  300. bz.Text = "z";
  301. b1.Text = "1";
  302. b2.Text = "2";
  303. b3.Text = "3";
  304. b4.Text = "4";
  305. b5.Text = "5";
  306. b6.Text = "6";
  307. b7.Text = "7";
  308. b8.Text = "8";
  309. b9.Text = "9";
  310. b0.Text = "0";
  311. bdash.Text = "-";
  312. bequal.Text = "=";
  313. blbrace.Text = "[";
  314. brbrace.Text = "]";
  315. bcolon.Text = ":";
  316. bcomma.Text = ",";
  317. bdot.Text = ".";
  318. bslash.Text = "/";
  319. }
  320. else
  321. {
  322. ba.Text = "A";
  323. bb.Text = "B";
  324. bc.Text = "C";
  325. bd.Text = "D";
  326. be.Text = "E";
  327. bf.Text = "F";
  328. bg.Text = "G";
  329. bh.Text = "H";
  330. bi.Text = "I";
  331. bj.Text = "J";
  332. bk.Text = "K";
  333. bl.Text = "L";
  334. bm.Text = "M";
  335. bn.Text = "N";
  336. bo.Text = "O";
  337. bp.Text = "P";
  338. bq.Text = "Q";
  339. br.Text = "R";
  340. bs.Text = "S";
  341. bt.Text = "T";
  342. bu.Text = "U";
  343. bv.Text = "V";
  344. bw.Text = "W";
  345. bx.Text = "X";
  346. by.Text = "Y";
  347. bz.Text = "Z";
  348. b1.Text = "!";
  349. b2.Text = "@";
  350. b3.Text = "#";
  351. b4.Text = "$";
  352. b5.Text = "%";
  353. b6.Text = "^";
  354. b7.Text = "&&";
  355. b8.Text = "*";
  356. b9.Text = "(";
  357. b0.Text = ")";
  358. bdash.Text = "_";
  359. bequal.Text = "+";
  360. blbrace.Text = "{";
  361. brbrace.Text = "}";
  362. bcolon.Text = ";";
  363. bcomma.Text = "<";
  364. bdot.Text = ">";
  365. bslash.Text = "?";
  366. }
  367. }
  368. private void Printer_SelectedIndexChanged(object sender, EventArgs e)
  369. {
  370. LabelPrinterSize.Text = "Label Size: " + PrinterHelper.getPrinterWidth(Printer.Text) + " X " + PrinterHelper.getPrinterHeight(Printer.Text);
  371. }
  372. private void PrintTest_Click(object sender, EventArgs e)
  373. {
  374. Program.PrinterWidth = PrinterWidth.Text;
  375. Program.PrinterHeight = PrinterHeight.Text;
  376. string[] sLabelPieces = LabelList.Text.Split(new char[] { '~' });
  377. if( sLabelPieces.Length >= 2 ) PrinterHelper.printTestLabel(Printer.Text, LabelFormat.Text);
  378. else PrinterHelper.printTestLabel(Printer.Text, LabelFormat.Text.Replace( "\r\n", "" ));
  379. }
  380. private void LoadLabelList_Click(object sender, EventArgs e)
  381. {
  382. System.Diagnostics.Debug.Print( "Loading Label List..." );
  383. if( URL.Text.Contains("localhost") ) Program.URL = "http://" + URL.Text;
  384. else Program.URL = "https://" + URL.Text;
  385. Program.Username = username.Text;
  386. Program.Password = password.Text;
  387. string[] labelList = PrinterHelper.fetchLabelList();
  388. if (labelList == null) return;
  389. LabelList.Items.Clear();
  390. LabelList.Text = "";
  391. LabelFormat.Text = "";
  392. foreach (var label in labelList)
  393. {
  394. LabelList.Items.Add(label);
  395. }
  396. }
  397. private void LabelList_SelectedIndexChanged(object sender, EventArgs e)
  398. {
  399. string[] sLabelPieces = LabelList.Text.Split( new char[] { '~' } );
  400. LabelFormat.Text = PrinterHelper.fetchLabelFormat( sLabelPieces[0], int.Parse( sLabelPieces[1] ) ).Replace("~", "~\r\n");
  401. }
  402. private void SaveLabel_Click(object sender, EventArgs e)
  403. {
  404. string[] sLabelPieces = LabelList.Text.Split(new char[] { '~' });
  405. PrinterHelper.saveLabelFormat(sLabelPieces[0], sLabelPieces[1], LabelFormat.Text.Replace("\r\n", ""));
  406. LoadLabelList.PerformClick();
  407. }
  408. private void SizeFromPrinter_Click(object sender, EventArgs e)
  409. {
  410. PrinterWidth.Text = PrinterHelper.getPrinterWidth(Printer.Text).ToString();
  411. PrinterHeight.Text = PrinterHelper.getPrinterHeight(Printer.Text).ToString();
  412. }
  413. private void AdvancedPageSize_CheckedChanged(object sender, EventArgs e)
  414. {
  415. if (AdvancedPageSize.Checked)
  416. {
  417. PageWidthLabel.Enabled = true;
  418. PageHeightLabel.Enabled = true;
  419. PrinterWidth.Enabled = true;
  420. PrinterHeight.Enabled = true;
  421. SizeFromPrinter.Enabled = true;
  422. PrinterWidth.Text = "";
  423. PrinterHeight.Text = "";
  424. }
  425. else
  426. {
  427. PageWidthLabel.Enabled = false;
  428. PageHeightLabel.Enabled = false;
  429. PrinterWidth.Enabled = false;
  430. PrinterHeight.Enabled = false;
  431. SizeFromPrinter.Enabled = false;
  432. PrinterWidth.Text = "";
  433. PrinterHeight.Text = "";
  434. }
  435. }
  436. private void PrintMode_SelectedIndexChanged(object sender, EventArgs e)
  437. {
  438. if (PrintMode.SelectedIndex == 2)
  439. {
  440. PrintKiosks.Enabled = true;
  441. label12.Enabled = true;
  442. label1.Enabled = true;
  443. }
  444. else
  445. {
  446. PrintKiosks.Enabled = false;
  447. label12.Enabled = false;
  448. label1.Enabled = false;
  449. PrintKiosks.Text = "";
  450. }
  451. }
  452. }
  453. }