KMSecurityWindowController.swift 7.3 KB

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