RecentFilesContentViewModel.cs 16 KB

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