// // KMSecurityView.swift // PDF Reader Pro // // Created by lizhe on 2023/11/13. // import Cocoa import KMComponentLibrary typealias KMSecurityViewBatchAction = (_ view: KMSecurityView, _ files: [KMFileAttribute]) -> Void typealias KMSecurityViewCancelAction = (_ view: KMSecurityView) -> Void typealias KMSecurityViewDoneAction = (_ view: KMSecurityView, _ model: KMSecureEncryptModel, _ files: [KMFileAttribute]) -> Void class KMSecurityView: BaseXibView { @IBOutlet weak var box1: NSBox! @IBOutlet weak var boxLabel1: NSTextField! @IBOutlet var openPwdCheckBox: ComponentCheckBox! @IBOutlet var openPasswordView: ComponentPasswordView! @IBOutlet var ownerPwdCheckBox: ComponentCheckBox! @IBOutlet var ownerPasswordView: ComponentPasswordView! @IBOutlet var notPrintCheckBox: ComponentCheckBox! @IBOutlet var notCopyCheckBox: ComponentCheckBox! @IBOutlet var batchButton: ComponentButton! @IBOutlet var cancelButton: ComponentButton! @IBOutlet var doneButton: ComponentButton! @IBOutlet var batchWidthConst: NSLayoutConstraint! @IBOutlet var cancelWidthConst: NSLayoutConstraint! @IBOutlet var doneWidthConst: NSLayoutConstraint! var batchAction: KMSecurityViewBatchAction? var cancelAction: KMSecurityViewCancelAction? var doneAction: KMSecurityViewDoneAction? var openPasswordString: String? var ownerPasswordString: String? var files: [KMFileAttribute] = [] private var model: KMSecureEncryptModel = KMSecureEncryptModel() var canEncrypt: Bool = false override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } override func setup() { updateLanguage() self.updateOwnerButtonState() } func updateLanguage() { box1.fillColor = NSColor.clear boxLabel1.stringValue = KMLocalizedString("Password Security Settings") boxLabel1.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1") boxLabel1.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium") openPwdCheckBox.properties = ComponentCheckBoxProperty(size: .s, state: .normal, showhelp: true, text: KMLocalizedString("Require a password to open the document.", comment: ""), checkboxType: .normal) openPwdCheckBox.setTarget(self, action: #selector(openPasswordButtonAction(_:))) openPasswordView.properties = ComponentPasswordProperty(size: .m, state: .normal, isError: false, placeholderString: "Please enter password", text: "", isDisabled: false, errorText: "Here is the error message description.") openPasswordView.delegate = self ownerPwdCheckBox.properties = ComponentCheckBoxProperty(size: .s, state: .normal, showhelp: true, text: KMLocalizedString("Require a password to open the document.", comment: ""), checkboxType: .normal) ownerPwdCheckBox.setTarget(self, action: #selector(ownerPaasswordButtonAction(_:))) ownerPasswordView.properties = ComponentPasswordProperty(size: .m, state: .normal, isError: false, placeholderString: "Please enter password", text: "", isDisabled: false, errorText: "Here is the error message description.") ownerPasswordView.delegate = self notPrintCheckBox.properties = ComponentCheckBoxProperty(size: .s, state: .normal, text: KMLocalizedString("Restrict document printing", comment: ""), checkboxType: .normal) notPrintCheckBox.setTarget(self, action: #selector(printButtonAction(_:))) notCopyCheckBox.properties = ComponentCheckBoxProperty(size: .s, state: .normal, text: KMLocalizedString("Restrict content copying", comment: ""), checkboxType: .normal) notCopyCheckBox.setTarget(self, action: #selector(copyButtonAction(_:))) batchButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .s, state: .normal, onlyIcon: false, buttonText: KMLocalizedString("Batch")) batchButton.setTarget(self, action: #selector(batchButtonAction(_:))) batchWidthConst.constant = batchButton.properties.propertyInfo.viewWidth cancelButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .s, state: .normal, onlyIcon: false, buttonText: KMLocalizedString("Cancel")) cancelButton.setTarget(self, action: #selector(cancelButtonAction(_:))) cancelWidthConst.constant = cancelButton.properties.propertyInfo.viewWidth doneButton.properties = ComponentButtonProperty(type: .primary, size: .s, state: .normal, onlyIcon: false, buttonText: KMLocalizedString("Encrypt")) doneButton.setTarget(self, action: #selector(doneButtonAction(_:))) doneWidthConst.constant = doneButton.properties.propertyInfo.viewWidth } func reloadData() { let openPWisdiable = model.openPasswordOn ? false : true if openPasswordView.properties.isDisabled != openPWisdiable { openPasswordView.properties.isDisabled = model.openPasswordOn ? false : true openPasswordView.reloadData() } let ownerPWisdiable = model.ownerPasswordOn ? false : true if ownerPasswordView.properties.isDisabled != ownerPWisdiable { ownerPasswordView.properties.isDisabled = model.ownerPasswordOn ? false : true ownerPasswordView.reloadData() } notPrintCheckBox.properties.isDisabled = model.printEnabled ? false : true notPrintCheckBox.properties.checkboxType = model.printAllowed ? .normal : .selected notPrintCheckBox.reloadData() notCopyCheckBox.properties.isDisabled = model.editEnabled ? false : true notCopyCheckBox.properties.checkboxType = model.editAllowed ? .normal : .selected notCopyCheckBox.reloadData() self.updateEncryptButtonEnabledState() } override func mouseDown(with event: NSEvent) { super.mouseDown(with: event) self.window?.makeFirstResponder(nil) } } extension KMSecurityView { func updateEncryptButtonEnabledState() { var enabled = false if model.openPasswordOn { if !model.openPassword.isEmpty { enabled = true } } if enabled { if model.ownerPasswordOn { if model.ownerPassword.count == 0 || (model.printAllowed && model.editAllowed) { enabled = false } } } else { if model.ownerPasswordOn { if !model.ownerPassword.isEmpty && (!model.printAllowed || !model.editAllowed) { enabled = true } } } self.canEncrypt = enabled if enabled { doneButton.properties.isDisabled = false } else { doneButton.properties.isDisabled = true } doneButton.reloadData() } func updatePasswordState() { self.model.openPassword = openPasswordView.properties.text self.model.ownerPassword = ownerPasswordView.properties.text self.reloadData() } func updateOwnerButtonState() { if self.model.ownerPasswordOn { self.model.printEnabled = true self.model.editEnabled = true self.model.printAllowed = false self.model.editAllowed = false } else { self.model.printEnabled = false self.model.editEnabled = false self.model.printAllowed = true self.model.editAllowed = true } self.reloadData() } } extension KMSecurityView { @objc func batchButtonAction(_ sender: Any) { if !IAPProductsManager.default().isAvailableAllFunction(){ KMPurchaseCompareWindowController.sharedInstance().showWindow(nil) return } guard let callBack = batchAction else { return } callBack(self, files) } @objc func cancelButtonAction(_ sender: Any) { guard let callBack = cancelAction else { return } callBack(self) } @objc func doneButtonAction(_ sender: Any) { self.updatePasswordState() if model.ownerPassword == model.openPassword { let alert = NSAlert() alert.alertStyle = .critical alert.messageText = NSLocalizedString("The Open and Owner passwords cannot be the same. Please change either the Open or the Owner Password.", comment: "") alert.runModal() return } guard let callBack = doneAction else { return } callBack(self, model, files) } @objc func openPasswordButtonAction(_ sender: Any) { self.model.openPasswordOn = self.openPwdCheckBox.properties.checkboxType == .selected ? true:false self.reloadData() } @objc func ownerPaasswordButtonAction(_ sender: Any) { self.model.ownerPasswordOn = self.ownerPwdCheckBox.properties.checkboxType == .selected ? true:false self.updateOwnerButtonState() } @objc func printButtonAction(_ sender: Any) { self.model.printAllowed = self.notPrintCheckBox.properties.checkboxType == .selected ? false:true self.reloadData() } @objc func copyButtonAction(_ sender: Any) { self.model.editAllowed = self.notCopyCheckBox.properties.checkboxType == .selected ? false:true self.reloadData() } } //MARK: - ComponentPasswordViewDelegate extension KMSecurityView: ComponentPasswordViewDelegate { func componentPasswordViewDidChanged(inputView: ComponentPasswordView) { self.updatePasswordState() } func componentPasswordViewDidEndEditing(inputView: ComponentPasswordView) { self.updatePasswordState() } }