123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- //
- // KMSecureEncryptWindowController.swift
- // PDF Reader Pro
- //
- // 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
- }
- }
|