123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- 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<IDialogResult> RequestClose;
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- if (tempDocument != null)
- {
- tempDocument.Release();
- }
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- CPDFDocument doc = null;
- parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
- parameters.TryGetValue<EnumPasswordKind>(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
- }
- }
|