KMSecurityView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // KMSecurityView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/11/13.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. typealias KMSecurityViewBatchAction = (_ view: KMSecurityView, _ files: [KMFileAttribute]) -> Void
  10. typealias KMSecurityViewCancelAction = (_ view: KMSecurityView) -> Void
  11. typealias KMSecurityViewDoneAction = (_ view: KMSecurityView, _ model: KMSecureEncryptModel, _ files: [KMFileAttribute]) -> Void
  12. class KMSecurityView: BaseXibView {
  13. @IBOutlet weak var box1: NSBox!
  14. @IBOutlet weak var boxLabel1: NSTextField!
  15. @IBOutlet var openPwdCheckBox: ComponentCheckBox!
  16. @IBOutlet var openPasswordView: ComponentPasswordView!
  17. @IBOutlet var ownerPwdCheckBox: ComponentCheckBox!
  18. @IBOutlet var ownerPasswordView: ComponentPasswordView!
  19. @IBOutlet var notPrintCheckBox: ComponentCheckBox!
  20. @IBOutlet var notCopyCheckBox: ComponentCheckBox!
  21. @IBOutlet var batchButton: ComponentButton!
  22. @IBOutlet var cancelButton: ComponentButton!
  23. @IBOutlet var doneButton: ComponentButton!
  24. @IBOutlet var batchWidthConst: NSLayoutConstraint!
  25. @IBOutlet var cancelWidthConst: NSLayoutConstraint!
  26. @IBOutlet var doneWidthConst: NSLayoutConstraint!
  27. var batchAction: KMSecurityViewBatchAction?
  28. var cancelAction: KMSecurityViewCancelAction?
  29. var doneAction: KMSecurityViewDoneAction?
  30. var openPasswordString: String?
  31. var ownerPasswordString: String?
  32. var files: [KMFileAttribute] = []
  33. private var model: KMSecureEncryptModel = KMSecureEncryptModel()
  34. var canEncrypt: Bool = false
  35. override func draw(_ dirtyRect: NSRect) {
  36. super.draw(dirtyRect)
  37. // Drawing code here.
  38. }
  39. override func setup() {
  40. updateLanguage()
  41. self.updateOwnerButtonState()
  42. }
  43. func updateLanguage() {
  44. box1.fillColor = NSColor.clear
  45. boxLabel1.stringValue = KMLocalizedString("Password Security Settings")
  46. boxLabel1.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  47. boxLabel1.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  48. openPwdCheckBox.properties = ComponentCheckBoxProperty(size: .s, state: .normal, showhelp: true, text: KMLocalizedString("Require a password to open the document.", comment: ""), checkboxType: .normal)
  49. openPwdCheckBox.setTarget(self, action: #selector(openPasswordButtonAction(_:)))
  50. openPasswordView.properties = ComponentPasswordProperty(size: .m,
  51. state: .normal,
  52. isError: false,
  53. placeholderString: "Please enter password",
  54. text: "",
  55. isDisabled: false,
  56. errorText: "Here is the error message description.")
  57. openPasswordView.delegate = self
  58. ownerPwdCheckBox.properties = ComponentCheckBoxProperty(size: .s, state: .normal, showhelp: true, text: KMLocalizedString("Require a password to open the document.", comment: ""), checkboxType: .normal)
  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. notPrintCheckBox.properties = ComponentCheckBoxProperty(size: .s, state: .normal, text: KMLocalizedString("Restrict document printing", comment: ""), checkboxType: .normal)
  69. notPrintCheckBox.setTarget(self, action: #selector(printButtonAction(_:)))
  70. notCopyCheckBox.properties = ComponentCheckBoxProperty(size: .s, state: .normal, text: KMLocalizedString("Restrict content copying", comment: ""), checkboxType: .normal)
  71. notCopyCheckBox.setTarget(self, action: #selector(copyButtonAction(_:)))
  72. batchButton.properties = ComponentButtonProperty(type: .default_tertiary,
  73. size: .s,
  74. state: .normal,
  75. onlyIcon: false,
  76. buttonText: KMLocalizedString("Batch"))
  77. batchButton.setTarget(self, action: #selector(batchButtonAction(_:)))
  78. batchWidthConst.constant = batchButton.properties.propertyInfo.viewWidth
  79. cancelButton.properties = ComponentButtonProperty(type: .default_tertiary,
  80. size: .s,
  81. state: .normal,
  82. onlyIcon: false,
  83. buttonText: KMLocalizedString("Cancel"))
  84. cancelButton.setTarget(self, action: #selector(cancelButtonAction(_:)))
  85. cancelWidthConst.constant = cancelButton.properties.propertyInfo.viewWidth
  86. doneButton.properties = ComponentButtonProperty(type: .primary,
  87. size: .s,
  88. state: .normal,
  89. onlyIcon: false,
  90. buttonText: KMLocalizedString("Encrypt"))
  91. doneButton.setTarget(self, action: #selector(doneButtonAction(_:)))
  92. doneWidthConst.constant = doneButton.properties.propertyInfo.viewWidth
  93. }
  94. func reloadData() {
  95. let openPWisdiable = model.openPasswordOn ? false : true
  96. if openPasswordView.properties.isDisabled != openPWisdiable {
  97. openPasswordView.properties.isDisabled = model.openPasswordOn ? false : true
  98. openPasswordView.reloadData()
  99. }
  100. let ownerPWisdiable = model.ownerPasswordOn ? false : true
  101. if ownerPasswordView.properties.isDisabled != ownerPWisdiable {
  102. ownerPasswordView.properties.isDisabled = model.ownerPasswordOn ? false : true
  103. ownerPasswordView.reloadData()
  104. }
  105. notPrintCheckBox.properties.isDisabled = model.printEnabled ? false : true
  106. notPrintCheckBox.properties.checkboxType = model.printAllowed ? .normal : .selected
  107. notPrintCheckBox.reloadData()
  108. notCopyCheckBox.properties.isDisabled = model.editEnabled ? false : true
  109. notCopyCheckBox.properties.checkboxType = model.editAllowed ? .normal : .selected
  110. notCopyCheckBox.reloadData()
  111. self.updateEncryptButtonEnabledState()
  112. }
  113. override func mouseDown(with event: NSEvent) {
  114. super.mouseDown(with: event)
  115. self.window?.makeFirstResponder(nil)
  116. }
  117. }
  118. extension KMSecurityView {
  119. func updateEncryptButtonEnabledState() {
  120. var enabled = false
  121. if model.openPasswordOn {
  122. if !model.openPassword.isEmpty {
  123. enabled = true
  124. }
  125. }
  126. if enabled {
  127. if model.ownerPasswordOn {
  128. if model.ownerPassword.count == 0 || (model.printAllowed && model.editAllowed) {
  129. enabled = false
  130. }
  131. }
  132. } else {
  133. if model.ownerPasswordOn {
  134. if !model.ownerPassword.isEmpty && (!model.printAllowed || !model.editAllowed) {
  135. enabled = true
  136. }
  137. }
  138. }
  139. self.canEncrypt = enabled
  140. if enabled {
  141. doneButton.properties.isDisabled = false
  142. } else {
  143. doneButton.properties.isDisabled = true
  144. }
  145. doneButton.reloadData()
  146. }
  147. func updatePasswordState() {
  148. self.model.openPassword = openPasswordView.properties.text
  149. self.model.ownerPassword = ownerPasswordView.properties.text
  150. self.reloadData()
  151. }
  152. func updateOwnerButtonState() {
  153. if self.model.ownerPasswordOn {
  154. self.model.printEnabled = true
  155. self.model.editEnabled = true
  156. self.model.printAllowed = false
  157. self.model.editAllowed = false
  158. } else {
  159. self.model.printEnabled = false
  160. self.model.editEnabled = false
  161. self.model.printAllowed = true
  162. self.model.editAllowed = true
  163. }
  164. self.reloadData()
  165. }
  166. }
  167. extension KMSecurityView {
  168. @objc func batchButtonAction(_ sender: Any) {
  169. if !IAPProductsManager.default().isAvailableAllFunction(){
  170. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  171. return
  172. }
  173. guard let callBack = batchAction else { return }
  174. callBack(self, files)
  175. }
  176. @objc func cancelButtonAction(_ sender: Any) {
  177. guard let callBack = cancelAction else { return }
  178. callBack(self)
  179. }
  180. @objc func doneButtonAction(_ sender: Any) {
  181. self.updatePasswordState()
  182. if model.ownerPassword == model.openPassword {
  183. let alert = NSAlert()
  184. alert.alertStyle = .critical
  185. alert.messageText = NSLocalizedString("The Open and Owner passwords cannot be the same. Please change either the Open or the Owner Password.", comment: "")
  186. alert.runModal()
  187. return
  188. }
  189. guard let callBack = doneAction else { return }
  190. callBack(self, model, files)
  191. }
  192. @objc func openPasswordButtonAction(_ sender: Any) {
  193. self.model.openPasswordOn = self.openPwdCheckBox.properties.checkboxType == .selected ? true:false
  194. self.reloadData()
  195. }
  196. @objc func ownerPaasswordButtonAction(_ sender: Any) {
  197. self.model.ownerPasswordOn = self.ownerPwdCheckBox.properties.checkboxType == .selected ? true:false
  198. self.updateOwnerButtonState()
  199. }
  200. @objc func printButtonAction(_ sender: Any) {
  201. self.model.printAllowed = self.notPrintCheckBox.properties.checkboxType == .selected ? false:true
  202. self.reloadData()
  203. }
  204. @objc func copyButtonAction(_ sender: Any) {
  205. self.model.editAllowed = self.notCopyCheckBox.properties.checkboxType == .selected ? false:true
  206. self.reloadData()
  207. }
  208. }
  209. //MARK: - ComponentPasswordViewDelegate
  210. extension KMSecurityView: ComponentPasswordViewDelegate {
  211. func componentPasswordViewDidChanged(inputView: ComponentPasswordView) {
  212. self.updatePasswordState()
  213. }
  214. func componentPasswordViewDidEndEditing(inputView: ComponentPasswordView) {
  215. self.updatePasswordState()
  216. }
  217. }