EncryptionDialog.xaml.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.PDFDocument;
  13. namespace Compdfkit_Tools.PDFControl
  14. {
  15. public partial class EncryptionDialog : Window, INotifyPropertyChanged
  16. {
  17. private int _currentFileNum;
  18. public int CurrentFileNum
  19. {
  20. get => _currentFileNum;
  21. set => UpdateProper(ref _currentFileNum, value);
  22. }
  23. private int _fileNum;
  24. public int FileNum
  25. {
  26. get => _fileNum;
  27. set => UpdateProper(ref _fileNum,value);
  28. }
  29. private bool isEnsure;
  30. public event PropertyChangedEventHandler PropertyChanged;
  31. public bool CanEncrypt
  32. {
  33. get { return FileListControl.FileInfoDataList.Count > 0 && SetEncryptionControl.IsSettingValid; }
  34. }
  35. private CPDFDocument document;
  36. private List<string> failedList = new List<string>();
  37. public EncryptionDialog()
  38. {
  39. InitializeComponent();
  40. DataContext = this;
  41. CurrentFileNum = 0;
  42. }
  43. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  44. {
  45. this.Close();
  46. }
  47. private void ButtonEncrypt_Click(object sender, RoutedEventArgs e)
  48. {
  49. var dialog = new FolderBrowserDialog();
  50. dialog.ShowDialog();
  51. var savePath = dialog.SelectedPath;
  52. if (string.IsNullOrEmpty(savePath)) return;
  53. CurrentFileNum = 0;
  54. var permissionsInfo = new CPDFPermissionsInfo();
  55. string userPassword = string.Empty;
  56. string ownerPassword = string.Empty;
  57. if (SetEncryptionControl.IsOwnerPasswordEnabled && !string.IsNullOrEmpty(SetEncryptionControl.OwnerPassword))
  58. {
  59. permissionsInfo.AllowsPrinting = SetEncryptionControl.IsAllowPrint;
  60. permissionsInfo.AllowsCopying = SetEncryptionControl.IsAllowCopy;
  61. ownerPassword = SetEncryptionControl.OwnerPassword;
  62. }
  63. if(SetEncryptionControl.IsUserPasswordEnabled && !string.IsNullOrEmpty(SetEncryptionControl.UserPassword))
  64. {
  65. userPassword = SetEncryptionControl.UserPassword;
  66. }
  67. foreach (var fileInfoData in FileListControl.FileInfoDataList)
  68. {
  69. CurrentFileNum++;
  70. document = CPDFDocument.InitWithFilePath(fileInfoData.Location);
  71. if (document.IsLocked)
  72. {
  73. PasswordWindow window = new PasswordWindow();
  74. window.InitWithDocument(document);
  75. window.Owner = this;
  76. window.PasswordDialog.SetShowText(fileInfoData.FileName + " is encrypted.");
  77. window.ShowDialog();
  78. if (document.IsLocked)
  79. {
  80. failedList.Add(fileInfoData.Location);
  81. continue;
  82. }
  83. }
  84. var fullSavePath = savePath + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(fileInfoData.FileName) + "_Encrypted.pdf";
  85. document.Encrypt(userPassword, ownerPassword, permissionsInfo, (CPDFDocumentEncryptionLevel)SetEncryptionControl.CryptographicLevel);
  86. if (!document.WriteToFilePath(fullSavePath))
  87. {
  88. failedList.Add(fileInfoData.Location);
  89. }
  90. document.Release();
  91. }
  92. if(failedList.Count > 0)
  93. {
  94. System.Windows.MessageBox.Show("Failed to encrypt the following files:\n" + string.Join("\n", failedList));
  95. }
  96. if(failedList.Count < FileListControl.FileInfoDataList.Count)
  97. {
  98. System.Diagnostics.Process.Start(savePath);
  99. }
  100. this.Close();
  101. }
  102. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  103. {
  104. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  105. }
  106. protected void UpdateProper<T>(ref T properValue,
  107. T newValue,
  108. [CallerMemberName] string properName = "")
  109. {
  110. if (object.Equals(properValue, newValue))
  111. return;
  112. properValue = newValue;
  113. OnPropertyChanged(properName);
  114. }
  115. }
  116. }