DSignatureSaveFolderViewController.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // DSignatureSaveFolderViewController.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2023/9/28.
  6. //
  7. import Cocoa
  8. class DSignatureSaveFolderViewController: NSViewController, NSTextFieldDelegate {
  9. @IBOutlet weak var titleLabel: NSTextField!
  10. @IBOutlet weak var subTitleLabel: NSTextField!
  11. @IBOutlet weak var filesLabel: NSTextField!
  12. @IBOutlet weak var passwordLabel: NSTextField!
  13. @IBOutlet weak var confirmPasswordLabel: NSTextField!
  14. @IBOutlet weak var filesTextField: NSTextField!
  15. @IBOutlet weak var passwordTextField: NSSecureTextField!
  16. @IBOutlet weak var confirmPasswordTextField: NSSecureTextField!
  17. @IBOutlet weak var errorLabel: NSTextField!
  18. @IBOutlet weak var openFileButton: NSButton!
  19. @IBOutlet weak var promptBox: NSBox!
  20. @IBOutlet var promptTextView: NSTextView!
  21. @IBOutlet weak var cancelButton: NSButton!
  22. @IBOutlet weak var continueButton: NSButton!
  23. // @property(nonatomic) IBOutlet NSLayoutConstraint *bottomOffset;
  24. var actionBlock: ((_ saveFolderVC: DSignatureSaveFolderViewController, _ actionType: DSignatureActionType, _ infoDic: NSDictionary)->Void)?
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27. // Do view setup here.
  28. self.titleLabel.textColor = NSColor.labelColor
  29. self.subTitleLabel.textColor = NSColor.labelColor
  30. self.filesLabel.textColor = NSColor.labelColor
  31. self.passwordLabel.textColor = NSColor.labelColor
  32. self.confirmPasswordLabel.textColor = NSColor.labelColor
  33. self.filesTextField.textColor = NSColor.labelColor
  34. self.passwordTextField.textColor = NSColor.labelColor
  35. self.confirmPasswordTextField.textColor = NSColor.labelColor
  36. self.promptTextView.textColor = NSColor.labelColor
  37. // self.bottomOffset.constant = 25.0;
  38. self.errorLabel.isHidden = true
  39. self.filesTextField.wantsLayer = true
  40. self.passwordTextField.wantsLayer = true
  41. self.confirmPasswordTextField.wantsLayer = true
  42. self.filesTextField.delegate = self;
  43. self.filesTextField.isEnabled = false
  44. self.filesTextField.layer?.borderWidth = 1.0
  45. self.passwordTextField.layer?.borderWidth = 1.0
  46. self.confirmPasswordTextField.layer?.borderWidth = 1.0;
  47. self.filesTextField.updateState(false)
  48. self.passwordTextField.layer?.borderColor = NSColor.clear.cgColor
  49. self.confirmPasswordTextField.layer?.borderColor = NSColor.clear.cgColor
  50. self.filesTextField.layer?.cornerRadius = 5.0
  51. self.passwordTextField.layer?.cornerRadius = 5.0
  52. self.confirmPasswordTextField.layer?.cornerRadius = 5.0;
  53. self.filesTextField.delegate = self
  54. self.confirmPasswordTextField.delegate = self
  55. self.localizedLanguage()
  56. if self.filesTextField.stringValue.isEmpty == false {
  57. self.continueButton.isEnabled = true
  58. } else {
  59. self.continueButton.isEnabled = false
  60. }
  61. }
  62. func localizedLanguage() {
  63. self.titleLabel.stringValue = NSLocalizedString("Save the Self-signed Digital ID to a File", comment: "");
  64. self.subTitleLabel.stringValue = NSLocalizedString("Your digital ID will be saved at the following location :", comment: "")
  65. self.filesLabel.stringValue = NSLocalizedString("File", comment: "");
  66. self.passwordLabel.stringValue = NSLocalizedString("Password", comment: "")
  67. self.confirmPasswordLabel.stringValue = NSLocalizedString("Confirm the Password", comment: "")
  68. self.promptTextView.string = String(format: "%@\n\n%@",NSLocalizedString("Add a password to protect the private key of the digital ID. You will need this password again to use the digital ID for signing.", comment: ""), NSLocalizedString("Save the digital ID file in a known location so that you can copy or backup it.", comment: ""))
  69. self.cancelButton.title = NSLocalizedString("Previous Step", comment: "")
  70. self.continueButton.title = NSLocalizedString("Continue", comment: "")
  71. self.openFileButton.title = String(format: "%@",NSLocalizedString("Browse", comment: ""))
  72. self.errorLabel.stringValue = NSLocalizedString("Passwords do not match", comment: "")
  73. }
  74. func getUniqueFilePath(_ filePath: String) -> (String) {
  75. var i = 0
  76. var isDirectory: ObjCBool = false
  77. var uniqueFilePath = filePath
  78. let filemanager = FileManager.default
  79. filemanager.fileExists(atPath: uniqueFilePath, isDirectory: &isDirectory)
  80. if isDirectory.boolValue {
  81. while filemanager.fileExists(atPath: uniqueFilePath) {
  82. i += 1
  83. uniqueFilePath = String(format: "%@(%d)", filePath, i)
  84. }
  85. } else {
  86. while filemanager.fileExists(atPath: uniqueFilePath) {
  87. i += 1
  88. let fileURL = URL(fileURLWithPath: filePath)
  89. let path = String(format: "%@(%d)", fileURL.deletingPathExtension().path, i)
  90. uniqueFilePath = String(format: "%@.%@", path, fileURL.pathExtension)
  91. }
  92. }
  93. return uniqueFilePath
  94. }
  95. //MARK: IBAction
  96. @IBAction func penFile(_ sender: Any) {
  97. let accessoryCtr = PDFCertExportAccessoryView.init()
  98. let openPanel = NSOpenPanel()
  99. openPanel.accessoryView = accessoryCtr.view
  100. openPanel.canChooseDirectories = true
  101. openPanel.canChooseFiles = false
  102. openPanel.beginSheetModal(for: self.view.window!) { result in
  103. if result == .OK {
  104. let outputULR = openPanel.url!
  105. var filePath = outputULR.path
  106. if accessoryCtr.formatMatrix.selectedRow == 0 {
  107. filePath = filePath + "/Untitled.p12"
  108. } else {
  109. filePath = filePath + "/Untitled.pfx"
  110. }
  111. self.filesTextField.stringValue = self.getUniqueFilePath(filePath)
  112. if self.filesTextField.stringValue.isEmpty {
  113. self.filesTextField.updateState(true)
  114. } else {
  115. self.filesTextField.updateState(false)
  116. }
  117. if self.filesTextField.stringValue.isEmpty == false {
  118. self.continueButton.isEnabled = true
  119. } else {
  120. self.continueButton.isEnabled = false
  121. }
  122. }
  123. }
  124. }
  125. @IBAction func closeAction(_ sender: Any) {
  126. guard let callBack = self.actionBlock else {
  127. return
  128. }
  129. callBack(self, .cancel, NSDictionary())
  130. }
  131. @IBAction func previousAction(_ sender: Any) {
  132. guard let callBack = self.actionBlock else {
  133. return
  134. }
  135. callBack(self, .previousStep, NSDictionary())
  136. }
  137. @IBAction func continueAction(_ sender: Any) {
  138. self.view.window?.makeFirstResponder(nil)
  139. if self.filesTextField.stringValue.count < 1 {
  140. return
  141. }
  142. if self.confirmPasswordTextField.stringValue == self.passwordTextField.stringValue {
  143. var isDir: ObjCBool = false
  144. let filePath = URL(fileURLWithPath: self.filesTextField.stringValue)
  145. let parentDirectory = filePath.deletingLastPathComponent().path
  146. let isExists = FileManager.default.fileExists(atPath: parentDirectory, isDirectory: &isDir)
  147. if isDir.boolValue == false || isExists == false {
  148. let alert = NSAlert.init()
  149. alert.messageText = NSLocalizedString("Output Folder can not be empty.", comment: "")
  150. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  151. alert.runModal()
  152. return
  153. }
  154. let dic = NSMutableDictionary()
  155. dic.setValue(self.filesTextField.stringValue, forKey: SAVEFILEPATH_KEY)
  156. if self.passwordTextField.stringValue.count > 0 {
  157. dic.setValue(self.passwordTextField.stringValue, forKey: PASSWORD_KEY)
  158. }
  159. self.errorLabel.isHidden = true
  160. // self.bottomOffset.constant = 25.0;
  161. guard let callBack = self.actionBlock else {
  162. return
  163. }
  164. callBack(self, .confirm, dic)
  165. } else {
  166. self.errorLabel.isHidden = false
  167. // self.bottomOffset.constant = self.errorLabel.frame.size.height + 10;
  168. }
  169. }
  170. //MARK: NSTextFieldDelegate
  171. func controlTextDidChange(_ obj: Notification) {
  172. if self.filesTextField.stringValue.isEmpty == false {
  173. self.continueButton.isEnabled = true
  174. } else {
  175. self.continueButton.isEnabled = false
  176. }
  177. let textField = obj.object as! NSTextField
  178. if self.filesTextField.isEqual(textField) {
  179. if self.filesTextField.stringValue.isEmpty == false {
  180. self.filesTextField.updateState(false)
  181. } else {
  182. self.filesTextField.updateState(true)
  183. }
  184. }
  185. self.errorLabel.isHidden = true
  186. // self.bottomOffset.constant = 25.0;
  187. }
  188. }
  189. extension NSTextField {
  190. func updateState(_ isError: Bool) {
  191. if isError {
  192. self.layer?.borderColor = NSColor.systemRed.cgColor
  193. } else {
  194. self.layer?.borderColor = NSColor.clear.cgColor
  195. }
  196. }
  197. }