EncryptionDialog.xaml.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.CompilerServices;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Forms;
  11. using Compdfkit_Tools.Common;
  12. using Compdfkit_Tools.Helper;
  13. using ComPDFKit.PDFDocument;
  14. using MessageBox = System.Windows.MessageBox;
  15. namespace Compdfkit_Tools.PDFControl
  16. {
  17. public partial class EncryptionDialog : Window, INotifyPropertyChanged
  18. {
  19. private int _currentFileNum;
  20. public int CurrentFileNum
  21. {
  22. get => _currentFileNum;
  23. set => UpdateProper(ref _currentFileNum, value);
  24. }
  25. private int _fileNum;
  26. public int FileNum
  27. {
  28. get => _fileNum;
  29. set => UpdateProper(ref _fileNum, value);
  30. }
  31. private bool isEnsure;
  32. public event PropertyChangedEventHandler PropertyChanged;
  33. public bool CanEncrypt
  34. {
  35. get { return FileListControl.FileInfoDataList.Count > 0 && SetEncryptionControl.IsSettingValid; }
  36. }
  37. private List<string> failedList = new List<string>();
  38. public EncryptionDialog()
  39. {
  40. InitializeComponent();
  41. DataContext = this;
  42. CurrentFileNum = 0;
  43. Title = LanguageHelper.SecurityManager.GetString("Title_Security");
  44. }
  45. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  46. {
  47. this.Close();
  48. }
  49. private void ButtonEncrypt_Click(object sender, RoutedEventArgs e)
  50. {
  51. if (SetEncryptionControl.IsUserPasswordEnabled && SetEncryptionControl.IsOwnerPasswordEnabled)
  52. {
  53. if(SetEncryptionControl.UserPassword == SetEncryptionControl.OwnerPassword)
  54. {
  55. MessageBox.Show(LanguageHelper.SecurityManager.GetString("Warn_SamePassword"),
  56. LanguageHelper.CommonManager.GetString("Caption_Warning"), MessageBoxButton.OK, MessageBoxImage.Warning);
  57. return;
  58. }
  59. }
  60. var dialog = new FolderBrowserDialog();
  61. dialog.ShowDialog();
  62. var savePath = dialog.SelectedPath;
  63. if (string.IsNullOrEmpty(savePath)) return;
  64. CurrentFileNum = 0;
  65. var permissionsInfo = new CPDFPermissionsInfo();
  66. string userPassword = string.Empty;
  67. string ownerPassword = string.Empty;
  68. if (SetEncryptionControl.IsOwnerPasswordEnabled && !string.IsNullOrEmpty(SetEncryptionControl.OwnerPassword))
  69. {
  70. permissionsInfo.AllowsPrinting = SetEncryptionControl.IsAllowPrint;
  71. permissionsInfo.AllowsCopying = SetEncryptionControl.IsAllowCopy;
  72. ownerPassword = SetEncryptionControl.OwnerPassword;
  73. }
  74. if (SetEncryptionControl.IsUserPasswordEnabled && !string.IsNullOrEmpty(SetEncryptionControl.UserPassword))
  75. {
  76. userPassword = SetEncryptionControl.UserPassword;
  77. }
  78. foreach (var fileInfoData in FileListControl.FileInfoDataList)
  79. {
  80. try
  81. {
  82. var fullSavePath = savePath + Path.DirectorySeparatorChar +
  83. Path.GetFileNameWithoutExtension(fileInfoData.FileName) + "_Encrypted.pdf";
  84. fileInfoData.Document.Encrypt(userPassword, ownerPassword, permissionsInfo,
  85. (CPDFDocumentEncryptionLevel)SetEncryptionControl.CryptographicLevel);
  86. if (!fileInfoData.Document.WriteToFilePath(fullSavePath))
  87. {
  88. failedList.Add(fileInfoData.Location);
  89. }
  90. fileInfoData.Document.Release();
  91. }
  92. catch (Exception exception)
  93. {
  94. failedList.Add(fileInfoData.Location);
  95. }
  96. }
  97. if (failedList.Count > 0)
  98. {
  99. System.Windows.MessageBox.Show("Failed to encrypt the following files:\n" + string.Join("\n", failedList));
  100. }
  101. if (failedList.Count < FileListControl.FileInfoDataList.Count)
  102. {
  103. System.Diagnostics.Process.Start("explorer.exe", "/select," + savePath + Path.DirectorySeparatorChar +
  104. Path.GetFileNameWithoutExtension(FileListControl.FileInfoDataList[0].FileName) + "_Encrypted.pdf");
  105. }
  106. this.Close();
  107. }
  108. protected override void OnClosed(EventArgs e)
  109. {
  110. foreach (var fileInfo in FileListControl.FileInfoDataList)
  111. {
  112. fileInfo.Document.Release();
  113. }
  114. base.OnClosed(e);
  115. }
  116. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  117. {
  118. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  119. }
  120. protected void UpdateProper<T>(ref T properValue,
  121. T newValue,
  122. [CallerMemberName] string properName = "")
  123. {
  124. if (object.Equals(properValue, newValue))
  125. return;
  126. properValue = newValue;
  127. OnPropertyChanged(properName);
  128. }
  129. }
  130. }