AuthenticationUC.xaml.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using Microsoft.WindowsAPICodePack.Dialogs;
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media.Imaging;
  8. namespace Phihong_EVSE_UI_Tool
  9. {
  10. /// <summary>
  11. /// AuthenticationUC.xaml 的互動邏輯
  12. /// </summary>
  13. public partial class AuthenticationUC : UserControl
  14. {
  15. private Properties.Settings mySettings = Properties.Settings.Default;
  16. private string verifyCustomImgPath;
  17. private string verifyOkCustomImgPath;
  18. private string verifyFailCustomImgPath;
  19. public AuthenticationUC()
  20. {
  21. InitializeComponent();
  22. verifyCustomImgPath = mySettings.VerifyCustomImgPath;
  23. uxVerifyDefaultRadioButton.IsChecked = mySettings.IsVerifyDefault;
  24. uxVerifyCustomRadioButton.IsChecked = !mySettings.IsVerifyDefault;
  25. uxVerifyExploreTextBox.Text = mySettings.VerifyCustomImgPath_org;
  26. verifyOkCustomImgPath = mySettings.VerifyOkCustomImgPath;
  27. uxVerifyOkDefaultRadioButton.IsChecked = mySettings.IsVerifyOkDefault;
  28. uxVerifyOkCustomRadioButton.IsChecked = !mySettings.IsVerifyOkDefault;
  29. uxVerifyOkExploreTextBox.Text = mySettings.VerifyOkCustomImgPath_org;
  30. verifyFailCustomImgPath = mySettings.VerifyFailCustomImgPath;
  31. uxVerifyFailDefaultRadioButton.IsChecked = mySettings.IsVerifyFailDefault;
  32. uxVerifyFailCustomRadioButton.IsChecked = !mySettings.IsVerifyFailDefault;
  33. uxVerifyFailExploreTextBox.Text = mySettings.VerifyFailCustomImgPath_org;
  34. }
  35. private void uxDefaultRadioButton_Checked(object sender, RoutedEventArgs e)
  36. {
  37. Uri srcPath;
  38. RadioButton rb = e.Source as RadioButton;
  39. if (rb is null)
  40. {
  41. return;
  42. }
  43. switch (rb.Tag.ToString())
  44. {
  45. case "Verify":
  46. srcPath = new Uri(Path.Combine(Utility.BG_PARENTFOLDER, Utility.BG_VERIFY), UriKind.Relative);
  47. uxVerifyImage.Source = new BitmapImage(srcPath);
  48. Utility.CopyFileFromResource(srcPath, Path.Combine(Utility.OUTPUT_DIRECTORY, Utility.BG_VERIFY));
  49. Utility.ChangedFiles[Utility.BG_VERIFY] = true;
  50. mySettings.IsVerifyDefault = true;
  51. break;
  52. case "VerifyOk":
  53. srcPath = new Uri(Path.Combine(Utility.BG_PARENTFOLDER, Utility.BG_VERIFYOK), UriKind.Relative);
  54. uxVerifyOkImage.Source = new BitmapImage(srcPath);
  55. Utility.CopyFileFromResource(srcPath, Path.Combine(Utility.OUTPUT_DIRECTORY, Utility.BG_VERIFYOK));
  56. Utility.ChangedFiles[Utility.BG_VERIFYOK] = true;
  57. mySettings.IsVerifyOkDefault = true;
  58. break;
  59. case "VerifyFail":
  60. srcPath = new Uri(Path.Combine(Utility.BG_PARENTFOLDER, Utility.BG_VERIFYFAIL), UriKind.Relative);
  61. uxVerifyFailImage.Source = new BitmapImage(srcPath);
  62. Utility.CopyFileFromResource(srcPath, Path.Combine(Utility.OUTPUT_DIRECTORY, Utility.BG_VERIFYFAIL));
  63. Utility.ChangedFiles[Utility.BG_VERIFYFAIL] = true;
  64. mySettings.IsVerifyFailDefault = true;
  65. break;
  66. default:
  67. return;
  68. }
  69. mySettings.Save();
  70. }
  71. private void uxCustomRadioButton_Checked(object sender, RoutedEventArgs e)
  72. {
  73. RadioButton rb = e.Source as RadioButton;
  74. if (rb is null)
  75. {
  76. return;
  77. }
  78. switch (rb.Tag.ToString())
  79. {
  80. case "Verify":
  81. if (File.Exists(verifyCustomImgPath))
  82. {
  83. Utility.LoadImageFromPath(uxVerifyImage, verifyCustomImgPath);
  84. File.Copy(verifyCustomImgPath, Path.Combine(Utility.OUTPUT_DIRECTORY, Utility.BG_VERIFY), true);
  85. Utility.ChangedFiles[Utility.BG_VERIFY] = true;
  86. mySettings.IsVerifyDefault = false;
  87. }
  88. break;
  89. case "VerifyOk":
  90. if (File.Exists(verifyOkCustomImgPath))
  91. {
  92. Utility.LoadImageFromPath(uxVerifyOkImage, verifyOkCustomImgPath);
  93. File.Copy(verifyOkCustomImgPath, Path.Combine(Utility.OUTPUT_DIRECTORY, Utility.BG_VERIFYOK), true);
  94. Utility.ChangedFiles[Utility.BG_VERIFYOK] = true;
  95. mySettings.IsVerifyOkDefault = false;
  96. }
  97. break;
  98. case "VerifyFail":
  99. if (File.Exists(verifyFailCustomImgPath))
  100. {
  101. Utility.LoadImageFromPath(uxVerifyFailImage, verifyFailCustomImgPath);
  102. File.Copy(verifyFailCustomImgPath, Path.Combine(Utility.OUTPUT_DIRECTORY, Utility.BG_VERIFYFAIL), true);
  103. Utility.ChangedFiles[Utility.BG_VERIFYFAIL] = true;
  104. mySettings.IsVerifyFailDefault = false;
  105. }
  106. break;
  107. default:
  108. return;
  109. }
  110. mySettings.Save();
  111. }
  112. private void uxBrowseButton_Click(object sender, RoutedEventArgs e)
  113. {
  114. Button btn = e.Source as Button;
  115. if (btn is null)
  116. {
  117. return;
  118. }
  119. var dlg = new CommonOpenFileDialog()
  120. {
  121. EnsureFileExists = true,
  122. Title = "Select an image file",
  123. };
  124. dlg.Filters.Add(new CommonFileDialogFilter("BMP images", "*.bmp"));
  125. if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
  126. {
  127. if (!Utility.IsBackgroundImageSizeQualified(dlg.FileName))
  128. {
  129. MessageBox.Show(dlg.FileName + "\r\n" + "The resolution of the image must be 800x480",
  130. "Incorrect Resolution", MessageBoxButton.OK, MessageBoxImage.Error);
  131. return;
  132. }
  133. switch (btn.Tag.ToString())
  134. {
  135. case "Verify":
  136. uxVerifyExploreTextBox.Text = dlg.FileName;
  137. verifyCustomImgPath = Path.Combine(Utility.CUSTOM_DIRECTORY, Utility.BG_VERIFY);
  138. File.Copy(dlg.FileName, verifyCustomImgPath, true);
  139. File.Copy(dlg.FileName, Path.Combine(Utility.OUTPUT_DIRECTORY, Utility.BG_VERIFY), true);
  140. Utility.LoadImageFromPath(uxVerifyImage, verifyCustomImgPath);
  141. Utility.ChangedFiles[Utility.BG_VERIFY] = true;
  142. mySettings.IsVerifyDefault = false;
  143. mySettings.VerifyCustomImgPath = verifyCustomImgPath;
  144. mySettings.VerifyCustomImgPath_org = uxVerifyExploreTextBox.Text;
  145. break;
  146. case "VerifyOk":
  147. uxVerifyOkExploreTextBox.Text = dlg.FileName;
  148. verifyOkCustomImgPath = Path.Combine(Utility.CUSTOM_DIRECTORY, Utility.BG_VERIFYOK);
  149. File.Copy(dlg.FileName, verifyOkCustomImgPath, true);
  150. File.Copy(dlg.FileName, Path.Combine(Utility.OUTPUT_DIRECTORY, Utility.BG_VERIFYOK), true);
  151. Utility.LoadImageFromPath(uxVerifyOkImage, verifyOkCustomImgPath);
  152. Utility.ChangedFiles[Utility.BG_VERIFYOK] = true;
  153. mySettings.IsVerifyOkDefault = false;
  154. mySettings.VerifyOkCustomImgPath = verifyOkCustomImgPath;
  155. mySettings.VerifyOkCustomImgPath_org = uxVerifyOkExploreTextBox.Text;
  156. break;
  157. case "VerifyFail":
  158. uxVerifyFailExploreTextBox.Text = dlg.FileName;
  159. verifyFailCustomImgPath = Path.Combine(Utility.CUSTOM_DIRECTORY, Utility.BG_VERIFYFAIL);
  160. File.Copy(dlg.FileName, verifyFailCustomImgPath, true);
  161. File.Copy(dlg.FileName, Path.Combine(Utility.OUTPUT_DIRECTORY, Utility.BG_VERIFYFAIL), true);
  162. Utility.LoadImageFromPath(uxVerifyFailImage, verifyFailCustomImgPath);
  163. Utility.ChangedFiles[Utility.BG_VERIFYFAIL] = true;
  164. mySettings.IsVerifyFailDefault = false;
  165. mySettings.VerifyFailCustomImgPath = verifyFailCustomImgPath;
  166. mySettings.VerifyFailCustomImgPath_org = uxVerifyFailExploreTextBox.Text;
  167. break;
  168. default:
  169. break;
  170. }
  171. }
  172. mySettings.Save();
  173. }
  174. }
  175. }