DigitalSignatureInfoControl.xaml.cs 4.1 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_Tools.Helper;
  8. using Compdfkit_Tools.PDFControl;
  9. using ComPDFKit.DigitalSign;
  10. namespace Compdfkit_Tools.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 = "Valid Signature";
  58. }
  59. else if (!isTrusted && isVerified && notModified)
  60. {
  61. status = SignatureStatus.Unknown;
  62. validity = "Unknown Signature";
  63. }
  64. else
  65. {
  66. status = SignatureStatus.Invalid;
  67. validity = "Invalid Signature";
  68. }
  69. Status = status;
  70. signerName = signature.Name;
  71. email = DictionaryValueConverter.GetEmailFormDictionary(signature.SignerList.First().CertificateList.Last().SubjectDict);
  72. time = signature.Date;
  73. SignerInfo = validity + ",Signed by" + "\"" + signerName;
  74. if (!string.IsNullOrEmpty(email))
  75. {
  76. SignerInfo += "<" + email + ">" + "\"";
  77. }
  78. else
  79. {
  80. SignerInfo += "\"";
  81. }
  82. TimeInfo = "Signature Time:" + CommonHelper.GetExactDateFromString(time);
  83. }
  84. private void SetStatus(SignatureStatus status)
  85. {
  86. ValidBorder.Visibility = Visibility.Collapsed;
  87. InvalidBorder.Visibility = Visibility.Collapsed;
  88. UnknownBorder.Visibility = Visibility.Collapsed;
  89. switch (status)
  90. {
  91. case SignatureStatus.None:
  92. break;
  93. case SignatureStatus.Valid:
  94. ValidBorder.Visibility = Visibility.Visible;
  95. break;
  96. case SignatureStatus.Invalid:
  97. InvalidBorder.Visibility = Visibility.Visible;
  98. break;
  99. case SignatureStatus.Unknown:
  100. UnknownBorder.Visibility = Visibility.Visible;
  101. break;
  102. }
  103. }
  104. public event PropertyChangedEventHandler PropertyChanged;
  105. protected void UpdateProper<T>(ref T properValue,
  106. T newValue,
  107. [CallerMemberName] string properName = "")
  108. {
  109. if (object.Equals(properValue, newValue))
  110. return;
  111. properValue = newValue;
  112. OnPropertyChanged(properName);
  113. }
  114. protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
  115. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  116. }
  117. }