123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- //
- // KMSecurityWindowController.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2023/11/13.
- //
- import Cocoa
- typealias KMSecurityWindowControllerDoneAction = (_ controller: NSWindowController, _ option: [CPDFDocumentWriteOption : Any], _ attribute: [CPDFDocumentAttribute: Any]) -> Void
- class KMSecurityWindowController: KMBaseWindowController {
- @IBOutlet weak var securityView: KMSecurityView!
-
- var batchAction: KMBatchActionBlock?
- var doneAction: KMSecurityWindowControllerDoneAction?
-
-
- var documentURL: URL? //{
- // didSet{
- // if self.password.count != 0 {
- // self.securityView.documentURL = self.documentURL
- // } else {
- // KMBaseWindowController.checkPassword(url: documentURL, type: .owner) { [unowned self] success, paasswordString in
- // if success {
- // self.securityView.documentURL = self.documentURL
- // } else {
- // guard let callBack = self.itemClick else { return }
- //
- // callBack()
- // }
- // }
- // }
- // }
- // }
-
- override func windowDidLoad() {
- super.windowDidLoad()
- // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
- securityView.cancelAction = { [unowned self] view in
- self.cancelAction?(self)
- }
-
- securityView.batchAction = { [unowned self] view, files in
- if files.count == 0 {
- let file: KMFileAttribute = KMFileAttribute()
- file.filePath = self.documentURL?.path ?? self.pdfDocument?.documentURL.path ?? ""
- self.batchAction?(self, [file])
- } else {
- self.batchAction?(self, files)
- }
- }
-
- securityView.doneAction = { [unowned self] view, model, files in
- self.setPassword(model: model, files: files)
- }
- }
- }
- extension KMSecurityWindowController {
- func setPassword(model: KMSecureEncryptModel, files: [KMFileAttribute]) {
- if pdfDocument == nil && documentURL == nil{
- guard let filePath = files.first?.filePath else { return }
- }
-
- // let myDocument = CPDFDocument(url: NSURL(fileURLWithPath: filePath) as URL)
- if (pdfDocument != nil || documentURL != nil) {
- var options: [CPDFDocumentWriteOption : Any] = [:]
- var attribute: [CPDFDocumentAttribute : 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 缺接口
- }
-
- attribute.updateValue(model.title, forKey: .titleAttribute)
- attribute.updateValue(model.author, forKey: .authorAttribute)
- attribute.updateValue(model.subject, forKey: .subjectAttribute)
- attribute.updateValue(model.keywords, forKey: .keywordsAttribute)
-
- // let result = myDocument.write(to: documentURL, withOptions: options)
- // myDocument.setPasswordOptions(options)
- guard let callback = doneAction else {
- return
- }
-
- callback(self, options, attribute)
- }
- }
- }
|