KMSecurityWindowController.swift 7.2 KB

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