DeleteSafetySettingsDialogViewModel.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using ComPDFKit.PDFDocument;
  2. using PDF_Master.CustomControl;
  3. using PDF_Master.Helper;
  4. using PDF_Master.Model;
  5. using PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs;
  6. using PDF_Master.Properties;
  7. using Prism.Commands;
  8. using Prism.Mvvm;
  9. using Prism.Services.Dialogs;
  10. using System;
  11. using System.Windows;
  12. using System.Windows.Forms;
  13. using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  14. using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.SetPasswordDialogModel;
  15. namespace PDF_Master.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
  16. {
  17. public class DeleteSafetySettingsDialogViewModel : BindableBase, IDialogAware
  18. {
  19. #region 文案
  20. private string T_yes;
  21. public string T_YES
  22. {
  23. get { return T_yes; }
  24. set
  25. {
  26. SetProperty(ref T_yes, value);
  27. }
  28. }
  29. private string T_no;
  30. public string T_No
  31. {
  32. get { return T_no; }
  33. set
  34. {
  35. SetProperty(ref T_no, value);
  36. }
  37. }
  38. private ViewContentViewModel viewContentViewModel;
  39. private void InitString()
  40. {
  41. T_YES = App.MainPageLoader.GetString("RemovePassword_YES");
  42. T_No = App.MainPageLoader.GetString("RemovePassword_No");
  43. }
  44. #endregion
  45. #region 参数和属性
  46. public IDialogService dialogs;
  47. private CPDFDocument document;
  48. private string _deleteSecurityMsg;
  49. public string DeleteSecurityMsg
  50. {
  51. get { return _deleteSecurityMsg; }
  52. set { SetProperty(ref _deleteSecurityMsg, value); }
  53. }
  54. #endregion
  55. #region 委托声明
  56. public DelegateCommand CancelCommand { get; set; }
  57. public DelegateCommand RemoveSecuritySettingsCommand { get; set; }
  58. #endregion
  59. public DeleteSafetySettingsDialogViewModel(IDialogService dialogService)
  60. {
  61. InitString();
  62. dialogs = dialogService;
  63. RemoveSecuritySettingsCommand = new DelegateCommand(RemoveSecuritySettings);
  64. CancelCommand = new DelegateCommand(Cancel);
  65. }
  66. #region 检查函数
  67. /// <summary>
  68. /// 检测是否需要输入密码
  69. /// </summary>
  70. /// <returns>
  71. /// EnumNeedPassword.Status
  72. ///
  73. ///
  74. ///
  75. /// :需要确认密码;
  76. /// EnumNeedPassword.StatusDirectDescrypt:直接执行操作
  77. /// EnumNeedPassword.StatusCannotDecrypt:无可执行操作。
  78. /// </returns>
  79. private DeleteSafetySettintgsModel.EnumNeedPassword CheckNeedPassword()
  80. {
  81. try
  82. {
  83. if (document.IsLocked)
  84. {
  85. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
  86. }
  87. else
  88. {
  89. if (SecurityHelper.CheckHaveAllPermissions(document))
  90. {
  91. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusDirectDescrypt;
  92. }
  93. else
  94. {
  95. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword;
  96. }
  97. }
  98. }
  99. catch
  100. {
  101. return DeleteSafetySettintgsModel.EnumNeedPassword.StatusCannotDecrypt;
  102. }
  103. }
  104. #endregion
  105. #region 逻辑函数
  106. ///// <summary>
  107. ///// 逻辑优化:判权操作放到密码弹窗里,所以一旦进入该状态,可以直接清除权限
  108. ///// </summary>
  109. //public void RemoveSecuritySettings()
  110. //{
  111. // //付费锁
  112. // //if (!App.IsLogin)
  113. // //{
  114. // // dialogs.ShowDialog(DialogNames.IAPCompareDialog);
  115. // // return;
  116. // //}
  117. // System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  118. // /*
  119. // *设置这个对话框的起始保存路径
  120. // */
  121. // sfd.InitialDirectory = document.FilePath;
  122. // /*
  123. // *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
  124. // */
  125. // sfd.Filter = "PDF|*.pdf;";
  126. // /*
  127. // *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  128. // */
  129. // sfd.FileName = document.FileName + "_DecryptedFile.pdf";
  130. // /*
  131. // * 做一些工作
  132. // */
  133. // if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  134. // {
  135. // if(document.Descrypt(sfd.FileName)){
  136. // MessageBoxEx.Show("保存成功");
  137. // RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  138. // }
  139. // else
  140. // {
  141. // ///TODO 不明原因干扰
  142. // }
  143. // }
  144. // else
  145. // {
  146. // MessageBox.Show("取消保存");
  147. // }
  148. //}
  149. /// <summary>
  150. /// 去除安全性设置2.0
  151. /// </summary>
  152. public void RemoveSecuritySettings()
  153. {
  154. viewContentViewModel.SecurityInfo.NeedOpenPassword = false;
  155. viewContentViewModel.SecurityInfo.NeedPermissionsPassword = false;
  156. viewContentViewModel.SecurityInfo.OpenPassword = string.Empty;
  157. viewContentViewModel.SecurityInfo.PermissionsPassword = string.Empty;
  158. viewContentViewModel.SecurityInfo.CPDFPermissionsInfo = CreateDefaultPermissionsInfo();
  159. viewContentViewModel.SecurityInfo.IsPasswordRemoved = true;
  160. viewContentViewModel.SecurityInfo.IsPasswordChanged = false;
  161. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  162. }
  163. private void Cancel()
  164. {
  165. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
  166. }
  167. #endregion
  168. #region 框架相关
  169. public string Title => "";
  170. public event Action<IDialogResult> RequestClose;
  171. public bool CanCloseDialog()
  172. {
  173. return true;
  174. }
  175. public void OnDialogClosed()
  176. {
  177. }
  178. public void OnDialogOpened(IDialogParameters parameters)
  179. {
  180. CPDFDocument doc = null;
  181. parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
  182. parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  183. if (doc != null)
  184. {
  185. document = doc;
  186. string info = App.MainPageLoader.GetString("RemovePassword_Context");
  187. string mas = string.Format(info, document.FileName);
  188. DeleteSecurityMsg = mas;
  189. }
  190. }
  191. #endregion
  192. }
  193. }