DeleteSafetySettingsDialogViewModel.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using ComPDFKit.PDFDocument;
  2. using PDF_Office.Model;
  3. using PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs;
  4. using Prism.Commands;
  5. using Prism.Mvvm;
  6. using Prism.Services.Dialogs;
  7. using System;
  8. using System.Windows.Forms;
  9. namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
  10. {
  11. public class DeleteSafetySettingsDialogViewModel : BindableBase, IDialogAware
  12. {
  13. #region 参数和属性
  14. DeleteSafetySettintgsModel deleteSafetySettintgsModel;
  15. public IDialogService dialogs;
  16. private CPDFDocument document;
  17. #endregion
  18. #region 委托声明
  19. public DelegateCommand DelegateCheckPasswordCommand { get; set; }
  20. public DelegateCommand DelegateCancelCommand { get; set; }
  21. #endregion
  22. public DeleteSafetySettingsDialogViewModel(IDialogService dialogService)
  23. {
  24. dialogs = dialogService;
  25. DelegateCheckPasswordCommand = new DelegateCommand(OpenCheckPasswordDialog);
  26. DelegateCancelCommand = new DelegateCommand(Cancel);
  27. }
  28. #region 检查函数
  29. private bool CheckHaveAllPermissions()
  30. {
  31. CPDFPermissionsInfo permissionsInfo = document.GetPermissionsInfo();
  32. if (permissionsInfo.AllowsDocumentChanges &&
  33. permissionsInfo.AllowsPrinting &&
  34. permissionsInfo.AllowsHighQualityPrinting &&
  35. permissionsInfo.AllowsCopying &&
  36. //permissionsInfo.AllowsDocumentAssembly&&
  37. //TODO: 这个权限有问题
  38. permissionsInfo.AllowsCommenting)
  39. {
  40. return true;
  41. }
  42. else
  43. {
  44. return false;
  45. }
  46. }
  47. /// <summary>
  48. /// 检测是否需要输入密码
  49. /// </summary>
  50. /// <returns>
  51. /// EnumNeedPassword.StatusVerifyPassword:需要确认密码;
  52. /// EnumNeedPassword.StatusDirectDescrypt:直接执行操作
  53. /// EnumNeedPassword.StatusCannotDecrypt:无可执行操作。
  54. /// </returns>
  55. private DeleteSafetySettintgsModel.EnumNeedPassword CheckNeedPassword()
  56. {
  57. if (document.IsLocked)
  58. {
  59. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
  60. }
  61. else { }
  62. if (document.IsEncrypted)
  63. {
  64. if (CheckHaveAllPermissions())
  65. {
  66. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusDirectDescrypt;
  67. }
  68. else
  69. {
  70. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
  71. }
  72. }
  73. else
  74. {
  75. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusCannotDecrypt;
  76. }
  77. }
  78. #endregion
  79. #region 逻辑函数
  80. private void OpenCheckPasswordDialog()
  81. {
  82. FolderBrowserDialog folderDialog = new FolderBrowserDialog();
  83. System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  84. /*
  85. *设置这个对话框的起始保存路径
  86. */
  87. sfd.InitialDirectory = document.FilePath;
  88. /*
  89. *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
  90. */
  91. sfd.Filter = "PDF|*.pdf;";
  92. /*
  93. *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  94. **/
  95. sfd.FileName = document.FileName + "_CompressFile.pdf";
  96. /*
  97. * 做一些工作
  98. */
  99. if (CheckNeedPassword() == DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword)
  100. {
  101. bool IfDiscryptied = false;
  102. DialogParameters value = new DialogParameters();
  103. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  104. dialogs.ShowDialog(DialogNames.CheckPasswordDialog, value, e => { IfDiscryptied = e.Parameters.GetValue<bool>("CheckPassword"); });
  105. if (IfDiscryptied)
  106. {
  107. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  108. {
  109. document.Descrypt(sfd.FileName);
  110. MessageBox.Show("保存成功");
  111. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  112. }
  113. else
  114. {
  115. MessageBox.Show("取消保存");
  116. }
  117. }
  118. else if (CheckNeedPassword() == DeleteSafetySettintgsModel.EnumNeedPassword.StatusDirectDescrypt)
  119. {
  120. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  121. {
  122. document.Descrypt(sfd.FileName);
  123. MessageBox.Show("Ok");
  124. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  125. }
  126. else
  127. {
  128. MessageBox.Show("取消保存");
  129. }
  130. }
  131. else if (CheckNeedPassword() == DeleteSafetySettintgsModel.EnumNeedPassword.StatusCannotDecrypt)
  132. {
  133. MessageBox.Show("无可用安全性设置");
  134. }
  135. else
  136. { }
  137. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
  138. }
  139. }
  140. private void Cancel()
  141. {
  142. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
  143. }
  144. #endregion
  145. #region 框架相关
  146. public string Title => "";
  147. public event Action<IDialogResult> RequestClose;
  148. public bool CanCloseDialog()
  149. {
  150. return true;
  151. }
  152. public void OnDialogClosed()
  153. {
  154. }
  155. public void OnDialogOpened(IDialogParameters parameters)
  156. {
  157. CPDFDocument doc = null;
  158. parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
  159. if (doc != null)
  160. {
  161. document = doc;
  162. }
  163. }
  164. #endregion
  165. }
  166. }