KMSecurityContentView.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 isDisable: Bool = false {
  20. didSet {
  21. self.reloadData()
  22. }
  23. }
  24. var model: KMSecureEncryptModel = KMSecureEncryptModel()
  25. override func draw(_ dirtyRect: NSRect) {
  26. super.draw(dirtyRect)
  27. // Drawing code here.
  28. }
  29. override func setup() {
  30. updateLanguage()
  31. self.updateOwnerButtonState()
  32. }
  33. func updateLanguage() {
  34. titleLabel.stringValue = KMLocalizedString("Password Security Settings")
  35. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  36. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  37. openPwdCheckBox.properties = ComponentCheckBoxProperty(size: .s, state: .normal, showhelp: true, text: KMLocalizedString("Require a password to open the document.", comment: ""), checkboxType: .normal)
  38. openPwdCheckBox.setTarget(self, action: #selector(openPasswordButtonAction(_:)))
  39. openPwdCheckBox.reloadData()
  40. openPwdCheckBoxWidthConstraint.constant = openPwdCheckBox.properties.propertyInfo.viewWidth
  41. openPasswordView.properties = ComponentPasswordProperty(size: .m,
  42. state: .normal,
  43. isError: false,
  44. placeholderString: "Please enter password",
  45. text: "",
  46. isDisabled: false,
  47. errorText: "Here is the error message description.")
  48. openPasswordView.toolTip = KMLocalizedString("Here is the error message description.")
  49. openPasswordView.delegate = self
  50. openPasswordView.reloadData()
  51. ownerPwdCheckBox.properties = ComponentCheckBoxProperty(size: .s, state: .normal, showhelp: true, text: KMLocalizedString("Require a password to open the document.", comment: ""), checkboxType: .normal)
  52. ownerPwdCheckBox.setTarget(self, action: #selector(ownerPaasswordButtonAction(_:)))
  53. ownerPasswordView.properties = ComponentPasswordProperty(size: .m,
  54. state: .normal,
  55. isError: false,
  56. placeholderString: "Please enter password",
  57. text: "",
  58. isDisabled: false,
  59. errorText: "Here is the error message description.")
  60. openPasswordView.toolTip = KMLocalizedString("Here is the error message description.")
  61. ownerPasswordView.delegate = self
  62. ownerPwdCheckBoxWidthConstraint.constant = openPwdCheckBox.properties.propertyInfo.viewWidth
  63. notPrintCheckBox.properties = ComponentCheckBoxProperty(size: .s, state: .normal, text: KMLocalizedString("Restrict document printing", comment: ""), checkboxType: .normal)
  64. notPrintCheckBox.setTarget(self, action: #selector(printButtonAction(_:)))
  65. notCopyCheckBox.properties = ComponentCheckBoxProperty(size: .s, state: .normal, text: KMLocalizedString("Restrict content copying", comment: ""), checkboxType: .normal)
  66. notCopyCheckBox.setTarget(self, action: #selector(copyButtonAction(_:)))
  67. }
  68. func reloadData() {
  69. self.notPrintCheckBox.properties.isDisabled = self.isDisable
  70. self.notPrintCheckBox.reloadData()
  71. self.notCopyCheckBox.properties.isDisabled = self.isDisable
  72. self.notCopyCheckBox.reloadData()
  73. self.openPwdCheckBox.properties.isDisabled = self.isDisable
  74. self.openPwdCheckBox.reloadData()
  75. self.openPasswordView.properties.isDisabled = self.isDisable
  76. self.openPasswordView.reloadData()
  77. self.ownerPwdCheckBox.properties.isDisabled = self.isDisable
  78. self.ownerPwdCheckBox.reloadData()
  79. self.ownerPasswordView.properties.isDisabled = self.isDisable
  80. self.ownerPasswordView.reloadData()
  81. let openPWisdiable = model.openPasswordOn ? false : true
  82. if openPasswordView.properties.isDisabled != openPWisdiable {
  83. openPasswordView.properties.isDisabled = model.openPasswordOn ? false : true
  84. openPasswordView.reloadData()
  85. }
  86. let ownerPWisdiable = model.ownerPasswordOn ? false : true
  87. if ownerPasswordView.properties.isDisabled != ownerPWisdiable {
  88. ownerPasswordView.properties.isDisabled = model.ownerPasswordOn ? false : true
  89. ownerPasswordView.reloadData()
  90. }
  91. notPrintCheckBox.properties.isDisabled = model.printEnabled ? false : true
  92. notPrintCheckBox.properties.checkboxType = model.printAllowed ? .normal : .selected
  93. notPrintCheckBox.reloadData()
  94. notCopyCheckBox.properties.isDisabled = model.editEnabled ? false : true
  95. notCopyCheckBox.properties.checkboxType = model.editAllowed ? .normal : .selected
  96. notCopyCheckBox.reloadData()
  97. }
  98. func updatePasswordState() {
  99. self.model.openPassword = openPasswordView.properties.text
  100. self.model.ownerPassword = ownerPasswordView.properties.text
  101. // self.reloadData()
  102. }
  103. func updateOwnerButtonState() {
  104. if self.model.ownerPasswordOn {
  105. self.model.printEnabled = true
  106. self.model.editEnabled = true
  107. self.model.printAllowed = false
  108. self.model.editAllowed = false
  109. } else {
  110. self.model.printEnabled = false
  111. self.model.editEnabled = false
  112. self.model.printAllowed = true
  113. self.model.editAllowed = true
  114. }
  115. self.reloadData()
  116. }
  117. }
  118. extension KMSecurityContentView {
  119. @objc func openPasswordButtonAction(_ sender: Any) {
  120. self.model.openPasswordOn = self.openPwdCheckBox.properties.checkboxType == .selected ? true:false
  121. self.reloadData()
  122. }
  123. @objc func ownerPaasswordButtonAction(_ sender: Any) {
  124. self.model.ownerPasswordOn = self.ownerPwdCheckBox.properties.checkboxType == .selected ? true:false
  125. self.updateOwnerButtonState()
  126. }
  127. @objc func printButtonAction(_ sender: Any) {
  128. self.model.printAllowed = self.notPrintCheckBox.properties.checkboxType == .selected ? false:true
  129. self.reloadData()
  130. }
  131. @objc func copyButtonAction(_ sender: Any) {
  132. self.model.editAllowed = self.notCopyCheckBox.properties.checkboxType == .selected ? false:true
  133. self.reloadData()
  134. }
  135. }
  136. //MARK: - ComponentPasswordViewDelegate
  137. extension KMSecurityContentView: ComponentPasswordViewDelegate {
  138. func componentPasswordViewDidChanged(inputView: ComponentPasswordView) {
  139. // self.updatePasswordState()
  140. }
  141. func componentPasswordViewDidEndEditing(inputView: ComponentPasswordView) {
  142. // self.updatePasswordState()
  143. }
  144. }