RecentFilesContentViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. using Microsoft.Win32;
  2. using PDF_Master.CustomControl;
  3. using PDF_Master.Helper;
  4. using PDF_Master.Properties;
  5. using PDF_Master.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_Master.ViewModels.HomePanel.RecentFiles
  21. {
  22. public class RecentFilesContentViewModel: BindableBase
  23. {
  24. #region 文案
  25. private string T_title;
  26. public string T_Title
  27. {
  28. get { return T_title; }
  29. set
  30. {
  31. SetProperty(ref T_title, value);
  32. }
  33. }
  34. private string T_menuShow;
  35. public string T_MenuShow
  36. {
  37. get { return T_menuShow; }
  38. set
  39. {
  40. SetProperty(ref T_menuShow, value);
  41. }
  42. }
  43. private string T_menuRemove;
  44. public string T_MenuRemove
  45. {
  46. get { return T_menuRemove; }
  47. set
  48. {
  49. SetProperty(ref T_menuRemove, value);
  50. }
  51. }
  52. private string T_noFilesTitle;
  53. public string T_NoFilesTitle
  54. {
  55. get { return T_noFilesTitle; }
  56. set
  57. {
  58. SetProperty(ref T_noFilesTitle, value);
  59. }
  60. }
  61. private string T_noFilesContent;
  62. public string T_NoFilesContent
  63. {
  64. get { return T_noFilesContent; }
  65. set
  66. {
  67. SetProperty(ref T_noFilesContent, value);
  68. }
  69. }
  70. private void InitString()
  71. {
  72. T_Title = App.HomePageLoader.GetString("HomeList_Title");
  73. T_MenuShow = App.HomePageLoader.GetString("HomeList_MenuShow");
  74. T_MenuRemove = App.HomePageLoader.GetString("HomeList_MenuRemove");
  75. T_NoFilesTitle = App.HomePageLoader.GetString("HomeList_NoFilesTitle");
  76. T_NoFilesContent = App.HomePageLoader.GetString("HomeList_NoFilesContent");
  77. }
  78. #endregion
  79. #region 属性
  80. private ObservableCollection<OpenFileInfo> _recentFilesGroup = new ObservableCollection<OpenFileInfo>();
  81. /// <summary>
  82. /// 最近列表:文件集合
  83. /// </summary>
  84. public ObservableCollection<OpenFileInfo> RecentFilesGroup
  85. {
  86. get { return _recentFilesGroup; }
  87. set
  88. {
  89. SetProperty(ref _recentFilesGroup, value);
  90. }
  91. }
  92. private bool _isEmpty = false;
  93. /// <summary>
  94. /// 最近列表是否为空
  95. /// </summary>
  96. public bool IsEmpty
  97. {
  98. get { return _isEmpty; }
  99. set
  100. {
  101. SetProperty(ref _isEmpty, value);
  102. }
  103. }
  104. private bool _isListMode = false;
  105. /// <summary>
  106. /// 是否为列表模式
  107. /// </summary>
  108. public bool IsListMode
  109. {
  110. get { return _isListMode; }
  111. set
  112. {
  113. SetProperty(ref _isListMode, value);
  114. }
  115. }
  116. #endregion
  117. #region Command
  118. public DelegateCommand<object> RemoveFileItemCommand { get; set; }
  119. public DelegateCommand<object> RemoveFilesFromContainerCommand { get; set; }
  120. public DelegateCommand<object> OpenRecentFilesCommand { get; set; }
  121. public DelegateCommand<object> OpenFilesCommand { get; set; }
  122. public DelegateCommand<object> ListModeCheckedCommand { get; set; }
  123. public DelegateCommand<object> ExplorerFileCommand { get; set; }
  124. public event EventHandler<bool> RecentFilesSelectionHandler;
  125. #endregion
  126. public RecentFilesContentViewModel()
  127. {
  128. InitVariables();
  129. InitCommands();
  130. InitEvents();
  131. InitString();
  132. }
  133. #region 初始化
  134. private void InitVariables()
  135. {
  136. RecentFilesGroup = new ObservableCollection<OpenFileInfo>(Settings.Default.RecentOpenFiles);
  137. RecentFileGroupIsEmpty();
  138. int mode = Settings.Default.AppProperties.RecentFileListMode;
  139. if (mode == 0)
  140. IsListMode = false;
  141. else
  142. IsListMode = true;
  143. }
  144. private void InitCommands()
  145. {
  146. RemoveFilesFromContainerCommand = new DelegateCommand<object>(RemoveFilesFromContainer_BtnClick);
  147. RemoveFileItemCommand = new DelegateCommand<object>(RemoveFileItem_MenuItemClick);
  148. OpenRecentFilesCommand = new DelegateCommand<object>(OpenRecentFiles_MouseDoubleClick);
  149. ListModeCheckedCommand = new DelegateCommand<object>(ListMode_Checked);
  150. OpenFilesCommand = new DelegateCommand<object>(OpenFiles_EmptyListsClick);
  151. ExplorerFileCommand = new DelegateCommand<object>(ExplorerFile_MenuItemClick);
  152. }
  153. private void InitEvents()
  154. {
  155. RecentFilesGroup.CollectionChanged -= RecentFilesGroup_CollectionChanged;
  156. RecentFilesGroup.CollectionChanged += RecentFilesGroup_CollectionChanged;
  157. }
  158. private void RecentFilesGroup_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  159. {
  160. RecentFileGroupIsEmpty();
  161. }
  162. private void RecentFileGroupIsEmpty()
  163. {
  164. if (RecentFilesGroup.Count == 0)
  165. {
  166. IsEmpty = true;
  167. }
  168. else
  169. {
  170. IsEmpty = false;
  171. }
  172. }
  173. #endregion
  174. /// <summary>
  175. /// 最近列表:文件列表模式选中事件
  176. /// </summary>
  177. /// <param name="obj"></param>
  178. private void ListMode_Checked(object obj)
  179. {
  180. if (obj is string)
  181. {
  182. var tag = obj as string;
  183. if (tag == "ListMode")
  184. {
  185. IsListMode = true;
  186. Settings.Default.AppProperties.RecentFileListMode = 1;
  187. }
  188. else
  189. {
  190. IsListMode = false;
  191. Settings.Default.AppProperties.RecentFileListMode = 0;
  192. }
  193. Settings.Default.Save();
  194. RecentFilesSelectionHandler?.Invoke(null, IsListMode);
  195. }
  196. }
  197. /// <summary>
  198. /// 移除文件记录:单个文件
  199. /// </summary>
  200. /// <param name="obj"></param>
  201. private void RemoveFileItem_MenuItemClick(object obj)
  202. {
  203. var openFileInfo = obj as OpenFileInfo;
  204. if (openFileInfo != null)
  205. {
  206. bool needDelete = false;
  207. //判断是否需要执行删除操作
  208. if (!Settings.Default.AppProperties.DontTipBeforeDeleteRecent)
  209. {
  210. AlertsMessage alertsMessage = new AlertsMessage();
  211. alertsMessage.HaveCheckBoxShow("提示", "删除文件", "不再提示", "取消", "OK");
  212. if(alertsMessage.result == ContentResult.Ok)
  213. {
  214. needDelete = true;
  215. //已经勾选 下次不再提示
  216. Settings.Default.AppProperties.DontTipBeforeDeleteRecent = alertsMessage.isChecked;
  217. Settings.Default.Save();
  218. }
  219. else
  220. {
  221. needDelete = false;
  222. }
  223. }
  224. else
  225. {
  226. needDelete = true;
  227. }
  228. if (needDelete)
  229. {
  230. SettingHelper.RemoveRecentOpenFile(openFileInfo.FilePath);
  231. RecentFilesGroup.Remove(openFileInfo);
  232. }
  233. }
  234. }
  235. /// <summary>
  236. /// 删除按钮触发事件:选中的文件
  237. /// </summary>
  238. /// <param name="obj">选中的文档</param>
  239. private void RemoveFilesFromContainer_BtnClick(object obj)
  240. {
  241. System.Collections.IList items = (System.Collections.IList)obj;
  242. if (items == null || items.Cast<OpenFileInfo>() == null)
  243. return;
  244. var collection = items.Cast<OpenFileInfo>();
  245. var openFileInfo = collection.ToList();
  246. if(openFileInfo != null)
  247. {
  248. string msg = "";
  249. int SelectedItemsType = 0;
  250. if (openFileInfo.Count == RecentFilesGroup.Count || openFileInfo.Count == 0)
  251. {
  252. if(openFileInfo.Count == 1 && RecentFilesGroup.Count == 1)
  253. msg = "ClearFile";
  254. else
  255. msg = "AllClearFiles";
  256. SelectedItemsType = 0;
  257. }
  258. else if(openFileInfo.Count == 1)
  259. {
  260. msg = "ClearFile";
  261. SelectedItemsType = 1;
  262. }
  263. else
  264. {
  265. msg = "ClearSelectedFiles";
  266. SelectedItemsType = 2;
  267. }
  268. bool needDelete = false;
  269. if (!Settings.Default.AppProperties.DontTipBeforeDeleteRecent)
  270. {
  271. AlertsMessage alertsMessage = new AlertsMessage();
  272. alertsMessage.HaveCheckBoxShow("提示", msg, "不再提示", "取消", "OK");
  273. if (alertsMessage.result == ContentResult.Ok)
  274. {
  275. needDelete = true;
  276. Settings.Default.AppProperties.DontTipBeforeDeleteRecent = alertsMessage.isChecked;
  277. Settings.Default.Save();
  278. }
  279. else
  280. {
  281. needDelete = false;
  282. }
  283. }
  284. else
  285. {
  286. needDelete = true;
  287. }
  288. if (needDelete)
  289. {
  290. RemoveRecentFilesFrom(SelectedItemsType, openFileInfo);
  291. }
  292. }
  293. }
  294. /// <summary>
  295. /// 删除最近文件的操作
  296. /// </summary>
  297. /// <param name="selectedItemsType">0:全部文件;1:一个文件;2:多个文件</param>
  298. /// <param name="openFileInfo">选中的文件</param>
  299. private void RemoveRecentFilesFrom(int selectedItemsType, List<OpenFileInfo> openFileInfo)
  300. {
  301. if (selectedItemsType == 0)
  302. {
  303. SettingHelper.RemoveAllRecentOpenFiles();
  304. RecentFilesGroup.Clear();
  305. }
  306. else if (selectedItemsType == 1)
  307. {
  308. var file = openFileInfo[0] as OpenFileInfo;
  309. SettingHelper.RemoveRecentOpenFile(file.FilePath);
  310. RecentFilesGroup.Remove(file);
  311. }
  312. else
  313. {
  314. foreach (var item in openFileInfo)
  315. {
  316. SettingHelper.RemoveRecentOpenFile(item.FilePath);
  317. RecentFilesGroup.Remove(item);
  318. }
  319. }
  320. }
  321. /// <summary>
  322. /// 空状态时,点击文件浏览器弹窗,打开文件
  323. /// </summary>
  324. private void OpenFiles_EmptyListsClick(object obj)
  325. {
  326. var dlg = new OpenFileDialog();
  327. dlg.Multiselect = true;
  328. dlg.Filter = Properties.Resources.OpenDialogFilter;
  329. if (dlg.ShowDialog() == true)
  330. {
  331. LoadPdfViewer(dlg.FileNames);
  332. }
  333. }
  334. /// <summary>
  335. /// 打开文件路径
  336. /// </summary>
  337. private void ExplorerFile_MenuItemClick(object obj)
  338. {
  339. try
  340. {
  341. var fileInfo = obj as OpenFileInfo;
  342. if (fileInfo != null)
  343. {
  344. if (string.IsNullOrEmpty(fileInfo.FilePath) == false)
  345. {
  346. if (!File.Exists(fileInfo.FilePath))
  347. {
  348. AlertsMessage alertsMessage = new AlertsMessage();
  349. alertsMessage.ShowDialog("提示", "文件不存在", "OK");
  350. SettingHelper.RemoveRecentOpenFile(fileInfo.FilePath);
  351. RecentFilesGroup.Remove(fileInfo);
  352. }
  353. else
  354. {
  355. Process.Start("explorer", "/select,\"" + fileInfo.FilePath + "\"");
  356. }
  357. }
  358. }
  359. }
  360. catch (Exception ex)
  361. {
  362. }
  363. }
  364. /// <summary>
  365. /// 从最近列表里,打开文档
  366. /// </summary>
  367. /// <param name="obj"></param>
  368. private void OpenRecentFiles_MouseDoubleClick(object obj)
  369. {
  370. var fileInfo = obj as OpenFileInfo;
  371. if (fileInfo != null)
  372. {
  373. if (File.Exists(fileInfo.FilePath))
  374. {
  375. string[] filePath = new string[1];
  376. filePath[0] = fileInfo.FilePath;
  377. LoadPdfViewer(filePath);
  378. }
  379. else
  380. {
  381. AlertsMessage alertsMessage = new AlertsMessage();
  382. alertsMessage.ShowDialog("提示", "文件不存在", "OK");
  383. SettingHelper.RemoveRecentOpenFile(fileInfo.FilePath);
  384. RecentFilesGroup.Remove(fileInfo);
  385. }
  386. }
  387. }
  388. /// <summary>
  389. /// 打开文档
  390. /// </summary>
  391. /// <param name="filePaths"></param>
  392. public void LoadPdfViewer(string[] filePaths)
  393. {
  394. var content = App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel;
  395. if (filePaths.Count() == 1)
  396. {
  397. if (App.OpenedFileList.Contains(filePaths[0]))
  398. {
  399. App.mainWindowViewModel.SelectItem(filePaths[0]);
  400. }
  401. else
  402. {
  403. content.OpenFile(filePaths[0]);
  404. }
  405. ToolMethod.SetFileThumbImg(filePaths[0]);
  406. }
  407. else
  408. {
  409. var fileList = filePaths.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  410. if (fileList.Count <= 0)
  411. return;
  412. content.OpenFile(filePaths[0]);
  413. for (int i = 1; i < fileList.Count(); i++)
  414. {
  415. if (!App.OpenedFileList.Contains(fileList[i]))
  416. {
  417. App.mainWindowViewModel.AddTabItem(fileList[i]);
  418. }
  419. ToolMethod.SetFileThumbImg(fileList[i]);
  420. }
  421. }
  422. Settings.Default.Save();
  423. }
  424. }
  425. }