DigitalSignatureInfoControl.xaml.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using ComPDFKit.Controls.Helper;
  8. using ComPDFKit.Controls.PDFControl;
  9. using ComPDFKit.DigitalSign;
  10. namespace ComPDFKit.Controls.PDFControl
  11. {
  12. public partial class DigitalSignatureInfoControl : UserControl, INotifyPropertyChanged
  13. {
  14. private string _signerInfo;
  15. public string SignerInfo
  16. {
  17. get => _signerInfo;
  18. set => UpdateProper(ref _signerInfo, value);
  19. }
  20. private string _timeInfo;
  21. public string TimeInfo
  22. {
  23. get => _timeInfo;
  24. set => UpdateProper(ref _timeInfo, value);
  25. }
  26. private SignatureStatus _status;
  27. public SignatureStatus Status
  28. {
  29. get => _status;
  30. set
  31. {
  32. _status = value;
  33. SetStatus(_status);
  34. }
  35. }
  36. public DigitalSignatureInfoControl()
  37. {
  38. InitializeComponent();
  39. DataContext = this;
  40. }
  41. public void InitWithSignature(CPDFSignature signature)
  42. {
  43. string validity;
  44. string signerName;
  45. string email;
  46. string time;
  47. if(signature == null)
  48. return;
  49. SignatureStatus status;
  50. CPDFSigner signer = signature.SignerList.First();
  51. bool isTrusted = signer.IsCertTrusted;
  52. bool isVerified = signer.IsSignVerified;
  53. bool notModified = signature.ModifyInfoList.Count == 0;
  54. if (isTrusted && isVerified && notModified)
  55. {
  56. status = SignatureStatus.Valid;
  57. validity = LanguageHelper.SigManager.GetString("Text_Valid");
  58. }
  59. else if (!isTrusted && isVerified && notModified)
  60. {
  61. status = SignatureStatus.Unknown;
  62. validity = LanguageHelper.SigManager.GetString("Text_Unknown");
  63. }
  64. else
  65. {
  66. status = SignatureStatus.Invalid;
  67. validity = LanguageHelper.SigManager.GetString("Text_Invalid");
  68. }
  69. Status = status;
  70. string signedBy = LanguageHelper.SigManager.GetString("Text_Signer");
  71. signerName = signature.Name;
  72. email = DictionaryValueConverter.GetEmailFormDictionary(signature.SignerList.First().CertificateList.Last().SubjectDict);
  73. time = signature.Date;
  74. SignerInfo = validity + LanguageHelper.SigManager.GetString("Text_Signer") + "\"" + signerName;
  75. if (!string.IsNullOrEmpty(email))
  76. {
  77. SignerInfo += "<" + email + ">" + "\"";
  78. }
  79. else
  80. {
  81. SignerInfo += "\"";
  82. }
  83. TimeInfo = LanguageHelper.SigManager.GetString("Text_SignTime") + CommonHelper.GetExactDateFromString(time);
  84. }
  85. private void SetStatus(SignatureStatus status)
  86. {
  87. ValidBorder.Visibility = Visibility.Collapsed;
  88. InvalidBorder.Visibility = Visibility.Collapsed;
  89. UnknownBorder.Visibility = Visibility.Collapsed;
  90. switch (status)
  91. {
  92. case SignatureStatus.None:
  93. break;
  94. case SignatureStatus.Valid:
  95. ValidBorder.Visibility = Visibility.Visible;
  96. break;
  97. case SignatureStatus.Invalid:
  98. InvalidBorder.Visibility = Visibility.Visible;
  99. break;
  100. case SignatureStatus.Unknown:
  101. UnknownBorder.Visibility = Visibility.Visible;
  102. break;
  103. }
  104. }
  105. public event PropertyChangedEventHandler PropertyChanged;
  106. protected void UpdateProper<T>(ref T properValue,
  107. T newValue,
  108. [CallerMemberName] string properName = "")
  109. {
  110. if (object.Equals(properValue, newValue))
  111. return;
  112. properValue = newValue;
  113. OnPropertyChanged(properName);
  114. }
  115. protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
  116. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  117. }
  118. }