PasswordHelper.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using Compdfkit_Tools.Common;
  4. using Compdfkit_Tools.Helper;
  5. using ComPDFKit.PDFDocument;
  6. namespace PasswordBoxPlus.Helper
  7. {
  8. public class PasswordHelper
  9. {
  10. public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordHelper),
  11. new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged)));
  12. public static string GetPassword(DependencyObject d)
  13. {
  14. return (string)d.GetValue(PasswordProperty);
  15. }
  16. public static void SetPassword(DependencyObject d, string value)
  17. {
  18. d.SetValue(PasswordProperty, value);
  19. }
  20. public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached("Attach", typeof(string), typeof(PasswordHelper),
  21. new PropertyMetadata(new PropertyChangedCallback(OnAttachChanged)));
  22. public static string GetAttach(DependencyObject d)
  23. {
  24. return (string)d.GetValue(AttachProperty);
  25. }
  26. public static void SetAttach(DependencyObject d, string value)
  27. {
  28. d.SetValue(AttachProperty, value);
  29. }
  30. static bool isUpdating = false;
  31. private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  32. {
  33. PasswordBox passwordBox = d as PasswordBox;
  34. passwordBox.PasswordChanged -= passwordBox_PasswordChanged;
  35. if (!isUpdating)
  36. (d as PasswordBox).Password = e.NewValue.ToString();
  37. passwordBox.PasswordChanged += passwordBox_PasswordChanged;
  38. }
  39. private static void OnAttachChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  40. {
  41. PasswordBox passwordBox = d as PasswordBox;
  42. passwordBox.PasswordChanged += passwordBox_PasswordChanged;
  43. }
  44. private static void passwordBox_PasswordChanged(object sender, RoutedEventArgs e)
  45. {
  46. PasswordBox passwordBox = sender as PasswordBox;
  47. isUpdating = true;
  48. SetPassword(passwordBox, passwordBox.Password);
  49. isUpdating = false;
  50. }
  51. public static bool UnlockWithOwnerPassword(CPDFDocument document)
  52. {
  53. PasswordWindow window = new PasswordWindow();
  54. window.InitWithDocument(document);
  55. window.PasswordType = PasswordType.OwnerPassword;
  56. if (Application.Current.MainWindow != null)
  57. window.Owner = Window.GetWindow(Application.Current.MainWindow);
  58. window.PasswordDialog.SetShowText(document.FileName + " " + LanguageHelper.CommonManager.GetString("Tip_Permission"));
  59. window.ShowDialog();
  60. return document.GetPermissionsInfo().AllowsCopying || document.GetPermissionsInfo().AllowsPrinting;
  61. }
  62. }
  63. }