KMSecurityWindowController.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. if files.count == 0 {
  21. let file: KMFileAttribute = KMFileAttribute()
  22. file.filePath = self.pdfDocument?.documentURL.path ?? ""
  23. self.batchAction?(self, [file])
  24. } else {
  25. self.batchAction?(self, files)
  26. }
  27. }
  28. securityView.doneAction = { [unowned self] view, model, files in
  29. self.setPassword(model: model, files: files)
  30. }
  31. }
  32. }
  33. extension KMSecurityWindowController {
  34. func setPassword(model: KMSecureEncryptModel, files: [KMFileAttribute]) {
  35. if pdfDocument == nil {
  36. guard let filePath = files.first?.filePath else { return }
  37. }
  38. // let myDocument = CPDFDocument(url: NSURL(fileURLWithPath: filePath) as URL)
  39. if (pdfDocument != nil) {
  40. var options: [CPDFDocumentWriteOption : Any] = [:]
  41. var attribute: [CPDFDocumentAttribute : Any] = [:]
  42. if model.openPasswordOn && model.ownerPasswordOn { /// 开启密码 & 权限密码
  43. if (!model.openPassword.isEmpty) {
  44. options.updateValue(model.openPassword, forKey: .userPasswordOption)
  45. }
  46. if (!model.ownerPassword.isEmpty) {
  47. options.updateValue(model.ownerPassword, forKey: .ownerPasswordOption)
  48. }
  49. /// 允许打印
  50. if model.printEnabled {
  51. if model.printAllowed == false {
  52. options.updateValue(false, forKey: .allowsPrintingOption)
  53. } else {
  54. options.updateValue(true, forKey: .allowsPrintingOption)
  55. if model.printSelectedIndex == 2 {
  56. options.updateValue(true, forKey: .allowsHighQualityPrintingOption)
  57. }
  58. }
  59. }
  60. /// 允许更改
  61. if model.editEnabled {
  62. if model.editSelectedIndex == 1 {
  63. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  64. options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
  65. } else if model.editSelectedIndex == 2 {
  66. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  67. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  68. } else if model.editSelectedIndex == 3 {
  69. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  70. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  71. options.updateValue(true, forKey: .allowsCommentingOption)
  72. } else if model.editAllowed == true {
  73. options.updateValue(true, forKey: .allowsCopyingOption)
  74. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  75. options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
  76. options.updateValue(true, forKey: .allowsCommentingOption)
  77. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  78. } else {
  79. options.updateValue(false, forKey: .allowsCopyingOption)
  80. }
  81. }
  82. /// 加密层级 sdk 缺接口
  83. } else if model.openPasswordOn { /// 开启密码
  84. if (!model.openPassword.isEmpty) {
  85. options.updateValue(model.openPassword, forKey: .userPasswordOption)
  86. }
  87. /// 加密层级 sdk 缺接口
  88. } else if model.ownerPasswordOn { /// 权限密码
  89. if (!model.ownerPassword.isEmpty) {
  90. options.updateValue(model.ownerPassword, forKey: .ownerPasswordOption)
  91. }
  92. /// 允许打印
  93. if model.printEnabled {
  94. if model.printAllowed == false {
  95. options.updateValue(false, forKey: .allowsPrintingOption)
  96. } else {
  97. options.updateValue(true, forKey: .allowsPrintingOption)
  98. if model.printSelectedIndex == 2 {
  99. options.updateValue(true, forKey: .allowsHighQualityPrintingOption)
  100. }
  101. }
  102. }
  103. /// 允许更改
  104. if model.editEnabled {
  105. if model.editSelectedIndex == 1 {
  106. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  107. options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
  108. } else if model.editSelectedIndex == 2 {
  109. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  110. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  111. } else if model.editSelectedIndex == 3 {
  112. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  113. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  114. options.updateValue(true, forKey: .allowsCommentingOption)
  115. } else if model.editAllowed == true {
  116. options.updateValue(true, forKey: .allowsCopyingOption)
  117. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  118. options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
  119. options.updateValue(true, forKey: .allowsCommentingOption)
  120. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  121. } else {
  122. options.updateValue(false, forKey: .allowsCopyingOption)
  123. }
  124. }
  125. /// 加密层级 sdk 缺接口
  126. }
  127. attribute.updateValue(model.title, forKey: .titleAttribute)
  128. attribute.updateValue(model.author, forKey: .authorAttribute)
  129. attribute.updateValue(model.subject, forKey: .subjectAttribute)
  130. attribute.updateValue(model.keywords, forKey: .keywordsAttribute)
  131. // let result = myDocument.write(to: documentURL, withOptions: options)
  132. // myDocument.setPasswordOptions(options)
  133. guard let callback = doneAction else {
  134. return
  135. }
  136. callback(self, options, attribute)
  137. }
  138. }
  139. }