using ComPDFKit.PDFDocument; using Microsoft.Office.Interop.Word; using PDF_Master.Helper; using PDF_Master.Model; using PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs; using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel; using Visibility = System.Windows.Visibility; namespace PDF_Master.ViewModels.Dialog.ToolsDialogs.SaftyDialogs { public class CheckPasswordDialogViewModel : BindableBase, IDialogAware { #region 参数和属性 private CPDFDocument document; private CPDFDocument tempDocument; #endregion private EnumPasswordKind passwordKind = EnumPasswordKind.StatusOpenPassword; private string _password = ""; public string Password { get { return _password; } set { SetProperty(ref _password, value); IsRightValue = "unknow"; if (!string.IsNullOrEmpty(value)) { ClearPasswordButtonVisibility = Visibility.Visible; } else { ClearPasswordButtonVisibility = Visibility.Hidden; } } } private string _inputPasswordMsg; public string InputPasswordMsg { get { return _inputPasswordMsg; } set { SetProperty(ref _inputPasswordMsg, value); } } private string _checkPasswordMsg; public string CheckPasswordMsg { get { return _checkPasswordMsg; } set { SetProperty(ref _checkPasswordMsg, value); } } private string _errorPasswordMsg; public string ErrorPasswordMsg { get { return _errorPasswordMsg; } set { SetProperty(ref _errorPasswordMsg, value); } } private string _passwordDialogTitle; public string PasswordDialogTitle { get { return _passwordDialogTitle; } set { SetProperty(ref _passwordDialogTitle, value); } } private string _isRightValue = "unknown"; public string IsRightValue { get { return _isRightValue; } set { SetProperty(ref _isRightValue, value); if (value == "false") { ErrorPasswordMsgVisibility = Visibility.Visible; } else { ErrorPasswordMsgVisibility = Visibility.Hidden; } } } private Visibility _clearPasswordButtonVisibility = Visibility.Hidden; public Visibility ClearPasswordButtonVisibility { get { return _clearPasswordButtonVisibility; } set { SetProperty(ref _clearPasswordButtonVisibility, value); } } private Visibility _errorPasswordMsgVisibility = Visibility.Hidden; public Visibility ErrorPasswordMsgVisibility { get { return _errorPasswordMsgVisibility; } set { SetProperty(ref _errorPasswordMsgVisibility, value); } } #region 委托声明 public DelegateCommand ConfirmCommand { get; set; } public DelegateCommand CancelCommand { get; set; } public DelegateCommand ClearPasswordCommand { get; set; } #endregion public CheckPasswordDialogViewModel() { ConfirmCommand = new DelegateCommand(Confirm); CancelCommand = new DelegateCommand(Cancel); ClearPasswordCommand = new DelegateCommand(ClearPassword); } #region 逻辑函数 private void Confirm() { if (passwordKind == EnumPasswordKind.StatusOpenPassword)//TODO: 用于检测输入密码的权限,以及是否输入了正确的密码 { if (tempDocument.UnlockWithPassword(Password)) { var dialogResult = new DialogResult(ButtonResult.OK); dialogResult.Parameters.Add(ParameterNames.Password, Password); dialogResult.Parameters.Add(ParameterNames.PasswordResult, true); RequestClose.Invoke(dialogResult); } else { IsRightValue = "false"; } } else { if (tempDocument.UnlockWithPassword(Password) && (tempDocument.CheckOwnerPassword(Password)||SecurityHelper.CheckHaveAllPermissions(tempDocument))) { var dialogResult = new DialogResult(ButtonResult.OK); dialogResult.Parameters.Add(ParameterNames.Password, Password); dialogResult.Parameters.Add(ParameterNames.PasswordResult, true); RequestClose.Invoke(dialogResult); } else { IsRightValue = "false"; } } } private void Cancel() { var dialogResult = new DialogResult(ButtonResult.Cancel); dialogResult.Parameters.Add(ParameterNames.Password, null); dialogResult.Parameters.Add(ParameterNames.PasswordResult, false); RequestClose.Invoke(dialogResult); } private void ClearPassword() { Password = ""; } #endregion #region 框架相关 public string Title => ""; public event Action RequestClose; public bool CanCloseDialog() { return true; } public void OnDialogClosed() { if (tempDocument != null) { tempDocument.Release(); } } public void OnDialogOpened(IDialogParameters parameters) { CPDFDocument doc = null; parameters.TryGetValue(ParameterNames.PDFDocument, out doc); parameters.TryGetValue(ParameterNames.PasswordKind, out passwordKind); if (doc != null) { document = doc; tempDocument = CPDFDocument.InitWithFilePath(document.FilePath); if(passwordKind == EnumPasswordKind.StatusOpenPassword) { PasswordDialogTitle = "Open Password"; } else { PasswordDialogTitle = "Permission Password"; } InputPasswordMsg = "\"" + document.FileName + ".pdf\"" + " is protected, please enter a Document Password."; CheckPasswordMsg = "Password"; //跟登陆窗口,错误密码的提示保持一致 ErrorPasswordMsg = App.ServiceLoader.GetString("code311"); ; } } #endregion } }