123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516 |
- using Microsoft.Win32;
- using PDF_Master.CustomControl;
- using PDF_Master.Helper;
- using PDF_Master.Properties;
- using PDF_Master.Views;
- using PDFSettings;
- 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_Master.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_menuOpenFile;
- public string T_MenuOpenFile
- {
- get { return T_menuOpenFile; }
- set
- {
- SetProperty(ref T_menuOpenFile, 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");
- T_MenuOpenFile = App.HomePageLoader.GetString("Home_OpenFiles");
- }
- #endregion
- #region 属性
- private ObservableCollection<OpenFileInfo> _recentFilesGroup = new ObservableCollection<OpenFileInfo>();
- /// <summary>
- /// 最近列表:文件集合
- /// </summary>
- public ObservableCollection<OpenFileInfo> RecentFilesGroup
- {
- get { return _recentFilesGroup; }
- set
- {
- SetProperty(ref _recentFilesGroup, value);
- }
- }
- private bool _isEmpty = false;
- /// <summary>
- /// 最近列表是否为空
- /// </summary>
- public bool IsEmpty
- {
- get { return _isEmpty; }
- set
- {
- SetProperty(ref _isEmpty, value);
- }
- }
- private bool _isListMode = false;
- /// <summary>
- /// 是否为列表模式
- /// </summary>
- public bool IsListMode
- {
- get { return _isListMode; }
- set
- {
- SetProperty(ref _isListMode, value);
- }
- }
- #endregion
- #region Command
- public DelegateCommand<object> RemoveFileItemCommand { get; set; }
- public DelegateCommand<object> RemoveFilesFromContainerCommand { get; set; }
- public DelegateCommand<object> OpenRecentFilesCommand { get; set; }
- public DelegateCommand<object> OpenFilesCommand { get; set; }
- public DelegateCommand<object> ListModeCheckedCommand { get; set; }
- public DelegateCommand<object> ExplorerFileCommand { get; set; }
- public event EventHandler<bool> RecentFilesSelectionHandler;
- #endregion
- public RecentFilesContentViewModel()
- {
- InitVariables();
- InitCommands();
- InitEvents();
- InitString();
- }
- #region 初始化
- private void InitVariables()
- {
- RecentFilesGroup = new ObservableCollection<OpenFileInfo>(Settings.Default.RecentOpenFiles);
- RecentFileGroupIsEmpty();
- int mode = Settings.Default.AppProperties.RecentFileListMode;
- if (mode == 0)
- IsListMode = false;
- else
- IsListMode = true;
- }
- /// <summary>
- /// 切换页签后同步更新最近文件列表集合
- /// </summary>
- public void UpdateItemSoucre()
- {
- //补充判空处理,避免清空最近文件列表后,切换多页签会报异常
- if (RecentFilesGroup.Count > 0)
- {
- var item = RecentFilesGroup[0];
- var compageitem = Settings.Default.RecentOpenFiles[0];
- //因为新增文件都会排序到第一个,因此只需要比较集合数量和第一个对象是否一致即可判断两个集合是否一致
- //如果不一致则更新数据源
- if (item != compageitem || RecentFilesGroup.Count != Settings.Default.RecentOpenFiles.Count)
- {
- InitVariables();
- }
- }
- }
- private void InitCommands()
- {
- RemoveFilesFromContainerCommand = new DelegateCommand<object>(RemoveFilesFromContainer_BtnClick);
- RemoveFileItemCommand = new DelegateCommand<object>(RemoveFileItem_MenuItemClick);
- OpenRecentFilesCommand = new DelegateCommand<object>(OpenRecentFiles_MouseDoubleClick);
- ListModeCheckedCommand = new DelegateCommand<object>(ListMode_Checked);
- OpenFilesCommand = new DelegateCommand<object>(OpenFiles_EmptyListsClick);
- ExplorerFileCommand = new DelegateCommand<object>(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
- /// <summary>
- /// 最近列表:文件列表模式选中事件
- /// </summary>
- /// <param name="obj"></param>
- 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);
- }
- }
- /// <summary>
- /// 移除文件记录:单个文件
- /// </summary>
- /// <param name="obj"></param>
- 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("", App.MainPageLoader.GetString("RecentFiles_AllClearMsg"), App.MainPageLoader.GetString("Main_DoNotRemindAgain"), App.ServiceLoader.GetString("Text_cancel"), App.ServiceLoader.GetString("Text_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);
- }
-
- }
- }
- /// <summary>
- /// 删除按钮触发事件:选中的文件
- /// </summary>
- /// <param name="obj">选中的文档</param>
- private void RemoveFilesFromContainer_BtnClick(object obj)
- {
- System.Collections.IList items = (System.Collections.IList)obj;
- if (items == null || items.Cast<OpenFileInfo>() == null)
- return;
- var collection = items.Cast<OpenFileInfo>();
- 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 = App.MainPageLoader.GetString("RecentFiles_ClearMsg");
- else
- msg = App.MainPageLoader.GetString("RecentFiles_AllClearMsg");
- SelectedItemsType = 0;
- }
- else if(openFileInfo.Count == 1)
- {
- msg = App.MainPageLoader.GetString("RecentFiles_ClearMsg");
- SelectedItemsType = 1;
- }
- else
- {
- msg = App.MainPageLoader.GetString("RecentFiles_SelectedClearMsg");
- SelectedItemsType = 2;
- }
- bool needDelete = false;
- if (!Settings.Default.AppProperties.DontTipBeforeDeleteRecent)
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.HaveCheckBoxShow("",msg, App.MainPageLoader.GetString("Main_DoNotRemindAgain"), App.ServiceLoader.GetString("Text_cancel"), App.ServiceLoader.GetString("Text_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);
- }
- }
- }
- /// <summary>
- /// 删除最近文件的操作
- /// </summary>
- /// <param name="selectedItemsType">0:全部文件;1:一个文件;2:多个文件</param>
- /// <param name="openFileInfo">选中的文件</param>
- private void RemoveRecentFilesFrom(int selectedItemsType, List<OpenFileInfo> 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);
- }
- }
- }
- /// <summary>
- /// 空状态时,点击文件浏览器弹窗,打开文件
- /// </summary>
- 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);
- }
- }
- /// <summary>
- /// 打开文件路径
- /// </summary>
- 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("", App.MainPageLoader.GetString("FileNotExistWarning"), App.ServiceLoader.GetString("Text_ok"));
- SettingHelper.RemoveRecentOpenFile(fileInfo.FilePath);
- RecentFilesGroup.Remove(fileInfo);
- }
- else
- {
- Process.Start("explorer", "/select,\"" + fileInfo.FilePath + "\"");
- }
- }
- }
- }
- catch
- {
- }
-
- }
- /// <summary>
- /// 从最近列表里,打开文档
- /// </summary>
- /// <param name="obj"></param>
- 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("", App.MainPageLoader.GetString("FileNotExistWarning"), App.ServiceLoader.GetString("Text_ok"));
- SettingHelper.RemoveRecentOpenFile(fileInfo.FilePath);
- RecentFilesGroup.Remove(fileInfo);
- }
- }
- }
- /// <summary>
- /// 打开文档
- /// </summary>
- /// <param name="filePaths"></param>
- 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();
- }
- }
- }
|