AddCustomCertificationControl.xaml.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using ComPDFKit.DigitalSign;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.Controls.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.Controls.PDFControl
  21. {
  22. /// <summary>
  23. /// Interaction logic for 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. FillComboBoxWithPropose();
  83. }
  84. private void FillComboBoxWithPropose()
  85. {
  86. PurposeCmb.Items.Clear();
  87. PurposeCmb.Items.Add(LanguageHelper.SigManager.GetString("Option_Sign"));
  88. PurposeCmb.Items.Add(LanguageHelper.SigManager.GetString("Option_Encrypt"));
  89. PurposeCmb.Items.Add(LanguageHelper.SigManager.GetString("Option_SignAndEncrypt"));
  90. }
  91. private void FillComboBoxWithCountries()
  92. {
  93. var countries = countryProvider.GetCountries();
  94. foreach (var country in countries)
  95. {
  96. var formattedName = $"{country.Alpha2Code} - {country.CommonName}";
  97. countryNames.Add(formattedName);
  98. }
  99. AreaCmb.ItemsSource = countryNames;
  100. }
  101. private void CancelBtn_Click(object sender, RoutedEventArgs e)
  102. {
  103. CancelEvent?.Invoke(this, EventArgs.Empty);
  104. }
  105. private void SaveBtn_Click(object sender, RoutedEventArgs e)
  106. {
  107. if (CertificateInfo.GrantorName == string.Empty)
  108. {
  109. ErrorTipsText.Text = "Please input Grantor Name";
  110. return;
  111. }
  112. if(CertificateInfo.Email == string.Empty)
  113. {
  114. ErrorTipsText.Text = "Please input Email";
  115. return;
  116. }
  117. if (!CommonHelper.IsValidEmail(CertificateInfo.Email))
  118. {
  119. ErrorTipsText.Text = "Email format is not correct";
  120. return;
  121. }
  122. ContinueEvent?.Invoke(this, CreateCertificationMode.SaveCertificate);
  123. }
  124. private void AreaCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  125. {
  126. if (AreaCmb.SelectedItem != null)
  127. {
  128. string selectedText = AreaCmb.SelectedItem.ToString();
  129. string[] parts = selectedText.Split('-');
  130. CertificateInfo.Area = parts[0].Trim(); // Extract the Alpha2Code
  131. }
  132. }
  133. private void PurposeCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  134. {
  135. if (PurposeCmb.SelectedItem != null)
  136. {
  137. CertificateInfo.PurposeType = (CPDFCertUsage)PurposeCmb.SelectedIndex + 1;
  138. }
  139. }
  140. }
  141. }