Ver código fonte

安全-删除安全设置

liuaoran 2 anos atrás
pai
commit
cdc8c4d532

+ 5 - 1
PDF Office/Helper/SecurityHelper.cs

@@ -50,11 +50,15 @@ namespace PDF_Office.Helper
                     }
                 }
             }
+            string password = "";
             DialogParameters value = new DialogParameters();
             value.Add(ParameterNames.PasswordKind, EnumPasswordKind.StatusPermissionsPassword);
             value.Add(ParameterNames.PDFDocument, document);
             bool isDiscryptied = false;
-            dialogService.ShowDialog(DialogNames.CheckPasswordDialog, value, e => { isDiscryptied = e.Parameters.GetValue<bool>(ParameterNames.PasswordResult); });
+            dialogService.ShowDialog(DialogNames.CheckPasswordDialog, value, e => { 
+                isDiscryptied = e.Parameters.GetValue<bool>(ParameterNames.PasswordResult);
+                password = e.Parameters.GetValue<string>(ParameterNames.Password);
+            });
             return isDiscryptied;
         }
     }

+ 1 - 0
PDF Office/Model/ParameterNames.cs

@@ -112,5 +112,6 @@ namespace PDF_Office.Model
         /// </summary>
         public static string PasswordKind = "PasswordKind";
         public static string PasswordResult = "PasswordResult";
+        public static string Password = "Password";
     }
 }

+ 2 - 1
PDF Office/ViewModels/Dialog/ToolsDialogs/SaftyDialogs/CheckPasswordDialogViewModel.cs

@@ -110,6 +110,7 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
                 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);
                 }
@@ -125,6 +126,7 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
                     if (SecurityHelper.CheckHaveAllPermissions(tempDocument))
                     {
                         var dialogResult = new DialogResult(ButtonResult.OK);
+                        dialogResult.Parameters.Add(ParameterNames.Password, Password);
                         dialogResult.Parameters.Add(ParameterNames.PasswordResult, true);
                         RequestClose.Invoke(dialogResult);
                     }
@@ -140,7 +142,6 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
                     IsRightValue = "false";
                 }
             }
-
         }
 
         private void Cancel()

+ 14 - 107
PDF Office/ViewModels/Dialog/ToolsDialogs/SaftyDialogs/DeleteSafetySettingsDialogViewModel.cs

@@ -28,19 +28,18 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
         #endregion
 
         #region 委托声明
-        public DelegateCommand RemoveSecurityCommand { get; set; }
         public DelegateCommand CancelCommand { get; set; }
+        public  DelegateCommand RemoveSecuritySettingsCommand { get; set; }
         #endregion
 
         public DeleteSafetySettingsDialogViewModel(IDialogService dialogService)
         {
             dialogs = dialogService;
-            RemoveSecurityCommand = new DelegateCommand(RemoveSecurity);
+            RemoveSecuritySettingsCommand = new DelegateCommand(RemoveSecuritySettings);
             CancelCommand = new DelegateCommand(Cancel);
         }
 
         #region 检查函数
-
         /// <summary>
         /// 检测是否需要输入密码
         /// </summary>
@@ -77,11 +76,11 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
         #endregion
 
         #region 逻辑函数
-        public void RemoveSecurity()
+        /// <summary>
+        /// 逻辑优化:判权操作放到密码弹窗里,所以一旦进入该状态,可以直接清除权限
+        /// </summary>
+        public void RemoveSecuritySettings()
         {
-            EnumNeedPassword enumNeedPassword = CheckNeedPassword();
-            bool isDiscryptied = false;
-            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
             System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
             /*
              *设置这个对话框的起始保存路径
@@ -94,114 +93,27 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
             /*
              *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
              */
-            sfd.FileName = document.FileName + "_CompressFile.pdf";
+            sfd.FileName = document.FileName + "_DecryptedFile.pdf";
             /*
              * 做一些工作
              */
-            if (enumNeedPassword == EnumNeedPassword.StatusVerifyPassword)
-            {
-                DialogParameters value = new DialogParameters();
-                value.Add("PasswordKind", EnumPasswordKind.StatusPermissionsPassword);
-                dialogs.ShowDialog(DialogNames.CheckPasswordDialog, value, e => { isDiscryptied = e.Parameters.GetValue<bool>("CheckPassword"); });
-                if (isDiscryptied)
-                {
-                    if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
-                    {
-                        document.Descrypt(sfd.FileName);
-                        MessageBoxEx.Show("保存成功");
-                        RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
-                    }
-                    else
-                    {
-                        MessageBox.Show("取消保存");
-                    }
-                }
-                else
-                {
-                }
-            }
-            else if (CheckNeedPassword() == DeleteSafetySettintgsModel.EnumNeedPassword.StatusDirectDescrypt)
+            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
             {
-                if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
-                {
-                    document.Descrypt(sfd.FileName);
-                    MessageBoxEx.Show("Ok");
+                if(document.Descrypt(sfd.FileName)){
+                    MessageBoxEx.Show("保存成功");
                     RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
                 }
                 else
                 {
-                    MessageBoxEx.Show("取消保存");
-                }
-            }
-            else if (CheckNeedPassword() == DeleteSafetySettintgsModel.EnumNeedPassword.StatusCannotDecrypt)
-            {
-                MessageBoxEx.Show("无可用安全性设置");
-                RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
+                    ///TODO 不明原因干扰
+                } 
             }
             else
-            { }
-        }
-
-        private void CheckPassword()
-        {
-            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
-            System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
-            /*
-             *设置这个对话框的起始保存路径
-             */
-            sfd.InitialDirectory = document.FilePath;
-            /*
-             *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
-             */
-            sfd.Filter = "PDF|*.pdf;";
-            /*
-             *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
-             **/
-            sfd.FileName = document.FileName + "_CompressFile.pdf";
-            /*
-             * 做一些工作
-             */
-            if (CheckNeedPassword() == DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword)
             {
-                bool IfDiscryptied = false;
-                DialogParameters value = new DialogParameters();
-                dialogs.ShowDialog(DialogNames.CheckPasswordDialog, value, e => { IfDiscryptied = e.Parameters.GetValue<bool>("CheckPassword"); });
-                if (IfDiscryptied)
-                {
-                    if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
-                    {
-                        document.Descrypt(sfd.FileName);
-                        MessageBox.Show("保存成功");
-                        RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
-                    }
-                    else
-                    {
-                        MessageBox.Show("取消保存");
-                    }
-                }
-                else if (CheckNeedPassword() == DeleteSafetySettintgsModel.EnumNeedPassword.StatusDirectDescrypt)
-                {
-                    if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
-                    {
-                        document.Descrypt(sfd.FileName);
-                        MessageBox.Show("Ok");
-                        RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
-                    }
-                    else
-                    {
-                        MessageBox.Show("取消保存");
-                    }
-                }
-                else if (CheckNeedPassword() == DeleteSafetySettintgsModel.EnumNeedPassword.StatusCannotDecrypt)
-                {
-                    MessageBox.Show("无可用安全性设置");
-                }
-                else
-                { }
-                RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
+                MessageBox.Show("取消保存");
             }
         }
-
+         
         private void Cancel()
         {
             RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
@@ -231,12 +143,7 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
             {
                 document = doc;
                 DeleteSecurityMsg = "Are you sure you want to remove the security settings for”" + document.FileName + "” documents?";
-                if (SecurityHelper.VerifyPassword(document, EnumPasswordKind.StatusPermissionsPassword, dialogs))
-                {
-
-                }
             }
-
         }
         #endregion
     }

+ 16 - 1
PDF Office/ViewModels/Dialog/ToolsDialogs/SaftyDialogs/SetPasswordDialogViewModel.cs

@@ -15,6 +15,7 @@ using System.Windows.Controls;
 using CheckBox = System.Windows.Controls.CheckBox;
 using System.Drawing.Printing;
 using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.SetPasswordDialogModel;
+using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
 
 namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
 {
@@ -23,6 +24,7 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
         #region 参数和属性
         private SetPasswordDialogModel setPasswordDialogModel = new SetPasswordDialogModel();
         private CPDFDocument document;
+        public IDialogService dialogs;
 
         private EnumPermissionsMod enumPermissionsMod = EnumPermissionsMod.StatusUnknown | EnumPermissionsMod.StatusRestrictPrinting | EnumPermissionsMod.StatusRestrictCopying;
 
@@ -177,17 +179,20 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
         public DelegateCommand<object> SetRestrictCommand { get; set; }
         public DelegateCommand EncryptCommand { get; set; }
         public DelegateCommand CancelEncryptCommand { get; set; }
+        public DelegateCommand BatchSetPasswordCommand { get; set; }
 
         #endregion
 
-        public SetPasswordDialogViewModel()
+        public SetPasswordDialogViewModel(IDialogService dialogService)
         {
+            dialogs = dialogService;
             DisplayPasswordCommand = new DelegateCommand<object>(DisplayPassword);
             SetOpenPasswordCommand = new DelegateCommand<object>(SetOpenPassword);
             SetPermissionsPasswordCommand = new DelegateCommand<object>(SetPermissionsPassword);
             SetRestrictCommand = new DelegateCommand<object>(SetRestrict);
             EncryptCommand = new DelegateCommand(Encrypt);
             CancelEncryptCommand = new DelegateCommand(CancelEncrypt);
+            BatchSetPasswordCommand = new DelegateCommand(BatchSetPassword);
         }
 
         #region 检查和初始化
@@ -309,6 +314,16 @@ namespace PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs
             CheckEnableConfirmEncrypt();
         }
 
+        private void BatchSetPassword()
+        {
+            DialogParameters setPasswordpdf = new DialogParameters();
+            setPasswordpdf.Add(ParameterNames.BatchProcessing_Name, "2");
+            HomePageBatchProcessingDialogModel.FilePaths = new List<string> { document.FilePath.ToString() };
+            HomePageBatchProcessingDialogModel.BatchProcessingIndex = 2;
+            setPasswordpdf.Add(ParameterNames.FilePath, new string[] { document.FilePath.ToString() });
+            dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, setPasswordpdf, e => { });
+        }
+
 
         public void Encrypt()
         {

+ 30 - 21
PDF Office/ViewModels/Tools/ToolsBarContentViewModel.cs

@@ -1,5 +1,7 @@
 using ComPDFKitViewer.PdfViewer;
+using PDF_Office.CustomControl;
 using PDF_Office.EventAggregators;
+using PDF_Office.Helper;
 using PDF_Office.Model;
 using Prism.Commands;
 using Prism.Events;
@@ -10,10 +12,12 @@ using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Security.Cryptography.X509Certificates;
+using System.Windows;
+using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
 
 namespace PDF_Office.ViewModels.Tools
 {
-    public class ToolsBarContentViewModel : BindableBase,INavigationAware
+    public class ToolsBarContentViewModel : BindableBase, INavigationAware
     {
         private CPDFViewer PDFViewer;
 
@@ -23,31 +27,23 @@ namespace PDF_Office.ViewModels.Tools
 
         public IEventAggregator eventAggregator;
 
-        public  string unicode = null;
+        public string unicode = null;
 
         /// <summary>
         /// 是否是第一次加载
         /// </summary>
         private bool isFirstLoad = true;
-
         public DelegateCommand CompressCommand { get; set; }
-
         public DelegateCommand MergeCommand { get; set; }
-
         public DelegateCommand SetPasswordCommand { get; set; }
-
         public DelegateCommand CancelPasswordCommand { get; set; }
-
         public DelegateCommand<object> SetEditToolsCommand { get; set; }
-
         public DelegateCommand SetWatermarkCommand { get; set; }
 
-
-
         public ToolsBarContentViewModel(IDialogService dialogService, IEventAggregator eventAggregator)
         {
-            dialogs= dialogService;
-            this.eventAggregator= eventAggregator;
+            dialogs = dialogService;
+            this.eventAggregator = eventAggregator;
             unicode = App.mainWindowViewModel.SelectedItem.Unicode;
             CompressCommand = new DelegateCommand(OpenCompressDialog);
             MergeCommand = new DelegateCommand(MergeDialog);
@@ -61,7 +57,7 @@ namespace PDF_Office.ViewModels.Tools
             var args = e as System.Windows.Controls.Button;
             if (args != null)
             {
-                this.eventAggregator.GetEvent<EnterSelectedEditToolEvent>().Publish(new StringWithUnicode() { Unicode=unicode,EditToolsContentName=args.Name}) ;
+                this.eventAggregator.GetEvent<EnterSelectedEditToolEvent>().Publish(new StringWithUnicode() { Unicode = unicode, EditToolsContentName = args.Name });
             }
         }
 
@@ -70,27 +66,40 @@ namespace PDF_Office.ViewModels.Tools
         {
             DialogParameters value = new DialogParameters();
             value.Add(ParameterNames.PDFViewer, PDFViewer);
-            dialogs.ShowDialog(DialogNames.CompressDialog, value, e => {  });
+            dialogs.ShowDialog(DialogNames.CompressDialog, value, e => { });
         }
         private void MergeDialog()
         {
             DialogParameters value = new DialogParameters();
             value.Add(ParameterNames.PDFViewer, PDFViewer);
-            dialogs.ShowDialog(DialogNames.MergeDialog, value, e => {  });
+            dialogs.ShowDialog(DialogNames.MergeDialog, value, e => { });
         }
 
         private void OpenSetPasswordDialog()
         {
-            DialogParameters value = new DialogParameters();
-            value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
-            dialogs.ShowDialog(DialogNames.SetPasswordDialog, value, e => { });
+            if (SecurityHelper.VerifyPassword(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs))
+            {
+                DialogParameters value = new DialogParameters();
+                value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
+                dialogs.ShowDialog(DialogNames.SetPasswordDialog, value, e => { });
+            }
         }
 
         private void OpenCancelPasswordDialog()
         {
-            DialogParameters value = new DialogParameters();
-            value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
-            dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e => { });
+            if (!PDFViewer.Document.IsEncrypted)
+            {
+                MessageBoxEx.Show("No security settings available ");
+            }
+            else
+            {
+                if (SecurityHelper.VerifyPassword(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs))
+                {
+                    DialogParameters value = new DialogParameters();
+                    value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
+                    dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e => { });
+                }
+            }
         }
 
         public bool IsNavigationTarget(NavigationContext navigationContext)

+ 2 - 2
PDF Office/Views/Dialog/ToolsDialogs/SaftyDialogs/CheckPasswordDialog.xaml

@@ -77,7 +77,7 @@
     </UserControl.Resources>
     <cus:DialogContent Header="Open Pasword" FontFamily="Segoe UI">
         <cus:DialogContent.Content>
-            <Grid Margin="16,0,16,0" Width="400" Height="106">
+            <Grid Margin="16,0,16,0" Width="400" MinHeight="106">
                 <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="80"></ColumnDefinition>
                     <ColumnDefinition></ColumnDefinition>
@@ -85,7 +85,7 @@
                 <Image Source="pack://application:,,,/Resources/Dialog/password.png"  Margin="0,-20,0,0"></Image>
                 <Grid Grid.Column="1" Margin="8,0,0,0">
                     <Grid.RowDefinitions>
-                        <RowDefinition Height="44"></RowDefinition>
+                        <RowDefinition MinHeight="44"></RowDefinition>
                         <RowDefinition Height="40"></RowDefinition>
                         <RowDefinition></RowDefinition>
                     </Grid.RowDefinitions>

+ 11 - 10
PDF Office/Views/Dialog/ToolsDialogs/SaftyDialogs/DeleteSafetySettingsDialog.xaml

@@ -6,24 +6,25 @@
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:saftydialogs="clr-namespace:PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs" d:DataContext="{d:DesignInstance Type=saftydialogs:DeleteSafetySettingsDialogViewModel}"
              prism:ViewModelLocator.AutoWireViewModel="True"
            prism:Dialog.WindowStyle="{StaticResource DialogWindowStyle}"
-             Height="156"
+             MinHeight="156"
              Width="392"
-             mc:Ignorable="d">
+             mc:Ignorable="d"
+             Background="#FFFFFF">
     <Grid>
         <Grid.RowDefinitions>
-            <RowDefinition Name="HeadRow" Height="88" />
-            <RowDefinition Height="80"/>
+            <RowDefinition Name="HeadRow" MinHeight="88"  />
+            <RowDefinition Height="48"/>
         </Grid.RowDefinitions>
-        <TextBlock Grid.Row="0" Text="{Binding DeleteSecurityMsg, Mode=TwoWay}" TextWrapping="Wrap" FontSize="14" Margin="16,32,16,0" Block.TextAlignment="Left"/>
-        <StackPanel Grid.Row="1"  Orientation="Horizontal" HorizontalAlignment="Right">
-            <Button Background="WhiteSmoke" Width="100" Height="25" Margin="0,0,16,0" Command="{Binding RemoveSecurityCommand}">
+        <TextBlock Grid.Row="0" Text="{Binding DeleteSecurityMsg, Mode=TwoWay}" TextWrapping="Wrap" FontSize="14" Margin="16,32,16,32" Block.TextAlignment="Left" FontFamily="Segoe UI"/>
+        <StackPanel Grid.Row="1"  Orientation="Horizontal" HorizontalAlignment="Right" >
+            <Button Style="{StaticResource Btn.cta}" Width="80" Height="32" Margin="0,0,16,16" Command="{Binding RemoveSecuritySettingsCommand}">
                 <Border>
-                    <TextBlock Text="确定" ></TextBlock>
+                    <TextBlock Text="Remove" ></TextBlock>
                 </Border>
             </Button>
-            <Button Background="WhiteSmoke" Width="100" Height="25" Margin="0,0,16,0" Command="{Binding CancelCommand}">
+            <Button Style="{StaticResource btn.ghost}" Width="80" Height="32" Margin="0,0,16,16" Command="{Binding CancelCommand}">
                 <Border>
-                    <TextBlock Text="取消" ></TextBlock>
+                    <TextBlock Text="Cancel" ></TextBlock>
                 </Border>
             </Button>
         </StackPanel>

+ 1 - 1
PDF Office/Views/Dialog/ToolsDialogs/SaftyDialogs/SetPasswordDialog.xaml

@@ -278,7 +278,7 @@
         </cus:DialogContent.Content>
         <cus:DialogContent.BottmBar>
             <Grid Width="400">
-                <Button  Width="80"  Height="32"  HorizontalAlignment="Left" Command="{Binding BatchCommand}">
+                <Button  Width="80"  Height="32"  HorizontalAlignment="Left" Command="{Binding BatchSetPasswordCommand}">
                     <TextBlock Text="Batch"></TextBlock>
                 </Button>
                 <Button  Width="80"  Height="32"  Margin="0,0,96,0"  HorizontalAlignment="Right" Command="{Binding EncryptCommand}"  IsEnabled="{Binding EnableConfirm, Mode=TwoWay}" RenderTransformOrigin="1.5,1192508.3">