RecentFilesContentViewModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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;
  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. //补充判空处理,避免清空最近文件列表后,切换多页签会报异常
  160. if (RecentFilesGroup.Count > 0)
  161. {
  162. var item = RecentFilesGroup[0];
  163. var compageitem = Settings.Default.RecentOpenFiles[0];
  164. //因为新增文件都会排序到第一个,因此只需要比较集合数量和第一个对象是否一致即可判断两个集合是否一致
  165. //如果不一致则更新数据源
  166. if (item != compageitem || RecentFilesGroup.Count != Settings.Default.RecentOpenFiles.Count)
  167. {
  168. InitVariables();
  169. }
  170. }
  171. }
  172. private void InitCommands()
  173. {
  174. RemoveFilesFromContainerCommand = new DelegateCommand<object>(RemoveFilesFromContainer_BtnClick);
  175. RemoveFileItemCommand = new DelegateCommand<object>(RemoveFileItem_MenuItemClick);
  176. OpenRecentFilesCommand = new DelegateCommand<object>(OpenRecentFiles_MouseDoubleClick);
  177. ListModeCheckedCommand = new DelegateCommand<object>(ListMode_Checked);
  178. OpenFilesCommand = new DelegateCommand<object>(OpenFiles_EmptyListsClick);
  179. ExplorerFileCommand = new DelegateCommand<object>(ExplorerFile_MenuItemClick);
  180. }
  181. private void InitEvents()
  182. {
  183. RecentFilesGroup.CollectionChanged -= RecentFilesGroup_CollectionChanged;
  184. RecentFilesGroup.CollectionChanged += RecentFilesGroup_CollectionChanged;
  185. }
  186. private void RecentFilesGroup_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  187. {
  188. RecentFileGroupIsEmpty();
  189. }
  190. private void RecentFileGroupIsEmpty()
  191. {
  192. if (RecentFilesGroup.Count == 0)
  193. {
  194. IsEmpty = true;
  195. }
  196. else
  197. {
  198. IsEmpty = false;
  199. }
  200. }
  201. #endregion
  202. /// <summary>
  203. /// 最近列表:文件列表模式选中事件
  204. /// </summary>
  205. /// <param name="obj"></param>
  206. private void ListMode_Checked(object obj)
  207. {
  208. if (obj is string)
  209. {
  210. var tag = obj as string;
  211. if (tag == "ListMode")
  212. {
  213. IsListMode = true;
  214. Settings.Default.AppProperties.RecentFileListMode = 1;
  215. }
  216. else
  217. {
  218. IsListMode = false;
  219. Settings.Default.AppProperties.RecentFileListMode = 0;
  220. }
  221. Settings.Default.Save();
  222. RecentFilesSelectionHandler?.Invoke(null, IsListMode);
  223. }
  224. }
  225. /// <summary>
  226. /// 移除文件记录:单个文件
  227. /// </summary>
  228. /// <param name="obj"></param>
  229. private void RemoveFileItem_MenuItemClick(object obj)
  230. {
  231. var openFileInfo = obj as OpenFileInfo;
  232. if (openFileInfo != null)
  233. {
  234. bool needDelete = false;
  235. //判断是否需要执行删除操作
  236. if (!Settings.Default.AppProperties.DontTipBeforeDeleteRecent)
  237. {
  238. AlertsMessage alertsMessage = new AlertsMessage();
  239. alertsMessage.HaveCheckBoxShow("", App.MainPageLoader.GetString("RecentFiles_AllClearMsg"), App.MainPageLoader.GetString("Main_DoNotRemindAgain"), App.ServiceLoader.GetString("Text_cancel"), App.ServiceLoader.GetString("Text_ok"));
  240. if(alertsMessage.result == ContentResult.Ok)
  241. {
  242. needDelete = true;
  243. //已经勾选 下次不再提示
  244. Settings.Default.AppProperties.DontTipBeforeDeleteRecent = alertsMessage.isChecked;
  245. Settings.Default.Save();
  246. }
  247. else
  248. {
  249. needDelete = false;
  250. }
  251. }
  252. else
  253. {
  254. needDelete = true;
  255. }
  256. if (needDelete)
  257. {
  258. SettingHelper.RemoveRecentOpenFile(openFileInfo.FilePath);
  259. RecentFilesGroup.Remove(openFileInfo);
  260. }
  261. }
  262. }
  263. /// <summary>
  264. /// 删除按钮触发事件:选中的文件
  265. /// </summary>
  266. /// <param name="obj">选中的文档</param>
  267. private void RemoveFilesFromContainer_BtnClick(object obj)
  268. {
  269. System.Collections.IList items = (System.Collections.IList)obj;
  270. if (items == null || items.Cast<OpenFileInfo>() == null)
  271. return;
  272. var collection = items.Cast<OpenFileInfo>();
  273. var openFileInfo = collection.ToList();
  274. if(openFileInfo != null)
  275. {
  276. string msg = "";
  277. int SelectedItemsType = 0;
  278. if (openFileInfo.Count == RecentFilesGroup.Count || openFileInfo.Count == 0)
  279. {
  280. if (openFileInfo.Count == 1 && RecentFilesGroup.Count == 1)
  281. msg = App.MainPageLoader.GetString("RecentFiles_ClearMsg");
  282. else
  283. msg = App.MainPageLoader.GetString("RecentFiles_AllClearMsg");
  284. SelectedItemsType = 0;
  285. }
  286. else if(openFileInfo.Count == 1)
  287. {
  288. msg = App.MainPageLoader.GetString("RecentFiles_ClearMsg");
  289. SelectedItemsType = 1;
  290. }
  291. else
  292. {
  293. msg = App.MainPageLoader.GetString("RecentFiles_SelectedClearMsg");
  294. SelectedItemsType = 2;
  295. }
  296. bool needDelete = false;
  297. if (!Settings.Default.AppProperties.DontTipBeforeDeleteRecent)
  298. {
  299. AlertsMessage alertsMessage = new AlertsMessage();
  300. alertsMessage.HaveCheckBoxShow("",msg, App.MainPageLoader.GetString("Main_DoNotRemindAgain"), App.ServiceLoader.GetString("Text_cancel"), App.ServiceLoader.GetString("Text_ok"));
  301. if (alertsMessage.result == ContentResult.Ok)
  302. {
  303. needDelete = true;
  304. Settings.Default.AppProperties.DontTipBeforeDeleteRecent = alertsMessage.isChecked;
  305. Settings.Default.Save();
  306. }
  307. else
  308. {
  309. needDelete = false;
  310. }
  311. }
  312. else
  313. {
  314. needDelete = true;
  315. }
  316. if (needDelete)
  317. {
  318. RemoveRecentFilesFrom(SelectedItemsType, openFileInfo);
  319. }
  320. }
  321. }
  322. /// <summary>
  323. /// 删除最近文件的操作
  324. /// </summary>
  325. /// <param name="selectedItemsType">0:全部文件;1:一个文件;2:多个文件</param>
  326. /// <param name="openFileInfo">选中的文件</param>
  327. private void RemoveRecentFilesFrom(int selectedItemsType, List<OpenFileInfo> openFileInfo)
  328. {
  329. if (selectedItemsType == 0)
  330. {
  331. SettingHelper.RemoveAllRecentOpenFiles();
  332. RecentFilesGroup.Clear();
  333. }
  334. else if (selectedItemsType == 1)
  335. {
  336. var file = openFileInfo[0] as OpenFileInfo;
  337. SettingHelper.RemoveRecentOpenFile(file.FilePath);
  338. RecentFilesGroup.Remove(file);
  339. }
  340. else
  341. {
  342. foreach (var item in openFileInfo)
  343. {
  344. SettingHelper.RemoveRecentOpenFile(item.FilePath);
  345. RecentFilesGroup.Remove(item);
  346. }
  347. }
  348. }
  349. /// <summary>
  350. /// 空状态时,点击文件浏览器弹窗,打开文件
  351. /// </summary>
  352. private void OpenFiles_EmptyListsClick(object obj)
  353. {
  354. var dlg = new OpenFileDialog();
  355. dlg.Multiselect = true;
  356. dlg.Filter = Properties.Resources.OpenDialogFilter;
  357. if (dlg.ShowDialog() == true)
  358. {
  359. LoadPdfViewer(dlg.FileNames);
  360. }
  361. }
  362. /// <summary>
  363. /// 打开文件路径
  364. /// </summary>
  365. private void ExplorerFile_MenuItemClick(object obj)
  366. {
  367. try
  368. {
  369. var fileInfo = obj as OpenFileInfo;
  370. if (fileInfo != null)
  371. {
  372. if (string.IsNullOrEmpty(fileInfo.FilePath) == false)
  373. {
  374. if (!File.Exists(fileInfo.FilePath))
  375. {
  376. AlertsMessage alertsMessage = new AlertsMessage();
  377. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("FileNotExistWarning"), App.ServiceLoader.GetString("Text_ok"));
  378. SettingHelper.RemoveRecentOpenFile(fileInfo.FilePath);
  379. RecentFilesGroup.Remove(fileInfo);
  380. }
  381. else
  382. {
  383. Process.Start("explorer", "/select,\"" + fileInfo.FilePath + "\"");
  384. }
  385. }
  386. }
  387. }
  388. catch
  389. {
  390. }
  391. }
  392. /// <summary>
  393. /// 从最近列表里,打开文档
  394. /// </summary>
  395. /// <param name="obj"></param>
  396. private void OpenRecentFiles_MouseDoubleClick(object obj)
  397. {
  398. var fileInfo = obj as OpenFileInfo;
  399. if (fileInfo != null)
  400. {
  401. if (File.Exists(fileInfo.FilePath))
  402. {
  403. string[] filePath = new string[1];
  404. filePath[0] = fileInfo.FilePath;
  405. LoadPdfViewer(filePath);
  406. }
  407. else
  408. {
  409. AlertsMessage alertsMessage = new AlertsMessage();
  410. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("FileNotExistWarning"), App.ServiceLoader.GetString("Text_ok"));
  411. SettingHelper.RemoveRecentOpenFile(fileInfo.FilePath);
  412. RecentFilesGroup.Remove(fileInfo);
  413. }
  414. }
  415. }
  416. /// <summary>
  417. /// 打开文档
  418. /// </summary>
  419. /// <param name="filePaths"></param>
  420. public void LoadPdfViewer(string[] filePaths)
  421. {
  422. var content = App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel;
  423. if (filePaths.Count() == 1)
  424. {
  425. if (App.OpenedFileList.Contains(filePaths[0]))
  426. {
  427. App.mainWindowViewModel.SelectItem(filePaths[0]);
  428. }
  429. else
  430. {
  431. content.OpenFile(filePaths[0]);
  432. }
  433. ToolMethod.SetFileThumbImg(filePaths[0]);
  434. }
  435. else
  436. {
  437. var fileList = filePaths.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  438. if (fileList.Count <= 0)
  439. return;
  440. content.OpenFile(filePaths[0]);
  441. for (int i = 1; i < fileList.Count(); i++)
  442. {
  443. if (!App.OpenedFileList.Contains(fileList[i]))
  444. {
  445. App.mainWindowViewModel.AddTabItem(fileList[i]);
  446. }
  447. ToolMethod.SetFileThumbImg(fileList[i]);
  448. }
  449. }
  450. Settings.Default.Save();
  451. }
  452. }
  453. }