// // KMSecureEncryptWindowController.swift // PDF Master // // Created by tangchao on 2022/11/28. // import Cocoa typealias KMSecureEncryptWindowControllerItemClick = (Int) -> () typealias KMSecureEncryptWindowControllerResultCallback = (Bool) -> () class KMSecureEncryptWindowController: NSWindowController { @IBOutlet weak var titleLabel: NSTextField! @IBOutlet weak var topLine: NSBox! @IBOutlet weak var tableView: NSTableView! @IBOutlet weak var bottomLine: NSBox! @IBOutlet weak var batchEncryptButton: NSButton! @IBOutlet weak var cancelBox: NSBox! @IBOutlet weak var encryptBox: NSBox! var documentURL: URL! var myDocument: CPDFDocument! var cancelVC: KMDesignButton! var encryptVC: KMDesignButton! private var model: KMSecureEncryptModel = KMSecureEncryptModel() var itemClick: KMSecureEncryptWindowControllerItemClick! var resultCallback: KMSecureEncryptWindowControllerResultCallback! private var myOptions: [CPDFDocumentWriteOption : Any]? var options: [CPDFDocumentWriteOption : Any]? { get { return myOptions } } var canEncrypt: Bool = true deinit { KMPrint("KMSecureEncryptWindowController 已释放了") } override func windowDidLoad() { super.windowDidLoad() self.window?.appearance = NSAppearance(named: .aqua) cancelVC = KMDesignButton.init(withType: .Text) encryptVC = KMDesignButton.init(withType: .Text) cancelBox.fillColor = .clear cancelBox.contentView = cancelVC.view encryptBox.fillColor = .clear encryptBox.contentView = encryptVC.view titleLabel.stringValue = NSLocalizedString("Set Passwords", comment: "") self.topLine.isHidden = true tableView.delegate = self tableView.dataSource = self tableView.selectionHighlightStyle = .none self.bottomLine.isHidden = true batchEncryptButton.title = NSLocalizedString("Batch", comment: "") batchEncryptButton.isBordered = false batchEncryptButton.wantsLayer = true batchEncryptButton.layer?.borderWidth = 1 batchEncryptButton.layer?.cornerRadius = 4 batchEncryptButton.target = self batchEncryptButton.action = #selector(batchEncryptButtonAction) batchEncryptButton.isHidden = true cancelVC.target = self cancelVC.action = #selector(cancelButtonAction) cancelVC.stringValue = NSLocalizedString("Cancel", comment: "") cancelVC.button(type: .Sec, size: .m) self.cancelVC.button.keyEquivalent = KMKeyEquivalent.esc.string() encryptVC.target = self encryptVC.action = #selector(encryptButtonAction) encryptVC.stringValue = NSLocalizedString("Encrypt", comment: "") encryptVC.button(type: .Cta, size: .m) self.encryptVC.button.keyEquivalent = KMKeyEquivalent.enter updateEncryptButtonEnabledState() self.initUIProperty() } private func initUIProperty() { self.titleLabel.textColor = NSColor.titleColor() self.batchEncryptButton.setTitleColor(color: NSColor.buttonTitleColor()) self.batchEncryptButton.layer?.borderColor = NSColor.buttonBorderColor().cgColor } @objc func batchEncryptButtonAction() { guard let callback = itemClick else { return } callback(1) } @objc func cancelButtonAction() { guard let callback = itemClick else { return } callback(2) } @objc func encryptButtonAction() { if (!self.canEncrypt) { return } if (myDocument != nil) { var options: [CPDFDocumentWriteOption : Any] = [:] if model.openPasswordOn && model.ownerPasswordOn { /// 开启密码 & 权限密码 if (!model.openPassword.isEmpty) { options.updateValue(model.openPassword, forKey: .userPasswordOption) } if (!model.ownerPassword.isEmpty) { options.updateValue(model.ownerPassword, forKey: .ownerPasswordOption) } /// 允许打印 if model.printEnabled { if model.printAllowed == false { options.updateValue(false, forKey: .allowsPrintingOption) } else { options.updateValue(true, forKey: .allowsPrintingOption) if model.printSelectedIndex == 2 { options.updateValue(true, forKey: .allowsHighQualityPrintingOption) } } } /// 允许更改 if model.editEnabled { if model.editSelectedIndex == 1 { options.updateValue(true, forKey: .allowsDocumentChangesOption) options.updateValue(true, forKey: .allowsDocumentAssemblyOption) } else if model.editSelectedIndex == 2 { options.updateValue(true, forKey: .allowsDocumentChangesOption) options.updateValue(true, forKey: .allowsFormFieldEntryOption) } else if model.editSelectedIndex == 3 { options.updateValue(true, forKey: .allowsDocumentChangesOption) options.updateValue(true, forKey: .allowsFormFieldEntryOption) options.updateValue(true, forKey: .allowsCommentingOption) } else if model.editAllowed == true { options.updateValue(true, forKey: .allowsCopyingOption) options.updateValue(true, forKey: .allowsDocumentChangesOption) options.updateValue(true, forKey: .allowsDocumentAssemblyOption) options.updateValue(true, forKey: .allowsCommentingOption) options.updateValue(true, forKey: .allowsFormFieldEntryOption) } else { options.updateValue(false, forKey: .allowsCopyingOption) } } /// 加密层级 sdk 缺接口 } else if model.openPasswordOn { /// 开启密码 if (!model.openPassword.isEmpty) { options.updateValue(model.openPassword, forKey: .userPasswordOption) } /// 加密层级 sdk 缺接口 } else if model.ownerPasswordOn { /// 权限密码 if (!model.ownerPassword.isEmpty) { options.updateValue(model.ownerPassword, forKey: .ownerPasswordOption) } /// 允许打印 if model.printEnabled { if model.printAllowed == false { options.updateValue(false, forKey: .allowsPrintingOption) } else { options.updateValue(true, forKey: .allowsPrintingOption) if model.printSelectedIndex == 2 { options.updateValue(true, forKey: .allowsHighQualityPrintingOption) } } } /// 允许更改 if model.editEnabled { if model.editSelectedIndex == 1 { options.updateValue(true, forKey: .allowsDocumentChangesOption) options.updateValue(true, forKey: .allowsDocumentAssemblyOption) } else if model.editSelectedIndex == 2 { options.updateValue(true, forKey: .allowsDocumentChangesOption) options.updateValue(true, forKey: .allowsFormFieldEntryOption) } else if model.editSelectedIndex == 3 { options.updateValue(true, forKey: .allowsDocumentChangesOption) options.updateValue(true, forKey: .allowsFormFieldEntryOption) options.updateValue(true, forKey: .allowsCommentingOption) } else if model.editAllowed == true { options.updateValue(true, forKey: .allowsCopyingOption) options.updateValue(true, forKey: .allowsDocumentChangesOption) options.updateValue(true, forKey: .allowsDocumentAssemblyOption) options.updateValue(true, forKey: .allowsCommentingOption) options.updateValue(true, forKey: .allowsFormFieldEntryOption) } else { options.updateValue(false, forKey: .allowsCopyingOption) } } /// 加密层级 sdk 缺接口 } // let result = myDocument.write(to: documentURL, withOptions: options) // myDocument.setPasswordOptions(options) self.myOptions = options guard let callback = resultCallback else { return } callback(true) } } func updateEncryptButtonEnabledState() { var enabled = false if model.openPasswordOn { if !model.openPassword.isEmpty { enabled = true } } if !enabled { if model.ownerPasswordOn { if !model.ownerPassword.isEmpty && (!model.printAllowed || !model.editAllowed) { enabled = true } } } else { if model.ownerPasswordOn { if !model.ownerPassword.isEmpty && (!model.printAllowed || !model.editAllowed) { } else { enabled = false } } } self.canEncrypt = enabled if enabled { encryptVC.state = .Norm } else { encryptVC.state = .Disabled } } } extension KMSecureEncryptWindowController: NSTableViewDataSource { func numberOfRows(in tableView: NSTableView) -> Int { return 5 } func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat { if (row == 0) { return 0.01 } else if (row == 1) { // 6 + 22 + 10 + 32 return 70 } else if (row == 2) { // 6 + 22 + 10 + 32 return 70 } // 22 + 8 return 30 } } extension KMSecureEncryptWindowController: NSTableViewDelegate { func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { if row == 1 { var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CellID"), owner: self) if cellView == nil { cellView = KMSecureEncryptPasswordCellView() } let myCellView: KMSecureEncryptPasswordCellView = cellView! as! KMSecureEncryptPasswordCellView myCellView.checkBox.title = NSLocalizedString("Document Open Password", comment: "") myCellView.checkBox.setTitleColor(color: NSColor.buttonTitleColor()) myCellView.setPlaceholderString(NSLocalizedString("Open Password", comment: "")) if model.openPasswordOn { myCellView.checkBox.state = .on } else { myCellView.checkBox.state = .off } myCellView.itemClick = { [unowned self] itemView, _ in self.model.openPasswordOn = itemView!.kmEnabled self.updateEncryptButtonEnabledState() } myCellView.valueChange = { [unowned self] string,_ in if let value = (string as? String) { self.model.openPassword = value } self.updateEncryptButtonEnabledState() } return cellView } else if row == 2 { var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CellID"), owner: self) if cellView == nil { cellView = KMSecureEncryptPasswordCellView() } let myCellView: KMSecureEncryptPasswordCellView = cellView! as! KMSecureEncryptPasswordCellView myCellView.checkBox.title = NSLocalizedString("Document Permission Password", comment: "") myCellView.checkBox.setTitleColor(color: NSColor.buttonTitleColor()) myCellView.setPlaceholderString(NSLocalizedString("Permission Password", comment: "")) if model.ownerPasswordOn { myCellView.checkBox.state = .on } else { myCellView.checkBox.state = .off } myCellView.itemClick = { [unowned self] itemView, _ in if (itemView!.kmEnabled) { self.model.ownerPasswordOn = true self.model.printEnabled = true self.model.editEnabled = true self.model.printAllowed = false self.model.editAllowed = false } else { self.model.ownerPasswordOn = false self.model.printEnabled = false self.model.editEnabled = false self.model.printAllowed = true self.model.editAllowed = true } self.updateEncryptButtonEnabledState() self.tableView.reloadData(forRowIndexes: IndexSet.init(arrayLiteral: 3, 4), columnIndexes: IndexSet.init(integer: 0)) } myCellView.valueChange = { [unowned self] string, _ in if let value = (string as? String) { self.model.ownerPassword = value } self.updateEncryptButtonEnabledState() } return cellView } else if row == 3 { var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CellID"), owner: self) if cellView == nil { cellView = KMSecureEncryptCheckCellView() } let myCellView: KMSecureEncryptCheckCellView = cellView! as! KMSecureEncryptCheckCellView myCellView.check?.title = NSLocalizedString("Restrict document printing", comment: "") myCellView.kmEnabled = model.printEnabled if (self.model.printAllowed) { myCellView.check?.state = .off } else { myCellView.check?.state = .on } myCellView.itemClick = { [unowned self] result in self.model.printAllowed = !result self.updateEncryptButtonEnabledState() } return cellView } else if row == 4 { var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CellID"), owner: self) if cellView == nil { cellView = KMSecureEncryptCheckCellView() } let myCellView: KMSecureEncryptCheckCellView = cellView! as! KMSecureEncryptCheckCellView myCellView.check?.title = NSLocalizedString("Restrict content copying", comment: "") myCellView.kmEnabled = model.editEnabled if (self.model.editAllowed) { myCellView.check?.state = .off } else { myCellView.check?.state = .on } myCellView.itemClick = { [unowned self] result in self.model.editAllowed = !result self.updateEncryptButtonEnabledState() } return cellView } else if row == 5 { var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CellID"), owner: self) if cellView == nil { cellView = KMSecureEncryptComboBoxCellView() } let myCellView: KMSecureEncryptComboBoxCellView = cellView! as! KMSecureEncryptComboBoxCellView myCellView.titleLabel.stringValue = NSLocalizedString("加密级别", comment: "") myCellView.comboBox.removeAllItems() myCellView.comboBox.addItems(withObjectValues: model.encryptLevetArray) myCellView.comboBox.selectItem(at: model.editSelectedIndex) myCellView.itemClick = { [unowned self] _, index in self.model.encryptLevelSelectedIndex = index } return cellView } var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CellID"), owner: self) if cellView == nil { cellView = NSView.init() } return cellView } }