AddCustomCertificationControl.xaml.cs 5.2 KB

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