KMSecureEncryptWindowController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. //
  2. // KMSecureEncryptWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/11/28.
  6. //
  7. import Cocoa
  8. typealias KMSecureEncryptWindowControllerItemClick = (Int) -> ()
  9. typealias KMSecureEncryptWindowControllerResultCallback = (Bool) -> ()
  10. class KMSecureEncryptWindowController: NSWindowController {
  11. @IBOutlet weak var titleLabel: NSTextField!
  12. @IBOutlet weak var topLine: NSBox!
  13. @IBOutlet weak var tableView: NSTableView!
  14. @IBOutlet weak var bottomLine: NSBox!
  15. @IBOutlet weak var batchEncryptButton: NSButton!
  16. @IBOutlet weak var cancelBox: NSBox!
  17. @IBOutlet weak var encryptBox: NSBox!
  18. var documentURL: URL!
  19. var myDocument: CPDFDocument!
  20. var cancelVC: KMDesignButton!
  21. var encryptVC: KMDesignButton!
  22. private var model: KMSecureEncryptModel = KMSecureEncryptModel()
  23. var itemClick: KMSecureEncryptWindowControllerItemClick!
  24. var resultCallback: KMSecureEncryptWindowControllerResultCallback!
  25. private var myOptions: [CPDFDocumentWriteOption : Any]?
  26. var options: [CPDFDocumentWriteOption : Any]? {
  27. get {
  28. return myOptions
  29. }
  30. }
  31. var canEncrypt: Bool = true
  32. deinit {
  33. KMPrint("KMSecureEncryptWindowController 已释放了")
  34. }
  35. override func windowDidLoad() {
  36. super.windowDidLoad()
  37. self.window?.appearance = NSAppearance(named: .aqua)
  38. cancelVC = KMDesignButton.init(withType: .Text)
  39. encryptVC = KMDesignButton.init(withType: .Text)
  40. cancelBox.fillColor = .clear
  41. cancelBox.contentView = cancelVC.view
  42. encryptBox.fillColor = .clear
  43. encryptBox.contentView = encryptVC.view
  44. titleLabel.stringValue = NSLocalizedString("Set Passwords", comment: "")
  45. self.topLine.isHidden = true
  46. tableView.delegate = self
  47. tableView.dataSource = self
  48. tableView.selectionHighlightStyle = .none
  49. self.bottomLine.isHidden = true
  50. batchEncryptButton.title = NSLocalizedString("Batch", comment: "")
  51. batchEncryptButton.isBordered = false
  52. batchEncryptButton.wantsLayer = true
  53. batchEncryptButton.layer?.borderWidth = 1
  54. batchEncryptButton.layer?.cornerRadius = 4
  55. batchEncryptButton.target = self
  56. batchEncryptButton.action = #selector(batchEncryptButtonAction)
  57. batchEncryptButton.isHidden = true
  58. cancelVC.target = self
  59. cancelVC.action = #selector(cancelButtonAction)
  60. cancelVC.stringValue = NSLocalizedString("Cancel", comment: "")
  61. cancelVC.button(type: .Sec, size: .m)
  62. self.cancelVC.button.keyEquivalent = KMKeyEquivalent.esc.string()
  63. encryptVC.target = self
  64. encryptVC.action = #selector(encryptButtonAction)
  65. encryptVC.stringValue = NSLocalizedString("Encrypt", comment: "")
  66. encryptVC.button(type: .Cta, size: .m)
  67. self.encryptVC.button.keyEquivalent = KMKeyEquivalent.enter
  68. updateEncryptButtonEnabledState()
  69. self.initUIProperty()
  70. }
  71. private func initUIProperty() {
  72. self.titleLabel.textColor = NSColor.titleColor()
  73. self.batchEncryptButton.setTitleColor(NSColor.buttonTitleColor())
  74. self.batchEncryptButton.layer?.borderColor = NSColor.buttonBorderColor().cgColor
  75. }
  76. @objc func batchEncryptButtonAction() {
  77. guard let callback = itemClick else {
  78. return
  79. }
  80. callback(1)
  81. }
  82. @objc func cancelButtonAction() {
  83. guard let callback = itemClick else {
  84. return
  85. }
  86. callback(2)
  87. }
  88. @objc func encryptButtonAction() {
  89. if (!self.canEncrypt) {
  90. return
  91. }
  92. if (myDocument != nil) {
  93. var options: [CPDFDocumentWriteOption : Any] = [:]
  94. if model.openPasswordOn && model.ownerPasswordOn { /// 开启密码 & 权限密码
  95. if (!model.openPassword.isEmpty) {
  96. options.updateValue(model.openPassword, forKey: .userPasswordOption)
  97. }
  98. if (!model.ownerPassword.isEmpty) {
  99. options.updateValue(model.ownerPassword, forKey: .ownerPasswordOption)
  100. }
  101. /// 允许打印
  102. if model.printEnabled {
  103. if model.printAllowed == false {
  104. options.updateValue(false, forKey: .allowsPrintingOption)
  105. } else {
  106. options.updateValue(true, forKey: .allowsPrintingOption)
  107. if model.printSelectedIndex == 2 {
  108. options.updateValue(true, forKey: .allowsHighQualityPrintingOption)
  109. }
  110. }
  111. }
  112. /// 允许更改
  113. if model.editEnabled {
  114. if model.editSelectedIndex == 1 {
  115. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  116. options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
  117. } else if model.editSelectedIndex == 2 {
  118. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  119. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  120. } else if model.editSelectedIndex == 3 {
  121. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  122. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  123. options.updateValue(true, forKey: .allowsCommentingOption)
  124. } else if model.editAllowed == true {
  125. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  126. options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
  127. options.updateValue(true, forKey: .allowsCommentingOption)
  128. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  129. }
  130. }
  131. /// 加密层级 sdk 缺接口
  132. } else if model.openPasswordOn { /// 开启密码
  133. if (!model.openPassword.isEmpty) {
  134. options.updateValue(model.openPassword, forKey: .userPasswordOption)
  135. }
  136. /// 加密层级 sdk 缺接口
  137. } else if model.ownerPasswordOn { /// 权限密码
  138. if (!model.ownerPassword.isEmpty) {
  139. options.updateValue(model.ownerPassword, forKey: .ownerPasswordOption)
  140. }
  141. /// 允许打印
  142. if model.printEnabled {
  143. if model.printAllowed == false {
  144. options.updateValue(false, forKey: .allowsPrintingOption)
  145. } else {
  146. options.updateValue(true, forKey: .allowsPrintingOption)
  147. if model.printSelectedIndex == 2 {
  148. options.updateValue(true, forKey: .allowsHighQualityPrintingOption)
  149. }
  150. }
  151. }
  152. /// 允许更改
  153. if model.editEnabled {
  154. if model.editSelectedIndex == 1 {
  155. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  156. options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
  157. } else if model.editSelectedIndex == 2 {
  158. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  159. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  160. } else if model.editSelectedIndex == 3 {
  161. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  162. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  163. options.updateValue(true, forKey: .allowsCommentingOption)
  164. } else if model.editAllowed == true {
  165. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  166. options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
  167. options.updateValue(true, forKey: .allowsCommentingOption)
  168. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  169. }
  170. }
  171. /// 加密层级 sdk 缺接口
  172. }
  173. // let result = myDocument.write(to: documentURL, withOptions: options)
  174. // myDocument.setPasswordOptions(options)
  175. self.myOptions = options
  176. guard let callback = resultCallback else {
  177. return
  178. }
  179. callback(true)
  180. }
  181. }
  182. func updateEncryptButtonEnabledState() {
  183. var enabled = false
  184. if model.openPasswordOn {
  185. if !model.openPassword.isEmpty {
  186. enabled = true
  187. }
  188. }
  189. if !enabled {
  190. if model.ownerPasswordOn {
  191. if !model.ownerPassword.isEmpty && (!model.printAllowed || !model.editAllowed) {
  192. enabled = true
  193. }
  194. }
  195. } else {
  196. if model.ownerPasswordOn {
  197. if !model.ownerPassword.isEmpty && (!model.printAllowed || !model.editAllowed) {
  198. } else {
  199. enabled = false
  200. }
  201. }
  202. }
  203. self.canEncrypt = enabled
  204. if enabled {
  205. encryptVC.state = .Norm
  206. } else {
  207. encryptVC.state = .Disabled
  208. }
  209. }
  210. }
  211. extension KMSecureEncryptWindowController: NSTableViewDataSource {
  212. func numberOfRows(in tableView: NSTableView) -> Int {
  213. return 5
  214. }
  215. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  216. if (row == 0) {
  217. return 0.01
  218. } else if (row == 1) { // 6 + 22 + 10 + 32
  219. return 70
  220. } else if (row == 2) { // 6 + 22 + 10 + 32
  221. return 70
  222. }
  223. // 22 + 8
  224. return 30
  225. }
  226. }
  227. extension KMSecureEncryptWindowController: NSTableViewDelegate {
  228. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  229. if row == 1 {
  230. var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CellID"), owner: self)
  231. if cellView == nil {
  232. cellView = KMSecureEncryptPasswordCellView()
  233. }
  234. let myCellView: KMSecureEncryptPasswordCellView = cellView! as! KMSecureEncryptPasswordCellView
  235. myCellView.checkBox.title = NSLocalizedString("Document Open Password", comment: "")
  236. myCellView.checkBox.setTitleColor(NSColor.buttonTitleColor())
  237. myCellView.setPlaceholderString(NSLocalizedString("Open Password", comment: ""))
  238. if model.openPasswordOn {
  239. myCellView.checkBox.state = .on
  240. } else {
  241. myCellView.checkBox.state = .off
  242. }
  243. myCellView.itemClick = { [unowned self] itemView, _ in
  244. self.model.openPasswordOn = itemView!.kmEnabled
  245. self.updateEncryptButtonEnabledState()
  246. }
  247. myCellView.valueChange = { [unowned self] string,_ in
  248. if let value = (string as? String) {
  249. self.model.openPassword = value
  250. }
  251. self.updateEncryptButtonEnabledState()
  252. }
  253. return cellView
  254. } else if row == 2 {
  255. var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CellID"), owner: self)
  256. if cellView == nil {
  257. cellView = KMSecureEncryptPasswordCellView()
  258. }
  259. let myCellView: KMSecureEncryptPasswordCellView = cellView! as! KMSecureEncryptPasswordCellView
  260. myCellView.checkBox.title = NSLocalizedString("Document Permission Password", comment: "")
  261. myCellView.checkBox.setTitleColor(NSColor.buttonTitleColor())
  262. myCellView.setPlaceholderString(NSLocalizedString("Permission Password", comment: ""))
  263. if model.ownerPasswordOn {
  264. myCellView.checkBox.state = .on
  265. } else {
  266. myCellView.checkBox.state = .off
  267. }
  268. myCellView.itemClick = { [unowned self] itemView, _ in
  269. if (itemView!.kmEnabled) {
  270. self.model.ownerPasswordOn = true
  271. self.model.printEnabled = true
  272. self.model.editEnabled = true
  273. self.model.printAllowed = false
  274. self.model.editAllowed = false
  275. } else {
  276. self.model.ownerPasswordOn = false
  277. self.model.printEnabled = false
  278. self.model.editEnabled = false
  279. self.model.printAllowed = true
  280. self.model.editAllowed = true
  281. }
  282. self.updateEncryptButtonEnabledState()
  283. self.tableView.reloadData(forRowIndexes: IndexSet.init(arrayLiteral: 3, 4), columnIndexes: IndexSet.init(integer: 0))
  284. }
  285. myCellView.valueChange = { [unowned self] string, _ in
  286. if let value = (string as? String) {
  287. self.model.ownerPassword = value
  288. }
  289. self.updateEncryptButtonEnabledState()
  290. }
  291. return cellView
  292. } else if row == 3 {
  293. var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CellID"), owner: self)
  294. if cellView == nil {
  295. cellView = KMSecureEncryptCheckCellView()
  296. }
  297. let myCellView: KMSecureEncryptCheckCellView = cellView! as! KMSecureEncryptCheckCellView
  298. myCellView.check?.title = NSLocalizedString("Restrict document printing", comment: "")
  299. myCellView.kmEnabled = model.printEnabled
  300. if (self.model.printAllowed) {
  301. myCellView.check?.state = .off
  302. } else {
  303. myCellView.check?.state = .on
  304. }
  305. myCellView.itemClick = { [unowned self] result in
  306. self.model.printAllowed = !result
  307. self.updateEncryptButtonEnabledState()
  308. }
  309. return cellView
  310. } else if row == 4 {
  311. var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CellID"), owner: self)
  312. if cellView == nil {
  313. cellView = KMSecureEncryptCheckCellView()
  314. }
  315. let myCellView: KMSecureEncryptCheckCellView = cellView! as! KMSecureEncryptCheckCellView
  316. myCellView.check?.title = NSLocalizedString("Restrict content copying", comment: "")
  317. myCellView.kmEnabled = model.editEnabled
  318. if (self.model.editAllowed) {
  319. myCellView.check?.state = .off
  320. } else {
  321. myCellView.check?.state = .on
  322. }
  323. myCellView.itemClick = { [unowned self] result in
  324. self.model.editAllowed = !result
  325. self.updateEncryptButtonEnabledState()
  326. }
  327. return cellView
  328. } else if row == 5 {
  329. var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CellID"), owner: self)
  330. if cellView == nil {
  331. cellView = KMSecureEncryptComboBoxCellView()
  332. }
  333. let myCellView: KMSecureEncryptComboBoxCellView = cellView! as! KMSecureEncryptComboBoxCellView
  334. myCellView.titleLabel.stringValue = NSLocalizedString("加密级别", comment: "")
  335. myCellView.comboBox.removeAllItems()
  336. myCellView.comboBox.addItems(withObjectValues: model.encryptLevetArray)
  337. myCellView.comboBox.selectItem(at: model.editSelectedIndex)
  338. myCellView.itemClick = { [unowned self] _, index in
  339. self.model.encryptLevelSelectedIndex = index
  340. }
  341. return cellView
  342. }
  343. var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CellID"), owner: self)
  344. if cellView == nil {
  345. cellView = NSView.init()
  346. }
  347. return cellView
  348. }
  349. }