SignatureStatusBarControl.xaml.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using ComPDFKit.DigitalSign;
  9. namespace Compdfkit_Tools.PDFControl
  10. {
  11. public enum SignatureStatus
  12. {
  13. None,
  14. Valid,
  15. Invalid,
  16. MultiSig,
  17. Unknown,
  18. }
  19. public partial class SignatureStatusBarControl : UserControl, INotifyPropertyChanged
  20. {
  21. private string _messageString;
  22. public string MessageString
  23. {
  24. get => _messageString;
  25. set => UpdateProper(ref _messageString, value);
  26. }
  27. private string validString = "The signature is valid";
  28. private string invalidString = "The signature is invalid";
  29. private string multiSigString = "At least one signature is invalid";
  30. private string unknownString = "Signature validity is unknown";
  31. public event EventHandler OnViewSignatureButtonClicked;
  32. private SignatureStatus _status;
  33. public SignatureStatus Status
  34. {
  35. get => _status;
  36. set
  37. {
  38. _status = value;
  39. SetStatus(_status);
  40. }
  41. }
  42. public SignatureStatusBarControl()
  43. {
  44. InitializeComponent();
  45. DataContext = this;
  46. }
  47. private void SetStatus(SignatureStatus status)
  48. {
  49. ValidBorder.Visibility = Visibility.Collapsed;
  50. InvalidBorder.Visibility = Visibility.Collapsed;
  51. UnknownBorder.Visibility = Visibility.Collapsed;
  52. switch (status)
  53. {
  54. case SignatureStatus.None:
  55. MessageString = "";
  56. Visibility = Visibility.Collapsed;
  57. break;
  58. case SignatureStatus.Valid:
  59. ValidBorder.Visibility = Visibility.Visible;
  60. Visibility = Visibility.Visible;
  61. MessageString = validString;
  62. break;
  63. case SignatureStatus.Invalid:
  64. InvalidBorder.Visibility = Visibility.Visible;
  65. Visibility = Visibility.Visible;
  66. MessageString = invalidString;
  67. break;
  68. case SignatureStatus.MultiSig:
  69. InvalidBorder.Visibility = Visibility.Visible;
  70. Visibility = Visibility.Visible;
  71. MessageString = multiSigString;
  72. break;
  73. case SignatureStatus.Unknown:
  74. UnknownBorder.Visibility = Visibility.Visible;
  75. Visibility = Visibility.Visible;
  76. MessageString = unknownString;
  77. break;
  78. }
  79. }
  80. public void SetStatus(List<CPDFSignature> signatureList)
  81. {
  82. SignatureStatus status;
  83. if (signatureList.Count == 1)
  84. {
  85. bool isTrusted = true;
  86. bool isVerified = true;
  87. bool notModified = true;
  88. foreach (var signature in signatureList)
  89. {
  90. CPDFSigner signer = signature.SignerList.First();
  91. if(signer.IsSignVerified == false)
  92. isVerified = false;
  93. if (signer.IsCertTrusted == false)
  94. isTrusted = false;
  95. if (signature.ModifyInfoList.Count > 0)
  96. notModified = false;
  97. }
  98. if (isTrusted && isVerified && notModified)
  99. {
  100. status = SignatureStatus.Valid;
  101. }
  102. else if (!isTrusted && isVerified && notModified)
  103. {
  104. status = SignatureStatus.Unknown;
  105. }
  106. else
  107. {
  108. status = SignatureStatus.Invalid;
  109. }
  110. }
  111. else if (signatureList.Count > 1)
  112. {
  113. status = SignatureStatus.MultiSig;
  114. }
  115. else
  116. {
  117. status = SignatureStatus.None;
  118. }
  119. Status = status;
  120. }
  121. public event PropertyChangedEventHandler PropertyChanged;
  122. protected void UpdateProper<T>(ref T properValue,
  123. T newValue,
  124. [CallerMemberName] string properName = "")
  125. {
  126. if (object.Equals(properValue, newValue))
  127. return;
  128. properValue = newValue;
  129. OnPropertyChanged(properName);
  130. }
  131. protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
  132. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  133. private void ButtonViewSignature_OnClick(object sender, RoutedEventArgs e)
  134. {
  135. OnViewSignatureButtonClicked?.Invoke(this,null);
  136. }
  137. }
  138. }