123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- //
- // 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: KMNBaseWindowController {
- @IBOutlet weak var securityView: KMSecurityView!
-
- var batchAction: KMBatchActionBlock?
- var doneAction: KMSecurityWindowControllerDoneAction?
-
- var cancelAction: KMCommonBlock?
- var pdfDocument: CPDFDocument?
-
- var documentURL: URL?
-
- override func windowDidLoad() {
- super.windowDidLoad()
-
- 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 = { [weak 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 }
- }
-
- 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)
- }
- }
- }
-
- attribute.updateValue(model.title, forKey: .titleAttribute)
- attribute.updateValue(model.author, forKey: .authorAttribute)
- attribute.updateValue(model.subject, forKey: .subjectAttribute)
- attribute.updateValue(model.keywords, forKey: .keywordsAttribute)
-
- guard let callback = doneAction else {
- return
- }
-
- callback(self, options, attribute)
- }
- }
- }
|