DSignatureCertifyDetailViewController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. if configItem.key == NAME_KEY && !_cert.name.isEmpty {
  129. let tConfigItem = CPDFSignatureConfigItem.init()
  130. tConfigItem.key = configItem.key
  131. tConfigItem.value = self.cert?.name
  132. contens.add(tConfigItem)
  133. } else if configItem.key == DN_KEY && !_cert.dn.isEmpty {
  134. let tConfigItem = CPDFSignatureConfigItem.init()
  135. tConfigItem.key = configItem.key
  136. tConfigItem.value = _cert.dn
  137. contens.add(tConfigItem)
  138. } else if configItem.key == REASON_KEY {
  139. let tConfigItem = CPDFSignatureConfigItem.init()
  140. tConfigItem.key = configItem.key
  141. tConfigItem.value = configItem.value
  142. contens.add(tConfigItem)
  143. } else if configItem.key == LOCATION_KEY {
  144. let tConfigItem = CPDFSignatureConfigItem.init()
  145. tConfigItem.key = configItem.key
  146. tConfigItem.value = configItem.value
  147. contens.add(tConfigItem)
  148. } else if configItem.key == NSLocalizedString(DATE_KEY, comment: "") {
  149. let tConfigItem = CPDFSignatureConfigItem.init()
  150. tConfigItem.key = configItem.key
  151. let dateFormatter = DateFormatter.init()
  152. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  153. tConfigItem.value = dateFormatter.string(from: NSDate() as Date)
  154. contens.add(tConfigItem)
  155. } else if configItem.key == VERSION_KEY && _cert.name.isEmpty == false {
  156. let tConfigItem = CPDFSignatureConfigItem.init()
  157. tConfigItem.key = configItem.key
  158. let infoDictionary = NSMutableDictionary(dictionary: Bundle.main.infoDictionary!)
  159. CFShow(infoDictionary)
  160. let app_Version = infoDictionary.object(forKey: "CFBundleShortVersionString")
  161. tConfigItem.value = app_Version as? String
  162. contens.add(tConfigItem)
  163. }
  164. }
  165. self.config.contents = contens as? [CPDFSignatureConfigItem]
  166. self.signatureWidget?.signAppearanceConfig(self.config)
  167. self.drawImageView.image = self.signatureWidget?.contentImage()
  168. }
  169. func updateTextFiedState () {
  170. self.passwordTextField.layer?.borderColor = NSColor.systemRed.cgColor;
  171. }
  172. func localizedLanguage () {
  173. self.contendView.wantsLayer = true
  174. self.contendView.layer?.backgroundColor = NSColor.clear.cgColor
  175. if self.cert?.name != nil {
  176. self.titleLabel.stringValue = NSLocalizedString("Sign as", comment: "") + " " + (self.cert?.name)!
  177. } else {
  178. self.titleLabel.stringValue = NSLocalizedString("Sign as", comment: "")
  179. }
  180. self.appearanceLabel.stringValue = NSLocalizedString("Appearance", comment: "") + ":"
  181. self.passwordTextField.placeholderString = NSLocalizedString("Enter the digital signature ID card PIN or password", comment: "")
  182. self.passwordLabel.stringValue = NSLocalizedString("Password", comment: "") + ":"
  183. self.editButton.toolTip = NSLocalizedString("Edit", comment: "")
  184. self.createButton.toolTip = NSLocalizedString("Create", comment: "")
  185. self.deleteButton.toolTip = NSLocalizedString("Delete", comment: "")
  186. self.continueButton.title = NSLocalizedString("Continue", comment: "");
  187. self.cancelButton.title = NSLocalizedString("Previous Step",comment: "");
  188. self.infoButton.title = NSLocalizedString("View Certificate Details",comment: "");
  189. self.lockDocButton.title = NSLocalizedString("Lock document after signing", comment: "");
  190. let txtColor = KMAppearance.Interactive.a0Color()
  191. self.infoButton.setTitleColor(txtColor)
  192. self.lockDocButton.isHidden = true
  193. self.createButton.wantsLayer = true
  194. self.editButton.wantsLayer = true
  195. self.deleteButton.wantsLayer = true
  196. self.createButton.layer?.borderWidth = 0.5
  197. self.editButton.layer?.borderWidth = 0.5
  198. self.deleteButton.layer?.borderWidth = 0.5
  199. self.createButton.layer?.cornerRadius = 5
  200. self.editButton.layer?.cornerRadius = 5
  201. self.deleteButton.layer?.cornerRadius = 5
  202. self.createButton.layer?.borderColor = NSColor(red: 165.0/255.0, green: 167.0/255.0, blue: 179.0/255.0, alpha: 1).cgColor
  203. self.editButton.layer?.borderColor = NSColor(red: 165.0/255.0, green: 167.0/255.0, blue: 179.0/255.0, alpha: 1).cgColor
  204. self.deleteButton.layer?.borderColor = NSColor(red: 165.0/255.0, green: 167.0/255.0, blue: 179.0/255.0, alpha: 1).cgColor
  205. let creatFileLayer = CALayer()
  206. creatFileLayer.frame = CGRectMake(0, 0, CGRectGetWidth(self.createButton.bounds), CGRectGetHeight(self.createButton.bounds));
  207. self.createButton.layer?.addSublayer(creatFileLayer)
  208. let editFileLayer = CALayer()
  209. editFileLayer.frame = CGRectMake(0, 0, CGRectGetWidth(self.editButton.bounds), CGRectGetHeight(self.editButton.bounds));
  210. self.editButton.layer?.addSublayer(editFileLayer)
  211. let deleteFileLayer = CALayer()
  212. deleteFileLayer.frame = CGRectMake(0, 0, CGRectGetWidth(self.deleteButton.bounds), CGRectGetHeight(self.deleteButton.bounds));
  213. self.deleteButton.layer?.addSublayer(deleteFileLayer)
  214. self.titleLabel.textColor = NSColor.labelColor
  215. self.appearanceLabel.textColor = NSColor.labelColor
  216. self.passwordLabel.textColor = NSColor.labelColor
  217. self.passwordTextField.textColor = NSColor.labelColor
  218. self.drawBox.borderColor = NSColor(red: 165.0/255.0, green: 167.0/255.0, blue: 179.0/255.0, alpha: 1)
  219. self.passwordTextField.wantsLayer = true
  220. self.passwordTextField.layer?.borderWidth = 1.0
  221. self.passwordTextField.layer?.cornerRadius = 5.0
  222. self.passwordTextField.placeholderString = NSLocalizedString("Enter the Digital ID PIN or Password...", comment: "")
  223. self.passwordTextField.updateState(false)
  224. self.errorLabel.stringValue = NSLocalizedString("Password is incorrect. Please re-enter the password.", comment: "")
  225. }
  226. //MARK: IBAction
  227. @IBAction func buttonItemClick_Appearence(_ sender: NSPopUpButton) {
  228. self.appearanceButton.title = sender.title;
  229. let datas = DSignatureApperanceManager.manager.loadDSignatureAppearanceDatas()
  230. self.appearanceData = datas.object(at: self.appearanceButton.indexOfSelectedItem) as? DSignatureAppearanceData
  231. if self.appearanceData?.isInputDSignatureText == false {
  232. self.appearanceData?.signatureConfig.text = _cert.name
  233. }
  234. self.reloadData()
  235. }
  236. @IBAction func buttonItemClick_Creat(_ sender: Any) {
  237. guard let callBack = self.actionBlock else {
  238. return
  239. }
  240. callBack(self, .create, self.appearanceData!, self.config, "", self.isLock)
  241. }
  242. @IBAction func buttonItemClick_Edit(_ sender: Any) {
  243. guard let callBack = self.actionBlock else {
  244. return
  245. }
  246. callBack(self, .edit, self.appearanceData!, self.config, "", self.isLock)
  247. }
  248. @IBAction func buttonItemClick_Delete(_ sender: Any) {
  249. _ = DSignatureApperanceManager.manager.removeDSignatureAppearanceData(watermark: self.appearanceData!)
  250. let datas = DSignatureApperanceManager.manager.loadDSignatureAppearanceDatas() as! [DSignatureAppearanceData]
  251. let titles = NSMutableArray()
  252. for i in 0..<datas.count {
  253. let data = datas[i]
  254. if data.appearanceName.length > 0 {
  255. titles.add(data.appearanceName as Any)
  256. }
  257. }
  258. self.appearanceButton.removeAllItems()
  259. self.appearanceButton.addItems(withTitles: titles as! [String])
  260. self.appearanceButton.selectItem(at: 0)
  261. self.appearanceData = datas.first
  262. self.reloadData()
  263. }
  264. @IBAction func lockDocumentButtonAction(_ sender: NSButton) {
  265. self.isLock = true
  266. }
  267. @IBAction func buttonItemViewDetail(_ sender: NSButton) {
  268. var filePath: String = ""
  269. var save: Bool = true
  270. var password: String = ""
  271. if self.cert!.isFormKeyChain == true {
  272. let folderPath = kDSignatureFolderPath
  273. if FileManager.default.fileExists(atPath: folderPath) == false {
  274. let success: ()? = try?FileManager.default.createDirectory(atPath: folderPath, withIntermediateDirectories: false)
  275. if success == nil {
  276. save = false
  277. }
  278. }
  279. filePath = kDSignatureFolderPath.appending("/KMDSignatures.p12") as String
  280. save = KMDSignatureManager.exportKeyChain(withP12FilePath: filePath as String?, signatureModel: self.cert, passWord: password as String?)
  281. self.cert?.filePath = filePath as String
  282. } else {
  283. password = self.passwordTextField.stringValue
  284. filePath = (self.cert?.filePath)!
  285. }
  286. if save {
  287. let certificate = CPDFSignatureCertificate(pkcs12Path: filePath, password: password)
  288. if certificate != nil {
  289. let signatureDetailVC = DSignatureDetailsViewController.init()
  290. signatureDetailVC.isDigitalFile = ((self.cert?.isFormKeyChain) == nil)
  291. signatureDetailVC.certificates = [certificate!]
  292. self.presentAsSheet(signatureDetailVC)
  293. return
  294. }
  295. }
  296. let alert = NSAlert.init()
  297. alert.messageText = NSLocalizedString("Failed to get certificate details!", comment: "")
  298. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  299. alert.runModal()
  300. }
  301. @IBAction func closeAction(_ sender: Any) {
  302. guard let callBack = self.actionBlock else {
  303. return
  304. }
  305. callBack(self, .cancel, self.appearanceData!, self.config, "", self.isLock)
  306. }
  307. @IBAction func cancelAction(_ sender: Any) {
  308. guard let callBack = self.actionBlock else {
  309. return
  310. }
  311. callBack(self, .previousStep, self.appearanceData!, self.config, "", self.isLock)
  312. }
  313. @IBAction func continueAction(_ sender: Any) {
  314. guard let callBack = self.actionBlock else {
  315. return
  316. }
  317. if self.cert?.isFormKeyChain == true {
  318. DSignatureApperanceManager.manager.saveDSignatureAppearanceData(watermark: self.appearanceData!)
  319. callBack(self, .confirm, self.appearanceData!, self.config, self.passwordTextField.stringValue, self.isLock)
  320. } else {
  321. let certificate = CPDFSignatureCertificate(pkcs12Path: self.cert?.filePath, password: self.passwordTextField.stringValue)
  322. if certificate == nil {
  323. self.errorLabel.isHidden = false
  324. self.passwordTextField.updateState(true)
  325. } else {
  326. DSignatureApperanceManager.manager.saveDSignatureAppearanceData(watermark: self.appearanceData!)
  327. callBack(self, .confirm, self.appearanceData!, self.config, self.passwordTextField.stringValue, self.isLock)
  328. }
  329. }
  330. }
  331. //MARK: NSTextFieldDelegate
  332. func controlTextDidChange(_ obj: Notification) {
  333. if obj.object == nil {
  334. return
  335. }
  336. let textField = obj.object as! NSTextField
  337. if self.passwordTextField.isEqual(textField) {
  338. self.errorLabel.isHidden = true
  339. }
  340. self.passwordTextField.updateState(false)
  341. }
  342. }