KMSecurityWindowController.swift 8.3 KB

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