DeleteSafetySettingsDialogViewModel.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. //付费锁
  109. if (!App.IsLogin)
  110. {
  111. dialogs.ShowDialog(DialogNames.IAPCompareDialog);
  112. return;
  113. }
  114. System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  115. /*
  116. *设置这个对话框的起始保存路径
  117. */
  118. sfd.InitialDirectory = document.FilePath;
  119. /*
  120. *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
  121. */
  122. sfd.Filter = "PDF|*.pdf;";
  123. /*
  124. *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  125. */
  126. sfd.FileName = document.FileName + "_DecryptedFile.pdf";
  127. /*
  128. * 做一些工作
  129. */
  130. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  131. {
  132. if(document.Descrypt(sfd.FileName)){
  133. MessageBoxEx.Show("保存成功");
  134. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  135. }
  136. else
  137. {
  138. ///TODO 不明原因干扰
  139. }
  140. }
  141. else
  142. {
  143. MessageBox.Show("取消保存");
  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. string info = App.MainPageLoader.GetString("RemovePassword_Context");
  169. string mas = string.Format(info, document.FileName);
  170. DeleteSecurityMsg = mas;
  171. }
  172. }
  173. #endregion
  174. }
  175. }