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. 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.IsLocked)
  94. {
  95. PasswordWindow window = new PasswordWindow();
  96. window.InitWithDocument(document);
  97. window.Owner = Window.GetWindow(this);
  98. window.PasswordDialog.SetShowText(fileInfoData.FileName + " is encrypted.");
  99. window.ShowDialog();
  100. if (document.IsLocked)
  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 ? "Remove" : "Remove All";
  148. }
  149. private void FileDataGrid_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  150. {
  151. FileDataGrid.UnselectAll();
  152. }
  153. }
  154. }