DSignatureCertifyDetailViewController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. //
  2. // DSignatureCertifyDetailViewController.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2023/9/28.
  6. //
  7. import Cocoa
  8. class DSignatureCertifyDetailViewController: NSViewController, NSTextFieldDelegate {
  9. @IBOutlet weak var contendView: NSView!
  10. @IBOutlet weak var titleLabel: NSTextField!
  11. @IBOutlet weak var closeButton: NSButton!
  12. @IBOutlet weak var appearanceLabel: NSTextField!
  13. @IBOutlet weak var appearanceButton: NSPopUpButton!
  14. @IBOutlet weak var createButton: KMButton!
  15. @IBOutlet weak var editButton: KMButton!
  16. @IBOutlet weak var deleteButton: KMButton!
  17. @IBOutlet weak var lockDocButton: NSButton!
  18. @IBOutlet weak var errorLabel: NSTextField!
  19. @IBOutlet weak var drawBox: NSBox!
  20. @IBOutlet weak var drawImageView: NSImageView!
  21. @IBOutlet weak var infoButton: NSButton!
  22. @IBOutlet weak var passwordBox: NSBox!
  23. @IBOutlet weak var passwordLabel: NSTextField!
  24. @IBOutlet weak var passwordTextField: NSSecureTextField!
  25. @IBOutlet weak var cancelButton: NSButton!
  26. @IBOutlet weak var continueButton: NSButton!
  27. //证书+外观配置信息
  28. var _cert: KMDSignatureModel!
  29. var _appearanceData: DSignatureAppearanceData!
  30. var config: CPDFSignatureConfig!
  31. var _signatureWidget: CPDFSignatureWidgetAnnotation!
  32. var isLock: Bool = false
  33. var actionBlock: ((_ detailVC: DSignatureCertifyDetailViewController, _ actionType: DSignatureActionType, _ appearanceData: DSignatureAppearanceData, _ config: CPDFSignatureConfig, _ password: String, _ isLock: Bool)->Void)?
  34. override func viewDidLoad() {
  35. super.viewDidLoad()
  36. // Do view setup here.
  37. self.passwordTextField.delegate = self;
  38. self.localizedLanguage()
  39. if self.appearanceData == nil {
  40. self.appearanceData = DSignatureApperanceManager.manager.getLastDSignatureAppearanceData()
  41. }
  42. self.reloadSignatures()
  43. }
  44. //MARK: Setter
  45. var cert: KMDSignatureModel? {
  46. set {
  47. _cert = newValue
  48. }
  49. get {
  50. return _cert
  51. }
  52. }
  53. var appearanceData: DSignatureAppearanceData? {
  54. set {
  55. _appearanceData = newValue
  56. if _appearanceData.appearanceName.length > 0 && _appearanceData.appearanceName as String == NSLocalizedString("Standard Text", comment: "") {
  57. self.deleteButton.isEnabled = false
  58. self.editButton.isEnabled = false
  59. } else {
  60. self.deleteButton.isEnabled = true
  61. self.editButton.isEnabled = true
  62. }
  63. }
  64. get {
  65. return _appearanceData
  66. }
  67. }
  68. var signatureWidget: CPDFSignatureWidgetAnnotation? {
  69. set {
  70. _signatureWidget = newValue
  71. }
  72. get {
  73. return _signatureWidget
  74. }
  75. }
  76. //MARK: Method
  77. func reloadSignatures () {
  78. self.appearanceButton.removeAllItems()
  79. let datas = DSignatureApperanceManager.manager.loadDSignatureAppearanceDatas()
  80. var dex = 0;
  81. let titles = NSMutableArray.init()
  82. if datas.count > 0 {
  83. for i in 0...datas.count-1 {
  84. let data = datas[i] as! DSignatureAppearanceData
  85. if data.appearanceName.isEqual(to: self.appearanceData?.appearanceName) {
  86. dex = i
  87. }
  88. titles.add(data.appearanceName as Any)
  89. }
  90. }
  91. self.errorLabel.isHidden = true
  92. self.appearanceButton.addItems(withTitles: titles as! [String])
  93. self.appearanceButton.selectItem(at: dex)
  94. self.appearanceData = datas[dex] as? DSignatureAppearanceData
  95. if self.appearanceData?.isInputDSignatureText == false {
  96. self.appearanceData?.signatureConfig.text = _cert.name;
  97. }
  98. if self.cert?.isFormKeyChain == true {
  99. self.passwordBox.isHidden = true;
  100. } else {
  101. self.passwordBox.isHidden = false;
  102. }
  103. self.reloadData()
  104. }
  105. func reloadData() {
  106. self.config = CPDFSignatureConfig.init()
  107. self.config.isDrawKey = self.appearanceData!.signatureConfig.isDrawKey
  108. self.config.isDrawLogo = self.appearanceData!.signatureConfig.isDrawLogo
  109. self.config.isContentAlginLeft = self.appearanceData!.signatureConfig.isContentAlginLeft
  110. var textStr = ""
  111. if self.appearanceData?.signatureConfig.text != nil {
  112. if (self.appearanceData?.signatureConfig.text.count)! > 0 {
  113. textStr = (self.appearanceData?.signatureConfig.text)!
  114. }
  115. }
  116. if (textStr.count > 0) {
  117. self.config.text = self.cert?.name
  118. } else if (self.appearanceData!.signatureConfig.image != nil) {
  119. self.config.image = self.appearanceData!.signatureConfig.image
  120. } else {
  121. self.config.text = nil
  122. self.config.image = nil
  123. self.config.isDrawOnlyContent = true
  124. }
  125. self.config.logo = self.appearanceData!.signatureConfig.logo;
  126. let contens = NSMutableArray.init()
  127. for configItem in self.appearanceData!.signatureConfig.contents {
  128. let cerName = _cert.name ?? ""
  129. if configItem.key == NAME_KEY && !cerName.isEmpty {
  130. let tConfigItem = CPDFSignatureConfigItem.init()
  131. tConfigItem.key = configItem.key
  132. tConfigItem.value = self.cert?.name
  133. contens.add(tConfigItem)
  134. } else if configItem.key == DN_KEY && !_cert.dn.isEmpty {
  135. let tConfigItem = CPDFSignatureConfigItem.init()
  136. tConfigItem.key = configItem.key
  137. tConfigItem.value = _cert.dn
  138. contens.add(tConfigItem)
  139. } else if configItem.key == REASON_KEY {
  140. let tConfigItem = CPDFSignatureConfigItem.init()
  141. tConfigItem.key = configItem.key
  142. tConfigItem.value = configItem.value
  143. contens.add(tConfigItem)
  144. } else if configItem.key == LOCATION_KEY {
  145. let tConfigItem = CPDFSignatureConfigItem.init()
  146. tConfigItem.key = configItem.key
  147. tConfigItem.value = configItem.value
  148. contens.add(tConfigItem)
  149. } else if configItem.key == NSLocalizedString(DATE_KEY, comment: "") {
  150. let tConfigItem = CPDFSignatureConfigItem.init()
  151. tConfigItem.key = configItem.key
  152. let dateFormatter = DateFormatter.init()
  153. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  154. tConfigItem.value = dateFormatter.string(from: NSDate() as Date)
  155. contens.add(tConfigItem)
  156. } else if configItem.key == VERSION_KEY && _cert.name.isEmpty == false {
  157. let tConfigItem = CPDFSignatureConfigItem.init()
  158. tConfigItem.key = configItem.key
  159. let infoDictionary = NSMutableDictionary(dictionary: Bundle.main.infoDictionary!)
  160. CFShow(infoDictionary)
  161. let app_Version = infoDictionary.object(forKey: "CFBundleShortVersionString")
  162. tConfigItem.value = app_Version as? String
  163. contens.add(tConfigItem)
  164. }
  165. }
  166. self.config.contents = contens as? [CPDFSignatureConfigItem]
  167. self.signatureWidget?.signAppearanceConfig(self.config)
  168. self.drawImageView.image = self.signatureWidget?.contentImage()
  169. }
  170. func updateTextFiedState () {
  171. self.passwordTextField.layer?.borderColor = NSColor.systemRed.cgColor;
  172. }
  173. func localizedLanguage () {
  174. self.contendView.wantsLayer = true
  175. self.contendView.layer?.backgroundColor = NSColor.clear.cgColor
  176. if self.cert?.name != nil {
  177. self.titleLabel.stringValue = NSLocalizedString("Sign as", comment: "") + " " + (self.cert?.name)!
  178. } else {
  179. self.titleLabel.stringValue = NSLocalizedString("Sign as", comment: "")
  180. }
  181. self.appearanceLabel.stringValue = NSLocalizedString("Appearance", comment: "") + ":"
  182. self.passwordTextField.placeholderString = NSLocalizedString("Enter the digital signature ID card PIN or password", comment: "")
  183. self.passwordLabel.stringValue = NSLocalizedString("Password", comment: "") + ":"
  184. self.editButton.toolTip = NSLocalizedString("Edit", comment: "")
  185. self.createButton.toolTip = NSLocalizedString("Create", comment: "")
  186. self.deleteButton.toolTip = NSLocalizedString("Delete", comment: "")
  187. self.continueButton.title = NSLocalizedString("Continue", comment: "");
  188. self.cancelButton.title = NSLocalizedString("Previous Step",comment: "");
  189. self.infoButton.title = NSLocalizedString("View Certificate Details",comment: "");
  190. self.lockDocButton.title = NSLocalizedString("Lock document after signing", comment: "");
  191. let txtColor = KMAppearance.Interactive.a0Color()
  192. self.infoButton.setTitleColor(txtColor)
  193. self.lockDocButton.isHidden = true
  194. self.createButton.wantsLayer = true
  195. self.editButton.wantsLayer = true
  196. self.deleteButton.wantsLayer = true
  197. self.createButton.layer?.borderWidth = 0.5
  198. self.editButton.layer?.borderWidth = 0.5
  199. self.deleteButton.layer?.borderWidth = 0.5
  200. self.createButton.layer?.cornerRadius = 5
  201. self.editButton.layer?.cornerRadius = 5
  202. self.deleteButton.layer?.cornerRadius = 5
  203. self.createButton.layer?.borderColor = NSColor(red: 165.0/255.0, green: 167.0/255.0, blue: 179.0/255.0, alpha: 1).cgColor
  204. self.editButton.layer?.borderColor = NSColor(red: 165.0/255.0, green: 167.0/255.0, blue: 179.0/255.0, alpha: 1).cgColor
  205. self.deleteButton.layer?.borderColor = NSColor(red: 165.0/255.0, green: 167.0/255.0, blue: 179.0/255.0, alpha: 1).cgColor
  206. let creatFileLayer = CALayer()
  207. creatFileLayer.frame = NSMakeRect(0, 0, NSWidth(self.createButton.bounds), NSHeight(self.createButton.bounds));
  208. self.createButton.layer?.addSublayer(creatFileLayer)
  209. let editFileLayer = CALayer()
  210. editFileLayer.frame = NSMakeRect(0, 0, NSWidth(self.editButton.bounds), NSHeight(self.editButton.bounds));
  211. self.editButton.layer?.addSublayer(editFileLayer)
  212. let deleteFileLayer = CALayer()
  213. deleteFileLayer.frame = NSMakeRect(0, 0, NSWidth(self.deleteButton.bounds), NSHeight(self.deleteButton.bounds));
  214. self.deleteButton.layer?.addSublayer(deleteFileLayer)
  215. self.titleLabel.textColor = NSColor.labelColor
  216. self.appearanceLabel.textColor = NSColor.labelColor
  217. self.passwordLabel.textColor = NSColor.labelColor
  218. self.passwordTextField.textColor = NSColor.labelColor
  219. self.drawBox.borderColor = NSColor(red: 165.0/255.0, green: 167.0/255.0, blue: 179.0/255.0, alpha: 1)
  220. self.passwordTextField.wantsLayer = true
  221. self.passwordTextField.layer?.borderWidth = 1.0
  222. self.passwordTextField.layer?.cornerRadius = 5.0
  223. self.passwordTextField.placeholderString = NSLocalizedString("Enter the Digital ID PIN or Password...", comment: "")
  224. self.passwordTextField.updateState(false)
  225. self.errorLabel.stringValue = NSLocalizedString("Password is incorrect. Please re-enter the password.", comment: "")
  226. }
  227. //MARK: IBAction
  228. @IBAction func buttonItemClick_Appearence(_ sender: NSPopUpButton) {
  229. self.appearanceButton.title = sender.title;
  230. let datas = DSignatureApperanceManager.manager.loadDSignatureAppearanceDatas()
  231. self.appearanceData = datas.object(at: self.appearanceButton.indexOfSelectedItem) as? DSignatureAppearanceData
  232. if self.appearanceData?.isInputDSignatureText == false {
  233. self.appearanceData?.signatureConfig.text = _cert.name
  234. }
  235. self.reloadData()
  236. }
  237. @IBAction func buttonItemClick_Creat(_ sender: Any) {
  238. guard let callBack = self.actionBlock else {
  239. return
  240. }
  241. callBack(self, .create, self.appearanceData!, self.config, "", self.isLock)
  242. }
  243. @IBAction func buttonItemClick_Edit(_ sender: Any) {
  244. guard let callBack = self.actionBlock else {
  245. return
  246. }
  247. callBack(self, .edit, self.appearanceData!, self.config, "", self.isLock)
  248. }
  249. @IBAction func buttonItemClick_Delete(_ sender: Any) {
  250. _ = DSignatureApperanceManager.manager.removeDSignatureAppearanceData(watermark: self.appearanceData!)
  251. let datas = DSignatureApperanceManager.manager.loadDSignatureAppearanceDatas() as! [DSignatureAppearanceData]
  252. let titles = NSMutableArray()
  253. for i in 0..<datas.count {
  254. let data = datas[i]
  255. if data.appearanceName.length > 0 {
  256. titles.add(data.appearanceName as Any)
  257. }
  258. }
  259. self.appearanceButton.removeAllItems()
  260. self.appearanceButton.addItems(withTitles: titles as! [String])
  261. self.appearanceButton.selectItem(at: 0)
  262. self.appearanceData = datas.first
  263. self.reloadData()
  264. }
  265. @IBAction func lockDocumentButtonAction(_ sender: NSButton) {
  266. self.isLock = true
  267. }
  268. @IBAction func buttonItemViewDetail(_ sender: NSButton) {
  269. var filePath: String = ""
  270. var save: Bool = true
  271. var password: String = ""
  272. if self.cert!.isFormKeyChain == true {
  273. let folderPath = kDSignatureFolderPath
  274. if FileManager.default.fileExists(atPath: folderPath) == false {
  275. let success: ()? = try?FileManager.default.createDirectory(atPath: folderPath, withIntermediateDirectories: false)
  276. if success == nil {
  277. save = false
  278. }
  279. }
  280. filePath = kDSignatureFolderPath.appending("/KMDSignatures.p12") as String
  281. save = KMDSignatureManager.exportKeyChain(withP12FilePath: filePath as String?, signatureModel: self.cert, passWord: password as String?)
  282. self.cert?.filePath = filePath as String
  283. } else {
  284. password = self.passwordTextField.stringValue
  285. filePath = (self.cert?.filePath)!
  286. }
  287. if save {
  288. let certificate = CPDFSignatureCertificate(pkcs12Path: filePath, password: password)
  289. if certificate != nil {
  290. let signatureDetailVC = DSignatureDetailsViewController.init()
  291. signatureDetailVC.isDigitalFile = ((self.cert?.isFormKeyChain) == nil)
  292. signatureDetailVC.certificates = [certificate!]
  293. self.presentAsSheet(signatureDetailVC)
  294. return
  295. }
  296. }
  297. let alert = NSAlert.init()
  298. alert.messageText = NSLocalizedString("Failed to get certificate details!", comment: "")
  299. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  300. alert.runModal()
  301. }
  302. @IBAction func closeAction(_ sender: Any) {
  303. guard let callBack = self.actionBlock else {
  304. return
  305. }
  306. callBack(self, .cancel, self.appearanceData!, self.config, "", self.isLock)
  307. }
  308. @IBAction func cancelAction(_ sender: Any) {
  309. guard let callBack = self.actionBlock else {
  310. return
  311. }
  312. callBack(self, .previousStep, self.appearanceData!, self.config, "", self.isLock)
  313. }
  314. @IBAction func continueAction(_ sender: Any) {
  315. guard let callBack = self.actionBlock else {
  316. return
  317. }
  318. if self.cert?.isFormKeyChain == true {
  319. DSignatureApperanceManager.manager.saveDSignatureAppearanceData(watermark: self.appearanceData!)
  320. callBack(self, .confirm, self.appearanceData!, self.config, self.passwordTextField.stringValue, self.isLock)
  321. } else {
  322. let certificate = CPDFSignatureCertificate(pkcs12Path: self.cert?.filePath, password: self.passwordTextField.stringValue)
  323. if certificate == nil {
  324. self.errorLabel.isHidden = false
  325. self.passwordTextField.updateState(true)
  326. } else {
  327. DSignatureApperanceManager.manager.saveDSignatureAppearanceData(watermark: self.appearanceData!)
  328. callBack(self, .confirm, self.appearanceData!, self.config, self.passwordTextField.stringValue, self.isLock)
  329. }
  330. }
  331. }
  332. //MARK: NSTextFieldDelegate
  333. func controlTextDidChange(_ obj: Notification) {
  334. if obj.object == nil {
  335. return
  336. }
  337. let textField = obj.object as! NSTextField
  338. if self.passwordTextField.isEqual(textField) {
  339. self.errorLabel.isHidden = true
  340. }
  341. self.passwordTextField.updateState(false)
  342. }
  343. }