KMSecureEncryptWindowController.swift 17 KB

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