KMSecurityView.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 var batchButton: ComponentButton!
  15. @IBOutlet var cancelButton: ComponentButton!
  16. @IBOutlet var doneButton: ComponentButton!
  17. @IBOutlet var batchWidthConst: NSLayoutConstraint!
  18. @IBOutlet var cancelWidthConst: NSLayoutConstraint!
  19. @IBOutlet var doneWidthConst: NSLayoutConstraint!
  20. @IBOutlet weak var securityContentView: KMSecurityContentView!
  21. var batchAction: KMSecurityViewBatchAction?
  22. var cancelAction: KMSecurityViewCancelAction?
  23. var doneAction: KMSecurityViewDoneAction?
  24. var openPasswordString: String?
  25. var ownerPasswordString: String?
  26. var files: [KMFileAttribute] = []
  27. private var model: KMSecureEncryptModel = KMSecureEncryptModel()
  28. var canEncrypt: Bool = false
  29. override func draw(_ dirtyRect: NSRect) {
  30. super.draw(dirtyRect)
  31. // Drawing code here.
  32. }
  33. override func setup() {
  34. self.securityContentView.model = self.model
  35. updateLanguage()
  36. }
  37. func updateLanguage() {
  38. box1.fillColor = NSColor.clear
  39. batchButton.properties = ComponentButtonProperty(type: .default_tertiary,
  40. size: .s,
  41. state: .normal,
  42. onlyIcon: false,
  43. buttonText: KMLocalizedString("Batch"))
  44. batchButton.setTarget(self, action: #selector(batchButtonAction(_:)))
  45. batchWidthConst.constant = batchButton.properties.propertyInfo.viewWidth
  46. cancelButton.properties = ComponentButtonProperty(type: .default_tertiary,
  47. size: .s,
  48. state: .normal,
  49. onlyIcon: false,
  50. buttonText: KMLocalizedString("Cancel"))
  51. cancelButton.setTarget(self, action: #selector(cancelButtonAction(_:)))
  52. cancelWidthConst.constant = cancelButton.properties.propertyInfo.viewWidth
  53. doneButton.properties = ComponentButtonProperty(type: .primary,
  54. size: .s,
  55. state: .normal,
  56. onlyIcon: false,
  57. buttonText: KMLocalizedString("Encrypt"),
  58. keepPressState: false)
  59. doneButton.setTarget(self, action: #selector(doneButtonAction(_:)))
  60. doneWidthConst.constant = doneButton.properties.propertyInfo.viewWidth
  61. }
  62. func reloadData() {
  63. self.updateEncryptButtonEnabledState()
  64. }
  65. override func mouseDown(with event: NSEvent) {
  66. super.mouseDown(with: event)
  67. self.window?.makeFirstResponder(nil)
  68. }
  69. }
  70. extension KMSecurityView {
  71. func updateEncryptButtonEnabledState() {
  72. var enabled = false
  73. if model.openPasswordOn {
  74. if !model.openPassword.isEmpty {
  75. enabled = true
  76. }
  77. }
  78. if enabled {
  79. if model.ownerPasswordOn {
  80. if model.ownerPassword.count == 0 || (model.printAllowed && model.editAllowed) {
  81. enabled = false
  82. }
  83. }
  84. } else {
  85. if model.ownerPasswordOn {
  86. if !model.ownerPassword.isEmpty && (!model.printAllowed || !model.editAllowed) {
  87. enabled = true
  88. }
  89. }
  90. }
  91. self.canEncrypt = enabled
  92. if enabled {
  93. doneButton.properties.isDisabled = false
  94. } else {
  95. doneButton.properties.isDisabled = true
  96. }
  97. doneButton.reloadData()
  98. }
  99. }
  100. extension KMSecurityView {
  101. @objc func batchButtonAction(_ sender: Any) {
  102. if !IAPProductsManager.default().isAvailableAllFunction(){
  103. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  104. return
  105. }
  106. guard let callBack = batchAction else { return }
  107. callBack(self, files)
  108. }
  109. @objc func cancelButtonAction(_ sender: Any) {
  110. guard let callBack = cancelAction else { return }
  111. callBack(self)
  112. }
  113. @objc func doneButtonAction(_ sender: Any) {
  114. self.securityContentView.updatePasswordState()
  115. if model.ownerPassword == model.openPassword {
  116. let alert = NSAlert()
  117. alert.alertStyle = .critical
  118. alert.messageText = NSLocalizedString("The Open and Owner passwords cannot be the same. Please change either the Open or the Owner Password.", comment: "")
  119. alert.runModal()
  120. return
  121. }
  122. guard let callBack = doneAction else { return }
  123. callBack(self, model, files)
  124. }
  125. }