ToolsBarContentViewModel.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using ComPDFKitViewer.PdfViewer;
  2. using PDF_Office.CustomControl;
  3. using PDF_Office.EventAggregators;
  4. using PDF_Office.Helper;
  5. using PDF_Office.Model;
  6. using PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs;
  7. using Prism.Commands;
  8. using Prism.Events;
  9. using Prism.Mvvm;
  10. using Prism.Regions;
  11. using Prism.Services.Dialogs;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Security.Cryptography.X509Certificates;
  16. using System.Windows;
  17. using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  18. namespace PDF_Office.ViewModels.Tools
  19. {
  20. public class ToolsBarContentViewModel : BindableBase, INavigationAware
  21. {
  22. private CPDFViewer PDFViewer;
  23. private ViewContentViewModel viewContentViewModel;
  24. public IDialogService dialogs;
  25. public IEventAggregator eventAggregator;
  26. public string unicode = null;
  27. /// <summary>
  28. /// 是否是第一次加载
  29. /// </summary>
  30. private bool isFirstLoad = true;
  31. public DelegateCommand CompressCommand { get; set; }
  32. public DelegateCommand MergeCommand { get; set; }
  33. public DelegateCommand SetPasswordCommand { get; set; }
  34. public DelegateCommand CancelPasswordCommand { get; set; }
  35. public DelegateCommand<object> SetEditToolsCommand { get; set; }
  36. public DelegateCommand SetWatermarkCommand { get; set; }
  37. public ToolsBarContentViewModel(IDialogService dialogService, IEventAggregator eventAggregator)
  38. {
  39. dialogs = dialogService;
  40. this.eventAggregator = eventAggregator;
  41. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  42. CompressCommand = new DelegateCommand(OpenCompressDialog);
  43. MergeCommand = new DelegateCommand(MergeDialog);
  44. SetPasswordCommand = new DelegateCommand(OpenSetPasswordDialog);
  45. CancelPasswordCommand = new DelegateCommand(OpenCancelPasswordDialog);
  46. SetEditToolsCommand = new DelegateCommand<object>(SetEditTools);
  47. }
  48. private void SetEditTools(object e)
  49. {
  50. var args = e as System.Windows.Controls.Button;
  51. if (args != null)
  52. {
  53. this.eventAggregator.GetEvent<EnterSelectedEditToolEvent>().Publish(new StringWithUnicode() { Unicode = unicode, EditToolsContentName = args.Name });
  54. }
  55. }
  56. private void OpenCompressDialog()
  57. {
  58. DialogParameters value = new DialogParameters();
  59. value.Add(ParameterNames.PDFViewer, PDFViewer);
  60. dialogs.ShowDialog(DialogNames.CompressDialog, value, e => { });
  61. }
  62. private void MergeDialog()
  63. {
  64. DialogParameters value = new DialogParameters();
  65. value.Add(ParameterNames.PDFViewer, PDFViewer);
  66. dialogs.ShowDialog(DialogNames.MergeDialog, value, e => { });
  67. }
  68. private void OpenSetPasswordDialog()
  69. {
  70. VerifyPasswordResult result = SecurityHelper.VerifyPasswordForSelectedPermissions(PDFViewer.Document, EnumPermissionsSet.StatusAllowsPrinting|EnumPermissionsSet.StatusAllowsHighQualityPrinting, dialogs);
  71. if (result.IsDiscryptied)
  72. {
  73. if (result.Password != null)
  74. {
  75. string filePath = PDFViewer.Document.FilePath;
  76. PDFViewer.Document.Release();
  77. PDFViewer.InitDocument(filePath);
  78. PDFViewer.Document.UnlockWithPassword(result.Password);
  79. }
  80. DialogParameters value = new DialogParameters();
  81. value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  82. dialogs.ShowDialog(DialogNames.SetPasswordDialog, value, e => { });
  83. }
  84. }
  85. private void OpenCancelPasswordDialog()
  86. {
  87. if (!PDFViewer.Document.IsEncrypted)
  88. {
  89. MessageBoxEx.Show("No security settings available ");
  90. }
  91. else
  92. {
  93. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  94. if (result.IsDiscryptied)
  95. {
  96. if (result.Password != null)
  97. {
  98. string filePath = PDFViewer.Document.FilePath;
  99. PDFViewer.Document.Release();
  100. PDFViewer.InitDocument(filePath);
  101. PDFViewer.Document.UnlockWithPassword(result.Password);
  102. }
  103. DialogParameters value = new DialogParameters();
  104. value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  105. dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e => { });
  106. }
  107. }
  108. }
  109. public bool IsNavigationTarget(NavigationContext navigationContext)
  110. {
  111. return true;
  112. }
  113. public void OnNavigatedFrom(NavigationContext navigationContext)
  114. {
  115. }
  116. public void OnNavigatedTo(NavigationContext navigationContext)
  117. {
  118. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  119. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  120. if (PDFViewer != null)
  121. {
  122. isFirstLoad = false;
  123. }
  124. }
  125. }
  126. }