RecentFilesContentViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using Microsoft.Win32;
  2. using PDF_Office.CustomControl;
  3. using PDF_Office.Helper;
  4. using PDF_Office.Properties;
  5. using PDF_Office.Views;
  6. using PDFSettings.Settings;
  7. using Prism.Commands;
  8. using Prism.Mvvm;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.Collections.Specialized;
  13. using System.Diagnostics;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using winform = System.Windows.Forms;
  20. namespace PDF_Office.ViewModels.HomePanel.RecentFiles
  21. {
  22. public class RecentFilesContentViewModel: BindableBase
  23. {
  24. #region 属性
  25. private ObservableCollection<OpenFileInfo> _recentFilesGroup = new ObservableCollection<OpenFileInfo>();
  26. /// <summary>
  27. /// 最近列表:文件集合
  28. /// </summary>
  29. public ObservableCollection<OpenFileInfo> RecentFilesGroup
  30. {
  31. get { return _recentFilesGroup; }
  32. set
  33. {
  34. SetProperty(ref _recentFilesGroup, value);
  35. }
  36. }
  37. private bool _isEmpty = false;
  38. /// <summary>
  39. /// 最近列表是否为空
  40. /// </summary>
  41. public bool IsEmpty
  42. {
  43. get { return _isEmpty; }
  44. set
  45. {
  46. SetProperty(ref _isEmpty, value);
  47. }
  48. }
  49. private bool _isListMode = false;
  50. /// <summary>
  51. /// 是否为列表模式
  52. /// </summary>
  53. public bool IsListMode
  54. {
  55. get { return _isListMode; }
  56. set
  57. {
  58. SetProperty(ref _isListMode, value);
  59. }
  60. }
  61. #endregion
  62. public DelegateCommand<object> RemoveFileItemCommand { get; set; }
  63. public DelegateCommand<object> RemoveFilesFromContainerCommand { get; set; }
  64. public DelegateCommand<object> OpenRecentFilesCommand { get; set; }
  65. public DelegateCommand<object> OpenFilesCommand { get; set; }
  66. public DelegateCommand<object> ListModeCheckedCommand { get; set; }
  67. public DelegateCommand<object> ExplorerFileCommand { get; set; }
  68. public event EventHandler<bool> RecentFilesSelectionHandler;
  69. public RecentFilesContentViewModel()
  70. {
  71. InitVariables();
  72. InitCommands();
  73. InitEvents();
  74. }
  75. #region 初始化
  76. private void InitVariables()
  77. {
  78. RecentFilesGroup = new ObservableCollection<OpenFileInfo>(Settings.Default.RecentOpenFiles);
  79. RecentFileGroupIsEmpty();
  80. int mode = Settings.Default.AppProperties.RecentFileListMode;
  81. if (mode == 0)
  82. IsListMode = false;
  83. else
  84. IsListMode = true;
  85. }
  86. private void InitCommands()
  87. {
  88. RemoveFilesFromContainerCommand = new DelegateCommand<object>(RemoveFilesFromContainer_Command);
  89. RemoveFileItemCommand = new DelegateCommand<object>(RemoveFileItem_Command);
  90. OpenRecentFilesCommand = new DelegateCommand<object>(OpenRecentFiles_Command);
  91. ListModeCheckedCommand = new DelegateCommand<object>(ListMode_Checked);
  92. OpenFilesCommand = new DelegateCommand<object>(OpenFiles_Command);
  93. ExplorerFileCommand = new DelegateCommand<object>(ExplorerFile_Command);
  94. }
  95. private void InitEvents()
  96. {
  97. RecentFilesGroup.CollectionChanged -= RecentFilesGroup_CollectionChanged;
  98. RecentFilesGroup.CollectionChanged += RecentFilesGroup_CollectionChanged;
  99. }
  100. private void RecentFilesGroup_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  101. {
  102. RecentFileGroupIsEmpty();
  103. }
  104. private void RecentFileGroupIsEmpty()
  105. {
  106. if (RecentFilesGroup.Count == 0)
  107. {
  108. IsEmpty = true;
  109. }
  110. else
  111. {
  112. IsEmpty = false;
  113. }
  114. }
  115. #endregion
  116. /// <summary>
  117. /// 最近列表:文件列表模式选中事件
  118. /// </summary>
  119. /// <param name="obj"></param>
  120. private void ListMode_Checked(object obj)
  121. {
  122. if (obj is string)
  123. {
  124. var tag = obj as string;
  125. if (tag == "ListMode")
  126. {
  127. IsListMode = true;
  128. Settings.Default.AppProperties.RecentFileListMode = 1;
  129. }
  130. else
  131. {
  132. IsListMode = false;
  133. Settings.Default.AppProperties.RecentFileListMode = 0;
  134. }
  135. Settings.Default.Save();
  136. RecentFilesSelectionHandler?.Invoke(null, IsListMode);
  137. }
  138. }
  139. /// <summary>
  140. /// 移除文件记录:单个文件
  141. /// </summary>
  142. /// <param name="obj"></param>
  143. private void RemoveFileItem_Command(object obj)
  144. {
  145. var openFileInfo = obj as OpenFileInfo;
  146. if (openFileInfo != null)
  147. {
  148. SettingHelper.RemoveRecentOpenFile(openFileInfo.FilePath);
  149. RecentFilesGroup.Remove(openFileInfo);
  150. }
  151. }
  152. /// <summary>
  153. /// 删除按钮触发事件:选中的文件
  154. /// </summary>
  155. /// <param name="obj">选中的文档</param>
  156. private void RemoveFilesFromContainer_Command(object obj)
  157. {
  158. System.Collections.IList items = (System.Collections.IList)obj;
  159. if (items == null || items.Cast<OpenFileInfo>() == null)
  160. return;
  161. var collection = items.Cast<OpenFileInfo>();
  162. var openFileInfo = collection.ToList();
  163. if(openFileInfo != null)
  164. {
  165. string msg = "";
  166. int SelectedItemsType = 0;
  167. if (openFileInfo.Count == RecentFilesGroup.Count || openFileInfo.Count == 0)
  168. {
  169. if(openFileInfo.Count == 1 && RecentFilesGroup.Count == 1)
  170. msg = "ClearFile";
  171. else
  172. msg = "AllClearFiles";
  173. SelectedItemsType = 0;
  174. }
  175. else if(openFileInfo.Count == 1)
  176. {
  177. msg = "ClearFile";
  178. SelectedItemsType = 1;
  179. }
  180. else
  181. {
  182. msg = "ClearSelectedFiles";
  183. SelectedItemsType = 2;
  184. }
  185. winform.DialogResult result = MessageBoxEx.Show(msg, "", winform.MessageBoxButtons.OKCancel, winform.MessageBoxIcon.Question);
  186. if (result == winform.DialogResult.OK)
  187. {
  188. RemoveRecentFilesFrom(SelectedItemsType, openFileInfo);
  189. }
  190. }
  191. }
  192. /// <summary>
  193. /// 删除最近文件的操作
  194. /// </summary>
  195. /// <param name="selectedItemsType">0:全部文件;1:一个文件;2:多个文件</param>
  196. /// <param name="openFileInfo">选中的文件</param>
  197. private void RemoveRecentFilesFrom(int selectedItemsType, List<OpenFileInfo> openFileInfo)
  198. {
  199. if (selectedItemsType == 0)
  200. {
  201. SettingHelper.RemoveAllRecentOpenFiles();
  202. RecentFilesGroup.Clear();
  203. }
  204. else if (selectedItemsType == 1)
  205. {
  206. var file = openFileInfo[0] as OpenFileInfo;
  207. SettingHelper.RemoveRecentOpenFile(file.FilePath);
  208. RecentFilesGroup.Remove(file);
  209. }
  210. else
  211. {
  212. foreach (var item in openFileInfo)
  213. {
  214. SettingHelper.RemoveRecentOpenFile(item.FilePath);
  215. RecentFilesGroup.Remove(item);
  216. }
  217. }
  218. }
  219. /// <summary>
  220. /// 空状态时,点击文件浏览器弹窗,打开文件
  221. /// </summary>
  222. private void OpenFiles_Command(object obj)
  223. {
  224. var dlg = new OpenFileDialog();
  225. dlg.Multiselect = true;
  226. dlg.Filter = Properties.Resources.OpenDialogFilter;
  227. if (dlg.ShowDialog() == true)
  228. {
  229. LoadPdfViewer(dlg.FileNames);
  230. }
  231. }
  232. /// <summary>
  233. /// 打开文件路径
  234. /// </summary>
  235. private void ExplorerFile_Command(object obj)
  236. {
  237. try
  238. {
  239. var fileInfo = obj as OpenFileInfo;
  240. if (fileInfo != null)
  241. {
  242. if (string.IsNullOrEmpty(fileInfo.FilePath) == false)
  243. {
  244. if (!File.Exists(fileInfo.FilePath))
  245. {
  246. MessageBoxEx.Show("文件不存在");
  247. SettingHelper.RemoveRecentOpenFile(fileInfo.FilePath);
  248. RecentFilesGroup.Remove(fileInfo);
  249. }
  250. else
  251. {
  252. Process.Start("explorer", "/select,\"" + fileInfo.FilePath + "\"");
  253. }
  254. }
  255. }
  256. }
  257. catch (Exception ex)
  258. {
  259. }
  260. }
  261. /// <summary>
  262. /// 从最近列表里,打开文档
  263. /// </summary>
  264. /// <param name="obj"></param>
  265. private void OpenRecentFiles_Command(object obj)
  266. {
  267. var fileInfo = obj as OpenFileInfo;
  268. if (fileInfo != null)
  269. {
  270. if (File.Exists(fileInfo.FilePath))
  271. {
  272. string[] filePath = new string[1];
  273. filePath[0] = fileInfo.FilePath;
  274. LoadPdfViewer(filePath);
  275. }
  276. else
  277. {
  278. SettingHelper.RemoveRecentOpenFile(fileInfo.FilePath);
  279. RecentFilesGroup.Remove(fileInfo);
  280. }
  281. }
  282. }
  283. /// <summary>
  284. /// 打开文档
  285. /// </summary>
  286. /// <param name="filePaths"></param>
  287. public void LoadPdfViewer(string[] filePaths)
  288. {
  289. var content = App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel;
  290. if (filePaths.Count() == 1)
  291. {
  292. if (App.OpenedFileList.Contains(filePaths[0]))
  293. {
  294. App.mainWindowViewModel.SelectItem(filePaths[0]);
  295. }
  296. else
  297. {
  298. content.OpenFile(filePaths[0]);
  299. }
  300. ToolMethod.SetFileThumbImg(filePaths[0]);
  301. }
  302. else
  303. {
  304. var fileList = filePaths.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  305. if (fileList.Count <= 0)
  306. return;
  307. content.OpenFile(filePaths[0]);
  308. for (int i = 1; i < fileList.Count(); i++)
  309. {
  310. if (!App.OpenedFileList.Contains(fileList[i]))
  311. {
  312. App.mainWindowViewModel.AddTabItem(fileList[i]);
  313. }
  314. ToolMethod.SetFileThumbImg(fileList[i]);
  315. }
  316. }
  317. Settings.Default.Save();
  318. }
  319. }
  320. }