KMSecurityContentView.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // KMSecurityContentView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2025/1/8.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMSecurityContentView: BaseXibView {
  10. @IBOutlet weak var titleLabel: NSTextField!
  11. @IBOutlet var openPwdCheckBox: ComponentCheckBox!
  12. @IBOutlet var openPasswordView: ComponentPasswordView!
  13. @IBOutlet weak var openPwdCheckBoxWidthConstraint: NSLayoutConstraint!
  14. @IBOutlet var ownerPwdCheckBox: ComponentCheckBox!
  15. @IBOutlet var ownerPasswordView: ComponentPasswordView!
  16. @IBOutlet weak var ownerPwdCheckBoxWidthConstraint: NSLayoutConstraint!
  17. @IBOutlet var notPrintCheckBox: ComponentCheckBox!
  18. @IBOutlet var notCopyCheckBox: ComponentCheckBox!
  19. var model: KMSecureEncryptModel = KMSecureEncryptModel()
  20. override func draw(_ dirtyRect: NSRect) {
  21. super.draw(dirtyRect)
  22. // Drawing code here.
  23. }
  24. override func setup() {
  25. updateLanguage()
  26. self.updateOwnerButtonState()
  27. }
  28. func updateLanguage() {
  29. titleLabel.stringValue = KMLocalizedString("Password Security Settings")
  30. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  31. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  32. openPwdCheckBox.properties = ComponentCheckBoxProperty(size: .s, state: .normal, showhelp: true, text: KMLocalizedString("Require a password to open the document.", comment: ""), checkboxType: .normal)
  33. openPwdCheckBox.setTarget(self, action: #selector(openPasswordButtonAction(_:)))
  34. openPwdCheckBox.reloadData()
  35. openPwdCheckBoxWidthConstraint.constant = openPwdCheckBox.properties.propertyInfo.viewWidth
  36. openPasswordView.properties = ComponentPasswordProperty(size: .m,
  37. state: .normal,
  38. isError: false,
  39. placeholderString: "Please enter password",
  40. text: "",
  41. isDisabled: false,
  42. errorText: "Here is the error message description.")
  43. openPasswordView.toolTip = KMLocalizedString("Here is the error message description.")
  44. openPasswordView.delegate = self
  45. openPasswordView.reloadData()
  46. ownerPwdCheckBox.properties = ComponentCheckBoxProperty(size: .s, state: .normal, showhelp: true, text: KMLocalizedString("Require a password to open the document.", comment: ""), checkboxType: .normal)
  47. ownerPwdCheckBox.setTarget(self, action: #selector(ownerPaasswordButtonAction(_:)))
  48. ownerPasswordView.properties = ComponentPasswordProperty(size: .m,
  49. state: .normal,
  50. isError: false,
  51. placeholderString: "Please enter password",
  52. text: "",
  53. isDisabled: false,
  54. errorText: "Here is the error message description.")
  55. openPasswordView.toolTip = KMLocalizedString("Here is the error message description.")
  56. ownerPasswordView.delegate = self
  57. ownerPwdCheckBoxWidthConstraint.constant = openPwdCheckBox.properties.propertyInfo.viewWidth
  58. notPrintCheckBox.properties = ComponentCheckBoxProperty(size: .s, state: .normal, text: KMLocalizedString("Restrict document printing", comment: ""), checkboxType: .normal)
  59. notPrintCheckBox.setTarget(self, action: #selector(printButtonAction(_:)))
  60. notCopyCheckBox.properties = ComponentCheckBoxProperty(size: .s, state: .normal, text: KMLocalizedString("Restrict content copying", comment: ""), checkboxType: .normal)
  61. notCopyCheckBox.setTarget(self, action: #selector(copyButtonAction(_:)))
  62. }
  63. func reloadData() {
  64. let openPWisdiable = model.openPasswordOn ? false : true
  65. if openPasswordView.properties.isDisabled != openPWisdiable {
  66. openPasswordView.properties.isDisabled = model.openPasswordOn ? false : true
  67. openPasswordView.reloadData()
  68. }
  69. let ownerPWisdiable = model.ownerPasswordOn ? false : true
  70. if ownerPasswordView.properties.isDisabled != ownerPWisdiable {
  71. ownerPasswordView.properties.isDisabled = model.ownerPasswordOn ? false : true
  72. ownerPasswordView.reloadData()
  73. }
  74. notPrintCheckBox.properties.isDisabled = model.printEnabled ? false : true
  75. notPrintCheckBox.properties.checkboxType = model.printAllowed ? .normal : .selected
  76. notPrintCheckBox.reloadData()
  77. notCopyCheckBox.properties.isDisabled = model.editEnabled ? false : true
  78. notCopyCheckBox.properties.checkboxType = model.editAllowed ? .normal : .selected
  79. notCopyCheckBox.reloadData()
  80. }
  81. func updatePasswordState() {
  82. self.model.openPassword = openPasswordView.properties.text
  83. self.model.ownerPassword = ownerPasswordView.properties.text
  84. self.reloadData()
  85. }
  86. func updateOwnerButtonState() {
  87. if self.model.ownerPasswordOn {
  88. self.model.printEnabled = true
  89. self.model.editEnabled = true
  90. self.model.printAllowed = false
  91. self.model.editAllowed = false
  92. } else {
  93. self.model.printEnabled = false
  94. self.model.editEnabled = false
  95. self.model.printAllowed = true
  96. self.model.editAllowed = true
  97. }
  98. self.reloadData()
  99. }
  100. }
  101. extension KMSecurityContentView {
  102. @objc func openPasswordButtonAction(_ sender: Any) {
  103. self.model.openPasswordOn = self.openPwdCheckBox.properties.checkboxType == .selected ? true:false
  104. self.reloadData()
  105. }
  106. @objc func ownerPaasswordButtonAction(_ sender: Any) {
  107. self.model.ownerPasswordOn = self.ownerPwdCheckBox.properties.checkboxType == .selected ? true:false
  108. self.updateOwnerButtonState()
  109. }
  110. @objc func printButtonAction(_ sender: Any) {
  111. self.model.printAllowed = self.notPrintCheckBox.properties.checkboxType == .selected ? false:true
  112. self.reloadData()
  113. }
  114. @objc func copyButtonAction(_ sender: Any) {
  115. self.model.editAllowed = self.notCopyCheckBox.properties.checkboxType == .selected ? false:true
  116. self.reloadData()
  117. }
  118. }
  119. //MARK: - ComponentPasswordViewDelegate
  120. extension KMSecurityContentView: ComponentPasswordViewDelegate {
  121. func componentPasswordViewDidChanged(inputView: ComponentPasswordView) {
  122. self.updatePasswordState()
  123. }
  124. func componentPasswordViewDidEndEditing(inputView: ComponentPasswordView) {
  125. self.updatePasswordState()
  126. }
  127. }