DeleteSafetySettingsDialogViewModel.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using ComPDFKit.PDFDocument;
  2. using PDF_Office.CustomControl;
  3. using PDF_Office.Helper;
  4. using PDF_Office.Model;
  5. using PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs;
  6. using Prism.Commands;
  7. using Prism.Mvvm;
  8. using Prism.Services.Dialogs;
  9. using System;
  10. using System.Windows.Forms;
  11. using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  12. namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
  13. {
  14. public class DeleteSafetySettingsDialogViewModel : BindableBase, IDialogAware
  15. {
  16. #region 参数和属性
  17. DeleteSafetySettintgsModel deleteSafetySettintgsModel;
  18. public IDialogService dialogs;
  19. private CPDFDocument document;
  20. private string _deleteSecurityMsg;
  21. public string DeleteSecurityMsg
  22. {
  23. get { return _deleteSecurityMsg; }
  24. set { SetProperty(ref _deleteSecurityMsg, value); }
  25. }
  26. #endregion
  27. #region 委托声明
  28. public DelegateCommand CancelCommand { get; set; }
  29. public DelegateCommand RemoveSecuritySettingsCommand { get; set; }
  30. #endregion
  31. public DeleteSafetySettingsDialogViewModel(IDialogService dialogService)
  32. {
  33. dialogs = dialogService;
  34. RemoveSecuritySettingsCommand = new DelegateCommand(RemoveSecuritySettings);
  35. CancelCommand = new DelegateCommand(Cancel);
  36. }
  37. #region 检查函数
  38. /// <summary>
  39. /// 检测是否需要输入密码
  40. /// </summary>
  41. /// <returns>
  42. /// EnumNeedPassword.StatusVerifyPassword:需要确认密码;
  43. /// EnumNeedPassword.StatusDirectDescrypt:直接执行操作
  44. /// EnumNeedPassword.StatusCannotDecrypt:无可执行操作。
  45. /// </returns>
  46. private DeleteSafetySettintgsModel.EnumNeedPassword CheckNeedPassword()
  47. {
  48. try
  49. {
  50. if (document.IsLocked)
  51. {
  52. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
  53. }
  54. else
  55. {
  56. if (SecurityHelper.CheckHaveAllPermissions(document))
  57. {
  58. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusDirectDescrypt;
  59. }
  60. else
  61. {
  62. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
  63. }
  64. }
  65. }
  66. catch
  67. {
  68. return EnumNeedPassword.StatusCannotDecrypt;
  69. }
  70. }
  71. #endregion
  72. #region 逻辑函数
  73. /// <summary>
  74. /// 逻辑优化:判权操作放到密码弹窗里,所以一旦进入该状态,可以直接清除权限
  75. /// </summary>
  76. public void RemoveSecuritySettings()
  77. {
  78. System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  79. /*
  80. *设置这个对话框的起始保存路径
  81. */
  82. sfd.InitialDirectory = document.FilePath;
  83. /*
  84. *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
  85. */
  86. sfd.Filter = "PDF|*.pdf;";
  87. /*
  88. *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  89. */
  90. sfd.FileName = document.FileName + "_DecryptedFile.pdf";
  91. /*
  92. * 做一些工作
  93. */
  94. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  95. {
  96. if(document.Descrypt(sfd.FileName)){
  97. MessageBoxEx.Show("保存成功");
  98. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  99. }
  100. else
  101. {
  102. ///TODO 不明原因干扰
  103. }
  104. }
  105. else
  106. {
  107. MessageBox.Show("取消保存");
  108. }
  109. }
  110. private void Cancel()
  111. {
  112. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
  113. }
  114. #endregion
  115. #region 框架相关
  116. public string Title => "";
  117. public event Action<IDialogResult> RequestClose;
  118. public bool CanCloseDialog()
  119. {
  120. return true;
  121. }
  122. public void OnDialogClosed()
  123. {
  124. }
  125. public void OnDialogOpened(IDialogParameters parameters)
  126. {
  127. CPDFDocument doc = null;
  128. parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
  129. if (doc != null)
  130. {
  131. document = doc;
  132. DeleteSecurityMsg = "Are you sure you want to remove the security settings for”" + document.FileName + "” documents?";
  133. }
  134. }
  135. #endregion
  136. }
  137. }