KMSecurityWindowController.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // KMSecurityWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/11/13.
  6. //
  7. import Cocoa
  8. typealias KMSecurityWindowControllerDoneAction = (_ controller: NSWindowController, _ option: [CPDFDocumentWriteOption : Any], _ attribute: [CPDFDocumentAttribute: Any]) -> Void
  9. class KMSecurityWindowController: KMBaseWindowController {
  10. @IBOutlet weak var securityView: KMSecurityView!
  11. var batchAction: KMBatchActionBlock?
  12. var doneAction: KMSecurityWindowControllerDoneAction?
  13. override func windowDidLoad() {
  14. super.windowDidLoad()
  15. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  16. securityView.cancelAction = { [unowned self] view in
  17. self.cancelAction?(self)
  18. }
  19. securityView.batchAction = { [unowned self] view, files in
  20. self.batchAction?(self, files)
  21. }
  22. securityView.doneAction = { [unowned self] view, model, files in
  23. self.setPassword(model: model, files: files)
  24. }
  25. }
  26. }
  27. extension KMSecurityWindowController {
  28. func setPassword(model: KMSecureEncryptModel, files: [KMFileAttribute]) {
  29. if pdfDocument == nil {
  30. guard let filePath = files.first?.filePath else { return }
  31. }
  32. // let myDocument = CPDFDocument(url: NSURL(fileURLWithPath: filePath) as URL)
  33. if (pdfDocument != nil) {
  34. var options: [CPDFDocumentWriteOption : Any] = [:]
  35. var attribute: [CPDFDocumentAttribute : Any] = [:]
  36. if model.openPasswordOn && model.ownerPasswordOn { /// 开启密码 & 权限密码
  37. if (!model.openPassword.isEmpty) {
  38. options.updateValue(model.openPassword, forKey: .userPasswordOption)
  39. }
  40. if (!model.ownerPassword.isEmpty) {
  41. options.updateValue(model.ownerPassword, forKey: .ownerPasswordOption)
  42. }
  43. /// 允许打印
  44. if model.printEnabled {
  45. if model.printAllowed == false {
  46. options.updateValue(false, forKey: .allowsPrintingOption)
  47. } else {
  48. options.updateValue(true, forKey: .allowsPrintingOption)
  49. if model.printSelectedIndex == 2 {
  50. options.updateValue(true, forKey: .allowsHighQualityPrintingOption)
  51. }
  52. }
  53. }
  54. /// 允许更改
  55. if model.editEnabled {
  56. if model.editSelectedIndex == 1 {
  57. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  58. options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
  59. } else if model.editSelectedIndex == 2 {
  60. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  61. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  62. } else if model.editSelectedIndex == 3 {
  63. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  64. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  65. options.updateValue(true, forKey: .allowsCommentingOption)
  66. } else if model.editAllowed == true {
  67. options.updateValue(true, forKey: .allowsCopyingOption)
  68. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  69. options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
  70. options.updateValue(true, forKey: .allowsCommentingOption)
  71. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  72. } else {
  73. options.updateValue(false, forKey: .allowsCopyingOption)
  74. }
  75. }
  76. /// 加密层级 sdk 缺接口
  77. } else if model.openPasswordOn { /// 开启密码
  78. if (!model.openPassword.isEmpty) {
  79. options.updateValue(model.openPassword, forKey: .userPasswordOption)
  80. }
  81. /// 加密层级 sdk 缺接口
  82. } else if model.ownerPasswordOn { /// 权限密码
  83. if (!model.ownerPassword.isEmpty) {
  84. options.updateValue(model.ownerPassword, forKey: .ownerPasswordOption)
  85. }
  86. /// 允许打印
  87. if model.printEnabled {
  88. if model.printAllowed == false {
  89. options.updateValue(false, forKey: .allowsPrintingOption)
  90. } else {
  91. options.updateValue(true, forKey: .allowsPrintingOption)
  92. if model.printSelectedIndex == 2 {
  93. options.updateValue(true, forKey: .allowsHighQualityPrintingOption)
  94. }
  95. }
  96. }
  97. /// 允许更改
  98. if model.editEnabled {
  99. if model.editSelectedIndex == 1 {
  100. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  101. options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
  102. } else if model.editSelectedIndex == 2 {
  103. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  104. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  105. } else if model.editSelectedIndex == 3 {
  106. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  107. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  108. options.updateValue(true, forKey: .allowsCommentingOption)
  109. } else if model.editAllowed == true {
  110. options.updateValue(true, forKey: .allowsCopyingOption)
  111. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  112. options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
  113. options.updateValue(true, forKey: .allowsCommentingOption)
  114. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  115. } else {
  116. options.updateValue(false, forKey: .allowsCopyingOption)
  117. }
  118. }
  119. /// 加密层级 sdk 缺接口
  120. }
  121. attribute.updateValue(model.title, forKey: .titleAttribute)
  122. attribute.updateValue(model.author, forKey: .authorAttribute)
  123. attribute.updateValue(model.subject, forKey: .subjectAttribute)
  124. attribute.updateValue(model.keywords, forKey: .keywordsAttribute)
  125. // let result = myDocument.write(to: documentURL, withOptions: options)
  126. // myDocument.setPasswordOptions(options)
  127. guard let callback = doneAction else {
  128. return
  129. }
  130. callback(self, options, attribute)
  131. }
  132. }
  133. }