DeleteSafetySettingsDialogViewModel.cs 6.7 KB

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