PageRenderTime 44ms CodeModel.GetById 19ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 1ms

/CmsCheckin/Login.cs

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