CheckPasswordDialogViewModel.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using ComPDFKit.PDFDocument;
  2. using Microsoft.Office.Interop.Word;
  3. using PDF_Master.Helper;
  4. using PDF_Master.Model;
  5. using PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs;
  6. using Prism.Commands;
  7. using Prism.Mvvm;
  8. using Prism.Services.Dialogs;
  9. using System;
  10. using System.Collections.Generic;
  11. using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  12. using Visibility = System.Windows.Visibility;
  13. namespace PDF_Master.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
  14. {
  15. public class CheckPasswordDialogViewModel : BindableBase, IDialogAware
  16. {
  17. #region 参数和属性
  18. private CPDFDocument document;
  19. private CPDFDocument tempDocument;
  20. #endregion
  21. private EnumPasswordKind passwordKind = EnumPasswordKind.StatusOpenPassword;
  22. private string _password = "";
  23. public string Password
  24. {
  25. get { return _password; }
  26. set
  27. {
  28. SetProperty(ref _password, value);
  29. IsRightValue = "unknow";
  30. if (!string.IsNullOrEmpty(value))
  31. {
  32. ClearPasswordButtonVisibility = Visibility.Visible;
  33. }
  34. else
  35. {
  36. ClearPasswordButtonVisibility = Visibility.Hidden;
  37. }
  38. }
  39. }
  40. private string _inputPasswordMsg;
  41. public string InputPasswordMsg
  42. {
  43. get { return _inputPasswordMsg; }
  44. set { SetProperty(ref _inputPasswordMsg, value); }
  45. }
  46. private string _checkPasswordMsg;
  47. public string CheckPasswordMsg
  48. {
  49. get { return _checkPasswordMsg; }
  50. set { SetProperty(ref _checkPasswordMsg, value); }
  51. }
  52. private string _errorPasswordMsg;
  53. public string ErrorPasswordMsg
  54. {
  55. get { return _errorPasswordMsg; }
  56. set { SetProperty(ref _errorPasswordMsg, value); }
  57. }
  58. private string _passwordDialogTitle;
  59. public string PasswordDialogTitle
  60. {
  61. get { return _passwordDialogTitle; }
  62. set { SetProperty(ref _passwordDialogTitle, value); }
  63. }
  64. private string _isRightValue = "unknown";
  65. public string IsRightValue
  66. {
  67. get { return _isRightValue; }
  68. set
  69. {
  70. SetProperty(ref _isRightValue, value);
  71. if (value == "false")
  72. {
  73. ErrorPasswordMsgVisibility = Visibility.Visible;
  74. }
  75. else
  76. {
  77. ErrorPasswordMsgVisibility = Visibility.Hidden;
  78. }
  79. }
  80. }
  81. private Visibility _clearPasswordButtonVisibility = Visibility.Hidden;
  82. public Visibility ClearPasswordButtonVisibility
  83. {
  84. get { return _clearPasswordButtonVisibility; }
  85. set { SetProperty(ref _clearPasswordButtonVisibility, value); }
  86. }
  87. private Visibility _errorPasswordMsgVisibility = Visibility.Hidden;
  88. public Visibility ErrorPasswordMsgVisibility
  89. {
  90. get { return _errorPasswordMsgVisibility; }
  91. set { SetProperty(ref _errorPasswordMsgVisibility, value); }
  92. }
  93. #region 委托声明
  94. public DelegateCommand ConfirmCommand { get; set; }
  95. public DelegateCommand CancelCommand { get; set; }
  96. public DelegateCommand ClearPasswordCommand { get; set; }
  97. #endregion
  98. public CheckPasswordDialogViewModel()
  99. {
  100. ConfirmCommand = new DelegateCommand(Confirm);
  101. CancelCommand = new DelegateCommand(Cancel);
  102. ClearPasswordCommand = new DelegateCommand(ClearPassword);
  103. }
  104. #region 逻辑函数
  105. private void Confirm()
  106. {
  107. if (passwordKind == EnumPasswordKind.StatusOpenPassword)//TODO: 用于检测输入密码的权限,以及是否输入了正确的密码
  108. {
  109. if (tempDocument.UnlockWithPassword(Password))
  110. {
  111. var dialogResult = new DialogResult(ButtonResult.OK);
  112. dialogResult.Parameters.Add(ParameterNames.Password, Password);
  113. dialogResult.Parameters.Add(ParameterNames.PasswordResult, true);
  114. RequestClose.Invoke(dialogResult);
  115. }
  116. else
  117. {
  118. IsRightValue = "false";
  119. }
  120. }
  121. else
  122. {
  123. if (tempDocument.UnlockWithPassword(Password) && (tempDocument.CheckOwnerPassword(Password)||SecurityHelper.CheckHaveAllPermissions(tempDocument)))
  124. {
  125. var dialogResult = new DialogResult(ButtonResult.OK);
  126. dialogResult.Parameters.Add(ParameterNames.Password, Password);
  127. dialogResult.Parameters.Add(ParameterNames.PasswordResult, true);
  128. RequestClose.Invoke(dialogResult);
  129. }
  130. else
  131. {
  132. IsRightValue = "false";
  133. }
  134. }
  135. }
  136. private void Cancel()
  137. {
  138. var dialogResult = new DialogResult(ButtonResult.Cancel);
  139. dialogResult.Parameters.Add(ParameterNames.Password, null);
  140. dialogResult.Parameters.Add(ParameterNames.PasswordResult, false);
  141. RequestClose.Invoke(dialogResult);
  142. }
  143. private void ClearPassword()
  144. {
  145. Password = "";
  146. }
  147. #endregion
  148. #region 框架相关
  149. public string Title => "";
  150. public event Action<IDialogResult> RequestClose;
  151. public bool CanCloseDialog()
  152. {
  153. return true;
  154. }
  155. public void OnDialogClosed()
  156. {
  157. if (tempDocument != null)
  158. {
  159. tempDocument.Release();
  160. }
  161. }
  162. public void OnDialogOpened(IDialogParameters parameters)
  163. {
  164. CPDFDocument doc = null;
  165. parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
  166. parameters.TryGetValue<EnumPasswordKind>(ParameterNames.PasswordKind, out passwordKind);
  167. if (doc != null)
  168. {
  169. document = doc;
  170. tempDocument = CPDFDocument.InitWithFilePath(document.FilePath);
  171. if(passwordKind == EnumPasswordKind.StatusOpenPassword)
  172. {
  173. PasswordDialogTitle = "Open Password";
  174. }
  175. else
  176. {
  177. PasswordDialogTitle = "Permission Password";
  178. }
  179. InputPasswordMsg = "\"" + document.FileName + ".pdf\"" + " is protected, please enter a Document Password.";
  180. CheckPasswordMsg = "Password";
  181. //跟登陆窗口,错误密码的提示保持一致
  182. ErrorPasswordMsg = App.ServiceLoader.GetString("code311"); ;
  183. }
  184. }
  185. #endregion
  186. }
  187. }