KMSecurityContentView.swift 8.4 KB

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