KMSecureEncryptWindowController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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(color: 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: .allowsCopyingOption)
  126. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  127. options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
  128. options.updateValue(true, forKey: .allowsCommentingOption)
  129. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  130. } else {
  131. options.updateValue(false, forKey: .allowsCopyingOption)
  132. }
  133. }
  134. /// 加密层级 sdk 缺接口
  135. } else if model.openPasswordOn { /// 开启密码
  136. if (!model.openPassword.isEmpty) {
  137. options.updateValue(model.openPassword, forKey: .userPasswordOption)
  138. }
  139. /// 加密层级 sdk 缺接口
  140. } else if model.ownerPasswordOn { /// 权限密码
  141. if (!model.ownerPassword.isEmpty) {
  142. options.updateValue(model.ownerPassword, forKey: .ownerPasswordOption)
  143. }
  144. /// 允许打印
  145. if model.printEnabled {
  146. if model.printAllowed == false {
  147. options.updateValue(false, forKey: .allowsPrintingOption)
  148. } else {
  149. options.updateValue(true, forKey: .allowsPrintingOption)
  150. if model.printSelectedIndex == 2 {
  151. options.updateValue(true, forKey: .allowsHighQualityPrintingOption)
  152. }
  153. }
  154. }
  155. /// 允许更改
  156. if model.editEnabled {
  157. if model.editSelectedIndex == 1 {
  158. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  159. options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
  160. } else if model.editSelectedIndex == 2 {
  161. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  162. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  163. } else if model.editSelectedIndex == 3 {
  164. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  165. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  166. options.updateValue(true, forKey: .allowsCommentingOption)
  167. } else if model.editAllowed == true {
  168. options.updateValue(true, forKey: .allowsCopyingOption)
  169. options.updateValue(true, forKey: .allowsDocumentChangesOption)
  170. options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
  171. options.updateValue(true, forKey: .allowsCommentingOption)
  172. options.updateValue(true, forKey: .allowsFormFieldEntryOption)
  173. } else {
  174. options.updateValue(false, forKey: .allowsCopyingOption)
  175. }
  176. }
  177. /// 加密层级 sdk 缺接口
  178. }
  179. // let result = myDocument.write(to: documentURL, withOptions: options)
  180. // myDocument.setPasswordOptions(options)
  181. self.myOptions = options
  182. guard let callback = resultCallback else {
  183. return
  184. }
  185. callback(true)
  186. }
  187. }
  188. func updateEncryptButtonEnabledState() {
  189. var enabled = false
  190. if model.openPasswordOn {
  191. if !model.openPassword.isEmpty {
  192. enabled = true
  193. }
  194. }
  195. if !enabled {
  196. if model.ownerPasswordOn {
  197. if !model.ownerPassword.isEmpty && (!model.printAllowed || !model.editAllowed) {
  198. enabled = true
  199. }
  200. }
  201. } else {
  202. if model.ownerPasswordOn {
  203. if !model.ownerPassword.isEmpty && (!model.printAllowed || !model.editAllowed) {
  204. } else {
  205. enabled = false
  206. }
  207. }
  208. }
  209. self.canEncrypt = enabled
  210. if enabled {
  211. encryptVC.state = .Norm
  212. } else {
  213. encryptVC.state = .Disabled
  214. }
  215. }
  216. }
  217. extension KMSecureEncryptWindowController: NSTableViewDataSource {
  218. func numberOfRows(in tableView: NSTableView) -> Int {
  219. return 5
  220. }
  221. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  222. if (row == 0) {
  223. return 0.01
  224. } else if (row == 1) { // 6 + 22 + 10 + 32
  225. return 70
  226. } else if (row == 2) { // 6 + 22 + 10 + 32
  227. return 70
  228. }
  229. // 22 + 8
  230. return 30
  231. }
  232. }
  233. extension KMSecureEncryptWindowController: NSTableViewDelegate {
  234. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  235. if row == 1 {
  236. var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CellID"), owner: self)
  237. if cellView == nil {
  238. cellView = KMSecureEncryptPasswordCellView()
  239. }
  240. let myCellView: KMSecureEncryptPasswordCellView = cellView! as! KMSecureEncryptPasswordCellView
  241. myCellView.checkBox.title = NSLocalizedString("Document Open Password", comment: "")
  242. myCellView.checkBox.setTitleColor(color: NSColor.buttonTitleColor())
  243. myCellView.setPlaceholderString(NSLocalizedString("Open Password", comment: ""))
  244. if model.openPasswordOn {
  245. myCellView.checkBox.state = .on
  246. } else {
  247. myCellView.checkBox.state = .off
  248. }
  249. myCellView.itemClick = { [unowned self] itemView, _ in
  250. self.model.openPasswordOn = itemView!.kmEnabled
  251. self.updateEncryptButtonEnabledState()
  252. }
  253. myCellView.valueChange = { [unowned self] string,_ in
  254. if let value = (string as? String) {
  255. self.model.openPassword = value
  256. }
  257. self.updateEncryptButtonEnabledState()
  258. }
  259. return cellView
  260. } else if row == 2 {
  261. var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CellID"), owner: self)
  262. if cellView == nil {
  263. cellView = KMSecureEncryptPasswordCellView()
  264. }
  265. let myCellView: KMSecureEncryptPasswordCellView = cellView! as! KMSecureEncryptPasswordCellView
  266. myCellView.checkBox.title = NSLocalizedString("Document Permission Password", comment: "")
  267. myCellView.checkBox.setTitleColor(color: NSColor.buttonTitleColor())
  268. myCellView.setPlaceholderString(NSLocalizedString("Permission Password", comment: ""))
  269. if model.ownerPasswordOn {
  270. myCellView.checkBox.state = .on
  271. } else {
  272. myCellView.checkBox.state = .off
  273. }
  274. myCellView.itemClick = { [unowned self] itemView, _ in
  275. if (itemView!.kmEnabled) {
  276. self.model.ownerPasswordOn = true
  277. self.model.printEnabled = true
  278. self.model.editEnabled = true
  279. self.model.printAllowed = false
  280. self.model.editAllowed = false
  281. } else {
  282. self.model.ownerPasswordOn = false
  283. self.model.printEnabled = false
  284. self.model.editEnabled = false
  285. self.model.printAllowed = true
  286. self.model.editAllowed = true
  287. }
  288. self.updateEncryptButtonEnabledState()
  289. self.tableView.reloadData(forRowIndexes: IndexSet.init(arrayLiteral: 3, 4), columnIndexes: IndexSet.init(integer: 0))
  290. }
  291. myCellView.valueChange = { [unowned self] string, _ in
  292. if let value = (string as? String) {
  293. self.model.ownerPassword = value
  294. }
  295. self.updateEncryptButtonEnabledState()
  296. }
  297. return cellView
  298. } else if row == 3 {
  299. var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CellID"), owner: self)
  300. if cellView == nil {
  301. cellView = KMSecureEncryptCheckCellView()
  302. }
  303. let myCellView: KMSecureEncryptCheckCellView = cellView! as! KMSecureEncryptCheckCellView
  304. myCellView.check?.title = NSLocalizedString("Restrict document printing", comment: "")
  305. myCellView.kmEnabled = model.printEnabled
  306. if (self.model.printAllowed) {
  307. myCellView.check?.state = .off
  308. } else {
  309. myCellView.check?.state = .on
  310. }
  311. myCellView.itemClick = { [unowned self] result in
  312. self.model.printAllowed = !result
  313. self.updateEncryptButtonEnabledState()
  314. }
  315. return cellView
  316. } else if row == 4 {
  317. var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CellID"), owner: self)
  318. if cellView == nil {
  319. cellView = KMSecureEncryptCheckCellView()
  320. }
  321. let myCellView: KMSecureEncryptCheckCellView = cellView! as! KMSecureEncryptCheckCellView
  322. myCellView.check?.title = NSLocalizedString("Restrict content copying", comment: "")
  323. myCellView.kmEnabled = model.editEnabled
  324. if (self.model.editAllowed) {
  325. myCellView.check?.state = .off
  326. } else {
  327. myCellView.check?.state = .on
  328. }
  329. myCellView.itemClick = { [unowned self] result in
  330. self.model.editAllowed = !result
  331. self.updateEncryptButtonEnabledState()
  332. }
  333. return cellView
  334. } else if row == 5 {
  335. var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CellID"), owner: self)
  336. if cellView == nil {
  337. cellView = KMSecureEncryptComboBoxCellView()
  338. }
  339. let myCellView: KMSecureEncryptComboBoxCellView = cellView! as! KMSecureEncryptComboBoxCellView
  340. myCellView.titleLabel.stringValue = NSLocalizedString("加密级别", comment: "")
  341. myCellView.comboBox.removeAllItems()
  342. myCellView.comboBox.addItems(withObjectValues: model.encryptLevetArray)
  343. myCellView.comboBox.selectItem(at: model.editSelectedIndex)
  344. myCellView.itemClick = { [unowned self] _, index in
  345. self.model.encryptLevelSelectedIndex = index
  346. }
  347. return cellView
  348. }
  349. var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CellID"), owner: self)
  350. if cellView == nil {
  351. cellView = NSView.init()
  352. }
  353. return cellView
  354. }
  355. }