DSignatureConfigWindowController.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. //
  2. // DSignatureConfigWindowController.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2023/9/27.
  6. //
  7. import Cocoa
  8. typealias configWActionInfoBlock = (_ controller: DSignatureConfigWindowController, _ type: DSignatureActionType) -> Void
  9. @objcMembers class DSignatureConfigWindowController: NSWindowController {
  10. @IBOutlet weak var contendBox: NSBox!
  11. var fileListVC: DSignatureFileListViewController = DSignatureFileListViewController.init()
  12. var certifyDetailVC: DSignatureCertifyDetailViewController!
  13. var createVC: DSignatureCreateViewController = DSignatureCreateViewController.init()
  14. var fromFileVC: DSignatureFromFileViewController = DSignatureFromFileViewController.init()
  15. var saveTypeVC: DSignatureSaveTypeViewController = DSignatureSaveTypeViewController.init()
  16. var createInfoVC: DSignatureCreateInfoViewController = DSignatureCreateInfoViewController.init()
  17. var saveFolderTypeVC: DSignatureSaveFolderViewController = DSignatureSaveFolderViewController.init()
  18. var certificateCustomVC: CDSignatureCertificateCustomViewController = CDSignatureCertificateCustomViewController.init()
  19. @objc var _viewType: DSignatureViewType = .none
  20. @objc var appearanceWidget: CPDFSignatureWidgetAnnotation!
  21. @objc var isCreatDS: Bool = false
  22. private var cert: KMDSignatureModel!
  23. private var certDictionary: NSDictionary!
  24. private var certUsage: CPDFCertUsage!
  25. private var saveType: CDSignatureSaveType = .none
  26. var actionBlock: ((DSignatureConfigWindowController, DSignatureActionType)->Void)?
  27. var complentionHandle: ((_ isSign: Bool, _ dic: NSDictionary, _ config: CPDFSignatureConfig, _ isLock: Bool) ->Void)?
  28. override func windowDidLoad() {
  29. super.windowDidLoad()
  30. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  31. self.initFileListVC()
  32. self.initCreateVC()
  33. self.initFromFileVC()
  34. self.initSaveTypeVC()
  35. self.initCreateInfoViewVC()
  36. self.initSaveFolderVC()
  37. self.initCertificateCustomVC()
  38. if KMDSignatureManager.default().signatures.count == 0 {
  39. KMDSignatureManager.default().loadAllKeyChainCertificates()
  40. }
  41. }
  42. //MARK: Setter
  43. @objc var viewType: DSignatureViewType {
  44. set {
  45. _viewType = newValue;
  46. switch _viewType {
  47. case .none:
  48. break
  49. case .fileList:
  50. self.setContendView(self.fileListVC.view)
  51. self.fileListVC.isFileList = false
  52. break
  53. case .certifyDetail:
  54. self.initCertifyDetailVC()
  55. self.setContendView(self.certifyDetailVC.view)
  56. break
  57. case .createNew:
  58. self.createVC.isCreatDS = self.isCreatDS
  59. self.setContendView(self.createVC.view)
  60. break
  61. case .createFromFile:
  62. self.setContendView(self.fromFileVC.view)
  63. break
  64. case .saveType:
  65. self.setContendView(self.saveTypeVC.view)
  66. break
  67. case .createInfo:
  68. self.setContendView(self.createInfoVC.view)
  69. break
  70. case .saveFolderType:
  71. self.setContendView(self.saveFolderTypeVC.view)
  72. break
  73. case .signatureDetail:
  74. break
  75. case .edit:
  76. self.certificateCustomVC.signatureWidget = self.appearanceWidget
  77. self.certificateCustomVC.cert = self.cert
  78. self.setContendView(self.certificateCustomVC.view)
  79. break
  80. case .fromFileList:
  81. self.fileListVC.isFileList = true
  82. self.setContendView(self.fileListVC.view)
  83. break
  84. }
  85. }
  86. get {
  87. return _viewType;
  88. }
  89. }
  90. func setContendView(_ view: NSView) {
  91. var frame = self.window?.frame
  92. let originX = (frame?.origin.x)!
  93. let originY = (frame?.origin.y)!
  94. let height: CGFloat = (frame?.size.height)!
  95. let width: CGFloat = (frame?.size.width)!
  96. var viewHeight: CGFloat = 500
  97. if self.viewType == .fileList ||
  98. self.viewType == .fromFileList {
  99. viewHeight = 499 + 22
  100. } else if self.viewType == .createNew {
  101. viewHeight = 500 + 22
  102. } else if self.viewType == .certifyDetail {
  103. viewHeight = 450 + 22
  104. } else if self.viewType == .createFromFile {
  105. viewHeight = 410 + 22
  106. } else if self.viewType == .edit {
  107. viewHeight = 530 + 22
  108. } else if self.viewType == .saveType {
  109. viewHeight = 430 + 22
  110. } else if self.viewType == .saveFolderType {
  111. viewHeight = 450 + 22
  112. } else {
  113. frame?.size.height = view.frame.size.height + 22
  114. }
  115. frame?.size.height = viewHeight
  116. frame?.origin.y = originY + (height - viewHeight)
  117. self.window?.setFrame(frame!, display: true)
  118. self.contendBox.contentView = view
  119. }
  120. //MARK: DSignatureFileListViewController
  121. func initFileListVC () {
  122. weak var weakSelf = self
  123. self.fileListVC.actionBlock = { fileListVC, type in
  124. if type == .cancel {
  125. if fileListVC.isFileList {
  126. weakSelf!.viewType = .createFromFile
  127. } else {
  128. guard let callback = weakSelf!.actionBlock else {
  129. return
  130. }
  131. callback(weakSelf!, type)
  132. }
  133. } else if type == .confirm {
  134. if fileListVC.isFileList {
  135. weakSelf!.viewType = .fileList
  136. } else {
  137. weakSelf!.cert = fileListVC.currentModel
  138. weakSelf!.viewType = .certifyDetail
  139. }
  140. } else if type == .createNewDsign {
  141. weakSelf!.viewType = .createNew
  142. }
  143. }
  144. self.fileListVC.viewCertDetailBlock = {fileListVC, certificate, isDigitalFile in
  145. DispatchQueue.main.async {
  146. let signatureDetailVC = DSignatureDetailsViewController.init()
  147. signatureDetailVC.isDigitalFile = isDigitalFile
  148. signatureDetailVC.certificates = [certificate]
  149. fileListVC.presentAsModalWindow(signatureDetailVC)
  150. }
  151. }
  152. }
  153. //MARK: DSignatureCertifyDetailViewController
  154. func initCertifyDetailVC () {
  155. if self.certifyDetailVC != nil {
  156. self.certifyDetailVC.view.removeFromSuperview()
  157. self.certifyDetailVC = nil
  158. }
  159. self.certifyDetailVC = DSignatureCertifyDetailViewController.init()
  160. self.certifyDetailVC.cert = self.cert
  161. self.certifyDetailVC.signatureWidget = self.appearanceWidget
  162. self.certifyDetailVC.actionBlock = {detailVC, actionType, appearanceData, config, password, isLock in
  163. if actionType == .cancel {
  164. self.closeAction()
  165. } else if actionType == .previousStep {
  166. self.viewType = .fileList
  167. } else if actionType == .confirm {
  168. var returnAcType = actionType
  169. let dic = NSMutableDictionary()
  170. if self.cert.isFormKeyChain {
  171. var save: Bool = false
  172. let folderPath = kDSignatureFolderPath
  173. if FileManager.default.fileExists(atPath: folderPath) == false {
  174. try?FileManager.default.createDirectory(atPath: kDSignatureFolderPath, withIntermediateDirectories: false)
  175. }
  176. let filePath = kDSignatureFolderPath.appending("/CDSignatures.p12")
  177. save = KMDSignatureManager.exportKeyChain(withP12FilePath: filePath, signatureModel: self.cert, passWord: "")
  178. if save {
  179. dic.setValue(filePath, forKey: SAVEFILEPATH_KEY)
  180. dic.setValue("", forKey: PASSWORD_KEY)
  181. } else {
  182. return
  183. }
  184. if save == false {
  185. returnAcType = .none
  186. }
  187. } else {
  188. dic.setValue(self.cert.filePath, forKey: SAVEFILEPATH_KEY)
  189. dic.setValue(password, forKey: PASSWORD_KEY)
  190. }
  191. guard let complentionCallBack = self.complentionHandle else {
  192. return
  193. }
  194. complentionCallBack(true, dic, config, isLock)
  195. guard let callback = self.actionBlock else {
  196. return
  197. }
  198. callback(self, returnAcType)
  199. } else if actionType == .create {
  200. self.certificateCustomVC.isCreat = true
  201. self.certificateCustomVC.appearanceData = nil
  202. self.viewType = .edit
  203. } else if actionType == .edit {
  204. self.certificateCustomVC.isCreat = false
  205. self.certificateCustomVC.appearanceData = appearanceData
  206. self.viewType = .edit
  207. } else if actionType == .delete {
  208. } else if actionType == .viewDetail {
  209. }
  210. }
  211. }
  212. //MARK: DSignatureCreateViewController
  213. func initCreateVC () {
  214. self.createVC.actionBlock = { createVC, actionType, createType in
  215. if actionType == .previousStep {
  216. self.viewType = .fileList
  217. } else if actionType == .confirm {
  218. if createType == .signCreate {
  219. self.viewType = .saveType
  220. } else if createType == .fromFile {
  221. self.viewType = .createFromFile
  222. }
  223. } else if actionType == .cancel {
  224. guard let callback = self.actionBlock else {
  225. return
  226. }
  227. callback(self, actionType)
  228. }
  229. }
  230. }
  231. //MARK: DSignatureFromFileViewController
  232. func initFromFileVC () {
  233. self.fromFileVC.actionBlock = {fromFileVC, action, cer in
  234. if action == .cancel {
  235. self.closeAction()
  236. } else if action == .previousStep {
  237. self.viewType = .createNew
  238. } else if action == .confirm {
  239. self.viewType = .fromFileList
  240. } else if action == .createNewDsign {
  241. self.viewType = .saveType
  242. }
  243. }
  244. }
  245. //MARK: DSignatureSaveTypeViewController
  246. func initSaveTypeVC () {
  247. self.saveTypeVC.actionBlock = { saveTypeVC, actionType, saveType in
  248. self.saveType = saveType
  249. if actionType == .cancel {
  250. self.closeAction()
  251. } else if actionType == .previousStep {
  252. self.viewType = .createNew
  253. } else if actionType == .confirm {
  254. self.viewType = .createInfo
  255. }
  256. }
  257. }
  258. //MARK: DSignatureCreateInfoViewController
  259. func initCreateInfoViewVC () {
  260. self.createInfoVC.actionBlock = { createVC, action, cer, certUsage in
  261. self.certDictionary = cer
  262. if certUsage == 0 {
  263. self.certUsage = .digSig
  264. } else if certUsage == 1 {
  265. self.certUsage = .dataEnc
  266. } else if certUsage == 2 {
  267. self.certUsage = .all
  268. }
  269. if action == .previousStep {
  270. self.viewType = .saveType
  271. } else if action == .confirm {
  272. if self.saveType == .folder {
  273. self.viewType = .saveFolderType
  274. } else {
  275. let folderPath = kDSignatureFolderPath
  276. var save: Bool = true
  277. if FileManager.default.fileExists(atPath: folderPath) == false {
  278. try?FileManager.default.createDirectory(atPath: folderPath, withIntermediateDirectories: false)
  279. save = FileManager.default.fileExists(atPath: folderPath)
  280. }
  281. let filePath = kDSignatureFolderPath.appending("/KMDSignatures.p12")
  282. save = CPDFSignature.generatePKCS12Cert(withInfo: cer as? [AnyHashable : Any], password: "1", toPath: filePath, certUsage: self.certUsage)
  283. let success = KMDSignatureManager.default().importKeyChain(withP12FilePath: filePath, passWord: "1")
  284. if save && success && FileManager.default.fileExists(atPath: filePath) {
  285. self.viewType = .fileList
  286. } else {
  287. let alert = NSAlert.init()
  288. alert.messageText = NSLocalizedString("Save failed!", comment: "")
  289. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  290. alert.runModal()
  291. }
  292. }
  293. } else if action == .cancel {
  294. self.closeAction()
  295. }
  296. }
  297. }
  298. //MARK: DSignatureSaveFolderViewController
  299. func initSaveFolderVC () {
  300. self.saveFolderTypeVC.actionBlock = { saveFolderVC, actionType, infoDic in
  301. if actionType == .cancel {
  302. self.closeAction()
  303. } else if actionType == .confirm {
  304. var save: Bool = CPDFSignature.generatePKCS12Cert(withInfo: self.certDictionary as? [AnyHashable : Any], password: infoDic[PASSWORD_KEY] as? String, toPath: infoDic[SAVEFILEPATH_KEY] as? String, certUsage: self.certUsage)
  305. KMDSignatureManager.default().moveP12DigitalFile(withFilePath: infoDic[SAVEFILEPATH_KEY] as? String, password: infoDic[PASSWORD_KEY] as? String)
  306. if save {
  307. self.viewType = .fileList
  308. } else {
  309. let alert = NSAlert.init()
  310. alert.messageText = NSLocalizedString("Save failed!", comment: "")
  311. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  312. alert.runModal()
  313. }
  314. } else if actionType == .previousStep {
  315. self.viewType = .createInfo
  316. }
  317. }
  318. }
  319. //MARK: CDSignatureCertificateCustomViewController
  320. func initCertificateCustomVC () {
  321. self.certificateCustomVC.actionBlock = {customVC, type, appearanceData in
  322. if type == .cancel {
  323. self.closeAction()
  324. } else if type == .previousStep {
  325. self.viewType = .certifyDetail
  326. } else if type == .confirm {
  327. _ = DSignatureApperanceManager.manager.addDSignatureAppearanceData(watermark: appearanceData)
  328. DSignatureApperanceManager.manager.saveDSignatureAppearanceData(watermark: appearanceData)
  329. self.viewType = .certifyDetail
  330. self.certifyDetailVC.cert = self.cert
  331. self.certifyDetailVC.appearanceData = appearanceData
  332. self.certifyDetailVC.signatureWidget = self.appearanceWidget
  333. self.certifyDetailVC.reloadSignatures()
  334. }
  335. }
  336. }
  337. //
  338. func closeAction() {
  339. guard let callback = self.actionBlock else {
  340. return
  341. }
  342. callback(self, .cancel)
  343. }
  344. }