PasswordHelper.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using ComPDFKit.Controls.Common;
  4. using ComPDFKit.Controls.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.InitDocument(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 !IsOwnerLocked(document);
  61. }
  62. public static bool IsOwnerLocked(CPDFDocument document)
  63. {
  64. var info = document.GetPermissionsInfo();
  65. return !info.AllowsCopying || !info.AllowsPrinting || !info.AllowsCommenting ||
  66. !info.AllowsHighQualityPrinting || !info.AllowsDocumentChanges || !info.AllowsDocumentAssembly;
  67. }
  68. }
  69. }