using Microsoft.Win32; using PDF_Office.CustomControl; using PDF_Office.Helper; using PDF_Office.Properties; using PDF_Office.Views; using PDFSettings.Settings; using Prism.Commands; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using winform = System.Windows.Forms; namespace PDF_Office.ViewModels.HomePanel.RecentFiles { public class RecentFilesContentViewModel: BindableBase { #region 文案 private string T_title; public string T_Title { get { return T_title; } set { SetProperty(ref T_title, value); } } private string T_menuShow; public string T_MenuShow { get { return T_menuShow; } set { SetProperty(ref T_menuShow, value); } } private string T_menuRemove; public string T_MenuRemove { get { return T_menuRemove; } set { SetProperty(ref T_menuRemove, value); } } private string T_noFilesTitle; public string T_NoFilesTitle { get { return T_noFilesTitle; } set { SetProperty(ref T_noFilesTitle, value); } } private string T_noFilesContent; public string T_NoFilesContent { get { return T_noFilesContent; } set { SetProperty(ref T_noFilesContent, value); } } private void InitString() { T_Title = App.HomePageLoader.GetString("HomeList_Title"); T_MenuShow = App.HomePageLoader.GetString("HomeList_MenuShow"); T_MenuRemove = App.HomePageLoader.GetString("HomeList_MenuRemove"); T_NoFilesTitle = App.HomePageLoader.GetString("HomeList_NoFilesTitle"); T_NoFilesContent = App.HomePageLoader.GetString("HomeList_NoFilesContent"); } #endregion #region 属性 private ObservableCollection _recentFilesGroup = new ObservableCollection(); /// /// 最近列表:文件集合 /// public ObservableCollection RecentFilesGroup { get { return _recentFilesGroup; } set { SetProperty(ref _recentFilesGroup, value); } } private bool _isEmpty = false; /// /// 最近列表是否为空 /// public bool IsEmpty { get { return _isEmpty; } set { SetProperty(ref _isEmpty, value); } } private bool _isListMode = false; /// /// 是否为列表模式 /// public bool IsListMode { get { return _isListMode; } set { SetProperty(ref _isListMode, value); } } #endregion #region Command public DelegateCommand RemoveFileItemCommand { get; set; } public DelegateCommand RemoveFilesFromContainerCommand { get; set; } public DelegateCommand OpenRecentFilesCommand { get; set; } public DelegateCommand OpenFilesCommand { get; set; } public DelegateCommand ListModeCheckedCommand { get; set; } public DelegateCommand ExplorerFileCommand { get; set; } public event EventHandler RecentFilesSelectionHandler; #endregion public RecentFilesContentViewModel() { InitVariables(); InitCommands(); InitEvents(); InitString(); } #region 初始化 private void InitVariables() { RecentFilesGroup = new ObservableCollection(Settings.Default.RecentOpenFiles); RecentFileGroupIsEmpty(); int mode = Settings.Default.AppProperties.RecentFileListMode; if (mode == 0) IsListMode = false; else IsListMode = true; } private void InitCommands() { RemoveFilesFromContainerCommand = new DelegateCommand(RemoveFilesFromContainer_BtnClick); RemoveFileItemCommand = new DelegateCommand(RemoveFileItem_MenuItemClick); OpenRecentFilesCommand = new DelegateCommand(OpenRecentFiles_MouseDoubleClick); ListModeCheckedCommand = new DelegateCommand(ListMode_Checked); OpenFilesCommand = new DelegateCommand(OpenFiles_EmptyListsClick); ExplorerFileCommand = new DelegateCommand(ExplorerFile_MenuItemClick); } private void InitEvents() { RecentFilesGroup.CollectionChanged -= RecentFilesGroup_CollectionChanged; RecentFilesGroup.CollectionChanged += RecentFilesGroup_CollectionChanged; } private void RecentFilesGroup_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { RecentFileGroupIsEmpty(); } private void RecentFileGroupIsEmpty() { if (RecentFilesGroup.Count == 0) { IsEmpty = true; } else { IsEmpty = false; } } #endregion /// /// 最近列表:文件列表模式选中事件 /// /// private void ListMode_Checked(object obj) { if (obj is string) { var tag = obj as string; if (tag == "ListMode") { IsListMode = true; Settings.Default.AppProperties.RecentFileListMode = 1; } else { IsListMode = false; Settings.Default.AppProperties.RecentFileListMode = 0; } Settings.Default.Save(); RecentFilesSelectionHandler?.Invoke(null, IsListMode); } } /// /// 移除文件记录:单个文件 /// /// private void RemoveFileItem_MenuItemClick(object obj) { var openFileInfo = obj as OpenFileInfo; if (openFileInfo != null) { bool needDelete = false; //判断是否需要执行删除操作 if (!Settings.Default.AppProperties.DontTipBeforeDeleteRecent) { AlertsMessage alertsMessage = new AlertsMessage(); alertsMessage.HaveCheckBoxShow("提示", "删除文件", "不再提示", "取消", "OK"); if(alertsMessage.result == ContentResult.Ok) { needDelete = true; //已经勾选 下次不再提示 Settings.Default.AppProperties.DontTipBeforeDeleteRecent = alertsMessage.isChecked; Settings.Default.Save(); } else { needDelete = false; } } else { needDelete = true; } if (needDelete) { SettingHelper.RemoveRecentOpenFile(openFileInfo.FilePath); RecentFilesGroup.Remove(openFileInfo); } } } /// /// 删除按钮触发事件:选中的文件 /// /// 选中的文档 private void RemoveFilesFromContainer_BtnClick(object obj) { System.Collections.IList items = (System.Collections.IList)obj; if (items == null || items.Cast() == null) return; var collection = items.Cast(); var openFileInfo = collection.ToList(); if(openFileInfo != null) { string msg = ""; int SelectedItemsType = 0; if (openFileInfo.Count == RecentFilesGroup.Count || openFileInfo.Count == 0) { if(openFileInfo.Count == 1 && RecentFilesGroup.Count == 1) msg = "ClearFile"; else msg = "AllClearFiles"; SelectedItemsType = 0; } else if(openFileInfo.Count == 1) { msg = "ClearFile"; SelectedItemsType = 1; } else { msg = "ClearSelectedFiles"; SelectedItemsType = 2; } bool needDelete = false; if (!Settings.Default.AppProperties.DontTipBeforeDeleteRecent) { AlertsMessage alertsMessage = new AlertsMessage(); alertsMessage.HaveCheckBoxShow("提示", msg, "不再提示", "取消", "OK"); if (alertsMessage.result == ContentResult.Ok) { needDelete = true; Settings.Default.AppProperties.DontTipBeforeDeleteRecent = alertsMessage.isChecked; Settings.Default.Save(); } else { needDelete = false; } } else { needDelete = true; } if (needDelete) { RemoveRecentFilesFrom(SelectedItemsType, openFileInfo); } } } /// /// 删除最近文件的操作 /// /// 0:全部文件;1:一个文件;2:多个文件 /// 选中的文件 private void RemoveRecentFilesFrom(int selectedItemsType, List openFileInfo) { if (selectedItemsType == 0) { SettingHelper.RemoveAllRecentOpenFiles(); RecentFilesGroup.Clear(); } else if (selectedItemsType == 1) { var file = openFileInfo[0] as OpenFileInfo; SettingHelper.RemoveRecentOpenFile(file.FilePath); RecentFilesGroup.Remove(file); } else { foreach (var item in openFileInfo) { SettingHelper.RemoveRecentOpenFile(item.FilePath); RecentFilesGroup.Remove(item); } } } /// /// 空状态时,点击文件浏览器弹窗,打开文件 /// private void OpenFiles_EmptyListsClick(object obj) { var dlg = new OpenFileDialog(); dlg.Multiselect = true; dlg.Filter = Properties.Resources.OpenDialogFilter; if (dlg.ShowDialog() == true) { LoadPdfViewer(dlg.FileNames); } } /// /// 打开文件路径 /// private void ExplorerFile_MenuItemClick(object obj) { try { var fileInfo = obj as OpenFileInfo; if (fileInfo != null) { if (string.IsNullOrEmpty(fileInfo.FilePath) == false) { if (!File.Exists(fileInfo.FilePath)) { AlertsMessage alertsMessage = new AlertsMessage(); alertsMessage.ShowDialog("提示", "文件不存在", "OK"); SettingHelper.RemoveRecentOpenFile(fileInfo.FilePath); RecentFilesGroup.Remove(fileInfo); } else { Process.Start("explorer", "/select,\"" + fileInfo.FilePath + "\""); } } } } catch (Exception ex) { } } /// /// 从最近列表里,打开文档 /// /// private void OpenRecentFiles_MouseDoubleClick(object obj) { var fileInfo = obj as OpenFileInfo; if (fileInfo != null) { if (File.Exists(fileInfo.FilePath)) { string[] filePath = new string[1]; filePath[0] = fileInfo.FilePath; LoadPdfViewer(filePath); } else { AlertsMessage alertsMessage = new AlertsMessage(); alertsMessage.ShowDialog("提示", "文件不存在", "OK"); SettingHelper.RemoveRecentOpenFile(fileInfo.FilePath); RecentFilesGroup.Remove(fileInfo); } } } /// /// 打开文档 /// /// public void LoadPdfViewer(string[] filePaths) { var content = App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel; if (filePaths.Count() == 1) { if (App.OpenedFileList.Contains(filePaths[0])) { App.mainWindowViewModel.SelectItem(filePaths[0]); } else { content.OpenFile(filePaths[0]); } ToolMethod.SetFileThumbImg(filePaths[0]); } else { var fileList = filePaths.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList(); if (fileList.Count <= 0) return; content.OpenFile(filePaths[0]); for (int i = 1; i < fileList.Count(); i++) { if (!App.OpenedFileList.Contains(fileList[i])) { App.mainWindowViewModel.AddTabItem(fileList[i]); } ToolMethod.SetFileThumbImg(fileList[i]); } } Settings.Default.Save(); } } }