AddCustomCertificationControl.xaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFDocument;
  3. using Compdfkit_Tools.Helper;
  4. using Nager.Country;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace Compdfkit_Tools.PDFControl
  20. {
  21. /// <summary>
  22. /// AddCustomCertificationControl.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class AddCustomCertificationControl : UserControl
  25. {
  26. public event EventHandler SaveEvent;
  27. public event EventHandler CancelEvent;
  28. public CertificateInfo certificateInfo = new CertificateInfo();
  29. private string _grantorName = string.Empty;
  30. public string GrantorName
  31. {
  32. get => _grantorName;
  33. set
  34. {
  35. _grantorName = value;
  36. certificateInfo.GrantorName = value;
  37. }
  38. }
  39. private string _email = string.Empty;
  40. public string Email
  41. {
  42. get => _email;
  43. set
  44. {
  45. _email = value;
  46. certificateInfo.Email = value;
  47. }
  48. }
  49. private string _organization = string.Empty;
  50. public string Organization
  51. {
  52. get => _organization;
  53. set
  54. {
  55. _organization = value;
  56. certificateInfo.Organization = value;
  57. }
  58. }
  59. private string _organizationalUnit = string.Empty;
  60. public string OrganizationalUnit
  61. {
  62. get => _organizationalUnit;
  63. set
  64. {
  65. _organizationalUnit = value;
  66. certificateInfo.OrganizationUnit = value;
  67. }
  68. }
  69. private readonly CountryProvider countryProvider = new CountryProvider();
  70. private readonly List<string> countryNames = new List<string>();
  71. public event EventHandler<CreateCertificationMode> ContinueEvent;
  72. public AddCustomCertificationControl()
  73. {
  74. InitializeComponent();
  75. this.DataContext = this;
  76. FillComboBox();
  77. }
  78. private void FillComboBox()
  79. {
  80. FillComboBoxWithCountries();
  81. FillComboBoxWithAlgorithm();
  82. FillComboBoxWithPropose();
  83. }
  84. private void FillComboBoxWithPropose()
  85. {
  86. PurposeCmb.Items.Clear();
  87. PurposeCmb.Items.Add("Digital Signatures");
  88. PurposeCmb.Items.Add("Data Encryption");
  89. PurposeCmb.Items.Add("Digital Signatures and Data Encryption");
  90. }
  91. private void FillComboBoxWithAlgorithm()
  92. {
  93. AlgorithmCmb.Items.Clear();
  94. AlgorithmCmb.Items.Add("1024-bit RSA");
  95. AlgorithmCmb.Items.Add("2048-bit RSA");
  96. }
  97. private void FillComboBoxWithCountries()
  98. {
  99. AlgorithmCmb.Items.Clear();
  100. var countries = countryProvider.GetCountries();
  101. foreach (var country in countries)
  102. {
  103. var formattedName = $"{country.Alpha2Code} - {country.CommonName}";
  104. countryNames.Add(formattedName);
  105. }
  106. AreaCmb.ItemsSource = countryNames;
  107. }
  108. private void CancelBtn_Click(object sender, RoutedEventArgs e)
  109. {
  110. CancelEvent?.Invoke(this, EventArgs.Empty);
  111. }
  112. private void SaveBtn_Click(object sender, RoutedEventArgs e)
  113. {
  114. if (certificateInfo.GrantorName == string.Empty)
  115. {
  116. return;
  117. }
  118. else if(certificateInfo.Email == string.Empty)
  119. {
  120. return;
  121. }
  122. else if (!CommonHelper.IsValidEmail(certificateInfo.Email))
  123. {
  124. return;
  125. }
  126. ContinueEvent?.Invoke(this, CreateCertificationMode.SaveCertificate);
  127. }
  128. private void AreaCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  129. {
  130. if (AreaCmb.SelectedItem != null)
  131. {
  132. string selectedText = AreaCmb.SelectedItem.ToString();
  133. string[] parts = selectedText.Split('-');
  134. certificateInfo.Area = parts[0].Trim(); // Extract the Alpha2Code
  135. }
  136. }
  137. private void AlgorithmCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  138. {
  139. if (AlgorithmCmb.SelectedItem != null)
  140. {
  141. certificateInfo.AlgorithmType = (AlgorithmType)AlgorithmCmb.SelectedIndex;
  142. }
  143. }
  144. private void PurposeCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  145. {
  146. if (PurposeCmb.SelectedItem != null)
  147. {
  148. certificateInfo.PurposeType = (PurposeType)PurposeCmb.SelectedIndex;
  149. }
  150. }
  151. }
  152. }