DeleteSafetySettingsDialogViewModel.cs 7.2 KB

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