FileGridListControl.xaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.CompilerServices;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Input;
  13. using Compdfkit_Tools.Common;
  14. using Compdfkit_Tools.Helper;
  15. using ComPDFKit.PDFDocument;
  16. using Microsoft.Win32;
  17. using PasswordBoxPlus.Helper;
  18. namespace Compdfkit_Tools.PDFControl
  19. {
  20. public partial class FileGridListControl : UserControl, INotifyPropertyChanged
  21. {
  22. public static readonly DependencyProperty IsEnsureProperty =
  23. DependencyProperty.Register(nameof(IsEnsure), typeof(bool), typeof(FileGridListControl), new PropertyMetadata(false));
  24. public bool IsEnsure
  25. {
  26. get => (bool)GetValue(IsEnsureProperty);
  27. private set
  28. {
  29. SetValue(IsEnsureProperty, value);
  30. }
  31. }
  32. public class FileInfoData
  33. {
  34. public string FileName { get; set; }
  35. public string Size { get; set; }
  36. public string Location { get; set; }
  37. public CPDFDocument Document { get; set; }
  38. };
  39. private int _fileNumText;
  40. public int FileNumText
  41. {
  42. get => _fileNumText;
  43. set
  44. {
  45. if (UpdateProper(ref _fileNumText, value))
  46. IsEnsure = _fileNumText > 0;
  47. }
  48. }
  49. public event PropertyChangedEventHandler PropertyChanged;
  50. private ObservableCollection<FileInfoData> _fileInfoDataList;
  51. public ObservableCollection<FileInfoData> FileInfoDataList
  52. {
  53. get
  54. {
  55. return _fileInfoDataList;
  56. }
  57. set
  58. {
  59. _fileInfoDataList = value;
  60. _fileInfoDataList.CollectionChanged += (sender, args) =>
  61. {
  62. FileNumText = _fileInfoDataList.Count;
  63. };
  64. }
  65. }
  66. public FileGridListControl()
  67. {
  68. InitializeComponent();
  69. FileInfoDataList = new ObservableCollection<FileInfoData>();
  70. DataContext = this;
  71. FileDataGrid.ItemsSource = FileInfoDataList;
  72. FileInfoDataList.Clear();
  73. }
  74. private void AddFiles_Click(object sender, RoutedEventArgs e)
  75. {
  76. var dialog = new OpenFileDialog();
  77. dialog.Multiselect = true;
  78. dialog.Filter = @"PDF Files (*.pdf)|*.pdf";
  79. dialog.ShowDialog();
  80. foreach (var fileName in dialog.FileNames)
  81. {
  82. var file = new FileInfo(fileName);
  83. var fileInfoData = new FileInfoData()
  84. {
  85. FileName = file.Name,
  86. Size = CommonHelper.GetFileSize(fileName),
  87. Location = file.FullName
  88. };
  89. if (FileInfoDataList.Any(item => item.Location == fileInfoData.Location))
  90. {
  91. continue;
  92. }
  93. var document = CPDFDocument.InitWithFilePath(fileInfoData.Location);
  94. if (document == null)
  95. {
  96. continue;
  97. }
  98. if (PasswordHelper.IsOwnerLocked(document))
  99. {
  100. if(!PasswordHelper.UnlockWithOwnerPassword(document))
  101. {
  102. document.Release();
  103. continue;
  104. }
  105. }
  106. fileInfoData.Document = document;
  107. FileInfoDataList.Add(fileInfoData);
  108. }
  109. }
  110. private void Remove_Click(object sender, RoutedEventArgs e)
  111. {
  112. if (FileDataGrid.SelectedItems.Count == 0)
  113. {
  114. foreach (var item in FileInfoDataList)
  115. {
  116. item.Document.Release();
  117. }
  118. FileInfoDataList.Clear();
  119. OnPropertyChanged("FileNum");
  120. return;
  121. }
  122. var selectedItems = FileDataGrid.SelectedItems;
  123. var selectedItemsList = selectedItems.Cast<FileInfoData>().ToList();
  124. foreach (var item in selectedItemsList)
  125. {
  126. item.Document.Release();
  127. FileInfoDataList.Remove(item);
  128. }
  129. OnPropertyChanged("FileNum");
  130. }
  131. protected virtual void OnPropertyChanged(string propertyName = null)
  132. {
  133. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  134. }
  135. protected bool UpdateProper<T>(ref T properValue,
  136. T newValue,
  137. [CallerMemberName] string properName = "")
  138. {
  139. if (object.Equals(properValue, newValue))
  140. return false;
  141. properValue = newValue;
  142. OnPropertyChanged(properName);
  143. return true;
  144. }
  145. private void FileDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  146. {
  147. BtnRemove.Content = FileDataGrid.SelectedItems.Count > 0 ? LanguageHelper.SecurityManager.GetString("Button_RemoveSelected") : LanguageHelper.SecurityManager.GetString("Button_RemoveAll");
  148. }
  149. private void FileDataGrid_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  150. {
  151. FileDataGrid.UnselectAll();
  152. }
  153. }
  154. }