DeleteSafetySettingsDialogViewModel.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. private string T_yes;
  18. public string T_YES
  19. {
  20. get { return T_yes; }
  21. set
  22. {
  23. SetProperty(ref T_yes, value);
  24. }
  25. }
  26. private string T_no;
  27. public string T_No
  28. {
  29. get { return T_no; }
  30. set
  31. {
  32. SetProperty(ref T_no, value);
  33. }
  34. }
  35. private void InitString()
  36. {
  37. T_YES = App.MainPageLoader.GetString("RemovePassword_YES");
  38. T_No = App.MainPageLoader.GetString("RemovePassword_No");
  39. }
  40. #endregion
  41. #region 参数和属性
  42. DeleteSafetySettintgsModel deleteSafetySettintgsModel;
  43. public IDialogService dialogs;
  44. private CPDFDocument document;
  45. private string _deleteSecurityMsg;
  46. public string DeleteSecurityMsg
  47. {
  48. get { return _deleteSecurityMsg; }
  49. set { SetProperty(ref _deleteSecurityMsg, value); }
  50. }
  51. #endregion
  52. #region 委托声明
  53. public DelegateCommand CancelCommand { get; set; }
  54. public DelegateCommand RemoveSecuritySettingsCommand { get; set; }
  55. #endregion
  56. public DeleteSafetySettingsDialogViewModel(IDialogService dialogService)
  57. {
  58. InitString();
  59. dialogs = dialogService;
  60. RemoveSecuritySettingsCommand = new DelegateCommand(RemoveSecuritySettings);
  61. CancelCommand = new DelegateCommand(Cancel);
  62. }
  63. #region 检查函数
  64. /// <summary>
  65. /// 检测是否需要输入密码
  66. /// </summary>
  67. /// <returns>
  68. /// EnumNeedPassword.Status
  69. ///
  70. ///
  71. ///
  72. /// :需要确认密码;
  73. /// EnumNeedPassword.StatusDirectDescrypt:直接执行操作
  74. /// EnumNeedPassword.StatusCannotDecrypt:无可执行操作。
  75. /// </returns>
  76. private DeleteSafetySettintgsModel.EnumNeedPassword CheckNeedPassword()
  77. {
  78. try
  79. {
  80. if (document.IsLocked)
  81. {
  82. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
  83. }
  84. else
  85. {
  86. if (SecurityHelper.CheckHaveAllPermissions(document))
  87. {
  88. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusDirectDescrypt;
  89. }
  90. else
  91. {
  92. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
  93. }
  94. }
  95. }
  96. catch
  97. {
  98. return EnumNeedPassword.StatusCannotDecrypt;
  99. }
  100. }
  101. #endregion
  102. #region 逻辑函数
  103. /// <summary>
  104. /// 逻辑优化:判权操作放到密码弹窗里,所以一旦进入该状态,可以直接清除权限
  105. /// </summary>
  106. public void RemoveSecuritySettings()
  107. {
  108. System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  109. /*
  110. *设置这个对话框的起始保存路径
  111. */
  112. sfd.InitialDirectory = document.FilePath;
  113. /*
  114. *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
  115. */
  116. sfd.Filter = "PDF|*.pdf;";
  117. /*
  118. *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  119. */
  120. sfd.FileName = document.FileName + "_DecryptedFile.pdf";
  121. /*
  122. * 做一些工作
  123. */
  124. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  125. {
  126. if(document.Descrypt(sfd.FileName)){
  127. MessageBoxEx.Show("保存成功");
  128. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  129. }
  130. else
  131. {
  132. ///TODO 不明原因干扰
  133. }
  134. }
  135. else
  136. {
  137. MessageBox.Show("取消保存");
  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. string info = App.MainPageLoader.GetString("RemovePassword_Context");
  163. string mas = string.Format(info, document.FileName);
  164. DeleteSecurityMsg = mas;
  165. }
  166. }
  167. #endregion
  168. }
  169. }