FileGridListControl.xaml.cs 5.6 KB

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