DeleteSafetySettingsDialogViewModel.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.Status
  43. ///
  44. ///
  45. ///
  46. /// :需要确认密码;
  47. /// EnumNeedPassword.StatusDirectDescrypt:直接执行操作
  48. /// EnumNeedPassword.StatusCannotDecrypt:无可执行操作。
  49. /// </returns>
  50. private DeleteSafetySettintgsModel.EnumNeedPassword CheckNeedPassword()
  51. {
  52. try
  53. {
  54. if (document.IsLocked)
  55. {
  56. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
  57. }
  58. else
  59. {
  60. if (SecurityHelper.CheckHaveAllPermissions(document))
  61. {
  62. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusDirectDescrypt;
  63. }
  64. else
  65. {
  66. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
  67. }
  68. }
  69. }
  70. catch
  71. {
  72. return EnumNeedPassword.StatusCannotDecrypt;
  73. }
  74. }
  75. #endregion
  76. #region 逻辑函数
  77. /// <summary>
  78. /// 逻辑优化:判权操作放到密码弹窗里,所以一旦进入该状态,可以直接清除权限
  79. /// </summary>
  80. public void RemoveSecuritySettings()
  81. {
  82. System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  83. /*
  84. *设置这个对话框的起始保存路径
  85. */
  86. sfd.InitialDirectory = document.FilePath;
  87. /*
  88. *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
  89. */
  90. sfd.Filter = "PDF|*.pdf;";
  91. /*
  92. *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  93. */
  94. sfd.FileName = document.FileName + "_DecryptedFile.pdf";
  95. /*
  96. * 做一些工作
  97. */
  98. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  99. {
  100. if(document.Descrypt(sfd.FileName)){
  101. MessageBoxEx.Show("保存成功");
  102. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  103. }
  104. else
  105. {
  106. ///TODO 不明原因干扰
  107. }
  108. }
  109. else
  110. {
  111. MessageBox.Show("取消保存");
  112. }
  113. }
  114. private void Cancel()
  115. {
  116. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
  117. }
  118. #endregion
  119. #region 框架相关
  120. public string Title => "";
  121. public event Action<IDialogResult> RequestClose;
  122. public bool CanCloseDialog()
  123. {
  124. return true;
  125. }
  126. public void OnDialogClosed()
  127. {
  128. }
  129. public void OnDialogOpened(IDialogParameters parameters)
  130. {
  131. CPDFDocument doc = null;
  132. parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
  133. if (doc != null)
  134. {
  135. document = doc;
  136. DeleteSecurityMsg = "Are you sure you want to remove the security settings for”" + document.FileName + "” documents?";
  137. }
  138. }
  139. #endregion
  140. }
  141. }