DecryptionDialog.xaml.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Runtime.CompilerServices;
  6. using System.Windows;
  7. using System.Windows.Forms;
  8. using Compdfkit_Tools.Common;
  9. using ComPDFKit.PDFDocument;
  10. namespace Compdfkit_Tools.PDFControl
  11. {
  12. public partial class DecryptionDialog : Window, INotifyPropertyChanged
  13. {
  14. private CPDFDocument document;
  15. private List<string> failedList = new List<string>();
  16. public bool CanRemove => FileListControl.FileInfoDataList.Count > 0;
  17. public event PropertyChangedEventHandler PropertyChanged;
  18. public DecryptionDialog()
  19. {
  20. InitializeComponent();
  21. DataContext = this;
  22. FileListControl.FileNumChanged += (sender, args) =>
  23. {
  24. OnPropertyChanged(nameof(CanRemove));
  25. };
  26. }
  27. private void Cancel_Click(object sender, RoutedEventArgs e)
  28. {
  29. this.Close();
  30. }
  31. private void Remove_Click(object sender, RoutedEventArgs e)
  32. {
  33. var dialog = new FolderBrowserDialog();
  34. dialog.ShowDialog();
  35. var savePath = dialog.SelectedPath;
  36. if (string.IsNullOrEmpty(savePath)) return;
  37. foreach (var fileInfoData in FileListControl.FileInfoDataList)
  38. {
  39. document = CPDFDocument.InitWithFilePath(fileInfoData.Location);
  40. if (document.IsLocked)
  41. {
  42. PasswordWindow window = new PasswordWindow();
  43. window.InitWithDocument(document);
  44. window.Owner = this;
  45. window.ShowDialog();
  46. if (document.IsLocked)
  47. {
  48. failedList.Add(fileInfoData.Location);
  49. continue;
  50. }
  51. }
  52. var fullSavePath = savePath + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(fileInfoData.FileName) + "_Encrypted.pdf";
  53. if(!document.Decrypt(fullSavePath))
  54. {
  55. failedList.Add(fileInfoData.Location);
  56. }
  57. document.Release();
  58. }
  59. if (failedList.Count > 0)
  60. {
  61. var message = "Failed to remove password for the following files:\n" + string.Join("\n", failedList);
  62. System.Windows.MessageBox.Show(message, "Failed to remove password", MessageBoxButton.OK, MessageBoxImage.Warning);
  63. }
  64. if(failedList.Count < FileListControl.FileInfoDataList.Count)
  65. {
  66. System.Diagnostics.Process.Start(savePath);
  67. }
  68. this.Close();
  69. }
  70. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  71. {
  72. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  73. }
  74. }
  75. }