DSignatureFromFileViewController.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // DSignatureFromFileViewController.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2023/9/28.
  6. //
  7. import Cocoa
  8. class DSignatureFromFileViewController: NSViewController, NSTextFieldDelegate {
  9. @IBOutlet weak var titleLabel: NSTextField!
  10. @IBOutlet weak var subTitleLabel: NSTextField!
  11. @IBOutlet weak var filesLabel: NSTextField!
  12. @IBOutlet weak var filesTextFiled: NSTextField!
  13. @IBOutlet weak var passwordLabel: NSTextField!
  14. @IBOutlet weak var passwordTextFiled: NSSecureTextField!
  15. @IBOutlet weak var browseButton: NSButton!
  16. @IBOutlet weak var errorLabel: NSTextField!
  17. @IBOutlet weak var promptBox: NSBox!
  18. @IBOutlet var promptTextView: NSTextView!
  19. @IBOutlet weak var continueButton: NSButton!
  20. @IBOutlet weak var previousStepButton: NSButton!
  21. @IBOutlet weak var creatButton: NSButton!
  22. var actionBlock: ((_ fromFileVC:DSignatureFromFileViewController, _ action:DSignatureActionType, _ cer:NSDictionary)->Void)?
  23. override func viewWillAppear() {
  24. super.viewWillAppear()
  25. }
  26. override func viewDidLoad() {
  27. super.viewDidLoad()
  28. // Do view setup here.
  29. self.filesTextFiled.delegate = self
  30. self.passwordTextFiled.delegate = self
  31. self.localizedLanguage()
  32. self.titleLabel.textColor = NSColor.labelColor
  33. self.subTitleLabel.textColor = NSColor.labelColor
  34. self.filesLabel.textColor = NSColor.labelColor
  35. self.passwordLabel.textColor = NSColor.labelColor
  36. self.filesTextFiled.textColor = NSColor.labelColor
  37. self.passwordTextFiled.textColor = NSColor.labelColor
  38. self.promptTextView.textColor = NSColor.labelColor
  39. self.filesTextFiled.wantsLayer = true
  40. self.passwordTextFiled.wantsLayer = true
  41. self.filesTextFiled.layer?.borderWidth = 1
  42. self.passwordTextFiled.layer?.borderWidth = 1
  43. self.filesTextFiled.layer?.cornerRadius = 5
  44. self.passwordTextFiled.layer?.cornerRadius = 5
  45. self.passwordTextFiled.updateState(false)
  46. self.errorLabel.isHidden = true
  47. self.filesTextFiled.updateState(false)
  48. }
  49. func localizedLanguage() {
  50. self.titleLabel.stringValue = NSLocalizedString("Find a Digital ID File", comment: "")
  51. self.subTitleLabel.stringValue = NSLocalizedString("Browse for a digital ID file. Digital ID files are password protected. You cannot access the digital ID if you don't know its password.", comment: "");
  52. self.filesLabel.stringValue = NSLocalizedString("File",comment: "");
  53. self.passwordLabel.stringValue = NSLocalizedString("Password",comment: "");
  54. self.filesTextFiled.placeholderString = NSLocalizedString("Select a file", comment: "");
  55. self.promptTextView.string = String(format: "%@\n\n%@",NSLocalizedString("Digital ID files generally have a P12 extension and contain the public key file (Certificate) and the associated private key file.", comment: ""),NSLocalizedString("To sign with a digital ID available as a file, follow the prompts to browse and select the file and type the password protecting the private key.",comment: ""))
  56. self.errorLabel.stringValue = NSLocalizedString("Password is incorrect. Please re-enter the password.", comment: "")
  57. self.browseButton.title = String(format:"%@",NSLocalizedString("Browse", comment: ""))
  58. self.previousStepButton.title = NSLocalizedString("Previous Step",comment: "");
  59. self.creatButton.title = NSLocalizedString("Configure New Digital ID", comment: "");
  60. self.continueButton.title = NSLocalizedString("Continue", comment: "");
  61. }
  62. func updateTextFiedState() {
  63. if (self.filesTextFiled.stringValue.isEmpty == false) {
  64. self.filesTextFiled.updateState(false)
  65. } else {
  66. self.filesTextFiled.updateState(true)
  67. }
  68. if self.filesTextFiled.stringValue.isEmpty == false {
  69. self.continueButton.isEnabled = true
  70. } else {
  71. self.continueButton.isEnabled = false
  72. }
  73. }
  74. //MARK: IBAction
  75. @IBAction func closeAction(_ sender: Any) {
  76. guard let callBack = self.actionBlock else {
  77. return
  78. }
  79. callBack(self, .cancel, NSDictionary())
  80. }
  81. @IBAction func fileBrowse(_ sender: Any) {
  82. let openPanel = NSOpenPanel()
  83. openPanel.allowedFileTypes = ["p12","pfx"]
  84. openPanel.beginSheetModal(for: self.view.window!) { result in
  85. if result == .OK {
  86. let fileURL = openPanel.urls.first
  87. self.filesTextFiled.stringValue = fileURL!.path
  88. }
  89. self.updateTextFiedState()
  90. }
  91. }
  92. @IBAction func configNewAction(_ sender: Any) {
  93. guard let callBack = self.actionBlock else {
  94. return
  95. }
  96. callBack(self, .createNewDsign, NSDictionary())
  97. }
  98. @IBAction func previousAction(_ sender: Any) {
  99. guard let callBack = self.actionBlock else {
  100. return
  101. }
  102. callBack(self, .previousStep, NSDictionary())
  103. }
  104. @IBAction func continueAction(_ sender: Any) {
  105. let filePath = self.filesTextFiled.stringValue
  106. if FileManager.default.fileExists(atPath: filePath) {
  107. let identity = KMDSignatureManager.privateKeyUsingSecItemImport(fromP12File: filePath, password: self.passwordTextFiled.stringValue)
  108. if identity != nil {
  109. let dic = NSMutableDictionary.init()
  110. dic.setValue(filePath, forKey: SAVEFILEPATH_KEY)
  111. dic.setValue(self.passwordTextFiled.stringValue, forKey: PASSWORD_KEY)
  112. let moveSuc = KMDSignatureManager.default().moveP12DigitalFile(withFilePath: filePath, password: self.passwordTextFiled.stringValue)
  113. if moveSuc {
  114. guard let callBack = self.actionBlock else {
  115. return
  116. }
  117. callBack(self, .confirm, dic)
  118. } else {
  119. let alert = NSAlert.init()
  120. alert.messageText = NSLocalizedString("Failed to import the P12 file!", comment: "")
  121. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  122. alert.runModal()
  123. }
  124. } else {
  125. self.errorLabel.isHidden = false
  126. }
  127. }
  128. }
  129. //MARK: NSTextFieldDelegate
  130. func controlTextDidChange(_ obj: Notification) {
  131. self.updateTextFiedState()
  132. let textField = obj.object;
  133. if self.passwordTextFiled.isEqual(textField) {
  134. self.filesTextFiled.updateState(false)
  135. }
  136. self.errorLabel.isHidden = true
  137. }
  138. }