DeleteSafetySettingsDialogViewModel.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 RemoveSecurityCommand { get; set; }
  29. public DelegateCommand CancelCommand { get; set; }
  30. #endregion
  31. public DeleteSafetySettingsDialogViewModel(IDialogService dialogService)
  32. {
  33. dialogs = dialogService;
  34. RemoveSecurityCommand = new DelegateCommand(RemoveSecurity);
  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. public void RemoveSecurity()
  74. {
  75. EnumNeedPassword enumNeedPassword = CheckNeedPassword();
  76. bool isDiscryptied = false;
  77. FolderBrowserDialog folderDialog = new FolderBrowserDialog();
  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 + "_CompressFile.pdf";
  91. /*
  92. * 做一些工作
  93. */
  94. if (enumNeedPassword == EnumNeedPassword.StatusVerifyPassword)
  95. {
  96. DialogParameters value = new DialogParameters();
  97. value.Add("PasswordKind", EnumPasswordKind.StatusPermissionsPassword);
  98. dialogs.ShowDialog(DialogNames.CheckPasswordDialog, value, e => { isDiscryptied = e.Parameters.GetValue<bool>("CheckPassword"); });
  99. if (isDiscryptied)
  100. {
  101. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  102. {
  103. document.Descrypt(sfd.FileName);
  104. MessageBoxEx.Show("保存成功");
  105. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  106. }
  107. else
  108. {
  109. MessageBox.Show("取消保存");
  110. }
  111. }
  112. else
  113. {
  114. }
  115. }
  116. else if (CheckNeedPassword() == DeleteSafetySettintgsModel.EnumNeedPassword.StatusDirectDescrypt)
  117. {
  118. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  119. {
  120. document.Descrypt(sfd.FileName);
  121. MessageBoxEx.Show("Ok");
  122. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  123. }
  124. else
  125. {
  126. MessageBoxEx.Show("取消保存");
  127. }
  128. }
  129. else if (CheckNeedPassword() == DeleteSafetySettintgsModel.EnumNeedPassword.StatusCannotDecrypt)
  130. {
  131. MessageBoxEx.Show("无可用安全性设置");
  132. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  133. }
  134. else
  135. { }
  136. }
  137. private void CheckPassword()
  138. {
  139. FolderBrowserDialog folderDialog = new FolderBrowserDialog();
  140. System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  141. /*
  142. *设置这个对话框的起始保存路径
  143. */
  144. sfd.InitialDirectory = document.FilePath;
  145. /*
  146. *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
  147. */
  148. sfd.Filter = "PDF|*.pdf;";
  149. /*
  150. *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  151. **/
  152. sfd.FileName = document.FileName + "_CompressFile.pdf";
  153. /*
  154. * 做一些工作
  155. */
  156. if (CheckNeedPassword() == DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword)
  157. {
  158. bool IfDiscryptied = false;
  159. DialogParameters value = new DialogParameters();
  160. dialogs.ShowDialog(DialogNames.CheckPasswordDialog, value, e => { IfDiscryptied = e.Parameters.GetValue<bool>("CheckPassword"); });
  161. if (IfDiscryptied)
  162. {
  163. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  164. {
  165. document.Descrypt(sfd.FileName);
  166. MessageBox.Show("保存成功");
  167. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  168. }
  169. else
  170. {
  171. MessageBox.Show("取消保存");
  172. }
  173. }
  174. else if (CheckNeedPassword() == DeleteSafetySettintgsModel.EnumNeedPassword.StatusDirectDescrypt)
  175. {
  176. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  177. {
  178. document.Descrypt(sfd.FileName);
  179. MessageBox.Show("Ok");
  180. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  181. }
  182. else
  183. {
  184. MessageBox.Show("取消保存");
  185. }
  186. }
  187. else if (CheckNeedPassword() == DeleteSafetySettintgsModel.EnumNeedPassword.StatusCannotDecrypt)
  188. {
  189. MessageBox.Show("无可用安全性设置");
  190. }
  191. else
  192. { }
  193. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
  194. }
  195. }
  196. private void Cancel()
  197. {
  198. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
  199. }
  200. #endregion
  201. #region 框架相关
  202. public string Title => "";
  203. public event Action<IDialogResult> RequestClose;
  204. public bool CanCloseDialog()
  205. {
  206. return true;
  207. }
  208. public void OnDialogClosed()
  209. {
  210. }
  211. public void OnDialogOpened(IDialogParameters parameters)
  212. {
  213. CPDFDocument doc = null;
  214. parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
  215. if (doc != null)
  216. {
  217. document = doc;
  218. DeleteSecurityMsg = "Are you sure you want to remove the security settings for”" + document.FileName + "” documents?";
  219. SecurityHelper.VerifyPassword(document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  220. }
  221. }
  222. #endregion
  223. }
  224. }