SetPasswordDialogViewModel.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. using ComPDFKit.PDFDocument;
  2. using PDF_Master.CustomControl;
  3. using PDF_Master.Model;
  4. using PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using Prism.Services.Dialogs;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Runtime.CompilerServices;
  12. using System.Windows;
  13. using System.Windows.Forms;
  14. using System.Windows.Controls;
  15. using CheckBox = System.Windows.Controls.CheckBox;
  16. using System.Drawing.Printing;
  17. using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.SetPasswordDialogModel;
  18. using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
  19. using ComPDFKitViewer.PdfViewer;
  20. using Prism.Regions;
  21. using PDF_Master.Helper;
  22. using System.Diagnostics;
  23. using PDF_Master.Properties;
  24. namespace PDF_Master.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
  25. {
  26. public class SetPasswordDialogViewModel : BindableBase, IDialogAware
  27. {
  28. #region 文案
  29. private string T_title;
  30. public string T_Title
  31. {
  32. get { return T_title; }
  33. set
  34. {
  35. SetProperty(ref T_title, value);
  36. }
  37. }
  38. private string T_openTitle;
  39. public string T_OpenTitle
  40. {
  41. get { return T_openTitle; }
  42. set
  43. {
  44. SetProperty(ref T_openTitle, value);
  45. }
  46. }
  47. private string T_permissionTitle;
  48. public string T_PermissionTitle
  49. {
  50. get { return T_permissionTitle; }
  51. set
  52. {
  53. SetProperty(ref T_permissionTitle, value);
  54. }
  55. }
  56. private string T_restrictPrinting;
  57. public string T_RestrictPrinting
  58. {
  59. get { return T_restrictPrinting; }
  60. set
  61. {
  62. SetProperty(ref T_restrictPrinting, value);
  63. }
  64. }
  65. private string T_restrictCopying;
  66. public string T_RestrictCopying
  67. {
  68. get { return T_restrictCopying; }
  69. set
  70. {
  71. SetProperty(ref T_restrictCopying, value);
  72. }
  73. }
  74. private string T_yes;
  75. public string T_Yes
  76. {
  77. get { return T_yes; }
  78. set
  79. {
  80. SetProperty(ref T_yes, value);
  81. }
  82. }
  83. private string T_no;
  84. public string T_No
  85. {
  86. get { return T_no; }
  87. set
  88. {
  89. SetProperty(ref T_no, value);
  90. }
  91. }
  92. private void InitString()
  93. {
  94. T_Title = App.MainPageLoader.GetString("SetPassword_Title");
  95. T_OpenTitle = App.MainPageLoader.GetString("SetPassword_OpenTitle");
  96. T_PermissionTitle = App.MainPageLoader.GetString("SetPassword_PermissionTitle");
  97. T_RestrictPrinting = App.MainPageLoader.GetString("SetPassword_RestrictPrinting");
  98. T_RestrictCopying = App.MainPageLoader.GetString("SetPassword_RestrictCopying");
  99. T_Yes = App.MainPageLoader.GetString("SetPassword_YES");
  100. T_No = App.MainPageLoader.GetString("SetPassword_No");
  101. }
  102. #endregion
  103. #region 参数和属性
  104. private SetPasswordDialogModel setPasswordDialogModel = new SetPasswordDialogModel();
  105. private CPDFViewer cPDFViewer;
  106. public IDialogService dialogs;
  107. private ViewContentViewModel viewContentViewModel;
  108. private EnumPermissionsMod enumPermissionsMod = EnumPermissionsMod.StatusUnknown | EnumPermissionsMod.StatusRestrictPrinting | EnumPermissionsMod.StatusRestrictCopying;
  109. private bool haveViewModel = false;
  110. private bool _enableConfirm = false;
  111. public bool EnableConfirm
  112. {
  113. get { return _enableConfirm; }
  114. set { SetProperty(ref _enableConfirm, value); }
  115. }
  116. private string _openPassword = "";
  117. public string OpenPassword
  118. {
  119. get { return _openPassword; }
  120. set
  121. {
  122. SetProperty(ref _openPassword, value);
  123. CheckEnableConfirmEncrypt();
  124. }
  125. }
  126. private string _permissionsPassword = "";
  127. public string PermissionsPassword
  128. {
  129. get { return _permissionsPassword; }
  130. set
  131. {
  132. SetProperty(ref _permissionsPassword, value);
  133. CheckEnableConfirmEncrypt();
  134. }
  135. }
  136. private string _openPasswordMsg = App.MainPageLoader.GetString("SetPassword_OpenEmptyText");
  137. public string OpenPasswordMsg
  138. {
  139. get { return _openPasswordMsg; }
  140. set
  141. {
  142. SetProperty(ref _openPasswordMsg, value);
  143. }
  144. }
  145. private string _permissionsPasswordMsg = App.MainPageLoader.GetString("SetPassword_PermissionEmptyText");
  146. public string PermissionsPasswordMsg
  147. {
  148. get { return _permissionsPasswordMsg; }
  149. set
  150. {
  151. SetProperty(ref _permissionsPasswordMsg, value);
  152. }
  153. }
  154. private Visibility _openTextVisibility = Visibility.Collapsed;
  155. public Visibility OpenTextVisibility
  156. {
  157. get { return _openTextVisibility; }
  158. set
  159. {
  160. SetProperty(ref _openTextVisibility, value);
  161. }
  162. }
  163. private Visibility _openPasswordVisibility = Visibility.Visible;
  164. public Visibility OpenPasswordVisibility
  165. {
  166. get { return _openPasswordVisibility; }
  167. set
  168. {
  169. if (value == Visibility.Visible)
  170. {
  171. OpenTextVisibility = Visibility.Collapsed;
  172. }
  173. else
  174. {
  175. OpenTextVisibility = Visibility.Visible;
  176. }
  177. SetProperty(ref _openPasswordVisibility, value);
  178. }
  179. }
  180. private Visibility _permissionsPasswordVisibility = Visibility.Visible;
  181. public Visibility PermissionsPasswordVisibility
  182. {
  183. get { return _permissionsPasswordVisibility; }
  184. set
  185. {
  186. if (value == Visibility.Visible)
  187. {
  188. PermissionsTextVisibility = Visibility.Collapsed;
  189. }
  190. else
  191. {
  192. PermissionsTextVisibility = Visibility.Visible;
  193. }
  194. SetProperty(ref _permissionsPasswordVisibility, value);
  195. }
  196. }
  197. private Visibility _permissionsTextVisibility = Visibility.Collapsed;
  198. public Visibility PermissionsTextVisibility
  199. {
  200. get { return _permissionsTextVisibility; }
  201. set { SetProperty(ref _permissionsTextVisibility, value); }
  202. }
  203. private bool? _isRestrictPrinting;
  204. public bool? IsRestrictPrinting
  205. {
  206. get { return _isRestrictPrinting; }
  207. set { SetProperty(ref _isRestrictPrinting, value); }
  208. }
  209. private bool? _isRestrictCopying;
  210. public bool? IsRestrictCopying
  211. {
  212. get { return _isRestrictCopying; }
  213. set { SetProperty(ref _isRestrictCopying, value); }
  214. }
  215. private bool _needOpenPassword;
  216. public bool NeedOpenPassword
  217. {
  218. get { return _needOpenPassword; }
  219. set { SetProperty(ref _needOpenPassword, value); }
  220. }
  221. private bool _needPermissionsPassword;
  222. public bool NeedPermissionsPassword
  223. {
  224. get { return _needPermissionsPassword; }
  225. set { SetProperty(ref _needPermissionsPassword, value); }
  226. }
  227. private bool _isOpenPasswordDisplayed = false;
  228. public bool IsOpenPasswordDisplayed
  229. {
  230. get { return _isOpenPasswordDisplayed; }
  231. set { SetProperty(ref _isOpenPasswordDisplayed, value); }
  232. }
  233. private bool _isPermissionsPasswordDisplayed = false;
  234. public bool IsPermissionsPasswordDisplayed
  235. {
  236. get { return _isPermissionsPasswordDisplayed; }
  237. set { SetProperty(ref _isPermissionsPasswordDisplayed, value); }
  238. }
  239. #endregion
  240. #region 委托声明
  241. public DelegateCommand<object> DisplayPasswordCommand { get; set; }
  242. public DelegateCommand<object> SetOpenPasswordCommand { get; set; }
  243. public DelegateCommand<object> SetPermissionsPasswordCommand { get; set; }
  244. public DelegateCommand<object> SetRestrictCommand { get; set; }
  245. public DelegateCommand EncryptCommand { get; set; }
  246. public DelegateCommand CancelEncryptCommand { get; set; }
  247. public DelegateCommand BatchSetPasswordCommand { get; set; }
  248. #endregion
  249. public SetPasswordDialogViewModel(IDialogService dialogService)
  250. {
  251. InitString();
  252. dialogs = dialogService;
  253. DisplayPasswordCommand = new DelegateCommand<object>(DisplayPassword);
  254. SetOpenPasswordCommand = new DelegateCommand<object>(SetOpenPassword);
  255. SetPermissionsPasswordCommand = new DelegateCommand<object>(SetPermissionsPassword);
  256. SetRestrictCommand = new DelegateCommand<object>(SetRestrict);
  257. EncryptCommand = new DelegateCommand(Encrypt);
  258. CancelEncryptCommand = new DelegateCommand(CancelEncrypt);
  259. BatchSetPasswordCommand = new DelegateCommand(BatchSetPassword);
  260. }
  261. #region 检查和初始化
  262. private void CheckEnableConfirmEncrypt()
  263. {
  264. EnableConfirm = true;
  265. if (!(NeedOpenPassword || NeedPermissionsPassword))
  266. {
  267. EnableConfirm = false;
  268. }
  269. if (NeedOpenPassword && string.IsNullOrEmpty(OpenPassword))
  270. {
  271. EnableConfirm = false;
  272. }
  273. if (NeedPermissionsPassword && string.IsNullOrEmpty(PermissionsPassword))
  274. {
  275. EnableConfirm = false;
  276. }
  277. if (NeedPermissionsPassword && (!((bool)IsRestrictCopying || (bool)IsRestrictPrinting)))
  278. {
  279. EnableConfirm = false;
  280. }
  281. }
  282. #endregion
  283. #region 逻辑函数
  284. public void DisplayPassword(object e)
  285. {
  286. var chk = e as CheckBox;
  287. if (chk != null)
  288. {
  289. if (chk.Name == "DisplayOpenPasswordChk")
  290. {
  291. if (chk.IsChecked == true)
  292. {
  293. OpenPasswordVisibility = Visibility.Collapsed;
  294. }
  295. else
  296. {
  297. OpenPasswordVisibility = Visibility.Visible;
  298. }
  299. }
  300. else
  301. {
  302. if (chk.IsChecked == true)
  303. {
  304. PermissionsPasswordVisibility = Visibility.Collapsed;
  305. }
  306. else
  307. {
  308. PermissionsPasswordVisibility = Visibility.Visible;
  309. }
  310. }
  311. }
  312. }
  313. public void SetOpenPassword(object e)
  314. {
  315. var chk = e as CheckBox;
  316. if (chk != null)
  317. {
  318. IsOpenPasswordDisplayed = false;
  319. OpenPasswordVisibility = Visibility.Visible;
  320. }
  321. CheckEnableConfirmEncrypt();
  322. }
  323. public void SetPermissionsPassword(object e)
  324. {
  325. var chk = e as CheckBox;
  326. if (chk != null)
  327. {
  328. IsPermissionsPasswordDisplayed = false;
  329. PermissionsPasswordVisibility = Visibility.Visible;
  330. if (chk.IsChecked == true)
  331. {
  332. IsRestrictPrinting = true;
  333. IsRestrictCopying = true;
  334. }
  335. else
  336. {
  337. IsRestrictPrinting = false;
  338. IsRestrictCopying = false;
  339. }
  340. }
  341. CheckEnableConfirmEncrypt();
  342. }
  343. public void SetRestrict(object e)
  344. {
  345. var chk = e as CheckBox;
  346. if (chk != null)
  347. {
  348. if ((bool)chk.IsChecked)
  349. {
  350. if (chk.Tag.ToString() == "Printing")
  351. {
  352. enumPermissionsMod |= EnumPermissionsMod.StatusRestrictPrinting;
  353. }
  354. else if (chk.Tag.ToString() == "Copying")
  355. {
  356. enumPermissionsMod |= EnumPermissionsMod.StatusRestrictCopying;
  357. }
  358. }
  359. else
  360. {
  361. if (chk.Tag.ToString() == "Printing")
  362. {
  363. enumPermissionsMod -= EnumPermissionsMod.StatusRestrictPrinting;
  364. }
  365. else if (chk.Tag.ToString() == "Copying")
  366. {
  367. enumPermissionsMod -= EnumPermissionsMod.StatusRestrictCopying;
  368. }
  369. }
  370. }
  371. CheckEnableConfirmEncrypt();
  372. }
  373. private void BatchSetPassword()
  374. {
  375. DialogParameters setPasswordpdf = new DialogParameters();
  376. setPasswordpdf.Add(ParameterNames.BatchProcessing_Name, "2");
  377. HomePageBatchProcessingDialogModel.FilePaths = new List<string> { cPDFViewer.Document.FilePath.ToString() };
  378. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 2;
  379. setPasswordpdf.Add(ParameterNames.FilePath, new string[] { cPDFViewer.Document.FilePath.ToString() });
  380. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, setPasswordpdf, e => { });
  381. }
  382. public void Encrypt()
  383. {
  384. if (haveViewModel)
  385. {
  386. viewContentViewModel.SecurityInfo.IsPasswordChanged = true;
  387. viewContentViewModel.SecurityInfo.IsPasswordRemoved= false;
  388. if (NeedOpenPassword && (!string.IsNullOrEmpty(OpenPassword)))
  389. {
  390. this.viewContentViewModel.SecurityInfo.NeedOpenPassword = true;
  391. this.viewContentViewModel.SecurityInfo.OpenPassword = OpenPassword;
  392. }
  393. else
  394. {
  395. this.viewContentViewModel.SecurityInfo.NeedOpenPassword = false;
  396. viewContentViewModel.SecurityInfo.OpenPassword = null;
  397. }
  398. if (NeedPermissionsPassword && (!string.IsNullOrEmpty(PermissionsPassword)))
  399. {
  400. this.viewContentViewModel.SecurityInfo.NeedPermissionsPassword = true;
  401. viewContentViewModel.SecurityInfo.PermissionsPassword = PermissionsPassword;
  402. viewContentViewModel.SecurityInfo.CPDFPermissionsInfo = CreatePermissionsInfo(enumPermissionsMod);
  403. }
  404. else
  405. {
  406. this.viewContentViewModel.SecurityInfo.NeedPermissionsPassword = false;
  407. viewContentViewModel.SecurityInfo.PermissionsPassword = null;
  408. viewContentViewModel.SecurityInfo.CPDFPermissionsInfo = CreateDefaultPermissionsInfo();
  409. }
  410. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  411. }
  412. else
  413. {
  414. string openPassword = "";
  415. string permissionsPassword = "";
  416. CPDFPermissionsInfo permissionsInfo = null;
  417. if (NeedOpenPassword)
  418. {
  419. if (!string.IsNullOrEmpty(OpenPassword))
  420. {
  421. openPassword = OpenPassword;
  422. }
  423. }
  424. if (NeedPermissionsPassword && (!string.IsNullOrEmpty(PermissionsPassword)))
  425. {
  426. permissionsPassword = PermissionsPassword;
  427. permissionsInfo = CreatePermissionsInfo(enumPermissionsMod);
  428. }
  429. else
  430. {
  431. permissionsInfo = CreateDefaultPermissionsInfo();
  432. }
  433. System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  434. /*
  435. *设置这个对话框的起始保存路径
  436. */
  437. sfd.InitialDirectory = cPDFViewer.Document.FilePath;
  438. /*
  439. *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
  440. */
  441. sfd.Filter = "PDF|*.pdf;";
  442. /*
  443. *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  444. */
  445. sfd.FileName = cPDFViewer.Document.FileName + "_EncryptedFile.pdf";
  446. /*
  447. * 做一些工作
  448. */
  449. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  450. {
  451. cPDFViewer.Document.Encrypt(openPassword, permissionsPassword, permissionsInfo);
  452. cPDFViewer.Document.WriteToFilePath(sfd.FileName);
  453. //保存成功后没必要弹窗提示,直接显示文件浏览器,选中保存后的文档流程更简洁
  454. //AlertsMessage alertsMessage = new AlertsMessage();
  455. //alertsMessage.ShowDialog("保存成功","","确认");
  456. DialogParameters dialogParameters = new DialogParameters();
  457. dialogParameters.Add("FilePath", sfd.FileName);
  458. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK, dialogParameters));
  459. }
  460. else
  461. {
  462. }
  463. }
  464. }
  465. //public void Encrypt()
  466. //{
  467. // string openPassword = "";
  468. // string permissionsPassword = "";
  469. // CPDFPermissionsInfo permissionsInfo = null;
  470. // if (NeedOpenPassword)
  471. // {
  472. // if (!string.IsNullOrEmpty(OpenPassword))
  473. // {
  474. // openPassword = OpenPassword;
  475. // }
  476. // }
  477. // if (NeedPermissionsPassword && (!string.IsNullOrEmpty(PermissionsPassword)))
  478. // {
  479. // permissionsPassword = PermissionsPassword;
  480. // permissionsInfo = CreatePermissionsInfo(enumPermissionsMod);
  481. // }
  482. // else
  483. // {
  484. // permissionsInfo = CreateDefaultPermissionsInfo();
  485. // }
  486. // System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  487. // /*
  488. // *设置这个对话框的起始保存路径
  489. // */
  490. // sfd.InitialDirectory = cPDFViewer.Document.FilePath;
  491. // /*
  492. // *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
  493. // */
  494. // sfd.Filter = "PDF|*.pdf;";
  495. // /*
  496. // *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  497. // */
  498. // sfd.FileName = cPDFViewer.Document.FileName + "_EncryptedFile.pdf";
  499. // /*
  500. // * 做一些工作
  501. // */
  502. // if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  503. // {
  504. // cPDFViewer.Document.Encrypt(openPassword, permissionsPassword, permissionsInfo);
  505. // cPDFViewer.Document.WriteToFilePath(sfd.FileName);
  506. // MessageBoxEx.Show("保存成功");
  507. // RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  508. // }
  509. // else
  510. // {
  511. // }
  512. //}
  513. public void CancelEncrypt() => RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
  514. #endregion
  515. #region 框架相关
  516. public string Title => "";
  517. public event Action<IDialogResult> RequestClose;
  518. public bool CanCloseDialog()
  519. {
  520. return true;
  521. }
  522. public void OnDialogClosed()
  523. {
  524. }
  525. public void OnDialogOpened(IDialogParameters parameters)
  526. {
  527. if (parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel))
  528. {
  529. haveViewModel = true;
  530. }
  531. else
  532. {
  533. haveViewModel = false;
  534. }
  535. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out cPDFViewer);
  536. }
  537. #endregion
  538. }
  539. }